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