]> git.madduck.net Git - code/molly-guard.git/blob - shutdown

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

* Now also protects the poweroff command (closes: #401042).
[code/molly-guard.git] / shutdown
1 #!/bin/sh
2 #
3 # shutdown -- wrapper script to prevent erroneous shutdowns via SSH
4 #
5 # Copyright © martin f. krafft <madduck@madduck.net>
6 # Released under the terms of the Artistic Licence 2.0
7 #
8 # $Id: shutdown 299 2006-10-16 14:40:47Z madduck $
9 #
10 set -eu
11
12 ME=molly-guard
13
14 CMD="${0##*/}"
15 EXEC="/sbin/$CMD"
16
17 case "$CMD" in
18   halt|reboot|shutdown|poweroff)
19     if [ ! -f $EXEC ]; then
20       echo "E: $ME: not a regular file: $EXEC" >&2
21       exit 4
22     fi
23     if [ ! -x $EXEC ]; then
24       echo "E: $ME: not an executable: $EXEC" >&2
25       exit 3
26     fi
27     ;;
28   *)
29     echo "E: $ME: unsupported command: $CMD" >&2
30     exit 1
31     ;;
32 esac
33 ARGS="$@"
34
35 do_real_cmd()
36 {
37   case "$ARGS" in
38     (*--molly-guard-do-nothing*)
39       ARGS0="${ARGS%%--molly-guard-do-nothing*}"
40       ARGS1="${ARGS##*--molly-guard-do-nothing}"
41       echo "$ME: would run: $EXEC ${ARGS0# } ${ARGS1# }"
42       exit 0;;
43     *) eval exec $EXEC "$ARGS";;
44   esac
45 }
46
47 # require $SSH_CONNECTION to be set, indicates an SSH session
48 [ -n "${SSH_CONNECTION:-}" ] || do_real_cmd
49 # require an interactive terminal connected to stdin
50 test -t 0                    || do_real_cmd
51 # pass through help commands
52 case "$CMD $ARGS" in
53   (shutdown\ *-c*) do_real_cmd;;
54   (shutdown\ *-h*) do_real_cmd;;
55   (*--help*) do_real_cmd;;
56   *) :;;
57 esac
58
59 HOSTNAME="$(hostname)"
60
61 sigh()
62 {
63   echo "Good thing I asked; I won't $CMD $HOSTNAME ..."
64   exit 2
65 }
66
67 trap 'echo;sigh' 1 2 3 9 10 12 15
68
69 echo "$ME: SSH session detected!"
70 echo -n "Please type in hostname of the machine to $CMD: "
71 read HOSTNAME_USER || :
72
73 [ "$HOSTNAME_USER" = "$HOSTNAME" ] || sigh
74
75 trap - 1 2 3 9 10 12 15
76
77 do_real_cmd