A hotfix pushed in a scope of ticket 3088 forced conversion of DN
object (baseDN) in IPA client discovery so that ipa-client-install
does not crash when creating an IPA default.conf. Since this is not
a preferred way to handle DN objects, improve its usage:

- make sure, that baseDN retrieved by client discovery is always
  a DN object
- update ipachangeconf.py code to handle strings better and instead
  of concatenating objects, make sure they are converted to string
  first

As a side-effect of ipachangeconf changes, default.conf config file
generated by ipa-client-install has no longer empty new line at the
end of a file.

https://fedorahosted.org/freeipa/ticket/3088

-- 
Martin Kosek <[email protected]>
Senior Software Engineer - Identity Management Team
Red Hat Inc.
From be95d1845b2460aedecb16f5581c2b86d1ce3755 Mon Sep 17 00:00:00 2001
From: Martin Kosek <[email protected]>
Date: Thu, 27 Sep 2012 12:40:55 +0200
Subject: [PATCH] Improve DN usage in ipa-client-install

A hotfix pushed in a scope of ticket 3088 forced conversion of DN
object (baseDN) in IPA client discovery so that ipa-client-install
does not crash when creating an IPA default.conf. Since this is not
a preferred way to handle DN objects, improve its usage:

- make sure, that baseDN retrieved by client discovery is always
  a DN object
- update ipachangeconf.py code to handle strings better and instead
  of concatenating objects, make sure they are converted to string
  first

As a side-effect of ipachangeconf changes, default.conf config file
generated by ipa-client-install has no longer empty new line at the
end of a file.

https://fedorahosted.org/freeipa/ticket/3088
---
 ipa-client/ipaclient/ipachangeconf.py | 85 +++++++++++++++++++++++------------
 ipa-client/ipaclient/ipadiscovery.py  |  2 +-
 ipapython/ipautil.py                  |  2 +-
 3 files changed, 58 insertions(+), 31 deletions(-)

diff --git a/ipa-client/ipaclient/ipachangeconf.py b/ipa-client/ipaclient/ipachangeconf.py
index f6288062be5c5d1b29341ed90814e1fa1431019c..2bb0cc487670ce74833fc09b06449d0a176ffaf5 100644
--- a/ipa-client/ipaclient/ipachangeconf.py
+++ b/ipa-client/ipaclient/ipachangeconf.py
@@ -68,7 +68,7 @@ class IPAChangeConf:
         elif type(indent) is str:
             self.indent = (indent, )
         else:
-           raise ValueError, 'Indent must be a list of strings'
+           raise ValueError('Indent must be a list of strings')
 
     def setOptionAssignment(self, assign):
         if type(assign) is tuple:
@@ -143,35 +143,50 @@ class IPAChangeConf:
     def getSectionLine(self, section):
         if len(self.sectnamdel) != 2:
             return section
-        return self.sectnamdel[0]+section+self.sectnamdel[1]+self.deol
+        return self._dump_line(self.sectnamdel[0],
+                               section,
+                               self.sectnamdel[1],
+                               self.deol)
+
+    def _dump_line(self, *args):
+        return u"".join(unicode(x) for x in args)
 
     def dump(self, options, level=0):
-        output = ""
+        output = []
         if level >= len(self.indent):
             level = len(self.indent)-1
 
         for o in options:
             if o['type'] == "section":
-                output += self.sectnamdel[0]+o['name']+self.sectnamdel[1]+self.deol
-                output += self.dump(o['value'], level+1)
+                output.append(self._dump_line(self.sectnamdel[0],
+                                              o['name'],
+                                              self.sectnamdel[1]))
+                output.append(self.dump(o['value'], level+1))
                 continue
             if o['type'] == "subsection":
-                output += self.indent[level]+o['name']+self.dassign+self.subsectdel[0]+self.deol
-                output += self.dump(o['value'], level+1)
-                output += self.indent[level]+self.subsectdel[1]+self.deol
+                output.append(self._dump_line(self.indent[level],
+                                              o['name'],
+                                              self.dassign,
+                                              self.subsectdel[0]))
+                output.append(self.dump(o['value'], level+1))
+                output.append(self._dump_line(self.indent[level],
+                                              self.subsectdel[1]))
                 continue
             if o['type'] == "option":
-                output += self.indent[level]+o['name']+self.dassign+o['value']+self.deol
+                output.append(self._dump_line(self.indent[level],
+                                              o['name'],
+                                              self.dassign,
+                                              o['value']))
                 continue
             if o['type'] == "comment":
-                output += self.dcomment+o['value']+self.deol
+                output.append(self._dump_line(self.dcomment, o['value']))
                 continue
             if o['type'] == "empty":
-                output += self.deol
+                output.append('')
                 continue
-            raise SyntaxError, 'Unknown type: ['+o['type']+']'
+            raise SyntaxError('Unknown type: [%s]' % o['type'])
 
-        return output
+        return self.deol.join(output)
 
     def parseLine(self, line):
 
@@ -184,7 +199,7 @@ class IPAChangeConf:
 
         parts = line.split(self.dassign, 1)
         if len(parts) < 2:
-            raise SyntaxError, 'Syntax Error: Unknown line format'
+            raise SyntaxError('Syntax Error: Unknown line format')
 
         return {'name':parts[0].strip(), 'type':'option', 'value':parts[1].rstrip()}
 
@@ -209,22 +224,30 @@ class IPAChangeConf:
         for o in inopts:
             if o['type'] == 'section':
                 no = self.commentOpts(o['value'], level+1)
