Gentoo Archives: gentoo-commits

From: Mike Gilbert <floppym@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] repo/gentoo:master commit in: eclass/
Date: Thu, 06 Feb 2020 21:41:12
Message-Id: 1581025250.9493cc97e52ad67a9586007271c4c2c838579e2c.floppym@gentoo
1 commit: 9493cc97e52ad67a9586007271c4c2c838579e2c
2 Author: Mike Gilbert <floppym <AT> gentoo <DOT> org>
3 AuthorDate: Wed Feb 5 20:55:14 2020 +0000
4 Commit: Mike Gilbert <floppym <AT> gentoo <DOT> org>
5 CommitDate: Thu Feb 6 21:40:50 2020 +0000
6 URL: https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=9493cc97
7
8 user.eclass: move read-only functionality to user-info.eclass
9
10 The new eclass can be used by ebuilds to look up user information
11 without triggering a deprecation warning from repoman and pkgcheck.
12
13 Signed-off-by: Mike Gilbert <floppym <AT> gentoo.org>
14
15 eclass/user-info.eclass | 158 ++++++++++++++++++++++++++++++++++++++++++++++++
16 eclass/user.eclass | 149 +--------------------------------------------
17 2 files changed, 161 insertions(+), 146 deletions(-)
18
19 diff --git a/eclass/user-info.eclass b/eclass/user-info.eclass
20 new file mode 100644
21 index 00000000000..ea037d54dfd
22 --- /dev/null
23 +++ b/eclass/user-info.eclass
24 @@ -0,0 +1,158 @@
25 +# Copyright 1999-2020 Gentoo Authors
26 +# Distributed under the terms of the GNU General Public License v2
27 +
28 +# @ECLASS: user-info.eclass
29 +# @MAINTAINER:
30 +# base-system@g.o (Linux)
31 +# Michał Górny <mgorny@g.o> (NetBSD)
32 +# @BLURB: Read-only access to user and group information
33 +
34 +if [[ -z ${_USER_INFO_ECLASS} ]]; then
35 +_USER_INFO_ECLASS=1
36 +
37 +# @FUNCTION: egetent
38 +# @USAGE: <database> <key>
39 +# @DESCRIPTION:
40 +# Small wrapper for getent (Linux), nidump (< Mac OS X 10.5),
41 +# dscl (Mac OS X 10.5), and pw (FreeBSD) used in enewuser()/enewgroup().
42 +#
43 +# Supported databases: group passwd
44 +egetent() {
45 + local db=$1 key=$2
46 +
47 + [[ $# -ge 3 ]] && die "usage: egetent <database> <key>"
48 +
49 + case ${db} in
50 + passwd|group) ;;
51 + *) die "sorry, database '${db}' not yet supported; file a bug" ;;
52 + esac
53 +
54 + case ${CHOST} in
55 + *-freebsd*|*-dragonfly*)
56 + case ${db} in
57 + passwd) db="user" ;;
58 + *) ;;
59 + esac
60 +
61 + # lookup by uid/gid
62 + local opts
63 + if [[ ${key} == [[:digit:]]* ]] ; then
64 + [[ ${db} == "user" ]] && opts="-u" || opts="-g"
65 + fi
66 +
67 + pw show ${db} ${opts} "${key}" -q
68 + ;;
69 + *-openbsd*)
70 + grep "${key}:\*:" /etc/${db}
71 + ;;
72 + *)
73 + # ignore nscd output if we're not running as root
74 + type -p nscd >/dev/null && nscd -i "${db}" 2>/dev/null
75 + getent "${db}" "${key}"
76 + ;;
77 + esac
78 +}
79 +
80 +# @FUNCTION: egetusername
81 +# @USAGE: <uid>
82 +# @DESCRIPTION:
83 +# Gets the username for given UID.
84 +egetusername() {
85 + [[ $# -eq 1 ]] || die "usage: egetusername <uid>"
86 +
87 + egetent passwd "$1" | cut -d: -f1
88 +}
89 +
90 +# @FUNCTION: egetgroupname
91 +# @USAGE: <gid>
92 +# @DESCRIPTION:
93 +# Gets the group name for given GID.
94 +egetgroupname() {
95 + [[ $# -eq 1 ]] || die "usage: egetgroupname <gid>"
96 +
97 + egetent group "$1" | cut -d: -f1
98 +}
99 +
100 +# @FUNCTION: egethome
101 +# @USAGE: <user>
102 +# @DESCRIPTION:
103 +# Gets the home directory for the specified user.
104 +egethome() {
105 + local pos
106 +
107 + [[ $# -eq 1 ]] || die "usage: egethome <user>"
108 +
109 + case ${CHOST} in
110 + *-freebsd*|*-dragonfly*)
111 + pos=9
112 + ;;
113 + *) # Linux, NetBSD, OpenBSD, etc...
114 + pos=6
115 + ;;
116 + esac
117 +
118 + egetent passwd "$1" | cut -d: -f${pos}
119 +}
120 +
121 +# @FUNCTION: egetshell
122 +# @USAGE: <user>
123 +# @DESCRIPTION:
124 +# Gets the shell for the specified user.
125 +egetshell() {
126 + local pos
127 +
128 + [[ $# -eq 1 ]] || die "usage: egetshell <user>"
129 +
130 + case ${CHOST} in
131 + *-freebsd*|*-dragonfly*)
132 + pos=10
133 + ;;
134 + *) # Linux, NetBSD, OpenBSD, etc...
135 + pos=7
136 + ;;
137 + esac
138 +
139 + egetent passwd "$1" | cut -d: -f${pos}
140 +}
141 +
142 +# @FUNCTION: egetcomment
143 +# @USAGE: <user>
144 +# @DESCRIPTION:
145 +# Gets the comment field for the specified user.
146 +egetcomment() {
147 + local pos
148 +
149 + [[ $# -eq 1 ]] || die "usage: egetshell <user>"
150 +
151 + case ${CHOST} in
152 + *-freebsd*|*-dragonfly*)
153 + pos=8
154 + ;;
155 + *) # Linux, NetBSD, OpenBSD, etc...
156 + pos=5
157 + ;;
158 + esac
159 +
160 + egetent passwd "$1" | cut -d: -f${pos}
161 +}
162 +
163 +# @FUNCTION: egetgroups
164 +# @USAGE: <user>
165 +# @DESCRIPTION:
166 +# Gets all the groups user belongs to. The primary group is returned
167 +# first, then all supplementary groups. Groups are ','-separated.
168 +egetgroups() {
169 + [[ $# -eq 1 ]] || die "usage: egetgroups <user>"
170 +
171 + local egroups_arr
172 + read -r -a egroups_arr < <(id -G -n "$1")
173 +
174 + local g groups=${egroups_arr[0]}
175 + # sort supplementary groups to make comparison possible
176 + while read -r g; do
177 + [[ -n ${g} ]] && groups+=",${g}"
178 + done < <(printf '%s\n' "${egroups_arr[@]:1}" | sort)
179 + echo "${groups}"
180 +}
181 +
182 +fi
183
184 diff --git a/eclass/user.eclass b/eclass/user.eclass
185 index f433d32bf7e..9bd0b607eab 100644
186 --- a/eclass/user.eclass
187 +++ b/eclass/user.eclass
188 @@ -1,4 +1,4 @@
189 -# Copyright 1999-2019 Gentoo Authors
190 +# Copyright 1999-2020 Gentoo Authors
191 # Distributed under the terms of the GNU General Public License v2
192
193 # @ECLASS: user.eclass
194 @@ -13,6 +13,8 @@
195 if [[ -z ${_USER_ECLASS} ]]; then
196 _USER_ECLASS=1
197
198 +inherit user-info
199 +
200 # @FUNCTION: _assert_pkg_ebuild_phase
201 # @INTERNAL
202 # @USAGE: <calling func name>
203 @@ -27,49 +29,6 @@ _assert_pkg_ebuild_phase() {
204 esac
205 }
206
207 -# @FUNCTION: egetent
208 -# @USAGE: <database> <key>
209 -# @DESCRIPTION:
210 -# Small wrapper for getent (Linux), nidump (< Mac OS X 10.5),
211 -# dscl (Mac OS X 10.5), and pw (FreeBSD) used in enewuser()/enewgroup().
212 -#
213 -# Supported databases: group passwd
214 -egetent() {
215 - local db=$1 key=$2
216 -
217 - [[ $# -ge 3 ]] && die "usage: egetent <database> <key>"
218 -
219 - case ${db} in
220 - passwd|group) ;;
221 - *) die "sorry, database '${db}' not yet supported; file a bug" ;;
222 - esac
223 -
224 - case ${CHOST} in
225 - *-freebsd*|*-dragonfly*)
226 - case ${db} in
227 - passwd) db="user" ;;
228 - *) ;;
229 - esac
230 -
231 - # lookup by uid/gid
232 - local opts
233 - if [[ ${key} == [[:digit:]]* ]] ; then
234 - [[ ${db} == "user" ]] && opts="-u" || opts="-g"
235 - fi
236 -
237 - pw show ${db} ${opts} "${key}" -q
238 - ;;
239 - *-openbsd*)
240 - grep "${key}:\*:" /etc/${db}
241 - ;;
242 - *)
243 - # ignore nscd output if we're not running as root
244 - type -p nscd >/dev/null && nscd -i "${db}" 2>/dev/null
245 - getent "${db}" "${key}"
246 - ;;
247 - esac
248 -}
249 -
250 # @FUNCTION: user_get_nologin
251 # @INTERNAL
252 # @DESCRIPTION:
253 @@ -351,108 +310,6 @@ enewgroup() {
254 esac
255 }
256
257 -# @FUNCTION: egetusername
258 -# @USAGE: <uid>
259 -# @DESCRIPTION:
260 -# Gets the username for given UID.
261 -egetusername() {
262 - [[ $# -eq 1 ]] || die "usage: egetusername <uid>"
263 -
264 - egetent passwd "$1" | cut -d: -f1
265 -}
266 -
267 -# @FUNCTION: egetgroupname
268 -# @USAGE: <gid>
269 -# @DESCRIPTION:
270 -# Gets the group name for given GID.
271 -egetgroupname() {
272 - [[ $# -eq 1 ]] || die "usage: egetgroupname <gid>"
273 -
274 - egetent group "$1" | cut -d: -f1
275 -}
276 -
277 -# @FUNCTION: egethome
278 -# @USAGE: <user>
279 -# @DESCRIPTION:
280 -# Gets the home directory for the specified user.
281 -egethome() {
282 - local pos
283 -
284 - [[ $# -eq 1 ]] || die "usage: egethome <user>"
285 -
286 - case ${CHOST} in
287 - *-freebsd*|*-dragonfly*)
288 - pos=9
289 - ;;
290 - *) # Linux, NetBSD, OpenBSD, etc...
291 - pos=6
292 - ;;
293 - esac
294 -
295 - egetent passwd "$1" | cut -d: -f${pos}
296 -}
297 -
298 -# @FUNCTION: egetshell
299 -# @USAGE: <user>
300 -# @DESCRIPTION:
301 -# Gets the shell for the specified user.
302 -egetshell() {
303 - local pos
304 -
305 - [[ $# -eq 1 ]] || die "usage: egetshell <user>"
306 -
307 - case ${CHOST} in
308 - *-freebsd*|*-dragonfly*)
309 - pos=10
310 - ;;
311 - *) # Linux, NetBSD, OpenBSD, etc...
312 - pos=7
313 - ;;
314 - esac
315 -
316 - egetent passwd "$1" | cut -d: -f${pos}
317 -}
318 -
319 -# @FUNCTION: egetcomment
320 -# @USAGE: <user>
321 -# @DESCRIPTION:
322 -# Gets the comment field for the specified user.
323 -egetcomment() {
324 - local pos
325 -
326 - [[ $# -eq 1 ]] || die "usage: egetshell <user>"
327 -
328 - case ${CHOST} in
329 - *-freebsd*|*-dragonfly*)
330 - pos=8
331 - ;;
332 - *) # Linux, NetBSD, OpenBSD, etc...
333 - pos=5
334 - ;;
335 - esac
336 -
337 - egetent passwd "$1" | cut -d: -f${pos}
338 -}
339 -
340 -# @FUNCTION: egetgroups
341 -# @USAGE: <user>
342 -# @DESCRIPTION:
343 -# Gets all the groups user belongs to. The primary group is returned
344 -# first, then all supplementary groups. Groups are ','-separated.
345 -egetgroups() {
346 - [[ $# -eq 1 ]] || die "usage: egetgroups <user>"
347 -
348 - local egroups_arr
349 - read -r -a egroups_arr < <(id -G -n "$1")
350 -
351 - local g groups=${egroups_arr[0]}
352 - # sort supplementary groups to make comparison possible
353 - while read -r g; do
354 - [[ -n ${g} ]] && groups+=",${g}"
355 - done < <(printf '%s\n' "${egroups_arr[@]:1}" | sort)
356 - echo "${groups}"
357 -}
358 -
359 # @FUNCTION: esethome
360 # @USAGE: <user> <homedir>
361 # @DESCRIPTION: