#!/usr/bin/env bash # Add a user to the Hartman Lab server # Copyright 2021-2025 Bryan C. Roessler # Licensed under the Apache License, Version 2.0 p="${BASH_SOURCE[0]%/*}"; [[ -r $p/script-functions ]] && . "$p"/script-functions || exit 1 is_root echo "Usage: $0 [username] [password]" case $# in 0) prompt user; prompt password ;; 1) user="$1"; prompt password ;; 2) user="$1"; password="$2" ;; *) echo "Too many arguments provided"; exit 1 ;; esac useradd_cmd=(useradd -m -U) group_str="" if id -u "$user" &>/dev/null; then ask_ok "User $user exists. Run script-user-remove first?" || exit $? "$p/script-user-remove" "$user" || exit $? fi ask_ok "Create user $user with password $password?" || exit $? restore=0 if [[ -d /mnt/array/home-retired/$user ]]; then ask_ok "Restore user $user's files from /mnt/array/home-retired/$user?" && restore=1 fi samba=0 ask_ok "Enable shared file access for user $user?" && group_str="smbgrp" && samba=1 ask_ok "Make $user an admin?" && group_str+=",wheel" [[ -n $group_str ]] && useradd_cmd+=("-G" "$group_str") useradd_cmd+=("$user") "${useradd_cmd[@]}" echo "$user:$password" | chpasswd if (( restore )); then if rsync -av --progress=info2 "/mnt/array/home-retired/$user/" "/home/$user/"; then ask_ok "User $user's files successfully restored, remove backup at /mnt/array/home-retired/$user?" && \ rm -rf "/mnt/array/home-retired/$user" fi fi if (( samba )); then (echo "$password"; echo "$password") | smbpasswd -a -s "$user" fi ask_ok "Prompt user to reset password on next login?" && passwd --expire "$user" && echo "NOTE: The file sharing (smbpasswd) will not be changed" # Copy manual to user desktop desktop="/home/$user/Desktop" mkdir -p "$desktop" "$p/script-deploy-manual" "$user" exit 0