add edit page for hosts and ethers
authorFelix Fietkau <nbd@openwrt.org>
Sun, 2 Oct 2005 19:22:25 +0000 (19:22 +0000)
committerFelix Fietkau <nbd@openwrt.org>
Sun, 2 Oct 2005 19:22:25 +0000 (19:22 +0000)
SVN-Revision: 2026

openwrt/package/webif/Config.in
openwrt/package/webif/files/usr/lib/webif/apply.sh
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/config.sh
openwrt/package/webif/files/www/cgi-bin/webif/hosts.sh [new file with mode: 0755]
openwrt/package/webif/files/www/cgi-bin/webif/upgrade.sh
openwrt/package/webif/files/www/webif.css

index 079b4d861788a02247d4a28c104d47ff9b831699..fa96980585e027a783e75e1d2f615f1b8f6ad4fd 100644 (file)
@@ -1,6 +1,7 @@
 config BR2_PACKAGE_WEBIF
+       select BR2_PACKAGE_HASERL
        tristate "webif - OpenWrt Administrative Console"
-       default n
+       default m if CONFIG_DEVEL
        help
          A web interface for configuring OpenWrt
          
index f7f1f17ade214aa95b488b87cabd36e3b6abd7ca..7194b4fa3bde814a88cda58498105c414a01aeef 100644 (file)
@@ -1,14 +1,29 @@
 cd /tmp/.webif
 
+
+# file-*               other config files
+for config in $(ls file-* 2>&-); do
+       name=${config#file-}
+       echo "Processing config file: $name"
+       case "$name" in
+               hosts) mv $config /etc/hosts;;
+               ethers) mv $config /etc/ethers;;
+               *)      # FIXME: add other config handlers
+                       ;;
+       esac
+done
+
+
+# config-*             simple config files
 [ -f /etc/nvram.overrides ] && ( # White Russian
        cd /proc/self
-       cat /tmp/.webif/config-* | tee fd/1 | xargs -n1 nvram set
+       cat /tmp/.webif/config-* 2>&- | tee fd/1 | xargs -n1 nvram set
 )
 
 nvram commit
-for config in config-*; do 
+for config in $(ls config-* 2>&-); do 
        name="/usr/lib/webif/apply-${config#config-}.sh"
-       [ -f "$name" ] && sh $name &
+       sh $name &
 done
 sleep 2
 rm -f config-*
index b402285000dbe88153f2c5ac697220b4f04a8325..554a9090f82050e1b1f987a67456305193786f0c 100644 (file)
@@ -83,5 +83,6 @@ $1 ~ /^end_form/ {
        if (form_help != "") form_help = "<dl>" form_help "</dl>"
        print "<div class=\"settings-help\"><blockquote><h3><strong>Short help:</strong></h3>" form_help form_help_link "</blockquote></div>"
        form_help = ""
+       form_help_link = ""
        print "<div style=\"clear: both\">&nbsp;</div></div>"
 }
