6 echo >&2 "novaboot-shell: $*"
13 - console (default command)
24 if [ "$NB_ADMIN" ]; then
27 - shell (use with ssh -t)
35 [ "$NB_ADMIN" ] || return 1
38 0) die "Usage: ssh ... add-key USERNAME < id_rsa.pub";;
40 *) die "User name must not contain spaces: $*";;
45 tmp=$(mktemp ~/.ssh/authorized_keys.XXXXXXXX)
47 cat ~/.ssh/authorized_keys
48 echo "command=\"user $user\" $key"
51 mv $tmp ~/.ssh/authorized_keys
55 [ "$NB_ADMIN" ] || die "Permission denied"
56 if ! tty > /dev/null; then
57 echo "novaboot-shell: Consider starting the shell with 'ssh -t'"
59 exec /usr/bin/env bash || exec /bin/sh
63 lslocks | awk '{ if ($9 == "'"$RUN_DIR"'") { print $2 } }'
70 for pid in $(lock_queue); do
71 echo $pid $(sed --null-data -ne '/^NOVABOOT_ID=/ s///p' /proc/$pid/environ)
74 echo "Target is occupied by:"
75 ( echo "PID USER LOGIN_TIME FROM"; echo "$queue" ) | column -t
81 exec flock --no-fork "$RUN_DIR" "$@"
89 . "${NOVABOOT_SHELL_CONFIG:-$HOME/.novaboot-shell}"
95 "on") cmd="${on_cmd:?}";;
96 "off") cmd="${off_cmd:?}";;
97 *) die "Unexpected power parameter";;
100 if [ "$PPID" -ne 1 ] && systemctl --user is-enabled --quiet novaboot-delayed-power-off.service; then
102 "on") systemctl --user start novaboot-delayed-power-off.service;;
103 "off") sudo novaboot-power off;;
111 trap "rm -f $RUN_DIR/ppid" EXIT
112 echo $NOVABOOT_PPID > $RUN_DIR/ppid
113 echo 'novaboot-shell: Connected'
114 # TODO: $reset_begin_cmd
115 [ -n "${on_cmd}" ] && power on
119 # Run novaboot with the same configuration as specified in
120 # ~/.novaboot-shell, but allow the caller to extend of override them
121 # via parameters of this function.
126 # Split $target_config below by newlines, not by words
129 novaboot "$nbscript" $target_config --server="$HOME/tftproot" --reset-cmd="${reset_cmd:?}" --remote-cmd="${console_cmd:?}" "$@"
133 # run_subcommand should be called only after permission checks and/or locking
138 run_console "${default_cmd:-${console_cmd:?}}";;
140 run_console "${console_cmd:?}";;
142 eval "${reset_cmd:?}";;
143 "rsync --server "*" . .")
144 if ! [ $# -eq 5 -o \( $# -eq 6 -a "$4" = '--log-format=X' \) ]; then
145 die "Unexpected rsync invocation: $*"
147 mkdir -p "$HOME/tftproot"
157 die "Unknown command: $*";;
162 if [ "$1" = "-c" ]; then
164 elif [ $# -gt 0 ]; then
165 die "Permission denied"
169 if [ "$1" = "user" ]; then
170 # Get user name encoded in ~/.ssh/authorized_keys
172 [ "$3" = "admin" ] && NB_ADMIN=1
173 set -- $SSH_ORIGINAL_COMMAND
176 IP=${SSH_CONNECTION%% *}
178 HOST=$(getent hosts $IP) || HOST=$IP
183 DATE=$(LANG=C date +'%F_%T')
184 export NOVABOOT_ID="${NB_USER:-?} $DATE ${REMOTE}"
185 export NOVABOOT_PPID=$PPID
190 # Commands allowed at any time
191 "") locked $0 default;;
192 "console") locked $0 console;;
193 "get-config") read_config && echo -n "${target_config}"; exit;;
194 "add-key") shift; add_key "$@"; exit;;
195 "shell") exec_shell; exit;;
198 # Commands allowed only when nobody or the same user is connected
199 # to the console. "The same user" means that we were executed by
200 # the same sshd process that has the lock. This is ensured by
201 # using SSH connection sharing on client side.
202 reset | rsync | on | off)
203 ALLOWED_PPID=$(cat $RUN_DIR/ppid 2>/dev/null || :)
204 if [ "$PPID" -eq "${ALLOWED_PPID:-0}" ]; then run=unlocked; else run=locked; fi
207 echo >&2 "novaboot-shell: Command not allowed: $*"
208 logger -p error "novaboot-shell: Command not allowed: $*"
213 if [ -d "$HOME" ]; then
216 RUN_DIR="/tmp/novaboot-shell@$USER"
220 if [ -z "$NOVABOOT_ID" ] && [ "$PPID" -ne 1 ]; then