add checks for pptp/pppoe and implement basic ip address validation for the wan/lan...
authorFelix Fietkau <[email protected]>
Sat, 24 Sep 2005 17:06:12 +0000 (17:06 +0000)
committerFelix Fietkau <[email protected]>
Sat, 24 Sep 2005 17:06:12 +0000 (17:06 +0000)
SVN-Revision: 1987

openwrt/package/webif/files/usr/lib/webif/form.awk
openwrt/package/webif/files/usr/lib/webif/webif.sh
openwrt/package/webif/files/www/cgi-bin/webif/lan.sh
openwrt/package/webif/files/www/cgi-bin/webif/wan.sh
openwrt/package/webif/files/www/webif.js

index 7b02bd979458e147c37211a6c99c7f6e1221539c..d6f170cb3a9dde5d4efe33c90913fd154b5e58e3 100644 (file)
@@ -5,6 +5,11 @@
 # $5 = string to append
 # $6 = additional attributes 
 
+# trim leading whitespaces 
+{
+       gsub(/^[ \t]+/,"",$1)
+}
+
 $1 ~ /^start_form/ {
        print "<form method=\"POST\" action=\"" $2 "\" enctype=\"multipart/form-data\">"
        print "<input type=\"hidden\" name=\"submit\" value=\"1\" />"
@@ -39,7 +44,11 @@ $1 ~ /^submit/ {
 }
 $1 ~ /^end_form/ {
        if (field_open == 1) print "</td></tr>"
+       field_open = 0
        print "</tbody>"
        print "</table>"
        print "</form>"
 }
+END {
+       if(field_open == 1) print "</td></tr>"
+}
index 92cc7c4927ea8b93906243d1a772d416e5591869..4c103c64d985fd4668acd8b59ebcccc600623e9a 100644 (file)
@@ -159,6 +159,53 @@ load_settings() {
        [ -f /tmp/.webif/config-$1 ] && . /tmp/.webif/config-$1 
 }
 
+validate_ip() {
+       [ \! -z "$1" ] && {
+               ipcalc "$1" >&- 2>&- && return 0 || {
+                       ERROR="$ERROR Invalid IP address: $2<br />"
+                       return 255
+               }
+       } || {
+               [ "$3" != "1" ] && return 0 || {
+                       ERROR="$ERROR No IP address entered: $2<br />"
+                       return 255
+               } 
+       }
+}
+
+validate_ips() {
+       [ \! -z "$1" ] && {
+               invalid_ip=0
+               for tmp_ip in $1; do
+                       ipcalc "$1" >&- 2>&- || invalid_ip=1
+               done
+               [ "$invalid_ip" != 1 ] && return 0 || {
+                       ERROR="$ERROR Invalid IP address list: $2<br />"
+                       return 255
+               }
+       } || {
+               [ "$3" != "1" ] && return 0 || {
+                       ERROR="$ERROR No IP address entered: $2<br />"
+                       return 255
+               } 
+       }
+}
+
+validate_netmask() {
+       [ \! -z "$1" ] && {
+               # FIXME
+               ipcalc "$1" >&- 2>&- && return 0 || {
+                       ERROR="$ERROR Invalid Netmask: $2<br />"
+                       return 255
+               }
+       } || {
+               [ "$3" != "1" ] && return 0 || {
+                       ERROR="$ERROR No Netmask entered: $2<br />"
+                       return 255
+               } 
+       }
+}
+
 save_setting() {
        oldval=$(eval "echo \${$2}")
        oldval=${oldval:-$(nvram get "$2")}
index 67de725cf0986ae26445336229c4c168acffcdd8..817b2bf3ea355c475052d4471d50bb483f200ab3 100755 (executable)
@@ -10,16 +10,18 @@ load_settings network
        FORM_lan_dns=${lan_dns:-$(nvram get lan_dns)}
 } || {
        SAVED=1
-       [ -z $FORM_lan_ipaddr ] || save_setting network lan_ipaddr $FORM_lan_ipaddr
-       [ -z $FORM_lan_netmask ] || save_setting network lan_netmask $FORM_lan_netmask
-       [ -z $FORM_lan_gateway ] || save_setting network lan_gateway $FORM_lan_gateway
-       [ -z $FORM_lan_dns ] || save_setting network lan_dns $FORM_lan_dns
+       validate_ip "$FORM_lan_ipaddr" "LAN IP" 1 && save_setting network lan_ipaddr $FORM_lan_ipaddr
+       validate_ip "$FORM_lan_netmask" "LAN Netmask" 1 && save_setting network lan_netmask $FORM_lan_netmask
+       validate_ip "$FORM_lan_gateway" "LAN Gateway" && save_setting network lan_gateway $FORM_lan_gateway
+       validate_ips "$FORM_lan_dns" "LAN DNS Servers" && save_setting network lan_dns $FORM_lan_dns
 }
 header "Network" "LAN" "LAN settings"
 ?>
 <?if [ "$SAVED" = "1" ] ?>
+       <? [ -z "$ERROR" ] || echo "<h2>Errors occured:</h2><h3>$ERROR</h3>" ?>
        <h2>Settings saved</h2>
-<?el?>
+       <br />
+<?fi?>
 <? display_form "start_form:$SCRIPT_NAME
 field:IP Address
 text:lan_ipaddr:$FORM_lan_ipaddr
@@ -32,7 +34,6 @@ text:lan_dns:$FORM_lan_dns
 field
 submit:action:Save settings
 end_form" ?>
-<?fi?>
 
 <? footer ?>
 <!--
index e5a5a9107d7110ce773f6847927e5536a2f9b641..d43f7d574ce571024466fbc5d106905baa535161 100755 (executable)
@@ -5,21 +5,37 @@
 load_settings network
 
 [ -z $FORM_submit ] && {
-       # common
        FORM_wan_proto=${wan_proto:-$(nvram get wan_proto)}
-       FORM_wan_proto=${FORM_wan_proto:-none}
+       case "$FORM_wan_proto" in
+               # supported types
+               static|dhcp|pptp|pppoe) ;;
+               # otherwise select "none"
+               *) FORM_wan_proto="none";;
+       esac
+       
+       # detect pptp package and compile option
+       [ -x /sbin/ifup.pptp ] && {
+               PPTP_OPTION="radio:wan_proto:$FORM_wan_proto:pptp:PPTP<br />:onChange=\"modechange()\""
+               PPTP_SERVER_OPTION="field:PPTP Server IP:pptp_server_ip
+text:pptp_server_ip:$FORM_pptp_server_ip"
+       }
+       [ -x /sbin/ifup.pppoe ] && {
+               PPPOE_OPTION="radio:wan_proto:$FORM_wan_proto:pppoe:PPPoE<br />:onChange=\"modechange()\""
+       }
        
