#!/bin/sh . /usr/share/libubox/jshn.sh . /lib/functions.sh get_if_creds() { local section=$1 local network=$2 local net dev ssid key encryption band config_get net $section "network" "lan" if [ "$net" == "$network" ]; then config_get dev $section "device" band="$(uci -q get wireless.$dev.band)" config_get ssid $section ssid config_get key $section key config_get encryption $section encryption config_get disabled $section disabled 0 [ $disabled -eq 1 ] && return [ "$ssid" == "" -o "$band" == "" ] && return json_add_object json_add_string "ssid" "$ssid" json_add_string "band" "$band" json_add_string "key" "$key" json_add_string "encryption" "$encryption" json_close_object fi } validate_file() { local file="$1" local real="$(readlink -f `dirname $file`)" real="${real}/`basename $file`" case "$real" in /tmp/*) touch $real && return 0 || return 1 ;; *) return 1 ;; esac } write_error(){ json_init json_add_string "status" "error: $1" json_dump exit 1 } duplicate_if_single_radio() { json_select "wifi-ifaces" local i=1 local dummy band dup_band num_radios num_radios="$(uci -q show wireless | grep wifi-device | wc -l)" [ "$num_radios" == "2" ] && return band="$(uci -q get wireless.@wifi-device[0].band)" [ "$band" == "a" ] && dup_band="b" [ "$band" == "b" ] && dup_band="a" [ -z "$dup_band" ] && return while json_get_var dummy $i; do i=$((i+1)) done i=$((i-1)) local ssid key encryption while [ $i -gt 0 ]; do json_select $i json_get_var ssid ssid json_get_var key key json_get_var encryption encryption json_select .. json_add_object json_add_string "ssid" "$ssid" json_add_string "band" "$dup_band" json_add_string "key" "$key" json_add_string "encryption" "$encryption" json_close_object i=$((i-1)) done } case "$1" in list) echo '{ "get_creds": { "network": "str", "file": "str" }, "set_creds": { "file": "str", "from_gui": "str" } }' ;; call) case "$2" in get_creds) #TODO: if not routed exit local curmode="$(uci -q get netmode.setup.curmode)" case $curmode in *repeater*) write_error "not in repeater mode";; esac local network file input read input json_load $input json_get_var network network json_get_var file file json_init json_add_array "wifi-ifaces" config_load wireless config_foreach get_if_creds "wifi-iface" "$network" json_close_array duplicate_if_single_radio if [ "$file" == "" ]; then json_dump else validate_file $file && json_dump -i >$file || write_error "invalid filename (\"$file\"), file must be in /tmp/ and in an existing directory" fi json_init json_add_string "status" success json_dump ;; set_creds) local file from_gui read input json_load "$input" json_get_var file file json_get_var from_gui from_gui validate_file "$file" || write_error "invalid filename (\"$file\"), file must be in /tmp and exist" json_load "`cat $file`" || write_error "invalid file content" json_select "wifi_ifaces" || write_error "invalid file content" start-netmode-conf "$file" "$from_gui" >/dev/null 2>/dev/null json_init json_add_string "status" success json_dump ;; esac ;; esac