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
30 int CtdlTryUserLDAP(char *username,
31 char *found_dn, int found_dn_size,
32 char *fullname, int fullname_size,
35 LDAP *ldserver = NULL;
37 LDAPMessage *search_result = NULL;
38 LDAPMessage *entry = NULL;
39 char searchstring[1024];
44 #ifndef LDAP_INITIALIZE
45 if (fullname) safestrncpy(fullname, username, fullname_size);
47 ldserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
48 if (ldserver == NULL) {
49 syslog(LOG_ALERT, "LDAP: Could not connect to %s:%d : %s",
50 config.c_ldap_host, config.c_ldap_port,
56 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
57 ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
59 striplt(config.c_ldap_bind_dn);
60 striplt(config.c_ldap_bind_pw);
61 syslog(LOG_DEBUG, "LDAP bind DN: %s", config.c_ldap_bind_dn);
62 i = ldap_simple_bind_s(ldserver,
63 (!IsEmptyStr(config.c_ldap_bind_dn) ? config.c_ldap_bind_dn : NULL),
64 (!IsEmptyStr(config.c_ldap_bind_pw) ? config.c_ldap_bind_pw : NULL)
66 if (i != LDAP_SUCCESS) {
67 syslog(LOG_ALERT, "LDAP: Cannot bind: %s (%d)", ldap_err2string(i), i);
71 if (ldap_initialize(&ldserver, config.c_ldap_host))
73 syslog(LOG_ALERT, "LDAP: Could not connect to %s:%d : %s",
74 config.c_ldap_host, config.c_ldap_port,
80 striplt(config.c_ldap_bind_dn);
81 striplt(config.c_ldap_bind_pw);
83 syslog(LOG_DEBUG, "LDAP bind DN: %s", config.c_ldap_bind_dn);
84 i = ldap_simple_bind_s(ldserver,
85 (!IsEmptyStr(config.c_ldap_bind_dn) ? config.c_ldap_bind_dn : NULL),
86 (!IsEmptyStr(config.c_ldap_bind_pw) ? config.c_ldap_bind_pw : NULL)
89 if (i != LDAP_SUCCESS) {
90 syslog(LOG_ALERT, "LDAP: Cannot bind: %s (%d)", ldap_err2string(i), i);
99 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
100 sprintf(searchstring, "(sAMAccountName=%s)", username);
103 sprintf(searchstring, "(&(objectclass=posixAccount)(uid=%s))", username);
106 syslog(LOG_DEBUG, "LDAP search: %s", searchstring);
107 (void) ldap_search_ext_s(
109 config.c_ldap_base_dn, /* base */
110 LDAP_SCOPE_SUBTREE, /* scope */
111 searchstring, /* filter */
112 NULL, /* attrs (all attributes) */
113 0, /* attrsonly (attrs + values) */
114 NULL, /* serverctrls (none) */
115 NULL, /* clientctrls (none) */
117 1, /* sizelimit (1 result max) */
118 &search_result /* res */
121 /* Ignore the return value of ldap_search_ext_s(). Sometimes it returns an error even when
122 * the search succeeds. Instead, we check to see whether search_result is still NULL.
124 if (search_result == NULL) {
125 syslog(LOG_DEBUG, "LDAP search: zero results were returned");
126 ldap_unbind(ldserver);
130 /* At this point we've got at least one result from our query. If there are multiple
131 * results, we still only look at the first one.
133 entry = ldap_first_entry(ldserver, search_result);
136 user_dn = ldap_get_dn(ldserver, entry);
138 syslog(LOG_DEBUG, "dn = %s", user_dn);
141 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
142 values = ldap_get_values(ldserver, search_result, "displayName");
145 if (fullname) safestrncpy(fullname, values[0], fullname_size);
146 syslog(LOG_DEBUG, "displayName = %s", values[0]);
148 ldap_value_free(values);
152 values = ldap_get_values(ldserver, search_result, "cn");
155 if (fullname) safestrncpy(fullname, values[0], fullname_size);
156 syslog(LOG_DEBUG, "cn = %s", values[0]);
158 ldap_value_free(values);
162 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
163 values = ldap_get_values(ldserver, search_result, "objectGUID");
167 *uid = abs(HashLittle(values[0], strlen(values[0])));
168 syslog(LOG_DEBUG, "uid hashed from objectGUID = %d", *uid);
171 ldap_value_free(values);
175 values = ldap_get_values(ldserver, search_result, "uidNumber");
178 syslog(LOG_DEBUG, "uidNumber = %s", values[0]);
180 *uid = atoi(values[0]);
183 ldap_value_free(values);
189 /* free the results */
190 ldap_msgfree(search_result);
192 /* unbind so we can go back in as the authenticating user */
193 ldap_unbind(ldserver);
196 syslog(LOG_DEBUG, "No such user was found.");
200 if (found_dn) safestrncpy(found_dn, user_dn, found_dn_size);
201 ldap_memfree(user_dn);
206 int CtdlTryPasswordLDAP(char *user_dn, const char *password)
208 LDAP *ldserver = NULL;
211 if (IsEmptyStr(password)) {
212 syslog(LOG_DEBUG, "LDAP: empty passwords are not permitted");
216 syslog(LOG_DEBUG, "LDAP: trying to bind as %s", user_dn);
217 ldserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
219 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
220 i = ldap_simple_bind_s(ldserver, user_dn, password);
221 if (i == LDAP_SUCCESS) {
222 syslog(LOG_DEBUG, "LDAP: bind succeeded");
225 syslog(LOG_DEBUG, "LDAP: Cannot bind: %s (%d)", ldap_err2string(i), i);
227 ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
228 ldap_unbind(ldserver);
231 if (i == LDAP_SUCCESS) {
239 // return !0 iff property changed.
241 int vcard_set_props_iff_different(struct vCard *v,char *propname,int numvals, char **vals) {
244 for(i=0;i<numvals;i++) {
245 oldval = vcard_get_prop(v,propname,0,i,0);
246 if (oldval == NULL) break;
247 if (strcmp(vals[i],oldval)) break;
250 for(i=0;i<numvals;i++) vcard_set_prop(v,propname,vals[i],(i==0) ? 0 : 1);
257 // return !0 iff property changed.
259 int vcard_set_one_prop_iff_different(struct vCard *v,char *propname, char *newfmt, ...) {
262 int changed_something;
263 va_start(args,newfmt);
264 if (-1==vasprintf(&newvalue,newfmt,args)) {
265 syslog(LOG_ALERT, "Out of memory!");
268 changed_something = vcard_set_props_iff_different(v,propname,1,&newvalue);
271 return changed_something;
275 // Learn LDAP attributes and stuff them into the vCard.
276 // Returns nonzero if we changed anything.
278 int Ctdl_LDAP_to_vCard(char *ldap_dn, struct vCard *v)
280 int changed_something = 0;
281 LDAP *ldserver = NULL;
284 LDAPMessage *search_result = NULL;
285 LDAPMessage *entry = NULL;
295 char **telephoneNumber;
298 char **facsimileTelephoneNumber;
301 char **homeDirectory;
308 char *attrs[] = { "*","+",NULL};
310 if (!ldap_dn) return(0);
312 ldserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
313 if (ldserver == NULL) {
314 syslog(LOG_ALERT, "LDAP: Could not connect to %s:%d : %s",
315 config.c_ldap_host, config.c_ldap_port,
321 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
322 ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
324 striplt(config.c_ldap_bind_dn);
325 striplt(config.c_ldap_bind_pw);
326 syslog(LOG_DEBUG, "LDAP bind DN: %s", config.c_ldap_bind_dn);
327 i = ldap_simple_bind_s(ldserver,
328 (!IsEmptyStr(config.c_ldap_bind_dn) ? config.c_ldap_bind_dn : NULL),
329 (!IsEmptyStr(config.c_ldap_bind_pw) ? config.c_ldap_bind_pw : NULL)
331 if (i != LDAP_SUCCESS) {
332 syslog(LOG_ALERT, "LDAP: Cannot bind: %s (%d)", ldap_err2string(i), i);
339 syslog(LOG_DEBUG, "LDAP search: %s", ldap_dn);
340 (void) ldap_search_ext_s(
343 LDAP_SCOPE_SUBTREE, // scope
345 attrs, // attrs (all attributes)
346 0, // attrsonly (attrs + values)
347 NULL, // serverctrls (none)
348 NULL, // clientctrls (none)
350 1, // sizelimit (1 result max)
351 &search_result // res
354 // Ignore the return value of ldap_search_ext_s(). Sometimes it returns an error even when
355 // the search succeeds. Instead, we check to see whether search_result is still NULL.
357 if (search_result == NULL) {
358 syslog(LOG_DEBUG, "LDAP search: zero results were returned");
359 ldap_unbind(ldserver);
363 // At this point we've got at least one result from our query. If there are multiple
364 // results, we still only look at the first one.
366 entry = ldap_first_entry(ldserver, search_result);
368 syslog(LOG_DEBUG, "LDAP search, got user details for vcard.");
369 givenName=ldap_get_values(ldserver, search_result, "givenName");
370 sn=ldap_get_values(ldserver, search_result, "sn");
371 cn=ldap_get_values(ldserver, search_result, "cn");
372 initials=ldap_get_values(ldserver, search_result, "initials");
373 title=ldap_get_values(ldserver, search_result, "title");
374 o=ldap_get_values(ldserver, search_result, "o");
375 street=ldap_get_values(ldserver, search_result, "street");
376 l=ldap_get_values(ldserver, search_result, "l");
377 st=ldap_get_values(ldserver, search_result, "st");
378 postalCode=ldap_get_values(ldserver, search_result, "postalCode");
379 telephoneNumber=ldap_get_values(ldserver, search_result, "telephoneNumber");
380 mobile=ldap_get_values(ldserver, search_result, "mobile");
381 homePhone=ldap_get_values(ldserver, search_result, "homePhone");
382 facsimileTelephoneNumber=ldap_get_values(ldserver, search_result, "facsimileTelephoneNumber");
383 mail=ldap_get_values(ldserver, search_result, "mail");
384 uid=ldap_get_values(ldserver, search_result, "uid");
385 homeDirectory=ldap_get_values(ldserver, search_result, "homeDirectory");
386 uidNumber=ldap_get_values(ldserver, search_result, "uidNumber");
387 loginShell=ldap_get_values(ldserver, search_result, "loginShell");
388 gidNumber=ldap_get_values(ldserver, search_result, "gidNumber");
389 c=ldap_get_values(ldserver, search_result, "c");
390 uuid=ldap_get_values(ldserver, search_result, "entryUUID");
392 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]);
393 if (telephoneNumber) changed_something |= vcard_set_one_prop_iff_different(v,"tel;work","%s",telephoneNumber[0]);
394 if (facsimileTelephoneNumber) changed_something |= vcard_set_one_prop_iff_different(v,"tel;fax","%s",facsimileTelephoneNumber[0]);
395 if (mobile) changed_something |= vcard_set_one_prop_iff_different(v,"tel;cell","%s",mobile[0]);
396 if (homePhone) changed_something |= vcard_set_one_prop_iff_different(v,"tel;home","%s",homePhone[0]);
397 if (givenName && sn) {
399 changed_something |= vcard_set_one_prop_iff_different(v,"n","%s;%s;%s",sn[0],givenName[0],initials[0]);
402 changed_something |= vcard_set_one_prop_iff_different(v,"n","%s;%s",sn[0],givenName[0]);
406 changed_something |= vcard_set_props_iff_different(v,"email;internet",ldap_count_values(mail),mail);
408 if (uuid) changed_something |= vcard_set_one_prop_iff_different(v,"uid","%s",uuid[0]);
409 if (o) changed_something |= vcard_set_one_prop_iff_different(v,"org","%s",o[0]);
410 if (cn) changed_something |= vcard_set_one_prop_iff_different(v,"fn","%s",cn[0]);
411 if (title) changed_something |= vcard_set_one_prop_iff_different(v,"title","%s",title[0]);
412 if (givenName) ldap_value_free(givenName);
413 if (initials) ldap_value_free(initials);
414 if (sn) ldap_value_free(sn);
415 if (cn) ldap_value_free(cn);
416 if (o) ldap_value_free(o);
417 if (street) ldap_value_free(street);
418 if (l) ldap_value_free(l);
419 if (st) ldap_value_free(st);
420 if (postalCode) ldap_value_free(postalCode);
421 if (telephoneNumber) ldap_value_free(telephoneNumber);
422 if (mobile) ldap_value_free(mobile);
423 if (homePhone) ldap_value_free(homePhone);
424 if (facsimileTelephoneNumber) ldap_value_free(facsimileTelephoneNumber);
425 if (mail) ldap_value_free(mail);
426 if (uid) ldap_value_free(uid);
427 if (homeDirectory) ldap_value_free(homeDirectory);
428 if (uidNumber) ldap_value_free(uidNumber);
429 if (loginShell) ldap_value_free(loginShell);
430 if (gidNumber) ldap_value_free(gidNumber);
431 if (c) ldap_value_free(c);
432 if (title) ldap_value_free(title);
433 if (uuid) ldap_value_free(uuid);
436 ldap_msgfree(search_result); // free the results
437 ldap_unbind(ldserver); // unbind so we can go back in as the authenticating user
438 return(changed_something); // tell the caller whether we made any changes
441 #endif /* HAVE_LDAP */