From 5bb4da3b7397a3ffb5497b67c731acdc49d0820c Mon Sep 17 00:00:00 2001
From: William Brown <firstyear@redhat.com>
Date: Tue, 8 Dec 2015 11:15:19 +1000
Subject: [PATCH] Ticket 48371 - weaker host check on localhost.localdomain

Bug Description:  The utility of the localhost.localdomain check is diminshed
given that we can now setup instances with weaker host checks. Additionally,
the hostname checks really only matter for GSSAPI instances.

Fix Description:  This sets the default host to localhost, and weakens the
default check. Additionally, we add a new args parameter that enforces strong
hostname checks on instances.

https://fedorahosted.org/389/ticket/48371

Author: wibrown

Review by: ???
---
 lib389/__init__.py   | 16 +++++++++++++---
 lib389/_constants.py |  1 +
 lib389/properties.py |  1 +
 lib389/tools.py      |  8 +++-----
 lib389/utils.py      |  2 ++
 5 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/lib389/__init__.py b/lib389/__init__.py
index 699a4c4..59e7d86 100644
--- a/lib389/__init__.py
+++ b/lib389/__init__.py
@@ -459,7 +459,6 @@ class DirSrv(SimpleLDAPObject):
            @raise ValueError - if missing mandatory properties or invalid
                                state of DirSrv
         '''
-        DirSrvTools.testLocalhost()
         if self.state != DIRSRV_STATE_INIT and \
            self.state != DIRSRV_STATE_ALLOCATED:
             raise ValueError("invalid state for calling allocate: %s" %
@@ -469,7 +468,17 @@ class DirSrv(SimpleLDAPObject):
             self.log.info('SER_SERVERID_PROP not provided')
 
         # Settings from args of server attributes
-        self.host = args.get(SER_HOST, LOCALHOST)
+        self.strict_hostname = args.get(SER_STRICT_HOSTNAME_CHECKING, False)
+        self.host = None
+        if self.strict_hostname is True:
+            self.host = args.get(SER_HOST, LOCALHOST)
+            if self.host == LOCALHOST:
+                DirSrvTools.testLocalhost()
+            else:
+                # Make sure our name is in hosts
+                DirSrvTools.searchHostsFile(self.host, None)
+        else:
+            self.host = args.get(SER_HOST, LOCALHOST_SHORT)
         self.port = args.get(SER_PORT, DEFAULT_PORT)
         self.sslport = args.get(SER_SECURE_PORT)
         self.binddn = args.get(SER_ROOT_DN, DN_DM)
@@ -821,7 +830,8 @@ class DirSrv(SimpleLDAPObject):
                 SER_SERVERID_PROP: self.serverid,
                 SER_GROUP_ID: self.groupid,
                 SER_DEPLOYED_DIR: self.prefix,
-                SER_BACKUP_INST_DIR: self.backupdir}
+                SER_BACKUP_INST_DIR: self.backupdir,
+                SER_STRICT_HOSTNAME_CHECKING: self.strict_hostname}
         content = formatInfData(args)
         result = DirSrvTools.runInfProg(prog, content, verbose,
                                         prefix=self.prefix)
diff --git a/lib389/_constants.py b/lib389/_constants.py
index 8e45bf9..6ab5503 100644
--- a/lib389/_constants.py
+++ b/lib389/_constants.py
@@ -78,6 +78,7 @@ DIRSRV_STATE_OFFLINE = 'offline'
 DIRSRV_STATE_ONLINE = 'online'
 
 LOCALHOST = "localhost.localdomain"
+LOCALHOST_SHORT = "localhost"
 DEFAULT_PORT = 389
 DEFAULT_SECURE_PORT = 636
 DEFAULT_SUFFIX = 'dc=example,dc=com'
diff --git a/lib389/properties.py b/lib389/properties.py
index 0fed907..8d1333b 100644
--- a/lib389/properties.py
+++ b/lib389/properties.py
@@ -39,6 +39,7 @@ SER_GROUP_ID = 'group-id'
 SER_DEPLOYED_DIR = 'deployed-dir'
 SER_BACKUP_INST_DIR = 'inst-backupdir'
 SER_CREATION_SUFFIX = 'suffix'
+SER_STRICT_HOSTNAME_CHECKING = 'strict_hostname_checking'
 
 ####################################
 #
diff --git a/lib389/tools.py b/lib389/tools.py
index f91c200..f044a65 100644
--- a/lib389/tools.py
+++ b/lib389/tools.py
@@ -932,9 +932,6 @@ class DirSrvTools(object):
         with open(hostFile, 'r') as hostfp:
             # The with statement will automatically close the file after use
 
-            # We are already at the start of the file
-            # hostfp.seek(0, os.SEEK_CUR)
-
             try:
                 for line in hostfp.readlines():
                     if ipPattern is None:
@@ -961,9 +958,10 @@ class DirSrvTools(object):
         Checks that the 127.0.0.1 is resolved as localhost.localdomain
         This is required by DSUtil.pm:checkHostname else setup-ds.pl fails
         '''
+        # One day I'll make this ::1. Ipv6 is the future. The future is now.
         loopbackIpPattern = '127.0.0.1'
-        expectedHost = 'localhost.localdomain'
-        DirSrvTools.searchHostsFile(expectedHost, loopbackIpPattern)
+        DirSrvTools.searchHostsFile(LOCALHOST, loopbackIpPattern)
+        DirSrvTools.searchHostsFile(LOCALHOST_SHORT, loopbackIpPattern)
 
     @staticmethod
     def runUpgrade(prefix, online=True):
diff --git a/lib389/utils.py b/lib389/utils.py
index 5c4747f..1fd8310 100644
--- a/lib389/utils.py
+++ b/lib389/utils.py
@@ -674,6 +674,7 @@ def formatInfData(args):
     content += ("FullMachineName= %s\n" % args[SER_HOST])
     content += ("SuiteSpotUserID= %s\n" % args[SER_USER_ID])
     content += ("nSuiteSpotGroup= %s\n" % args[SER_GROUP_ID])
+    content += ("StrictHostCheck= %s\n" % args[SER_STRICT_HOSTNAME_CHECKING])
 
     if args.get('have_admin'):
         content += ("AdminDomain= %(admin_domain)s" "\n"
@@ -689,6 +690,7 @@ def formatInfData(args):
     content += ("ServerIdentifier= %s\n" % args[SER_SERVERID_PROP])
     content += ("Suffix= %s\n" % args[SER_CREATION_SUFFIX])
 
+
     # Create admin?
     if args.get('setup_admin'):
         content += ("SlapdConfigForMC= Yes" "\n"
-- 
2.5.0