index 43f54b006716e118a1a01013dd52a292b71589b5..40023bd4ccb45813b2934e55f5a881867619d9c3 100644 (file)
@@ -35,6 +35,7 @@ subcategories() {
 }
 
 header() {
+       CHANGES=$(($( (cat /tmp/.webif/config-* ; ls /tmp/.webif/file-*) 2>&- | wc -l)))
        ERROR=${ERROR:+<h3>$ERROR</h3><br /><br />}
        SAVED=${SAVED:+: Settings saved}
        _category="$1"
@@ -134,10 +135,9 @@ EOF
 }
 
 footer() {
-  _changes=$(($(cat /tmp/.webif/config-* 2>&- | wc -l)))
-  _changes=${_changes#0}
-  _changes=${_changes:+(${_changes})}
-  cat <<EOF
+       _changes=${CHANGES#0}
+       _changes=${_changes:+(${_changes})}
+       cat <<EOF
                        </div>
                        <hr width="40%" />
                </div>
@@ -232,3 +232,19 @@ save_setting() {
        [ "$oldval" != "$3" ] && echo "$2=\"$3\"" >> /tmp/.webif/config-$1
        rm -f /tmp/.webif/config-$1-old
 }
+
+
+# common awk code for forms
+AWK_START_FORM='
+       print "<div class=\"settings\">"
+       print "<div class=\"settings-title\"><h3><strong>" title "</strong></h3></div>"
+       print "<div class=\"settings-content\">"
+'
+AWK_END_FORM='
+       print "</div>"
+       if (form_help != "") form_help = "<dl>" form_help "</dl>"
+       print "<div class=\"settings-help\"><blockquote><h3><strong>Short help:</strong></h3>" form_help form_help_link "</blockquote></div>"
+       form_help = ""
+       form_help_link = ""
+       print "<div style=\"clear: both\">&nbsp;</div></div>"
+'
index f1e8806a5cbc037f728247cce0683766b0ee0d67..7f56d9e1733bc27fca9a04d3643d6502e3792ea2 100755 (executable)
@@ -1,39 +1,43 @@
 #!/usr/bin/haserl
 <? 
 . /usr/lib/webif/webif.sh
-[ "$FORM_mode" = "clear" ] && rm -rf /tmp/.webif >&- 2>&- 
-header $FORM_cat .
-?>
 
-<?if [ "$FORM_mode" = "clear" ] ?>
-       <h2>All configuration changes have been cleared.</h2>
-<?el?>
-       <?if grep = /tmp/.webif/config-* >&- 2>&- ?>
-               <?if [ "$FORM_mode" = "save" ] ?>
-                       <h2>Updating configuration...</h2>
-                       <br />
-                       <pre><? sh /usr/lib/webif/apply.sh ?></pre>
-                       <h2>Done</h2>
-               <?fi?>
-       
-               <?if [ "$FORM_mode" = "review" ] ?>
-                       <h2>Configuration changes:</h2>
-       <? (
-       cd /tmp/.webif
-       for configname in config-*; do
-               grep = $configname >&- 2>&- && {
-                       echo -n "<h3>${configname#config-}</h3><pre>"
-                       cat $configname
-                       echo '</pre>'
-               }
-       done
-       ) ?>
-               <?fi?>
-               
-       <?el?>
-               <h2>No configuration changes were made.</h2>
-       <?fi?>
-<?fi?>
+case "$CHANGES" in
+       ""|0)FORM_mode=nochange
+esac
+case "$FORM_mode" in 
+       nochange) header $FORM_cat . "Configuration: no changes were made.";;
+       clear)
+               rm -rf /tmp/.webif >&- 2>&- 
+               header $FORM_cat . "Configuration: cleared."
+               CHANGES=""
+               ;;
+       review)
+               header $FORM_cat . "Configuration changes:"
+               cd /tmp/.webif
+               for configname in config-*; do
+                       grep = $configname >&- 2>&- && {
+                               echo -n "<h3>${configname#config-}</h3><br /><pre>"
+                               cat $configname
+                               echo '</pre><br />'
+                       }
+               done
+               CONFIGFILES=""
+               for configname in $(ls file-* 2>&-); do
+                       CONFIGFILES="$CONFIGFILES ${configname#file-}"
+               done
+               CONFIGFILES="${CONFIGFILES:+<h3 style="display:inline">Config files: </h3>$CONFIGFILES<br />}"
+               echo $CONFIGFILES
+               ;;
+       save)
+               header $FORM_cat . "Configuration: updating..."
+               CHANGES=""
+               echo '<pre>'
+               sh /usr/lib/webif/apply.sh
+               echo '</pre>'
+               ;;
+esac
 
+footer
 