-       # pptp and static common
+       # pptp, dhcp and static common
        FORM_wan_ipaddr=${wan_ipaddr:-$(nvram get wan_ipaddr)}
        FORM_wan_netmask=${wan_netmask:-$(nvram get wan_netmask)}
        FORM_wan_gateway=${wan_gateway:-$(nvram get wan_gateway)}
+       FORM_wan_dns=${wan_dns:-$(nvram get wan_dns)}
        
-       # pppoe and pptp common
+       # ppp common
        FORM_ppp_username=${ppp_username:-$(nvram get ppp_username)}
        FORM_ppp_passwd=${ppp_passwd:-$(nvram get ppp_passwd)}
        FORM_ppp_idletime=${ppp_idletime:-$(nvram get ppp_idletime)}
        FORM_ppp_redialperiod=${ppp_redialperiod:-$(nvram get ppp_redialperiod)}
        FORM_ppp_mtu=${ppp_mtu:-$(nvram get ppp_mtu)}
+
        redial=${ppp_demand:-$(nvram get ppp_demand)}
        case "$redial" in
                1|enabled|on)
@@ -30,40 +46,65 @@ load_settings network
                ;;      
        esac
        
-       # static specific
-       FORM_wan_dns=${wan_dns:-$(nvram get wan_dns)}
+       FORM_pptp_server_ip=${pptp_server_ip:-$(nvram get pptp_server_ip)}
 } || {
        SAVED=1
-       # common 
-       [ -z $FORM_wan_proto ] || save_setting network wan_proto $FORM_wan_proto
+
+       [ -z $FORM_wan_proto ] && {
+               ERROR="No WAN protocol selected" 
+               return -1
+       }
+
+       save_setting network wan_proto $FORM_wan_proto
        
-       # pptp and static common
+       # Settings specific to one protocol type
+       case "$FORM_wan_proto" in
+               static)
+                       validate_ip "$FORM_wan_dns" "WAN DNS Server" 1 && \
+                               save_setting network wan_dns $FORM_wan_dns
+
+                       validate_ip "$FORM_wan_gateway" "WAN Gateway" && \
+                               save_setting network wan_gateway $FORM_wan_gateway
+
+                       # Requirements for input validation
+                       REQ_IP=1
+                       REQ_NETMASK=1
+                       ;;
+               pptp)
+                       validate_ip "$FORM_pptp_server_ip" "PPTP Server" 1 && \
+                               save_setting network pptp_server_ip "$FORM_pptp_server_ip"
+                       ;;
+       esac
+       
+       # Common settings for PPTP, Static and DHCP 
        [ "$FORM_wan_proto" = "pptp" -o "$FORM_wan_proto" = "static" -o "$FORM_wan_proto" = "dhcp" ] && {
-               [ -z $FORM_wan_ipaddr ] || save_setting network wan_ipaddr $FORM_wan_ipaddr
-               [ -z $FORM_wan_netmask ] || save_setting network wan_netmask $FORM_wan_netmask
-               [ -z $FORM_wan_gateway ] || save_setting network wan_gateway $FORM_wan_gateway
+               validate_ip "$FORM_wan_ipaddr" "WAN IP" $REQ_IP && \
+                       save_setting network wan_ipaddr $FORM_wan_ipaddr
+       
+               validate_netmask "$FORM_wan_netmask" "WAN Netmask" $REQ_NETMASK && \
+                       save_setting network wan_netmask $FORM_wan_netmask 
        }
        
-       # pppoe and pptp common
+       # Common PPP settings
        [ "$FORM_wan_proto" = "pppoe" -o "$FORM_wan_proto" = "pptp" ] && {
                [ -z $FORM_ppp_username ] || save_setting network ppp_username $FORM_ppp_username
                [ -z $FORM_ppp_passwd ] || save_setting network ppp_passwd $FORM_ppp_passwd
-               [ -z $FORM_ppp_idletime ] || save_setting network ppp_idletime $FORM_ppp_idletime
-               [ -z $FORM_ppp_redialperiod ] || save_setting network ppp_redialperiod $FORM_ppp_redialperiod
-               [ -z $FORM_ppp_mtu ] || save_setting network ppp_mtu $FORM_ppp_mtu
+
+               # These can be blank
+               save_setting network ppp_idletime $FORM_ppp_idletime
+               save_setting network ppp_redialperiod $FORM_ppp_redialperiod
+               save_setting network ppp_mtu $FORM_ppp_mtu
+
                case "$FORM_ppp_redial" in
                        demand)
                                save_setting network ppp_demand 1
                                ;;
                        persist)
-                               save_setting network ppp_demand 0
+                               save_setting network ppp_demand ""
                                ;;
                esac    
-       }       
-       # static specific       
-       [ "$FORM_wan_proto" = "static" ] && {
-               [ -z $FORM_wan_dns ]  || save_setting network wan_dns $FORM_wan_dns
-       }       
+       }
+
 }
 
 header "Network" "WAN" "WAN settings" ' onLoad="modechange()" '
