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