2 * These functions implement the portions of AUTHMODE_LDAP and AUTHMODE_LDAP_AD which
3 * actually speak to the LDAP server.
5 * Copyright (c) 2011-2017 by the citadel.org development team.
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.
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.
16 int ctdl_require_ldap_version = 3;
18 #define _GNU_SOURCE // Needed to suppress warning about vasprintf() when running on Linux/Linux
20 #include <libcitadel.h>
21 #include "citserver.h"
22 #include "citadel_ldap.h"
23 #include "ctdl_module.h"
25 #include "internet_addressing.h"
29 #define LDAP_DEPRECATED 1 // Suppress libldap's warning that we are using deprecated API calls
34 * Utility function, supply a search result and get back the fullname (display name, common name, etc) from the first result
36 void derive_fullname_from_ldap_result(char *fullname, int fullname_size, LDAP *ldserver, LDAPMessage *search_result)
40 if (fullname == NULL) return;
42 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD) {
43 values = ldap_get_values(ldserver, search_result, "displayName");
46 if (fullname) safestrncpy(fullname, values[0], fullname_size);
47 syslog(LOG_DEBUG, "ldap: displayName = %s", values[0]);
49 ldap_value_free(values);
53 values = ldap_get_values(ldserver, search_result, "cn");
56 if (fullname) safestrncpy(fullname, values[0], fullname_size);
57 syslog(LOG_DEBUG, "ldap: cn = %s", values[0]);
59 ldap_value_free(values);
66 * Utility function, supply a search result and get back the uid from the first result
68 uid_t derive_uid_from_ldap(LDAP *ldserver, LDAPMessage *entry)
73 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD) {
74 values = ldap_get_values(ldserver, entry, "objectGUID"); // AD schema: uid hashed from objectGUID
77 uid = abs(HashLittle(values[0], strlen(values[0])));
79 ldap_value_free(values);
83 values = ldap_get_values(ldserver, entry, "uidNumber"); // POSIX schema: uid = uidNumber
86 uid = atoi(values[0]);
88 ldap_value_free(values);
97 * Wrapper function for ldap_initialize() that consistently fills in the correct fields
99 int ctdl_ldap_initialize(LDAP **ld) {
101 char server_url[256];
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);
117 * Look up a user in the directory to see if this is an account that can be authenticated
119 int CtdlTryUserLDAP(char *username,
120 char *found_dn, int found_dn_size,
121 char *fullname, int fullname_size,
124 LDAP *ldserver = NULL;
126 LDAPMessage *search_result = NULL;
127 LDAPMessage *entry = NULL;
128 char searchstring[1024];
130 char *user_dn = NULL;
132 if (fullname) safestrncpy(fullname, username, fullname_size);
134 if (ctdl_ldap_initialize(&ldserver) != LDAP_SUCCESS) {
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);
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)
148 if (i != LDAP_SUCCESS) {
149 syslog(LOG_ERR, "ldap: Cannot bind: %s (%d)", ldap_err2string(i), i);
156 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD) {
157 snprintf(searchstring, sizeof(searchstring), "(sAMAccountName=%s)", username);
160 snprintf(searchstring, sizeof(searchstring), "(&(objectclass=posixAccount)(cn=%s))", username);
161 // snprintf(searchstring, sizeof(searchstring), "(&(objectclass=posixAccount)(uid=%s))", username);
164 syslog(LOG_DEBUG, "ldap: search: %s", searchstring);
165 (void) ldap_search_ext_s(
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) */
175 1, /* sizelimit (1 result max) */
176 &search_result /* res */
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.
182 if (search_result == NULL) {
183 syslog(LOG_DEBUG, "ldap: zero search results were returned");
184 ldap_unbind(ldserver);
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.
191 entry = ldap_first_entry(ldserver, search_result);
194 user_dn = ldap_get_dn(ldserver, entry);
196 syslog(LOG_DEBUG, "ldap: dn = %s", user_dn);
199 derive_fullname_from_ldap_result(fullname, fullname_size, ldserver, search_result);
200 *uid = derive_uid_from_ldap(ldserver, search_result);
203 /* free the results */
204 ldap_msgfree(search_result);
206 /* unbind so we can go back in as the authenticating user */
207 ldap_unbind(ldserver);
210 syslog(LOG_DEBUG, "ldap: No such user was found.");
214 if (found_dn) safestrncpy(found_dn, user_dn, found_dn_size);
215 ldap_memfree(user_dn);
220 int CtdlTryPasswordLDAP(char *user_dn, const char *password)
222 LDAP *ldserver = NULL;
225 if (IsEmptyStr(password)) {
226 syslog(LOG_DEBUG, "ldap: empty passwords are not permitted");
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");
239 syslog(LOG_DEBUG, "ldap: Cannot bind: %s (%d)", ldap_err2string(i), i);
241 ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
242 ldap_unbind(ldserver);
245 if (i == LDAP_SUCCESS) {
253 //return !0 iff property changed.
254 int vcard_set_props_iff_different(struct vCard *v,char *propname,int numvals, char **vals) {
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;
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);
271 //return !0 iff property changed.
272 int vcard_set_one_prop_iff_different(struct vCard *v,char *propname, char *newfmt, ...) {
275 int changed_something;
276 va_start(args,newfmt);
277 if (-1==vasprintf(&newvalue,newfmt,args)) {
278 syslog(LOG_ERR, "ldap: out of memory!");
281 changed_something = vcard_set_props_iff_different(v,propname,1,&newvalue);
284 return changed_something;
289 * Learn LDAP attributes and stuff them into the vCard.
290 * Returns nonzero if we changed anything.
292 int Ctdl_LDAP_to_vCard(char *ldap_dn, struct vCard *v)
294 int changed_something = 0;
295 LDAP *ldserver = NULL;
298 LDAPMessage *search_result = NULL;
299 LDAPMessage *entry = NULL;
309 char **telephoneNumber;
312 char **facsimileTelephoneNumber;
315 char **homeDirectory;
322 char *attrs[] = { "*","+",NULL};
324 if (!ldap_dn) return(0);
327 if (ctdl_ldap_initialize(&ldserver) != LDAP_SUCCESS) {
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);
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)
341 if (i != LDAP_SUCCESS) {
342 syslog(LOG_ERR, "ldap: Cannot bind: %s (%d)", ldap_err2string(i), i);
349 syslog(LOG_DEBUG, "ldap: search: %s", ldap_dn);
350 (void) ldap_search_ext_s(
353 LDAP_SCOPE_SUBTREE, // scope
355 attrs, // attrs (all attributes)
356 0, // attrsonly (attrs + values)
357 NULL, // serverctrls (none)
358 NULL, // clientctrls (none)
360 1, // sizelimit (1 result max)
361 &search_result // res
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.
367 if (search_result == NULL) {
368 syslog(LOG_DEBUG, "ldap: zero search results were returned");
369 ldap_unbind(ldserver);
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.
376 entry = ldap_first_entry(ldserver, search_result);
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");
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) {
409 changed_something |= vcard_set_one_prop_iff_different(v,"n","%s;%s;%s",sn[0],givenName[0],initials[0]);
412 changed_something |= vcard_set_one_prop_iff_different(v,"n","%s;%s",sn[0],givenName[0]);
416 changed_something |= vcard_set_props_iff_different(v,"email;internet",ldap_count_values(mail),mail);
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]);
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);
446 /* free the results */
447 ldap_msgfree(search_result);
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 */
456 * Extract a user's Internet email addresses from LDAP.
457 * Returns zero if we got a valid set of addresses; nonzero for error.
459 int extract_email_addresses_from_ldap(char *ldap_dn, char *emailaddrs)
461 LDAP *ldserver = NULL;
464 LDAPMessage *search_result = NULL;
465 LDAPMessage *entry = NULL;
467 char *attrs[] = { "*","+",NULL};
469 if (!ldap_dn) return(1);
470 if (!emailaddrs) return(1);
472 if (ctdl_ldap_initialize(&ldserver) != LDAP_SUCCESS) {
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);
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)
486 if (i != LDAP_SUCCESS) {
487 syslog(LOG_ERR, "ldap: Cannot bind: %s (%d)", ldap_err2string(i), i);
494 syslog(LOG_DEBUG, "ldap: search: %s", ldap_dn);
495 (void) ldap_search_ext_s(
498 LDAP_SCOPE_SUBTREE, // scope
500 attrs, // attrs (all attributes)
501 0, // attrsonly (attrs + values)
502 NULL, // serverctrls (none)
503 NULL, // clientctrls (none)
505 1, // sizelimit (1 result max)
506 &search_result // res
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.
512 if (search_result == NULL) {
513 syslog(LOG_DEBUG, "ldap: zero search results were returned");
514 ldap_unbind(ldserver);
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.
521 emailaddrs[0] = 0; /* clear out any previous results */
522 entry = ldap_first_entry(ldserver, search_result);
524 syslog(LOG_DEBUG, "ldap: search got user details");
525 mail = ldap_get_values(ldserver, search_result, "mail");
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");
535 if (!IsEmptyStr(emailaddrs)) {
536 strcat(emailaddrs, "|");
538 strcat(emailaddrs, mail[q]);
545 /* free the results */
546 ldap_msgfree(search_result);
548 /* unbind so we can go back in as the authenticating user */
549 ldap_unbind(ldserver);
555 * Scan LDAP for users and populate Citadel's user database with everyone
557 void CtdlSynchronizeUsersFromLDAP(void)
559 LDAP *ldserver = NULL;
561 LDAPMessage *search_result = NULL;
562 LDAPMessage *entry = NULL;
563 char *user_dn = NULL;
564 char searchstring[1024];
567 if ((CtdlGetConfigInt("c_auth_mode") != AUTHMODE_LDAP) && (CtdlGetConfigInt("c_auth_mode") != AUTHMODE_LDAP_AD)) {
568 return; // not running LDAP
571 syslog(LOG_INFO, "ldap: synchronizing Citadel user database from LDAP");
573 if (ctdl_ldap_initialize(&ldserver) != LDAP_SUCCESS) {
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);
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)
587 if (i != LDAP_SUCCESS) {
588 syslog(LOG_ERR, "ldap: Cannot bind: %s (%d)", ldap_err2string(i), i);
595 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD) {
596 snprintf(searchstring, sizeof(searchstring), "(&(objectClass=user)(objectClass=person)(!(objectClass=computer)))");
598 snprintf(searchstring, sizeof(searchstring), "(objectClass=inetOrgPerson)");
601 syslog(LOG_DEBUG, "ldap: search: %s", searchstring);
602 (void) ldap_search_ext_s(
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)
612 INT_MAX, // sizelimit (max)
613 &search_result // result
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.
619 if (search_result == NULL) {
620 syslog(LOG_DEBUG, "ldap: zero search results were returned");
621 ldap_unbind(ldserver);
625 syslog(LOG_DEBUG, "ldap: %d entries returned", ldap_count_entries(ldserver, search_result));
626 entry = ldap_first_entry(ldserver, search_result);
629 user_dn = ldap_get_dn(ldserver, entry);
631 syslog(LOG_DEBUG, "ldap: found %s", user_dn);
633 int fullname_size = 256;
634 char fullname[256] = { 0 } ;
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);
641 // FIXME now create or update the user
646 entry = ldap_next_entry(ldserver, entry);
649 /* free the results */
650 ldap_msgfree(search_result);
652 /* unbind so we can go back in as the authenticating user */
653 ldap_unbind(ldserver);
656 #endif /* HAVE_LDAP */