File: NGLdapSearchResultEnumerator.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 (335 lines) | stat: -rw-r--r-- 8,108 bytes parent folder | download | duplicates (3)
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
  Copyright (C) 2000-2005 SKYRIX Software AG

  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 "NGLdapSearchResultEnumerator.h"
#include "NGLdapConnection+Private.h"
#include "NGLdapAttribute.h"
#include "NGLdapEntry.h"
#include "common.h"

#include <sys/time.h>

@implementation NGLdapSearchResultEnumerator

- (id)initWithConnection:(NGLdapConnection *)_con messageID:(int)_mid {
  if ((self = [super init]) != nil) {
    self->connection = [_con retain];
    self->handle     = [_con ldapHandle];
    self->msgid      = _mid;
    
    // TODO: -timeIntervalSince1970 deprecated on Cocoa
    self->startTime  = [[NSDate date] timeIntervalSince1970];
  }
  return self;
}

- (id)init {
  return [self initWithConnection:NULL messageID:-1];
}

- (void)dealloc {
  [self->connection release];
  [super dealloc];
}

/* state */

- (int)messageID {
  return self->msgid;
}

- (NSTimeInterval)duration {
  // TODO: -timeIntervalSince1970 deprecated on Cocoa
  return [[NSDate date] timeIntervalSince1970] - self->startTime;
}

- (unsigned)index {
  return self->index;
}

- (void)setTimeout:(NSTimeInterval)_value {
  self->timeout = _value;
}
- (NSTimeInterval)timeout {
  return self->timeout;
}

/* enumerator */

- (void)cancel {
  if (self->handle) {
    ldap_abandon(self->handle, self->msgid);
    
    self->handle = NULL;
    [self->connection release]; self->connection = nil;
  }
}

- (NSArray *)_attributesFromResult:(LDAPMessage *)result {
  NSMutableArray *attributes;
  char           *attr;
  BerElement     *ber;

  attributes = [[NSMutableArray alloc] initWithCapacity:32];
      
  for (attr = ldap_first_attribute(self->handle, result, &ber);
       attr != NULL;
       free(attr), attr = ldap_next_attribute(self->handle, result, ber)) {
    NSString        *key;
    NGLdapAttribute *attribute;
    struct berval   **values;
    unsigned        valueCount;
    NSArray         *ovalues;

    if (!(key = [[NSString alloc] initWithCString:attr]))
      /* missing attribute name */
      continue;

    /* process values */
        
    if ((values = ldap_get_values_len(self->handle, result, attr)) == NULL) {
      ovalues = [[NSArray alloc] init];
    }
    else if ((valueCount = ldap_count_values_len(values)) == 1) {
      NSData *value;
          
      value = [[NSData alloc] initWithBytes:values[0]->bv_val
                              length:values[0]->bv_len];
          
      ovalues = [[NSArray alloc] initWithObjects:&value count:1];
          
      [value release];
    }
    else {
      NSMutableArray *a;
      int j;
          
      a = [[NSMutableArray alloc] initWithCapacity:valueCount];
          
      for (j = 0; values[j]; j++) {
        NSData *data;

        data = [[NSData alloc] initWithBytes:values[j]->bv_val
                               length:values[j]->bv_len];
        [a addObject:data];
        [data release];
      }
      ovalues = [a copy];
      [a release];
    }

    if (values) {
      ldap_value_free_len(values);
      values = NULL;
    }
        
    /* create attribute */
        
    attribute =
      [[NGLdapAttribute alloc] initWithAttributeName:key values:ovalues];
        
    [key     release]; key    = nil;
    [ovalues release]; ovalues = nil;

    [attributes addObject:attribute];
    [attribute release];
  }
#if 0
  if (first) {
    ldap_memfree(first);
  }
#endif
  if (ber) {
    ber_free(ber, 0);
  }
  return attributes;
}

