#!/usr/bin/env bash # Add a user to the Hartman Lab server # Copyright Bryan C. Roessler parent="${BASH_SOURCE[0]}" parent=${parent%/*} [[ -f "$parent"/script-functions ]] && . "$parent"/script-functions || exit 1 is_root echo "This script supports two optional arguments, a username and password" if [[ $# -eq 0 ]]; then prompt username prompt password elif [[ $# -eq 1 ]]; then username="$1" prompt password elif [[ $# -eq 2 ]]; then username="$1" password="$2" elif [[ $# -gt 2 ]]; then echo "Too many arguments provided" exit 1 fi useradd_cmd=(useradd -m -U) if id -u "$username" &>/dev/null; then ask_ok "User $username exists. Run script-user-remove first?" || exit $? "$parent"/script-user-remove "$username" || exit $? fi 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 samba="no" ask_ok "Enable shared file access for user $username?" && group_str="smbgrp," && samba="yes" ask_ok "Make $username an admin?" && \ group_str+="wheel" useradd_cmd+=("-G" "$group_str") useradd_cmd+=("$username") 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 "Running: ${useradd_cmd[*]}" "${useradd_cmd[@]}" echo "$username":"$password" | chpasswd if [[ "$samba" == "yes" ]]; then (echo "$password"; echo "$password") | smbpasswd -a -s "$username" fi ask_ok "Prompt user to reset password on next login?" && sudo passwd --expire "$username" # TODO check if centos 9 does by default # 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 mkdir -p /home/"$username"/Desktop/ ln -fs "$parent"/README.html /home/"$username"/Desktop/ chown "$username":"$username" -R /home/"$username"/Desktop exit $?