Commit bb0e7959 authored by Alban Bruder's avatar Alban Bruder

Add adduser script

parent e5c9b5bd
......@@ -87,6 +87,7 @@ ssh-2fa SSH 2FA utilities for GnuPG-compatible smartcards.
connect-agent Connect the SSH agent to the GPG agent socket.
util General helper tools and utilities.
adduser Creates a user on a remote system.
authorized-ssh-keys Collect all SSH keys in use across salt minions' user accounts.
backup-btrfs-vol Take a snapshot of a btrfs subvolume and back up its contents to another location.
list-buw-dns List all assigned Webis host names / IP addresses at BUW.
......
#!/usr/bin/env bash
### \b
### Creates a user on a remote system.
### \b
### Generates a 20 character long password and stores it in the ~/pw.txt file.
### Copies the supplied ssh public key to the ~/.ssh/authorized_keys file.
### \b
### Copyright 2020-today
### Author: Alban Bruder
###
### : -u : --user : str :: Username
### : -n : --name : str :: Full Name
### : -i : --identityfile : fpath! :: Public Key
### : destination ::
if [ -n "$WEBIS_LIB_PATH" ]; then
. "${WEBIS_LIB_PATH}/bashhelper.sh"
check_tools sshpass
fi
[[ -z "$ARG_USER" ]] && read -p "Username: " ARG_USER
[[ ! "$ARG_DESTINATION" == *@* ]] && ARG_DESTINATION="webis@${ARG_DESTINATION}"
[[ -z "$SSHPASS" ]] && read_password "${ARG_DESTINATION}'s password:" SSHPASS
export SSHPASS
RESULTS=$( echo "$SSHPASS" | sshpass -e ssh -tt "${ARG_DESTINATION}" "sudo -SE bash -c '(useradd -s /bin/bash -m -G sudo ${ARG_USER} && (
umask 066;
head /dev/urandom | tr -dc A-Za-z0-9 | head -c 20 | tee /home/${ARG_USER}/pw.txt | sed s/^/${ARG_USER}:/ | chpasswd;
chown ${ARG_USER}:${ARG_USER} /home/${ARG_USER}/pw.txt;
usermod -c \"${ARG_NAME}\" ${ARG_USER}
))'" 2> /dev/null )
if [[ ! $? -eq 0 ]]; then
logError "Creation of user ${ARG_USER} failed."
exit 1
fi
if [[ ! -z "$ARG_IDENTITYFILE" ]]; then
IDENTITY=$(<${ARG_IDENTITYFILE})
echo "$SSHPASS" | sshpass -e ssh -tt "${ARG_DESTINATION}" "sudo -SE bash -c '(umask 077;
mkdir -p /home/${ARG_USER}/.ssh;
echo ${IDENTITY} > /home/${ARG_USER}/.ssh/authorized_keys;
chown -R ${ARG_USER}:${ARG_USER} /home/${ARG_USER}/.ssh)' 2> /dev/null" 2> /dev/null
fi
logInfo "Done."
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment