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

Diff of /eclass/user.eclass

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.13 Revision 1.21
1# Copyright 1999-2011 Gentoo Foundation 1# Copyright 1999-2012 Gentoo Foundation
2# Distributed under the terms of the GNU General Public License v2 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 $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/user.eclass,v 1.21 2012/06/22 18:57:33 axs Exp $
4 4
5# @ECLASS: user.eclass 5# @ECLASS: user.eclass
6# @MAINTAINER: 6# @MAINTAINER:
7# base-system@gentoo.org (Linux) 7# base-system@gentoo.org (Linux)
8# Joe Jezak <josejx@gmail.com> (OS X) 8# Joe Jezak <josejx@gmail.com> (OS X)
11# @BLURB: user management in ebuilds 11# @BLURB: user management in ebuilds
12# @DESCRIPTION: 12# @DESCRIPTION:
13# The user eclass contains a suite of functions that allow ebuilds 13# The user eclass contains a suite of functions that allow ebuilds
14# to quickly make sure users in the installed system are sane. 14# to quickly make sure users in the installed system are sane.
15 15
16if [[ ${___ECLASS_ONCE_USER} != "recur -_+^+_- spank" ]] ; then
17___ECLASS_ONCE_USER="recur -_+^+_- spank"
18
16# @FUNCTION: _assert_pkg_ebuild_phase 19# @FUNCTION: _assert_pkg_ebuild_phase
17# @INTERNAL 20# @INTERNAL
18# @USAGE: <calling func name> 21# @USAGE: <calling func name>
19_assert_pkg_ebuild_phase() { 22_assert_pkg_ebuild_phase() {
20 case ${EBUILD_PHASE} in 23 case ${EBUILD_PHASE} in
21 setup|preinst|postinst) ;; 24 setup|preinst|postinst) ;;
22 *) 25 *)
23 eerror "'$1()' called from '${EBUILD_PHASE}()' which is not a pkg_* function." 26 eerror "'$1()' called from '${EBUILD_PHASE}' phase which is not OK:"
27 eerror "You may only call from pkg_{setup,preinst,postinst} functions."
24 eerror "Package fails at QA and at life. Please file a bug." 28 eerror "Package fails at QA and at life. Please file a bug."
25 die "Bad package! $1 is only for use in pkg_* functions!" 29 die "Bad package! $1 is only for use in some pkg_* functions!"
26 esac 30 esac
27} 31}
28 32
29# @FUNCTION: egetent 33# @FUNCTION: egetent
30# @USAGE: <database> <key> 34# @USAGE: <database> <key>
101# Same as enewgroup, you are not required to understand how to properly add 105# 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. 106# 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 107# 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. 108# /bin/false, default homedir is /dev/null, and there are no default groups.
105enewuser() { 109enewuser() {
106 _assert_pkg_ebuild_phase enewuser 110 _assert_pkg_ebuild_phase ${FUNCNAME}
107 111
108 # get the username 112 # get the username
109 local euser=$1; shift 113 local euser=$1; shift
110 if [[ -z ${euser} ]] ; then 114 if [[ -z ${euser} ]] ; then
111 eerror "No username specified !" 115 eerror "No username specified !"
117 return 0 121 return 0
118 fi 122 fi
119 einfo "Adding user '${euser}' to your system ..." 123 einfo "Adding user '${euser}' to your system ..."
120 124
121 # options to pass to useradd 125 # options to pass to useradd
122 local opts= 126 local opts=()
123 127
124 # handle uid 128 # handle uid
125 local euid=$1; shift 129 local euid=$1; shift
126 if [[ -n ${euid} && ${euid} != -1 ]] ; then 130 if [[ -n ${euid} && ${euid} != -1 ]] ; then
127 if [[ ${euid} -gt 0 ]] ; then 131 if [[ ${euid} -gt 0 ]] ; then
138 if [[ ${euid} == "next" ]] ; then 142 if [[ ${euid} == "next" ]] ; then
139 for ((euid = 101; euid <= 999; euid++)); do 143 for ((euid = 101; euid <= 999; euid++)); do
140 [[ -z $(egetent passwd ${euid}) ]] && break 144 [[ -z $(egetent passwd ${euid}) ]] && break
141 done 145 done
142 fi 146 fi
143 opts+=" -u ${euid}" 147 opts+=( -u ${euid} )
144 einfo " - Userid: ${euid}" 148 einfo " - Userid: ${euid}"
145 149
146 # handle shell 150 # handle shell
147 local eshell=$1; shift 151 local eshell=$1; shift
148 if [[ ! -z ${eshell} ]] && [[ ${eshell} != "-1" ]] ; then 152 if [[ ! -z ${eshell} ]] && [[ ${eshell} != "-1" ]] ; then
168 *) die "Unable to identify the default shell for userland ${USERLAND}" 172 *) die "Unable to identify the default shell for userland ${USERLAND}"
169 esac 173 esac
170 fi 174 fi
171 fi 175 fi
172 einfo " - Shell: ${eshell}" 176 einfo " - Shell: ${eshell}"
173 opts+=" -s ${eshell}" 177 opts+=( -s "${eshell}" )
174 178
175 # handle homedir 179 # handle homedir
176 local ehome=$1; shift 180 local ehome=$1; shift
177 if [[ -z ${ehome} ]] || [[ ${ehome} == "-1" ]] ; then 181 if [[ -z ${ehome} ]] || [[ ${ehome} == "-1" ]] ; then
178 ehome="/dev/null" 182 ehome="/dev/null"
179 fi 183 fi
180 einfo " - Home: ${ehome}" 184 einfo " - Home: ${ehome}"
181 opts+=" -d ${ehome}" 185 opts+=( -d "${ehome}" )
182 186
183 # handle groups 187 # handle groups
184 local egroups=$1; shift 188 local egroups=$1; shift
185 if [[ ! -z ${egroups} ]] ; then 189 local g egroups_arr
186 local oldifs=${IFS} 190 IFS="," read -r -a egroups_arr <<<"${egroups}"
191 shift
192 if [[ ${#egroups_arr[@]} -gt 0 ]] ; then
187 local defgroup="" exgroups="" 193 local defgroup exgroups
188
189 export IFS=","
190 for g in ${egroups} ; do 194 for g in "${egroups_arr[@]}" ; do
191 export IFS=${oldifs}
192 if [[ -z $(egetent group "${g}") ]] ; then 195 if [[ -z $(egetent group "${g}") ]] ; then
193 eerror "You must add group ${g} to the system first" 196 eerror "You must add group ${g} to the system first"
194 die "${g} is not a valid GID" 197 die "${g} is not a valid GID"
195 fi 198 fi
196 if [[ -z ${defgroup} ]] ; then 199 if [[ -z ${defgroup} ]] ; then
197 defgroup=${g} 200 defgroup=${g}
198 else 201 else
199 exgroups="${exgroups},${g}" 202 exgroups+=",${g}"
200 fi 203 fi
201 export IFS=","
202 done 204 done
203 export IFS=${oldifs}
204
205 opts+=" -g ${defgroup}" 205 opts+=( -g "${defgroup}" )
206 if [[ ! -z ${exgroups} ]] ; then 206 if [[ ! -z ${exgroups} ]] ; then
207 opts+=" -G ${exgroups:1}" 207 opts+=( -G "${exgroups:1}" )
208 fi
209 else
210 egroups="(none)"
211 fi 208 fi
209 fi
212 einfo " - Groups: ${egroups}" 210 einfo " - Groups: ${egroups:-(none)}"
213 211
214 # handle extra args 212 # handle extra args
215 if [[ $# -gt 0 ]] ; then 213 if [[ $# -gt 0 ]] ; then
216 die "extra arguments no longer supported; please file a bug" 214 die "extra arguments no longer supported; please file a bug"
217 else 215 else
218 set -- -c "added by portage for ${PN}" 216 local comment="added by portage for ${PN}"
219 einfo " - Extra: $@" 217 opts+=( -c "${comment}" )
218 einfo " - GECOS: ${comment}"
220 fi 219 fi
221 220
222 # add the user 221 # add the user
223 case ${CHOST} in 222 case ${CHOST} in
224 *-darwin*) 223 *-darwin*)
225 ### Make the user 224 ### Make the user
226 dscl . create /users/${euser} uid ${euid} 225 dscl . create "/users/${euser}" uid ${euid}
227 dscl . create /users/${euser} shell ${eshell} 226 dscl . create "/users/${euser}" shell "${eshell}"
228 dscl . create /users/${euser} home ${ehome} 227 dscl . create "/users/${euser}" home "${ehome}"
229 dscl . create /users/${euser} realname "added by portage for ${PN}" 228 dscl . create "/users/${euser}" realname "added by portage for ${PN}"
230 ### Add the user to the groups specified 229 ### Add the user to the groups specified
231 local oldifs=${IFS}
232 export IFS=","
233 for g in ${egroups} ; do 230 for g in "${egroups_arr[@]}" ; do
234 dscl . merge /groups/${g} users ${euser} 231 dscl . merge "/groups/${g}" users "${euser}"
235 done 232 done
236 export IFS=${oldifs}
237 ;; 233 ;;
238 234
239 *-freebsd*|*-dragonfly*) 235 *-freebsd*|*-dragonfly*)
240 pw useradd ${euser} ${opts} "$@" || die 236 pw useradd "${euser}" "${opts[@]}" || die
241 ;; 237 ;;
242 238
243 *-netbsd*) 239 *-netbsd*)
244 useradd ${opts} ${euser} "$@" || die 240 useradd "${opts[@]}" "${euser}" || die
245 ;; 241 ;;
246 242
247 *-openbsd*) 243 *-openbsd*)
248 # all ops the same, except the -g vs -g/-G ... 244 # all ops the same, except the -g vs -g/-G ...
249 useradd -u ${euid} -s ${eshell} \ 245 useradd -u ${euid} -s "${eshell}" \
250 -d ${ehome} -g ${egroups} "$@" ${euser} || die 246 -d "${ehome}" -g "${egroups}" "${euser}" || die
251 ;; 247 ;;
252 248
253 *) 249 *)
254 useradd -r ${opts} "$@" ${euser} || die 250 useradd -r "${opts[@]}" "${euser}" || die
255 ;; 251 ;;
256 esac 252 esac
257 253
258 if [[ ! -e ${ROOT}/${ehome} ]] ; then 254 if [[ ! -e ${ROOT}/${ehome} ]] ; then
259 einfo " - Creating ${ehome} in ${ROOT}" 255 einfo " - Creating ${ehome} in ${ROOT}"
260 mkdir -p "${ROOT}/${ehome}" 256 mkdir -p "${ROOT}/${ehome}"
261 chown ${euser} "${ROOT}/${ehome}" 257 chown "${euser}" "${ROOT}/${ehome}"
262 chmod 755 "${ROOT}/${ehome}" 258 chmod 755 "${ROOT}/${ehome}"
263 fi 259 fi
264} 260}
265 261
266# @FUNCTION: enewgroup 262# @FUNCTION: enewgroup
269# This function does not require you to understand how to properly add a 265# This function does not require you to understand how to properly add a
270# group to the system. Just give it a group name to add and enewgroup will 266# group to the system. Just give it a group name to add and enewgroup will
271# do the rest. You may specify the gid for the group or allow the group to 267# do the rest. You may specify the gid for the group or allow the group to
272# allocate the next available one. 268# allocate the next available one.
273enewgroup() { 269enewgroup() {
274 _assert_pkg_ebuild_phase enewgroup 270 _assert_pkg_ebuild_phase ${FUNCNAME}
275 271
276 # get the group 272 # get the group
277 local egroup="$1"; shift 273 local egroup=$1; shift
278 if [ -z "${egroup}" ] 274 if [[ -z ${egroup} ]] ; then
279 then
280 eerror "No group specified !" 275 eerror "No group specified !"
281 die "Cannot call enewgroup without a group" 276 die "Cannot call enewgroup without a group"
282 fi 277 fi
283 278
284 # see if group already exists 279 # see if group already exists
285 if [[ -n $(egetent group "${egroup}") ]]; then 280 if [[ -n $(egetent group "${egroup}") ]] ; then
286 return 0 281 return 0
287 fi 282 fi
288 einfo "Adding group '${egroup}' to your system ..." 283 einfo "Adding group '${egroup}' to your system ..."
289 284
290 # options to pass to useradd
291 local opts=
292
293 # handle gid 285 # handle gid
294 local egid="$1"; shift 286 local egid=$1; shift
295 if [ ! -z "${egid}" ] 287 if [[ ! -z ${egid} ]] ; then
296 then
297 if [ "${egid}" -gt 0 ] 288 if [[ ${egid} -gt 0 ]] ; then
298 then
299 if [ -z "`egetent group ${egid}`" ] 289 if [[ -n $(egetent group ${egid}) ]] ; then
300 then
301 if [[ "${CHOST}" == *-darwin* ]]; then
302 opts+=" ${egid}"
303 else
304 opts+=" -g ${egid}"
305 fi
306 else
307 egid="next available; requested gid taken" 290 egid="next available; requested gid taken"
308 fi 291 fi
309 else 292 else
310 eerror "Groupid given but is not greater than 0 !" 293 eerror "Groupid given but is not greater than 0 !"
311 die "${egid} is not a valid GID" 294 die "${egid} is not a valid GID"
314 egid="next available" 297 egid="next available"
315 fi 298 fi
316 einfo " - Groupid: ${egid}" 299 einfo " - Groupid: ${egid}"
317 300
318 # handle extra 301 # handle extra
319 if [ $# -gt 0 ] ; then 302 if [[ $# -gt 0 ]] ; then
320 die "extra arguments no longer supported; please file a bug" 303 die "extra arguments no longer supported; please file a bug"
321 fi 304 fi
322 305
323 # add the group 306 # Some targets need to find the next available GID manually
324 case ${CHOST} in 307 _enewgroup_next_gid() {
325 *-darwin*) 308 if [[ ${egid} == *[!0-9]* ]] ; then
326 # If we need the next available 309 # Non numeric
327 case ${egid} in
328 *[!0-9]*) # Non numeric
329 for ((egid = 101; egid <= 999; egid++)); do 310 for ((egid = 101; egid <= 999; egid++)) ; do
330 [[ -z $(egetent group ${egid}) ]] && break 311 [[ -z $(egetent group ${egid}) ]] && break
331 done 312 done
332 esac 313 fi
314 }
315
316 # add the group
317 case ${CHOST} in
318 *-darwin*)
319 _enewgroup_next_gid
333 dscl . create /groups/${egroup} gid ${egid} 320 dscl . create "/groups/${egroup}" gid ${egid}
334 dscl . create /groups/${egroup} passwd '*' 321 dscl . create "/groups/${egroup}" passwd '*'
335 ;; 322 ;;
336 323
337 *-freebsd*|*-dragonfly*) 324 *-freebsd*|*-dragonfly*)
338 case ${egid} in 325 _enewgroup_next_gid
339 *[!0-9]*) # Non numeric
340 for ((egid = 101; egid <= 999; egid++)); do
341 [[ -z $(egetent group ${egid}) ]] && break
342 done
343 esac
344 pw groupadd ${egroup} -g ${egid} || die 326 pw groupadd "${egroup}" -g ${egid} || die
345 ;; 327 ;;
346 328
347 *-netbsd*) 329 *-netbsd*)
348 case ${egid} in 330 _enewgroup_next_gid
349 *[!0-9]*) # Non numeric
350 for ((egid = 101; egid <= 999; egid++)); do
351 [[ -z $(egetent group ${egid}) ]] && break
352 done
353 esac
354 groupadd -g ${egid} ${egroup} || die 331 groupadd -g ${egid} "${egroup}" || die
355 ;; 332 ;;
356 333
357 *) 334 *)
335 local opts
336 if [[ ${egid} == *[!0-9]* ]] ; then
337 # Non numeric; let groupadd figure out a GID for us
338 opts=""
339 else
340 opts="-g ${egid}"
341 fi
358 # We specify -r so that we get a GID in the system range from login.defs 342 # We specify -r so that we get a GID in the system range from login.defs
359 groupadd -r ${opts} ${egroup} || die 343 groupadd -r ${opts} "${egroup}" || die
360 ;; 344 ;;
361 esac 345 esac
362} 346}
363 347
364# @FUNCTION: egethome 348# @FUNCTION: egethome
377 *) # Linux, NetBSD, OpenBSD, etc... 361 *) # Linux, NetBSD, OpenBSD, etc...
378 pos=6 362 pos=6
379 ;; 363 ;;
380 esac 364 esac
381 365
382 egetent passwd $1 | cut -d: -f${pos} 366 egetent passwd "$1" | cut -d: -f${pos}
383} 367}
384 368
385# @FUNCTION: egetshell 369# @FUNCTION: egetshell
386# @USAGE: <user> 370# @USAGE: <user>
387# @DESCRIPTION: 371# @DESCRIPTION:
400 ;; 384 ;;
401 esac 385 esac
402 386
403 egetent passwd "$1" | cut -d: -f${pos} 387 egetent passwd "$1" | cut -d: -f${pos}
404} 388}
389
390# @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.
399esethome() {
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 # 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 # update the home directory
436 case ${CHOST} in
437 *-darwin*)
438 dscl . change "/users/${euser}" home "${ehome}"
439 ;;
440
441 *-freebsd*|*-dragonfly*)
442 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 ;;
448
449 *)
450 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 ;;
456 esac
457}
458
459fi

Legend:
Removed from v.1.13  
changed lines
  Added in v.1.21

  ViewVC Help
Powered by ViewVC 1.1.20