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

Diff of /eclass/user.eclass

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

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

Legend:
Removed from v.1.11  
changed lines
  Added in v.1.14

  ViewVC Help
Powered by ViewVC 1.1.20