From ff86881db69e174697c60b8a904e9041c778ff81 Mon Sep 17 00:00:00 2001 From: Sakurajima Mai Date: Wed, 21 Sep 2022 17:44:01 -0700 Subject: [PATCH] Rewrite pythonic sorry --- staff/acct/sorry/sorry | 361 +++++++++++++++++++++++------------------ 1 file changed, 206 insertions(+), 155 deletions(-) diff --git a/staff/acct/sorry/sorry b/staff/acct/sorry/sorry index 0f3eed5..6842756 100755 --- a/staff/acct/sorry/sorry +++ b/staff/acct/sorry/sorry @@ -1,155 +1,206 @@ -#!/bin/bash -# Script for sorrying OCF user accounts - -REASONPATH=/opt/share/utils/staff/acct/sorry -LDAPSEARCH=$(command -v ldapsearch) -LDAPMODIFY=$(command -v ldapmodify) -KINIT=$(command -v kinit) -KDESTROY=$(command -v kdestroy) -#KRB5CCNAME=/root/krb5cc_sorry -#export KRB5CCNAME - -# check to see if running as root -if [ "$(/usr/bin/id -u)" != 0 ]; then - echo "You must be root to run this." - exit 2 -fi - -if [ -z "$1" ] || [ -z "$2" ]; then - echo "Usage: $0 [user to be sorried] [sorry reason file]" - echo - echo "Standard Sorry Reasons:" - find "$REASONPATH" -maxdepth 1 -mindepth 1 -type f -printf " %f\\n" - echo - echo "For custom sorry reasons you can pass in your own file" - exit 0 -fi - -sorriedUser=$1 - -if [[ ! "$sorriedUser" =~ ^[a-z0-9]+ ]]; then - echo "$sorriedUser is an invalid username" - exit 3 -fi - -if [ -z "$(getent passwd "$sorriedUser")" ]; then - echo "User $sorriedUser does not exist" - exit 3 -fi - -sorryFile=$2 - -if [ ! -f "$sorryFile" ]; then - sorryFile="$REASONPATH/$sorryFile" - if [ ! -f "$sorryFile" ]; then - echo "Invalid sorry file" - exit 3 - fi -fi - -userdir=$(ldapsearch -x uid="$sorriedUser" | grep homeDirectory | cut -d' ' -f2) - -# Rewriting this to use bash variable substitution is annoying and confusing, so -# just use sed instead and ignore the shellcheck warning -# shellcheck disable=SC2001 -httpdir=$(echo "$sorriedUser" | sed -E 's%([a-z])[a-z]*%/services/http/users/\1/\0%') - -rootstaffer="$SUDO_USER" - -if [ "$rootstaffer" = "root" ] || [ -z "$rootstaffer" ]; then - echo "The sorry.log is much more useful when it logs who you are" - echo "rather than simply 'root'. Please enter your username:" - read -r rootstaffer -fi - -if [ -z "$SORRY_KRB5CCNAME" ]; then - echo "You are $rootstaffer" - if ! $KINIT "${rootstaffer}/admin"; then - echo "kinit failed, bailing out!" - exit 1 - fi -else - echo "SORRY_KRB5CCNAME set in environment." - echo "Assuming this file contains current admin credentials." - KRB5CCNAME="$SORRY_KRB5CCNAME" - export KRB5CCNAME -fi - -sorryshell=/opt/share/utils/bin/sorried - -echo "" -echo "Copying sorry file and making .oldshell file" - -oldshell=$($LDAPSEARCH -x "(uid=$sorriedUser)" loginShell | grep "^loginShell:" | cut -d" " -f2) - -if [[ ! -f .oldshell ]]; then - echo "$oldshell" > "$userdir/.oldshell" -fi - -cp "$sorryFile" "$userdir/.sorry" -chmod 400 "$userdir/.sorry" -chown "$sorriedUser:ocf" "$userdir/.sorry" - -echo "" -echo "Changing user's shell to a sorry shell" - -sorrygid=$(getent group sorry | cut -d : -f 3) -$LDAPMODIFY -H ldaps://ldap.ocf.berkeley.edu <> /opt/acct/sorry.log - -# Notify user by email if email address is available. -email_from='Open Computing Facility ' -email_subject='[OCF] Account disabled' -email_to="$(ldapsearch "uid=${sorriedUser}" mail | grep ^mail | cut -d' ' -f2)" -[ -n "$email_to" ] && \ - mail -a "From: ${email_from}" -s "$email_subject" "$email_to" <