3 # Copyright (c) 2020 The DragonFly Project.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in
14 # the documentation and/or other materials provided with the
16 # 3. Neither the name of The DragonFly Project nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific, prior written permission.
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 # Exit if any untested command fails in non-interactive mode
36 # Exit when an undefined variable is referenced
39 # usage: add_users <etcdir> <master.passwd> <group>
41 # Add new users and groups in <etcdir> according to the given <master.passwd>
44 # NOTE: Existing users and groups are not modified.
46 # NOTE: There is circular dependence: 'groupadd' requires the members
47 # already exist, while 'useradd' requires the group exists.
48 # So first assign new users to the 'nogroup' group, and then make
49 # adjustments after group creation.
55 local _name _pw _uid _gid _gids item
56 local _class _change _expire _gecos _home _shell _members
58 echo "===> Adding new users ..."
60 while IFS=':' read -r _name _pw _uid _gid _class \
61 _change _expire _gecos _home _shell; do
65 if pw -V ${etcdir} usershow ${_name} -q >/dev/null; then
68 echo " * ${_name}: ${_uid}, ${_gid}, ${_gecos}, ${_home}, ${_shell}"
69 # Assign to the 'nogroup' group, and then adjust later.
70 pw -V ${etcdir} useradd ${_name} \
77 _gids="${_gids} ${_name}:${_gid}"
80 echo "===> Adding new groups ..."
81 while IFS=':' read -r _name _pw _gid _members; do
85 if pw -V ${etcdir} groupshow ${_name} -q >/dev/null; then
88 echo " * ${_name}: ${_gid}, ${_members}"
89 pw -V ${etcdir} groupadd ${_name} -g ${_gid} -M "${_members}"
92 echo "===> Adjusting the group of new users ..."
93 for item in ${_gids}; do
96 echo " * ${_name}: ${_gid}"
97 pw -V ${etcdir} usermod ${_name} -g ${_gid}
102 cat > /dev/stderr << _EOF_
103 Add new users and groups.
105 Usage: ${0##*/} -d <etc-dir> -g <group-file> -p <master.passwd-file>
116 while getopts :d:g:hp: opt; do
125 PASSWD_FILE=${OPTARG}
133 shift $((OPTIND - 1))
134 [ $# -eq 0 ] || usage
135 [ -n "${ETC_DIR}" ] || usage
136 [ -n "${GROUP_FILE}" ] || usage
137 [ -n "${PASSWD_FILE}" ] || usage
139 add_users "${ETC_DIR}" "${PASSWD_FILE}" "${GROUP_FILE}"
141 echo "Update password databases ..."
142 pwd_mkdb -p -d "${ETC_DIR}" "${ETC_DIR}/master.passwd"