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

Contents of /eclass/user.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.13 - (show annotations) (download)
Sat Nov 26 06:50:27 2011 UTC (2 years, 10 months ago) by vapier
Branch: MAIN
Changes since 1.12: +7 -9 lines
re-use "eshell" when searching for a shell to avoid missing "local shell" decl pointed out by Brian Harring

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.12 2011/11/26 06:45:38 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 setup|preinst|postinst) ;;
22 *)
23 eerror "'$1()' called from '${EBUILD_PHASE}()' which is not a pkg_* function."
24 eerror "Package fails at QA and at life. Please file a bug."
25 die "Bad package! $1 is only for use in pkg_* functions!"
26 esac
27 }
28
29 # @FUNCTION: egetent
30 # @USAGE: <database> <key>
31 # @DESCRIPTION:
32 # Small wrapper for getent (Linux), nidump (< Mac OS X 10.5),
33 # dscl (Mac OS X 10.5), and pw (FreeBSD) used in enewuser()/enewgroup().
34 #
35 # Supported databases: group passwd
36 egetent() {
37 local db=$1 key=$2
38
39 [[ $# -ge 3 ]] && die "usage: egetent <database> <key>"
40
41 case ${db} in
42 passwd|group) ;;
43 *) die "sorry, database '${db}' not yet supported; file a bug" ;;
44 esac
45
46 case ${CHOST} in
47 *-darwin[678])
48 case ${key} in
49 *[!0-9]*) # Non numeric
50 nidump ${db} . | awk -F: "(\$1 ~ /^${key}\$/) {print;exit;}"
51 ;;
52 *) # Numeric
53 nidump ${db} . | awk -F: "(\$3 == ${key}) {print;exit;}"
54 ;;
55 esac
56 ;;
57 *-darwin*)
58 local mykey
59 case ${db} in
60 passwd) db="Users" mykey="UniqueID" ;;
61 group) db="Groups" mykey="PrimaryGroupID" ;;
62 esac
63
64 case ${key} in
65 *[!0-9]*) # Non numeric
66 dscl . -read /${db}/${key} 2>/dev/null |grep RecordName
67 ;;
68 *) # Numeric
69 dscl . -search /${db} ${mykey} ${key} 2>/dev/null
70 ;;
71 esac
72 ;;
73 *-freebsd*|*-dragonfly*)
74 case ${db} in
75 passwd) db="user" ;;
76 *) ;;
77 esac
78
79 # lookup by uid/gid
80 local opts
81 if [[ ${key} == [[:digit:]]* ]] ; then
82 [[ ${db} == "user" ]] && opts="-u" || opts="-g"
83 fi
84
85 pw show ${db} ${opts} "${key}" -q
86 ;;
87 *-netbsd*|*-openbsd*)
88 grep "${key}:\*:" /etc/${db}
89 ;;
90 *)
91 # ignore output if nscd doesn't exist, or we're not running as root
92 nscd -i "${db}" 2>/dev/null
93 getent "${db}" "${key}"
94 ;;
95 esac
96 }
97
98 # @FUNCTION: enewuser
99 # @USAGE: <user> [uid] [shell] [homedir] [groups]
100 # @DESCRIPTION:
101 # Same as enewgroup, you are not required to understand how to properly add
102 # a user to the system. The only required parameter is the username.
103 # Default uid is (pass -1 for this) next available, default shell is
104 # /bin/false, default homedir is /dev/null, and there are no default groups.
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+=" -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 eshell in /sbin/nologin /usr/sbin/nologin /bin/false /usr/bin/false /dev/null ; do
159 [[ -x ${ROOT}${eshell} ]] && break
160 done
161
162 if [[ ${eshell} == "/dev/null" ]] ; then
163 eerror "Unable to identify the shell to use, proceeding with userland default."
164 case ${USERLAND} in
165 GNU) eshell="/bin/false" ;;
166 BSD) eshell="/sbin/nologin" ;;
167 Darwin) eshell="/usr/sbin/nologin" ;;
168 *) die "Unable to identify the default shell for userland ${USERLAND}"
169 esac
170 fi
171 fi
172 einfo " - Shell: ${eshell}"
173 opts+=" -s ${eshell}"
174
175 # handle homedir
176 local ehome=$1; shift
177 if [[ -z ${ehome} ]] || [[ ${ehome} == "-1" ]] ; then
178 ehome="/dev/null"
179 fi
180 einfo " - Home: ${ehome}"
181 opts+=" -d ${ehome}"
182
183 # handle groups
184 local egroups=$1; shift
185 if [[ ! -z ${egroups} ]] ; then
186 local oldifs=${IFS}
187 local defgroup="" exgroups=""
188
189 export IFS=","
190 for g in ${egroups} ; do
191 export IFS=${oldifs}
192 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 exgroups="${exgroups},${g}"
200 fi
201 export IFS=","
202 done
203 export IFS=${oldifs}
204
205 opts+=" -g ${defgroup}"
206 if [[ ! -z ${exgroups} ]] ; then
207 opts+=" -G ${exgroups:1}"
208 fi
209 else
210 egroups="(none)"
211 fi
212 einfo " - Groups: ${egroups}"
213
214 # handle extra args
215 if [[ $# -gt 0 ]] ; then
216 die "extra arguments no longer supported; please file a bug"
217 else
218 set -- -c "added by portage for ${PN}"
219 einfo " - Extra: $@"
220 fi
221
222 # add the user
223 case ${CHOST} in
224 *-darwin*)
225 ### Make the user
226 dscl . create /users/${euser} uid ${euid}
227 dscl . create /users/${euser} shell ${eshell}
228 dscl . create /users/${euser} home ${ehome}
229 dscl . create /users/${euser} realname "added by portage for ${PN}"
230 ### Add the user to the groups specified
231 local oldifs=${IFS}
232 export IFS=","
233 for g in ${egroups} ; do
234 dscl . merge /groups/${g} users ${euser}
235 done
236 export IFS=${oldifs}
237 ;;
238
239 *-freebsd*|*-dragonfly*)
240 pw useradd ${euser} ${opts} "$@" || die
241 ;;
242
243 *-netbsd*)
244 useradd ${opts} ${euser} "$@" || die
245 ;;
246
247 *-openbsd*)
248 # all ops the same, except the -g vs -g/-G ...
249 useradd -u ${euid} -s ${eshell} \
250 -d ${ehome} -g ${egroups} "$@" ${euser} || die
251 ;;
252
253 *)
254 useradd -r ${opts} "$@" ${euser} || die
255 ;;
256 esac
257
258 if [[ ! -e ${ROOT}/${ehome} ]] ; then
259 einfo " - Creating ${ehome} in ${ROOT}"
260 mkdir -p "${ROOT}/${ehome}"
261 chown ${euser} "${ROOT}/${ehome}"
262 chmod 755 "${ROOT}/${ehome}"
263 fi
264 }
265
266 # @FUNCTION: enewgroup
267 # @USAGE: <group> [gid]
268 # @DESCRIPTION:
269 # This function does not require you to understand how to properly add a
270 # group to the system. Just give it a group name to add and enewgroup will
271 # do the rest. You may specify the gid for the group or allow the group to
272 # allocate the next available one.
273 enewgroup() {
274 _assert_pkg_ebuild_phase enewgroup
275
276 # get the group
277 local egroup="$1"; shift
278 if [ -z "${egroup}" ]
279 then
280 eerror "No group specified !"
281 die "Cannot call enewgroup without a group"
282 fi
283
284 # see if group already exists
285 if [[ -n $(egetent group "${egroup}") ]]; then
286 return 0
287 fi
288 einfo "Adding group '${egroup}' to your system ..."
289
290 # options to pass to useradd
291 local opts=
292
293 # handle gid
294 local egid="$1"; shift
295 if [ ! -z "${egid}" ]
296 then
297 if [ "${egid}" -gt 0 ]
298 then
299 if [ -z "`egetent group ${egid}`" ]
300 then
301 if [[ "${CHOST}" == *-darwin* ]]; then
302 opts+=" ${egid}"
303 else
304 opts+=" -g ${egid}"
305 fi
306 else
307 egid="next available; requested gid taken"
308 fi
309 else
310 eerror "Groupid given but is not greater than 0 !"
311 die "${egid} is not a valid GID"
312 fi
313 else
314 egid="next available"
315 fi
316 einfo " - Groupid: ${egid}"
317
318 # handle extra
319 if [ $# -gt 0 ] ; then
320 die "extra arguments no longer supported; please file a bug"
321 fi
322
323 # add the group
324 case ${CHOST} in
325 *-darwin*)
326 # If we need the next available
327 case ${egid} in
328 *[!0-9]*) # Non numeric
329 for ((egid = 101; egid <= 999; egid++)); do
330 [[ -z $(egetent group ${egid}) ]] && break
331 done
332 esac
333 dscl . create /groups/${egroup} gid ${egid}
334 dscl . create /groups/${egroup} passwd '*'
335 ;;
336
337 *-freebsd*|*-dragonfly*)
338 case ${egid} in
339 *[!0-9]*) # Non numeric
340 for ((egid = 101; egid <= 999; egid++)); do
341 [[ -z $(egetent group ${egid}) ]] && break
342 done
343 esac
344 pw groupadd ${egroup} -g ${egid} || die
345 ;;
346
347 *-netbsd*)
348 case ${egid} in
349 *[!0-9]*) # Non numeric
350 for ((egid = 101; egid <= 999; egid++)); do
351 [[ -z $(egetent group ${egid}) ]] && break
352 done
353 esac
354 groupadd -g ${egid} ${egroup} || die
355 ;;
356
357 *)
358 # We specify -r so that we get a GID in the system range from login.defs
359 groupadd -r ${opts} ${egroup} || die
360 ;;
361 esac
362 }
363
364 # @FUNCTION: egethome
365 # @USAGE: <user>
366 # @DESCRIPTION:
367 # Gets the home directory for the specified user.
368 egethome() {
369 local pos
370
371 [[ $# -eq 1 ]] || die "usage: egethome <user>"
372
373 case ${CHOST} in
374 *-darwin*|*-freebsd*|*-dragonfly*)
375 pos=9
376 ;;
377 *) # Linux, NetBSD, OpenBSD, etc...
378 pos=6
379 ;;
380 esac
381
382 egetent passwd $1 | cut -d: -f${pos}
383 }
384
385 # @FUNCTION: egetshell
386 # @USAGE: <user>
387 # @DESCRIPTION:
388 # Gets the shell for the specified user.
389 egetshell() {
390 local pos
391
392 [[ $# -eq 1 ]] || die "usage: egetshell <user>"
393
394 case ${CHOST} in
395 *-darwin*|*-freebsd*|*-dragonfly*)
396 pos=10
397 ;;
398 *) # Linux, NetBSD, OpenBSD, etc...
399 pos=7
400 ;;
401 esac
402
403 egetent passwd "$1" | cut -d: -f${pos}
404 }

  ViewVC Help
Powered by ViewVC 1.1.20