~ubuntu-branches/ubuntu/lucid/wpasupplicant/lucid-updates

« back to all changes in this revision

Viewing changes to debian/wpa_action.sh

  • Committer: Bazaar Package Importer
  • Author(s): Kel Modderman
  • Date: 2006-10-05 08:04:01 UTC
  • mfrom: (1.1.5 upstream) (3 etch)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20061005080401-r8lqlix4390yos7b
Tags: 0.5.5-2
* Update madwifi headers to latest SVN. (Closes: #388316)
* Remove failed attempt at action locking. [debian/functions.sh,
  debian/wpa_action.sh]
* Add hysteresis checking functions, to avoid "event loops" while
  using wpa-roam. [debian/functions.sh, debian/wpa_action.sh]
* Change of co-maintainer email address.
* Add ishex() function to functions.sh to determine wpa-psk value type in
  plaintext or hex. This effectively eliminates the need for the bogus and
  somewhat confusing wpa-passphrase contruct specific to our scripts and
  allows wpa-psk to work with either a 8 to 63 character long plaintext
  string or 64 character long hex string.
* Adjust README.modes to not refer to the redundant wpa-passphrase stuff.
* Add big fat NOTE about acceptable wpa-psk's to top of example gallery.
* Strip surrounding quotes from wpa-ssid if present, instead of just whining
  about them.
* Update email address in copyright blurb of functions.sh, ifupdown.sh and
  wpa_action.sh.  

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/bin/sh
2
2
 
3
 
# Copyright (C) 2006 Kel Modderman <kelrin@tpg.com.au>
 
3
# Copyright (C) 2006 Kel Modderman <kelmo@kanotixguide.org>
4
4
#
5
5
# This program is free software; you can redistribute it and/or
6
6
# modify it under the terms of the GNU General Public License
15
15
# On Debian GNU/Linux systems, the text of the GPL license can be
16
16
# found in /usr/share/common-licenses/GPL.
17
17
 
18
 
if [ -z "$1" -o -z "$2" ]; then
 
18
if [ -n "$IF_WPA_ROAM_MAINT_DEBUG" ]; then
 
19
        set -x
 
20
fi
 
21
 
 
22
if [ -z "$1" ] || [ -z "$2" ]; then
19
23
        echo "Usage: $0 IFACE ACTION"
20
24
        exit 1
21
25
fi
22
26
 
23
 
# network interface is first argument
 
27
# network interface
24
28
IFACE="$1"
25
 
# action in [CONNECTED|DISCONNECTED|stop|reload] is second argument
26
 
ACTION="$2"
27
 
 
28
 
# log actions to file
29
 
WPA_LOGFILE="/var/log/wpa_action.log"
30
 
# lock wpa_cli ACTION events
31
 
WPA_LOCKFILE="/var/lock/wpa_action.$IFACE.lock"
32
 
 
33
 
if [ -n "$IF_WPA_ROAM_MAINT_DEBUG" ]; then
34
 
        set -x
 
29
# [CONNECTED|DISCONNECTED|stop|reload|check]
 
30
WPA_ACTION="$2"
 
31
 
 
32
if [ -f /etc/wpa_supplicant/functions.sh ]; then
 
33
        . /etc/wpa_supplicant/functions.sh
 
34
else
 
35
        exit 0
35
36
fi
36
37
 
37
 
# ACTION locking
38
 
# the purpose of the following locking function is to ensure we are not
39
 
# interrupting the currently active action
40
 
action_lock () {
41
 
        # test and set lock file in an atomic action
42
 
        if ! (umask 222; echo "$$" >"$WPA_LOCKFILE") 2>/dev/null; then
43
 
                # action is locked
44
 
                echo "$0 is locked on $IFACE, \"$ACTION\" ACTION aborted"
45
 
                exit 0
46
 
        fi
47
 
}
48
 
 
49
 
action_wait () {
50
 
        if [ -e "$WPA_LOCKFILE" ]; then
51
 
                echo "Waiting for current ACTION to finish before processing \"$ACTION\" ACTION"
52
 
                local ACTION_WAIT="0" MAX_ACTION_WAIT="60"
53
 
                until [ ! -e "$WPA_LOCKFILE" ]; do
54
 
                        if [ "$ACTION_WAIT" -ge "$MAX_ACTION_WAIT" ]; then
55
 
                                rm -f "$WPA_LOCKFILE"
56
 
                        else
57
 
                                ACTION_WAIT=$(($ACTION_WAIT+1))
58
 
                        fi
59
 
                        sleep 1
60
 
                done
61
 
        fi
62
 
}
63
 
 
64
 
action_unlock () {
65
 
        # clean up lockfile
66
 
        if [ -e "$WPA_LOCKFILE" ]; then
67
 
                rm -f "$WPA_LOCKFILE"
68
 
        fi
69
 
}
70
 
 
71
 
# Log functions
72
 
log_init () {
73
 
        exec >> "$WPA_LOGFILE" 2>&1
74
 
}
75
 
 
76
 
log_action () {
77
 
        echo "########## $(date +"%H:%M:%S  %Y-%m-%d") ##########"
78
 
        echo "IFACE=$IFACE ACTION=$ACTION"
79
 
}
80
 
 
81
 
log_environment () {
82
 
        echo "WPA_ID=$WPA_ID WPA_ID_STR=$WPA_ID_STR"
83
 
        echo "WPA_CTRL_DIR=$WPA_CTRL_DIR"
84
 
}
85
 
 
86
 
check_ifupdown () {
87
 
        # interfaces file
88
 
        if [ -e /etc/network/interfaces ]; then
89
 
                INTERFACES_FILE="/etc/network/interfaces"
90
 
        else
91
 
                echo "Cannot locate ifupdown's \"interfaces\" file, $IFACE will not be configured"
92
 
                return 1
93
 
        fi
94
 
 
95
 
        # ifstate file
96
 
        if [ -e /etc/network/run/ifstate ]; then
97
 
                # debian's ifupdown
98
 
                IFSTATE_FILE="/etc/network/run/ifstate"
99
 
        elif [ -e /var/run/network/ifstate ]; then
100
 
                # ifstate file lives in /var/run on Ubuntu
101
 
                IFSTATE_FILE="/var/run/network/ifstate"
102
 
        else
103
 
                echo "Cannot locate ifupdown's \"ifstate\" file, $IFACE will not be configured"
104
 
                return 1
105
 
        fi
106
 
 
107
 
        return 0
108
 
}
109
 
 
110
 
ifup () {
111
 
        # ensure this is not present in environment and private to function
112
 
        local WPA_LOGICAL_IFACE
113
 
        # If "wpa-script-priority 1" is set in /e/n/i we'll skip the id_str matching
114
 
        if [ -z "$IF_WPA_MAPPING_SCRIPT_PRIORITY" -a -n "$WPA_ID_STR" ]; then
115
 
                WPA_LOGICAL_IFACE="$WPA_ID_STR"
116
 
                echo "Mapping logical interface via id_str: $WPA_LOGICAL_IFACE"
117
 
        fi
118
 
        
119
 
        if [ -z "$WPA_LOGICAL_IFACE" -a -n "$IF_WPA_MAPPING_SCRIPT" ]; then
120
 
                # find a suitable WPA_LOGICAL_IFACE by running the mapping script
121
 
                echo "Mapping logical interface via wpa-mapping-script: $IF_WPA_MAPPING_SCRIPT"
122
 
                
123
 
                # ensure this is private to function
124
 
                local WPA_MAP_STDIN
125
 
                WPA_MAP_STDIN=$(set | sed -n -e 's/^\(IF_WPA_MAP[0-9]*\)=.*/echo\ \$\1/p')
126
 
                if [ -n "$WPA_MAP_STDIN" ]; then
127
 
                        WPA_LOGICAL_IFACE=$(eval "$WPA_MAP_STDIN" | "$IF_WPA_MAPPING_SCRIPT" "$IFACE")
128
 
                else            
129
 
                        WPA_LOGICAL_IFACE=$("$IF_WPA_MAPPING_SCRIPT" "$IFACE")
130
 
                fi
131
 
                
132
 
                if [ -n "$WPA_LOGICAL_IFACE" ]; then
133
 
                        echo "Mapping script result: $WPA_LOGICAL_IFACE"
134
 
                else
135
 
                        echo "Mapping script failed."
136
 
                fi
137
 
        fi
138
 
 
139
 
        # use fallback mapping if present
140
 
        if [ -z "$WPA_LOGICAL_IFACE" ]; then
141
 
                if [ -n "$IF_WPA_ROAM_DEFAULT_IFACE" ]; then
142
 
                        # wpa-roam-default-iface
143
 
                        WPA_LOGICAL_IFACE="$IF_WPA_ROAM_DEFAULT_IFACE"
144
 
                        echo "Using wpa-roam-default-iface: $WPA_LOGICAL_IFACE"
145
 
                else
146
 
                        WPA_LOGICAL_IFACE="default"
147
 
                        echo "Using fallback logical interface: $WPA_LOGICAL_IFACE"
148
 
                fi
149
 
        fi
150
 
 
151
 
        # grep for matching LOGICAL interface stanza in interfaces file
152
 
        if [ -n "$WPA_LOGICAL_IFACE" ]; then
153
 
                if grep --quiet "^iface $WPA_LOGICAL_IFACE inet" "$INTERFACES_FILE"; then
154
 
                        echo "ifup $IFACE=$WPA_LOGICAL_IFACE"
155
 
                        # Map to LOGICAL and bring up the IFACE
156
 
                        if grep --quiet "^$IFACE=$IFACE" "$IFSTATE_FILE"; then
157
 
                                # Force settings over the unconfigured "master" IFACE
158
 
                                /sbin/ifup --force "$IFACE=$WPA_LOGICAL_IFACE"
159
 
                        else
160
 
                                /sbin/ifup "$IFACE=$WPA_LOGICAL_IFACE"
161
 
                        fi
162
 
                else
163
 
                        echo "No network defined for \"$WPA_LOGICAL_IFACE\" in \"$INTERFACES_FILE\""
164
 
                fi
165
 
        else
166
 
                echo "No suitable logical interface mapping for ifupdown to configure"
167
 
        fi
168
 
}
169
 
 
170
 
ifdown () {
171
 
        # grep for IFACE state in ifstate file, is it up or down?
172
 
        if grep --quiet "^$IFACE" "$IFSTATE_FILE"; then
173
 
                echo "ifdown $IFACE"
174
 
                # Take down the IFACE
175
 
                /sbin/ifdown "$IFACE"
176
 
        else
177
 
                echo "Ignoring request to take \"$IFACE\" down, it is not up"
178
 
        fi
179
 
}
180
 
 
181
 
iface_up () {
182
 
        # use iproute if installed
183
 
        if [ -x /sbin/ip ]; then
184
 
                # flush previous protocol address
185
 
                # it almost never needs flushing, so keep it quiet
186
 
                /sbin/ip addr flush dev "$IFACE" 2>/dev/null
187
 
                # Keep IFACE in 'up' operstate
188
 
                /sbin/ip link set "$IFACE" up
189
 
        else
190
 
                # Keep IFACE in 'up' operstate
191
 
                /sbin/ifconfig "$IFACE" up
192
 
        fi
193
 
}
194
 
 
195
 
# wpa_cli wrapper, ctrl_iface path and interface supplied
196
 
# cannot be used for interactive actions, environmental variableWPA_CTRL_DIR is not present
197
 
wpa_cli () {
198
 
        LC_ALL=C /sbin/wpa_cli -p $WPA_CTRL_DIR -i $IFACE "$@"
199
 
}
200
 
 
201
 
kill_wpa_cli () {
202
 
        WPA_CLI_PNAME="wpa_cli"
203
 
        WPA_CLI_PIDFILE="/var/run/wpa_action.$IFACE.pid"
204
 
        # Kill wpa_cli daemon
205
 
        if [ -f "$WPA_CLI_PIDFILE" ]; then
206
 
                start-stop-daemon --stop --oknodo --verbose \
207
 
                        --name "$WPA_CLI_PNAME" --pidfile "$WPA_CLI_PIDFILE"
208
 
        
209
 
                if [ -f "$WPA_CLI_PIDFILE" ]; then
210
 
                        rm -f "$WPA_CLI_PIDFILE"
211
 
                fi
212
 
        fi
213
 
}
214
 
 
215
 
kill_wpa_supplicant () {
216
 
        WPA_SUP_PNAME="wpa_supplicant"
217
 
        WPA_SUP_PIDFILE="/var/run/wpa_supplicant.$IFACE.pid"
218
 
        # Kill wpa_supplicant daemon
219
 
        if [ -f "$WPA_SUP_PIDFILE" ]; then
220
 
                start-stop-daemon --stop --oknodo --verbose \
221
 
                        --name "$WPA_SUP_PNAME" --pidfile "$WPA_SUP_PIDFILE"
222
 
        
223
 
                if [ -f "$WPA_SUP_PIDFILE" ]; then
224
 
                        rm -f "$WPA_SUP_PIDFILE"
225
 
                fi
226
 
        fi
227
 
}
228
 
 
229
 
reload_wpa_supplicant () {
230
 
        WPA_SUP_PNAME="wpa_supplicant"
231
 
        WPA_SUP_PIDFILE="/var/run/wpa_supplicant.$IFACE.pid"
232
 
        # Reload wpa_supplicant's configuration file
233
 
        if [ -f "$WPA_SUP_PIDFILE" ]; then
234
 
                echo "Reloading wpa_supplicant configuration file via HUP signal"
235
 
                start-stop-daemon --stop --signal HUP \
236
 
                        --name "$WPA_SUP_PNAME" --pidfile "$WPA_SUP_PIDFILE"
237
 
        else
238
 
                echo "Cannot $ACTION, $WPA_SUP_PIDFILE does not exist"
239
 
        fi
240
 
}
241
 
 
242
 
case "$ACTION" in
 
38
case "$WPA_ACTION" in
243
39
        "CONNECTED")
