#!/bin/bash

# JJs script with DIALOG for Untangle hack installations

# Untangle 7.1 is running on debian 5.0.3 lenny (cat /etc/debian_version)



function install_pyzor {

if [ "$PYZORPING" = "NOT RUNNING" ] ;
   then
       cp -f /etc/apt/sources.list.d/untangle.list /etc/apt/sources.list.d/untangle.list.tmp
     echo "deb http://ftp.us.debian.org/debian/ lenny main contrib non-free " >>/etc/apt/sources.list.d/untangle.list  
   
     apt-get update
      apt-get -y install pyzor
      apt-get -y install gcc g++ make
       mv -f /etc/apt/sources.list.d/untangle.list.tmp /etc/apt/sources.list.d/untangle.list 
      su spamc -c 'pyzor discover'
      sed 's/pyzor_timeout/#pyzor_timeout/g' /etc/spamassassin/local.cf > /etc/spamassassin/local.cf.tmp
      cp -u /etc/spamassassin/local.cf.tmp /etc/spamassassin/local.cf   
      rm /etc/spamassassin/local.cf.tmp
   else
      echo "PYZOR is already running."
fi
   
}




function install_botnet {

if [ "$BOTNETFILES" = "NOT RUNNING" ] ;
   then
     cd /etc/spamassassin
     curl -s -O http://juank.myweb.uga.edu/untangle/Botnet.cf
     curl -s -O http://juank.myweb.uga.edu/untangle/Botnet.pm
     chmod 0444 Botnet.cf
     chmod 0444 Botnet.pm
     # if you don't have EXTERNAL clients sending/relaying email to you from the OUTSIDE, 
     # run the following TWO lines:
     sed 's/#score/score/g' /etc/spamassassin/Botnet.cf > /etc/spamassassin/Botnet.cf.tmp
     cp -u /etc/spamassassin/Botnet.cf.tmp /etc/spamassassin/Botnet.cf  
     rm /etc/spamassassin/Botnet.cf.tmp
  else
     echo "BOTNET already installed" 
fi

}




function install_dcc { 

if [ "$DCCMSG" = "NOT INSTALLED" ] ;
   then
      cd /usr/local/src
      curl -O http://www.rhyolite.com/dcc/source/dcc.tar.Z
      tar xzf dcc.tar.Z
      cd dcc-*
      ./configure --disable-dccm
      make
      make install
      cdcc "delete 127.0.0.1"
      cdcc "delete 127.0.0.1 Greylist"
      ln -s /var/dcc/libexec/cron-dccd /usr/bin/cron-dccd
      ln -s /etc/init.d /etc/rc.d
      echo "0 5 * * * root /usr/bin/cron-dccd" >>/etc/crontab
      echo "dcc_home /var/dcc" >> /etc/spamassassin/local.cf
      sed 's/DCCIFD_ENABLE=off/DCCIFD_ENABLE=on/g' /var/dcc/dcc_conf > /var/dcc/dcc_conf.tmp
      cp -u /var/dcc/dcc_conf.tmp /var/dcc/dcc_conf
      sed 's/#loadplugin Mail::SpamAssassin::Plugin::DC/loadplugin Mail::SpamAssassin::Plugin::DC/g' /etc/spamassassin/v310.pre > /etc/spamassassin/v310.pre.tmp
      cp -u /etc/spamassassin/v310.pre.tmp /etc/spamassassin/v310.pre
      cp /var/dcc/libexec/rcDCC /etc/init.d/DCC
      update-rc.d DCC defaults
      /etc/init.d/DCC start
      cd /tmp
      rm -R /usr/local/src/dcc-*
      rm /usr/local/src/dcc.tar.Z
   else
      echo "DCC is already installed" 
fi

}



function install_sa_stats {

if [ "$SASTATS" = "NOT INSTALLED"  ]  ;
   then
      cd /usr/bin
      curl -O http://juank.myweb.uga.edu/untangle/sa-stats.pl
      chmod 0755 sa-stats.pl
      #sa-stats.pl -f mail.log
      sa-stats.pl -f mail.log -n 1000 -w |grep -v -E Content-type > /usr/share/untangle/web/reports/sa-stats.htm
      echo "19 2 * * * root sa-stats.pl -f mail.log -n 1000 -w |grep -v -E "Content-type" > /usr/share/untangle/web/reports/sa-stats.htm " >>/etc/crontab
   else
      echo "SASTAT are installed on your system "
fi

}



dialog --backtitle "J and J script for Untangle 7.1" --title "JJ script for Untangle" \
       --msgbox "Welcome to our JJ script, Spam module modifications for Untangle, Version 1.2 \
                 Written by Juan Machado and Jason Ellingson " 15 70 \

