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