]> git.madduck.net Git - code/molly-guard.git/blob - checks.d/molly-guard

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:

Use run-parts to run a selection of different guards.
[code/molly-guard.git] / checks.d / molly-guard
1 #!/bin/sh
2 #
3 # molly-guard -- 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 PRETEND_SSH=0
13 CMD=$1
14 shift
15 for arg in "$@"; do
16   case "$arg" in
17     (*-help)
18       usage 2>&1
19       eval $EXEC --help 2>&1
20       exit 1
21       ;;
22     (*-pretend-ssh) PRETEND_SSH=1;;
23   esac
24 done
25
26 # require an interactive terminal connected to stdin
27 test -t 0                    || exit 0
28
29 # only run if we are being called over SSH, that is if the current terminal
30 # was created by sshd.
31 PTS=$(readlink /proc/$$/fd/0)
32 if ! pgrep -f "^sshd.+${PTS#/dev/}[[:space:]]*$" >/dev/null \
33   && [ -z "${SSH_CONNECTION:-}" ]; then
34     if [ $PRETEND_SSH -eq 1 ]; then
35       echo "I: this is not an SSH session, but --pretend-ssh was given..."
36     else
37       exit 0
38     fi
39 else
40   echo "W: $ME: SSH session detected!"
41 fi
42
43
44 HOSTNAME="$(hostname --short)"
45
46 sigh()
47 {
48   echo "Good thing I asked; I won't $CMD $HOSTNAME ..."
49   exit 1
50 }
51
52 trap 'echo;sigh' 1 2 3 9 10 12 15
53
54 echo -n "Please type in hostname of the machine to $CMD: "
55 read HOSTNAME_USER || :
56
57 [ "$HOSTNAME_USER" = "$HOSTNAME" ] || sigh
58
59 trap - 1 2 3 9 10 12 15
60
61 exit 0