/[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.17
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.17 2011/11/29 19:32:23 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 local g egroups_arr
188 local oldifs=${IFS} 187 IFS="," read -r -a egroups_arr <<<"${egroups}"
188 shift
189 if [[ ${#egroups_arr[@]} -gt 0 ]] ; then
189 local defgroup="" exgroups="" 190 local defgroup exgroups
190
191 export IFS=","
192 for g in ${egroups} ; do 191 for g in "${egroups_arr[@]}" ; do
193 export IFS=${oldifs}
194 if [[ -z $(egetent group "${g}") ]] ; then 192 if [[ -z $(egetent group "${g}") ]] ; then
195 eerror "You must add group ${g} to the system first" 193 eerror "You must add group ${g} to the system first"
196 die "${g} is not a valid GID" 194 die "${g} is not a valid GID"
197 fi 195 fi
198 if [[ -z ${defgroup} ]] ; then 196 if [[ -z ${defgroup} ]] ; then
199 defgroup=${g} 197 defgroup=${g}
200 else 198 else
201 exgroups="${exgroups},${g}" 199 exgroups+=",${g}"
202 fi 200 fi
203 export IFS=","
204 done 201 done
205 export IFS=${oldifs}
206
207 opts+=" -g ${defgroup}" 202 opts+=( -g "${defgroup}" )
208 if [[ ! -z ${exgroups} ]] ; then 203 if [[ ! -z ${exgroups} ]] ; then
209 opts+=" -G ${exgroups:1}" 204 opts+=( -G "${exgroups:1}" )
210 fi
211 else
212 egroups="(none)"
213 fi 205 fi
206 fi
214 einfo " - Groups: ${egroups}" 207 einfo " - Groups: ${egroups:-(none)}"
215 208
216 # handle extra args 209 # handle extra args
217 if [[ $# -gt 0 ]] ; then 210 if [[ $# -gt 0 ]] ; then
218 die "extra arguments no longer supported; please file a bug" 211 die "extra arguments no longer supported; please file a bug"
219 else 212 else
220 set -- -c "added by portage for ${PN}" 213 local comment="added by portage for ${PN}"
221 einfo " - Extra: $@" 214 opts+=( -c "${comment}" )
215 einfo " - GECOS: ${comment}"
222 fi 216 fi
223 217
224 # add the user 218 # add the user
225 local oldsandbox=${SANDBOX_ON}
226 export SANDBOX_ON="0"
227 case ${CHOST} in 219 case ${CHOST} in
228 *-darwin*) 220 *-darwin*)
229 ### Make the user 221 ### Make the user
230 dscl . create /users/${euser} uid ${euid} 222 dscl . create "/users/${euser}" uid ${euid}
231 dscl . create /users/${euser} shell ${eshell} 223 dscl . create "/users/${euser}" shell "${eshell}"
232 dscl . create /users/${euser} home ${ehome} 224 dscl . create "/users/${euser}" home "${ehome}"
233 dscl . create /users/${euser} realname "added by portage for ${PN}" 225 dscl . create "/users/${euser}" realname "added by portage for ${PN}"
234 ### Add the user to the groups specified 226 ### Add the user to the groups specified
235 local oldifs=${IFS}
236 export IFS=","
237 for g in ${egroups} ; do 227 for g in "${egroups_arr[@]}" ; do
238 dscl . merge /groups/${g} users ${euser} 228 dscl . merge "/groups/${g}" users "${euser}"
239 done 229 done
240 export IFS=${oldifs}
241 ;; 230 ;;
242 231
243 *-freebsd*|*-dragonfly*) 232 *-freebsd*|*-dragonfly*)
244 pw useradd ${euser} ${opts} "$@" || die 233 pw useradd "${euser}" "${opts[@]}" || die
245 ;; 234 ;;
246 235
247 *-netbsd*) 236 *-netbsd*)
248 useradd ${opts} ${euser} "$@" || die 237 useradd "${opts[@]}" "${euser}" || die
249 ;; 238 ;;
250 239
251 *-openbsd*) 240 *-openbsd*)
252 # all ops the same, except the -g vs -g/-G ... 241 # all ops the same, except the -g vs -g/-G ...
253 useradd -u ${euid} -s ${eshell} \ 242 useradd -u ${euid} -s "${eshell}" \
254 -d ${ehome} -g ${egroups} "$@" ${euser} || die 243 -d "${ehome}" -g "${egroups}" "${euser}" || die
255 ;; 244 ;;
256 245
257 *) 246 *)
258 useradd -r ${opts} "$@" ${euser} || die 247 useradd -r "${opts[@]}" "${euser}" || die
259 ;; 248 ;;
260 esac 249 esac
261 250
262 if [[ ! -e ${ROOT}/${ehome} ]] ; then 251 if [[ ! -e ${ROOT}/${ehome} ]] ; then
263 einfo " - Creating ${ehome} in ${ROOT}" 252 einfo " - Creating ${ehome} in ${ROOT}"
264 mkdir -p "${ROOT}/${ehome}" 253 mkdir -p "${ROOT}/${ehome}"
265 chown ${euser} "${ROOT}/${ehome}" 254 chown "${euser}" "${ROOT}/${ehome}"
266 chmod 755 "${ROOT}/${ehome}" 255 chmod 755 "${ROOT}/${ehome}"
267 fi 256 fi
268
269 export SANDBOX_ON=${oldsandbox}
270} 257}
271 258
272# @FUNCTION: enewgroup 259# @FUNCTION: enewgroup
273# @USAGE: <group> [gid] 260# @USAGE: <group> [gid]
274# @DESCRIPTION: 261# @DESCRIPTION:
275# This function does not require you to understand how to properly add a 262# This function does not require you to understand how to properly add a
276# group to the system. Just give it a group name to add and enewgroup will 263# group to the system. Just give it a group name to add and enewgroup will
277# do the rest. You may specify the gid for the group or allow the group to 264# do the rest. You may specify the gid for the group or allow the group to
278# allocate the next available one. 265# allocate the next available one.
279enewgroup() { 266enewgroup() {
280 _assert_pkg_ebuild_phase enewgroup 267 _assert_pkg_ebuild_phase ${FUNCNAME}
281 268
282 # get the group 269 # get the group
283 local egroup="$1"; shift 270 local egroup=$1; shift
284 if [ -z "${egroup}" ] 271 if [[ -z ${egroup} ]] ; then
285 then
286 eerror "No group specified !" 272 eerror "No group specified !"
287 die "Cannot call enewgroup without a group" 273 die "Cannot call enewgroup without a group"
288 fi 274 fi
289 275
290 # see if group already exists 276 # see if group already exists
291 if [[ -n $(egetent group "${egroup}") ]]; then 277 if [[ -n $(egetent group "${egroup}") ]] ; then
292 return 0 278 return 0
293 fi 279 fi
294 einfo "Adding group '${egroup}' to your system ..." 280 einfo "Adding group '${egroup}' to your system ..."
295 281
296 # options to pass to useradd
297 local opts=
298
299 # handle gid 282 # handle gid
300 local egid="$1"; shift 283 local egid=$1; shift
301 if [ ! -z "${egid}" ] 284 if [[ ! -z ${egid} ]] ; then
302 then
303 if [ "${egid}" -gt 0 ] 285 if [[ ${egid} -gt 0 ]] ; then
304 then
305 if [ -z "`egetent group ${egid}`" ] 286 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" 287 egid="next available; requested gid taken"
314 fi 288 fi
315 else 289 else
316 eerror "Groupid given but is not greater than 0 !" 290 eerror "Groupid given but is not greater than 0 !"
317 die "${egid} is not a valid GID" 291 die "${egid} is not a valid GID"
320 egid="next available" 294 egid="next available"
321 fi 295 fi
322 einfo " - Groupid: ${egid}" 296 einfo " - Groupid: ${egid}"
323 297
324 # handle extra 298 # handle extra
325 if [ $# -gt 0 ] ; then 299 if [[ $# -gt 0 ]] ; then
326 die "extra arguments no longer supported; please file a bug" 300 die "extra arguments no longer supported; please file a bug"
327 fi 301 fi
328 302
329 # add the group 303 # Some targets need to find the next available GID manually
330 local oldsandbox="${SANDBOX_ON}" 304 _enewgroup_next_gid() {
331 export SANDBOX_ON="0" 305 if [[ ${egid} == *[!0-9]* ]] ; then
332 case ${CHOST} in 306 # Non numeric
333 *-darwin*)
334 # If we need the next available
335 case ${egid} in
336 *[!0-9]*) # Non numeric
337 for ((egid = 101; egid <= 999; egid++)); do 307 for ((egid = 101; egid <= 999; egid++)) ; do
338 [[ -z $(egetent group ${egid}) ]] && break 308 [[ -z $(egetent group ${egid}) ]] && break
339 done 309 done
340 esac 310 fi
311 }
312
313 # add the group
314 case ${CHOST} in
315 *-darwin*)
316 _enewgroup_next_gid
341 dscl . create /groups/${egroup} gid ${egid} 317 dscl . create "/groups/${egroup}" gid ${egid}
342 dscl . create /groups/${egroup} passwd '*' 318 dscl . create "/groups/${egroup}" passwd '*'
343 ;; 319 ;;
344 320
345 *-freebsd*|*-dragonfly*) 321 *-freebsd*|*-dragonfly*)
346 case ${egid} in 322 _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 323 pw groupadd "${egroup}" -g ${egid} || die
353 ;; 324 ;;
354 325
355 *-netbsd*) 326 *-netbsd*)
356 case ${egid} in 327 _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 328 groupadd -g ${egid} "${egroup}" || die
363 ;; 329 ;;
364 330
365 *) 331 *)
332 local opts
333 if [[ ${egid} == *[!0-9]* ]] ; then
334 # Non numeric; let groupadd figure out a GID for us
335 opts=""
336 else
337 opts="-g ${egid}"
338 fi
366 # We specify -r so that we get a GID in the system range from login.defs 339 # We specify -r so that we get a GID in the system range from login.defs
367 groupadd -r ${opts} ${egroup} || die 340 groupadd -r ${opts} "${egroup}" || die
368 ;; 341 ;;
369 esac 342 esac
370 export SANDBOX_ON="${oldsandbox}"
371} 343}
372 344
373# @FUNCTION: egethome 345# @FUNCTION: egethome
374# @USAGE: <user> 346# @USAGE: <user>
375# @DESCRIPTION: 347# @DESCRIPTION:
386 *) # Linux, NetBSD, OpenBSD, etc... 358 *) # Linux, NetBSD, OpenBSD, etc...
387 pos=6 359 pos=6
388 ;; 360 ;;
389 esac 361 esac
390 362
391 egetent passwd $1 | cut -d: -f${pos} 363 egetent passwd "$1" | cut -d: -f${pos}
392} 364}
393 365
394# @FUNCTION: egetshell 366# @FUNCTION: egetshell
395# @USAGE: <user> 367# @USAGE: <user>
396# @DESCRIPTION: 368# @DESCRIPTION:

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

  ViewVC Help
Powered by ViewVC 1.1.20