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

Contents of /eclass/user.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.11 - (hide annotations) (download)
Sat Nov 26 06:42:07 2011 UTC (2 years, 4 months ago) by vapier
Branch: MAIN
Changes since 1.10: +3 -2 lines
restrict enew{user,group} to pkg_{setup,preinst,postinst}

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

  ViewVC Help
Powered by ViewVC 1.1.20