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

Contents of /eclass/user.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.21 - (hide annotations) (download)
Fri Jun 22 18:57:33 2012 UTC (2 years ago) by axs
Branch: MAIN
Changes since 1.20: +11 -4 lines
esethome: eerror and not die when home dir cannot be updated, due to for instance user being in use

1 axs 1.19 # Copyright 1999-2012 Gentoo Foundation
2 vapier 1.1 # Distributed under the terms of the GNU General Public License v2
3 axs 1.21 # $Header: /var/cvsroot/gentoo-x86/eclass/user.eclass,v 1.20 2012/06/22 15:14:10 axs 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.18 if [[ ${___ECLASS_ONCE_USER} != "recur -_+^+_- spank" ]] ; then
17     ___ECLASS_ONCE_USER="recur -_+^+_- spank"
18    
19 vapier 1.2 # @FUNCTION: _assert_pkg_ebuild_phase
20     # @INTERNAL
21     # @USAGE: <calling func name>
22     _assert_pkg_ebuild_phase() {
23     case ${EBUILD_PHASE} in
24 vapier 1.11 setup|preinst|postinst) ;;
25     *)
26 vapier 1.15 eerror "'$1()' called from '${EBUILD_PHASE}' phase which is not OK:"
27     eerror "You may only call from pkg_{setup,preinst,postinst} functions."
28 vapier 1.2 eerror "Package fails at QA and at life. Please file a bug."
29 vapier 1.15 die "Bad package! $1 is only for use in some pkg_* functions!"
30 vapier 1.2 esac
31     }
32    
33 vapier 1.1 # @FUNCTION: egetent
34     # @USAGE: <database> <key>
35     # @DESCRIPTION:
36     # Small wrapper for getent (Linux), nidump (< Mac OS X 10.5),
37     # dscl (Mac OS X 10.5), and pw (FreeBSD) used in enewuser()/enewgroup().
38 vapier 1.5 #
39     # Supported databases: group passwd
40 vapier 1.1 egetent() {
41 vapier 1.5 local db=$1 key=$2
42    
43     [[ $# -ge 3 ]] && die "usage: egetent <database> <key>"
44    
45     case ${db} in
46     passwd|group) ;;
47     *) die "sorry, database '${db}' not yet supported; file a bug" ;;
48     esac
49    
50 vapier 1.1 case ${CHOST} in
51     *-darwin[678])
52 vapier 1.5 case ${key} in
53 vapier 1.1 *[!0-9]*) # Non numeric
54 vapier 1.5 nidump ${db} . | awk -F: "(\$1 ~ /^${key}\$/) {print;exit;}"
55 vapier 1.1 ;;
56     *) # Numeric
57 vapier 1.5 nidump ${db} . | awk -F: "(\$3 == ${key}) {print;exit;}"
58 vapier 1.1 ;;
59     esac
60     ;;
61     *-darwin*)
62 vapier 1.5 local mykey
63     case ${db} in
64     passwd) db="Users" mykey="UniqueID" ;;
65     group) db="Groups" mykey="PrimaryGroupID" ;;
66     esac
67    
68     case ${key} in
69 vapier 1.1 *[!0-9]*) # Non numeric
70 vapier 1.5 dscl . -read /${db}/${key} 2>/dev/null |grep RecordName
71 vapier 1.1 ;;
72     *) # Numeric
73 vapier 1.5 dscl . -search /${db} ${mykey} ${key} 2>/dev/null
74 vapier 1.1 ;;
75     esac
76     ;;
77     *-freebsd*|*-dragonfly*)
78 vapier 1.5 case ${db} in
79     passwd) db="user" ;;
80     *) ;;
81     esac
82 vapier 1.1
83     # lookup by uid/gid
84 vapier 1.5 local opts
85     if [[ ${key} == [[:digit:]]* ]] ; then
86 naota 1.10 [[ ${db} == "user" ]] && opts="-u" || opts="-g"
87 vapier 1.1 fi
88    
89 naota 1.10 pw show ${db} ${opts} "${key}" -q
90 vapier 1.1 ;;
91     *-netbsd*|*-openbsd*)
92 vapier 1.5 grep "${key}:\*:" /etc/${db}
93 vapier 1.1 ;;
94     *)
95 vapier 1.3 # ignore output if nscd doesn't exist, or we're not running as root
96 vapier 1.5 nscd -i "${db}" 2>/dev/null
97     getent "${db}" "${key}"
98 vapier 1.1 ;;
99     esac
100     }
101    
102     # @FUNCTION: enewuser
103 vapier 1.9 # @USAGE: <user> [uid] [shell] [homedir] [groups]
104 vapier 1.1 # @DESCRIPTION:
105     # Same as enewgroup, you are not required to understand how to properly add
106     # a user to the system. The only required parameter is the username.
107     # Default uid is (pass -1 for this) next available, default shell is
108 vapier 1.9 # /bin/false, default homedir is /dev/null, and there are no default groups.
109 vapier 1.1 enewuser() {
110 vapier 1.15 _assert_pkg_ebuild_phase ${FUNCNAME}
111 vapier 1.1
112     # get the username
113     local euser=$1; shift
114     if [[ -z ${euser} ]] ; then
115     eerror "No username specified !"
116     die "Cannot call enewuser without a username"
117     fi
118    
119     # lets see if the username already exists
120     if [[ -n $(egetent passwd "${euser}") ]] ; then
121     return 0
122     fi
123     einfo "Adding user '${euser}' to your system ..."
124    
125     # options to pass to useradd
126 vapier 1.14 local opts=()
127 vapier 1.1
128     # handle uid
129     local euid=$1; shift
130     if [[ -n ${euid} && ${euid} != -1 ]] ; then
131     if [[ ${euid} -gt 0 ]] ; then
132     if [[ -n $(egetent passwd ${euid}) ]] ; then
133     euid="next"
134     fi
135     else
136     eerror "Userid given but is not greater than 0 !"
137     die "${euid} is not a valid UID"
138     fi
139     else
140     euid="next"
141     fi
142     if [[ ${euid} == "next" ]] ; then
143     for ((euid = 101; euid <= 999; euid++)); do
144     [[ -z $(egetent passwd ${euid}) ]] && break
145     done
146     fi
147 vapier 1.14 opts+=( -u ${euid} )
148 vapier 1.1 einfo " - Userid: ${euid}"
149    
150     # handle shell
151     local eshell=$1; shift
152     if [[ ! -z ${eshell} ]] && [[ ${eshell} != "-1" ]] ; then
153     if [[ ! -e ${ROOT}${eshell} ]] ; then
154     eerror "A shell was specified but it does not exist !"
155     die "${eshell} does not exist in ${ROOT}"
156     fi
157     if [[ ${eshell} == */false || ${eshell} == */nologin ]] ; then
158     eerror "Do not specify ${eshell} yourself, use -1"
159     die "Pass '-1' as the shell parameter"
160     fi
161     else
162 vapier 1.13 for eshell in /sbin/nologin /usr/sbin/nologin /bin/false /usr/bin/false /dev/null ; do
163     [[ -x ${ROOT}${eshell} ]] && break
164 vapier 1.1 done
165    
166 vapier 1.13 if [[ ${eshell} == "/dev/null" ]] ; then
167 vapier 1.1 eerror "Unable to identify the shell to use, proceeding with userland default."
168     case ${USERLAND} in
169 vapier 1.13 GNU) eshell="/bin/false" ;;
170     BSD) eshell="/sbin/nologin" ;;
171     Darwin) eshell="/usr/sbin/nologin" ;;
172 vapier 1.1 *) die "Unable to identify the default shell for userland ${USERLAND}"
173     esac
174     fi
175     fi
176     einfo " - Shell: ${eshell}"
177 vapier 1.14 opts+=( -s "${eshell}" )
178 vapier 1.1
179     # handle homedir
180     local ehome=$1; shift
181     if [[ -z ${ehome} ]] || [[ ${ehome} == "-1" ]] ; then
182     ehome="/dev/null"
183     fi
184     einfo " - Home: ${ehome}"
185 vapier 1.14 opts+=( -d "${ehome}" )
186 vapier 1.1
187     # handle groups
188     local egroups=$1; shift
189 vapier 1.16 local g egroups_arr
190     IFS="," read -r -a egroups_arr <<<"${egroups}"
191     shift
192     if [[ ${#egroups_arr[@]} -gt 0 ]] ; then
193     local defgroup exgroups
194     for g in "${egroups_arr[@]}" ; do
195 vapier 1.1 if [[ -z $(egetent group "${g}") ]] ; then
196     eerror "You must add group ${g} to the system first"
197     die "${g} is not a valid GID"
198     fi
199     if [[ -z ${defgroup} ]] ; then
200     defgroup=${g}
201     else
202 vapier 1.16 exgroups+=",${g}"
203 vapier 1.1 fi
204     done
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     fi
210 vapier 1.16 einfo " - Groups: ${egroups:-(none)}"
211 vapier 1.1
212 vapier 1.9 # handle extra args
213     if [[ $# -gt 0 ]] ; then
214     die "extra arguments no longer supported; please file a bug"
215     else
216 vapier 1.14 local comment="added by portage for ${PN}"
217     opts+=( -c "${comment}" )
218     einfo " - GECOS: ${comment}"
219 vapier 1.9 fi
220    
221     # add the user
222 vapier 1.1 case ${CHOST} in
223     *-darwin*)
224     ### Make the user
225 vapier 1.14 dscl . create "/users/${euser}" uid ${euid}
226     dscl . create "/users/${euser}" shell "${eshell}"
227     dscl . create "/users/${euser}" home "${ehome}"
228     dscl . create "/users/${euser}" realname "added by portage for ${PN}"
229 vapier 1.9 ### Add the user to the groups specified
230 vapier 1.16 for g in "${egroups_arr[@]}" ; do
231 vapier 1.14 dscl . merge "/groups/${g}" users "${euser}"
232 vapier 1.9 done
233 vapier 1.1 ;;
234 vapier 1.9
235 vapier 1.1 *-freebsd*|*-dragonfly*)
236 vapier 1.14 pw useradd "${euser}" "${opts[@]}" || die
237 vapier 1.1 ;;
238    
239     *-netbsd*)
240 vapier 1.14 useradd "${opts[@]}" "${euser}" || die
241 vapier 1.1 ;;
242    
243     *-openbsd*)
244 vapier 1.9 # all ops the same, except the -g vs -g/-G ...
245 vapier 1.14 useradd -u ${euid} -s "${eshell}" \
246     -d "${ehome}" -g "${egroups}" "${euser}" || die
247 vapier 1.1 ;;
248    
249     *)
250 vapier 1.14 useradd -r "${opts[@]}" "${euser}" || die
251 vapier 1.1 ;;
252     esac
253    
254     if [[ ! -e ${ROOT}/${ehome} ]] ; then
255     einfo " - Creating ${ehome} in ${ROOT}"
256     mkdir -p "${ROOT}/${ehome}"
257 vapier 1.14 chown "${euser}" "${ROOT}/${ehome}"
258 vapier 1.1 chmod 755 "${ROOT}/${ehome}"
259     fi
260     }
261    
262     # @FUNCTION: enewgroup
263     # @USAGE: <group> [gid]
264     # @DESCRIPTION:
265     # This function does not require you to understand how to properly add a
266     # group to the system. Just give it a group name to add and enewgroup will
267     # do the rest. You may specify the gid for the group or allow the group to
268     # allocate the next available one.
269     enewgroup() {
270 vapier 1.15 _assert_pkg_ebuild_phase ${FUNCNAME}
271 vapier 1.1
272     # get the group
273 vapier 1.14 local egroup=$1; shift
274     if [[ -z ${egroup} ]] ; then
275 vapier 1.1 eerror "No group specified !"
276     die "Cannot call enewgroup without a group"
277     fi
278    
279     # see if group already exists
280 vapier 1.14 if [[ -n $(egetent group "${egroup}") ]] ; then
281 vapier 1.1 return 0
282     fi
283     einfo "Adding group '${egroup}' to your system ..."
284    
285     # handle gid
286 vapier 1.14 local egid=$1; shift
287     if [[ ! -z ${egid} ]] ; then
288     if [[ ${egid} -gt 0 ]] ; then
289     if [[ -n $(egetent group ${egid}) ]] ; then
290 vapier 1.1 egid="next available; requested gid taken"
291     fi
292     else
293     eerror "Groupid given but is not greater than 0 !"
294     die "${egid} is not a valid GID"
295     fi
296     else
297     egid="next available"
298     fi
299     einfo " - Groupid: ${egid}"
300    
301     # handle extra
302 vapier 1.14 if [[ $# -gt 0 ]] ; then
303 vapier 1.9 die "extra arguments no longer supported; please file a bug"
304     fi
305 vapier 1.1
306 vapier 1.14 # Some targets need to find the next available GID manually
307     _enewgroup_next_gid() {
308     if [[ ${egid} == *[!0-9]* ]] ; then
309 vapier 1.17 # Non numeric
310 vapier 1.14 for ((egid = 101; egid <= 999; egid++)) ; do
311     [[ -z $(egetent group ${egid}) ]] && break
312     done
313     fi
314     }
315    
316 vapier 1.1 # add the group
317     case ${CHOST} in
318     *-darwin*)
319 vapier 1.14 _enewgroup_next_gid
320     dscl . create "/groups/${egroup}" gid ${egid}
321     dscl . create "/groups/${egroup}" passwd '*'
322 vapier 1.1 ;;
323    
324     *-freebsd*|*-dragonfly*)
325 vapier 1.14 _enewgroup_next_gid
326     pw groupadd "${egroup}" -g ${egid} || die
327 vapier 1.1 ;;
328    
329     *-netbsd*)
330 vapier 1.14 _enewgroup_next_gid
331     groupadd -g ${egid} "${egroup}" || die
332 vapier 1.1 ;;
333    
334     *)
335 vapier 1.14 local opts
336     if [[ ${egid} == *[!0-9]* ]] ; then
337 vapier 1.17 # Non numeric; let groupadd figure out a GID for us
338 vapier 1.14 opts=""
339     else
340     opts="-g ${egid}"
341     fi
342 vapier 1.1 # We specify -r so that we get a GID in the system range from login.defs
343 vapier 1.14 groupadd -r ${opts} "${egroup}" || die
344 vapier 1.1 ;;
345     esac
346     }
347 vapier 1.6
348     # @FUNCTION: egethome
349     # @USAGE: <user>
350     # @DESCRIPTION:
351     # Gets the home directory for the specified user.
352     egethome() {
353     local pos
354    
355 vapier 1.7 [[ $# -eq 1 ]] || die "usage: egethome <user>"
356    
357 vapier 1.6 case ${CHOST} in
358     *-darwin*|*-freebsd*|*-dragonfly*)
359     pos=9
360     ;;
361     *) # Linux, NetBSD, OpenBSD, etc...
362     pos=6
363     ;;
364     esac
365    
366 vapier 1.14 egetent passwd "$1" | cut -d: -f${pos}
367 vapier 1.6 }
368 vapier 1.8
369     # @FUNCTION: egetshell
370     # @USAGE: <user>
371     # @DESCRIPTION:
372     # Gets the shell for the specified user.
373     egetshell() {
374     local pos
375    
376     [[ $# -eq 1 ]] || die "usage: egetshell <user>"
377    
378     case ${CHOST} in
379     *-darwin*|*-freebsd*|*-dragonfly*)
380     pos=10
381     ;;
382     *) # Linux, NetBSD, OpenBSD, etc...
383     pos=7
384     ;;
385     esac
386    
387     egetent passwd "$1" | cut -d: -f${pos}
388     }
389 vapier 1.18
390 axs 1.19 # @FUNCTION: esethome
391     # @USAGE: <user> <homedir>
392     # @DESCRIPTION:
393     # Update the home directory in a platform-agnostic way.
394     # Required parameters is the username and the new home directory.
395     # Specify -1 if you want to set home to the enewuser default
396     # of /dev/null.
397     # If the new home directory does not exist, it is created.
398     # Any previously existing home directory is NOT moved.
399     esethome() {
400     _assert_pkg_ebuild_phase ${FUNCNAME}
401    
402     # get the username
403     local euser=$1; shift
404     if [[ -z ${euser} ]] ; then
405     eerror "No username specified !"
406     die "Cannot call esethome without a username"
407     fi
408    
409     # lets see if the username already exists
410     if [[ -z $(egetent passwd "${euser}") ]] ; then
411     ewarn "User does not exist, cannot set home dir -- skipping."
412     return 1
413     fi
414    
415     # handle homedir
416     local ehome=$1; shift
417     if [[ -z ${ehome} ]] ; then
418     eerror "No home directory specified !"
419     die "Cannot call esethome without a home directory or '-1'"
420     fi
421    
422     if [[ ${ehome} == "-1" ]] ; then
423     ehome="/dev/null"
424     fi
425     einfo " - Home: ${ehome}"
426    
427 axs 1.20 # ensure home directory exists, otherwise update will fail
428     if [[ ! -e ${ROOT}/${ehome} ]] ; then
429     einfo " - Creating ${ehome} in ${ROOT}"
430     mkdir -p "${ROOT}/${ehome}"
431     chown "${euser}" "${ROOT}/${ehome}"
432     chmod 755 "${ROOT}/${ehome}"
433     fi
434    
435 axs 1.19 # update the home directory
436     case ${CHOST} in
437     *-darwin*)
438     dscl . change "/users/${euser}" home "${ehome}"
439     ;;
440    
441     *-freebsd*|*-dragonfly*)
442 axs 1.21 pw usermod "${euser}" -d "${ehome}" && return 0
443     [[ $? == 8 ]] && eerror "${euser} is in use, cannot update home"
444     eerror "There was an error when attempting to update the home directory for ${euser}"
445     eerror "Please update it manually on your system:"
446     eerror "\t pw usermod \"${euser}\" -d \"${ehome}\""
447 axs 1.19 ;;
448    
449     *)
450 axs 1.21 usermod -d "${ehome}" "${euser}" && return 0
451     [[ $? == 8 ]] && eerror "${euser} is in use, cannot update home"
452     eerror "There was an error when attempting to update the home directory for ${euser}"
453     eerror "Please update it manually on your system (as root):"
454     eerror "\t usermod -d \"${ehome}\" \"${euser}\""
455 axs 1.19 ;;
456     esac
457     }
458    
459 vapier 1.18 fi

  ViewVC Help
Powered by ViewVC 1.1.20