Initial commit
This commit is contained in:
51
script-user-add
Normal file
51
script-user-add
Normal file
@@ -0,0 +1,51 @@
|
||||
#!/usr/bin/env bash
|
||||
# Add a user to the Hartman Lab server
|
||||
# Copyright 2021 Bryan C. Roessler
|
||||
|
||||
[[ -f functions ]] && . functions || exit 1
|
||||
|
||||
is_root
|
||||
|
||||
[[ $# -lt 1 ]] && echo "No username provided!" && exit 1
|
||||
[[ $# -eq 1 ]] && username="$1"; password="$username"
|
||||
[[ $# -ge 2 ]] && username="$1"; password="$2"
|
||||
|
||||
useradd_cmd=("useradd" "--create-home")
|
||||
|
||||
restore="no"
|
||||
if [[ -d /mnt/array/home-retired/"$user" ]]; then
|
||||
if ask_ok "Restore user $user's files from backup /mnt/array/home-retired/$user?"; then
|
||||
restore="yes"
|
||||
fi
|
||||
fi
|
||||
|
||||
! ask_ok "Create user $username with password $password?" || exit $?
|
||||
|
||||
ask_ok "Make user $username an admin?" && \
|
||||
useradd_cmd+=("-G" "wheel")
|
||||
|
||||
ask_ok "Enable shared file access for user $username?" && \
|
||||
useradd_cmd+=("-G" "smbgrp") && \
|
||||
(echo "$password"; echo "$password") | smbpasswd -a -s "$username"
|
||||
|
||||
"${useradd_cmd[@]}"
|
||||
|
||||
if [[ "$restore" == "yes" ]]; then
|
||||
if rsync -av --progress=info2 /mnt/array/home-retired/"$user" /home/"$user"; then
|
||||
rm -rf /mnt/array/home-retired/"$user"
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "$username":"$password" | chpasswd
|
||||
|
||||
# Add subuids & subgids for container namespace
|
||||
id_offset=100000
|
||||
id_num=65536
|
||||
last_uid=$(tail -1 /etc/subuid | cut -d':' -f2)
|
||||
last_gid=$(tail -1 /etc/subgid | cut -d':' -f2)
|
||||
start_uid=$(( last_uid + id_offset ))
|
||||
start_gid=$(( last_gid + id_offset ))
|
||||
echo "$username:$start_uid:$id_num" >> /etc/subuid
|
||||
echo "$username:$start_gid:$id_num" >> /etc/subgid
|
||||
|
||||
exit $?
|
||||
Reference in New Issue
Block a user