| 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.6 2011/11/03 00:26:17 vapier Exp $ |
3 | # $Header: /var/cvsroot/gentoo-x86/eclass/user.eclass,v 1.12 2011/11/26 06:45:38 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) |
| … | |
… | |
| 16 | # @FUNCTION: _assert_pkg_ebuild_phase |
16 | # @FUNCTION: _assert_pkg_ebuild_phase |
| 17 | # @INTERNAL |
17 | # @INTERNAL |
| 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 | unpack|prepare|configure|compile|test|install) |
21 | setup|preinst|postinst) ;; |
|
|
22 | *) |
| 22 | eerror "'$1()' called from '${EBUILD_PHASE}()' which is not a pkg_* function." |
23 | eerror "'$1()' called from '${EBUILD_PHASE}()' which is not a pkg_* function." |
| 23 | eerror "Package fails at QA and at life. Please file a bug." |
24 | eerror "Package fails at QA and at life. Please file a bug." |
| 24 | die "Bad package! $1 is only for use in pkg_* functions!" |
25 | die "Bad package! $1 is only for use in pkg_* functions!" |
| 25 | esac |
26 | esac |
| 26 | } |
27 | } |
| … | |
… | |
| 76 | esac |
77 | esac |
| 77 | |
78 | |
| 78 | # lookup by uid/gid |
79 | # lookup by uid/gid |
| 79 | local opts |
80 | local opts |
| 80 | if [[ ${key} == [[:digit:]]* ]] ; then |
81 | if [[ ${key} == [[:digit:]]* ]] ; then |
| 81 | [[ ${action} == "user" ]] && opts="-u" || opts="-g" |
82 | [[ ${db} == "user" ]] && opts="-u" || opts="-g" |
| 82 | fi |
83 | fi |
| 83 | |
84 | |
| 84 | pw show ${action} ${opts} "${key}" -q |
85 | pw show ${db} ${opts} "${key}" -q |
| 85 | ;; |
86 | ;; |
| 86 | *-netbsd*|*-openbsd*) |
87 | *-netbsd*|*-openbsd*) |
| 87 | grep "${key}:\*:" /etc/${db} |
88 | grep "${key}:\*:" /etc/${db} |
| 88 | ;; |
89 | ;; |
| 89 | *) |
90 | *) |
| … | |
… | |
| 93 | ;; |
94 | ;; |
| 94 | esac |
95 | esac |
| 95 | } |
96 | } |
| 96 | |
97 | |
| 97 | # @FUNCTION: enewuser |
98 | # @FUNCTION: enewuser |
| 98 | # @USAGE: <user> [uid] [shell] [homedir] [groups] [params] |
99 | # @USAGE: <user> [uid] [shell] [homedir] [groups] |
| 99 | # @DESCRIPTION: |
100 | # @DESCRIPTION: |
| 100 | # Same as enewgroup, you are not required to understand how to properly add |
101 | # Same as enewgroup, you are not required to understand how to properly add |
| 101 | # a user to the system. The only required parameter is the username. |
102 | # a user to the system. The only required parameter is the username. |
| 102 | # Default uid is (pass -1 for this) next available, default shell is |
103 | # Default uid is (pass -1 for this) next available, default shell is |
| 103 | # /bin/false, default homedir is /dev/null, there are no default groups, |
104 | # /bin/false, default homedir is /dev/null, and there are no default groups. |
| 104 | # and default params sets the comment as 'added by portage for ${PN}'. |
|
|
| 105 | enewuser() { |
105 | enewuser() { |
| 106 | _assert_pkg_ebuild_phase enewuser |
106 | _assert_pkg_ebuild_phase enewuser |
| 107 | |
107 | |
| 108 | # get the username |
108 | # get the username |
| 109 | local euser=$1; shift |
109 | local euser=$1; shift |
| … | |
… | |
| 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="${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 |
| … | |
… | |
| 170 | fi |
170 | fi |
| 171 | |
171 | |
| 172 | eshell=${shell} |
172 | eshell=${shell} |
| 173 | fi |
173 | fi |
| 174 | einfo " - Shell: ${eshell}" |
174 | einfo " - Shell: ${eshell}" |
| 175 | opts="${opts} -s ${eshell}" |
175 | opts+=" -s ${eshell}" |
| 176 | |
176 | |
| 177 | # handle homedir |
177 | # handle homedir |
| 178 | local ehome=$1; shift |
178 | local ehome=$1; shift |
| 179 | if [[ -z ${ehome} ]] || [[ ${ehome} == "-1" ]] ; then |
179 | if [[ -z ${ehome} ]] || [[ ${ehome} == "-1" ]] ; then |
| 180 | ehome="/dev/null" |
180 | ehome="/dev/null" |
| 181 | fi |
181 | fi |
| 182 | einfo " - Home: ${ehome}" |
182 | einfo " - Home: ${ehome}" |
| 183 | opts="${opts} -d ${ehome}" |
183 | opts+=" -d ${ehome}" |
| 184 | |
184 | |
| 185 | # handle groups |
185 | # handle groups |
| 186 | local egroups=$1; shift |
186 | local egroups=$1; shift |
| 187 | if [[ ! -z ${egroups} ]] ; then |
187 | if [[ ! -z ${egroups} ]] ; then |
| 188 | local oldifs=${IFS} |
188 | local oldifs=${IFS} |
| … | |
… | |
| 202 | fi |
202 | fi |
| 203 | export IFS="," |
203 | export IFS="," |
| 204 | done |
204 | done |
| 205 | export IFS=${oldifs} |
205 | export IFS=${oldifs} |
| 206 | |
206 | |
| 207 | opts="${opts} -g ${defgroup}" |
207 | opts+=" -g ${defgroup}" |
| 208 | if [[ ! -z ${exgroups} ]] ; then |
208 | if [[ ! -z ${exgroups} ]] ; then |
| 209 | opts="${opts} -G ${exgroups:1}" |
209 | opts+=" -G ${exgroups:1}" |
| 210 | fi |
210 | fi |
| 211 | else |
211 | else |
| 212 | egroups="(none)" |
212 | egroups="(none)" |
| 213 | fi |
213 | fi |
| 214 | einfo " - Groups: ${egroups}" |
214 | einfo " - Groups: ${egroups}" |
| 215 | |
215 | |
| 216 | # handle extra and add the user |
216 | # handle extra args |
| 217 | local oldsandbox=${SANDBOX_ON} |
217 | if [[ $# -gt 0 ]] ; then |
| 218 | export SANDBOX_ON="0" |
218 | die "extra arguments no longer supported; please file a bug" |
|
|
219 | else |
|
|
220 | set -- -c "added by portage for ${PN}" |
|
|
221 | einfo " - Extra: $@" |
|
|
222 | fi |
|
|
223 | |
|
|
224 | # add the user |
| 219 | case ${CHOST} in |
225 | case ${CHOST} in |
| 220 | *-darwin*) |
226 | *-darwin*) |
| 221 | ### Make the user |
227 | ### Make the user |
| 222 | if [[ -z $@ ]] ; then |
|
|
| 223 | dscl . create /users/${euser} uid ${euid} |
228 | dscl . create /users/${euser} uid ${euid} |
| 224 | dscl . create /users/${euser} shell ${eshell} |
229 | dscl . create /users/${euser} shell ${eshell} |
| 225 | dscl . create /users/${euser} home ${ehome} |
230 | dscl . create /users/${euser} home ${ehome} |
| 226 | dscl . create /users/${euser} realname "added by portage for ${PN}" |
231 | dscl . create /users/${euser} realname "added by portage for ${PN}" |
| 227 | ### Add the user to the groups specified |
232 | ### Add the user to the groups specified |
| 228 | local oldifs=${IFS} |
233 | local oldifs=${IFS} |
| 229 | export IFS="," |
234 | export IFS="," |
| 230 | for g in ${egroups} ; do |
235 | for g in ${egroups} ; do |
| 231 | dscl . merge /groups/${g} users ${euser} |
236 | dscl . merge /groups/${g} users ${euser} |
| 232 | done |
237 | done |
| 233 | export IFS=${oldifs} |
238 | export IFS=${oldifs} |
| 234 | else |
|
|
| 235 | einfo "Extra options are not supported on Darwin yet" |
|
|
| 236 | einfo "Please report the ebuild along with the info below" |
|
|
| 237 | einfo "eextra: $@" |
|
|
| 238 | die "Required function missing" |
|
|
| 239 | fi |
|
|
| 240 | ;; |
239 | ;; |
|
|
240 | |
| 241 | *-freebsd*|*-dragonfly*) |
241 | *-freebsd*|*-dragonfly*) |
| 242 | if [[ -z $@ ]] ; then |
|
|
| 243 | pw useradd ${euser} ${opts} \ |
242 | pw useradd ${euser} ${opts} "$@" || die |
| 244 | -c "added by portage for ${PN}" \ |
|
|
| 245 | die "enewuser failed" |
|
|
| 246 | else |
|
|
| 247 | einfo " - Extra: $@" |
|
|
| 248 | pw useradd ${euser} ${opts} \ |
|
|
| 249 | "$@" || die "enewuser failed" |
|
|
| 250 | fi |
|
|
| 251 | ;; |
243 | ;; |
| 252 | |
244 | |
| 253 | *-netbsd*) |
245 | *-netbsd*) |
| 254 | if [[ -z $@ ]] ; then |
|
|
| 255 | useradd ${opts} ${euser} || die "enewuser failed" |
|
|
| 256 | else |
|
|
| 257 | einfo " - Extra: $@" |
|
|
| 258 | useradd ${opts} ${euser} "$@" || die "enewuser failed" |
246 | useradd ${opts} ${euser} "$@" || die |
| 259 | fi |
|
|
| 260 | ;; |
247 | ;; |
| 261 | |
248 | |
| 262 | *-openbsd*) |
249 | *-openbsd*) |
| 263 | if [[ -z $@ ]] ; then |
250 | # all ops the same, except the -g vs -g/-G ... |
| 264 | useradd -u ${euid} -s ${eshell} \ |
251 | useradd -u ${euid} -s ${eshell} \ |
| 265 | -d ${ehome} -c "Added by portage for ${PN}" \ |
252 | -d ${ehome} -g ${egroups} "$@" ${euser} || die |
| 266 | -g ${egroups} ${euser} || die "enewuser failed" |
|
|
| 267 | else |
|
|
| 268 | einfo " - Extra: $@" |
|
|
| 269 | useradd -u ${euid} -s ${eshell} \ |
|
|
| 270 | -d ${ehome} -c "Added by portage for ${PN}" \ |
|
|
| 271 | -g ${egroups} ${euser} "$@" || die "enewuser failed" |
|
|
| 272 | fi |
|
|
| 273 | ;; |
253 | ;; |
| 274 | |
254 | |
| 275 | *) |
255 | *) |
| 276 | if [[ -z $@ ]] ; then |
|
|
| 277 | useradd -r ${opts} \ |
|
|
| 278 | -c "added by portage for ${PN}" \ |
|
|
| 279 | ${euser} \ |
|
|
| 280 | || die "enewuser failed" |
|
|
| 281 | else |
|
|
| 282 | einfo " - Extra: $@" |
|
|
| 283 | useradd -r ${opts} "$@" \ |
256 | useradd -r ${opts} "$@" ${euser} || die |
| 284 | ${euser} \ |
|
|
| 285 | || die "enewuser failed" |
|
|
| 286 | fi |
|
|
| 287 | ;; |
257 | ;; |
| 288 | esac |
258 | esac |
| 289 | |
259 | |
| 290 | if [[ ! -e ${ROOT}/${ehome} ]] ; then |
260 | if [[ ! -e ${ROOT}/${ehome} ]] ; then |
| 291 | einfo " - Creating ${ehome} in ${ROOT}" |
261 | einfo " - Creating ${ehome} in ${ROOT}" |
| 292 | mkdir -p "${ROOT}/${ehome}" |
262 | mkdir -p "${ROOT}/${ehome}" |
| 293 | chown ${euser} "${ROOT}/${ehome}" |
263 | chown ${euser} "${ROOT}/${ehome}" |
| 294 | chmod 755 "${ROOT}/${ehome}" |
264 | chmod 755 "${ROOT}/${ehome}" |
| 295 | fi |
265 | fi |
| 296 | |
|
|
| 297 | export SANDBOX_ON=${oldsandbox} |
|
|
| 298 | } |
266 | } |
| 299 | |
267 | |
| 300 | # @FUNCTION: enewgroup |
268 | # @FUNCTION: enewgroup |
| 301 | # @USAGE: <group> [gid] |
269 | # @USAGE: <group> [gid] |
| 302 | # @DESCRIPTION: |
270 | # @DESCRIPTION: |
| … | |
… | |
| 331 | if [ "${egid}" -gt 0 ] |
299 | if [ "${egid}" -gt 0 ] |
| 332 | then |
300 | then |
| 333 | if [ -z "`egetent group ${egid}`" ] |
301 | if [ -z "`egetent group ${egid}`" ] |
| 334 | then |
302 | then |
| 335 | if [[ "${CHOST}" == *-darwin* ]]; then |
303 | if [[ "${CHOST}" == *-darwin* ]]; then |
| 336 | opts="${opts} ${egid}" |
304 | opts+=" ${egid}" |
| 337 | else |
305 | else |
| 338 | opts="${opts} -g ${egid}" |
306 | opts+=" -g ${egid}" |
| 339 | fi |
307 | fi |
| 340 | else |
308 | else |
| 341 | egid="next available; requested gid taken" |
309 | egid="next available; requested gid taken" |
| 342 | fi |
310 | fi |
| 343 | else |
311 | else |
| … | |
… | |
| 348 | egid="next available" |
316 | egid="next available" |
| 349 | fi |
317 | fi |
| 350 | einfo " - Groupid: ${egid}" |
318 | einfo " - Groupid: ${egid}" |
| 351 | |
319 | |
| 352 | # handle extra |
320 | # handle extra |
| 353 | local eextra="$@" |
321 | if [ $# -gt 0 ] ; then |
| 354 | opts="${opts} ${eextra}" |
322 | die "extra arguments no longer supported; please file a bug" |
|
|
323 | fi |
| 355 | |
324 | |
| 356 | # add the group |
325 | # add the group |
| 357 | local oldsandbox="${SANDBOX_ON}" |
|
|
| 358 | export SANDBOX_ON="0" |
|
|
| 359 | case ${CHOST} in |
326 | case ${CHOST} in |
| 360 | *-darwin*) |
327 | *-darwin*) |
| 361 | if [ ! -z "${eextra}" ]; |
|
|
| 362 | then |
|
|
| 363 | einfo "Extra options are not supported on Darwin/OS X yet" |
|
|
| 364 | einfo "Please report the ebuild along with the info below" |
|
|
| 365 | einfo "eextra: ${eextra}" |
|
|
| 366 | die "Required function missing" |
|
|
| 367 | fi |
|
|
| 368 | |
|
|
| 369 | # If we need the next available |
328 | # If we need the next available |
| 370 | case ${egid} in |
329 | case ${egid} in |
| 371 | *[!0-9]*) # Non numeric |
330 | *[!0-9]*) # Non numeric |
| 372 | for ((egid = 101; egid <= 999; egid++)); do |
331 | for ((egid = 101; egid <= 999; egid++)); do |
| 373 | [[ -z $(egetent group ${egid}) ]] && break |
332 | [[ -z $(egetent group ${egid}) ]] && break |
| … | |
… | |
| 382 | *[!0-9]*) # Non numeric |
341 | *[!0-9]*) # Non numeric |
| 383 | for ((egid = 101; egid <= 999; egid++)); do |
342 | for ((egid = 101; egid <= 999; egid++)); do |
| 384 | [[ -z $(egetent group ${egid}) ]] && break |
343 | [[ -z $(egetent group ${egid}) ]] && break |
| 385 | done |
344 | done |
| 386 | esac |
345 | esac |
| 387 | pw groupadd ${egroup} -g ${egid} || die "enewgroup failed" |
346 | pw groupadd ${egroup} -g ${egid} || die |
| 388 | ;; |
347 | ;; |
| 389 | |
348 | |
| 390 | *-netbsd*) |
349 | *-netbsd*) |
| 391 | case ${egid} in |
350 | case ${egid} in |
| 392 | *[!0-9]*) # Non numeric |
351 | *[!0-9]*) # Non numeric |
| 393 | for ((egid = 101; egid <= 999; egid++)); do |
352 | for ((egid = 101; egid <= 999; egid++)); do |
| 394 | [[ -z $(egetent group ${egid}) ]] && break |
353 | [[ -z $(egetent group ${egid}) ]] && break |
| 395 | done |
354 | done |
| 396 | esac |
355 | esac |
| 397 | groupadd -g ${egid} ${egroup} || die "enewgroup failed" |
356 | groupadd -g ${egid} ${egroup} || die |
| 398 | ;; |
357 | ;; |
| 399 | |
358 | |
| 400 | *) |
359 | *) |
| 401 | # We specify -r so that we get a GID in the system range from login.defs |
360 | # We specify -r so that we get a GID in the system range from login.defs |
| 402 | groupadd -r ${opts} ${egroup} || die "enewgroup failed" |
361 | groupadd -r ${opts} ${egroup} || die |
| 403 | ;; |
362 | ;; |
| 404 | esac |
363 | esac |
| 405 | export SANDBOX_ON="${oldsandbox}" |
|
|
| 406 | } |
364 | } |
| 407 | |
365 | |
| 408 | # @FUNCTION: egethome |
366 | # @FUNCTION: egethome |
| 409 | # @USAGE: <user> |
367 | # @USAGE: <user> |
| 410 | # @DESCRIPTION: |
368 | # @DESCRIPTION: |
| 411 | # Gets the home directory for the specified user. |
369 | # Gets the home directory for the specified user. |
| 412 | egethome() { |
370 | egethome() { |
| 413 | local pos |
371 | local pos |
| 414 | |
372 | |
|
|
373 | [[ $# -eq 1 ]] || die "usage: egethome <user>" |
|
|
374 | |
| 415 | case ${CHOST} in |
375 | case ${CHOST} in |
| 416 | *-darwin*|*-freebsd*|*-dragonfly*) |
376 | *-darwin*|*-freebsd*|*-dragonfly*) |
| 417 | pos=9 |
377 | pos=9 |
| 418 | ;; |
378 | ;; |
| 419 | *) # Linux, NetBSD, OpenBSD, etc... |
379 | *) # Linux, NetBSD, OpenBSD, etc... |
| … | |
… | |
| 421 | ;; |
381 | ;; |
| 422 | esac |
382 | esac |
| 423 | |
383 | |
| 424 | egetent passwd $1 | cut -d: -f${pos} |
384 | egetent passwd $1 | cut -d: -f${pos} |
| 425 | } |
385 | } |
|
|
386 | |
|
|
387 | # @FUNCTION: egetshell |
|
|
388 | # @USAGE: <user> |
|
|
389 | # @DESCRIPTION: |
|
|
390 | # Gets the shell for the specified user. |
|
|
391 | egetshell() { |
|
|
392 | local pos |
|
|
393 | |
|
|
394 | [[ $# -eq 1 ]] || die "usage: egetshell <user>" |
|
|
395 | |
|
|
396 | case ${CHOST} in |
|
|
397 | *-darwin*|*-freebsd*|*-dragonfly*) |
|
|
398 | pos=10 |
|
|
399 | ;; |
|
|
400 | *) # Linux, NetBSD, OpenBSD, etc... |
|
|
401 | pos=7 |
|
|
402 | ;; |
|
|
403 | esac |
|
|
404 | |
|
|
405 | egetent passwd "$1" | cut -d: -f${pos} |
|
|
406 | } |