1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: user-info.eclass
6 # base-system@gentoo.org (Linux)
7 # Michał Górny <mgorny@gentoo.org> (NetBSD)
8 # @BLURB: Read-only access to user and group information
10 if [[ -z ${_USER_INFO_ECLASS} ]]; then
14 # @USAGE: <database> <key>
16 # Small wrapper for getent (Linux), nidump (< Mac OS X 10.5),
17 # dscl (Mac OS X 10.5), and pw (FreeBSD) used in enewuser()/enewgroup().
19 # Supported databases: group passwd
23 [[ $# -ge 3 ]] && die "usage: egetent <database> <key>"
27 *) die "sorry, database '${db}' not yet supported; file a bug" ;;
31 *-freebsd*|*-dragonfly*)
39 if [[ ${key} == [[:digit:]]* ]] ; then
40 [[ ${db} == "user" ]] && opts="-u" || opts="-g"
43 pw show ${db} ${opts} "${key}" -q
46 grep "${key}:\*:" /etc/${db}
49 # ignore nscd output if we're not running as root
50 type -p nscd >/dev/null && nscd -i "${db}" 2>/dev/null
51 getent "${db}" "${key}"
56 # @FUNCTION: egetusername
59 # Gets the username for given UID.
61 [[ $# -eq 1 ]] || die "usage: egetusername <uid>"
63 egetent passwd "$1" | cut -d: -f1
66 # @FUNCTION: egetgroupname
69 # Gets the group name for given GID.
71 [[ $# -eq 1 ]] || die "usage: egetgroupname <gid>"
73 egetent group "$1" | cut -d: -f1
79 # Gets the home directory for the specified user.
83 [[ $# -eq 1 ]] || die "usage: egethome <user>"
86 *-freebsd*|*-dragonfly*)
89 *) # Linux, NetBSD, OpenBSD, etc...
94 egetent passwd "$1" | cut -d: -f${pos}
97 # @FUNCTION: egetshell
100 # Gets the shell for the specified user.
104 [[ $# -eq 1 ]] || die "usage: egetshell <user>"
107 *-freebsd*|*-dragonfly*)
110 *) # Linux, NetBSD, OpenBSD, etc...
115 egetent passwd "$1" | cut -d: -f${pos}
118 # @FUNCTION: egetcomment
121 # Gets the comment field for the specified user.
125 [[ $# -eq 1 ]] || die "usage: egetcomment <user>"
128 *-freebsd*|*-dragonfly*)
131 *) # Linux, NetBSD, OpenBSD, etc...
136 egetent passwd "$1" | cut -d: -f${pos}
139 # @FUNCTION: egetgroups
142 # Gets all the groups user belongs to. The primary group is returned
143 # first, then all supplementary groups. Groups are ','-separated.
145 [[ $# -eq 1 ]] || die "usage: egetgroups <user>"
148 read -r -a egroups_arr < <(id -G -n "$1")
150 local g groups=${egroups_arr[0]}
151 # sort supplementary groups to make comparison possible
153 [[ -n ${g} ]] && groups+=",${g}"
154 done < <(printf '%s\n' "${egroups_arr[@]:1}" | sort)