dialog --backtitle "J and J script for Untangle 7.1" --title "JJ script for Untangle" --title " ... Disclaimer ..."  --yesno "Perform this at your own risk. While we were careful in constructing these scripts, they may not be perfect. The scripts you are going to execute were made so you would only have to hit ENTER. If your Untangle Gremlins leave and your server no longer runs, then you have been warned and we accept NO responsibility. So, are you sure?" 20 40


if [ $? = 1 ]; then
   clear
   echo " ************************************************************" 
   echo " ************************************************************"
   echo " ************************************************************"
   echo " ************************************************************"
   echo " ************************************************************"
   echo " ************************************************************"
   echo " ************************************************************"
   echo "You pressed CANCEL ... Come on! You know you want to do it ..."
   echo " ************************************************************"
   echo " ************************************************************"
   echo " ************************************************************"
   echo " ************************************************************"
   echo " ************************************************************"
   echo " ************************************************************"
   exit 0
fi



dialog --backtitle "J and J script for Untangle 7.1" --title "BOTNET installation option ..."  --yesno "If you don't have PHYSICAL EXTERNAL users (your users) sending/relaying email to you from the OUTSIDE (Which is the RECOMMENDED configuration) INSTALLING Botnet is OK. So, Do you want to install BOTNET ?" 20 40

if [ $? = 1 ]; then
     BOTNET_Install="NO"
 else
     BOTNET_Install="YES"
fi 



{
  while [ true ]
      do

COMPILERMSG="NO"
COMPILERMSG=`dpkg -l | grep gcc | awk '{ print $2 }' | head -1`

if [ "$COMPILERMSG" = "gcc" ] ;
           then
             COMPILERMSG="INSTALLED"
           else
             COMPILERMSG="NOT INSTALLED"
         fi  

 echo 10
   echo "XXX"
   echo "checking if Compiler is installed..."
   echo "XXX"
   sleep 1
echo $COMPILERMSG > COMPILERMSG.txt



 SAVERSION=`spamassassin -V | awk '/SpamAssassin/, / /' | awk '{ print $3 }'`
       # other than 3.2.5?


 echo 30
   echo "XXX"
   echo "checking your version of SpamAssassin..."
   echo "XXX"
   sleep 1
echo $SAVERSION > SAVERSION.txt

PYZORPING="No"
( pyzor ping | gawk '{print substr($3,2,2)}' > PYZORPING.out ) >& PYZORPING.err  

PYZORPING=`cat PYZORPING.out`

if [ "$PYZORPING" = "OK" ] ;
 then
  PYZORPING="IS RUNNING" 
 else
  PYZORPING="NOT RUNNING"
fi
echo $PYZORPING > PYZORPING.txt

PYZORTO=`grep pyzor_timeout /etc/spamassassin/local.cf | awk '{ print $1 }'`

if [ "$PYZORTO" = "#pyzor_timeout" ]
  then
    PYZORMSG="TIMEOUTS are commented in SpamAssassin"
  else
    PYZORMSG="TIMEOUTS are NOT commented in SpamAssassin"
fi

echo $PYZORMSG > PYZORMSG.txt


 echo 50
   echo "XXX"
   echo "checking that PYZOR is installed and running..."
   echo "XXX"
   sleep 1


BOTNETCONF="#score"
BOTNETFILES=0
BOTNETMSG="score NOT added in SpamAssassin"

if [ -e /etc/spamassassin/Botnet.cf ] ;
           then
              BOTNETFILES="RUNNING from /etc/spamassassin directory"
              BOTNETCONF=`grep "#score" /etc/spamassassin/Botnet.cf | awk '{ print $1 }'`
              BOTNETSCORECOUNTS=`grep "#score" /etc/spamassassin/Botnet.cf | awk '{ print $1 }' | wc |  awk '{ print $1 }'`
           else
             BOTNETFILES="NOT RUNNING"
         fi

echo $BOTNETFILES > BOTNETFILES.txt



if [ "$BOTNETCONF" = "#score" ]
  then
    BOTNETMSG="score NOT added in SpamAssassin"
  else
    BOTNETMSG="score added in SpamAssassin"
fi


if [ "$BOTNETSCORECOUNTS" = "0" ]
   then 
     BOTNETMSG="score added in SpamAssassin"
fi

echo $BOTNETMSG > BOTNETMSG.txt

 echo 60
   echo "XXX"
   echo "checking that BOTNET is running/configured in Spamassassin..."
   echo "XXX"
   sleep 1


DCCMSG=$(2>&1 /var/dcc/libexec/dccd -V | awk ' {print $1; exit}')
# latest version as of 08/12/08 is 1.3.92

if [ ! -e /var/dcc/libexec/dccd ]
 then
   DCCMSG="NOT INSTALLED"
fi

echo $DCCMSG > DCCMSG.txt

echo 80
   echo "XXX"
   echo "checking if DCC is installed..."
   echo "XXX"
   sleep 1


if [  -e /usr/bin/sa-stats.pl ]
 then
   SASTATS="INSTALLED"
 else
   SASTATS="NOT INSTALLED"
fi

echo $SASTATS > SASTATS.txt

echo 90
   echo "XXX"
   echo "checking if SA Stats are installed..."
   echo "XXX"
   sleep 1








   break
done
echo; } | dialog --backtitle "J and J script for Untangle 7.1" --title "Pre-flight Analysis of your Untangle box ..." --guage " Installing ... " 10 70 0


