From a825e6f30fa7ec5cac0e34dceb6fdec116120092 Mon Sep 17 00:00:00 2001 From: Roy Marples Date: Thu, 25 Oct 2007 09:11:18 +0000 Subject: Format the script correctly, and fix ssids with spaces in hopefully once and for all --- net.BSD/iwconfig.sh | 477 ++++++++++++++++++++++++++-------------------------- 1 file changed, 240 insertions(+), 237 deletions(-) diff --git a/net.BSD/iwconfig.sh b/net.BSD/iwconfig.sh index f1206edf..63e9623a 100644 --- a/net.BSD/iwconfig.sh +++ b/net.BSD/iwconfig.sh @@ -4,227 +4,230 @@ _config_vars="$_config_vars ssid mode associate_timeout preferred_aps blacklist_aps" iwconfig_depend() { - program /sbin/ifconfig - after plug - before interface - provide wireless + program /sbin/ifconfig + after plug + before interface + provide wireless } iwconfig_get_wep_status() { - local status="disabled" - local mode=$(LC_ALL=C ifconfig "${IFACE}" \ - | sed -n -e 's/^[[:space:]]*authmode \([^ ]*\) privacy ON .*/\1/p') - if [ -n "${mode}" ] ; then - status="enabled - ${mode}" - fi - - echo "(WEP ${status})" + local status="disabled" + local mode=$(LC_ALL=C ifconfig "${IFACE}" \ + | sed -n -e 's/^[[:space:]]*authmode \([^ ]*\) privacy ON .*/\1/p') + if [ -n "${mode}" ] ; then + status="enabled - ${mode}" + fi + + echo "(WEP ${status})" } _iwconfig_get() { - LC_ALL=C ifconfig "${IFACE}" | \ - sed -n -e "s/^[[:space:]]*ssid \(.*\) channel \([0-9]*\) bssid \(..:..:..:..:..:..\)\$/\\$1/p" + LC_ALL=C ifconfig "${IFACE}" | \ + sed -n -e "s/^[[:space:]]*ssid \(.*\) channel \([0-9]*\) bssid \(..:..:..:..:..:..\)\$/\\$1/p" } _get_ssid() { - _iwconfig_get 1 + local ssid="$(_iwconfig_get 1)" + # If the ssid has a space then it's wrapped in quotes. + # This is a problem if the real ssid has a quote at the start or the end :/ + ssid=${ssid#\"} + ssid=${ssid%\"} + echo "${ssid}" } _get_ap_mac_address() { - _iwconfig_get 3 + _iwconfig_get 3 } _get_channel() { - _iwconfig_get 2 + _iwconfig_get 2 } iwconfig_report() { - local m="connected to" - local ssid=$(_get_ssid) - local mac=$(_get_ap_mac_address "${iface}") - [ -n "${mac}" ] && mac=" at ${mac}" - local wep_status="$(iwconfig_get_wep_status "${iface}")" - local channel=$(_get_channel) - [ -n "${channel}" ] && channel="on channel ${channel} " - - eindent - einfo "${IFACE} ${m} \"${ssid}\"${mac}" - einfo "${channel}${wep_status}" - eoutdent + local m="connected to" + local ssid="$(_get_ssid)" + local mac="$(_get_ap_mac_address "${iface}")" + [ -n "${mac}" ] && mac=" at ${mac}" + local wep_status="$(iwconfig_get_wep_status "${iface}")" + local channel="$(_get_channel)" + [ -n "${channel}" ] && channel="on channel ${channel} " + + eindent + einfo "${IFACE} ${m} \"${ssid}\"${mac}" + einfo "${channel}${wep_status}" + eoutdent } iwconfig_get_wep_key() { - local mac="$1" key= - [ -n "${mac}" ] && mac="$(echo "${mac}" | sed -e 's/://g')" - eval key=\$mac_key_${mac} - [ -z "${key}" ] && eval key=\$key_${SSIDVAR} - echo "${key:--}" + local mac="$1" key= + [ -n "${mac}" ] && mac="$(echo "${mac}" | sed -e 's/://g')" + eval key=\$mac_key_${mac} + [ -z "${key}" ] && eval key=\$key_${SSIDVAR} + echo "${key:--}" } iwconfig_user_config() { - local conf= - eval set -- \$ifconfig_${SSIDVAR} - for conf in "$@" ; do - ifconfig "${IFACE}" ${conf} - done + local conf= + eval set -- \$ifconfig_${SSIDVAR} + for conf in "$@" ; do + ifconfig "${IFACE}" ${conf} + done } iwconfig_set_mode() { - local x= opt= unopt="hostap adhoc" - case "$1" in - master|hostap) unopt="adhoc" opt="hostap" ;; - ad-hoc|adhoc) unopt="hostap" opt="adhoc" ;; - esac - for x in ${unopt} ; do - ifconfig "${IFACE}" -mediaopt ${x} - done - for x in ${opt} ; do - ifconfig "${IFACE}" mediaopt ${x} - done + local x= opt= unopt="hostap adhoc" + case "$1" in + master|hostap) unopt="adhoc" opt="hostap" ;; + ad-hoc|adhoc) unopt="hostap" opt="adhoc" ;; + esac + for x in ${unopt} ; do + ifconfig "${IFACE}" -mediaopt ${x} + done + for x in ${opt} ; do + ifconfig "${IFACE}" mediaopt ${x} + done } iwconfig_setup_specific() { - local mode="${1:-master}" channel= - if [ -z "${SSID}" ]; then - eerror "${IFACE} requires an SSID to be set to operate in ${mode} mode" - eerror "adjust the ssid_${IFVAR} setting in /etc/conf.d/net" - return 1 - fi + local mode="${1:-master}" channel= + if [ -z "${SSID}" ]; then + eerror "${IFACE} requires an SSID to be set to operate in ${mode} mode" + eerror "adjust the ssid_${IFVAR} setting in /etc/conf.d/net" + return 1 + fi - iwconfig_set_mode "${mode}" || return 1 + iwconfig_set_mode "${mode}" || return 1 - SSIDVAR=$(_shell_var "${SSID}") - local key=$(iwconfig_get_wep_key) + SSIDVAR=$(_shell_var "${SSID}") + local key=$(iwconfig_get_wep_key) - # Now set the key - eval ifconfig "${IFACE}" wepkey "${key}" + # Now set the key + ifconfig "${IFACE}" wepkey "${key}" - eval ifconfig "${IFACE}" ssid "${SSID}" || return 1 + ifconfig "${IFACE}" ssid "${SSID}" || return 1 - eval channel=\$channel_${IFVAR} - # We default the channel to 3 - ifconfig "${IFACE}" channel "${channel:-3}" || return 1 + eval channel=\$channel_${IFVAR} + # We default the channel to 3 + ifconfig "${IFACE}" channel "${channel:-3}" || return 1 - iwconfig_user_config - iwconfig_report "${iface}" - return 0 + iwconfig_user_config + iwconfig_report "${iface}" + return 0 } iwconfig_associate() { - local mac="$1" channel="$2" caps="$3" - local mode= w="(WEP Disabled)" key= - - SSIDVAR=$(_shell_var "${SSID}") - key=$(iwconfig_get_wep_key "${mac}") - case "${caps}" in - [EI]P*) - if [ "${key}" = "-" ] ; then - ewarn "WEP key is not set for \"${SSID}\"; not connecting" - return 1 - fi - ;; - "") ;; - *) - if [ "${key}" != "-" ] ; then - key="-" - ewarn "\"${SSID}\" is not WEP enabled; ignoring setting" - fi - ;; - esac - - # Set mode accordingly - case "${caps}" in - *E*) - mode="managed" - if LC_ALL=C ifconfig "${IFACE}" | \ - grep -q "^[[:space:]]*media: .*adhoc" ; then - ifconfig "${IFACE}" down -mediaopt adhoc up - fi - ;; - *I*) - mode="adhoc" - if ! LC_ALL=C ifconfig "${IFACE}" | \ - grep -q "^[[:space:]]*media: .*adhoc" ; then - ifconfig "${IFACE}" down mediaopt adhoc up - fi - ;; - *) - if LC_ALL=C ifconfig "${IFACE}" \ - | grep -q "^[[:space:]]*media: .*adhoc" ; then - mode="adhoc" - else + local mac="$1" channel="$2" caps="$3" + local mode= w="(WEP Disabled)" key= + + SSIDVAR=$(_shell_var "${SSID}") + key=$(iwconfig_get_wep_key "${mac}") + case "${caps}" in + [EI]P*) + if [ "${key}" = "-" ] ; then + ewarn "WEP key is not set for \"${SSID}\"; not connecting" + return 1 + fi + ;; + "") ;; + *) + if [ "${key}" != "-" ] ; then + key="-" + ewarn "\"${SSID}\" is not WEP enabled; ignoring setting" + fi + ;; + esac + + # Set mode accordingly + case "${caps}" in + *E*) mode="managed" - fi - ;; - esac - - if [ "${key}" = "-" ] ; then - ifconfig "${IFACE}" wepmode off - else - ifconfig "${IFACE}" wepmode on - ifconfig "${IFACE}" deftxkey 1 - w=$(iwconfig_get_wep_status) - fi - - ebegin "Connecting to \"${SSID}\" in ${mode} mode ${w}" - - if ! eval ifconfig "${IFACE}" wepkey "${key}" ; then - eerror "Invalid WEP key ${key}" - return 1 - fi - - eval ifconfig "${IFACE}" ssid "${SSID}" || return 1 - iwconfig_user_config - - if [ "${SSID}" != "any" ] && type preassociate >/dev/null 2>/dev/null ; then - veinfo "Running preassociate function" - veindent - ( preassociate ) - local e=$? - veoutdent - if [ ${e} -eq 0 ] ; then - veend 1 "preassociate \"${SSID}\" on ${IFACE} failed" - return 1 + if LC_ALL=C ifconfig "${IFACE}" | \ + grep -q "^[[:space:]]*media: .*adhoc" ; then + ifconfig "${IFACE}" down -mediaopt adhoc up + fi + ;; + *I*) + mode="adhoc" + if ! LC_ALL=C ifconfig "${IFACE}" | \ + grep -q "^[[:space:]]*media: .*adhoc" ; then + ifconfig "${IFACE}" down mediaopt adhoc up + fi + ;; + *) + if LC_ALL=C ifconfig "${IFACE}" \ + | grep -q "^[[:space:]]*media: .*adhoc" ; then + mode="adhoc" + else + mode="managed" + fi + ;; + esac + + if [ "${key}" = "-" ] ; then + ifconfig "${IFACE}" wepmode off + else + ifconfig "${IFACE}" wepmode on + ifconfig "${IFACE}" deftxkey 1 + w=$(iwconfig_get_wep_status) fi - fi - local timeout= i=0 - eval timeout=\$associate_timeout_${IFVAR} - timeout=${timeout:-10} + ebegin "Connecting to \"${SSID}\" in ${mode} mode ${w}" - [ ${timeout} -eq 0 ] \ - && vewarn "WARNING: infinite timeout set for association on ${IFACE}" + if ! ifconfig "${IFACE}" wepkey "${key}" ; then + eerror "Invalid WEP key ${key}" + return 1 + fi - while true; do - _has_carrier && break - sleep 1 - [ ${timeout} -eq 0 ] && continue - i=$((${i} + 1)) - [ ${i} -ge ${timeout} ] && return 1 - done + ifconfig "${IFACE}" ssid "${SSID}" || return 1 + iwconfig_user_config + + if [ "${SSID}" != "any" ] && type preassociate >/dev/null 2>/dev/null ; then + veinfo "Running preassociate function" + veindent + ( preassociate ) + local e=$? + veoutdent + if [ ${e} -eq 0 ] ; then + veend 1 "preassociate \"${SSID}\" on ${IFACE} failed" + return 1 + fi + fi - if ! _has_carrier ; then - eend 1 - return 1 - fi - eend 0 + local timeout= i=0 + eval timeout=\$associate_timeout_${IFVAR} + timeout=${timeout:-10} + + [ ${timeout} -eq 0 ] \ + && vewarn "WARNING: infinite timeout set for association on ${IFACE}" + + while true; do + _has_carrier && break + sleep 1 - if [ "${SSID}" = "any" ]; then - SSID="$(_get_ssid)" - iwconfig_associate - return $? - fi + [ ${timeout} -eq 0 ] && continue + i=$((${i} + 1)) + [ ${i} -ge ${timeout} ] && { eend 1; return 1; } + done + + _has_carrier || { eend 1; return 1; } + eend 0 - iwconfig_report + if [ "${SSID}" = "any" ]; then + SSID="$(_get_ssid)" + iwconfig_associate + return $? + fi - if type postassociate >/dev/null 2>/dev/null ; then - veinfo "Running postassociate function" - veindent - ( postassociate ) - veoutdent - fi + iwconfig_report - return 0 + if type postassociate >/dev/null 2>/dev/null ; then + veinfo "Running postassociate function" + veindent + ( postassociate ) + veoutdent + fi + + return 0 } iwconfig_scan() { @@ -234,35 +237,35 @@ iwconfig_scan() { scan="$(LC_ALL=C ifconfig -v "${IFACE}" list scan 2>/dev/null | sed -e "1 d" -e "s/$/'/g" -e "s/^/'/g")" while [ ${i} -lt 3 -a -z "${scan}" ] ; do - scan="${scan}${scan:+ }$(LC_ALL=C ifconfig -v "${IFACE}" scan 2>/dev/null | sed -e "1 d" -e "s/$/'/g" -e "s/^/'/g")" - i=$((${i} + 1)) + scan="${scan}${scan:+ }$(LC_ALL=C ifconfig -v "${IFACE}" scan 2>/dev/null | sed -e "1 d" -e "s/$/'/g" -e "s/^/'/g")" + i=$((${i} + 1)) done - + local OIFS=$IFS APS=-1 eval set -- ${scan} for line in "$@" ; do - APS=$((${APS} + 1)) - set -- ${line} - while true ; do + APS=$((${APS} + 1)) + set -- ${line} + while true ; do + case "$1" in + *:*:*:*:*:*) break ;; + esac + eval SSID_${APS}="\"\${SSID_${APS}}\${SSID_${APS}:+ }$1\"" + shift + done + eval MAC_${APS}="$(echo "$1" | tr '[:lower:]' '[:upper:]')" + eval CHAN_${APS}="$2" + quality=${4%:*} + shift ; shift ; shift ; shift ; shift + eval CAPS_${APS}="\"$*\"" + + # Add 1000 for managed nodes as we prefer them to adhoc + set -- $* case "$1" in - *:*:*:*:*:*) break ;; + *E*) eval QUAL_${APS}=$((${quality} + 1000)) ;; + *) eval QUAL_${APS}=\$quality ;; esac - eval SSID_${APS}="\"\${SSID_${APS}}\${SSID_${APS}:+ }$1\"" - shift - done - eval MAC_${APS}="$(echo "$1" | tr '[:lower:]' '[:upper:]')" - eval CHAN_${APS}="$2" - quality=${4%:*} - shift ; shift ; shift ; shift ; shift - eval CAPS_${APS}="\"$*\"" - - # Add 1000 for managed nodes as we prefer them to adhoc - set -- $* - case "$1" in - *E*) eval QUAL_${APS}=$((${quality} + 1000)) ;; - *) eval QUAL_${APS}=\$quality ;; - esac done if [ -z "${MAC_0}" ]; then @@ -274,46 +277,46 @@ iwconfig_scan() { # Sort based on quality local i=0 k=1 a= b= x= t= while [ ${i} -lt ${APS} ] ; do - k=$((${i} + 1)) - while [ ${k} -le ${APS} ] ; do - eval a=\$QUALITY_${i} - [ -z "${a}" ] && break - eval b=\$QUALITY_${k} - if [ -n "${b}" -a "${a}" -lt "${b}" ] ; then - for x in MAC SSID CHAN QUALITY CAPS ; do - eval t=\$${x}_${i} - eval ${x}_${i}=\$${x}_${k} - eval ${x}_${k}=\$t - done - fi - k=$((${k} + 1)) - done - i=$((${i} + 1)) + k=$((${i} + 1)) + while [ ${k} -le ${APS} ] ; do + eval a=\$QUALITY_${i} + [ -z "${a}" ] && break + eval b=\$QUALITY_${k} + if [ -n "${b}" -a "${a}" -lt "${b}" ] ; then + for x in MAC SSID CHAN QUALITY CAPS ; do + eval t=\$${x}_${i} + eval ${x}_${i}=\$${x}_${k} + eval ${x}_${k}=\$t + done + fi + k=$((${k} + 1)) + done + i=$((${i} + 1)) done # Strip any duplicates local i=0 k=1 a= b= while [ ${i} -lt ${APS} ] ; do - k=$((${i} + 1)) - while [ ${k} -le ${APS} ] ; do - eval a=\$MAC_${i} - eval b=\$MAC_${k} - if [ "${a}" = "${b}" ] ; then - eval a=\$QUALITY_${i} - eval b=\$QUALITY_${k} - if [ -n "${a}" -a -n "${b}" ] ; then - if [ ${a} -ge ${b} ] ; then - unset MAC_${k} SSID_${k} CHAN_${k} QUALITY_${k} CAPS_${k} - else - unset MAC_${i} SSID_${i} CHAN_${i} QUALITY_${i} CAPS_${i} + k=$((${i} + 1)) + while [ ${k} -le ${APS} ] ; do + eval a=\$MAC_${i} + eval b=\$MAC_${k} + if [ "${a}" = "${b}" ] ; then + eval a=\$QUALITY_${i} + eval b=\$QUALITY_${k} + if [ -n "${a}" -a -n "${b}" ] ; then + if [ ${a} -ge ${b} ] ; then + unset MAC_${k} SSID_${k} CHAN_${k} QUALITY_${k} CAPS_${k} + else + unset MAC_${i} SSID_${i} CHAN_${i} QUALITY_${i} CAPS_${i} + fi + else + unset MAC_${k} SSID_${k} CHAN_${k} QUALITY_${k} CAPS_${k} + fi fi - else - unset MAC_${k} SSID_${k} CHAN_${k} QUALITY_${k} CAPS_${k} - fi - fi - k=$((${k} + 1)) - done - i=$((${i} + 1)) + k=$((${k} + 1)) + done + i=$((${i} + 1)) done local i=0 e= m= black= s= @@ -323,8 +326,8 @@ iwconfig_scan() { while [ ${i} -le ${APS} ] ; do eval x=\$MAC_${i} if [ -z "${x}" ] ; then - i=$((${i} + 1)) - continue + i=$((${i} + 1)) + continue fi eval m=\$MODE_${i} @@ -333,8 +336,8 @@ iwconfig_scan() { eval q=\$QUALITY_${i} eval e=\$CAPS_${i} case "${e}" in - [EI]P*) e=", encrypted" ;; - *) e="" ;; + [EI]P*) e=", encrypted" ;; + *) e="" ;; esac if [ -z "${s}" ] ; then einfo "Found ${x}${m}${e}" @@ -424,8 +427,8 @@ iwconfig_connect_not_preferred() { while [ ${i} -le ${APS} ] ; do eval e=\$SSID_${i} if [ -z "${e}" ] ; then - i=$((${i} + 1)) - continue + i=$((${i} + 1)) + continue fi eval "$(_get_array preferred_aps)" @@ -444,7 +447,7 @@ iwconfig_connect_not_preferred() { eval freq=\$FREQ_${i} eval chan=\$CHAN_${i} iwconfig_associate "${mac}" \ - "${chan}" "${caps}" && return 0 + "${chan}" "${caps}" && return 0 fi i=$((${i} + 1)) done @@ -471,7 +474,7 @@ iwconfig_configure() { eval x=\$mode_${IFVAR} x=${x:-managed} case "${x}" in - ad-hoc|adhoc|hostap|master) iwconfig_setup_specific "${x}" ;; + ad-hoc|adhoc|hostap|master) iwconfig_setup_specific "${x}" ;; esac if [ "${x}" != "managed" -a "${x}" != "auto" -a "${x}" != "ad-hoc" -a "${x}" != "adhoc" -a ${x} != "master" ] ; then @@ -543,7 +546,7 @@ iwconfig_pre_start() { iwconfig_defaults iwconfig_user_config - + # Set the base metric to be 2000 metric=2000 -- cgit v1.2.3