1 /* LDAPSource.m - this file is part of SOGo
3 * Copyright (C) 2007 Inverse groupe conseil
5 * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
7 * This file is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This file is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 #import <Foundation/NSArray.h>
24 #import <Foundation/NSDictionary.h>
25 #import <Foundation/NSString.h>
27 #import <EOControl/EOControl.h>
28 #import <NGLdap/NGLdapConnection.h>
29 #import <NGLdap/NGLdapAttribute.h>
30 #import <NGLdap/NGLdapEntry.h>
32 #import "LDAPSource.h"
34 static NSArray *commonSearchFields;
36 @implementation LDAPSource
40 if (!commonSearchFields)
42 commonSearchFields = [NSArray arrayWithObjects:
55 @"mozillaHomeCountryName",
72 @"facsimileTelephoneNumber",
75 @"mozillaSecondEmail",
76 @"xmozillasecondemail",
88 @"mozillaHomePostalCode",
89 @"mozillaHomeLocalityName",
90 @"mozillaWorkStreet2",
91 @"mozillaUseHtmlMail",
92 @"xmozillausehtmlmail",
93 @"mozillaHomeStreet2",
111 [commonSearchFields retain];
115 + (id) sourceFromUDSource: (NSDictionary *) udSource
119 newSource = [[self alloc] initFromUDSource: udSource];
120 [newSource autorelease];
127 if ((self = [super init]))
135 IDField = @"cn"; /* the first part of a user DN */
139 ldapConnection = nil;
140 searchAttributes = nil;
155 [ldapConnection release];
159 - (id) initFromUDSource: (NSDictionary *) udSource
163 [self setBindDN: [udSource objectForKey: @"bindDN"]
164 hostname: [udSource objectForKey: @"hostname"]
165 port: [udSource objectForKey: @"port"]
166 andPassword: [udSource objectForKey: @"bindPassword"]];
167 [self setBaseDN: [udSource objectForKey: @"baseDN"]
168 IDField: [udSource objectForKey: @"IDFieldName"]
169 CNField: [udSource objectForKey: @"CNFieldName"]
170 andUIDField: [udSource objectForKey: @"UIDFieldName"]];
175 - (void) setBindDN: (NSString *) newBindDN
176 hostname: (NSString *) newBindHostname
177 port: (NSString *) newBindPort
178 andPassword: (NSString *) newBindPassword
180 ASSIGN (bindDN, newBindDN);
181 ASSIGN (hostname, newBindHostname);
183 port = [newBindPort intValue];
184 ASSIGN (password, newBindPassword);
187 - (void) setBaseDN: (NSString *) newBaseDN
188 IDField: (NSString *) newIDField
189 CNField: (NSString *) newCNField
190 andUIDField: (NSString *) newUIDField
192 ASSIGN (baseDN, newBaseDN);
194 ASSIGN (IDField, newIDField);
196 ASSIGN (CNField, newCNField);
198 ASSIGN (UIDField, newUIDField);
201 - (void) _initLDAPConnection
203 ldapConnection = [[NGLdapConnection alloc] initWithHostName: hostname
205 [ldapConnection bindWithMethod: @"simple"
207 credentials: password];
210 /* user management */
211 - (BOOL) checkLogin: (NSString *) loginToCheck
212 andPassword: (NSString *) passwordToCheck
216 NGLdapConnection *bindConnection;
218 bindConnection = [[NGLdapConnection alloc] initWithHostName: hostname
220 userDN = [NSString stringWithFormat: @"%@=%@,%@",
221 IDField, loginToCheck, baseDN];
223 didBind = [bindConnection bindWithMethod: @"simple" binddn: userDN
224 credentials: passwordToCheck];
229 [bindConnection release];
234 /* contact management */
235 - (EOQualifier *) _qualifierForFilter: (NSString *) filter
238 EOQualifier *qualifier;
240 if (filter && [filter length] > 0)
242 if ([filter isEqualToString: @"."])
245 qs = [NSString stringWithFormat:
248 @"OR (displayName='%@*')"
250 @"OR (telephoneNumber='*%@*')",
251 filter, filter, filter, filter, filter];
252 qualifier = [EOQualifier qualifierWithQualifierFormat: qs];
260 - (EOQualifier *) _qualifierForUIDFilter: (NSString *) uid
264 qs = [NSString stringWithFormat: (@"(%@='%@') OR (mail='%@')"
265 @" OR (mozillaSecondEmail='%@')"
266 @" OR (xmozillasecondemail='%@')"),
267 UIDField, uid, uid, uid, uid];
269 return [EOQualifier qualifierWithQualifierFormat: qs];
272 - (NSArray *) _searchAttributes
274 if (!searchAttributes)
276 searchAttributes = [NSMutableArray new];
278 [searchAttributes addObject: CNField];
280 [searchAttributes addObject: UIDField];
281 [searchAttributes addObjectsFromArray: commonSearchFields];
284 return searchAttributes;
287 - (NSArray *) allEntryIDs
290 NSEnumerator *entries;
291 NGLdapEntry *currentEntry;
294 ids = [NSMutableArray array];
297 [self _initLDAPConnection];
298 entries = [ldapConnection deepSearchAtBaseDN: baseDN
300 attributes: [NSArray arrayWithObject: IDField]];
303 currentEntry = [entries nextObject];
306 value = [[currentEntry attributeWithName: IDField]
307 stringValueAtIndex: 0];
308 if ([value length] > 0)
309 [ids addObject: value];
310 currentEntry = [entries nextObject];
317 - (NSDictionary *) _convertLDAPEntryToContact: (NGLdapEntry *) ldapEntry
319 NSMutableDictionary *contactEntry;
320 NSEnumerator *attributes;
321 NSString *currentAttribute, *value;
323 contactEntry = [NSMutableDictionary dictionary];
324 attributes = [[self _searchAttributes] objectEnumerator];
325 currentAttribute = [attributes nextObject];
326 while (currentAttribute)
328 value = [[ldapEntry attributeWithName: currentAttribute]
329 stringValueAtIndex: 0];
331 [contactEntry setObject: value forKey: currentAttribute];
332 currentAttribute = [attributes nextObject];
334 value = [[ldapEntry attributeWithName: IDField] stringValueAtIndex: 0];
337 [contactEntry setObject: value forKey: @"c_name"];
338 value = [[ldapEntry attributeWithName: UIDField] stringValueAtIndex: 0];
341 [contactEntry setObject: value forKey: @"c_uid"];
342 value = [[ldapEntry attributeWithName: CNField] stringValueAtIndex: 0];
345 [contactEntry setObject: value forKey: @"c_cn"];
350 - (NSArray *) fetchContactsMatching: (NSString *) match
352 NSMutableArray *contacts;
353 NGLdapEntry *currentEntry;
354 NSEnumerator *entries;
356 contacts = [NSMutableArray array];
359 [self _initLDAPConnection];
360 entries = [ldapConnection deepSearchAtBaseDN: baseDN
361 qualifier: [self _qualifierForFilter: match]
362 attributes: [self _searchAttributes]];
365 currentEntry = [entries nextObject];
369 [self _convertLDAPEntryToContact: currentEntry]];
370 currentEntry = [entries nextObject];
377 - (NSDictionary *) lookupContactEntry: (NSString *) entryID;
379 NSDictionary *contactEntry;
380 NGLdapEntry *ldapEntry;
383 [self _initLDAPConnection];
385 = [ldapConnection entryAtDN: [NSString stringWithFormat: @"%@=%@,%@",
386 IDField, entryID, baseDN]
387 attributes: [self _searchAttributes]];
389 contactEntry = [self _convertLDAPEntryToContact: ldapEntry];
396 - (NSDictionary *) lookupContactEntryWithUIDorEmail: (NSString *) uid;
398 NSDictionary *contactEntry;
399 NGLdapEntry *ldapEntry;
400 NSEnumerator *entries;
401 EOQualifier *qualifier;
404 [self _initLDAPConnection];
405 qualifier = [self _qualifierForUIDFilter: uid];
406 entries = [ldapConnection deepSearchAtBaseDN: baseDN
408 attributes: [self _searchAttributes]];
409 ldapEntry = [entries nextObject];
411 contactEntry = [self _convertLDAPEntryToContact: ldapEntry];