-                val = self.dcomment+self.sectnamdel[0]+o['name']+self.sectnamdel[1]
+                val = self._dump_line(self.dcomment,
+                                      self.sectnamdel[0],
+                                      o['name'],
+                                      self.sectnamdel[1])
                 opts.append({'name':'comment', 'type':'comment', 'value':val})
                 for n in no:
                     opts.append(n)
                 continue
             if o['type'] == 'subsection':
                 no = self.commentOpts(o['value'], level+1)
-                val = self.indent[level]+o['name']+self.dassign+self.subsectdel[0]
+                val = self._dump_line(self.indent[level],
+                                      o['name'],
+                                      self.dassign,
+                                      self.subsectdel[0])
                 opts.append({'name':'comment', 'type':'comment', 'value':val})
-                for n in no:
-                    opts.append(n)
-                val = self.indent[level]+self.subsectdel[1]
+                opts.extend(no)
+                val = self._dump_line(self.indent[level], self.subsectdel[1])
                 opts.append({'name':'comment', 'type':'comment', 'value':val})
                 continue
             if o['type'] == 'option':
-                val = self.indent[level]+o['name']+self.dassign+o['value']
+                val = self._dump_line(self.indent[level],
+                                      o['name'],
+                                      self.dassign,
+                                      o['value'])
                 opts.append({'name':'comment', 'type':'comment', 'value':val})
                 continue
             if o['type'] == 'comment':
@@ -233,7 +256,7 @@ class IPAChangeConf:
             if o['type'] == 'empty':
                 opts.append({'name':'comment', 'type':'comment', 'value':''})
                 continue
-            raise SyntaxError, 'Unknown type: ['+o['type']+']'
+            raise SyntaxError('Unknown type: [%s]' % o['type'])
 
         return opts
 
@@ -258,7 +281,7 @@ class IPAChangeConf:
                     continue
                 if no['action'] == "remove":
                     continue
-                raise SyntaxError, 'Unknown action: ['+no['action']+']'
+                raise SyntaxError('Unknown action: [%s]' % no['action'])
 
             if o['type'] == "comment" or o['type'] == "empty":
                  opts.append(o)
@@ -274,15 +297,19 @@ class IPAChangeConf:
                         opts.append(o)
                         continue
                     if no['action'] == 'comment':
-                       opts.append({'name':'comment', 'type':'comment',
-                                    'value':self.dcomment+o['name']+self.dassign+o['value']})
+                        value = self._dump_line(self.dcomment,
+                                                o['name'],
+                                                self.dassign,
+                                                o['value'])
+                        opts.append({'name':'comment', 'type':'comment',
+                                     'value':value})
                     continue
                 if no['action'] == 'set':
                     opts.append(no)
                     continue
-                raise SyntaxError, 'Unknown action: ['+o['action']+']'
+                raise SyntaxError('Unknown action: [%s]' % o['action'])
 
-            raise SyntaxError, 'Unknown type: ['+o['type']+']'
+            raise SyntaxError('Unknown type: [%s]' % o['type'])
 
         return opts
 
@@ -318,7 +345,7 @@ class IPAChangeConf:
                 cline += 1
                 continue
 
-            raise SyntaxError, 'Unknown type: ['+no['type']+']'
+            raise SyntaxError('Unknown type: [%s]' % no['type'])
 
 
     def merge(self, oldopts, newopts):
@@ -363,7 +390,7 @@ class IPAChangeConf:
             value = self.matchSubSection(line)
             if value:
                 if subsection is not None:
-                    raise SyntaxError, 'nested subsections are not supported yet'
+                    raise SyntaxError('nested subsections are not supported yet')
                 subsectopts = []
                 curopts = subsectopts
                 subsection = value
@@ -372,7 +399,7 @@ class IPAChangeConf:
             value = self.matchSubSectionEnd(line)
             if value:
                 if subsection is None:
-                    raise SyntaxError, 'Unmatched end subsection terminator found'
+                    raise SyntaxError('Unmatched end subsection terminator found')
                 fatheropts.append({'name':subsection, 'type':'subsection', 'value':subsectopts})
                 subsection = None
                 curopts = fatheropts
diff --git a/ipa-client/ipaclient/ipadiscovery.py b/ipa-client/ipaclient/ipadiscovery.py
index 234e67e9b0f7102b1ee642fb3279793ff631ff9d..f91d4075aeafc71c3951ae094ddf075c143533f7 100644
--- a/ipa-client/ipaclient/ipadiscovery.py
+++ b/ipa-client/ipaclient/ipadiscovery.py
@@ -249,7 +249,7 @@ class IPADiscovery(object):
 
         if not ldapaccess and self.basedn is None:
             # Generate suffix from realm
-            self.basedn = str(realm_to_suffix(self.realm))
+            self.basedn = realm_to_suffix(self.realm)
             self.basedn_source = 'Generated from Kerberos realm'
             root_logger.debug("Generated basedn from realm: %s" % self.basedn)
 
diff --git a/ipapython/ipautil.py b/ipapython/ipautil.py
index d6e97b89ba3c6d288a4b9e60602a1918ae6e9e01..11433b4be832c1f6a79d17056e830c9582f3ca6e 100644
--- a/ipapython/ipautil.py
+++ b/ipapython/ipautil.py
@@ -831,7 +831,7 @@ def get_ipa_basedn(conn):
                 % (info, IPA_BASEDN_INFO))
             continue
         root_logger.debug("Naming context '%s' is a valid IPA context" % context)
-        return context
+        return DN(context)
 
     return None
 
-- 
1.7.11.4

_______________________________________________
Freeipa-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to