#! /bin/sh # # Qmail-init-script # # chkconfig: 2345 90 10 # description: Qmail, the low-impact mail server. PATH="/var/qmail/bin:/usr/local/sbin:/usr/local/bin:$PATH" export PATH mailhost=0 ## bind to all addresses, including localhost and ether tcps=$( which tcpserver ) qmaild=$(id -u qmaild ) nofiles=$(id -g qmaild ) export HI_Q_CONF=/var/qmail/control/filters.conf export HI_Q_AUCONF=/var/qmail/control/aufilters.conf smtpconf=/etc/stunnel/smtp.conf pop3conf=/etc/stunnel/pop3.conf PEM=/etc/stunnel/stunnel.pem CDB=/etc/tcpserver/smtp.cdb ## Plain smtp never asks for a password, ## and will not relay except from inside the firewall. ## in accordance with /etc/tcpserver/smtp.rules. ## SMTPS will relay for anybody who has a password. ## The firewall ensures that plain pop3 is ## accessible only on local-area-network, inside the firewall. # The following can be overridden by command-line # environment setting, e.g. pop3s=no ./qmail start : ${CKPW:=/bin/qmaild/checkpassword} : ${PIDO:=/var/qmail/bin/pido} banner="Starting Qmail MTA:" : ${mailhost:=0} # Some people might use mailhost=$(hostname -f ) # but mailhost=0 is almost always better. # In particular it is much better as a /default/ value. proc_running(){ proc=$1 if test -f /var/run/$proc.pid ; then pid=$( cat /var/run/$proc.pid ) if test -n "pid" && 2>/dev/null kill -0 $pid ; then return 0 fi fi return 1 } ## Note that this will EXIT if the proc can't start ## within ten seconds ## cutting off any chance of continuing toward partial success. proc_ok(){ proc=$1 ii=0 while test $ii -lt 10 ; do if proc_running $proc ; then echo " ok." return else echo -n . sleep 1 fi : $((ii += 1)) done echo "failed!" exit 1 } stop_proc() { proc="$1" sign="$2" proc_running $proc || return 0 pid=$( cat /var/run/$proc.pid ) echo -n "Stopping $proc ($pid): " kill -15 $sign$pid ii=0 while test $ii -lt 10 ; do if proc_running $proc ; then echo -n . sleep 1 else echo "stopped." return fi : $((ii += 1)) done echo "Ooops, still running ($pid)." } stop_all() { flag="$1" ## first, the fancy server daemons: test -z "${smtp%%[Nn0]*}" || stop_proc qmail-smtp "" test -z "${smtps%%[Nn0]*}" || stop_proc qmail-smtps "" test -z "${pop3%%[Nn0]*}" || stop_proc qmail-pop3 "" test -z "${pop3s%%[Nn0]*}" || stop_proc qmail-pop3s "" test -z "${send%%[Nn0]*}" || stop_proc qmail-send "$flag" } verb="$1"; if test "$#" -gt 0 ; then shift ; fi for thing in $* ; do case $thing in send) send=yes ;; smtp) smtp=yes ;; smtps) smtps=yes ;; pop3) pop3=yes ;; pop3s) pop3s=yes ;; *) 1>&2 echo "Extraneous verbiage '$thing'" ; exit 1 ;; esac done if test -n "${smtp%%[Nn0]*}${smtps%%[Nn0]*}${pop3%%[Nn0]*}${pop3s%%[Nn0]*}${send%%[Nn0]*}" ; then : ${default:=no} else : ${default:=yes} fi insecure_default=no if test "_$verb" != "_start" ; then insecure_default=yes fi : ${send:=$default} : ${smtps:=$default} : ${pop3s:=$default} : ${pop3:=$insecure_default} : ${smtp:=$default} flag="" case "$verb" in restart) $0 stop $@ $0 start $@ ;; start) if test -n "${send%%[Nn0]*}" ; then test -n "$banner" && echo $banner ; banner="" proc=qmail-send if proc_running $proc ; then 1>&2 echo " Oops, $proc is already running ($pid)." else echo -n " qmail-send: " /bin/rm -f /var/run/$proc.pid qmail-start ./Maildir/ splogger qmail >/dev/null 2>&1 & echo $! > /var/run/$proc.pid proc_ok $proc fi fi if test -n "${smtp%%[Nn0]*}" ; then test -n "$banner" && echo $banner ; banner="" proc=qmail-smtp if proc_running $proc ; then 1>&2 echo " Oops, $proc is already running ($pid)." else echo -n " SMTP-server " /bin/rm -f /var/run/$proc.pid QMAILQUEUE="/var/qmail/bin/hi-q" \ $PIDO /var/run/$proc.pid \ $tcps -R -x$CDB -u$qmaild -g$nofiles $mailhost smtp \ qmail-smtpd 2>&1 \ | splogger smtp & proc_ok $proc fi fi if test -n "${smtps%%[Nn0]*}" ; then test -n "$banner" && echo $banner ; banner="" proc=qmail-smtps if proc_running $proc ; then 1>&2 echo " Oops, $proc is already running ($pid)." else echo -n " SMTPS-server " /bin/rm -f /var/run/$proc.pid QMAILQUEUE="/var/qmail/bin/hi-q" \ $PIDO /var/run/$proc.pid \ $tcps -R -u$qmaild -g$nofiles $mailhost smtps \ stunnel4 $smtpconf 2>&1 \ | splogger smtps & proc_ok $proc fi fi if test -n "${pop3%%[Nn0]*}" ; then test -n "$banner" && echo $banner ; banner="" proc=qmail-pop3 if proc_running $proc ; then 1>&2 echo " Oops, insecure $proc is already running ($pid)." else echo -n " insecure POP3-server " /bin/rm -f /var/run/$proc.pid $PIDO /var/run/$proc.pid \ $tcps -v -p -R $mailhost pop3 qmail-popup $mailhost \ $CKPW qmail-pop3d Maildir 2>&1 \ | splogger pop3 & proc_ok $proc fi fi if test -n "${pop3s%%[Nn0]*}" ; then test -n "$banner" && echo $banner ; banner="" proc=qmail-pop3s if proc_running $proc ; then 1>&2 echo " Oops, $proc is already running ($pid)." else echo -n " POP3S-server " /bin/rm -f /var/run/$proc.pid $PIDO /var/run/$proc.pid \ $tcps -v -p -R $mailhost pop3s \ stunnel4 $pop3conf 2>&1 | \ splogger pop3s & proc_ok $proc fi fi ;; zap) stop_all "-" ;; stop) stop_all "" ;; ## cause qmail-send to reread a couple of control files: reload) echo -n "Reread control/locals, control/rcpthosts, and control/virtualdomains: " if kill -1 $( cat /var/run/qmail-send.pid ) ; then echo " ok." else echo " failed." fi ;; status) for thing in send smtp smtps pop3 pop3s ; do xthing="qmail-$thing" printf "%-12s : " "$xthing" pid=$( cat /var/run/$xthing.pid 2>/dev/null ) : ${pid:=xxx} if kill -0 "$pid" 2>/dev/null ; then echo " up. $pid" else echo " down. $pid" fi done for file in $pop3conf $smtpconf $HI_Q_CONF $HI_Q_AUCONF \ $CDB $PEM ; do if ! test -r "$file" ; then echo "Beware: file '$file' is missing." fi done /var/qmail/bin/qmail-tls-check_certs -stunnel ;; *) echo "Usage: $0 {start|stop|reload|zap|restart|status}" exit 1 ;; esac