
#!/bin/sh -x
# /etc/init.d/config_secn  

# This script creates config files based on parameters in 
# /etc/config/secn, /etc/config/network and /etc/config/wireless
# The files created are:
#  /etc/asterisk/potato.sip.conf  for SIP/VoIP provider details
#  /etc/hostapd.conf  for WPA encryption support
#  /etc/init.d/mesh-bat-adv  for setting up the virtual Access Point


### Set up Asterisk config files
# Remove old files
rm /etc/asterisk/potato.sip.conf >> /dev/null
rm /etc/asterisk/potato.extensions.conf >> /dev/null

# Get the parameters from uci config file /etc/config/secn
ENABLE=`uci get secn.asterisk.enable`
REGISTER=`uci get secn.asterisk.register`
USERNAME=`uci get secn.asterisk.username`
FROMUSERNAME=`uci get secn.asterisk.fromusername`
SECRET=`uci get secn.asterisk.secret`
HOST=`uci get secn.asterisk.host`
REGHOST=`uci get secn.asterisk.reghost`
PROXY=`uci get secn.asterisk.proxy`
CODEC1=`uci get secn.asterisk.codec1`
CODEC2=`uci get secn.asterisk.codec2`
CODEC3=`uci get secn.asterisk.codec3`
DIALOUT=`uci get secn.asterisk.dialout`
ENABLENAT=`uci get secn.asterisk.enablenat`
EXTERNIP=`uci get secn.asterisk.externip`
SOFTPH=`uci get secn.asterisk.softph`

# Get localnet and netmask from br-lan settings
LOCALNET=`uci get network.lan.ipaddr | awk -F. '{print $1"."$2"."$3"."0}'`
LOCALNETMASK=`uci get network.lan.netmask`

if [ $ENABLE = "checked" ]; then
ENFLAG=" "
else
ENFLAG=";"
fi

if [ $REGISTER = "checked" ]; then
REGFLAG=" "
else
REGFLAG=";"
fi

if [ $ENABLENAT = "checked" ]; then
ENABLENATFLAG=" "
else
ENABLENATFLAG=";"
fi

# Check whether device is already at Softphone Master IP address
CURRENTOCTET4=`uci show network.lan.ipaddr | cut -d = -f 2 | cut -d . -f 4`

# If so, set new IP addr to default of .20 in case device is made non-Master
# and the user forgets to change the IP address in the web UI at the same time

if [ $CURRENTOCTET4 = "252" ]; then
OCTET4="20"
else
# Otherwise, use the current IP address
OCTET4="$CURRENTOCTET4"
fi

if [ $SOFTPH = "OFF" ]; then
SOFTPHFLAG=";"
SOFTPHMASTER=";"
SOFTPHCLIENT=";"
fi

if [ $SOFTPH = "CLIENT" ]; then
SOFTPHFLAG=";"
SOFTPHMASTER=";"
SOFTPHCLIENT=" "
fi

if [ $SOFTPH = "MASTER" ]; then
SOFTPHFLAG=" "
SOFTPHMASTER=" "
SOFTPHCLIENT=";"
# Default Softphone Master IP is .252
OCTET4="252"
fi


# Set up the default Softphone Master IP address 
OCTET123=`uci show network.lan.ipaddr | cut -d = -f 2 | cut -d . -f 1,2,3`
# Default Softphone Master IP is .252
SOFTPHMASTERIP=$OCTET123.252
# Set up new device IP
DEVICEIP=$OCTET123.$OCTET4
uci set network.lan.ipaddr=$DEVICEIP
uci commit network


# Create new Asterisk sip.conf include file
# Everything from here to EOF will be written to the file
cat >> /etc/asterisk/potato.sip.conf << EOF

;----------------
; /etc/asterisk/potato.sip.conf'
; CAUTION: This is an automatically generated file
; Do not edit this file manually
; See /etc/config/secn for config parameters

; Configure for NAT if required
$ENABLENATFLAG localnet=$LOCALNET/$LOCALNETMASK                     
$ENABLENATFLAG externip=$EXTERNIP 

; Register to VoIP Provider
$REGFLAG $ENFLAG register => $USERNAME:$SECRET@$REGHOST

[sipaccount]
$ENFLAG host=$HOST
$ENFLAG proxy=$PROXY			;;; Check syntax
$ENFLAG secret=$SECRET
$ENFLAG username=$USERNAME
$ENFLAG fromuser=$USERNAME

insecure=port,invite
type=friend
disallow=all
allow=$CODEC1,$CODEC2,$CODEC3
dtmfmod=rfc2833
qualify=yes
canreinvite=no
nat=yes
context=default

; Softphone extensions
; This include file sets up definitions for using  
; softphone extensions for cell phones or PCs
$SOFTPHFLAG #include softphone.sip.conf

;----------------
EOF

#-----------------------------------------------------------

# Create new extensions.conf include file
# Everything from here to EOF will be written to the file
cat >> /etc/asterisk/potato.extensions.conf << EOF

;-------------------------------------
; /etc/asterisk/potato.extensions.conf
; ------------------------------------ 
; Dialplan to support SIP client functionality
; CAUTION: This is an automatically generated file
; Do not edit this file manually
; See /etc/config/secn for config parameters

