57 lines
1.7 KiB
Bash
Executable File
57 lines
1.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Add a user to the Hartman Lab server
|
|
# Copyright 2021 Bryan C. Roessler
|
|
|
|
[[ -f functions ]] && . functions || exit 1
|
|
|
|
is_root
|
|
|
|
echo "$@"
|
|
|
|
[[ $# -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")
|
|
|
|
ask_ok "Create user $username with password $password?" || exit $?
|
|
|
|
restore="no"
|
|
if [[ -d /mnt/array/home-retired/"$username" ]]; then
|
|
ask_ok "Restore user $username's files from /mnt/array/home-retired/$user?" && restore="yes"
|
|
fi
|
|
|
|
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"
|
|
|
|
echo "Command: ${useradd_cmd[*]}"
|
|
"${useradd_cmd[@]}"
|
|
|
|
if [[ "$restore" == "yes" ]]; then
|
|
if rsync -av --progress=info2 /mnt/array/home-retired/"$username" /home/"$username"; then
|
|
ask_ok "User $username's files successfully restored, remove backup at /mnt/array/home-retired/$username?" && \
|
|
rm -rf /mnt/array/home-retired/"$username"
|
|
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
|
|
|
|
# Copy manual to user desktop
|
|
ln -s README.html /home/"$username"/Desktop/
|
|
|
|
exit $?
|