Attention: All pages of this wiki depend on the pages that come before it, in order as they are listed on the Main Page. Please check for Dependencies.
Please also look at What You Need to Know Before Using This Wiki

/root/scripts/mailutils.sh

From COCNM
Revision as of 13:33, 14 March 2014 by Bob (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
#!/bin/sh

## 2013.01.07 - Computerisms.ca
## Script written by Bob Miller
## Use it, share it, don't claim it.

echo "Welcome.  I am the Computerisms mail utility helper.  I can help you with the following tasks:"
echo ""
echo "	[1]	Manage Aliases (make an existing address answer for more than one address)"
echo "	[2]	Manage Forwarding (make mail that arrives to an address get resent to another address)"
echo ""
read -p "Please enter the [number] of the taks you wish me to perform:  " TASK

echo $TASK

case $TASK in 
		1)
			echo "Okay, we are managing mail Aliases."  
			echo ""
			read -p "This section requires Network Administrator Credentials.  Please enter a username with sufficient privileges:  " UNM
			echo ""
			read -sp "Thank you.  Please enter the password that goes with this account (what you type will not display):  " PASS && echo
			echo ""
			echo "Thank you.  Please pick your task:"
			echo ""
			echo " (1)	Show all addresses with one or more aliases, and list the aliases (will output to less)"
			echo " (2)	List any aliases a specific address has"
			echo " (3)	Add or Remove an alias to an address"
			echo ""
			read -p "Enter the number corresponding to the task you wish me to perform:  " ALSTSK

			case $ALSTSK in
					1)
						ldbsearch -H ldap://localhost -b "CN=Users,DC=computerisms,DC=com" -U $UNM\@computerisms.com\%$PASS mailAlternateAddress=\* > /tmp/aliasdata.txt
						ALNUM=$(cat /tmp/aliasdata.txt | tail -2 | head -1 | cut -f 2 -d " ")
						echo "There are $ALNUM addresses with Aliases"
						while [[ $(grep sAMAccountName /tmp/aliasdata.txt) != "" ]]; do
							sed -n '1,/^$/ p' /tmp/aliasdata.txt | grep -e sAMAccountName -e mailAlternateAddress >> /tmp/aliaslist.txt
							echo "" >> /tmp/aliaslist.txt
							sed -i '1,/^$/ d' /tmp/aliasdata.txt
						done
						sed 's/sAMAccountName/Account Name/' /tmp/aliaslist.txt | sed 's/mailAlternateAddress/Has Alias/' | less
						;;

					2)
						read -p "Sure, please enter which account you want me to look up (do not include the @computerisms.com part):  " ACCTNM
						echo ""
						echo "Account $ACCTNM has the following Aliases:"
						ldbsearch -H ldap://localhost -b "CN=Users,DC=computerisms,DC=com" -U $UNM\@computerisms.com\%$PASS sAMAccountName=$ACCTNM | grep  mailAlternateAddress | sed 's/mailAlternateAddress: //'
						;;

					3)
						read -p "Sure, please enter which account you want me to Modify (do not include the @computerisms.com part):  " ACCTNM
						echo ""
						ldbsearch -H ldap://localhost -b "CN=Users,DC=computerisms,DC=com" -U $UNM\@computerisms.com\%$PASS sAMAccountName=$ACCTNM | grep  mailAlternateAddress | sed 's/mailAlternateAddress: //' > /tmp/alias.$ACCTNM
						ANUM=$(cat /tmp/alias.$ACCTNM | wc -l) 
						FN=$(echo $ACCTNM | cut -f 1 -d .)
						LN=$(echo $ACCTNM | cut -f 2 -d .)
						echo "dn: CN=$FN $LN,CN=Users,DC=computerisms,DC=com" >> /tmp/aliases.ldif
						echo "changetype: modify" >> /tmp/aliases.ldif
 
						if [ -s /tmp/alias.$ACCTNM ]; then
							echo "Account $ACCTNM has $ANUM aliases already.  Let's go through them and you can tell me if anything needs changing"
							echo ""
							for ALS in $(cat /tmp/alias.$ACCTNM); do
								read -p "$ALS is an alias for $ACCTNM@computerisms.com.  Do you want to [k]eep or [r]emove this alias?  " KR
								if [[ $KR = r ]]; then
									if [[ $(grep delete /tmp/aliases.ldif) == "" ]]; then 
										echo "delete: mailAlternateAddress" >> /tmp/aliases.ldif
									fi
									echo "mailAlternateAddress: $ALS" >> /tmp/aliases.ldif
								fi
							done
						fi
						echo ""
						ADDAL=y
						while [[ $ADDAL = y ]]; do
							read -n 1 -p "would you like to add an alias to $ACCTNM@computerisms.com? [y]es or [any key] for no" ADDAL
							echo ""
							if [[ $ADDAL == y ]]; then
								if [[ $(grep ^add /tmp/aliases.ldif) == "" ]]; then
									echo "-" >> /tmp/aliases.ldif
									echo "add: mailAlternateAddress" >> /tmp/aliases.ldif
								fi
								read -p "Please enter the alias (include the @computerisms.com part):  " ADDALNM
								echo "mailAlternateAddress: $ADDALNM" >> /tmp/aliases.ldif
							fi
						done
						ldbmodify -H ldap://localhost -U $UNM\@computerisms.com\%$PASS /tmp/aliases.ldif
						echo ""
						echo "Alias records have been modified.  Have a nice day!"
						;;
			
					*)
						echo "Ohh!.  I am sorry to have send you back, but that was an invalid choice"
						;;

			esac
			rm /tmp/alias*
			;;

		2)
			echo "Okay, we are manageing mail Forwards.  You need to know that dotQmail are easy to use."
			echo "You should really learn to modify them manually.  Please choose your task:"
			echo ""
			echo " (1)	List all accounts that forward their mail, and show all the forwards for each account (will output to less)"
			echo " (2)	List all forwards for a specific account"
			echo " (3)	Modify the forwards for a specific account"
			echo ""
			read -p "Enter the number corresponding to the task you wish me to perform:  " FWDTSK
			
			case $FWDTSK in
					1)
						echo "These are all the accounts that have forwards, and the list of where the mail is forwarded too:"
						for i in $(ls /home/mail/*/.qmail | cut -f 1,2,3,4 -d /); do 
							if [[ -e $i/Maildir ]]; then 
								HOME=$(echo $i | cut -f 4 -d /)
								if [[ $(grep ^\& /home/mail/$HOME/.qmail) != "" ]]; then
									echo "$HOME@computerisms.com forwards too:" >> /tmp/fwd.txt
									grep ^\& /home/mail/$HOME/.qmail | cut -f 2 -d \& >> /tmp/fwd.txt
									echo "" >> /tmp/fwd.txt
								fi
							fi 
						done
						less /tmp/fwd.txt
						echo ""
						echo "These are all the accounts that are forwarding their mail right now"
						echo "Have a Wonderful Day!"
						echo ""
						;;

					2)
						read -p "Sure, please enter which account you want me to look up (do not include the @computerisms.com part):  " ACCTNM
						echo ""
						if [ -e /home/mail/$ACCTNM/.qmail ]; then
							if [[ $(grep ^\& /home/mail/$ACCTNM/.qmail) != "" ]]; then
								echo "Account $ACCTNM has the following Forwards:"
								grep ^\& /home/mail/$ACCTNM/.qmail
								echo ""
							fi
						else
							echo "Account $ACCTNM has no forwards"
							echo ""
						fi
						echo ""
						echo "That is all the forwards I can find right now for $ACCTNM"
						echo "Have a Wonderful Day!"
						echo ""
						;;

					3)
						read -p "Sure, please enter which account you want me to Modify (do not include the @computerisms.com part):  " ACCTNM
						echo ""
						if [ -e /home/mail/$ACCTNM/.qmail ]; then
							if [[ $(grep ^\& /home/mail/$ACCTNM/.qmail) != "" ]]; then
								grep ^\& /home/mail/$ACCTNM/.qmail >> /tmp/fwd.txt
								FWDNUM=$(wc -l /tmp/fwd.txt)
								echo "Account $ACCTNM has $FWDNUM Forward(s).  Let's go through them and you can tell me what needs to change:"
								echo "Please note the leading ampersand (&) is supposed to be there"
								echo ""
								for FWD in $(cat /tmp/fwd.txt); do
									read -n 1 -p "$ACCTNM@computerisms.com forwards to $FWD.  Do you want to [k]eep or [r]emove  this forward?  " KR
									echo ""
									if [ $KR == k ]; then
										echo $FWD >> /tmp/fwd.txt.2
									fi
								done
							fi
						fi
						ADDFWD=y
						while [[ $ADDFWD == y ]]; do
							read -n 1 -p "would you like to add a forward to $ACCTNM@computerisms.com?  [y]es or [any key] for no" ADDFWD
							echo ""
							if [[ $ADDFWD == y ]]; then
								read -p "Okay, please enter the address to forward mail for $ACCTNM to:  "  FWDADDR
								echo "&$FWDADDR" >> /tmp/fwd.txt.2
							fi
						done
						read -n 1 -p "Should I keep a local copy of the mail I forward for $ACCTNM?  [y]es or [any key] for no" LOC
						echo ""
						if [[ $LOC == y ]]; then
							echo "/home/mail/$ACCTNM/Maildir/" >> /tmp/fwd.txt.2
						fi
						if [ -e /tmp/fwd.txt.2 ]; then
							chmod +t /home/mail/$ACCTNM
							cat /tmp/fwd.txt.2 > /home/mail/$ACCTNM/.qmail
							chmod -t /home/mail/$ACCTNM
						else
							rm /home/mail/$ACCTNM/.qmail
						fi
						echo ""
						echo "Forwards updated for $ACCTNM"
						echo "Have a Wonderful Day!"
						echo ""
						;;
	
			esac
			rm /tmp/fwd.txt*
			;;

		*)
			echo "Invalid Entry, Sorry, Please Start Over"
			
			;;
esac