File: NGLdapURL.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 (239 lines) | stat: -rw-r--r-- 5,708 bytes parent folder | download | duplicates (8)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
  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.
*/

#include "NGLdapURL.h"
#include "NGLdapConnection.h"
#include "NGLdapEntry.h"
#include "EOQualifier+LDAP.h"
#include <NGExtensions/NSString+misc.h>
#include "common.h"
#include <string.h>

@implementation NGLdapURL

+ (id)ldapURLWithString:(NSString *)_url {
  if (!ldap_is_ldap_url((char *)[_url UTF8String]))
    return nil;

  return [[[self alloc] initWithString:_url] autorelease];
}

- (id)initWithString:(NSString *)_url {
  LDAPURLDesc *urld = NULL;
  unsigned attrCount, i;
  int err;

  self->scope = -1;

  if ((err = ldap_url_parse((char *)[_url UTF8String], &urld)) != 0) {
    [self release];
    return nil;
  }
  if (urld == NULL) {
    [self release];
    return nil;
  }
    
  self->host   = [[NSString alloc] initWithCString:urld->lud_host];
  self->port   = urld->lud_port;
  self->base   = [[NSString alloc] initWithCString:urld->lud_dn];
  self->scope  = urld->lud_scope;
  self->filter = [[NSString alloc] initWithCString:urld->lud_filter];
  
  if (urld != NULL && urld->lud_attrs != NULL) {
    register char *tmp, **a;
    a = urld->lud_attrs;
    for (i = 0; (tmp = a[i]); i++)
      ;
    attrCount = i;
  }
  else
    attrCount = 0;
    
  if (attrCount > 0) {
    id *attrs;

    attrs = calloc(attrCount+1, sizeof(id));
    
    for (i = 0; i < attrCount; i++)
      attrs[i] = [[NSString alloc] initWithCString:urld->lud_attrs[i]];
      
    self->attributes = [[NSArray alloc] initWithObjects:attrs count:attrCount];

    for (i = 0; i < attrCount; i++)
      [attrs[i] release];
    if (attrs) free(attrs);
  }
  
  if (urld) ldap_free_urldesc(urld);
  
  return self;
}

- (id)init {
  return [self initWithString:nil];
}

- (void)dealloc {
  [self->host       release];
  [self->base       release];
  [self->filter     release];
  [self->attributes release];
  [super dealloc];
}

/* accessors */

- (NSString *)hostName {
  return self->host;
}
- (NSHost *)host {
  return [NSHost hostWithName:[self hostName]];
}

- (int)port {
  return self->port;
}
- (NSString *)baseDN {
  return self->base;
}
- (int)scope {
  return self->scope;
}

- (NSString *)searchFilter {
  return self->filter;
}
- (EOQualifier *)searchFilterQualifier {
  EOQualifier *q;
  
  if (self->filter == nil)
    return nil;

  q = nil;
  q = [[EOQualifier alloc] initWithLDAPFilterString:self->filter];

  return [q autorelease];
}

- (NSArray *)attributes {
  return self->attributes;
}

/* perform fetches */

- (NGLdapConnection *)openConnection {
  NGLdapConnection *con;

  con = [[NGLdapConnection alloc] initWithHostName:[self hostName]
                                  port:[self port] ? [self port] : 389];
  return [con autorelease];
}

- (NSEnumerator *)fetchEntries {
  NGLdapConnection *con;

  if ((con = [self openConnection]) == nil)
    return nil;
  
  switch (self->scope) {
    case LDAP_SCOPE_ONELEVEL:
      return [con flatSearchAtBaseDN:[self baseDN]
                  qualifier:[self searchFilterQualifier]
                  attributes:[self attributes]];
      
    case LDAP_SCOPE_SUBTREE:
      return [con deepSearchAtBaseDN:[self baseDN]
                  qualifier:[self searchFilterQualifier]
                  attributes:[self attributes]];
      
    case LDAP_SCOPE_BASE:
      return [con baseSearchAtBaseDN:[self baseDN]
                  qualifier:[self searchFilterQualifier]
                  attributes:[self attributes]];
  }

  return nil;
}
- (NGLdapEntry *)fetchEntry {
  return [[self fetchEntries] nextObject];
}

/* url */

- (NSString *)urlString {
  NSMutableString *s;
  NSString *r;
  
  s = [[NSMutableString alloc] initWithCapacity:200];

  [s appendString:@"ldap://"];
  [s appendString:self->host != nil ? self->host : (NSString *)@"localhost"];
  if (self->port > 0) [s appendFormat:@":%i", self->port];

  [s appendString:@"/"];
  [s appendString:[self->base stringByEscapingURL]];
  
  if ((self->attributes != nil) || (self->scope!=-1) || (self->filter != nil)){
    NSString *is;
    [s appendString:@"?"];
    is = [self->attributes componentsJoinedByString:@","];
    [s appendString:[is stringByEscapingURL]];
  }
  if ((self->scope != -1) || (self->filter != nil)) {
    [s appendString:@"?"];
    switch (self->scope) {
      case LDAP_SCOPE_ONELEVEL:
        [s appendString:@"one"];
        break;
      case LDAP_SCOPE_SUBTREE:
        [s appendString:@"sub"];
        break;
      case LDAP_SCOPE_BASE:
      default:
        [s appendString:@"base"];
        break;
    }
  }
  if ([self->filter isNotEmpty]) {
    [s appendString:@"?"];
    [s appendString:[self->filter stringByEscapingURL]];
  }

  r = [[s copy] autorelease];
  [s release]; s = nil;
  return r;
}

/* NSCopying */

- (id)copyWithZone:(NSZone *)_zone {
  return [[[self class] allocWithZone:_zone] initWithString:[self urlString]];
}

/* description */

- (NSString *)description {
  return [self urlString];
}

@end /* NGLdapURL */