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