2007-05-09 21:06:15 +02:00
|
|
|
/* LDAPSource.m - this file is part of SOGo
|
|
|
|
*
|
2012-01-12 21:01:34 +01:00
|
|
|
* Copyright (C) 2007-2012 Inverse inc.
|
2007-05-09 21:06:15 +02:00
|
|
|
*
|
|
|
|
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
|
2010-12-14 19:08:36 +01:00
|
|
|
* Ludovic Marcotte <lmarcotte@inverse.ca>
|
2011-04-14 20:41:10 +02:00
|
|
|
* Francis Lachapelle <flachapelle@inverse.ca>
|
2007-05-09 21:06:15 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2010-04-20 23:30:17 +02:00
|
|
|
#include <ldap.h>
|
|
|
|
|
2007-05-09 21:06:15 +02:00
|
|
|
#import <Foundation/NSArray.h>
|
|
|
|
#import <Foundation/NSDictionary.h>
|
2012-01-04 16:26:30 +01:00
|
|
|
#import <Foundation/NSException.h>
|
2007-05-09 21:06:15 +02:00
|
|
|
#import <Foundation/NSString.h>
|
|
|
|
|
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>
|
2010-03-08 16:18:05 +01:00
|
|
|
#import <NGLdap/NGLdapModification.h>
|
2012-02-06 14:55:46 +01:00
|
|
|
#import <NGLdap/NSString+DN.h>
|
2007-05-09 21:06:15 +02:00
|
|
|
|
2011-12-30 21:39:07 +01:00
|
|
|
#import "LDAPSourceSchema.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"
|
2009-11-29 05:19:32 +01:00
|
|
|
#import "SOGoDomainDefaults.h"
|
|
|
|
#import "SOGoSystemDefaults.h"
|
2008-08-21 21:19:11 +02:00
|
|
|
|
|
|
|
#import "LDAPSource.h"
|
2010-12-14 19:08:36 +01:00
|
|
|
#import "../../Main/SOGo.h"
|
|
|
|
|
2011-12-30 21:39:07 +01:00
|
|
|
static Class NSStringK;
|
|
|
|
|
2010-09-24 19:50:40 +02:00
|
|
|
#define SafeLDAPCriteria(x) [[[x stringByReplacingString: @"\\" withString: @"\\\\"] \
|
|
|
|
stringByReplacingString: @"'" withString: @"\\'"] \
|
|
|
|
stringByReplacingString: @"%" withString: @"%%"]
|
2008-12-19 17:18:03 +01:00
|
|
|
|
2011-12-30 21:39:07 +01:00
|
|
|
@interface NGLdapAttribute (SOGoLDAP)
|
2007-05-09 21:06:15 +02:00
|
|
|
|
2011-12-30 21:39:07 +01:00
|
|
|
- (id) _asArrayOrString;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NGLdapAttribute (SOGoLDAP)
|
|
|
|
|
|
|
|
- (id) _asArrayOrString
|
|
|
|
{
|
|
|
|
id value;
|
|
|
|
NSArray *arrayValue;
|
|
|
|
|
|
|
|
arrayValue = [self allStringValues];
|
|
|
|
if ([arrayValue count] == 1)
|
|
|
|
value = [arrayValue objectAtIndex: 0];
|
|
|
|
else
|
|
|
|
value = arrayValue;
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface NGLdapEntry (SOGoLDAP)
|
|
|
|
|
|
|
|
- (NSMutableDictionary *) _asDictionary;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NGLdapEntry (SOGoLDAP)
|
|
|
|
|
|
|
|
- (NSMutableDictionary *) _asDictionary
|
2007-05-09 21:06:15 +02:00
|
|
|
{
|
2011-12-30 21:39:07 +01:00
|
|
|
NSMutableDictionary *ldapRecord;
|
|
|
|
NSDictionary *ldapAttributes;
|
|
|
|
NSArray *keys;
|
|
|
|
NSString *key;
|
|
|
|
NSUInteger count, max;
|
|
|
|
id value;
|
|
|
|
|
|
|
|
ldapAttributes = [self attributes];
|
|
|
|
keys = [ldapAttributes allKeys];
|
|
|
|
max = [keys count];
|
|
|
|
|
|
|
|
ldapRecord = [NSMutableDictionary dictionaryWithCapacity: max];
|
|
|
|
for (count = 0; count < max; count++)
|
2007-05-09 21:06:15 +02:00
|
|
|
{
|
2011-12-30 21:39:07 +01:00
|
|
|
key = [keys objectAtIndex: count];
|
|
|
|
value = [[ldapAttributes objectForKey: key] _asArrayOrString];
|
|
|
|
if (value)
|
|
|
|
[ldapRecord setObject: value forKey: [key lowercaseString]];
|
2007-05-09 21:06:15 +02:00
|
|
|
}
|
2011-12-30 21:39:07 +01:00
|
|
|
|
|
|
|
return ldapRecord;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation LDAPSource
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
{
|
|
|
|
NSStringK = [NSString class];
|
2007-05-09 21:06:15 +02:00
|
|
|
}
|
|
|
|
|
2011-07-05 22:10:45 +02:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2007-05-09 21:06:15 +02:00
|
|
|
+ (id) sourceFromUDSource: (NSDictionary *) udSource
|
2010-04-20 23:30:17 +02:00
|
|
|
inDomain: (NSString *) sourceDomain
|
2007-05-09 21:06:15 +02:00
|
|
|
{
|
|
|
|
id newSource;
|
|
|
|
|
2009-11-29 05:19:32 +01:00
|
|
|
newSource = [[self alloc] initFromUDSource: udSource
|
2010-04-20 23:30:17 +02:00
|
|
|
inDomain: sourceDomain];
|
2007-05-09 21:06:15 +02:00
|
|
|
[newSource autorelease];
|
|
|
|
|
|
|
|
return newSource;
|
|
|
|
}
|
|
|
|
|
2011-07-05 22:10:45 +02:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2007-05-09 21:06:15 +02:00
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
2012-01-04 16:26:30 +01:00
|
|
|
sourceID = nil;
|
|
|
|
displayName = nil;
|
|
|
|
|
2007-05-09 21:06:15 +02:00
|
|
|
bindDN = nil;
|
2011-07-05 22:10:45 +02:00
|
|
|
password = nil;
|
|
|
|
sourceBindDN = nil;
|
|
|
|
sourceBindPassword = nil;
|
2007-05-09 21:06:15 +02:00
|
|
|
hostname = nil;
|
|
|
|
port = 389;
|
2009-04-02 22:51:46 +02:00
|
|
|
encryption = nil;
|
2009-11-29 05:19:32 +01:00
|
|
|
domain = nil;
|
2007-05-09 21:06:15 +02:00
|
|
|
|
|
|
|
baseDN = nil;
|
2011-12-30 21:39:07 +01:00
|
|
|
schema = nil;
|
2007-05-09 21:06:15 +02:00
|
|
|
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];
|
2011-12-30 21:39:07 +01:00
|
|
|
contactMapping = nil;
|
2011-01-24 19:02:12 +01:00
|
|
|
searchFields = [NSArray arrayWithObjects: @"sn", @"displayname", @"telephonenumber", nil];
|
|
|
|
[searchFields retain];
|
2009-05-12 21:56:39 +02:00
|
|
|
IMAPHostField = nil;
|
2011-04-14 20:41:10 +02:00
|
|
|
IMAPLoginField = 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;
|
2012-01-26 17:22:28 +01:00
|
|
|
_userPasswordAlgorithm = nil;
|
2012-01-04 16:26:30 +01:00
|
|
|
listRequiresDot = YES;
|
2007-05-09 21:06:15 +02:00
|
|
|
|
|
|
|
searchAttributes = nil;
|
2010-03-08 16:18:05 +01:00
|
|
|
passwordPolicy = NO;
|
2010-12-14 19:08:36 +01:00
|
|
|
|
2011-04-25 12:31:08 +02:00
|
|
|
kindField = nil;
|
|
|
|
multipleBookingsField = nil;
|
|
|
|
|
2012-01-12 21:01:34 +01:00
|
|
|
MSExchangeHostname = nil;
|
|
|
|
|
2010-12-14 19:08:36 +01:00
|
|
|
_dnCache = [[NSMutableDictionary alloc] init];
|
2011-12-30 21:39:07 +01:00
|
|
|
modifiers = nil;
|
2007-05-09 21:06:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2011-07-05 22:10:45 +02:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2007-05-09 21:06:15 +02:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
2011-12-30 21:39:07 +01:00
|
|
|
[schema release];
|
2007-05-09 21:06:15 +02:00
|
|
|
[bindDN release];
|
2011-07-05 22:10:45 +02:00
|
|
|
[password release];
|
|
|
|
[sourceBindDN release];
|
|
|
|
[sourceBindPassword release];
|
2007-05-09 21:06:15 +02:00
|
|
|
[hostname release];
|
2009-04-02 22:51:46 +02:00
|
|
|
[encryption release];
|
2007-05-09 21:06:15 +02:00
|
|
|
[baseDN release];
|
|
|
|
[IDField release];
|
|
|
|
[CNField release];
|
|
|
|
[UIDField release];
|
2011-12-30 21:39:07 +01:00
|
|
|
[contactMapping release];
|
2007-11-18 11:16:25 +01:00
|
|
|
[mailFields release];
|
2011-01-24 19:02:12 +01:00
|
|
|
[searchFields release];
|
2009-05-12 21:56:39 +02:00
|
|
|
[IMAPHostField release];
|
2011-04-14 20:41:10 +02:00
|
|
|
[IMAPLoginField release];
|
2007-05-10 23:18:06 +02:00
|
|
|
[bindFields release];
|
2009-03-16 22:13:01 +01:00
|
|
|
[_filter release];
|
2012-01-26 17:22:28 +01:00
|
|
|
[_userPasswordAlgorithm 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];
|
2009-10-13 16:09:04 +02:00
|
|
|
[searchAttributes release];
|
2009-11-29 22:44:04 +01:00
|
|
|
[domain release];
|
2010-12-14 19:08:36 +01:00
|
|
|
[_dnCache release];
|
2011-04-25 12:31:08 +02:00
|
|
|
[kindField release];
|
|
|
|
[multipleBookingsField release];
|
2012-01-12 21:01:34 +01:00
|
|
|
[MSExchangeHostname release];
|
2011-12-30 21:39:07 +01:00
|
|
|
[modifiers release];
|
2007-05-09 21:06:15 +02:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2011-07-05 22:10:45 +02:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2007-05-09 21:06:15 +02:00
|
|
|
- (id) initFromUDSource: (NSDictionary *) udSource
|
2009-11-29 05:19:32 +01:00
|
|
|
inDomain: (NSString *) sourceDomain
|
2007-05-09 21:06:15 +02:00
|
|
|
{
|
2009-11-29 05:19:32 +01:00
|
|
|
SOGoDomainDefaults *dd;
|
2012-01-04 16:26:30 +01:00
|
|
|
NSNumber *udQueryLimit, *udQueryTimeout, *dotValue;
|
2009-11-29 05:19:32 +01:00
|
|
|
|
|
|
|
if ((self = [self init]))
|
|
|
|
{
|
2012-01-04 16:26:30 +01:00
|
|
|
[self setSourceID: [udSource objectForKey: @"id"]];
|
|
|
|
[self setDisplayName: [udSource objectForKey: @"displayName"]];
|
2009-11-29 05:19:32 +01:00
|
|
|
|
|
|
|
[self setBindDN: [udSource objectForKey: @"bindDN"]
|
|
|
|
password: [udSource objectForKey: @"bindPassword"]
|
|
|
|
hostname: [udSource objectForKey: @"hostname"]
|
|
|
|
port: [udSource objectForKey: @"port"]
|
2010-12-14 19:08:36 +01:00
|
|
|
encryption: [udSource objectForKey: @"encryption"]
|
|
|
|
bindAsCurrentUser: [udSource objectForKey: @"bindAsCurrentUser"]];
|
|
|
|
|
2009-11-29 05:19:32 +01:00
|
|
|
[self setBaseDN: [udSource objectForKey: @"baseDN"]
|
|
|
|
IDField: [udSource objectForKey: @"IDFieldName"]
|
|
|
|
CNField: [udSource objectForKey: @"CNFieldName"]
|
|
|
|
UIDField: [udSource objectForKey: @"UIDFieldName"]
|
|
|
|
mailFields: [udSource objectForKey: @"MailFieldNames"]
|
2011-01-24 19:02:12 +01:00
|
|
|
searchFields: [udSource objectForKey: @"SearchFieldNames"]
|
2009-05-12 21:56:39 +02:00
|
|
|
IMAPHostField: [udSource objectForKey: @"IMAPHostFieldName"]
|
2011-04-14 20:41:10 +02:00
|
|
|
IMAPLoginField: [udSource objectForKey: @"IMAPLoginFieldName"]
|
2011-04-25 12:31:08 +02:00
|
|
|
bindFields: [udSource objectForKey: @"bindFields"]
|
|
|
|
kindField: [udSource objectForKey: @"KindFieldName"]
|
2012-01-12 21:01:34 +01:00
|
|
|
andMultipleBookingsField: [udSource objectForKey: @"MultipleBookingsFieldName"]];
|
2009-11-29 05:19:32 +01:00
|
|
|
|
2012-01-04 16:26:30 +01:00
|
|
|
dotValue = [udSource objectForKey: @"listRequiresDot"];
|
|
|
|
if (dotValue)
|
|
|
|
[self setListRequiresDot: [dotValue boolValue]];
|
2011-12-30 21:39:07 +01:00
|
|
|
[self setContactMapping: [udSource objectForKey: @"mapping"]
|
|
|
|
andObjectClasses: [udSource objectForKey: @"objectClasses"]];
|
|
|
|
|
2012-01-04 16:26:30 +01:00
|
|
|
[self setModifiers: [udSource objectForKey: @"modifiers"]];
|
|
|
|
ASSIGN (abOU, [udSource objectForKey: @"abOU"]);
|
|
|
|
|
2009-11-29 05:19:32 +01:00
|
|
|
if ([sourceDomain length])
|
|
|
|
{
|
2009-11-29 22:44:04 +01:00
|
|
|
dd = [SOGoDomainDefaults defaultsForDomain: sourceDomain];
|
|
|
|
ASSIGN (domain, sourceDomain);
|
2009-11-29 05:19:32 +01:00
|
|
|
}
|
|
|
|
else
|
2009-11-29 22:44:04 +01:00
|
|
|
dd = [SOGoSystemDefaults sharedSystemDefaults];
|
2009-11-29 05:19:32 +01:00
|
|
|
|
|
|
|
contactInfoAttribute
|
|
|
|
= [udSource objectForKey: @"SOGoLDAPContactInfoAttribute"];
|
|
|
|
if (!contactInfoAttribute)
|
|
|
|
contactInfoAttribute = [dd ldapContactInfoAttribute];
|
|
|
|
[contactInfoAttribute retain];
|
|
|
|
|
|
|
|
udQueryLimit = [udSource objectForKey: @"SOGoLDAPQueryLimit"];
|
|
|
|
if (udQueryLimit)
|
|
|
|
queryLimit = [udQueryLimit intValue];
|
|
|
|
else
|
|
|
|
queryLimit = [dd ldapQueryLimit];
|
|
|
|
|
|
|
|
udQueryTimeout = [udSource objectForKey: @"SOGoLDAPQueryTimeout"];
|
|
|
|
if (udQueryTimeout)
|
|
|
|
queryTimeout = [udQueryTimeout intValue];
|
|
|
|
else
|
|
|
|
queryTimeout = [dd ldapQueryTimeout];
|
|
|
|
|
2011-07-05 22:10:45 +02:00
|
|
|
ASSIGN(modulesConstraints,
|
|
|
|
[udSource objectForKey: @"ModulesConstraints"]);
|
|
|
|
ASSIGN(_filter, [udSource objectForKey: @"filter"]);
|
2012-01-26 17:22:28 +01:00
|
|
|
ASSIGN(_userPasswordAlgorithm, [udSource objectForKey: @"userPasswordAlgorithm"]);
|
2011-07-05 22:10:45 +02:00
|
|
|
ASSIGN(_scope, ([udSource objectForKey: @"scope"]
|
2009-11-29 05:19:32 +01:00
|
|
|
? [udSource objectForKey: @"scope"]
|
|
|
|
: (id)@"sub"));
|
2010-03-08 16:18:05 +01:00
|
|
|
|
2012-01-26 17:22:28 +01:00
|
|
|
if (!_userPasswordAlgorithm)
|
|
|
|
_userPasswordAlgorithm = @"none";
|
|
|
|
|
2010-03-08 16:18:05 +01:00
|
|
|
if ([udSource objectForKey: @"passwordPolicy"])
|
|
|
|
passwordPolicy = [[udSource objectForKey: @"passwordPolicy"] boolValue];
|
2012-01-12 21:01:34 +01:00
|
|
|
|
|
|
|
ASSIGN(MSExchangeHostname, [udSource objectForKey: @"MSExchangeHostname"]);
|
2009-11-29 05:19:32 +01:00
|
|
|
}
|
2012-01-04 16:26:30 +01:00
|
|
|
|
2007-05-09 21:06:15 +02:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2010-12-14 19:08:36 +01:00
|
|
|
- (void) setBindDN: (NSString *) theDN
|
|
|
|
{
|
2011-07-05 22:10:45 +02:00
|
|
|
//NSLog(@"Setting bind DN to %@", theDN);
|
2010-12-14 19:08:36 +01:00
|
|
|
ASSIGN(bindDN, theDN);
|
|
|
|
}
|
|
|
|
|
2012-01-12 21:01:34 +01:00
|
|
|
- (NSString *) bindDN
|
|
|
|
{
|
|
|
|
return bindDN;
|
|
|
|
}
|
|
|
|
|
2010-12-14 19:08:36 +01:00
|
|
|
- (void) setBindPassword: (NSString *) thePassword
|
|
|
|
{
|
|
|
|
ASSIGN (password, thePassword);
|
|
|
|
}
|
|
|
|
|
2012-01-12 21:01:34 +01:00
|
|
|
- (NSString *) bindPassword
|
|
|
|
{
|
|
|
|
return password;
|
|
|
|
}
|
|
|
|
|
2010-12-14 19:08:36 +01:00
|
|
|
- (BOOL) bindAsCurrentUser
|
|
|
|
{
|
|
|
|
return _bindAsCurrentUser;
|
|
|
|
}
|
|
|
|
|
2007-05-09 21:06:15 +02:00
|
|
|
- (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
|
2010-12-14 19:08:36 +01:00
|
|
|
bindAsCurrentUser: (NSString *) bindAsCurrentUser
|
2007-05-09 21:06:15 +02:00
|
|
|
{
|
2011-07-05 22:10:45 +02:00
|
|
|
ASSIGN(bindDN, newBindDN);
|
|
|
|
ASSIGN(password, newBindPassword);
|
|
|
|
ASSIGN(sourceBindDN, newBindDN);
|
|
|
|
ASSIGN(sourceBindPassword, newBindPassword);
|
|
|
|
|
|
|
|
ASSIGN(encryption, [newEncryption uppercaseString]);
|
2009-04-02 22:51:46 +02:00
|
|
|
if ([encryption isEqualToString: @"SSL"])
|
|
|
|
port = 636;
|
2011-07-05 22:10:45 +02:00
|
|
|
ASSIGN(hostname, newBindHostname);
|
2007-05-09 21:06:15 +02:00
|
|
|
if (newBindPort)
|
|
|
|
port = [newBindPort intValue];
|
2010-12-14 19:08:36 +01:00
|
|
|
_bindAsCurrentUser = [bindAsCurrentUser boolValue];
|
2007-05-09 21:06:15 +02:00
|
|
|
}
|
|
|
|
|
2011-07-05 22:10:45 +02:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2007-05-09 21:06:15 +02:00
|
|
|
- (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
|
2011-01-24 19:02:12 +01:00
|
|
|
searchFields: (NSArray *) newSearchFields
|
2009-05-12 21:56:39 +02:00
|
|
|
IMAPHostField: (NSString *) newIMAPHostField
|
2011-04-14 20:41:10 +02:00
|
|
|
IMAPLoginField: (NSString *) newIMAPLoginField
|
2011-04-25 12:31:08 +02:00
|
|
|
bindFields: (id) newBindFields
|
|
|
|
kindField: (NSString *) newKindField
|
|
|
|
andMultipleBookingsField: (NSString *) newMultipleBookingsField
|
2007-05-09 21:06:15 +02:00
|
|
|
{
|
2011-07-05 22:10:45 +02:00
|
|
|
ASSIGN(baseDN, [newBaseDN lowercaseString]);
|
2007-05-09 21:06:15 +02:00
|
|
|
if (newIDField)
|
2011-12-30 21:39:07 +01:00
|
|
|
ASSIGN(IDField, [newIDField lowercaseString]);
|
2009-03-16 19:26:08 +01:00
|
|
|
if (newCNField)
|
2011-12-30 21:39:07 +01:00
|
|
|
ASSIGN(CNField, [newCNField lowercaseString]);
|
2009-03-16 19:26:08 +01:00
|
|
|
if (newUIDField)
|
2011-12-30 21:39:07 +01:00
|
|
|
ASSIGN(UIDField, [newUIDField lowercaseString]);
|
2009-05-12 21:56:39 +02:00
|
|
|
if (newIMAPHostField)
|
2011-12-30 21:39:07 +01:00
|
|
|
ASSIGN(IMAPHostField, [newIMAPHostField lowercaseString]);
|
2011-04-14 20:41:10 +02:00
|
|
|
if (newIMAPLoginField)
|
2011-12-30 21:39:07 +01:00
|
|
|
ASSIGN(IMAPLoginField, [newIMAPLoginField lowercaseString]);
|
2007-11-18 11:16:25 +01:00
|
|
|
if (newMailFields)
|
2011-07-05 22:10:45 +02:00
|
|
|
ASSIGN(mailFields, newMailFields);
|
2011-01-24 19:02:12 +01:00
|
|
|
if (newSearchFields)
|
2011-07-05 22:10:45 +02:00
|
|
|
ASSIGN(searchFields, newSearchFields);
|
2007-05-10 23:18:06 +02:00
|
|
|
if (newBindFields)
|
2010-01-08 18:48:45 +01:00
|
|
|
{
|
|
|
|
// Before SOGo v1.2.0, bindFields was a comma-separated list
|
|
|
|
// of values. So it could be configured as:
|
|
|
|
//
|
|
|
|
// bindFields = foo;
|
|
|
|
// bindFields = "foo, bar, baz";
|
|
|
|
//
|
|
|
|
// SOGo v1.2.0 and upwards redefined that parameter as an array
|
|
|
|
// so we would have instead:
|
|
|
|
//
|
|
|
|
// bindFields = (foo);
|
|
|
|
// bindFields = (foo, bar, baz);
|
|
|
|
//
|
|
|
|
// We check for the old format and we support it.
|
|
|
|
if ([newBindFields isKindOfClass: [NSArray class]])
|
|
|
|
ASSIGN(bindFields, newBindFields);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self logWithFormat: @"WARNING: using old bindFields format - please update it"];
|
|
|
|
ASSIGN(bindFields, [newBindFields componentsSeparatedByString: @","]);
|
|
|
|
}
|
|
|
|
}
|
2011-04-25 12:31:08 +02:00
|
|
|
if (newKindField)
|
2011-12-30 21:39:07 +01:00
|
|
|
ASSIGN(kindField, [newKindField lowercaseString]);
|
2011-04-25 12:31:08 +02:00
|
|
|
if (newMultipleBookingsField)
|
2011-12-30 21:39:07 +01:00
|
|
|
ASSIGN(multipleBookingsField, [newMultipleBookingsField lowercaseString]);
|
|
|
|
}
|
|
|
|
|
2012-01-04 16:26:30 +01:00
|
|
|
- (void) setListRequiresDot: (BOOL) aBool
|
|
|
|
{
|
|
|
|
listRequiresDot = aBool;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) listRequiresDot
|
|
|
|
{
|
|
|
|
return listRequiresDot;
|
|
|
|
}
|
|
|
|
|
2011-12-30 21:39:07 +01:00
|
|
|
- (void) setContactMapping: (NSDictionary *) newMapping
|
|
|
|
andObjectClasses: (NSArray *) newObjectClasses
|
|
|
|
{
|
|
|
|
ASSIGN (contactMapping, newMapping);
|
|
|
|
ASSIGN (contactObjectClasses, newObjectClasses);
|
2007-05-09 21:06:15 +02:00
|
|
|
}
|
|
|
|
|
2011-07-05 22:10:45 +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;
|
|
|
|
}
|
|
|
|
|
2011-07-05 22:10:45 +02:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2009-09-30 17:11:47 +02:00
|
|
|
- (NGLdapConnection *) _ldapConnection
|
2007-05-09 21:06:15 +02:00
|
|
|
{
|
2009-09-30 17:11:47 +02:00
|
|
|
NGLdapConnection *ldapConnection;
|
2008-09-08 16:54:33 +02:00
|
|
|
|
|
|
|
NS_DURING
|
|
|
|
{
|
2010-12-14 19:08:36 +01:00
|
|
|
//NSLog(@"Creating NGLdapConnection instance for bindDN '%@'", bindDN);
|
|
|
|
|
2008-09-08 16:54:33 +02:00
|
|
|
ldapConnection = [[NGLdapConnection alloc] initWithHostName: hostname
|
|
|
|
port: port];
|
2009-09-30 17:11:47 +02:00
|
|
|
[ldapConnection autorelease];
|
2009-04-02 22:51:46 +02:00
|
|
|
if (![encryption length] || [self _setupEncryption: ldapConnection])
|
|
|
|
{
|
|
|
|
[ldapConnection bindWithMethod: @"simple"
|
|
|
|
binddn: bindDN
|
|
|
|
credentials: password];
|
2009-11-29 05:19:32 +01:00
|
|
|
if (queryLimit > 0)
|
|
|
|
[ldapConnection setQuerySizeLimit: queryLimit];
|
|
|
|
if (queryTimeout > 0)
|
|
|
|
[ldapConnection setQueryTimeLimit: queryTimeout];
|
2012-01-04 16:26:30 +01:00
|
|
|
if (!schema)
|
|
|
|
{
|
|
|
|
schema = [LDAPSourceSchema new];
|
|
|
|
[schema readSchemaFromConnection: ldapConnection];
|
|
|
|
}
|
2009-04-02 22:51:46 +02:00
|
|
|
}
|
|
|
|
else
|
2009-09-30 17:11:47 +02:00
|
|
|
ldapConnection = nil;
|
2008-09-08 16:54:33 +02:00
|
|
|
}
|
|
|
|
NS_HANDLER
|
|
|
|
{
|
2011-12-30 21:39:07 +01:00
|
|
|
[self errorWithFormat: @"Could not bind to the LDAP server %@ (%d)"
|
|
|
|
@" using the bind DN: %@",
|
|
|
|
hostname, port, bindDN];
|
2009-09-30 17:11:47 +02:00
|
|
|
ldapConnection = nil;
|
2008-09-08 16:54:33 +02:00
|
|
|
}
|
|
|
|
NS_ENDHANDLER;
|
|
|
|
|
2009-09-30 17:11:47 +02:00
|
|
|
return ldapConnection;
|
2007-05-09 21:06:15 +02:00
|
|
|
}
|
|
|
|
|
2009-11-29 05:19:32 +01:00
|
|
|
- (NSString *) domain
|
|
|
|
{
|
|
|
|
return domain;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2010-09-24 19:50:40 +02:00
|
|
|
escapedUid = SafeLDAPCriteria(uid);
|
2009-06-04 16:02:45 +02:00
|
|
|
|
2010-01-08 18:48:45 +01:00
|
|
|
fields = [bindFields 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
|
|
|
|
{
|
|
|
|
NSEnumerator *entries;
|
2009-09-30 17:11:47 +02:00
|
|
|
EOQualifier *qualifier;
|
|
|
|
NSArray *attributes;
|
|
|
|
NGLdapConnection *ldapConnection;
|
|
|
|
NSString *userDN;
|
2008-09-08 16:54:33 +02:00
|
|
|
|
2009-09-30 17:11:47 +02:00
|
|
|
ldapConnection = [self _ldapConnection];
|
|
|
|
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];
|
2007-05-10 23:18:06 +02:00
|
|
|
else
|
2009-09-30 17:11:47 +02:00
|
|
|
entries = [ldapConnection deepSearchAtBaseDN: baseDN
|
|
|
|
qualifier: qualifier
|
|
|
|
attributes: attributes];
|
2008-09-08 16:54:33 +02:00
|
|
|
|
2009-09-30 17:11:47 +02:00
|
|
|
userDN = [[entries nextObject] dn];
|
2007-05-10 23:18:06 +02:00
|
|
|
|
|
|
|
return userDN;
|
|
|
|
}
|
|
|
|
|
2011-07-05 22:10:45 +02:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2010-03-08 16:18:05 +01:00
|
|
|
- (BOOL) checkLogin: (NSString *) _login
|
|
|
|
password: (NSString *) _pwd
|
|
|
|
perr: (SOGoPasswordPolicyError *) _perr
|
|
|
|
expire: (int *) _expire
|
|
|
|
grace: (int *) _grace
|
2007-05-09 21:06:15 +02:00
|
|
|
{
|
|
|
|
NGLdapConnection *bindConnection;
|
2010-03-08 16:18:05 +01:00
|
|
|
NSString *userDN;
|
|
|
|
BOOL didBind;
|
2007-05-09 21:06:15 +02:00
|
|
|
|
2007-08-29 18:01:11 +02:00
|
|
|
didBind = NO;
|
|
|
|
|
2011-06-23 21:09:01 +02:00
|
|
|
if ([_login length] > 0 && [_pwd length] > 0)
|
2007-05-10 16:56:47 +02:00
|
|
|
{
|
|
|
|
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-11-29 05:19:32 +01:00
|
|
|
if (queryTimeout > 0)
|
|
|
|
[bindConnection setQueryTimeLimit: queryTimeout];
|
2010-12-14 19:08:36 +01:00
|
|
|
|
|
|
|
userDN = [_dnCache objectForKey: _login];
|
|
|
|
|
|
|
|
if (!userDN)
|
|
|
|
{
|
|
|
|
if (bindFields)
|
2011-07-05 22:10:45 +02:00
|
|
|
{
|
|
|
|
// We MUST always use the source's bindDN/password in
|
|
|
|
// order to lookup the user's DN. This is important since
|
|
|
|
// if we use bindAsCurrentUser, we could stay bound and
|
|
|
|
// lookup the user's DN (for an other user that is trying
|
|
|
|
// to log in) but not be able to do so due to ACLs in LDAP.
|
|
|
|
[self setBindDN: sourceBindDN];
|
|
|
|
[self setBindPassword: sourceBindPassword];
|
|
|
|
userDN = [self _fetchUserDNForLogin: _login];
|
|
|
|
}
|
2010-12-14 19:08:36 +01:00
|
|
|
else
|
|
|
|
userDN = [NSString stringWithFormat: @"%@=%@,%@",
|
2012-02-06 14:55:46 +01:00
|
|
|
IDField, [_login escapedForLDAPDN], baseDN];
|
2010-12-14 19:08:36 +01:00
|
|
|
}
|
|
|
|
|
2009-04-02 22:51:46 +02:00
|
|
|
if (userDN)
|
|
|
|
{
|
2011-01-27 17:01:24 +01:00
|
|
|
// We cache the _login <-> userDN entry to speed up things
|
|
|
|
[_dnCache setObject: userDN forKey: _login];
|
|
|
|
|
2009-04-02 22:51:46 +02:00
|
|
|
NS_DURING
|
2010-03-08 16:18:05 +01:00
|
|
|
if (!passwordPolicy)
|
|
|
|
didBind = [bindConnection bindWithMethod: @"simple"
|
|
|
|
binddn: userDN
|
|
|
|
credentials: _pwd];
|
|
|
|
else
|
|
|
|
didBind = [bindConnection bindWithMethod: @"simple"
|
|
|
|
binddn: userDN
|
|
|
|
credentials: _pwd
|
|
|
|
perr: (void *)_perr
|
|
|
|
expire: _expire
|
|
|
|
grace: _grace];
|
2009-04-02 22:51:46 +02:00
|
|
|
NS_HANDLER
|
2009-09-11 16:31:02 +02:00
|
|
|
;
|
|
|
|
NS_ENDHANDLER
|
|
|
|
;
|
|
|
|
}
|
2007-05-10 23:18:06 +02:00
|
|
|
}
|
2009-09-30 17:11:47 +02:00
|
|
|
[bindConnection release];
|
2007-05-10 16:56:47 +02:00
|
|
|
}
|
2010-03-08 16:18:05 +01:00
|
|
|
|
2007-05-09 21:06:15 +02:00
|
|
|
return didBind;
|
|
|
|
}
|
|
|
|
|
2012-01-26 17:22:28 +01:00
|
|
|
/**
|
|
|
|
* Encrypts a string using this source password algorithm.
|
|
|
|
* @param plainPassword the unencrypted password.
|
|
|
|
* @return a new encrypted string.
|
|
|
|
* @see _isPassword:equalTo:
|
|
|
|
*/
|
|
|
|
- (NSString *) _encryptPassword: (NSString *) plainPassword
|
|
|
|
{
|
|
|
|
if ([_userPasswordAlgorithm caseInsensitiveCompare: @"none"] == NSOrderedSame)
|
|
|
|
{
|
|
|
|
return plainPassword;
|
|
|
|
}
|
|
|
|
else if ([_userPasswordAlgorithm caseInsensitiveCompare: @"crypt"] == NSOrderedSame)
|
|
|
|
{
|
|
|
|
return [NSString stringWithFormat: @"{CRYPT}%@", [plainPassword asCryptStringUsingSalt: [plainPassword asMD5String]]];
|
|
|
|
}
|
|
|
|
else if ([_userPasswordAlgorithm caseInsensitiveCompare: @"md5"] == NSOrderedSame)
|
|
|
|
{
|
|
|
|
return [NSString stringWithFormat: @"{MD5}%@", [plainPassword asMD5String]];
|
|
|
|
}
|
|
|
|
else if ([_userPasswordAlgorithm caseInsensitiveCompare: @"sha"] == NSOrderedSame)
|
|
|
|
{
|
|
|
|
return [NSString stringWithFormat: @"{SHA}%@", [plainPassword asSHA1String]];
|
|
|
|
}
|
|
|
|
|
|
|
|
[self errorWithFormat: @"Unsupported user-password algorithm: %@", _userPasswordAlgorithm];
|
|
|
|
|
|
|
|
return plainPassword;
|
|
|
|
}
|
|
|
|
|
2011-07-05 22:10:45 +02:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2010-03-08 16:18:05 +01:00
|
|
|
- (BOOL) changePasswordForLogin: (NSString *) login
|
|
|
|
oldPassword: (NSString *) oldPassword
|
|
|
|
newPassword: (NSString *) newPassword
|
|
|
|
perr: (SOGoPasswordPolicyError *) perr
|
|
|
|
|
|
|
|
{
|
|
|
|
NGLdapConnection *bindConnection;
|
|
|
|
NSString *userDN;
|
|
|
|
BOOL didChange;
|
|
|
|
|
|
|
|
didChange = NO;
|
|
|
|
|
|
|
|
if ([login length] > 0)
|
|
|
|
{
|
|
|
|
bindConnection = [[NGLdapConnection alloc] initWithHostName: hostname
|
|
|
|
port: port];
|
|
|
|
if (![encryption length] || [self _setupEncryption: bindConnection])
|
|
|
|
{
|
|
|
|
if (queryTimeout > 0)
|
|
|
|
[bindConnection setQueryTimeLimit: queryTimeout];
|
|
|
|
if (bindFields)
|
|
|
|
userDN = [self _fetchUserDNForLogin: login];
|
|
|
|
else
|
|
|
|
userDN = [NSString stringWithFormat: @"%@=%@,%@",
|
2012-02-06 14:55:46 +01:00
|
|
|
IDField, [login escapedForLDAPDN], baseDN];
|
2010-03-08 16:18:05 +01:00
|
|
|
if (userDN)
|
|
|
|
{
|
|
|
|
NS_DURING
|
|
|
|
if (!passwordPolicy)
|
|
|
|
{
|
|
|
|
// We don't use a password policy - we simply use
|
|
|
|
// a modify-op to change the password
|
|
|
|
NGLdapModification *mod;
|
|
|
|
NGLdapAttribute *attr;
|
|
|
|
NSArray *changes;
|
|
|
|
|
|
|
|
attr = [[NGLdapAttribute alloc] initWithAttributeName: @"userPassword"];
|
2012-01-26 17:22:28 +01:00
|
|
|
[attr addStringValue: [self _encryptPassword: newPassword]];
|
2010-03-08 16:18:05 +01:00
|
|
|
|
|
|
|
mod = [NGLdapModification replaceModification: attr];
|
|
|
|
changes = [NSArray arrayWithObject: mod];
|
2010-03-30 16:32:27 +02:00
|
|
|
*perr = PolicyNoError;
|
2010-03-08 16:18:05 +01:00
|
|
|
|
|
|
|
if ([bindConnection bindWithMethod: @"simple"
|
|
|
|
binddn: userDN
|
|
|
|
credentials: oldPassword])
|
|
|
|
didChange = [bindConnection modifyEntryWithDN: userDN
|
|
|
|
changes: changes];
|
|
|
|
else
|
|
|
|
didChange = NO;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
didChange = [bindConnection changePasswordAtDn: userDN
|
|
|
|
oldPassword: oldPassword
|
|
|
|
newPassword: newPassword
|
|
|
|
perr: (void *)perr];
|
|
|
|
NS_HANDLER
|
|
|
|
;
|
|
|
|
NS_ENDHANDLER
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
[bindConnection release];
|
|
|
|
}
|
|
|
|
|
|
|
|
return didChange;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-24 19:02:12 +01:00
|
|
|
/**
|
|
|
|
* Search for contacts matching some string.
|
2011-01-24 19:04:43 +01:00
|
|
|
* @param filter the string to search for
|
2011-01-24 19:02:12 +01:00
|
|
|
* @see fetchContactsMatching:
|
2011-01-24 19:04:43 +01:00
|
|
|
* @return a EOQualifier matching the filter
|
2011-01-24 19:02:12 +01:00
|
|
|
*/
|
2007-05-09 21:06:15 +02:00
|
|
|
- (EOQualifier *) _qualifierForFilter: (NSString *) filter
|
|
|
|
{
|
2011-01-24 19:02:12 +01:00
|
|
|
NSMutableArray *fields;
|
2011-03-21 19:24:06 +01:00
|
|
|
NSString *fieldFormat, *searchFormat, *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
|
|
|
|
2010-09-24 19:50:40 +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
|
|
|
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
|
2011-01-24 19:02:12 +01:00
|
|
|
{
|
|
|
|
fieldFormat = [NSString stringWithFormat: @"(%%@='%@*')", escapedFilter];
|
|
|
|
fields = [NSMutableArray arrayWithArray: searchFields];
|
|
|
|
[fields addObjectsFromArray: mailFields];
|
2011-03-21 19:24:06 +01:00
|
|
|
[fields addObject: CNField];
|
2011-01-24 19:02:12 +01:00
|
|
|
searchFormat = [[[fields uniqueObjects] stringsWithFormat: fieldFormat]
|
|
|
|
componentsJoinedByString: @" OR "];
|
2011-03-21 19:24:06 +01:00
|
|
|
[qs appendString: searchFormat];
|
2011-01-24 19:02:12 +01:00
|
|
|
}
|
2009-06-04 16:02:45 +02:00
|
|
|
|
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];
|
|
|
|
}
|
2012-01-04 16:26:30 +01:00
|
|
|
else if (!listRequiresDot)
|
|
|
|
{
|
|
|
|
qs = [NSMutableString stringWithFormat: @"(%@='*')", CNField];
|
|
|
|
if ([_filter length])
|
|
|
|
[qs appendFormat: @" AND %@", _filter];
|
|
|
|
qualifier = [EOQualifier qualifierWithQualifierFormat: qs];
|
|
|
|
}
|
2007-05-09 21:06:15 +02:00
|
|
|
else
|
|
|
|
qualifier = nil;
|
|
|
|
|
|
|
|
return qualifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (EOQualifier *) _qualifierForUIDFilter: (NSString *) uid
|
|
|
|
{
|
2009-09-11 16:31:02 +02:00
|
|
|
NSString *mailFormat, *fieldFormat, *escapedUid, *currentField;
|
|
|
|
NSEnumerator *bindFieldsEnum;
|
2009-03-16 22:13:01 +01:00
|
|
|
NSMutableString *qs;
|
2007-05-09 21:06:15 +02:00
|
|
|
|
2010-09-24 19:50:40 +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-09-11 16:31:02 +02:00
|
|
|
qs = [NSMutableString stringWithFormat: @"(%@='%@') OR %@",
|
|
|
|
UIDField, escapedUid, mailFormat];
|
|
|
|
if (bindFields)
|
|
|
|
{
|
2010-01-08 18:48:45 +01:00
|
|
|
bindFieldsEnum = [bindFields objectEnumerator];
|
2009-09-11 16:31:02 +02:00
|
|
|
while ((currentField = [bindFieldsEnum nextObject]))
|
2010-10-13 19:49:44 +02:00
|
|
|
{
|
|
|
|
if ([currentField caseInsensitiveCompare: UIDField] != NSOrderedSame
|
|
|
|
&& ![mailFields containsObject: currentField])
|
|
|
|
[qs appendFormat: @" OR (%@='%@')", [currentField stringByTrimmingSpaces], escapedUid];
|
|
|
|
}
|
2009-09-11 16:31:02 +02:00
|
|
|
}
|
2009-06-04 16:02:45 +02:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-04-13 17:01:21 +02:00
|
|
|
- (NSArray *) allEntryIDs
|
2007-05-09 21:06:15 +02:00
|
|
|
{
|
|
|
|
NSEnumerator *entries;
|
|
|
|
NGLdapEntry *currentEntry;
|
2009-09-30 17:11:47 +02:00
|
|
|
NGLdapConnection *ldapConnection;
|
2012-03-30 16:07:36 +02:00
|
|
|
EOQualifier *qualifier;
|
|
|
|
NSMutableString *qs;
|
2007-05-09 21:06:15 +02:00
|
|
|
NSString *value;
|
2009-09-30 17:11:47 +02:00
|
|
|
NSArray *attributes;
|
|
|
|
NSMutableArray *ids;
|
2007-05-09 21:06:15 +02:00
|
|
|
|
|
|
|
ids = [NSMutableArray array];
|
|
|
|
|
2009-09-30 17:11:47 +02:00
|
|
|
ldapConnection = [self _ldapConnection];
|
|
|
|
attributes = [NSArray arrayWithObject: IDField];
|
2012-03-30 16:07:36 +02:00
|
|
|
|
|
|
|
qs = [NSMutableString stringWithFormat: @"(%@='*')", CNField];
|
|
|
|
if ([_filter length])
|
|
|
|
[qs appendFormat: @" AND %@", _filter];
|
|
|
|
qualifier = [EOQualifier qualifierWithQualifierFormat: qs];
|
|
|
|
|
2009-09-30 17:11:47 +02:00
|
|
|
if ([_scope caseInsensitiveCompare: @"BASE"] == NSOrderedSame)
|
|
|
|
entries = [ldapConnection baseSearchAtBaseDN: baseDN
|
2012-03-30 16:07:36 +02:00
|
|
|
qualifier: qualifier
|
2009-09-30 17:11:47 +02:00
|
|
|
attributes: attributes];
|
|
|
|
else if ([_scope caseInsensitiveCompare: @"ONE"] == NSOrderedSame)
|
|
|
|
entries = [ldapConnection flatSearchAtBaseDN: baseDN
|
2012-03-30 16:07:36 +02:00
|
|
|
qualifier: qualifier
|
2009-09-30 17:11:47 +02:00
|
|
|
attributes: attributes];
|
2008-09-08 16:54:33 +02:00
|
|
|
else
|
2009-09-30 17:11:47 +02:00
|
|
|
entries = [ldapConnection deepSearchAtBaseDN: baseDN
|
2012-03-30 16:07:36 +02:00
|
|
|
qualifier: qualifier
|
2009-09-30 17:11:47 +02:00
|
|
|
attributes: attributes];
|
2008-09-08 16:54:33 +02:00
|
|
|
|
2009-09-30 17:11:47 +02:00
|
|
|
while ((currentEntry = [entries nextObject]))
|
2007-05-09 21:06:15 +02:00
|
|
|
{
|
2009-09-30 17:11:47 +02:00
|
|
|
value = [[currentEntry attributeWithName: IDField]
|
2007-05-09 21:06:15 +02:00
|
|
|
stringValueAtIndex: 0];
|
2009-09-30 17:11:47 +02:00
|
|
|
if ([value length] > 0)
|
|
|
|
[ids addObject: value];
|
2007-05-09 21:06:15 +02:00
|
|
|
}
|
2008-09-08 16:54:33 +02:00
|
|
|
|
2007-05-09 21:06:15 +02:00
|
|
|
return ids;
|
|
|
|
}
|
|
|
|
|
2007-11-18 11:16:25 +01:00
|
|
|
- (void) _fillEmailsOfEntry: (NGLdapEntry *) ldapEntry
|
2011-12-30 21:39:07 +01:00
|
|
|
intoLDIFRecord: (NSMutableDictionary *) ldifRecord
|
2007-11-18 11:16:25 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2009-09-25 16:42:33 +02:00
|
|
|
emails = [[NSMutableArray alloc] init];
|
2007-11-18 11:16:25 +01:00
|
|
|
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
|
|
|
}
|
2011-12-30 21:39:07 +01:00
|
|
|
[ldifRecord 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)
|
2011-12-30 21:39:07 +01:00
|
|
|
[ldifRecord setObject: ldapValue forKey: @"c_imaphostname"];
|
2009-05-12 21:56:39 +02:00
|
|
|
}
|
2011-04-14 20:41:10 +02:00
|
|
|
|
|
|
|
if (IMAPLoginField)
|
|
|
|
{
|
|
|
|
ldapValue = [[ldapEntry attributeWithName: IMAPLoginField] stringValueAtIndex: 0];
|
|
|
|
if ([ldapValue length] > 0)
|
2011-12-30 21:39:07 +01:00
|
|
|
[ldifRecord setObject: ldapValue forKey: @"c_imaplogin"];
|
2011-04-14 20:41:10 +02:00
|
|
|
}
|
2007-11-18 11:16:25 +01:00
|
|
|
}
|
|
|
|
|
2007-11-26 21:41:59 +01:00
|
|
|
- (void) _fillConstraints: (NGLdapEntry *) ldapEntry
|
|
|
|
forModule: (NSString *) module
|
2011-12-30 21:39:07 +01:00
|
|
|
intoLDIFRecord: (NSMutableDictionary *) ldifRecord
|
2007-11-26 21:41:59 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-30 21:39:07 +01:00
|
|
|
[ldifRecord setObject: [NSNumber numberWithBool: result]
|
|
|
|
forKey: [NSString stringWithFormat: @"%@Access", module]];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* conversion LDAP -> SOGo inetOrgPerson entry */
|
|
|
|
- (void) _applyContactMappingToResult: (NSMutableDictionary *) ldifRecord
|
|
|
|
{
|
|
|
|
NSArray *sourceFields;
|
|
|
|
NSArray *keys;
|
|
|
|
NSString *key, *field, *value;
|
|
|
|
NSUInteger count, max, fieldCount, fieldMax;
|
|
|
|
BOOL filled;
|
|
|
|
|
|
|
|
keys = [contactMapping allKeys];
|
|
|
|
max = [keys count];
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
{
|
|
|
|
key = [keys objectAtIndex: count];
|
|
|
|
sourceFields = [contactMapping objectForKey: key];
|
|
|
|
if ([sourceFields isKindOfClass: NSStringK])
|
|
|
|
sourceFields = [NSArray arrayWithObject: sourceFields];
|
|
|
|
fieldMax = [sourceFields count];
|
|
|
|
filled = NO;
|
|
|
|
for (fieldCount = 0;
|
|
|
|
!filled && fieldCount < fieldMax;
|
|
|
|
fieldCount++)
|
|
|
|
{
|
|
|
|
field = [[sourceFields objectAtIndex: fieldCount] lowercaseString];
|
|
|
|
value = [ldifRecord objectForKey: field];
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
[ldifRecord setObject: value forKey: [key lowercaseString]];
|
|
|
|
filled = YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* conversion SOGo inetOrgPerson entry -> LDAP */
|
|
|
|
- (void) _applyContactMappingToOutput: (NSMutableDictionary *) ldifRecord
|
|
|
|
{
|
|
|
|
NSArray *sourceFields;
|
|
|
|
NSArray *keys;
|
|
|
|
NSString *key, *lowerKey, *field, *value;
|
|
|
|
NSUInteger count, max, fieldCount, fieldMax;
|
|
|
|
|
|
|
|
if (contactObjectClasses)
|
|
|
|
[ldifRecord setObject: contactObjectClasses
|
|
|
|
forKey: @"objectclass"];
|
|
|
|
|
|
|
|
keys = [contactMapping allKeys];
|
|
|
|
max = [keys count];
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
{
|
|
|
|
key = [keys objectAtIndex: count];
|
|
|
|
lowerKey = [key lowercaseString];
|
|
|
|
value = [ldifRecord objectForKey: lowerKey];
|
|
|
|
if ([value length] > 0)
|
|
|
|
{
|
|
|
|
sourceFields = [contactMapping objectForKey: key];
|
|
|
|
if ([sourceFields isKindOfClass: NSStringK])
|
|
|
|
sourceFields = [NSArray arrayWithObject: sourceFields];
|
|
|
|
|
|
|
|
fieldMax = [sourceFields count];
|
|
|
|
for (fieldCount = 0; fieldCount < fieldMax; fieldCount++)
|
|
|
|
{
|
|
|
|
field = [[sourceFields objectAtIndex: fieldCount]
|
|
|
|
lowercaseString];
|
|
|
|
[ldifRecord setObject: value forKey: field];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-11-26 21:41:59 +01:00
|
|
|
}
|
|
|
|
|
2007-05-09 21:06:15 +02:00
|
|
|
- (NSDictionary *) _convertLDAPEntryToContact: (NGLdapEntry *) ldapEntry
|
|
|
|
{
|
2011-12-30 21:39:07 +01:00
|
|
|
NSMutableDictionary *ldifRecord;
|
|
|
|
NSString *value;
|
|
|
|
static NSArray *resourceKinds = nil;
|
2009-09-25 16:42:33 +02:00
|
|
|
NSMutableArray *classes;
|
|
|
|
id o;
|
2007-05-09 21:06:15 +02:00
|
|
|
|
2011-12-30 21:39:07 +01:00
|
|
|
if (!resourceKinds)
|
|
|
|
resourceKinds = [[NSArray alloc] initWithObjects: @"location", @"thing",
|
|
|
|
@"group", nil];
|
2009-08-17 11:48:04 +02:00
|
|
|
|
2011-12-30 21:39:07 +01:00
|
|
|
ldifRecord = [ldapEntry _asDictionary];
|
2012-01-12 23:12:02 +01:00
|
|
|
[ldifRecord setObject: self forKey: @"source"];
|
2011-12-30 21:39:07 +01:00
|
|
|
[ldifRecord setObject: [ldapEntry dn] forKey: @"dn"];
|
|
|
|
|
2009-09-25 16:42:33 +02:00
|
|
|
// We get our objectClass attribute values. We lowercase
|
|
|
|
// everything for ease of search after.
|
|
|
|
o = [ldapEntry objectClasses];
|
|
|
|
classes = nil;
|
|
|
|
|
|
|
|
if (o)
|
|
|
|
{
|
|
|
|
int i, c;
|
|
|
|
|
|
|
|
classes = [NSMutableArray arrayWithArray: o];
|
|
|
|
c = [classes count];
|
|
|
|
for (i = 0; i < c; i++)
|
|
|
|
[classes replaceObjectAtIndex: i
|
|
|
|
withObject: [[classes objectAtIndex: i] lowercaseString]];
|
|
|
|
}
|
|
|
|
|
2009-09-10 20:31:20 +02:00
|
|
|
if (classes)
|
|
|
|
{
|
2009-09-25 16:42:33 +02:00
|
|
|
// We check if our entry is a group. If so, we set the
|
|
|
|
// 'isGroup' custom attribute.
|
2009-09-10 20:31:20 +02:00
|
|
|
if ([classes containsObject: @"group"] ||
|
2009-09-25 16:42:33 +02:00
|
|
|
[classes containsObject: @"groupofnames"] ||
|
|
|
|
[classes containsObject: @"groupofuniquenames"] ||
|
|
|
|
[classes containsObject: @"posixgroup"])
|
2009-09-10 20:31:20 +02:00
|
|
|
{
|
2011-12-30 21:39:07 +01:00
|
|
|
[ldifRecord setObject: [NSNumber numberWithInt: 1]
|
|
|
|
forKey: @"isGroup"];
|
2009-09-10 20:31:20 +02:00
|
|
|
}
|
2011-04-25 12:31:08 +02:00
|
|
|
// We check if our entry is a resource. We also support
|
|
|
|
// determining resources based on the KindFieldName attribute
|
|
|
|
// value - see below.
|
|
|
|
else if ([classes containsObject: @"calendarresource"])
|
|
|
|
{
|
2011-12-30 21:39:07 +01:00
|
|
|
[ldifRecord setObject: [NSNumber numberWithInt: 1]
|
|
|
|
forKey: @"isResource"];
|
2011-04-25 12:31:08 +02:00
|
|
|
}
|
2009-09-10 20:31:20 +02:00
|
|
|
}
|
2009-09-25 16:42:33 +02:00
|
|
|
|
2011-12-30 21:39:07 +01:00
|
|
|
// We check if that entry corresponds to a resource. For this,
|
|
|
|
// kindField must be defined and it must hold one of those values
|
|
|
|
//
|
|
|
|
// location
|
|
|
|
// thing
|
|
|
|
// group
|
|
|
|
//
|
|
|
|
if ([kindField length] > 0)
|
2007-05-09 21:06:15 +02:00
|
|
|
{
|
2011-12-30 21:39:07 +01:00
|
|
|
value = [ldifRecord objectForKey: [kindField lowercaseString]];
|
|
|
|
if ([value isKindOfClass: NSStringK]
|
|
|
|
&& [resourceKinds containsObject: value])
|
|
|
|
[ldifRecord setObject: [NSNumber numberWithInt: 1]
|
|
|
|
forKey: @"isResource"];
|
|
|
|
}
|
2009-09-25 16:42:33 +02:00
|
|
|
|
2011-12-30 21:39:07 +01:00
|
|
|
// We check for the number of simultanous bookings that is allowed.
|
|
|
|
// A value of 0 means that there's no limit.
|
|
|
|
if ([multipleBookingsField length] > 0)
|
|
|
|
{
|
|
|
|
value = [ldifRecord objectForKey: [multipleBookingsField lowercaseString]];
|
|
|
|
[ldifRecord setObject: [NSNumber numberWithInt: [value intValue]]
|
|
|
|
forKey: @"numberOfSimultaneousBookings"];
|
2007-05-09 21:06:15 +02:00
|
|
|
}
|
2009-09-25 16:42:33 +02:00
|
|
|
|
2007-05-09 21:06:15 +02:00
|
|
|
value = [[ldapEntry attributeWithName: IDField] stringValueAtIndex: 0];
|
|
|
|
if (!value)
|
|
|
|
value = @"";
|
2011-12-30 21:39:07 +01:00
|
|
|
[ldifRecord setObject: value forKey: @"c_name"];
|
2007-05-09 21:06:15 +02:00
|
|
|
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 "@"
|
|
|
|
// }
|
2011-12-30 21:39:07 +01:00
|
|
|
[ldifRecord setObject: value forKey: @"c_uid"];
|
2007-05-09 21:06:15 +02:00
|
|
|
value = [[ldapEntry attributeWithName: CNField] stringValueAtIndex: 0];
|
|
|
|
if (!value)
|
|
|
|
value = @"";
|
2011-12-30 21:39:07 +01:00
|
|
|
[ldifRecord setObject: value forKey: @"c_cn"];
|
|
|
|
/* if "displayName" is not set, we use CNField because it must exist */
|
|
|
|
if (![ldifRecord objectForKey: @"displayname"])
|
|
|
|
[ldifRecord setObject: value forKey: @"displayname"];
|
2009-11-29 05:19:32 +01:00
|
|
|
|
|
|
|
if (contactInfoAttribute)
|
|
|
|
{
|
|
|
|
value = [[ldapEntry attributeWithName: contactInfoAttribute]
|
|
|
|
stringValueAtIndex: 0];
|
|
|
|
if (!value)
|
|
|
|
value = @"";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
value = @"";
|
2011-12-30 21:39:07 +01:00
|
|
|
[ldifRecord setObject: value forKey: @"c_info"];
|
2009-11-29 05:19:32 +01:00
|
|
|
|
2009-11-29 22:44:04 +01:00
|
|
|
if (domain)
|
2009-11-29 05:19:32 +01:00
|
|
|
value = domain;
|
|
|
|
else
|
|
|
|
value = @"";
|
2011-12-30 21:39:07 +01:00
|
|
|
[ldifRecord setObject: value forKey: @"c_domain"];
|
2009-11-29 05:19:32 +01:00
|
|
|
|
2011-12-30 21:39:07 +01:00
|
|
|
[self _fillEmailsOfEntry: ldapEntry intoLDIFRecord: ldifRecord];
|
2007-11-26 21:41:59 +01:00
|
|
|
[self _fillConstraints: ldapEntry forModule: @"Calendar"
|
2011-12-30 21:39:07 +01:00
|
|
|
intoLDIFRecord: (NSMutableDictionary *) ldifRecord];
|
2007-11-26 21:41:59 +01:00
|
|
|
[self _fillConstraints: ldapEntry forModule: @"Mail"
|
2011-12-30 21:39:07 +01:00
|
|
|
intoLDIFRecord: (NSMutableDictionary *) ldifRecord];
|
|
|
|
|
|
|
|
if (contactMapping)
|
|
|
|
[self _applyContactMappingToResult: ldifRecord];
|
2007-05-09 21:06:15 +02:00
|
|
|
|
2011-12-30 21:39:07 +01:00
|
|
|
return ldifRecord;
|
2007-05-09 21:06:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *) fetchContactsMatching: (NSString *) match
|
2012-02-11 08:08:02 +01:00
|
|
|
inDomain: (NSString *) domain
|
2007-05-09 21:06:15 +02:00
|
|
|
{
|
2009-09-30 17:11:47 +02:00
|
|
|
NGLdapConnection *ldapConnection;
|
2007-05-09 21:06:15 +02:00
|
|
|
NGLdapEntry *currentEntry;
|
|
|
|
NSEnumerator *entries;
|
2009-09-30 17:11:47 +02:00
|
|
|
NSMutableArray *contacts;
|
|
|
|
EOQualifier *qualifier;
|
|
|
|
NSArray *attributes;
|
2007-05-09 21:06:15 +02:00
|
|
|
|
|
|
|
contacts = [NSMutableArray array];
|
|
|
|
|
2012-01-04 16:26:30 +01:00
|
|
|
if ([match length] > 0 || !listRequiresDot)
|
2007-05-09 21:06:15 +02:00
|
|
|
{
|
2009-09-30 17:11:47 +02:00
|
|
|
ldapConnection = [self _ldapConnection];
|
|
|
|
qualifier = [self _qualifierForFilter: match];
|
2011-12-30 21:39:07 +01:00
|
|
|
// attributes = [self _searchAttributes];
|
|
|
|
attributes = [NSArray arrayWithObject: @"*"];
|
2010-03-30 16:32:27 +02:00
|
|
|
|
2009-09-30 17:11:47 +02:00
|
|
|
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 /* we do it like before */
|
|
|
|
entries = [ldapConnection deepSearchAtBaseDN: baseDN
|
|
|
|
qualifier: qualifier
|
|
|
|
attributes: attributes];
|
|
|
|
while ((currentEntry = [entries nextObject]))
|
|
|
|
[contacts addObject:
|
|
|
|
[self _convertLDAPEntryToContact: currentEntry]];
|
2007-05-09 21:06:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return contacts;
|
|
|
|
}
|
|
|
|
|
2011-12-30 21:39:07 +01:00
|
|
|
- (NGLdapEntry *) _lookupLDAPEntry: (EOQualifier *) qualifier
|
2007-05-09 21:06:15 +02:00
|
|
|
{
|
2009-09-30 17:11:47 +02:00
|
|
|
NGLdapConnection *ldapConnection;
|
2011-12-30 21:39:07 +01:00
|
|
|
NSArray *attributes;
|
2009-09-30 17:11:47 +02:00
|
|
|
NSEnumerator *entries;
|
2011-12-30 21:39:07 +01:00
|
|
|
|
|
|
|
// attributes = [self _searchAttributes];
|
|
|
|
ldapConnection = [self _ldapConnection];
|
|
|
|
attributes = [NSArray arrayWithObject: @"*"];
|
|
|
|
|
|
|
|
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];
|
|
|
|
|
|
|
|
return [entries nextObject];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDictionary *) lookupContactEntry: (NSString *) theID
|
|
|
|
{
|
|
|
|
NGLdapEntry *ldapEntry;
|
2009-09-30 17:11:47 +02:00
|
|
|
EOQualifier *qualifier;
|
|
|
|
NSString *s;
|
2011-12-30 21:39:07 +01:00
|
|
|
NSDictionary *ldifRecord;
|
2007-05-09 21:06:15 +02:00
|
|
|
|
2011-12-30 21:39:07 +01:00
|
|
|
ldifRecord = nil;
|
2007-05-10 16:56:47 +02:00
|
|
|
|
2009-04-13 17:01:21 +02:00
|
|
|
if ([theID length] > 0)
|
2007-05-10 16:56:47 +02:00
|
|
|
{
|
2009-09-30 17:11:47 +02:00
|
|
|
s = [NSString stringWithFormat: @"(%@='%@')",
|
2010-09-24 19:50:40 +02:00
|
|
|
IDField, SafeLDAPCriteria(theID)];
|
2009-09-30 17:11:47 +02:00
|
|
|
qualifier = [EOQualifier qualifierWithQualifierFormat: s];
|
2011-12-30 21:39:07 +01:00
|
|
|
ldapEntry = [self _lookupLDAPEntry: qualifier];
|
2009-09-30 17:11:47 +02:00
|
|
|
if (ldapEntry)
|
2011-12-30 21:39:07 +01:00
|
|
|
ldifRecord = [self _convertLDAPEntryToContact: ldapEntry];
|
2007-05-10 16:56:47 +02:00
|
|
|
}
|
2007-05-09 21:06:15 +02:00
|
|
|
|
2011-12-30 21:39:07 +01:00
|
|
|
return ldifRecord;
|
2007-05-09 21:06:15 +02:00
|
|
|
}
|
|
|
|
|
2009-04-13 17:01:21 +02:00
|
|
|
- (NSDictionary *) lookupContactEntryWithUIDorEmail: (NSString *) uid
|
2012-02-11 08:08:02 +01:00
|
|
|
inDomain: (NSString *) domain
|
2007-05-09 21:06:15 +02:00
|
|
|
{
|
|
|
|
NGLdapEntry *ldapEntry;
|
2009-09-30 17:11:47 +02:00
|
|
|
EOQualifier *qualifier;
|
2011-12-30 21:39:07 +01:00
|
|
|
NSDictionary *ldifRecord;
|
2007-05-09 21:06:15 +02:00
|
|
|
|
2011-12-30 21:39:07 +01:00
|
|
|
ldifRecord = nil;
|
2007-05-10 16:56:47 +02:00
|
|
|
|
|
|
|
if ([uid length] > 0)
|
|
|
|
{
|
2009-09-30 17:11:47 +02:00
|
|
|
qualifier = [self _qualifierForUIDFilter: uid];
|
2011-12-30 21:39:07 +01:00
|
|
|
ldapEntry = [self _lookupLDAPEntry: qualifier];
|
2007-05-10 16:56:47 +02:00
|
|
|
if (ldapEntry)
|
2011-12-30 21:39:07 +01:00
|
|
|
ldifRecord = [self _convertLDAPEntryToContact: ldapEntry];
|
2007-05-10 16:56:47 +02:00
|
|
|
}
|
2007-05-09 21:06:15 +02:00
|
|
|
|
2011-12-30 21:39:07 +01:00
|
|
|
return ldifRecord;
|
2007-05-09 21:06:15 +02:00
|
|
|
}
|
|
|
|
|
2009-05-03 19:50:57 +02:00
|
|
|
- (NSString *) lookupLoginByDN: (NSString *) theDN
|
|
|
|
{
|
2009-09-30 17:11:47 +02:00
|
|
|
NGLdapConnection *ldapConnection;
|
2009-05-03 19:50:57 +02:00
|
|
|
NGLdapEntry *entry;
|
|
|
|
NSString *login;
|
|
|
|
|
|
|
|
login = nil;
|
2009-09-30 17:11:47 +02:00
|
|
|
|
|
|
|
ldapConnection = [self _ldapConnection];
|
|
|
|
entry = [ldapConnection entryAtDN: theDN
|
|
|
|
attributes: [NSArray arrayWithObject: UIDField]];
|
|
|
|
if (entry)
|
|
|
|
login = [[entry attributeWithName: UIDField] stringValueAtIndex: 0];
|
|
|
|
|
2009-05-03 19:50:57 +02:00
|
|
|
return login;
|
|
|
|
}
|
|
|
|
|
2010-12-14 19:08:36 +01:00
|
|
|
- (NSString *) lookupDNByLogin: (NSString *) theLogin
|
|
|
|
{
|
|
|
|
return [_dnCache objectForKey: theLogin];
|
|
|
|
}
|
|
|
|
|
2009-05-03 19:50:57 +02:00
|
|
|
- (NGLdapEntry *) lookupGroupEntryByUID: (NSString *) theUID
|
|
|
|
{
|
|
|
|
return [self lookupGroupEntryByAttribute: UIDField
|
|
|
|
andValue: theUID];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NGLdapEntry *) lookupGroupEntryByEmail: (NSString *) theEmail
|
|
|
|
{
|
2009-09-10 20:31:20 +02:00
|
|
|
#warning We should support MailFieldNames
|
2009-05-03 19:50:57 +02:00
|
|
|
return [self lookupGroupEntryByAttribute: @"mail"
|
|
|
|
andValue: theEmail];
|
|
|
|
}
|
|
|
|
|
2009-09-10 20:31:20 +02:00
|
|
|
// This method should accept multiple attributes
|
2009-05-03 19:50:57 +02:00
|
|
|
- (NGLdapEntry *) lookupGroupEntryByAttribute: (NSString *) theAttribute
|
|
|
|
andValue: (NSString *) theValue
|
2009-04-30 23:17:55 +02:00
|
|
|
{
|
2009-09-30 17:11:47 +02:00
|
|
|
EOQualifier *qualifier;
|
|
|
|
NSString *s;
|
2009-04-30 23:17:55 +02:00
|
|
|
NGLdapEntry *ldapEntry;
|
|
|
|
|
2009-05-03 19:50:57 +02:00
|
|
|
if ([theValue length] > 0)
|
2009-04-30 23:17:55 +02:00
|
|
|
{
|
2009-09-30 17:11:47 +02:00
|
|
|
s = [NSString stringWithFormat: @"(%@='%@')",
|
2010-09-24 19:50:40 +02:00
|
|
|
theAttribute, SafeLDAPCriteria(theValue)];
|
2009-09-30 17:11:47 +02:00
|
|
|
qualifier = [EOQualifier qualifierWithQualifierFormat: s];
|
2009-11-29 05:19:32 +01:00
|
|
|
|
2009-09-30 17:11:47 +02:00
|
|
|
// We look for additional attributes - the ones related to group
|
|
|
|
// membership
|
2011-12-30 21:39:07 +01:00
|
|
|
// attributes = [NSMutableArray arrayWithArray: [self _searchAttributes]];
|
|
|
|
// [attributes addObject: @"member"];
|
|
|
|
// [attributes addObject: @"uniqueMember"];
|
|
|
|
// [attributes addObject: @"memberUid"];
|
|
|
|
// [attributes addObject: @"memberOf"];
|
|
|
|
ldapEntry = [self _lookupLDAPEntry: qualifier];
|
2009-04-30 23:17:55 +02:00
|
|
|
}
|
2009-09-30 17:11:47 +02:00
|
|
|
else
|
|
|
|
ldapEntry = nil;
|
2009-04-30 23:17:55 +02:00
|
|
|
|
|
|
|
return ldapEntry;
|
|
|
|
}
|
|
|
|
|
2012-01-04 16:26:30 +01:00
|
|
|
- (void) setSourceID: (NSString *) newSourceID
|
|
|
|
{
|
|
|
|
ASSIGN (sourceID, newSourceID);
|
|
|
|
}
|
|
|
|
|
2007-10-10 20:44:26 +02:00
|
|
|
- (NSString *) sourceID
|
|
|
|
{
|
|
|
|
return sourceID;
|
|
|
|
}
|
|
|
|
|
2012-01-04 16:26:30 +01:00
|
|
|
- (void) setDisplayName: (NSString *) newDisplayName
|
|
|
|
{
|
|
|
|
ASSIGN (displayName, newDisplayName);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) displayName
|
|
|
|
{
|
|
|
|
return displayName;
|
|
|
|
}
|
|
|
|
|
2009-05-03 19:50:57 +02:00
|
|
|
- (NSString *) baseDN
|
|
|
|
{
|
|
|
|
return baseDN;
|
|
|
|
}
|
|
|
|
|
2012-01-12 21:01:34 +01:00
|
|
|
- (NSString *) MSExchangeHostname
|
|
|
|
{
|
|
|
|
return MSExchangeHostname;
|
|
|
|
}
|
|
|
|
|
2012-01-04 16:26:30 +01:00
|
|
|
- (void) setModifiers: (NSArray *) newModifiers
|
|
|
|
{
|
|
|
|
ASSIGN (modifiers, newModifiers);
|
|
|
|
}
|
|
|
|
|
2011-12-30 21:39:07 +01:00
|
|
|
- (NSArray *) modifiers
|
|
|
|
{
|
|
|
|
return modifiers;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NSArray *
|
|
|
|
_convertRecordToLDAPAttributes (LDAPSourceSchema *schema, NSDictionary *ldifRecord)
|
|
|
|
{
|
|
|
|
/* convert resulting record to NGLdapEntry:
|
2012-01-04 16:26:30 +01:00
|
|
|
- strip non-existing object classes
|
2011-12-30 21:39:07 +01:00
|
|
|
- ignore fields with empty values
|
|
|
|
- ignore extra fields
|
|
|
|
- use correct case for LDAP attribute matching classes */
|
2012-01-04 16:26:30 +01:00
|
|
|
NSMutableArray *validClasses, *validFields, *attributes;
|
2011-12-30 21:39:07 +01:00
|
|
|
NGLdapAttribute *attribute;
|
|
|
|
NSArray *classes, *fields, *values;
|
2012-01-04 16:26:30 +01:00
|
|
|
NSString *objectClass, *field, *lowerField, *value;
|
2011-12-30 21:39:07 +01:00
|
|
|
NSUInteger count, max, valueCount, valueMax;
|
|
|
|
|
|
|
|
classes = [ldifRecord objectForKey: @"objectclass"];
|
|
|
|
if ([classes isKindOfClass: NSStringK])
|
|
|
|
classes = [NSArray arrayWithObject: classes];
|
2012-01-04 16:26:30 +01:00
|
|
|
max = [classes count];
|
|
|
|
validClasses = [NSMutableArray array];
|
|
|
|
validFields = [NSMutableArray array];
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
{
|
|
|
|
objectClass = [classes objectAtIndex: count];
|
|
|
|
fields = [schema fieldsForClass: objectClass];
|
|
|
|
if ([fields count] > 0)
|
|
|
|
{
|
|
|
|
[validClasses addObject: objectClass];
|
|
|
|
[validFields addObjectsFromArray: fields];
|
|
|
|
}
|
|
|
|
}
|
2012-03-01 22:57:16 +01:00
|
|
|
[validFields removeDoubles];
|
2011-12-30 21:39:07 +01:00
|
|
|
|
2012-01-04 16:26:30 +01:00
|
|
|
attributes = [NSMutableArray new];
|
|
|
|
max = [validFields count];
|
2011-12-30 21:39:07 +01:00
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
{
|
|
|
|
attribute = nil;
|
2012-01-04 16:26:30 +01:00
|
|
|
field = [validFields objectAtIndex: count];
|
2011-12-30 21:39:07 +01:00
|
|
|
lowerField = [field lowercaseString];
|
|
|
|
if (![lowerField isEqualToString: @"dn"])
|
|
|
|
{
|
2012-01-04 16:26:30 +01:00
|
|
|
if ([lowerField isEqualToString: @"objectclass"])
|
|
|
|
values = validClasses;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
values = [ldifRecord objectForKey: lowerField];
|
|
|
|
if ([values isKindOfClass: NSStringK])
|
|
|
|
values = [NSArray arrayWithObject: values];
|
|
|
|
}
|
2011-12-30 21:39:07 +01:00
|
|
|
valueMax = [values count];
|
|
|
|
for (valueCount = 0; valueCount < valueMax; valueCount++)
|
|
|
|
{
|
|
|
|
value = [values objectAtIndex: valueCount];
|
|
|
|
if ([value length] > 0)
|
|
|
|
{
|
|
|
|
if (!attribute)
|
|
|
|
{
|
|
|
|
attribute = [[NGLdapAttribute alloc]
|
|
|
|
initWithAttributeName: field];
|
|
|
|
[attributes addObject: attribute];
|
|
|
|
[attribute release];
|
|
|
|
}
|
|
|
|
[attribute addStringValue: value];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return attributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSException *) addContactEntry: (NSDictionary *) roLdifRecord
|
|
|
|
withID: (NSString *) aId
|
|
|
|
{
|
2012-01-04 16:26:30 +01:00
|
|
|
NSException *result;
|
2011-12-30 21:39:07 +01:00
|
|
|
NGLdapEntry *newEntry;
|
|
|
|
NSMutableDictionary *ldifRecord;
|
|
|
|
NSArray *attributes;
|
|
|
|
NSString *dn, *cnValue;
|
|
|
|
NGLdapConnection *ldapConnection;
|
|
|
|
|
|
|
|
if ([aId length] > 0)
|
|
|
|
{
|
|
|
|
ldapConnection = [self _ldapConnection];
|
|
|
|
ldifRecord = [roLdifRecord mutableCopy];
|
|
|
|
[ldifRecord autorelease];
|
|
|
|
[ldifRecord setObject: aId forKey: UIDField];
|
|
|
|
|
|
|
|
/* if CN is not set, we use aId because it must exist */
|
|
|
|
if (![ldifRecord objectForKey: CNField])
|
|
|
|
{
|
|
|
|
cnValue = [ldifRecord objectForKey: @"displayname"];
|
|
|
|
if ([cnValue length] == 0)
|
|
|
|
cnValue = aId;
|
|
|
|
[ldifRecord setObject: aId forKey: @"cn"];
|
|
|
|
}
|
|
|
|
|
|
|
|
[self _applyContactMappingToOutput: ldifRecord];
|
|
|
|
|
|
|
|
/* since the id might have changed due to the mapping above, we
|
|
|
|
reload the record ID */
|
|
|
|
aId = [ldifRecord objectForKey: UIDField];
|
2012-02-06 14:55:46 +01:00
|
|
|
dn = [NSString stringWithFormat: @"%@=%@,%@", IDField,
|
|
|
|
[aId escapedForLDAPDN], baseDN];
|
2011-12-30 21:39:07 +01:00
|
|
|
attributes = _convertRecordToLDAPAttributes (schema, ldifRecord);
|
|
|
|
|
|
|
|
newEntry = [[NGLdapEntry alloc] initWithDN: dn
|
|
|
|
attributes: attributes];
|
|
|
|
[newEntry autorelease];
|
|
|
|
[attributes release];
|
|
|
|
NS_DURING
|
|
|
|
{
|
|
|
|
[ldapConnection addEntry: newEntry];
|
2012-01-04 16:26:30 +01:00
|
|
|
result = nil;
|
2011-12-30 21:39:07 +01:00
|
|
|
}
|
|
|
|
NS_HANDLER
|
|
|
|
{
|
|
|
|
result = localException;
|
2012-03-01 22:57:16 +01:00
|
|
|
[result retain];
|
2011-12-30 21:39:07 +01:00
|
|
|
}
|
|
|
|
NS_ENDHANDLER;
|
2012-03-01 22:57:16 +01:00
|
|
|
[result autorelease];
|
2011-12-30 21:39:07 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
[self errorWithFormat: @"no value for id field '%@'", IDField];
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NSArray *
|
|
|
|
_makeLDAPChanges (NGLdapConnection *ldapConnection,
|
|
|
|
NSString *dn, NSArray *attributes)
|
|
|
|
{
|
|
|
|
NSMutableArray *changes, *attributeNames, *origAttributeNames;
|
|
|
|
NGLdapEntry *origEntry;
|
2012-03-01 22:57:16 +01:00
|
|
|
// NSArray *values;
|
2011-12-30 21:39:07 +01:00
|
|
|
NGLdapAttribute *attribute, *origAttribute;
|
|
|
|
NSString *name;
|
|
|
|
NSDictionary *origAttributes;
|
2012-03-01 22:57:16 +01:00
|
|
|
NSUInteger count, max/* , valueCount, valueMax */;
|
|
|
|
// BOOL allStrings;
|
2011-12-30 21:39:07 +01:00
|
|
|
|
|
|
|
/* additions and modifications */
|
|
|
|
origEntry = [ldapConnection entryAtDN: dn
|
|
|
|
attributes: [NSArray arrayWithObject: @"*"]];
|
|
|
|
origAttributes = [origEntry attributes];
|
|
|
|
|
|
|
|
max = [attributes count];
|
|
|
|
changes = [NSMutableArray arrayWithCapacity: max];
|
|
|
|
attributeNames = [NSMutableArray arrayWithCapacity: max];
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
{
|
|
|
|
attribute = [attributes objectAtIndex: count];
|
|
|
|
name = [attribute attributeName];
|
|
|
|
[attributeNames addObject: name];
|
|
|
|
origAttribute = [origAttributes objectForKey: name];
|
|
|
|
if (origAttribute)
|
|
|
|
{
|
|
|
|
if (![origAttribute isEqual: attribute])
|
|
|
|
[changes
|
|
|
|
addObject: [NGLdapModification replaceModification: attribute]];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
[changes addObject: [NGLdapModification addModification: attribute]];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* deletions */
|
|
|
|
origAttributeNames = [[origAttributes allKeys] mutableCopy];
|
|
|
|
[origAttributeNames autorelease];
|
|
|
|
[origAttributeNames removeObjectsInArray: attributeNames];
|
|
|
|
max = [origAttributeNames count];
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
{
|
|
|
|
name = [origAttributeNames objectAtIndex: count];
|
|
|
|
origAttribute = [origAttributes objectForKey: name];
|
|
|
|
/* the attribute must only have string values, otherwise it will anyway
|
|
|
|
be missing from the new record */
|
2012-03-01 22:57:16 +01:00
|
|
|
// allStrings = YES;
|
|
|
|
// values = [origAttribute allValues];
|
|
|
|
// valueMax = [values count];
|
|
|
|
// for (valueCount = 0; allStrings && valueCount < valueMax; valueCount++)
|
|
|
|
// if (![[values objectAtIndex: valueCount] isKindOfClass: NSStringK])
|
|
|
|
// allStrings = NO;
|
|
|
|
// if (allStrings)
|
|
|
|
[changes
|
|
|
|
addObject: [NGLdapModification deleteModification: origAttribute]];
|
2011-12-30 21:39:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return changes;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSException *) updateContactEntry: (NSDictionary *) roLdifRecord
|
|
|
|
{
|
2012-01-04 16:26:30 +01:00
|
|
|
NSException *result;
|
2011-12-30 21:39:07 +01:00
|
|
|
NSString *dn;
|
|
|
|
NSMutableDictionary *ldifRecord;
|
|
|
|
NSArray *attributes, *changes;
|
|
|
|
NGLdapConnection *ldapConnection;
|
|
|
|
|
|
|
|
dn = [roLdifRecord objectForKey: @"dn"];
|
|
|
|
if ([dn length] > 0)
|
|
|
|
{
|
|
|
|
ldapConnection = [self _ldapConnection];
|
|
|
|
ldifRecord = [roLdifRecord mutableCopy];
|
|
|
|
[ldifRecord autorelease];
|
|
|
|
[self _applyContactMappingToOutput: ldifRecord];
|
|
|
|
attributes = _convertRecordToLDAPAttributes (schema, ldifRecord);
|
|
|
|
|
|
|
|
changes = _makeLDAPChanges (ldapConnection, dn, attributes);
|
|
|
|
|
|
|
|
NS_DURING
|
|
|
|
{
|
|
|
|
[ldapConnection modifyEntryWithDN: dn
|
|
|
|
changes: changes];
|
2012-01-04 16:26:30 +01:00
|
|
|
result = nil;
|
2011-12-30 21:39:07 +01:00
|
|
|
}
|
|
|
|
NS_HANDLER
|
|
|
|
{
|
|
|
|
result = localException;
|
2012-03-01 22:57:16 +01:00
|
|
|
[result retain];
|
2011-12-30 21:39:07 +01:00
|
|
|
}
|
|
|
|
NS_ENDHANDLER;
|
2012-03-01 22:57:16 +01:00
|
|
|
[result autorelease];
|
2011-12-30 21:39:07 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
[self errorWithFormat: @"expected dn for modified record"];
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSException *) removeContactEntryWithID: (NSString *) aId
|
|
|
|
{
|
2012-01-04 16:26:30 +01:00
|
|
|
NSException *result;
|
2011-12-30 21:39:07 +01:00
|
|
|
NGLdapConnection *ldapConnection;
|
|
|
|
NSString *dn;
|
|
|
|
|
|
|
|
ldapConnection = [self _ldapConnection];
|
2012-02-06 14:55:46 +01:00
|
|
|
dn = [NSString stringWithFormat: @"%@=%@,%@", IDField,
|
|
|
|
[aId escapedForLDAPDN], baseDN];
|
2011-12-30 21:39:07 +01:00
|
|
|
NS_DURING
|
|
|
|
{
|
|
|
|
[ldapConnection removeEntryWithDN: dn];
|
2012-01-04 16:26:30 +01:00
|
|
|
result = nil;
|
2011-12-30 21:39:07 +01:00
|
|
|
}
|
|
|
|
NS_HANDLER
|
|
|
|
{
|
|
|
|
result = localException;
|
2012-03-01 22:57:16 +01:00
|
|
|
[result retain];
|
2011-12-30 21:39:07 +01:00
|
|
|
}
|
|
|
|
NS_ENDHANDLER;
|
|
|
|
|
2012-03-01 22:57:16 +01:00
|
|
|
[result autorelease];
|
|
|
|
|
2011-12-30 21:39:07 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-01-04 16:26:30 +01:00
|
|
|
/* user addressbooks */
|
|
|
|
- (BOOL) hasUserAddressBooks
|
|
|
|
{
|
|
|
|
return ([abOU length] > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *) addressBookSourcesForUser: (NSString *) user
|
|
|
|
{
|
|
|
|
NSMutableArray *sources;
|
|
|
|
NSString *abBaseDN;
|
|
|
|
NGLdapConnection *ldapConnection;
|
|
|
|
NSArray *attributes, *modifier;
|
|
|
|
NSEnumerator *entries;
|
|
|
|
NGLdapEntry *entry;
|
|
|
|
NSMutableDictionary *entryRecord;
|
|
|
|
NSDictionary *sourceRec;
|
|
|
|
LDAPSource *ab;
|
|
|
|
|
|
|
|
if ([self hasUserAddressBooks])
|
|
|
|
{
|
|
|
|
/* list subentries */
|
|
|
|
sources = [NSMutableArray array];
|
|
|
|
|
|
|
|
ldapConnection = [self _ldapConnection];
|
2012-02-06 14:55:46 +01:00
|
|
|
abBaseDN = [NSString stringWithFormat: @"ou=%@,%@=%@,%@",
|
|
|
|
[abOU escapedForLDAPDN], IDField,
|
|
|
|
[user escapedForLDAPDN], baseDN];
|
2012-01-04 16:26:30 +01:00
|
|
|
|
|
|
|
/* test ou=addressbooks entry */
|
|
|
|
attributes = [NSArray arrayWithObject: @"*"];
|
|
|
|
entries = [ldapConnection baseSearchAtBaseDN: abBaseDN
|
|
|
|
qualifier: nil
|
|
|
|
attributes: attributes];
|
|
|
|
entry = [entries nextObject];
|
|
|
|
if (entry)
|
|
|
|
{
|
|
|
|
attributes = [NSArray arrayWithObjects: @"ou", @"description", nil];
|
|
|
|
entries = [ldapConnection flatSearchAtBaseDN: abBaseDN
|
|
|
|
qualifier: nil
|
|
|
|
attributes: attributes];
|
|
|
|
modifier = [NSArray arrayWithObject: user];
|
|
|
|
while ((entry = [entries nextObject]))
|
|
|
|
{
|
|
|
|
sourceRec = [entry _asDictionary];
|
|
|
|
ab = [LDAPSource new];
|
|
|
|
[ab setSourceID: [sourceRec objectForKey: @"ou"]];
|
|
|
|
[ab setDisplayName: [sourceRec objectForKey: @"description"]];
|
|
|
|
[ab setBindDN: bindDN
|
|
|
|
password: password
|
|
|
|
hostname: hostname
|
|
|
|
port: [NSString stringWithFormat: @"%d", port]
|
|
|
|
encryption: encryption
|
|
|
|
bindAsCurrentUser: NO];
|
|
|
|
[ab setBaseDN: [entry dn]
|
|
|
|
IDField: @"cn"
|
|
|
|
CNField: @"displayName"
|
|
|
|
UIDField: @"cn"
|
|
|
|
mailFields: nil
|
|
|
|
searchFields: nil
|
|
|
|
IMAPHostField: nil
|
|
|
|
IMAPLoginField: nil
|
|
|
|
bindFields: nil
|
|
|
|
kindField: nil
|
|
|
|
andMultipleBookingsField: nil];
|
|
|
|
[ab setListRequiresDot: NO];
|
|
|
|
[ab setModifiers: modifier];
|
|
|
|
[sources addObject: ab];
|
|
|
|
[ab release];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
entryRecord = [NSMutableDictionary dictionary];
|
|
|
|
[entryRecord setObject: @"organizationalUnit" forKey: @"objectclass"];
|
|
|
|
[entryRecord setObject: @"addressbooks" forKey: @"ou"];
|
|
|
|
attributes = _convertRecordToLDAPAttributes (schema, entryRecord);
|
|
|
|
entry = [[NGLdapEntry alloc] initWithDN: abBaseDN
|
|
|
|
attributes: attributes];
|
|
|
|
[entry autorelease];
|
|
|
|
[attributes release];
|
|
|
|
NS_DURING
|
|
|
|
{
|
|
|
|
[ldapConnection addEntry: entry];
|
|
|
|
}
|
|
|
|
NS_HANDLER
|
|
|
|
{
|
|
|
|
[self errorWithFormat: @"failed to create ou=addressbooks"
|
|
|
|
@" entry for user"];
|
|
|
|
}
|
|
|
|
NS_ENDHANDLER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
sources = nil;
|
|
|
|
|
|
|
|
return sources;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSException *) addAddressBookSource: (NSString *) newId
|
|
|
|
withDisplayName: (NSString *) newDisplayName
|
|
|
|
forUser: (NSString *) user
|
|
|
|
{
|
|
|
|
NSException *result;
|
|
|
|
NSString *abDN;
|
|
|
|
NGLdapConnection *ldapConnection;
|
|
|
|
NSArray *attributes;
|
|
|
|
NGLdapEntry *entry;
|
|
|
|
NSMutableDictionary *entryRecord;
|
|
|
|
|
|
|
|
if ([self hasUserAddressBooks])
|
|
|
|
{
|
|
|
|
abDN = [NSString stringWithFormat: @"ou=%@,ou=%@,%@=%@,%@",
|
2012-02-06 14:55:46 +01:00
|
|
|
[newId escapedForLDAPDN], [abOU escapedForLDAPDN],
|
|
|
|
IDField, [user escapedForLDAPDN], baseDN];
|
2012-01-04 16:26:30 +01:00
|
|
|
entryRecord = [NSMutableDictionary dictionary];
|
|
|
|
[entryRecord setObject: @"organizationalUnit" forKey: @"objectclass"];
|
|
|
|
[entryRecord setObject: newId forKey: @"ou"];
|
|
|
|
if ([newDisplayName length] > 0)
|
|
|
|
[entryRecord setObject: newDisplayName forKey: @"description"];
|
|
|
|
ldapConnection = [self _ldapConnection];
|
|
|
|
attributes = _convertRecordToLDAPAttributes (schema, entryRecord);
|
|
|
|
entry = [[NGLdapEntry alloc] initWithDN: abDN
|
|
|
|
attributes: attributes];
|
|
|
|
[entry autorelease];
|
|
|
|
[attributes release];
|
|
|
|
NS_DURING
|
|
|
|
{
|
|
|
|
[ldapConnection addEntry: entry];
|
|
|
|
result = nil;
|
|
|
|
}
|
|
|
|
NS_HANDLER
|
|
|
|
{
|
|
|
|
[self errorWithFormat: @"failed to create addressbook entry"];
|
|
|
|
result = localException;
|
2012-03-01 22:57:16 +01:00
|
|
|
[result retain];
|
2012-01-04 16:26:30 +01:00
|
|
|
}
|
|
|
|
NS_ENDHANDLER;
|
2012-03-01 22:57:16 +01:00
|
|
|
[result autorelease];
|
2012-01-04 16:26:30 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
result = [NSException exceptionWithName: @"LDAPSourceIOException"
|
|
|
|
reason: @"user addressbooks"
|
|
|
|
@" are not supported"
|
|
|
|
userInfo: nil];
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSException *) renameAddressBookSource: (NSString *) newId
|
|
|
|
withDisplayName: (NSString *) newDisplayName
|
|
|
|
forUser: (NSString *) user
|
|
|
|
{
|
|
|
|
NSException *result;
|
|
|
|
NSString *abDN;
|
|
|
|
NGLdapConnection *ldapConnection;
|
|
|
|
NSArray *attributes, *changes;
|
|
|
|
NSMutableDictionary *entryRecord;
|
|
|
|
|
|
|
|
if ([self hasUserAddressBooks])
|
|
|
|
{
|
|
|
|
abDN = [NSString stringWithFormat: @"ou=%@,ou=%@,%@=%@,%@",
|
2012-02-06 14:55:46 +01:00
|
|
|
[newId escapedForLDAPDN], [abOU escapedForLDAPDN],
|
|
|
|
IDField, [user escapedForLDAPDN], baseDN];
|
2012-01-04 16:26:30 +01:00
|
|
|
entryRecord = [NSMutableDictionary dictionary];
|
|
|
|
[entryRecord setObject: @"organizationalUnit" forKey: @"objectclass"];
|
|
|
|
[entryRecord setObject: newId forKey: @"ou"];
|
|
|
|
if ([newDisplayName length] > 0)
|
|
|
|
[entryRecord setObject: newDisplayName forKey: @"description"];
|
|
|
|
ldapConnection = [self _ldapConnection];
|
|
|
|
attributes = _convertRecordToLDAPAttributes (schema, entryRecord);
|
|
|
|
changes = _makeLDAPChanges (ldapConnection, abDN, attributes);
|
|
|
|
[attributes release];
|
|
|
|
NS_DURING
|
|
|
|
{
|
|
|
|
[ldapConnection modifyEntryWithDN: abDN
|
|
|
|
changes: changes];
|
|
|
|
result = nil;
|
|
|
|
}
|
|
|
|
NS_HANDLER
|
|
|
|
{
|
|
|
|
[self errorWithFormat: @"failed to rename addressbook entry"];
|
|
|
|
result = localException;
|
2012-03-01 22:57:16 +01:00
|
|
|
[result retain];
|
2012-01-04 16:26:30 +01:00
|
|
|
}
|
|
|
|
NS_ENDHANDLER;
|
2012-03-01 22:57:16 +01:00
|
|
|
[result autorelease];
|
2012-01-04 16:26:30 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
result = [NSException exceptionWithName: @"LDAPSourceIOException"
|
|
|
|
reason: @"user addressbooks"
|
|
|
|
@" are not supported"
|
|
|
|
userInfo: nil];
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSException *) removeAddressBookSource: (NSString *) newId
|
|
|
|
forUser: (NSString *) user
|
|
|
|
{
|
|
|
|
NSException *result;
|
|
|
|
NSString *abDN;
|
|
|
|
NGLdapConnection *ldapConnection;
|
|
|
|
NSEnumerator *entries;
|
|
|
|
NGLdapEntry *entry;
|
|
|
|
|
|
|
|
if ([self hasUserAddressBooks])
|
|
|
|
{
|
|
|
|
abDN = [NSString stringWithFormat: @"ou=%@,ou=%@,%@=%@,%@",
|
2012-02-06 14:55:46 +01:00
|
|
|
[newId escapedForLDAPDN], [abOU escapedForLDAPDN],
|
|
|
|
IDField, [user escapedForLDAPDN], baseDN];
|
2012-01-04 16:26:30 +01:00
|
|
|
ldapConnection = [self _ldapConnection];
|
|
|
|
NS_DURING
|
|
|
|
{
|
|
|
|
/* we must remove the ab sub=entries prior to the ab entry */
|
|
|
|
entries = [ldapConnection flatSearchAtBaseDN: abDN
|
|
|
|
qualifier: nil
|
|
|
|
attributes: nil];
|
|
|
|
while ((entry = [entries nextObject]))
|
|
|
|
[ldapConnection removeEntryWithDN: [entry dn]];
|
|
|
|
[ldapConnection removeEntryWithDN: abDN];
|
|
|
|
result = nil;
|
|
|
|
}
|
|
|
|
NS_HANDLER
|
|
|
|
{
|
|
|
|
[self errorWithFormat: @"failed to remove addressbook entry"];
|
|
|
|
result = localException;
|
2012-03-01 22:57:16 +01:00
|
|
|
[result retain];
|
2012-01-04 16:26:30 +01:00
|
|
|
}
|
|
|
|
NS_ENDHANDLER;
|
2012-03-01 22:57:16 +01:00
|
|
|
[result autorelease];
|
2012-01-04 16:26:30 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
result = [NSException exceptionWithName: @"LDAPSourceIOException"
|
|
|
|
reason: @"user addressbooks"
|
|
|
|
@" are not supported"
|
|
|
|
userInfo: nil];
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-05-09 21:06:15 +02:00
|
|
|
@end
|