]> err.no Git - scalable-opengroupware.org/blob - SoObjects/SOGo/LDAPUserManager.m
be9ed79dcff309740b95c773952c384427a19e45
[scalable-opengroupware.org] / SoObjects / SOGo / LDAPUserManager.m
1 /* LDAPUserManager.m - this file is part of SOGo
2  *
3  * Copyright (C) 2007 Inverse groupe conseil
4  *
5  * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
6  *
7  * This file is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This file is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #import <Foundation/NSArray.h>
24 #import <Foundation/NSDictionary.h>
25 #import <Foundation/NSString.h>
26 #import <Foundation/NSTimer.h>
27 #import <Foundation/NSUserDefaults.h>
28 #import <Foundation/NSValue.h>
29
30 #import "LDAPSource.h"
31 #import "LDAPUserManager.h"
32
33 static NSString *defaultMailDomain = nil;
34
35 @implementation LDAPUserManager
36
37 + (void) initialize
38 {
39   NSUserDefaults *ud;
40
41   ud = [NSUserDefaults standardUserDefaults];
42   if (!defaultMailDomain)
43     {
44       defaultMailDomain = [ud stringForKey: @"SOGoDefaultMailDomain"];
45       [defaultMailDomain retain];
46     }
47 }
48
49 + (id) sharedUserManager
50 {
51   static id sharedUserManager = nil;
52
53   if (!sharedUserManager)
54     sharedUserManager = [self new];
55
56   return sharedUserManager;
57 }
58
59 - (void) _registerSource: (NSDictionary *) udSource
60 {
61   NSMutableDictionary *metadata;
62   LDAPSource *ldapSource;
63   NSString *sourceID, *value;
64   
65   sourceID = [udSource objectForKey: @"id"];
66   ldapSource = [LDAPSource sourceFromUDSource: udSource];
67   [sources setObject: ldapSource forKey: sourceID];
68   metadata = [NSMutableDictionary dictionary];
69   value = [udSource objectForKey: @"canAuthenticate"];
70   if (value)
71     [metadata setObject: value forKey: @"canAuthenticate"];
72   value = [udSource objectForKey: @"isAddressBook"];
73   if (value)
74     [metadata setObject: value forKey: @"isAddressBook"];
75   value = [udSource objectForKey: @"displayName"];
76   if (value)
77     [metadata setObject: value forKey: @"displayName"];
78   value = [udSource objectForKey: @"MailFieldNames"];
79   if (value)
80     [metadata setObject: value forKey: @"MailFieldNames"];
81   [sourcesMetadata setObject: metadata forKey: sourceID];
82 }
83
84 - (void) _prepareLDAPSourcesWithDefaults: (NSUserDefaults *) ud
85 {
86   NSArray *udSources;
87   unsigned int count, max;
88
89   sources = [NSMutableDictionary new];
90   sourcesMetadata = [NSMutableDictionary new];
91
92   udSources = [ud arrayForKey: @"SOGoLDAPSources"];
93   max = [udSources count];
94   for (count = 0; count < max; count++)
95     [self _registerSource: [udSources objectAtIndex: count]];
96 }
97
98 - (id) init
99 {
100   NSUserDefaults *ud;
101
102   if ((self = [super init]))
103     {
104       ud = [NSUserDefaults standardUserDefaults];
105
106       sources = nil;
107       sourcesMetadata = nil;
108       users = [NSMutableDictionary new];
109       cleanupInterval
110         = [ud integerForKey: @"SOGOLDAPUserManagerCleanupInterval"];
111       if (cleanupInterval)
112         cleanupTimer = [NSTimer timerWithTimeInterval: cleanupInterval
113                                 target: self
114                                 selector: @selector (cleanupUsers)
115                                 userInfo: nil
116                                 repeats: YES];
117       [self _prepareLDAPSourcesWithDefaults: ud];
118     }
119
120   return self;
121 }
122
123 - (void) dealloc
124 {
125   [sources release];
126   [users release];
127   [super dealloc];
128 }
129
130 - (NSArray *) sourceIDs
131 {
132   return [sources allKeys];
133 }
134
135 - (NSArray *) _sourcesOfType: (NSString *) sourceType
136 {
137   NSMutableArray *sourceIDs;
138   NSEnumerator *allIDs;
139   NSString *currentID;
140   NSNumber *canAuthenticate;
141
142   sourceIDs = [NSMutableArray array];
143   allIDs = [[sources allKeys] objectEnumerator];
144   currentID = [allIDs nextObject];
145   while (currentID)
146     {
147       canAuthenticate = [[sourcesMetadata objectForKey: currentID]
148                           objectForKey: sourceType];
149       if ([canAuthenticate boolValue])
150         [sourceIDs addObject: currentID];
151       currentID = [allIDs nextObject];
152     }
153
154   return sourceIDs;
155 }
156
157 - (NSDictionary *) metadataForSourceID: (NSString *) sourceID
158 {
159   return [sourcesMetadata objectForKey: sourceID];
160 }
161
162 - (NSArray *) authenticationSourceIDs
163 {
164   return [self _sourcesOfType: @"canAuthenticate"];
165 }
166
167 - (NSArray *) addressBookSourceIDs
168 {
169   return [self _sourcesOfType: @"isAddressBook"];
170 }
171
172 - (LDAPSource *) sourceWithID: (NSString *) sourceID
173 {
174   return [sources objectForKey: sourceID];
175 }
176
177 - (NSString *) displayNameForSourceWithID: (NSString *) sourceID
178 {
179   NSDictionary *metadata;
180
181   metadata = [sourcesMetadata objectForKey: sourceID];
182
183   return [metadata objectForKey: @"displayName"];
184 }
185
186 - (NSString *) getCNForUID: (NSString *) uid
187 {
188   NSDictionary *contactInfos;
189
190 //   NSLog (@"getCNForUID: %@", uid);
191   contactInfos = [self contactInfosForUserWithUIDorEmail: uid];
192
193   return [contactInfos objectForKey: @"cn"];
194 }
195
196 - (NSString *) getEmailForUID: (NSString *) uid
197 {
198   NSDictionary *contactInfos;
199
200 //   NSLog (@"getEmailForUID: %@", uid);
201   contactInfos = [self contactInfosForUserWithUIDorEmail: uid];
202
203   return [contactInfos objectForKey: @"c_email"];
204 }
205
206 - (NSString *) getFullEmailForUID: (NSString *) uid
207 {
208   NSDictionary *contactInfos;
209
210   contactInfos = [self contactInfosForUserWithUIDorEmail: uid];
211
212   return [NSString stringWithFormat: @"%@ <%@>",
213                    [contactInfos objectForKey: @"cn"],
214                    [contactInfos objectForKey: @"c_email"]];
215 }
216
217 - (NSString *) getUIDForEmail: (NSString *) email
218 {
219   NSDictionary *contactInfos;
220
221 //   NSLog (@"getUIDForEmail: %@", email);
222   contactInfos = [self contactInfosForUserWithUIDorEmail: email];
223
224   return [contactInfos objectForKey: @"c_uid"];
225 }
226
227 - (BOOL) _ldapCheckLogin: (NSString *) login
228              andPassword: (NSString *) password
229
230   BOOL checkOK;
231   LDAPSource *ldapSource;
232   NSEnumerator *authIDs;
233   NSString *currentID;
234
235   checkOK = NO;
236
237   authIDs = [[self authenticationSourceIDs] objectEnumerator];
238   currentID = [authIDs nextObject];
239   while (currentID && !checkOK)
240     {
241       ldapSource = [sources objectForKey: currentID];
242       checkOK = [ldapSource checkLogin: login andPassword: password];
243       if (!checkOK)
244         currentID = [authIDs nextObject];
245     }
246
247   return checkOK;
248 }
249
250 - (BOOL) checkLogin: (NSString *) login
251         andPassword: (NSString *) password
252 {
253   BOOL checkOK;
254   NSDate *cleanupDate;
255   NSMutableDictionary *currentUser;
256   NSString *dictPassword;
257
258   currentUser = [users objectForKey: login];
259   dictPassword = [currentUser objectForKey: @"password"];
260   if (currentUser && dictPassword)
261     checkOK = ([dictPassword isEqualToString: password]);
262   else if ([self _ldapCheckLogin: login andPassword: password])
263     {
264       checkOK = YES;
265       if (!currentUser)
266         {
267           currentUser = [NSMutableDictionary dictionary];
268           [users setObject: currentUser forKey: login];
269         }
270       [currentUser setObject: password forKey: @"password"];
271     }
272   else
273     checkOK = NO;
274
275   if (cleanupInterval)
276     {
277       cleanupDate = [[NSDate date] addTimeInterval: cleanupInterval];
278       [currentUser setObject: cleanupDate forKey: @"cleanupDate"];
279     }
280
281   return checkOK;
282 }
283
284 - (void) _fillContactMailRecords: (NSMutableDictionary *) contact
285 {
286   NSMutableArray *emails;
287   NSString *uid, *systemEmail;
288
289   emails = [contact objectForKey: @"emails"];
290   uid = [contact objectForKey: @"c_uid"];
291   systemEmail = [NSString stringWithFormat: @"%@@%@", uid, defaultMailDomain];
292   if ([emails containsObject: systemEmail])
293     [emails removeObject: systemEmail];
294   [emails addObject: systemEmail];
295   [contact setObject: [emails objectAtIndex: 0] forKey: @"c_email"];
296 }
297
298 - (void) _fillContactInfosForUser: (NSMutableDictionary *) currentUser
299                    withUIDorEmail: (NSString *) uid
300 {
301   NSMutableArray *emails;
302   NSDictionary *userEntry;
303   NSEnumerator *ldapSources;
304   LDAPSource *currentSource;
305   NSString *cn, *email, *c_uid;
306   NSArray *attrs;
307   
308   emails = [NSMutableArray array];
309   cn = nil;
310   c_uid = nil;
311
312   ldapSources = [sources objectEnumerator];
313   currentSource = [ldapSources nextObject];
314   while (currentSource)
315     {
316       userEntry = [currentSource lookupContactEntryWithUIDorEmail: uid];
317       if (userEntry)
318         {
319           if (!cn)
320             cn = [userEntry objectForKey: @"c_cn"];
321           if (!c_uid)
322             c_uid = [userEntry objectForKey: @"c_uid"];
323
324           if ((attrs = [[sourcesMetadata objectForKey: [currentSource sourceID]] objectForKey: @"MailFieldNames"]))
325             {
326               int i;
327
328               for (i = 0; i < [attrs count]; i++)
329                 {
330                   email = [userEntry objectForKey: [attrs objectAtIndex: i]];
331                   if (email && ![emails containsObject: email])
332                     [emails addObject: email];
333                 }
334             }
335         }
336       currentSource = [ldapSources nextObject];
337     }
338
339   if (!cn)
340     cn = @"";
341   if (!c_uid)
342     c_uid = @"";
343
344   [currentUser setObject: emails forKey: @"emails"];
345   [currentUser setObject: cn forKey: @"cn"];
346   [currentUser setObject: c_uid forKey: @"c_uid"];
347
348   // If our LDAP queries gave us nothing, we add at least one default
349   // email address based on the default domain.
350   [self _fillContactMailRecords: currentUser];
351 }
352
353 - (void) _retainUser: (NSDictionary *) newUser
354 {
355   NSString *key;
356   NSEnumerator *emails;
357
358   key = [newUser objectForKey: @"c_uid"];
359   if (key)
360     [users setObject: newUser forKey: key];
361   emails = [[newUser objectForKey: @"emails"] objectEnumerator];
362   key = [emails nextObject];
363   while (key)
364     {
365       [users setObject: newUser forKey: key];
366       key = [emails nextObject];
367     }
368 }
369
370 - (NSDictionary *) contactInfosForUserWithUIDorEmail: (NSString *) uid
371 {
372   NSMutableDictionary *currentUser, *contactInfos;
373   NSDate *cleanupDate;
374   BOOL newUser;
375
376   if ([uid length] > 0)
377     {
378       contactInfos = [NSMutableDictionary dictionary];
379       currentUser = [users objectForKey: uid];
380       if (!([currentUser objectForKey: @"emails"]
381             && [currentUser objectForKey: @"cn"]))
382         {
383           if (!currentUser)
384             {
385               newUser = YES;
386               currentUser = [NSMutableDictionary dictionary];
387             }
388           else
389             newUser = NO;
390           [self _fillContactInfosForUser: currentUser
391                 withUIDorEmail: uid];
392           if (newUser)
393             {
394               if ([[currentUser objectForKey: @"c_uid"] length] > 0)
395                 [self _retainUser: currentUser];
396               else
397                 currentUser = nil;
398             }
399         }
400
401       if (cleanupInterval && currentUser)
402         {
403           cleanupDate = [[NSDate date] addTimeInterval: cleanupInterval];
404           [currentUser setObject: cleanupDate forKey: @"cleanupDate"];
405         }
406     }
407   else
408     currentUser = nil;
409
410   return currentUser;
411 }
412
413 - (void) _fillContactsMailRecords: (NSEnumerator *) contacts
414 {
415   NSMutableDictionary *currentContact;
416
417   currentContact = [contacts nextObject];
418   while (currentContact)
419     {
420       [self _fillContactMailRecords: currentContact];
421       currentContact = [contacts nextObject];
422     }
423 }
424
425 - (NSArray *) _compactAndCompleteContacts: (NSEnumerator *) contacts
426 {
427   NSMutableDictionary *compactContacts, *returnContact;
428   NSDictionary *userEntry;
429   NSArray *newContacts;
430   NSMutableArray *emails;
431   NSString *uid, *email;
432
433   compactContacts = [NSMutableDictionary dictionary];
434   userEntry = [contacts nextObject];
435   while (userEntry)
436     {
437       uid = [userEntry objectForKey: @"c_uid"];
438       if ([uid length])
439         {
440           returnContact = [compactContacts objectForKey: uid];
441           if (!returnContact)
442             {
443               returnContact = [NSMutableDictionary dictionary];
444               [returnContact setObject: uid forKey: @"c_uid"];
445               [compactContacts setObject: returnContact forKey: uid];
446             }
447           if (![[returnContact objectForKey: @"c_name"] length])
448             [returnContact setObject: [userEntry objectForKey: @"c_name"]
449                            forKey: @"c_name"];
450           if (![[returnContact objectForKey: @"cn"] length])
451             [returnContact setObject: [userEntry objectForKey: @"c_cn"]
452                            forKey: @"cn"];
453           emails = [returnContact objectForKey: @"emails"];
454           if (!emails)
455             {
456               emails = [NSMutableArray array];
457               [returnContact setObject: emails forKey: @"emails"];
458             }
459           email = [userEntry objectForKey: @"mail"];
460           if (email && ![emails containsObject: email])
461             [emails addObject: email];
462           email = [userEntry objectForKey: @"mozillaSecondEmail"];
463           if (email && ![emails containsObject: email])
464             [emails addObject: email];
465           email = [userEntry objectForKey: @"xmozillasecondemail"];
466           if (email && ![emails containsObject: email])
467             [emails addObject: email];
468         }
469
470       userEntry = [contacts nextObject];
471     }
472   newContacts = [compactContacts allValues];
473   [self _fillContactsMailRecords: [newContacts objectEnumerator]];
474
475   return newContacts;
476 }
477
478 - (NSArray *) fetchContactsMatching: (NSString *) filter
479 {
480   NSMutableArray *contacts;
481   NSEnumerator *ldapSources;
482   LDAPSource *currentSource;
483
484   contacts = [NSMutableArray array];
485   ldapSources = [sources objectEnumerator];
486   currentSource = [ldapSources nextObject];
487   while (currentSource)
488     {
489       [contacts addObjectsFromArray:
490                   [currentSource fetchContactsMatching: filter]];
491       currentSource = [ldapSources nextObject];
492     }
493
494   return [self _compactAndCompleteContacts: [contacts objectEnumerator]];
495 }
496
497 - (void) cleanupSources
498 {
499   NSEnumerator *userIDs;
500   NSString *currentID;
501   NSDictionary *currentUser;
502   NSDate *now;
503
504   now = [NSDate date];
505   userIDs = [[users allKeys] objectEnumerator];
506   currentID = [userIDs nextObject];
507   while (currentID)
508     {
509       currentUser = [users objectForKey: currentID];
510       if ([now earlierDate:
511                  [currentUser objectForKey: @"cleanupDate"]] == now)
512         [users removeObjectForKey: currentID];
513       currentID = [userIDs nextObject];
514     }
515 }
516
517 @end