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_subcommand should be called only after permission checks and/or locking
121 run_console "${default_cmd:-${console_cmd:?}}";;
123 run_console "${console_cmd:?}";;
125 eval exec "${reset_cmd:?}";;
126 "rsync --server "*" . .")
127 if ! [ $# -eq 5 -o \( $# -eq 6 -a "$4" = '--log-format=X' \) ]; then
128 die "Unexpected rsync invocation: $*"
130 mkdir -p "$HOME/tftproot"
140 die "Unknown command: $*";;
145 if [ "$1" = "-c" ]; then
147 elif [ $# -gt 0 ]; then
148 die "Permission denied"
152 if [ "$1" = "user" ]; then
153 # Get user name encoded in ~/.ssh/authorized_keys
155 [ "$3" = "admin" ] && NB_ADMIN=1
156 set -- $SSH_ORIGINAL_COMMAND
159 IP=${SSH_CONNECTION%% *}
161 HOST=$(getent hosts $IP) || HOST=$IP
166 DATE=$(LANG=C date +'%F_%T')
167 export NOVABOOT_ID="${NB_USER:-?} $DATE ${REMOTE}"
168 export NOVABOOT_PPID=$PPID
173 # Commands allowed at any time
174 "") locked $0 default;;
175 "console") locked $0 console;;
176 "get-config") read_config && echo -n "${target_config}"; exit;;
177 "add-key") shift; add_key "$@"; exit;;
178 "shell") exec_shell; exit;;
181 # Commands allowed only when nobody or the same user is connected
182 # to the console. "The same user" means that we were executed by
183 # the same sshd process that has the lock. This is ensured by
184 # using SSH connection sharing on client side.
185 reset | rsync | on | off)
186 ALLOWED_PPID=$(cat $RUN_DIR/ppid 2>/dev/null || :)
187 if [ "$PPID" -eq "${ALLOWED_PPID:-0}" ]; then run=unlocked; else run=locked; fi
190 echo >&2 "novaboot-shell: Command not allowed: $*"
191 logger -p error "novaboot-shell: Command not allowed: $*"
196 if [ -d "$HOME" ]; then
199 RUN_DIR="/tmp/novaboot-shell@$USER"
203 if [ -z "$NOVABOOT_ID" ] && [ "$PPID" -ne 1 ]; then