New configs for Xenial (16.04)
[sysconfig/postfix.git] / postfix-script
1 #!/bin/sh
2
3 #++
4 # NAME
5 #       postfix-script 1
6 # SUMMARY
7 #       execute Postfix administrative commands
8 # SYNOPSIS
9 #       \fBpostfix-script\fR \fIcommand\fR
10 # DESCRIPTION
11 #       The \fBpostfix-script\fR script executes Postfix administrative
12 #       commands in an environment that is set up by the \fBpostfix\fR(1)
13 #       command.
14 # SEE ALSO
15 #       master(8) Postfix master program
16 #       postfix(1) Postfix administrative interface
17 # LICENSE
18 # .ad
19 # .fi
20 #       The Secure Mailer license must be distributed with this software.
21 # AUTHOR(S)
22 #       Wietse Venema
23 #       IBM T.J. Watson Research
24 #       P.O. Box 704
25 #       Yorktown Heights, NY 10598, USA
26 #--
27
28 # Avoid POSIX death due to SIGHUP when some parent process exits.
29
30 trap '' 1
31
32 case $daemon_directory in
33 "") echo This script must be run by the postfix command. 1>&2
34     echo Do not run directly. 1>&2
35     exit 1
36 esac
37
38 LOGGER="$command_directory/postlog -t $MAIL_LOGTAG/postfix-script"
39 INFO="$LOGGER -p info"
40 WARN="$LOGGER -p warn"
41 ERROR="$LOGGER -p error"
42 FATAL="$LOGGER -p fatal"
43 PANIC="$LOGGER -p panic"
44
45 if [ "X${1#quiet-}" != "X${1}" ]; then
46     INFO=:
47     x=${1#quiet-}
48     shift
49     set -- $x "$@"
50 fi
51
52 umask 022
53 SHELL=/bin/sh
54
55 #
56 # Can't do much without these in place.
57 #
58 cd $command_directory || {
59         $FATAL no Postfix command directory $command_directory!
60         exit 1
61 }
62 cd $daemon_directory || {
63         $FATAL no Postfix daemon directory $daemon_directory!
64         exit 1
65 }
66 test -f master || {
67         $FATAL no Postfix master program $daemon_directory/master!
68         exit 1
69 }
70 cd $config_directory || {
71         $FATAL no Postfix configuration directory $config_directory!
72         exit 1
73 }
74 case $shlib_directory in
75 no) ;;
76  *) cd $shlib_directory || {
77         $FATAL no Postfix shared-library directory $shlib_directory!
78         exit 1
79     }
80 esac
81 cd $meta_directory || {
82         $FATAL no Postfix meta directory $meta_directory!
83         exit 1
84 }
85 cd $queue_directory || {
86         $FATAL no Postfix queue directory $queue_directory!
87         exit 1
88 }
89 def_config_directory=`$command_directory/postconf -dh config_directory` || {
90         $FATAL cannot execute $command_directory/postconf!
91         exit 1
92 }
93
94 # If this is a secondary instance, don't touch shared files.
95
96 instances=`test ! -f $def_config_directory/main.cf ||
97     $command_directory/postconf -c $def_config_directory \
98     -h multi_instance_directories | sed 's/,/ /'` || {
99         $FATAL cannot execute $command_directory/postconf!
100         exit 1
101 }
102
103 check_shared_files=1
104 for name in $instances
105 do
106     case "$name" in
107     "$def_config_directory") ;;
108     "$config_directory") check_shared_files=; break;;
109     esac
110 done
111
112 #
113 # Parse JCL
114 #
115 case $1 in
116
117 start_msg)
118
119         echo "Start postfix"
120         ;;
121
122 stop_msg)
123
124         echo "Stop postfix"
125         ;;
126
127 quick-start)
128
129         $daemon_directory/master -t 2>/dev/null || {
130                 $FATAL the Postfix mail system is already running
131                 exit 1
132         }
133         $daemon_directory/postfix-script quick-check || {
134                 $FATAL Postfix integrity check failed!
135                 exit 1
136         }
137         $INFO starting the Postfix mail system
138         $daemon_directory/master &
139         ;;
140
141 start)
142
143         $daemon_directory/master -t 2>/dev/null || {
144                 $FATAL the Postfix mail system is already running
145                 exit 1
146         }
147         if [ -f $queue_directory/quick-start ]
148         then
149                 rm -f $queue_directory/quick-start
150         else
151                 $daemon_directory/postfix-script check-fatal || {
152                         $FATAL Postfix integrity check failed!
153                         exit 1
154                 }
155                 # Foreground this so it can be stopped. All inodes are cached.
156                 $daemon_directory/postfix-script check-warn
157         fi
158         $INFO starting the Postfix mail system
159         # NOTE: wait in foreground process to get the initialization status.
160         $daemon_directory/master -w || {
161             $FATAL "mail system startup failed"
162             exit 1
163         }
164         ;;
165
166 drain)
167
168         $daemon_directory/master -t 2>/dev/null && {
169                 $FATAL the Postfix mail system is not running
170                 exit 1
171         }
172         $INFO stopping the Postfix mail system
173         kill -9 `sed 1q pid/master.pid`
174         ;;
175
176 quick-stop)
177
178         $daemon_directory/postfix-script stop
179         touch $queue_directory/quick-start
180         ;;
181
182 stop)
183
184         $daemon_directory/master -t 2>/dev/null && {
185                 $FATAL the Postfix mail system is not running
186                 exit 0
187         }
188         $INFO stopping the Postfix mail system
189         kill `sed 1q pid/master.pid`
190         for i in 5 4 3 2 1
191         do
192             $daemon_directory/master -t && exit 0
193             $INFO waiting for the Postfix mail system to terminate
194             sleep 1
195         done
196         $WARN stopping the Postfix mail system with force
197         pid=`awk '{ print $1; exit 0 } END { exit 1 }' pid/master.pid` && 
198                 kill -9 -$pid
199         ;;
200
201 abort)
202
203         $daemon_directory/master -t 2>/dev/null && {
204                 $FATAL the Postfix mail system is not running
205                 exit 0
206         }
207         $INFO aborting the Postfix mail system
208         kill `sed 1q pid/master.pid`
209         ;;
210
211 reload)
212
213         $daemon_directory/master -t 2>/dev/null && {
214                 $FATAL the Postfix mail system is not running
215                 exit 1
216         }
217         $INFO refreshing the Postfix mail system
218         $command_directory/postsuper active || exit 1
219         kill -HUP `sed 1q pid/master.pid`
220         $command_directory/postsuper &
221         ;;
222
223 flush)
224
225         cd $queue_directory || {
226                 $FATAL no Postfix queue directory $queue_directory!
227                 exit 1
228         }
229         $command_directory/postqueue -f
230         ;;
231
232 check)
233
234         $daemon_directory/postfix-script check-fatal || exit 1
235         $daemon_directory/postfix-script check-warn
236         exit 0
237         ;;
238
239 status)
240
241         $daemon_directory/master -t 2>/dev/null && {
242                 $INFO the Postfix mail system is not running
243                 exit 1
244         }
245         $INFO the Postfix mail system is running: PID: `sed 1q pid/master.pid`
246         exit 0
247         ;;
248
249 quick-check)
250         # This command is NOT part of the public interface.
251
252         $SHELL $daemon_directory/post-install create-missing || {
253                 $WARN unable to create missing queue directories
254                 exit 1
255         }
256
257         # Look for incomplete installations.
258
259         test -f $config_directory/master.cf || {
260                 $FATAL no $config_directory/master.cf file found
261                 exit 1
262         }
263         exit 0
264         ;;
265
266 check-fatal)
267         # This command is NOT part of the public interface.
268
269         $daemon_directory/postfix-script quick-check
270
271         # See if all queue files are in the right place. This is slow.
272         # We must scan all queues for mis-named queue files before the
273         # mail system can run.
274
275         $command_directory/postsuper || exit 1
276         exit 0
277         ;;
278
279 check-warn)
280         # This command is NOT part of the public interface.
281
282         # Check Postfix root-owned directory owner/permissions.
283
284         find $queue_directory/. $queue_directory/pid \
285             -prune ! -user root \
286             -exec $WARN not owned by root: {} \;
287
288         find $queue_directory/. $queue_directory/pid \
289             -prune \( -perm -020 -o -perm -002 \) \
290             -exec $WARN group or other writable: {} \;
291
292         # Check Postfix root-owned directory tree owner/permissions.
293
294         todo="$config_directory/."
295         test -n "$check_shared_files" && {
296                 todo="$daemon_directory/. $meta_directory/. $todo"
297                 test "$shlib_directory" = "no" || 
298                     todo="$shlib_directory/. $todo"
299         }
300         todo=`echo "$todo" | tr ' ' '\12' | sort -u`
301
302         find $todo ! -user root \
303             -exec $WARN not owned by root: {} \;
304
305         find $todo \( -perm -020 -o -perm -002 \) \
306             -exec $WARN group or other writable: {} \;
307
308         # Check Postfix mail_owner-owned directory tree owner/permissions.
309
310         find $data_directory/. ! -user $mail_owner \
311             -exec $WARN not owned by $mail_owner: {} \;
312
313         find $data_directory/. \( -perm -020 -o -perm -002 \) \
314             -exec $WARN group or other writable: {} \;
315
316         # Check Postfix mail_owner-owned directory tree owner.
317
318         find `ls -d $queue_directory/* | \
319             egrep '/(saved|incoming|active|defer|deferred|bounce|hold|trace|corrupt|public|private|flush)$'` \
320             ! \( -type p -o -type s \) ! -user $mail_owner \
321                 -exec $WARN not owned by $mail_owner: {} \;
322
323         # WARNING: this should not descend into the maildrop directory.
324         # maildrop is the least trusted Postfix directory.
325
326         find $queue_directory/maildrop -prune ! -user $mail_owner \
327             -exec $WARN not owned by $mail_owner: $queue_directory/maildrop \;
328
329         # Check Postfix setgid_group-owned directory and file group/permissions.
330
331         todo="$queue_directory/public $queue_directory/maildrop"
332         test -n "$check_shared_files" && 
333            todo="$command_directory/postqueue $command_directory/postdrop $todo"
334
335         find $todo \
336             -prune ! -group $setgid_group \
337             -exec $WARN not owned by group $setgid_group: {} \;
338
339         test -n "$check_shared_files" &&
340         find $command_directory/postqueue $command_directory/postdrop \
341             -prune ! -perm -02111 \
342             -exec $WARN not set-gid or not owner+group+world executable: {} \;
343
344         # Check non-Postfix root-owned directory tree owner/content.
345
346         for dir in bin etc lib sbin usr
347         do
348             test -d $dir && {
349                 find $dir ! -user root \
350                     -exec $WARN not owned by root: $queue_directory/{} \;
351
352                 find $dir -type f -print | while read path
353                 do
354                     test -f /$path && {
355                         cmp -s $path /$path || 
356                             $WARN $queue_directory/$path and /$path differ
357                     }
358                 done
359             }
360         done
361
362         find corrupt -type f -exec $WARN damaged message: {} \;
363
364         # Check for non-Postfix MTA remnants.
365
366         test -n "$check_shared_files" -a -f /usr/sbin/sendmail -a \
367                 -f /usr/lib/sendmail && {
368             cmp -s /usr/sbin/sendmail /usr/lib/sendmail || {
369                 $WARN /usr/lib/sendmail and /usr/sbin/sendmail differ
370                 $WARN Replace one by a symbolic link to the other
371             }
372         }
373         exit 0
374         ;;
375
376 set-permissions|upgrade-configuration)
377         $daemon_directory/post-install create-missing "$@"
378         ;;
379
380 post-install)
381         # Currently not part of the public interface.
382         shift
383         $daemon_directory/post-install "$@"
384         ;;
385
386 tls)
387         shift
388         $daemon_directory/postfix-tls-script "$@"
389         ;;
390
391 /*)
392         # Currently not part of the public interface.
393         "$@"
394         ;;
395
396 *)
397         $ERROR "unknown command: '$1'"
398         $FATAL "usage: postfix start (or stop, reload, abort, flush, check, status, set-permissions, upgrade-configuration)"
399         exit 1
400         ;;
401
402 esac