]> 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:

cb567c299405b772656b8249f7140168bab0c86c
[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)
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     *) 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   (*-h*) do_real_cmd;;
55   *) :;;
56 esac
57
58 HOSTNAME="$(hostname)"
59
60 sigh()
61 {
62   echo "Good thing I asked; I won't $CMD $HOSTNAME ..."
63   exit 2
64 }
65
66 trap 'echo;sigh' 1 2 3 9 10 12 15
67
68 echo "$ME: SSH session detected!"
69 echo -n "Please type in hostname of the machine to $CMD: "
70 read HOSTNAME_USER || :
71
72 [ "$HOSTNAME_USER" = "$HOSTNAME" ] || sigh
73
74 trap - 1 2 3 9 10 12 15
75
76 do_real_cmd