/[gentoo-x86]/eclass/user.eclass
Gentoo

Contents of /eclass/user.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.16 - (hide annotations) (download)
Tue Nov 29 19:31:20 2011 UTC (2 years, 10 months ago) by vapier
Branch: MAIN
Changes since 1.15: +9 -18 lines
avoid IFS abuse by using `IFS=, read -a` to have bash create an array for us as pointed out by David James

1 vapier 1.1 # Copyright 1999-2011 Gentoo Foundation
2     # Distributed under the terms of the GNU General Public License v2
3 vapier 1.15 # $Header: /var/cvsroot/gentoo-x86/eclass/user.eclass,v 1.14 2011/11/26 07:20:31 vapier Exp $
4 vapier 1.1
5     # @ECLASS: user.eclass
6     # @MAINTAINER:
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
12     # @DESCRIPTION:
13     # The user eclass contains a suite of functions that allow ebuilds
14     # to quickly make sure users in the installed system are sane.
15    
16 vapier 1.2 # @FUNCTION: _assert_pkg_ebuild_phase
17     # @INTERNAL
18     # @USAGE: <calling func name>
19     _assert_pkg_ebuild_phase() {
20     case ${EBUILD_PHASE} in
21 vapier 1.11 setup|preinst|postinst) ;;
22     *)
23 vapier 1.15 eerror "'$1()' called from '${EBUILD_PHASE}' phase which is not OK:"
24     eerror "You may only call from pkg_{setup,preinst,postinst} functions."
25 vapier 1.2 eerror "Package fails at QA and at life. Please file a bug."
26 vapier 1.15 die "Bad package! $1 is only for use in some pkg_* functions!"
27 vapier 1.2 esac
28     }
29    
30 vapier 1.1 # @FUNCTION: egetent
31     # @USAGE: <database> <key>
32     # @DESCRIPTION:
33     # Small wrapper for getent (Linux), nidump (< Mac OS X 10.5),
34     # dscl (Mac OS X 10.5), and pw (FreeBSD) used in enewuser()/enewgroup().
35 vapier 1.5 #
36     # Supported databases: group passwd
37 vapier 1.1 egetent() {
38 vapier 1.5 local db=$1 key=$2
39    
40     [[ $# -ge 3 ]] && die "usage: egetent <database> <key>"
41    
42     case ${db} in
43     passwd|group) ;;
44     *) die "sorry, database '${db}' not yet supported; file a bug" ;;
45     esac
46    
47 vapier 1.1 case ${CHOST} in
48     *-darwin[678])
49 vapier 1.5 case ${key} in
50 vapier 1.1 *[!0-9]*) # Non numeric
51 vapier 1.5 nidump ${db} . | awk -F: "(\$1 ~ /^${key}\$/) {print;exit;}"
52 vapier 1.1 ;;
53     *) # Numeric
54 vapier 1.5 nidump ${db} . | awk -F: "(\$3 == ${key}) {print;exit;}"
55 vapier 1.1 ;;
56     esac
57     ;;
58     *-darwin*)
59 vapier 1.5 local mykey
60     case ${db} in
61     passwd) db="Users" mykey="UniqueID" ;;
62     group) db="Groups" mykey="PrimaryGroupID" ;;
63     esac
64    
65     case ${key} in
66 vapier 1.1 *[!0-9]*) # Non numeric
67 vapier 1.5 dscl . -read /${db}/${key} 2>/dev/null |grep RecordName
68 vapier 1.1 ;;
69     *) # Numeric
70 vapier 1.5 dscl . -search /${db} ${mykey} ${key} 2>/dev/null
71 vapier 1.1 ;;
72     esac
73     ;;
74     *-freebsd*|*-dragonfly*)
75 vapier 1.5 case ${db} in
76     passwd) db="user" ;;
77     *) ;;
78     esac
79 vapier 1.1
80     # lookup by uid/gid
81 vapier 1.5 local opts
82     if [[ ${key} == [[:digit:]]* ]] ; then
83 naota 1.10 [[ ${db} == "user" ]] && opts="-u" || opts="-g"
84 vapier 1.1 fi
85    
86 naota 1.10 pw show ${db} ${opts} "${key}" -q
87 vapier 1.1 ;;
88     *-netbsd*|*-openbsd*)
89 vapier 1.5 grep "${key}:\*:" /etc/${db}
90 vapier 1.1 ;;
91     *)
92 vapier 1.3 # ignore output if nscd doesn't exist, or we're not running as root
93 vapier 1.5 nscd -i "${db}" 2>/dev/null
94     getent "${db}" "${key}"
95 vapier 1.1 ;;
96     esac
97     }
98    
99     # @FUNCTION: enewuser
100 vapier 1.9 # @USAGE: <user> [uid] [shell] [homedir] [groups]
101 vapier 1.1 # @DESCRIPTION:
102     # Same as enewgroup, you are not required to understand how to properly add
103     # a user to the system. The only required parameter is the username.
104     # Default uid is (pass -1 for this) next available, default shell is
105 vapier 1.9 # /bin/false, default homedir is /dev/null, and there are no default groups.
106 vapier 1.1 enewuser() {
107 vapier 1.15 _assert_pkg_ebuild_phase ${FUNCNAME}
108 vapier 1.1
109     # get the username
110     local euser=$1; shift
111     if [[ -z ${euser} ]] ; then
112     eerror "No username specified !"
113     die "Cannot call enewuser without a username"
114     fi
115    
116     # lets see if the username already exists
117     if [[ -n $(egetent passwd "${euser}") ]] ; then
118     return 0
119     fi
120     einfo "Adding user '${euser}' to your system ..."
121    
122     # options to pass to useradd
123 vapier 1.14 local opts=()
124 vapier 1.1
125     # handle uid
126     local euid=$1; shift
127     if [[ -n ${euid} && ${euid} != -1 ]] ; then
128     if [[ ${euid} -gt 0 ]] ; then
129     if [[ -n $(egetent passwd ${euid}) ]] ; then
130     euid="next"
131     fi
132     else
133     eerror "Userid given but is not greater than 0 !"
134     die "${euid} is not a valid UID"
135     fi
136     else
137     euid="next"
138     fi
139     if [[ ${euid} == "next" ]] ; then
140     for ((euid = 101; euid <= 999; euid++)); do
141     [[ -z $(egetent passwd ${euid}) ]] && break
142     done
143     fi
144 vapier 1.14 opts+=( -u ${euid} )
145 vapier 1.1 einfo " - Userid: ${euid}"
146    
147     # handle shell
148     local eshell=$1; shift
149     if [[ ! -z ${eshell} ]] && [[ ${eshell} != "-1" ]] ; then
150     if [[ ! -e ${ROOT}${eshell} ]] ; then
151     eerror "A shell was specified but it does not exist !"
152     die "${eshell} does not exist in ${ROOT}"
153     fi
154     if [[ ${eshell} == */false || ${eshell} == */nologin ]] ; then
155     eerror "Do not specify ${eshell} yourself, use -1"
156     die "Pass '-1' as the shell parameter"
157     fi
158     else
159 vapier 1.13 for eshell in /sbin/nologin /usr/sbin/nologin /bin/false /usr/bin/false /dev/null ; do
160     [[ -x ${ROOT}${eshell} ]] && break
161 vapier 1.1 done
162    
163 vapier 1.13 if [[ ${eshell} == "/dev/null" ]] ; then
164 vapier 1.1 eerror "Unable to identify the shell to use, proceeding with userland default."
165     case ${USERLAND} in
166 vapier 1.13 GNU) eshell="/bin/false" ;;
167     BSD) eshell="/sbin/nologin" ;;
168     Darwin) eshell="/usr/sbin/nologin" ;;
169 vapier 1.1 *) die "Unable to identify the default shell for userland ${USERLAND}"
170     esac
171     fi
172     fi
173     einfo " - Shell: ${eshell}"
174 vapier 1.14 opts+=( -s "${eshell}" )
175 vapier 1.1
176     # handle homedir
177     local ehome=$1; shift
178     if [[ -z ${ehome} ]] || [[ ${ehome} == "-1" ]] ; then
179     ehome="/dev/null"
180     fi
181     einfo " - Home: ${ehome}"
182 vapier 1.14 opts+=( -d "${ehome}" )
183 vapier 1.1
184     # handle groups
185     local egroups=$1; shift
186 vapier 1.16 local g egroups_arr
187     IFS="," read -r -a egroups_arr <<<"${egroups}"
188     shift
189     if [[ ${#egroups_arr[@]} -gt 0 ]] ; then
190     local defgroup exgroups
191     for g in "${egroups_arr[@]}" ; do
192 vapier 1.1 if [[ -z $(egetent group "${g}") ]] ; then
193     eerror "You must add group ${g} to the system first"
194     die "${g} is not a valid GID"
195     fi
196     if [[ -z ${defgroup} ]] ; then
197     defgroup=${g}
198     else
199 vapier 1.16 exgroups+=",${g}"
200 vapier 1.1 fi
201     done
202 vapier 1.14 opts+=( -g "${defgroup}" )
203 vapier 1.1 if [[ ! -z ${exgroups} ]] ; then
204 vapier 1.14 opts+=( -G "${exgroups:1}" )
205 vapier 1.1 fi
206     fi
207 vapier 1.16 einfo " - Groups: ${egroups:-(none)}"
208 vapier 1.1
209 vapier 1.9 # handle extra args
210     if [[ $# -gt 0 ]] ; then
211     die "extra arguments no longer supported; please file a bug"
212     else
213 vapier 1.14 local comment="added by portage for ${PN}"
214     opts+=( -c "${comment}" )
215     einfo " - GECOS: ${comment}"
216 vapier 1.9 fi
217    
218     # add the user
219 vapier 1.1 case ${CHOST} in
220     *-darwin*)
221     ### Make the user
222 vapier 1.14 dscl . create "/users/${euser}" uid ${euid}
223     dscl . create "/users/${euser}" shell "${eshell}"
224     dscl . create "/users/${euser}" home "${ehome}"
225     dscl . create "/users/${euser}" realname "added by portage for ${PN}"
226 vapier 1.9 ### Add the user to the groups specified
227 vapier 1.16 for g in "${egroups_arr[@]}" ; do
228 vapier 1.14 dscl . merge "/groups/${g}" users "${euser}"
229 vapier 1.9 done
230 vapier 1.1 ;;
231 vapier 1.9
232 vapier 1.1 *-freebsd*|*-dragonfly*)
233 vapier 1.14 pw useradd "${euser}" "${opts[@]}" || die
234 vapier 1.1 ;;
235    
236     *-netbsd*)
237 vapier 1.14 useradd "${opts[@]}" "${euser}" || die
238 vapier 1.1 ;;
239    
240     *-openbsd*)
241 vapier 1.9 # all ops the same, except the -g vs -g/-G ...
242 vapier 1.14 useradd -u ${euid} -s "${eshell}" \
243     -d "${ehome}" -g "${egroups}" "${euser}" || die
244 vapier 1.1 ;;
245    
246     *)
247 vapier 1.14 useradd -r "${opts[@]}" "${euser}" || die
248 vapier 1.1 ;;
249     esac
250    
251     if [[ ! -e ${ROOT}/${ehome} ]] ; then
252     einfo " - Creating ${ehome} in ${ROOT}"
253     mkdir -p "${ROOT}/${ehome}"
254 vapier 1.14 chown "${euser}" "${ROOT}/${ehome}"
255 vapier 1.1 chmod 755 "${ROOT}/${ehome}"
256     fi
257     }
258    
259     # @FUNCTION: enewgroup
260     # @USAGE: <group> [gid]
261     # @DESCRIPTION:
262     # This function does not require you to understand how to properly add a
263     # group to the system. Just give it a group name to add and enewgroup will
264     # do the rest. You may specify the gid for the group or allow the group to
265     # allocate the next available one.
266     enewgroup() {
267 vapier 1.15 _assert_pkg_ebuild_phase ${FUNCNAME}
268 vapier 1.1
269     # get the group
270 vapier 1.14 local egroup=$1; shift
271     if [[ -z ${egroup} ]] ; then
272 vapier 1.1 eerror "No group specified !"
273     die "Cannot call enewgroup without a group"
274     fi
275    
276     # see if group already exists
277 vapier 1.14 if [[ -n $(egetent group "${egroup}") ]] ; then
278 vapier 1.1 return 0
279     fi
280     einfo "Adding group '${egroup}' to your system ..."
281    
282     # handle gid
283 vapier 1.14 local egid=$1; shift
284     if [[ ! -z ${egid} ]] ; then
285     if [[ ${egid} -gt 0 ]] ; then
286     if [[ -n $(egetent group ${egid}) ]] ; then
287 vapier 1.1 egid="next available; requested gid taken"
288     fi
289     else
290     eerror "Groupid given but is not greater than 0 !"
291     die "${egid} is not a valid GID"
292     fi
293     else
294     egid="next available"
295     fi
296     einfo " - Groupid: ${egid}"
297    
298     # handle extra
299 vapier 1.14 if [[ $# -gt 0 ]] ; then
300 vapier 1.9 die "extra arguments no longer supported; please file a bug"
301     fi
302 vapier 1.1
303 vapier 1.14 # Some targets need to find the next available GID manually
304     _enewgroup_next_gid() {
305     if [[ ${egid} == *[!0-9]* ]] ; then
306     # Non numeric
307     for ((egid = 101; egid <= 999; egid++)) ; do
308     [[ -z $(egetent group ${egid}) ]] && break
309     done
310     fi
311     }
312    
313 vapier 1.1 # add the group
314     case ${CHOST} in
315     *-darwin*)
316 vapier 1.14 _enewgroup_next_gid
317     dscl . create "/groups/${egroup}" gid ${egid}
318     dscl . create "/groups/${egroup}" passwd '*'
319 vapier 1.1 ;;
320    
321     *-freebsd*|*-dragonfly*)
322 vapier 1.14 _enewgroup_next_gid
323     pw groupadd "${egroup}" -g ${egid} || die
324 vapier 1.1 ;;
325    
326     *-netbsd*)
327 vapier 1.14 _enewgroup_next_gid
328     groupadd -g ${egid} "${egroup}" || die
329 vapier 1.1 ;;
330    
331     *)
332 vapier 1.14 local opts
333     if [[ ${egid} == *[!0-9]* ]] ; then
334     # Non numeric; let groupadd figure out a GID for us
335     opts=""
336     else
337     opts="-g ${egid}"
338     fi
339 vapier 1.1 # We specify -r so that we get a GID in the system range from login.defs
340 vapier 1.14 groupadd -r ${opts} "${egroup}" || die
341 vapier 1.1 ;;
342     esac
343     }
344 vapier 1.6
345     # @FUNCTION: egethome
346     # @USAGE: <user>
347     # @DESCRIPTION:
348     # Gets the home directory for the specified user.
349     egethome() {
350     local pos
351    
352 vapier 1.7 [[ $# -eq 1 ]] || die "usage: egethome <user>"
353    
354 vapier 1.6 case ${CHOST} in
355     *-darwin*|*-freebsd*|*-dragonfly*)
356     pos=9
357     ;;
358     *) # Linux, NetBSD, OpenBSD, etc...
359     pos=6
360     ;;
361     esac
362    
363 vapier 1.14 egetent passwd "$1" | cut -d: -f${pos}
364 vapier 1.6 }
365 vapier 1.8
366     # @FUNCTION: egetshell
367     # @USAGE: <user>
368     # @DESCRIPTION:
369     # Gets the shell for the specified user.
370     egetshell() {
371     local pos
372    
373     [[ $# -eq 1 ]] || die "usage: egetshell <user>"
374    
375     case ${CHOST} in
376     *-darwin*|*-freebsd*|*-dragonfly*)
377     pos=10
378     ;;
379     *) # Linux, NetBSD, OpenBSD, etc...
380     pos=7
381     ;;
382     esac
383    
384     egetent passwd "$1" | cut -d: -f${pos}
385     }

  ViewVC Help
Powered by ViewVC 1.1.20