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
101 sudo novaboot-power "$1"
108 trap "rm -f $RUN_DIR/ppid" EXIT
109 echo $NOVABOOT_PPID > $RUN_DIR/ppid
110 echo 'novaboot-shell: Connected'
111 # TODO: $reset_begin_cmd
112 [ -n "${on_cmd}" ] && power on
116 # Run novaboot with the same configuration as specified in
117 # ~/.novaboot-shell, but allow the caller to extend of override them
118 # via parameters of this function.
123 # Split $target_config below by newlines, not by words
126 novaboot "$nbscript" $target_config --server="$HOME/tftproot" --reset-cmd="${reset_cmd:?}" --remote-cmd="${console_cmd:?}" "$@"
130 # run_subcommand should be called only after permission checks and/or locking
135 run_console "${default_cmd:-${console_cmd:?}}";;
137 run_console "${console_cmd:?}";;
139 eval "${reset_cmd:?}";;
140 "rsync --server "*" . .")
141 if ! [ $# -eq 5 -o \( $# -eq 6 -a "$4" = '--log-format=X' \) ]; then
142 die "Unexpected rsync invocation: $*"
144 mkdir -p "$HOME/tftproot"
154 die "Unknown command: $*";;
159 if [ "$1" = "-c" ]; then
161 elif [ $# -gt 0 ]; then
162 die "Permission denied"
166 if [ "$1" = "user" ]; then
167 # Get user name encoded in ~/.ssh/authorized_keys
169 [ "$3" = "admin" ] && NB_ADMIN=1
170 set -- $SSH_ORIGINAL_COMMAND
173 IP=${SSH_CONNECTION%% *}
175 HOST=$(getent hosts $IP) || HOST=$IP
180 DATE=$(LANG=C date +'%F_%T')
181 export NOVABOOT_ID="${NB_USER:-?} $DATE ${REMOTE}"
182 export NOVABOOT_PPID=$PPID
187 # Commands allowed at any time
188 "") locked $0 default;;
189 "console") locked $0 console;;
190 "get-config") read_config && echo -n "${target_config}"; exit;;
191 "add-key") shift; add_key "$@"; exit;;
192 "shell") exec_shell; exit;;
195 # Commands allowed only when nobody or the same user is connected
196 # to the console. "The same user" means that we were executed by
197 # the same sshd process that has the lock. This is ensured by
198 # using SSH connection sharing on client side.
199 reset | rsync | on | off)
200 ALLOWED_PPID=$(cat $RUN_DIR/ppid 2>/dev/null || :)
201 if [ "$PPID" -eq "${ALLOWED_PPID:-0}" ]; then run=unlocked; else run=locked; fi
204 echo >&2 "novaboot-shell: Command not allowed: $*"
205 logger -p error "novaboot-shell: Command not allowed: $*"
210 if [ -d "$HOME" ]; then
213 RUN_DIR="/tmp/novaboot-shell@$USER"
217 if [ -z "$NOVABOOT_ID" ] && [ "$PPID" -ne 1 ]; then