From 20b1c6c2158fb12b6422c170d4a2a0f402864308 Mon Sep 17 00:00:00 2001 From: makefu Date: Mon, 10 Jun 2013 20:30:41 +0200 Subject: finish refactoring new wpa_supplicant autowifi --- new/autowifi | 212 --------------------------------- usr/bin/autowifi | 197 +++++++++++++++++++++--------- usr/bin/autowifi_old | 65 ++++++++++ usr/lib/autowifi/lib/core | 20 ++++ usr/lib/autowifi/lib/network | 21 ++++ usr/lib/autowifi/lib/plugin_core | 4 +- usr/lib/autowifi/lib/wpa_supplicant | 56 +++++++++ usr/lib/autowifi/lib/wps | 19 +-- usr/lib/autowifi/plugins/00profile | 2 +- usr/lib/autowifi/plugins/01open | 4 +- usr/lib/autowifi/plugins/30default_wps | 13 +- 11 files changed, 333 insertions(+), 280 deletions(-) delete mode 100755 new/autowifi mode change 100755 => 100644 usr/bin/autowifi create mode 100755 usr/bin/autowifi_old create mode 100644 usr/lib/autowifi/lib/core create mode 100644 usr/lib/autowifi/lib/wpa_supplicant diff --git a/new/autowifi b/new/autowifi deleted file mode 100755 index c954106f..00000000 --- a/new/autowifi +++ /dev/null @@ -1,212 +0,0 @@ -#!/bin/sh -x - -confdir=${confdir:-"$(dirname $0)/confdir"} -interface="wlan0" - -exists() { type "$1" >/dev/null 2>/dev/null; } - -start_wpa_supplicant(){ - killall wpa_supplicant - sleep 1 -cat>wpa.conf< $confdir/wifi_stats2 - mv $confdir/wifi_stats2 $confdir/wifi_stats - return 0 - fi - return 1 - -} -print_iwlist_env(){ - # takes environment: - # MAC - # FREQ - # QUALITY - # ENCRYPTION - # ESSID - for i in MAC FREQ QUALITY ENCRYPTION ESSID;do - eval echo ${i}_${count}=\\\"\$"${i}"\\\" - done -} - -iwlist_scan(){ - # usage: iwlist_scan $wifi-itf - - count=0 - wpa_cli scan >/dev/null - sleep 10 - - wpa_cli scan_results 2>/dev/null | grep -E "^??:" | sed 's/ / /g' | (while IFS=' ' read MAC FREQ QUALITY ENCRYPTION ESSID - do - : $((count+=1)) - print_iwlist_env - - done; echo WIFI_COUNT=$count) -} - -find_count_of_ssid(){ - c=0 - for i in `seq 1 $WIFI_COUNT`; do - eval SSID=\${ESSID_${i}} - if [ "$SSID" = $1 ]; then - c+=1 - echo $i - fi - done - if [ $c -eq 0 ];then - exit 1 - fi - exit 0 -} - -find_unscanned_networks(){ - for i in `seq 1 $WIFI_COUNT`; do - eval SSID=\${ESSID_${i}} - eval MAC=\${MAC_${i}} - cat $confdir/wifi_stats 2>/dev/null | (while IFS='|' read SSID MAC BANDW KEY; do - if [ "$1" = "$SSID" -a "$2" = "$MAC" ]; then - continue - fi - done; echo $i) - done - exit 0 -} - -connect_to_network_by_ssid(){ - find_count_of_ssid "$1" | (while read i - do - loop_over_cracks $i - done;exit 1) - if [ $? -eq 0 ]; then - exit 0 - fi - echo "no network found :(" - exit 1 -} - -connect_with_pw(){ - find_count_of_ssid "$1" | (while read i - do - KEY="$2" - eval connect \"\${MAC_${i}}\" \"\${ESSID_${i}}\" \${ENCRYPTION_${i}} \"\${KEY}\" - if [ $? -eq 0 ]; then - exit 0 - fi - done;exit 1) -} - -loop_over_networks(){ - for i in `seq 1 $WIFI_COUNT`; do - loop_over_cracks $i - if [ $? -eq 0 ]; then - exit 0 - fi - done -} - -loop_over_cracks(){ - i=$1 - KEY='' - for crack in $(find $confdir/cracks -type f | sort -u); do - KEY="$(eval root=$confdir \$crack \"\${ESSID_${i}}\" \"\${MAC_${i}}\" \${FREQ_${i}} \${ENCRYPTION_${i}})" - if [ $? -eq 0 ]; then - eval connect \"\${MAC_${i}}\" \"\${ESSID_${i}}\" \${ENCRYPTION_${i}} \"\${KEY}\" - if [ $? -eq 0 ]; then - return 0 - fi - fi - done - return 1 -} - -scan_all(){ - for i in `seq 1 $WIFI_COUNT`; do - loop_over_cracks $i - done -} - -scan_unscanned(){ - find_unscanned_networks | (while read i - do - loop_over_cracks $i - done) -} - - -check_gateway(){ - echo ping -c 1 -w 5 $(ip route | grep $interface | awk '/default/{print $3}') -} - -check_internet(){ - ping -c 1 -w 5 8.8.8.8 -} - -check_bandwidth(){ - echo $(curl ftp://ftp.microsoft.com/Products/mspress/library/ANIMAT.ZIP -w "%{speed_download}" -o /dev/null 2>/dev/null | sed 's/\..*//') -} - -start_wpa_supplicant -iwlist_scan > /tmp/$interface.scan -. /tmp/$interface.scan -if [ -n "$2" ]; then - echo connecting to $1 with pw $2 - connect_with_pw "$1" "$2" -elif [ -n "$1" ]; then - echo connecting to $1 - connect_to_network_by_ssid "$1" -else - echo looping network now - loop_over_networks -fi diff --git a/usr/bin/autowifi b/usr/bin/autowifi old mode 100755 new mode 100644 index 55eaefcd..792c78b8 --- a/usr/bin/autowifi +++ b/usr/bin/autowifi @@ -1,65 +1,154 @@ #!/bin/sh -x -# States (LED): -# Blinking - running wifi plugins -# Turned off - connected to wifi -# Turned on - waiting for next scan round - -wifi=wlan0 -iface=@wifi-iface[0] -radio=$(uci get wireless.${iface}.device) - -# for connect_wifi -. /usr/lib/autowifi/lib/openwrt - -# for iwlist_scan -. /usr/lib/autowifi/lib/iwlist - -# for check_internet and check_gateway -. /usr/lib/autowifi/lib/network - -crack_wifi(){ - #SSID MAC CHANNEL ENCRYPTION WPA WPA2 - all_led timer - if [ "$4" == off ];then - encr=open - elif [ "$6" -eq 1 ]; then - encr=psk2 - elif [ "$5" -eq 1 ]; then - encr=psk - elif [ "$4" == on ]; then - encr=wep +cd $(dirname $(readlink -f $0)) + + +interface=${interface:-wlan0} +root=${root:-../../} +crackdir=$root/usr/lib/autowifi/plugins +wifi_keys=$root/etc/autowifi/wifi_keys + +# exists() run_hooks() +. $root/usr/lib/autowifi/lib/core + + +. $root/usr/lib/autowifi/lib/network + +# start_wpa_supplicant() +. $root/usr/lib/autowifi/lib/wpa_supplicant + + + +connect(){ + #mac ssid encryption key + + run_hooks interface pre + run_hooks profile pre + + connect_wifi "$@" + + ip_start dhcp + + if check_gateway && check_internet; then + echo yay internet >&2 + + run_hooks interface post + run_hooks profile post + + write_profile "$@" + return 0 fi - for hack in $(find /usr/lib/autowifi/plugins -type f); do - key=$($hack "$@"); - ret=$? - if [ $ret -eq 0 ];then - connect_wifi "$3" "$1" $encr "$key" - sleep 20 - if check_gateway; then - (cat /etc/autowifi/wifi_keys | grep -v "$1|$2|" ; echo "$1|$2|$key" ) | sort | uniq > /etc/autowifi/wifi_keys2 - mv /etc/autowifi/wifi_keys2 /etc/autowifi/wifi_keys - echo "yay gateway" - check_internet && all_led none && return 0 - fi + return 1 +} +write_profile(){ + bandw=$(check_bandwidth) + (cat $wifi_keys | grep -v "|$1|" ; echo "$2|$1|$bandw|$4" ) | sort | uniq > "${wifi_keys}2" + mv "${wifi_keys}2" "$wifi_keys" +} + + +find_count_of_ssid(){ + c=0 + for i in `seq 1 $WIFI_COUNT`; do + eval SSID=\${ESSID_${i}} + if [ "$SSID" = "$1" ]; then + c+=1 + echo "$i" fi done - return 1 + if [ $c -eq 0 ];then + exit 1 + fi + exit 0 +} + + +connect_to_network_by_ssid(){ + find_count_of_ssid "$1" | (while read i + do + loop_over_cracks $i + done;exit 1) + if [ $? -eq 0 ]; then + exit 0 + fi + echo "no network found :(" + exit 1 } + +connect_with_pw(){ + find_count_of_ssid "$1" | (while read i + do + KEY="$2" + eval connect \"\${MAC_${i}}\" \"\${ESSID_${i}}\" \${ENCRYPTION_${i}} \"\${KEY}\" + if [ $? -eq 0 ]; then + exit 0 + fi + done;exit 1) +} + loop_over_networks(){ - . /tmp/${wifi}.scan for i in `seq 1 $WIFI_COUNT`; do - eval grep -q \${MAC_${i}} /etc/autowifi/blacklist && continue - eval crack_wifi \"\${ESSID_${i}}\" \"\${MAC_${i}}\" \${CHANNEL_${i}} \${ENCRYPTION_${i}} \${WPA_${i}} \${WPA2_${i}} && break + loop_over_cracks $i + if [ $? -eq 0 ]; then + exit 0 + fi done } -iwlist_scan > /tmp/${wifi}.scan -loop_over_networks +loop_over_cracks(){ + i=$1 + KEY='' + for crack in $(find $crackdir -type f | sort -u); do + KEY="$(eval root=$root \$crack \"\${ESSID_${i}}\" \"\${MAC_${i}}\" \${FREQ_${i}} \${ENCRYPTION_${i}})" + if [ $? -eq 0 ]; then + eval connect \"\${MAC_${i}}\" \"\${ESSID_${i}}\" \${ENCRYPTION_${i}} \"\${KEY}\" + if [ $? -eq 0 ]; then + return 0 + fi + fi + done + return 1 +} -while sleep 60; do - if ! check_internet; then - all_led on - iwlist_scan > /tmp/${wifi}.scan - loop_over_networks - fi -done +#scan_all(){ +# for i in `seq 1 $WIFI_COUNT`; do +# loop_over_cracks $i +# done +#} +# +#scan_unscanned(){ +# find_unscanned_networks | (while read i +# do +# loop_over_cracks $i +# done) +#} + +#find_unscanned_networks(){ +# #TODO broken +# for i in `seq 1 $WIFI_COUNT`; do +# eval SSID=\${ESSID_${i}} +# eval MAC=\${MAC_${i}} +# cat $wifi_stats 2>/dev/null | (while IFS='|' read SSID MAC BANDW KEY; do +# if [ "$1" = "$SSID" -a "$2" = "$MAC" ]; then +# continue +# fi +# done; echo $i) +# done +# exit 0 +#} + + + + +start_wpa_supplicant /tmp/autowifi.wpa_supplicant +wifi_scan > /tmp/${interface}.scan +. /tmp/${interface}.scan +if [ -n "$2" ]; then + echo connecting to $1 with pw $2 + connect_with_pw "$1" "$2" +elif [ -n "$1" ]; then + echo connecting to $1 + connect_to_network_by_ssid "$1" +else + echo looping network now + loop_over_networks +fi diff --git a/usr/bin/autowifi_old b/usr/bin/autowifi_old new file mode 100755 index 00000000..55eaefcd --- /dev/null +++ b/usr/bin/autowifi_old @@ -0,0 +1,65 @@ +#!/bin/sh -x +# States (LED): +# Blinking - running wifi plugins +# Turned off - connected to wifi +# Turned on - waiting for next scan round + +wifi=wlan0 +iface=@wifi-iface[0] +radio=$(uci get wireless.${iface}.device) + +# for connect_wifi +. /usr/lib/autowifi/lib/openwrt + +# for iwlist_scan +. /usr/lib/autowifi/lib/iwlist + +# for check_internet and check_gateway +. /usr/lib/autowifi/lib/network + +crack_wifi(){ + #SSID MAC CHANNEL ENCRYPTION WPA WPA2 + all_led timer + if [ "$4" == off ];then + encr=open + elif [ "$6" -eq 1 ]; then + encr=psk2 + elif [ "$5" -eq 1 ]; then + encr=psk + elif [ "$4" == on ]; then + encr=wep + fi + for hack in $(find /usr/lib/autowifi/plugins -type f); do + key=$($hack "$@"); + ret=$? + if [ $ret -eq 0 ];then + connect_wifi "$3" "$1" $encr "$key" + sleep 20 + if check_gateway; then + (cat /etc/autowifi/wifi_keys | grep -v "$1|$2|" ; echo "$1|$2|$key" ) | sort | uniq > /etc/autowifi/wifi_keys2 + mv /etc/autowifi/wifi_keys2 /etc/autowifi/wifi_keys + echo "yay gateway" + check_internet && all_led none && return 0 + fi + fi + done + return 1 +} +loop_over_networks(){ + . /tmp/${wifi}.scan + for i in `seq 1 $WIFI_COUNT`; do + eval grep -q \${MAC_${i}} /etc/autowifi/blacklist && continue + eval crack_wifi \"\${ESSID_${i}}\" \"\${MAC_${i}}\" \${CHANNEL_${i}} \${ENCRYPTION_${i}} \${WPA_${i}} \${WPA2_${i}} && break + done +} + +iwlist_scan > /tmp/${wifi}.scan +loop_over_networks + +while sleep 60; do + if ! check_internet; then + all_led on + iwlist_scan > /tmp/${wifi}.scan + loop_over_networks + fi +done diff --git a/usr/lib/autowifi/lib/core b/usr/lib/autowifi/lib/core new file mode 100644 index 00000000..80ae75b4 --- /dev/null +++ b/usr/lib/autowifi/lib/core @@ -0,0 +1,20 @@ +#!/bin/sh + +exists() { type "$1" >/dev/null 2>/dev/null; } + +run_hooks(){ + # (interface|profile) (pre|post) + typ=$1 + action=$2 + shift;shift + : ${interface?please provide interface} + if [ "$typ" = "interface" ];then + path=interface/$interface/$action + else + path=profile/$2/$action + fi + for hook in $(find "$root/etc/autowifi/hooks/$path" -type f 2>/dev/null | sort -u ); do + $hook "$@" + done +} + diff --git a/usr/lib/autowifi/lib/network b/usr/lib/autowifi/lib/network index 1b105f85..fd7e3b5a 100644 --- a/usr/lib/autowifi/lib/network +++ b/usr/lib/autowifi/lib/network @@ -1,4 +1,5 @@ #!/bin/sh + check_gateway(){ ping -c 1 -w 5 $(ip route | awk '/default/{print $3}') } @@ -10,3 +11,23 @@ check_internet(){ return 1 fi } + +check_bandwidth(){ + echo $(curl ftp://ftp.microsoft.com/Products/mspress/library/ANIMAT.ZIP -w "%{speed_download}" -o /dev/null 2>/dev/null | sed 's/\..*//') +} + +ip_start(){ + : ${interface?interface variable not set} ${1?please provide method to start ip} + # usage: method [extra parms] + case "$1" in + dhcp) + if exists dhcpcd; then + dhcpcd -x $interface + dhcpcd -w -A $interface + elif exists dhclient; then + dhclient -x $interface + dhclient $interface + fi ;; + *) echo "do not know ip starter $1";; + esac +} diff --git a/usr/lib/autowifi/lib/plugin_core b/usr/lib/autowifi/lib/plugin_core index 025d9dbd..da003350 100644 --- a/usr/lib/autowifi/lib/plugin_core +++ b/usr/lib/autowifi/lib/plugin_core @@ -1,12 +1,10 @@ parse_plugin_args(){ - [ $# -ne 6 ] && plugin_usage && exit 1 + [ $# -ne 4 ] && plugin_usage && exit 1 # convenience function to put args in ENV variables ESSID="$1" MAC="$2" CHANNEL="$3" ENC="$4" - WPA="$5" - WPA2="$6" if [ ${#MAC} -ne 17 ] ;then echo "MAC malformed" exit 1 diff --git a/usr/lib/autowifi/lib/wpa_supplicant b/usr/lib/autowifi/lib/wpa_supplicant new file mode 100644 index 00000000..28c327e9 --- /dev/null +++ b/usr/lib/autowifi/lib/wpa_supplicant @@ -0,0 +1,56 @@ +#!/bin/sh +start_wpa_supplicant(){ + wpa_conf=${1?please supply wpa_supplicant.conf path} + killall wpa_supplicant + sleep 1 +cat>$wpa_conf</dev/null + sleep 10 + + wpa_cli scan_results 2>/dev/null | grep -E "^??:" | sed 's/ / /g' | (while IFS=' ' read MAC FREQ QUALITY ENCRYPTION ESSID + do + : $((count+=1)) + print_wifi_env + + done; echo WIFI_COUNT=$count) +} + +print_wifi_env(){ + # takes environment: + # MAC + # FREQ + # QUALITY + # ENCRYPTION + # ESSID + for i in MAC FREQ QUALITY ENCRYPTION ESSID;do + eval echo ${i}_${count}=\\\"\$"${i}"\\\" + done +} diff --git a/usr/lib/autowifi/lib/wps b/usr/lib/autowifi/lib/wps index 668bbbff..9fa01801 100644 --- a/usr/lib/autowifi/lib/wps +++ b/usr/lib/autowifi/lib/wps @@ -1,5 +1,8 @@ #!/bin/sh - +has_wps(){ + # the-wpa_supplicant-encryption-string + echo "$1" | grep -q "\[WPS\]" +} try_wps_pin(){ # # ESSID MAC CHANNEL ENC WPA WPA2 PIN @@ -7,14 +10,16 @@ try_wps_pin(){ ESSID="$1" MAC="$2" CHANNEL="$3" + + # TODO refactor to use all the encryption + # the wpa_supplicant encryption string ENC="$4" - WPA="$5" - WPA2="$6" - PIN="$7" - [ "$ENC" == off ] && return 2 - WPA_CONF=/tmp/wpa.conf - WPA_LOG=/tmp/wpa.log + PIN="$5" + + [ "$ENC" = "[ESS]" ] && return 2 + WPA_CONF=/tmp/wpa_trywps.conf + WPA_LOG=/tmp/wpa_trywps.log rm $WPA_LOG #mkfifo $WPA_LOG killall wpa_supplicant 2>/dev/null && sleep 1 diff --git a/usr/lib/autowifi/plugins/00profile b/usr/lib/autowifi/plugins/00profile index fdba2c9e..d7fb9c75 100755 --- a/usr/lib/autowifi/plugins/00profile +++ b/usr/lib/autowifi/plugins/00profile @@ -3,7 +3,7 @@ # ENV: # root (default: /) root=${root:-/} -cat $root/etc/autowifi/wifi_keys 2>/dev/null | (while IFS='|' read SSID MAC KEY; do +cat $root/etc/autowifi/wifi_keys 2>/dev/null | (while IFS='|' read SSID MAC BANDWIDTH KEY; do if [ "$1" == "$SSID" -a "$2" == "$MAC" ]; then echo $KEY exit 0 diff --git a/usr/lib/autowifi/plugins/01open b/usr/lib/autowifi/plugins/01open index f3b9640c..881f47ea 100755 --- a/usr/lib/autowifi/plugins/01open +++ b/usr/lib/autowifi/plugins/01open @@ -1,6 +1,6 @@ #!/bin/sh -#ESSID MAC CHANNEL ENCRYPTION WPA WPA2 -if [ "$4" == "off" ]; then +#ESSID MAC CHANNEL ENCRYPTION +if [ "$4" == "[ESS]" ]; then exit 0 fi exit 1 diff --git a/usr/lib/autowifi/plugins/30default_wps b/usr/lib/autowifi/plugins/30default_wps index 98be6a66..7f66d117 100755 --- a/usr/lib/autowifi/plugins/30default_wps +++ b/usr/lib/autowifi/plugins/30default_wps @@ -3,4 +3,15 @@ # http://www.wotan.cc/?p=75 cd $(dirname $(readlink -f $0)) . ../lib/wps -try_wps_pin "$@" 12345670 +. ../lib/plugin_core + +parse_plugin_args "$@" +DEFAULT_PIN="${DEFAULT_PIN:-12345670}" + +if has_wps "$ENC"; then + echo "trying PIN $DEFAULT_PIN against $ESSID" >&2 + try_wps_pin "$@" $DEFAULT_PIN +else + echo "Network $ESSID not WPS enabled" >&2 + exit 1 +fi -- cgit v1.2.3