Advertisement
Guest User

Untitled

a guest
Feb 15th, 2016
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh -e
  2.  
  3. # Start or stop Postfix
  4. #
  5. # LaMont Jones <[email protected]>
  6. # based on sendmail's init.d script
  7.  
  8. ### BEGIN INIT INFO
  9. # Provides:          postfix mail-transport-agent
  10. # Required-Start:    $local_fs $remote_fs $syslog $named $network $time
  11. # Required-Stop:     $local_fs $remote_fs $syslog $named $network
  12. # Should-Start:      postgresql mysql clamav-daemon postgrey spamassassin saslauthd dovecot
  13. # Should-Stop:       postgresql mysql clamav-daemon postgrey spamassassin saslauthd dovecot
  14. # Default-Start:     2 3 4 5
  15. # Default-Stop:      0 1 6
  16. # Short-Description: Postfix Mail Transport Agent
  17. # Description:       postfix is a Mail Transport agent
  18. ### END INIT INFO
  19.  
  20. CONFDIR=/etc/postfix2525
  21. PATH=/bin:/usr/bin:/sbin:/usr/sbin
  22. DAEMON=/usr/sbin/postfix
  23. DAEMON_OPTIONS="-c /etc/postfix2525"
  24. NAME=Postfix
  25. TZ=
  26. unset TZ
  27.  
  28. # Defaults - don't touch, edit /etc/default/postfix
  29. SYNC_CHROOT="y"
  30.  
  31. test -f /etc/default/postfix && . /etc/default/postfix
  32.  
  33. test -x $DAEMON && test -f /etc/postfix2525/main.cf || exit 0
  34.  
  35. . /lib/lsb/init-functions
  36. #DISTRO=$(lsb_release -is 2>/dev/null || echo Debian)
  37.  
  38. enabled_instances() {
  39.     postmulti -l -a | awk '($3=="y") { print $1}'
  40. }
  41.  
  42. running() {
  43.     INSTANCE="$1"
  44.     if [ "X$INSTANCE" = X ]; then
  45.         POSTCONF="postconf"
  46.     else
  47.         POSTCONF="postmulti -i $INSTANCE -x postconf"
  48.     fi
  49.  
  50.     queue=$($POSTCONF -c $CONFDIR -h queue_directory 2>/dev/null || echo /var/spool/postfix2525)
  51.     if [ -f ${queue}/pid/master.pid ]; then
  52.     pid=$(sed 's/ //g' ${queue}/pid/master.pid)
  53.     # what directory does the executable live in.  stupid prelink systems.
  54.     dir=$(ls -l /proc/$pid/exe 2>/dev/null | sed 's/.* -> //; s/\/[^\/]*$//')
  55.     if [ "X$dir" = "X/usr/lib/postfix" ]; then
  56.         echo y
  57.     fi
  58.     fi
  59. }
  60.  
  61. configure_instance() {
  62.     INSTANCE="$1"
  63.     if [ "X$INSTANCE" = X ]; then
  64.         POSTCONF="postconf"
  65.     else
  66.         POSTCONF="postmulti -i $INSTANCE -x postconf"
  67.     fi
  68.  
  69.  
  70.     # if you set myorigin to 'ubuntu.com' or 'debian.org', it's wrong, and annoys the admins of
  71.     # those domains.  See also sender_canonical_maps.
  72.  
  73.     MYORIGIN=$($POSTCONF -h myorigin | tr 'A-Z' 'a-z')
  74.     if [ "X${MYORIGIN#/}" != "X${MYORIGIN}" ]; then
  75.     MYORIGIN=$(tr 'A-Z' 'a-z' < $MYORIGIN)
  76.     fi
  77.     if [ "X$MYORIGIN" = Xubuntu.com ] || [ "X$MYORIGIN" = Xdebian.org ]; then
  78.     log_failure_msg "Invalid \$myorigin ($MYORIGIN), refusing to start"
  79.     log_end_msg 1
  80.     exit 1
  81.     fi
  82.  
  83.     config_dir=$($POSTCONF -h config_directory)
  84.     # see if anything is running chrooted.
  85.     NEED_CHROOT=$(awk '/^[0-9a-z]/ && ($5 ~ "[-yY]") { print "y"; exit}' ${config_dir}/master.cf)
  86.  
  87.     if [ -n "$NEED_CHROOT" ] && [ -n "$SYNC_CHROOT" ]; then
  88.     # Make sure that the chroot environment is set up correctly.
  89.     oldumask=$(umask)
  90.     umask 022
  91.     queue_dir=$($POSTCONF -h queue_directory)
  92.     cd "$queue_dir"
  93.  
  94.     # copy the CA path if specified
  95.     ca_path=$($POSTCONF -h smtp_tls_CApath)
  96.     case "$ca_path" in
  97.         '') :;; # no ca_path
  98.         $queue_dir/*) :;;  # skip stuff already in chroot, (and to make vim syntax happy: */)
  99.         *)
  100.         if test -d "$ca_path"; then
  101.             dest_dir="$queue_dir/${ca_path#/}"
  102.             # strip any/all trailing /
  103.             while [ "${dest_dir%/}" != "${dest_dir}" ]; do
  104.             dest_dir="${dest_dir%/}"
  105.             done
  106.             new=0
  107.             if test -d "$dest_dir"; then
  108.             # write to a new directory ...
  109.             dest_dir="${dest_dir}.NEW"
  110.             new=1
  111.             fi
  112.             mkdir --parent ${dest_dir}
  113.             # handle files in subdirectories
  114.             (cd "$ca_path" && find . -name '*.pem' -print0 | cpio -0pdL --quiet "$dest_dir") 2>/dev/null ||
  115.                 (log_failure_msg failure copying certificates; exit 1)
  116.             c_rehash "$dest_dir" >/dev/null 2>&1
  117.             if [ "$new" = 1 ]; then
  118.             # and replace the old directory
  119.             rm -rf "${dest_dir%.NEW}"
  120.             mv "$dest_dir" "${dest_dir%.NEW}"
  121.             fi
  122.         fi
  123.         ;;
  124.     esac
  125.  
  126.     # if there is a CA file, copy it
  127.     ca_file=$($POSTCONF -h smtp_tls_CAfile)
  128.     case "$ca_file" in
  129.         $queue_dir/*) :;;  # skip stuff already in chroot
  130.         '') # no ca_file
  131.         # or copy the bundle to preserve functionality
  132.         ca_bundle=/etc/ssl/certs/ca-certificates.crt
  133.         if [ -f $ca_bundle ]; then
  134.             mkdir --parent "$queue_dir/${ca_bundle%/*}"
  135.             cp -L "$ca_bundle" "$queue_dir/${ca_bundle%/*}"
  136.         fi
  137.         ;;
  138.         *)
  139.         if test -f "$ca_file"; then
  140.             dest_dir="$queue_dir/${ca_path#/}"
  141.             mkdir --parent "$dest_dir"
  142.             cp -L "$ca_file" "$dest_dir"
  143.         fi
  144.         ;;
  145.     esac
  146.  
  147.     # if we're using unix:passwd.byname, then we need to add etc/passwd.
  148.     local_maps=$($POSTCONF -h local_recipient_maps)
  149.     if [ "X$local_maps" != "X${local_maps#*unix:passwd.byname}" ]; then
  150.         if [ "X$local_maps" = "X${local_maps#*proxy:unix:passwd.byname}" ]; then
  151.         sed 's/^\([^:]*\):[^:]*/\1:x/' /etc/passwd > etc/passwd
  152.         chmod a+r etc/passwd
  153.         fi
  154.     fi
  155.  
  156.     FILES="etc/localtime etc/services etc/resolv.conf etc/hosts \
  157.         etc/host.conf etc/nsswitch.conf etc/nss_mdns.config"
  158.     for file in $FILES; do
  159.         [ -d ${file%/*} ] || mkdir -p ${file%/*}
  160.         if [ -f /${file} ]; then rm -f ${file} && cp /${file} ${file}; fi
  161.         if [ -f  ${file} ]; then chmod a+rX ${file}; fi
  162.     done
  163.     # ldaps needs this. debian bug 572841
  164.     (echo /dev/random; echo /dev/urandom) | cpio -pdL --quiet . 2>/dev/null || true
  165.     rm -f usr/lib/zoneinfo/localtime
  166.     mkdir -p usr/lib/zoneinfo
  167.     ln -sf /etc/localtime usr/lib/zoneinfo/localtime
  168.  
  169.     LIBLIST=$(for name in gcc_s nss resolv; do
  170.         for f in /lib/*/lib${name}*.so* /lib/lib${name}*.so*; do
  171.            if [ -f "$f" ]; then  echo ${f#/}; fi;
  172.         done;
  173.     done)
  174.  
  175.     if [ -n "$LIBLIST" ]; then
  176.         for f in $LIBLIST; do
  177.         rm -f "$f"
  178.         done
  179.         tar cf - -C / $LIBLIST 2>/dev/null |tar xf -
  180.     fi
  181.     umask $oldumask
  182.     fi
  183. }
  184.  
  185. case "$1" in
  186.     start)
  187.     log_daemon_msg "Starting Postfix Mail Transport Agent" postfix
  188.     RET=0
  189.     # for all instances that are not already running, handle chroot setup if needed, and start
  190.     for INSTANCE in $(enabled_instances); do
  191.         RUNNING=$(running $INSTANCE)
  192.         if [ "X$RUNNING" = X ]; then
  193.         configure_instance $INSTANCE
  194.         CMD="/usr/sbin/postmulti -- -i $INSTANCE -x ${DAEMON}"
  195.         if ! start-stop-daemon --start --exec $CMD -- ${DAEMON_OPTIONS} quiet-quick-start; then
  196.             RET=1
  197.         fi
  198.         fi
  199.     done
  200.     log_end_msg $RET
  201.     ;;
  202.  
  203.     stop)
  204.     log_daemon_msg "Stopping Postfix Mail Transport Agent" postfix
  205.     RET=0
  206.     # for all instances that are not already running, handle chroot setup if needed, and start
  207.     for INSTANCE in $(enabled_instances); do
  208.         RUNNING=$(running $INSTANCE)
  209.         if [ "X$RUNNING" != X ]; then
  210.         CMD="/usr/sbin/postmulti -i $INSTANCE -x ${DAEMON}"
  211.         if ! ${CMD} ${DAEMON_OPTIONS} quiet-stop; then
  212.             RET=1
  213.         fi
  214.         fi
  215.     done
  216.     log_end_msg $RET
  217.     ;;
  218.  
  219.     restart)
  220.         $0 stop
  221.         $0 start
  222.     ;;
  223.  
  224.     force-reload|reload)
  225.     log_action_begin_msg "Reloading Postfix configuration"
  226.     if ${DAEMON} quiet-reload; then
  227.         log_action_end_msg 0
  228.     else
  229.         log_action_end_msg 1
  230.     fi
  231.     ;;
  232.  
  233.     status)
  234.     ALL=1
  235.     ANY=0
  236.     # for all instances that are not already running, handle chroot setup if needed, and start
  237.     for INSTANCE in $(enabled_instances); do
  238.         RUNNING=$(running $INSTANCE)
  239.         if [ "X$RUNNING" != X ]; then
  240.             ANY=1
  241.         else
  242.             ALL=0
  243.         fi
  244.     done
  245.     # handle the case when postmulti returns *no* configured instances
  246.     if [ $ANY = 0 ]; then
  247.        ALL=0
  248.     fi
  249.     if [ $ALL = 1 ]; then
  250.        log_success_msg "postfix is running"
  251.        exit 0
  252.     elif [ $ANY = 1 ]; then
  253.        log_success_msg "some postfix instances are running"
  254.        exit 0
  255.     else
  256.        log_success_msg "postfix is not running"
  257.        exit 3
  258.     fi
  259.     ;;
  260.  
  261.     flush|check|abort)
  262.     ${DAEMON} $1
  263.     ;;
  264.  
  265.     *)
  266.     log_action_msg "Usage: /etc/init.d/postfix {start|stop|restart|reload|flush|check|abort|force-reload|status}"
  267.     exit 1
  268.     ;;
  269. esac
  270.  
  271. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement