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

10eac5505d05eb0fcefa1d86ff2c6884e4de16fa
[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 set -eu
9
10 ME=molly-guard
11
12 CMD="${0##*/}"
13 EXEC="/sbin/$CMD"
14
15 case "$CMD" in
16   halt|reboot|shutdown|poweroff)
17     if [ ! -f $EXEC ]; then
18       echo "E: $ME: not a regular file: $EXEC" >&2
19       exit 4
20     fi
21     if [ ! -x $EXEC ]; then
22       echo "E: $ME: not an executable: $EXEC" >&2
23       exit 3
24     fi
25     ;;
26   *)
27     echo "E: $ME: unsupported command: $CMD" >&2
28     exit 1
29     ;;
30 esac
31
32 usage()
33 {
34   cat <<-_eousage
35         Usage: $ME [options]
36                (shielding $EXEC)
37
38         Instead of invoking $EXEC directly, $ME will run a number of checks
39         to guard against accidental shutdowns/reboots.
40
41         Some of the checks available are:
42          - Prompt the user for the machine's if the current shell is a child
43            of an SSH connection (or --pretend-ssh) has been given on the
44            command line, if the shell is connected to an interactive
45            terminal, and the actual command to execute is does not involve
46            --help or is \`shutdown -c'.
47
48           $ME will always interpose the prompt if the environment variable
49           ALWAYS_MOLLY is set to '1'.  This variable may be set in the file
50           /etc/default/${ME} .
51
52         - Print out a warning message with information about this host
53           before the action takes place.  You can use:
54                 /etc/molly-guard.<Action>.message
55           To print out a message specific to the command you're trying to
56           use.  Or:
57                 /etc/molly-guard.message
58           For a general warning message.
59
60         Only if the user satisfies all the checks will $ME take action.
61         Specifying --molly-guard-do-nothing as argument to the command will
62         make $ME echo the command it would execute rather than actually executing
63         it.
64
65         The actual command's help output follows:
66
67         _eousage
68 }
69
70 ARGS=
71 DO_NOTHING=0
72 CHECK_ARGS=
73 for arg in "$@"; do
74   case "$arg" in
75     (*-molly-guard-do-nothing) DO_NOTHING=1;;
76     (*-help)
77       usage 2>&1
78       eval $EXEC --help 2>&1
79       exit 0
80       ;;
81     (*-pretend-ssh) CHECK_ARGS="${CHECK_ARGS:+$CHECK_ARGS }--arg --pretend-ssh";;
82     *) ARGS="${ARGS:+$ARGS }$arg";;
83   esac
84 done
85
86 do_real_cmd()
87 {
88   if [ $DO_NOTHING -eq 1 ]; then
89     echo "$ME: would run: $EXEC $ARGS"
90     exit 0
91   else
92     eval exec $EXEC "$ARGS"
93   fi
94 }
95
96 if [ $DO_NOTHING -eq 1 ]; then
97   echo "I: demo mode; $ME will not do anything due to --molly-guard-do-nothing."
98 fi
99
100 # pass through certain commands
101 case "$CMD $ARGS" in
102   (*shutdown\ *-c*) 
103     echo "I: executing $CMD $ARGS regardless of check results."
104     do_real_cmd
105     ;;
106 esac
107
108 run-parts --exit-on-error --arg $CMD $CHECK_ARGS /usr/share/molly-guard/checks.d
109
110 # run-parts won't return to us if there are failures, but I'm paranoid.
111 if [ $? == 0 ]; then
112   do_real_cmd
113   exit
114 fi