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