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