# # emailasterisk installer for Debian v0.4, Kyle Yoksh K0KN 10/2015. Last revised 10/11/2016 # # This script installs software and configures both inbound and outbound email for your Asterisk / Allstar node # GMAIL and YAHOO are the only providers supported by this script. # # NOTE: To undo the changes made by this installer, simply call it with /u on the command line. For example: # /home/kyle/emailasterisk_installer /u # # This script uses Fetchmail and Procmail to scan incoming email body lines for Asterisk commands. To use, the email or text body # must contain 'do rpt fun' - without the quotes. For example: # do rpt fun 1234 *70 (omit the #) # Depending on your provider, text messages may also work. # # The 'who_is' script is also supported. Note that this script must be edited to include your email addresses. # # Use the syntax 'who 2000' (without the quotes) to have the node callsign and description # sent to you via email or text message. For example: # who 2000 (omit the #) # would email/text you the result: # 2000 ALBACORE HUB WB6NIL Downtown Los Angeles, CA # # Sending Asterisk text messages will also work. Use the syntax: # text 2000 "hello everyone" ( omit the #) # in the body of an email to the node to send an Asterisk text message to all nodes connected to node 2000. This can be seen at the # CLI prompt or via the Web Transceiver text box. # # You can also have your node email you a list of connected nodes for any public Allstar node by using this syntax: # conn 2000 (replace 2000 with the node number of interest) # # # useprovider="" if ! [ `whoami` = root ]; then echo "Please run this script as root or using sudo" exit fi # undo_choice () { echo "Undo emailasterisk_installer? [y/n]" read YESNO if [ "$YESNO" = "y" -o "$YESNO" = "Y" ] ; then echo "Enter non-root username used by Fetchmail (if known)" read GETNAME ; undo_installer else echo "Exiting." ; exit fi } # undo_installer () { if [ "$GETNAME" = root ] ; then echo "non-root user cannot be ROOT! Uninstall will proceed, but some files will not be removed." GETNAME="" ; echo "" fi echo "Please wait. Reverting to backup files and uninstalling packages" FETCHPATH="/home/$GETNAME" cp -f /etc/crontab.original-k0kn /etc/crontab cp -f /etc/sudoers.original-k0kn /etc/sudoers # yum -y remove postfix fetchmail procmail w3m mc apt-get -y remove postfix mailutils fetchmail libsasl2-modules sasl2-bin service crond restart # if [ -s /etc/postfix/main.cf ] ; then rm -f /etc/postfix/main.cf fi if [ -s /etc/postfix/sasl_passwd ] ; then rm -f /etc/postfix/sasl_passwd fi if [ -s /etc/postfix/sasl_passwd.db ] ; then rm -f /etc/postfix/sasl_passwd.db fi if [ -s "$FETCHPATH"/.procmailrc ] ; then rm -f "$FETCHPATH"/.procmailrc fi if [ -s "$FETCHPATH"/.fetchmailrc ] ; then rm -f "$FETCHPATH"/.fetchmailrc fi if [ -s "$FETCHPATH"/.forward ] ; then rm -f "$FETCHPATH"/.forward fi if [ -s "$FETCHPATH"/who_is ] ; then rm -f "$FETCHPATH"/who_is fi if [ -s "$FETCHPATH"/who_is_helper ] ; then rm -f "$FETCHPATH"/who_is_helper fi if [ -s "$FETCHPATH"/conn ] ; then rm -f "$FETCHPATH"/conn fi if [ -s "$FETCHPATH"/conn_helper ] ; then rm -f "$FETCHPATH"/conn_helper fi if [ -s "$FETCHPATH"/ast_text ] ; then rm -f "$FETCHPATH"/ast_text fi if [ -s /etc/sudoers.original-k0kn ] ; then rm -f /etc/sudoers.original-k0kn fi if [ -s /etc/crontab.original-k0kn ] ; then rm -f /etc/crontab.original-k0kn fi echo "" echo "Uninstall complete" ; exit } # passwordverifier () { echo "" echo "Type password for this $useprovider account" read -s thepassword1 # echo "" echo "Type password again to verify" read -s thepassword2 # if [ "$thepassword1" != "$thepassword2" ] ; then echo "Passwords do not match - try again!" ; passwordverifier else emailpassword="$thepassword1" echo "Passwords matched - continuing script" echo "" fi } # clear echo "" echo "This script will configure your system to send and receive" echo "Internet email. It was designed for CentOS 5 and GMAIL or YAHOO only. No other" echo "distributions or email providers have been tested." echo "" # if [ "$1" = "/u" -o "$1" = "/U" ] ; then undo_choice fi # Create backup files # if ! [ -s /etc/sudoers.original-k0kn ] ; then cp /etc/sudoers /etc/sudoers.original-k0kn else echo "Sudoers backup already exists!" ; UNINSTALLFLAG=1 fi if ! [ -s /etc/crontab.original-k0kn ] ; then cp /etc/crontab /etc/crontab.original-k0kn else echo "Crontab backup already exists!" ; UNINSTALLFLAG=1 fi # Revert if script has already been ran on this machine if [ "$UNINSTALLFLAG" = 1 ] ; then echo "" ; echo "This installer has already been ran on this machine!" ; undo_choice fi echo "Fetchmail requires a user (not root) to check email. If you do not have a" echo "non-root user on your system, quit now and "adduser" before running this script" echo "" echo "Type name of user (not root) on your system" echo "" read username if id -u $username >/dev/null 2>&1; then echo "user exists - continuing script" else echo "user does not exist - exiting" ; exit fi echo "" if [ $username = root ]; then echo "DO NOT USE ROOT!!" ; exit fi # Make sure user owns directory mkdir /home/$username 2> /dev/null chown $username:$username /home/$username hostname > /dev/shm/yourhostname.tmp yourhost=$(cat /dev/shm/yourhostname.tmp) echo "Your system has a hostname of: $yourhost" IFS=. set -- $yourhost # note hostname is $1 # note domain is $2 if ! [[ "$yourhost" == *.* ]] then echo "" echo "Your system does not have a fully qualified domain name (FQDN)" echo "Please change your hostname now." echo "Example: hostname myasterisk.local" ; exit fi echo "" echo "Do you want to use [G]mail or [Y]ahoo as your provider?" read provider if [ "$provider" = "G" -o "$provider" = "g" ] ; then echo "Using Gmail as email provider" ; useprovider="gmail" fi # if [ "$provider" = "Y" -o "$provider" = "y" ] ; then echo "Using Yahoo as email provider" ; useprovider="yahoo" fi if [ -z $useprovider ] ; then echo "Exiting - neither email provider selected" rm -f /etc/crontab.original-k0kn rm -f /etc/sudoers.original-k0kn exit fi echo "" echo "Type entire email address that will *SEND* email from Linux" echo "Example: test@$useprovider.com" echo "" read sendingemail passwordverifier # Install packages # yum -y install postfix fetchmail procmail w3m mc apt-get update apt-get -y install postfix mailutils libsasl2-modules sasl2-bin fetchmail w3m mc ntpdate procmail sudo # Get emailasterisk script cd /home/$username wget http://www.qsl.net/k0kn/emailasterisk chown $username:$username /home/$username/emailasterisk chmod 750 /home/$username/emailasterisk # Get other scripts cd /home/$username wget http://www.qsl.net/k0kn/who_is wget http://www.qsl.net/k0kn/who_is_helper wget http://www.qsl.net/k0kn/conn wget http://www.qsl.net/k0kn/conn_helper wget http://www.qsl.net/k0kn/ast_text # chown $username:$username /home/$username/who_is chmod 750 /home/$username/who_is # chown $username:$username /home/$username/who_is_helper chmod 750 /home/$username/who_is_helper # chown $username:$username /home/$username/conn chmod 750 /home/$username/conn # chown $username:$username /home/$username/conn_helper chmod 750 /home/$username/conn_helper # chown $username:$username /home/$username/ast_text chmod 750 /home/$username/ast_text # # Create Asterisk connectlog if needed touch /var/log/asterisk/connectlog chown $username:$username /var/log/asterisk/connectlog # Comment lines from Postfix main.cf file sed -i 's:myorigin = /etc/mailname:#myorigin = /etc/mailname:g' /etc/postfix/main.cf sed -i 's:relayhost =:#relayhost =:g' /etc/postfix/main.cf # Add lines to Postfix main.cf file touch /etc/postfix/main.cf echo "" >> /etc/postfix/main.cf echo "# Added by K0KN email installer" >> /etc/postfix/main.cf # if [ "$useprovider" = "gmail" ] ; then echo "relayhost = [smtp.gmail.com]:587" >> /etc/postfix/main.cf fi # if [ "$useprovider" = "yahoo" ] ; then echo "relayhost = [smtp.mail.yahoo.com]:587" >> /etc/postfix/main.cf fi # echo "smtp_sasl_auth_enable = yes" >> /etc/postfix/main.cf echo "smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd" >> /etc/postfix/main.cf echo "smtp_sasl_security_options = noanonymous" >> /etc/postfix/main.cf echo "smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt" >> /etc/postfix/main.cf echo "smtp_use_tls = yes" >> /etc/postfix/main.cf echo "myhostname = $yourhost" >> /etc/postfix/main.cf echo "mydomain = $2" >> /etc/postfix/main.cf echo "myorigin = $yourhost" >> /etc/postfix/main.cf echo "smtpd_sasl_path = smtpd" >> /etc/postfix/main.cf echo "smtp_sasl_type = cyrus" >> /etc/postfix/main.cf echo "smtp_sasl_tls_security_options = noanonymous" >> /etc/postfix/main.cf sed -i 's/#local_recipient_maps = unix:passwd.byname $alias_maps/local_recipient_maps = unix:passwd.byname $alias_maps/g' /etc/postfix/main.cf sed -i 's/#mynetworks_style = host/mynetworks_style = host/g' /etc/postfix/main.cf # Create encrypted password file touch /etc/postfix/sasl_passwd if [ "$useprovider" = "gmail" ] ; then echo "[smtp.gmail.com]:587 $sendingemail:$emailpassword" > /etc/postfix/sasl_passwd fi if [ "$useprovider" = "yahoo" ] ; then echo "[smtp.mail.yahoo.com]:587 $sendingemail:$emailpassword" > /etc/postfix/sasl_passwd fi chmod 400 /etc/postfix/sasl_passwd postmap /etc/postfix/sasl_passwd rm -f /etc/postfix/sasl_passwd cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | tee -a /etc/postfix/cacert.pem # Create Fetchmailrc file echo "set postmaster $username" > /home/$username/.fetchmailrc if [ "$useprovider" = "gmail" ] ; then echo "poll pop.gmail.com with proto pop3" >> /home/$username/.fetchmailrc fi # if [ "$useprovider" = "yahoo" ] ; then echo "poll plus.pop.mail.yahoo.com with proto pop3" >> /home/$username/.fetchmailrc fi # echo "user \"$sendingemail\"" there with password \"$emailpassword\"" is \"$username\"" here ssl nokeep"" >> /home/$username/.fetchmailrc echo "mda '/usr/bin/procmail -d %T'" >> /home/$username/.fetchmailrc chown $username:$username /home/$username/.fetchmailrc chmod 600 /home/$username/.fetchmailrc # Create Procmailrc file echo "# .procmailrc" > /home/$username/.procmailrc echo "PATH=/usr/bin:/usr/local/bin" >> /home/$username/.procmailrc echo "MAILDIR=/dev/null" >> /home/$username/.procmailrc echo "DEFAULT=/dev/null" >> /home/$username/.procmailrc echo "LOGFILE=/home/$username/procmail.log" >> /home/$username/.procmailrc echo "SHELL=/bin/sh" >> /home/$username/.procmailrc echo "COMSAT=no" >> /home/$username/.procmailrc echo "#Troubleshooting:" >> /home/$username/.procmailrc echo "VERBOSE=no" >> /home/$username/.procmailrc echo "# LOGABSTRACT=all" >> /home/$username/.procmailrc echo "#" >> /home/$username/.procmailrc echo "# Match in the body - rpt fun commands" >> /home/$username/.procmailrc echo ":0Bb" >> /home/$username/.procmailrc echo "* ^do\/.* rpt fun.*" >> /home/$username/.procmailrc echo "| /home/$username/emailasterisk" '"$MATCH"' >> /home/$username/.procmailrc echo "/dev/null" >> /home/$username/.procmailrc # echo "#" >> /home/$username/.procmailrc echo "# Match in the body - who_is command" >> /home/$username/.procmailrc echo ":0Bb" >> /home/$username/.procmailrc echo "* ^who\/.* *" >> /home/$username/.procmailrc echo "| /home/$username/who_is_helper" '"$MATCH"' >> /home/$username/.procmailrc echo "/dev/null" >> /home/$username/.procmailrc # echo "#" >> /home/$username/.procmailrc echo "# Match in the body - text commands" >> /home/$username/.procmailrc echo ":0Bb" >> /home/$username/.procmailrc echo "* ^text\/.* *" >> /home/$username/.procmailrc echo "| /home/$username/ast_text" '"$MATCH"' >> /home/$username/.procmailrc echo "/dev/null" >> /home/$username/.procmailrc # echo "#" >> /home/$username/.procmailrc echo "# Match in the body - conn commands" >> /home/$username/.procmailrc echo ":0Bb" >> /home/$username/.procmailrc echo "* ^conn\/.* *" >> /home/$username/.procmailrc echo "| /home/$username/conn_helper" '"$MATCH"' >> /home/$username/.procmailrc echo "/dev/null" >> /home/$username/.procmailrc # chown $username:$username /home/$username/.procmailrc chmod 644 /home/$username/.procmailrc # # Create forward file echo "| /usr/local/bin/procmail -t" > /home/$username/.forward chown $username:$username /home/$username/.forward chmod 644 /home/$username/.forward # Disable sendmail and start postfix service sendmail stop update-rc.d -f sendmail remove service postfix restart # add cron job echo "*/5 07-23 * * * root su -c fetchmail $username > /dev/null 2>&1" >> /etc/crontab service crond restart # echo "$username ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers sed -i 's/Defaults requiretty/#Defaults requiretty/g' /etc/sudoers # Create resolv.conf for Postfix echo "nameserver 8.8.8.8" > /var/spool/postfix/etc/resolv.conf echo "" echo "The scripts are now installed. Outbound and inbound email should now be working for" echo "email account $sendingemail" echo "" echo "By default, email will be checked every 5 minutes between 7am and Midnight. You can change this in /etc/crontab" echo "" echo "" # done exit 0 fi