From 21f18217d638c3b11dd9800def3613512366899b Mon Sep 17 00:00:00 2001
From: Adam Tkac <vonsch@gmail.com>
Date: Wed, 19 Dec 2012 21:08:05 +0100
Subject: [PATCH] NSString+DN.m:dnComponents method failed to parse DNs which
 contain commas

NSString+DN.m:dnComponents failed to parse for example following DN:

dn: CN=Tkac\, Adam,OU=ITZ,DC=geodis,DC=cz

The DN was splitted into

"CN=Tkac\", "Adam", "OU=ITZ", "DC=geodis", "DC=cz"

which is apparently wrong. Now the DN is splitted correctly into

"CN=Tkac\, Adam", "OU=ITZ", "DC=geodis", "DC=cz"

Signed-off-by: Adam Tkac <vonsch@gmail.com>
---
 sope-ldap/NGLdap/NSString+DN.m | 42 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 41 insertions(+), 1 deletion(-)

diff --git a/sope-ldap/NGLdap/NSString+DN.m b/sope-ldap/NGLdap/NSString+DN.m
index eddc067..e5a979f 100644
--- a/sope-ldap/NGLdap/NSString+DN.m
+++ b/sope-ldap/NGLdap/NSString+DN.m
@@ -22,6 +22,9 @@
 
 #import <Foundation/NSCharacterSet.h>
 
+#define LDAP_DEPRECATED 1
+#include <ldap.h>
+
 #include "NSString+DN.h"
 #include <NGExtensions/NSString+Ext.h>
 #include "common.h"
@@ -48,6 +51,43 @@ static NSArray *cleanDNComponents(NSArray *_components) {
   return _components;
 }
 
+static NSArray *explodeDN(const char *dn) {
+  char **exploded;
+  unsigned i;
+  NSMutableArray *array;
+  NSArray *ret;
+  id *cs;
+
+  if (dn == NULL)
+    return nil;
+
+  if (dn[0] == '\0') {
+    return [NSArray arrayWithObjects: @"", nil];
+  }
+
+  exploded = ldap_explode_dn(dn, 0);
+  if (exploded == NULL)
+    return nil;
+
+  /* Count number of RDNs */
+  for (i = 0; exploded[i] != NULL; i++);
+
+  cs = calloc(i, sizeof(id));
+
+  array = [[NSMutableArray alloc] initWithCapacity:i];
+  for (i = 0; exploded[i] != NULL; i++) {
+    [array addObject: [NSString stringWithCString:exploded[i]]];
+  }
+
+  ldap_value_free(exploded);
+
+  ret = [array copy];
+
+  if (cs != NULL) { free(cs); cs = NULL; }
+
+  return cleanDNComponents(ret);
+}
+
 @implementation NSString(DNSupport)
 
 + (NSString *)dnWithComponents:(NSArray *)_components {
@@ -55,7 +95,7 @@ static NSArray *cleanDNComponents(NSArray *_components) {
 }
 
 - (NSArray *)dnComponents {
-  return cleanDNComponents([self componentsSeparatedByString:dnSeparator]);
+  return explodeDN([self cString]);
 }
 
 - (NSString *)stringByAppendingDNComponent:(NSString *)_component {
-- 
1.8.0.2

