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

Contents of /eclass/user.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.14 - (show annotations) (download)
Sat Nov 26 07:20:31 2011 UTC (2 years, 9 months ago) by vapier
Branch: MAIN
Changes since 1.13: +56 -67 lines
rewrite a bit of code to quote all user/group/shell/home strings

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.13 2011/11/26 06:50:27 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 local comment="added by portage for ${PN}"
219 opts+=( -c "${comment}" )
220 einfo " - GECOS: ${comment}"
221 fi
222
223 # add the user
224 case ${CHOST} in
225 *-darwin*)
226 ### Make the user
227 dscl . create "/users/${euser}" uid ${euid}
228 dscl . create "/users/${euser}" shell "${eshell}"
229 dscl . create "/users/${euser}" home "${ehome}"
230 dscl . create "/users/${euser}" realname "added by portage for ${PN}"
231 ### Add the user to the groups specified
232 local g oldifs=${IFS}
233 export IFS=","
234 for g in ${egroups} ; do
235 dscl . merge "/groups/${g}" users "${euser}"
236 done
237 export IFS=${oldifs}
238 ;;
239
240 *-freebsd*|*-dragonfly*)
241 pw useradd "${euser}" "${opts[@]}" || die
242 ;;
243
244 *-netbsd*)
245 useradd "${opts[@]}" "${euser}" || die
246 ;;
247
248 *-openbsd*)
249 # all ops the same, except the -g vs -g/-G ...
250 useradd -u ${euid} -s "${eshell}" \
251 -d "${ehome}" -g "${egroups}" "${euser}" || die
252 ;;
253
254 *)
255 useradd -r "${opts[@]}" "${euser}" || die
256 ;;
257 esac
258
259 if [[ ! -e ${ROOT}/${ehome} ]] ; then
260 einfo " - Creating ${ehome} in ${ROOT}"
261 mkdir -p "${ROOT}/${ehome}"
262 chown "${euser}" "${ROOT}/${ehome}"
263 chmod 755 "${ROOT}/${ehome}"
264 fi
265 }
266
267 # @FUNCTION: enewgroup
268 # @USAGE: <group> [gid]
269 # @DESCRIPTION:
270 # This function does not require you to understand how to properly add a
271 # group to the system. Just give it a group name to add and enewgroup will
272 # do the rest. You may specify the gid for the group or allow the group to
273 # allocate the next available one.
274 enewgroup() {
275 _assert_pkg_ebuild_phase enewgroup
276
277 # get the group
278 local egroup=$1; shift
279 if [[ -z ${egroup} ]] ; 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 # handle gid
291 local egid=$1; shift
292 if [[ ! -z ${egid} ]] ; then
293 if [[ ${egid} -gt 0 ]] ; then
294 if [[ -n $(egetent group ${egid}) ]] ; then
295 egid="next available; requested gid taken"
296 fi
297 else
298 eerror "Groupid given but is not greater than 0 !"
299 die "${egid} is not a valid GID"
300 fi
301 else
302 egid="next available"
303 fi
304 einfo " - Groupid: ${egid}"
305
306 # handle extra
307 if [[ $# -gt 0 ]] ; then
308 die "extra arguments no longer supported; please file a bug"
309 fi
310
311 # Some targets need to find the next available GID manually
312 _enewgroup_next_gid() {
313 if [[ ${egid} == *[!0-9]* ]] ; then
314 # Non numeric
315 for ((egid = 101; egid <= 999; egid++)) ; do
316 [[ -z $(egetent group ${egid}) ]] && break
317 done
318 fi
319 }
320
321 # add the group
322 case ${CHOST} in
323 *-darwin*)
324 _enewgroup_next_gid
325 dscl . create "/groups/${egroup}" gid ${egid}
326 dscl . create "/groups/${egroup}" passwd '*'
327 ;;
328
329 *-freebsd*|*-dragonfly*)
330 _enewgroup_next_gid
331 pw groupadd "${egroup}" -g ${egid} || die
332 ;;
333
334 *-netbsd*)
335 _enewgroup_next_gid
336 groupadd -g ${egid} "${egroup}" || die
337 ;;
338
339 *)
340 local opts
341 if [[ ${egid} == *[!0-9]* ]] ; then
342 # Non numeric; let groupadd figure out a GID for us
343 opts=""
344 else
345 opts="-g ${egid}"
346 fi
347 # We specify -r so that we get a GID in the system range from login.defs
348 groupadd -r ${opts} "${egroup}" || die
349 ;;
350 esac
351 }
352
353 # @FUNCTION: egethome
354 # @USAGE: <user>
355 # @DESCRIPTION:
356 # Gets the home directory for the specified user.
357 egethome() {
358 local pos
359
360 [[ $# -eq 1 ]] || die "usage: egethome <user>"
361
362 case ${CHOST} in
363 *-darwin*|*-freebsd*|*-dragonfly*)
364 pos=9
365 ;;
366 *) # Linux, NetBSD, OpenBSD, etc...
367 pos=6
368 ;;
369 esac
370
371 egetent passwd "$1" | cut -d: -f${pos}
372 }
373
374 # @FUNCTION: egetshell
375 # @USAGE: <user>
376 # @DESCRIPTION:
377 # Gets the shell for the specified user.
378 egetshell() {
379 local pos
380
381 [[ $# -eq 1 ]] || die "usage: egetshell <user>"
382
383 case ${CHOST} in
384 *-darwin*|*-freebsd*|*-dragonfly*)
385 pos=10
386 ;;
387 *) # Linux, NetBSD, OpenBSD, etc...
388 pos=7
389 ;;
390 esac
391
392 egetent passwd "$1" | cut -d: -f${pos}
393 }

  ViewVC Help
Powered by ViewVC 1.1.20