#!/bin/bash # This tool generates the users and/or appends the given SSH kjey to the authorized file # $1 = username # $2 = tempfile LOGFILE="/etc/ttyserver/mkuser.log" if [ "$EUID" -ne 0 ];then #echo "Please run as root" echo "Not run as root" >> $LOGFILE exit 60 fi #echo $1 >> $LOGFILE # If not exists, create new user with if [[ "$1" =~ ^[a-zA-Z0-9][a-zA-Z0-9-]+$ ]]; then echo "Valid username: \"$1\"" >> $LOGFILE # Is valid username CHECKUSER=`getent passwd "$1"` if [[ ${#CHECKUSER} -gt 6 ]]; then # User Exists echo "\"$1\" Exists" >> $LOGFILE else useradd -G webadd -m -b /htusers -s /usr/bin/bash "$1" echo "Added \"$1\" to the system" >> $LOGFILE fi # Ensure SSH filder exists USERDIR="$(eval echo "~$1")" if [ ! -d "$USERDIR/.ssh" ]; then echo "Generate ssh directory for \"$1\"" >> $LOGFILE mkdir -p "$USERDIR/.ssh" chown -R "$1" "USERDIR/.ssh" else echo "User .ssh exists" >> $LOGFILE fi # Append new key echo "$2 -> $USERDIR/.ssh/authorized_keys" >> $LOGFILE cat "$2" >> "$USERDIR/.ssh/authorized_keys" chmod 700 "$USERDIR/.ssh/authorized_keys" chown "$1" "$USERDIR/.ssh/authorized_keys" # Remove temp file rm -f "$2" echo "Done appending key to \"$1\"" >> $LOGFILE exit 0 else echo "\"$1\" Was an invalid username" >> $LOGFILE exit 64 fi