From 039dab0a06124df6c0d05b07e56f4bf03d2c3cf2 Mon Sep 17 00:00:00 2001 From: Tilde Black Admin Date: Thu, 1 Aug 2019 12:04:13 +0000 Subject: [PATCH] black ops scripts --- assign.sh | 30 ++++++++++++++++++++++++++++++ docsgen.sh | 30 ++++++++++++++++++++++++++++++ gopherroot.sh | 13 +++++++++++++ remove.sh | 23 +++++++++++++++++++++++ restore.sh | 17 +++++++++++++++++ webroot.sh | 19 +++++++++++++++++++ 6 files changed, 132 insertions(+) create mode 100755 assign.sh create mode 100755 docsgen.sh create mode 100755 gopherroot.sh create mode 100755 remove.sh create mode 100755 restore.sh create mode 100755 webroot.sh diff --git a/assign.sh b/assign.sh new file mode 100755 index 0000000..6d1489c --- /dev/null +++ b/assign.sh @@ -0,0 +1,30 @@ +#!/bin/sh +email="$1" +key="$2" +keystore="$HOME/keystore.txt" + +if [ -n "$email" ] && [ -n "$key" ]; then + all_users=$(grep /home < /etc/passwd | awk -F: '{print $1}' | gshuf) + # once we find 10 matches quit + matches=1 + match_users=$( + printf "%s" "$all_users" | while read -r n; do + if [ $matches -gt 10 ]; then + exit + fi + if [ ! -s "/home/${n}/.ssh/authorized_keys" ]; then + printf "%s " "$n" + matches=$((matches+1)) + printf "%s\\n" "$key" > "/home/${n}/.ssh/authorized_keys" + fi + done + ) + sed -e "s/USERS/${match_users}/" /etc/templates/assign.tmpl | sendmail "${email}" + { + printf "Email: %s\\n" "$email" + printf "Key: %s\\n" "$key" + printf "Users: %s\\n\\n" "$match_users" + } >> "$keystore" +else + printf "Provide email address and ssh-key as params." +fi diff --git a/docsgen.sh b/docsgen.sh new file mode 100755 index 0000000..1d9a4fb --- /dev/null +++ b/docsgen.sh @@ -0,0 +1,30 @@ +#!/bin/sh +set -e +DOC_DIR="$HOME/docs" +cd "${DOC_DIR}" || exit 1 + +# fetch the latest remote repo changes +git fetch origin master -q 2> /dev/null + +# get shas for our local repo and remote repo +remotesha=$(git rev-parse origin/master) +localsha=$(git rev-parse HEAD) + +# if shas are different, we need to update +if [ "$remotesha" != "$localsha" ] || [ "$1" = "force" ]; then + # clear all our local changes + git reset --hard HEAD -q + + # pull in the latest updates from remote + git pull -q origin master 2> /dev/null + + # build the web version + /usr/local/bin/mkdocs build > /dev/null 2> /dev/null + + # clear the gopher version + rm /var/gopher/docs/*.md + + # copy files to the gopher version + cd "$HOME/docs/docs/" || exit 1 + cp ./*.md /var/gopher/docs/ +fi diff --git a/gopherroot.sh b/gopherroot.sh new file mode 100755 index 0000000..87085c0 --- /dev/null +++ b/gopherroot.sh @@ -0,0 +1,13 @@ +#!/bin/sh + +all_users=$(grep /home < /etc/passwd | awk -F: '{print $1}') + +output="/var/gopher/users.gophermap" +printf "iUsers:\\n" > "$output" +printf "%s" "$all_users" | while read -r n; do + if [ -f "/var/gopher/users/${n}/gophermap" ]; then + printf "1~%s\\t/users/%s\\n" "$n" "$n" >> "$output" + fi +done + +cat /var/gopher/head.gophermap /var/gopher/users.gophermap > /var/gopher/gophermap diff --git a/remove.sh b/remove.sh new file mode 100755 index 0000000..a90ad66 --- /dev/null +++ b/remove.sh @@ -0,0 +1,23 @@ +#!/bin/sh + +key="$1" +keystore="$HOME/keystore.txt" + +if [ -n "$1" ]; then + all_users=$(grep /home < /etc/passwd | awk -F: '{print $1}') + match_users=$( + printf "%s" "$all_users" | while read -r n; do + if [ -s "/home/${n}/.ssh/authorized_keys" ]; then + if grep -q "$key" "/home/${n}/.ssh/authorized_keys"; then + if [ "$n" != "fox" ]; then + printf "%s " "$n" + printf "DISABLED" > "/home/${n}/.ssh/authorized_keys" + usermod -s /sbin/nologin "${n}" + fi + fi + fi + done + ) + printf "DISABLED: %s\\n" "$match_users" + printf "DISABLED: %s\\n" "$match_users" >> "$keystore" +fi diff --git a/restore.sh b/restore.sh new file mode 100755 index 0000000..2dc59eb --- /dev/null +++ b/restore.sh @@ -0,0 +1,17 @@ +#!/bin/sh + +key="$1" +users="$*" +if [ -n "$key" ] && [ -n "$users" ]; then + for n in $users; do + if [ -s "/home/${n}/.ssh/authorized_keys" ]; then + if grep -q "DISABLED" "/home/${n}/.ssh/authorized_keys"; then + if [ "$n" != "fox" ]; then + printf "%s\\n" "$key" > "/home/${n}/.ssh/authorized_keys" + printf "%s restored\\n" "$n" + usermod -s /bin/ksh "${n}" + fi + fi + fi + done +fi diff --git a/webroot.sh b/webroot.sh new file mode 100755 index 0000000..41cfefc --- /dev/null +++ b/webroot.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +all_users=$(grep /home < /etc/passwd | awk -F: '{print $1}' | sort) + +output="/var/www/htdocs/users.partial" +printf "

Users:

\\n" > "$output" +printf "\\n" >> "$output" + +cat /var/www/htdocs/head.partial \ + /var/www/htdocs/intro.partial \ + /var/www/htdocs/users.partial \ + /var/www/htdocs/foot.partial > /var/www/htdocs/index.html +