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

Contents of /eclass/user.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (show annotations) (download)
Thu Nov 3 00:24:19 2011 UTC (2 years, 5 months ago) by vapier
Branch: MAIN
Changes since 1.4: +34 -19 lines
clean up egetent style to make it clear how it works

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

  ViewVC Help
Powered by ViewVC 1.1.20