; Send incoming calls to the MP                   
exten => s,1,Dial(MP/1) 
; Make external SIP / VoIP calls using [sipaccount] details
; Dial # for access, and then required number string
exten => _$DIALOUT.,1,Dial(SIP/\${EXTEN:1}@sipaccount,120,r)

; Softphone extensions mode
; This sets up definitions for using softphone extensions for 
; cell phones or PCs. 
; Device is Master (registrar) or Client 

; For the Master device
$SOFTPHMASTER exten => _3XX,1,Dial(SIP/softph\${EXTEN})

; For the Client device
$SOFTPHCLIENT exten => _3XX,1,Dial(SIP/\${EXTEN}@$SOFTPHMASTERIP, 120, r)

;-------------------------------
EOF

# ------------------------------------------------------------------
### Set up WiFi Access-Point encryption  

# Get the parameters from uci config file /etc/config/secn and wireless
SSID=`uci get secn.accesspoint.ssid`
PASSPHRASE=`uci get secn.accesspoint.passphrase`
ENCRYPTION=`uci get secn.accesspoint.encryption`
AP_ENABLE=`uci get secn.accesspoint.ap_enable`

# Set to WPA1 by default                                 
ENCRYPT="psk"

if [ $ENCRYPTION = "WPA2" ]; then                                   
ENCRYPT="psk2"
fi

if [ $ENCRYPTION = "WEP" ]; then                          
ENCRYPT="wep"                                                     
fi

if [ $ENCRYPTION = "NONE" ]; then                          
ENCRYPT="none"                                                      
fi

if [ $AP_ENABLE = "checked" ]; then                                   
AP_DISABLE="0"
else
AP_DISABLE="1"
fi

uci set wireless.@wifi-iface[0].encryption=$ENCRYPT
uci set wireless.@wifi-iface[0].key=$PASSPHRASE
uci set wireless.@wifi-iface[0].ssid=$SSID
uci set wireless.@wifi-iface[0].mode="ap"
uci set wireless.@wifi-iface[0].disabled="$AP_DISABLE"

uci commit wireless


#------------------------------------------------------------------

# Set up DHCP server
# Get the DHCP parameters from uci config file /etc/config/secn

DHCP_ENABLE=`uci get secn.dhcp.enable`
STARTIP=`uci get secn.dhcp.startip | cut -d = -f 2 | cut -d . -f 4`
ENDIP=`uci get secn.dhcp.endip | cut -d = -f 2 | cut -d . -f 4`
ENDIP=$((ENDIP - STARTIP))
MAXLEASES=`uci get secn.dhcp.maxleases`
LEASETERM=`uci get secn.dhcp.leaseterm`
LEASETERM=$((LEASETERM / 60))'m'
DOMAIN=`uci get secn.dhcp.domain`
OPTION_DNS=`uci get secn.dhcp.dns`
OPTION_SUBNET=`uci get secn.dhcp.subnet`
OPTION_ROUTER=`uci get secn.dhcp.router`

uci set dhcp.lan.start=$STARTIP
uci set dhcp.lan.limit=$ENDIP
uci set dhcp.lan.leasetime=$LEASETERM
uci set dhcp.lan.dhcp_option="3,$OPTION_ROUTER"
uci set dhcp.@dnsmasq[0].dhcpleasemax=$MAXLEASES
uci set dhcp.@dnsmasq[0].domain=$DOMAIN

uci commit dhcp
sleep 1

if [ $DHCP_ENABLE = "checked" ]; then

  # Enable start on boot
  ln -s ../init.d/dnsmasq /etc/rc.d/S60dnsmasq

  # Stop dnsmasq now in case settings have changed
  /etc/init.d/dnsmasq stop
  # Start dnsmasq now
  /etc/init.d/dnsmasq start

else

  #Disable start on boot
  rm -f /etc/rc.d/S60dnsmasq
                
  # Stop dnsmasq now
  /etc/init.d/dnsmasq stop
fi


#----------------------------------------------
# Set up web server security configuration

# Get checkbox values
AUTH=`uci get secn.http.auth`
LIMITIP=`uci get secn.http.limitip`
ENSSL=`uci get secn.http.enssl`

# Set up basic auth
if [ $AUTH = "checked" ]; then                          
  uci set uhttpd.main.config="/etc/http.conf"
else
  uci set uhttpd.main.config="/etc/http.conf.off"
fi


# Set up Restricted IP and SSL

if [ $LIMITIP != "checked" ] && [ $ENSSL != "checked" ]; then
  uci set uhttpd.main.listen_http="0.0.0.0:80"
  uci set uhttpd.main.listen_https=""
fi

if [ $LIMITIP = "checked" ] && [ $ENSSL != "checked" ]; then
  uci set uhttpd.main.listen_http="172.31.255.254:80"
  uci set uhttpd.main.listen_https=""
fi

if [ $LIMITIP != "checked" ] && [ $ENSSL = "checked" ]; then
  uci set uhttpd.main.listen_http=""                                                  
  uci set uhttpd.main.listen_https="0.0.0.0:443"
fi

if [ $LIMITIP = "checked" ] && [ $ENSSL = "checked" ]; then
  uci set uhttpd.main.listen_http=""
  uci set uhttpd.main.listen_https="172.31.255.254:443"
fi


# Save the changes 
uci commit uhttpd
sleep 1

# Reboot for changes to be effective

#----------------------------------------------


