diff options
author | Cullum Smith <cullum@sacredheartsc.com> | 2024-08-05 14:59:15 -0400 |
---|---|---|
committer | Cullum Smith <cullum@sacredheartsc.com> | 2024-08-05 14:59:15 -0400 |
commit | d9c18b3fcb9b036b6cdf69397828b59ab4c53091 (patch) | |
tree | 783725ce085d962440c30c25f0fad9e79fee0f19 /lib/40-user | |
parent | d4be5bbc3682bb9d7f74db5592988ec0838b6138 (diff) | |
download | infrastructure-d9c18b3fcb9b036b6cdf69397828b59ab4c53091.tar.gz |
cleanup add_user function
Diffstat (limited to 'lib/40-user')
-rw-r--r-- | lib/40-user | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/lib/40-user b/lib/40-user index 42bbb82..305fab6 100644 --- a/lib/40-user +++ b/lib/40-user @@ -24,3 +24,66 @@ set_password(){ # $2 = password printf '%s\n%s\n' "$2" "$2" | passwd "$1" > /dev/null } + +add_user(){ + # Add a local user if it doesn't exist. + # options: mostly same as `pw useradd` + # $1 = username + _bcalu_homedir_mode=700 + _bcalu_create_homedir= + _bcalu_homedir= + _bcalu_comment= + _bcalu_shell=/sbin/nologin + _bcalu_pgroup= + _bcalu_grouplist= + _bcalu_uid= + _bcalu_password= + + while getopts c:d:G:g:mM:p:s:u: _bcalu_opt; do + case $_bcalu_opt in + c) _bcalu_comment=$OPTARG ;; + d) _bcalu_homedir=$OPTARG ;; + G) _bcalu_grouplist=$OPTARG ;; + g) _bcalu_pgroup=$OPTARG ;; + M) _bcalu_homedir_mode=$OPTARG ;; + m) _bcalu_create_homedir=true ;; + p) _bcalu_password=$OPTARG ;; + s) _bcalu_shell=$OPTARG ;; + u) _bcalu_uid=$OPTARG ;; + esac + done + shift $((OPTIND - 1)) + + _bcalu_username=$1 + : ${_bcalu_homedir:="/home/${_bcalu_username}"} + : ${_bcalu_comment:="${_bcalu_username} user"} + + case $BOXCONF_OS in + freebsd) + if pw usershow "$_bcalu_username" > /dev/null 2>&1; then + log "local user ${_bcalu_username} already exists" + return 0 + fi + + pw useradd \ + -n "$_bcalu_username" \ + -c "$_bcalu_comment" \ + -s "$_bcalu_shell" \ + -M "$_bcalu_homedir_mode" \ + -d "$_bcalu_homedir" \ + ${_bcalu_create_homedir:+-m} \ + ${_bcalu_grouplist:+-G ${_bcalu_grouplist}} \ + ${_bcalu_pgroup:+-g ${_bcalu_pgroup}} \ + ${_bcalu_uid:+-u ${_bcalu_uid}} + + log "added local user ${_bcalu_username}" + ;; + *) + die "add_local_user unimplemented for ${BOXCONF_OS}" + ;; + esac + + if [ -n "${_bcalu_password}" ]; then + set_password "$_bcalu_user" "$_bcalu_password" + fi +} |