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

Diff of /eclass/user.eclass

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

Revision 1.12 Revision 1.15
1# Copyright 1999-2011 Gentoo Foundation 1# Copyright 1999-2011 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.12 2011/11/26 06:45:38 vapier Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/user.eclass,v 1.15 2011/11/29 19:01:28 vapier 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)
18# @USAGE: <calling func name> 18# @USAGE: <calling func name>
19_assert_pkg_ebuild_phase() { 19_assert_pkg_ebuild_phase() {
20 case ${EBUILD_PHASE} in 20 case ${EBUILD_PHASE} in
21 setup|preinst|postinst) ;; 21 setup|preinst|postinst) ;;
22 *) 22 *)
23 eerror "'$1()' called from '${EBUILD_PHASE}()' which is not a pkg_* function." 23 eerror "'$1()' called from '${EBUILD_PHASE}' phase which is not OK:"
24 eerror "You may only call from pkg_{setup,preinst,postinst} functions."
24 eerror "Package fails at QA and at life. Please file a bug." 25 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 die "Bad package! $1 is only for use in some pkg_* functions!"
26 esac 27 esac
27} 28}
28 29
29# @FUNCTION: egetent 30# @FUNCTION: egetent
30# @USAGE: <database> <key> 31# @USAGE: <database> <key>
101# Same as enewgroup, you are not required to understand how to properly add 102# 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# 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# 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. 105# /bin/false, default homedir is /dev/null, and there are no default groups.
105enewuser() { 106enewuser() {
106 _assert_pkg_ebuild_phase enewuser 107 _assert_pkg_ebuild_phase ${FUNCNAME}
107 108
108 # get the username 109 # get the username
109 local euser=$1; shift 110 local euser=$1; shift
110 if [[ -z ${euser} ]] ; then 111 if [[ -z ${euser} ]] ; then
111 eerror "No username specified !" 112 eerror "No username specified !"
117 return 0 118 return 0
118 fi 119 fi
119 einfo "Adding user '${euser}' to your system ..." 120 einfo "Adding user '${euser}' to your system ..."
120 121
121 # options to pass to useradd 122 # options to pass to useradd
122 local opts= 123 local opts=()
123 124
124 # handle uid 125 # handle uid
125 local euid=$1; shift 126 local euid=$1; shift
126 if [[ -n ${euid} && ${euid} != -1 ]] ; then 127 if [[ -n ${euid} && ${euid} != -1 ]] ; then
127 if [[ ${euid} -gt 0 ]] ; then 128 if [[ ${euid} -gt 0 ]] ; then
138 if [[ ${euid} == "next" ]] ; then 139 if [[ ${euid} == "next" ]] ; then
139 for ((euid = 101; euid <= 999; euid++)); do 140 for ((euid = 101; euid <= 999; euid++)); do
140 [[ -z $(egetent passwd ${euid}) ]] && break 141 [[ -z $(egetent passwd ${euid}) ]] && break
141 done 142 done
142 fi 143 fi
143 opts+=" -u ${euid}" 144 opts+=( -u ${euid} )
144 einfo " - Userid: ${euid}" 145 einfo " - Userid: ${euid}"
145 146
146 # handle shell 147 # handle shell
147 local eshell=$1; shift 148 local eshell=$1; shift
148 if [[ ! -z ${eshell} ]] && [[ ${eshell} != "-1" ]] ; then 149 if [[ ! -z ${eshell} ]] && [[ ${eshell} != "-1" ]] ; then
153 if [[ ${eshell} == */false || ${eshell} == */nologin ]] ; then 154 if [[ ${eshell} == */false || ${eshell} == */nologin ]] ; then
154 eerror "Do not specify ${eshell} yourself, use -1" 155 eerror "Do not specify ${eshell} yourself, use -1"
155 die "Pass '-1' as the shell parameter" 156 die "Pass '-1' as the shell parameter"
156 fi 157 fi
157 else 158 else
158 for shell in /sbin/nologin /usr/sbin/nologin /bin/false /usr/bin/false /dev/null ; do 159 for eshell in /sbin/nologin /usr/sbin/nologin /bin/false /usr/bin/false /dev/null ; do
159 [[ -x ${ROOT}${shell} ]] && break 160 [[ -x ${ROOT}${eshell} ]] && break
160 done 161 done
161 162
162 if [[ ${shell} == "/dev/null" ]] ; then 163 if [[ ${eshell} == "/dev/null" ]] ; then
163 eerror "Unable to identify the shell to use, proceeding with userland default." 164 eerror "Unable to identify the shell to use, proceeding with userland default."
164 case ${USERLAND} in 165 case ${USERLAND} in
165 GNU) shell="/bin/false" ;; 166 GNU) eshell="/bin/false" ;;
166 BSD) shell="/sbin/nologin" ;; 167 BSD) eshell="/sbin/nologin" ;;
167 Darwin) shell="/usr/sbin/nologin" ;; 168 Darwin) eshell="/usr/sbin/nologin" ;;
168 *) die "Unable to identify the default shell for userland ${USERLAND}" 169 *) die "Unable to identify the default shell for userland ${USERLAND}"
169 esac 170 esac
170 fi 171 fi
171
172 eshell=${shell}
173 fi 172 fi
174 einfo " - Shell: ${eshell}" 173 einfo " - Shell: ${eshell}"
175 opts+=" -s ${eshell}" 174 opts+=( -s "${eshell}" )
176 175
177 # handle homedir 176 # handle homedir
178 local ehome=$1; shift 177 local ehome=$1; shift
179 if [[ -z ${ehome} ]] || [[ ${ehome} == "-1" ]] ; then 178 if [[ -z ${ehome} ]] || [[ ${ehome} == "-1" ]] ; then
180 ehome="/dev/null" 179 ehome="/dev/null"
181 fi 180 fi
182 einfo " - Home: ${ehome}" 181 einfo " - Home: ${ehome}"
183 opts+=" -d ${ehome}" 182 opts+=( -d "${ehome}" )
184 183
185 # handle groups 184 # handle groups
186 local egroups=$1; shift 185 local egroups=$1; shift
187 if [[ ! -z ${egroups} ]] ; then 186 if [[ ! -z ${egroups} ]] ; then
188 local oldifs=${IFS} 187 local oldifs=${IFS}
202 fi 201 fi
203 export IFS="," 202 export IFS=","
204 done 203 done
205 export IFS=${oldifs} 204 export IFS=${oldifs}
206 205
207 opts+=" -g ${defgroup}" 206 opts+=( -g "${defgroup}" )
208 if [[ ! -z ${exgroups} ]] ; then 207 if [[ ! -z ${exgroups} ]] ; then
209 opts+=" -G ${exgroups:1}" 208 opts+=( -G "${exgroups:1}" )
210 fi 209 fi
211 else 210 else
212 egroups="(none)" 211 egroups="(none)"
213 fi 212 fi
214 einfo " - Groups: ${egroups}" 213 einfo " - Groups: ${egroups}"
215 214
216 # handle extra args 215 # handle extra args
217 if [[ $# -gt 0 ]] ; then 216 if [[ $# -gt 0 ]] ; then
218 die "extra arguments no longer supported; please file a bug" 217 die "extra arguments no longer supported; please file a bug"
219 else 218 else
220 set -- -c "added by portage for ${PN}" 219 local comment="added by portage for ${PN}"
221 einfo " - Extra: $@" 220 opts+=( -c "${comment}" )
221 einfo " - GECOS: ${comment}"
222 fi 222 fi
223 223
224 # add the user 224 # add the user
225 case ${CHOST} in 225 case ${CHOST} in
226 *-darwin*) 226 *-darwin*)
227 ### Make the user 227 ### Make the user
228 dscl . create /users/${euser} uid ${euid} 228 dscl . create "/users/${euser}" uid ${euid}
229 dscl . create /users/${euser} shell ${eshell} 229 dscl . create "/users/${euser}" shell "${eshell}"
230 dscl . create /users/${euser} home ${ehome} 230 dscl . create "/users/${euser}" home "${ehome}"
231 dscl . create /users/${euser} realname "added by portage for ${PN}" 231 dscl . create "/users/${euser}" realname "added by portage for ${PN}"
232 ### Add the user to the groups specified 232 ### Add the user to the groups specified
233 local oldifs=${IFS} 233 local g oldifs=${IFS}
234 export IFS="," 234 export IFS=","
235 for g in ${egroups} ; do 235 for g in ${egroups} ; do
236 dscl . merge /groups/${g} users ${euser} 236 dscl . merge "/groups/${g}" users "${euser}"
237 done 237 done
238 export IFS=${oldifs} 238 export IFS=${oldifs}
239 ;; 239 ;;
240 240
241 *-freebsd*|*-dragonfly*) 241 *-freebsd*|*-dragonfly*)
242 pw useradd ${euser} ${opts} "$@" || die 242 pw useradd "${euser}" "${opts[@]}" || die
243 ;; 243 ;;
244 244
245 *-netbsd*) 245 *-netbsd*)
246 useradd ${opts} ${euser} "$@" || die 246 useradd "${opts[@]}" "${euser}" || die
247 ;; 247 ;;
248 248
249 *-openbsd*) 249 *-openbsd*)
250 # all ops the same, except the -g vs -g/-G ... 250 # all ops the same, except the -g vs -g/-G ...
251 useradd -u ${euid} -s ${eshell} \ 251 useradd -u ${euid} -s "${eshell}" \
252 -d ${ehome} -g ${egroups} "$@" ${euser} || die 252 -d "${ehome}" -g "${egroups}" "${euser}" || die
253 ;; 253 ;;
254 254
255 *) 255 *)
256 useradd -r ${opts} "$@" ${euser} || die 256 useradd -r "${opts[@]}" "${euser}" || die
257 ;; 257 ;;
258 esac 258 esac
259 259
260 if [[ ! -e ${ROOT}/${ehome} ]] ; then 260 if [[ ! -e ${ROOT}/${ehome} ]] ; then
261 einfo " - Creating ${ehome} in ${ROOT}" 261 einfo " - Creating ${ehome} in ${ROOT}"
262 mkdir -p "${ROOT}/${ehome}" 262 mkdir -p "${ROOT}/${ehome}"
263 chown ${euser} "${ROOT}/${ehome}" 263 chown "${euser}" "${ROOT}/${ehome}"
264 chmod 755 "${ROOT}/${ehome}" 264 chmod 755 "${ROOT}/${ehome}"
265 fi 265 fi
266} 266}
267 267
268# @FUNCTION: enewgroup 268# @FUNCTION: enewgroup
271# This function does not require you to understand how to properly add a 271# This function does not require you to understand how to properly add a
272# group to the system. Just give it a group name to add and enewgroup will 272# group to the system. Just give it a group name to add and enewgroup will
273# do the rest. You may specify the gid for the group or allow the group to 273# do the rest. You may specify the gid for the group or allow the group to
274# allocate the next available one. 274# allocate the next available one.
275enewgroup() { 275enewgroup() {
276 _assert_pkg_ebuild_phase enewgroup 276 _assert_pkg_ebuild_phase ${FUNCNAME}
277 277
278 # get the group 278 # get the group
279 local egroup="$1"; shift 279 local egroup=$1; shift
280 if [ -z "${egroup}" ] 280 if [[ -z ${egroup} ]] ; then
281 then
282 eerror "No group specified !" 281 eerror "No group specified !"
283 die "Cannot call enewgroup without a group" 282 die "Cannot call enewgroup without a group"
284 fi 283 fi
285 284
286 # see if group already exists 285 # see if group already exists
287 if [[ -n $(egetent group "${egroup}") ]]; then 286 if [[ -n $(egetent group "${egroup}") ]] ; then
288 return 0 287 return 0
289 fi 288 fi
290 einfo "Adding group '${egroup}' to your system ..." 289 einfo "Adding group '${egroup}' to your system ..."
291 290
292 # options to pass to useradd
293 local opts=
294
295 # handle gid 291 # handle gid
296 local egid="$1"; shift 292 local egid=$1; shift
297 if [ ! -z "${egid}" ] 293 if [[ ! -z ${egid} ]] ; then
298 then
299 if [ "${egid}" -gt 0 ] 294 if [[ ${egid} -gt 0 ]] ; then
300 then
301 if [ -z "`egetent group ${egid}`" ] 295 if [[ -n $(egetent group ${egid}) ]] ; then
302 then
303 if [[ "${CHOST}" == *-darwin* ]]; then
304 opts+=" ${egid}"
305 else
306 opts+=" -g ${egid}"
307 fi
308 else
309 egid="next available; requested gid taken" 296 egid="next available; requested gid taken"
310 fi 297 fi
311 else 298 else
312 eerror "Groupid given but is not greater than 0 !" 299 eerror "Groupid given but is not greater than 0 !"
313 die "${egid} is not a valid GID" 300 die "${egid} is not a valid GID"
316 egid="next available" 303 egid="next available"
317 fi 304 fi
318 einfo " - Groupid: ${egid}" 305 einfo " - Groupid: ${egid}"
319 306
320 # handle extra 307 # handle extra
321 if [ $# -gt 0 ] ; then 308 if [[ $# -gt 0 ]] ; then
322 die "extra arguments no longer supported; please file a bug" 309 die "extra arguments no longer supported; please file a bug"
323 fi 310 fi
324 311
325 # add the group 312 # Some targets need to find the next available GID manually
326 case ${CHOST} in 313 _enewgroup_next_gid() {
327 *-darwin*) 314 if [[ ${egid} == *[!0-9]* ]] ; then
328 # If we need the next available
329 case ${egid} in
330 *[!0-9]*) # Non numeric 315 # Non numeric
331 for ((egid = 101; egid <= 999; egid++)); do 316 for ((egid = 101; egid <= 999; egid++)) ; do
332 [[ -z $(egetent group ${egid}) ]] && break 317 [[ -z $(egetent group ${egid}) ]] && break
333 done 318 done
334 esac 319 fi
320 }
321
322 # add the group
323 case ${CHOST} in
324 *-darwin*)
325 _enewgroup_next_gid
335 dscl . create /groups/${egroup} gid ${egid} 326 dscl . create "/groups/${egroup}" gid ${egid}
336 dscl . create /groups/${egroup} passwd '*' 327 dscl . create "/groups/${egroup}" passwd '*'
337 ;; 328 ;;
338 329
339 *-freebsd*|*-dragonfly*) 330 *-freebsd*|*-dragonfly*)
340 case ${egid} in 331 _enewgroup_next_gid
341 *[!0-9]*) # Non numeric
342 for ((egid = 101; egid <= 999; egid++)); do
343 [[ -z $(egetent group ${egid}) ]] && break
344 done
345 esac
346 pw groupadd ${egroup} -g ${egid} || die 332 pw groupadd "${egroup}" -g ${egid} || die
347 ;; 333 ;;
348 334
349 *-netbsd*) 335 *-netbsd*)
350 case ${egid} in 336 _enewgroup_next_gid
351 *[!0-9]*) # Non numeric
352 for ((egid = 101; egid <= 999; egid++)); do
353 [[ -z $(egetent group ${egid}) ]] && break
354 done
355 esac
356 groupadd -g ${egid} ${egroup} || die 337 groupadd -g ${egid} "${egroup}" || die
357 ;; 338 ;;
358 339
359 *) 340 *)
341 local opts
342 if [[ ${egid} == *[!0-9]* ]] ; then
343 # Non numeric; let groupadd figure out a GID for us
344 opts=""
345 else
346 opts="-g ${egid}"
347 fi
360 # We specify -r so that we get a GID in the system range from login.defs 348 # We specify -r so that we get a GID in the system range from login.defs
361 groupadd -r ${opts} ${egroup} || die 349 groupadd -r ${opts} "${egroup}" || die
362 ;; 350 ;;
363 esac 351 esac
364} 352}
365 353
366# @FUNCTION: egethome 354# @FUNCTION: egethome
379 *) # Linux, NetBSD, OpenBSD, etc... 367 *) # Linux, NetBSD, OpenBSD, etc...
380 pos=6 368 pos=6
381 ;; 369 ;;
382 esac 370 esac
383 371
384 egetent passwd $1 | cut -d: -f${pos} 372 egetent passwd "$1" | cut -d: -f${pos}
385} 373}
386 374
387# @FUNCTION: egetshell 375# @FUNCTION: egetshell
388# @USAGE: <user> 376# @USAGE: <user>
389# @DESCRIPTION: 377# @DESCRIPTION:

Legend:
Removed from v.1.12  
changed lines
  Added in v.1.15

  ViewVC Help
Powered by ViewVC 1.1.20