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

Diff of /eclass/user.eclass

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

Revision 1.1 Revision 1.21
1# Copyright 1999-2011 Gentoo Foundation 1# Copyright 1999-2012 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.1 2011/10/27 07:16:08 vapier Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/user.eclass,v 1.21 2012/06/22 18:57:33 axs 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)
11# @BLURB: user management in ebuilds 11# @BLURB: user management in ebuilds
12# @DESCRIPTION: 12# @DESCRIPTION:
13# The user eclass contains a suite of functions that allow ebuilds 13# The user eclass contains a suite of functions that allow ebuilds
14# to quickly make sure users in the installed system are sane. 14# to quickly make sure users in the installed system are sane.
15 15
16if [[ ${___ECLASS_ONCE_USER} != "recur -_+^+_- spank" ]] ; then
17___ECLASS_ONCE_USER="recur -_+^+_- spank"
18
19# @FUNCTION: _assert_pkg_ebuild_phase
20# @INTERNAL
21# @USAGE: <calling func name>
22_assert_pkg_ebuild_phase() {
23 case ${EBUILD_PHASE} in
24 setup|preinst|postinst) ;;
25 *)
26 eerror "'$1()' called from '${EBUILD_PHASE}' phase which is not OK:"
27 eerror "You may only call from pkg_{setup,preinst,postinst} functions."
28 eerror "Package fails at QA and at life. Please file a bug."
29 die "Bad package! $1 is only for use in some pkg_* functions!"
30 esac
31}
32
16# @FUNCTION: egetent 33# @FUNCTION: egetent
17# @USAGE: <database> <key> 34# @USAGE: <database> <key>
18# @DESCRIPTION: 35# @DESCRIPTION:
19# Small wrapper for getent (Linux), nidump (< Mac OS X 10.5), 36# Small wrapper for getent (Linux), nidump (< Mac OS X 10.5),
20# dscl (Mac OS X 10.5), and pw (FreeBSD) used in enewuser()/enewgroup(). 37# dscl (Mac OS X 10.5), and pw (FreeBSD) used in enewuser()/enewgroup().
38#
39# Supported databases: group passwd
21egetent() { 40egetent() {
41 local db=$1 key=$2
42
43 [[ $# -ge 3 ]] && die "usage: egetent <database> <key>"
44
45 case ${db} in
46 passwd|group) ;;
47 *) die "sorry, database '${db}' not yet supported; file a bug" ;;
48 esac
49
22 case ${CHOST} in 50 case ${CHOST} in
23 *-darwin[678]) 51 *-darwin[678])
24 case "$2" in 52 case ${key} in
25 *[!0-9]*) # Non numeric 53 *[!0-9]*) # Non numeric
26 nidump $1 . | awk -F":" "{ if (\$1 ~ /^$2\$/) {print \$0;exit;} }" 54 nidump ${db} . | awk -F: "(\$1 ~ /^${key}\$/) {print;exit;}"
27 ;; 55 ;;
28 *) # Numeric 56 *) # Numeric
29 nidump $1 . | awk -F":" "{ if (\$3 == $2) {print \$0;exit;} }" 57 nidump ${db} . | awk -F: "(\$3 == ${key}) {print;exit;}"
30 ;; 58 ;;
31 esac 59 esac
32 ;; 60 ;;
33 *-darwin*) 61 *-darwin*)
34 local mytype=$1 62 local mykey
35 [[ "passwd" == $mytype ]] && mytype="Users"
36 [[ "group" == $mytype ]] && mytype="Groups"
37 case "$2" in 63 case ${db} in
64 passwd) db="Users" mykey="UniqueID" ;;
65 group) db="Groups" mykey="PrimaryGroupID" ;;
66 esac
67
68 case ${key} in
38 *[!0-9]*) # Non numeric 69 *[!0-9]*) # Non numeric
39 dscl . -read /$mytype/$2 2>/dev/null |grep RecordName 70 dscl . -read /${db}/${key} 2>/dev/null |grep RecordName
40 ;; 71 ;;
41 *) # Numeric 72 *) # Numeric
42 local mykey="UniqueID"
43 [[ $mytype == "Groups" ]] && mykey="PrimaryGroupID"
44 dscl . -search /$mytype $mykey $2 2>/dev/null 73 dscl . -search /${db} ${mykey} ${key} 2>/dev/null
45 ;; 74 ;;
46 esac 75 esac
47 ;; 76 ;;
48 *-freebsd*|*-dragonfly*) 77 *-freebsd*|*-dragonfly*)
49 local opts action="user" 78 case ${db} in
50 [[ $1 == "passwd" ]] || action="group" 79 passwd) db="user" ;;
80 *) ;;
81 esac
51 82
52 # lookup by uid/gid 83 # lookup by uid/gid
84 local opts
53 if [[ $2 == [[:digit:]]* ]] ; then 85 if [[ ${key} == [[:digit:]]* ]] ; then
54 [[ ${action} == "user" ]] && opts="-u" || opts="-g" 86 [[ ${db} == "user" ]] && opts="-u" || opts="-g"
55 fi 87 fi
56 88
57 pw show ${action} ${opts} "$2" -q 89 pw show ${db} ${opts} "${key}" -q
58 ;; 90 ;;
59 *-netbsd*|*-openbsd*) 91 *-netbsd*|*-openbsd*)
60 grep "$2:\*:" /etc/$1 92 grep "${key}:\*:" /etc/${db}
61 ;; 93 ;;
62 *) 94 *)
63 type -p nscd >& /dev/null && nscd -i "$1" 95 # ignore output if nscd doesn't exist, or we're not running as root
64 getent "$1" "$2" 96 nscd -i "${db}" 2>/dev/null
97 getent "${db}" "${key}"
65 ;; 98 ;;
66 esac 99 esac
67} 100}
68 101
69# @FUNCTION: enewuser 102# @FUNCTION: enewuser
70# @USAGE: <user> [uid] [shell] [homedir] [groups] [params] 103# @USAGE: <user> [uid] [shell] [homedir] [groups]
71# @DESCRIPTION: 104# @DESCRIPTION:
72# Same as enewgroup, you are not required to understand how to properly add 105# Same as enewgroup, you are not required to understand how to properly add
73# a user to the system. The only required parameter is the username. 106# a user to the system. The only required parameter is the username.
74# Default uid is (pass -1 for this) next available, default shell is 107# Default uid is (pass -1 for this) next available, default shell is
75# /bin/false, default homedir is /dev/null, there are no default groups, 108# /bin/false, default homedir is /dev/null, and there are no default groups.
76# and default params sets the comment as 'added by portage for ${PN}'.
77enewuser() { 109enewuser() {
78 case ${EBUILD_PHASE} in 110 _assert_pkg_ebuild_phase ${FUNCNAME}
79 unpack|compile|test|install)
80 eerror "'enewuser()' called from '${EBUILD_PHASE}()' which is not a pkg_* function."
81 eerror "Package fails at QA and at life. Please file a bug."
82 die "Bad package! enewuser is only for use in pkg_* functions!"
83 esac
84 111
85 # get the username 112 # get the username
86 local euser=$1; shift 113 local euser=$1; shift
87 if [[ -z ${euser} ]] ; then 114 if [[ -z ${euser} ]] ; then
88 eerror "No username specified !" 115 eerror "No username specified !"
94 return 0 121 return 0
95 fi 122 fi
96 einfo "Adding user '${euser}' to your system ..." 123 einfo "Adding user '${euser}' to your system ..."
97 124
98 # options to pass to useradd 125 # options to pass to useradd
99 local opts= 126 local opts=()
100 127
101 # handle uid 128 # handle uid
102 local euid=$1; shift 129 local euid=$1; shift
103 if [[ -n ${euid} && ${euid} != -1 ]] ; then 130 if [[ -n ${euid} && ${euid} != -1 ]] ; then
104 if [[ ${euid} -gt 0 ]] ; then 131 if [[ ${euid} -gt 0 ]] ; then
115 if [[ ${euid} == "next" ]] ; then 142 if [[ ${euid} == "next" ]] ; then
116 for ((euid = 101; euid <= 999; euid++)); do 143 for ((euid = 101; euid <= 999; euid++)); do
117 [[ -z $(egetent passwd ${euid}) ]] && break 144 [[ -z $(egetent passwd ${euid}) ]] && break
118 done 145 done
119 fi 146 fi
120 opts="${opts} -u ${euid}" 147 opts+=( -u ${euid} )
121 einfo " - Userid: ${euid}" 148 einfo " - Userid: ${euid}"
122 149
123 # handle shell 150 # handle shell
124 local eshell=$1; shift 151 local eshell=$1; shift
125 if [[ ! -z ${eshell} ]] && [[ ${eshell} != "-1" ]] ; then 152 if [[ ! -z ${eshell} ]] && [[ ${eshell} != "-1" ]] ; then
130 if [[ ${eshell} == */false || ${eshell} == */nologin ]] ; then 157 if [[ ${eshell} == */false || ${eshell} == */nologin ]] ; then
131 eerror "Do not specify ${eshell} yourself, use -1" 158 eerror "Do not specify ${eshell} yourself, use -1"
132 die "Pass '-1' as the shell parameter" 159 die "Pass '-1' as the shell parameter"
133 fi 160 fi
134 else 161 else
135 for shell in /sbin/nologin /usr/sbin/nologin /bin/false /usr/bin/false /dev/null ; do 162 for eshell in /sbin/nologin /usr/sbin/nologin /bin/false /usr/bin/false /dev/null ; do
136 [[ -x ${ROOT}${shell} ]] && break 163 [[ -x ${ROOT}${eshell} ]] && break
137 done 164 done
138 165
139 if [[ ${shell} == "/dev/null" ]] ; then 166 if [[ ${eshell} == "/dev/null" ]] ; then
140 eerror "Unable to identify the shell to use, proceeding with userland default." 167 eerror "Unable to identify the shell to use, proceeding with userland default."
141 case ${USERLAND} in 168 case ${USERLAND} in
142 GNU) shell="/bin/false" ;; 169 GNU) eshell="/bin/false" ;;
143 BSD) shell="/sbin/nologin" ;; 170 BSD) eshell="/sbin/nologin" ;;
144 Darwin) shell="/usr/sbin/nologin" ;; 171 Darwin) eshell="/usr/sbin/nologin" ;;
145 *) die "Unable to identify the default shell for userland ${USERLAND}" 172 *) die "Unable to identify the default shell for userland ${USERLAND}"
146 esac 173 esac
147 fi 174 fi
148
149 eshell=${shell}
150 fi 175 fi
151 einfo " - Shell: ${eshell}" 176 einfo " - Shell: ${eshell}"
152 opts="${opts} -s ${eshell}" 177 opts+=( -s "${eshell}" )
153 178
154 # handle homedir 179 # handle homedir
155 local ehome=$1; shift 180 local ehome=$1; shift
156 if [[ -z ${ehome} ]] || [[ ${ehome} == "-1" ]] ; then 181 if [[ -z ${ehome} ]] || [[ ${ehome} == "-1" ]] ; then
157 ehome="/dev/null" 182 ehome="/dev/null"
158 fi 183 fi
159 einfo " - Home: ${ehome}" 184 einfo " - Home: ${ehome}"
160 opts="${opts} -d ${ehome}" 185 opts+=( -d "${ehome}" )
161 186
162 # handle groups 187 # handle groups
163 local egroups=$1; shift 188 local egroups=$1; shift
164 if [[ ! -z ${egroups} ]] ; then 189 local g egroups_arr
165 local oldifs=${IFS} 190 IFS="," read -r -a egroups_arr <<<"${egroups}"
191 shift
192 if [[ ${#egroups_arr[@]} -gt 0 ]] ; then
166 local defgroup="" exgroups="" 193 local defgroup exgroups
167
168 export IFS=","
169 for g in ${egroups} ; do 194 for g in "${egroups_arr[@]}" ; do
170 export IFS=${oldifs}
171 if [[ -z $(egetent group "${g}") ]] ; then 195 if [[ -z $(egetent group "${g}") ]] ; then
172 eerror "You must add group ${g} to the system first" 196 eerror "You must add group ${g} to the system first"
173 die "${g} is not a valid GID" 197 die "${g} is not a valid GID"
174 fi 198 fi
175 if [[ -z ${defgroup} ]] ; then 199 if [[ -z ${defgroup} ]] ; then
176 defgroup=${g} 200 defgroup=${g}
177 else 201 else
178 exgroups="${exgroups},${g}" 202 exgroups+=",${g}"
179 fi 203 fi
180 export IFS=","
181 done 204 done
182 export IFS=${oldifs}
183
184 opts="${opts} -g ${defgroup}" 205 opts+=( -g "${defgroup}" )
185 if [[ ! -z ${exgroups} ]] ; then 206 if [[ ! -z ${exgroups} ]] ; then
186 opts="${opts} -G ${exgroups:1}" 207 opts+=( -G "${exgroups:1}" )
208 fi
187 fi 209 fi
210 einfo " - Groups: ${egroups:-(none)}"
211
212 # handle extra args
213 if [[ $# -gt 0 ]] ; then
214 die "extra arguments no longer supported; please file a bug"
188 else 215 else
189 egroups="(none)" 216 local comment="added by portage for ${PN}"
217 opts+=( -c "${comment}" )
218 einfo " - GECOS: ${comment}"
190 fi 219 fi
191 einfo " - Groups: ${egroups}"
192 220
193 # handle extra and add the user 221 # add the user
194 local oldsandbox=${SANDBOX_ON}
195 export SANDBOX_ON="0"
196 case ${CHOST} in 222 case ${CHOST} in
197 *-darwin*) 223 *-darwin*)
198 ### Make the user 224 ### Make the user
199 if [[ -z $@ ]] ; then
200 dscl . create /users/${euser} uid ${euid} 225 dscl . create "/users/${euser}" uid ${euid}
201 dscl . create /users/${euser} shell ${eshell} 226 dscl . create "/users/${euser}" shell "${eshell}"
202 dscl . create /users/${euser} home ${ehome} 227 dscl . create "/users/${euser}" home "${ehome}"
203 dscl . create /users/${euser} realname "added by portage for ${PN}" 228 dscl . create "/users/${euser}" realname "added by portage for ${PN}"
204 ### Add the user to the groups specified 229 ### Add the user to the groups specified
205 local oldifs=${IFS}
206 export IFS=","
207 for g in ${egroups} ; do 230 for g in "${egroups_arr[@]}" ; do
208 dscl . merge /groups/${g} users ${euser} 231 dscl . merge "/groups/${g}" users "${euser}"
209 done 232 done
210 export IFS=${oldifs}
211 else
212 einfo "Extra options are not supported on Darwin yet"
213 einfo "Please report the ebuild along with the info below"
214 einfo "eextra: $@"
215 die "Required function missing"
216 fi
217 ;; 233 ;;
234
218 *-freebsd*|*-dragonfly*) 235 *-freebsd*|*-dragonfly*)
219 if [[ -z $@ ]] ; then 236 pw useradd "${euser}" "${opts[@]}" || die
220 pw useradd ${euser} ${opts} \
221 -c "added by portage for ${PN}" \
222 die "enewuser failed"
223 else
224 einfo " - Extra: $@"
225 pw useradd ${euser} ${opts} \
226 "$@" || die "enewuser failed"
227 fi
228 ;; 237 ;;
229 238
230 *-netbsd*) 239 *-netbsd*)
231 if [[ -z $@ ]] ; then 240 useradd "${opts[@]}" "${euser}" || die
232 useradd ${opts} ${euser} || die "enewuser failed"
233 else
234 einfo " - Extra: $@"
235 useradd ${opts} ${euser} "$@" || die "enewuser failed"
236 fi
237 ;; 241 ;;
238 242
239 *-openbsd*) 243 *-openbsd*)
240 if [[ -z $@ ]] ; then 244 # all ops the same, except the -g vs -g/-G ...
241 useradd -u ${euid} -s ${eshell} \ 245 useradd -u ${euid} -s "${eshell}" \
242 -d ${ehome} -c "Added by portage for ${PN}" \ 246 -d "${ehome}" -g "${egroups}" "${euser}" || die
243 -g ${egroups} ${euser} || die "enewuser failed"
244 else
245 einfo " - Extra: $@"
246 useradd -u ${euid} -s ${eshell} \
247 -d ${ehome} -c "Added by portage for ${PN}" \
248 -g ${egroups} ${euser} "$@" || die "enewuser failed"
249 fi
250 ;; 247 ;;
251 248
252 *) 249 *)
253 if [[ -z $@ ]] ; then 250 useradd -r "${opts[@]}" "${euser}" || die
254 useradd -r ${opts} \
255 -c "added by portage for ${PN}" \
256 ${euser} \
257 || die "enewuser failed"
258 else
259 einfo " - Extra: $@"
260 useradd -r ${opts} "$@" \
261 ${euser} \
262 || die "enewuser failed"
263 fi
264 ;; 251 ;;
265 esac 252 esac
266 253
267 if [[ ! -e ${ROOT}/${ehome} ]] ; then 254 if [[ ! -e ${ROOT}/${ehome} ]] ; then
268 einfo " - Creating ${ehome} in ${ROOT}" 255 einfo " - Creating ${ehome} in ${ROOT}"
269 mkdir -p "${ROOT}/${ehome}" 256 mkdir -p "${ROOT}/${ehome}"
270 chown ${euser} "${ROOT}/${ehome}" 257 chown "${euser}" "${ROOT}/${ehome}"
271 chmod 755 "${ROOT}/${ehome}" 258 chmod 755 "${ROOT}/${ehome}"
272 fi 259 fi
273
274 export SANDBOX_ON=${oldsandbox}
275} 260}
276 261
277# @FUNCTION: enewgroup 262# @FUNCTION: enewgroup
278# @USAGE: <group> [gid] 263# @USAGE: <group> [gid]
279# @DESCRIPTION: 264# @DESCRIPTION:
280# This function does not require you to understand how to properly add a 265# This function does not require you to understand how to properly add a
281# group to the system. Just give it a group name to add and enewgroup will 266# group to the system. Just give it a group name to add and enewgroup will
282# do the rest. You may specify the gid for the group or allow the group to 267# do the rest. You may specify the gid for the group or allow the group to
283# allocate the next available one. 268# allocate the next available one.
284enewgroup() { 269enewgroup() {
285 case ${EBUILD_PHASE} in 270 _assert_pkg_ebuild_phase ${FUNCNAME}
286 unpack|compile|test|install)
287 eerror "'enewgroup()' called from '${EBUILD_PHASE}()' which is not a pkg_* function."
288 eerror "Package fails at QA and at life. Please file a bug."
289 die "Bad package! enewgroup is only for use in pkg_* functions!"
290 esac
291 271
292 # get the group 272 # get the group
293 local egroup="$1"; shift 273 local egroup=$1; shift
294 if [ -z "${egroup}" ] 274 if [[ -z ${egroup} ]] ; then
295 then
296 eerror "No group specified !" 275 eerror "No group specified !"
297 die "Cannot call enewgroup without a group" 276 die "Cannot call enewgroup without a group"
298 fi 277 fi
299 278
300 # see if group already exists 279 # see if group already exists
301 if [[ -n $(egetent group "${egroup}") ]]; then 280 if [[ -n $(egetent group "${egroup}") ]] ; then
302 return 0 281 return 0
303 fi 282 fi
304 einfo "Adding group '${egroup}' to your system ..." 283 einfo "Adding group '${egroup}' to your system ..."
305 284
306 # options to pass to useradd
307 local opts=
308
309 # handle gid 285 # handle gid
310 local egid="$1"; shift 286 local egid=$1; shift
311 if [ ! -z "${egid}" ] 287 if [[ ! -z ${egid} ]] ; then
312 then
313 if [ "${egid}" -gt 0 ] 288 if [[ ${egid} -gt 0 ]] ; then
314 then
315 if [ -z "`egetent group ${egid}`" ] 289 if [[ -n $(egetent group ${egid}) ]] ; then
316 then
317 if [[ "${CHOST}" == *-darwin* ]]; then
318 opts="${opts} ${egid}"
319 else
320 opts="${opts} -g ${egid}"
321 fi
322 else
323 egid="next available; requested gid taken" 290 egid="next available; requested gid taken"
324 fi 291 fi
325 else 292 else
326 eerror "Groupid given but is not greater than 0 !" 293 eerror "Groupid given but is not greater than 0 !"
327 die "${egid} is not a valid GID" 294 die "${egid} is not a valid GID"
330 egid="next available" 297 egid="next available"
331 fi 298 fi
332 einfo " - Groupid: ${egid}" 299 einfo " - Groupid: ${egid}"
333 300
334 # handle extra 301 # handle extra
335 local eextra="$@" 302 if [[ $# -gt 0 ]] ; then
336 opts="${opts} ${eextra}" 303 die "extra arguments no longer supported; please file a bug"
337
338 # add the group
339 local oldsandbox="${SANDBOX_ON}"
340 export SANDBOX_ON="0"
341 case ${CHOST} in
342 *-darwin*)
343 if [ ! -z "${eextra}" ];
344 then
345 einfo "Extra options are not supported on Darwin/OS X yet"
346 einfo "Please report the ebuild along with the info below"
347 einfo "eextra: ${eextra}"
348 die "Required function missing"
349 fi 304 fi
350 305
351 # If we need the next available 306 # Some targets need to find the next available GID manually
352 case ${egid} in 307 _enewgroup_next_gid() {
353 *[!0-9]*) # Non numeric 308 if [[ ${egid} == *[!0-9]* ]] ; then
309 # Non numeric
354 for ((egid = 101; egid <= 999; egid++)); do 310 for ((egid = 101; egid <= 999; egid++)) ; do
355 [[ -z $(egetent group ${egid}) ]] && break 311 [[ -z $(egetent group ${egid}) ]] && break
356 done 312 done
357 esac 313 fi
314 }
315
316 # add the group
317 case ${CHOST} in
318 *-darwin*)
319 _enewgroup_next_gid
358 dscl . create /groups/${egroup} gid ${egid} 320 dscl . create "/groups/${egroup}" gid ${egid}
359 dscl . create /groups/${egroup} passwd '*' 321 dscl . create "/groups/${egroup}" passwd '*'
360 ;; 322 ;;
361 323
362 *-freebsd*|*-dragonfly*) 324 *-freebsd*|*-dragonfly*)
363 case ${egid} in 325 _enewgroup_next_gid
364 *[!0-9]*) # Non numeric
365 for ((egid = 101; egid <= 999; egid++)); do
366 [[ -z $(egetent group ${egid}) ]] && break
367 done
368 esac
369 pw groupadd ${egroup} -g ${egid} || die "enewgroup failed" 326 pw groupadd "${egroup}" -g ${egid} || die
370 ;; 327 ;;
371 328
372 *-netbsd*) 329 *-netbsd*)
373 case ${egid} in 330 _enewgroup_next_gid
374 *[!0-9]*) # Non numeric
375 for ((egid = 101; egid <= 999; egid++)); do
376 [[ -z $(egetent group ${egid}) ]] && break
377 done
378 esac
379 groupadd -g ${egid} ${egroup} || die "enewgroup failed" 331 groupadd -g ${egid} "${egroup}" || die
380 ;; 332 ;;
381 333
382 *) 334 *)
335 local opts
336 if [[ ${egid} == *[!0-9]* ]] ; then
337 # Non numeric; let groupadd figure out a GID for us
338 opts=""
339 else
340 opts="-g ${egid}"
341 fi
383 # We specify -r so that we get a GID in the system range from login.defs 342 # We specify -r so that we get a GID in the system range from login.defs
384 groupadd -r ${opts} ${egroup} || die "enewgroup failed" 343 groupadd -r ${opts} "${egroup}" || die
385 ;; 344 ;;
386 esac 345 esac
387 export SANDBOX_ON="${oldsandbox}"
388} 346}
389 347
348# @FUNCTION: egethome
349# @USAGE: <user>
350# @DESCRIPTION:
390# Gets the home directory for the specified user 351# Gets the home directory for the specified user.
391# it's a wrap around egetent as the position of the home directory in the line
392# varies depending on the os used.
393#
394# To use that, inherit eutils, not portability!
395egethome() { 352egethome() {
396 ent=$(egetent passwd $1) 353 local pos
354
355 [[ $# -eq 1 ]] || die "usage: egethome <user>"
397 356
398 case ${CHOST} in 357 case ${CHOST} in
399 *-darwin*|*-freebsd*|*-dragonfly*) 358 *-darwin*|*-freebsd*|*-dragonfly*)
400 # Darwin, OSX, FreeBSD and DragonFly use position 9 to store homedir 359 pos=9
401 echo ${ent} | cut -d: -f9
402 ;; 360 ;;
361 *) # Linux, NetBSD, OpenBSD, etc...
362 pos=6
363 ;;
364 esac
365
366 egetent passwd "$1" | cut -d: -f${pos}
367}
368
369# @FUNCTION: egetshell
370# @USAGE: <user>
371# @DESCRIPTION:
372# Gets the shell for the specified user.
373egetshell() {
374 local pos
375
376 [[ $# -eq 1 ]] || die "usage: egetshell <user>"
377
378 case ${CHOST} in
379 *-darwin*|*-freebsd*|*-dragonfly*)
380 pos=10
381 ;;
382 *) # Linux, NetBSD, OpenBSD, etc...
383 pos=7
384 ;;
385 esac
386
387 egetent passwd "$1" | cut -d: -f${pos}
388}
389
390# @FUNCTION: esethome
391# @USAGE: <user> <homedir>
392# @DESCRIPTION:
393# Update the home directory in a platform-agnostic way.
394# Required parameters is the username and the new home directory.
395# Specify -1 if you want to set home to the enewuser default
396# of /dev/null.
397# If the new home directory does not exist, it is created.
398# Any previously existing home directory is NOT moved.
399esethome() {
400 _assert_pkg_ebuild_phase ${FUNCNAME}
401
402 # get the username
403 local euser=$1; shift
404 if [[ -z ${euser} ]] ; then
405 eerror "No username specified !"
406 die "Cannot call esethome without a username"
407 fi
408
409 # lets see if the username already exists
410 if [[ -z $(egetent passwd "${euser}") ]] ; then
411 ewarn "User does not exist, cannot set home dir -- skipping."
412 return 1
413 fi
414
415 # handle homedir
416 local ehome=$1; shift
417 if [[ -z ${ehome} ]] ; then
418 eerror "No home directory specified !"
419 die "Cannot call esethome without a home directory or '-1'"
420 fi
421
422 if [[ ${ehome} == "-1" ]] ; then
423 ehome="/dev/null"
424 fi
425 einfo " - Home: ${ehome}"
426
427 # ensure home directory exists, otherwise update will fail
428 if [[ ! -e ${ROOT}/${ehome} ]] ; then
429 einfo " - Creating ${ehome} in ${ROOT}"
430 mkdir -p "${ROOT}/${ehome}"
431 chown "${euser}" "${ROOT}/${ehome}"
432 chmod 755 "${ROOT}/${ehome}"
433 fi
434
435 # update the home directory
436 case ${CHOST} in
437 *-darwin*)
438 dscl . change "/users/${euser}" home "${ehome}"
439 ;;
440
441 *-freebsd*|*-dragonfly*)
442 pw usermod "${euser}" -d "${ehome}" && return 0
443 [[ $? == 8 ]] && eerror "${euser} is in use, cannot update home"
444 eerror "There was an error when attempting to update the home directory for ${euser}"
445 eerror "Please update it manually on your system:"
446 eerror "\t pw usermod \"${euser}\" -d \"${ehome}\""
447 ;;
448
403 *) 449 *)
404 # Linux, NetBSD and OpenBSD use position 6 instead 450 usermod -d "${ehome}" "${euser}" && return 0
405 echo ${ent} | cut -d: -f6 451 [[ $? == 8 ]] && eerror "${euser} is in use, cannot update home"
452 eerror "There was an error when attempting to update the home directory for ${euser}"
453 eerror "Please update it manually on your system (as root):"
454 eerror "\t usermod -d \"${ehome}\" \"${euser}\""
406 ;; 455 ;;
407 esac 456 esac
408} 457}
409 458
410# Gets the shell for the specified user 459fi
411# it's a wrap around egetent as the position of the home directory in the line
412# varies depending on the os used.
413#
414# To use that, inherit eutils, not portability!
415egetshell() {
416 ent=$(egetent passwd "$1")
417
418 case ${CHOST} in
419 *-darwin*|*-freebsd*|*-dragonfly*)
420 # Darwin, OSX, FreeBSD and DragonFly use position 9 to store homedir
421 echo ${ent} | cut -d: -f10
422 ;;
423 *)
424 # Linux, NetBSD and OpenBSD use position 6 instead
425 echo ${ent} cut -d: -f7
426 ;;
427 esac
428}
429
430# Returns true if specified user has a shell that precludes logins
431# on whichever operating system.
432is-login-disabled() {
433 shell=$(egetshell "$1")
434
435 case ${shell} in
436 /bin/false|/usr/bin/false|/sbin/nologin|/usr/sbin/nologin)
437 return 0 ;;
438 *)
439 return 1 ;;
440 esac
441}

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.21

  ViewVC Help
Powered by ViewVC 1.1.20