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

Contents of /eclass/user.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.14 - (hide annotations) (download)
Sat Nov 26 07:20:31 2011 UTC (2 years, 7 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 vapier 1.1 # Copyright 1999-2011 Gentoo Foundation
2     # Distributed under the terms of the GNU General Public License v2
3 vapier 1.14 # $Header: /var/cvsroot/gentoo-x86/eclass/user.eclass,v 1.13 2011/11/26 06:50:27 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 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 vapier 1.14 local opts=()
123 vapier 1.1
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.14 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 vapier 1.13 for eshell in /sbin/nologin /usr/sbin/nologin /bin/false /usr/bin/false /dev/null ; do
159     [[ -x ${ROOT}${eshell} ]] && break
160 vapier 1.1 done
161    
162 vapier 1.13 if [[ ${eshell} == "/dev/null" ]] ; then
163 vapier 1.1 eerror "Unable to identify the shell to use, proceeding with userland default."
164     case ${USERLAND} in
165 vapier 1.13 GNU) eshell="/bin/false" ;;
166     BSD) eshell="/sbin/nologin" ;;
167     Darwin) eshell="/usr/sbin/nologin" ;;
168 vapier 1.1 *) die "Unable to identify the default shell for userland ${USERLAND}"
169     esac
170     fi
171     fi
172     einfo " - Shell: ${eshell}"
173 vapier 1.14 opts+=( -s "${eshell}" )
174 vapier 1.1
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 vapier 1.14 opts+=( -d "${ehome}" )
182 vapier 1.1
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 vapier 1.14 opts+=( -g "${defgroup}" )
206 vapier 1.1 if [[ ! -z ${exgroups} ]] ; then
207 vapier 1.14 opts+=( -G "${exgroups:1}" )
208 vapier 1.1 fi
209     else
210     egroups="(none)"
211     fi
212     einfo " - Groups: ${egroups}"
213    
214 vapier 1.9 # handle extra args
215     if [[ $# -gt 0 ]] ; then
216     die "extra arguments no longer supported; please file a bug"
217     else
218 vapier 1.14 local comment="added by portage for ${PN}"
219     opts+=( -c "${comment}" )
220     einfo " - GECOS: ${comment}"
221 vapier 1.9 fi
222    
223     # add the user
224 vapier 1.1 case ${CHOST} in
225     *-darwin*)
226     ### Make the user
227 vapier 1.14 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 vapier 1.9 ### Add the user to the groups specified
232 vapier 1.14 local g oldifs=${IFS}
233 vapier 1.9 export IFS=","
234     for g in ${egroups} ; do
235 vapier 1.14 dscl . merge "/groups/${g}" users "${euser}"
236 vapier 1.9 done
237     export IFS=${oldifs}
238 vapier 1.1 ;;
239 vapier 1.9
240 vapier 1.1 *-freebsd*|*-dragonfly*)
241 vapier 1.14 pw useradd "${euser}" "${opts[@]}" || die
242 vapier 1.1 ;;
243    
244     *-netbsd*)
245 vapier 1.14 useradd "${opts[@]}" "${euser}" || die
246 vapier 1.1 ;;
247    
248     *-openbsd*)
249 vapier 1.9 # all ops the same, except the -g vs -g/-G ...
250 vapier 1.14 useradd -u ${euid} -s "${eshell}" \
251     -d "${ehome}" -g "${egroups}" "${euser}" || die
252 vapier 1.1 ;;
253    
254     *)
255 vapier 1.14 useradd -r "${opts[@]}" "${euser}" || die
256 vapier 1.1 ;;
257     esac
258    
259     if [[ ! -e ${ROOT}/${ehome} ]] ; then
260     einfo " - Creating ${ehome} in ${ROOT}"
261     mkdir -p "${ROOT}/${ehome}"
262 vapier 1.14 chown "${euser}" "${ROOT}/${ehome}"
263 vapier 1.1 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 vapier 1.2 _assert_pkg_ebuild_phase enewgroup
276 vapier 1.1
277     # get the group
278 vapier 1.14 local egroup=$1; shift
279     if [[ -z ${egroup} ]] ; then
280 vapier 1.1 eerror "No group specified !"
281     die "Cannot call enewgroup without a group"
282     fi
283    
284     # see if group already exists
285 vapier 1.14 if [[ -n $(egetent group "${egroup}") ]] ; then
286 vapier 1.1 return 0
287     fi
288     einfo "Adding group '${egroup}' to your system ..."
289    
290     # handle gid
291 vapier 1.14 local egid=$1; shift
292     if [[ ! -z ${egid} ]] ; then
293     if [[ ${egid} -gt 0 ]] ; then
294     if [[ -n $(egetent group ${egid}) ]] ; then
295 vapier 1.1 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 vapier 1.14 if [[ $# -gt 0 ]] ; then
308 vapier 1.9 die "extra arguments no longer supported; please file a bug"
309     fi
310 vapier 1.1
311 vapier 1.14 # 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 vapier 1.1 # add the group
322     case ${CHOST} in
323     *-darwin*)
324 vapier 1.14 _enewgroup_next_gid
325     dscl . create "/groups/${egroup}" gid ${egid}
326     dscl . create "/groups/${egroup}" passwd '*'
327 vapier 1.1 ;;
328    
329     *-freebsd*|*-dragonfly*)
330 vapier 1.14 _enewgroup_next_gid
331     pw groupadd "${egroup}" -g ${egid} || die
332 vapier 1.1 ;;
333    
334     *-netbsd*)
335 vapier 1.14 _enewgroup_next_gid
336     groupadd -g ${egid} "${egroup}" || die
337 vapier 1.1 ;;
338    
339     *)
340 vapier 1.14 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 vapier 1.1 # We specify -r so that we get a GID in the system range from login.defs
348 vapier 1.14 groupadd -r ${opts} "${egroup}" || die
349 vapier 1.1 ;;
350     esac
351     }
352 vapier 1.6
353     # @FUNCTION: egethome
354     # @USAGE: <user>
355     # @DESCRIPTION:
356     # Gets the home directory for the specified user.
357     egethome() {
358     local pos
359    
360 vapier 1.7 [[ $# -eq 1 ]] || die "usage: egethome <user>"
361    
362 vapier 1.6 case ${CHOST} in
363     *-darwin*|*-freebsd*|*-dragonfly*)
364     pos=9
365     ;;
366     *) # Linux, NetBSD, OpenBSD, etc...
367     pos=6
368     ;;
369     esac
370    
371 vapier 1.14 egetent passwd "$1" | cut -d: -f${pos}
372 vapier 1.6 }
373 vapier 1.8
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