2007-05-09 21:06:15 +02:00
|
|
|
/* LDAPSource.m - this file is part of SOGo
|
|
|
|
*
|
2009-03-16 22:13:01 +01:00
|
|
|
* Copyright (C) 2007-2009 Inverse inc.
|
2007-05-09 21:06:15 +02:00
|
|
|
*
|
|
|
|
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
|
|
|
|
*
|
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This file 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import <Foundation/NSArray.h>
|
|
|
|
#import <Foundation/NSDictionary.h>
|
2008-12-19 17:18:03 +01:00
|
|
|
#import <Foundation/NSLock.h>
|
2007-05-09 21:06:15 +02:00
|
|
|
#import <Foundation/NSString.h>
|
2008-10-04 01:12:11 +02:00
|
|
|
#import <Foundation/NSUserDefaults.h>
|
2007-05-09 21:06:15 +02:00
|
|
|
|
2009-04-02 22:51:46 +02:00
|
|
|
#import <NGExtensions/NSObject+Logs.h>
|
2007-05-09 21:06:15 +02:00
|
|
|
#import <EOControl/EOControl.h>
|
|
|
|
#import <NGLdap/NGLdapConnection.h>
|
|
|
|
#import <NGLdap/NGLdapAttribute.h>
|
|
|
|
#import <NGLdap/NGLdapEntry.h>
|
|
|
|
|
2007-10-10 20:44:26 +02:00
|
|
|
#import "LDAPUserManager.h"
|
2008-04-25 17:16:58 +02:00
|
|
|
#import "NSArray+Utilities.h"
|
2008-08-21 21:19:11 +02:00
|
|
|
#import "NSString+Utilities.h"
|
|
|
|
|
|
|
|
#import "LDAPSource.h"
|
2007-05-09 21:06:15 +02:00
|
|
|
|
2009-06-04 16:33:39 +02:00
|
|
|
#define SafeLDAPCriteria(x) [[x stringByReplacingString: @"\\" withString: @"\\\\"] \
|
|
|
|
stringByReplacingString: @"'" withString: @"\\'"]
|
2007-05-09 21:06:15 +02:00
|
|
|
static NSArray *commonSearchFields;
|
2008-10-04 01:12:11 +02:00
|
|
|
static NSString *LDAPContactInfoAttribute = nil;
|
2007-07-23 21:04:54 +02:00
|
|
|
static int timeLimit;
|
|
|
|
static int sizeLimit;
|
2007-05-09 21:06:15 +02:00
|
|
|
|
2008-12-19 17:18:03 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
static NSLock *lock;
|
|
|
|
#endif
|
|
|
|
|
2007-05-09 21:06:15 +02:00
|
|
|
@implementation LDAPSource
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
{
|
2007-07-23 21:04:54 +02:00
|
|
|
NSUserDefaults *ud;
|
|
|
|
|
2007-05-09 21:06:15 +02:00
|
|
|
if (!commonSearchFields)
|
|
|
|
{
|
2007-07-23 21:04:54 +02:00
|
|
|
ud = [NSUserDefaults standardUserDefaults];
|
2008-11-23 09:31:05 +01:00
|
|
|
LDAPContactInfoAttribute
|
|
|
|
= [ud stringForKey: @"SOGoLDAPContactInfoAttribute"];
|
|
|
|
[LDAPContactInfoAttribute retain];
|
2007-07-23 21:04:54 +02:00
|
|
|
sizeLimit = [ud integerForKey: @"SOGoLDAPQueryLimit"];
|
|
|
|
timeLimit = [ud integerForKey: @"SOGoLDAPQueryTimeout"];
|
|
|
|
|
2007-05-09 21:06:15 +02:00
|
|
|
commonSearchFields = [NSArray arrayWithObjects:
|
|
|
|
@"title",
|
|
|
|
@"company",
|
|
|
|
@"o",
|
|
|
|
@"displayName",
|
|
|
|
@"modifytimestamp",
|
|
|
|
@"mozillaHomeState",
|
|
|
|
@"mozillaHomeUrl",
|
|
|
|
@"homeurl",
|
|
|
|
@"st",
|
|
|
|
@"region",
|
|
|
|
@"mozillaCustom2",
|
|
|
|
@"custom2",
|
|
|
|
@"mozillaHomeCountryName",
|
|
|
|
@"description",
|
|
|
|
@"notes",
|
|
|
|
@"department",
|
|
|
|
@"departmentnumber",
|
|
|
|
@"ou",
|
|
|
|
@"orgunit",
|
|
|
|
@"mobile",
|
|
|
|
@"cellphone",
|
|
|
|
@"carphone",
|
|
|
|
@"mozillaCustom1",
|
|
|
|
@"custom1",
|
|
|
|
@"mozillaNickname",
|
|
|
|
@"xmozillanickname",
|
|
|
|
@"mozillaWorkUrl",
|
|
|
|
@"workurl",
|
|
|
|
@"fax",
|
|
|
|
@"facsimileTelephoneNumber",
|
|
|
|
@"telephoneNumber",
|
|
|
|
@"mozillaHomeStreet",
|
|
|
|
@"mozillaSecondEmail",
|
|
|
|
@"xmozillasecondemail",
|
|
|
|
@"mozillaCustom4",
|
|
|
|
@"custom4",
|
|
|
|
@"nsAIMid",
|
|
|
|
@"nscpaimscreenname",
|
|
|
|
@"street",
|
|
|
|
@"streetAddress",
|
|
|
|
@"postOfficeBox",
|
|
|
|
@"homePhone",
|
|
|
|
@"cn",
|
|
|
|
@"commonname",
|
|
|
|
@"givenName",
|
|
|
|
@"mozillaHomePostalCode",
|
|
|
|
@"mozillaHomeLocalityName",
|
|
|
|
@"mozillaWorkStreet2",
|
|
|
|
@"mozillaUseHtmlMail",
|
|
|
|
@"xmozillausehtmlmail",
|
|
|
|
@"mozillaHomeStreet2",
|
|
|
|
@"postalCode",
|
|
|
|
@"zip",
|
|
|
|
@"c",
|
|
|
|
@"countryname",
|
|
|
|
@"pager",
|
|
|
|
@"pagerphone",
|
|
|
|
@"mail",
|
|
|
|
@"sn",
|
|
|
|
@"surname",
|
|
|
|
@"mozillaCustom3",
|
|
|
|
@"custom3",
|
|
|
|
@"l",
|
|
|
|
@"locality",
|
|
|
|
@"birthyear",
|
|
|
|
@"serialNumber",
|
2007-10-10 20:44:26 +02:00
|
|
|
@"calFBURL", @"proxyAddresses",
|
2008-11-23 09:31:05 +01:00
|
|
|
nil];
|
2007-05-09 21:06:15 +02:00
|
|
|
[commonSearchFields retain];
|
2008-12-19 17:18:03 +01:00
|
|
|
|
|
|
|
#if defined(THREADSAFE)
|
|
|
|
lock = [NSLock new];
|
|
|
|
#endif
|
2007-05-09 21:06:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (id) sourceFromUDSource: (NSDictionary *) udSource
|
|
|
|
{
|
|
|
|
id newSource;
|
|
|
|
|
|
|
|
newSource = [[self alloc] initFromUDSource: udSource];
|
|
|
|
[newSource autorelease];
|
|
|
|
|
|
|
|
return newSource;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
|
|
|
bindDN = nil;
|
|
|
|
hostname = nil;
|
|
|
|
port = 389;
|
2009-04-02 22:51:46 +02:00
|
|
|
encryption = nil;
|
2007-05-09 21:06:15 +02:00
|
|
|
password = nil;
|
2007-10-10 20:44:26 +02:00
|
|
|
sourceID = nil;
|
2007-05-09 21:06:15 +02:00
|
|
|
|
|
|
|
baseDN = nil;
|
|
|
|
IDField = @"cn"; /* the first part of a user DN */
|
|
|
|
CNField = @"cn";
|
|
|
|
UIDField = @"uid";
|
2007-11-18 11:16:25 +01:00
|
|
|
mailFields = [NSArray arrayWithObject: @"mail"];
|
|
|
|
[mailFields retain];
|
2009-05-12 21:56:39 +02:00
|
|
|
IMAPHostField = nil;
|
2007-05-10 23:18:06 +02:00
|
|
|
bindFields = nil;
|
2009-04-06 14:54:03 +02:00
|
|
|
_scope = @"sub";
|
2009-03-16 22:13:01 +01:00
|
|
|
_filter = nil;
|
2007-05-09 21:06:15 +02:00
|
|
|
|
|
|
|
ldapConnection = nil;
|
|
|
|
searchAttributes = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[bindDN release];
|
|
|
|
[hostname release];
|
2009-04-02 22:51:46 +02:00
|
|
|
[encryption release];
|
2007-05-09 21:06:15 +02:00
|
|
|
[password release];
|
|
|
|
[baseDN release];
|
|
|
|
[IDField release];
|
|
|
|
[CNField release];
|
|
|
|
[UIDField release];
|
2007-11-18 11:16:25 +01:00
|
|
|
[mailFields release];
|
2009-05-12 21:56:39 +02:00
|
|
|
[IMAPHostField release];
|
2007-05-10 23:18:06 +02:00
|
|
|
[bindFields release];
|
2009-03-16 22:13:01 +01:00
|
|
|
[_filter release];
|
2007-05-09 21:06:15 +02:00
|
|
|
[ldapConnection release];
|
2007-10-10 20:44:26 +02:00
|
|
|
[sourceID release];
|
2007-11-26 21:41:59 +01:00
|
|
|
[modulesConstraints release];
|
2009-04-06 14:54:03 +02:00
|
|
|
[_scope release];
|
2007-05-09 21:06:15 +02:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initFromUDSource: (NSDictionary *) udSource
|
|
|
|
{
|
|
|
|
self = [self init];
|
|
|
|
|
2009-06-04 16:02:45 +02:00
|
|
|
ASSIGN (sourceID, [udSource objectForKey: @"id"]);
|
2007-10-10 20:44:26 +02:00
|
|
|
|
2007-05-09 21:06:15 +02:00
|
|
|
[self setBindDN: [udSource objectForKey: @"bindDN"]
|
2009-04-02 22:51:46 +02:00
|
|
|
password: [udSource objectForKey: @"bindPassword"]
|
2007-05-09 21:06:15 +02:00
|
|
|
hostname: [udSource objectForKey: @"hostname"]
|
|
|
|
port: [udSource objectForKey: @"port"]
|
2009-04-02 22:51:46 +02:00
|
|
|
encryption: [udSource objectForKey: @"encryption"]];
|
2007-05-09 21:06:15 +02:00
|
|
|
[self setBaseDN: [udSource objectForKey: @"baseDN"]
|
|
|
|
IDField: [udSource objectForKey: @"IDFieldName"]
|
|
|
|
CNField: [udSource objectForKey: @"CNFieldName"]
|
2007-11-18 11:16:25 +01:00
|
|
|
UIDField: [udSource objectForKey: @"UIDFieldName"]
|
|
|
|
mailFields: [udSource objectForKey: @"MailFieldNames"]
|
2009-05-12 21:56:39 +02:00
|
|
|
IMAPHostField: [udSource objectForKey: @"IMAPHostFieldName"]
|
2007-05-10 23:18:06 +02:00
|
|
|
andBindFields: [udSource objectForKey: @"bindFields"]];
|
2009-06-04 16:02:45 +02:00
|
|
|
ASSIGN (modulesConstraints,
|
|
|
|
[udSource objectForKey: @"ModulesConstraints"]);
|
|
|
|
ASSIGN (_filter, [udSource objectForKey: @"filter"]);
|
|
|
|
ASSIGN (_scope, ([udSource objectForKey: @"scope"]
|
|
|
|
? [udSource objectForKey: @"scope"]
|
|
|
|
: @"sub"));
|
2009-04-06 14:54:03 +02:00
|
|
|
|
2007-05-09 21:06:15 +02:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setBindDN: (NSString *) newBindDN
|
2009-04-02 22:51:46 +02:00
|
|
|
password: (NSString *) newBindPassword
|
2007-05-09 21:06:15 +02:00
|
|
|
hostname: (NSString *) newBindHostname
|
|
|
|
port: (NSString *) newBindPort
|
2009-04-02 22:51:46 +02:00
|
|
|
encryption: (NSString *) newEncryption
|
2007-05-09 21:06:15 +02:00
|
|
|
{
|
|
|
|
ASSIGN (bindDN, newBindDN);
|
2009-04-02 22:51:46 +02:00
|
|
|
ASSIGN (encryption, [newEncryption uppercaseString]);
|
|
|
|
if ([encryption isEqualToString: @"SSL"])
|
|
|
|
port = 636;
|
2007-05-09 21:06:15 +02:00
|
|
|
ASSIGN (hostname, newBindHostname);
|
|
|
|
if (newBindPort)
|
|
|
|
port = [newBindPort intValue];
|
|
|
|
ASSIGN (password, newBindPassword);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setBaseDN: (NSString *) newBaseDN
|
|
|
|
IDField: (NSString *) newIDField
|
|
|
|
CNField: (NSString *) newCNField
|
2007-05-10 23:18:06 +02:00
|
|
|
UIDField: (NSString *) newUIDField
|
2007-11-18 11:16:25 +01:00
|
|
|
mailFields: (NSArray *) newMailFields
|
2009-05-12 21:56:39 +02:00
|
|
|
IMAPHostField: (NSString *) newIMAPHostField
|
2007-05-10 23:18:06 +02:00
|
|
|
andBindFields: (NSString *) newBindFields
|
2007-05-09 21:06:15 +02:00
|
|
|
{
|
2009-07-06 23:25:38 +02:00
|
|
|
ASSIGN (baseDN, [newBaseDN lowercaseString]);
|
2007-05-09 21:06:15 +02:00
|
|
|
if (newIDField)
|
|
|
|
ASSIGN (IDField, newIDField);
|
2009-03-16 19:26:08 +01:00
|
|
|
if (newCNField)
|
2007-05-09 21:06:15 +02:00
|
|
|
ASSIGN (CNField, newCNField);
|
2009-03-16 19:26:08 +01:00
|
|
|
if (newUIDField)
|
2007-05-09 21:06:15 +02:00
|
|
|
ASSIGN (UIDField, newUIDField);
|
2009-05-12 21:56:39 +02:00
|
|
|
if (newIMAPHostField)
|
|
|
|
ASSIGN (IMAPHostField, newIMAPHostField);
|
2007-11-18 11:16:25 +01:00
|
|
|
if (newMailFields)
|
|
|
|
ASSIGN (mailFields, newMailFields);
|
2007-05-10 23:18:06 +02:00
|
|
|
if (newBindFields)
|
|
|
|
ASSIGN (bindFields, newBindFields);
|
2007-05-09 21:06:15 +02:00
|
|
|
}
|
|
|
|
|
2009-04-02 22:51:46 +02:00
|
|
|
- (BOOL) _setupEncryption: (NGLdapConnection *) encryptedConn
|
|
|
|
{
|
|
|
|
BOOL rc;
|
|
|
|
|
|
|
|
if ([encryption isEqualToString: @"SSL"])
|
|
|
|
rc = [encryptedConn useSSL];
|
2009-04-03 02:35:21 +02:00
|
|
|
else if ([encryption isEqualToString: @"STARTTLS"])
|
2009-04-02 22:51:46 +02:00
|
|
|
rc = [encryptedConn startTLS];
|
|
|
|
else
|
|
|
|
{
|
2009-04-03 02:35:21 +02:00
|
|
|
[self errorWithFormat:
|
|
|
|
@"encryption scheme '%@' not supported:"
|
|
|
|
@" use 'SSL' or 'STARTTLS'", encryption];
|
2009-04-02 22:51:46 +02:00
|
|
|
rc = NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2008-09-08 16:54:33 +02:00
|
|
|
- (BOOL) _initLDAPConnection
|
2007-05-09 21:06:15 +02:00
|
|
|
{
|
2008-09-08 16:54:33 +02:00
|
|
|
BOOL b;
|
|
|
|
|
|
|
|
NS_DURING
|
|
|
|
{
|
|
|
|
ldapConnection = [[NGLdapConnection alloc] initWithHostName: hostname
|
|
|
|
port: port];
|
2009-04-02 22:51:46 +02:00
|
|
|
if (![encryption length] || [self _setupEncryption: ldapConnection])
|
|
|
|
{
|
|
|
|
[ldapConnection bindWithMethod: @"simple"
|
|
|
|
binddn: bindDN
|
|
|
|
credentials: password];
|
|
|
|
if (sizeLimit > 0)
|
|
|
|
[ldapConnection setQuerySizeLimit: sizeLimit];
|
|
|
|
if (timeLimit > 0)
|
|
|
|
[ldapConnection setQueryTimeLimit: timeLimit];
|
|
|
|
b = YES;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
b = NO;
|
2008-09-08 16:54:33 +02:00
|
|
|
}
|
|
|
|
NS_HANDLER
|
|
|
|
{
|
2008-10-20 16:04:09 +02:00
|
|
|
NSLog(@"Could not bind to the LDAP server %@ (%d) using the bind DN: %@", hostname, port, bindDN);
|
2008-09-08 16:54:33 +02:00
|
|
|
b = NO;
|
|
|
|
}
|
|
|
|
NS_ENDHANDLER;
|
|
|
|
|
|
|
|
return b;
|
2007-05-09 21:06:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* user management */
|
2007-05-10 23:18:06 +02:00
|
|
|
- (EOQualifier *) _qualifierForBindFilter: (NSString *) uid
|
|
|
|
{
|
|
|
|
NSMutableString *qs;
|
2009-06-04 16:02:45 +02:00
|
|
|
NSString *escapedUid;
|
2007-05-10 23:18:06 +02:00
|
|
|
NSEnumerator *fields;
|
|
|
|
NSString *currentField;
|
|
|
|
|
|
|
|
qs = [NSMutableString string];
|
2008-05-15 23:06:37 +02:00
|
|
|
|
2009-06-04 16:33:39 +02:00
|
|
|
escapedUid = SafeLDAPCriteria (uid);
|
2009-06-04 16:02:45 +02:00
|
|
|
|
2007-05-10 23:18:06 +02:00
|
|
|
fields = [[bindFields componentsSeparatedByString: @","] objectEnumerator];
|
2008-05-15 23:06:37 +02:00
|
|
|
while ((currentField = [fields nextObject]))
|
2009-06-04 16:02:45 +02:00
|
|
|
[qs appendFormat: @" OR (%@='%@')", currentField, escapedUid];
|
|
|
|
|
2009-03-16 22:13:01 +01:00
|
|
|
if (_filter && [_filter length])
|
|
|
|
[qs appendFormat: @" AND %@", _filter];
|
|
|
|
|
|
|
|
[qs deleteCharactersInRange: NSMakeRange(0, 4)];
|
2007-05-10 23:18:06 +02:00
|
|
|
|
|
|
|
return [EOQualifier qualifierWithQualifierFormat: qs];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) _fetchUserDNForLogin: (NSString *) loginToCheck
|
|
|
|
{
|
|
|
|
NSString *userDN;
|
|
|
|
NSEnumerator *entries;
|
|
|
|
NGLdapEntry *userEntry;
|
|
|
|
|
2008-09-08 16:54:33 +02:00
|
|
|
if ([self _initLDAPConnection])
|
|
|
|
{
|
2009-04-06 14:54:03 +02:00
|
|
|
EOQualifier *qualifier;
|
|
|
|
NSArray *attributes;
|
|
|
|
|
|
|
|
qualifier = [self _qualifierForBindFilter: loginToCheck];
|
|
|
|
attributes = [NSArray arrayWithObject: @"dn"];
|
|
|
|
|
|
|
|
if ([_scope caseInsensitiveCompare: @"BASE"] == NSOrderedSame)
|
|
|
|
{
|
|
|
|
entries = [ldapConnection baseSearchAtBaseDN: baseDN
|
|
|
|
qualifier: qualifier
|
|
|
|
attributes: attributes];
|
|
|
|
}
|
|
|
|
else if ([_scope caseInsensitiveCompare: @"ONE"] == NSOrderedSame)
|
|
|
|
{
|
|
|
|
entries = [ldapConnection flatSearchAtBaseDN: baseDN
|
|
|
|
qualifier: qualifier
|
|
|
|
attributes: attributes];
|
|
|
|
}
|
2009-04-13 17:01:21 +02:00
|
|
|
else
|
2009-04-06 14:54:03 +02:00
|
|
|
{
|
|
|
|
entries = [ldapConnection deepSearchAtBaseDN: baseDN
|
|
|
|
qualifier: qualifier
|
|
|
|
attributes: attributes];
|
|
|
|
}
|
|
|
|
|
2008-09-08 16:54:33 +02:00
|
|
|
userEntry = [entries nextObject];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
userEntry = nil;
|
|
|
|
|
2007-05-10 23:18:06 +02:00
|
|
|
if (userEntry)
|
|
|
|
userDN = [userEntry dn];
|
|
|
|
else
|
|
|
|
userDN = nil;
|
2008-09-08 16:54:33 +02:00
|
|
|
|
|
|
|
[ldapConnection autorelease];
|
2007-05-10 23:18:06 +02:00
|
|
|
|
|
|
|
return userDN;
|
|
|
|
}
|
|
|
|
|
2007-05-09 21:06:15 +02:00
|
|
|
- (BOOL) checkLogin: (NSString *) loginToCheck
|
|
|
|
andPassword: (NSString *) passwordToCheck
|
|
|
|
{
|
|
|
|
BOOL didBind;
|
|
|
|
NSString *userDN;
|
|
|
|
NGLdapConnection *bindConnection;
|
|
|
|
|
2008-12-19 17:18:03 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock lock];
|
|
|
|
#endif
|
|
|
|
|
2007-08-29 18:01:11 +02:00
|
|
|
didBind = NO;
|
|
|
|
|
2007-05-10 16:56:47 +02:00
|
|
|
if ([loginToCheck length] > 0)
|
|
|
|
{
|
|
|
|
bindConnection = [[NGLdapConnection alloc] initWithHostName: hostname
|
|
|
|
port: port];
|
2009-04-02 22:51:46 +02:00
|
|
|
if (![encryption length] || [self _setupEncryption: bindConnection])
|
2007-05-10 23:18:06 +02:00
|
|
|
{
|
2009-04-02 22:51:46 +02:00
|
|
|
if (timeLimit > 0)
|
|
|
|
[ldapConnection setQueryTimeLimit: timeLimit];
|
|
|
|
if (bindFields)
|
|
|
|
userDN = [self _fetchUserDNForLogin: loginToCheck];
|
|
|
|
else
|
|
|
|
userDN = [NSString stringWithFormat: @"%@=%@,%@",
|
|
|
|
IDField, loginToCheck, baseDN];
|
|
|
|
if (userDN)
|
|
|
|
{
|
|
|
|
NS_DURING
|
|
|
|
didBind = [bindConnection bindWithMethod: @"simple"
|
|
|
|
binddn: userDN
|
|
|
|
credentials: passwordToCheck];
|
|
|
|
NS_HANDLER
|
|
|
|
NS_ENDHANDLER
|
|
|
|
}
|
|
|
|
[bindConnection release];
|
2007-05-10 23:18:06 +02:00
|
|
|
}
|
2007-05-10 16:56:47 +02:00
|
|
|
}
|
2007-05-09 21:06:15 +02:00
|
|
|
|
2008-12-19 17:18:03 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock unlock];
|
|
|
|
#endif
|
|
|
|
|
2007-05-09 21:06:15 +02:00
|
|
|
return didBind;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* contact management */
|
|
|
|
- (EOQualifier *) _qualifierForFilter: (NSString *) filter
|
|
|
|
{
|
2009-06-04 16:02:45 +02:00
|
|
|
NSString *mailFormat, *fieldFormat, *escapedFilter;
|
2007-05-09 21:06:15 +02:00
|
|
|
EOQualifier *qualifier;
|
2009-03-16 22:13:01 +01:00
|
|
|
NSMutableString *qs;
|
2008-04-25 17:16:58 +02:00
|
|
|
|
2009-06-04 16:33:39 +02:00
|
|
|
escapedFilter = SafeLDAPCriteria (filter);
|
2009-06-04 16:02:45 +02:00
|
|
|
if ([escapedFilter length] > 0)
|
2007-05-09 21:06:15 +02:00
|
|
|
{
|
2009-06-04 16:02:45 +02:00
|
|
|
fieldFormat = [NSString stringWithFormat: @"(%%@='%@*')", escapedFilter];
|
|
|
|
mailFormat = [[mailFields stringsWithFormat: fieldFormat]
|
|
|
|
componentsJoinedByString: @" OR "];
|
|
|
|
|
|
|
|
qs = [NSMutableString string];
|
|
|
|
if ([escapedFilter isEqualToString: @"."])
|
2009-03-16 22:13:01 +01:00
|
|
|
[qs appendFormat: @"(%@='*')", CNField];
|
2007-05-09 21:06:15 +02:00
|
|
|
else
|
2009-06-04 16:02:45 +02:00
|
|
|
[qs appendFormat: @"(%@='%@*') OR (sn='%@*') OR (displayName='%@*')"
|
|
|
|
@"OR %@ OR (telephoneNumber='*%@*')",
|
|
|
|
CNField, escapedFilter, escapedFilter, escapedFilter, mailFormat,
|
|
|
|
escapedFilter];
|
|
|
|
|
2009-03-16 22:13:01 +01:00
|
|
|
if (_filter && [_filter length])
|
|
|
|
[qs appendFormat: @" AND %@", _filter];
|
|
|
|
|
2007-05-09 21:06:15 +02:00
|
|
|
qualifier = [EOQualifier qualifierWithQualifierFormat: qs];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
qualifier = nil;
|
|
|
|
|
|
|
|
return qualifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (EOQualifier *) _qualifierForUIDFilter: (NSString *) uid
|
|
|
|
{
|
2009-06-04 16:02:45 +02:00
|
|
|
NSString *mailFormat, *fieldFormat, *escapedUid;
|
2009-03-16 22:13:01 +01:00
|
|
|
NSMutableString *qs;
|
2007-05-09 21:06:15 +02:00
|
|
|
|
2009-06-04 16:33:39 +02:00
|
|
|
escapedUid = SafeLDAPCriteria (uid);
|
2009-06-04 16:02:45 +02:00
|
|
|
|
|
|
|
fieldFormat = [NSString stringWithFormat: @"(%%@='%@')", escapedUid];
|
2008-04-25 17:16:58 +02:00
|
|
|
mailFormat = [[mailFields stringsWithFormat: fieldFormat]
|
|
|
|
componentsJoinedByString: @" OR "];
|
2009-03-16 22:13:01 +01:00
|
|
|
qs = [NSMutableString string];
|
|
|
|
|
2009-06-04 16:02:45 +02:00
|
|
|
[qs appendFormat: (@"(%@='%@') OR %@"), UIDField, escapedUid, mailFormat];
|
|
|
|
|
2009-03-16 22:13:01 +01:00
|
|
|
if (_filter && [_filter length])
|
|
|
|
[qs appendFormat: @" AND %@", _filter];
|
2007-05-09 21:06:15 +02:00
|
|
|
|
|
|
|
return [EOQualifier qualifierWithQualifierFormat: qs];
|
|
|
|
}
|
|
|
|
|
2009-07-06 23:25:38 +02:00
|
|
|
- (NSArray *) _constraintsFields
|
2007-11-26 21:41:59 +01:00
|
|
|
{
|
|
|
|
NSMutableArray *fields;
|
|
|
|
NSEnumerator *values;
|
|
|
|
NSDictionary *currentConstraint;
|
|
|
|
|
|
|
|
fields = [NSMutableArray array];
|
|
|
|
values = [[modulesConstraints allValues] objectEnumerator];
|
|
|
|
while ((currentConstraint = [values nextObject]))
|
|
|
|
[fields addObjectsFromArray: [currentConstraint allKeys]];
|
|
|
|
|
|
|
|
return fields;
|
|
|
|
}
|
|
|
|
|
2007-05-09 21:06:15 +02:00
|
|
|
- (NSArray *) _searchAttributes
|
|
|
|
{
|
|
|
|
if (!searchAttributes)
|
|
|
|
{
|
2009-04-13 17:01:21 +02:00
|
|
|
NSUserDefaults *ud;
|
2009-05-12 21:56:39 +02:00
|
|
|
NSString *attribute;
|
2009-04-13 17:01:21 +02:00
|
|
|
|
2008-10-04 01:12:11 +02:00
|
|
|
ud = [NSUserDefaults standardUserDefaults];
|
2007-05-09 21:06:15 +02:00
|
|
|
searchAttributes = [NSMutableArray new];
|
2009-07-06 23:25:38 +02:00
|
|
|
[searchAttributes addObject: @"objectClass"];
|
2007-05-09 21:06:15 +02:00
|
|
|
if (CNField)
|
|
|
|
[searchAttributes addObject: CNField];
|
|
|
|
if (UIDField)
|
|
|
|
[searchAttributes addObject: UIDField];
|
2007-11-18 11:16:25 +01:00
|
|
|
[searchAttributes addObjectsFromArray: mailFields];
|
2009-07-06 23:25:38 +02:00
|
|
|
[searchAttributes addObjectsFromArray: [self _constraintsFields]];
|
2007-05-09 21:06:15 +02:00
|
|
|
[searchAttributes addObjectsFromArray: commonSearchFields];
|
2008-10-04 01:12:11 +02:00
|
|
|
|
|
|
|
// Add SOGoLDAPContactInfoAttribute from user defaults
|
2009-05-12 21:56:39 +02:00
|
|
|
attribute = [ud stringForKey: @"SOGoLDAPContactInfoAttribute"];
|
|
|
|
if ([attribute length] > 0 &&
|
|
|
|
![searchAttributes containsObject: attribute])
|
|
|
|
[searchAttributes addObject: attribute];
|
|
|
|
|
|
|
|
// Add IMAP hostname from user defaults
|
|
|
|
if (IMAPHostField && [IMAPHostField length] > 0 &&
|
|
|
|
![searchAttributes containsObject: IMAPHostField])
|
|
|
|
[searchAttributes addObject: IMAPHostField];
|
2007-05-09 21:06:15 +02:00
|
|
|
}
|
2009-05-12 21:56:39 +02:00
|
|
|
|
2007-05-09 21:06:15 +02:00
|
|
|
return searchAttributes;
|
|
|
|
}
|
|
|
|
|
2009-04-13 17:01:21 +02:00
|
|
|
- (NSArray *) allEntryIDs
|
2007-05-09 21:06:15 +02:00
|
|
|
{
|
|
|
|
NSMutableArray *ids;
|
|
|
|
NSEnumerator *entries;
|
|
|
|
NGLdapEntry *currentEntry;
|
|
|
|
NSString *value;
|
|
|
|
|
2008-12-19 17:18:03 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock lock];
|
|
|
|
#endif
|
|
|
|
|
2007-05-09 21:06:15 +02:00
|
|
|
ids = [NSMutableArray array];
|
|
|
|
|
2008-09-08 16:54:33 +02:00
|
|
|
if ([self _initLDAPConnection])
|
2009-04-06 14:54:03 +02:00
|
|
|
{
|
|
|
|
NSArray *attributes;
|
|
|
|
|
2009-04-13 17:01:21 +02:00
|
|
|
attributes = [NSArray arrayWithObject: IDField];
|
2009-04-06 14:54:03 +02:00
|
|
|
if ([_scope caseInsensitiveCompare: @"BASE"] == NSOrderedSame)
|
|
|
|
entries = [ldapConnection baseSearchAtBaseDN: baseDN
|
|
|
|
qualifier: nil
|
|
|
|
attributes: attributes];
|
|
|
|
else if ([_scope caseInsensitiveCompare: @"ONE"] == NSOrderedSame)
|
|
|
|
entries = [ldapConnection flatSearchAtBaseDN: baseDN
|
|
|
|
qualifier: nil
|
|
|
|
attributes: attributes];
|
2009-04-13 17:01:21 +02:00
|
|
|
else
|
2009-04-06 14:54:03 +02:00
|
|
|
entries = [ldapConnection deepSearchAtBaseDN: baseDN
|
|
|
|
qualifier: nil
|
|
|
|
attributes: attributes];
|
|
|
|
}
|
2008-09-08 16:54:33 +02:00
|
|
|
else
|
|
|
|
entries = nil;
|
|
|
|
|
2007-05-09 21:06:15 +02:00
|
|
|
if (entries)
|
|
|
|
{
|
|
|
|
currentEntry = [entries nextObject];
|
|
|
|
while (currentEntry)
|
|
|
|
{
|
2009-04-13 17:01:21 +02:00
|
|
|
value = [[currentEntry attributeWithName: IDField]
|
2007-05-09 21:06:15 +02:00
|
|
|
stringValueAtIndex: 0];
|
|
|
|
if ([value length] > 0)
|
|
|
|
[ids addObject: value];
|
|
|
|
currentEntry = [entries nextObject];
|
|
|
|
}
|
|
|
|
}
|
2008-09-08 16:54:33 +02:00
|
|
|
|
|
|
|
[ldapConnection autorelease];
|
2007-05-09 21:06:15 +02:00
|
|
|
|
2008-12-19 17:18:03 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock unlock];
|
|
|
|
#endif
|
|
|
|
|
2007-05-09 21:06:15 +02:00
|
|
|
return ids;
|
|
|
|
}
|
|
|
|
|
2007-11-18 11:16:25 +01:00
|
|
|
- (void) _fillEmailsOfEntry: (NGLdapEntry *) ldapEntry
|
|
|
|
intoContactEntry: (NSMutableDictionary *) contactEntry
|
|
|
|
{
|
|
|
|
NSEnumerator *emailFields;
|
2009-05-12 21:56:39 +02:00
|
|
|
NSString *currentFieldName, *ldapValue;
|
2007-11-18 11:16:25 +01:00
|
|
|
NSMutableArray *emails;
|
2008-04-24 21:56:55 +02:00
|
|
|
NSArray *allValues;
|
2007-11-18 11:16:25 +01:00
|
|
|
|
|
|
|
emails = [NSMutableArray new];
|
|
|
|
emailFields = [mailFields objectEnumerator];
|
|
|
|
while ((currentFieldName = [emailFields nextObject]))
|
|
|
|
{
|
2008-04-24 21:56:55 +02:00
|
|
|
allValues = [[ldapEntry attributeWithName: currentFieldName]
|
2008-04-24 22:28:02 +02:00
|
|
|
allStringValues];
|
2008-04-24 21:56:55 +02:00
|
|
|
[emails addObjectsFromArray: allValues];
|
2007-11-18 11:16:25 +01:00
|
|
|
}
|
|
|
|
[contactEntry setObject: emails forKey: @"c_emails"];
|
2008-04-24 21:56:55 +02:00
|
|
|
[emails release];
|
2009-05-12 21:56:39 +02:00
|
|
|
|
|
|
|
if (IMAPHostField)
|
|
|
|
{
|
|
|
|
ldapValue = [[ldapEntry attributeWithName: IMAPHostField] stringValueAtIndex: 0];
|
|
|
|
if ([ldapValue length] > 0)
|
|
|
|
[contactEntry setObject: ldapValue forKey: @"c_imaphostname"];
|
|
|
|
}
|
2007-11-18 11:16:25 +01:00
|
|
|
}
|
|
|
|
|
2007-11-26 21:41:59 +01:00
|
|
|
- (void) _fillConstraints: (NGLdapEntry *) ldapEntry
|
|
|
|
forModule: (NSString *) module
|
|
|
|
intoContactEntry: (NSMutableDictionary *) contactEntry
|
|
|
|
{
|
|
|
|
NSDictionary *constraints;
|
|
|
|
NSEnumerator *matches;
|
|
|
|
NSString *currentMatch, *currentValue, *ldapValue;
|
|
|
|
BOOL result;
|
|
|
|
|
|
|
|
result = YES;
|
|
|
|
|
|
|
|
constraints = [modulesConstraints objectForKey: module];
|
|
|
|
if (constraints)
|
|
|
|
{
|
|
|
|
matches = [[constraints allKeys] objectEnumerator];
|
|
|
|
currentMatch = [matches nextObject];
|
|
|
|
while (result && currentMatch)
|
|
|
|
{
|
|
|
|
ldapValue = [[ldapEntry attributeWithName: currentMatch]
|
|
|
|
stringValueAtIndex: 0];
|
|
|
|
currentValue = [constraints objectForKey: currentMatch];
|
2008-08-21 21:19:11 +02:00
|
|
|
if ([ldapValue caseInsensitiveMatches: currentValue])
|
2007-11-26 21:41:59 +01:00
|
|
|
currentMatch = [matches nextObject];
|
|
|
|
else
|
|
|
|
result = NO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[contactEntry setObject: [NSNumber numberWithBool: result]
|
|
|
|
forKey: [NSString stringWithFormat: @"%@Access", module]];
|
|
|
|
}
|
|
|
|
|
2007-05-09 21:06:15 +02:00
|
|
|
- (NSDictionary *) _convertLDAPEntryToContact: (NGLdapEntry *) ldapEntry
|
|
|
|
{
|
|
|
|
NSMutableDictionary *contactEntry;
|
|
|
|
NSEnumerator *attributes;
|
|
|
|
NSString *currentAttribute, *value;
|
|
|
|
|
|
|
|
contactEntry = [NSMutableDictionary dictionary];
|
|
|
|
attributes = [[self _searchAttributes] objectEnumerator];
|
2008-04-25 17:16:58 +02:00
|
|
|
while ((currentAttribute = [attributes nextObject]))
|
2007-05-09 21:06:15 +02:00
|
|
|
{
|
|
|
|
value = [[ldapEntry attributeWithName: currentAttribute]
|
|
|
|
stringValueAtIndex: 0];
|
|
|
|
if (value)
|
|
|
|
[contactEntry setObject: value forKey: currentAttribute];
|
|
|
|
}
|
|
|
|
value = [[ldapEntry attributeWithName: IDField] stringValueAtIndex: 0];
|
|
|
|
if (!value)
|
|
|
|
value = @"";
|
|
|
|
[contactEntry setObject: value forKey: @"c_name"];
|
|
|
|
value = [[ldapEntry attributeWithName: UIDField] stringValueAtIndex: 0];
|
|
|
|
if (!value)
|
|
|
|
value = @"";
|
2009-05-03 19:50:57 +02:00
|
|
|
// else
|
|
|
|
// {
|
|
|
|
// Eventually, we could check at this point if the entry is a group
|
|
|
|
// and prefix the UID with a "@"
|
|
|
|
// }
|
2007-05-09 21:06:15 +02:00
|
|
|
[contactEntry setObject: value forKey: @"c_uid"];
|
|
|
|
value = [[ldapEntry attributeWithName: CNField] stringValueAtIndex: 0];
|
|
|
|
if (!value)
|
|
|
|
value = @"";
|
|
|
|
[contactEntry setObject: value forKey: @"c_cn"];
|
2007-11-18 11:16:25 +01:00
|
|
|
[self _fillEmailsOfEntry: ldapEntry intoContactEntry: contactEntry];
|
2007-11-26 21:41:59 +01:00
|
|
|
[self _fillConstraints: ldapEntry forModule: @"Calendar"
|
|
|
|
intoContactEntry: (NSMutableDictionary *) contactEntry];
|
|
|
|
[self _fillConstraints: ldapEntry forModule: @"Mail"
|
|
|
|
intoContactEntry: (NSMutableDictionary *) contactEntry];
|
2007-05-09 21:06:15 +02:00
|
|
|
|
|
|
|
return contactEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *) fetchContactsMatching: (NSString *) match
|
|
|
|
{
|
|
|
|
NSMutableArray *contacts;
|
|
|
|
NGLdapEntry *currentEntry;
|
|
|
|
NSEnumerator *entries;
|
|
|
|
|
2008-12-19 17:18:03 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock lock];
|
|
|
|
#endif
|
|
|
|
|
2007-05-09 21:06:15 +02:00
|
|
|
contacts = [NSMutableArray array];
|
|
|
|
|
2007-05-10 16:56:47 +02:00
|
|
|
if ([match length] > 0)
|
2007-05-09 21:06:15 +02:00
|
|
|
{
|
2008-09-08 16:54:33 +02:00
|
|
|
if ([self _initLDAPConnection])
|
2009-04-06 14:54:03 +02:00
|
|
|
{
|
|
|
|
EOQualifier *qualifier;
|
|
|
|
NSArray *attributes;
|
|
|
|
|
|
|
|
qualifier = [self _qualifierForFilter: match];
|
|
|
|
attributes = [self _searchAttributes];
|
|
|
|
|
|
|
|
if ([_scope caseInsensitiveCompare: @"BASE"] == NSOrderedSame)
|
|
|
|
entries = [ldapConnection baseSearchAtBaseDN: baseDN
|
|
|
|
qualifier: qualifier
|
|
|
|
attributes: attributes];
|
|
|
|
else if ([_scope caseInsensitiveCompare: @"ONE"] == NSOrderedSame)
|
|
|
|
entries = [ldapConnection flatSearchAtBaseDN: baseDN
|
|
|
|
qualifier: qualifier
|
|
|
|
attributes: attributes];
|
2009-05-03 19:50:57 +02:00
|
|
|
else /* we do it like before */
|
2009-04-06 14:54:03 +02:00
|
|
|
entries = [ldapConnection deepSearchAtBaseDN: baseDN
|
|
|
|
qualifier: qualifier
|
|
|
|
attributes: attributes];
|
|
|
|
}
|
2008-09-08 16:54:33 +02:00
|
|
|
else
|
|
|
|
entries = nil;
|
|
|
|
|
2007-05-10 16:56:47 +02:00
|
|
|
if (entries)
|
2008-09-19 16:03:56 +02:00
|
|
|
while ((currentEntry = [entries nextObject]))
|
|
|
|
[contacts addObject:
|
|
|
|
[self _convertLDAPEntryToContact: currentEntry]];
|
2008-09-08 16:54:33 +02:00
|
|
|
|
2007-05-10 23:18:06 +02:00
|
|
|
[ldapConnection release];
|
2007-05-09 21:06:15 +02:00
|
|
|
}
|
|
|
|
|
2008-12-19 17:18:03 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock unlock];
|
|
|
|
#endif
|
|
|
|
|
2007-05-09 21:06:15 +02:00
|
|
|
return contacts;
|
|
|
|
}
|
|
|
|
|
2009-04-13 17:01:21 +02:00
|
|
|
- (NSDictionary *) lookupContactEntry: (NSString *) theID
|
2007-05-09 21:06:15 +02:00
|
|
|
{
|
|
|
|
NSDictionary *contactEntry;
|
|
|
|
NGLdapEntry *ldapEntry;
|
|
|
|
|
2008-12-19 17:18:03 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock lock];
|
|
|
|
#endif
|
|
|
|
|
2007-05-10 16:56:47 +02:00
|
|
|
contactEntry = nil;
|
|
|
|
|
2009-04-13 17:01:21 +02:00
|
|
|
if ([theID length] > 0)
|
2007-05-10 16:56:47 +02:00
|
|
|
{
|
2008-09-08 16:54:33 +02:00
|
|
|
if ([self _initLDAPConnection])
|
2009-04-13 17:01:21 +02:00
|
|
|
{
|
|
|
|
NSEnumerator *entries;
|
|
|
|
EOQualifier *qualifier;
|
|
|
|
NSArray *attributes;
|
|
|
|
NSString *s;
|
2009-06-04 16:02:45 +02:00
|
|
|
|
2009-06-04 16:33:39 +02:00
|
|
|
s = [NSString stringWithFormat: @"(%@='%@')",
|
|
|
|
IDField, SafeLDAPCriteria (theID)];
|
2009-04-13 17:01:21 +02:00
|
|
|
qualifier = [EOQualifier qualifierWithQualifierFormat: s];
|
|
|
|
attributes = [self _searchAttributes];
|
|
|
|
|
|
|
|
if ([_scope caseInsensitiveCompare: @"BASE"] == NSOrderedSame)
|
|
|
|
entries = [ldapConnection baseSearchAtBaseDN: baseDN
|
|
|
|
qualifier: qualifier
|
|
|
|
attributes: attributes];
|
|
|
|
else if ([_scope caseInsensitiveCompare: @"ONE"] == NSOrderedSame)
|
|
|
|
entries = [ldapConnection flatSearchAtBaseDN: baseDN
|
|
|
|
qualifier: qualifier
|
|
|
|
attributes: attributes];
|
|
|
|
else
|
|
|
|
entries = [ldapConnection deepSearchAtBaseDN: baseDN
|
|
|
|
qualifier: qualifier
|
|
|
|
attributes: attributes];
|
2009-06-04 16:02:45 +02:00
|
|
|
|
2009-04-13 17:01:21 +02:00
|
|
|
ldapEntry = [entries nextObject];
|
|
|
|
}
|
2008-09-08 16:54:33 +02:00
|
|
|
else
|
|
|
|
ldapEntry = nil;
|
|
|
|
|
2007-05-10 16:56:47 +02:00
|
|
|
if (ldapEntry)
|
|
|
|
contactEntry = [self _convertLDAPEntryToContact: ldapEntry];
|
2008-09-08 16:54:33 +02:00
|
|
|
|
|
|
|
[ldapConnection autorelease];
|
2007-05-10 16:56:47 +02:00
|
|
|
}
|
2007-05-09 21:06:15 +02:00
|
|
|
|
2008-12-19 17:18:03 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock unlock];
|
|
|
|
#endif
|
|
|
|
|
2007-05-09 21:06:15 +02:00
|
|
|
return contactEntry;
|
|
|
|
}
|
|
|
|
|
2009-04-13 17:01:21 +02:00
|
|
|
- (NSDictionary *) lookupContactEntryWithUIDorEmail: (NSString *) uid
|
2007-05-09 21:06:15 +02:00
|
|
|
{
|
|
|
|
NSDictionary *contactEntry;
|
|
|
|
NGLdapEntry *ldapEntry;
|
|
|
|
|
2008-12-19 17:18:03 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock lock];
|
|
|
|
#endif
|
|
|
|
|
2007-05-10 16:56:47 +02:00
|
|
|
contactEntry = nil;
|
|
|
|
|
|
|
|
if ([uid length] > 0)
|
|
|
|
{
|
2008-09-08 16:54:33 +02:00
|
|
|
if ([self _initLDAPConnection])
|
2009-04-13 17:01:21 +02:00
|
|
|
{
|
|
|
|
NSEnumerator *entries;
|
2009-04-06 14:54:03 +02:00
|
|
|
EOQualifier *qualifier;
|
|
|
|
NSArray *attributes;
|
|
|
|
|
2008-09-08 16:54:33 +02:00
|
|
|
qualifier = [self _qualifierForUIDFilter: uid];
|
2009-04-06 14:54:03 +02:00
|
|
|
attributes = [self _searchAttributes];
|
|
|
|
|
|
|
|
if ([_scope caseInsensitiveCompare: @"BASE"] == NSOrderedSame)
|
|
|
|
entries = [ldapConnection baseSearchAtBaseDN: baseDN
|
|
|
|
qualifier: qualifier
|
|
|
|
attributes: attributes];
|
|
|
|
else if ([_scope caseInsensitiveCompare: @"ONE"] == NSOrderedSame)
|
|
|
|
entries = [ldapConnection flatSearchAtBaseDN: baseDN
|
|
|
|
qualifier: qualifier
|
|
|
|
attributes: attributes];
|
2009-04-13 17:01:21 +02:00
|
|
|
else
|
2009-04-06 14:54:03 +02:00
|
|
|
entries = [ldapConnection deepSearchAtBaseDN: baseDN
|
|
|
|
qualifier: qualifier
|
|
|
|
attributes: attributes];
|
|
|
|
|
2008-09-08 16:54:33 +02:00
|
|
|
ldapEntry = [entries nextObject];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ldapEntry = nil;
|
|
|
|
|
2007-05-10 16:56:47 +02:00
|
|
|
if (ldapEntry)
|
|
|
|
contactEntry = [self _convertLDAPEntryToContact: ldapEntry];
|
2008-09-08 16:54:33 +02:00
|
|
|
|
2007-05-10 23:18:06 +02:00
|
|
|
[ldapConnection release];
|
2007-05-10 16:56:47 +02:00
|
|
|
}
|
2007-05-09 21:06:15 +02:00
|
|
|
|
2008-12-19 17:18:03 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock unlock];
|
|
|
|
#endif
|
|
|
|
|
2007-05-09 21:06:15 +02:00
|
|
|
return contactEntry;
|
|
|
|
}
|
|
|
|
|
2009-05-03 19:50:57 +02:00
|
|
|
- (NSString *) lookupLoginByDN: (NSString *) theDN
|
|
|
|
{
|
|
|
|
NGLdapEntry *entry;
|
|
|
|
NSString *login;
|
|
|
|
|
|
|
|
login = nil;
|
|
|
|
if ([self _initLDAPConnection])
|
|
|
|
{
|
|
|
|
entry = [ldapConnection entryAtDN: theDN
|
|
|
|
attributes: [NSArray arrayWithObject: UIDField]];
|
|
|
|
if (entry)
|
|
|
|
login = [[entry attributeWithName: UIDField] stringValueAtIndex: 0];
|
|
|
|
[ldapConnection autorelease];
|
|
|
|
}
|
|
|
|
|
|
|
|
return login;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NGLdapEntry *) lookupGroupEntryByUID: (NSString *) theUID
|
|
|
|
{
|
|
|
|
return [self lookupGroupEntryByAttribute: UIDField
|
|
|
|
andValue: theUID];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NGLdapEntry *) lookupGroupEntryByEmail: (NSString *) theEmail
|
|
|
|
{
|
|
|
|
return [self lookupGroupEntryByAttribute: @"mail"
|
|
|
|
andValue: theEmail];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NGLdapEntry *) lookupGroupEntryByAttribute: (NSString *) theAttribute
|
|
|
|
andValue: (NSString *) theValue
|
2009-04-30 23:17:55 +02:00
|
|
|
{
|
|
|
|
NGLdapEntry *ldapEntry;
|
|
|
|
|
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock lock];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ldapEntry = nil;
|
|
|
|
|
2009-05-03 19:50:57 +02:00
|
|
|
if ([theValue length] > 0)
|
2009-04-30 23:17:55 +02:00
|
|
|
{
|
|
|
|
if ([self _initLDAPConnection])
|
|
|
|
{
|
|
|
|
NSMutableArray *attributes;
|
|
|
|
NSEnumerator *entries;
|
|
|
|
EOQualifier *qualifier;
|
|
|
|
NSString *s;
|
|
|
|
|
|
|
|
// FIXME
|
2009-07-02 19:12:06 +02:00
|
|
|
|
|
|
|
// we should support MailFieldNames?
|
2009-06-04 16:33:39 +02:00
|
|
|
s = [NSString stringWithFormat: @"(%@='%@')",
|
|
|
|
theAttribute, SafeLDAPCriteria (theValue)];
|
2009-04-30 23:17:55 +02:00
|
|
|
qualifier = [EOQualifier qualifierWithQualifierFormat: s];
|
|
|
|
|
|
|
|
// We look for additional attributes - the ones related to group membership
|
|
|
|
attributes = [NSMutableArray arrayWithArray: [self _searchAttributes]];
|
|
|
|
[attributes addObject: @"member"];
|
2009-05-03 19:50:57 +02:00
|
|
|
[attributes addObject: @"uniqueMember"];
|
|
|
|
[attributes addObject: @"memberUid"];
|
2009-04-30 23:17:55 +02:00
|
|
|
[attributes addObject: @"memberOf"];
|
|
|
|
|
|
|
|
if ([_scope caseInsensitiveCompare: @"BASE"] == NSOrderedSame)
|
|
|
|
entries = [ldapConnection baseSearchAtBaseDN: baseDN
|
|
|
|
qualifier: qualifier
|
|
|
|
attributes: attributes];
|
|
|
|
else if ([_scope caseInsensitiveCompare: @"ONE"] == NSOrderedSame)
|
|
|
|
entries = [ldapConnection flatSearchAtBaseDN: baseDN
|
|
|
|
qualifier: qualifier
|
|
|
|
attributes: attributes];
|
|
|
|
else
|
|
|
|
entries = [ldapConnection deepSearchAtBaseDN: baseDN
|
|
|
|
qualifier: qualifier
|
|
|
|
attributes: attributes];
|
|
|
|
|
|
|
|
ldapEntry = [entries nextObject];
|
|
|
|
}
|
|
|
|
|
|
|
|
[ldapConnection autorelease];
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock unlock];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ldapEntry;
|
|
|
|
}
|
|
|
|
|
2007-10-10 20:44:26 +02:00
|
|
|
- (NSString *) sourceID
|
|
|
|
{
|
|
|
|
return sourceID;
|
|
|
|
}
|
|
|
|
|
2009-05-03 19:50:57 +02:00
|
|
|
- (NSString *) baseDN
|
|
|
|
{
|
|
|
|
return baseDN;
|
|
|
|
}
|
|
|
|
|
2007-05-09 21:06:15 +02:00
|
|
|
@end
|