-<? footer ?>
+?>
diff --git a/openwrt/package/webif/files/www/cgi-bin/webif/hosts.sh b/openwrt/package/webif/files/www/cgi-bin/webif/hosts.sh
new file mode 100755 (executable)
index 0000000..673818a
--- /dev/null
@@ -0,0 +1,148 @@
+#!/usr/bin/haserl
+<? 
+. /usr/lib/webif/webif.sh
+
+[ -f /tmp/.webif/file-hosts ] && HOSTS_FILE=/tmp/.webif/file-hosts || HOSTS_FILE=/etc/hosts
+[ -f /tmp/.webif/file-ethers ] && ETHERS_FILE=/tmp/.webif/file-ethers || ETHERS_FILE=/etc/ethers
+touch $HOSTS_FILE $ETHERS_FILE
+
+update_hosts() {
+       mkdir -p /tmp/.webif
+       awk -v "mode=$1" -v "ip=$2" -v "name=$3" '
+BEGIN {
+       FS="[ \t]"
+       host_added = 0
+}
+{ processed = 0 }
+(mode == "del") && (ip == $1) {
+       names_found = 0
+       n = split($0, names, "[ \t]")
+       output = $1 "   "
+       for (i = 2; i <= n; i++) {
+               if ((names[i] != "") && (names[i] != name)) {
+                       output = output names[i] " "
+                       names_found++
+               }
+       }
+       if (names_found > 0) print output
+       processed = 1
+}
+(mode == "add") && (ip == $1) {
+       print $0 " " name
+       host_added = 1
+       processed = 1
+}
+processed == 0 {
+       print $0
+}
+END {
+       if ((mode == "add") && (host_added == 0)) print ip "    " name
+}' - < "$HOSTS_FILE" > /tmp/.webif/file-hosts-new
+       mv "/tmp/.webif/file-hosts-new" "/tmp/.webif/file-hosts"
+       HOSTS_FILE=/tmp/.webif/file-hosts
+}
+
+update_ethers() {
+       mkdir -p /tmp/.webif
+       case "$1" in
+               add)
+                       grep -E -v "^[ \t]*$2" $ETHERS_FILE > /tmp/.webif/file-ethers-new
+                       echo "$2        $3" >> /tmp/.webif/file-ethers-new
+                       mv /tmp/.webif/file-ethers-new  /tmp/.webif/file-ethers
+               ;;
+               del)
+                       grep -E -v "^[ \t]*$2" $ETHERS_FILE > /tmp/.webif/file-ethers-new
+                       mv /tmp/.webif/file-ethers-new  /tmp/.webif/file-ethers
+               ;;      
+       esac
+       ETHERS_FILE=/tmp/.webif/file-ethers
+}
+
+[ ! -z "$FORM_add_host" ] && {
+       # add a host to /etc/hosts
+       validate "ip|FORM_host_ip|IP Address|required|$FORM_host_ip
+hostname|FORM_host_name|Hostname|required|$FORM_host_name" && update_hosts add "$FORM_host_ip" "$FORM_host_name"
+}
+[ ! -z "$FORM_add_dhcp" ] && {
+       # add a host to /etc/ethers
+       [ -f /tmp/.webif/file-ethers ] || cat /etc/ethers > /tmp/.webif/file-ethers
+       validate "mac|FORM_dhcp_mac|MAC Address|required|$FORM_dhcp_mac
+ip|FORM_dhcp_ip|IP|required|$FORM_dhcp_ip" && update_ethers add "$FORM_dhcp_mac" "$FORM_dhcp_ip"
+}
+[ ! -z "$FORM_remove_host" ] && update_hosts del "$FORM_remove_ip" "$FORM_remove_name"
+[ ! -z "$FORM_remove_dhcp" ] && update_ethers del "$FORM_remove_mac"
+
+header "Network" "Hosts" "Configured hosts" ''
+
+# Hosts in /etc/hosts
+# FIXME: move formatting code in form.awk if possible
+awk -v "url=$SCRIPT_NAME" '
+BEGIN {
+       FS="[ \t]"
+       title = "Hostnames"
+       '"$AWK_START_FORM"'
+       print "<table width=\"70%\" summary=\"Settings\">"
+       print "<tr><th>IP</th><th>Hostname</th><th></th></tr>"
+       print "<tr><td colspan=\"3\"><hr class=\"separator\" /></td></tr>"
+}
+
+# only for valid IPv4 addresses
+($1 ~ /^[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$/) {
+       gsub(/#.*$/, "");
+       output = ""
+       names_found = 0
+       n = split($0, names, "[ \t]")
+       first = 1
+       for (i = 2; i <= n; i++) {
+               if (names[i] != "") {
+                       if (first != 1) output = output "<tr>"
+                       output = output "<td>" names[i] "</td><td align=\"right\" width=\"10%\"><a href=\"" url "?remove_host=1&remove_ip=" $1 "&remove_name=" names[i] "\">Remove</a></td></tr>"
+                       first = 0
+                       names_found++
+               }
+       }
+       if (names_found > 0) {
+               print "<tr><td rowspan=\"" names_found "\">" $1 "</td>" output
+               print "<tr><td colspan=\"3\"><hr class=\"separator\" /></td></tr>"
+       }
+}
+
+END {
+       print "<form enctype=\"multipart/form-data\" method=\"post\">"
+       print "<tr><td><input type\"text\" name=\"host_ip\" value=\"'"$FORM_host_ip"'\" /></td><td><input type=\"text\" name=\"host_name\" value=\"'"$FORM_host_name"'\" /></td><td><input type=\"submit\" name=\"add_host\" value=\"Add\" /></td></tr>"
+       print "</form>"
+       print "</table>"
+       '"$AWK_END_FORM"'
+}
+' $HOSTS_FILE
+
+# Static DHCP mappings (/etc/ethers)
+# FIXME: move formatting code in form.awk if possible
+awk -v "url=$SCRIPT_NAME" '
+BEGIN {
+       FS="[ \t]"
+       title = "Static IP addresses (for DHCP)"
+       '"$AWK_START_FORM"'
+       print "<form enctype=\"multipart/form-data\" method=\"post\">"
+       print "<table width=\"70%\" summary=\"Settings\">"
+       print "<tr><th>MAC address</th><th>IP</th><th></th></tr>"
+}
+
+# only for valid MAC addresses
+($1 ~ /^[0-9A-Fa-f][0-9A-Fa-f]:[0-9A-Fa-f][0-9A-Fa-f]:[0-9A-Fa-f][0-9A-Fa-f]:[0-9A-Fa-f][0-9A-Fa-f]:[0-9A-Fa-f][0-9A-Fa-f]:[0-9A-Fa-f][0-9A-Fa-f]$/) {
+       gsub(/#.*$/, "");
+       print "<tr><td>" $1 "</td><td>" $2 "</td><td align=\"right\" width=\"10%\"><a href=\"" url "?remove_dhcp=1&remove_mac=" $1 "\">Remove</a></td></tr>"
+}
+
+END {
+       print "<tr><td><input type\"text\" name=\"dhcp_mac\" value=\"'"$FORM_dhcp_mac"'\" /></td><td><input type=\"text\" name=\"dhcp_ip\" value=\"'"$FORM_dhcp_ip"'\" /></td><td><input type=\"submit\" name=\"add_dhcp\" value=\"Add\" /></td></tr>"
+       print "</table>"
+       print "</form>"
+       '"$AWK_END_FORM"'
+}
+' - < $ETHERS_FILE
+
+footer ?>
+<!--
+##WEBIF:name:Network:4:Hosts
+-->
index fbf0d79d6823fccb940705ad17903a1311245ca0..4aae81fdabf0a86d98b8fe760f92978bb075456d 100755 (executable)
@@ -36,7 +36,6 @@ strip_cybertan() {
 }
 ?>
 <?if [ -z "$UPGRADE" ] ?>
-       <? $ERROR ?>
        <form method="POST" name="upgrade" action="<? echo -n $SCRIPT_NAME ?>" enctype="multipart/form-data">
        <table style="width: 90%; text-align: left;" border="0" cellpadding="2" cellspacing="2" align="center">
        <tbody>
index 947bcb1ac5b645ec8d77bc677fd4fc72c3f0194a..35799016bd95ba0ae98f29447722b612c721db33 100644 (file)
@@ -36,6 +36,10 @@ hr {
        display: none;
 }
 
+hr.separator {
+       display: block;
+}
+
 .selected-maincat {
        font-weight: bold;
 }
@@ -199,6 +203,7 @@ hr {
 }
 
 #content .settings-block .settings .settings-content {
+       padding-top: 1em;
        margin-left: 1em;
        float: left;
        width: 58%;
@@ -211,6 +216,7 @@ hr {
 
 #content .settings-block .settings .settings-help {
        padding: 0.4em;
+       padding-top: 1.4em;
        margin-left: 42%;
 }