tazpanel view network.cgi @ rev 419
Bunch of changes. Development in progress, please note it have few known bugs.
author | Aleksej Bobylev <al.bobylev@gmail.com> |
---|---|
date | Tue Mar 24 03:39:08 2015 +0200 (2015-03-24) |
parents | 106b85c1951c |
children | a279382f786f |
line source
1 #!/bin/sh
2 #
3 # Network configuration CGI interface
4 #
5 # Copyright (C) 2012-2015 SliTaz GNU/Linux - BSD License
6 #
9 # Common functions from libtazpanel
11 . lib/libtazpanel
12 get_config
13 header
15 TITLE=$(gettext 'TazPanel - Network')
18 # Start a Wi-Fi connection
20 start_wifi() {
21 sed -i \
22 -e 's|^WIFI=.*|WIFI="yes"|' \
23 -e 's|^DHCP=.*|DHCP="yes"|' \
24 -e 's|^STATIC=.*|STATIC="no"|' /etc/network.conf
25 ifconfig $WIFI_INTERFACE up
26 iwconfig $WIFI_INTERFACE txpower auto
27 /etc/init.d/network.sh restart | log
28 # Sleep until connection established (max 20 seconds)
29 for i in $(seq 20); do
30 [ -n "$(iwconfig 2>/dev/null | fgrep Link)" ] && break
31 sleep 1
32 done
33 }
36 # Use /etc/wpa/wpa.conf as single database for known networks, passwords, etc.
37 # Translate this data to use in javascript.
39 parse_wpa_conf() {
40 awk '
41 BEGIN { print "networks = ["; begin_list = 1; network = 0; }
42 {
43 if ($0 == "network={") {
44 if (begin_list == 0) print ",";
45 begin_list = 0;
46 printf "{"; begin_obj = 1;
47 network = 1; next;
48 }
49 if (network == 1) {
50 if ($0 ~ "=") {
51 if (begin_obj == 0) printf ", ";
52 begin_obj = 0;
53 split($0, a, "=");
54 if (a[2] ~ "\"")
55 printf "%s:%s", a[1], a[2];
56 else
57 printf "%s:\"%s\"", a[1], a[2];
58 }
59 }
60 if (network == 1 && $0 ~ "}") { printf "}"; network = 0; next; }
61 }
62 END {print "\n];"}
63 ' /etc/wpa/wpa.conf | sed 's|\t||g;'
64 }
67 # Waiting for network link up
69 wait_up() {
70 for i in $(seq 10); do
71 [ -z "$(cat /sys/class/net/*/operstate | fgrep up)"] && sleep 1
72 done
73 }
75 # Actions commands before page is displayed
77 case " $(GET) " in
78 *\ start\ *)
79 /etc/init.d/network.sh start | log
80 # Here we sleep a bit to let udhcp get the lease before reloading
81 # the page with status
82 wait_up ;;
83 *\ stop\ *)
84 /etc/init.d/network.sh stop | log ;;
85 *\ restart\ *)
86 /etc/init.d/network.sh restart | log
87 wait_up ;;
88 *\ start-wifi\ *)
89 start_wifi ;;
90 *\ host\ *)
91 get_hostname="$(GET host)"
92 echo $(eval_gettext 'Changed hostname: $get_hostname') | log
93 echo "$get_hostname" > /etc/hostname ;;
94 esac
97 # Get values only now since they could have been modified by actions.
99 . /etc/network.conf
105 #
106 # Main Commands for pages
107 #
109 case " $(GET) " in
111 *\ scan\ *)
112 # Scan open ports
113 scan=$(GET scan); back=$(GET back)
114 xhtml_header
115 LOADING_MSG=$(gettext 'Scanning open ports...'); loading_msg
117 cat <<EOT
118 <section>
119 <header>
120 $(eval_gettext 'Port scanning for $scan')
121 $(back_button "$back" "$(gettext 'Network')" "")
122 </header>
123 <pre>$(pscan -b $scan)</pre>
124 </section>
125 EOT
126 ;;
129 *\ eth\ *)
130 # Wired connections settings
131 xhtml_header
132 if [ "$(GET ip)" ]; then
133 DHCP=no
134 STATIC=no
135 [ -n "$(GET dhcp)" ] && DHCP=yes
136 [ -n "$(GET static)" ] && STATIC=yes
137 LOADING_MSG=$(gettext 'Setting up IP...'); loading_msg
139 sed -i \
140 -e s"/^INTERFACE=.*/INTERFACE=\"$(GET iface)\""/ \
141 -e s"/^DHCP=.*/DHCP=\"$DHCP\"/" \
142 -e s"/^STATIC=.*/STATIC=\"$STATIC\"/" \
143 -e s"/^NETMASK=.*/NETMASK=\"$(GET netmask)\"/" \
144 -e s"/^GATEWAY=.*/GATEWAY=\"$(GET gateway)\"/" \
145 -e s"/^DNS_SERVER=.*/DNS_SERVER=\"$(GET dns)\"/" \
146 -e s"/^IP=.*/IP=\"$(GET ip)\"/" /etc/network.conf
147 /etc/init.d/network stop | log
148 sleep 2
149 /etc/init.d/network start | log
150 . /etc/network.conf
151 fi
153 PAR1="size=\"20\" required"; PAR="$PAR1 pattern=\"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\" data-x=\"Four numbers, each in range 0-255, delimited by full stop.\""
154 cat <<EOT
155 <h2>$(gettext 'Ethernet connection')</h2>
157 <p>$(gettext "Here you can configure a wired connection using DHCP to \
158 automatically get a random IP or configure a static/fixed IP")</p>
160 <section>
161 <header>$(gettext 'Configuration')</header>
162 <form id="conf">
163 <input type="hidden" name="eth"/>
164 <div>
165 <table>
166 <tr><td>$(gettext 'Interface')</td>
167 <td><select name="iface" value="$INTERFACE" style="width:100%"/>
168 $(cd /sys/class/net; ls -1 | awk -viface="$INTERFACE" '{
169 sel = ($0 == iface) ? " selected":""
170 printf "<option value=\"%s\"%s>%s", $0, sel, $0
171 }')
172 </select></td>
173 </tr>
174 <tr><td> </td>
175 <td><label><input type="checkbox" name="staticip" id="staticip"/>
176 $(gettext 'Use static IP')</td>
177 <tr><td>$(gettext 'IP address')</td>
178 <td><input type="text" name="ip" id="st1" value="$IP" $PAR/></td>
179 </tr>
180 <tr><td>$(gettext 'Netmask')</td>
181 <td><input type="text" name="netmask" id="st2" value="$NETMASK" $PAR/></td>
182 </tr>
183 <tr><td>$(gettext 'Gateway')</td>
184 <td><input type="text" name="gateway" id="st3" value="$GATEWAY" $PAR/></td>
185 </tr>
186 <tr><td>$(gettext 'DNS server')</td>
187 <td><input type="text" name="dns" id="st4" value="$DNS_SERVER" $PAR/></td>
188 </tr>
189 </table>
190 </div>
191 </form>
192 <footer><!--
193 --><button form="conf" type="submit" name="static" data-icon="ok" >$(gettext 'Activate (static)')</button><!--
194 --><button form="conf" type="submit" name="dhcp" data-icon="ok" >$(gettext 'Activate (DHCP)' )</button><!--
195 --><button form="conf" name="disable" data-icon="cancel">$(gettext 'Disable' )</button><!--
196 --></footer>
197 </section>
199 <script type="text/javascript">
200 document.getElementById('staticip').onchange = static_change;
202 function static_change() {
203 staticip = document.getElementById('staticip');
204 alert(staticip.checked);
205 }
206 </script>
208 <section>
209 <header>
210 $(gettext 'Configuration file')
211 <form action="index.cgi">
212 <input type="hidden" name="file" value="/etc/network.conf"/>
213 <button name="action" value="edit" data-icon="edit">$(gettext 'Edit')</button>
214 </form>
215 </header>
216 <div>$(gettext "These values are the ethernet settings in the main /etc/network.conf configuration file")</div>
217 <pre>$(awk '{if($1 !~ "WIFI" && $1 !~ "#" && $1 != ""){print $0}}' /etc/network.conf | syntax_highlighter conf)</pre>
218 </section>
219 EOT
220 ;;
224 *\ wifi_list\ *)
225 # Catch ESSIDs and format output.
226 # We get the list of networks by Cell and without spaces.
228 HIDDEN="$(gettext '(hidden)')"
230 cat <<EOT
231 <table class="wide center zebra">
232 <thead>
233 <tr>
234 <td>$(gettext 'Name')</td>
235 <td>$(gettext 'Signal level')</td>
236 <td>$(gettext 'Channel')</td>
237 <td>$(gettext 'Encryption')</td>
238 <td>$(gettext 'Status')</td>
239 </tr>
240 </thead>
241 <tbody>
242 EOT
243 if [ -d /sys/class/net/$WIFI_INTERFACE/wireless ]; then
244 ifconfig $WIFI_INTERFACE up
245 for i in $(iwlist $WIFI_INTERFACE scan | sed '/Cell /!d;s/.*Cell \([^ ]*\).*/Cell.\1/')
246 do
247 SCAN=$(iwlist $WIFI_INTERFACE scan last | sed "/$i/,/Cell/!d" | sed '$d')
249 BSSID=$(echo "$SCAN" | sed -n 's|.*Address: \([^ ]*\).*|\1|p')
251 CHANNEL=$(echo "$SCAN" | sed -n 's|.*Channel[:=]\([^ ]*\).*|\1|p')
253 QUALITY=$(echo "$SCAN" | sed -n 's|.*Quality[:=]\([^ ]*\).*|\1|p')
254 QUALITY_ICON="lvl$(( 5*${QUALITY:-0} ))" # lvl0 .. lvl4, lvl5
255 LEVEL=$(echo "$SCAN" | sed -n 's|.*Signal level[:=]\([^ ]*\).*|\1|p; s|-|−|')
257 ENCRYPTION=$(echo "$SCAN" | sed -n 's|.*Encryption key[:=]\([^ ]*\).*|\1|p') # on/off
259 ESSID=$(echo "$SCAN" | sed -n 's|.*ESSID:"\([^"]*\).*|\1|p')
261 # WPA Type - Group Cipher - Pairwise Ciphers - Authentication Suites
262 # {WPA|WPA2}-{TKIP|CCMP}-{TKIP|CCMP|TKIP CCMP}-{PSK|802.1x}
263 #CAPABILITIES="$(echo "$SCAN" | grep -e 'IE: .*WPA*' -A3 | cut -d: -f2 | sed -e 's|^ ||' -e '/WPA2/s|.*|=WPA2|' -e '/WPA /s|.*|=WPA|' -e '/--/d' | tr '\n' '-' | tr '=' '\n' | sed -e '/^$/d' -e 's|-$||')"
265 # Authentication type
266 AUTH="$(echo "$SCAN" | sed -n 's|.*Authentication Suites[^:]*: *\(.*\)|\1|p')"
267 if [ -n "$(echo -n $AUTH | fgrep PSK)" ]; then
268 # WPA-Personal. Authentication using password (PSK = pre-shared key)
269 WIFI_KEY_TYPE='WPA'
270 elif [ -n "$(echo -n $AUTH | fgrep 802.1x)" ]; then
271 # WPA-Enterprise. Authentication using username, password, certificates...
272 WIFI_KEY_TYPE='EAP'
273 else
274 WIFI_KEY_TYPE='NONE'
275 fi
277 # Check encryption type
278 if [ "$ENCRYPTION" == 'on' ]; then
279 # "WPA" or "WPA2" or "WPA/WPA2" (maybe also "WPA2/WPA")
280 ENC_SIMPLE=$(echo "$SCAN" | sed -n '/.*WPA.*/ s|.*\(WPA[^ ]*\).*|\1|p')
281 ENC_SIMPLE=$(echo $ENC_SIMPLE | sed 's| |/|')
282 ENC_ICON='sechi' # high
283 if [ -z "$ENC_SIMPLE" ]; then
284 WIFI_KEY_TYPE='WEP'
285 ENC_SIMPLE='WEP'; ENC_ICON='secmi' # middle
286 fi
287 else
288 WIFI_KEY_TYPE='NONE'
289 ENC_SIMPLE="$(gettext 'None')"; ENC_ICON='seclo' # low
290 fi
292 #
293 #if echo $SCAN | grep -q 'Mode:Managed'; then
294 # AP="&ap=$(echo $SCAN | sed 's/.*Address: \([^ ]*\).*/\1/')"
295 #else
296 # AP=''
297 #fi
299 # Connected or not connected...
300 if ifconfig $WIFI_INTERFACE | fgrep -q inet && \
301 iwconfig $WIFI_INTERFACE | fgrep -q "ESSID:\"$ESSID\""; then
302 status="$(gettext 'Connected')"
303 else
304 status='---'
305 fi
307 cat <<EOT
308 <tr>
309 <td><a data-icon="wifi" onclick="loadcfg('$ESSID', '$BSSID', '$WIFI_KEY_TYPE')">${ESSID:-$HIDDEN}</a></td>
310 <td><span data-icon="$QUALITY_ICON" title="Quality: $QUALITY"> $LEVEL dBm</span></td>
311 <td>$CHANNEL</td>
312 <td><span data-icon="$ENC_ICON">$ENC_SIMPLE</span></td>
313 <td>$status</td>
314 </tr>
315 EOT
316 done
317 fi
318 cat <<EOT
319 </tbody>
320 </table>
321 EOT
322 exit 0
323 ;;
326 *\ wifi\ *)
327 # Wireless connections settings
328 xhtml_header
330 . /etc/network.conf
331 cat <<EOT
332 <h2>$(gettext 'Wireless connection')</h2>
334 <form>
335 <input type="hidden" name="wifi"/>
336 EOT
338 start_disabled=''; stop_disabled=''
339 if iwconfig 2>/dev/null | grep -q 'Tx-Power=off'; then
340 stop_disabled='disabled'
341 else
342 start_disabled='disabled'
343 fi
345 cat <<EOT
346 <button name="start-wifi" data-icon="start" $start_disabled>$(gettext 'Start')</button><!--
347 --><button name="stop" data-icon="stop" $stop_disabled >$(gettext 'Stop' )</button><!--
348 --><button type="submit" data-icon="refresh" $stop_disabled >$(gettext 'Scan' )</button>
349 </form>
350 EOT
352 if [ -n "$start_disabled" ]; then
353 cat <<EOT
354 <section id="wifiList">
355 <div style="text-align: center;"><span id="ajaxStatus"></span>$(gettext 'Scanning wireless interface...')</div>
356 </section>
358 <script type="text/javascript">
359 ajax('/network.cgi?wifi_list', '1', 'wifiList');
360 $(parse_wpa_conf)
361 </script>
362 EOT
364 ESSID="$(GET essid)"
365 #WIFI_KEY_TYPE="$(GET keyType)"
366 #WIFI_KEY="$(GET key)"
367 #WIFI_AP="$(GET ap)"
369 if [ -n "$ESSID" ]; then
370 /etc/init.d/network.sh stop | log
371 sed -i \
372 -e "s/^WIFI_ESSID=.*/WIFI_ESSID=\"$essid\"/" \
373 -e "s/^WIFI_KEY_TYPE=.*/WIFI_KEY_TYPE=\"$WIFI_KEY_TYPE\"/" \
374 -e "s/^WIFI_KEY=.*/WIFI_KEY=\"$WIFI_KEY\"/" \
375 -e "s/^WIFI_AP=.*/WIFI_AP=\"$WIFI_AP\"/" \
376 /etc/network.conf
377 . /etc/network.conf
378 start_wifi
379 fi
381 # ESSID names are clickable
382 #SELECT="$(GET select)"
383 #if [ -n "$SELECT" ]; then
384 # [ "$SELECT" != "$WIFI_ESSID" ] && WIFI_KEY=''
385 # WIFI_ESSID="$SELECT"
386 #fi
388 cat <<EOT
389 <section>
390 <header>$(gettext 'Connection')</header>
391 <div>
392 <form id="connection">
393 <input type="hidden" name="connect-wifi"/>
394 <table>
395 <tr><td>$(gettext 'Network SSID')</td>
396 <td><input type="text" name="essid" value="$WIFI_ESSID" id="essid"/></td>
397 </tr>
399 <tr><td>$(gettext 'Security')</td>
400 <td><select name="keyType" id="keyType">
401 <option value="NONE">$(gettext 'None')</option>
402 <option value="WEP" >WEP</option>
403 <option value="WPA" >WPA/WPA2 PSK</option>
404 <option value="EAP" >802.1x EAP</option>
405 </select>
406 </td>
407 </tr>
409 <tr class="eap">
410 <td><div>$(gettext 'EAP method')</div></td>
411 <td><div><select name="eap" id="eap">
412 <option value="PEAP">PEAP</option>
413 <option value="TLS" >TLS</option>
414 <option value="TTLS">TTLS</option>
415 <option value="PWD" >PWD</option>
416 </select>
417 </div></td>
418 </tr>
420 <tr class="eap1">
421 <td><div>$(gettext 'Phase 2 authentication')</div></td>
422 <td><div><select name="phase2" id="phase2">
423 <option value="none" >$(gettext 'None')</option>
424 <option value="pap" >PAP</option>
425 <option value="mschap" >MSCHAP</option>
426 <option value="mschapv2">MSCHAPV2</option>
427 <option value="gtc" >GTC</option>
428 </select>
429 </div></td>
430 </tr>
432 <tr class="eap1">
433 <td><div>$(gettext 'CA certificate')</div></td>
434 <td><div><input type="text" name="caCert" id="caCert"></div></td>
435 </tr>
437 <tr class="eap1">
438 <td><div>$(gettext 'User certificate')</div></td>
439 <td><div><input type="text" name="clientCert" id="clientCert"></div></td>
440 </tr>
442 <tr class="eap">
443 <td><div>$(gettext 'Identity')</div></td>
444 <td><div><input type="text" name="identity" id="identity"></div></td>
445 </tr>
447 <tr class="eap1">
448 <td><div>$(gettext 'Anonymous identity')</div></td>
449 <td><div><input type="text" name="anonymousIdentity" id="anonymousIdentity"></div></td>
450 </tr>
452 <tr class="wep wpa eap">
453 <td><div>$(gettext 'Password')</div></td>
454 <td><div>
455 <input type="password" name="password" value="$WIFI_KEY" id="password"/>
456 <span data-img="view" title="$(gettext 'Show password')"
457 onmousedown="document.getElementById('password').type='text'; return false"
458 onmouseup="document.getElementById('password').type='password'"
459 onmouseout="document.getElementById('password').type='password'"
460 ></span>
461 </div></td>
462 </tr>
465 <!--
466 <tr><td>$(gettext 'Access point')</td>
467 <td><input type="text" name="ap" value="$WIFI_AP"/></td>
468 </tr>
469 -->
471 <script type="text/javascript">
472 function wifiSettingsChange() {
473 document.getElementById('connection').className =
474 document.getElementById('keyType').value.toLowerCase() + ' ' +
475 document.getElementById('eap').value.toLowerCase();
476 }
477 document.getElementById('keyType').onchange = wifiSettingsChange;
478 document.getElementById('eap').onchange = wifiSettingsChange;
480 document.getElementById('keyType').value = "$WIFI_KEY_TYPE"; wifiSettingsChange();
481 </script>
483 <style type="text/css">
484 #connection input[type="text"], #connection input[type="password"] { width: 14rem; }
485 #connection select { width: 14.4rem; }
487 #connection td { padding: 0; margin: 0; }
488 #connection [class] div {
489 max-height: 0; overflow: hidden; padding: 0; margin: 0;
490 -webkit-transition: all 0.5s ease-in-out;
491 -moz-transition: all 0.5s ease-in-out;
492 transition: all 0.5s ease-in-out;
493 }
494 .wep .wep div, .wpa .wpa div, .eap .eap div,
495 .eap.peap .eap1 div, .eap.tls .eap1 div, .eap.ttls .eap1 div {
496 max-height: 2em !important;
497 }
498 </style>
500 </table>
501 </form>
502 </div>
503 <footer>
504 <button form="connection" type="submit" name="wifi" data-icon="ok">$(gettext 'Configure')</button>
505 </footer>
506 </section>
507 EOT
508 fi
510 cat <<EOT
511 <section>
512 <header>
513 $(gettext 'Configuration file')
514 <form action="index.cgi">
515 <input type="hidden" name="file" value="/etc/network.conf"/>
516 <button name="action" value="edit" data-icon="edit">$(gettext 'Edit')</button>
517 </form>
518 </header>
519 <div>$(gettext "These values are the wifi settings in the main /etc/network.conf configuration file")</div>
520 <pre>$(grep ^WIFI /etc/network.conf | sed '/WIFI_KEY=/s|".*"|"********"|' | syntax_highlighter conf)</pre>
521 </section>
524 <section>
525 <header>$(gettext 'Output of iwconfig')</header>
526 <pre>$(iwconfig)</pre>
527 </section>
528 EOT
529 ;;
532 *)
533 # Main Network page starting with a summary
534 xhtml_header
536 stop_disabled=''; start_disabled=''
537 if cat /sys/class/net/*/operstate | fgrep -q up; then
538 start_disabled='disabled'
539 else
540 stop_disabled='disabled'
541 fi
543 cat <<EOT
544 <h2>$(gettext 'Networking')</h2>
546 <p>$(gettext 'Manage network connections and services')</p>
548 <form action="index.cgi" id="indexform"></form>
550 <form id="mainform"><!--
551 --><button name="start" data-icon="start" $start_disabled>$(gettext 'Start' )</button><!--
552 --><button name="stop" data-icon="stop" $stop_disabled >$(gettext 'Stop' )</button><!--
553 --><button name="restart" data-icon="restart" $stop_disabled >$(gettext 'Restart')</button>
554 </form>
555 <div class="float-right"><!--
556 -->$(gettext 'Configuration:')<!--
557 --><button form="indexform" name="file" value="/etc/network.conf" data-icon="conf">network.conf</button><!--
558 --><button form="mainform" name="eth" data-icon="eth">Ethernet</button><!--
559 --><button form="mainform" name="wifi" data-icon="wifi">Wireless</button>
560 </div>
563 <section>
564 <header>$(gettext 'Network interfaces')</header>
565 $(list_network_interfaces)
566 </section>
569 <section>
570 <header id="hosts">$(gettext 'Hosts')</header>
571 <pre>$(cat /etc/hosts)</pre>
572 <footer>
573 <form action="index.cgi">
574 <input type="hidden" name="file" value="/etc/hosts"/>
575 <button name="action" value="edit" data-icon="edit">$(gettext 'Edit')</button>
576 </form>
577 </footer>
578 </section>
581 <section>
582 <header>$(gettext 'Hostname')</header>
583 <footer>
584 <form>
585 <!-- was: name="hostname"; please don't use 'name' in name: unwanted webkit styling -->
586 <input type="text" name="host" value="$(cat /etc/hostname)"/><!--
587 --><button type="submit" data-icon="ok">$(gettext 'Change')</button>
588 </form>
589 </footer>
590 </section>
593 <section>
594 <header id="ifconfig">$(gettext 'Output of ifconfig')</header>
595 <pre>$(ifconfig)</pre>
596 </section>
599 <section>
600 <header id="routing">$(gettext 'Routing table')</header>
601 <pre>$(route -n)</pre>
602 </section>
605 <section>
606 <header id="dns">$(gettext 'Domain name resolution')</header>
607 <pre>$(cat /etc/resolv.conf)</pre>
608 </section>
611 <section>
612 <header id="arp">$(gettext 'ARP table')</header>
613 <pre>$(arp)</pre>
614 </section>
617 <section>
618 <header id="connections">$(gettext 'IP Connections')</header>
619 <pre>$(netstat -anp 2>/dev/null | sed -e '/UNIX domain sockets/,$d' \
620 -e 's#\([0-9]*\)/#<a href="boot.cgi?daemons=pid=\1">\1</a>/#')</pre>
621 </section>
622 EOT
623 ;;
624 esac
626 xhtml_footer
627 exit 0