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

Change molly-guard to have a number to guarantee ordering, and rename to more specifi...
[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 run a number of checks
41         to guard against accidental shutdowns/reboots.
42
43         Some of the checks available are:
44          - Prompt the user for the machine's if the current shell is a child
45            of an SSH connection (or --pretend-ssh) has been given on the
46            command line, if the shell is connected to an interactive
47            terminal, and the actual command to execute is does not involve
48            --help or is \`shutdown -c'.
49
50           $ME will always interpose the prompt if the environment variable
51           ALWAYS_MOLLY is set to '1'.  This variable may be set in the file
52           /etc/default/${ME} .
53
54         Only if the user satisfies all the checks will $ME take action.
55         Specifying --molly-guard-do-nothing as argument to the command will
56         make $ME echo the command it would execute rather than actually executing
57         it.
58
59         The actual command's help output follows:
60
61         _eousage
62 }
63
64 ARGS=
65 DO_NOTHING=0
66 CHECK_ARGS=
67 for arg in "$@"; do
68   case "$arg" in
69     (*-molly-guard-do-nothing) DO_NOTHING=1;;
70     (*-help)
71       usage 2>&1
72       eval $EXEC --help 2>&1
73       exit 0
74       ;;
75     (*-pretend-ssh) CHECK_ARGS="${CHECK_ARGS:+$CHECK_ARGS }--arg --pretend-ssh";;
76     *) ARGS="${ARGS:+$ARGS }$arg";;
77   esac
78 done
79
80 do_real_cmd()
81 {
82   if [ $DO_NOTHING -eq 1 ]; then
83     echo "$ME: would run: $EXEC $ARGS"
84     exit 0
85   else
86     eval exec $EXEC "$ARGS"
87   fi
88 }
89
90 if [ $DO_NOTHING -eq 1 ]; then
91   echo "I: demo mode; $ME will not do anything due to --molly-guard-do-nothing."
92 fi
93
94 # pass through certain commands
95 case "$CMD $ARGS" in
96   (*shutdown\ *-c*) 
97     echo "I: executing $CMD $ARGS regardless of check results."
98     do_real_cmd
99     ;;
100 esac
101
102 run-parts --exit-on-error $CHECK_ARGS /usr/share/molly-guard/checks.d
103
104 # run-parts won't return to us if there are failures, but I'm paranoid.
105 if [ $? == 0 ]; then
106   do_real_cmd
107   exit
108 fi