]> 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 should also work in su/sudo sessions because it now checks whether the
[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 an interactive terminal connected to stdin
48 test -t 0                    || do_real_cmd
49
50 # only run if we are being called over SSH, that is if the current terminal
51 # was created by sshd.
52 PTS=$(readlink /proc/$$/fd/0)
53 pgrep -f "^sshd.+${PTS#/dev/}$" >/dev/null || do_real_cmd
54
55 # pass through help commands
56 case "$CMD $ARGS" in
57   (shutdown\ *-c*) do_real_cmd;;
58   (shutdown\ *-h*) do_real_cmd;;
59   (*--help*) do_real_cmd;;
60   *) :;;
61 esac
62
63 HOSTNAME="$(hostname)"
64
65 sigh()
66 {
67   echo "Good thing I asked; I won't $CMD $HOSTNAME ..."
68   exit 2
69 }
70
71 trap 'echo;sigh' 1 2 3 9 10 12 15
72
73 echo "$ME: SSH session detected!"
74 echo -n "Please type in hostname of the machine to $CMD: "
75 read HOSTNAME_USER || :
76
77 [ "$HOSTNAME_USER" = "$HOSTNAME" ] || sigh
78
79 trap - 1 2 3 9 10 12 15
80
81 do_real_cmd