Skip to content

Commit 324392c

Browse files
authored
Update enable-wifi to read PSK input from stdin (#1870)
Resolves tiny-pilot/tinypilot-pro#1464 This change updates the enable-wifi script to read the PSK from stdin instead of using an argument. We also update the enable_wifi function in network.py to use the new stdin interface. <a data-ca-tag href="https://codeapprove.com/pr/tiny-pilot/tinypilot/1870"><img src="https://codeapprove.com/external/github-tag-allbg.png" alt="Review on CodeApprove" /></a>
1 parent 3921b8e commit 324392c

2 files changed

Lines changed: 23 additions & 14 deletions

File tree

app/network.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,16 @@ def enable_wifi(wifi_settings):
153153
'sudo', '/opt/tinypilot-privileged/scripts/enable-wifi', '--country',
154154
wifi_settings.country_code, '--ssid', wifi_settings.ssid
155155
]
156-
if wifi_settings.psk:
157-
args.extend(['--psk', wifi_settings.psk])
158156
try:
159157
# Ignore pylint since we're not managing the child process.
160158
# pylint: disable=consider-using-with
161-
subprocess.Popen(args)
159+
if wifi_settings.psk:
160+
args.append('--psk')
161+
process = subprocess.Popen(args, stdin=subprocess.PIPE, text=True)
162+
process.communicate(input=wifi_settings.psk)
163+
else:
164+
subprocess.Popen(args)
165+
162166
except subprocess.CalledProcessError as e:
163167
raise NetworkError(str(e.output).strip()) from e
164168

debian-pkg/opt/tinypilot-privileged/scripts/enable-wifi

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ set -e
77

88
print_help() {
99
cat <<EOF
10-
Usage: ${0##*/} [--help] --country COUNTRY --ssid SSID [--psk PSK]
10+
Usage: ${0##*/} [--help] --country COUNTRY --ssid SSID [--psk]
1111
Enables a WiFi network connection.
1212
--help Optional. Display this help and exit.
1313
--country COUNTRY A two-digit country code, as of ISO 3166-1 alpha-2.
1414
--ssid SSID The name (SSID) of the WiFi network.
15-
--psk PSK Optional. The password for authenticating. If specified,
16-
it must be 8-63 characters in length.
15+
--psk Optional. Authenticate with the WiFi network using a
16+
password read from stdin. The password must be 8-63
17+
characters in length.
1718
1819
Note: this script only stores the WiFi configuration and triggers its
1920
activation, but it doesn't (and cannot) verify whether the device can actually
@@ -49,13 +50,14 @@ while (( "$#" > 0 )); do
4950
shift # For flag value.
5051
;;
5152
--psk)
52-
if (( "$#" < 2 )); then
53-
shift
54-
break
53+
# Read the password from stdin. Otherwise, prompt the user.
54+
if [[ -p /dev/stdin ]]; then
55+
WIFI_PSK="$(cat -)"
56+
else
57+
read -r -s -p 'Provide the WiFi password: ' WIFI_PSK
58+
echo
5559
fi
56-
WIFI_PSK="$2"
5760
shift # For flag name.
58-
shift # For flag value.
5961
;;
6062
*)
6163
>&2 echo "Unknown flag/argument: $1"
@@ -94,7 +96,7 @@ if [[ -n "${WIFI_PSK}" ]]; then
9496
PSK_LENGTH="$(echo -n "${WIFI_PSK}" | wc --bytes)"
9597
readonly PSK_LENGTH
9698
if (( "${PSK_LENGTH}" < 8 || "${PSK_LENGTH}" > 63 )); then
97-
>&2 echo 'Invalid argument: PSK'
99+
>&2 echo 'Invalid PSK.'
98100
>&2 echo "Use the '--help' flag for more information"
99101
exit 1
100102
fi
@@ -116,7 +118,10 @@ readonly CONFIG_FILE='/etc/wpa_supplicant/wpa_supplicant.conf'
116118
# Write out the new configuration.
117119
# shellcheck source=lib/markers.sh
118120
. "${SCRIPT_DIR}/lib/markers.sh"
119-
{
121+
(
122+
# Avoid leaking passwords by disabling command tracing in subshell.
123+
set +x
124+
120125
echo "${MARKER_START}"
121126
echo "country=${WIFI_COUNTRY}"
122127

@@ -138,7 +143,7 @@ readonly CONFIG_FILE='/etc/wpa_supplicant/wpa_supplicant.conf'
138143
fi
139144

140145
echo "${MARKER_END}"
141-
} >> "${CONFIG_FILE}"
146+
) >> "${CONFIG_FILE}"
142147

143148
# Effectuate changes.
144149
rfkill unblock wifi

0 commit comments

Comments
 (0)