9042f0699593f81c1478aea8d878adb0efeae5bf
[mjo-overlay.git] / eclass / sys-user.eclass
1 # Copyright 1999-2017 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: sys-user.eclass
5 # @MAINTAINER:
6 # Michael Orlitzky <mjo@gentoo.org>
7 # @BLURB: handle installation and removal of system users.
8 # @DESCRIPTION:
9 # This eclass does most of the work for the sys-user/ packages that
10 # supply system user accounts.
11
12 # Needed for egetshell and egethome.
13 inherit user
14
15 EXPORT_FUNCTIONS pkg_pretend src_unpack src_configure src_compile src_install src_test pkg_preinst pkg_postinst pkg_prerm
16
17 : ${HOMEPAGE:="https://www.gentoo.org/"}
18 : ${DESCRIPTION:="The ${PN} system user"}
19 : ${LICENSE:="GPL-2"}
20
21 # If you want a different username, use a different package name. This
22 # prevents different people from claiming the same username.
23 SYS_USER_NAME="${PN}"
24
25 # @ECLASS-VARIABLE: SYS_USER_GROUPS
26 # @DESCRIPTION:
27 # A space-separated list of groups that the user will belong to.
28 # Dependencies on the appropriate sys-group packages are generated
29 # automatically.
30 : ${SYS_USER_GROUPS:=${PN}}
31
32 # @ECLASS-VARIABLE: SYS_USER_UID
33 # @REQUIRED
34 # @DESCRIPTION:
35 # etc.
36
37
38 # @ECLASS-VARIABLE: SYS_USER_UID_IMPORTANT
39 # @REQUIRED
40 # @DESCRIPTION:
41 # Set to "true" if you want to die() if you don't get your desired UID.
42 : ${SYS_USER_UID_IMPORTANT:=false}
43
44 # In many cases, if the UID of a user changes, packages depending on it
45 # will want to rebuild. We always use SLOT=0, because you can't install
46 # the same user twice. Then we use the UID as our subslot so that
47 # subslot deps can be used to rebuild packages when our UID changes.
48 SLOT="0/${SYS_USER_UID}"
49
50 # @ECLASS-VARIABLE: SYS_USER_HOME
51 # @DESCRIPTION:
52 # etc.
53 : ${SYS_USER_HOME:=/home/${SYS_USER_NAME}}
54
55 # @ECLASS-VARIABLE: SYS_USER_SHELL
56 # @DESCRIPTION:
57 # etc.
58 : ${SYS_USER_SHELL:=/bin/false}
59
60 case ${EAPI} in
61 6) ;;
62 *)
63 die "${ECLASS} is not compatible with EAPI=${EAPI}"
64 esac
65
66 # Depend on any groups we might need.
67 for _group in ${SYS_USER_GROUPS}; do
68 DEPEND+=" sys-group/${_group} "
69 RDEPEND+=" sys-group/${_group}:= "
70 done
71 unset _group
72
73 S="${WORKDIR}"
74
75 sys-user_src_unpack() { :; }
76 sys-user_src_compile() { :; }
77 sys-user_src_test() { :; }
78
79 sys-user_getuid() {
80 [[ $# -eq 1 ]] || die "usage: sys-user_getuid <username>"
81
82 # Output the real UID of the given user, or the empty string if the
83 # user does not exist on the system.
84 echo $(id --real --user "${1}")
85 }
86
87 sys-user_create() {
88 # Create the user whose information is contained in the following
89 # variables:
90 #
91 # * SYS_USER_NAME
92 # * SYS_USER_UID
93 # * SYS_USER_SHELL
94 # * SYS_USER_HOME
95 # * SYS_USER_GROUPS
96 #
97 # We don't create a group with the same name; that should be the
98 # job of the matching sys-group package.
99 useradd --no-user-group \
100 ${SYS_USER_UID:+--uid }"${SYS_USER_UID}" \
101 ${SYS_USER_GROUPS:+--groups }"${SYS_USER_GROUPS}" \
102 --shell "${SYS_USER_SHELL}" \
103 --home-dir "${SYS_USER_HOME}" \
104 "${SYS_USER_NAME}"
105 }
106
107
108 sys-user_modify() {
109 # Modify the existing user named $SYS_USER_NAME to match the values
110 # contained in the following variables:
111 #
112 # * SYS_USER_UID
113 # * SYS_USER_SHELL
114 # * SYS_USER_HOME
115 # * SYS_USER_GROUPS
116 #
117 usermod ${SYS_USER_UID:+--uid }"${SYS_USER_UID}" \
118 ${SYS_USER_GROUPS:+--append --groups }"${SYS_USER_GROUPS}" \
119 --shell "${SYS_USER_SHELL}" \
120 --home-dir "${SYS_USER_HOME}" \
121 "${SYS_USER_NAME}"
122 }
123
124 sys-user_pkg_pretend() {
125 # Sanity checks that would otherwise run code in global scope.
126 #
127 # First ensure that the user didn't say his UID is important and
128 # then fail to specify one.
129 if [[ -z "${SYS_USER_UID}" ]] &&
130 [[ "${SYS_USER_UID_IMPORTANT}" == "true" ]]; then
131 # Don't make no damn sense.
132 die "arbitrary UID requested with SYS_USER_UID_IMPORTANT=true"
133 fi
134
135 # Next ensure that no other username owns an important UID.
136 if [[ "${SYS_USER_UID_IMPORTANT}" == "true" ]]; then
137 # Ok, the UID is important. Make sure nobody else has it. Or
138 # rather, nobody else *with a different username* has it.
139 local oldname=$(egetent passwd "${SYS_USER_UID}" | cut -f1 -d':')
140 if [[ "${SYS_USER_NAME}" != "${oldname}" ]]; then
141 die "important UID ${SYS_USER_UID} already belongs to ${oldname}"
142 fi
143 fi
144
145 # Finally, ensure that this username doesn't already exist with
146 # another UID if its UID is supposedly important.
147 local olduid=$(sys-user_getuid "${SYS_USER_NAME}")
148 if [[ -n "${olduid}" ]]; then
149 if [[ "${SYS_USER_UID_IMPORTANT}" == "true" ]] && \
150 [[ "${SYS_USER_UID}" != "${olduid}" ]]; then
151 # The UID is important and specified, but there is already a
152 # system user with this name and a different UID. Halp.
153 die "user ${SYS_USER_NAME} already exists with UID ${olduid}"
154 fi
155 fi
156 }
157
158 sys-user_src_configure() {
159 local current_uid=$(sys-user_getuid "${SYS_USER_NAME}")
160 if [[ -n "${current_uid}" ]]; then
161 # UPGRADE PATH: This user already exists, so if the eclass
162 # consumer doesn't care about some settings, we can reuse the
163 # pre-existing ones.
164 #
165 # This is also useful for sys-user package upgrades, because it
166 # prevents us from incrementing the UID on a reinstall, and doing
167 # so would break most packages that need a system user to exist.
168 if [[ "${SYS_USER_UID_IMPORTANT}" != "true" ]]; then
169 SYS_USER_UID="${current_uid}"
170 fi
171
172 if [[ -z "${SYS_USER_HOME}" ]]; then
173 SYS_USER_HOME=$(egethome "${SYS_USER_NAME}")
174 fi
175
176 if [[ -z "${SYS_USER_SHELL}" ]]; then
177 SYS_USER_SHELL=$(egetshell "${SYS_USER_NAME}")
178 fi
179 fi
180
181 # TODO: if the requested (unimportant) UID is not available, we
182 # should set SYS_USER_UID to the empty string.
183
184 # The "useradd" and "usermod" tools expect a comma-separated list,
185 # so change our spaces to commas. Having duplicates in the list is
186 # not a problem for those two tools.
187 SYS_USER_GROUPS="${SYS_USER_GROUPS// /,}"
188 }
189
190 sys-user_src_install() {
191 # Install a placeholder file to /var/lib/sys-user/$uid. This will
192 # cause collisions if two packages try to install users with the
193 # same UID. The same problem potentially exists with the username,
194 # but as long as SYS_USER_NAME is hard-coded to $PN, that shouldn't
195 # be possible.
196 #
197 # Beware, this only works if SYS_USER_UID is guaranteed to have a
198 # real UID and not, for example, -1.
199 #
200 # TODO: this is a problem now!
201 #
202 #touch "${T}/${SYS_USER_UID}" || die
203 #insinto "/var/lib/sys-user"
204 #doins "${T}/${SYS_USER_UID}"
205 }
206
207 sys-user_pkg_preinst() {
208 if [[ -z $(sys-user_getuid "${SYS_USER_NAME}") ]]; then
209 # The user does not already exist. This is the nice and easy
210 # case because no matter how we got here, we want to go ahead
211 # and create the (new) user.
212 sys-user_create || die "failed to add user ${SYS_USER_NAME}"
213 elif [[ -n "${REPLACING_VERSIONS}" ]]; then
214 #
215 # This case is done in pkg_postint() to avoid clobbering a
216 # new user when we remove the old one.
217 #
218 :
219 else
220 # UPGRADE PATH: Ok, the user exists but this isn't an upgrade of
221 # a sys-user package. This is the upgrade path from the old
222 # style of user/group management to the new style. Lets see if
223 # the new user is compatible with the old one; it usually will be.
224 # We only bail out if there's a homedir or shell conflict.
225 #
226 # We should make it policy that new sys-user packages have the
227 # same homedir and shell as the existing ones created by
228 # ebuilds, but it can't hurt to check again here. These checks
229 # are done here (and not in pkg_pretend, where they would be
230 # more consistent) because the PMS states that REPLACING_VERSIONS
231 # may not be defined there.
232 #
233 # If a homedir/shell changes during a sys-user upgrade, we don't
234 # consider that a problem, because the change was knowingly made
235 # by a developer who just edited an ebuild to make that change.
236 local oldhome=$(egethome "${SYS_USER_NAME}")
237 local oldshell=$(egetshell "${SYS_USER_NAME}")
238
239 if [[ "${oldhome}" != "${SYS_USER_HOME}" ]]; then
240 die "home directory conflict for new user: ${SYS_USER_HOME}"
241 fi
242
243 if [[ "${oldhshell}" != "${SYS_USER_SHELL}" ]]; then
244 die "shell conflict for new user: ${SYS_USER_SHELL}"
245 fi
246
247 # The user already exists, so all we have left to do is to try
248 # to append SYS_USER_GROUPS to the existing groups. The home
249 # dir, shell, and uid should all match already.
250 sys-user_modify \
251 || die "failed to append groups to existing user ${SYS_USER_NAME}"
252 fi
253 }
254
255 sys-user_pkg_postinst() {
256 if [[ -n "${REPLACING_VERSIONS}" ]]; then
257 # This is an upgrade from a previous version of a sys-user
258 # package. This case has to be handled carefully to make sure
259 # that the pkg_prerm() of the old version doesn't remove the user
260 # that this new version is going to add. At this point, in our
261 # pkg_postinst(), the old version's pkg_prerm() phase should have
262 # already happened.
263 if [[ -n $(sys-user_getuid "${SYS_USER_NAME}") ]]; then
264 die "User ${SYS_USER_NAME} already exists during an upgrade."
265 else
266 sys-user_create || die "failed to add user ${SYS_USER_NAME}"
267 fi
268 fi
269 }
270
271 sys-user_pkg_prerm() {
272 if [[ -z $(sys-user_getuid "${SYS_USER_NAME}") ]]; then
273 # We have successfully done nothing.
274 ewarn "Tried to remove nonexistent user ${SYS_USER_NAME}."
275 else
276 userdel "${SYS_USER_NAME}" || \
277 die "failed to remove user ${SYS_USER_NAME}"
278 einfo "Removed user ${SYS_USER_NAME} from the system."
279 fi
280 }