244
 
                log_init
245
 
                action_lock
246
 
                log_action
247
 
                log_environment
248
 
                if check_ifupdown; then
 
40
                wpa_log_init
 
41
                wpa_hysteresis_check || exit 1
 
42
                wpa_log_action
 
43
                wpa_log_environment
 
44
                if ifupdown_check; then
249
45
                        ifup
250
46
                fi
 
47
                wpa_hysteresis_event
251
48
                wpa_cli status
252
 
                action_unlock
253
49
                ;;
254
50
 
255
51
        "DISCONNECTED")
256
 
                log_init
257
 
                action_lock
258
 
                log_action
259
 
                log_environment
260
 
                if check_ifupdown; then
 
52
                wpa_log_init
 
53
                wpa_hysteresis_check || exit 1
 
54
                wpa_log_action
 
55
                wpa_log_environment
 
56
                if ifupdown_check; then
261
57
                        ifdown
262
 
                        iface_up
 
58
                        if_post_down_up
263
59
                fi
264
 
                action_unlock
265
60
                ;;
266
61
 
267
62
        "stop"|"down")
 
63
                test_wpa_supplicant || exit 1
268
64
                kill_wpa_cli
269
 
                action_wait
270
 
                if check_ifupdown; then
 
65
                if ifupdown_check; then
271
66
                        ifdown
272
67
                fi
273
68
                kill_wpa_supplicant
274
 
                log_init
275
 
                log_action
 
69
                wpa_log_init
 
70
                wpa_log_action
276
71
                ;;
277
72
 
278
73
        "restart"|"reload")
 
74
                test_wpa_supplicant || exit 1
279
75
                reload_wpa_supplicant
280
 
                log_init
281
 
                log_action
 
76
                wpa_log_init
 
77
                wpa_log_action
 
78
                ;;
 
79
        
 
80
        "check")
 
81
                test_wpa_supplicant || exit 1
 
82
                test_wpa_cli || exit 1
282
83
                ;;
283
84
 
284
85
        *)
285
 
                echo "Unknown action: \"$ACTION\""
 
86
                echo "Unknown action: \"$WPA_ACTION\""
286
87
                exit 1
287
88
                ;;
288
89
esac