diff options
Diffstat (limited to 'lib/40-user')
-rw-r--r-- | lib/40-user | 111 |
1 files changed, 71 insertions, 40 deletions
diff --git a/lib/40-user b/lib/40-user index 305fab6..bb3fc05 100644 --- a/lib/40-user +++ b/lib/40-user @@ -29,61 +29,92 @@ 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 ;; + _bcau_homedir_mode=700 + _bcau_create_homedir= + _bcau_homedir= + _bcau_comment= + _bcau_shell=/sbin/nologin + _bcau_pgroup= + _bcau_grouplist= + _bcau_uid= + _bcau_password= + + while getopts c:d:G:g:mM:p:s:u: _bcau_opt; do + case $_bcau_opt in + c) _bcau_comment=$OPTARG ;; + d) _bcau_homedir=$OPTARG ;; + G) _bcau_grouplist=$OPTARG ;; + g) _bcau_pgroup=$OPTARG ;; + M) _bcau_homedir_mode=$OPTARG ;; + m) _bcau_create_homedir=true ;; + p) _bcau_password=$OPTARG ;; + s) _bcau_shell=$OPTARG ;; + u) _bcau_uid=$OPTARG ;; esac done shift $((OPTIND - 1)) - _bcalu_username=$1 - : ${_bcalu_homedir:="/home/${_bcalu_username}"} - : ${_bcalu_comment:="${_bcalu_username} user"} + _bcau_username=$1 + : ${_bcau_homedir:="/home/${_bcau_username}"} + : ${_bcau_comment:="${_bcau_username} user"} case $BOXCONF_OS in freebsd) - if pw usershow "$_bcalu_username" > /dev/null 2>&1; then - log "local user ${_bcalu_username} already exists" + if pw usershow "$_bcau_username" > /dev/null 2>&1; then + log "local user ${_bcau_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}" + -n "$_bcau_username" \ + -c "$_bcau_comment" \ + -s "$_bcau_shell" \ + -M "$_bcau_homedir_mode" \ + -d "$_bcau_homedir" \ + ${_bcau_create_homedir:+-m} \ + ${_bcau_grouplist:+-G ${_bcau_grouplist}} \ + ${_bcau_pgroup:+-g ${_bcau_pgroup}} \ + ${_bcau_uid:+-u ${_bcau_uid}} + + log "added local user ${_bcau_username}" ;; *) - die "add_local_user unimplemented for ${BOXCONF_OS}" + die "add_user unimplemented for ${BOXCONF_OS}" ;; esac - if [ -n "${_bcalu_password}" ]; then - set_password "$_bcalu_user" "$_bcalu_password" + if [ -n "${_bcau_password}" ]; then + set_password "$_bcau_user" "$_bcau_password" fi } + +add_group(){ + # Add a local group if it doesn't exist. + # options: mostly same as `pw groupadd` + # $1 = groupname + _bcag_gid= + + while getopts g: _bcag_opt; do + case $_bcag_opt in + g) _bcag_gid=$OPTARG ;; + esac + done + shift $((OPTIND - 1)) + + _bcag_groupname=$1 + + case $BOXCONF_OS in + freebsd) + if pw groupshow "$_bcag_groupname" > /dev/null 2>&1; then + log "local group ${_bcag_groupname} already exists" + return 0 + fi + + pw groupadd -n "$_bcag_groupname" ${_bcag_gid:+-g ${_bcag_gid}} + log "added local group ${_bcag_groupname}" + ;; + *) + die "add_group unimplemented for ${BOXCONF_OS}" + ;; + esac +} |