#!/bin/bash

# Travelin' Man 4 (c) Copyright 2005-2014 Ward Mundy & Associates LLC. All rights reserved.
#                    Travelin' Man 4 is licensed under the GPL2 license
#
#  For a copy of license, visit http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
#  A copy of the license should also be available in /root/tm4/COPYING for your review.
#
#   For additional information, contact us: http://pbxinaflash.com/about/comment.php

# Unless required by applicable law or agreed to in writing, software distributed under this License 
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
# See the License for the specific language governing permissions and limitations under the License.

# BY USING TRAVELIN' MAN 4, YOU AGREE TO ASSUME ALL RESPONSIBILITY
# FOR USE OF THE PROGRAMS INCLUDED IN THIS INSTALLATION. NO WARRANTIES
# EXPRESS OR IMPLIED INCLUDING MERCHANTABILITY AND FITNESS FOR PARTICULAR
# USE ARE PROVIDED. YOU ASSUME ALL RISKS KNOWN AND UNKNOWN AND AGREE TO
# HOLD WARD MUNDY, WARD MUNDY & ASSOCIATES LLC, NERD VITTLES, AND THE PBX
# IN A FLASH DEVELOPMENT TEAM HARMLESS FROM ANY AND ALL LOSS OR DAMAGE
# WHICH RESULTS FROM YOUR USE OF THIS SOFTWARE. AS CONFIGURED, THIS
# SOFTWARE CANNOT BE USED TO MAKE 911 CALLS, AND YOU AGREE TO PROVIDE
# AN ALTERNATE PHONE CAPABLE OF MAKING EMERGENCY CALLS. IF ANY OF THESE TERMS
# AND CONDITIONS ARE RULED TO BE UNENFORCEABLE, YOU AGREE TO ACCEPT ONE
# DOLLAR IN U.S. CURRENCY AS COMPENSATORY AND PUNITIVE LIQUIDATED DAMAGES
# FOR ANY AND ALL CLAIMS YOU AND ANY USERS OF THIS SOFTWARE MIGHT HAVE.


version="4.0.1"


# --- MAKE CHANGES BELOW THIS LINE AT YOUR PERIL ---

DIR=/etc/asterisk/tm4

if [ "$(ls -A $DIR)" ]; then
 lowest=12345
 highest=34567
 tmpfile="/tmp/$[ ( $RANDOM % ( $[ $highest - $lowest ] + 1 ) ) + $lowest ]"
 FILES=/etc/asterisk/tm4/*

# Figure out MySQL password and IPtables location for operating system
 OS=`cat /proc/version | grep "Red Hat"`
 if [ -z "$OS" ]
 then
   OS=`cat /proc/version | grep "Ubuntu"`
   if [ -z "$OS" ]; then
    PW=raspberry
    IPT=/etc/iptables/rules.v4
    RESTART=/usr/bin
   else
    PW=passw0rd
    IPT=/etc/iptables/rules.v4
    RESTART=/usr/local/sbin
   fi
 else
  PW=passw0rd
  IPT=/etc/sysconfig/iptables
  RESTART=/usr/local/sbin
 fi

service[0]="ALL Services"
service[1]="SIP (UDP)"
service[2]="SIP (TCP)"
service[3]="IAX"
service[4]="Web"
service[5]="WebMin"
service[6]="FTP"
service[7]="TFTP"
service[8]="SSH"
service[9]="FOP"


 for f in $FILES
 do
  echo "Processing $f file..."
#  take action on each file. $f stores current file name
#  trim to just filename
  f=`echo $f | cut -f 5 -d "/"`
#  get email address and acctname from MySQL
  EMAIL=`mysql -B -uroot -p$PW TravMan4 --disable-column-names -e "select email from Accounts where acctno=$f"`
  ACCTNAME=`mysql -B -uroot -p$PW TravMan4 --disable-column-names -e "select acctname from Accounts where acctno=$f"`
#  grab the ip address from the file here and save to $IPADDR
  MYIPADDR=`cat $DIR/$f`
#  convert * to periods
  IPADDR=`echo ${MYIPADDR//\*/.}`
#  check to be sure it's a proper IP address syntax
  dotlist="${IPADDR//[^.]}"
  dots=${#dotlist}
  if [ "$dots" != "3" ]; then
   rm -f $DIR/$f
   echo "Travelin' Man Account $f for IP address $IPADDR failed. Invalid IP address. Please try again." | mail -s "Account Activation FAILED" $EMAIL
   exit 0
  fi
#  grab the permissions from MySQL DB
  PERMISSIONS=`mysql -B -uroot -p$PW TravMan4 --disable-column-names -e "select permissions from Accounts where acctno=$f"`

#  process the permissions selection and create the temp file for iptables

iptest=`echo ${MYIPADDR//\*/.}`
entry[0]="-A INPUT -s $iptest -j ACCEPT"
entry[1]="-A INPUT -p udp -m udp -s $iptest --dport 5060:5069 -j ACCEPT"
entry[2]="-A INPUT -p tcp -m tcp -s $iptest --dport 5060:5069 -j ACCEPT"
entry[3]="-A INPUT -p udp -m udp -s $iptest --dport 4569 -j ACCEPT"
entry[4]="-A INPUT -p tcp -m multiport -s $iptest --dports 80,9080 -j ACCEPT"
entry[5]="-A INPUT -p tcp -m tcp -s $iptest --dport 9001 -j ACCEPT"
entry[6]="-A INPUT -p tcp -m tcp -s $iptest --dport 21 -j ACCEPT"
entry[7]="-A INPUT -p udp -m udp -s $iptest --dport 69 -j ACCEPT"
entry[8]="-A INPUT -p tcp -m multiport -s $iptest --dports 22,9022 -j ACCEPT"
entry[9]="-A INPUT -p tcp -m tcp -s $iptest --dport 4445 -j ACCEPT"

foo=$PERMISSIONS
bar=(`echo $foo | tr ',' ' '`)
LEN=${#bar[@]}
#echo "The following services have been enabled for $iptest:"
 echo "# // New entry for $f -- $ACCTNAME" > $tmpfile
for (( i=0; i<${LEN}; i++ ));
do
  option=${bar[$i]}
  if [ ! -z "${service[$option]}" ]; then
#   echo ${service[$option]}
    echo ${entry[$option]} >> $tmpfile
  fi
done
echo "# // End entry for $f" >> $tmpfile

#  remove any existing IP tables entry
  sed -i '\:// New entry for '$f':,\:// End entry for '$f':d' $IPT
#  add new IP tables entry from $tmpfile
  sed -i '/# End of Trusted Provider Section/r '$tmpfile'' $IPT
#  remove blank lines
  sed -i '/^\s*$/d' $IPT
#  update MySQL account with new IP address
  mysql -uroot -p$PW TravMan4 <<EOF
  use TravMan4;
  update Accounts set ipaddress="$IPADDR" where acctno=$f limit 1;
EOF
#remove file stub and tempfile
  rm -f $DIR/$f
  rm $tmpfile
#send email notification
  echo "Travelin' Man Account $f ($ACCTNAME) for IP address $IPADDR has been activated as requested." | mail -s "Account Activated" $EMAIL
 done
else
 exit
fi
#restart IPtables
 $RESTART/iptables-restart
# end of tm4-update script
