Gentoo Archives: gentoo-dev

From: "Jérémy Connat" <morderca@××××××××.net>
To: gentoo-dev@l.g.o
Cc: "Jérémy Connat" <morderca@××××××××.net>
Subject: [gentoo-dev] [PATCH 2/3] eclass/user-info.eclass: Fixing user/group creation when using different ROOT
Date: Fri, 15 Apr 2022 13:47:12
Message-Id: 20220415134628.23069-3-morderca@morderca.net
In Reply to: [gentoo-dev] [PATCH 0/3] eclass: Fixing user/group creation when using different ROOT by "Jérémy Connat"
1 Signed-off-by: Jérémy Connat <morderca@××××××××.net>
2 ---
3 eclass/user-info.eclass | 35 +++++++++++++++++++++++++++++------
4 1 file changed, 29 insertions(+), 6 deletions(-)
5
6 diff --git a/eclass/user-info.eclass b/eclass/user-info.eclass
7 index 3838585ab6c..5550e4f08ee 100644
8 --- a/eclass/user-info.eclass
9 +++ b/eclass/user-info.eclass
10 @@ -23,6 +23,7 @@ _USER_INFO_ECLASS=1
11 # dscl (Mac OS X 10.5), and pw (FreeBSD) used in enewuser()/enewgroup().
12 #
13 # Supported databases: group passwd
14 +# Warning: This function can be used only in pkg_* phases when ROOT is valid.
15 egetent() {
16 local db=$1 key=$2
17
18 @@ -43,18 +44,31 @@ egetent() {
19 # lookup by uid/gid
20 local opts
21 if [[ ${key} == [[:digit:]]* ]] ; then
22 - [[ ${db} == "user" ]] && opts="-u" || opts="-g"
23 + [[ ${db} == "user" ]] && opts=( -u ) || opts=( -g )
24 fi
25
26 + # Handle different ROOT
27 + [[ -n ${ROOT} ]] && opts+=( -R "${ROOT}" )
28 +
29 pw show ${db} ${opts} "${key}" -q
30 ;;
31 *-openbsd*)
32 - grep "${key}:\*:" /etc/${db}
33 + grep "${key}:\*:" "${EROOT}/etc/${db}"
34 ;;
35 *)
36 - # ignore nscd output if we're not running as root
37 - type -p nscd >/dev/null && nscd -i "${db}" 2>/dev/null
38 - getent "${db}" "${key}"
39 + # getent does not support -R option, if we are working on a different
40 + # ROOT than /, fallback to grep technique.
41 + if [[ -z ${ROOT} ]]; then
42 + # ignore nscd output if we're not running as root
43 + type -p nscd >/dev/null && nscd -i "${db}" 2>/dev/null
44 + getent "${db}" "${key}"
45 + else
46 + if [[ ${key} =~ ^[[:digit:]]+$ ]]; then
47 + grep -E "^([^:]*:){2}${key}" "${ROOT}/etc/${db}"
48 + else
49 + grep "^${key}:" "${ROOT}/etc/${db}"
50 + fi
51 + fi
52 ;;
53 esac
54 }
55 @@ -151,7 +165,16 @@ egetgroups() {
56 [[ $# -eq 1 ]] || die "usage: egetgroups <user>"
57
58 local egroups_arr
59 - read -r -a egroups_arr < <(id -G -n "$1")
60 + if [[ -n "${ROOT}" ]]; then
61 + local pgroup=$(egetent passwd "$1" | cut -d: -f1)
62 + local sgroups=( $(grep -E ":([^:]*,)?$1(,[^:]*)?$" "${ROOT}/etc/group" | cut -d: -f1) )
63 +
64 + # Remove primary group from list
65 + sgroups=${sgroups#${pgroup}}
66 + egroups_arr=( ${pgroup} ${sgroups[@]} )
67 + else
68 + read -r -a egroups_arr < <(id -G -n "$1")
69 + fi
70
71 local g groups=${egroups_arr[0]}
72 # sort supplementary groups to make comparison possible
73 --
74 2.35.1