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);
67 * Utility function, supply a search result and get back the uid from the first result
69 uid_t derive_uid_from_ldap(LDAP *ldserver, LDAPMessage *entry)
74 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD) {
75 values = ldap_get_values(ldserver, entry, "objectGUID"); // AD schema: uid hashed from objectGUID
78 uid = abs(HashLittle(values[0], strlen(values[0])));
80 ldap_value_free(values);
84 values = ldap_get_values(ldserver, entry, "uidNumber"); // POSIX schema: uid = uidNumber
87 uid = atoi(values[0]);
89 ldap_value_free(values);
93 syslog(LOG_DEBUG, "\033[31mldap: uid: <%d> \033[0m", uid);
101 * Wrapper function for ldap_initialize() that consistently fills in the correct fields
103 int ctdl_ldap_initialize(LDAP **ld) {
105 char server_url[256];
108 snprintf(server_url, sizeof server_url, "ldap://%s:%d", CtdlGetConfigStr("c_ldap_host"), CtdlGetConfigInt("c_ldap_port"));
109 ret = ldap_initialize(ld, server_url);
110 if (ret != LDAP_SUCCESS) {
111 syslog(LOG_ERR, "ldap: could not connect to %s : %m", server_url);
121 * Look up a user in the directory to see if this is an account that can be authenticated
123 int CtdlTryUserLDAP(char *username,
124 char *found_dn, int found_dn_size,
125 char *fullname, int fullname_size,
126 uid_t *uid, int lookup_based_on_username)
128 LDAP *ldserver = NULL;
130 LDAPMessage *search_result = NULL;
131 LDAPMessage *entry = NULL;
132 char searchstring[1024];
134 char *user_dn = NULL;
136 if (fullname) safestrncpy(fullname, username, fullname_size);
138 if (ctdl_ldap_initialize(&ldserver) != LDAP_SUCCESS) {
142 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
143 ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
145 striplt(CtdlGetConfigStr("c_ldap_bind_dn"));
146 striplt(CtdlGetConfigStr("c_ldap_bind_pw"));
147 syslog(LOG_DEBUG, "ldap: bind DN: %s", CtdlGetConfigStr("c_ldap_bind_dn"));
148 i = ldap_simple_bind_s(ldserver,
149 (!IsEmptyStr(CtdlGetConfigStr("c_ldap_bind_dn")) ? CtdlGetConfigStr("c_ldap_bind_dn") : NULL),
150 (!IsEmptyStr(CtdlGetConfigStr("c_ldap_bind_pw")) ? CtdlGetConfigStr("c_ldap_bind_pw") : NULL)
152 if (i != LDAP_SUCCESS) {
153 syslog(LOG_ERR, "ldap: Cannot bind: %s (%d)", ldap_err2string(i), i);
160 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD) {
161 if (lookup_based_on_username != 0)
162 snprintf(searchstring, sizeof(searchstring), "(displayName=%s)",username);
164 snprintf(searchstring, sizeof(searchstring), "(sAMAccountName=%s)", username);
167 if (lookup_based_on_username != 0) {
168 snprintf(searchstring, sizeof(searchstring), "(cn=%s)",username);
171 snprintf(searchstring, sizeof(searchstring), "(&(objectclass=posixAccount)(uid=%s))", username);
175 syslog(LOG_DEBUG, "ldap: search: %s", searchstring);
176 (void) ldap_search_ext_s(
178 CtdlGetConfigStr("c_ldap_base_dn"), /* base */
179 LDAP_SCOPE_SUBTREE, /* scope */
180 searchstring, /* filter */
181 NULL, /* attrs (all attributes) */
182 0, /* attrsonly (attrs + values) */
183 NULL, /* serverctrls (none) */
184 NULL, /* clientctrls (none) */
186 1, /* sizelimit (1 result max) */
187 &search_result /* res */
190 /* Ignore the return value of ldap_search_ext_s(). Sometimes it returns an error even when
191 * the search succeeds. Instead, we check to see whether search_result is still NULL.
193 if (search_result == NULL) {
194 syslog(LOG_DEBUG, "ldap: zero search results were returned");
195 ldap_unbind(ldserver);
199 /* At this point we've got at least one result from our query. If there are multiple
200 * results, we still only look at the first one.
202 entry = ldap_first_entry(ldserver, search_result);
205 user_dn = ldap_get_dn(ldserver, entry);
207 syslog(LOG_DEBUG, "ldap: dn = %s", user_dn);
210 derive_fullname_from_ldap_result(fullname, fullname_size, ldserver, search_result);
212 /* If we know the username is the CN/displayName, we already set the uid*/
213 // FIXME old skool crap , fix this
214 if (lookup_based_on_username == 0) {
215 *uid = derive_uid_from_ldap(ldserver, search_result);
219 /* free the results */
220 ldap_msgfree(search_result);
222 /* unbind so we can go back in as the authenticating user */
223 ldap_unbind(ldserver);
226 syslog(LOG_DEBUG, "ldap: No such user was found.");
230 if (found_dn) safestrncpy(found_dn, user_dn, found_dn_size);
231 ldap_memfree(user_dn);
236 int CtdlTryPasswordLDAP(char *user_dn, const char *password)
238 LDAP *ldserver = NULL;
241 if (IsEmptyStr(password)) {
242 syslog(LOG_DEBUG, "ldap: empty passwords are not permitted");
246 syslog(LOG_DEBUG, "ldap: trying to bind as %s", user_dn);
247 i = ctdl_ldap_initialize(&ldserver);
248 if (i == LDAP_SUCCESS) {
249 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
250 i = ldap_simple_bind_s(ldserver, user_dn, password);
251 if (i == LDAP_SUCCESS) {
252 syslog(LOG_DEBUG, "ldap: bind succeeded");
255 syslog(LOG_DEBUG, "ldap: Cannot bind: %s (%d)", ldap_err2string(i), i);
257 ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
258 ldap_unbind(ldserver);
261 if (i == LDAP_SUCCESS) {
269 //return !0 iff property changed.
270 int vcard_set_props_iff_different(struct vCard *v,char *propname,int numvals, char **vals) {
273 for(i=0;i<numvals;i++) {
274 oldval = vcard_get_prop(v,propname,0,i,0);
275 if (oldval == NULL) break;
276 if (strcmp(vals[i],oldval)) break;
279 syslog(LOG_DEBUG, "ldap: vcard property %s, element %d of %d changed from %s to %s\n", propname, i, numvals, oldval, vals[i]);
280 for(i=0;i<numvals;i++) vcard_set_prop(v,propname,vals[i],(i==0) ? 0 : 1);
287 //return !0 iff property changed.
288 int vcard_set_one_prop_iff_different(struct vCard *v,char *propname, char *newfmt, ...) {
291 int changed_something;
292 va_start(args,newfmt);
293 if (-1==vasprintf(&newvalue,newfmt,args)) {
294 syslog(LOG_ERR, "ldap: out of memory!");
297 changed_something = vcard_set_props_iff_different(v,propname,1,&newvalue);
300 return changed_something;
305 * Learn LDAP attributes and stuff them into the vCard.
306 * Returns nonzero if we changed anything.
308 int Ctdl_LDAP_to_vCard(char *ldap_dn, struct vCard *v)
310 int changed_something = 0;
311 LDAP *ldserver = NULL;
314 LDAPMessage *search_result = NULL;
315 LDAPMessage *entry = NULL;
325 char **telephoneNumber;
328 char **facsimileTelephoneNumber;
331 char **homeDirectory;
338 char *attrs[] = { "*","+",NULL};
340 if (!ldap_dn) return(0);
343 if (ctdl_ldap_initialize(&ldserver) != LDAP_SUCCESS) {
347 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
348 ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
350 striplt(CtdlGetConfigStr("c_ldap_bind_dn"));
351 striplt(CtdlGetConfigStr("c_ldap_bind_pw"));
352 syslog(LOG_DEBUG, "ldap: bind DN: %s", CtdlGetConfigStr("c_ldap_bind_dn"));
353 i = ldap_simple_bind_s(ldserver,
354 (!IsEmptyStr(CtdlGetConfigStr("c_ldap_bind_dn")) ? CtdlGetConfigStr("c_ldap_bind_dn") : NULL),
355 (!IsEmptyStr(CtdlGetConfigStr("c_ldap_bind_pw")) ? CtdlGetConfigStr("c_ldap_bind_pw") : NULL)
357 if (i != LDAP_SUCCESS) {
358 syslog(LOG_ERR, "ldap: Cannot bind: %s (%d)", ldap_err2string(i), i);
365 syslog(LOG_DEBUG, "ldap: search: %s", ldap_dn);
366 (void) ldap_search_ext_s(
369 LDAP_SCOPE_SUBTREE, // scope
371 attrs, // attrs (all attributes)
372 0, // attrsonly (attrs + values)
373 NULL, // serverctrls (none)
374 NULL, // clientctrls (none)
376 1, // sizelimit (1 result max)
377 &search_result // res
380 /* Ignore the return value of ldap_search_ext_s(). Sometimes it returns an error even when
381 * the search succeeds. Instead, we check to see whether search_result is still NULL.
383 if (search_result == NULL) {
384 syslog(LOG_DEBUG, "ldap: zero search results were returned");
385 ldap_unbind(ldserver);
389 /* At this point we've got at least one result from our query. If there are multiple
390 * results, we still only look at the first one.
392 entry = ldap_first_entry(ldserver, search_result);
394 syslog(LOG_DEBUG, "ldap: search got user details for vcard.");
395 givenName=ldap_get_values(ldserver, search_result, "givenName");
396 sn=ldap_get_values(ldserver, search_result, "sn");
397 cn=ldap_get_values(ldserver, search_result, "cn");
398 initials=ldap_get_values(ldserver, search_result, "initials");
399 title=ldap_get_values(ldserver, search_result, "title");
400 o=ldap_get_values(ldserver, search_result, "o");
401 street=ldap_get_values(ldserver, search_result, "street");
402 l=ldap_get_values(ldserver, search_result, "l");
403 st=ldap_get_values(ldserver, search_result, "st");
404 postalCode=ldap_get_values(ldserver, search_result, "postalCode");
405 telephoneNumber=ldap_get_values(ldserver, search_result, "telephoneNumber");
406 mobile=ldap_get_values(ldserver, search_result, "mobile");
407 homePhone=ldap_get_values(ldserver, search_result, "homePhone");
408 facsimileTelephoneNumber=ldap_get_values(ldserver, search_result, "facsimileTelephoneNumber");
409 mail=ldap_get_values(ldserver, search_result, "mail");
410 uid=ldap_get_values(ldserver, search_result, "uid");
411 homeDirectory=ldap_get_values(ldserver, search_result, "homeDirectory");
412 uidNumber=ldap_get_values(ldserver, search_result, "uidNumber");
413 loginShell=ldap_get_values(ldserver, search_result, "loginShell");
414 gidNumber=ldap_get_values(ldserver, search_result, "gidNumber");
415 c=ldap_get_values(ldserver, search_result, "c");
416 uuid=ldap_get_values(ldserver, search_result, "entryUUID");
418 if (street && l && st && postalCode && c) changed_something |= vcard_set_one_prop_iff_different(v,"adr",";;%s;%s;%s;%s;%s",street[0],l[0],st[0],postalCode[0],c[0]);
419 if (telephoneNumber) changed_something |= vcard_set_one_prop_iff_different(v,"tel;work","%s",telephoneNumber[0]);
420 if (facsimileTelephoneNumber) changed_something |= vcard_set_one_prop_iff_different(v,"tel;fax","%s",facsimileTelephoneNumber[0]);
421 if (mobile) changed_something |= vcard_set_one_prop_iff_different(v,"tel;cell","%s",mobile[0]);
422 if (homePhone) changed_something |= vcard_set_one_prop_iff_different(v,"tel;home","%s",homePhone[0]);
423 if (givenName && sn) {
425 changed_something |= vcard_set_one_prop_iff_different(v,"n","%s;%s;%s",sn[0],givenName[0],initials[0]);
428 changed_something |= vcard_set_one_prop_iff_different(v,"n","%s;%s",sn[0],givenName[0]);
432 changed_something |= vcard_set_props_iff_different(v,"email;internet",ldap_count_values(mail),mail);
434 if (uuid) changed_something |= vcard_set_one_prop_iff_different(v,"X-uuid","%s",uuid[0]);
435 if (o) changed_something |= vcard_set_one_prop_iff_different(v,"org","%s",o[0]);
436 if (cn) changed_something |= vcard_set_one_prop_iff_different(v,"fn","%s",cn[0]);
437 if (title) changed_something |= vcard_set_one_prop_iff_different(v,"title","%s",title[0]);
439 if (givenName) ldap_value_free(givenName);
440 if (initials) ldap_value_free(initials);
441 if (sn) ldap_value_free(sn);
442 if (cn) ldap_value_free(cn);
443 if (o) ldap_value_free(o);
444 if (street) ldap_value_free(street);
445 if (l) ldap_value_free(l);
446 if (st) ldap_value_free(st);
447 if (postalCode) ldap_value_free(postalCode);
448 if (telephoneNumber) ldap_value_free(telephoneNumber);
449 if (mobile) ldap_value_free(mobile);
450 if (homePhone) ldap_value_free(homePhone);
451 if (facsimileTelephoneNumber) ldap_value_free(facsimileTelephoneNumber);
452 if (mail) ldap_value_free(mail);
453 if (uid) ldap_value_free(uid);
454 if (homeDirectory) ldap_value_free(homeDirectory);
455 if (uidNumber) ldap_value_free(uidNumber);
456 if (loginShell) ldap_value_free(loginShell);
457 if (gidNumber) ldap_value_free(gidNumber);
458 if (c) ldap_value_free(c);
459 if (title) ldap_value_free(title);
460 if (uuid) ldap_value_free(uuid);
462 /* free the results */
463 ldap_msgfree(search_result);
465 /* unbind so we can go back in as the authenticating user */
466 ldap_unbind(ldserver);
467 return(changed_something); /* tell the caller whether we made any changes */
472 * Extract a user's Internet email addresses from LDAP.
473 * Returns zero if we got a valid set of addresses; nonzero for error.
475 int extract_email_addresses_from_ldap(char *ldap_dn, char *emailaddrs)
477 LDAP *ldserver = NULL;
480 LDAPMessage *search_result = NULL;
481 LDAPMessage *entry = NULL;
483 char *attrs[] = { "*","+",NULL};
485 if (!ldap_dn) return(1);
486 if (!emailaddrs) return(1);
488 if (ctdl_ldap_initialize(&ldserver) != LDAP_SUCCESS) {
492 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
493 ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
495 striplt(CtdlGetConfigStr("c_ldap_bind_dn"));
496 striplt(CtdlGetConfigStr("c_ldap_bind_pw"));
497 syslog(LOG_DEBUG, "ldap: bind DN: %s", CtdlGetConfigStr("c_ldap_bind_dn"));
498 i = ldap_simple_bind_s(ldserver,
499 (!IsEmptyStr(CtdlGetConfigStr("c_ldap_bind_dn")) ? CtdlGetConfigStr("c_ldap_bind_dn") : NULL),
500 (!IsEmptyStr(CtdlGetConfigStr("c_ldap_bind_pw")) ? CtdlGetConfigStr("c_ldap_bind_pw") : NULL)
502 if (i != LDAP_SUCCESS) {
503 syslog(LOG_ERR, "ldap: Cannot bind: %s (%d)", ldap_err2string(i), i);
510 syslog(LOG_DEBUG, "ldap: search: %s", ldap_dn);
511 (void) ldap_search_ext_s(
514 LDAP_SCOPE_SUBTREE, // scope
516 attrs, // attrs (all attributes)
517 0, // attrsonly (attrs + values)
518 NULL, // serverctrls (none)
519 NULL, // clientctrls (none)
521 1, // sizelimit (1 result max)
522 &search_result // res
525 /* Ignore the return value of ldap_search_ext_s(). Sometimes it returns an error even when
526 * the search succeeds. Instead, we check to see whether search_result is still NULL.
528 if (search_result == NULL) {
529 syslog(LOG_DEBUG, "ldap: zero search results were returned");
530 ldap_unbind(ldserver);
534 /* At this point we've got at least one result from our query. If there are multiple
535 * results, we still only look at the first one.
537 emailaddrs[0] = 0; /* clear out any previous results */
538 entry = ldap_first_entry(ldserver, search_result);
540 syslog(LOG_DEBUG, "ldap: search got user details");
541 mail=ldap_get_values(ldserver, search_result, "mail");
545 for (q=0; q<ldap_count_values(mail); ++q) {
546 if (IsDirectory(mail[q], 0)) {
547 syslog(LOG_DEBUG, "\035FIXME YES DIRECTORY %s\033[0m", mail[q]);
548 if ((strlen(emailaddrs) + strlen(mail[q]) + 2) > 512) {
549 syslog(LOG_ERR, "ldap: can't fit all email addresses into user record");
552 if (!IsEmptyStr(emailaddrs)) {
553 strcat(emailaddrs, "|");
555 strcat(emailaddrs, mail[q]);
562 /* free the results */
563 ldap_msgfree(search_result);
565 /* unbind so we can go back in as the authenticating user */
566 ldap_unbind(ldserver);
572 * Scan LDAP for users and populate Citadel's user database with everyone
574 void CtdlSynchronizeUsersFromLDAP(void)
576 LDAP *ldserver = NULL;
578 LDAPMessage *search_result = NULL;
579 LDAPMessage *entry = NULL;
580 char *user_dn = NULL;
581 char searchstring[1024];
584 if ((CtdlGetConfigInt("c_auth_mode") != AUTHMODE_LDAP) && (CtdlGetConfigInt("c_auth_mode") != AUTHMODE_LDAP_AD)) {
585 return; // not running LDAP
588 syslog(LOG_INFO, "ldap: synchronizing Citadel user database from LDAP");
590 if (ctdl_ldap_initialize(&ldserver) != LDAP_SUCCESS) {
594 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
595 ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
597 striplt(CtdlGetConfigStr("c_ldap_bind_dn"));
598 striplt(CtdlGetConfigStr("c_ldap_bind_pw"));
599 syslog(LOG_DEBUG, "ldap: bind DN: %s", CtdlGetConfigStr("c_ldap_bind_dn"));
600 i = ldap_simple_bind_s(ldserver,
601 (!IsEmptyStr(CtdlGetConfigStr("c_ldap_bind_dn")) ? CtdlGetConfigStr("c_ldap_bind_dn") : NULL),
602 (!IsEmptyStr(CtdlGetConfigStr("c_ldap_bind_pw")) ? CtdlGetConfigStr("c_ldap_bind_pw") : NULL)
604 if (i != LDAP_SUCCESS) {
605 syslog(LOG_ERR, "ldap: Cannot bind: %s (%d)", ldap_err2string(i), i);
612 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD) {
613 snprintf(searchstring, sizeof(searchstring), "(&(objectClass=user)(objectClass=person)(!(objectClass=computer)))");
615 snprintf(searchstring, sizeof(searchstring), "(objectClass=inetOrgPerson)");
618 syslog(LOG_DEBUG, "ldap: search: %s", searchstring);
619 (void) ldap_search_ext_s(
621 CtdlGetConfigStr("c_ldap_base_dn"), // base
622 LDAP_SCOPE_SUBTREE, // scope
623 searchstring, // filter
624 NULL, // attrs (all attributes)
625 0, // attrsonly (attrs + values)
626 NULL, // serverctrls (none)
627 NULL, // clientctrls (none)
629 INT_MAX, // sizelimit (max)
630 &search_result // result
633 /* Ignore the return value of ldap_search_ext_s(). Sometimes it returns an error even when
634 * the search succeeds. Instead, we check to see whether search_result is still NULL.
636 if (search_result == NULL) {
637 syslog(LOG_DEBUG, "ldap: zero search results were returned");
638 ldap_unbind(ldserver);
642 syslog(LOG_DEBUG, "ldap: %d entries returned", ldap_count_entries(ldserver, search_result));
643 entry = ldap_first_entry(ldserver, search_result);
646 user_dn = ldap_get_dn(ldserver, entry);
648 syslog(LOG_DEBUG, "ldap: found %s", user_dn);
650 int fullname_size = 256;
651 char fullname[256] = { 0 } ;
654 derive_fullname_from_ldap_result(fullname, fullname_size, ldserver, entry);
655 uid = derive_uid_from_ldap(ldserver, entry);
656 syslog(LOG_DEBUG, "\033[33mldap: display name: <%s> , uid = <%d>\033[0m", fullname, uid);
658 // FIXME now create or update the user
663 entry = ldap_next_entry(ldserver, entry);
666 /* free the results */
667 ldap_msgfree(search_result);
669 /* unbind so we can go back in as the authenticating user */
670 ldap_unbind(ldserver);
673 #endif /* HAVE_LDAP */