#!/bin/sh #---------------------------------------------------------------------------- # /etc/init.d/postfix - Postfix init script for eisfair-1 and eisfair-2 # # Copyright (c) 2004 Jens Vehlhaber # # Creation: 2004-04-23 jv # Last Update: $Id$ # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. #---------------------------------------------------------------------------- # include configuration . /etc/config.d/vmail postfix_root='/var/spool/postfix' #postfix_chroot="$postfix_root" # chroot not correct work with dns resolv if not copy libnss_* files # default value nreturn="0" ### ------------------------------------------------------------------------- ### start/stop and failure message color output ### ------------------------------------------------------------------------- COLOR_RED='\033[1;31m' COLOR_NRM='\033[0;39m' log_end_msg () { echo -n " " if [ "$1" = "0" ] then echo -e "\033[300C\033[$[7]D [ OK ]" elif [ "$1" = "9" ] then echo -e "\033[300C\033[$[9]D disabled" else echo -e "\033[300C\033[$[7]D [${COLOR_RED}fail${COLOR_NRM}]" nreturn=1 fi } ### ------------------------------------------------------------------------- ### check if program running ### ------------------------------------------------------------------------- get_program_pid() { if [ -n "`ps -ef | grep -v grep | grep "$1 " 2>/dev/null`" ] then echo "0" return 0 else return 1 fi } get_program_portnr() { if [ -n "`netstat -tnl | grep -v grep | grep ":$1 " 2>/dev/null`" ] then echo "0" return 0 else return 1 fi } get_program_sock() { if [ -n "`netstat -xnl | grep -v grep | grep ${1} 2>/dev/null`" ] then echo "0" return 0 else return 1 fi } ### ------------------------------------------------------------------------- ### start if not running ### ------------------------------------------------------------------------- start_and_wait() { local start_type="$1" local start_prg_file="$2" local start_param="$3" if ! get_program_pid $start_prg_file >/dev/null then eval "${start_type} ${start_prg_file} ${start_param}" for count in 1 2 3 do if get_program_pid $start_prg_file >/dev/null then break fi echo -n "." sleep 1 done fi } ### ------------------------------------------------------------------------- ### start smtpd service ### ------------------------------------------------------------------------- start_smtpd() { local count=0 if ! get_program_portnr 25 >/dev/null then /usr/sbin/postfix start >/dev/null 2>&1 for count in 1 2 3 4 do if get_program_portnr 25 >/dev/null then break fi echo -n "." sleep 1 done fi if [ $count -eq 4 ] then log_end_msg 1 else log_end_msg 0 fi } ### ------------------------------------------------------------------------- ### stop smtpd service ### ------------------------------------------------------------------------- stop_smtpd() { local count=0 if get_program_portnr 25 >/dev/null then /usr/sbin/postfix stop >/dev/null 2>&1 for count in 1 2 3 4 do if ! get_program_portnr 25 >/dev/null then break fi echo -n "." sleep 1 done if [ $count -eq 4 ] then killall -wq master fi fi } ### ------------------------------------------------------------------------- ### stop an wait ### ------------------------------------------------------------------------- stop_and_wait() { local stop_prg_type="$1" local stop_prg_file="$2" local stop_pid_file="$3" if get_program_pid $stop_prg_file >/dev/null then for count in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 do eval "${stop_prg_type} ${stop_prg_file}" >/dev/null 2>&1 if ! get_program_pid $stop_prg_file >/dev/null then break fi if [ ${count} -gt 5 ] then killall -KILL ${stop_prg_file} >/dev/null 2>&1 fi echo -n "." sleep 1 done fi if [ -f ${stop_pid_file} ] then rm -f ${stop_pid_file} fi } ### ------------------------------------------------------------------------- ### update chroot ### ------------------------------------------------------------------------- # Link source file to destination directory if possible. If the link is a # symbolic link, make a copy of the link in the destination directory, # otherwise copy the file. copy() { file=`basename ${1}` cp -dpf ${1} ${2}/${file} } update_chroot() { # setup the empty chroot directory structure for i in /etc /lib /usr/lib/zoneinfo /usr/share/zoneinfo /var/run do mkdir -p ${postfix_root}${i} done rm -f ${postfix_root}/etc/* rm -f ${postfix_root}/lib/* if [ -n "$postfix_chroot" ] then chmod 0777 ${postfix_root}/var/run for i in /etc/localtime /usr/lib/zoneinfo/localtime \ /usr/share/zoneinfo/localtime \ /etc/host.conf /etc/resolv.conf /etc/nsswitch.conf \ /etc/hosts /etc/services do [ -e ${i} ] && copy ${i} `dirname ${postfix_root}${i}` done # determine glibc version LIBCVER=`ls -l /lib/libc.so.6* | sed "s/.*libc-\(.*\).so$/\1/g"` # copy the relevant parts of glibc into the chroot for i in compat dns files hesoid ldap nis nisplus winbind wins do [ -e /lib/libnss_${i}-${LIBCVER}.so ] && copy /lib/libnss_${i}-${LIBCVER}.so ${postfix_root}/lib/ [ -e /lib/libnss_${i}.so ] && copy /lib/libnss_${i}.so ${postfix_root}/lib/ [ -e /lib/libnss_${i}.so.2 ] && copy /lib/libnss_${i}.so.2 ${postfix_root}/lib/ done [ -e /lib/libnss_db.so.2.0.0 ] && copy /lib/libnss_db.so.2.0.0 ${postfix_root}/lib/ [ -e /lib/libresolv-${LIBCVER}.so ] && copy /lib/libresolv-${LIBCVER}.so ${postfix_root}/lib/ [ -e /lib/libresolv-${LIBCVER}.so.2 ] && copy /lib/libresolv-${LIBCVER}.so.2 ${postfix_root}/lib/ ldconfig -n ${postfix_root}/lib # not convinced this is necessary mkdir -p ${postfix_root}/var/lib/mysql chown mysql ${postfix_root}/var/lib/mysql tmp=`mount | grep '/var/spool/postfix/'` if [ -z "$tmp" ] then mount -o bind /var/lib/mysql ${postfix_root}/var/lib/mysql fi fi } ### ------------------------------------------------------------------------- ### main ### ------------------------------------------------------------------------- case "$1" in start) update_chroot echo -n " * Starting Postfix ..." start_and_wait /usr/local/postfix/clamsmtpd "-p /var/run/clamsmtpd.pid" if [ "$POSTFIX_DSPAM" = 'yes' ] then start_and_wait "startproc -s -u vmail" /usr/local/dspam/dspam "--daemon" 2>/dev/null & fi if [ "$POSTFIX_GLD" = "yes" ] then start_and_wait startproc /usr/local/policyd/policyd "-c /etc/postfix/policyd.conf" fi if [ "$POSTFIX_DRACD" = 'yes' ] then start_and_wait startproc /usr/local/postfix/dracd "-i -e ${POSTFIX_DRACD_RELAYTIME} /etc/postfix/dracd.db" fi start_smtpd ;; stop) echo -n " * Stopping Postfix ..." stop_smtpd stop_and_wait killall /usr/local/postfix/clamsmtpd /var/run/clamsmtpd.pid stop_and_wait killproc /usr/local/dspam/dspam /var/run/dspam.pid stop_and_wait killproc /usr/local/policyd/policyd /var/run/policyd.pid stop_and_wait killproc /usr/local/postfix/dracd log_end_msg 0 ;; reload) echo " * Reloading Postfix ..." /usr/sbin/postfix reload ;; abort) echo " * Aborting Postfix ..." /usr/sbin/postfix abort ;; flush) echo " * Flushing Postfix ..." /usr/sbin/postfix flush ;; check) echo " * Checking Postfix ..." /usr/sbin/postfix check ;; restart) $0 stop if [ "$START_POSTFIX" = 'yes' ] then $0 start fi ;; status) echo -n " SMTP " log_end_msg `get_program_portnr 25` echo -n " SMTP-TLS " if [ ! "$POSTFIX_SMTP_TLS" = 'yes' ] then log_end_msg 9 else log_end_msg `get_program_portnr 465` fi if [ "$POSTFIX_GLD" = 'yes' ] then echo -n " Greylisting " log_end_msg `get_program_portnr 10031` fi echo -n " ClamSMTP " log_end_msg `get_program_portnr 10025` echo -n " DSPAM " if [ ! "$POSTFIX_DSPAM" = 'yes' ] then log_end_msg 9 else log_end_msg `get_program_sock /var/run/dspam.sock` fi echo -n " DRACD " if [ ! "$POSTFIX_DRACD" = 'yes' ] then log_end_msg 9 else log_end_msg `get_program_sock /var/run/dracd.sock` fi ;; *) echo "Usage: $0 {start|stop|reload|abort|flush|check|restart}" exit 1 ;; esac ### ------------------------------------------------------------------------- exit $nreturn