Gentoo Archives: gentoo-dev

From: "Michał Górny" <mgorny@g.o>
To: gentoo-dev@l.g.o
Cc: "Michał Górny" <mgorny@g.o>
Subject: [gentoo-dev] [PATCH v3 06/19] user.eclass: Support forcing specified UID/GID
Date: Sun, 09 Jun 2019 11:30:48
Message-Id: 20190609112814.15907-7-mgorny@gentoo.org
In Reply to: [gentoo-dev] [PATCH v3 00/19] User/group packages by "Michał Górny"
1 Signed-off-by: Michał Górny <mgorny@g.o>
2 ---
3 eclass/user.eclass | 23 +++++++++++++++++++++--
4 1 file changed, 21 insertions(+), 2 deletions(-)
5
6 diff --git a/eclass/user.eclass b/eclass/user.eclass
7 index 0577df81ae78..92a07bb6b41b 100644
8 --- a/eclass/user.eclass
9 +++ b/eclass/user.eclass
10 @@ -71,13 +71,15 @@ egetent() {
11 }
12
13 # @FUNCTION: enewuser
14 -# @USAGE: <user> [-M] [uid] [shell] [homedir] [groups]
15 +# @USAGE: <user> [-F] [-M] [uid] [shell] [homedir] [groups]
16 # @DESCRIPTION:
17 # Same as enewgroup, you are not required to understand how to properly add
18 # a user to the system. The only required parameter is the username.
19 # Default uid is (pass -1 for this) next available, default shell is
20 # /bin/false, default homedir is /dev/null, and there are no default groups.
21 #
22 +# If -F is passed, enewuser will always enforce specified UID and fail if it
23 +# can not be assigned.
24 # If -M is passed, enewuser does not create the home directory if it does not
25 # exist.
26 enewuser() {
27 @@ -87,9 +89,10 @@ enewuser() {
28 fi
29 _assert_pkg_ebuild_phase ${FUNCNAME}
30
31 - local create_home=1
32 + local create_home=1 force_uid=
33 while [[ $1 == -* ]]; do
34 case $1 in
35 + -F) force_uid=1;;
36 -M) create_home=;;
37 *) die "${FUNCNAME}: invalid option ${1}";;
38 esac
39 @@ -117,6 +120,7 @@ enewuser() {
40 if [[ -n ${euid} && ${euid} != -1 ]] ; then
41 if [[ ${euid} -gt 0 ]] ; then
42 if [[ -n $(egetent passwd ${euid}) ]] ; then
43 + [[ -n ${force_uid} ]] && die "${FUNCNAME}: UID ${euid} already taken"
44 euid="next"
45 fi
46 else
47 @@ -124,6 +128,7 @@ enewuser() {
48 die "${euid} is not a valid UID"
49 fi
50 else
51 + [[ -n ${force_uid} ]] && die "${FUNCNAME}: -F with uid==-1 makes no sense"
52 euid="next"
53 fi
54 if [[ ${euid} == "next" ]] ; then
55 @@ -240,6 +245,9 @@ enewuser() {
56 # group to the system. Just give it a group name to add and enewgroup will
57 # do the rest. You may specify the gid for the group or allow the group to
58 # allocate the next available one.
59 +#
60 +# If -F is passed, enewgroup will always enforce specified GID and fail if it
61 +# can not be assigned.
62 enewgroup() {
63 if [[ ${EUID} != 0 ]] ; then
64 einfo "Insufficient privileges to execute ${FUNCNAME[0]}"
65 @@ -247,6 +255,15 @@ enewgroup() {
66 fi
67 _assert_pkg_ebuild_phase ${FUNCNAME}
68
69 + local force_gid=
70 + while [[ $1 == -* ]]; do
71 + case $1 in
72 + -F) force_gid=1;;
73 + *) die "${FUNCNAME}: invalid option ${1}";;
74 + esac
75 + shift
76 + done
77 +
78 # get the group
79 local egroup=$1; shift
80 if [[ -z ${egroup} ]] ; then
81 @@ -265,6 +282,7 @@ enewgroup() {
82 if [[ ! -z ${egid} ]] ; then
83 if [[ ${egid} -gt 0 ]] ; then
84 if [[ -n $(egetent group ${egid}) ]] ; then
85 + [[ -n ${force_gid} ]] && die "${FUNCNAME}: GID ${egid} already taken"
86 egid="next available; requested gid taken"
87 fi
88 else
89 @@ -272,6 +290,7 @@ enewgroup() {
90 die "${egid} is not a valid GID"
91 fi
92 else
93 + [[ -n ${force_gid} ]] && die "${FUNCNAME}: -F with gid==-1 makes no sense"
94 egid="next available"
95 fi
96 einfo " - Groupid: ${egid}"
97 --
98 2.22.0.rc3