add edit page for hosts and ethers
[openwrt/svn-archive/archive.git] / openwrt / package / webif / files / www / cgi-bin / webif / hosts.sh
1 #!/usr/bin/haserl
2 <?
3 . /usr/lib/webif/webif.sh
4
5 [ -f /tmp/.webif/file-hosts ] && HOSTS_FILE=/tmp/.webif/file-hosts || HOSTS_FILE=/etc/hosts
6 [ -f /tmp/.webif/file-ethers ] && ETHERS_FILE=/tmp/.webif/file-ethers || ETHERS_FILE=/etc/ethers
7 touch $HOSTS_FILE $ETHERS_FILE
8
9 update_hosts() {
10 mkdir -p /tmp/.webif
11 awk -v "mode=$1" -v "ip=$2" -v "name=$3" '
12 BEGIN {
13 FS="[ \t]"
14 host_added = 0
15 }
16 { processed = 0 }
17 (mode == "del") && (ip == $1) {
18 names_found = 0
19 n = split($0, names, "[ \t]")
20 output = $1 " "
21 for (i = 2; i <= n; i++) {
22 if ((names[i] != "") && (names[i] != name)) {
23 output = output names[i] " "
24 names_found++
25 }
26 }
27 if (names_found > 0) print output
28 processed = 1
29 }
30 (mode == "add") && (ip == $1) {
31 print $0 " " name
32 host_added = 1
33 processed = 1
34 }
35 processed == 0 {
36 print $0
37 }
38 END {
39 if ((mode == "add") && (host_added == 0)) print ip " " name
40 }' - < "$HOSTS_FILE" > /tmp/.webif/file-hosts-new
41 mv "/tmp/.webif/file-hosts-new" "/tmp/.webif/file-hosts"
42 HOSTS_FILE=/tmp/.webif/file-hosts
43 }
44
45 update_ethers() {
46 mkdir -p /tmp/.webif
47 case "$1" in
48 add)
49 grep -E -v "^[ \t]*$2" $ETHERS_FILE > /tmp/.webif/file-ethers-new
50 echo "$2 $3" >> /tmp/.webif/file-ethers-new
51 mv /tmp/.webif/file-ethers-new /tmp/.webif/file-ethers
52 ;;
53 del)
54 grep -E -v "^[ \t]*$2" $ETHERS_FILE > /tmp/.webif/file-ethers-new
55 mv /tmp/.webif/file-ethers-new /tmp/.webif/file-ethers
56 ;;
57 esac
58 ETHERS_FILE=/tmp/.webif/file-ethers
59 }
60
61 [ ! -z "$FORM_add_host" ] && {
62 # add a host to /etc/hosts
63 validate "ip|FORM_host_ip|IP Address|required|$FORM_host_ip
64 hostname|FORM_host_name|Hostname|required|$FORM_host_name" && update_hosts add "$FORM_host_ip" "$FORM_host_name"
65 }
66 [ ! -z "$FORM_add_dhcp" ] && {
67 # add a host to /etc/ethers
68 [ -f /tmp/.webif/file-ethers ] || cat /etc/ethers > /tmp/.webif/file-ethers
69 validate "mac|FORM_dhcp_mac|MAC Address|required|$FORM_dhcp_mac
70 ip|FORM_dhcp_ip|IP|required|$FORM_dhcp_ip" && update_ethers add "$FORM_dhcp_mac" "$FORM_dhcp_ip"
71 }
72 [ ! -z "$FORM_remove_host" ] && update_hosts del "$FORM_remove_ip" "$FORM_remove_name"
73 [ ! -z "$FORM_remove_dhcp" ] && update_ethers del "$FORM_remove_mac"
74
75 header "Network" "Hosts" "Configured hosts" ''
76
77 # Hosts in /etc/hosts
78 # FIXME: move formatting code in form.awk if possible
79 awk -v "url=$SCRIPT_NAME" '
80 BEGIN {
81 FS="[ \t]"
82 title = "Hostnames"
83 '"$AWK_START_FORM"'
84 print "<table width=\"70%\" summary=\"Settings\">"
85 print "<tr><th>IP</th><th>Hostname</th><th></th></tr>"
86 print "<tr><td colspan=\"3\"><hr class=\"separator\" /></td></tr>"
87 }
88
89 # only for valid IPv4 addresses
90 ($1 ~ /^[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$/) {
91 gsub(/#.*$/, "");
92 output = ""
93 names_found = 0
94 n = split($0, names, "[ \t]")
95 first = 1
96 for (i = 2; i <= n; i++) {
97 if (names[i] != "") {
98 if (first != 1) output = output "<tr>"
99 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>"
100 first = 0
101 names_found++
102 }
103 }
104 if (names_found > 0) {
105 print "<tr><td rowspan=\"" names_found "\">" $1 "</td>" output
106 print "<tr><td colspan=\"3\"><hr class=\"separator\" /></td></tr>"
107 }
108 }
109
110 END {
111 print "<form enctype=\"multipart/form-data\" method=\"post\">"
112 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>"
113 print "</form>"
114 print "</table>"
115 '"$AWK_END_FORM"'
116 }
117 ' $HOSTS_FILE
118
119 # Static DHCP mappings (/etc/ethers)
120 # FIXME: move formatting code in form.awk if possible
121 awk -v "url=$SCRIPT_NAME" '
122 BEGIN {
123 FS="[ \t]"
124 title = "Static IP addresses (for DHCP)"
125 '"$AWK_START_FORM"'
126 print "<form enctype=\"multipart/form-data\" method=\"post\">"
127 print "<table width=\"70%\" summary=\"Settings\">"
128 print "<tr><th>MAC address</th><th>IP</th><th></th></tr>"
129 }
130
131 # only for valid MAC addresses
132 ($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]$/) {
133 gsub(/#.*$/, "");
134 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>"
135 }
136
137 END {
138 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>"
139 print "</table>"
140 print "</form>"
141 '"$AWK_END_FORM"'
142 }
143 ' - < $ETHERS_FILE
144
145 footer ?>
146 <!--
147 ##WEBIF:name:Network:4:Hosts
148 -->