From 9a3078d8cec470b3820c5597d5c8819d642e558d Mon Sep 17 00:00:00 2001 From: James Rayner Date: Mon, 10 Aug 2009 16:09:16 +1000 Subject: revert pryor patch --- src/8021x | 36 ++- src/connections/ethernet | 211 ++++-------------- src/connections/ethernet-iproute | 214 ++++-------------- src/connections/ppp | 53 +---- src/connections/wireless | 368 +++++-------------------------- src/globals | 118 ---------- src/net-profiles | 63 ++---- src/net-rename | 10 +- src/netcfg | 56 ++--- src/netcfg-menu | 59 +++-- src/network | 460 +++++++++++++++------------------------ src/wireless_utils | 141 ------------ 12 files changed, 405 insertions(+), 1384 deletions(-) delete mode 100644 src/globals delete mode 100644 src/wireless_utils (limited to 'src') diff --git a/src/8021x b/src/8021x index 7daecce..75030e9 100644 --- a/src/8021x +++ b/src/8021x @@ -1,53 +1,45 @@ -################################## -## -# /usr/lib/network/8021x -## -################################## - # Uses wpa_supplicant to check for association to a network # wpa_check interface [timeout] wpa_check() { - local INTERFACE="$1" TIMEOUT="${2:-15}" timeout=0 + local timeout=0 INTERFACE=$1 TIMEOUT=${2:-15} - while [[ "$timeout" -lt "$TIMEOUT" ]]; do + while [[ $timeout -lt $TIMEOUT ]]; do ( # Sometimes wpa_supplicant isn't ready so silence errors for 2s only to avoid hiding real errors - if [[ "$timeout" -lt 2 ]]; then - eval $(wpa_cli -i "$INTERFACE" status 2> /dev/null | fgrep "wpa_state=") + if [[ $timeout -lt 2 ]]; then + eval `wpa_cli status 2> /dev/null|grep wpa_state` else - eval $(wpa_cli -i "$INTERFACE" status | fgrep "wpa_state=") + eval `wpa_cli status|grep wpa_state` fi - [[ "$wpa_state" = COMPLETED ]] + [[ "$wpa_state" = "COMPLETED" ]] ) && return 0 sleep 1 let timeout++ done wpa_cli terminate >/dev/null 2>&1 - report_fail "Couldn't associate/authenticate with wireless network." + err_append "Authentication/association failed" return 1 } start_wpa() { - local INTERFACE="$1" WPA_CONF="$2" WPA_OPTS="$3" + INTERFACE="$1"; WPA_CONF="$2"; WPA_OPTS="$3" - wpa_supplicant -B -P "/var/run/wpa_supplicant_$INTERFACE.pid" -i "$INTERFACE" -c "$WPA_CONF" $WPA_OPTS + wpa_supplicant -B -P/var/run/wpa_supplicant_${INTERFACE}.pid -i"${INTERFACE}" -c "$WPA_CONF" $WPA_OPTS sleep 1 - if [[ ! -f "/var/run/wpa_supplicant_$INTERFACE.pid" ]]; then - report_fail "wpa_supplicant did not start, possible configuration error" + if [[ ! -f "/var/run/wpa_supplicant_${INTERFACE}.pid" ]]; then + err_append "wpa_supplicant did not start, possible configuration error" return 1 fi } stop_wpa() { - wpa_cli -i "$1" terminate &> /dev/null - sleep 1 # JP: need this else the file tends to disappear after [[ -f ... ]] but before cat... - # see - if [[ -f "/var/run/wpa_supplicant_$1.pid" ]]; then - kill $(cat "/var/run/wpa_supplicant_$1.pid") &>/dev/null & + wpa_cli terminate &> /dev/null + if [[ -f /var/run/wpa_supplicant_$1.pid ]]; then + kill $(cat /var/run/wpa_supplicant_$1.pid) &>/dev/null & fi } diff --git a/src/connections/ethernet b/src/connections/ethernet index 933dbd6..e24e9f5 100644 --- a/src/connections/ethernet +++ b/src/connections/ethernet @@ -1,114 +1,86 @@ #! /bin/bash - -################################## -## -# /usr/lib/network/connections/ethernet -## -################################## - -. /etc/rc.conf -. /etc/rc.d/functions . /usr/lib/network/network ethernet_up() { - load_profile "$1" + load_profile $1 - if [[ ! -e "/sys/class/net/$INTERFACE" ]]; then - if ! echo "$INTERFACE" | fgrep -q ":"; then - report_fail "Interface $INTERFACE does not exist." + if [[ ! -e /sys/class/net/"$INTERFACE" ]]; then + if ! echo "$INTERFACE"|grep ":"; then + err_append "interface $INTERFACE does not exist" return 1 fi fi - report_debug ethernet_up ifup - ethernet_control "$INTERFACE" up - sleep 3 + ip link set $INTERFACE up + sleep 1 - # don't think it's possible to detect carrier using ifconfig alone (at least, not without ifdown/ifupping the interface) - # if ip link show dev "$INTERFACE" | fgrep -q "NO-CARRIER"; then... - # if ! ethernet_query "$INTERFACE" address; then... - if [[ $(cat /sys/class/net/$INTERFACE/carrier 2>/dev/null) -ne 1 ]]; then # gives err if iface inactive (i.e. ifdown) - # 0 if up but not connected to network, 1 if connected - report_fail "No connection." + if ip link show $INTERFACE|grep -q "NO-CARRIER"; then + err_append "No connection" return 1 fi if checkyesno "${AUTH8021X:-no}"; then - . "$SUBR_DIR/8021x" + . "${SUBR_DIR}"/8021x [[ -z "$WPA_CONF" ]] && WPA_CONF="/etc/wpa_supplicant.conf" [[ -z "$WPA_OPTS" ]] && WPA_OPTS="-Dwired" - report_debug ethernet_up start_wpa "$INTERFACE" "$WPA_CONF" "$WPA_OPTS" start_wpa "$INTERFACE" "$WPA_CONF" "$WPA_OPTS" if ! wpa_check "$INTERFACE"; then - ethernet_control "$INTERFACE" forcedown # JP: forcedown ignores the nodown quirk, matching - # what was already here in the code...do we in fact want to ignore - # the nodown quirk here? + ifconfig "$INTERFACE" down return 1 fi fi - case "$IP" in + case $IP in dhcp) if checkyesno "${DHCLIENT:-no}"; then - rm -r "/var/run/dhclient-$INTERFACE.pid" >/dev/null 2>&1 - report_debug ethernet_up dhclient -q -e TIMEOUT="${DHCP_TIMEOUT:-10}" -pf "/var/run/dhclient-$INTERFACE.pid" "$INTERFACE" - if ! dhclient -q -e TIMEOUT="${DHCP_TIMEOUT:-10}" -pf "/var/run/dhclient-$INTERFACE.pid" "$INTERFACE"; then - report_fail "Couldn't get DHCP IP lease." + rm -r /var/run/dhclient-${INTERFACE}.pid >/dev/null 2>&1 + if ! dhclient -q -e TIMEOUT="${DHCP_TIMEOUT:-10}" -pf /var/run/dhclient-${INTERFACE}.pid $INTERFACE; then + err_append "DHCP IP lease attempt failed." return 1 fi else # Clear remaining pid files. - rm -f "/var/run/dhcpcd-$INTERFACE".{pid,cache} >/dev/null 2>&1 + rm -f /var/run/dhcpcd-${INTERFACE}.{pid,cache} >/dev/null 2>&1 # If using own dns, tell dhcpcd to NOT replace resolv.conf - [[ -n "$DNS1" || -n "$DNS" ]] && DHCP_OPTIONS="-C resolv.conf $DHCP_OPTIONS" - - #if ! dhcpcd -qL -t "${DHCP_TIMEOUT:-10}" $DHCP_OPTIONS "$INTERFACE" >/dev/null 2>&1; then - report_debug ethernet_up dhcpcd -qL -t "${DHCP_TIMEOUT:-10}" $DHCP_OPTIONS "$INTERFACE" - dhcpcd -qL -t "${DHCP_TIMEOUT:-10}" $DHCP_OPTIONS "$INTERFACE" 2>&1 | report_debug $(cat) - if [[ "$PIPESTATUS" -ne 0 ]]; then - report_fail "Couldn't get DHCP IP lease." + [[ -n "$DNS1" ]] && DHCP_OPTIONS="-C resolv.conf $DHCP_OPTIONS" + # Start dhcpcd + if ! dhcpcd -qL -t "${DHCP_TIMEOUT:-10}" $DHCP_OPTIONS "$INTERFACE"; then + err_append "DHCP IP lease attempt failed." return 1 fi fi - if [[ -n "$IFOPTS" ]]; then - report_debug ethernet_up ifup $IFOPTS - ifconfig "$INTERFACE" $IFOPTS - fi + [[ -n "$IFOPTS" ]] && ifconfig "$INTERFACE" $IFOPTS ;; static) - report_debug ethernet_up ifup $IFOPTS if ! ifconfig "$INTERFACE" $IFOPTS up; then - report_fail "Couldn't configure $INTERFACE interface." + err_append "Bringing interface up failed." return 1 fi # bring up the default route (gateway) if [[ -n "$GATEWAY" ]]; then - report_debug ethernet_up route add default gw "$GATEWAY" - if ! route add default gw "$GATEWAY"; then - ethernet_control "$INTERFACE" down # JP: don't we want to take the interface down if we failed (as ethernet-iproute does?) - report_fail "Couldn't add gateway $GATEWAY." + if ! route add default gw $GATEWAY; then + err_append "Adding gateway failed." return 1 fi fi ;; *) - report_err "Profile error: must be either IP=dhcp or IP=static." + err_append "IP=\"\" must be either 'dhcp' or 'static'." return 1 ;; esac # set the hostname if [[ -n "$HOSTNAME" ]]; then - report_debug ethernet_up hostname "$HOSTNAME" if ! hostname "$HOSTNAME"; then - report_fail "Couldn't set hostname." + err_append "Setting hostname failed." return 1 fi fi # Generate a new resolv.conf - if [[ -n "$DNS1" || -n "$DNS" ]]; then + if [[ -n "$DNS1" ]] || [[ -n "$DNS" ]]; then : >/etc/resolv.conf [[ -n "$DOMAIN" ]] && echo "domain $DOMAIN" >>/etc/resolv.conf @@ -122,141 +94,36 @@ ethernet_up() { done fi fi - - # successfully running a new profile; erase any suspended profiles on this interface - local iface="$INTERFACE" - find "$STATE_DIR/suspend/" -maxdepth 1 -type f -printf '%f\n' \ - | while read prof; do - # the pipe to "while read" will create a subshell, so sourced variables will already be in a sandbox - # we just need to clear INTERFACE which is all we care about - unset INTERFACE - . "$STATE_DIR/suspend/$prof" - if [[ "$iface" == "$INTERFACE" ]]; then - rm "$STATE_DIR/suspend/$prof" - fi - done - return 0 } ethernet_down() { - load_profile "$1" - case "$IP" in + load_profile $1 + case $IP in dhcp) if checkyesno "${DHCLIENT:-no}"; then - if [[ -f "/var/run/dhclient-$INTERFACE.pid" ]]; then - report_debug ethernet_down kill dhclient - kill $(cat "/var/run/dhclient-$INTERFACE.pid") + if [[ -f /var/run/dhclient-${INTERFACE}.pid ]]; then + kill `cat /var/run/dhclient-${INTERFACE}.pid` fi else - if [[ -f "/var/run/dhcpcd-$INTERFACE.pid" ]]; then - report_debug ethernet_down dhcpcd -qx "$INTERFACE" - dhcpcd -qx "$INTERFACE" >/dev/null 2>&1 + if [[ -f /var/run/dhcpcd-${INTERFACE}.pid ]]; then + dhcpcd -qx "$INTERFACE" fi fi ;; static) - if [[ -n "$GATEWAY" ]]; then - report_debug ethernet_down route del default gw "$GATEWAY" - route del default gw "$GATEWAY" - fi + [[ -n "$GATEWAY" ]] && route del default gw $GATEWAY ;; esac - - report_debug ethernet_down ifdown - ethernet_control "$INTERFACE" down -} - -# CONNECTION_query $INTERFACE [ profile | enabled (i.e. not rfkill-ed) | active (i.e. ifup'd) | associated | address ] -ethernet_query() { - local INTERFACE="$1" REQUEST="$2" - # report_debug ethernet_query "$@" - case "$REQUEST" in - - # JP: this duplicates code in ethernet-iproute, but doing so was necessary to refactor these functions into the CONNECTION scripts - profile) - # return code = 1 if iface is available to be used - # return code = 0 if unavailable (disabled, or in use by another profile or by external config) - # echo "" | "disabled" | "external" | PROFILE_NAME - if [[ ! -f "$STATE_DIR/interfaces/$INTERFACE" ]]; then - # interface is up but has no registered profile - if ethernet_query "$INTERFACE" address; then - set_iface up "$INTERFACE" external - echo "external" - return 0 - fi - # no ip address, count as inactive - # should we ifconfig iface down? no, may be up for a reason - return 1 - - else # interface is up and thinks it's running a profile - . "$STATE_DIR/interfaces/$INTERFACE" # these files should only contain a PROFILE=... statement - case "$PROFILE" in - external) - echo "external" - return 0 - ;; - disabled) # shouldn't happen for non-wireless interfaces, but leaving this here in case... - # iface should be down if it's disabled - ethernet_control "$INTERFACE" down - echo "disabled" - return 0 - ;; - *) - ( # use subshell to prevent overwriting currently loaded profile - . "$STATE_DIR/profiles/$PROFILE" - if ! ethernet_query "$INTERFACE" address; then - report_warn "INTERFACE $INTERFACE is inactive; no longer being controlled by $PROFILE" - set_profile down "$PROFILE" - exit 1 - else - echo "$PROFILE" # associated with loaded profile - exit 0 - fi - ) - # return $? - ;; - esac - fi - ;; - - active) - ifconfig "$INTERFACE" | fgrep -qw UP - # cat /sys/class/net/wlan0/carrier >/dev/null # gives err if iface inactive, 0 if up but not connected to network, 1 if connected - ;; - - address) - ifconfig "$INTERFACE" | fgrep -q addr: - ;; - - *) return 1 ;; - esac -} - -# CONNECTION_control $INTERFACE [ up | down | forcedown | enable | disable ] -ethernet_control() { - local INTERFACE="$1" ACTION="$2" - #report_debug ethernet_control "$@" - case "$ACTION" in - up) ifconfig "$INTERFACE" up 2>/dev/null - ethernet_query "$INTERFACE" active - ;; - down|forcedown) - ifconfig "$1" 0.0.0.0 - if [[ "$2" = forcedown ]] || ! quirk nodown; then - ifconfig "$INTERFACE" down - fi - ;; - *) return 1 + ifconfig $INTERFACE 0.0.0.0 + + case "$CONNECTION" in # Keep interface up for wireless + ethernet|ethernet-old) + quirk "nodown" || ifconfig $INTERFACE down ;; esac } -ethernet_verify() { - local INTERFACE="$1" - # do nothing -} - -ethernet_$1 "$2" "$3" +ethernet_$1 $2 exit $? # vim: set ts=4 et sw=4: diff --git a/src/connections/ethernet-iproute b/src/connections/ethernet-iproute index 8ecabe8..1a4a6e4 100644 --- a/src/connections/ethernet-iproute +++ b/src/connections/ethernet-iproute @@ -1,102 +1,85 @@ #! /bin/bash - -################################## -## -# /usr/lib/network/connections/ethernet-iproute -## -################################## - -. /etc/rc.conf -. /etc/rc.d/functions . /usr/lib/network/network -report_iproute() { - report_"$@" - ethernet_control "$INTERFACE" down +error() +{ + err_append "$*" + ip addr flush $INTERFACE &>/dev/null + quirk "nodown" || ip link set $INTERFACE down &>/dev/null exit 1 } ethernet_up() { - load_profile "$1" + load_profile $1 - if [[ ! -e "/sys/class/net/$INTERFACE" ]]; then - if ! echo "$INTERFACE" | fgrep -q ":"; then - report_iproute fail "Interface $INTERFACE does not exist." + if [[ ! -e /sys/class/net/$INTERFACE ]]; then + if ! echo "$INTERFACE"|grep ":"; then + error "Interface $INTERFACE does not exist" fi fi - report_debug ethernet_iproute_up ifup - ethernet_control "$INTERFACE" up - sleep 3 + ip link set $INTERFACE up + sleep 1 - # if ! ethernet_query "$INTERFACE" address; then... - if ip link show dev "$INTERFACE" | fgrep -q "NO-CARRIER"; then # JP: shouldn''t we include the "dev"? - report_iproute fail "No connection." + if ip link show $INTERFACE|grep -q "NO-CARRIER"; then + err_append "No connection" + return 1 fi - - if checkyesno "${AUTH8021X:-no}"; then - . "$SUBR_DIR/8021x" + + if checkyesno ${AUTH8021X:-no}; then + . ${SUBR_DIR}/8021x [[ -z "$WPA_CONF" ]] && WPA_CONF="/etc/wpa_supplicant.conf" [[ -z "$WPA_OPTS" ]] && WPA_OPTS="-Dwired" - report_debug ethernet_iproute_up start_wpa "$INTERFACE" "$WPA_CONF" "$WPA_OPTS" + start_wpa "$INTERFACE" "$WPA_CONF" "$WPA_OPTS" if ! wpa_check "$INTERFACE"; then - ethernet_control "$INTERFACE" forcedown # JP: forcedown ignores the nodown quirk, matching - # what was already here in the code...do we in fact want to ignore - # the nodown quirk here? + ip link set $INTERFACE down return 1 fi fi - case "$IP" in + case $IP in dhcp) # Clear remaining pid files. - rm -f "/var/run/dhcpcd-$INTERFACE".{pid,cache} >/dev/null 2>&1 + rm -f /var/run/dhcpcd-${INTERFACE}.{pid,cache} >/dev/null 2>&1 + # If using own dns, tell dhcpcd to NOT replace resolv.conf [[ -n "$DNS" ]] && DHCP_OPTIONS="-C resolv.conf $DHCP_OPTIONS" - - #if ! dhcpcd -qL -t "${DHCP_TIMEOUT:-10}" $DHCP_OPTIONS "$INTERFACE" >/dev/null 2>&1; then - report_debug ethernet_iproute_up dhcpcd -qL -t "${DHCP_TIMEOUT:-10}" $DHCP_OPTIONS "$INTERFACE" - dhcpcd -qL -t "${DHCP_TIMEOUT:-10}" $DHCP_OPTIONS "$INTERFACE" 2>&1 | report_iproute debug $(cat) - if [[ "$PIPESTATUS" -ne 0 ]]; then - report_iproute fail "Couldn't get DHCP IP lease." + + if ! dhcpcd -qL -t ${DHCP_TIMEOUT:-10} $DHCP_OPTIONS $INTERFACE; then + error "DHCP IP lease attempt failed" fi ;; static) if [[ -n "$ADDR" ]]; then - report_debug ethernet_iproute_up ip addr add "$ADDR/24" brd + dev "$INTERFACE" - if ! ip addr add "$ADDR/24" brd + dev "$INTERFACE"; then - report_iproute fail "Couldn't configure $INTERFACE interface." + if ! ip addr add ${ADDR}/24 brd + dev $INTERFACE; then + error "Could not configure interface" fi fi if [[ -n "$GATEWAY" ]]; then - report_debug ethernet_iproute_up ip route add default via "$GATEWAY" - if ! ip route add default via "$GATEWAY"; then - - report_iproute fail "Couldn't add gateway $GATEWAY." + if ! ip route add default via $GATEWAY; then + error "Adding gateway failed" fi fi ;; *) - report_iproute err "Profile error: must be either IP=dhcp or IP=static." + error "Profile error: IP must be either 'dhcp' or 'static'" ;; esac if [[ -n "$IPCFG" ]]; then for line in "${IPCFG[@]}"; do - report_debug ethernet_iproute_up ip "$line" - if ! ip "$line"; then - report_iproute fail "Couldn't configure $INTERFACE interface ($line)." + if ! ip $line; then + error "Could not configure interface" fi done fi - # set the hostname + # Set hostname if [[ -n "$HOSTNAME" ]]; then - report_debug ethernet_iproute_up hostname "$HOSTNAME" - if ! hostname "$HOSTNAME"; then - report_iproute fail "Couldn't set hostname." + if ! hostname $HOSTNAME; then + error "Cannot set hostname" fi fi @@ -106,132 +89,29 @@ ethernet_up() { [[ -n "$DOMAIN" ]] && echo "domain $DOMAIN" >>/etc/resolv.conf [[ -n "$SEARCH" ]] && echo "search $SEARCH" >>/etc/resolv.conf - for dns in "${DNS[@]}"; do - echo "nameserver $dns" >>/etc/resolv.conf - done - fi - - # successfully running a new profile; erase any suspended profiles on this interface - local iface="$INTERFACE" - find "$STATE_DIR/suspend/" -maxdepth 1 -type f -printf '%f\n' \ - | while read prof; do - # the pipe to "while read" will create a subshell, so sourced variables will already be in a sandbox - # we just need to clear INTERFACE which is all we care about - unset INTERFACE - . "$STATE_DIR/suspend/$prof" - if [[ "$iface" == "$INTERFACE" ]]; then - rm "$STATE_DIR/suspend/$prof" + if [[ -n "$DNS" ]]; then + for dns in ${DNS[@]}; do + echo "nameserver $dns" >>/etc/resolv.conf + done fi - done - + fi return 0 } ethernet_down() { - load_profile "$1" + load_profile $1 - if [[ "$IP" == dhcp ]]; then - if [[ -f "/var/run/dhcpcd-$INTERFACE.pid" ]]; then - report_debug ethernet_iproute_down dhcpcd -qx "$INTERFACE" - dhcpcd -qx "$INTERFACE" >/dev/null 2>&1 + if [[ "$IP" == "dhcp" ]]; then + if [[ -f /var/run/dhcpcd-${INTERFACE}.pid ]]; then + dhcpcd -qx $INTERFACE fi fi - report_debug ethernet_iproute_down if_down - ethernet_control "$INTERFACE" down -} - -# CONNECTION_query $INTERFACE [ profile | enabled (i.e. not rfkill-ed) | active (i.e. ifup'd) | associated | address ] -ethernet_query() { - local INTERFACE="$1" REQUEST="$2" - # report_debug ethernet_iproute_query "$@" - case "$REQUEST" in - - profile) - # return code = 1 if iface is available to be used - # return code = 0 if unavailable (disabled, or in use by another profile or by external config) - # echo "" | "disabled" | "external" | PROFILE_NAME - if [[ ! -f "$STATE_DIR/interfaces/$INTERFACE" ]]; then - # interface is up but has no registered profile - if ethernet_query "$INTERFACE" address; then - set_iface up "$INTERFACE" external - echo "external" - return 0 - fi - # no ip address, count as inactive - # should we ifconfig iface down? no, may be up for a reason - return 1 - - else # interface is up and thinks it's running a profile - . "$STATE_DIR/interfaces/$INTERFACE" # these files should only contain a PROFILE=... statement - case "$PROFILE" in - external) - echo "external" - return 0 - ;; - disabled) # shouldn't happen for non-wireless interfaces, but leaving this here in case... - # iface should be down if it's disabled - ethernet_control "$INTERFACE" down - echo "disabled" - return 0 - ;; - *) - ( # use subshell to prevent overwriting currently loaded profile - . "$STATE_DIR/profiles/$PROFILE" - if ! ethernet_query "$INTERFACE" address; then - report_warn "INTERFACE $INTERFACE is inactive; no longer being controlled by $PROFILE" - set_profile down "$PROFILE" - exit 1 - else - echo "$PROFILE" # associated with loaded profile - exit 0 - fi - ) - # return $? - ;; - esac - fi - ;; - - active) - ip -o link show dev "$INTERFACE" | egrep -qw '^[^>]*,UP' # man ip is inconsistent about whether to use "dev" - # cat /sys/class/net/wlan0/carrier >/dev/null # gives err if iface inactive, 0 if up but not connected to network, 1 if connected - ;; - - address) - test -n "$(ip -o addr show dev "$INTERFACE" | sed -n '2p')" - # ip link show dev "$INTERFACE" | fgrep -q "NO-CARRIER" - ;; - - *) return 1 ;; - esac -} - -# CONNECTION_control $INTERFACE [ up | down | forcedown | enable | disable ] -ethernet_control() { - local INTERFACE="$1" ACTION="$2" - #report_debug ethernet_iproute_control "$@" - case "$ACTION" in - up) - ip link set dev "$INTERFACE" up 2>/dev/null # man ip is inconsistent about whether to use "dev" - ethernet_query "$INTERFACE" active - ;; - down|forcedown) - ip addr flush dev "$INTERFACE" &>/dev/null - if [[ "$2" = forcedown ]] || ! quirk nodown; then - ip link set dev "$INTERFACE" down &>/dev/null # man ip is inconsistent about whether to use "dev" - fi - ;; - *) return 1 - ;; - esac -} + ip addr flush $INTERFACE + quirk "nodown" || ip link set $INTERFACE down -ethernet_verify() { - local INTERFACE="$1" - # do nothing } -ethernet_$1 "$2" "$3" +ethernet_$1 $2 exit $? # vim: set ts=4 et sw=4: diff --git a/src/connections/ppp b/src/connections/ppp index ce3c8ab..9432323 100644 --- a/src/connections/ppp +++ b/src/connections/ppp @@ -1,58 +1,25 @@ #! /bin/bash -################################## -## -# /usr/lib/network/connections/ppp -## -################################## - -. /etc/rc.conf -. /etc/rc.d/functions . /usr/lib/network/network ppp_up() { - load_profile "$1" - [[ -z "$PEER" ]] && PEER=provider + load_profile $1 + [[ -z "$PEER" ]] && PEER="provider" [[ -z "$PPP_TIMEOUT" ]] && PPP_TIMEOUT=30 - if ! /usr/sbin/pppd call "$PEER" updetach child-timeout "$PPP_TIMEOUT" linkname "$PEER"; then - report_fail "Couldn't make pppd connection." - return 1 - fi + /usr/sbin/pppd call $PEER updetach child-timeout $PPP_TIMEOUT linkname $PEER - # wireless and wireless-dbus connections run this block when they call "ethernet up"; but ppp needs to do it itself - # successfully running a new profile; erase any suspended profiles on this interface - local interface="$INTERFACE" - find "$STATE_DIR/suspend/" -maxdepth 1 -type f -printf '%f\n' \ - | while read prof; do - # the pipe to "while read" will create a subshell, so sourced variables will already be in a sandbox - # we just need to clear INTERFACE which is all we care about - unset INTERFACE - . "$STATE_DIR/suspend/$prof" - if [[ "$interface" == "$INTERFACE" ]]; then - rm "$STATE_DIR/suspend/$prof" - fi - done + if [[ $? -ne 0 ]]; then + err_append "pppd connection failed" + exit 1 + fi } ppp_down() { - load_profile "$1" - kill $(head -1 "/var/run/ppp-$(basename "$PEER").pid") -} - -ppp_query() { - "$CONN_DIR/ethernet" query "$@" -} - -ppp_control() { - "$CONN_DIR/wireless" control "$@" -} - -ppp_verify() { - local INTERFACE="$1" - # do nothing + load_profile $1 + kill $(head -1 /var/run/ppp-$(basename $PEER).pid) } -ppp_$1 "$2" +ppp_$1 $2 exit $? # vim: set ts=4 et sw=4: diff --git a/src/connections/wireless b/src/connections/wireless index f316cb7..8b43ed1 100644 --- a/src/connections/wireless +++ b/src/connections/wireless @@ -1,378 +1,124 @@ #! /bin/bash - -################################## -## -# /usr/lib/network/connections/wireless -## -################################## - -. /etc/rc.conf -. /etc/rc.d/functions . /usr/lib/network/network wireless_up() { - load_profile "$1" - [[ -n "$2" ]] && ESSID="$2" # JP: use the literal ESSID (though currently we only interpret wireless-dbus ESSIDs as regexps) - - . "$SUBR_DIR/8021x" - . "$SUBR_DIR/wireless_utils" + load_profile $1 + . ${SUBR_DIR}/8021x + . ${SUBR_DIR}/wireless # Check if interface exists - if [[ ! -e "/sys/class/net/$INTERFACE" ]]; then - if ! echo "$INTERFACE" | fgrep -q ":"; then - report_fail "Interface $INTERFACE does not exist." + if [[ ! -e /sys/class/net/"$INTERFACE" ]]; then + if ! echo "$INTERFACE"|grep ":"; then + err_append "interface $INTERFACE does not exist" return 1 fi fi # Kill any lingering wpa_supplicants. - report_debug wireless_up stop_wpa "$INTERFACE" - stop_wpa "$INTERFACE" + stop_wpa $INTERFACE # Most drivers (mac80211) need mode set before device is brought up # Drivers generally default to managed, but set this to be sure. - if [[ $(iwgetid -sm "$INTERFACE") -ne Managed ]]; then - report_debug wireless_up iwconfig "$INTERFACE" mode managed - iwconfig "$INTERFACE" mode managed - fi - - report_debug wireless_up ifup - wireless_control "$INTERFACE" up || return 1 - - quirk prescan && iwlist "$INTERFACE" scan &> /dev/null # bcm43xx - + if [[ "$(iwgetid -sm $INTERFACE)" -ne "Managed" ]]; then + iwconfig $INTERFACE mode managed + fi - if quirk preessid; then # ipw3945 - if [[ -n "$AP" ]]; then # JP: enable use of AP - # JP: Since I don't undertand why the else block below is an eval, I'm not sure - # if this command also needs to be eval'd - iwconfig "$INTERFACE" mode managed ap "$AP" - else - # JP: I don't understand why this needs to be an eval. What's wrong with just: - # iwconfig "$INTERFACE" mode managed essid "$ESSID" - eval "iwconfig \"$INTERFACE\" mode managed essid \"$ESSID\"" - fi + ifconfig $INTERFACE up - fi + quirk "prescan" && iwlist $INTERFACE scan &> /dev/null # bcm43xx + quirk "preessid" && eval "iwconfig $INTERFACE mode managed essid \"$ESSID\"" # ipw3945 - if checkyesno "${SCAN:-no}"; then - report_debug wireless_up scanning - local OLDESSID="$ESSID" - if [[ -n "$AP" ]]; then - ESSID=$(find_ap "$INTERFACE" "$AP") - else - ESSID=$(find_essid "$INTERFACE" "$ESSID" "$CONNECTION") # JP: we could have left $3 null for default of treating ESSID as literal - # but instead we explicitly pass $CONNECTION - fi - if [[ $? -gt 0 ]]; then - report_fail "Wireless network \"$OLDESSID\" not present." + if checkyesno ${SCAN:-no}; then + if ! find_essid $INTERFACE "$ESSID"; then + err_append "Network not present." return 1 fi fi # Manually set iwconfig options - if [[ -n "$IWCONFIG" ]]; then - report_debug wireless_up iwconfig "$INTERFACE" $IWCONFIG - iwconfig "$INTERFACE" $IWCONFIG - fi + [[ "$IWCONFIG" ]] && iwconfig $INTERFACE $IWCONFIG # Set to 'none' if not set - [[ -z "$SECURITY" ]] && SECURITY=none + [[ -z "$SECURITY" ]] && SECURITY="none" - case "$SECURITY" in + case $SECURITY in wep|none) # 'none' uses iwconfig like wep. Use sane default if WEP_OPTS="" - if [[ -z "$WEP_OPTS" ]]; then - if [[ "$SECURITY" = wep ]]; then - if [[ -n "$AP" ]]; then - WEP_OPTS="ap \"$AP\" key $KEY" # JP: formerly I had "...key open $KEY"; is it correct to omit the 'open'? - else - WEP_OPTS="essid \"$ESSID\" key $KEY" # JP: formerly I had "...key open $KEY"; is it correct to omit the 'open'? - fi - else # SECURITY=none - if [[ -n "$AP" ]]; then - WEP_OPTS="ap \"$AP\"" - else - WEP_OPTS="essid \"$ESSID\"" - fi - fi - fi + if [[ "$SECURITY" = "wep" ]]; then + WEP_OPTS="essid \"$ESSID\" key $KEY" + elif [[ "$SECURITY" = "none" ]]; then + WEP_OPTS="essid \"$ESSID\"" + fi - quirk predown && wireless_control "$INTERFACE" forcedown # madwifi FS#10585 + quirk "predown" && ifconfig $INTERFACE down # madwifi FS#10585 - report_debug wireless_up iwconfig "$INTERFACE" $WEP_OPTS - # JP: I don't understand why this needs to be an eval. What's wrong with just: - # iwconfig "$INTERFACE" $WEP_OPTS - if ! eval "iwconfig \"$INTERFACE\" $WEP_OPTS"; then - report_fail "Couldn't configure wireless." + if ! eval iwconfig $INTERFACE $WEP_OPTS; then + err_append "Could not set wireless configuration." return 1 fi - - - quirk predown && wireless_control "$INTERFACE" up # madwifi FS#10585 + quirk "predown" && ifconfig $INTERFACE up # madwifi FS#10585 - report_debug ethernet_up wep_check - wep_check "$INTERFACE" "$TIMEOUT" || return 1 + wep_check $INTERFACE $TIMEOUT||return 1 ;; - wpa) # Quirk for broken drivers... http://bbs.archlinux.org/viewtopic.php?id=36384 - if quirk wpaessid; then - if [[ -n "$AP" ]]; then - # JP: Since I don't undertand why the else block below is an eval, I'm not sure - # if this command also needs to be eval'd - iwconfig "$INTERFACE" ap "$AP" - else - # JP: I don't understand why this needs to be an eval. What's wrong with just: - # iwconfig "$INTERFACE" essid "$ESSID" - eval "iwconfig \"$INTERFACE\" essid \"$ESSID\"" - fi - fi + quirk "wpaessid" && eval iwconfig $INTERFACE essid "\"$ESSID\"" - local WPA_CONF="${TMPDIR:-/tmp}/wpa.${1// /}" # substitute spaces out - # make empty tmp dir with correct permissions, rename it - rm -rf "$WPA_CONF" - mv -f $(mktemp -d) "$WPA_CONF" || return 1 - echo "ctrl_interface=/var/run/wpa_supplicant" >> "$WPA_CONF/wpa.conf" # we know $WPA_CONF now has no spaces, but it may have other nasty chars, so still needs to be quoted - echo "ctrl_interface_group=${WPA_GROUP:-wheel}" >> "$WPA_CONF/wpa.conf" + local WPA_CONF="/tmp/wpa.${1// /}" # substitute spaces out + echo "ctrl_interface=/var/run/wpa_supplicant" >> $WPA_CONF + echo "ctrl_interface_group=${WPA_GROUP:-wheel}" >> $WPA_CONF + chmod 600 $WPA_CONF # Generate configuration - if [[ "${#KEY}" -eq 64 ]]; then - echo -e "network={ \nssid=\"$ESSID\" \npsk=$KEY \n}">> "$WPA_CONF/wpa.conf" - # JP: formerly I had { \nssid=\"$ESSID\" \nproto=WPA \nkey_mgmt=WPA-PSK \npsk=$KEY \n} - # JP: is what's above better? - elif ! echo "$KEY" | wpa_passphrase "$ESSID" >> "$WPA_CONF/wpa.conf"; then - report_fail "Couldn't generate WPA configuration." - cat "$WPA_CONF/wpa.conf" >&2 + if [[ "${#KEY}" == "64" ]]; then + echo -e "network={ \nssid=\"$ESSID\" \npsk=$KEY \n}">> $WPA_CONF + elif ! echo "$KEY" | wpa_passphrase "$ESSID" >> $WPA_CONF; then + err_append "Configuration generation failed. $(cat $WPA_CONF)" return 1 fi # Connect! [[ -z "$WPA_OPTS" ]] && WPA_OPTS="-Dwext" - report_debug wireless_up start_wpa "$INTERFACE" "$WPA_CONF/wpa.conf" "$WPA_OPTS" - start_wpa "$INTERFACE" "$WPA_CONF/wpa.conf" "$WPA_OPTS" || return 1 - report_debug wireless_up wpa_check - if ! wpa_check "$INTERFACE" "$TIMEOUT"; then - stop_wpa "$INTERFACE" + start_wpa $INTERFACE $WPA_CONF $WPA_OPTS || return 1 + if ! wpa_check $INTERFACE $TIMEOUT; then + stop_wpa $INTERFACE return 1 fi ;; wpa-config) - . "$SUBR_DIR/8021x" + . ${SUBR_DIR}/8021x [[ -z "$WPA_CONF" ]] && WPA_CONF="/etc/wpa_supplicant.conf" # defaults [[ -z "$WPA_OPTS" ]] && WPA_OPTS="-Dwext" - report_debug wireless_up start_wpa "$INTERFACE" "$WPA_CONF" "$WPA_OPTS" - start_wpa "$INTERFACE" "$WPA_CONF" "$WPA_OPTS" || return 1 - report_debug wireless_up wpa_check - if ! wpa_check "$INTERFACE" "$TIMEOUT"; then - stop_wpa "$INTERFACE" + start_wpa $INTERFACE $WPA_CONF $WPA_OPTS || return 1 + if ! wpa_check $INTERFACE $TIMEOUT; then + stop_wpa $INTERFACE return 1 fi ;; esac - if ! "$CONN_DIR/ethernet" up "$1"; then - wireless_down "$1" YES + if ! ${CONN_DIR}/ethernet up $1; then + wireless_down $1 YES return 1 fi } -# wireless_down PROFILE [ LEAVE ifconfig up? default no ] wireless_down() { - load_profile "$1" - . "$SUBR_DIR/8021x" - local PROFILE="$1" NOETHERNETDOWN="$2" # JP: made declarations local?? also, why introduce these vars if you're not going to use them? - if ! checkyesno "$NOETHERNETDOWN"; then - "$CONN_DIR/ethernet" down "$PROFILE" + load_profile $1 + . ${SUBR_DIR}/8021x + PROFILE=$1 NOETHERNETDOWN=$2 + if ! checkyesno $2; then + ${CONN_DIR}/ethernet down $1 fi - report_debug wireless_down stop_wpa "$INTERFACE" - stop_wpa "$INTERFACE" - [[ "$SECURITY" == wpa ]] && rm -rf "/tmp/wpa.${PROFILE// /}" # remove tmp wpa config - report_debug wireless_down iwconfig "$INTERFACE" essid off key off - iwconfig "$INTERFACE" essid off key off &> /dev/null - #wireless_control "$INTERFACE" forcedown # this is now handled in ethernet down, isn't it? + stop_wpa $INTERFACE + [[ "$SECURITY" == "wpa" ]] && rm -f "/tmp/wpa.${1// /}" # remove wpa config + iwconfig $INTERFACE essid off key off &> /dev/null + ifconfig $INTERFACE down } -# CONNECTION_query $INTERFACE [ profile | enabled (i.e. not rfkill-ed) | active (i.e. ifup'd) | associated | address ] [ "" | "-iproute" ] -# "query associated" also uses $4=ESSID $5=AP if known -wireless_query() { - local INTERFACE="$1" REQUEST="$2" NEW="$3" - #report_debug wireless_query "$@" - case "$REQUEST" in - - profile) - # return code = 1 if iface is available to be used - # return code = 0 if unavailable (disabled, or in use by another profile or by external config) - # echo "" | "disabled" | "external" | PROFILE_NAME - if [[ ! -f "$STATE_DIR/interfaces/$INTERFACE" ]]; then - # wireless interface is up but has no registered profile - if ! wireless_query "$INTERFACE" enabled "$NEW"; then - wireless_control disable "$INTERFACE" # STATE_DIR/interface should say "disabled" instead - # iface should be down if it''s disabled - wireless_control "$INTERFACE" down "$NEW" - echo "disabled" - return 0 - fi - if wireless_query "$INTERFACE" address "$NEW"; then - set_iface up "$INTERFACE" external - echo "external" - return 0 - fi - # no ip address, count as inactive (even if associated) - # should we tell iface to de-associate if still associated? - # should we ifconfig iface down? no, may be up for a reason - return 1 - - else # interface is up and thinks it's running a profile - . "$STATE_DIR/interfaces/$INTERFACE" # these files should only contain a PROFILE=... statement - case "$PROFILE" in - external) - echo "external" - return 0 - ;; - disabled) - # iface should be down if it's disabled - wireless_control "$INTERFACE" down "$NEW" - echo "disabled" - return 0 - ;; - *) - ( # use subshell to prevent overwriting currently loaded profile - . "$STATE_DIR/profiles/$PROFILE" - if ! "$CONN_DIR/$CONNECTION" query "$INTERFACE" enabled; then - report_warn "INTERFACE $INTERFACE is disabled; no longer being controlled by $PROFILE" - set_profile down "$PROFILE" - wireless_control disable "$INTERFACE" - wireless_control "$INTERFACE" down "$NEW" - echo "disabled" - exit 0 - elif ! wireless_query "$INTERFACE" address "$NEW"; then - report_warn "INTERFACE $INTERFACE is inactive; no longer being controlled by $PROFILE" - set_profile down "$PROFILE" - exit 1 - elif "$CONN_DIR/$CONNECTION" query "$INTERFACE" associated "$NEW" "$ESSID" "$AP"; then - echo "$PROFILE" # associated with loaded profile - exit 0 - else - report_warn "INTERFACE $INTERFACE no longer being controlled by $PROFILE" - set_profile down "$PROFILE" - set_iface up "$INTERFACE" external - echo "external" - exit 0 - fi - ) - # return $? - ;; - esac - fi - ;; - - enabled) - local rfkill iftype - for rfkill in /sys/class/rfkill/*; do - if [[ -e "$rfkill/type" && -e "$rfkill/state" ]]; then - iftype=$(cat "$rfkill/type") - if [[ "$iftype" == wlan || "$iftype" == "${INTERFACE:0:${#INTERFACE}-1}" ]]; then # JP: don't really know what the range of values for this is - # on my machine, the INTERFACE is wlan0 and the type=wlan - test $(cat "$rfkill/state") -eq 1 # state is 1 when enabled, 2 when killed - return $? - fi - fi - done -# # JP: this is easier, but I don't know if it can be relied on to stay in place... -# if [[ -e /sys/class/net/$INTERFACE/device/rfkill/rfkill?/state ]]; then -# test $(cat /sys/class/net/$INTERFACE/device/rfkill/rfkill?/state) -eq 1 -# return $? -# fi - return 0 # default to enabled - ;; - - associated) - # assumed to have ip address - # return 0: associated with loaded profile - # 1: associated with other - # 2: still has ip address but unassociated: can this happen? - local ESSID="$4" AP="$5" - local essid=$(iwgetid "$INTERFACE" -r) ap=$(iwgetid "$INTERFACE" -ra) - [[ -z "$ap" ]] && return 2 - if [[ -n "$AP" ]]; then - [[ $(echo "$AP" | tr 'abcdef' 'ABCDEF') == "$ap" ]] - return $? - fi - case "$ESSID" in - # [Rr][Oo][Aa][Mm]|[Oo][Pp][Ee][Nn]) - # return 0 - # ;; - *) - if [[ "$NEW" == -iproute ]]; then - # JP: ESSID is a regexp; we handle this here instead of in the wireless-dbus script for now - expr match "$essid" "^$ESSID\$" >/dev/null - return $? # returns true if matched more than 0 chars - else - [[ "$ESSID" == "$essid" ]] - return $? - fi - ;; - esac - ;; - - active|address) - "$CONN_DIR/ethernet$NEW" query "$INTERFACE" "$REQUEST" - ;; - - *) return 1;; - esac -} - -# CONNECTION_control $INTERFACE [ up | down | forcedown | enable | disable ] [ "" | "-iproute" ] -wireless_control() { - local INTERFACE="$1" ACTION="$2" NEW="$3" - #report_debug wireless_control "$@" - case "$ACTION" in - enable) - [[ $(query_iface "$INTERFACE") == disabled ]] && set_iface "$INTERFACE" down - ;; - disable) - set_iface up "$INTERFACE" disabled - ;; - up) - if [[ "$NEW" = -iproute ]]; then - "$CONN_DIR/ethernet-iproute" control "$INTERFACE" "$ACTION" - else - # JP: same as ethernet control ... up - #ifconfig "$INTERFACE" up 2>/dev/null - ifconfig "$INTERFACE" up - wireless_query "$INTERFACE" active - fi - ;; - down|forcedown) - if [[ "$NEW" = -iproute ]]; then - "$CONN_DIR/ethernet-iproute" control "$INTERFACE" "$ACTION" - else - # JP: differs slightly from ethernet control ... down (but I don't know whether the difference is really appropriate, I'm just preserving it from older codebase) - ifconfig "$INTERFACE" 0.0.0.0 - if [[ "$2" = forcedown ]]; then - ifconfig "$INTERFACE" down - fi - fi - ;; - *) return 1;; - esac -} - -wireless_verify() { - local INTERFACE="$1" - if [[ -d "/sys/class/net/$INTERFACE/phy80211" || -d "/sys/class/net/$INTERFACE/wireless" ]]; then - return 0 - else - return 1 - fi -} - -wireless_$1 "$2" "$3" "$4" "$5" "$6" +wireless_$1 $2 exit $? # vim: set ts=4 et sw=4: - diff --git a/src/globals b/src/globals deleted file mode 100644 index e227d51..0000000 --- a/src/globals +++ /dev/null @@ -1,118 +0,0 @@ -################################## -## -# /usr/lib/network/globals -## -################################## - -# JP: rather than declare these in several library files, we just declare them -# once here, so they only need to be changed at a single point - -# JP: added the /etc/network.d/hooks directory -# any +x files in that directory will be sourced when this file is sourced -# they can override any of the utility functions defined here for custom behavior -# (such as logging error messages to syslog, as I like to do) -# this lets us keep netcfg simple but gives it the flexibility for users -# to make modular use of it to do more complex things - - -### Globals -PROFILE_DIR="/etc/network.d/" -HOOKS_DIR="$PROFILE_DIR/hooks/" -SUBR_DIR="/usr/lib/network/" -CONN_DIR="${SUBR_DIR}/connections/" -STATE_DIR="/var/run/network/" - - -### Logging/Error reporting -# - -function report_err { - printhl "$*" -} - -function report_warn { - printhl "$*" -} - -function report_notify { - true -} - -function report_debug { - true -} - -function report_try { - stat_busy "$*" - REPORT_TRYING=1 -} - -function report_fail { - if [[ -n "$*" ]]; then - if [[ -n "$REPORT_TRYING" ]]; then - stat_append "- $*" - REPORT_TRYING= - stat_fail - else - printhl "$*" - fi - elif [[ -n "$REPORT_TRYING" ]]; then - REPORT_TRYING= - stat_fail - fi -} - -function report_success { - if [[ -n "$*" ]]; then - stat_append "- $*" - fi - stat_done -} - -### For calling scripts only; don't use in library functions - -function exit_stderr { echo "$*" >&2; exit 1; } -function exit_err { report_err "$*"; exit 1; } -function exit_fail { report_fail "$*"; exit 1; } - - - -function line_input { - # saves $IFS and noglob shell option (to restore them, eval the output of this function) - if [[ $(shopt -o noglob) =~ off ]]; then - printf "set +f; IFS=%q" "$IFS" - else - printf "set -f; IFS=%q" "$IFS" - fi - # now set $IFS and noglob to handle line-at-a-time input - set -f - IFS=$'\n' -} - -function load_hooks() { - ### Load any +x files in $HOOKS_DIR - - # JP: we need to process line-at-a-time input (don't want to assume path to HOOKS_DIR and its content are all single bash tokens) - # find ... | while read ... is no good because anything we do inside the while read ... subshell will be invisible to the outside calling context - -# # JP: this technique works in general (and is used elsewhere in the code) -# # <(cmd) creates a file descriptor whose content is the stdout of cmd, which we then use as stdin by saying "< <(cmd)" -# -# # JP: HOWEVER, for reasons I don't understand, this construct can't be sourced from the Python wireless-dbus script -# # Why bash can source the rest of the file but not that construct, just in that context, eludes me -# if [[ -d "$HOOKS_DIR" ]]; then -# while read h; do -# source "$h" -# done < <(find -L "$HOOKS_DIR/" -maxdepth 1 -type f -executable) -# fi - - # JP: this might be a more elegant way to process input line-at-a-time without a pipe, anyway - local h OLDIFS=$(line_input) - for h in $(find -L "$HOOKS_DIR/" -maxdepth 1 -type f -executable | sort); do - source "$h" - done - eval $OLDIFS -} - -load_hooks - diff --git a/src/net-profiles b/src/net-profiles index a4b964b..67ab5e8 100644 --- a/src/net-profiles +++ b/src/net-profiles @@ -2,17 +2,17 @@ . /etc/rc.conf . /etc/rc.d/functions -. /usr/lib/network/globals case "$1" in start) if ! ck_daemon net-profiles; then - exit_stderr "net-profiles is already running: try \"/etc/rc.d/net-profiles restart\"" + echo "net-profiles has already been started. Try '/etc/rc.d/net-profiles restart'" + exit fi # Ensure any device renaming has occurred as intended for daemon in "${DAEMONS[@]}"; do - if [[ "$daemon" = "${daemon#!}" && "$daemon" = net-rename ]]; then + if [ "$daemon" = "${daemon#!}" -a "$daemon" = "net-rename" ]; then if ck_daemon net-rename; then /etc/rc.d/net-rename start fi @@ -20,65 +20,40 @@ case "$1" in done # $NET env var is passed from the kernel boot line - # JP: is that true? or does $NET come from /etc/rc.conf? - [[ -z "$NETWORKS_MENU_TIMEOUT" ]] && NETWORKS_MENU_TIMEOUT=5 + [ ! "$NETWORKS_MENU_TIMEOUT" ] && NETWORKS_MENU_TIMEOUT=5 - if [[ "$NET" = menu ]]; then - if /usr/bin/netcfg-menu "$NETWORKS_MENU_TIMEOUT"; then - mv "$STATE_DIR"/{menu,net-profiles} # JP: user may want to disconnect profile by calling net-profiles stop - add_daemon net-profiles - exit 0 - fi - elif [[ -n "$NET" ]]; then - if /usr/bin/netcfg check-iface "$NET"; then - echo "$NET" > "$STATE_DIR/net-profiles" # JP: user may want to disconnect profile by calling net-profiles stop - add_daemon net-profiles - exit 0 - fi - else + if [[ "$NET" = "menu" ]]; then + /usr/bin/netcfg-menu $NETWORKS_MENU_TIMEOUT + elif [[ "$NET" ]]; then + /usr/bin/netcfg2 -c $NET + else # No NET= passed at boot, go to NETWORKS=() - for network in "${NETWORKS[@]}"; do - case "$network" in + for network in ${NETWORKS[@]}; do + case $network in menu) # Pull up menu of networks - if /usr/bin/netcfg-menu "$NETWORKS_MENU_TIMEOUT"; then - mv "$STATE_DIR"/{menu,net-profiles} # JP: user may want to disconnect profile by calling net-profiles stop - add_daemon net-profiles - exit 0 - fi - break # if netcfg-menu was called but failed: exit for loop + /usr/bin/netcfg-menu $NETWORKS_MENU_TIMEOUT ;; *) # Either interface or profile - if [[ "$network" = "${network#!}" ]]; then # otherwise profile - if /usr/bin/netcfg check-iface "$network"; then - echo "$network" > "$STATE_DIR/net-profiles" # JP: user may want to disconnect profile by calling net-profiles stop - add_daemon net-profiles - exit 0 - fi - break # found an enabled profile but failed to bring it up: exit for loop + if [ "$network" = "${network#!}" ]; then # otherwise profile + /usr/bin/netcfg2 -c $network fi ;; esac done - fi + fi - exit_err "No profile started." # JP: don't add_daemon unless we were successful (above) + add_daemon net-profiles ;; stop) - if ck_daemon net-profiles; then - exit_stderr "net-profiles not running" - fi - # shutdown any profiles started by netcfg (or from NET_PROFILES in rc.conf) - # JP: only attempt to disconnect the profiles we're controlling - cat "${state_DIR}/net-profiles" 2>/dev/null | xargs -d'\n' /usr/bin/netcfg down # JP: use xargs in case any of the profile names contain spaces etc - rm -f "$STATE_DIR/net-profiles" + /usr/bin/netcfg2 -a rm_daemon net-profiles ;; restart) - "$0" stop; sleep 1; "$0" start + $0 stop; sleep 1; $0 start ;; *) - exit_stderr "Usage: $0 {start|stop|restart}" + echo "usage: $0 {start|stop|restart}" esac # vim: set ts=4 et sw=4: diff --git a/src/net-rename b/src/net-rename index 1cff1c1..db2c461 100644 --- a/src/net-rename +++ b/src/net-rename @@ -2,13 +2,12 @@ . /etc/rc.conf . /etc/rc.d/functions -. /usr/lib/network/globals case "$1" in start) - report_try "Renaming network devices" + stat_busy "Renaming network devices" ifrename -p -t - report_success + stat_done add_daemon net-rename ;; @@ -18,10 +17,9 @@ case "$1" in /bin/true ;; restart) - "$0" start + $0 start ;; *) - exit_stderr "Usage: $0 {start|stop|restart}" - ;; + echo "usage: $0 {start|stop|restart}" esac # vim: set ts=4 et sw=4: diff --git a/src/netcfg b/src/netcfg index 8f03ea2..3222d46 100644 --- a/src/netcfg +++ b/src/netcfg @@ -4,7 +4,7 @@ . /etc/rc.d/functions . /usr/lib/network/network -NETCFG_VER=2.3.0b1 +NETCFG_VER=2.2.1 version() { @@ -18,69 +18,45 @@ usage() echo " Start specified profile: netcfg profile " echo " Other functions: netcfg argument profile" echo "Arguments:" - echo " current Report currently running profiles" echo "-a, all-down Take all active profiles down" echo "-c, check-iface Do not start profile if interface is already up" echo "-d, down Take specified profile down" echo "-h, help This help message" echo "-i, iface-down Take down profile active on specified interface" - echo "-l, list List all available profiles" echo "-r, reconnect Disconnect and reconnect specified profile" echo "-u, up Start specified profile" echo "-v, version Output version information and exit" echo " all-resume Resume previously suspended profiles and reconnect them" echo " all-suspend Store a list of current running profiles and suspend them" - exit 1 } -# TODO: rewrite with getopts from BashFAQ +# TODO: Re-add ROOT check and rewrite with getopts from BashFAQ -case "$1" in +case $1 in --version|-v|version) - version - exit 0;; + version;; --help|-h|help) usage;; - list|-l) - echo "Available Profiles" - echo "------------------" - list_profiles - exit 0;; - current) - if [[ -d "$STATE_DIR/profiles/" ]]; then - ls "$STATE_DIR/profiles/" - exit 0 - else - exit_err "No active profiles." - fi;; -esac - -if [[ $(id -u) -gt 0 ]]; then - exit_stderr "This script should be run as root." -fi - -case "$1" in - -c|check-iface|-u|up) - CHECK=YES - profile_up "$2";; + CHECK="YES"; + profile_up $2;; clean) - rm "$STATE_DIR/interfaces"/* 2> /dev/null - rm "$STATE_DIR/profiles"/* 2> /dev/null - rm "$STATE_DIR/suspend"/* 2> /dev/null - rm "$STATE_DIR/last_profile" 2> /dev/null + rm /var/run/network/interfaces/* 2> /dev/null + rm /var/run/network/profiles/* 2> /dev/null + rm /var/run/network/suspend/* 2> /dev/null + rm /var/run/network/last_profile 2> /dev/null killall wpa_supplicant 2> /dev/null killall dhcpcd 2> /dev/null ;; -d|down) - profile_down "$2";; + profile_down $2;; -i|iface-down) - interface_down "$2";; + interface_down $2;; -a|all-down) all_down;; -r|reconnect) - profile_down "$2" - profile_up "$2";; + profile_down $2 + profile_up $2;; all-resume) all_resume;; all-suspend) @@ -88,8 +64,8 @@ case "$1" in -*|--*) usage;; *) - if [[ -n "$1" ]]; then - profile_up "$1" + if [ -n "$1" ]; then + profile_up $1 else usage fi diff --git a/src/netcfg-menu b/src/netcfg-menu index 70acf5a..8c58edc 100644 --- a/src/netcfg-menu +++ b/src/netcfg-menu @@ -4,58 +4,57 @@ . /etc/rc.d/functions . /usr/lib/network/network -# JP: we'll use $STATE_DIR/menu to record what profile is being connected in this way -rm -f "$STATE_DIR/menu" # Scan all profiles i=0 -while read prof; do +for prof in `find -L $PROFILE_DIR -maxdepth 1 -type f -printf "%f\n"|sort`; do # if there is a profile called "main", Use as default - [[ "$prof" = main ]] && DEFAULT=main - unset DESCRIPTION # JP: we cant sandbox the sourced profiles, because we need to expose the profiles array - . "$PROFILE_DIR/$prof" - profiles[$i]="$prof" - let i++ - profiles[$i]="$DESCRIPTION" - let i++ -done < <(list_profiles | sort) # JP: re-use list_profiles instead of duplicating it; avoid subshell we'd get by piping it to the while read... - -if [[ ${#profiles} -eq 0 ]]; then - exit_err "No profiles were found in $PROFILE_DIR" + [ "$prof" = "main" ] && DEFAULT=$prof + unset DESCRIPTION + . $PROFILE_DIR/$prof + profiles[$i]=$prof + i=$((i+1)) + profiles[$i]=$DESCRIPTION + i=$((i+1)) +done + +if [ ${#profiles} -eq 0 ]; then + echo "No profiles were found in $PROFILE_DIR" + return 1 fi -[[ -n "$NETWORKS_MENU_DEFAULT" ]] && DEFAULT="$NETWORKS_MENU_DEFAULT" # if no default yet, use the first entry -[[ -z "$DEFAULT" ]] && DEFAULT="${profiles[0]}" -ANSWER=$(mktemp /tmp/menu.XXXXXXXX) || exit 1 +[ "$NETWORKS_MENU_DEFAULT" ] && DEFAULT="$NETWORKS_MENU_DEFAULT" + +[ "$DEFAULT" = "" ] && DEFAULT=${profiles[0]} +ANSWER=$(mktemp) || exit 1 # Set timeout -TIMEOUT="${1:-0}" # JP: equivalent to the block that was here before +if [ "$1" = "" ]; then + TIMEOUT="0" +else + TIMEOUT="$1" +fi # Display Dialog -dialog --timeout "$TIMEOUT" --default-item "$DEFAULT" \ +dialog --timeout $TIMEOUT --default-item $DEFAULT \ --menu "Select the network profile you wish to use" \ - 13 50 6 "${profiles[@]}" 2> $ANSWER + 13 50 6 "${profiles[@]}" 2>$ANSWER ret=$? case $ret in 1) ;; # Cancel - do nothing 255) # timeout - use default - profile_up "$DEFAULT" # JP: use profile_up and catch $? - ret=$? - if [[ $ret -eq 0 ]]; then echo "$DEFAULT" > "$STATE_DIR/menu"; fi + netcfg2 $DEFAULT ;; 0) # User selection - profile_up "$(cat $ANSWER)" - ret=$? - if [[ $ret -eq 0 ]]; then mv $ANSWER "$STATE_DIR/menu"; fi + netcfg2 $(cat $ANSWER) ;; - *) # Shouldn't happen - echo "Abnormal ret code from dialog: $ret" >&2 + *) # Shouldnt happen + echo "Abnormal ret code from dialog: $ret" ;; esac -rm -f $ANSWER # JP: add -f -exit $ret # JP: exit with caught $? +rm $ANSWER # vim: set ts=4 et sw=4: diff --git a/src/network b/src/network index 2720caa..b45caaa 100644 --- a/src/network +++ b/src/network @@ -1,402 +1,284 @@ -################################## -## -# /usr/lib/network/network -## -################################## +### Globals +PROFILE_DIR="/etc/network.d/" +SUBR_DIR="/usr/lib/network/" +CONN_DIR="${SUBR_DIR}/connections/" +STATE_DIR="/var/run/network/" -. /usr/lib/network/globals +### Messages +## +# err msg +# output specified message +err_append() { + echo " - $*" +} +err() { + printhl "$*" +} -### Profile listing and loading +### Profile loading ## -# list_profiles -# Outputs a list of all profiles -list_profiles() { - # JP: follow aliases with -L, also skip profiles that start with '.' or end with '~' or '.conf' (so profile.conf can be the wpa.conf file for profile) - find -L "$PROFILE_DIR/" -maxdepth 1 -type f -not -name '*~' -not -name '*.conf' -not -name '.*' -printf "%f\n" -} # load_profile profile # source the profile load_profile() { - validate_profile "$1" || return 1 - . "$PROFILE_DIR/$1" + validate_profile $1 || return 1 + . $PROFILE_DIR/$1 } # validate_profile profile # check whether profile exists and is usable -validate_profile() { +validate_profile() +{ [[ -z "$1" ]] && return 1 - if [[ ! -f "$PROFILE_DIR/$1" ]]; then - report_err "Profile \"$1\" does not exist." + if [[ ! -f $PROFILE_DIR/$1 ]]; then + err "Profile \"$1\" does not exist" return 1 fi - . "$PROFILE_DIR/$1" + . $PROFILE_DIR/$1 if [[ -z "$INTERFACE" ]]; then - report_err "Profile error: no INTERFACE to configure." + err "Profile missing an interface to configure" return 1 fi - if [[ ! -f "$CONN_DIR/$CONNECTION" ]]; then - report_err "Profile error: $CONNECTION is not a valid connection, check spelling or look at examples." + if [[ ! -f $CONN_DIR/$CONNECTION ]]; then + err "$CONNECTION is not a valid connection, check spelling or look at examples" return 1 fi } -### Profile suspend/resume +### Profile up/down ## -# interface_suspend interface/all [call_profile_down? default=yes] -# store a list of running profiles and take them down (unless $2 is no) -interface_suspend() { - report_debug interface_suspend "$@" - - [[ ! -d "$STATE_DIR" ]] && mkdir -p "$STATE_DIR"/{interfaces,profiles} - [[ ! -d "$STATE_DIR/suspend" ]] && mkdir "$STATE_DIR/suspend" - - find "$STATE_DIR/profiles/" -maxdepth 1 -type f -printf '%f\n' \ - | while read prof; do - # the pipe to "while read" will create a subshell, so sourced variables will already be in a sandbox - # we just need to clear INTERFACE which is all we care about - unset INTERFACE - . "$STATE_DIR/profiles/$prof" - if [[ "$1" == all || "$1" == "$INTERFACE" ]]; then - report_notify "suspending interface $INTERFACE with profile $prof" - cp "$STATE_DIR/profiles/$prof" "$STATE_DIR/suspend/" - if checkyesno "${2:-yes}"; then - profile_down "$prof" - fi - fi +# all_down +# take all profiles down +# +all_down() +{ + for prof in $(find ${STATE_DIR}/profiles/ -maxdepth 1 -type f -printf '%f\n'); do + profile_down $prof done } + # all_suspend # store a list of running profiles and take them down -all_suspend() { - interface_suspend all +# +all_suspend() +{ + [[ ! -d $STATE_DIR ]] && mkdir -p $STATE_DIR/{interfaces,profiles} + [[ ! -d $STATE_DIR/suspend ]] && mkdir $STATE_DIR/suspend + + for prof in $(find $STATE_DIR/profiles/ -maxdepth 1 -type f); do + cp $prof $STATE_DIR/suspend/ + profile_down $(basename $prof) + done } -# all_resume -# resume suspended interfaces -# optional arguments: interfaces not to resume (e.g., because they're disabled) -all_resume() { - report_debug all_resume "$@" - find "$STATE_DIR/suspend/" -maxdepth 1 -type f -printf '%f\n' \ - | while read prof; do - # the pipe to "while read" will create a subshell, so sourced variables will already be in a sandbox - # we just need to clear INTERFACE which is all we care about - unset INTERFACE - . "$STATE_DIR/suspend/$prof" - if [[ $# -eq 0 || ! " $* " =~ " $INTERFACE " ]]; then - report_notify "resuming interface $INTERFACE with profile $prof" - profile_up "$prof" - rm -f "$STATE_DIR/suspend/$prof" # if profile_up succeeds, it will have already removed this - fi +# all_suspend +# store a list of running profiles and take them down +# +all_resume() +{ + for prof in $(find $STATE_DIR/suspend/ -maxdepth 1 -type f); do + profile_up $(basename $prof) + rm $prof done } -### Profile up/down -## -# profile_up profile [literal essid] +# profile_up profile # put all profiles up # -profile_up() { +profile_up() +{ ( # Keep inside subshell so that options from one profile don't cross to others - # exit 1 used in a subshell is effectively exiting a new process - [[ ! -d "$STATE_DIR" ]] && mkdir -p "$STATE_DIR"/{interfaces,profiles,suspend} - - local status PROFILE="$1" # save PROFILE in a variable so that it's available to PREUP/POSTDOWN etc hooks + # exit 1 used as a subshell is effectively a new process + [[ ! -d $STATE_DIR ]] && mkdir -p $STATE_DIR/{interfaces,profiles} - load_profile "$PROFILE" || exit 1 + load_profile $1 || exit 1 - if check_profile "$PROFILE"; then - report_err "$PROFILE already connected." - exit 1 - fi + check_profile $1 && err "$1 already connected" && exit 1 # NETWORKS_EXCLUSIVE, rc.conf: Profiles are globally mutually exclusive # EXCLUSIVE, network.d/profile: Individual profile is mutually exclusive - if checkyesno "$NETWORKS_EXCLUSIVE" || checkyesno "$EXCLUSIVE"; then + if checkyesno $NETWORKS_EXCLUSIVE || checkyesno $EXCLUSIVE; then all_down fi - report_try "$PROFILE up" + stat_busy "$1 up" - status=$(query_iface "$INTERFACE" "$CONNECTION") - report_debug "status reported to profile_up as: $status" - - if [[ -n "$status" && "$status" != "$PROFILE" ]]; then - case "$status" in - disabled) - # interface radio-switch disabled - report_fail "INTERFACE $INTERFACE is disabled." - exit 1 - ;; - external) - # interface being controlled externally - report_fail "INTERFACE $INTERFACE was configured by another application." - exit 1 - ;; - *) - # report_fail if iface in use by another profile? (default no) - if checkyesno "$CHECK"; then - report_fail "INTERFACE $INTERFACE already in use by another profile." - exit 1 - elif ! ( # use subshell so interface_down -> profile_down doesn't overwrite loaded profile - interface_down "$INTERFACE" "$CONNECTION" # supply current CONNECTION-type as hint - ); then - report_fail - exit 1 - fi - ;; - esac - fi + if check_iface $INTERFACE; then + if checkyesno $CHECK; then + err_append "Interface $INTERFACE already in use" + stat_fail && exit 1 + else + interface_down $INTERFACE || exit 1 + load_profile $1 + fi + fi - - if ! ( eval $PRE_UP ); then # JP: sandbox the eval so variables don't bleed into current function - report_debug profile_up "PRE_UP failed" - report_fail - exit 1 - fi + eval $PRE_UP || exit 1 - if ! "$CONN_DIR/$CONNECTION" up "$PROFILE" "$2"; then - report_debug profile_up "connect failed" - report_fail - # "$CONN_DIR/$CONNECTION" down "$PROFILE" "$2" # JP: should we do this to make sure? + if ! ${CONN_DIR}/${CONNECTION} up $1; then + stat_fail exit 1 fi - if ! ( eval $POST_UP ); then # JP: sandbox the eval - report_debug profile_up "POST_UP failed" - report_fail - # failing POST_UP will take interface down - "$CONN_DIR/$CONNECTION" down "$PROFILE" "$2" - exit 1 - fi + eval $POST_UP || exit 1 - set_profile up "$PROFILE" - # unset EXCLUSIVE - report_success + set_profile up $1 + unset EXCLUSIVE + + stat_done ); return $? } + # profile_down profile # take profile down # -profile_down() { +profile_down() +{ ( - [[ ! -d "$STATE_DIR" ]] && mkdir -p "$STATE_DIR"/{interfaces,profiles,suspend} + [[ ! -d $STATE_DIR ]] && mkdir -p $STATE_DIR/{interfaces,profiles} - local status PROFILE="$1" # save PROFILE in a variable so that it's available to PREUP/POSTDOWN etc hooks - - load_profile "$PROFILE" || exit 1 - - status=$(query_iface "$INTERFACE" "$CONNECTION") - report_debug "status reported to profile_down as: $status" + load_profile $1 || exit 1 - if [[ "$status" != "$PROFILE" ]]; then - # if interface not available to be controlled by netcfg, then - # any profiles will have been removed by check_iface - # else another profile is running - report_err "Profile wasn't connected." - exit 1 - fi + if ! check_profile $1; then + err "Profile not connected" + exit 1 + fi - report_try "$PROFILE down" -# JP: the following will have already been caught by the block above... -# if [[ "$status" == external ]]; then -# report_fail "Interface $INTERFACE was configured by another application." -# exit 1 -# fi + stat_busy "$1 down" + if [[ "$(get_iface_prof $INTERFACE)" == "external" ]]; then + err_append "$interface was connected by another application" + stat_fail + exit 1 + fi - if ! ( eval $PRE_DOWN ); then # JP: sandbox the eval - report_debug profile_down "PRE_DOWN failed" - true # JP: did we want failing PRE_DOWN to leave the profile active? - # report_fail - # exit 1 - fi + eval $PRE_DOWN || exit 1 - if ! "$CONN_DIR/$CONNECTION" down "$PROFILE" "$2"; then - report_debug profile_up "disconnect failed" - report_fail + if ! ${CONN_DIR}/${CONNECTION} down $1; then + stat_fail exit 1 fi - if ! ( eval $POST_DOWN ); then # JP: sandbox the eval - report_debug profile_down "POST_DOWN failed" - report_fail - exit 1 - fi + eval $POST_DOWN || exit 1 - set_profile down "$PROFILE" - report_success + set_profile down $1 + stat_done ); return $? } -# all_down -# take all registered profiles down -all_down() { - find "$STATE_DIR/profiles/" -maxdepth 1 -type f -printf '%f\n' \ - | while read prof; do - profile_down "$prof" - done -} -# interface_down interface -# take interface down -# -interface_down() { - local status=$(query_iface "$@") # we may not be hinting what CONNECTION type to use, e.g. when called from /usr/bin/netcfg - case "$status" in - none|disabled) return 0;; - external) return 1;; - *) profile_down "$status";; - esac -} - -### Status setting functions -## -# set_profile up/down profile -# Set profile state, either up or down -# -set_profile() { - if [[ "$1" == up ]]; then - ( # subshell creates sandbox for sourced variables - . "$PROFILE_DIR/$2" - cp "$PROFILE_DIR/$2" "$STATE_DIR/profiles/" - echo "$2" > "$STATE_DIR/last_profile" - set_iface up "$INTERFACE" "$2" - ) - elif [[ "$1" == down && -f "$STATE_DIR/profiles/$2" ]]; then - ( # subshell - . "$STATE_DIR/profiles/$2" - rm "$STATE_DIR/profiles/$2" - set_iface down "$INTERFACE" "$2" - ) +# Check if variable is a member of an array +inarray() +{ +search=$1 +shift +for item in $*; do + if [[ "$item" == "$search" ]]; then + return 0 fi +done +return 1 } -# set_iface up/down interface [profile/disabled/external] -# Set interface status to up/down (really up means "unavailable" (may be down & disabled) and down means "available" -# optionally link it to a profile. -# -set_iface() { - local PROFILE="${3:-external}" # JP: this is equivalent to the two-line block that was here before - if [[ "$1" == up ]]; then - echo "PROFILE=$PROFILE" > "$STATE_DIR/interfaces/$2" - elif [[ "$1" == down ]]; then - rm -f "$STATE_DIR/interfaces/$2" # JP: add -f so we don't complain if the interface isn't up - fi +quirk() { +inarray $1 ${QUIRKS[@]} +return $? } +# interface_down interface +# take interface down +# +interface_down() +{ + local prof=$(get_iface_prof $1) + profile_down $prof + return $? +} - -### Checking/querying functions +### Query functions ## # check_iface interface # Return 0 if interface up # Return 1 if interface down # check_iface() { - [[ -f "$STATE_DIR/interfaces/$1" ]] && return 0 - return 1 + [[ -f $STATE_DIR/interfaces/$1 ]] && return 0 + return 1 } -############################################################################ -# Note: get_iface_prof left in for compatibility with any external code that calls it -# but in this codebase, it's superceded by query_iface # get_iface_prof interface # Echo interface profile and return 0 if up # Return 1 if down. # get_iface_prof() { - if check_iface "$1"; then - . "$STATE_DIR/interfaces/$1" + if check_iface $1; then + . $STATE_DIR/interfaces/$1 echo $PROFILE else return 1 fi } -############################################################################ + +# list_profiles +# Outputs a list of all profiles +list_profiles() { + find $PROFILE_DIR/ -maxdepth 1 -type f -printf "%f\n" +} # check_profile profile -# Return 0 if profile registered as being up -# Return 1 if profile not registered +# Return 0 if profile up +# Return 1 if profile down # check_profile() { - [[ -f "$STATE_DIR/profiles/$1" && ! -f "$STATE_DIR/suspend/$1" ]] && return 0 + [[ -f $STATE_DIR/profiles/$1 ]] && return 0 return 1 -} - - -# query_iface interface [connection] -# Return 0 if interface up or otherwise unavailable for connecting -# Return 1 if interface down (and so free to be connected) -# echoes which profile controls the interface (or "external" or "disabled") - -query_iface() { - - local INTERFACE="$1" PROFILE connection="$2" - - if [[ -z "$connection" ]]; then # JP: the difficulty with factoring this code into the CONNECTION routines - # is that we sometimes want to query/control the interface without knowing what CONNECTION type it instantiates - # so we supply the CONNECTION type as a hint ($2) when possible - # and when CONNECTION wasn't hinted, we default to wireless-dbus if iface verifies as wireless, else ethernet-iproute - if "$CONN_DIR/wireless-dbus" verify "$INTERFACE"; then - connection=wireless-dbus - else - connection=ethernet-iproute - fi - fi - - if ! "$CONN_DIR/$connection" query "$INTERFACE" active; then - # interface is not up - if [[ -f "$STATE_DIR/interfaces/$INTERFACE" ]]; then # but the interface thinks it's running a profile... - . "$STATE_DIR/interfaces/$INTERFACE" # these files should only contain a PROFILE=... statement - if [[ "$PROFILE" == disabled ]]; then - echo "disabled" - return 0 - elif [[ "$PROFILE" != external ]]; then - report_warn "INTERFACE $INTERFACE no longer being controlled by $PROFILE" - set_profile down "$PROFILE" - PROFILE= - fi - set_iface down "$INTERFACE" # update iface status: no longer profile/external - fi - echo "$PROFILE" - return 1 - else - "$CONN_DIR/$connection" query "$INTERFACE" profile - return $? - fi +} -} - - -### Utility functions +### Status setting functions ## -# Check if variable is a member of an array -inarray() { - local item search="$1" - shift - for item; do - if [[ "$item" == "$search" ]]; then - return 0 - fi - done - return 1 +# set_profile up/down profile +# Set profile state, either up or down +# +set_profile() { + if [[ "$1" == "up" ]]; then + . $PROFILE_DIR/$2 + cp $PROFILE_DIR/$2 $STATE_DIR/profiles/ + echo $2 > $STATE_DIR/last_profile + set_iface up $INTERFACE $2 + + elif [[ "$1" == "down" ]]; then + . $STATE_DIR/profiles/$2 + rm $STATE_DIR/profiles/$2 + set_iface down $INTERFACE $2 + fi } -quirk() { - inarray "$1" "${QUIRKS[@]}" +# set_iface up/down interface [profile] +# Set interface status to up/down +# optionally link it to a profile. +# +set_iface() { + PROFILE=$3 + [[ -z "$PROFILE" ]] && PROFILE=external + if [[ "$1" == "up" ]]; then + echo "PROFILE=$PROFILE" > $STATE_DIR/interfaces/$2 + elif [[ "$1" == "down" ]]; then + rm $STATE_DIR/interfaces/$2 + fi } + ### From FreeBSD's /etc/rc.subr ## # checkyesno var # Test $1 variable, and warn if not set to YES or NO. # Return 0 if it's "yes" (et al), nonzero otherwise. # -checkyesno() { - local _value="$1" +checkyesno() +{ + _value=${1} #debug "checkyesno: $1 is set to $_value." - case "$_value" in + case $_value in # "yes", "true", "on", or "1" [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) @@ -413,5 +295,3 @@ checkyesno() { ;; esac } - - diff --git a/src/wireless_utils b/src/wireless_utils deleted file mode 100644 index d27c43b..0000000 --- a/src/wireless_utils +++ /dev/null @@ -1,141 +0,0 @@ -################################## -## -# /usr/lib/network/wireless_utils -## -################################## - -# Uses wireless_tools, to check for association to a network. -# wep_check interface [timeout] -wep_check() { - local INTERFACE="$1" TIMEOUT="${2:-15}" timeout=0 bssid - - while [[ "$timeout" -lt "$TIMEOUT" ]]; do - bssid=$(iwgetid "$INTERFACE" -ra) - [[ -n "$bssid" && "$bssid" != "00:00:00:00:00:00" ]] && return 0 - sleep 1 - let timeout++ - done - - report_fail "Couldn't associate with wireless network." - return 1 -} - -# Check if a particular network is within range -# find_essid interface essid connection (we treat ESSID as regexp when CONNECTION=wireless-dbus) -find_essid() { - local INTERFACE="$1" ESSID="$2" CONNECTION="$3" RETRIES=20 try=0 res scanned - while [[ "$try" -lt "$RETRIES" ]]; do - sleep 0.5 - let try++ - if [[ "$CONNECTION" == wireless-dbus ]]; then - # JP: ESSID is a regexp - found=$( - res=$(iwlist "$INTERFACE" scan 2>/dev/null) - [[ -z "$res" ]] && exit 1 - # if results were non-null, process them and exit 0 - echo "$res" | sed -nr 's/^\s+ESSID:"([^"]*)"$/\1/p' | egrep -xm1 "$ESSID" - ) - else - found=$( - res=$(iwlist "$INTERFACE" scan 2>/dev/null) - [[ -z "$res" ]] && exit 1 - # if results were non-null, process them and exit 0 - echo "$res" | sed -nr 's/^\s+ESSID:"([^"]*)"$/\1/p' | fgrep -xm1 "$ESSID" - ) - fi && { - scanned=1 - report_debug find_essid "\"$found\"" - # we only bother with at most 5 successful scans - if (( try < RETRIES-4 )); then try=$((RETRIES-4)); fi - } - if [[ -n "$found" ]]; then - echo "$found" # JP: echo literal ESSID - return 0 # network found - fi - done - if [[ "$scanned" -ne 1 ]]; then - report_debug find_essid "unable to scan" - fi - return 1 -} - -# Check if a particular network is within range -# find_ap interface ap -find_ap() { - local INTERFACE="$1" ap=$(echo "$2" | tr 'abcdef' 'ABCDEF') RETRIES=20 try=0 res scanned - while [[ "$try" -lt "$RETRIES" ]]; do - sleep 0.5 - let try++ - found=$( - res=$(iwlist "$INTERFACE" scan 2> /dev/null) - [[ -z "$res" ]] && exit 1 - # if results were non-null, process them and exit 0 - echo "$res" | sed -nr '/^\s+Cell .. - Address: ([[:xdigit:]:]+)$/ { s//\1/; N; s/(.*)\n\s+ESSID:"([^"]*)"$/\1\t\2/p }' \ - | egrep -m1 "^$ap\t" - ) && { - scanned=1 - report_debug find_ap "\"$found\"" - # we only bother with at most 5 successful scans - if (( try < RETRIES-4 )); then try=$((RETRIES-4)); fi - } - if [[ -n "$found" ]]; then - echo "$found" | cut -f2 # JP: echo literal ESSID - return 0 - fi - done - if [[ "$scanned" -ne 1 ]]; then - report_debug find_ap "unable to scan" - fi - return 1 -} - -# Return a filename containing a list of network APs and ESSIDs found (sorted by decreasing signal strength) -# list_networks interface -list_networks() { - local INTERFACE="$1" essids try=0 RETRIES=20 res scanned - # temp file used, as keeping ESSID's with spaces in their name in arrays - # is hard, obscure and kinda nasty. This is simpler and clearer. - - [[ -z "$INTERFACE" ]] && return 1 - essids=$(mktemp /tmp/essid.XXXXXXXX) - -# # James suggested using this, but it requires wpa_supplicant to be running -# wpa_cli -i "$INTERFACE" scan 2>/dev/null || { rm $essids; return 1; } -# sleep 0.5 -# wpa_cli -i "$INTERFACE" scan_results > $essids 2>/dev/null || { rm $essids; return 1; } - - { - while [[ "$try" -lt "$RETRIES" ]]; do - sleep 0.5 - let try++ - # iwlist "$INTERFACE" scan 2> /dev/null | fgrep "ESSID" | sed 's/.*ESSID:"\([^"]\+\)".*/\1/' > $essids - res=$(iwlist "$INTERFACE" scan 2> /dev/null) - [[ -z "$res" ]] && continue - scanned=1 - # we only bother with at most 5 successful scans - if (( try < RETRIES-4 )); then try=$((RETRIES-4)); fi - echo "$res" | sed -r '1d; $ { H; ba }; 2 { h; d }; /^\s+Cell /! { H; d }; :a; x; s/\n/ /g' - done - } \ - | sed -rne 's/.*Address: ([[:xdigit:]:]+).*ESSID:"([^"]*)".*Quality=([0-9]+).*/\1\t\3\t\1\t\2/p' \ - -e 's/.*Address: ([[:xdigit:]:]+).*Quality=([0-9]+).*ESSID:"([^"]*)".*/\1\t\2\t\1\t\3/p' \ - | sort -k1 -k2nr | uniq -w17 \ - | sort -k2nr \ - | cut -f3- > "$essids" - # 1. make tab-separated fields: ap, signal-strength, ap, essid (easiest way to use uniq and cut here requires ap twice) - # 2. eliminate duplicate aps (keeping strongest signal) - # 3. sort entire list by decreasing signal - # 4. then return tab-separated fields: ap, essid (ap needs to come first so that read can assume format is ) -# - # File of 0 length, ie. no ssid's. - if [[ ! -s $essids ]]; then - rm $essids - return 1 - fi - - echo $essids - return 0 -} - - -# vim: set ts=4 et sw=4 ft=sh: -- cgit v1.2.3-24-g4f1b