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

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