wifi-scripts: add modular wps button handler scripts
authorFelix Fietkau <[email protected]>
Fri, 8 Aug 2025 16:18:49 +0000 (18:18 +0200)
committerFelix Fietkau <[email protected]>
Fri, 8 Aug 2025 16:53:30 +0000 (18:53 +0200)
Execute scripts in /etc/rc.wps until one of them returns a exit code of 0.
Split up sta and ap handling of wps into separate scripts.

Signed-off-by: Felix Fietkau <[email protected]>
package/network/config/wifi-scripts/files/etc/rc.button/wps [new file with mode: 0755]
package/network/config/wifi-scripts/files/etc/rc.wps/40-wps_ap [new file with mode: 0755]
package/network/config/wifi-scripts/files/etc/rc.wps/50-wps_sta [new file with mode: 0755]
package/network/services/hostapd/Makefile
package/network/services/hostapd/files/wps-hotplug.sh [deleted file]

diff --git a/package/network/config/wifi-scripts/files/etc/rc.button/wps b/package/network/config/wifi-scripts/files/etc/rc.button/wps
new file mode 100755 (executable)
index 0000000..59b1aef
--- /dev/null
@@ -0,0 +1,6 @@
+#!/bin/sh
+[ "$ACTION" = "pressed" ] && exit 5
+for script in /etc/rc.wps/*; do
+       [ -x "$script" ] || continue
+       "$script" && break
+done
diff --git a/package/network/config/wifi-scripts/files/etc/rc.wps/40-wps_ap b/package/network/config/wifi-scripts/files/etc/rc.wps/40-wps_ap
new file mode 100755 (executable)
index 0000000..2cb7f6b
--- /dev/null
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+[ "$SEEN" -ge 3 ] && exit 1
+wps_done=0
+ubusobjs="$( ubus -S list hostapd.* )"
+for ubusobj in $ubusobjs; do
+       ubus -S call $ubusobj wps_start && wps_done=1
+done
+[ $wps_done = 1 ]
+
diff --git a/package/network/config/wifi-scripts/files/etc/rc.wps/50-wps_sta b/package/network/config/wifi-scripts/files/etc/rc.wps/50-wps_sta
new file mode 100755 (executable)
index 0000000..67a21e3
--- /dev/null
@@ -0,0 +1,57 @@
+#!/bin/sh
+[ "$SEEN" -lt 3 ] && exit 1
+[ "$ACTION" = "released" ] || exit 1
+
+wps_catch_credentials() {
+       local iface ifaces ifc ifname ssid encryption key radio radios
+       local found=0
+
+       . /usr/share/libubox/jshn.sh
+       ubus -S -t 30 listen wps_credentials | while read creds; do
+               json_init
+               json_load "$creds"
+               json_select wps_credentials || continue
+               json_get_vars ifname ssid key encryption
+               local ifcname="$ifname"
+               json_init
+               json_load "$(ubus -S call network.wireless status)"
+               json_get_keys radios
+               for radio in $radios; do
+                       json_select $radio
+                       json_select interfaces
+                       json_get_keys ifaces
+                       for ifc in $ifaces; do
+                               json_select $ifc
+                               json_get_vars ifname
+                               [ "$ifname" = "$ifcname" ] && {
+                                       ubus -S call uci set "{\"config\":\"wireless\", \"type\":\"wifi-iface\",                \
+                                                               \"match\": { \"device\": \"$radio\", \"encryption\": \"wps\" }, \
+                                                               \"values\": { \"encryption\": \"$encryption\",                  \
+                                                                               \"ssid\": \"$ssid\",                            \
+                                                                               \"key\": \"$key\" } }"
+                                       ubus -S call uci commit '{"config": "wireless"}'
+                                       ubus -S call uci apply
+                               }
+                               json_select ..
+                       done
+                       json_select ..
+                       json_select ..
+               done
+       done
+}
+
+wps_done=0
+ubusobjs="$( ubus -S list wpa_supplicant.* )"
+for ubusobj in $ubusobjs; do
+       ifname="$(echo $ubusobj | cut -d'.' -f2 )"
+       multi_ap=""
+       if [ -e "/var/run/wpa_supplicant-${ifname}.conf.is_multiap" ]; then
+               ubus -S call $ubusobj wps_start '{ "multi_ap": true }' && wps_done=1
+       else
+               ubus -S call $ubusobj wps_start && wps_done=1
+       fi
+done
+[ $wps_done = 0 ] && exit 1
+
+wps_catch_credentials &
+exit 0
index cdaf636c4ae34660a3c06b8791d451a196c1802a..4522eec8d4e2ad8bbe1c89b032f488ffcc764987 100644 (file)
@@ -726,14 +726,12 @@ endef
 define Package/hostapd-common/install
        $(INSTALL_DIR) \
          $(1)/etc/capabilities \
-         $(1)/etc/rc.button \
          $(1)/etc/hotplug.d/ieee80211 \
          $(1)/etc/init.d $(1)/lib/netifd \
          $(1)/usr/share/acl.d \
          $(1)/usr/share/hostap
        $(INSTALL_BIN) ./files/dhcp-get-server.sh $(1)/lib/netifd/dhcp-get-server.sh
        $(INSTALL_BIN) ./files/wpad.init $(1)/etc/init.d/wpad
-       $(INSTALL_BIN) ./files/wps-hotplug.sh $(1)/etc/rc.button/wps
        $(INSTALL_DATA) ./files/wpad_acl.json $(1)/usr/share/acl.d
        $(INSTALL_DATA) ./files/wpad.json $(1)/etc/capabilities
 endef
diff --git a/package/network/services/hostapd/files/wps-hotplug.sh b/package/network/services/hostapd/files/wps-hotplug.sh
deleted file mode 100644 (file)
index 073bdd1..0000000
+++ /dev/null
@@ -1,69 +0,0 @@
-#!/bin/sh
-
-wps_catch_credentials() {
-       local iface ifaces ifc ifname ssid encryption key radio radios
-       local found=0
-
-       . /usr/share/libubox/jshn.sh
-       ubus -S -t 30 listen wps_credentials | while read creds; do
-               json_init
-               json_load "$creds"
-               json_select wps_credentials || continue
-               json_get_vars ifname ssid key encryption
-               local ifcname="$ifname"
-               json_init
-               json_load "$(ubus -S call network.wireless status)"
-               json_get_keys radios
-               for radio in $radios; do
-                       json_select $radio
-                       json_select interfaces
-                       json_get_keys ifaces
-                       for ifc in $ifaces; do
-                               json_select $ifc
-                               json_get_vars ifname
-                               [ "$ifname" = "$ifcname" ] && {
-                                       ubus -S call uci set "{\"config\":\"wireless\", \"type\":\"wifi-iface\",                \
-                                                               \"match\": { \"device\": \"$radio\", \"encryption\": \"wps\" }, \
-                                                               \"values\": { \"encryption\": \"$encryption\",                  \
-                                                                               \"ssid\": \"$ssid\",                            \
-                                                                               \"key\": \"$key\" } }"
-                                       ubus -S call uci commit '{"config": "wireless"}'
-                                       ubus -S call uci apply
-                               }
-                               json_select ..
-                       done
-                       json_select ..
-                       json_select ..
-               done
-       done
-}
-
-if [ "$ACTION" = "released" ] && [ "$BUTTON" = "wps" ]; then
-       # If the button was pressed for 3 seconds or more, trigger WPS on
-       # wpa_supplicant only, no matter if hostapd is running or not.  If
-       # was pressed for less than 3 seconds, try triggering on
-       # hostapd. If there is no hostapd instance to trigger it on or WPS
-       # is not enabled on them, trigger it on wpa_supplicant.
-       if [ "$SEEN" -lt 3 ] ; then
-               wps_done=0
-               ubusobjs="$( ubus -S list hostapd.* )"
-               for ubusobj in $ubusobjs; do
-                       ubus -S call $ubusobj wps_start && wps_done=1
-               done
-               [ $wps_done = 0 ] || return 0
-       fi
-       wps_done=0
-       ubusobjs="$( ubus -S list wpa_supplicant.* )"
-       for ubusobj in $ubusobjs; do
-               ifname="$(echo $ubusobj | cut -d'.' -f2 )"
-               multi_ap=""
-               if [ -e "/var/run/wpa_supplicant-${ifname}.conf.is_multiap" ]; then
-                       ubus -S call $ubusobj wps_start '{ "multi_ap": true }' && wps_done=1
-               else
-                       ubus -S call $ubusobj wps_start && wps_done=1
-               fi
-       done
-       [ $wps_done = 0 ] || wps_catch_credentials &
-fi
-
-return 0