1 # Copyright 1999-2014 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
7 # base-system@gentoo.org (Linux)
8 # Joe Jezak <josejx@gmail.com> (OS X)
9 # usata@gentoo.org (OS X)
10 # Aaron Walker <ka0ttic@gentoo.org> (FreeBSD)
11 # @BLURB: user management in ebuilds
13 # The user eclass contains a suite of functions that allow ebuilds
14 # to quickly make sure users in the installed system are sane.
16 if [[ -z ${_USER_ECLASS} ]]; then
19 # @FUNCTION: _assert_pkg_ebuild_phase
21 # @USAGE: <calling func name>
22 _assert_pkg_ebuild_phase() {
23 case ${EBUILD_PHASE} in
24 setup|preinst|postinst) ;;
26 eerror "'$1()' called from '${EBUILD_PHASE}' phase which is not OK:"
27 eerror "You may only call from pkg_{setup,preinst,postinst} functions."
28 eerror "Package fails at QA and at life. Please file a bug."
29 die "Bad package! $1 is only for use in some pkg_* functions!"
34 # @USAGE: <database> <key>
36 # Small wrapper for getent (Linux), nidump (< Mac OS X 10.5),
37 # dscl (Mac OS X 10.5), and pw (FreeBSD) used in enewuser()/enewgroup().
39 # Supported databases: group passwd
43 [[ $# -ge 3 ]] && die "usage: egetent <database> <key>"
47 *) die "sorry, database '${db}' not yet supported; file a bug" ;;
53 *[!0-9]*) # Non numeric
54 nidump ${db} . | awk -F: "(\$1 ~ /^${key}\$/) {print;exit;}"
57 nidump ${db} . | awk -F: "(\$3 == ${key}) {print;exit;}"
64 passwd) db="Users" mykey="UniqueID" ;;
65 group) db="Groups" mykey="PrimaryGroupID" ;;
69 *[!0-9]*) # Non numeric
70 dscl . -read /${db}/${key} 2>/dev/null |grep RecordName
73 dscl . -search /${db} ${mykey} ${key} 2>/dev/null
77 *-freebsd*|*-dragonfly*)
85 if [[ ${key} == [[:digit:]]* ]] ; then
86 [[ ${db} == "user" ]] && opts="-u" || opts="-g"
89 pw show ${db} ${opts} "${key}" -q
92 grep "${key}:\*:" /etc/${db}
95 # ignore output if nscd doesn't exist, or we're not running as root
96 nscd -i "${db}" 2>/dev/null
97 getent "${db}" "${key}"
102 # @FUNCTION: enewuser
103 # @USAGE: <user> [uid] [shell] [homedir] [groups]
105 # Same as enewgroup, you are not required to understand how to properly add
106 # a user to the system. The only required parameter is the username.
107 # Default uid is (pass -1 for this) next available, default shell is
108 # /bin/false, default homedir is /dev/null, and there are no default groups.
110 _assert_pkg_ebuild_phase ${FUNCNAME}
113 local euser=$1; shift
114 if [[ -z ${euser} ]] ; then
115 eerror "No username specified !"
116 die "Cannot call enewuser without a username"
119 # lets see if the username already exists
120 if [[ -n $(egetent passwd "${euser}") ]] ; then
123 einfo "Adding user '${euser}' to your system ..."
125 # options to pass to useradd
130 if [[ -n ${euid} && ${euid} != -1 ]] ; then
131 if [[ ${euid} -gt 0 ]] ; then
132 if [[ -n $(egetent passwd ${euid}) ]] ; then
136 eerror "Userid given but is not greater than 0 !"
137 die "${euid} is not a valid UID"
142 if [[ ${euid} == "next" ]] ; then
143 for ((euid = 101; euid <= 999; euid++)); do
144 [[ -z $(egetent passwd ${euid}) ]] && break
148 einfo " - Userid: ${euid}"
151 local eshell=$1; shift
152 if [[ ! -z ${eshell} ]] && [[ ${eshell} != "-1" ]] ; then
153 if [[ ! -e ${ROOT}${eshell} ]] ; then
154 eerror "A shell was specified but it does not exist !"
155 die "${eshell} does not exist in ${ROOT}"
157 if [[ ${eshell} == */false || ${eshell} == */nologin ]] ; then
158 eerror "Do not specify ${eshell} yourself, use -1"
159 die "Pass '-1' as the shell parameter"
162 for eshell in /sbin/nologin /usr/sbin/nologin /bin/false /usr/bin/false /dev/null ; do
163 [[ -x ${ROOT}${eshell} ]] && break
166 if [[ ${eshell} == "/dev/null" ]] ; then
167 eerror "Unable to identify the shell to use, proceeding with userland default."
169 GNU) eshell="/bin/false" ;;
170 BSD) eshell="/sbin/nologin" ;;
171 Darwin) eshell="/usr/sbin/nologin" ;;
172 *) die "Unable to identify the default shell for userland ${USERLAND}"
176 einfo " - Shell: ${eshell}"
177 opts+=( -s "${eshell}" )
180 local ehome=$1; shift
181 if [[ -z ${ehome} ]] || [[ ${ehome} == "-1" ]] ; then
184 einfo " - Home: ${ehome}"
185 opts+=( -d "${ehome}" )
188 local egroups=$1; shift
190 IFS="," read -r -a egroups_arr <<<"${egroups}"
192 if [[ ${#egroups_arr[@]} -gt 0 ]] ; then
193 local defgroup exgroups
194 for g in "${egroups_arr[@]}" ; do
195 if [[ -z $(egetent group "${g}") ]] ; then
196 eerror "You must add group ${g} to the system first"
197 die "${g} is not a valid GID"
199 if [[ -z ${defgroup} ]] ; then
205 opts+=( -g "${defgroup}" )
206 if [[ ! -z ${exgroups} ]] ; then
207 opts+=( -G "${exgroups:1}" )
210 einfo " - Groups: ${egroups:-(none)}"
213 if [[ $# -gt 0 ]] ; then
214 die "extra arguments no longer supported; please file a bug"
216 local comment="added by portage for ${PN}"
217 opts+=( -c "${comment}" )
218 einfo " - GECOS: ${comment}"
225 dscl . create "/users/${euser}" uid ${euid}
226 dscl . create "/users/${euser}" shell "${eshell}"
227 dscl . create "/users/${euser}" home "${ehome}"
228 dscl . create "/users/${euser}" realname "added by portage for ${PN}"
229 ### Add the user to the groups specified
230 for g in "${egroups_arr[@]}" ; do
231 dscl . merge "/groups/${g}" users "${euser}"
235 *-freebsd*|*-dragonfly*)
236 pw useradd "${euser}" "${opts[@]}" || die
240 useradd "${opts[@]}" "${euser}" || die
244 # all ops the same, except the -g vs -g/-G ...
245 useradd -u ${euid} -s "${eshell}" \
246 -d "${ehome}" -g "${egroups}" "${euser}" || die
250 useradd -r "${opts[@]}" "${euser}" || die
254 if [[ ! -e ${ROOT}/${ehome} ]] ; then
255 einfo " - Creating ${ehome} in ${ROOT}"
256 mkdir -p "${ROOT}/${ehome}"
257 chown "${euser}" "${ROOT}/${ehome}"
258 chmod 755 "${ROOT}/${ehome}"
262 # @FUNCTION: enewgroup
263 # @USAGE: <group> [gid]
265 # This function does not require you to understand how to properly add a
266 # group to the system. Just give it a group name to add and enewgroup will
267 # do the rest. You may specify the gid for the group or allow the group to
268 # allocate the next available one.
270 _assert_pkg_ebuild_phase ${FUNCNAME}
273 local egroup=$1; shift
274 if [[ -z ${egroup} ]] ; then
275 eerror "No group specified !"
276 die "Cannot call enewgroup without a group"
279 # see if group already exists
280 if [[ -n $(egetent group "${egroup}") ]] ; then
283 einfo "Adding group '${egroup}' to your system ..."
287 if [[ ! -z ${egid} ]] ; then
288 if [[ ${egid} -gt 0 ]] ; then
289 if [[ -n $(egetent group ${egid}) ]] ; then
290 egid="next available; requested gid taken"
293 eerror "Groupid given but is not greater than 0 !"
294 die "${egid} is not a valid GID"
297 egid="next available"
299 einfo " - Groupid: ${egid}"
302 if [[ $# -gt 0 ]] ; then
303 die "extra arguments no longer supported; please file a bug"
306 # Some targets need to find the next available GID manually
307 _enewgroup_next_gid() {
308 if [[ ${egid} == *[!0-9]* ]] ; then
310 for ((egid = 101; egid <= 999; egid++)) ; do
311 [[ -z $(egetent group ${egid}) ]] && break
320 dscl . create "/groups/${egroup}" gid ${egid}
321 dscl . create "/groups/${egroup}" passwd '*'
324 *-freebsd*|*-dragonfly*)
326 pw groupadd "${egroup}" -g ${egid} || die
331 groupadd -g ${egid} "${egroup}" || die
336 if [[ ${egid} == *[!0-9]* ]] ; then
337 # Non numeric; let groupadd figure out a GID for us
342 # We specify -r so that we get a GID in the system range from login.defs
343 groupadd -r ${opts} "${egroup}" || die
348 # @FUNCTION: egethome
351 # Gets the home directory for the specified user.
355 [[ $# -eq 1 ]] || die "usage: egethome <user>"
358 *-darwin*|*-freebsd*|*-dragonfly*)
361 *) # Linux, NetBSD, OpenBSD, etc...
366 egetent passwd "$1" | cut -d: -f${pos}
369 # @FUNCTION: egetshell
372 # Gets the shell for the specified user.
376 [[ $# -eq 1 ]] || die "usage: egetshell <user>"
379 *-darwin*|*-freebsd*|*-dragonfly*)
382 *) # Linux, NetBSD, OpenBSD, etc...
387 egetent passwd "$1" | cut -d: -f${pos}
390 # @FUNCTION: esethome
391 # @USAGE: <user> <homedir>
393 # Update the home directory in a platform-agnostic way.
394 # Required parameters is the username and the new home directory.
395 # Specify -1 if you want to set home to the enewuser default
397 # If the new home directory does not exist, it is created.
398 # Any previously existing home directory is NOT moved.
400 _assert_pkg_ebuild_phase ${FUNCNAME}
403 local euser=$1; shift
404 if [[ -z ${euser} ]] ; then
405 eerror "No username specified !"
406 die "Cannot call esethome without a username"
409 # lets see if the username already exists
410 if [[ -z $(egetent passwd "${euser}") ]] ; then
411 ewarn "User does not exist, cannot set home dir -- skipping."
416 local ehome=$1; shift
417 if [[ -z ${ehome} ]] ; then
418 eerror "No home directory specified !"
419 die "Cannot call esethome without a home directory or '-1'"
422 if [[ ${ehome} == "-1" ]] ; then
426 # exit with no message if home dir is up to date
427 if [[ $(egethome "${euser}") == ${ehome} ]]; then
431 einfo "Updating home for user '${euser}' ..."
432 einfo " - Home: ${ehome}"
434 # ensure home directory exists, otherwise update will fail
435 if [[ ! -e ${ROOT}/${ehome} ]] ; then
436 einfo " - Creating ${ehome} in ${ROOT}"
437 mkdir -p "${ROOT}/${ehome}"
438 chown "${euser}" "${ROOT}/${ehome}"
439 chmod 755 "${ROOT}/${ehome}"
442 # update the home directory
445 dscl . change "/users/${euser}" home "${ehome}"
448 *-freebsd*|*-dragonfly*)
449 pw usermod "${euser}" -d "${ehome}" && return 0
450 [[ $? == 8 ]] && eerror "${euser} is in use, cannot update home"
451 eerror "There was an error when attempting to update the home directory for ${euser}"
452 eerror "Please update it manually on your system:"
453 eerror "\t pw usermod \"${euser}\" -d \"${ehome}\""
457 usermod -d "${ehome}" "${euser}" && return 0
458 [[ $? == 8 ]] && eerror "${euser} is in use, cannot update home"
459 eerror "There was an error when attempting to update the home directory for ${euser}"
460 eerror "Please update it manually on your system (as root):"
461 eerror "\t usermod -d \"${ehome}\" \"${euser}\""