add webif
[openwrt/svn-archive/archive.git] / openwrt / package / webif / files / usr / lib / webif / webif.sh
1 libdir=/usr/lib/webif
2 wwwdir=/www
3 cgidir=/www/cgi-bin/webif
4 indexpage=index.sh
5
6 header() {
7 UPTIME=$(uptime)
8 UPTIME="up ${UPTIME##*up}"
9 HOSTNAME=$(cat /proc/sys/kernel/hostname)
10 CHANGES=$(($(cat /tmp/.webif/config-* 2>&- | wc -l)))
11 CHANGES=${CHANGES#0}
12 CHANGES=${CHANGES:+( ${CHANGES} )}
13 cat <<EOF
14 Content-Type: text/html
15 Pragma: no-cache
16
17 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
18 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en-US">
19 <head>
20 <meta http-equiv="content-type" content="application/xhtml+xml; charset=ISO-8859-15" />
21 <link rel="stylesheet" type="text/css" href="/webif.css" media="screen, tv, projection" title="Default" />
22 <title>OpenWrt Administrative Console</title>
23 </head>
24 <body $4>
25 <div id="container">
26 <div id="header">
27 <div class="topHeader">&nbsp;</div>
28 <div class="midHeader">
29 <h1 class="headerTitle">OpenWrt Admin Console</h1>
30 <div class="headerSubTitle">$UPTIME</div>
31 <br class="doNotDisplay doNotPrint" />
32 <div class="headerInfo">
33 <span>Hostname: &nbsp;</span>
34 $HOSTNAME
35 </div>
36 <div class="headerLinks">
37 <a href="config.sh?mode=save&cat=$1">Apply settings &laquo;</a>
38 <a href="config.sh?mode=clear&cat=$1">Clear changes &laquo;</a>
39 <a href="config.sh?mode=review&cat=$1">Review changes $CHANGES &laquo;</a>
40 </div>
41 </div>
42 <div class="doNotDisplay doNotPrint">
43 <br />
44 <br />
45 <br />
46 </div>
47 EOF
48 grep '##WEBIF:category' $cgidir/.categories $cgidir/*.sh 2>/dev/null | awk -F: '
49 BEGIN {
50 print "<div class=\"categoryHeader\"><span>Categories: &nbsp;&nbsp;&nbsp; </span>"
51 }
52 categories !~ /:$4:/ {
53 categories = categories ":" $4 ":";
54 if ($4 ~ /^'"$1"'$/) print "<a class=\"active\">&raquo;" $4 "&laquo;</a> &nbsp;"
55 else print "<a href=\"'"$indexpage"'?cat=" $4 "\">&nbsp;" $4 "&nbsp;</a> &nbsp;";
56 }
57 END {
58 print "</div>"
59 }' -
60 [ \! -z "$2" ] && {
61 grep "##WEBIF:name:$1:" *.sh 2>/dev/null | sed -e 's,^\([a-zA-Z\.\-]*\):\(.*\)$,\2:\1,' | sort -n | awk -F: '
62 BEGIN {
63 print "<div class=\"subHeader\"><span class=\"doNotDisplay doNotPrint\">Config pages: &nbsp;</span>";
64 }
65 {
66 if ($5 ~ /^'"$2"'$/) print "<a class=\"active\">&raquo;" $5 "&laquo;</a>&nbsp;&nbsp;&nbsp;"
67 else print "<a href=\"" $6 "\">" $5 "</a>&nbsp;&nbsp;&nbsp;";
68 }
69 END {
70 print "</div>";
71 }
72 ' -
73 }
74 [ -z "$3" ] && HEAD="" || HEAD="<h1>$3</h1><br />"
75 cat <<EOF
76 </div>
77 <div id="main-copy">
78 <div class="rowOfBoxes">
79 <div class="noBorderOnLeft">
80 $HEAD
81 EOF
82 [ -z "$REMOTE_USER" ] && {
83 [ -z $FORM_passwd1 ] || {
84 echo '<pre>'
85 (
86 echo "$FORM_passwd1"
87 sleep 1
88 echo "$FORM_passwd2"
89 ) | passwd root
90 apply_passwd
91 echo '</pre>'
92 footer
93 exit
94 }
95 grep 'root:!' /etc/passwd >&- 2>&- && {
96 cat <<EOF
97 <br />
98 <br />
99 <br />
100 <h3>Warning: you haven't set a password for the Web interface and SSH access<br />
101 Please enter one now</h3>
102 <br />
103 <form enctype="multipart/form-data" action="$SCRIPT_NAME" method="POST">
104 <table>
105 <tr>
106 <td>Enter Password:</td>
107 <td><input type="password" name="passwd1" /></td>
108 </tr>
109 <tr>
110 <td>Repeat Password: &nbsp; </td>
111 <td><input type="password" name="passwd2" /></td>
112 </tr>
113 <tr>
114 <td />
115 <td><input type="submit" name="action" value="Set" /></td>
116 </tr>
117 </table>
118 </form>
119 EOF
120 footer
121 exit
122 } || {
123 apply_passwd
124 }
125 }
126 }
127
128 footer() {
129 cat <<EOF
130 <br />
131 </div> </div> </div>
132 <div id="footer">
133 OpenWrt Administrative Console
134 </div>
135 </div>
136 </body>
137 </html>
138 EOF
139 }
140
141 apply_passwd() {
142 case ${SERVER_SOFTWARE%% *} in
143 busybox)
144 echo -n '/:' > /etc/httpd.conf
145 cat /etc/passwd | grep root | cut -d: -f1,2 >> /etc/httpd.conf
146 killall -HUP httpd
147 ;;
148 esac
149 }
150
151 display_form() {
152 echo "$1" | awk -F: -f /usr/lib/webif/form.awk
153 }
154
155 mkdir -p /tmp/.webif
156
157 load_settings() {
158 [ \! "$1" = "nvram" -a -f /etc/config/$1 ] && . /etc/config/$1
159 [ -f /tmp/.webif/config-$1 ] && . /tmp/.webif/config-$1
160 }
161
162 save_setting() {
163 oldval=$(eval "echo \${$2}")
164 oldval=${oldval:-$(nvram get "$2")}
165 [ "$oldval" != "$3" ] && echo "$2=\"$3\"" >> /tmp/.webif/config-$1
166 }