| 1 |
# Copyright 1999-2011 Gentoo Foundation
|
| 2 |
# Distributed under the terms of the GNU General Public License v2
|
| 3 |
# $Header: /var/cvsroot/gentoo-x86/eclass/user.eclass,v 1.14 2011/11/26 07:20:31 vapier Exp $
|
| 4 |
|
| 5 |
# @ECLASS: user.eclass
|
| 6 |
# @MAINTAINER:
|
| 7 |
# base-system@gentoo.org (Linux)
|
| 8 |
# Joe Jezak <josejx@gmail.com> (OS X)
|
| 9 |
# usata@gentoo.org (OS X)
|
| 10 |
# Aaron Walker <ka0ttic@gentoo.org> (FreeBSD)
|
| 11 |
# @BLURB: user management in ebuilds
|
| 12 |
# @DESCRIPTION:
|
| 13 |
# The user eclass contains a suite of functions that allow ebuilds
|
| 14 |
# to quickly make sure users in the installed system are sane.
|
| 15 |
|
| 16 |
# @FUNCTION: _assert_pkg_ebuild_phase
|
| 17 |
# @INTERNAL
|
| 18 |
# @USAGE: <calling func name>
|
| 19 |
_assert_pkg_ebuild_phase() {
|
| 20 |
case ${EBUILD_PHASE} in
|
| 21 |
setup|preinst|postinst) ;;
|
| 22 |
*)
|
| 23 |
eerror "'$1()' called from '${EBUILD_PHASE}' phase which is not OK:"
|
| 24 |
eerror "You may only call from pkg_{setup,preinst,postinst} functions."
|
| 25 |
eerror "Package fails at QA and at life. Please file a bug."
|
| 26 |
die "Bad package! $1 is only for use in some pkg_* functions!"
|
| 27 |
esac
|
| 28 |
}
|
| 29 |
|
| 30 |
# @FUNCTION: egetent
|
| 31 |
# @USAGE: <database> <key>
|
| 32 |
# @DESCRIPTION:
|
| 33 |
# Small wrapper for getent (Linux), nidump (< Mac OS X 10.5),
|
| 34 |
# dscl (Mac OS X 10.5), and pw (FreeBSD) used in enewuser()/enewgroup().
|
| 35 |
#
|
| 36 |
# Supported databases: group passwd
|
| 37 |
egetent() {
|
| 38 |
local db=$1 key=$2
|
| 39 |
|
| 40 |
[[ $# -ge 3 ]] && die "usage: egetent <database> <key>"
|
| 41 |
|
| 42 |
case ${db} in
|
| 43 |
passwd|group) ;;
|
| 44 |
*) die "sorry, database '${db}' not yet supported; file a bug" ;;
|
| 45 |
esac
|
| 46 |
|
| 47 |
case ${CHOST} in
|
| 48 |
*-darwin[678])
|
| 49 |
case ${key} in
|
| 50 |
*[!0-9]*) # Non numeric
|
| 51 |
nidump ${db} . | awk -F: "(\$1 ~ /^${key}\$/) {print;exit;}"
|
| 52 |
;;
|
| 53 |
*) # Numeric
|
| 54 |
nidump ${db} . | awk -F: "(\$3 == ${key}) {print;exit;}"
|
| 55 |
;;
|
| 56 |
esac
|
| 57 |
;;
|
| 58 |
*-darwin*)
|
| 59 |
local mykey
|
| 60 |
case ${db} in
|
| 61 |
passwd) db="Users" mykey="UniqueID" ;;
|
| 62 |
group) db="Groups" mykey="PrimaryGroupID" ;;
|
| 63 |
esac
|
| 64 |
|
| 65 |
case ${key} in
|
| 66 |
*[!0-9]*) # Non numeric
|
| 67 |
dscl . -read /${db}/${key} 2>/dev/null |grep RecordName
|
| 68 |
;;
|
| 69 |
*) # Numeric
|
| 70 |
dscl . -search /${db} ${mykey} ${key} 2>/dev/null
|
| 71 |
;;
|
| 72 |
esac
|
| 73 |
;;
|
| 74 |
*-freebsd*|*-dragonfly*)
|
| 75 |
case ${db} in
|
| 76 |
passwd) db="user" ;;
|
| 77 |
*) ;;
|
| 78 |
esac
|
| 79 |
|
| 80 |
# lookup by uid/gid
|
| 81 |
local opts
|
| 82 |
if [[ ${key} == [[:digit:]]* ]] ; then
|
| 83 |
[[ ${db} == "user" ]] && opts="-u" || opts="-g"
|
| 84 |
fi
|
| 85 |
|
| 86 |
pw show ${db} ${opts} "${key}" -q
|
| 87 |
;;
|
| 88 |
*-netbsd*|*-openbsd*)
|
| 89 |
grep "${key}:\*:" /etc/${db}
|
| 90 |
;;
|
| 91 |
*)
|
| 92 |
# ignore output if nscd doesn't exist, or we're not running as root
|
| 93 |
nscd -i "${db}" 2>/dev/null
|
| 94 |
getent "${db}" "${key}"
|
| 95 |
;;
|
| 96 |
esac
|
| 97 |
}
|
| 98 |
|
| 99 |
# @FUNCTION: enewuser
|
| 100 |
# @USAGE: <user> [uid] [shell] [homedir] [groups]
|
| 101 |
# @DESCRIPTION:
|
| 102 |
# Same as enewgroup, you are not required to understand how to properly add
|
| 103 |
# a user to the system. The only required parameter is the username.
|
| 104 |
# Default uid is (pass -1 for this) next available, default shell is
|
| 105 |
# /bin/false, default homedir is /dev/null, and there are no default groups.
|
| 106 |
enewuser() {
|
| 107 |
_assert_pkg_ebuild_phase ${FUNCNAME}
|
| 108 |
|
| 109 |
# get the username
|
| 110 |
local euser=$1; shift
|
| 111 |
if [[ -z ${euser} ]] ; then
|
| 112 |
eerror "No username specified !"
|
| 113 |
die "Cannot call enewuser without a username"
|
| 114 |
fi
|
| 115 |
|
| 116 |
# lets see if the username already exists
|
| 117 |
if [[ -n $(egetent passwd "${euser}") ]] ; then
|
| 118 |
return 0
|
| 119 |
fi
|
| 120 |
einfo "Adding user '${euser}' to your system ..."
|
| 121 |
|
| 122 |
# options to pass to useradd
|
| 123 |
local opts=()
|
| 124 |
|
| 125 |
# handle uid
|
| 126 |
local euid=$1; shift
|
| 127 |
if [[ -n ${euid} && ${euid} != -1 ]] ; then
|
| 128 |
if [[ ${euid} -gt 0 ]] ; then
|
| 129 |
if [[ -n $(egetent passwd ${euid}) ]] ; then
|
| 130 |
euid="next"
|
| 131 |
fi
|
| 132 |
else
|
| 133 |
eerror "Userid given but is not greater than 0 !"
|
| 134 |
die "${euid} is not a valid UID"
|
| 135 |
fi
|
| 136 |
else
|
| 137 |
euid="next"
|
| 138 |
fi
|
| 139 |
if [[ ${euid} == "next" ]] ; then
|
| 140 |
for ((euid = 101; euid <= 999; euid++)); do
|
| 141 |
[[ -z $(egetent passwd ${euid}) ]] && break
|
| 142 |
done
|
| 143 |
fi
|
| 144 |
opts+=( -u ${euid} )
|
| 145 |
einfo " - Userid: ${euid}"
|
| 146 |
|
| 147 |
# handle shell
|
| 148 |
local eshell=$1; shift
|
| 149 |
if [[ ! -z ${eshell} ]] && [[ ${eshell} != "-1" ]] ; then
|
| 150 |
if [[ ! -e ${ROOT}${eshell} ]] ; then
|
| 151 |
eerror "A shell was specified but it does not exist !"
|
| 152 |
die "${eshell} does not exist in ${ROOT}"
|
| 153 |
fi
|
| 154 |
if [[ ${eshell} == */false || ${eshell} == */nologin ]] ; then
|
| 155 |
eerror "Do not specify ${eshell} yourself, use -1"
|
| 156 |
die "Pass '-1' as the shell parameter"
|
| 157 |
fi
|
| 158 |
else
|
| 159 |
for eshell in /sbin/nologin /usr/sbin/nologin /bin/false /usr/bin/false /dev/null ; do
|
| 160 |
[[ -x ${ROOT}${eshell} ]] && break
|
| 161 |
done
|
| 162 |
|
| 163 |
if [[ ${eshell} == "/dev/null" ]] ; then
|
| 164 |
eerror "Unable to identify the shell to use, proceeding with userland default."
|
| 165 |
case ${USERLAND} in
|
| 166 |
GNU) eshell="/bin/false" ;;
|
| 167 |
BSD) eshell="/sbin/nologin" ;;
|
| 168 |
Darwin) eshell="/usr/sbin/nologin" ;;
|
| 169 |
*) die "Unable to identify the default shell for userland ${USERLAND}"
|
| 170 |
esac
|
| 171 |
fi
|
| 172 |
fi
|
| 173 |
einfo " - Shell: ${eshell}"
|
| 174 |
opts+=( -s "${eshell}" )
|
| 175 |
|
| 176 |
# handle homedir
|
| 177 |
local ehome=$1; shift
|
| 178 |
if [[ -z ${ehome} ]] || [[ ${ehome} == "-1" ]] ; then
|
| 179 |
ehome="/dev/null"
|
| 180 |
fi
|
| 181 |
einfo " - Home: ${ehome}"
|
| 182 |
opts+=( -d "${ehome}" )
|
| 183 |
|
| 184 |
# handle groups
|
| 185 |
local egroups=$1; shift
|
| 186 |
local g egroups_arr
|
| 187 |
IFS="," read -r -a egroups_arr <<<"${egroups}"
|
| 188 |
shift
|
| 189 |
if [[ ${#egroups_arr[@]} -gt 0 ]] ; then
|
| 190 |
local defgroup exgroups
|
| 191 |
for g in "${egroups_arr[@]}" ; do
|
| 192 |
if [[ -z $(egetent group "${g}") ]] ; then
|
| 193 |
eerror "You must add group ${g} to the system first"
|
| 194 |
die "${g} is not a valid GID"
|
| 195 |
fi
|
| 196 |
if [[ -z ${defgroup} ]] ; then
|
| 197 |
defgroup=${g}
|
| 198 |
else
|
| 199 |
exgroups+=",${g}"
|
| 200 |
fi
|
| 201 |
done
|
| 202 |
opts+=( -g "${defgroup}" )
|
| 203 |
if [[ ! -z ${exgroups} ]] ; then
|
| 204 |
opts+=( -G "${exgroups:1}" )
|
| 205 |
fi
|
| 206 |
fi
|
| 207 |
einfo " - Groups: ${egroups:-(none)}"
|
| 208 |
|
| 209 |
# handle extra args
|
| 210 |
if [[ $# -gt 0 ]] ; then
|
| 211 |
die "extra arguments no longer supported; please file a bug"
|
| 212 |
else
|
| 213 |
local comment="added by portage for ${PN}"
|
| 214 |
opts+=( -c "${comment}" )
|
| 215 |
einfo " - GECOS: ${comment}"
|
| 216 |
fi
|
| 217 |
|
| 218 |
# add the user
|
| 219 |
case ${CHOST} in
|
| 220 |
*-darwin*)
|
| 221 |
### Make the user
|
| 222 |
dscl . create "/users/${euser}" uid ${euid}
|
| 223 |
dscl . create "/users/${euser}" shell "${eshell}"
|
| 224 |
dscl . create "/users/${euser}" home "${ehome}"
|
| 225 |
dscl . create "/users/${euser}" realname "added by portage for ${PN}"
|
| 226 |
### Add the user to the groups specified
|
| 227 |
for g in "${egroups_arr[@]}" ; do
|
| 228 |
dscl . merge "/groups/${g}" users "${euser}"
|
| 229 |
done
|
| 230 |
;;
|
| 231 |
|
| 232 |
*-freebsd*|*-dragonfly*)
|
| 233 |
pw useradd "${euser}" "${opts[@]}" || die
|
| 234 |
;;
|
| 235 |
|
| 236 |
*-netbsd*)
|
| 237 |
useradd "${opts[@]}" "${euser}" || die
|
| 238 |
;;
|
| 239 |
|
| 240 |
*-openbsd*)
|
| 241 |
# all ops the same, except the -g vs -g/-G ...
|
| 242 |
useradd -u ${euid} -s "${eshell}" \
|
| 243 |
-d "${ehome}" -g "${egroups}" "${euser}" || die
|
| 244 |
;;
|
| 245 |
|
| 246 |
*)
|
| 247 |
useradd -r "${opts[@]}" "${euser}" || die
|
| 248 |
;;
|
| 249 |
esac
|
| 250 |
|
| 251 |
if [[ ! -e ${ROOT}/${ehome} ]] ; then
|
| 252 |
einfo " - Creating ${ehome} in ${ROOT}"
|
| 253 |
mkdir -p "${ROOT}/${ehome}"
|
| 254 |
chown "${euser}" "${ROOT}/${ehome}"
|
| 255 |
chmod 755 "${ROOT}/${ehome}"
|
| 256 |
fi
|
| 257 |
}
|
| 258 |
|
| 259 |
# @FUNCTION: enewgroup
|
| 260 |
# @USAGE: <group> [gid]
|
| 261 |
# @DESCRIPTION:
|
| 262 |
# This function does not require you to understand how to properly add a
|
| 263 |
# group to the system. Just give it a group name to add and enewgroup will
|
| 264 |
# do the rest. You may specify the gid for the group or allow the group to
|
| 265 |
# allocate the next available one.
|
| 266 |
enewgroup() {
|
| 267 |
_assert_pkg_ebuild_phase ${FUNCNAME}
|
| 268 |
|
| 269 |
# get the group
|
| 270 |
local egroup=$1; shift
|
| 271 |
if [[ -z ${egroup} ]] ; then
|
| 272 |
eerror "No group specified !"
|
| 273 |
die "Cannot call enewgroup without a group"
|
| 274 |
fi
|
| 275 |
|
| 276 |
# see if group already exists
|
| 277 |
if [[ -n $(egetent group "${egroup}") ]] ; then
|
| 278 |
return 0
|
| 279 |
fi
|
| 280 |
einfo "Adding group '${egroup}' to your system ..."
|
| 281 |
|
| 282 |
# handle gid
|
| 283 |
local egid=$1; shift
|
| 284 |
if [[ ! -z ${egid} ]] ; then
|
| 285 |
if [[ ${egid} -gt 0 ]] ; then
|
| 286 |
if [[ -n $(egetent group ${egid}) ]] ; then
|
| 287 |
egid="next available; requested gid taken"
|
| 288 |
fi
|
| 289 |
else
|
| 290 |
eerror "Groupid given but is not greater than 0 !"
|
| 291 |
die "${egid} is not a valid GID"
|
| 292 |
fi
|
| 293 |
else
|
| 294 |
egid="next available"
|
| 295 |
fi
|
| 296 |
einfo " - Groupid: ${egid}"
|
| 297 |
|
| 298 |
# handle extra
|
| 299 |
if [[ $# -gt 0 ]] ; then
|
| 300 |
die "extra arguments no longer supported; please file a bug"
|
| 301 |
fi
|
| 302 |
|
| 303 |
# Some targets need to find the next available GID manually
|
| 304 |
_enewgroup_next_gid() {
|
| 305 |
if [[ ${egid} == *[!0-9]* ]] ; then
|
| 306 |
# Non numeric
|
| 307 |
for ((egid = 101; egid <= 999; egid++)) ; do
|
| 308 |
[[ -z $(egetent group ${egid}) ]] && break
|
| 309 |
done
|
| 310 |
fi
|
| 311 |
}
|
| 312 |
|
| 313 |
# add the group
|
| 314 |
case ${CHOST} in
|
| 315 |
*-darwin*)
|
| 316 |
_enewgroup_next_gid
|
| 317 |
dscl . create "/groups/${egroup}" gid ${egid}
|
| 318 |
dscl . create "/groups/${egroup}" passwd '*'
|
| 319 |
;;
|
| 320 |
|
| 321 |
*-freebsd*|*-dragonfly*)
|
| 322 |
_enewgroup_next_gid
|
| 323 |
pw groupadd "${egroup}" -g ${egid} || die
|
| 324 |
;;
|
| 325 |
|
| 326 |
*-netbsd*)
|
| 327 |
_enewgroup_next_gid
|
| 328 |
groupadd -g ${egid} "${egroup}" || die
|
| 329 |
;;
|
| 330 |
|
| 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
|
| 339 |
# We specify -r so that we get a GID in the system range from login.defs
|
| 340 |
groupadd -r ${opts} "${egroup}" || die
|
| 341 |
;;
|
| 342 |
esac
|
| 343 |
}
|
| 344 |
|
| 345 |
# @FUNCTION: egethome
|
| 346 |
# @USAGE: <user>
|
| 347 |
# @DESCRIPTION:
|
| 348 |
# Gets the home directory for the specified user.
|
| 349 |
egethome() {
|
| 350 |
local pos
|
| 351 |
|
| 352 |
[[ $# -eq 1 ]] || die "usage: egethome <user>"
|
| 353 |
|
| 354 |
case ${CHOST} in
|
| 355 |
*-darwin*|*-freebsd*|*-dragonfly*)
|
| 356 |
pos=9
|
| 357 |
;;
|
| 358 |
*) # Linux, NetBSD, OpenBSD, etc...
|
| 359 |
pos=6
|
| 360 |
;;
|
| 361 |
esac
|
| 362 |
|
| 363 |
egetent passwd "$1" | cut -d: -f${pos}
|
| 364 |
}
|
| 365 |
|
| 366 |
# @FUNCTION: egetshell
|
| 367 |
# @USAGE: <user>
|
| 368 |
# @DESCRIPTION:
|
| 369 |
# Gets the shell for the specified user.
|
| 370 |
egetshell() {
|
| 371 |
local pos
|
| 372 |
|
| 373 |
[[ $# -eq 1 ]] || die "usage: egetshell <user>"
|
| 374 |
|
| 375 |
case ${CHOST} in
|
| 376 |
*-darwin*|*-freebsd*|*-dragonfly*)
|
| 377 |
pos=10
|
| 378 |
;;
|
| 379 |
*) # Linux, NetBSD, OpenBSD, etc...
|
| 380 |
pos=7
|
| 381 |
;;
|
| 382 |
esac
|
| 383 |
|
| 384 |
egetent passwd "$1" | cut -d: -f${pos}
|
| 385 |
}
|