2009-09-25 16:42:33 +02:00
|
|
|
/* SQLSource.h - this file is part of SOGo
|
|
|
|
*
|
2010-03-08 16:18:05 +01:00
|
|
|
* Copyright (C) 2009-2010 Inverse inc.
|
2009-09-25 16:42:33 +02:00
|
|
|
*
|
|
|
|
* Author: Ludovic Marcotte <lmarcotte@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.
|
|
|
|
*/
|
|
|
|
|
2010-08-12 20:29:13 +02:00
|
|
|
#define _XOPEN_SOURCE 1
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/md5.h>
|
|
|
|
|
2009-09-25 16:42:33 +02:00
|
|
|
#import <Foundation/NSArray.h>
|
|
|
|
#import <Foundation/NSObject.h>
|
|
|
|
#import <Foundation/NSDictionary.h>
|
|
|
|
#import <Foundation/NSString.h>
|
|
|
|
#import <Foundation/NSValue.h>
|
|
|
|
#import <Foundation/NSURL.h>
|
|
|
|
|
|
|
|
#import <NGExtensions/NSObject+Logs.h>
|
|
|
|
|
|
|
|
#import <GDLContentStore/GCSChannelManager.h>
|
|
|
|
#import <GDLContentStore/NSURL+GCS.h>
|
|
|
|
#import <GDLAccess/EOAdaptorChannel.h>
|
|
|
|
#import <GDLAccess/EOAdaptorContext.h>
|
|
|
|
#import <GDLAccess/EOAttribute.h>
|
|
|
|
|
2010-08-12 20:29:13 +02:00
|
|
|
#import "SOGoConstants.h"
|
2009-09-25 16:42:33 +02:00
|
|
|
|
2010-08-12 20:29:13 +02:00
|
|
|
#import "SQLSource.h"
|
2010-03-08 16:18:05 +01:00
|
|
|
|
2009-09-25 16:42:33 +02:00
|
|
|
/**
|
|
|
|
* The view MUST contain the following columns:
|
|
|
|
*
|
|
|
|
* c_uid - will be used for authentication - it's a username or username@domain.tld)
|
|
|
|
* c_name - which can be identical to c_uid - will be used to uniquely identify entries)
|
|
|
|
* c_password - password of the user, plain-text or md5 encoded for now
|
|
|
|
* c_cn - the user's common name
|
2010-09-24 17:24:24 +02:00
|
|
|
* mail - the user's mail address
|
2009-09-25 16:42:33 +02:00
|
|
|
*
|
|
|
|
* Other columns can be defined - see LDAPSource.m for the complete list.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* A SQL source can be defined like this:
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* id = zot;
|
|
|
|
* type = sql;
|
|
|
|
* viewURL = "http://sogo:sogo@127.0.0.1:5432/sogo/sogo_view";
|
|
|
|
* canAuthenticate = YES;
|
|
|
|
* isAddressBook = YES;
|
|
|
|
* userPasswordAlgorithm = md5;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
@implementation SQLSource
|
|
|
|
|
|
|
|
+ (id) sourceFromUDSource: (NSDictionary *) udSource
|
2009-11-29 05:19:32 +01:00
|
|
|
inDomain: (NSString *) domain
|
2009-09-25 16:42:33 +02:00
|
|
|
{
|
2009-11-29 05:19:32 +01:00
|
|
|
return [[[self alloc] initFromUDSource: udSource
|
|
|
|
inDomain: domain] autorelease];
|
2009-09-25 16:42:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
|
|
|
_sourceID = nil;
|
|
|
|
_mailFields = nil;
|
|
|
|
_userPasswordAlgorithm = nil;
|
|
|
|
_viewURL = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[_sourceID release];
|
|
|
|
[_mailFields release];
|
|
|
|
[_userPasswordAlgorithm release];
|
|
|
|
[_viewURL release];
|
|
|
|
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initFromUDSource: (NSDictionary *) udSource
|
2009-11-29 05:19:32 +01:00
|
|
|
inDomain: (NSString *) sourceDomain
|
2009-09-25 16:42:33 +02:00
|
|
|
{
|
|
|
|
self = [self init];
|
|
|
|
|
|
|
|
ASSIGN(_sourceID, [udSource objectForKey: @"id"]);
|
|
|
|
ASSIGN(_mailFields, [udSource objectForKey: @"MailFieldNames"]);
|
|
|
|
ASSIGN(_userPasswordAlgorithm, [udSource objectForKey: @"userPasswordAlgorithm"]);
|
|
|
|
|
|
|
|
if (!_userPasswordAlgorithm)
|
|
|
|
_userPasswordAlgorithm = @"none";
|
|
|
|
|
|
|
|
if ([udSource objectForKey: @"viewURL"])
|
|
|
|
_viewURL = [[NSURL alloc] initWithString: [udSource objectForKey: @"viewURL"]];
|
2009-11-29 05:19:32 +01:00
|
|
|
|
|
|
|
#warning this domain code has no effect yet
|
|
|
|
if ([sourceDomain length])
|
2009-11-29 22:44:04 +01:00
|
|
|
ASSIGN (_domain, sourceDomain);
|
2009-11-29 05:19:32 +01:00
|
|
|
|
2009-09-25 16:42:33 +02:00
|
|
|
if (!_viewURL)
|
|
|
|
{
|
|
|
|
[self autorelease];
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2009-11-29 05:19:32 +01:00
|
|
|
- (NSString *) domain
|
|
|
|
{
|
|
|
|
return _domain;
|
|
|
|
}
|
|
|
|
|
2009-09-25 16:42:33 +02:00
|
|
|
- (BOOL) _isPassword: (NSString *) plainPassword
|
|
|
|
equalTo: (NSString *) encryptedPassword
|
|
|
|
{
|
|
|
|
if ([_userPasswordAlgorithm caseInsensitiveCompare: @"none"] == NSOrderedSame)
|
|
|
|
{
|
|
|
|
return [plainPassword isEqualToString: encryptedPassword];
|
|
|
|
}
|
2010-08-03 16:49:28 +02:00
|
|
|
else if ([_userPasswordAlgorithm caseInsensitiveCompare: @"crypt"] == NSOrderedSame)
|
|
|
|
{
|
|
|
|
NSString *s;
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
buf = (char *)crypt([plainPassword UTF8String], [encryptedPassword UTF8String]);
|
|
|
|
s = [NSString stringWithUTF8String: buf];
|
|
|
|
|
|
|
|
return [s isEqualToString: encryptedPassword];
|
|
|
|
}
|
2009-09-25 16:42:33 +02:00
|
|
|
else if ([_userPasswordAlgorithm caseInsensitiveCompare: @"md5"] == NSOrderedSame)
|
|
|
|
{
|
|
|
|
NSString *s;
|
|
|
|
|
|
|
|
unsigned char md[MD5_DIGEST_LENGTH];
|
|
|
|
char buf[80];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
memset(md, 0, MD5_DIGEST_LENGTH);
|
|
|
|
memset(buf, 0, 80);
|
|
|
|
|
2010-04-20 23:30:17 +02:00
|
|
|
EVP_Digest((const void *) [plainPassword UTF8String], strlen([plainPassword UTF8String]), md, NULL, EVP_md5(), NULL);
|
2009-09-25 16:42:33 +02:00
|
|
|
for (i = 0; i < MD5_DIGEST_LENGTH; i++)
|
|
|
|
sprintf(&(buf[i*2]), "%02x", md[i]);
|
|
|
|
|
|
|
|
s = [NSString stringWithUTF8String: buf];
|
|
|
|
return [s isEqualToString: encryptedPassword];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[self errorWithFormat: @"Unsupported user-password algorithm: %@", _userPasswordAlgorithm];
|
|
|
|
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2010-03-08 16:18:05 +01:00
|
|
|
//
|
|
|
|
// SQL sources don't support right now all the password policy
|
|
|
|
// stuff supported by OpenLDAP (and others). If we want to support
|
|
|
|
// this for SQL sources, we'll have to implement the same
|
|
|
|
// kind of logic in this module.
|
|
|
|
//
|
|
|
|
- (BOOL) checkLogin: (NSString *) _login
|
|
|
|
password: (NSString *) _pwd
|
|
|
|
perr: (SOGoPasswordPolicyError *) _perr
|
|
|
|
expire: (int *) _expire
|
|
|
|
grace: (int *) _grace
|
2009-09-25 16:42:33 +02:00
|
|
|
{
|
|
|
|
EOAdaptorChannel *channel;
|
|
|
|
GCSChannelManager *cm;
|
|
|
|
NSException *ex;
|
|
|
|
NSString *sql;
|
2009-11-19 21:02:45 +01:00
|
|
|
BOOL rc;
|
|
|
|
|
|
|
|
rc = NO;
|
2009-09-25 16:42:33 +02:00
|
|
|
|
2010-03-25 02:20:06 +01:00
|
|
|
_login = [_login stringByReplacingString: @"'" withString: @"''"];
|
2009-09-25 16:42:33 +02:00
|
|
|
cm = [GCSChannelManager defaultChannelManager];
|
|
|
|
channel = [cm acquireOpenChannelForURL: _viewURL];
|
2009-11-19 21:02:45 +01:00
|
|
|
if (channel)
|
|
|
|
{
|
|
|
|
sql = [NSString stringWithFormat: (@"SELECT c_password"
|
|
|
|
@" FROM %@"
|
|
|
|
@" WHERE c_uid = '%@'"),
|
2010-03-08 16:18:05 +01:00
|
|
|
[_viewURL gcsTableName], _login];
|
2009-11-19 21:02:45 +01:00
|
|
|
|
|
|
|
ex = [channel evaluateExpressionX: sql];
|
|
|
|
if (!ex)
|
|
|
|
{
|
|
|
|
NSDictionary *row;
|
|
|
|
NSArray *attrs;
|
|
|
|
NSString *value;
|
|
|
|
|
|
|
|
attrs = [channel describeResults: NO];
|
|
|
|
row = [channel fetchAttributes: attrs withZone: NULL];
|
|
|
|
value = [row objectForKey: @"c_password"];
|
2009-09-25 16:42:33 +02:00
|
|
|
|
2010-03-08 16:18:05 +01:00
|
|
|
rc = [self _isPassword: _pwd equalTo: value];
|
2009-11-24 21:19:21 +01:00
|
|
|
[channel cancelFetch];
|
2009-11-19 21:02:45 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
[self errorWithFormat: @"could not run SQL '%@': %@", sql, ex];
|
|
|
|
[cm releaseChannel: channel];
|
2009-09-25 16:42:33 +02:00
|
|
|
}
|
|
|
|
else
|
2009-11-19 21:02:45 +01:00
|
|
|
[self errorWithFormat:@"failed to acquire channel for URL: %@",
|
|
|
|
[_viewURL absoluteString]];
|
2009-09-25 16:42:33 +02:00
|
|
|
|
2009-11-19 21:02:45 +01:00
|
|
|
return rc;
|
2009-09-25 16:42:33 +02:00
|
|
|
}
|
|
|
|
|
2010-03-08 16:18:05 +01:00
|
|
|
- (BOOL) changePasswordForLogin: (NSString *) login
|
|
|
|
oldPassword: (NSString *) oldPassword
|
|
|
|
newPassword: (NSString *) newPassword
|
|
|
|
perr: (SOGoPasswordPolicyError *) perr
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2009-09-25 16:42:33 +02:00
|
|
|
- (NSDictionary *) _lookupContactEntry: (NSString *) theID
|
|
|
|
considerEmail: (BOOL) b
|
|
|
|
{
|
|
|
|
EOAdaptorChannel *channel;
|
|
|
|
GCSChannelManager *cm;
|
|
|
|
NSException *ex;
|
|
|
|
NSString *sql;
|
2009-11-19 21:02:45 +01:00
|
|
|
NSMutableDictionary *response;
|
|
|
|
|
|
|
|
response = nil;
|
2009-09-25 16:42:33 +02:00
|
|
|
|
2010-03-25 02:20:06 +01:00
|
|
|
theID = [theID stringByReplacingString: @"'" withString: @"''"];
|
2009-09-25 16:42:33 +02:00
|
|
|
cm = [GCSChannelManager defaultChannelManager];
|
|
|
|
channel = [cm acquireOpenChannelForURL: _viewURL];
|
2009-11-19 21:02:45 +01:00
|
|
|
if (channel)
|
2009-09-25 16:42:33 +02:00
|
|
|
{
|
2009-11-19 21:02:45 +01:00
|
|
|
if (!b)
|
|
|
|
sql = [NSString stringWithFormat: (@"SELECT *"
|
|
|
|
@" FROM %@"
|
|
|
|
@" WHERE c_uid = '%@'"),
|
|
|
|
[_viewURL gcsTableName], theID];
|
|
|
|
else
|
|
|
|
sql = [NSString stringWithFormat: (@"SELECT *"
|
|
|
|
@" FROM %@"
|
|
|
|
@" WHERE c_uid = '%@' OR"
|
|
|
|
@" LOWER(mail) = '%@'"),
|
|
|
|
[_viewURL gcsTableName], theID, [theID lowercaseString]];
|
|
|
|
|
|
|
|
ex = [channel evaluateExpressionX: sql];
|
|
|
|
if (!ex)
|
|
|
|
{
|
|
|
|
response = [[channel fetchAttributes: [channel describeResults: NO]
|
|
|
|
withZone: NULL] mutableCopy];
|
|
|
|
[response autorelease];
|
2009-11-24 21:19:21 +01:00
|
|
|
[channel cancelFetch];
|
2009-11-19 21:02:45 +01:00
|
|
|
|
|
|
|
// We have to do this here since we do not manage modules
|
|
|
|
// constraints right now over a SQL backend.
|
|
|
|
[response setObject: [NSNumber numberWithBool: YES] forKey: @"CalendarAccess"];
|
|
|
|
[response setObject: [NSNumber numberWithBool: YES] forKey: @"MailAccess"];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
[self errorWithFormat: @"could not run SQL '%@': %@", sql, ex];
|
|
|
|
[cm releaseChannel: channel];
|
2009-09-25 16:42:33 +02:00
|
|
|
}
|
|
|
|
else
|
2009-11-19 21:02:45 +01:00
|
|
|
[self errorWithFormat:@"failed to acquire channel for URL: %@",
|
|
|
|
[_viewURL absoluteString]];
|
2009-09-25 16:42:33 +02:00
|
|
|
|
2009-11-19 21:02:45 +01:00
|
|
|
return response;
|
2009-09-25 16:42:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (NSDictionary *) lookupContactEntry: (NSString *) theID
|
|
|
|
{
|
|
|
|
return [self _lookupContactEntry: theID considerEmail: NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDictionary *) lookupContactEntryWithUIDorEmail: (NSString *) entryID
|
|
|
|
{
|
|
|
|
return [self _lookupContactEntry: entryID considerEmail: YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *) allEntryIDs
|
|
|
|
{
|
|
|
|
EOAdaptorChannel *channel;
|
|
|
|
NSMutableArray *results;
|
|
|
|
GCSChannelManager *cm;
|
|
|
|
NSException *ex;
|
|
|
|
NSString *sql;
|
|
|
|
|
|
|
|
results = [NSMutableArray array];
|
|
|
|
|
|
|
|
cm = [GCSChannelManager defaultChannelManager];
|
|
|
|
channel = [cm acquireOpenChannelForURL: _viewURL];
|
2009-11-19 21:02:45 +01:00
|
|
|
if (channel)
|
2009-09-25 16:42:33 +02:00
|
|
|
{
|
2009-11-19 21:02:45 +01:00
|
|
|
sql = [NSString stringWithFormat: @"SELECT c_uid FROM %@",
|
|
|
|
[_viewURL gcsTableName]];
|
|
|
|
|
|
|
|
ex = [channel evaluateExpressionX: sql];
|
|
|
|
if (!ex)
|
|
|
|
{
|
|
|
|
NSDictionary *row;
|
|
|
|
NSArray *attrs;
|
|
|
|
NSString *value;
|
|
|
|
|
|
|
|
attrs = [channel describeResults: NO];
|
|
|
|
|
|
|
|
while ((row = [channel fetchAttributes: attrs withZone: NULL]))
|
|
|
|
{
|
|
|
|
value = [row objectForKey: @"c_uid"];
|
|
|
|
if (value)
|
|
|
|
[results addObject: value];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
[self errorWithFormat: @"could not run SQL '%@': %@", sql, ex];
|
|
|
|
[cm releaseChannel: channel];
|
2009-09-25 16:42:33 +02:00
|
|
|
}
|
|
|
|
else
|
2009-11-19 21:02:45 +01:00
|
|
|
[self errorWithFormat:@"failed to acquire channel for URL: %@",
|
|
|
|
[_viewURL absoluteString]];
|
|
|
|
|
2009-09-25 16:42:33 +02:00
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *) fetchContactsMatching: (NSString *) filter
|
|
|
|
{
|
|
|
|
EOAdaptorChannel *channel;
|
|
|
|
NSMutableArray *results;
|
|
|
|
GCSChannelManager *cm;
|
|
|
|
NSException *ex;
|
2009-11-19 21:02:45 +01:00
|
|
|
NSString *sql, *lowerFilter;
|
2009-09-25 16:42:33 +02:00
|
|
|
|
|
|
|
results = [NSMutableArray array];
|
|
|
|
|
|
|
|
cm = [GCSChannelManager defaultChannelManager];
|
|
|
|
channel = [cm acquireOpenChannelForURL: _viewURL];
|
2009-11-19 21:02:45 +01:00
|
|
|
if (channel)
|
2009-09-25 16:42:33 +02:00
|
|
|
{
|
2009-11-19 21:02:45 +01:00
|
|
|
lowerFilter = [filter lowercaseString];
|
2010-03-25 02:20:06 +01:00
|
|
|
lowerFilter = [lowerFilter stringByReplacingString: @"'" withString: @"''"];
|
|
|
|
|
2009-11-19 21:02:45 +01:00
|
|
|
sql = [NSString stringWithFormat: (@"SELECT *"
|
|
|
|
@" FROM %@"
|
|
|
|
@" WHERE LOWER(c_cn) LIKE '%%%@%%'"
|
|
|
|
@" OR LOWER(mail) LIKE '%%%@%%'"),
|
|
|
|
[_viewURL gcsTableName],
|
|
|
|
lowerFilter, lowerFilter];
|
|
|
|
|
|
|
|
ex = [channel evaluateExpressionX: sql];
|
|
|
|
if (!ex)
|
|
|
|
{
|
|
|
|
NSDictionary *row;
|
|
|
|
NSArray *attrs;
|
|
|
|
|
|
|
|
attrs = [channel describeResults: NO];
|
|
|
|
|
|
|
|
while ((row = [channel fetchAttributes: attrs withZone: NULL]))
|
|
|
|
[results addObject: row];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
[self errorWithFormat: @"could not run SQL '%@': %@", sql, ex];
|
|
|
|
[cm releaseChannel: channel];
|
2009-09-25 16:42:33 +02:00
|
|
|
}
|
|
|
|
else
|
2009-11-19 21:02:45 +01:00
|
|
|
[self errorWithFormat:@"failed to acquire channel for URL: %@",
|
|
|
|
[_viewURL absoluteString]];
|
2009-09-25 16:42:33 +02:00
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) sourceID
|
|
|
|
{
|
|
|
|
return _sourceID;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|