Sophie

Sophie

distrib > Mandriva > 2009.0 > i586 > by-pkgid > 50dfdb1b1881d3b98176e574091bcaef > files > 20

postfix-2.5.5-2mdv2009.0.src.rpm

#!/bin/sh
#
# postfix      Postfix Mail Transfer Agent
#
# chkconfig: 2345 80 30
# description: Postfix is a Mail Transport Agent, which is the program \
#              that moves mail from one machine to another.
# processname: master
# pidfile: /var/spool/postfix/pid/master.pid
# config: /etc/postfix/main.cf
# config: /etc/postfix/master.cf

### BEGIN INIT INFO
# Provides: postfix
# Required-Start: $network
# Required-Stop: $network
# Should-Start: $named mysqld postgresql ldap saslauthd
# Should-Stop: $named mysqld postgresql ldap saslauthd
# Default-Start: 2 3 4 5
# Short-Description: Starts the postfix daemons
# Description: Postfix is a Mail Transport Agent, which is the program \
#              that moves mail from one machine to another.
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Source package configuration.
CHROOT=0
REBUILD_ALIASES=1
REBUILD_MAPS=1
. /etc/sysconfig/postfix


# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

[ -x /usr/sbin/postfix ] || exit 5 #program is not installed
[ -f /etc/postfix/main.cf ] || exit 6 #program is not configured
[ -d /var/spool/postfix ] || exit 06 #program is not configured

RETVAL=0
UA=/usr/sbin/update-alternatives

# http://archives.mandrivalinux.com/cooker/2005-07/msg01012.php
# http://archives.mandrivalinux.com/cooker/2005-07/msg01691.php
fix_alternatives() {
	if [ -e /var/lib/rpm/alternatives/mta ]; then
		echo -n "Found old alternatives, fixing: "
		$UA --remove mta /usr/sbin/sendmail.postfix
		if [ -e /var/lib/rpm/alternatives/sendmail-command ]; then
			$UA --auto sendmail-command
		fi
		if [ ! -e /usr/bin/mailq ]; then
			ln -sf /usr/sbin/sendmail.postfix /usr/bin/mailq
		fi
		if [ ! -e /usr/bin/newaliases ]; then
			ln -sf /usr/sbin/sendmail.postfix /usr/bin/newaliases
		fi
		if [ ! -e /usr/bin/rmail ]; then
			cat > /usr/bin/rmail <<EOF
#!/bin/sh

# Dummy UUCP rmail command for postfix/qmail systems

SENDMAIL="/usr/sbin/sendmail"
IFS=" " read junk from junk junk junk junk junk junk junk relay

case "$from" in
 *[@!]*) ;;
      *) from="$from@$relay";;
esac

exec $SENDMAIL -i -f "$from" -- "$@"
EOF
		fi
		if [ ! -e /var/lib/rpm/alternatives/mta -a \
		  -e /usr/bin/mailq -a \
		  -e /usr/bin/newaliases -a \
		  -e /usr/bin/rmail ]; then
			echo_success
		else
			echo_failure
		fi
		echo
	fi
}

readconf() {
	for d in `postconf -h config_directory slave_config_directories 2>/dev/null`; do
		[ X`postconf -c $d -h disable_start 2>/dev/null` = Xyes ] && continue
		# postconf will also print defaults
		/usr/sbin/postconf -c $d
		# postconf does not print unknown parameters
		awk '
			/^[[:space:]]*#/ {next}
			/^[[:space:]]*$/ {next}
			/^[[:space:]]/ {l = l $0; next}
			{print l; l = $0}
			END {print l}
		' $d/main.cf
		# some options could be overridden in master.cf
		awk '
			function opt(l) {
			  $0 = l
			  for (i=1;i<NF;i++) {
			    if ($i == "-o") {i++;print $i}
		          }
			}
			/^[[:space:]]*#/ {next}
			/^[[:space:]]*$/ {next}
			/^[[:space:]]/ {l = l $0; next}
			{nl=$0; opt(l); l = nl}
			END {opt(l)}
		' $d/master.cf
	done
}
		

start() {
	fix_alternatives
	echo -n "Starting postfix: "
	# refresh chroot
	[ "${CHROOT}" = "1" ] && /usr/sbin/postfix-chroot.sh -q check_update
	# refresh aliases
	[ "${REBUILD_ALIASES}" = "1" ] && for i in `readconf | perl -ne '
			/^alias_(database|maps)[[:space:]]*=/ || next;
			while (/(hash|btree):([^,\s]+)/g) {$a{"$1:$2"}=1;}
			END {print join(" ",keys %a),"\n";}'`; do
		[ -e ${i#*:} -a ! -e ${i#*:}.db -o ${i#*:} -nt ${i#*:}.db ] && /usr/sbin/postalias $i
	done
	# refresh other maps
	[ "${REBUILD_MAPS}" = "1" ] && for i in `readconf | perl -ne '
			/^alias_(database|maps)\s*=/ && next;
			while (/(hash|btree):([^,\s]+)/g) {$a{"$1:$2"}=1;}
			END {print join(" ",keys %a),"\n";}'`; do
		[ -e ${i#*:} -a ! -e ${i#*:}.db -o ${i#*:} -nt ${i#*:}.db ] && /usr/sbin/postmap $i
	done

	/usr/sbin/postfix start 2>/dev/null 1>&2 && success || failure
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/postfix
        echo
	return $RETVAL
}

stop() {
	echo -n "Shutting down postfix: "
	/usr/sbin/postfix stop 2>/dev/null 1>&2 && success || failure
	RETVAL=$?
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/postfix
	echo
	return $RETVAL
}

reload() {
	echo -n "Reloading postfix: "
	/usr/sbin/postfix reload 2>/dev/null 1>&2 && success || failure
	RETVAL=$?
	echo
	return $RETVAL
}

abort() {
	/usr/sbin/postfix abort 2>/dev/null 1>&2 && success || failure
	RETVAL=$?
	return $RETVAL
}

flush() {
	/usr/sbin/postfix flush 2>/dev/null 1>&2 && success || failure
	RETVAL=$?
	return $RETVAL
}

check() {
	/usr/sbin/postfix check 2>/dev/null 1>&2 && success || failure
	RETVAL=$?
	return $RETVAL
}

restart() {
	stop
	start
	return $?
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	restart
	;;
  force-reload|reload)
	reload
	;;
  abort)
	abort
	;;
  flush)
	flush
	;;
  check)
	check
	;;
  status)
  	status master
	;;
  try-restart|condrestart)
	if [ -f /var/lock/subsys/postfix ]; then
	    restart
	else
	    RETVAL=7 # program is not running
	fi
	;;
  *)
	echo "Usage: postfix {start|stop|restart|force-reload|reload|abort|flush|check|status|try-restart|condrestart}"
	exit 2 # invalid or excess argument(s)
	;;
esac

exit $RETVAL