COMPILERMSG=`cat COMPILERMSG.txt`
rm COMPILERMSG.txt

SAVERSION=`cat SAVERSION.txt`
rm SAVERSION.txt

PYZORPING=`cat PYZORPING.txt`
rm PYZORPING.txt

PYZORMSG=`cat PYZORMSG.txt`
rm PYZORMSG.txt

BOTNETFILES=`cat BOTNETFILES.txt`
rm BOTNETFILES.txt

BOTNETMSG=`cat BOTNETMSG.txt`
rm BOTNETMSG.txt

DCCMSG=`cat DCCMSG.txt`
rm DCCMSG.txt

SASTATS=`cat SASTATS.txt`
rm SASTATS.txt

rm PYZORPING.err
rm PYZORPING.out

dialog --backtitle "J and J script for Untangle 7.1" --title "JJ script for Untangle" --title "Pre-flight Analysis result of your Untangle box" \
       --msgbox "`echo ; \
                  echo "** A GCC Compiler is $COMPILERMSG."; \
                  echo "** Your current version of SpamAssasin is $SAVERSION."; \
                  echo "**Your PYZOR install is $PYZORPING and $PYZORMSG."; \
                  echo "** BOTNET is $BOTNETFILES and $BOTNETMSG."; \
                  echo "** DCC is $DCCMSG."; \
                  echo "** SAStats are $SASTATS."; \
                  echo "** Decision to install BOTNET is: $BOTNET_Install "; \
                  echo ; \
                  echo ; \
                  echo "        ... Please press ENTER to continue ..." \
      `" \
       20 85

if [ "$BOTNET_Install" = "YES" ] ;
 then
(( install_compiler; upgrade_SA_to_3_2_5; install_pyzor; install_botnet; install_dcc; install_sa_stats ; /etc/init.d/spamassassin restart; \rm OUTPUT.* ; echo -e "\n" ;  echo -e "\n" ; echo -e "\n" ; echo -e "\n" ; echo -e "\n" ; echo -e "\n" ; echo -e "\n" ;  echo "END OF THE SCRIPT... PLEASE PRESS EXIT" ; echo -e "\n" ;  echo -e "\n" ; echo -e "\n" ;) > OUTPUT.txt) >& OUTPUT.err & 
dialog --backtitle "J and J script for Untangle 7.1" --title "Running installation/configuration scripts... DO NOT EXIT" --tailbox OUTPUT.txt 30 70
fi

if [ "$BOTNET_Install" != "YES" ] ;
 then
(( install_compiler; upgrade_SA_to_3_2_5; install_pyzor; install_dcc; install_sa_stats ; /etc/init.d/spamassassin restart;  \rm OUTPUT.* ; echo -e "\n" ;  echo -e "\n" ; echo -e "\n" ; echo -e "\n" ; echo -e "\n" ; echo -e "\n" ; echo -e "\n" ; echo -e "\n" ; echo -e "\n" ; echo -e "\n" ;  echo "*** END OF THE SCRIPT... PLEASE PRESS EXIT ***" ; echo -e "\n" ;  echo -e "\n" ; echo -e "\n" ;) > OUTPUT.txt) >& OUTPUT.err & 
dialog --backtitle "J and J script for Untangle 7.1" --tailbox OUTPUT.txt 25 65
fi


rm OUTPUT.txt
rm OUTPUT.err

dialog --backtitle "J and J script for Untangle 7.1" --title "Installation/Configuration Complete ..."  --yesno "... Now your system is going to start catching more SPAM than ever ... Please remember that your Untangle box will be smarter every day, at least the SPAM filtering. Enjoy it and PLEASE, PLEASE DO post your comments on the forum. Do you want me to RESTART your Untangle (RECOMMENDED)?" 25 65

if [ $? = 1 ]; then
     echo "Please don't forget to REBOOT your Untangle box at your conveniece" 
 else
     reboot 
fi 