2009-04-30 23:17:55 +02:00
|
|
|
/* SOGoGroup.m - this file is part of SOGo
|
|
|
|
*
|
2014-02-14 22:39:26 +01:00
|
|
|
* Copyright (C) 2009-2014 Inverse inc.
|
2009-04-30 23:17:55 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
Here are some group samples:
|
|
|
|
|
|
|
|
[ POSIX group ]
|
|
|
|
|
|
|
|
dn: cn=it-staff,ou=Group,dc=zzz,dc=xxx,dc=yyy
|
|
|
|
objectClass: posixGroup
|
|
|
|
objectClass: top
|
|
|
|
cn: it-staff
|
|
|
|
userPassword: {crypt}x
|
|
|
|
gidNumber: 8000
|
|
|
|
memberUid: lsa
|
|
|
|
memberUid: mrm
|
|
|
|
memberUid: ij
|
|
|
|
memberUid: no
|
|
|
|
memberUid: ld
|
|
|
|
memberUid: db
|
|
|
|
memberUid: rgl
|
|
|
|
memberUid: ja
|
|
|
|
memberUid: hbt
|
|
|
|
memberUid: hossein
|
|
|
|
|
2012-04-18 19:39:11 +02:00
|
|
|
dn: cn=inverse,ou=groups,dc=inverse,dc=ca
|
|
|
|
objectClass: groupOfUniqueNames
|
|
|
|
objectClass: top
|
|
|
|
objectClass: extensibleObject
|
|
|
|
uniqueMember: uid=flachapelle,ou=users,dc=inverse,dc=ca
|
|
|
|
uniqueMember: uid=lmarcotte,ou=users,dc=inverse,dc=ca
|
|
|
|
uniqueMember: uid=wsourdeau,ou=users,dc=inverse,dc=ca
|
|
|
|
cn: inverse
|
|
|
|
mail: inverse@inverse.ca
|
|
|
|
|
2009-04-30 23:17:55 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SOGoGroup.h"
|
|
|
|
|
|
|
|
#import <Foundation/NSArray.h>
|
2013-03-01 22:24:16 +01:00
|
|
|
#import <Foundation/NSAutoreleasePool.h>
|
2012-05-31 14:36:00 +02:00
|
|
|
#import <Foundation/NSDictionary.h>
|
2009-04-30 23:17:55 +02:00
|
|
|
#import <Foundation/NSString.h>
|
|
|
|
|
2010-09-24 17:24:24 +02:00
|
|
|
#include "SOGoCache.h"
|
2009-11-29 05:19:32 +01:00
|
|
|
#include "SOGoSource.h"
|
2009-09-25 16:42:33 +02:00
|
|
|
#include "SOGoUserManager.h"
|
2009-04-30 23:17:55 +02:00
|
|
|
#include "SOGoUser.h"
|
|
|
|
|
|
|
|
#import <NGLdap/NGLdapConnection.h>
|
|
|
|
#import <NGLdap/NGLdapAttribute.h>
|
|
|
|
#import <NGLdap/NGLdapEntry.h>
|
|
|
|
|
2012-11-19 15:45:48 +01:00
|
|
|
#define CHECK_CLASS(o) ({ \
|
|
|
|
if ([o isKindOfClass: [NSString class]]) \
|
|
|
|
o = [NSArray arrayWithObject: o]; \
|
|
|
|
})
|
|
|
|
|
2009-04-30 23:17:55 +02:00
|
|
|
@implementation SOGoGroup
|
|
|
|
|
|
|
|
- (id) initWithIdentifier: (NSString *) theID
|
2010-09-24 17:24:24 +02:00
|
|
|
domain: (NSString *) theDomain
|
2009-11-29 05:19:32 +01:00
|
|
|
source: (NSObject <SOGoSource> *) theSource
|
2009-04-30 23:17:55 +02:00
|
|
|
entry: (NGLdapEntry *) theEntry
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
|
|
|
|
if (self)
|
|
|
|
{
|
|
|
|
ASSIGN(_identifier, theID);
|
2010-09-24 17:24:24 +02:00
|
|
|
ASSIGN(_domain, theDomain);
|
2009-04-30 23:17:55 +02:00
|
|
|
ASSIGN(_source, theSource);
|
|
|
|
ASSIGN(_entry, theEntry);
|
2009-09-29 20:17:12 +02:00
|
|
|
_members = nil;
|
2009-04-30 23:17:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
RELEASE(_identifier);
|
2010-09-24 17:24:24 +02:00
|
|
|
RELEASE(_domain);
|
2009-04-30 23:17:55 +02:00
|
|
|
RELEASE(_source);
|
|
|
|
RELEASE(_entry);
|
2009-09-29 20:17:12 +02:00
|
|
|
RELEASE(_members);
|
2009-04-30 23:17:55 +02:00
|
|
|
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2009-05-03 19:50:57 +02:00
|
|
|
+ (id) groupWithIdentifier: (NSString *) theID
|
2009-11-29 05:19:32 +01:00
|
|
|
inDomain: (NSString *) domain
|
2009-05-03 19:50:57 +02:00
|
|
|
{
|
|
|
|
NSString *uid;
|
|
|
|
|
|
|
|
uid = [theID hasPrefix: @"@"] ? [theID substringFromIndex: 1] : theID;
|
2009-09-29 20:17:12 +02:00
|
|
|
|
|
|
|
return [SOGoGroup groupWithValue: uid
|
2015-09-23 14:51:46 +02:00
|
|
|
andSourceSelector: @selector (lookupGroupEntryByUID:inDomain:)
|
2009-11-29 05:19:32 +01:00
|
|
|
inDomain: domain];
|
2009-05-03 19:50:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (id) groupWithEmail: (NSString *) theEmail
|
2009-11-29 05:19:32 +01:00
|
|
|
inDomain: (NSString *) domain
|
2009-05-03 19:50:57 +02:00
|
|
|
{
|
2009-09-29 20:17:12 +02:00
|
|
|
return [SOGoGroup groupWithValue: theEmail
|
2015-09-23 14:51:46 +02:00
|
|
|
andSourceSelector: @selector (lookupGroupEntryByEmail:inDomain:)
|
2009-11-29 05:19:32 +01:00
|
|
|
inDomain: domain];
|
2009-05-03 19:50:57 +02:00
|
|
|
}
|
|
|
|
|
2009-04-30 23:17:55 +02:00
|
|
|
//
|
2009-05-03 19:50:57 +02:00
|
|
|
// Returns nil if theValue doesn't match to a group
|
|
|
|
// (so its objectClass isn't a group)
|
2009-04-30 23:17:55 +02:00
|
|
|
//
|
2009-05-03 19:50:57 +02:00
|
|
|
+ (id) groupWithValue: (NSString *) theValue
|
|
|
|
andSourceSelector: (SEL) theSelector
|
2009-11-29 05:19:32 +01:00
|
|
|
inDomain: (NSString *) domain
|
2009-04-30 23:17:55 +02:00
|
|
|
{
|
|
|
|
NSArray *allSources;
|
|
|
|
NGLdapEntry *entry;
|
2015-11-05 18:29:26 +01:00
|
|
|
NSObject <SOGoSource, SOGoDNSource> *source;
|
2009-04-30 23:17:55 +02:00
|
|
|
id o;
|
2015-03-24 13:40:13 +01:00
|
|
|
NSEnumerator *gclasses;
|
|
|
|
NSString *gclass;
|
2009-04-30 23:17:55 +02:00
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
// Don't bother looking in all sources if the
|
2009-05-03 19:50:57 +02:00
|
|
|
// supplied value is nil.
|
|
|
|
if (!theValue)
|
2009-04-30 23:17:55 +02:00
|
|
|
return nil;
|
|
|
|
|
2009-11-29 05:19:32 +01:00
|
|
|
allSources = [[SOGoUserManager sharedUserManager]
|
|
|
|
sourceIDsInDomain: domain];
|
2009-11-24 21:19:21 +01:00
|
|
|
entry = nil;
|
2009-04-30 23:17:55 +02:00
|
|
|
o = nil;
|
|
|
|
|
|
|
|
for (i = 0; i < [allSources count]; i++)
|
|
|
|
{
|
2015-11-05 18:29:26 +01:00
|
|
|
source = (NSObject <SOGoSource, SOGoDNSource> *) [[SOGoUserManager sharedUserManager] sourceWithID: [allSources objectAtIndex: i]];
|
2009-04-30 23:17:55 +02:00
|
|
|
|
2009-10-07 22:46:16 +02:00
|
|
|
// Our different sources might not all implements groups support
|
|
|
|
if ([source respondsToSelector: theSelector])
|
2015-09-23 14:51:46 +02:00
|
|
|
entry = [source performSelector: theSelector
|
|
|
|
withObject: theValue
|
|
|
|
withObject: domain];
|
2009-04-30 23:17:55 +02:00
|
|
|
if (entry)
|
|
|
|
break;
|
|
|
|
|
|
|
|
entry = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry)
|
|
|
|
{
|
|
|
|
NSArray *classes;
|
2012-05-31 14:36:00 +02:00
|
|
|
|
2009-04-30 23:17:55 +02:00
|
|
|
// We check to see if it's a group
|
2012-05-31 14:36:00 +02:00
|
|
|
classes = [[entry asDictionary] objectForKey: @"objectclass"];
|
|
|
|
|
|
|
|
if (classes)
|
|
|
|
{
|
2012-06-12 16:41:51 +02:00
|
|
|
/* LDAP records returned as dictionaries may contain NSString or
|
|
|
|
NSArray values, depending on whether the amount of values
|
|
|
|
assigned to a key is 1 or more. Since this can occur with
|
|
|
|
"objectclass" too, we need to check whether "classes" is actually
|
|
|
|
an NSString instance... */
|
|
|
|
if ([classes isKindOfClass: [NSString class]])
|
|
|
|
classes = [NSArray arrayWithObject:
|
|
|
|
[(NSString *) classes lowercaseString]];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int i, c;
|
2012-05-31 14:36:00 +02:00
|
|
|
|
2012-06-12 16:41:51 +02:00
|
|
|
classes = [NSMutableArray arrayWithArray: classes];
|
|
|
|
c = [classes count];
|
|
|
|
for (i = 0; i < c; i++)
|
|
|
|
[(id)classes replaceObjectAtIndex: i
|
|
|
|
withObject: [[classes objectAtIndex: i] lowercaseString]];
|
|
|
|
}
|
2012-05-31 14:36:00 +02:00
|
|
|
}
|
2009-04-30 23:17:55 +02:00
|
|
|
|
2015-03-24 13:40:13 +01:00
|
|
|
gclasses = [[source groupObjectClasses] objectEnumerator];
|
|
|
|
while (gclass = [gclasses nextObject])
|
|
|
|
if ([classes containsObject: gclass])
|
|
|
|
{
|
|
|
|
// Found a group, let's return it.
|
2009-05-03 19:50:57 +02:00
|
|
|
o = [[self alloc] initWithIdentifier: theValue
|
2010-09-24 17:24:24 +02:00
|
|
|
domain: domain
|
2009-11-29 05:19:32 +01:00
|
|
|
source: source
|
|
|
|
entry: entry];
|
2009-04-30 23:17:55 +02:00
|
|
|
AUTORELEASE(o);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// This method actually try to obtain all members
|
|
|
|
// from either dynamic of static groups.
|
|
|
|
//
|
|
|
|
- (NSArray *) members
|
|
|
|
{
|
2010-09-24 17:24:24 +02:00
|
|
|
NSMutableArray *dns, *uids, *logins;
|
2009-05-03 19:50:57 +02:00
|
|
|
NSString *dn, *login;
|
2012-05-31 14:36:00 +02:00
|
|
|
SOGoUserManager *um;
|
|
|
|
NSDictionary *d;
|
2009-04-30 23:17:55 +02:00
|
|
|
SOGoUser *user;
|
|
|
|
NSArray *o;
|
2013-03-01 22:24:16 +01:00
|
|
|
NSAutoreleasePool *pool;
|
2009-04-30 23:17:55 +02:00
|
|
|
int i, c;
|
|
|
|
|
2009-09-29 20:17:12 +02:00
|
|
|
if (!_members)
|
|
|
|
{
|
|
|
|
_members = [NSMutableArray new];
|
|
|
|
uids = [NSMutableArray array];
|
|
|
|
dns = [NSMutableArray array];
|
2010-09-24 17:24:24 +02:00
|
|
|
logins = [NSMutableArray array];
|
2009-04-30 23:17:55 +02:00
|
|
|
|
2010-09-24 17:24:24 +02:00
|
|
|
// We check if it's a static group
|
2009-09-29 20:17:12 +02:00
|
|
|
// Fetch "members" - we get DNs
|
2012-05-31 14:36:00 +02:00
|
|
|
d = [_entry asDictionary];
|
|
|
|
o = [d objectForKey: @"member"];
|
2012-11-19 15:45:48 +01:00
|
|
|
CHECK_CLASS(o);
|
2009-09-29 20:17:12 +02:00
|
|
|
if (o) [dns addObjectsFromArray: o];
|
2009-04-30 23:17:55 +02:00
|
|
|
|
2009-09-29 20:17:12 +02:00
|
|
|
// Fetch "uniqueMembers" - we get DNs
|
2012-05-31 14:36:00 +02:00
|
|
|
o = [d objectForKey: @"uniquemember"];
|
2012-11-19 15:45:48 +01:00
|
|
|
CHECK_CLASS(o);
|
2009-09-29 20:17:12 +02:00
|
|
|
if (o) [dns addObjectsFromArray: o];
|
2009-04-30 23:17:55 +02:00
|
|
|
|
2009-09-29 20:17:12 +02:00
|
|
|
// Fetch "memberUid" - we get UID (like login names)
|
2012-05-31 14:36:00 +02:00
|
|
|
o = [d objectForKey: @"memberuid"];
|
2012-11-19 15:45:48 +01:00
|
|
|
CHECK_CLASS(o);
|
2009-09-29 20:17:12 +02:00
|
|
|
if (o) [uids addObjectsFromArray: o];
|
|
|
|
|
|
|
|
c = [dns count] + [uids count];
|
|
|
|
|
|
|
|
// We deal with a static group, let's add the members
|
|
|
|
if (c)
|
|
|
|
{
|
|
|
|
um = [SOGoUserManager sharedUserManager];
|
|
|
|
|
|
|
|
// We add members for whom we have their associated DN
|
|
|
|
for (i = 0; i < [dns count]; i++)
|
2013-03-01 22:24:16 +01:00
|
|
|
{
|
|
|
|
pool = [NSAutoreleasePool new];
|
2009-09-29 20:17:12 +02:00
|
|
|
dn = [dns objectAtIndex: i];
|
|
|
|
login = [um getLoginForDN: [dn lowercaseString]];
|
|
|
|
user = [SOGoUser userWithLogin: login roles: nil];
|
|
|
|
if (user)
|
2013-03-01 07:26:15 +01:00
|
|
|
{
|
|
|
|
[logins addObject: login];
|
|
|
|
[_members addObject: user];
|
|
|
|
}
|
2013-03-01 22:24:16 +01:00
|
|
|
[pool release];
|
2009-09-29 20:17:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// We add members for whom we have their associated login name
|
|
|
|
for (i = 0; i < [uids count]; i++)
|
|
|
|
{
|
2013-03-01 22:24:16 +01:00
|
|
|
pool = [NSAutoreleasePool new];
|
2009-09-29 20:17:12 +02:00
|
|
|
login = [uids objectAtIndex: i];
|
|
|
|
user = [SOGoUser userWithLogin: login roles: nil];
|
|
|
|
|
|
|
|
if (user)
|
2013-03-01 07:26:15 +01:00
|
|
|
{
|
|
|
|
[logins addObject: login];
|
|
|
|
[_members addObject: user];
|
|
|
|
}
|
2013-03-01 22:24:16 +01:00
|
|
|
[pool release];
|
2009-09-29 20:17:12 +02:00
|
|
|
}
|
2010-09-24 17:24:24 +02:00
|
|
|
|
|
|
|
|
2013-03-01 07:26:15 +01:00
|
|
|
// We are done fetching members, let's cache the members of the group
|
|
|
|
// (ie., their UIDs) in memcached to speed up -hasMemberWithUID.
|
|
|
|
[[SOGoCache sharedCache] setValue: [logins componentsJoinedByString: @","]
|
|
|
|
forKey: [NSString stringWithFormat: @"%@+%@", _identifier, _domain]];
|
2009-09-29 20:17:12 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We deal with a dynamic group, let's search all users for whom
|
|
|
|
// memberOf is equal to our group's DN.
|
|
|
|
// We also need to look for labelelURI?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return _members;
|
|
|
|
}
|
2009-04-30 23:17:55 +02:00
|
|
|
|
2010-09-24 17:24:24 +02:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2009-09-29 20:17:12 +02:00
|
|
|
- (BOOL) hasMemberWithUID: (NSString *) memberUID
|
|
|
|
{
|
2010-09-24 17:24:24 +02:00
|
|
|
|
2009-09-29 20:17:12 +02:00
|
|
|
BOOL rc;
|
2009-05-03 19:50:57 +02:00
|
|
|
|
2009-09-29 20:17:12 +02:00
|
|
|
rc = NO;
|
2009-04-30 23:17:55 +02:00
|
|
|
|
2010-09-24 17:24:24 +02:00
|
|
|
// If _members is initialized, we use it as it's very accurate.
|
|
|
|
// Otherwise, we fallback on memcached in order to avoid
|
|
|
|
// decomposing the group all the time just to see if a user
|
|
|
|
// is a member of it.
|
|
|
|
if (_members)
|
2009-04-30 23:17:55 +02:00
|
|
|
{
|
2010-09-24 17:24:24 +02:00
|
|
|
NSString *currentUID;
|
|
|
|
|
|
|
|
int count, max;
|
|
|
|
max = [_members count];
|
|
|
|
for (count = 0; !rc && count < max; count++)
|
|
|
|
{
|
|
|
|
currentUID = [[_members objectAtIndex: count] login];
|
|
|
|
rc = [memberUID isEqualToString: currentUID];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NSString *key, *value;;
|
|
|
|
NSArray *a;
|
|
|
|
|
|
|
|
key = [NSString stringWithFormat: @"%@+%@", _identifier, _domain];
|
|
|
|
value = [[SOGoCache sharedCache] valueForKey: key];
|
|
|
|
|
|
|
|
|
|
|
|
// If the value isn't in memcached, that probably means -members was never called.
|
|
|
|
// We call it only once here.
|
|
|
|
if (!value)
|
|
|
|
{
|
|
|
|
[self members];
|
|
|
|
value = [[SOGoCache sharedCache] valueForKey: key];
|
|
|
|
}
|
|
|
|
|
|
|
|
a = [value componentsSeparatedByString: @","];
|
|
|
|
rc = [a containsObject: memberUID];
|
2009-04-30 23:17:55 +02:00
|
|
|
}
|
2009-09-29 20:17:12 +02:00
|
|
|
|
|
|
|
return rc;
|
2009-04-30 23:17:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|