wps-hotplug.sh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/bin/sh
  2. wps_catch_credentials() {
  3. local iface ifaces ifc ifname ssid encryption key radio radios
  4. local found=0
  5. . /usr/share/libubox/jshn.sh
  6. ubus -S -t 30 listen wps_credentials | while read creds; do
  7. json_init
  8. json_load "$creds"
  9. json_select wps_credentials || continue
  10. json_get_vars ifname ssid key encryption
  11. local ifcname="$ifname"
  12. json_init
  13. json_load "$(ubus -S call network.wireless status)"
  14. json_get_keys radios
  15. for radio in $radios; do
  16. json_select $radio
  17. json_select interfaces
  18. json_get_keys ifaces
  19. for ifc in $ifaces; do
  20. json_select $ifc
  21. json_get_vars ifname
  22. [ "$ifname" = "$ifcname" ] && {
  23. ubus -S call uci set "{\"config\":\"wireless\", \"type\":\"wifi-iface\", \
  24. \"match\": { \"device\": \"$radio\", \"encryption\": \"wps\" }, \
  25. \"values\": { \"encryption\": \"$encryption\", \
  26. \"ssid\": \"$ssid\", \
  27. \"key\": \"$key\" } }"
  28. ubus -S call uci commit '{"config": "wireless"}'
  29. ubus -S call uci apply
  30. }
  31. json_select ..
  32. done
  33. json_select ..
  34. json_select ..
  35. done
  36. done
  37. }
  38. if [ "$ACTION" = "released" ] && [ "$BUTTON" = "wps" ]; then
  39. # If the button was pressed for 3 seconds or more, trigger WPS on
  40. # wpa_supplicant only, no matter if hostapd is running or not. If
  41. # was pressed for less than 3 seconds, try triggering on
  42. # hostapd. If there is no hostapd instance to trigger it on or WPS
  43. # is not enabled on them, trigger it on wpa_supplicant.
  44. if [ "$SEEN" -lt 3 ] ; then
  45. wps_done=0
  46. ubusobjs="$( ubus -S list hostapd.* )"
  47. for ubusobj in $ubusobjs; do
  48. ubus -S call $ubusobj wps_start && wps_done=1
  49. done
  50. [ $wps_done = 0 ] || return 0
  51. fi
  52. wps_done=0
  53. ubusobjs="$( ubus -S list wpa_supplicant.* )"
  54. for ubusobj in $ubusobjs; do
  55. ifname="$(echo $ubusobj | cut -d'.' -f2 )"
  56. multi_ap=""
  57. if [ -e "/var/run/wpa_supplicant-${ifname}.conf.is_multiap" ]; then
  58. ubus -S call $ubusobj wps_start '{ "multi_ap": true }' && wps_done=1
  59. else
  60. ubus -S call $ubusobj wps_start && wps_done=1
  61. fi
  62. done
  63. [ $wps_done = 0 ] || wps_catch_credentials &
  64. fi
  65. return 0