1 # Copyright 1999-2017 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: sys-user.eclass
6 # Michael Orlitzky <mjo@gentoo.org>
7 # @BLURB: handle installation and removal of system users.
9 # This eclass does most of the work for the sys-user/ packages that
10 # supply system user accounts.
12 # Needed for egetshell and egethome.
15 EXPORT_FUNCTIONS pkg_pretend src_unpack src_configure src_compile src_install src_test pkg_preinst pkg_postinst pkg_prerm
17 : ${HOMEPAGE:="https://www.gentoo.org/"}
18 : ${DESCRIPTION:="The ${PN} system user"}
21 # If you want a different username, use a different package name. This
22 # prevents different people from claiming the same username.
25 # @ECLASS-VARIABLE: SYS_USER_GROUPS
27 # A space-separated list of groups that the user will belong to.
28 # Dependencies on the appropriate sys-group packages are generated
30 : ${SYS_USER_GROUPS:=${PN}}
32 # @ECLASS-VARIABLE: SYS_USER_UID
38 # @ECLASS-VARIABLE: SYS_USER_UID_IMPORTANT
41 # Set to "true" if you want to die() if you don't get your desired UID.
42 : ${SYS_USER_UID_IMPORTANT:=false}
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}"
50 # @ECLASS-VARIABLE: SYS_USER_HOME
53 : ${SYS_USER_HOME:=/home/${SYS_USER_NAME}}
55 # @ECLASS-VARIABLE: SYS_USER_SHELL
58 : ${SYS_USER_SHELL:=/bin/false}
63 die "${ECLASS} is not compatible with EAPI=${EAPI}"
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}:= "
75 sys-user_src_unpack() { :; }
76 sys-user_src_compile() { :; }
77 sys-user_src_test() { :; }
80 # Output the real UID of the given user, or the empty string if the
81 # user does not exist on the system.
82 [[ $# -eq 1 ]] || die "usage: sys-user_getuid <username>"
83 echo $(id --real --user "${1}")
87 # Output the username associated with the given UID, or the empty string
88 # if the given UID is still available.
89 [[ $# -eq 1 ]] || die "usage: sys-user_getname <uid>"
90 echo $(egetent passwd "${1}" | cut -f1 -d':')
94 # Create the user whose information is contained in the following
103 # We don't create a group with the same name; that should be the
104 # job of the matching sys-group package.
105 useradd --no-user-group \
106 ${SYS_USER_UID:+--uid }"${SYS_USER_UID}" \
107 ${SYS_USER_GROUPS:+--groups }"${SYS_USER_GROUPS}" \
108 --shell "${SYS_USER_SHELL}" \
109 --home-dir "${SYS_USER_HOME}" \
115 # Modify the existing user named $SYS_USER_NAME to match the values
116 # contained in the following variables:
123 usermod ${SYS_USER_UID:+--uid }"${SYS_USER_UID}" \
124 ${SYS_USER_GROUPS:+--append --groups }"${SYS_USER_GROUPS}" \
125 --shell "${SYS_USER_SHELL}" \
126 --home-dir "${SYS_USER_HOME}" \
130 sys-user_pkg_pretend() {
131 # Sanity checks that would otherwise run code in global scope.
133 # First ensure that the user didn't say his UID is important and
134 # then fail to specify one.
135 if [[ -z "${SYS_USER_UID}" ]] &&
136 [[ "${SYS_USER_UID_IMPORTANT}" == "true" ]]; then
137 # Don't make no damn sense.
138 die "arbitrary UID requested with SYS_USER_UID_IMPORTANT=true"
141 # Next ensure that no other username owns an important UID.
142 if [[ "${SYS_USER_UID_IMPORTANT}" == "true" ]]; then
143 # Ok, the UID is important. Make sure nobody else has it. Or
144 # rather, nobody else *with a different username* has it.
145 local oldname=$(sys-user_getname "${SYS_USER_UID}")
146 if [[ "${SYS_USER_NAME}" != "${oldname}" ]]; then
147 die "important UID ${SYS_USER_UID} already belongs to ${oldname}"
151 # Finally, ensure that this username doesn't already exist with
152 # another UID if its UID is supposedly important.
153 local olduid=$(sys-user_getuid "${SYS_USER_NAME}")
154 if [[ -n "${olduid}" ]]; then
155 if [[ "${SYS_USER_UID_IMPORTANT}" == "true" ]] && \
156 [[ "${SYS_USER_UID}" != "${olduid}" ]]; then
157 # The UID is important and specified, but there is already a
158 # system user with this name and a different UID. Halp.
159 die "user ${SYS_USER_NAME} already exists with UID ${olduid}"
164 sys-user_src_configure() {
165 local current_uid=$(sys-user_getuid "${SYS_USER_NAME}")
166 if [[ -n "${current_uid}" ]]; then
167 # UPGRADE PATH: This user already exists, so if the eclass
168 # consumer doesn't care about some settings, we can reuse the
171 # This is also useful for sys-user package upgrades, because it
172 # prevents us from incrementing the UID on a reinstall, and doing
173 # so would break most packages that need a system user to exist.
174 if [[ "${SYS_USER_UID_IMPORTANT}" != "true" ]]; then
175 SYS_USER_UID="${current_uid}"
178 if [[ -z "${SYS_USER_HOME}" ]]; then
179 SYS_USER_HOME=$(egethome "${SYS_USER_NAME}")
182 if [[ -z "${SYS_USER_SHELL}" ]]; then
183 SYS_USER_SHELL=$(egetshell "${SYS_USER_NAME}")
187 if [[ -n "${SYS_USER_UID}" ]]; then
188 # A specific UID was requested.
189 local current_name=$(sys-user_getname "${SYS_USER_UID}")
190 if [[ "${current_name}" != "${SYS_USER_NAME}" ]]; then
191 # This UID is already taken by another user, but this
192 # specific UID was not important (we checked in
193 # pkg_pretend), so fall back to an arbitrary one.
198 # The "useradd" and "usermod" tools expect a comma-separated list,
199 # so change our spaces to commas. Having duplicates in the list is
200 # not a problem for those two tools.
201 SYS_USER_GROUPS="${SYS_USER_GROUPS// /,}"
204 sys-user_src_install() {
205 # Install a placeholder file to /var/lib/sys-user/$uid. This will
206 # cause collisions if two packages try to install users with the
207 # same UID. The same problem potentially exists with the username,
208 # but as long as SYS_USER_NAME is hard-coded to $PN, that shouldn't
211 # Beware, this only works if SYS_USER_UID is guaranteed to have a
212 # real UID and not, for example, -1.
214 # TODO: this is a problem now!
216 #touch "${T}/${SYS_USER_UID}" || die
217 #insinto "/var/lib/sys-user"
218 #doins "${T}/${SYS_USER_UID}"
221 sys-user_pkg_preinst() {
222 if [[ -z $(sys-user_getuid "${SYS_USER_NAME}") ]]; then
223 # The user does not already exist. This is the nice and easy
224 # case because no matter how we got here, we want to go ahead
225 # and create the (new) user.
226 sys-user_create || die "failed to add user ${SYS_USER_NAME}"
227 elif [[ -n "${REPLACING_VERSIONS}" ]]; then
229 # This case is done in pkg_postint() to avoid clobbering a
230 # new user when we remove the old one.
234 # UPGRADE PATH: Ok, the user exists but this isn't an upgrade of
235 # a sys-user package. This is the upgrade path from the old
236 # style of user/group management to the new style. Lets see if
237 # the new user is compatible with the old one; it usually will be.
238 # We only bail out if there's a homedir or shell conflict.
240 # We should make it policy that new sys-user packages have the
241 # same homedir and shell as the existing ones created by
242 # ebuilds, but it can't hurt to check again here. These checks
243 # are done here (and not in pkg_pretend, where they would be
244 # more consistent) because the PMS states that REPLACING_VERSIONS
245 # may not be defined there.
247 # If a homedir/shell changes during a sys-user upgrade, we don't
248 # consider that a problem, because the change was knowingly made
249 # by a developer who just edited an ebuild to make that change.
250 local oldhome=$(egethome "${SYS_USER_NAME}")
251 local oldshell=$(egetshell "${SYS_USER_NAME}")
253 if [[ "${oldhome}" != "${SYS_USER_HOME}" ]]; then
254 die "home directory conflict for new user: ${SYS_USER_HOME}"
257 if [[ "${oldhshell}" != "${SYS_USER_SHELL}" ]]; then
258 die "shell conflict for new user: ${SYS_USER_SHELL}"
261 # The user already exists, so all we have left to do is to try
262 # to append SYS_USER_GROUPS to the existing groups. The home
263 # dir, shell, and uid should all match already.
265 || die "failed to append groups to existing user ${SYS_USER_NAME}"
269 sys-user_pkg_postinst() {
270 if [[ -n "${REPLACING_VERSIONS}" ]]; then
271 # This is an upgrade from a previous version of a sys-user
272 # package. This case has to be handled carefully to make sure
273 # that the pkg_prerm() of the old version doesn't remove the user
274 # that this new version is going to add. At this point, in our
275 # pkg_postinst(), the old version's pkg_prerm() phase should have
277 if [[ -n $(sys-user_getuid "${SYS_USER_NAME}") ]]; then
278 die "User ${SYS_USER_NAME} already exists during an upgrade."
280 sys-user_create || die "failed to add user ${SYS_USER_NAME}"
285 sys-user_pkg_prerm() {
286 if [[ -z $(sys-user_getuid "${SYS_USER_NAME}") ]]; then
287 # We have successfully done nothing.
288 ewarn "Tried to remove nonexistent user ${SYS_USER_NAME}."
290 userdel "${SYS_USER_NAME}" || \
291 die "failed to remove user ${SYS_USER_NAME}"
292 einfo "Removed user ${SYS_USER_NAME} from the system."