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