- (id)nextObject {
  int            res;
  struct timeval to;
  struct timeval *top;
  LDAPMessage *msg;
  id          record;

  if (self->handle == NULL)
    return nil;

  msg = NULL;
  record = nil;

  top = NULL;
  if (self->timeout > 0) {
    to.tv_sec = self->timeout;
    to.tv_usec = (long)(self->timeout * 1000.0) - (to.tv_sec * 1000);
    top = &to;
  }
  
  res = ldap_result(self->handle, self->msgid, 0, top, &msg);

  if (res <= 0 || msg == NULL)
    {
      int err;
      ldap_get_option(self->handle, LDAP_OPT_RESULT_CODE, &err);
      NSLog(@"Fatal LDAP error during ldap_result: %s", ldap_err2string(err));
      return nil;
    }
  
  switch(res) {
#if defined(LDAP_RES_SEARCH_REFERENCE)
  case LDAP_RES_SEARCH_REFERENCE: {
    int         rres;
    char        **rptr = NULL;
    LDAPControl **ctrl = NULL;

    rres = ldap_parse_reference(self->handle, msg, &rptr, &ctrl,
				0 /* don't free msg */);
    if (rres == LDAP_SUCCESS) {
    }
    else {
      /* error */
      NSLog(@"%s: couldn't parse result reference ..", __PRETTY_FUNCTION__);
    }

    NSLog(@"ERROR(%s): does not support result references yet ..",
	  __PRETTY_FUNCTION__);
        
    if (rptr != NULL) ldap_value_free(rptr);
    if (ctrl != NULL) ldap_controls_free(ctrl);
        
    break;
  }
#endif
      
  case LDAP_RES_SEARCH_ENTRY: {
    int resultCount;
        
    if ((resultCount = ldap_count_entries(self->handle, msg)) == -1) {
      /* failed */
      int err;
    
      err = ldap_result2error(self->handle, msg, 1 /* free msg */);
          
      [[self->connection _exceptionForErrorCode:err
	    operation:@"count-fetch"
	    userInfo:nil]
	raise];
      return nil;
    }
    
    if (resultCount == 1) {
      LDAPMessage *result;
      NSString    *dn = nil;
      char        *tmp;
      NSArray     *attributes;
          
      if ((result = ldap_first_entry(self->handle, msg)) == NULL) {
	/* could not get entry */
	[[self->connection _exceptionForErrorCode:resultCount
	      operation:@"fetch"
	      userInfo:nil]
	  raise];
            
	return nil;
      }
    
      /* get distinguished name */
          
      if ((tmp = ldap_get_dn(self->handle, result)) != NULL) {
	// TODO: slow ..., somehow fix that.

	/* try UTF-8 (as per spec?) */

	NS_DURING {
	  dn = [[[NSString alloc] initWithUTF8String:tmp] autorelease];
	}
	NS_HANDLER {
	  fprintf(stderr, "Got exception %s while NSUTF8StringEncoding, "
		  "use defaultCStringEncoding",
		  [[localException description] cString]);
	  dn = nil;
	}
	NS_ENDHANDLER;
	
	/* try system encoding (Latin-1 on libFoundation) */
	
	if (dn == nil) // TODO: print a warning?
	  dn = [[[NSString alloc] initWithCString:tmp] autorelease];

	if (tmp != NULL) free(tmp);
      }
      
      /* get all attributes */
          
      attributes = [self _attributesFromResult:result];

      if (result != NULL) {
	// TODO: ldap_msgfree(result); // do not release result-msg ???
	result = NULL;
      }
      
      record = [[NGLdapEntry alloc] initWithDN:dn attributes:attributes];
          
      [attributes release]; attributes = nil;
    }
    else if (resultCount == 0) {
      /* no more results */
      record = nil;
    }
    break;
  }

  case LDAP_RES_SEARCH_RESULT:
    self->handle = NULL;
    [self->connection release]; self->connection = nil;
    break;

  default:
    NSLog(@"NGLdap(%s): unexpected msg-code: %X", __PRETTY_FUNCTION__,res);
    break;
  }
  if (msg != NULL)
    ldap_msgfree(msg);
  
  if (record != nil)
    self->index++;

  return [record autorelease];
}

/* description */

- (NSString *)description {
  NSMutableString *s;
  
  s = [NSMutableString stringWithCapacity:100];
  [s appendFormat:@"<0x%p[%@]:", self, NSStringFromClass([self class])];

  [s appendFormat:@" msgid=%i", [self messageID]];
  [s appendFormat:@" duration=%.2fs", [self duration]];
  [s appendFormat:@" index=%i", [self index]];
  
  [s appendString:@">"];

  return s;
}

@end /* LDAPResultEnumerator */