Gentoo Archives: gentoo-dev

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

Replies