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

Contents of /eclass/user.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.10 - (hide annotations) (download)
Fri Nov 4 13:08:23 2011 UTC (2 years, 9 months ago) by naota
Branch: MAIN
Changes since 1.9: +3 -3 lines
Change possible mis-used ${action} to ${db}

1 vapier 1.1 # Copyright 1999-2011 Gentoo Foundation
2     # Distributed under the terms of the GNU General Public License v2
3 naota 1.10 # $Header: /var/cvsroot/gentoo-x86/eclass/user.eclass,v 1.9 2011/11/03 15:29:39 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     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 vapier 1.1 # @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 vapier 1.5 #
34     # Supported databases: group passwd
35 vapier 1.1 egetent() {
36 vapier 1.5 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 vapier 1.1 case ${CHOST} in
46     *-darwin[678])
47 vapier 1.5 case ${key} in
48 vapier 1.1 *[!0-9]*) # Non numeric
49 vapier 1.5 nidump ${db} . | awk -F: "(\$1 ~ /^${key}\$/) {print;exit;}"
50 vapier 1.1 ;;
51     *) # Numeric
52 vapier 1.5 nidump ${db} . | awk -F: "(\$3 == ${key}) {print;exit;}"
53 vapier 1.1 ;;
54     esac
55     ;;
56     *-darwin*)
57 vapier 1.5 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 vapier 1.1 *[!0-9]*) # Non numeric
65 vapier 1.5 dscl . -read /${db}/${key} 2>/dev/null |grep RecordName
66 vapier 1.1 ;;
67     *) # Numeric
68 vapier 1.5 dscl . -search /${db} ${mykey} ${key} 2>/dev/null
69 vapier 1.1 ;;
70     esac
71     ;;
72     *-freebsd*|*-dragonfly*)
73 vapier 1.5 case ${db} in
74     passwd) db="user" ;;
75     *) ;;
76     esac
77 vapier 1.1
78     # lookup by uid/gid
79 vapier 1.5 local opts
80     if [[ ${key} == [[:digit:]]* ]] ; then
81 naota 1.10 [[ ${db} == "user" ]] && opts="-u" || opts="-g"
82 vapier 1.1 fi
83    
84 naota 1.10 pw show ${db} ${opts} "${key}" -q
85 vapier 1.1 ;;
86     *-netbsd*|*-openbsd*)
87 vapier 1.5 grep "${key}:\*:" /etc/${db}
88 vapier 1.1 ;;
89     *)
90 vapier 1.3 # ignore output if nscd doesn't exist, or we're not running as root
91 vapier 1.5 nscd -i "${db}" 2>/dev/null
92     getent "${db}" "${key}"
93 vapier 1.1 ;;
94     esac
95     }
96    
97     # @FUNCTION: enewuser
98 vapier 1.9 # @USAGE: <user> [uid] [shell] [homedir] [groups]
99 vapier 1.1 # @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 vapier 1.9 # /bin/false, default homedir is /dev/null, and there are no default groups.
104 vapier 1.1 enewuser() {
105 vapier 1.2 _assert_pkg_ebuild_phase enewuser
106 vapier 1.1
107     # get the username
108     local euser=$1; shift
109     if [[ -z ${euser} ]] ; then
110     eerror "No username specified !"
111     die "Cannot call enewuser without a username"
112     fi
113    
114     # lets see if the username already exists
115     if [[ -n $(egetent passwd "${euser}") ]] ; then
116     return 0
117     fi
118     einfo "Adding user '${euser}' to your system ..."
119    
120     # options to pass to useradd
121     local opts=
122    
123     # handle uid
124     local euid=$1; shift
125     if [[ -n ${euid} && ${euid} != -1 ]] ; then
126     if [[ ${euid} -gt 0 ]] ; then
127     if [[ -n $(egetent passwd ${euid}) ]] ; then
128     euid="next"
129     fi
130     else
131     eerror "Userid given but is not greater than 0 !"
132     die "${euid} is not a valid UID"
133     fi
134     else
135     euid="next"
136     fi
137     if [[ ${euid} == "next" ]] ; then
138     for ((euid = 101; euid <= 999; euid++)); do
139     [[ -z $(egetent passwd ${euid}) ]] && break
140     done
141     fi
142 vapier 1.9 opts+=" -u ${euid}"
143 vapier 1.1 einfo " - Userid: ${euid}"
144    
145     # handle shell
146     local eshell=$1; shift
147     if [[ ! -z ${eshell} ]] && [[ ${eshell} != "-1" ]] ; then
148     if [[ ! -e ${ROOT}${eshell} ]] ; then
149     eerror "A shell was specified but it does not exist !"
150     die "${eshell} does not exist in ${ROOT}"
151     fi
152     if [[ ${eshell} == */false || ${eshell} == */nologin ]] ; then
153     eerror "Do not specify ${eshell} yourself, use -1"
154     die "Pass '-1' as the shell parameter"
155     fi
156     else
157     for shell in /sbin/nologin /usr/sbin/nologin /bin/false /usr/bin/false /dev/null ; do
158     [[ -x ${ROOT}${shell} ]] && break
159     done
160    
161     if [[ ${shell} == "/dev/null" ]] ; then
162     eerror "Unable to identify the shell to use, proceeding with userland default."
163     case ${USERLAND} in
164     GNU) shell="/bin/false" ;;
165     BSD) shell="/sbin/nologin" ;;
166     Darwin) shell="/usr/sbin/nologin" ;;
167     *) die "Unable to identify the default shell for userland ${USERLAND}"
168     esac
169     fi
170    
171     eshell=${shell}
172     fi
173     einfo " - Shell: ${eshell}"
174 vapier 1.9 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.9 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.9 opts+=" -g ${defgroup}"
207 vapier 1.1 if [[ ! -z ${exgroups} ]] ; then
208 vapier 1.9 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     set -- -c "added by portage for ${PN}"
220     einfo " - Extra: $@"
221     fi
222    
223     # add the user
224 vapier 1.1 local oldsandbox=${SANDBOX_ON}
225     export SANDBOX_ON="0"
226     case ${CHOST} in
227     *-darwin*)
228     ### Make the user
229 vapier 1.9 dscl . create /users/${euser} uid ${euid}
230     dscl . create /users/${euser} shell ${eshell}
231     dscl . create /users/${euser} home ${ehome}
232     dscl . create /users/${euser} realname "added by portage for ${PN}"
233     ### Add the user to the groups specified
234     local oldifs=${IFS}
235     export IFS=","
236     for g in ${egroups} ; do
237     dscl . merge /groups/${g} users ${euser}
238     done
239     export IFS=${oldifs}
240 vapier 1.1 ;;
241 vapier 1.9
242 vapier 1.1 *-freebsd*|*-dragonfly*)
243 vapier 1.9 pw useradd ${euser} ${opts} "$@" || die
244 vapier 1.1 ;;
245    
246     *-netbsd*)
247 vapier 1.9 useradd ${opts} ${euser} "$@" || die
248 vapier 1.1 ;;
249    
250     *-openbsd*)
251 vapier 1.9 # all ops the same, except the -g vs -g/-G ...
252     useradd -u ${euid} -s ${eshell} \
253     -d ${ehome} -g ${egroups} "$@" ${euser} || die
254 vapier 1.1 ;;
255    
256     *)
257 vapier 1.9 useradd -r ${opts} "$@" ${euser} || die
258 vapier 1.1 ;;
259     esac
260    
261     if [[ ! -e ${ROOT}/${ehome} ]] ; then
262     einfo " - Creating ${ehome} in ${ROOT}"
263     mkdir -p "${ROOT}/${ehome}"
264     chown ${euser} "${ROOT}/${ehome}"
265     chmod 755 "${ROOT}/${ehome}"
266     fi
267    
268     export SANDBOX_ON=${oldsandbox}
269     }
270    
271     # @FUNCTION: enewgroup
272     # @USAGE: <group> [gid]
273     # @DESCRIPTION:
274     # This function does not require you to understand how to properly add a
275     # group to the system. Just give it a group name to add and enewgroup will
276     # do the rest. You may specify the gid for the group or allow the group to
277     # allocate the next available one.
278     enewgroup() {
279 vapier 1.2 _assert_pkg_ebuild_phase enewgroup
280 vapier 1.1
281     # get the group
282     local egroup="$1"; shift
283     if [ -z "${egroup}" ]
284     then
285     eerror "No group specified !"
286     die "Cannot call enewgroup without a group"
287     fi
288    
289     # see if group already exists
290     if [[ -n $(egetent group "${egroup}") ]]; then
291     return 0
292     fi
293     einfo "Adding group '${egroup}' to your system ..."
294    
295     # options to pass to useradd
296     local opts=
297    
298     # handle gid
299     local egid="$1"; shift
300     if [ ! -z "${egid}" ]
301     then
302     if [ "${egid}" -gt 0 ]
303     then
304     if [ -z "`egetent group ${egid}`" ]
305     then
306     if [[ "${CHOST}" == *-darwin* ]]; then
307 vapier 1.9 opts+=" ${egid}"
308 vapier 1.1 else
309 vapier 1.9 opts+=" -g ${egid}"
310 vapier 1.1 fi
311     else
312     egid="next available; requested gid taken"
313     fi
314     else
315     eerror "Groupid given but is not greater than 0 !"
316     die "${egid} is not a valid GID"
317     fi
318     else
319     egid="next available"
320     fi
321     einfo " - Groupid: ${egid}"
322    
323     # handle extra
324 vapier 1.9 if [ $# -gt 0 ] ; then
325     die "extra arguments no longer supported; please file a bug"
326     fi
327 vapier 1.1
328     # add the group
329     local oldsandbox="${SANDBOX_ON}"
330     export SANDBOX_ON="0"
331     case ${CHOST} in
332     *-darwin*)
333     # If we need the next available
334     case ${egid} in
335     *[!0-9]*) # Non numeric
336     for ((egid = 101; egid <= 999; egid++)); do
337     [[ -z $(egetent group ${egid}) ]] && break
338     done
339     esac
340     dscl . create /groups/${egroup} gid ${egid}
341     dscl . create /groups/${egroup} passwd '*'
342     ;;
343    
344     *-freebsd*|*-dragonfly*)
345     case ${egid} in
346     *[!0-9]*) # Non numeric
347     for ((egid = 101; egid <= 999; egid++)); do
348     [[ -z $(egetent group ${egid}) ]] && break
349     done
350     esac
351 vapier 1.9 pw groupadd ${egroup} -g ${egid} || die
352 vapier 1.1 ;;
353    
354     *-netbsd*)
355     case ${egid} in
356     *[!0-9]*) # Non numeric
357     for ((egid = 101; egid <= 999; egid++)); do
358     [[ -z $(egetent group ${egid}) ]] && break
359     done
360     esac
361 vapier 1.9 groupadd -g ${egid} ${egroup} || die
362 vapier 1.1 ;;
363    
364     *)
365     # We specify -r so that we get a GID in the system range from login.defs
366 vapier 1.9 groupadd -r ${opts} ${egroup} || die
367 vapier 1.1 ;;
368     esac
369     export SANDBOX_ON="${oldsandbox}"
370     }
371 vapier 1.6
372     # @FUNCTION: egethome
373     # @USAGE: <user>
374     # @DESCRIPTION:
375     # Gets the home directory for the specified user.
376     egethome() {
377     local pos
378    
379 vapier 1.7 [[ $# -eq 1 ]] || die "usage: egethome <user>"
380    
381 vapier 1.6 case ${CHOST} in
382     *-darwin*|*-freebsd*|*-dragonfly*)
383     pos=9
384     ;;
385     *) # Linux, NetBSD, OpenBSD, etc...
386     pos=6
387     ;;
388     esac
389    
390     egetent passwd $1 | cut -d: -f${pos}
391     }
392 vapier 1.8
393     # @FUNCTION: egetshell
394     # @USAGE: <user>
395     # @DESCRIPTION:
396     # Gets the shell for the specified user.
397     egetshell() {
398     local pos
399    
400     [[ $# -eq 1 ]] || die "usage: egetshell <user>"
401    
402     case ${CHOST} in
403     *-darwin*|*-freebsd*|*-dragonfly*)
404     pos=10
405     ;;
406     *) # Linux, NetBSD, OpenBSD, etc...
407     pos=7
408     ;;
409     esac
410    
411     egetent passwd "$1" | cut -d: -f${pos}
412     }

  ViewVC Help
Powered by ViewVC 1.1.20