483b06
From a4a85c69a945b023b4017ecf4285f9f5e97d5f20 Mon Sep 17 00:00:00 2001
483b06
From: David Kupka <dkupka@redhat.com>
483b06
Date: Tue, 11 Apr 2017 11:43:40 +0200
483b06
Subject: [PATCH] Create system users for FreeIPA services during package
483b06
 installation
483b06
483b06
Previously system users needed by FreeIPA server services was created during
483b06
ipa-server-install. This led to problem when DBus policy was configured during
483b06
package installation but the user specified in the policy didn't exist yet
483b06
(and potentionally similar ones). Now the users will be created in package %pre
483b06
section so all users freeipa-server package needs exist before any installation
483b06
or configuration begins.
483b06
Another possibility would be using systemd-sysusers(8) for this purpose but
483b06
given that systemd is not available during container build the traditional
483b06
approach is superior.
483b06
Also dirsrv and pkiuser users are no longer created by FreeIPA instead it
483b06
depends on 389ds and dogtag to create those users.
483b06
483b06
https://pagure.io/freeipa/issue/6743
483b06
483b06
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
483b06
Reviewed-By: Christian Heimes <cheimes@redhat.com>
483b06
Reviewed-By: Stanislav Laznicka <slaznick@redhat.com>
483b06
---
483b06
 freeipa.spec.in                            |  9 +++++
483b06
 ipaplatform/base/tasks.py                  | 53 ------------------------------
483b06
 ipaplatform/redhat/tasks.py                | 26 ---------------
483b06
 ipaserver/install/cainstance.py            | 12 -------
483b06
 ipaserver/install/dsinstance.py            | 11 -------
483b06
 ipaserver/install/httpinstance.py          | 13 --------
483b06
 ipaserver/install/installutils.py          | 13 --------
483b06
 ipaserver/install/ipa_restore.py           |  7 ----
483b06
 ipaserver/install/server/install.py        |  6 +---
483b06
 ipaserver/install/server/replicainstall.py |  6 +---
483b06
 ipaserver/install/server/upgrade.py        |  2 --
483b06
 11 files changed, 11 insertions(+), 147 deletions(-)
483b06
483b06
diff --git a/freeipa.spec.in b/freeipa.spec.in
483b06
index 829c3f0b2898de1ecbf0cfb769fde5cd978c241c..978ebb184f7d051b303940560f44c7a094b071a1 100644
483b06
--- a/freeipa.spec.in
483b06
+++ b/freeipa.spec.in
483b06
@@ -1030,6 +1030,15 @@ if [ -e /usr/sbin/ipa_kpasswd ]; then
483b06
 # END
483b06
 fi
483b06
 
483b06
+# create users and groups
483b06
+# create kdcproxy group and user
483b06
+getent group kdcproxy >/dev/null || groupadd -f -r kdcproxy
483b06
+getent passwd kdcproxy >/dev/null || useradd -r -g kdcproxy -s /sbin/nologin -d / -c "IPA KDC Proxy User" kdcproxy
483b06
+# create ipaapi group and user
483b06
+getent group ipaapi >/dev/null || groupadd -f -r ipaapi
483b06
+getent passwd ipaapi >/dev/null || useradd -r -g ipaapi -s /sbin/nologin -d / -c "IPA Framework User" ipaapi
483b06
+# add apache to ipaaapi group
483b06
+id -Gn apache | grep '\bipaapi\b' >/dev/null || usermod apache -a -G ipaapi
483b06
 
483b06
 %postun server-trust-ad
483b06
 if [ "$1" -ge "1" ]; then
483b06
diff --git a/ipaplatform/base/tasks.py b/ipaplatform/base/tasks.py
483b06
index 9f91fef2b572a29bf876641fd9ad879604054a2f..3358b7d257cc60ceaecfbbac5155d79b0e63de2e 100644
483b06
--- a/ipaplatform/base/tasks.py
483b06
+++ b/ipaplatform/base/tasks.py
483b06
@@ -22,9 +22,6 @@
483b06
 This module contains default platform-specific implementations of system tasks.
