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 if (fullname) safestrncpy(fullname, username, fullname_size);
46 ldserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
47 if (ldserver == NULL) {
48 syslog(LOG_ALERT, "LDAP: Could not connect to %s:%d : %s",
49 config.c_ldap_host, config.c_ldap_port,
55 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
56 ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
58 striplt(config.c_ldap_bind_dn);
59 striplt(config.c_ldap_bind_pw);
60 syslog(LOG_DEBUG, "LDAP bind DN: %s", config.c_ldap_bind_dn);
61 i = ldap_simple_bind_s(ldserver,
62 (!IsEmptyStr(config.c_ldap_bind_dn) ? config.c_ldap_bind_dn : NULL),
63 (!IsEmptyStr(config.c_ldap_bind_pw) ? config.c_ldap_bind_pw : NULL)
65 if (i != LDAP_SUCCESS) {
66 syslog(LOG_ALERT, "LDAP: Cannot bind: %s (%d)", ldap_err2string(i), i);
73 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
74 snprintf(searchstring, sizeof(searchstring), "(sAMAccountName=%s)", username);
77 snprintf(searchstring, sizeof(searchstring), "(&(objectclass=posixAccount)(uid=%s))", username);
80 syslog(LOG_DEBUG, "LDAP search: %s", searchstring);
81 (void) ldap_search_ext_s(
83 config.c_ldap_base_dn, /* base */
84 LDAP_SCOPE_SUBTREE, /* scope */
85 searchstring, /* filter */
86 NULL, /* attrs (all attributes) */
87 0, /* attrsonly (attrs + values) */
88 NULL, /* serverctrls (none) */
89 NULL, /* clientctrls (none) */
91 1, /* sizelimit (1 result max) */
92 &search_result /* res */
95 /* Ignore the return value of ldap_search_ext_s(). Sometimes it returns an error even when
96 * the search succeeds. Instead, we check to see whether search_result is still NULL.
98 if (search_result == NULL) {
99 syslog(LOG_DEBUG, "LDAP search: zero results were returned");
100 ldap_unbind(ldserver);
104 /* At this point we've got at least one result from our query. If there are multiple
105 * results, we still only look at the first one.
107 entry = ldap_first_entry(ldserver, search_result);
110 user_dn = ldap_get_dn(ldserver, entry);
112 syslog(LOG_DEBUG, "dn = %s", user_dn);
115 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
116 values = ldap_get_values(ldserver, search_result, "displayName");
119 if (fullname) safestrncpy(fullname, values[0], fullname_size);
120 syslog(LOG_DEBUG, "displayName = %s", values[0]);
122 ldap_value_free(values);
126 values = ldap_get_values(ldserver, search_result, "cn");
129 if (fullname) safestrncpy(fullname, values[0], fullname_size);
130 syslog(LOG_DEBUG, "cn = %s", values[0]);
132 ldap_value_free(values);
136 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
137 values = ldap_get_values(ldserver, search_result, "objectGUID");
141 *uid = abs(HashLittle(values[0], strlen(values[0])));
142 syslog(LOG_DEBUG, "uid hashed from objectGUID = %d", *uid);
145 ldap_value_free(values);
149 values = ldap_get_values(ldserver, search_result, "uidNumber");
152 syslog(LOG_DEBUG, "uidNumber = %s", values[0]);
154 *uid = atoi(values[0]);
157 ldap_value_free(values);
163 /* free the results */
164 ldap_msgfree(search_result);
166 /* unbind so we can go back in as the authenticating user */
167 ldap_unbind(ldserver);
170 syslog(LOG_DEBUG, "No such user was found.");
174 if (found_dn) safestrncpy(found_dn, user_dn, found_dn_size);
175 ldap_memfree(user_dn);
180 int CtdlTryPasswordLDAP(char *user_dn, const char *password)
182 LDAP *ldserver = NULL;
185 if (IsEmptyStr(password)) {
186 syslog(LOG_DEBUG, "LDAP: empty passwords are not permitted");
190 syslog(LOG_DEBUG, "LDAP: trying to bind as %s", user_dn);
191 ldserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
193 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
194 i = ldap_simple_bind_s(ldserver, user_dn, password);
195 if (i == LDAP_SUCCESS) {
196 syslog(LOG_DEBUG, "LDAP: bind succeeded");
199 syslog(LOG_DEBUG, "LDAP: Cannot bind: %s (%d)", ldap_err2string(i), i);
201 ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
202 ldap_unbind(ldserver);
205 if (i == LDAP_SUCCESS) {
212 //return !0 iff property changed.
213 int vcard_set_props_iff_different(struct vCard *v,char *propname,int numvals, char **vals) {
216 for(i=0;i<numvals;i++) {
217 oldval = vcard_get_prop(v,propname,0,i,0);
218 if (oldval == NULL) break;
219 if (strcmp(vals[i],oldval)) break;
222 for(i=0;i<numvals;i++) vcard_set_prop(v,propname,vals[i],(i==0) ? 0 : 1);
229 //return !0 iff property changed.
230 int vcard_set_one_prop_iff_different(struct vCard *v,char *propname, char *newfmt, ...) {
233 int changed_something;
234 va_start(args,newfmt);
235 if (-1==vasprintf(&newvalue,newfmt,args)) {
236 syslog(LOG_ALERT, "Out of memory!\n");
239 changed_something = vcard_set_props_iff_different(v,propname,1,&newvalue);
242 return changed_something;
246 * Learn LDAP attributes and stuff them into the vCard.
247 * Returns nonzero if we changed anything.
249 int Ctdl_LDAP_to_vCard(char *ldap_dn, struct vCard *v)
251 int changed_something = 0;
252 LDAP *ldserver = NULL;
255 LDAPMessage *search_result = NULL;
256 LDAPMessage *entry = NULL;
266 char **telephoneNumber;
269 char **facsimileTelephoneNumber;
272 char **homeDirectory;
279 char *attrs[] = { "*","+",NULL};
281 if (!ldap_dn) return(0);
283 ldserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
284 if (ldserver == NULL) {
285 syslog(LOG_ALERT, "LDAP: Could not connect to %s:%d : %s",
286 config.c_ldap_host, config.c_ldap_port,
292 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
293 ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
295 striplt(config.c_ldap_bind_dn);
296 striplt(config.c_ldap_bind_pw);
297 syslog(LOG_DEBUG, "LDAP bind DN: %s", config.c_ldap_bind_dn);
298 i = ldap_simple_bind_s(ldserver,
299 (!IsEmptyStr(config.c_ldap_bind_dn) ? config.c_ldap_bind_dn : NULL),
300 (!IsEmptyStr(config.c_ldap_bind_pw) ? config.c_ldap_bind_pw : NULL)
302 if (i != LDAP_SUCCESS) {
303 syslog(LOG_ALERT, "LDAP: Cannot bind: %s (%d)", ldap_err2string(i), i);
310 syslog(LOG_DEBUG, "LDAP search: %s", ldap_dn);
311 (void) ldap_search_ext_s(
314 LDAP_SCOPE_SUBTREE, /* scope */
316 attrs, /* attrs (all attributes) */
317 0, /* attrsonly (attrs + values) */
318 NULL, /* serverctrls (none) */
319 NULL, /* clientctrls (none) */
321 1, /* sizelimit (1 result max) */
322 &search_result /* res */
325 /* Ignore the return value of ldap_search_ext_s(). Sometimes it returns an error even when
326 * the search succeeds. Instead, we check to see whether search_result is still NULL.
329 if (search_result == NULL) {
330 syslog(LOG_DEBUG, "LDAP search: zero results were returned");
331 ldap_unbind(ldserver);
335 /* At this point we've got at least one result from our query. If there are multiple
336 * results, we still only look at the first one.
339 entry = ldap_first_entry(ldserver, search_result);
341 syslog(LOG_DEBUG, "LDAP search, got user details for vcard.");
342 givenName=ldap_get_values(ldserver, search_result, "givenName");
343 sn=ldap_get_values(ldserver, search_result, "sn");
344 cn=ldap_get_values(ldserver, search_result, "cn");
345 initials=ldap_get_values(ldserver, search_result, "initials");
346 title=ldap_get_values(ldserver, search_result, "title");
347 o=ldap_get_values(ldserver, search_result, "o");
348 street=ldap_get_values(ldserver, search_result, "street");
349 l=ldap_get_values(ldserver, search_result, "l");
350 st=ldap_get_values(ldserver, search_result, "st");
351 postalCode=ldap_get_values(ldserver, search_result, "postalCode");
352 telephoneNumber=ldap_get_values(ldserver, search_result, "telephoneNumber");
353 mobile=ldap_get_values(ldserver, search_result, "mobile");
354 homePhone=ldap_get_values(ldserver, search_result, "homePhone");
355 facsimileTelephoneNumber=ldap_get_values(ldserver, search_result, "facsimileTelephoneNumber");
356 mail=ldap_get_values(ldserver, search_result, "mail");
357 uid=ldap_get_values(ldserver, search_result, "uid");
358 homeDirectory=ldap_get_values(ldserver, search_result, "homeDirectory");
359 uidNumber=ldap_get_values(ldserver, search_result, "uidNumber");
360 loginShell=ldap_get_values(ldserver, search_result, "loginShell");
361 gidNumber=ldap_get_values(ldserver, search_result, "gidNumber");
362 c=ldap_get_values(ldserver, search_result, "c");
363 uuid=ldap_get_values(ldserver, search_result, "entryUUID");
365 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]);
366 if (telephoneNumber) changed_something |= vcard_set_one_prop_iff_different(v,"tel;work","%s",telephoneNumber[0]);
367 if (facsimileTelephoneNumber) changed_something |= vcard_set_one_prop_iff_different(v,"tel;fax","%s",facsimileTelephoneNumber[0]);
368 if (mobile) changed_something |= vcard_set_one_prop_iff_different(v,"tel;cell","%s",mobile[0]);
369 if (homePhone) changed_something |= vcard_set_one_prop_iff_different(v,"tel;home","%s",homePhone[0]);
370 if (givenName && sn) {
372 changed_something |= vcard_set_one_prop_iff_different(v,"n","%s;%s;%s",sn[0],givenName[0],initials[0]);
375 changed_something |= vcard_set_one_prop_iff_different(v,"n","%s;%s",sn[0],givenName[0]);
379 changed_something |= vcard_set_props_iff_different(v,"email;internet",ldap_count_values(mail),mail);
381 if (uuid) changed_something |= vcard_set_one_prop_iff_different(v,"uid","%s",uuid[0]);
382 if (o) changed_something |= vcard_set_one_prop_iff_different(v,"org","%s",o[0]);
383 if (cn) changed_something |= vcard_set_one_prop_iff_different(v,"fn","%s",cn[0]);
384 if (title) changed_something |= vcard_set_one_prop_iff_different(v,"title","%s",title[0]);
386 if (givenName) ldap_value_free(givenName);
387 if (initials) ldap_value_free(initials);
388 if (sn) ldap_value_free(sn);
389 if (cn) ldap_value_free(cn);
390 if (o) ldap_value_free(o);
391 if (street) ldap_value_free(street);
392 if (l) ldap_value_free(l);
393 if (st) ldap_value_free(st);
394 if (postalCode) ldap_value_free(postalCode);
395 if (telephoneNumber) ldap_value_free(telephoneNumber);
396 if (mobile) ldap_value_free(mobile);
397 if (homePhone) ldap_value_free(homePhone);
398 if (facsimileTelephoneNumber) ldap_value_free(facsimileTelephoneNumber);
399 if (mail) ldap_value_free(mail);
400 if (uid) ldap_value_free(uid);
401 if (homeDirectory) ldap_value_free(homeDirectory);
402 if (uidNumber) ldap_value_free(uidNumber);
403 if (loginShell) ldap_value_free(loginShell);
404 if (gidNumber) ldap_value_free(gidNumber);
405 if (c) ldap_value_free(c);
406 if (title) ldap_value_free(title);
407 if (uuid) ldap_value_free(uuid);
409 /* free the results */
410 ldap_msgfree(search_result);
412 /* unbind so we can go back in as the authenticating user */
413 ldap_unbind(ldserver);
415 return(changed_something); /* tell the caller whether we made any changes */
418 #endif /* HAVE_LDAP */