#  wrt-autoinet.sh - automatic internet access point discovery with NAT
#  version 0.1
#  
#  Copyright (c) 2005 Joseph Battaglia <sephail@sephail.net>
#  
#  Permission is hereby granted, free of charge, to any person obtaining a copy
#  of this software and associated documentation files (the "Software"), to
#  deal in the Software without restriction, including without limitation the
#  rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
#  sell copies of the Software, and to permit persons to whom the Software is
#  furnished to do so, subject to the following conditions:
#  
#  The above copyright notice and this permission notice shall be included in
#  all copies or substantial portions of the Software.
#  
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
#  IN THE SOFTWARE.
#  

#initial delay
sleep 10
echo INIT
date -u

### configuration ###
# default scan time - timeout (seconds)
TIMEOUT=3
# external host to ping test for internet access
INET_HOST="193.2.1.66" # arnes nameserver
# executables
BRCTL=/usr/sbin/brctl
BUSYBOX=/bin/busybox
IFCONFIG=/sbin/ifconfig
IPTABLES=/usr/sbin/iptables
IWCONFIG=/sbin/iwconfig
NVRAM=/usr/sbin/nvram
WL=/usr/sbin/wl
# interfaces
BRIDGE="br0"
LAN=$($NVRAM get lan_ifname)
WLIF=$($NVRAM get wl0_ifname)
### end configuration ###

# set dmz led state
#     arg: 0=off, 1=on
dmz_led() {
  echo $1 > /proc/diag/led/power
}

# associate with a network
#     arg: ssid of network to connect to
#  return: 1=failure, 0=success
w_associate() {
  echo -n "associating with $1: "
  $WL join "$1"
  t_start=`$BUSYBOX date +%s`
  while [ $(( $t_start + $TIMEOUT )) -gt `$BUSYBOX date +%s` ]; do
    if $WL bssid 2>/dev/null; then
      return 0
    fi
  done
  echo "failed"
  return 1
}

# obtain ip address with dhcp
#  return: 1=failure, 0=success
w_dhcp() {
  echo -n "obtaining ip address: "
  if $BUSYBOX udhcpc -i $WLIF -n -q > /dev/null 2>&1; then
    $IFCONFIG $WLIF | $BUSYBOX sed "/inet addr:/!d;s/\(.*\)addr:\(.*\)Bcast\(.*\)/\2/;s/ //g"
    return 0
  fi
  echo "failed"
  return 1
}

# determine whether internet access is present
#  return: 1=failure, 0=success
w_hasinet() {
  echo -n "checking for internet access: "
  wl rssi
  if curl -sm 3 http://193.2.132.73/~luka/etph.php?ch=`date -u +%D\;%TZ\;` | head -1 > /tmp/etph.txt
  then    
#  if $BUSYBOX ping -c 1 $INET_HOST > /dev/null; then

    if grep "re " /tmp/etph.txt
    then
      echo "present"
      echo b 1 > /dev/usb/tts/1
      return 0
    fi	
  fi
  echo "not present"
  echo b 0 > /dev/usb/tts/1
  return 1
}

# initilize wl state
w_init() {
  echo -n "deleting $WLIF from bridge $BRIDGE..."
  $BRCTL delif $BRIDGE $WLIF > /dev/null 2>&1
  echo
  echo -n "killing udhcpc and setting wl parameters..."
  $BUSYBOX killall -9 udhcpc > /dev/null 2>&1
  $WL ap 0 
  $WL monitor 0
  $WL passive 0
  $WL promisc 0
  $IFCONFIG $WLIF down
  $BUSYBOX sleep 1
  $IFCONFIG $WLIF up
  echo
  echo -n "setting up nat..."
  $IPTABLES -F
  $IPTABLES -t nat -F
  $IPTABLES -X
  $IPTABLES -A FORWARD -i $WLIF -o $WLIF -j REJECT
  $IPTABLES -A FORWARD -i $LAN -j ACCEPT
  $IPTABLES -t nat -A POSTROUTING -o $WLIF -j MASQUERADE
  $BUSYBOX sysctl -w net.ipv4.ip_forward=1 > /dev/null
  echo
}

# main connection function
conn_ap() {
  echo -n "scanning for access points..."
  $WL scan
  $BUSYBOX sleep $TIMEOUT
  echo
  $WL scanresults | tee /tmp/scanresults.log
#ignore WEP capable access points
  for i in `cat /tmp/scanresults.log | $BUSYBOX sed "/^SSID:/!d;N;N;/WEP/d;s/SSID: //g;s/\"//g;s/\n.*\n.*//"`; do
#led off
  date -u
  
	echo blink red
    echo b 0 > /dev/usb/tts/1
   
    if ! w_associate "$i"; then
      continue
    fi

	echo blink blue
    echo b 2 > /dev/usb/tts/1
	 
    if ! w_dhcp; then
      continue
    fi

	echo blink green
    echo b 1  > /dev/usb/tts/1

    if w_hasinet; then
      #dmz_led 1
      return 0
    fi
  done
  return 1
}

w_init

while true; do
  date -u
  if ! w_hasinet; then
    #dmz_led 0
    #led off
    echo w 0 0 > /dev/usb/tts/1
    echo w 1 0 > /dev/usb/tts/1
    echo w 2 0 > /dev/usb/tts/1
                  
    while true; do
      if conn_ap; then
      #green led
      echo w 0 0 > /dev/usb/tts/1
      echo w 1 1 > /dev/usb/tts/1
      echo w 2 0 > /dev/usb/tts/1
      /usr/sbin/ntpclient -l -h ntp1.arnes.si -c 1 -i 1 -s
    if  (ps ax | grep rsyn[c]) then
       echo Already running sync.
    else
       echo Running sync.
      chroot /opt/debian /home/frida/frida-sync
    fi
                                            
        break;
      fi
    done
  fi
  $BUSYBOX sleep 5
  date -u
done
