File: NSString%2BDN.m

package info (click to toggle)
sope 4.0.7-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 16,492 kB
  • sloc: objc: 167,993; sh: 3,648; ansic: 3,405; python: 318; makefile: 189
file content (220 lines) | stat: -rw-r--r-- 5,971 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
  Copyright (C) 2000-2007 SKYRIX Software AG
  Copyright (C) 2007      Helge Hess

  This file is part of SOPE.

  SOPE is free software; you can redistribute it and/or modify it under
  the terms of the GNU Lesser General Public License as published by the
  Free Software Foundation; either version 2, or (at your option) any
  later version.

  SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with SOPE; see the file COPYING.  If not, write to the
  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  02111-1307, USA.
*/

#import <Foundation/NSCharacterSet.h>
#import <Foundation/NSArray.h>

#include <ldap.h>

#include "NSString+DN.h"
#include <NGExtensions/NSString+Ext.h>
#include "common.h"

static NSString *dnSeparator = @",";

static NSCharacterSet *escapableChars = nil;

static NSArray *cleanDNComponents(NSArray *_components) {
  unsigned i, count;
  id *cs;
  
  if ((count = [_components count]) == 0)
    return nil;
  
  cs = calloc(count, sizeof(id));
  
  for (i = 0; i < count; i++)
    cs[i] = [[_components objectAtIndex:i] stringByTrimmingWhiteSpaces];
  
  _components = [NSArray arrayWithObjects:cs count:count];
  if (cs != NULL) { free(cs); cs = NULL; }
  
  return _components;
}

@implementation NSString(DNSupport)

+ (NSString *)dnWithComponents:(NSArray *)_components {
  return [cleanDNComponents(_components) componentsJoinedByString:dnSeparator];
}

/* returns each dn component in a NSArray 
 * returns nil if there is a decoding error
 */
- (NSArray *)dnComponents {
  char *componentStr;
  int i, err;

  LDAPDN dn;

  NSMutableArray *components;

  if (![self length])
    return nil;

  dn = NULL;
  components = [NSMutableArray arrayWithCapacity:0];

  err = ldap_str2dn([self cStringUsingEncoding: NSUTF8StringEncoding],
                    &dn, LDAP_DN_FORMAT_LDAPV3);
  if(err) {
    /* sorry for the noise but this has to be known */
    NSLog(@"ldap_str2dn: %s\n", ldap_err2string(err));
    ldap_dnfree(dn);
    return nil;
  }
    
  /* loop through the dn parts
   * convert them back to properly quoted/escaped strings 
   */
  for (i=0; dn[i]; i++) {
    componentStr = NULL;
    err = ldap_rdn2str(dn[i], &componentStr,
                       LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY);
    if(err) {
      NSLog(@"ldap_rdn2dn: %s\n", ldap_err2string(err));
      ldap_dnfree(dn);
      return nil;
    }

    if(componentStr) {
      [components addObject:
                    [NSString stringWithCString: componentStr
                                       encoding: NSUTF8StringEncoding]];
      free(componentStr);
    }
  }

  ldap_dnfree(dn);
  return [NSArray arrayWithArray: components];
}

- (NSString *)stringByAppendingDNComponent:(NSString *)_component {
  NSString *s;

  if (![(s = [self stringByTrimmingWhiteSpaces]) isNotEmpty])
    return _component;
  
  s = [dnSeparator stringByAppendingString:self];
  return [_component stringByAppendingString:s];
}

- (NSString *)stringByDeletingLastDNComponent {
  NSMutableArray *components;

  components = [NSMutableArray arrayWithArray: [self dnComponents]];
  if (![components count])
    return nil;

  /* "Last DN component" is actually the first component :
   * For "cn=bob,ou=users,dc=example,dc=com", remove "cn=bob"
   */
  [components removeObjectAtIndex: 0];

  return [components componentsJoinedByString:dnSeparator];
}

- (NSString *)lastDNComponent {
  NSMutableArray *components;

  components = [NSMutableArray arrayWithArray: [self dnComponents]];
  if (![components count])
    return nil;

  /* "Last DN component" is actually the first component :
   * For "cn=bob,ou=users,dc=example,dc=com", return "cn=bob"
   */
  return [components objectAtIndex: 0];
}

- (const char *)ldapRepresentation {
  return [self UTF8String];
}

- (NSDate *)ldapTimestamp {
  /* eg: '20000403055250Z' */
  unsigned   len;
  short      year, month, day, hour, minute, second;
  NSString   *tzname;
  NSTimeZone *tz;
  
  if ((len = [self length]) == 0)
    return nil;

  if (len < 14)
    return nil;
  
  year   = [[self substringWithRange:NSMakeRange(0,  4)] intValue];
  month  = [[self substringWithRange:NSMakeRange(4,  2)] intValue];
  day    = [[self substringWithRange:NSMakeRange(6,  2)] intValue];
  hour   = [[self substringWithRange:NSMakeRange(8,  2)] intValue];
  minute = [[self substringWithRange:NSMakeRange(10, 2)] intValue];
  second = [[self substringWithRange:NSMakeRange(12, 2)] intValue];

  /* timezone ??? */
  tzname = @"GMT";
  tz = [NSTimeZone timeZoneWithAbbreviation:tzname];

  return [NSCalendarDate dateWithYear:year month:month
                         day:day hour:hour minute:minute second:second
                         timeZone:tz];
}

- (NSString *) escapedForLDAPDN
{
  NSMutableString *newString;
  NSString *format;
  unichar *uniString;
  unichar currentChar;
  NSUInteger count, length;

  if (!escapableChars)
    {
      escapableChars = [NSCharacterSet
                         characterSetWithCharactersInString: @"\"+,;<>\\"];
      [escapableChars retain];
    }

  length = [self length];
  newString = [NSMutableString stringWithCapacity: length];

  uniString = NSZoneMalloc (NULL, sizeof (unichar) * length);
  [self getCharacters: uniString];

  /* see rfc4514, section 2.4 */
  for (count = 0; count < length; count++)
    {
      currentChar = uniString[count];
      if ((currentChar == ' ' && (count == 0 || count == (length - 1)))
          || [escapableChars characterIsMember: currentChar])
        format = @"\\%Lc";
      else
        format = @"%Lc";
      [newString appendFormat: format, currentChar];
    }

  NSZoneFree (NULL, uniString);

  return newString;
}

@end /* NSString(DNSupport) */