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 by Art Cancro and 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;
19 #include <libcitadel.h>
21 #include "citserver.h"
22 #include "citadel_ldap.h"
23 #include "ctdl_module.h"
28 #define LDAP_DEPRECATED 1 /* Suppress libldap's warning that we are using deprecated API calls */
31 int CtdlTryUserLDAP(char *username,
32 char *found_dn, int found_dn_size,
33 char *fullname, int fullname_size,
36 LDAP *ldserver = NULL;
38 LDAPMessage *search_result = NULL;
39 LDAPMessage *entry = NULL;
40 char searchstring[1024];
45 #ifndef LDAP_INITIALIZE
46 if (fullname) safestrncpy(fullname, username, fullname_size);
48 ldserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
49 if (ldserver == NULL) {
50 syslog(LOG_ALERT, "LDAP: Could not connect to %s:%d : %s\n",
51 config.c_ldap_host, config.c_ldap_port,
57 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
58 ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
60 striplt(config.c_ldap_bind_dn);
61 striplt(config.c_ldap_bind_pw);
62 syslog(LOG_DEBUG, "LDAP bind DN: %s\n", config.c_ldap_bind_dn);
63 i = ldap_simple_bind_s(ldserver,
64 (!IsEmptyStr(config.c_ldap_bind_dn) ? config.c_ldap_bind_dn : NULL),
65 (!IsEmptyStr(config.c_ldap_bind_pw) ? config.c_ldap_bind_pw : NULL)
67 if (i != LDAP_SUCCESS) {
68 syslog(LOG_ALERT, "LDAP: Cannot bind: %s (%d)\n", ldap_err2string(i), i);
72 if (ldap_initialize(&ldserver, config.c_ldap_host))
74 syslog(LOG_ALERT, "LDAP: Could not connect to %s:%d : %s\n",
75 config.c_ldap_host, config.c_ldap_port,
81 striplt(config.c_ldap_bind_dn);
82 striplt(config.c_ldap_bind_pw);
84 syslog(LOG_DEBUG, "LDAP bind DN: %s\n", config.c_ldap_bind_dn);
85 i = ldap_simple_bind_s(ldserver,
86 (!IsEmptyStr(config.c_ldap_bind_dn) ? config.c_ldap_bind_dn : NULL),
87 (!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)\n", ldap_err2string(i), i);
100 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
101 sprintf(searchstring, "(sAMAccountName=%s)", username);
104 sprintf(searchstring, "(&(objectclass=posixAccount)(uid=%s))", username);
107 syslog(LOG_DEBUG, "LDAP search: %s\n", searchstring);
108 (void) ldap_search_ext_s(
110 config.c_ldap_base_dn, /* base */
111 LDAP_SCOPE_SUBTREE, /* scope */
112 searchstring, /* filter */
113 NULL, /* attrs (all attributes) */
114 0, /* attrsonly (attrs + values) */
115 NULL, /* serverctrls (none) */
116 NULL, /* clientctrls (none) */
118 1, /* sizelimit (1 result max) */
119 &search_result /* res */
122 /* Ignore the return value of ldap_search_ext_s(). Sometimes it returns an error even when
123 * the search succeeds. Instead, we check to see whether search_result is still NULL.
125 if (search_result == NULL) {
126 syslog(LOG_DEBUG, "LDAP search: zero results were returned\n");
127 ldap_unbind(ldserver);
131 /* At this point we've got at least one result from our query. If there are multiple
132 * results, we still only look at the first one.
134 entry = ldap_first_entry(ldserver, search_result);
137 user_dn = ldap_get_dn(ldserver, entry);
139 syslog(LOG_DEBUG, "dn = %s\n", user_dn);
142 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
143 values = ldap_get_values(ldserver, search_result, "displayName");
146 if (fullname) safestrncpy(fullname, values[0], fullname_size);
147 syslog(LOG_DEBUG, "displayName = %s\n", values[0]);
149 ldap_value_free(values);
153 values = ldap_get_values(ldserver, search_result, "cn");
156 if (fullname) safestrncpy(fullname, values[0], fullname_size);
157 syslog(LOG_DEBUG, "cn = %s\n", values[0]);
159 ldap_value_free(values);
163 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
164 values = ldap_get_values(ldserver, search_result, "objectGUID");
168 *uid = abs(HashLittle(values[0], strlen(values[0])));
169 syslog(LOG_DEBUG, "uid hashed from objectGUID = %d\n", *uid);
172 ldap_value_free(values);
176 values = ldap_get_values(ldserver, search_result, "uidNumber");
179 syslog(LOG_DEBUG, "uidNumber = %s\n", values[0]);
181 *uid = atoi(values[0]);
184 ldap_value_free(values);
190 /* free the results */
191 ldap_msgfree(search_result);
193 /* unbind so we can go back in as the authenticating user */
194 ldap_unbind(ldserver);
197 syslog(LOG_DEBUG, "No such user was found.\n");
201 if (found_dn) safestrncpy(found_dn, user_dn, found_dn_size);
202 ldap_memfree(user_dn);
207 int CtdlTryPasswordLDAP(char *user_dn, const char *password)
209 LDAP *ldserver = NULL;
212 if (IsEmptyStr(password)) {
213 syslog(LOG_DEBUG, "LDAP: empty passwords are not permitted\n");
217 syslog(LOG_DEBUG, "LDAP: trying to bind as %s\n", user_dn);
218 ldserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
220 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
221 i = ldap_simple_bind_s(ldserver, user_dn, password);
222 if (i == LDAP_SUCCESS) {
223 syslog(LOG_DEBUG, "LDAP: bind succeeded\n");
226 syslog(LOG_DEBUG, "LDAP: Cannot bind: %s (%d)\n", ldap_err2string(i), i);
228 ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
229 ldap_unbind(ldserver);
232 if (i == LDAP_SUCCESS) {
241 * Learn LDAP attributes and stuff them into the vCard.
242 * Returns nonzero if we changed anything.
244 int Ctdl_LDAP_to_vCard(char *ldap_dn, struct vCard *v)
246 int changed_something = 0;
248 if (!ldap_dn) return(0);
252 * FIXME this is a stub function
254 * ldap_dn will contain the DN of the user, and v will contain a pointer to
255 * the vCard that needs to be (re-)populated. Put the requisite LDAP code here.
257 vcard_set_prop(v, "email;internet", xxx, 0);
259 * return nonzero to tell the caller that we made changes that need to be saved
260 changed_something = 1;
264 return(changed_something); /* tell the caller whether we made any changes */
267 #endif /* HAVE_LDAP */