Gentoo Archives: gentoo-dev

From: Thomas Deutschmann <whissi@g.o>
To: gentoo-dev@l.g.o
Subject: [gentoo-dev] [PATCH v3] acct-user.eclass: allow opt-out of user modification
Date: Fri, 08 Jan 2021 22:46:20
Message-Id: 20210108224553.12282-1-whissi@gentoo.org
1 In some setups where users are changed/managed not only via ebuilds,
2 for example through configuration management systems, it could be
3 problematic if acct-user.eclass will restore user/group settings
4 to values set in ebuild.
5
6 Setting ACCT_USER_NO_MODIFY to a non-zero value will allow system
7 administrator to disable modification of any existing user.
8
9 Note: Lock/unlock when acct-* package will be installed/removed
10 will still happen.
11
12 Signed-off-by: Thomas Deutschmann <whissi@g.o>
13 ---
14
15 v3:
16 - Fixed eclass documentation
17 - Honor 80 chars limit
18 - Prefixed internal variable ACCT_USER_ALREADY_EXISTS
19
20 eclass/acct-user.eclass | 27 +++++++++++++++++++++++++++
21 1 file changed, 27 insertions(+)
22
23 diff --git a/eclass/acct-user.eclass b/eclass/acct-user.eclass
24 index 47890e48409a..dcda661d39ea 100644
25 --- a/eclass/acct-user.eclass
26 +++ b/eclass/acct-user.eclass
27 @@ -72,6 +72,11 @@ readonly ACCT_USER_NAME
28 # Overlays should set this to -1 to dynamically allocate UID. Using -1
29 # in ::gentoo is prohibited by policy.
30
31 +# @ECLASS-VARIABLE: _ACCT_USER_ALREADY_EXISTS
32 +# @INTERNAL
33 +# @DESCRIPTION:
34 +# Status variable which indicates if user already exists.
35 +
36 # @ECLASS-VARIABLE: ACCT_USER_ENFORCE_ID
37 # @DESCRIPTION:
38 # If set to a non-null value, the eclass will require the user to have
39 @@ -79,6 +84,13 @@ readonly ACCT_USER_NAME
40 # the UID is taken by another user, the install will fail.
41 : ${ACCT_USER_ENFORCE_ID:=}
42
43 +# @ECLASS-VARIABLE: ACCT_USER_NO_MODIFY
44 +# @DEFAULT_UNSET
45 +# @DESCRIPTION:
46 +# If set to a non-null value, the eclass will not make any changes
47 +# to an already existing user.
48 +: ${ACCT_USER_NO_MODIFY:=}
49 +
50 # @ECLASS-VARIABLE: ACCT_USER_SHELL
51 # @DESCRIPTION:
52 # The shell to use for the user. If not specified, a 'nologin' variant
53 @@ -344,6 +356,13 @@ acct-user_src_install() {
54 acct-user_pkg_preinst() {
55 debug-print-function ${FUNCNAME} "${@}"
56
57 + # check if user already exists
58 + _ACCT_USER_ALREADY_EXISTS=
59 + if [[ -n $(egetent passwd "${ACCT_USER_NAME}") ]]; then
60 + _ACCT_USER_ALREADY_EXISTS=yes
61 + fi
62 + readonly _ACCT_USER_ALREADY_EXISTS
63 +
64 local groups=${ACCT_USER_GROUPS[*]}
65 enewuser ${ACCT_USER_ENFORCE_ID:+-F} -M "${ACCT_USER_NAME}" \
66 "${ACCT_USER_ID}" "${ACCT_USER_SHELL}" "${ACCT_USER_HOME}" \
67 @@ -379,6 +398,14 @@ acct-user_pkg_postinst() {
68 return 0
69 fi
70
71 + if [[ -n ${ACCT_USER_NO_MODIFY} && -n ${_ACCT_USER_ALREADY_EXISTS} ]] ; then
72 + eunlockuser "${ACCT_USER_NAME}"
73 +
74 + ewarn "User ${ACCT_USER_NAME} already exists; Not touching existing user"
75 + ewarn "due to set ACCT_USER_NO_MODIFY."
76 + return 0
77 + fi
78 +
79 # NB: eset* functions check current value
80 esethome "${ACCT_USER_NAME}" "${ACCT_USER_HOME}"
81 esetshell "${ACCT_USER_NAME}" "${ACCT_USER_SHELL}"
82 --
83 2.30.0

Replies