tazpanel view index.cgi @ rev 434

boot.cgi: user support
author Pascal Bellard <pascal.bellard@slitaz.org>
date Mon Apr 06 20:48:48 2015 +0200 (2015-04-06)
parents 73305834575f
children b0146d791379
line source
1 #!/bin/sh
2 #
3 # Main CGI interface for TazPanel. In on word: KISS. Use the main CSS form
4 # command so we are faster and do not load unneeded functions. If necessary
5 # you can use the lib/ dir to handle external resources.
6 #
7 # Copyright (C) 2011-2015 SliTaz GNU/Linux - BSD License
8 #
11 # Common functions from libtazpanel
13 . lib/libtazpanel
14 get_config
16 TITLE="TazPanel"
20 # Check whether a configuration file has been modified after installation
22 file_is_modified()
23 {
24 grep -l " $1$" $INSTALLED/*/md5sum | while read file; do
26 # Found, but can we do diff?
27 [ "$(grep -h " $1$" $file)" != "$(md5sum $1)" ] || break
28 orig=$(dirname $file)/volatile.cpio.gz
29 zcat $orig 2>/dev/null | cpio -t 2>/dev/null | grep -q "^${1#/}$" || break
31 case "$2" in
32 diff)
33 tmp=$(mktemp -d)
34 ( cd $tmp; zcat $orig | cpio -id ${1#/} )
35 diff -abu $tmp$1 $1 | sed "s|$tmp||"
36 rm -rf $tmp;;
37 button)
38 echo -n '<button name="action" value="diff" data-icon="diff">'$(gettext 'Differences')'</button>';;
39 esac
40 break
41 done
42 }
46 # OK status in table
48 ok_status_t() {
49 echo '<td><span class="diff-add" data-img="ok"></span></td></tr>'
50 }
54 #
55 # Things to do before displaying the page
56 #
58 [ -n "$(GET panel_pass)" ] &&
59 sed -i s@/:root:.*@/:root:$(GET panel_pass)@ $HTTPD_CONF
65 #
66 # Commands
67 #
69 case " $(GET) " in
72 *\ exec\ *)
73 # Execute command and display its result in a terminal-like window
75 header; TITLE=$(gettext 'TazPanel - exec'); xhtml_header
77 exec="$(GET exec)"
78 font="${TERM_FONT:-monospace}"
79 palette=$(echo $TERM_PALETTE | tr A-Z a-z)
81 cat <<EOT
82 <section>
83 <header>
84 $exec
85 $(back_button "$(GET back)" "$(GET back_caption)" "$(GET back_icon)")
86 </header>
87 <div class="term_">
88 <pre class="term $palette" style="font-family: '$font'">$($exec 2>&1 | htmlize | filter_taztools_msgs)</pre>
89 </div>
90 </section>
91 EOT
92 ;;
95 *\ file\ *)
96 #
97 # Handle files
98 #
99 header
100 file="$(GET file)"
101 action="$(POST action)"; [ -z "$action" ] && action="$(GET action)" # receive 'action' both on POST or GET
103 case $file in
104 *.html)
105 cat $file; exit 0 ;;
106 *)
107 TITLE=$(gettext 'TazPanel - File'); xhtml_header ;;
108 esac
110 case "$action" in
111 edit)
112 cat <<EOT
113 <section>
114 <header>
115 <span data-icon="edit">$file</span>
116 <form id="editform" method="post" action="?file=$file" class="nogap">
117 <button data-icon="save">$(gettext 'Save')</button>
118 <button name="action" value="diff" data-icon="diff">$(gettext 'Differences')</button>
119 </form>
120 </header>
121 <textarea form="editform" name="content" class="wide" rows="30" autofocus>$(cat $file | htmlize)</textarea>
122 </section>
123 EOT
124 #The space before textarea gets muddled when the form is submitted.
125 #It prevents anything else from getting messed up
126 ;;
128 setvar)
129 data="$(. $(GET file) ;eval echo \$$(GET var))"
130 cat <<EOT
131 <section>
132 <header>$(GET var)</header>
133 <form method="post" action="?file=$file" class="nogap">
134 <input type="hidden" name="var" value="$(GET var)">
135 <input type="text" name="content" value="${data:-$(GET default)}">
136 <button type="submit" data-icon="save">$(gettext 'Save')</button>
137 </form>
138 </section>
139 EOT
140 ;;
142 diff)
143 cat <<EOT
144 <section>
145 <pre id="diff">$(file_is_modified $file diff | syntax_highlighter diff)</pre>
146 </section>
147 EOT
148 ;;
150 *)
151 R=$(echo -en '\r')
152 if [ -n "$(POST content)" ]; then
153 if [ -n "$(POST var)" ]; then
154 sed -i "s|^\\($(POST var)=\\).*|\1\"$(POST content)\"|" $file
155 else
156 sed "s/$R /\n/g;s/$R%0//g" > $file <<EOT
157 $(POST content)
158 EOT
159 fi
160 fi
162 cat <<EOT
163 <section>
164 EOT
165 [ -w "$file" ] && cat <<EOT
166 <header>
167 <span data-icon="text">$file</span>
168 <form>
169 <input type="hidden" name="file" value="$file"/>
170 <button name="action" value="edit" data-icon="edit">$(gettext 'Edit')</button><!--
171 -->$(file_is_modified $file button)
172 </form>
173 </header>
174 EOT
175 cat <<EOT
176 <div>
177 <pre>
178 EOT
179 # Handle file type by extension as a Web Server does it.
180 case "$file" in
181 *.conf|*.lst)
182 syntax_highlighter conf ;;
183 *.sh|*.cgi)
184 syntax_highlighter sh ;;
185 *Xorg.0.log)
186 syntax_highlighter xlog ;;
187 *dmesg.log)
188 syntax_highlighter kernel ;;
189 *)
190 cat | htmlize ;;
191 esac < $file
192 cat <<EOT
193 </pre>
194 </div>
195 </section>
196 EOT
197 esac
198 ;;
202 *\ terminal\ *|*\ cmd\ *)
203 # Cmdline terminal
205 TITLE=$(gettext 'TazPanel - Terminal'); header; xhtml_header
207 historyfile=/root/.ash_history
208 #commands='cat cd date du fdisk help ls ping pwd who wget'
209 cmd=$(GET cmd)
211 path="$(GET path)"; path="${path:-/tmp}"; cd "${path/~//root}"
212 [ "$path" == '/root' ] && path='~'
213 user="$REMOTE_USER"
214 [ -z "$HOME" ] && HOME=/root # for apps that modified user settings
216 font="${TERM_FONT:-monospace}"
217 palette=$(echo $TERM_PALETTE | tr A-Z a-z)
219 [ -n "$cmd" -a "$cmd" != "$(tail -n1 $historyfile)" ] && echo "$cmd" >> $historyfile
222 # Terminal history
224 if [ "$cmd" == 'history' ]; then
225 cat <<EOT
226 <section>
227 <header>
228 $(gettext 'History')
229 <form><button name="terminal" data-icon="terminal">$(gettext 'Back')</button></form>
230 </header>
231 <form>
232 <input type="hidden" name="path" value="$path"/>
233 <pre class="term $palette" style="font-family: '$font'">
234 EOT
235 htmlize < $historyfile | awk -vrun="$(gettext 'run')" -vpath="$path" '
236 BEGIN { num=1 }
237 {
238 printf("%3d ", num);
239 cmd = $0
240 gsub("%", "%25", cmd); gsub("+", "%2B", cmd); gsub(" ", "+", cmd);
241 gsub("\"", "%22", cmd); gsub("!", "%21", cmd); gsub("'\''", "%27", cmd);
242 printf("<a data-icon=\"run\" href=\"?cmd=%s&path=%s\">%s</a> ", cmd, path, run);
243 printf("<input type=\"checkbox\" name=\"rm\" value=\"%d\" id=\"hist%d\">", num, num);
244 printf("<label for=\"hist%d\">%s</label>\n", num, $0);
245 num++
246 }'
247 cat <<EOT
248 </pre>
249 <footer>
250 <button name="rmhistory" data-icon="remove">$(gettext 'Clear')</button>
251 </footer>
252 </form>
253 </section>
254 EOT
255 xhtml_footer
256 exit 0
257 fi
260 # Terminal window
262 cat <<EOT
263 <span id="num_hist"></span>
264 <section>
265 <pre class="term $palette" style="font-family: '$font'" onclick="document.getElementById('typeField').focus()">
266 EOT
267 if [ -n "$cmd" ]; then
268 echo -n "<span class='color31'>$user@$(hostname)</span>:<span class='color33'>$path</span># "
269 echo "$cmd" | htmlize
270 fi
272 case "$cmd" in
273 usage|help)
274 gettext 'Small non-interactive terminal emulator.'; echo
275 gettext 'Run any command at your own risk, avoid interactive commands (nano, mc, ...)'; echo
276 ;;
277 wget*)
278 dl=/var/cache/downloads
279 [ ! -d "$dl" ] && mkdir -p $dl
280 eval_gettext 'Downloading to: $dl'; echo
281 cd $dl; $cmd 2>&1 ;;
282 cd|cd\ *)
283 path="${cmd#cd}"; path="${path:-/root}"; path="$(realpath $path)" ;;
284 ls|ls\ *)
285 $cmd -w80 --color=always 2>&1 | filter_taztools_msgs ;;
286 cat)
287 # Cmd must be used with an arg.
288 eval_gettext '$cmd needs an argument' ;;
289 mc|nano)
290 # List of restricted (interactive) commands
291 eval_gettext "Please, don't run interactive command \"$cmd\""; echo; echo ;;
292 *)
293 unset HTTP_REFERER # for fooling /lib/libtaz.sh formatting utils (<hr> in the terminal is so-so)
294 export DISPLAY=:0.0 # for run X applications
295 /bin/sh -c "$cmd" 2>&1 | htmlize | filter_taztools_msgs
296 ;;
297 esac
299 [ "$path" == '/root' ] && path='~'
300 cat <<EOT
301 <form id="term">
302 <div class="cmdline" id="cmdline"><span id="prompt"><span class="color31">$user@$(hostname)</span>:<span class="color33">$path</span># </span><span id="typeField"> </span></div>
303 <input type="hidden" name="path" value="$path"/>
304 <input type="hidden" name="cmd" id="cmd"/>
305 </form>
306 </pre>
307 </section>
309 <form>
310 <button name="termsettings" data-icon="settings">$(gettext 'Settings')</button>
311 <button name="cmd" value="history" data-icon="history">$(gettext 'History')</button>
312 </form>
314 <script type="text/javascript">
315 with (document.getElementById('typeField')) {
316 contentEditable=true;
317 focus();
318 }
319 document.onkeydown=keydownHandler;
320 EOT
322 # Export history as array.
323 # Escape "all \"quotes\" in quotes", and all '\'
324 # (because \u, \x, \c has special meaning and can produce syntax error and stop js)
325 sed 's|\\|\\\\|g; s|"|\\"|g' $historyfile | awk '
326 BEGIN{ i=1; printf("ash_history=[") }
327 { printf("\"%s\",", $0); i++ }
328 END{
329 printf("\"\"];")
330 i--; printf("cur_hist=\"%d\";max_hist=\"%d\";", i, i)
331 }'
332 cat <<EOT
333 </script>
334 EOT
335 ;;
338 *\ rmhistory\ *)
339 # Manage shell commandline history
340 historyfile=/root/.ash_history
342 # Return sed command for removing history lines ('8d12d' to remove 8 and 12 lines)
343 rms=$(echo $QUERY_STRING | awk 'BEGIN{RS="&";FS="="}{if($1=="rm")printf "%dd", $2}')
345 if [ -n "$rms" ]; then
346 # Actually remove lines
347 sed -i $rms $historyfile
348 # Redirects back to the history table
349 header "HTTP/1.1 301 Moved Permanently" "Location: ?terminal&cmd=history&path=$(GET path)"
350 exit 0
351 fi
352 ;;
355 *\ termsettings\ *)
356 # Terminal settings
357 TITLE=$(gettext 'TazPanel - Terminal'); header; xhtml_header;
358 user="$REMOTE_USER"
359 font="$(GET font)"; font="${font:-$TERM_FONT}"
360 palette="$(GET palette)"; palette="${palette:-$TERM_PALETTE}"
361 pal=$(echo $palette | tr A-Z a-z)
363 # Add or change settings in TazPanel config
364 if [ -z "$TERM_FONT" ]; then
365 echo -e "\n# Terminal font family\nTERM_FONT=\"$font\"" >> $CONFIG
366 else
367 sed -i "s|TERM_FONT=.*|TERM_FONT=\"$font\"|" $CONFIG
368 fi
369 if [ -z "$TERM_PALETTE" ]; then
370 echo -e "\n# Terminal color palette\nTERM_PALETTE=\"$palette\"" >> $CONFIG
371 else
372 sed -i "s|TERM_PALETTE=.*|TERM_PALETTE=\"$palette\"|" $CONFIG
373 fi
375 cat <<EOT
376 <section>
377 <header>
378 $(gettext 'Terminal settings')
379 <form>
380 <button name="terminal" data-icon="terminal">$(gettext 'Terminal')</button>
381 </form>
382 </header>
383 <pre class="term $pal" style="height: auto; font-family: '$font'">
384 <span class='color31'>$user@$(hostname)</span>:<span class='color33'>~</span># palette
386 $(
387 for i in $(seq 30 37); do
388 for b in 0 1; do
389 for j in $(seq 40 47); do
390 echo -n "<span class=\"color$b color$i color$j\">$i:$j</span>"
391 done
392 done
393 echo
394 done
395 )
396 </pre>
397 <footer>
398 <form class="wide">
399 $(gettext 'Font:')
400 <select name="font">
401 <option value="">$(gettext 'Default')</option>
402 $(fc-list :spacing=mono:lang=en family | sed '/\.pcf/d;/,/d;s|\\-|-|g' | sort -u | \
403 awk -vfont="$font" '{
404 printf("<option value=\"%s\"%s>%s</option>\n", $0, ($0 == font)?" selected":"", $0)
405 }')
406 </select>
407 $(gettext 'Palette:')
408 <select name="palette">
409 $(echo -e 'Tango\nLinux\nXterm\nRxvt\nEcho' | awk -vpal="$palette" '{
410 printf("<option value=\"%s\"%s>%s</option>\n", $0, ($0 == pal)?" selected":"", $0)
411 }')
412 </select>
413 <button name="termsettings" data-icon="ok">$(gettext 'Apply')</button>
414 </form>
415 </footer>
416 </section>
417 EOT
419 ;;
422 *\ top\ *)
423 TITLE=$(gettext 'TazPanel - Process activity'); header; xhtml_header
425 r=$(GET refresh)
426 cat <<EOT
427 <form>
428 <p>$(gettext 'Refresh:')
429 <input type="hidden" name="top"/>
430 <input type="radio" name="refresh" value="1" id="r1" $([ "$r" == 1 ] && echo checked) onchange="this.form.submit()"/>
431 <label for="r1">$(gettext '1s' )</label>
432 <input type="radio" name="refresh" value="5" id="r2" $([ "$r" == 5 ] && echo checked) onchange="this.form.submit()"/>
433 <label for="r2">$(gettext '5s' )</label>
434 <input type="radio" name="refresh" value="10" id="r3" $([ "$r" == 10 ] && echo checked) onchange="this.form.submit()"/>
435 <label for="r3">$(gettext '10s' )</label>
436 <input type="radio" name="refresh" value="" id="r4" $([ -z "$r" ] && echo checked) onchange="this.form.submit()"/>
437 <label for="r4">$(gettext 'none')</label>
438 </p>
439 </form>
440 EOT
441 [ -n "$r" ] && echo "<meta http-equiv=\"refresh\" content=\"$r\">"
443 echo '<section><div><pre class="term log">'
444 top -n1 -b | htmlize | sed \
445 -e 's|^[A-Z].*:|<span class="color1 color31">\0</span>|g' \
446 -e 's|^\ *PID|<span class="color1 color32">\0</span>|g'
447 echo '</pre></div></section>' ;;
450 *\ debug\ *)
451 TITLE=$(gettext 'TazPanel - Debug'); header; xhtml_header
453 cat <<EOT
454 <h2>$(gettext 'HTTP Environment')</h2>
456 <section>
457 <div>
458 <pre>$(httpinfo | syntax_highlighter conf)</pre>
459 </div>
460 </section>
461 EOT
462 ;;
465 *\ report\ *)
466 TITLE=$(gettext 'TazPanel - System report'); header; xhtml_header
468 [ -d /var/cache/slitaz ] || mkdir -p /var/cache/slitaz
469 output=/var/cache/slitaz/sys-report.html
471 cat <<EOT
473 <section>
474 <header>$(eval_gettext 'Reporting to: $output')</header>
475 <table class="wide zebra">
476 <tbody>
477 <tr><td>$(gettext 'Creating report header...')</td>
478 EOT
479 cat > $output <<EOT
480 <!DOCTYPE html>
481 <html xmlns="http://www.w3.org/1999/xhtml">
482 <head>
483 <meta charset="utf-8"/>
484 <title>$(gettext 'SliTaz system report')</title>
485 <style type="text/css">
486 body { padding: 20px 60px; font-size: 13px; }
487 h1, h2 { color: #444; }
488 pre { background: #f1f1f1; border: 1px solid #ddd;
489 padding: 10px; border-radius: 4px; }
490 span.diff-rm { color: red; }
491 span.diff-add { color: green; }
492 </style>
493 </head>
494 <body>
495 EOT
496 cat <<EOT
497 $(ok_status_t)
498 <tr><td>$(gettext 'Creating system summary...')</td>
499 EOT
500 cat >> $output <<EOT
501 <h1>$(gettext 'SliTaz system report')</h1>
502 $(gettext 'Date:') $(date)
503 <pre>
504 uptime : $(uptime)
505 cmdline : $(cat /proc/cmdline)
506 version : $(cat /etc/slitaz-release)
507 packages : $(ls /var/lib/tazpkg/installed | wc -l) installed
508 kernel : $(uname -r)
509 </pre>
510 EOT
511 cat <<EOT
512 $(ok_status_t)
513 <tr><td>$(gettext 'Getting hardware info...')</td>
514 EOT
515 cat >> $output <<EOT
516 <h2>free</h2>
517 <pre>$(free)</pre>
519 <h2>lspci -k</h2>
520 <pre>$(lspci -k)</pre>
522 <h2>lsusb</h2>
523 <pre>$(lsusb)</pre>
525 <h2>lsmod</h2>
526 <pre>$(lsmod)</pre>
528 EOT
529 cat <<EOT
530 $(ok_status_t)
531 <tr><td>$(gettext 'Getting networking info...')</td>
532 EOT
533 cat >> $output <<EOT
534 <h2>ifconfig -a</h2>
535 <pre>$(ifconfig -a)</pre>
537 <h2>route -n</h2>
538 <pre>$(route -n)</pre>
540 <h2>/etc/resolv.conf</h2>
541 <pre>$(cat /etc/resolv.conf)</pre>
542 EOT
543 cat <<EOT
544 $(ok_status_t)
545 <tr><td>$(gettext 'Getting filesystems info...')</td>
546 EOT
547 cat >> $output <<EOT
548 <h2>blkid</h2>
549 <pre>$(blkid)</pre>
551 <h2>fdisk -l</h2>
552 <pre>$(fdisk -l)</pre>
554 <h2>mount</h2>
555 <pre>$(mount)</pre>
557 <h2>df -h</h2>
558 <pre>$(df -h)</pre>
560 <h2>df -i</h2>
561 <pre>$(df -i)</pre>
562 EOT
563 cat <<EOT
564 $(ok_status_t)
565 <tr><td>$(gettext 'Getting boot logs...')</td>
566 EOT
567 cat >> $output <<EOT
568 <h2>$(gettext 'Kernel messages')</h2>
569 <pre>$(cat /var/log/dmesg.log)</pre>
571 <h2>$(gettext 'Boot scripts')</h2>
572 <pre>$(cat /var/log/boot.log | filter_taztools_msgs)</pre>
573 EOT
574 cat <<EOT
575 $(ok_status_t)
576 <tr><td>$(gettext 'Creating report footer...')</td>
577 EOT
578 cat cat >> $output <<EOT
579 </body>
580 </html>
581 EOT
582 cat <<EOT
583 $(ok_status_t)
584 </tbody>
585 </table>
586 <footer>
587 <form><button name="file" value="$output" data-icon="view">$(gettext 'View report')</button></form>
588 </footer>
589 </section>
592 $(msg tip "$(gettext 'This report can be attached with a bug report on:')
593 <a href="http://bugs.slitaz.org/" target="_blank">bugs.slitaz.org</a></p>")
594 EOT
595 ;;
598 *)
599 #
600 # Default xHTML content
601 #
602 header; xhtml_header
603 [ -n "$(GET gen_locale)" ] && new_locale=$(GET gen_locale)
604 [ -n "$(GET rdate)" ] && echo ""
605 hostname=$(hostname)
607 cat <<EOT
608 <h2>$(eval_gettext 'Host: $hostname')</h2>
609 <p>$(gettext 'SliTaz administration and configuration Panel')<p>
611 <form class="nogap"><!--
612 EOT
613 [ "$REMOTE_USER" == "root" ] && cat <<EOT
614 --><button name="terminal" data-icon="terminal">$(gettext 'Terminal' )</button><!--
615 --><button name="report" data-icon="report" >$(gettext 'Create a report' )</button><!--
616 EOT
617 cat <<EOT
618 --><button name="top" data-icon="proc" >$(gettext 'Process activity')</button>
619 </form>
621 <section>
622 <header>$(gettext 'Summary')</header>
623 <table>
624 <tr><td>$(gettext 'Uptime:')</td>
625 <td id="uptime">$(uptime | sed 's|\([0-9.:][0-9.:]*\)|<b>\1</b>|g')</td>
626 </tr>
627 <tr><td>$(gettext 'Memory in Mb:')</td>
628 <td>$(free -m | grep Mem: | \
629 awk -vline="$(gettext 'Total: %d, Used: %d, Free: %d')" \
630 '{ printf(line, $2, $3, $4) }' | \
631 sed 's|\([0-9][0-9]*\)|<b>\1</b>|g')</td>
632 </tr>
633 <tr><td>$(gettext 'Linux kernel:')</td>
634 <td>$(uname -r)</td>
635 </tr>
636 </table>
637 </section>
640 <section>
641 <header>
642 $(gettext 'Network status')
643 <form action="network.cgi">
644 <button data-icon="wifi">$(gettext 'Network')</button>
645 </form>
646 </header>
647 $(list_network_interfaces)
648 </section>
651 <section>
652 <header>
653 $(gettext 'Filesystem usage statistics')
654 <form action="hardware.cgi">
655 <button data-icon="hdd">$(gettext 'Disks')</button>
656 </form>
657 </header>
658 <table class="wide zebra center">
659 EOT
660 # Disk stats (management is done as hardware.cgi)
661 df_thead
662 echo '<tbody>'
663 df -h | grep ^/dev | while read fs size used av pct mp; do
664 cat <<EOT
665 <tr>
666 <td><span data-icon="hdd">${fs#/dev/}</span></td>
667 <td>$(blkid $fs | sed '/LABEL=/!d;s/.*LABEL="\([^"]*\).*/\1/')</td>
668 <td>$(blkid $fs | sed '/TYPE=/!d;s/.*TYPE="\([^"]*\).*/\1/')</td>
669 <td>$size</td>
670 <td>$av</td>
671 <td class="meter"><meter min="0" max="100" value="$(echo $pct | cut -d% -f1)"
672 low="$DU_WARN" high="$DU_CRIT" optimum="10"></meter>
673 <span>$used - $pct</span>
674 </td>
675 <td>$mp</td>
676 <td>$(blkid $fs | sed '/UUID=/!d;s/.*UUID="\([^"]*\).*/\1/')</td>
677 </tr>
678 EOT
679 done
680 cat <<EOT
681 </tbody>
682 </table>
683 </section>
685 <section>
686 <header>
687 $(gettext 'Panel Activity')
688 <form>
689 <button name="file" value="$LOG_FILE" data-icon="view">$(gettext 'View')</button>
690 </form>
691 </header>
692 <div>
693 <pre id="panel-activity">
694 $(cat $LOG_FILE | tail -n 8 | sort -r | syntax_highlighter activity)</pre>
695 </div>
696 </section>
697 EOT
698 ;;
699 esac
701 xhtml_footer
702 exit 0