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-2014 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"
27 #define LDAP_DEPRECATED 1 /* Suppress libldap's warning that we are using deprecated API calls */
33 * Wrapper function for ldap_initialize() that consistently fills in the correct fields
35 int ctdl_ldap_initialize(LDAP **ld) {
40 snprintf(server_url, sizeof server_url, "ldap://%s:%d", config.c_ldap_host, config.c_ldap_port);
41 ret = ldap_initialize(ld, server_url);
42 if (ret != LDAP_SUCCESS) {
43 syslog(LOG_ALERT, "LDAP: Could not connect to %s : %s",
58 * Look up a user in the directory to see if this is an account that can be authenticated
60 int CtdlTryUserLDAP(char *username,
61 char *found_dn, int found_dn_size,
62 char *fullname, int fullname_size,
63 uid_t *uid, int lookup_based_on_username)
65 LDAP *ldserver = NULL;
67 LDAPMessage *search_result = NULL;
68 LDAPMessage *entry = NULL;
69 char searchstring[1024];
74 if (fullname) safestrncpy(fullname, username, fullname_size);
76 if (ctdl_ldap_initialize(&ldserver) != LDAP_SUCCESS) {
80 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
81 ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
83 striplt(config.c_ldap_bind_dn);
84 striplt(config.c_ldap_bind_pw);
85 syslog(LOG_DEBUG, "LDAP bind DN: %s", config.c_ldap_bind_dn);
86 i = ldap_simple_bind_s(ldserver,
87 (!IsEmptyStr(config.c_ldap_bind_dn) ? config.c_ldap_bind_dn : NULL),
88 (!IsEmptyStr(config.c_ldap_bind_pw) ? config.c_ldap_bind_pw : NULL)
90 if (i != LDAP_SUCCESS) {
91 syslog(LOG_ALERT, "LDAP: Cannot bind: %s (%d)", ldap_err2string(i), i);
98 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
99 if (lookup_based_on_username != 0)
100 snprintf(searchstring, sizeof(searchstring), "(displayName=%s)",username);
102 snprintf(searchstring, sizeof(searchstring), "(sAMAccountName=%s)", username);
105 if (lookup_based_on_username != 0)
106 snprintf(searchstring, sizeof(searchstring), "(cn=%s)",username);
108 snprintf(searchstring, sizeof(searchstring), "(&(objectclass=posixAccount)(uid=%s))", username);
111 syslog(LOG_DEBUG, "LDAP search: %s", searchstring);
112 (void) ldap_search_ext_s(
114 config.c_ldap_base_dn, /* base */
115 LDAP_SCOPE_SUBTREE, /* scope */
116 searchstring, /* filter */
117 NULL, /* attrs (all attributes) */
118 0, /* attrsonly (attrs + values) */
119 NULL, /* serverctrls (none) */
120 NULL, /* clientctrls (none) */
122 1, /* sizelimit (1 result max) */
123 &search_result /* res */
126 /* Ignore the return value of ldap_search_ext_s(). Sometimes it returns an error even when
127 * the search succeeds. Instead, we check to see whether search_result is still NULL.
129 if (search_result == NULL) {
130 syslog(LOG_DEBUG, "LDAP search: zero results were returned");
131 ldap_unbind(ldserver);
135 /* At this point we've got at least one result from our query. If there are multiple
136 * results, we still only look at the first one.
138 entry = ldap_first_entry(ldserver, search_result);
141 user_dn = ldap_get_dn(ldserver, entry);
143 syslog(LOG_DEBUG, "dn = %s", user_dn);
146 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
147 values = ldap_get_values(ldserver, search_result, "displayName");
150 if (fullname) safestrncpy(fullname, values[0], fullname_size);
151 syslog(LOG_DEBUG, "displayName = %s", values[0]);
153 ldap_value_free(values);
157 values = ldap_get_values(ldserver, search_result, "cn");
160 if (fullname) safestrncpy(fullname, values[0], fullname_size);
161 syslog(LOG_DEBUG, "cn = %s", values[0]);
163 ldap_value_free(values);
166 /* If we know the username is the CN/displayName, we already set the uid*/
167 if (lookup_based_on_username==0) {
168 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
169 values = ldap_get_values(ldserver, search_result, "objectGUID");
173 *uid = abs(HashLittle(values[0], strlen(values[0])));
174 syslog(LOG_DEBUG, "uid hashed from objectGUID = %d", *uid);
177 ldap_value_free(values);
181 values = ldap_get_values(ldserver, search_result, "uidNumber");
184 syslog(LOG_DEBUG, "uidNumber = %s", values[0]);
186 *uid = atoi(values[0]);
189 ldap_value_free(values);
196 /* free the results */
197 ldap_msgfree(search_result);
199 /* unbind so we can go back in as the authenticating user */
200 ldap_unbind(ldserver);
203 syslog(LOG_DEBUG, "No such user was found.");
207 if (found_dn) safestrncpy(found_dn, user_dn, found_dn_size);
208 ldap_memfree(user_dn);
213 int CtdlTryPasswordLDAP(char *user_dn, const char *password)
215 LDAP *ldserver = NULL;
218 if (IsEmptyStr(password)) {
219 syslog(LOG_DEBUG, "LDAP: empty passwords are not permitted");
223 syslog(LOG_DEBUG, "LDAP: trying to bind as %s", user_dn);
224 i = ctdl_ldap_initialize(&ldserver);
225 if (i == LDAP_SUCCESS) {
226 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
227 i = ldap_simple_bind_s(ldserver, user_dn, password);
228 if (i == LDAP_SUCCESS) {
229 syslog(LOG_DEBUG, "LDAP: bind succeeded");
232 syslog(LOG_DEBUG, "LDAP: Cannot bind: %s (%d)", ldap_err2string(i), i);
234 ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
235 ldap_unbind(ldserver);
238 if (i == LDAP_SUCCESS) {
245 //return !0 iff property changed.
246 int vcard_set_props_iff_different(struct vCard *v,char *propname,int numvals, char **vals) {
249 for(i=0;i<numvals;i++) {
250 oldval = vcard_get_prop(v,propname,0,i,0);
251 if (oldval == NULL) break;
252 if (strcmp(vals[i],oldval)) break;
255 syslog(LOG_DEBUG, "LDAP: vcard property %s, element %d of %d changed from %s to %s\n", propname, i, numvals, oldval, vals[i]);
256 for(i=0;i<numvals;i++) vcard_set_prop(v,propname,vals[i],(i==0) ? 0 : 1);
263 //return !0 iff property changed.
264 int vcard_set_one_prop_iff_different(struct vCard *v,char *propname, char *newfmt, ...) {
267 int changed_something;
268 va_start(args,newfmt);
269 if (-1==vasprintf(&newvalue,newfmt,args)) {
270 syslog(LOG_ALERT, "Out of memory!\n");
273 changed_something = vcard_set_props_iff_different(v,propname,1,&newvalue);
276 return changed_something;
280 * Learn LDAP attributes and stuff them into the vCard.
281 * Returns nonzero if we changed anything.
283 int Ctdl_LDAP_to_vCard(char *ldap_dn, struct vCard *v)
285 int changed_something = 0;
286 LDAP *ldserver = NULL;
289 LDAPMessage *search_result = NULL;
290 LDAPMessage *entry = NULL;
300 char **telephoneNumber;
303 char **facsimileTelephoneNumber;
306 char **homeDirectory;
313 char *attrs[] = { "*","+",NULL};
315 if (!ldap_dn) return(0);
318 if (ctdl_ldap_initialize(&ldserver) != LDAP_SUCCESS) {
322 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
323 ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
325 striplt(config.c_ldap_bind_dn);
326 striplt(config.c_ldap_bind_pw);
327 syslog(LOG_DEBUG, "LDAP bind DN: %s", config.c_ldap_bind_dn);
328 i = ldap_simple_bind_s(ldserver,
329 (!IsEmptyStr(config.c_ldap_bind_dn) ? config.c_ldap_bind_dn : NULL),
330 (!IsEmptyStr(config.c_ldap_bind_pw) ? config.c_ldap_bind_pw : NULL)
332 if (i != LDAP_SUCCESS) {
333 syslog(LOG_ALERT, "LDAP: Cannot bind: %s (%d)", ldap_err2string(i), i);
340 syslog(LOG_DEBUG, "LDAP search: %s", ldap_dn);
341 (void) ldap_search_ext_s(
344 LDAP_SCOPE_SUBTREE, /* scope */
346 attrs, /* attrs (all attributes) */
347 0, /* attrsonly (attrs + values) */
348 NULL, /* serverctrls (none) */
349 NULL, /* clientctrls (none) */
351 1, /* sizelimit (1 result max) */
352 &search_result /* res */
355 /* Ignore the return value of ldap_search_ext_s(). Sometimes it returns an error even when
356 * the search succeeds. Instead, we check to see whether search_result is still NULL.
359 if (search_result == NULL) {
360 syslog(LOG_DEBUG, "LDAP search: zero results were returned");
361 ldap_unbind(ldserver);
365 /* At this point we've got at least one result from our query. If there are multiple
366 * results, we still only look at the first one.
369 entry = ldap_first_entry(ldserver, search_result);
371 syslog(LOG_DEBUG, "LDAP search, got user details for vcard.");
372 givenName=ldap_get_values(ldserver, search_result, "givenName");
373 sn=ldap_get_values(ldserver, search_result, "sn");
374 cn=ldap_get_values(ldserver, search_result, "cn");
375 initials=ldap_get_values(ldserver, search_result, "initials");
376 title=ldap_get_values(ldserver, search_result, "title");
377 o=ldap_get_values(ldserver, search_result, "o");
378 street=ldap_get_values(ldserver, search_result, "street");
379 l=ldap_get_values(ldserver, search_result, "l");
380 st=ldap_get_values(ldserver, search_result, "st");
381 postalCode=ldap_get_values(ldserver, search_result, "postalCode");
382 telephoneNumber=ldap_get_values(ldserver, search_result, "telephoneNumber");
383 mobile=ldap_get_values(ldserver, search_result, "mobile");
384 homePhone=ldap_get_values(ldserver, search_result, "homePhone");
385 facsimileTelephoneNumber=ldap_get_values(ldserver, search_result, "facsimileTelephoneNumber");
386 mail=ldap_get_values(ldserver, search_result, "mail");
387 uid=ldap_get_values(ldserver, search_result, "uid");
388 homeDirectory=ldap_get_values(ldserver, search_result, "homeDirectory");
389 uidNumber=ldap_get_values(ldserver, search_result, "uidNumber");
390 loginShell=ldap_get_values(ldserver, search_result, "loginShell");
391 gidNumber=ldap_get_values(ldserver, search_result, "gidNumber");
392 c=ldap_get_values(ldserver, search_result, "c");
393 uuid=ldap_get_values(ldserver, search_result, "entryUUID");
395 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]);
396 if (telephoneNumber) changed_something |= vcard_set_one_prop_iff_different(v,"tel;work","%s",telephoneNumber[0]);
397 if (facsimileTelephoneNumber) changed_something |= vcard_set_one_prop_iff_different(v,"tel;fax","%s",facsimileTelephoneNumber[0]);
398 if (mobile) changed_something |= vcard_set_one_prop_iff_different(v,"tel;cell","%s",mobile[0]);
399 if (homePhone) changed_something |= vcard_set_one_prop_iff_different(v,"tel;home","%s",homePhone[0]);
400 if (givenName && sn) {
402 changed_something |= vcard_set_one_prop_iff_different(v,"n","%s;%s;%s",sn[0],givenName[0],initials[0]);
405 changed_something |= vcard_set_one_prop_iff_different(v,"n","%s;%s",sn[0],givenName[0]);
409 changed_something |= vcard_set_props_iff_different(v,"email;internet",ldap_count_values(mail),mail);
411 if (uuid) changed_something |= vcard_set_one_prop_iff_different(v,"X-uuid","%s",uuid[0]);
412 if (o) changed_something |= vcard_set_one_prop_iff_different(v,"org","%s",o[0]);
413 if (cn) changed_something |= vcard_set_one_prop_iff_different(v,"fn","%s",cn[0]);
414 if (title) changed_something |= vcard_set_one_prop_iff_different(v,"title","%s",title[0]);
416 if (givenName) ldap_value_free(givenName);
417 if (initials) ldap_value_free(initials);
418 if (sn) ldap_value_free(sn);
419 if (cn) ldap_value_free(cn);
420 if (o) ldap_value_free(o);
421 if (street) ldap_value_free(street);
422 if (l) ldap_value_free(l);
423 if (st) ldap_value_free(st);
424 if (postalCode) ldap_value_free(postalCode);
425 if (telephoneNumber) ldap_value_free(telephoneNumber);
426 if (mobile) ldap_value_free(mobile);
427 if (homePhone) ldap_value_free(homePhone);
428 if (facsimileTelephoneNumber) ldap_value_free(facsimileTelephoneNumber);
429 if (mail) ldap_value_free(mail);
430 if (uid) ldap_value_free(uid);
431 if (homeDirectory) ldap_value_free(homeDirectory);
432 if (uidNumber) ldap_value_free(uidNumber);
433 if (loginShell) ldap_value_free(loginShell);
434 if (gidNumber) ldap_value_free(gidNumber);
435 if (c) ldap_value_free(c);
436 if (title) ldap_value_free(title);
437 if (uuid) ldap_value_free(uuid);
439 /* free the results */
440 ldap_msgfree(search_result);
442 /* unbind so we can go back in as the authenticating user */
443 ldap_unbind(ldserver);
445 return(changed_something); /* tell the caller whether we made any changes */
448 #endif /* HAVE_LDAP */