53e4816795cb7a457cee53bd1e9fd6904a0f8303
[citadel.git] / citadel / ldap.c
1 /*
2  * These functions implement the portions of AUTHMODE_LDAP and AUTHMODE_LDAP_AD which
3  * actually speak to the LDAP server.
4  *
5  * Copyright (c) 2011-2017 by the citadel.org development team.
6  *
7  * This program is open source software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License, version 3.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 int ctdl_require_ldap_version = 3;
17
18 #define _GNU_SOURCE             // Needed to suppress warning about vasprintf() when running on Linux/Linux
19 #include <stdio.h>
20 #include <libcitadel.h>
21 #include "citserver.h"
22 #include "citadel_ldap.h"
23 #include "ctdl_module.h"
24 #include "user_ops.h"
25 #include "internet_addressing.h"
26 #include "config.h"
27
28 #ifdef HAVE_LDAP
29 #define LDAP_DEPRECATED 1       // Suppress libldap's warning that we are using deprecated API calls
30 #include <ldap.h>
31
32
33 /*
34  * Utility function, supply a search result and get back the fullname (display name, common name, etc) from the first result
35  */
36 void derive_fullname_from_ldap_result(char *fullname, int fullname_size, LDAP *ldserver, LDAPMessage *search_result)
37 {
38         char **values;
39
40         if (fullname == NULL) return;
41
42         if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD) {
43                 values = ldap_get_values(ldserver, search_result, "displayName");
44                 if (values) {
45                         if (values[0]) {
46                                 if (fullname) safestrncpy(fullname, values[0], fullname_size);
47                                 syslog(LOG_DEBUG, "ldap: displayName = %s", values[0]);
48                         }
49                         ldap_value_free(values);
50                 }
51         }
52         else {
53                 values = ldap_get_values(ldserver, search_result, "cn");
54                 if (values) {
55                         if (values[0]) {
56                                 if (fullname) safestrncpy(fullname, values[0], fullname_size);
57                                 syslog(LOG_DEBUG, "ldap: cn = %s", values[0]);
58                         }
59                         ldap_value_free(values);
60                 }
61         }
62         syslog(LOG_DEBUG, "\033[31mldap: display name: <%s> \033[0m", fullname);
63 }
64
65
66 /*
67  * Utility function, supply a search result and get back the uid from the first result
68  */
69 uid_t derive_uid_from_ldap(LDAP *ldserver, LDAPMessage *entry)
70 {
71         char **values;
72         uid_t uid = (-1);
73
74         if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD) {
75                 values = ldap_get_values(ldserver, entry, "objectGUID");        // AD schema: uid hashed from objectGUID
76                 if (values) {
77                         if (values[0]) {
78                                 uid = abs(HashLittle(values[0], strlen(values[0])));
79                         }
80                         ldap_value_free(values);
81                 }
82         }
83         else {
84                 values = ldap_get_values(ldserver, entry, "uidNumber");         // POSIX schema: uid = uidNumber
85                 if (values) {
86                         if (values[0]) {
87                                 uid = atoi(values[0]);
88                         }
89                         ldap_value_free(values);
90                 }
91         }
92
93         syslog(LOG_DEBUG, "\033[31mldap: uid: <%d> \033[0m", uid);
94         return(uid);
95 }
96
97
98
99
100 /*
101  * Wrapper function for ldap_initialize() that consistently fills in the correct fields
102  */
103 int ctdl_ldap_initialize(LDAP **ld) {
104
105         char server_url[256];
106         int ret;
107
108         snprintf(server_url, sizeof server_url, "ldap://%s:%d", CtdlGetConfigStr("c_ldap_host"), CtdlGetConfigInt("c_ldap_port"));
109         ret = ldap_initialize(ld, server_url);
110         if (ret != LDAP_SUCCESS) {
111                 syslog(LOG_ERR, "ldap: could not connect to %s : %m", server_url);
112                 *ld = NULL;
113                 return(errno);
114         }
115
116         return(ret);
117 }
118
119
120 /*
121  * Look up a user in the directory to see if this is an account that can be authenticated
122  */
123 int CtdlTryUserLDAP(char *username,
124                 char *found_dn, int found_dn_size,
125                 char *fullname, int fullname_size,
126                 uid_t *uid, int lookup_based_on_username)
127 {
128         LDAP *ldserver = NULL;
129         int i;
130         LDAPMessage *search_result = NULL;
131         LDAPMessage *entry = NULL;
132         char searchstring[1024];
133         struct timeval tv;
134         char *user_dn = NULL;
135
136         if (fullname) safestrncpy(fullname, username, fullname_size);
137
138         if (ctdl_ldap_initialize(&ldserver) != LDAP_SUCCESS) {
139                 return(errno);
140         }
141
142         ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
143         ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
144
145         striplt(CtdlGetConfigStr("c_ldap_bind_dn"));
146         striplt(CtdlGetConfigStr("c_ldap_bind_pw"));
147         syslog(LOG_DEBUG, "ldap: bind DN: %s", CtdlGetConfigStr("c_ldap_bind_dn"));
148         i = ldap_simple_bind_s(ldserver,
149                 (!IsEmptyStr(CtdlGetConfigStr("c_ldap_bind_dn")) ? CtdlGetConfigStr("c_ldap_bind_dn") : NULL),
150                 (!IsEmptyStr(CtdlGetConfigStr("c_ldap_bind_pw")) ? CtdlGetConfigStr("c_ldap_bind_pw") : NULL)
151         );
152         if (i != LDAP_SUCCESS) {
153                 syslog(LOG_ERR, "ldap: Cannot bind: %s (%d)", ldap_err2string(i), i);
154                 return(i);
155         }
156
157         tv.tv_sec = 10;
158         tv.tv_usec = 0;
159
160         if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD) {
161                 if (lookup_based_on_username != 0)
162                         snprintf(searchstring, sizeof(searchstring), "(displayName=%s)",username);
163                 else
164                         snprintf(searchstring, sizeof(searchstring), "(sAMAccountName=%s)", username);
165         }
166         else {
167                 if (lookup_based_on_username != 0) {
168                         snprintf(searchstring, sizeof(searchstring), "(cn=%s)",username);
169                 }
170                 else {
171                         snprintf(searchstring, sizeof(searchstring), "(&(objectclass=posixAccount)(uid=%s))", username);
172                 }
173         }
174
175         syslog(LOG_DEBUG, "ldap: search: %s", searchstring);
176         (void) ldap_search_ext_s(
177                 ldserver,                                       /* ld                           */
178                 CtdlGetConfigStr("c_ldap_base_dn"),             /* base                         */
179                 LDAP_SCOPE_SUBTREE,                             /* scope                        */
180                 searchstring,                                   /* filter                       */
181                 NULL,                                           /* attrs (all attributes)       */
182                 0,                                              /* attrsonly (attrs + values)   */
183                 NULL,                                           /* serverctrls (none)           */
184                 NULL,                                           /* clientctrls (none)           */
185                 &tv,                                            /* timeout                      */
186                 1,                                              /* sizelimit (1 result max)     */
187                 &search_result                                  /* res                          */
188         );
189
190         /* Ignore the return value of ldap_search_ext_s().  Sometimes it returns an error even when
191          * the search succeeds.  Instead, we check to see whether search_result is still NULL.
192          */
193         if (search_result == NULL) {
194                 syslog(LOG_DEBUG, "ldap: zero search results were returned");
195                 ldap_unbind(ldserver);
196                 return(2);
197         }
198
199         /* At this point we've got at least one result from our query.  If there are multiple
200          * results, we still only look at the first one.
201          */
202         entry = ldap_first_entry(ldserver, search_result);
203         if (entry) {
204
205                 user_dn = ldap_get_dn(ldserver, entry);
206                 if (user_dn) {
207                         syslog(LOG_DEBUG, "ldap: dn = %s", user_dn);
208                 }
209
210                 derive_fullname_from_ldap_result(fullname, fullname_size, ldserver, search_result);
211
212                 /* If we know the username is the CN/displayName, we already set the uid*/
213                 // FIXME old skool crap , fix this
214                 if (lookup_based_on_username == 0) {
215                         *uid = derive_uid_from_ldap(ldserver, search_result);
216                 }
217         }
218
219         /* free the results */
220         ldap_msgfree(search_result);
221
222         /* unbind so we can go back in as the authenticating user */
223         ldap_unbind(ldserver);
224
225         if (!user_dn) {
226                 syslog(LOG_DEBUG, "ldap: No such user was found.");
227                 return(4);
228         }
229
230         if (found_dn) safestrncpy(found_dn, user_dn, found_dn_size);
231         ldap_memfree(user_dn);
232         return(0);
233 }
234
235
236 int CtdlTryPasswordLDAP(char *user_dn, const char *password)
237 {
238         LDAP *ldserver = NULL;
239         int i = (-1);
240
241         if (IsEmptyStr(password)) {
242                 syslog(LOG_DEBUG, "ldap: empty passwords are not permitted");
243                 return(1);
244         }
245
246         syslog(LOG_DEBUG, "ldap: trying to bind as %s", user_dn);
247         i = ctdl_ldap_initialize(&ldserver);
248         if (i == LDAP_SUCCESS) {
249                 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
250                 i = ldap_simple_bind_s(ldserver, user_dn, password);
251                 if (i == LDAP_SUCCESS) {
252                         syslog(LOG_DEBUG, "ldap: bind succeeded");
253                 }
254                 else {
255                         syslog(LOG_DEBUG, "ldap: Cannot bind: %s (%d)", ldap_err2string(i), i);
256                 }
257                 ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
258                 ldap_unbind(ldserver);
259         }
260
261         if (i == LDAP_SUCCESS) {
262                 return(0);
263         }
264
265         return(1);
266 }
267
268
269 //return !0 iff property changed.
270 int vcard_set_props_iff_different(struct vCard *v,char *propname,int numvals, char **vals) {
271         int i;
272         char *oldval = "";
273         for(i=0;i<numvals;i++) {
274           oldval = vcard_get_prop(v,propname,0,i,0);
275           if (oldval == NULL) break;
276           if (strcmp(vals[i],oldval)) break;
277         }
278         if (i!=numvals) {
279                 syslog(LOG_DEBUG, "ldap: vcard property %s, element %d of %d changed from %s to %s\n", propname, i, numvals, oldval, vals[i]);
280                 for(i=0;i<numvals;i++) vcard_set_prop(v,propname,vals[i],(i==0) ? 0 : 1);
281                 return 1;
282         }
283         return 0;
284 }
285
286
287 //return !0 iff property changed.
288 int vcard_set_one_prop_iff_different(struct vCard *v,char *propname, char *newfmt, ...) {
289         va_list args;
290         char *newvalue;
291         int changed_something;
292         va_start(args,newfmt);
293         if (-1==vasprintf(&newvalue,newfmt,args)) {
294                 syslog(LOG_ERR, "ldap: out of memory!");
295                 return 0;
296         }
297         changed_something = vcard_set_props_iff_different(v,propname,1,&newvalue);
298         va_end(args);
299         free(newvalue);
300         return changed_something;
301 }
302
303
304 /*
305  * Learn LDAP attributes and stuff them into the vCard.
306  * Returns nonzero if we changed anything.
307  */
308 int Ctdl_LDAP_to_vCard(char *ldap_dn, struct vCard *v)
309 {
310         int changed_something = 0;
311         LDAP *ldserver = NULL;
312         int i;
313         struct timeval tv;
314         LDAPMessage *search_result = NULL;
315         LDAPMessage *entry = NULL;
316         char **givenName;
317         char **sn;
318         char **cn;
319         char **initials;
320         char **o;
321         char **street;
322         char **l;
323         char **st;
324         char **postalCode;
325         char **telephoneNumber;
326         char **mobile;
327         char **homePhone;
328         char **facsimileTelephoneNumber;
329         char **mail;
330         char **uid;
331         char **homeDirectory;
332         char **uidNumber;
333         char **loginShell;
334         char **gidNumber;
335         char **c;
336         char **title;
337         char **uuid;
338         char *attrs[] = { "*","+",NULL};
339
340         if (!ldap_dn) return(0);
341         if (!v) return(0);
342
343         if (ctdl_ldap_initialize(&ldserver) != LDAP_SUCCESS) {
344                 return(0);
345         }
346
347         ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
348         ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
349
350         striplt(CtdlGetConfigStr("c_ldap_bind_dn"));
351         striplt(CtdlGetConfigStr("c_ldap_bind_pw"));
352         syslog(LOG_DEBUG, "ldap: bind DN: %s", CtdlGetConfigStr("c_ldap_bind_dn"));
353         i = ldap_simple_bind_s(ldserver,
354                 (!IsEmptyStr(CtdlGetConfigStr("c_ldap_bind_dn")) ? CtdlGetConfigStr("c_ldap_bind_dn") : NULL),
355                 (!IsEmptyStr(CtdlGetConfigStr("c_ldap_bind_pw")) ? CtdlGetConfigStr("c_ldap_bind_pw") : NULL)
356         );
357         if (i != LDAP_SUCCESS) {
358                 syslog(LOG_ERR, "ldap: Cannot bind: %s (%d)", ldap_err2string(i), i);
359                 return(0);
360         }
361
362         tv.tv_sec = 10;
363         tv.tv_usec = 0;
364
365         syslog(LOG_DEBUG, "ldap: search: %s", ldap_dn);
366         (void) ldap_search_ext_s(
367                 ldserver,                               // ld
368                 ldap_dn,                                // base
369                 LDAP_SCOPE_SUBTREE,                     // scope
370                 NULL,                                   // filter
371                 attrs,                                  // attrs (all attributes)
372                 0,                                      // attrsonly (attrs + values)
373                 NULL,                                   // serverctrls (none)
374                 NULL,                                   // clientctrls (none)
375                 &tv,                                    // timeout
376                 1,                                      // sizelimit (1 result max)
377                 &search_result                          // res
378         );
379         
380         /* Ignore the return value of ldap_search_ext_s().  Sometimes it returns an error even when
381          * the search succeeds.  Instead, we check to see whether search_result is still NULL.
382          */
383         if (search_result == NULL) {
384                 syslog(LOG_DEBUG, "ldap: zero search results were returned");
385                 ldap_unbind(ldserver);
386                 return(0);
387         }
388
389         /* At this point we've got at least one result from our query.  If there are multiple
390          * results, we still only look at the first one.
391          */
392         entry = ldap_first_entry(ldserver, search_result);
393         if (entry) {
394                 syslog(LOG_DEBUG, "ldap: search got user details for vcard.");
395                 givenName=ldap_get_values(ldserver, search_result, "givenName");
396                 sn=ldap_get_values(ldserver, search_result, "sn");
397                 cn=ldap_get_values(ldserver, search_result, "cn");
398                 initials=ldap_get_values(ldserver, search_result, "initials");
399                 title=ldap_get_values(ldserver, search_result, "title");
400                 o=ldap_get_values(ldserver, search_result, "o");
401                 street=ldap_get_values(ldserver, search_result, "street");
402                 l=ldap_get_values(ldserver, search_result, "l");
403                 st=ldap_get_values(ldserver, search_result, "st");
404                 postalCode=ldap_get_values(ldserver, search_result, "postalCode");
405                 telephoneNumber=ldap_get_values(ldserver, search_result, "telephoneNumber");
406                 mobile=ldap_get_values(ldserver, search_result, "mobile");
407                 homePhone=ldap_get_values(ldserver, search_result, "homePhone");
408                 facsimileTelephoneNumber=ldap_get_values(ldserver, search_result, "facsimileTelephoneNumber");
409                 mail=ldap_get_values(ldserver, search_result, "mail");
410                 uid=ldap_get_values(ldserver, search_result, "uid");
411                 homeDirectory=ldap_get_values(ldserver, search_result, "homeDirectory");
412                 uidNumber=ldap_get_values(ldserver, search_result, "uidNumber");
413                 loginShell=ldap_get_values(ldserver, search_result, "loginShell");
414                 gidNumber=ldap_get_values(ldserver, search_result, "gidNumber");
415                 c=ldap_get_values(ldserver, search_result, "c");
416                 uuid=ldap_get_values(ldserver, search_result, "entryUUID");
417
418                 if (street && l && st && postalCode && c) changed_something |= vcard_set_one_prop_iff_different(v,"adr",";;%s;%s;%s;%s;%s",street[0],l[0],st[0],postalCode[0],c[0]);
419                 if (telephoneNumber) changed_something |= vcard_set_one_prop_iff_different(v,"tel;work","%s",telephoneNumber[0]);
420                 if (facsimileTelephoneNumber) changed_something |= vcard_set_one_prop_iff_different(v,"tel;fax","%s",facsimileTelephoneNumber[0]);
421                 if (mobile) changed_something |= vcard_set_one_prop_iff_different(v,"tel;cell","%s",mobile[0]);
422                 if (homePhone) changed_something |= vcard_set_one_prop_iff_different(v,"tel;home","%s",homePhone[0]);
423                 if (givenName && sn) {
424                         if (initials) {
425                                 changed_something |= vcard_set_one_prop_iff_different(v,"n","%s;%s;%s",sn[0],givenName[0],initials[0]);
426                         }
427                         else {
428                                 changed_something |= vcard_set_one_prop_iff_different(v,"n","%s;%s",sn[0],givenName[0]);
429                         }
430                 }
431                 if (mail) {
432                         changed_something |= vcard_set_props_iff_different(v,"email;internet",ldap_count_values(mail),mail);
433                 }
434                 if (uuid) changed_something |= vcard_set_one_prop_iff_different(v,"X-uuid","%s",uuid[0]);
435                 if (o) changed_something |= vcard_set_one_prop_iff_different(v,"org","%s",o[0]);
436                 if (cn) changed_something |= vcard_set_one_prop_iff_different(v,"fn","%s",cn[0]);
437                 if (title) changed_something |= vcard_set_one_prop_iff_different(v,"title","%s",title[0]);
438                 
439                 if (givenName) ldap_value_free(givenName);
440                 if (initials) ldap_value_free(initials);
441                 if (sn) ldap_value_free(sn);
442                 if (cn) ldap_value_free(cn);
443                 if (o) ldap_value_free(o);
444                 if (street) ldap_value_free(street);
445                 if (l) ldap_value_free(l);
446                 if (st) ldap_value_free(st);
447                 if (postalCode) ldap_value_free(postalCode);
448                 if (telephoneNumber) ldap_value_free(telephoneNumber);
449                 if (mobile) ldap_value_free(mobile);
450                 if (homePhone) ldap_value_free(homePhone);
451                 if (facsimileTelephoneNumber) ldap_value_free(facsimileTelephoneNumber);
452                 if (mail) ldap_value_free(mail);
453                 if (uid) ldap_value_free(uid);
454                 if (homeDirectory) ldap_value_free(homeDirectory);
455                 if (uidNumber) ldap_value_free(uidNumber);
456                 if (loginShell) ldap_value_free(loginShell);
457                 if (gidNumber) ldap_value_free(gidNumber);
458                 if (c) ldap_value_free(c);
459                 if (title) ldap_value_free(title);
460                 if (uuid) ldap_value_free(uuid);
461         }
462         /* free the results */
463         ldap_msgfree(search_result);
464
465         /* unbind so we can go back in as the authenticating user */
466         ldap_unbind(ldserver);
467         return(changed_something);      /* tell the caller whether we made any changes */
468 }
469
470
471 /*
472  * Extract a user's Internet email addresses from LDAP.
473  * Returns zero if we got a valid set of addresses; nonzero for error.
474  */
475 int extract_email_addresses_from_ldap(char *ldap_dn, char *emailaddrs)
476 {
477         LDAP *ldserver = NULL;
478         int i;
479         struct timeval tv;
480         LDAPMessage *search_result = NULL;
481         LDAPMessage *entry = NULL;
482         char **mail;
483         char *attrs[] = { "*","+",NULL};
484
485         if (!ldap_dn) return(1);
486         if (!emailaddrs) return(1);
487
488         if (ctdl_ldap_initialize(&ldserver) != LDAP_SUCCESS) {
489                 return(2);
490         }
491
492         ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
493         ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
494
495         striplt(CtdlGetConfigStr("c_ldap_bind_dn"));
496         striplt(CtdlGetConfigStr("c_ldap_bind_pw"));
497         syslog(LOG_DEBUG, "ldap: bind DN: %s", CtdlGetConfigStr("c_ldap_bind_dn"));
498         i = ldap_simple_bind_s(ldserver,
499                 (!IsEmptyStr(CtdlGetConfigStr("c_ldap_bind_dn")) ? CtdlGetConfigStr("c_ldap_bind_dn") : NULL),
500                 (!IsEmptyStr(CtdlGetConfigStr("c_ldap_bind_pw")) ? CtdlGetConfigStr("c_ldap_bind_pw") : NULL)
501         );
502         if (i != LDAP_SUCCESS) {
503                 syslog(LOG_ERR, "ldap: Cannot bind: %s (%d)", ldap_err2string(i), i);
504                 return(3);
505         }
506
507         tv.tv_sec = 10;
508         tv.tv_usec = 0;
509
510         syslog(LOG_DEBUG, "ldap: search: %s", ldap_dn);
511         (void) ldap_search_ext_s(
512                 ldserver,                               // ld
513                 ldap_dn,                                // base
514                 LDAP_SCOPE_SUBTREE,                     // scope
515                 NULL,                                   // filter
516                 attrs,                                  // attrs (all attributes)
517                 0,                                      // attrsonly (attrs + values)
518                 NULL,                                   // serverctrls (none)
519                 NULL,                                   // clientctrls (none)
520                 &tv,                                    // timeout
521                 1,                                      // sizelimit (1 result max)
522                 &search_result                          // res
523         );
524         
525         /* Ignore the return value of ldap_search_ext_s().  Sometimes it returns an error even when
526          * the search succeeds.  Instead, we check to see whether search_result is still NULL.
527          */
528         if (search_result == NULL) {
529                 syslog(LOG_DEBUG, "ldap: zero search results were returned");
530                 ldap_unbind(ldserver);
531                 return(4);
532         }
533
534         /* At this point we've got at least one result from our query.  If there are multiple
535          * results, we still only look at the first one.
536          */
537         emailaddrs[0] = 0;      /* clear out any previous results */
538         entry = ldap_first_entry(ldserver, search_result);
539         if (entry) {
540                 syslog(LOG_DEBUG, "ldap: search got user details");
541                 mail=ldap_get_values(ldserver, search_result, "mail");
542
543                 if (mail) {
544                         int q;
545                         for (q=0; q<ldap_count_values(mail); ++q) {
546                                 if (IsDirectory(mail[q], 0)) {
547                                         syslog(LOG_DEBUG, "\035FIXME YES DIRECTORY %s\033[0m", mail[q]);
548                                         if ((strlen(emailaddrs) + strlen(mail[q]) + 2) > 512) {
549                                                 syslog(LOG_ERR, "ldap: can't fit all email addresses into user record");
550                                         }
551                                         else {
552                                                 if (!IsEmptyStr(emailaddrs)) {
553                                                         strcat(emailaddrs, "|");
554                                                 }
555                                                 strcat(emailaddrs, mail[q]);
556                                         }
557                                 }
558                         }
559                 }
560         }
561
562         /* free the results */
563         ldap_msgfree(search_result);
564
565         /* unbind so we can go back in as the authenticating user */
566         ldap_unbind(ldserver);
567         return(0);
568 }
569
570
571 /*
572  * Scan LDAP for users and populate Citadel's user database with everyone
573  */
574 void CtdlSynchronizeUsersFromLDAP(void)
575 {
576         LDAP *ldserver = NULL;
577         int i;
578         LDAPMessage *search_result = NULL;
579         LDAPMessage *entry = NULL;
580         char *user_dn = NULL;
581         char searchstring[1024];
582         struct timeval tv;
583
584         if ((CtdlGetConfigInt("c_auth_mode") != AUTHMODE_LDAP) && (CtdlGetConfigInt("c_auth_mode") != AUTHMODE_LDAP_AD)) {
585                 return;         // not running LDAP
586         }
587
588         syslog(LOG_INFO, "ldap: synchronizing Citadel user database from LDAP");
589
590         if (ctdl_ldap_initialize(&ldserver) != LDAP_SUCCESS) {
591                 return;
592         }
593
594         ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
595         ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
596
597         striplt(CtdlGetConfigStr("c_ldap_bind_dn"));
598         striplt(CtdlGetConfigStr("c_ldap_bind_pw"));
599         syslog(LOG_DEBUG, "ldap: bind DN: %s", CtdlGetConfigStr("c_ldap_bind_dn"));
600         i = ldap_simple_bind_s(ldserver,
601                 (!IsEmptyStr(CtdlGetConfigStr("c_ldap_bind_dn")) ? CtdlGetConfigStr("c_ldap_bind_dn") : NULL),
602                 (!IsEmptyStr(CtdlGetConfigStr("c_ldap_bind_pw")) ? CtdlGetConfigStr("c_ldap_bind_pw") : NULL)
603         );
604         if (i != LDAP_SUCCESS) {
605                 syslog(LOG_ERR, "ldap: Cannot bind: %s (%d)", ldap_err2string(i), i);
606                 return;
607         }
608
609         tv.tv_sec = 10;
610         tv.tv_usec = 0;
611
612         if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD) {
613                         snprintf(searchstring, sizeof(searchstring), "(&(objectClass=user)(objectClass=person)(!(objectClass=computer)))");
614         } else {
615                         snprintf(searchstring, sizeof(searchstring), "(objectClass=inetOrgPerson)");
616         }
617
618         syslog(LOG_DEBUG, "ldap: search: %s", searchstring);
619         (void) ldap_search_ext_s(
620                 ldserver,                                       // ld
621                 CtdlGetConfigStr("c_ldap_base_dn"),             // base
622                 LDAP_SCOPE_SUBTREE,                             // scope
623                 searchstring,                                   // filter
624                 NULL,                                           // attrs (all attributes)
625                 0,                                              // attrsonly (attrs + values)
626                 NULL,                                           // serverctrls (none)
627                 NULL,                                           // clientctrls (none)
628                 &tv,                                            // timeout
629                 INT_MAX,                                        // sizelimit (max)
630                 &search_result                                  // result
631         );
632
633         /* Ignore the return value of ldap_search_ext_s().  Sometimes it returns an error even when
634          * the search succeeds.  Instead, we check to see whether search_result is still NULL.
635          */
636         if (search_result == NULL) {
637                 syslog(LOG_DEBUG, "ldap: zero search results were returned");
638                 ldap_unbind(ldserver);
639                 return;
640         }
641
642         syslog(LOG_DEBUG, "ldap: %d entries returned", ldap_count_entries(ldserver, search_result));
643         entry = ldap_first_entry(ldserver, search_result);
644         while (entry) {
645
646                 user_dn = ldap_get_dn(ldserver, entry);
647                 if (user_dn) {
648                         syslog(LOG_DEBUG, "ldap: found %s", user_dn);
649
650                         int fullname_size = 256;
651                         char fullname[256] = { 0 } ;
652                         uid_t uid = (-1);
653
654                         derive_fullname_from_ldap_result(fullname, fullname_size, ldserver, entry);
655                         uid = derive_uid_from_ldap(ldserver, entry);
656                         syslog(LOG_DEBUG, "\033[33mldap: display name: <%s> , uid = <%d>\033[0m", fullname, uid);
657
658                         // FIXME now create or update the user
659
660
661                 }
662
663                 entry = ldap_next_entry(ldserver, entry);
664         }
665
666         /* free the results */
667         ldap_msgfree(search_result);
668
669         /* unbind so we can go back in as the authenticating user */
670         ldap_unbind(ldserver);
671 }
672
673 #endif /* HAVE_LDAP */