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

65fd374b2dfd87e8050394085ae2f07941bb9606
[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     if [ ! -x $EXEC ]; then
23       echo "E: $ME: not an executable: $EXEC" >&2
24       exit 3
25     fi
26     ;;
27   *)
28     echo "E: $ME: unsupported command: $CMD" >&2
29     exit 1
30     ;;
31 esac
32 ARGS="$@"
33
34 do_real_cmd()
35 {
36   exec $EXEC "$ARGS"
37 }
38
39 # require $SSH_CONNECTION to be set, indicates an SSH session
40 [ -n "${SSH_CONNECTION:-}" ] || do_real_cmd
41 # require an interactive terminal connected to stdin
42 test -t 0                    || do_real_cmd
43 # pass through help commands
44 case "$CMD $ARGS" in
45   'shutdown*-c'|'*-h') do_real_cmd;;
46   *) :;;
47 esac
48
49 echo -n "$ME: SSH session detected, type in hostname of the machine to $CMD: "
50 read HOSTNAME_USER
51
52 HOSTNAME="$(hostname)"
53
54 if [ "$HOSTNAME_USER" != "$HOSTNAME" ]; then
55   echo "Good thing I asked; I won't $CMD $HOSTNAME ..."
56   exit 2
57 fi