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);
62 syslog(LOG_DEBUG, "\033[31mldap: display name: <%s> \033[0m", fullname);
69 * Wrapper function for ldap_initialize() that consistently fills in the correct fields
71 int ctdl_ldap_initialize(LDAP **ld) {
76 snprintf(server_url, sizeof server_url, "ldap://%s:%d", CtdlGetConfigStr("c_ldap_host"), CtdlGetConfigInt("c_ldap_port"));
77 ret = ldap_initialize(ld, server_url);
78 if (ret != LDAP_SUCCESS) {
79 syslog(LOG_ERR, "ldap: could not connect to %s : %m", server_url);
89 * Look up a user in the directory to see if this is an account that can be authenticated
91 int CtdlTryUserLDAP(char *username,
92 char *found_dn, int found_dn_size,
93 char *fullname, int fullname_size,
94 uid_t *uid, int lookup_based_on_username)
96 LDAP *ldserver = NULL;
98 LDAPMessage *search_result = NULL;
99 LDAPMessage *entry = NULL;
100 char searchstring[1024];
103 char *user_dn = NULL;
105 if (fullname) safestrncpy(fullname, username, fullname_size);
107 if (ctdl_ldap_initialize(&ldserver) != LDAP_SUCCESS) {
111 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
112 ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
114 striplt(CtdlGetConfigStr("c_ldap_bind_dn"));
115 striplt(CtdlGetConfigStr("c_ldap_bind_pw"));
116 syslog(LOG_DEBUG, "ldap: bind DN: %s", CtdlGetConfigStr("c_ldap_bind_dn"));
117 i = ldap_simple_bind_s(ldserver,
118 (!IsEmptyStr(CtdlGetConfigStr("c_ldap_bind_dn")) ? CtdlGetConfigStr("c_ldap_bind_dn") : NULL),
119 (!IsEmptyStr(CtdlGetConfigStr("c_ldap_bind_pw")) ? CtdlGetConfigStr("c_ldap_bind_pw") : NULL)
121 if (i != LDAP_SUCCESS) {
122 syslog(LOG_ERR, "ldap: Cannot bind: %s (%d)", ldap_err2string(i), i);
129 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD) {
130 if (lookup_based_on_username != 0)
131 snprintf(searchstring, sizeof(searchstring), "(displayName=%s)",username);
133 snprintf(searchstring, sizeof(searchstring), "(sAMAccountName=%s)", username);
136 if (lookup_based_on_username != 0) {
137 snprintf(searchstring, sizeof(searchstring), "(cn=%s)",username);
140 snprintf(searchstring, sizeof(searchstring), "(&(objectclass=posixAccount)(uid=%s))", username);
144 syslog(LOG_DEBUG, "ldap: search: %s", searchstring);
145 (void) ldap_search_ext_s(
147 CtdlGetConfigStr("c_ldap_base_dn"), /* base */
148 LDAP_SCOPE_SUBTREE, /* scope */
149 searchstring, /* filter */
150 NULL, /* attrs (all attributes) */
151 0, /* attrsonly (attrs + values) */
152 NULL, /* serverctrls (none) */
153 NULL, /* clientctrls (none) */
155 1, /* sizelimit (1 result max) */
156 &search_result /* res */
159 /* Ignore the return value of ldap_search_ext_s(). Sometimes it returns an error even when
160 * the search succeeds. Instead, we check to see whether search_result is still NULL.
162 if (search_result == NULL) {
163 syslog(LOG_DEBUG, "ldap: zero search results were returned");
164 ldap_unbind(ldserver);
168 /* At this point we've got at least one result from our query. If there are multiple
169 * results, we still only look at the first one.
171 entry = ldap_first_entry(ldserver, search_result);
174 user_dn = ldap_get_dn(ldserver, entry);
176 syslog(LOG_DEBUG, "ldap: dn = %s", user_dn);
179 derive_fullname_from_ldap_result(fullname, fullname_size, ldserver, search_result);
181 /* If we know the username is the CN/displayName, we already set the uid*/
182 if (lookup_based_on_username==0) {
183 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD) {
184 values = ldap_get_values(ldserver, search_result, "objectGUID");
188 *uid = abs(HashLittle(values[0], strlen(values[0])));
189 syslog(LOG_DEBUG, "ldap: uid hashed from objectGUID = %d", *uid);
192 ldap_value_free(values);
196 values = ldap_get_values(ldserver, search_result, "uidNumber");
199 syslog(LOG_DEBUG, "ldap: uidNumber = %s", values[0]);
201 *uid = atoi(values[0]);
204 ldap_value_free(values);
211 /* free the results */
212 ldap_msgfree(search_result);
214 /* unbind so we can go back in as the authenticating user */
215 ldap_unbind(ldserver);
218 syslog(LOG_DEBUG, "ldap: No such user was found.");
222 if (found_dn) safestrncpy(found_dn, user_dn, found_dn_size);
223 ldap_memfree(user_dn);
228 int CtdlTryPasswordLDAP(char *user_dn, const char *password)
230 LDAP *ldserver = NULL;
233 if (IsEmptyStr(password)) {
234 syslog(LOG_DEBUG, "ldap: empty passwords are not permitted");
238 syslog(LOG_DEBUG, "ldap: trying to bind as %s", user_dn);
239 i = ctdl_ldap_initialize(&ldserver);
240 if (i == LDAP_SUCCESS) {
241 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
242 i = ldap_simple_bind_s(ldserver, user_dn, password);
243 if (i == LDAP_SUCCESS) {
244 syslog(LOG_DEBUG, "ldap: bind succeeded");
247 syslog(LOG_DEBUG, "ldap: Cannot bind: %s (%d)", ldap_err2string(i), i);
249 ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
250 ldap_unbind(ldserver);
253 if (i == LDAP_SUCCESS) {
261 //return !0 iff property changed.
262 int vcard_set_props_iff_different(struct vCard *v,char *propname,int numvals, char **vals) {
265 for(i=0;i<numvals;i++) {
266 oldval = vcard_get_prop(v,propname,0,i,0);
267 if (oldval == NULL) break;
268 if (strcmp(vals[i],oldval)) break;
271 syslog(LOG_DEBUG, "ldap: vcard property %s, element %d of %d changed from %s to %s\n", propname, i, numvals, oldval, vals[i]);
272 for(i=0;i<numvals;i++) vcard_set_prop(v,propname,vals[i],(i==0) ? 0 : 1);
279 //return !0 iff property changed.
280 int vcard_set_one_prop_iff_different(struct vCard *v,char *propname, char *newfmt, ...) {
283 int changed_something;
284 va_start(args,newfmt);
285 if (-1==vasprintf(&newvalue,newfmt,args)) {
286 syslog(LOG_ERR, "ldap: out of memory!");
289 changed_something = vcard_set_props_iff_different(v,propname,1,&newvalue);
292 return changed_something;
297 * Learn LDAP attributes and stuff them into the vCard.
298 * Returns nonzero if we changed anything.
300 int Ctdl_LDAP_to_vCard(char *ldap_dn, struct vCard *v)
302 int changed_something = 0;
303 LDAP *ldserver = NULL;
306 LDAPMessage *search_result = NULL;
307 LDAPMessage *entry = NULL;
317 char **telephoneNumber;
320 char **facsimileTelephoneNumber;
323 char **homeDirectory;
330 char *attrs[] = { "*","+",NULL};
332 if (!ldap_dn) return(0);
335 if (ctdl_ldap_initialize(&ldserver) != LDAP_SUCCESS) {
339 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
340 ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
342 striplt(CtdlGetConfigStr("c_ldap_bind_dn"));
343 striplt(CtdlGetConfigStr("c_ldap_bind_pw"));
344 syslog(LOG_DEBUG, "ldap: bind DN: %s", CtdlGetConfigStr("c_ldap_bind_dn"));
345 i = ldap_simple_bind_s(ldserver,
346 (!IsEmptyStr(CtdlGetConfigStr("c_ldap_bind_dn")) ? CtdlGetConfigStr("c_ldap_bind_dn") : NULL),
347 (!IsEmptyStr(CtdlGetConfigStr("c_ldap_bind_pw")) ? CtdlGetConfigStr("c_ldap_bind_pw") : NULL)
349 if (i != LDAP_SUCCESS) {
350 syslog(LOG_ERR, "ldap: Cannot bind: %s (%d)", ldap_err2string(i), i);
357 syslog(LOG_DEBUG, "ldap: search: %s", ldap_dn);
358 (void) ldap_search_ext_s(
361 LDAP_SCOPE_SUBTREE, // scope
363 attrs, // attrs (all attributes)
364 0, // attrsonly (attrs + values)
365 NULL, // serverctrls (none)
366 NULL, // clientctrls (none)
368 1, // sizelimit (1 result max)
369 &search_result // res
372 /* Ignore the return value of ldap_search_ext_s(). Sometimes it returns an error even when
373 * the search succeeds. Instead, we check to see whether search_result is still NULL.
375 if (search_result == NULL) {
376 syslog(LOG_DEBUG, "ldap: zero search results were returned");
377 ldap_unbind(ldserver);
381 /* At this point we've got at least one result from our query. If there are multiple
382 * results, we still only look at the first one.
384 entry = ldap_first_entry(ldserver, search_result);
386 syslog(LOG_DEBUG, "ldap: search got user details for vcard.");
387 givenName=ldap_get_values(ldserver, search_result, "givenName");
388 sn=ldap_get_values(ldserver, search_result, "sn");
389 cn=ldap_get_values(ldserver, search_result, "cn");
390 initials=ldap_get_values(ldserver, search_result, "initials");
391 title=ldap_get_values(ldserver, search_result, "title");
392 o=ldap_get_values(ldserver, search_result, "o");
393 street=ldap_get_values(ldserver, search_result, "street");
394 l=ldap_get_values(ldserver, search_result, "l");
395 st=ldap_get_values(ldserver, search_result, "st");
396 postalCode=ldap_get_values(ldserver, search_result, "postalCode");
397 telephoneNumber=ldap_get_values(ldserver, search_result, "telephoneNumber");
398 mobile=ldap_get_values(ldserver, search_result, "mobile");
399 homePhone=ldap_get_values(ldserver, search_result, "homePhone");
400 facsimileTelephoneNumber=ldap_get_values(ldserver, search_result, "facsimileTelephoneNumber");
401 mail=ldap_get_values(ldserver, search_result, "mail");
402 uid=ldap_get_values(ldserver, search_result, "uid");
403 homeDirectory=ldap_get_values(ldserver, search_result, "homeDirectory");
404 uidNumber=ldap_get_values(ldserver, search_result, "uidNumber");
405 loginShell=ldap_get_values(ldserver, search_result, "loginShell");
406 gidNumber=ldap_get_values(ldserver, search_result, "gidNumber");
407 c=ldap_get_values(ldserver, search_result, "c");
408 uuid=ldap_get_values(ldserver, search_result, "entryUUID");
410 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]);
411 if (telephoneNumber) changed_something |= vcard_set_one_prop_iff_different(v,"tel;work","%s",telephoneNumber[0]);
412 if (facsimileTelephoneNumber) changed_something |= vcard_set_one_prop_iff_different(v,"tel;fax","%s",facsimileTelephoneNumber[0]);
413 if (mobile) changed_something |= vcard_set_one_prop_iff_different(v,"tel;cell","%s",mobile[0]);
414 if (homePhone) changed_something |= vcard_set_one_prop_iff_different(v,"tel;home","%s",homePhone[0]);
415 if (givenName && sn) {
417 changed_something |= vcard_set_one_prop_iff_different(v,"n","%s;%s;%s",sn[0],givenName[0],initials[0]);
420 changed_something |= vcard_set_one_prop_iff_different(v,"n","%s;%s",sn[0],givenName[0]);
424 changed_something |= vcard_set_props_iff_different(v,"email;internet",ldap_count_values(mail),mail);
426 if (uuid) changed_something |= vcard_set_one_prop_iff_different(v,"X-uuid","%s",uuid[0]);
427 if (o) changed_something |= vcard_set_one_prop_iff_different(v,"org","%s",o[0]);
428 if (cn) changed_something |= vcard_set_one_prop_iff_different(v,"fn","%s",cn[0]);
429 if (title) changed_something |= vcard_set_one_prop_iff_different(v,"title","%s",title[0]);
431 if (givenName) ldap_value_free(givenName);
432 if (initials) ldap_value_free(initials);
433 if (sn) ldap_value_free(sn);
434 if (cn) ldap_value_free(cn);
435 if (o) ldap_value_free(o);
436 if (street) ldap_value_free(street);
437 if (l) ldap_value_free(l);
438 if (st) ldap_value_free(st);
439 if (postalCode) ldap_value_free(postalCode);
440 if (telephoneNumber) ldap_value_free(telephoneNumber);
441 if (mobile) ldap_value_free(mobile);
442 if (homePhone) ldap_value_free(homePhone);
443 if (facsimileTelephoneNumber) ldap_value_free(facsimileTelephoneNumber);
444 if (mail) ldap_value_free(mail);
445 if (uid) ldap_value_free(uid);
446 if (homeDirectory) ldap_value_free(homeDirectory);
447 if (uidNumber) ldap_value_free(uidNumber);
448 if (loginShell) ldap_value_free(loginShell);
449 if (gidNumber) ldap_value_free(gidNumber);
450 if (c) ldap_value_free(c);
451 if (title) ldap_value_free(title);
452 if (uuid) ldap_value_free(uuid);
454 /* free the results */
455 ldap_msgfree(search_result);
457 /* unbind so we can go back in as the authenticating user */
458 ldap_unbind(ldserver);
459 return(changed_something); /* tell the caller whether we made any changes */
464 * Extract a user's Internet email addresses from LDAP.
465 * Returns zero if we got a valid set of addresses; nonzero for error.
467 int extract_email_addresses_from_ldap(char *ldap_dn, char *emailaddrs)
469 LDAP *ldserver = NULL;
472 LDAPMessage *search_result = NULL;
473 LDAPMessage *entry = NULL;
475 char *attrs[] = { "*","+",NULL};
477 if (!ldap_dn) return(1);
478 if (!emailaddrs) return(1);
480 if (ctdl_ldap_initialize(&ldserver) != LDAP_SUCCESS) {
484 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
485 ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
487 striplt(CtdlGetConfigStr("c_ldap_bind_dn"));
488 striplt(CtdlGetConfigStr("c_ldap_bind_pw"));
489 syslog(LOG_DEBUG, "ldap: bind DN: %s", CtdlGetConfigStr("c_ldap_bind_dn"));
490 i = ldap_simple_bind_s(ldserver,
491 (!IsEmptyStr(CtdlGetConfigStr("c_ldap_bind_dn")) ? CtdlGetConfigStr("c_ldap_bind_dn") : NULL),
492 (!IsEmptyStr(CtdlGetConfigStr("c_ldap_bind_pw")) ? CtdlGetConfigStr("c_ldap_bind_pw") : NULL)
494 if (i != LDAP_SUCCESS) {
495 syslog(LOG_ERR, "ldap: Cannot bind: %s (%d)", ldap_err2string(i), i);
502 syslog(LOG_DEBUG, "ldap: search: %s", ldap_dn);
503 (void) ldap_search_ext_s(
506 LDAP_SCOPE_SUBTREE, // scope
508 attrs, // attrs (all attributes)
509 0, // attrsonly (attrs + values)
510 NULL, // serverctrls (none)
511 NULL, // clientctrls (none)
513 1, // sizelimit (1 result max)
514 &search_result // res
517 /* Ignore the return value of ldap_search_ext_s(). Sometimes it returns an error even when
518 * the search succeeds. Instead, we check to see whether search_result is still NULL.
520 if (search_result == NULL) {
521 syslog(LOG_DEBUG, "ldap: zero search results were returned");
522 ldap_unbind(ldserver);
526 /* At this point we've got at least one result from our query. If there are multiple
527 * results, we still only look at the first one.
529 emailaddrs[0] = 0; /* clear out any previous results */
530 entry = ldap_first_entry(ldserver, search_result);
532 syslog(LOG_DEBUG, "ldap: search got user details");
533 mail=ldap_get_values(ldserver, search_result, "mail");
537 for (q=0; q<ldap_count_values(mail); ++q) {
538 if (IsDirectory(mail[q], 0)) {
539 syslog(LOG_DEBUG, "\035FIXME YES DIRECTORY %s\033[0m", mail[q]);
540 if ((strlen(emailaddrs) + strlen(mail[q]) + 2) > 512) {
541 syslog(LOG_ERR, "ldap: can't fit all email addresses into user record");
544 if (!IsEmptyStr(emailaddrs)) {
545 strcat(emailaddrs, "|");
547 strcat(emailaddrs, mail[q]);
554 /* free the results */
555 ldap_msgfree(search_result);
557 /* unbind so we can go back in as the authenticating user */
558 ldap_unbind(ldserver);
564 * Scan LDAP for users and populate Citadel's user database with everyone
566 void CtdlSynchronizeUsersFromLDAP(void)
568 LDAP *ldserver = NULL;
570 LDAPMessage *search_result = NULL;
571 LDAPMessage *entry = NULL;
572 char *user_dn = NULL;
573 char searchstring[1024];
577 if ((CtdlGetConfigInt("c_auth_mode") != AUTHMODE_LDAP) && (CtdlGetConfigInt("c_auth_mode") != AUTHMODE_LDAP_AD)) {
578 return; // not running LDAP
581 syslog(LOG_INFO, "ldap: synchronizing Citadel user database from LDAP");
583 if (ctdl_ldap_initialize(&ldserver) != LDAP_SUCCESS) {
587 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
588 ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
590 striplt(CtdlGetConfigStr("c_ldap_bind_dn"));
591 striplt(CtdlGetConfigStr("c_ldap_bind_pw"));
592 syslog(LOG_DEBUG, "ldap: bind DN: %s", CtdlGetConfigStr("c_ldap_bind_dn"));
593 i = ldap_simple_bind_s(ldserver,
594 (!IsEmptyStr(CtdlGetConfigStr("c_ldap_bind_dn")) ? CtdlGetConfigStr("c_ldap_bind_dn") : NULL),
595 (!IsEmptyStr(CtdlGetConfigStr("c_ldap_bind_pw")) ? CtdlGetConfigStr("c_ldap_bind_pw") : NULL)
597 if (i != LDAP_SUCCESS) {
598 syslog(LOG_ERR, "ldap: Cannot bind: %s (%d)", ldap_err2string(i), i);
605 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD) {
606 snprintf(searchstring, sizeof(searchstring), "(&(objectClass=user)(objectClass=person)(!(objectClass=computer)))");
608 snprintf(searchstring, sizeof(searchstring), "(objectClass=inetOrgPerson)");
611 syslog(LOG_DEBUG, "ldap: search: %s", searchstring);
612 (void) ldap_search_ext_s(
614 CtdlGetConfigStr("c_ldap_base_dn"), // base
615 LDAP_SCOPE_SUBTREE, // scope
616 searchstring, // filter
617 NULL, // attrs (all attributes)
618 0, // attrsonly (attrs + values)
619 NULL, // serverctrls (none)
620 NULL, // clientctrls (none)
622 INT_MAX, // sizelimit (max)
623 &search_result // result
626 /* Ignore the return value of ldap_search_ext_s(). Sometimes it returns an error even when
627 * the search succeeds. Instead, we check to see whether search_result is still NULL.
629 if (search_result == NULL) {
630 syslog(LOG_DEBUG, "ldap: zero search results were returned");
631 ldap_unbind(ldserver);
635 syslog(LOG_DEBUG, "ldap: %d entries returned", ldap_count_entries(ldserver, search_result));
636 entry = ldap_first_entry(ldserver, search_result);
639 user_dn = ldap_get_dn(ldserver, entry);
641 syslog(LOG_DEBUG, "ldap: found %s", user_dn);
643 int fullname_size = 256;
644 char fullname[256] = { 0 } ;
647 derive_fullname_from_ldap_result(fullname, fullname_size, ldserver, entry);
649 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD) {
650 values = ldap_get_values(ldserver, entry, "objectGUID"); // AD schema: uid hashed from objectGUID
653 uid = abs(HashLittle(values[0], strlen(values[0])));
655 ldap_value_free(values);
659 values = ldap_get_values(ldserver, entry, "uidNumber"); // POSIX schema: uid = uidNumber
662 uid = atoi(values[0]);
664 ldap_value_free(values);
668 syslog(LOG_DEBUG, "\033[33mldap: display name: <%s> , uid = <%d>\033[0m", fullname, uid);
670 // FIXME now create or update the user
675 entry = ldap_next_entry(ldserver, entry);
678 /* free the results */
679 ldap_msgfree(search_result);
681 /* unbind so we can go back in as the authenticating user */
682 ldap_unbind(ldserver);
685 #endif /* HAVE_LDAP */