#!/bin/sh ###################################################################### # # aliasme # # Program allows a user to add e-mail aliases # for him or herself without bugging the sysadmin # # REQ FILES & FACILITIES # cron # sendmail # /etc/mail/aliasme.conf # /etc/mail/aliases # /etc/mail/aliasme/ (dir) # /etc/passwd # "alias" group # aliasme script # # FILES USED WITHIN: # /etc/mail/aliasme/useraliases.tmp validated user aliases # /tmp/aliases.tmp saved system aliases # /etc/mail/aliasme/aliases.bak.$DATE backed up aliases file # # C. Geigner - (c)2003, This program may be distributed under the # LGPL. Read the LGPL here: http://www.gnu.org/copyleft/lesser.html # ######################################################################### VER="aliasme v0.1-4 beta - By Chuck Geigner (c) 2003" USAGE="Usage: aliasme [-k]\ -k runs aliasme in check-only mode (does not update).\ When run as root, the -k flag will validate all .aliases files in home dirs\ belonging to users in the \"alias\" group." CHK_ALIAS() { SUCCESS=1 # if were not root, then we must not be running GEN_LIST, which would in turn make # AUSER undefined. So we set AUSER to the username of who called us. if [ ${USER} != "root" ]; then AUSER=${USER} fi echo "Checking aliases for user ${AUSER}:" if [ -s /home/$AUSER/.aliases ]; then for ENTRY in `cat /home/$AUSER/.aliases` do # check for illegal chars in aliases echo "${ENTRY}" | grep "[^0-9a-z_-]" 2> /dev/null if [ $? -eq 0 ]; then SUCCESS=0 echo "The ${AUSER} alias \"${ENTRY}\" contains illegal or uppercase letters" if [ ${MODE} = "usercheck" ]; then echo "Please edit your .aliases file to correct this conflict." fi fi # check for conflicts in aliases grep -v "${AUSER}$" /etc/mail/aliases | grep "^${ENTRY}:" if [ $? -eq 0 ]; then SUCCESS=0 echo "The ${AUSER} defined alias \"${ENTRY}\" is already in use by another user." if [ ${MODE} = "usercheck" ]; then echo "Please edit your .aliases file to correct this conflict." fi elif [ $? -gt 1 ]; then echo "File problem encountered with /etc/mail/aliases." echo "Please check file and re-try (or contact admin)." exit fi # check for conflicts in system passwd file grep "^${ENTRY}:" /etc/passwd if [ $? -eq 0 ]; then SUCCESS=0 echo "\"${ENTRY}\" is already an existing username." if [ ${MODE} = "usercheck" ]; then echo "Please edit your .aliases file to correct this conflict." fi elif [ $? -gt 1 ]; then echo "File problem encountered with /etc/passwd." SUCCESS=0 if [ ${MODE} = "usercheck" ]; then echo "Please check file and re-try (or contact admin)." exit fi fi # check for conflicts in aliases already in tmp file if [ ${MODE} = "admin" ]; then if [ -s /etc/mail/aliasme/useraliases.tmp ]; then grep "^${ENTRY}:" /etc/mail/aliasme/useraliases.tmp if [ $? -eq 0 ]; then SUCCESS=0 echo "\"${ENTRY}\" is already an existing alias." elif [ $? -gt 1 ]; then echo "File problem encountered with /etc/mail/aliasme/useraliases.tmp." SUCCESS=0 exit fi fi fi # note all-ok if no probs with alias choices, set go-ahead tag for write to aliases_restr done else echo "The alias file /home/${AUSER}/.aliases is missing or empty." SUCCESS=0 if [ ${MODE} = "usercheck" ]; then exit fi fi if [ ${SUCCESS} -eq 1 ]; then echo "All aliases for ${AUSER} checked and valid." fi } GEN_LIST() { if [ ${USER} = "root" ]; then if [ ! -d /etc/mail/aliasme/ ]; then mkdir /etc/mail/aliasme/ fi # reset this file if it exists and > 0 if [ -s /etc/mail/aliasme/useraliases.tmp ]; then cat /dev/null > /etc/mail/aliasme/useraliases.tmp fi # generate a list of alias users from those found in the alias group # and then run validity checks and add valids to the tmp file. for AUSER in `grep "^alias:" /etc/group |\ awk -F: '{print $4}'|\ awk -F, '{for (i=1;i<=NF;i++) print $i}'` do CHK_ALIAS; if [ ${SUCCESS} -eq 1 -a ${MODE} = "admin" ]; then echo "##aliasme ${AUSER}" >> /etc/mail/aliasme/useraliases.tmp for LINES in `cat /home/$AUSER/.aliases` do echo "${LINES}: ${AUSER}" >> /etc/mail/aliasme/useraliases.tmp done echo "##aliasme--end" >> /etc/mail/aliasme/useraliases.tmp fi done if [ ${MODE} = "admincheck" ]; then rm /etc/mail/aliasme/useraliases.tmp #If in check-only mode, exit; #No need to stick around, we're done fi BAK="/etc/mail/aliasme/aliases.bak.`date +%b%d-%H%M`" cp /etc/mail/aliases ${BAK} # back up orig aliases file by date/time # let's see if we already have entries grep "##aliasme" /etc/mail/aliases > /dev/null if [ $? -eq 0 ]; then #yup, we have some #NOTE: All aliasme entries begin with "##aliasme" #and end with "##aliasme--end" # Munge munge munge. Hopefully it all goes back together, eh? ;) # NEXT: Preserve the current non-aliasme aliases!!! SNIPSTART=`awk '$1 ~ /##aliasme/{print NR}' /etc/mail/aliases |head -1` #line number of start SNIPEND=`awk '$1 ~ /##aliasme--end/{print NR}' /etc/mail/aliases |tail -1` #line num of last entry LINECT=`awk 'END{print NR}' /etc/mail/aliases` if [ ${LINECT} -gt 2 -a \ ${SNIPSTART} != "" -a \ ${SNIPEND} != "" -a \ ${SNIPEND} -gt ${SNIPSTART} ]; then SNIPSTART=`expr ${SNIPSTART} - 1` #actually we need to stop at the line before... awk 'NR==1,NR=='${SNIPSTART} /etc/mail/aliases > /tmp/aliases.tmp #dump all non-aliasme entries fm line 1 to start if [ ${LINECT} -gt ${SNIPEND} ]; then #if there's more non-aliasme entries at EOF SNIPEND=`expr ${SNIPEND} + 1` #...resume on the line after awk 'NR=='${SNIPEND}',NR=='${LINECT} /etc/mail/aliases >> /tmp/aliases.tmp #then dump all non-aliasme entries from end to EOF fi else echo "Not enough lines in aliases file to save!" fi else cat /etc/mail/aliases > /tmp/aliases.tmp #no prev aliasme entries found, so dump the whole file fi # Put system aliases and user aliases back into the aliases file and rebuild. if [ -s /etc/mail/aliasme/useraliases.tmp ]; then cat /tmp/aliases.tmp /etc/mail/aliasme/useraliases.tmp > /etc/mail/aliases else # no user aliases defined so... cat /tmp/aliases.tmp > /etc/mail/aliases fi if [ -f ${BAK} ]; then SUM1=`sum ${BAK} |awk '{print $1}'` else SUM1=0 fi SUM2=`sum /etc/mail/aliases | awk '{print $1}'` if [ ${SUM1} -eq ${SUM2} ]; then echo "No changes occured. Exiting." rm -f ${BAK} #no need to backup or process if the files are the same else #build the new aliases db echo "Updating aliases database..". newaliases echo "...done." fi rm -f /etc/mail/aliasme/useraliases.tmp rm -f /tmp/aliashead.tmp #signal end of process regardless of errors else echo "Only the root user may update the aliases table." exit fi } # $ aliasme if [ ${USER} != "root" ]; then MODE="usercheck" CHK_ALIAS; #aliasme -k else if [ $# -ge 2 ]; then if [ $1 -eq "-k" ]; then MODE="admincheck" else echo ${USAGE}; exit fi else MODE="admin" fi GEN_LIST; fi