@@ -73,64 +114,37 @@ header "Network" "WAN" "WAN settings" ' onLoad="modechange()" '
 <!--
 function modechange()
 {
-       // pppoe and pptp common
-       if (checked('wan_proto_pppoe') || checked('wan_proto_pptp')) {
-               show('ppp_username');
-               show('ppp_passwd');
-               show('ppp_redial');
-               show('ppp_mtu');
-               
-               if (checked('ppp_redial_demand')) {
-                       show('ppp_demand_idletime');
-                       hide('ppp_persist_redialperiod');
-               } else {        
-                       hide('ppp_demand_idletime');
-                       show('ppp_persist_redialperiod');
-               }       
-       } else {
-               hide('ppp_username');
-               hide('ppp_passwd');
-               hide('ppp_demand_idletime');
-               hide('ppp_persist_redialperiod');
-               hide('ppp_redial');
-               hide('ppp_mtu');
-               
-       }       
+       var v;
+       v = (checked('wan_proto_pppoe') || checked('wan_proto_pptp'));
+       set_visible('ppp_username', v);
+       set_visible('ppp_passwd', v);
+       set_visible('ppp_redial', v);
+       set_visible('ppp_mtu', v);
+       set_visible('ppp_demand_idletime', v && checked('ppp_redial_demand'));
+       set_visible('ppp_persist_redialperiod', v && !checked('ppp_redial_demand'));
        
-       // pptp and static common
-       if(checked('wan_proto_static') || checked('wan_proto_pptp')) {
-               show('wan_ipaddr');
-               show('wan_netmask');
-               show('wan_gateway');
-       } else {
-               if (checked('wan_proto_dhcp')) {
-                       show('wan_ipaddr');
-               } else {
-                       hide('wan_ipaddr');
-               }
-               hide('wan_netmask');
-               hide('wan_gateway');
-       }
+       v = (checked('wan_proto_static') || checked('wan_proto_pptp') || checked('wan_proto_dhcp'));
+       set_visible('wan_ipaddr', v);
+       set_visible('wan_netmask', v);
        
-       // static specific
-       if(checked('wan_proto_static')) {
-               show('wan_dns');
-       } else {        
-               hide('wan_dns');
-       }
+       v = checked('wan_proto_static');
+       set_visible('wan_gateway', v);
+       set_visible('wan_dns', v);
 }
 -->
 </script>
 <?if [ "$SAVED" = "1" ] ?>
+       <? [ -z "$ERROR" ] || echo "<h2>Errors occured:</h2><h3>$ERROR</h3>" ?>
        <h2>Settings Saved</h2>
-<?el?>
+       <br />
+<?fi?>
 <? display_form "start_form:$SCRIPT_NAME
 field:Internet Connection Type
 radio:wan_proto:$FORM_wan_proto:none:None<br />:onchange=\"modechange()\"
 radio:wan_proto:$FORM_wan_proto:dhcp:DHCP<br />:onchange=\"modechange()\"
 radio:wan_proto:$FORM_wan_proto:static:Static IP<br />:onchange=\"modechange()\"
-radio:wan_proto:$FORM_wan_proto:pppoe:PPPoE<br />:onChange=\"modechange()\"
-radio:wan_proto:$FORM_wan_proto:pptp:PPTP<br />:onChange=\"modechange()\"
+$PPPOE_OPTION
+$PPTP_OPTION
 
 field:Internet IP Address:wan_ipaddr
 text:wan_ipaddr:$FORM_wan_ipaddr
@@ -140,6 +154,7 @@ field:Gateway:wan_gateway
 text:wan_gateway:$FORM_wan_gateway
 field:DNS Server(s):wan_dns
 text:wan_dns:$FORM_wan_dns
+$PPTP_SERVER_OPTION
 
 field:PPP Redial Policy:ppp_redial
 radio:ppp_redial:$FORM_ppp_redial:demand:Connect on Demand<br />:onChange=\"modechange()\"
@@ -159,9 +174,6 @@ field
 submit:action:Save Settings
 end_form" ?>
 
-
-<?fi?>
-
 <? footer ?>
 <!--
 ##WEBIF:name:Network:2:WAN
index b93693b95b7ef285ca47dafdd4f4132165423077..e906f8a856b38812d637b9c62899023a95a1013f 100644 (file)
@@ -1,13 +1,24 @@
 function checked(name)
 {
-       return document.getElementById(name).checked;
+       var item = document.getElementById(name);
+       return ((item) && item.checked);
 }
 function hide(name)
 {
-       document.getElementById(name).style.display = 'none';
+       var item = document.getElementById(name);
+       if (item) 
+               item.style.display = 'none';
 }
 function show(name)
 {
-       document.getElementById(name).style.display = '';
+       var item = document.getElementById(name);
+       if (item)
+               item.style.display = '';
+}
+function set_visible(name, value)
+{
+       if (value)
+               show(name)
+       else
+               hide(name)
 }
-