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

Contents of /eclass/user.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.15 - (show annotations) (download)
Tue Nov 29 19:01:28 2011 UTC (2 years, 8 months ago) by vapier
Branch: MAIN
Changes since 1.14: +6 -5 lines
tweak failure message as pointed out by David James

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.14 2011/11/26 07:20:31 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}' phase which is not OK:"
24 eerror "You may only call from pkg_{setup,preinst,postinst} functions."
25 eerror "Package fails at QA and at life. Please file a bug."
26 die "Bad package! $1 is only for use in some pkg_* functions!"
27 esac
28 }
29
30 # @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 #
36 # Supported databases: group passwd
37 egetent() {
38 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 case ${CHOST} in
48 *-darwin[678])
49 case ${key} in
50 *[!0-9]*) # Non numeric
51 nidump ${db} . | awk -F: "(\$1 ~ /^${key}\$/) {print;exit;}"
52 ;;
53 *) # Numeric
54 nidump ${db} . | awk -F: "(\$3 == ${key}) {print;exit;}"
55 ;;
56 esac
57 ;;
58 *-darwin*)
59 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 *[!0-9]*) # Non numeric
67 dscl . -read /${db}/${key} 2>/dev/null |grep RecordName
68 ;;
69 *) # Numeric
70 dscl . -search /${db} ${mykey} ${key} 2>/dev/null
71 ;;
72 esac
73 ;;
74 *-freebsd*|*-dragonfly*)
75 case ${db} in
76 passwd) db="user" ;;
77 *) ;;
78 esac
79
80 # lookup by uid/gid
81 local opts
82 if [[ ${key} == [[:digit:]]* ]] ; then
83 [[ ${db} == "user" ]] && opts="-u" || opts="-g"
84 fi
85
86 pw show ${db} ${opts} "${key}" -q
87 ;;
88 *-netbsd*|*-openbsd*)
89 grep "${key}:\*:" /etc/${db}
90 ;;
91 *)
92 # ignore output if nscd doesn't exist, or we're not running as root
93 nscd -i "${db}" 2>/dev/null
94 getent "${db}" "${key}"
95 ;;
96 esac
97 }
98
99 # @FUNCTION: enewuser
100 # @USAGE: <user> [uid] [shell] [homedir] [groups]
101 # @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 # /bin/false, default homedir is /dev/null, and there are no default groups.
106 enewuser() {
107 _assert_pkg_ebuild_phase ${FUNCNAME}
108
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 local opts=()
124
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 opts+=( -u ${euid} )
145 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 for eshell in /sbin/nologin /usr/sbin/nologin /bin/false /usr/bin/false /dev/null ; do
160 [[ -x ${ROOT}${eshell} ]] && break
161 done
162
163 if [[ ${eshell} == "/dev/null" ]] ; then
164 eerror "Unable to identify the shell to use, proceeding with userland default."
165 case ${USERLAND} in
166 GNU) eshell="/bin/false" ;;
167 BSD) eshell="/sbin/nologin" ;;
168 Darwin) eshell="/usr/sbin/nologin" ;;
169 *) die "Unable to identify the default shell for userland ${USERLAND}"
170 esac
171 fi
172 fi
173 einfo " - Shell: ${eshell}"
174 opts+=( -s "${eshell}" )
175
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 opts+=( -d "${ehome}" )
183
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 opts+=( -g "${defgroup}" )
207 if [[ ! -z ${exgroups} ]] ; then
208 opts+=( -G "${exgroups:1}" )
209 fi
210 else
211 egroups="(none)"
212 fi
213 einfo " - Groups: ${egroups}"
214
215 # handle extra args
216 if [[ $# -gt 0 ]] ; then
217 die "extra arguments no longer supported; please file a bug"
218 else
219 local comment="added by portage for ${PN}"
220 opts+=( -c "${comment}" )
221 einfo " - GECOS: ${comment}"
222 fi
223
224 # add the user
225 case ${CHOST} in
226 *-darwin*)
227 ### Make the user
228 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 ### Add the user to the groups specified
233 local g oldifs=${IFS}
234 export IFS=","
235 for g in ${egroups} ; do
236 dscl . merge "/groups/${g}" users "${euser}"
237 done
238 export IFS=${oldifs}
239 ;;
240
241 *-freebsd*|*-dragonfly*)
242 pw useradd "${euser}" "${opts[@]}" || die
243 ;;
244
245 *-netbsd*)
246 useradd "${opts[@]}" "${euser}" || die
247 ;;
248
249 *-openbsd*)
250 # all ops the same, except the -g vs -g/-G ...
251 useradd -u ${euid} -s "${eshell}" \
252 -d "${ehome}" -g "${egroups}" "${euser}" || die
253 ;;
254
255 *)
256 useradd -r "${opts[@]}" "${euser}" || die
257 ;;
258 esac
259
260 if [[ ! -e ${ROOT}/${ehome} ]] ; then
261 einfo " - Creating ${ehome} in ${ROOT}"
262 mkdir -p "${ROOT}/${ehome}"
263 chown "${euser}" "${ROOT}/${ehome}"
264 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 _assert_pkg_ebuild_phase ${FUNCNAME}
277
278 # get the group
279 local egroup=$1; shift
280 if [[ -z ${egroup} ]] ; then
281 eerror "No group specified !"
282 die "Cannot call enewgroup without a group"
283 fi
284
285 # see if group already exists
286 if [[ -n $(egetent group "${egroup}") ]] ; then
287 return 0
288 fi
289 einfo "Adding group '${egroup}' to your system ..."
290
291 # handle gid
292 local egid=$1; shift
293 if [[ ! -z ${egid} ]] ; then
294 if [[ ${egid} -gt 0 ]] ; then
295 if [[ -n $(egetent group ${egid}) ]] ; then
296 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 if [[ $# -gt 0 ]] ; then
309 die "extra arguments no longer supported; please file a bug"
310 fi
311
312 # 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 # add the group
323 case ${CHOST} in
324 *-darwin*)
325 _enewgroup_next_gid
326 dscl . create "/groups/${egroup}" gid ${egid}
327 dscl . create "/groups/${egroup}" passwd '*'
328 ;;
329
330 *-freebsd*|*-dragonfly*)
331 _enewgroup_next_gid
332 pw groupadd "${egroup}" -g ${egid} || die
333 ;;
334
335 *-netbsd*)
336 _enewgroup_next_gid
337 groupadd -g ${egid} "${egroup}" || die
338 ;;
339
340 *)
341 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 # We specify -r so that we get a GID in the system range from login.defs
349 groupadd -r ${opts} "${egroup}" || die
350 ;;
351 esac
352 }
353
354 # @FUNCTION: egethome
355 # @USAGE: <user>
356 # @DESCRIPTION:
357 # Gets the home directory for the specified user.
358 egethome() {
359 local pos
360
361 [[ $# -eq 1 ]] || die "usage: egethome <user>"
362
363 case ${CHOST} in
364 *-darwin*|*-freebsd*|*-dragonfly*)
365 pos=9
366 ;;
367 *) # Linux, NetBSD, OpenBSD, etc...
368 pos=6
369 ;;
370 esac
371
372 egetent passwd "$1" | cut -d: -f${pos}
373 }
374
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