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

* Pass --short option to hostname to prevent the user from having to type
[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
34 usage()
35 {
36   cat <<-_eousage
37         Usage: $ME [options]
38                (shielding $EXEC)
39
40         Instead of invoking $EXEC directly, $ME will prompt the user for the
41         machine's hostname to guard against accidental shutdowns/reboots, if the
42         current shell is a child of an SSH connection (or --pretend-ssh) has been
43         given on the command line, if the shell is connected to an interactive
44         terminal, and the actual command to execute is does not involve --help or is
45         \`shutdown -c'.
46
47         Only if the user enters the machine's hostname correctly will $ME take
48         action. Specifying --molly-guard-do-nothing as argument to the command will
49         make $ME echo the command it would execute rather than actually executing
50         it.
51
52         The actual command's help output follows:
53
54         _eousage
55 }
56
57 ARGS=
58 DO_NOTHING=0
59 PRETEND_SSH=0
60 for arg in "$@"; do
61   case "$arg" in
62     (*-molly-guard-do-nothing) DO_NOTHING=1;;
63     (*-help)
64       usage 2>&1
65       eval $EXEC --help 2>&1
66       exit 0
67       ;;
68     (*-pretend-ssh) PRETEND_SSH=1;;
69     *) ARGS="${ARGS:+$ARGS }$arg";;
70   esac
71 done
72
73 do_real_cmd()
74 {
75   if [ $DO_NOTHING -eq 1 ]; then
76     echo "$ME: would run: $EXEC $ARGS"
77     exit 0
78   else
79     eval exec $EXEC "$ARGS"
80   fi
81 }
82
83 if [ $DO_NOTHING -eq 1 ]; then
84   echo "I: demo mode; $ME will not do anything due to --molly-guard-do-nothing."
85 fi
86
87 # require an interactive terminal connected to stdin
88 test -t 0                    || do_real_cmd
89
90 # only run if we are being called over SSH, that is if the current terminal
91 # was created by sshd.
92 PTS=$(readlink /proc/$$/fd/0)
93 if ! pgrep -f "^sshd.+${PTS#/dev/}[[:space:]]*$" >/dev/null; then
94   if [ $PRETEND_SSH -eq 1 ]; then
95     echo "I: this is not an SSH session, but --pretend-ssh was given..."
96   else
97     do_real_cmd
98   fi
99 else
100   echo "W: $ME: SSH session detected!"
101 fi
102
103 # pass through certain commands
104 case "$CMD $ARGS" in
105   (*shutdown\ *-c*) 
106     echo "I: executing $CMD $ARGS regardless of SSH session."
107     do_real_cmd
108     ;;
109 esac
110
111 HOSTNAME="$(hostname --short)"
112
113 sigh()
114 {
115   echo "Good thing I asked; I won't $CMD $HOSTNAME ..."
116   exit 2
117 }
118
119 trap 'echo;sigh' 1 2 3 9 10 12 15
120
121 echo -n "Please type in hostname of the machine to $CMD: "
122 read HOSTNAME_USER || :
123
124 [ "$HOSTNAME_USER" = "$HOSTNAME" ] || sigh
125
126 trap - 1 2 3 9 10 12 15
127
128 do_real_cmd