Skip to content

Commit

Permalink
Squash write to nmconnection file
Browse files Browse the repository at this point in the history
* First prototype of writing to nmconnection file
* Support open networks
* Support hidden SSIDs
* Remove TODOs
* Remove overzealous escaping
* Special characters in the SSID and PSK don't need escaping in the
  nmconnection file, so remove the extra layer of escaping which was
  being added.
  • Loading branch information
cillian64 authored and XECDesign committed Nov 7, 2023
1 parent 71635ca commit c8a2ee7
Showing 1 changed file with 28 additions and 45 deletions.
73 changes: 28 additions & 45 deletions usr/lib/raspberrypi-sys-mods/imager_custom
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,12 @@ set_wlan_country () (


set_wlan () (
HIDDEN=0
HIDDEN="false"
PLAIN=0
SCRIPT='/var/lib/raspberrypi-sys-mods/set-wlan'
for arg in "$@"; do
# shellcheck disable=SC2031
if [ "$arg" = "-h" ] || [ "$arg" = "--hidden" ]; then
HIDDEN=1
HIDDEN="true"
elif [ "$arg" = "-p" ] || [ "$arg" = "--plain" ]; then
PLAIN=1
elif [ -z "${SSID+set}" ]; then
Expand All @@ -124,52 +123,36 @@ set_wlan () (
set_wlan_country "$COUNTRY"
fi

# Replace ' with '\'' in SSID and PASS to support single-quote characters
SSID=$(printf '%s' "$SSID" | sed "s/'/'\\\\''/g")
PASS=$(printf '%s' "$PASS" | sed "s/'/'\\\\''/g")

mkdir -p "$(dirname "$SCRIPT")"
# shellcheck disable=SC2094
cat <<- EOF > "$SCRIPT"
#!/bin/sh
COUNTER=0
while [ "\$COUNTER" -lt 10 ]; do
COUNTER=\$((COUNTER + 1))
if raspi-config nonint do_wifi_ssid_passphrase '$SSID' '$PASS' '$HIDDEN' '$PLAIN'; then
break
fi
sleep 5
done
systemctl stop set-wlan.timer
systemctl disable set-wlan.timer
rm -f "\$0"
rm -f /etc/systemd/system/set-wlan.timer
rm -f /etc/systemd/system/set-wlan.service
rmdir --ignore-fail-on-non-empty '$(dirname "$SCRIPT")'
CONNFILE=/etc/NetworkManager/system-connections/preconfigured.nmconnection
UUID=$(uuid -v4)
cat <<- EOF >${CONNFILE}
[connection]
id=preconfigured
uuid=${UUID}
type=wifi
[wifi]
mode=infrastructure
ssid=${SSID}
hidden=${HIDDEN}
[ipv4]
method=auto
[ipv6]
addr-gen-mode=default
method=auto
[proxy]
EOF
chmod 700 "$SCRIPT"

cat <<- EOF > /etc/systemd/system/set-wlan.timer
[Unit]
Description=Configure WLAN for rpi-imager
[Timer]
OnBootSec=1
OnUnitActiveSec=10
[Install]
WantedBy=timers.target
if [ ! -z "${PASS}" ]; then
cat <<- EOF >>${CONNFILE}
[wifi-security]
key-mgmt=wpa-psk
psk=${PASS}
EOF
fi

cat <<- EOF > /etc/systemd/system/set-wlan.service
[Unit]
Description=Configure WLAN for rpi-imager
After=NetworkManager.service
ConditionPathIsDirectory=|/run/wpa_supplicant
[Service]
Type=oneshot
ExecStart=$SCRIPT
EOF
ln -f -s /etc/systemd/system/set-wlan.timer \
/etc/systemd/system/timers.target.wants/set-wlan.timer
# NetworkManager will ignore nmconnection files with incorrect permissions,
# to prevent Wi-Fi credentials accidentally being world-readable.
chmod 600 ${CONNFILE}
)

import_ssh_id () (
Expand Down

0 comments on commit c8a2ee7

Please sign in to comment.