]> rtime.felk.cvut.cz Git - novaboot.git/blob - server/novaboot-shell
server: Fix creation of tftproot
[novaboot.git] / server / novaboot-shell
1 #!/bin/sh
2
3 set -e
4
5 die() {
6     echo >&2 "novaboot-shell: $*"
7     exit 1
8 }
9
10 print_help() {
11     cat <<EOF
12 Target commands:
13 - console
14 - reset
15 - on
16 - off
17 - rsync ...
18
19 Management commands:
20 - help
21 EOF
22
23     if [ "$NB_ADMIN" ]; then
24         cat <<EOF
25 - add-key
26 - shell (use with ssh -t)
27 EOF
28     fi
29     exit 0
30 }
31
32 add_key() {
33     local user
34     [ "$NB_ADMIN" ] || return 1
35
36     case $# in
37         0) die "Usage: ssh ... add-key USERNAME < id_rsa.pub";;
38         1) break;;
39         *) die "User name must not contain spaces: $*";;
40     esac
41     user="$1"
42     key=$(cat)
43
44     tmp=$(mktemp ~/.ssh/authorized_keys.XXXXXXXX)
45     {
46         cat ~/.ssh/authorized_keys
47         echo "command=\"user $user\" $key"
48     } | sort -u > $tmp
49
50     mv $tmp ~/.ssh/authorized_keys
51 }
52
53 exec_shell() {
54     [ "$NB_ADMIN" ] || die "Permission denied"
55     exec /bin/bash || exec /bin/sh
56 }
57
58 lock_queue() {
59     lslocks | awk '{ if ($9 == "'"$RUN_DIR"'") { print $2 } }'
60 }
61
62 print_queue() {
63     local queue
64
65     queue=$(
66         for pid in $(lock_queue); do
67             echo $pid $(sed --null-data -ne '/^NOVABOOT_ID=/ s///p' /proc/$pid/environ)
68         done | sort)
69     if [ "$queue" ]; then
70         echo "Target is occupied by:"
71         ( echo "PID USER LOGIN_TIME FROM"; echo "$queue" ) | column -t
72     fi
73 }
74
75 locked() {
76     print_queue
77     tmp=$(mktemp)
78     no_fork=
79     flock -h 2>&1 | grep -q -e "--no-fork" && no_fork=--no-fork
80     rm -f "$tmp"
81     exec flock $no_fork "$RUN_DIR" "$@"
82 }
83
84 unlocked() {
85     exec "$@"
86 }
87
88 read_config() {
89     . "${NOVABOOT_SHELL_CONFIG:-$HOME/.novaboot-shell}"
90 }
91
92 # run_subcommand should be called only after permission checks and/or locking
93 run_subcommand() {
94     read_config
95     case "$*" in
96         "console")
97             trap "rm -f $RUN_DIR/ppid" EXIT
98             echo $NOVABOOT_PPID > $RUN_DIR/ppid
99             echo 'novaboot-shell: Connected'
100             # TODO: $reset_begin_cmd
101             eval exec "${console_cmd:?}";;
102         "reset")
103             eval exec "${reset_cmd:?}";;
104         "rsync --server "*" . .")
105             if ! [ $# -eq 5 -o \( $# -eq 6 -a "$4" = '--log-format=X' \) ]; then
106                 die "Unexpected rsync invocation: $*"
107             fi
108             mkdir -p "$HOME/tftproot"
109             cd "$HOME/tftproot"
110             exec "$@";;
111         "on")
112             eval exec "${on_cmd:?}";;
113         "off")
114             eval exec "${off_cmd:?}";;
115     esac
116 }
117
118 main() {
119     if [ "$1" = "-c" ]; then
120         set -- $2
121     elif [ $# -gt 0 ]; then
122         die "Permission denied"
123     fi
124
125     NB_ADMIN=
126     if [ "$1" = "user" ]; then
127         # Get user name encoded in ~/.ssh/authorized_keys
128         NB_USER="$2";
129         [ "$3" = "admin" ] && NB_ADMIN=1
130         set -- $SSH_ORIGINAL_COMMAND
131     fi
132
133     if [ $# -eq 0 ]; then print_help; fi
134
135     IP=${SSH_CONNECTION%% *}
136     HOST=$(getent hosts $IP) || HOST=$IP
137     REMOTE=${HOST##* }
138     DATE=$(LANG=C date +'%F_%T')
139     export NOVABOOT_ID="${NB_USER:-?} $DATE ${REMOTE}"
140     export NOVABOOT_PPID=$PPID
141
142     mkdir -p "$RUN_DIR"
143
144     case "$1" in
145         # Commands allowed at any time
146         "console") locked $0 console;;
147         "get-config") read_config && echo -n "${target_config}"; exit;;
148         "add-key") shift; add_key "$@"; exit;;
149         "shell") exec_shell; exit;;
150         "help") print_help;;
151
152         # Commands allowed only when nobody or the same user is connected
153         # to the console. "The same user" means that we were executed by
154         # the same sshd process that has the lock. This is ensured by
155         # using SSH connection sharing on cline side.
156         reset | rsync | on | off)
157             ALLOWED_PPID=$(cat $RUN_DIR/ppid 2>/dev/null || :)
158             if [ "$PPID" -eq "${ALLOWED_PPID:-0}" ]; then run=unlocked; else run=locked; fi
159             $run $0 "$@";;
160         *)
161             echo >&2 "novaboot-shell: Command not allowed: $*"
162             logger -p error "novaboot-shell: Command not allowed: $*"
163             exit 1;;
164     esac
165 }
166
167 RUN_DIR="$HOME"
168
169 if [ -z "$NOVABOOT_ID" ]; then
170     main "$@"
171 else
172     run_subcommand "$@"
173 fi