summaryrefslogtreecommitdiff
path: root/tools/qmail
blob: f765a8f9daeeae440d1d712bbd83300380110f01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#! /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