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

Contents of /eclass/user.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.15 - (hide annotations) (download)
Tue Nov 29 19:01:28 2011 UTC (2 years, 9 months ago) by vapier
Branch: MAIN
Changes since 1.14: +6 -5 lines
tweak failure message 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     if [[ ! -z ${egroups} ]] ; then
187     local oldifs=${IFS}
188     local defgroup="" exgroups=""
189    
190     export IFS=","
191     for g in ${egroups} ; do
192     export IFS=${oldifs}
193     if [[ -z $(egetent group "${g}") ]] ; then
194     eerror "You must add group ${g} to the system first"
195     die "${g} is not a valid GID"
196     fi
197     if [[ -z ${defgroup} ]] ; then
198     defgroup=${g}
199     else
200     exgroups="${exgroups},${g}"
201     fi
202     export IFS=","
203     done
204     export IFS=${oldifs}
205    
206 vapier 1.14 opts+=( -g "${defgroup}" )
207 vapier 1.1 if [[ ! -z ${exgroups} ]] ; then
208 vapier 1.14 opts+=( -G "${exgroups:1}" )
209 vapier 1.1 fi
210     else
211     egroups="(none)"
212     fi
213     einfo " - Groups: ${egroups}"
214    
215 vapier 1.9 # handle extra args
216     if [[ $# -gt 0 ]] ; then
217     die "extra arguments no longer supported; please file a bug"
218     else
219 vapier 1.14 local comment="added by portage for ${PN}"
220     opts+=( -c "${comment}" )
221     einfo " - GECOS: ${comment}"
222 vapier 1.9 fi
223    
224     # add the user
225 vapier 1.1 case ${CHOST} in
226     *-darwin*)
227     ### Make the user
228 vapier 1.14 dscl . create "/users/${euser}" uid ${euid}
229     dscl . create "/users/${euser}" shell "${eshell}"
230     dscl . create "/users/${euser}" home "${ehome}"
231     dscl . create "/users/${euser}" realname "added by portage for ${PN}"
232 vapier 1.9 ### Add the user to the groups specified
233 vapier 1.14 local g oldifs=${IFS}
234 vapier 1.9 export IFS=","
235     for g in ${egroups} ; do
236 vapier 1.14 dscl . merge "/groups/${g}" users "${euser}"
237 vapier 1.9 done
238     export IFS=${oldifs}
239 vapier 1.1 ;;
240 vapier 1.9
241 vapier 1.1 *-freebsd*|*-dragonfly*)
242 vapier 1.14 pw useradd "${euser}" "${opts[@]}" || die
243 vapier 1.1 ;;
244    
245     *-netbsd*)
246 vapier 1.14 useradd "${opts[@]}" "${euser}" || die
247 vapier 1.1 ;;
248    
249     *-openbsd*)
250 vapier 1.9 # all ops the same, except the -g vs -g/-G ...
251 vapier 1.14 useradd -u ${euid} -s "${eshell}" \
252     -d "${ehome}" -g "${egroups}" "${euser}" || die
253 vapier 1.1 ;;
254    
255     *)
256 vapier 1.14 useradd -r "${opts[@]}" "${euser}" || die
257 vapier 1.1 ;;
258     esac
259    
260     if [[ ! -e ${ROOT}/${ehome} ]] ; then
261     einfo " - Creating ${ehome} in ${ROOT}"
262     mkdir -p "${ROOT}/${ehome}"
263 vapier 1.14 chown "${euser}" "${ROOT}/${ehome}"
264 vapier 1.1 chmod 755 "${ROOT}/${ehome}"
265     fi
266     }
267    
268     # @FUNCTION: enewgroup
269     # @USAGE: <group> [gid]
270     # @DESCRIPTION:
271     # This function does not require you to understand how to properly add a
272     # group to the system. Just give it a group name to add and enewgroup will
273     # do the rest. You may specify the gid for the group or allow the group to
274     # allocate the next available one.
275     enewgroup() {
276 vapier 1.15 _assert_pkg_ebuild_phase ${FUNCNAME}
277 vapier 1.1
278     # get the group
279 vapier 1.14 local egroup=$1; shift
280     if [[ -z ${egroup} ]] ; then
281 vapier 1.1 eerror "No group specified !"
282     die "Cannot call enewgroup without a group"
283     fi
284    
285     # see if group already exists
286 vapier 1.14 if [[ -n $(egetent group "${egroup}") ]] ; then
287 vapier 1.1 return 0
288     fi
289     einfo "Adding group '${egroup}' to your system ..."
290    
291     # handle gid
292 vapier 1.14 local egid=$1; shift
293     if [[ ! -z ${egid} ]] ; then
294     if [[ ${egid} -gt 0 ]] ; then
295     if [[ -n $(egetent group ${egid}) ]] ; then
296 vapier 1.1 egid="next available; requested gid taken"
297     fi
298     else
299     eerror "Groupid given but is not greater than 0 !"
300     die "${egid} is not a valid GID"
301     fi
302     else
303     egid="next available"
304     fi
305     einfo " - Groupid: ${egid}"
306    
307     # handle extra
308 vapier 1.14 if [[ $# -gt 0 ]] ; then
309 vapier 1.9 die "extra arguments no longer supported; please file a bug"
310     fi
311 vapier 1.1
312 vapier 1.14 # Some targets need to find the next available GID manually
313     _enewgroup_next_gid() {
314     if [[ ${egid} == *[!0-9]* ]] ; then
315     # Non numeric
316     for ((egid = 101; egid <= 999; egid++)) ; do
317     [[ -z $(egetent group ${egid}) ]] && break
318     done
319     fi
320     }
321    
322 vapier 1.1 # add the group
323     case ${CHOST} in
324     *-darwin*)
325 vapier 1.14 _enewgroup_next_gid
326     dscl . create "/groups/${egroup}" gid ${egid}
327     dscl . create "/groups/${egroup}" passwd '*'
328 vapier 1.1 ;;
329    
330     *-freebsd*|*-dragonfly*)
331 vapier 1.14 _enewgroup_next_gid
332     pw groupadd "${egroup}" -g ${egid} || die
333 vapier 1.1 ;;
334    
335     *-netbsd*)
336 vapier 1.14 _enewgroup_next_gid
337     groupadd -g ${egid} "${egroup}" || die
338 vapier 1.1 ;;
339    
340     *)
341 vapier 1.14 local opts
342     if [[ ${egid} == *[!0-9]* ]] ; then
343     # Non numeric; let groupadd figure out a GID for us
344     opts=""
345     else
346     opts="-g ${egid}"
347     fi
348 vapier 1.1 # We specify -r so that we get a GID in the system range from login.defs
349 vapier 1.14 groupadd -r ${opts} "${egroup}" || die
350 vapier 1.1 ;;
351     esac
352     }
353 vapier 1.6
354     # @FUNCTION: egethome
355     # @USAGE: <user>
356     # @DESCRIPTION:
357     # Gets the home directory for the specified user.
358     egethome() {
359     local pos
360    
361 vapier 1.7 [[ $# -eq 1 ]] || die "usage: egethome <user>"
362    
363 vapier 1.6 case ${CHOST} in
364     *-darwin*|*-freebsd*|*-dragonfly*)
365     pos=9
366     ;;
367     *) # Linux, NetBSD, OpenBSD, etc...
368     pos=6
369     ;;
370     esac
371    
372 vapier 1.14 egetent passwd "$1" | cut -d: -f${pos}
373 vapier 1.6 }
374 vapier 1.8
375     # @FUNCTION: egetshell
376     # @USAGE: <user>
377     # @DESCRIPTION:
378     # Gets the shell for the specified user.
379     egetshell() {
380     local pos
381    
382     [[ $# -eq 1 ]] || die "usage: egetshell <user>"
383    
384     case ${CHOST} in
385     *-darwin*|*-freebsd*|*-dragonfly*)
386     pos=10
387     ;;
388     *) # Linux, NetBSD, OpenBSD, etc...
389     pos=7
390     ;;
391     esac
392    
393     egetent passwd "$1" | cut -d: -f${pos}
394     }

  ViewVC Help
Powered by ViewVC 1.1.20