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-*
}
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"
}
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>
[ "$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\"> </div></div>"
+'
#!/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 ?>
+?>
--- /dev/null
+#!/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
+-->