diff options
Diffstat (limited to 'files/common/www/service/cgi-bin')
-rwxr-xr-x | files/common/www/service/cgi-bin/cfg_client.json | 9 | ||||
-rw-r--r-- | files/common/www/service/cgi-bin/cfg_router.commit | 99 | ||||
-rwxr-xr-x | files/common/www/service/cgi-bin/cfg_router.json | 33 | ||||
-rwxr-xr-x | files/common/www/service/cgi-bin/client_net.json | 19 | ||||
-rw-r--r-- | files/common/www/service/cgi-bin/common.sh | 78 | ||||
-rwxr-xr-x | files/common/www/service/cgi-bin/node_position.json | 34 | ||||
-rwxr-xr-x | files/common/www/service/cgi-bin/nodes.json | 10 | ||||
-rwxr-xr-x | files/common/www/service/cgi-bin/online.json | 8 | ||||
-rwxr-xr-x | files/common/www/service/cgi-bin/reflect | 10 | ||||
-rwxr-xr-x | files/common/www/service/cgi-bin/splash.html (renamed from files/common/www/service/cgi-bin/splash) | 2 | ||||
-rwxr-xr-x | files/common/www/service/cgi-bin/splash_click.html (renamed from files/common/www/service/cgi-bin/splash_click) | 25 | ||||
-rwxr-xr-x | files/common/www/service/cgi-bin/status.xml | 3 | ||||
-rw-r--r-- | files/common/www/service/cgi-bin/template.status.xml | 31 |
13 files changed, 319 insertions, 42 deletions
diff --git a/files/common/www/service/cgi-bin/cfg_client.json b/files/common/www/service/cgi-bin/cfg_client.json new file mode 100755 index 0000000..dc675c9 --- /dev/null +++ b/files/common/www/service/cgi-bin/cfg_client.json @@ -0,0 +1,9 @@ +#!/bin/sh + +echo -e 'Status: 200 OK\r +Content-Type: application/json\r +\r +{"client_name": "mobilaptop", + "client_splash": [false], + "client_access": ["bridged"] +}' diff --git a/files/common/www/service/cgi-bin/cfg_router.commit b/files/common/www/service/cgi-bin/cfg_router.commit new file mode 100644 index 0000000..4af1da7 --- /dev/null +++ b/files/common/www/service/cgi-bin/cfg_router.commit @@ -0,0 +1,99 @@ +update_hostname() { + uci set system.@system[0].hostname=$1 + echo $1 > /proc/sys/kernel/hostname +} + +update_geo() { + local auto=$1 + local public=$2 + local del=$3 + uci -q batch <<EOF +set system.position.automatic=$auto +set system.position.public=$public +EOF + if $del; then + uci -q batch <<EOF +delete system.position.lon +delete system.position.lat +delete system.position.street +EOF + fi + if $auto && have_internet; then + # geolocate may fail (e.g. if another instance was running or + # the position could not be determined) + /sbin/geolocate || true + # close previously aquired lock + exec 667>&- + fi +} + +update_node_db() { + true # TODO +} + +use_router_pos=false +# the POST input is given to this loop via HEREDOC to allow +# manipulating variables in the current scope (especially for lazy +# execution) +while read key val; do + case "$key" in + router_name) + [ "$val" == "$(echo "$val" | tr -dc 'a-zA-Z0-9_')" ] \ + || fail 400 'Unerlaubte Zeichen verwendet' + router_name=$val + lazy 10 update_hostname $router_name + lazy 90 update_node_db + lazy 99 uci commit system + ;; + router_qos|router_vpn|router_filter_wan) + fail 403 'Diese Funktion ist noch nicht fertig' + ;; + router_password) + fail 403 'Diese Funktion ist noch nicht fertig' + ;; + router_geo) + router_geo="$val" + case "$router_geo" in + private) + lazy 00 update_geo false false true + ;; + manual) + lazy 00 update_geo false true false + use_router_pos=true + ;; + automatic) + # only delete current position if we have internet + # to immediately get the new position + if have_internet; then + lazy 00 update_geo true true true + # obtain writing lock until geolocate obtains + # the same lock to close race cond; ignore + # failure to lock (prefer to process the + # request instead (-:) + exec 666>>/tmp/lock/geolocate + flock -n -x 667 || true + else + lazy 00 update_geo true true false + fi + ;; + *) + fail + ;; + esac + lazy 90 update_node_db + lazy 99 uci commit system + ;; + router_pos_lon|router_pos_lat) + if $use_router_pos; then + true + fi + ;; + router_pos_street) + if $use_router_pos; then + true + fi + ;; + esac +done <<EOF +$(tr '=&' ' \n' | sort) +EOF diff --git a/files/common/www/service/cgi-bin/cfg_router.json b/files/common/www/service/cgi-bin/cfg_router.json new file mode 100755 index 0000000..3315cc3 --- /dev/null +++ b/files/common/www/service/cgi-bin/cfg_router.json @@ -0,0 +1,33 @@ +#!/bin/sh -e + +. /www/service/cgi-bin/common.sh +check_node_auth + +# parse changes +if [ $REQUEST_METHOD == POST ]; then + . /www/service/cgi-bin/cfg_router.commit +fi + +# compute geo mode value (if not changed during this request) +if [ -z "$router_geo" ]; then + if [ "$(uci get system.position.public)" == 'false' ]; then + router_geo=private + elif [ "$(uci get system.position.automatic)" == 'false' ]; then + router_geo=manual + else + router_geo=automatic + fi +fi + +echo -e "Status: 200 OK\r +Content-Type: application/json\r +\r +{\"router_name\": \"${router_name-$( uci get system.@system[0].hostname)}\", + \"router_password\": \"${router_password-$( uci get system.mesh.webif_password)}\", + \"router_qos\": [${router_qos-$( uci get system.mesh.qos)}], + \"router_vpn\": [${router_vpn-$( uci get system.mesh.vpn)}], + \"router_filter_wan\": [${router_filter_wan-$(uci get system.mesh.filter_wan)}], + \"router_geo\": [\"$router_geo\"] +}" + +exec_lazy
\ No newline at end of file diff --git a/files/common/www/service/cgi-bin/client_net.json b/files/common/www/service/cgi-bin/client_net.json new file mode 100755 index 0000000..21c5767 --- /dev/null +++ b/files/common/www/service/cgi-bin/client_net.json @@ -0,0 +1,19 @@ +#!/bin/sh -e + +[ -n "$REMOTE_HOST" ] +{ + grep ^$REMOTE_HOST </proc/net/arp \ + | awk 'BEGIN { FS = " " } ; { print $4, $6 }' +} | { + read client_mac iface + if [ "$iface" == br-lan ]; then wired=true; else wired=false; fi + router_name="$(uci get 'system.@system[0].hostname')" + echo -e "Status: 200 OK\r +Content-Type: application/json\r +\r +{\"client_mac\": \"$client_mac\", + \"router_name\": \"$router_name\", + \"iface\": \"$iface\", + \"wired\": $wired +}" +} diff --git a/files/common/www/service/cgi-bin/common.sh b/files/common/www/service/cgi-bin/common.sh new file mode 100644 index 0000000..9a26904 --- /dev/null +++ b/files/common/www/service/cgi-bin/common.sh @@ -0,0 +1,78 @@ +exec 2>/tmp/www.log."$(basename $SCRIPT_NAME)" +set -x + +fail() { + code=${1:-420} + reason=${2:-'Unknown reason'} + echo -e "Status: $code\r +\r +$reason" + exit 1 +} + +read_uci() { + local val + read val + if [ "$val" == "uci: Entry not found" ]; then + val='' + fi + read $1 <<EOF +$val +EOF +} + +## lazy execution of commands after the web response has been send +lazy() { + pos=$1 + shift + lazy_cmd="$pos $* +$lazy_cmd" +} + +exec_lazy() { + echo "$lazy_cmd" \ + | grep -v '^$' \ + | sort \ + | cut -f2- -d ' ' \ + | uniq \ + | while read; do + $REPLY + done 1>/dev/null 2>/tmp/www.lazy."$(basename $SCRIPT_NAME)" & +} + +## some tests +have_internet() { + if [ -z "$have_internet_cached_result" ]; then + local mode=$(cat /tmp/fsm/inetable || echo boot) + if [ "$mode" == drone -o "$mode" == queen -o "$mode" == ghost ]; then + have_internet_cached_result=true + else + have_internet_cached_result=false + fi + fi + $have_internet_cached_result +} + +is_wired() { + if [ -z "$is_wired_cached_result" ]; then + [ -n "$REMOTE_HOST" ] + local iface=$(grep ^$REMOTE_HOST </proc/net/arp \ + | awk 'BEGIN { FS = " " } ; { print $6 }') + if [ "$iface" == br-lan ]; then + is_wired_cached_result=true + else + is_wired_cached_result=false + fi + fi + $is_wired_cached_result +} + +check_node_auth() { + is_wired || fail 401 'Keine Berechtigung' + # TODO: check password (if set) +} + +check_self_auth() { + # think about what to check + true +} diff --git a/files/common/www/service/cgi-bin/node_position.json b/files/common/www/service/cgi-bin/node_position.json new file mode 100755 index 0000000..3fe3680 --- /dev/null +++ b/files/common/www/service/cgi-bin/node_position.json @@ -0,0 +1,34 @@ +#!/bin/sh -e + +. /www/service/cgi-bin/common.sh + +# try to get a lock within 3 seconds, get info without lock otherwise +exec 666>>/tmp/lock/geolocate +flock -w 3 -s 666 || true + +uci batch <<EOF 2>&1 | tee /tmp/www.foo | { +get system.position.public +get system.position.lon +get system.position.lat +get system.position.street +EOF + read_uci public + read_uci lon + read_uci lat + read_uci street + + if ! $public; then + fail 403 'Geolokation deaktiviert' + fi + if [ -z "$lon" -o -z "$lat" ]; then + fail 503 'Position noch nicht ermittelt' + fi + + echo -e "Status: 200 OK\r +Content-Type: application/json\r +\r +{\"router_pos_lon\": $lon, + \"router_pos_lat\": $lat, + \"router_pos_street\": \"$street\" +}" +}
\ No newline at end of file diff --git a/files/common/www/service/cgi-bin/nodes.json b/files/common/www/service/cgi-bin/nodes.json new file mode 100755 index 0000000..e0c5206 --- /dev/null +++ b/files/common/www/service/cgi-bin/nodes.json @@ -0,0 +1,10 @@ +#!/bin/sh -e + +echo -e 'Status: 200 OK\r +Content-Type: application/json\r +\r +[{"id": "unique123", "uplink": true, "pos": {"lat": 50.927222, "lon": 11.586111}, "name": "Papst", "owner": "Peter"}, + {"id": "unique124", "uplink": false, "pos": {"lat": 50.937222, "lon": 11.588111}}, + {"id": "unique125", "uplink": false, "pos": {"lat": 50.921222, "lon": 11.587111}, "name": "Wifihorst"}, + {"id": "unique126", "uplink": true, "pos": {"lat": 50.925222, "lon": 11.580111}}, + {"id": "unique127", "uplink": false, "pos": {"lat": 50.930222, "lon": 11.582111}, "owner": "Hans Wurst"}]'
\ No newline at end of file diff --git a/files/common/www/service/cgi-bin/online.json b/files/common/www/service/cgi-bin/online.json new file mode 100755 index 0000000..847ee5e --- /dev/null +++ b/files/common/www/service/cgi-bin/online.json @@ -0,0 +1,8 @@ +#!/bin/sh -e + +. /www/service/cgi-bin/common.sh + +echo -e "Status: 200 OK\r +Content-Type: application/json\r +\r +$(have_internet && echo true || echo false)" diff --git a/files/common/www/service/cgi-bin/reflect b/files/common/www/service/cgi-bin/reflect new file mode 100755 index 0000000..79db9f0 --- /dev/null +++ b/files/common/www/service/cgi-bin/reflect @@ -0,0 +1,10 @@ +#!/bin/sh + +echo -e 'Status: 200 OK\r +\r' + +env +echo ================= +cat +echo ======== +pwd diff --git a/files/common/www/service/cgi-bin/splash b/files/common/www/service/cgi-bin/splash.html index 1237ce0..b8c8264 100755 --- a/files/common/www/service/cgi-bin/splash +++ b/files/common/www/service/cgi-bin/splash.html @@ -4,4 +4,4 @@ echo -en "Content-Type: text/html\r\n" echo -en "\r\n" targeturl="$(echo "$QUERY_STRING" | tr -d '/')" -sed "s/targeturl/$targeturl/g" splash.html +sed "s/targeturl/$targeturl/g" index.html diff --git a/files/common/www/service/cgi-bin/splash_click b/files/common/www/service/cgi-bin/splash_click.html index 1eb4e85..59926d0 100755 --- a/files/common/www/service/cgi-bin/splash_click +++ b/files/common/www/service/cgi-bin/splash_click.html @@ -1,15 +1,15 @@ #!/bin/sh -e + . $IPKG_INSTROOT/etc/functions.sh . /etc/splash.sh - -echo -en "Status: 200 OK\r\n" -echo -en "Content-Type: text/html\r\n" -echo -en "\r\n" +. /www/service/cgi-bin/common.sh # decode request params -URL="http://$(sed 's/target_url=//g' | urldecode)" +#URL="http://$(sed 's/target_url=//g' | urldecode || true)" +URL="http://sotun.de/" USER_MAC=$(grep ^$REMOTE_HOST </proc/net/arp \ | awk 'BEGIN { FS = " " } ; { print $4 }') +[ -n "$USER_MAC" ] # check if user is already splashed; splash otherwise lockSplash @@ -25,7 +25,9 @@ config_foreach is_mac user if [ $ISSPLASHED -eq 0 ]; then # user is now free to go chain=$chain_prefix$chain_id - iptables -t nat -I $chain 1 -m mac --mac-source $USER_MAC -j ACCEPT + if have_internet; then + iptables -t nat -I $chain 1 -m mac --mac-source $USER_MAC -j ACCEPT + fi uci -q add splash_users user >/dev/null uci -q set splash_users.@user[-1].mac=$USER_MAC @@ -34,4 +36,13 @@ fi unlockSplash # send response (encode / to not confuse sed) -sed "s/targeturl/$(echo "$URL" | sed 's#/#\\/#g')/g" iframe.html + +echo -en "Status: 200 OK\r +Content-Type: text/html\r +\r +" +if have_internet; then + sed "s/targeturl/$(echo "$URL" | sed 's#/#\\/#g')/g" iframe.html +else + cat internet.html +fi diff --git a/files/common/www/service/cgi-bin/status.xml b/files/common/www/service/cgi-bin/status.xml deleted file mode 100755 index 8d45a90..0000000 --- a/files/common/www/service/cgi-bin/status.xml +++ /dev/null @@ -1,3 +0,0 @@ -#/bin/bash! -hostname=`hostname` -eval "echo \"$(cat template.status.xml)\"" diff --git a/files/common/www/service/cgi-bin/template.status.xml b/files/common/www/service/cgi-bin/template.status.xml deleted file mode 100644 index 7df4e9f..0000000 --- a/files/common/www/service/cgi-bin/template.status.xml +++ /dev/null @@ -1,31 +0,0 @@ -<?xml version="1.0" encoding="UTF-8" standalone="yes"?> -<node> - <hostname>$hostname</hostname> - <is_hna>0</is_hna> - <network> - <ip_4></ip_4> - <ip_6></ip_6> - </network> - <owner>Max Mustermann</owner> - <godfather>Susi Mustermann</godfather> - <position> - <lat></lat> - <long></long> - <text></text> - </position> - <statistics> - <uptime></uptime> - <dhcp_leases></dhcp_leases> - <load></load> - - </statistics> - - <batman-adv> - <route> - <hostname></hostname> - <mac></mac> - </route> - </batman-adv> -</node> - - |