print_help() {
cat <<EOF
Target commands:
-- console
+- console (default command)
- reset
- on
- off
- rsync ...
+- get-config
Management commands:
- help
if [ "$NB_ADMIN" ]; then
cat <<EOF
- add-key
+- shell (use with ssh -t)
EOF
fi
exit 0
[ "$NB_ADMIN" ] || return 1
case $# in
- 0) die "Username argument missing";;
+ 0) die "Usage: ssh ... add-key USERNAME < id_rsa.pub";;
1) break;;
*) die "User name must not contain spaces: $*";;
esac
mv $tmp ~/.ssh/authorized_keys
}
+exec_shell() {
+ [ "$NB_ADMIN" ] || die "Permission denied"
+ if ! tty > /dev/null; then
+ echo "novaboot-shell: Consider starting the shell with 'ssh -t'"
+ fi
+ exec /usr/bin/env bash || exec /bin/sh
+}
+
lock_queue() {
lslocks | awk '{ if ($9 == "'"$RUN_DIR"'") { print $2 } }'
}
exec "$@"
}
-check_var() {
- if eval [ "\"\$$1\"" ]; then
- return 0
+read_config() {
+ . "${NOVABOOT_SHELL_CONFIG:-$HOME/.novaboot-shell}"
+}
+
+power() {
+ local cmd
+ case "$1" in
+ "on") cmd="${on_cmd:?}";;
+ "off") cmd="${off_cmd:?}";;
+ *) die "Unexpected power parameter";;
+ esac
+
+ if [ "$PPID" -ne 1 ] && systemctl --user is-enabled --quiet novaboot-delayed-power-off.service; then
+ case "$1" in
+ "on") systemctl --user start novaboot-delayed-power-off.service;;
+ "off") sudo novaboot-power off;;
+ esac
else
- die "$1 variable not defined in $CFG"
+ eval "$cmd"
fi
}
+run_console() {
+ trap "rm -f $RUN_DIR/ppid" EXIT
+ echo $NOVABOOT_PPID > $RUN_DIR/ppid
+ echo 'novaboot-shell: Connected'
+ # TODO: $reset_begin_cmd
+ [ -n "${on_cmd}" ] && power on
+ eval "$1"
+}
+
+# Run novaboot with the same configuration as specified in
+# ~/.novaboot-shell, but allow the caller to extend of override them
+# via parameters of this function.
+run_novaboot() {
+ nbscript=$1
+ shift
+ OLD_IFS=$IFS
+ # Split $target_config below by newlines, not by words
+ IFS="
+"
+ novaboot "$nbscript" $target_config --server="$HOME/tftproot" --reset-cmd="${reset_cmd:?}" --remote-cmd="${console_cmd:?}" "$@"
+ IFS=$OLD_IFS
+}
+
# run_subcommand should be called only after permission checks and/or locking
run_subcommand() {
+ read_config
case "$*" in
+ "default")
+ run_console "${default_cmd:-${console_cmd:?}}";;
"console")
- trap "rm -f $RUN_DIR/ppid" EXIT
- echo $NOVABOOT_PPID > $RUN_DIR/ppid
- echo 'novaboot-shell: Connected'
- # TODO: $reset_begin_cmd
- check_var console_cmd && eval exec $console_cmd;;
+ run_console "${console_cmd:?}";;
"reset")
- check_var reset_cmd && eval exec $reset_cmd;;
+ eval "${reset_cmd:?}";;
"rsync --server "*" . .")
if ! [ $# -eq 5 -o \( $# -eq 6 -a "$4" = '--log-format=X' \) ]; then
die "Unexpected rsync invocation: $*"
cd "$HOME/tftproot"
exec "$@";;
"on")
- check_var on_cmd && eval exec $on_cmd;;
+ power on
+ exit;;
"off")
- check_var off_cmd && eval exec $off_cmd;;
+ power off
+ exit;;
+ *)
+ die "Unknown command: $*";;
esac
}
main() {
- if [ "$1" = "-c" ]; then shift
- else die "Permission denied"; fi
+ if [ "$1" = "-c" ]; then
+ set -- $2
+ elif [ $# -gt 0 ]; then
+ die "Permission denied"
+ fi
NB_ADMIN=
- if [ "${1%% *}" = "user" ]; then
+ if [ "$1" = "user" ]; then
# Get user name encoded in ~/.ssh/authorized_keys
- set -- $1
- NB_USER="$2";
+ export NB_USER="$2";
[ "$3" = "admin" ] && NB_ADMIN=1
set -- $SSH_ORIGINAL_COMMAND
fi
- if [ $# -eq 0 ]; then print_help; fi
-
IP=${SSH_CONNECTION%% *}
- HOST=$(getent hosts $IP) || HOST=$IP
+ if [ "$IP" ]; then
+ HOST=$(getent hosts $IP) || HOST=$IP
+ else
+ HOST=localhost
+ fi
REMOTE=${HOST##* }
DATE=$(LANG=C date +'%F_%T')
export NOVABOOT_ID="${NB_USER:-?} $DATE ${REMOTE}"
case "$1" in
# Commands allowed at any time
+ "") locked $0 default;;
"console") locked $0 console;;
- "get-config") target_config; exit;;
+ "get-config") read_config && echo -n "${target_config}"; exit;;
"add-key") shift; add_key "$@"; exit;;
+ "shell") exec_shell; exit;;
"help") print_help;;
# Commands allowed only when nobody or the same user is connected
# to the console. "The same user" means that we were executed by
# the same sshd process that has the lock. This is ensured by
- # using SSH connection sharing on cline side.
+ # using SSH connection sharing on client side.
reset | rsync | on | off)
ALLOWED_PPID=$(cat $RUN_DIR/ppid 2>/dev/null || :)
if [ "$PPID" -eq "${ALLOWED_PPID:-0}" ]; then run=unlocked; else run=locked; fi
esac
}
-RUN_DIR="$HOME"
-
-if [ "$NOVABOOT_SHELL_CONFIG" ]; then
- CFG="$NOVABOOT_SHELL_CONFIG"
+if [ -d "$HOME" ]; then
+ RUN_DIR="$HOME"
else
- CFG="$HOME/.novaboot-shell"
+ RUN_DIR="/tmp/novaboot-shell@$USER"
+ mkdir -p "$RUN_DIR"
fi
-. "$CFG"
-
-if [ -z "$NOVABOOT_ID" ]; then
+if [ -z "$NOVABOOT_ID" ] && [ "$PPID" -ne 1 ]; then
main "$@"
else
run_subcommand "$@"