483b06
 '''
483b06
 
483b06
-import pwd
483b06
-import grp
483b06
-
483b06
 from pkg_resources import parse_version
483b06
 
483b06
 from ipaplatform.paths import paths
483b06
@@ -186,56 +183,6 @@ class BaseTaskNamespace(object):
483b06
 
483b06
         raise NotImplementedError()
483b06
 
483b06
-    def create_system_user(self, name, group, homedir, shell,
483b06
-                           uid=None, gid=None, comment=None,
483b06
-                           create_homedir=False, groups=None):
483b06
-        """Create a system user with a corresponding group"""
483b06
-        try:
483b06
-            grp.getgrnam(group)
483b06
-        except KeyError:
483b06
-            log.debug('Adding group %s', group)
483b06
-            args = [paths.GROUPADD, '-r', group]
483b06
-            if gid:
483b06
-                args += ['-g', str(gid)]
483b06
-            try:
483b06
-                ipautil.run(args)
483b06
-                log.debug('Done adding group')
483b06
-            except ipautil.CalledProcessError as e:
483b06
-                log.critical('Failed to add group: %s', e)
483b06
-                raise
483b06
-        else:
483b06
-            log.debug('group %s exists', group)
483b06
-
483b06
-        try:
483b06
-            pwd.getpwnam(name)
483b06
-        except KeyError:
483b06
-            log.debug('Adding user %s', name)
483b06
-            args = [
483b06
-                paths.USERADD,
483b06
-                '-g', group,
483b06
-                '-d', homedir,
483b06
-                '-s', shell,
483b06
-                '-r', name,
483b06
-            ]
483b06
-            if uid:
483b06
-                args += ['-u', str(uid)]
483b06
-            if comment:
483b06
-                args += ['-c', comment]
483b06
-            if create_homedir:
483b06
-                args += ['-m']
483b06
-            else:
483b06
-                args += ['-M']
483b06
-            if groups is not None:
483b06
-                args += ['-G', groups.join(',')]
483b06
-            try:
483b06
-                ipautil.run(args)
483b06
-                log.debug('Done adding user')
483b06
-            except ipautil.CalledProcessError as e:
483b06
-                log.critical('Failed to add user: %s', e)
483b06
-                raise
483b06
-        else:
483b06
-            log.debug('user %s exists', name)
483b06
-
483b06
     @staticmethod
483b06
     def parse_ipa_version(version):
483b06
         """
483b06
diff --git a/ipaplatform/redhat/tasks.py b/ipaplatform/redhat/tasks.py
483b06
index d0ef5fbd1ceb8110dd417dda44a74dc63898456a..07efebab97eabcf2dc39bd345920a1c7be56e9f5 100644
483b06
--- a/ipaplatform/redhat/tasks.py
483b06
+++ b/ipaplatform/redhat/tasks.py
483b06
@@ -431,32 +431,6 @@ class RedHatTaskNamespace(BaseTaskNamespace):
483b06
 
483b06
         return True
483b06
 
483b06
-    def create_system_user(self, name, group, homedir, shell,
483b06
-                           uid=None, gid=None, comment=None,
483b06
-                           create_homedir=False, groups=None):
483b06
-        """
483b06
-        Create a system user with a corresponding group
483b06
-
483b06
-        According to https://fedoraproject.org/wiki/Packaging:UsersAndGroups?rd=Packaging/UsersAndGroups#Soft_static_allocation
483b06
-        some system users should have fixed UID, GID and other parameters set.
483b06
-        This values should be constant and may be hardcoded.
483b06
-        Add other values for other users when needed.
483b06
-        """
483b06
-        if name == constants.PKI_USER:
483b06
-            if uid is None:
483b06
-                uid = 17
483b06
-            if gid is None:
483b06
-                gid = 17
483b06
-            if comment is None:
483b06
-                comment = 'CA System User'
483b06
-        if name == constants.DS_USER:
483b06
-            if comment is None:
483b06
-                comment = 'DS System User'
483b06
-
483b06
-        super(RedHatTaskNamespace, self).create_system_user(
483b06
-            name, group, homedir, shell, uid, gid, comment, create_homedir,
483b06
-            groups)
483b06
-
483b06
     def parse_ipa_version(self, version):
483b06
         """
483b06
         :param version: textual version
483b06
diff --git a/ipaserver/install/cainstance.py b/ipaserver/install/cainstance.py
483b06
index 3980e412603437b0db5804623f6626d11e52c009..ac5d9e2fc633c5ad732670245b72bee0f03268a6 100644
483b06
--- a/ipaserver/install/cainstance.py
483b06
+++ b/ipaserver/install/cainstance.py
483b06
@@ -46,7 +46,6 @@ from ipalib import errors
483b06
 import ipalib.constants
483b06
 from ipalib.install import certmonger
483b06
 from ipaplatform import services
483b06
-from ipaplatform.constants import constants
483b06
 from ipaplatform.paths import paths
483b06
 from ipaplatform.tasks import tasks
483b06
 
483b06
@@ -263,16 +262,6 @@ def is_ca_installed_locally():
483b06
     return os.path.exists(paths.CA_CS_CFG_PATH)
483b06
 
483b06
 
483b06
-def create_ca_user():
483b06
-    """Create PKI user/group if it doesn't exist yet."""
483b06
-    tasks.create_system_user(
483b06
-        name=constants.PKI_USER,
483b06
-        group=constants.PKI_GROUP,
483b06
-        homedir=paths.VAR_LIB,
483b06
-        shell=paths.NOLOGIN,
483b06
-    )
483b06
-
483b06
-
483b06
 class CAInstance(DogtagInstance):
483b06
     """
483b06
     When using a dogtag CA the DS database contains just the
483b06
@@ -382,7 +371,6 @@ class CAInstance(DogtagInstance):
483b06
             has_ra_cert = False
483b06
 
483b06
         if not ra_only:
483b06
-            self.step("creating certificate server user", create_ca_user)
483b06
             if promote:
483b06
                 # Setup Database
483b06
                 self.step("creating certificate server db", self.__create_ds_db)
483b06
diff --git a/ipaserver/install/dsinstance.py b/ipaserver/install/dsinstance.py
483b06
index 72fcb65f2eb699d0077d3c5cc02a3fcaaad9b8e5..99a1781ca4475805e9bf3b2bac3f26b5fb107a43 100644
483b06
--- a/ipaserver/install/dsinstance.py
483b06
+++ b/ipaserver/install/dsinstance.py
483b06
@@ -158,16 +158,6 @@ def is_ds_running(server_id=''):
483b06
     return services.knownservices.dirsrv.is_running(instance_name=server_id)
483b06
 
483b06
 
483b06
-def create_ds_user():
483b06
-    """Create DS user/group if it doesn't exist yet."""
483b06
-    tasks.create_system_user(
483b06
-        name=DS_USER,
483b06
-        group=DS_USER,
483b06
-        homedir=paths.VAR_LIB_DIRSRV,
483b06
-        shell=paths.NOLOGIN,
483b06
-    )
483b06
-
483b06
-
483b06
 def get_domain_level(api=api):
483b06
     ldap_uri = ipaldap.get_ldap_uri(protocol='ldapi', realm=api.env.realm)
483b06
     conn = ipaldap.LDAPClient(ldap_uri)
483b06
@@ -258,7 +248,6 @@ class DsInstance(service.Service):
483b06
 
483b06
     def __common_setup(self):
483b06
 
483b06
-        self.step("creating directory server user", create_ds_user)
483b06
         self.step("creating directory server instance", self.__create_instance)
483b06
         self.step("enabling ldapi", self.__enable_ldapi)
483b06
         self.step("configure autobind for root", self.__root_autobind)
483b06
diff --git a/ipaserver/install/httpinstance.py b/ipaserver/install/httpinstance.py
483b06
index 45bf479d1088c3b3396d955bf2592c4bce1e886f..8e444be2d23ec5e7890d221508bc866de2854c89 100644
483b06
--- a/ipaserver/install/httpinstance.py
483b06
+++ b/ipaserver/install/httpinstance.py
483b06
@@ -102,18 +102,6 @@ def httpd_443_configured():
483b06
     return False
483b06
 
483b06
 
483b06
-def create_kdcproxy_user():
483b06
-    """Create KDC proxy user/group if it doesn't exist yet."""
483b06
-    tasks.create_system_user(
483b06
-        name=KDCPROXY_USER,
483b06
-        group=KDCPROXY_USER,
483b06
-        homedir=paths.VAR_LIB_KDCPROXY,
483b06
-        shell=paths.NOLOGIN,
483b06
-        comment="IPA KDC Proxy User",
483b06
-        create_homedir=True,
483b06
-    )
483b06
-
483b06
-
483b06
 class WebGuiInstance(service.SimpleServiceInstance):
483b06
     def __init__(self):
483b06
         service.SimpleServiceInstance.__init__(self, "ipa_webgui")
483b06
@@ -183,7 +171,6 @@ class HTTPInstance(service.Service):
483b06
                   self.remove_httpd_ccaches)
483b06
         self.step("configuring SELinux for httpd", self.configure_selinux_for_httpd)
483b06
         if not self.is_kdcproxy_configured():
483b06
-            self.step("create KDC proxy user", create_kdcproxy_user)
483b06
             self.step("create KDC proxy config", self.create_kdcproxy_conf)
483b06
             self.step("enable KDC proxy", self.enable_kdcproxy)
483b06
         self.step("starting httpd", self.start)
483b06
diff --git a/ipaserver/install/installutils.py b/ipaserver/install/installutils.py
483b06
index ef6a399ad28ae8b8646864baea9965f762050484..9230e70056b1a773246a0d95e6ecb943cada953c 100644
483b06
--- a/ipaserver/install/installutils.py
483b06
+++ b/ipaserver/install/installutils.py
483b06
@@ -44,7 +44,6 @@ import six
483b06
 from six.moves.configparser import SafeConfigParser, NoOptionError
483b06
 # pylint: enable=import-error
483b06
 
483b06
-from ipalib.constants import IPAAPI_USER, IPAAPI_GROUP
483b06
 from ipalib.install import sysrestore
483b06
 from ipalib.install.kinit import kinit_password
483b06
 import ipaplatform
483b06
@@ -56,7 +55,6 @@ from ipalib import api, errors, x509
483b06
 from ipapython.dn import DN
483b06
 from ipaserver.install import certs, service, sysupgrade
483b06
 from ipaplatform import services
483b06
-from ipaplatform.constants import constants
483b06
 from ipaplatform.paths import paths
483b06
 from ipaplatform.tasks import tasks
483b06
 
483b06
@@ -1515,14 +1513,3 @@ def default_subject_base(realm_name):
483b06
 
483b06
 def default_ca_subject_dn(subject_base):
483b06
     return DN(('CN', 'Certificate Authority'), subject_base)
483b06
-
483b06
-
483b06
-def create_ipaapi_user():
483b06
-    """Create IPA API user/group if it doesn't exist yet."""
483b06
-    tasks.create_system_user(
483b06
-        name=IPAAPI_USER,
483b06
-        group=IPAAPI_GROUP,
483b06
-        homedir=paths.VAR_LIB,
483b06
-        shell=paths.NOLOGIN
483b06
-    )
483b06
-    tasks.add_user_to_group(constants.HTTPD_USER, IPAAPI_GROUP)
483b06
diff --git a/ipaserver/install/ipa_restore.py b/ipaserver/install/ipa_restore.py
483b06
index 2552bbdef36f653f1c377ea096ca227d09e5f3e6..378c013b6f4a4656768d7a484d2014a0f9eef3c0 100644
483b06
--- a/ipaserver/install/ipa_restore.py
483b06
+++ b/ipaserver/install/ipa_restore.py
483b06
@@ -36,8 +36,6 @@ from ipapython import version, ipautil
483b06
 from ipapython.ipautil import run, user_input
483b06
 from ipapython import admintool
483b06
 from ipapython.dn import DN
483b06
-from ipaserver.install.dsinstance import create_ds_user
483b06
-from ipaserver.install.cainstance import create_ca_user
483b06
 from ipaserver.install.replication import (wait_for_task, ReplicationManager,
483b06
                                            get_cs_replication_manager)
483b06
 from ipaserver.install import installutils
483b06
@@ -296,7 +294,6 @@ class Restore(admintool.AdminTool):
483b06
                     not user_input("Continue to restore?", False)):
483b06
                 raise admintool.ScriptError("Aborted")
483b06
 
483b06
-        create_ds_user()
483b06
         pent = pwd.getpwnam(constants.DS_USER)
483b06
 
483b06
         # Temporary directory for decrypting files before restoring
483b06
@@ -379,15 +376,11 @@ class Restore(admintool.AdminTool):
483b06
             # We do either a full file restore or we restore data.
483b06
             if restore_type == 'FULL':
483b06
                 self.remove_old_files()
483b06
-                if 'CA' in self.backup_services:
483b06
-                    create_ca_user()
483b06
                 self.cert_restore_prepare()
483b06
                 self.file_restore(options.no_logs)
483b06
                 self.cert_restore()
483b06
                 if 'CA' in self.backup_services:
483b06
                     self.__create_dogtag_log_dirs()
483b06
-                if http.is_kdcproxy_configured():
483b06
-                    httpinstance.create_kdcproxy_user()
483b06
 
483b06
             # Always restore the data from ldif
483b06
             # We need to restore both userRoot and ipaca.
483b06
diff --git a/ipaserver/install/server/install.py b/ipaserver/install/server/install.py
483b06
index bf2e248dceaae36ba0030d3eaa47976f51ce60ba..197f01ccef58bb3564eb4c6b5b4d615bff1e523d 100644
483b06
--- a/ipaserver/install/server/install.py
483b06
+++ b/ipaserver/install/server/install.py
483b06
@@ -39,7 +39,7 @@ from ipaserver.install import (
483b06
 from ipaserver.install.installutils import (
483b06
     IPA_MODULES, BadHostError, get_fqdn, get_server_ip_address,
483b06
     is_ipa_configured, load_pkcs12, read_password, verify_fqdn,
483b06
-    update_hosts_file, create_ipaapi_user)
483b06
+    update_hosts_file)
483b06
 
483b06
 if six.PY3:
483b06
     unicode = str
483b06
@@ -721,12 +721,8 @@ def install(installer):
483b06
         update_hosts_file(ip_addresses, host_name, fstore)
483b06
 
483b06
     # Make sure tmpfiles dir exist before installing components
483b06
-    create_ipaapi_user()
483b06
     tasks.create_tmpfiles_dirs()
483b06
 
483b06
-    # Create DS user/group if it doesn't exist yet
483b06
-    dsinstance.create_ds_user()
483b06
-
483b06
     # Create a directory server instance
483b06
     if not options.external_cert_files:
483b06
         # Configure ntpd
483b06
diff --git a/ipaserver/install/server/replicainstall.py b/ipaserver/install/server/replicainstall.py
483b06
index 6f1a0d6d29b20d53986205a63382a385e75f80ea..b82d7b474640e24da7d978e9546ebd7a8e602c29 100644
483b06
--- a/ipaserver/install/server/replicainstall.py
483b06
+++ b/ipaserver/install/server/replicainstall.py
483b06
@@ -41,8 +41,7 @@ from ipaserver.install import (
483b06
     installutils, kra, krbinstance,
483b06
     ntpinstance, otpdinstance, custodiainstance, service)
483b06
 from ipaserver.install.installutils import (
483b06
-    create_replica_config, ReplicaConfig, load_pkcs12, is_ipa_configured,
483b06
-    create_ipaapi_user)
483b06
+    create_replica_config, ReplicaConfig, load_pkcs12, is_ipa_configured)
483b06
 from ipaserver.install.replication import (
483b06
     ReplicationManager, replica_conn_check)
483b06
 import SSSDConfig
483b06
@@ -1347,7 +1346,6 @@ def install(installer):
483b06
     ccache = os.environ['KRB5CCNAME']
483b06
 
483b06
     # Make sure tmpfiles dir exist before installing components
483b06
-    create_ipaapi_user()
483b06
     tasks.create_tmpfiles_dirs()
483b06
 
483b06
     if promote:
483b06
@@ -1376,8 +1374,6 @@ def install(installer):
483b06
         ntp = ntpinstance.NTPInstance()
483b06
         ntp.create_instance()
483b06
 
483b06
-    dsinstance.create_ds_user()
483b06
-
483b06
     try:
483b06
         if promote:
483b06
             conn.connect(ccache=ccache)
483b06
diff --git a/ipaserver/install/server/upgrade.py b/ipaserver/install/server/upgrade.py
483b06
index 25b86297af3ae9d5f21cebb93f493b90670dcfc3..927acb011172de926773196eb1d032af8376f3d9 100644
483b06
--- a/ipaserver/install/server/upgrade.py
483b06
+++ b/ipaserver/install/server/upgrade.py
483b06
@@ -1652,7 +1652,6 @@ def upgrade_configuration():
483b06
 
483b06
     if not http.is_kdcproxy_configured():
483b06
         root_logger.info('[Enabling KDC Proxy]')
483b06
-        httpinstance.create_kdcproxy_user()
483b06
         http.create_kdcproxy_conf()
483b06
         http.enable_kdcproxy()
483b06
 
483b06
@@ -1837,7 +1836,6 @@ def upgrade_check(options):
483b06
 
483b06
 def upgrade():
483b06
     # Do this early so that any code depending on these dirs will not fail
483b06
-    installutils.create_ipaapi_user()
483b06
     tasks.create_tmpfiles_dirs()
483b06
     tasks.configure_tmpfiles()
483b06
 
483b06
-- 
483b06
2.9.3
483b06