head 1.1; access; symbols; locks alexis:1.1; strict; comment @# @; 1.1 date 98.08.07.11.33.43; author alexis; state Exp; branches; next ; desc @two-way mail transfer program via SMTP with sendmail. @ 1.1 log @Initial revision @ text @#!SCRIPTSHELLCMD_MARKER PATH=/bin:/usr/bin PROGNAME=`basename $0` ############################################################################### # # # MAIL TRANSFER PROGRAM - USES SENDMAIL # # # ############################################################################### ############################################################################### # # Things you should know about this script: # # It is stored under RCS! Keep it that way! If you want a history of # changes made to this script then refer to rlog(1) - don't look here! # ############################################################################### ############################################################################### # # CONFIGURABLE STUFF STARTS HERE # ############################################################################### ############################################################################### # # CONFIGURABLE STUFF ENDS HERE # ############################################################################### VERSION="PATCHLEVEL_MARKER" # $Id: sndmail-sendmail.in,v 1.5 1998/02/05 12:27:22 alexis Exp $ ############################################################################### # # MAIN FUNCTION (called from very bottom of script) # ############################################################################### main() { ########################################################################### # # PROCESS OPTIONS # ########################################################################### while [ "X$1" != X ]; do case "$1" in -V) if expr "$VERSION" : 'P.*R$' > /dev/null; then warning "this is a development version; use 'ident' for version information" exit 1 else echo "$PROGNAME version $VERSION" exit 0 fi ;; -d) [ "X$2" = X ] && usage VERBOSE_LEVEL=$2 shift ;; -v) VERBOSE_LEVEL=3 ;; -*) usage ;; *) break ;; esac shift done # No other commandline arguments should be specified [ "X$1" != X ] && usage # Are locks required? No. Sendmail does it's own locking. ########################################################################### # # SEND MAIL # ########################################################################### exec SENDMAILCMD_MARKER -q ########################################################################### # # GET MAIL # ########################################################################### # Nothing to do! } gen_lock_file_name() { echo LOCKDIR_MARKER/$PROGNAME.pid } usage() { { echo "Usage: $PROGNAME [ -d | -v" echo " $PROGNAME -V" } >&2 exit 1 } ############################################################################### # # RESILIANT MESSAGING FUNCTIONS # ############################################################################### internal() { MSG="INTERNAL ERROR: $*" DATE=`date` # echo it so that the user can see it or cron can trap it (split line # to avoid confusing 'ident') echo "$PROGNAME: \ $MSG" >&2 # try to write it into $LOG_FILE if that's defined - other message types # treat unwritable but defined LOG_FILE as an error ( echo "$DATE: \ $MSG" >> ${LOG_FILE:-/dev/null} ) 2>/dev/null # try to syslog it - other message types are only syslog'ed if there is # no controlling terminal (as when run from 'at' or 'cron') logger -i -p local0.alert -t $PROGNAME "$MSG" # try to log it on the console echo "$DATE: \ $MSG" > /dev/console exit 2 } error() { DO_EXIT=${ERRORS_CAUSE_EXITS:-true} STDIN=false while :; do case "$1" in -) STDIN=true ;; -e) DO_EXIT=true ;; -ne) DO_EXIT=false ;; -*) internal "invalid option to error() '$1'" ;; *) break ;; esac shift done if [ ${VERBOSE_LEVEL:-2} -ge 1 ]; then DATE=`date` { { [ $STDIN = false ] && echo "$@@"; } || cat; } | while read LINE; do MSG="ERROR: $LINE" ( echo "$DATE: \ $MSG" >> ${LOG_FILE:-/dev/null} ) 2>/dev/null || internal "log file $LOG_FILE not writable!" { [ -t 2 ] && echo "$PROGNAME: \ $MSG" >&2; } || logger -i -p $FACILITY.err -t $PROGNAME "$MSG" done fi [ $DO_EXIT = false ] && return 1 exitdel exit 1 } warning() { STDIN=false while :; do case "$1" in -) STDIN=true ;; -*) internal "invalid option to error() '$1'" ;; *) break ;; esac shift done if [ ${VERBOSE_LEVEL:-2} -ge 2 ]; then DATE=`date` { { [ $STDIN = false ] && echo "$@@"; } || cat; } | while read LINE; do MSG="WARNING: $LINE" ( echo "$DATE: \ $MSG" >> ${LOG_FILE:-/dev/null} ) 2>/dev/null || internal "log file $LOG_FILE not writable!" { [ -t 2 ] && echo "$PROGNAME: \ $MSG" >&2; } || logger -i -p $FACILITY.warning -t $PROGNAME "$MSG" done fi return 0 } debug() { STDIN=false while :; do case "$1" in -) STDIN=true ;; -*) internal "invalid option to error() '$1'" ;; *) break ;; esac shift done LEVEL=$1 shift if [ ${VERBOSE_LEVEL:-2} -ge $LEVEL ]; then DATE=`date` { { [ $STDIN = false ] && echo "$@@"; } || cat; } | while read LINE; do MSG="DEBUG[$LEVEL]: $LINE" ( echo "$DATE: \ $MSG" >> ${LOG_FILE:-/dev/null} ) 2>/dev/null || internal "log file $LOG_FILE not writable!" { [ -t 2 ] && echo "$PROGNAME: \ $MSG" >&2; } || logger -i -p $FACILITY.debug -t $PROGNAME "$MSG" done fi return 0 } info() { STDIN=false while :; do case "$1" in -) STDIN=true ;; -*) internal "invalid option to error() '$1'" ;; *) break ;; esac shift done if [ ${VERBOSE_LEVEL:-2} -ge 3 ]; then DATE=`date` { { [ $STDIN = false ] && echo "$@@"; } || cat; } | while read LINE; do MSG="INFO: $LINE" ( echo "$DATE: \ $MSG" >> ${LOG_FILE:-/dev/null} ) 2>/dev/null || internal "log file $LOG_FILE not writable!" { [ -t 2 ] && echo "$PROGNAME: \ $MSG" >&2; } || logger -i -p $FACILITY.info -t $PROGNAME "$MSG" done fi return 0 } ############################################################################### # # GENERIC SUPPORT FUNCTIONS (not specifically related to backup task) # ############################################################################### locatecmd() { for POSSCMD in $*; do [ X`whichcmd $POSSCMD` != X ] && { echo $POSSCMD; return 0; } done return 1 } whichcmd() { for DIR in `echo $PATH | sed 's/:/ /g'`; do [ -x $DIR/$1 ] && { echo $DIR/$1; return 0; } done return 1 } ############################################################################### # # TEMPORARY FILE MANAGEMENT FUNCTIONS # ############################################################################### delonexit() { DELONEXIT="$DELONEXIT $*" } dontdelonexit() { for FILE in $*; do DELONEXIT=`echo $DELONEXIT | sed "s@@$FILE@@@@g"` done } exitdel() { debug 5 "cleaning up" rm -f $DELONEXIT } sighandler() { warning "signal recieved, clearing up and exiting" exitdel exit 3 } ############################################################################### # # LOCKING ROUTINES # ############################################################################### lock() { debug 5 "lock: locking $1" LOCKFILE=`gen_lock_file_name $1` { echo $$ > $LOCKFILE.$$; } 2>/dev/null || error -e "can't create lock" ln $LOCKFILE.$$ $LOCKFILE 2>/dev/null && { rm $LOCKFILE.$$; return 0; } HOLDING_PID=`cat $LOCKFILE` [ "X$HOLDING_PID" = X ] && { rm $LOCKFILE.$$; error -e "empty lockfile: $LOCKFILE"; } [ -d /proc/$HOLDING_PID ] && { rm $LOCKFILE.$$; error -e "program running already! (pid=$HOLDING_PID)"; } info "removing stale lock (pid=$HOLDING_PID)" rm -f $LOCKFILE ln $LOCKFILE.$$ $LOCKFILE 2>/dev/null && { rm $LOCKFILE.$$; return 0; } rm $LOCKFILE.$$ error -e "couldn't lock after removing stale lockfile" } unlock() { debug 5 "unlock: unlocking $1" LOCKFILE=`gen_lock_file_name $1` rm -f $LOCKFILE } umask 022 main "$@@" exit $? @