slitaz-dev-tools view mirror-tools/rootfs/usr/share/mirror-host/boot/shellinabox.sh @ rev 309

Remove ashism ==
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Feb 26 08:23:49 2019 +0100 (2019-02-26)
parents e0a09d90b2cb
children
line source
1 #!/bin/sh
3 services="/ /reset /reboot /ssh"
5 mkexe()
6 {
7 exe=$0
8 while true; do
9 cd $(dirname $exe)
10 exe=$(basename $exe)
11 [ -L $exe ] || break
12 exe=$(readlink $exe)
13 done
14 echo $(pwd)/$exe
15 }
16 exe=$(mkexe)
18 services_arg()
19 {
20 for i in $services ; do
21 echo -n "-s '$i:root:root:/tmp:LINES=25 /bin/sh -c \"$exe service $i "
22 echo -n "\${peer} \${url} \${columns} \${lines}\"' "
23 done
24 }
26 launch_ssh()
27 {
28 # Get SSH server
29 server=""
30 echo -n "SSH server: "
31 read -t 300 server || exit 1
32 [ -n "$server" ] || exit 1
34 # Allow non default SSH port with format server:port
35 sshport=""
36 case "$server" in
37 *:*) sshport="-p ${server#*:}"
38 server=${server%:*};;
39 esac
41 # heading ! in server name means open remote ssh port with a knock sequence
42 if [ -x /usr/bin/knock ]; then
43 case "$server" in
44 !*) server=${server#*!}
45 echo -n "Knock sequence (port[:proto]...): "
46 read -t 30 sequence && knock ${server#*@} $sequence
47 esac
48 fi
50 # Get SSH user if missing
51 case "$server" in
52 *@*) ;;
53 *) echo -n "$server login: "
54 read -t 30 user || exit 1
55 server="$user@$server";;
56 esac
58 # Launch OpenSSH or Dropbear
59 sshargs="-oPreferredAuthentications=keyboard-interactive,password -oNoHostAuthenticationForLocalhost=yes -oLogLevel=FATAL -F/dev/null";
60 [ -L /usr/bin/ssh ] && sshargs=""
61 exec ssh $sshport $sshargs $server
62 }
64 auth()
65 {
66 while read host md5 ; do
67 [ "${host#*.}" = "${1#*.}" ] && break
68 done < $(dirname $exe)/shellinabox.secrets
69 echo -n "$host password: "
70 read -s -t 30 password || exit 1
71 [ "$(echo $password | md5sum)" = "$md5 -" ] || exit 1
72 echo ""
73 }
75 pidfile=/var/run/shellinaboxd.pid
76 case "$1" in
77 service)
78 host=$(echo $4 | sed 's/.*\/\/\(.*\):.*/\1/')
79 tty=$(awk "/$host/ { print \$2 }" /etc/inittab)
80 vm=$(awk "/$host/ { print \$3 }" /etc/inittab)
81 [ -n "$vm" ] || exit 1
82 case "$2" in
83 /)
84 auth $host
85 exec /usr/bin/conspy ${tty#tty} ;;
86 /reboot)
87 auth $host
88 echo -n "Sure to reboot $host now (YES or NO) ?"
89 read -t 30 answer || exit 1
90 case "$answer" in
91 YES*) ;;
92 *) exit 1;;
93 esac
94 exec /bin/kill $(ps ww | grep $vm | awk '/lguest/ { printf "%s ",$1 }') ;;
95 /ssh)
96 auth $host
97 launch_ssh ;;
98 esac
99 ;;
100 start)
101 dir=$(dirname $exe)
102 eval shellinaboxd --background=$pidfile --cert=/boot/cert $(services_arg)
103 ;;
104 stop)
105 [ -s $pidfile ] && kill $(cat $pidfile)
106 ;;
107 esac