]> git.madduck.net Git - code/molly-guard.git/blob - run.d/30-ask-hostname

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:

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