| 1 |
#!/bin/sh |
| 2 |
# Copyright (c) 2020 Ariadne Conill <[email protected]> |
| 3 |
# |
| 4 |
# Permission to use, copy, modify, and/or distribute this software for any |
| 5 |
# purpose with or without fee is hereby granted, provided that the above |
| 6 |
# copyright notice and this permission notice appear in all copies. |
| 7 |
# |
| 8 |
# This software is provided 'as is' and without any warranty, express or |
| 9 |
# implied. In no event shall the authors be liable for any damages arising |
| 10 |
# from the use of this software. |
| 11 |
# |
| 12 |
# Manage wifi connections using wpa_supplicant. |
| 13 |
# |
| 14 |
# Vocabulary: |
| 15 |
# wifi-ssid - The SSID name to connect to. |
| 16 |
# wifi-psk - The pre-shared key to use. |
| 17 |
# wifi-config - A path to a wpa_supplicant config file, for special configs. |
| 18 |
# |
| 19 |
# If wifi-config is not set, wifi-ssid and wifi-psk are required, and a config |
| 20 |
# will be generated as /run/wpa_supplicant.$IFACE.conf. |
| 21 |
# |
| 22 |
# The wpa_supplicant PID is stored in /run/wpa_supplicant.$IFACE.pid. |
| 23 |
|
| 24 |
die() { |
| 25 |
printf "ERROR: %s\n" "$1" >&2 |
| 26 |
exit 1 |
| 27 |
} |
| 28 |
|
| 29 |
[ -z "$IFACE" ] && die "IFACE not set" |
| 30 |
[ -z "$PHASE" ] && die "PHASE not set" |
| 31 |
PIDFILE="/run/wpa_supplicant.$IFACE.pid" |
| 32 |
|
| 33 |
# Do not allow mixing wifi-config-path and wifi-ssid/wifi-psk. |
| 34 |
[ -n "$IF_WIFI_CONFIG_PATH" -a -n "$IF_WIFI_SSID" ] && die "wifi-config-path cannot be used with wifi-ssid" |
| 35 |
[ -n "$IF_WIFI_CONFIG_PATH" -a -n "$IF_WIFI_PSK" ] && die "wifi-config-path cannot be used with wifi-psk" |
| 36 |
|
| 37 |
# Set IF_WIFI_CONFIG_PATH to the default path if not already set. |
| 38 |
WIFI_CONFIG_PATH="$IF_WIFI_CONFIG_PATH" |
| 39 |
[ -z "$WIFI_CONFIG_PATH" ] && WIFI_CONFIG_PATH="/run/wpa_supplicant.$IFACE.conf" |
| 40 |
|
| 41 |
# Supplicant options. |
| 42 |
WPA_SUPPLICANT_OPTS="-qq -B -i$IFACE -c$WIFI_CONFIG_PATH -P$PIDFILE" |
| 43 |
|
| 44 |
# Given $IF_WIFI_SSID and $IF_WIFI_PSK, generate a config file at $WIFI_CONFIG_PATH. |
| 45 |
generate_config() { |
| 46 |
[ -z "$IF_WIFI_SSID" ] && die "wifi-ssid not set" |
| 47 |
[ -z "$IF_WIFI_PSK" ] && die "wifi-psk not set" |
| 48 |
|
| 49 |
# We use a pipeline here to avoid leaking PSK into the process name. |
| 50 |
(echo "$IF_WIFI_PSK" | /sbin/wpa_passphrase "$IF_WIFI_SSID") >$WIFI_CONFIG_PATH |
| 51 |
|
| 52 |
[ ! -e "$WIFI_CONFIG_PATH" ] && die "failed to write temporary config: $WIFI_CONFIG_PATH" |
| 53 |
} |
| 54 |
|
| 55 |
# Should we use the supplicant? |
| 56 |
use_supplicant() { |
| 57 |
[ -n "$IF_WIFI_CONFIG_PATH" ] && return 0 |
| 58 |
[ -n "$IF_WIFI_PSK" ] && return 0 |
| 59 |
|
| 60 |
return 1 |
| 61 |
} |
| 62 |
|
| 63 |
# Either start a supplicant process for $IFACE, or use iwconfig to trigger an |
| 64 |
# association attempt. |
| 65 |
start() { |
| 66 |
if use_supplicant; then |
| 67 |
# If there is no config file located at $WIFI_CONFIG_PATH, generate one. |
| 68 |
[ ! -e "$WIFI_CONFIG_PATH" ] && generate_config |
| 69 |
|
| 70 |
/sbin/wpa_supplicant $WPA_SUPPLICANT_OPTS |
| 71 |
else |
| 72 |
/usr/sbin/iwconfig $IFACE essid -- "$IF_WIFI_SSID" ap any |
| 73 |
fi |
| 74 |
} |
| 75 |
|
| 76 |
# Stop wpa_supplicant safely |
| 77 |
stop_wpa_supplicant() { |
| 78 |
# Remove generated config file |
| 79 |
[ -z "$IF_WIFI_CONFIG_PATH" ] && rm -- "$WIFI_CONFIG_PATH" |
| 80 |
|
| 81 |
# If there is no PIDFILE, there is nothing we can do |
| 82 |
[ ! -f "$PIDFILE" ] && return |
| 83 |
|
| 84 |
pid=$(cat "$PIDFILE") |
| 85 |
rm -- "$PIDFILE" |
| 86 |
|
| 87 |
# If there is no process with this PID running, we're done here |
| 88 |
if [ ! -d "/proc/$pid/" ]; then |
| 89 |
return |
| 90 |
fi |
| 91 |
|
| 92 |
# Verify that the name of the running process matches wpa_supplicant |
| 93 |
progname_path=$(readlink -n "/proc/$pid/exe") |
| 94 |
progname=$(basename "$progname_path") |
| 95 |
if [ "$progname" = "wpa_supplicant" ]; then |
| 96 |
kill -9 $pid 2>/dev/null |
| 97 |
fi |
| 98 |
} |
| 99 |
|
| 100 |
# Either stop the supplicant process for $IFACE, or use iwconfig to dissociate |
| 101 |
# from the current SSID. |
| 102 |
stop() { |
| 103 |
if use_supplicant; then |
| 104 |
stop_wpa_supplicant |
| 105 |
else |
| 106 |
/usr/sbin/iwconfig $IFACE essid any |
| 107 |
fi |
| 108 |
} |
| 109 |
|
| 110 |
[ -z "$VERBOSE" ] || set -x |
| 111 |
|
| 112 |
case "$PHASE" in |
| 113 |
pre-up) |
| 114 |
start |
| 115 |
;; |
| 116 |
post-down) |
| 117 |
stop |
| 118 |
;; |
| 119 |
esac |