]> git.madduck.net Git - code/molly-guard.git/blobdiff - 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:

rework the scripts given andrew's patch
[code/molly-guard.git] / run.d / 30-ask-hostname
similarity index 61%
rename from checks.d/30-ask-hostname
rename to run.d/30-ask-hostname
index 968dd954f4fb694c2e37b3fcbf9f76a7127dfcd3..6e33fe1fda833e91f493e7bdc5d207deafb7c495 100755 (executable)
@@ -1,6 +1,6 @@
 #!/bin/sh
 #
-# molly-guard -- wrapper script to prevent erroneous shutdowns via SSH
+# 30-ask-hostname - request the user to type in the hostname of the local host
 #
 # Copyright © martin f. krafft <madduck@madduck.net>
 # Released under the terms of the Artistic Licence 2.0
@@ -9,28 +9,24 @@ set -eu
 
 ME=molly-guard
 
-ALWAYS_MOLLY=${ALWAYS_MOLLY:-"0"}
-[ -f /etc/default/${ME} ] && . /etc/default/${ME}
+[ -f "$MOLLYGUARD_SETTINGS" ] && . "$MOLLYGUARD_SETTINGS"
 
-CMD=$1; shift
 PRETEND_SSH=0
 for arg in "$@"; do
   case "$arg" in
-    (*-help)
-      usage 2>&1
-      eval $EXEC --help 2>&1
-      exit 1
-      ;;
     (*-pretend-ssh) PRETEND_SSH=1;;
   esac
 done
 
 # require an interactive terminal connected to stdin
-test -t 0                    || exit 0
+if ! test -t 0; then
+  echo "E: $ME: not connected to a terminal."
+  exit 1
+fi
 
 # we've been asked to always protect this host
-if [ ${ALWAYS_MOLLY} -eq 1 ]; then
-  echo "W: $ME: ${CMD} is always molly-guarded on this server."
+if [ ${ALWAYS_QUERY_HOSTNAME:-0} -eq 1 ]; then
+  echo "I: $ME: $MOLLYGUARD_CMD is always molly-guarded on this system."
 else
   # only run if we are being called over SSH, that is if the current terminal
   # was created by sshd.
@@ -38,7 +34,7 @@ else
   if ! pgrep -f "^sshd.+${PTS#/dev/}[[:space:]]*$" >/dev/null \
     && [ -z "${SSH_CONNECTION:-}" ]; then
       if [ $PRETEND_SSH -eq 1 ]; then
-        echo "I: this is not an SSH session, but --pretend-ssh was given..."
+        echo "I: $ME: this is not an SSH session, but --pretend-ssh was given..."
       else
         exit 0
       fi
@@ -47,18 +43,17 @@ else
   fi
 fi
 
-
 HOSTNAME="$(hostname --short)"
 
 sigh()
 {
-  echo "Good thing I asked; I won't $CMD $HOSTNAME ..."
+  echo "Good thing I asked; I won't $MOLLYGUARD_CMD $HOSTNAME ..."
   exit 1
 }
 
 trap 'echo;sigh' 1 2 3 9 10 12 15
 
-echo -n "Please type in hostname of the machine to $CMD: "
+echo -n "Please type in hostname of the machine to $MOLLYGUARD_CMD: "
 read HOSTNAME_USER || :
 
 [ "$HOSTNAME_USER" = "$HOSTNAME" ] || sigh