4 * These functions implement the portions of AUTHMODE_LDAP and AUTHMODE_LDAP_AD which
5 * actually speak to the LDAP server.
7 * Copyright (c) 2010 by Art Cancro and the citadel.org development team.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 int ctdl_require_ldap_version = 3;
35 #include <sys/types.h>
37 #ifdef HAVE_SYS_STAT_H
41 #if TIME_WITH_SYS_TIME
42 # include <sys/time.h>
46 # include <sys/time.h>
54 #include <libcitadel.h>
60 #include "sysdep_decls.h"
67 #include "citserver.h"
68 #include "citadel_dirs.h"
71 #include "citadel_ldap.h"
72 #include "ctdl_module.h"
75 #define LDAP_DEPRECATED 1 /* Suppress libldap's warning that we are using deprecated API calls */
78 int CtdlTryUserLDAP(char *username,
79 char *found_dn, int found_dn_size,
80 char *fullname, int fullname_size,
83 LDAP *ldserver = NULL;
85 LDAPMessage *search_result = NULL;
86 LDAPMessage *entry = NULL;
87 char searchstring[1024];
92 if (fullname) safestrncpy(fullname, username, fullname_size);
94 ldserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
95 if (ldserver == NULL) {
96 CtdlLogPrintf(CTDL_ALERT, "LDAP: Could not connect to %s:%d : %s\n",
97 config.c_ldap_host, config.c_ldap_port,
103 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
105 striplt(config.c_ldap_bind_dn);
106 striplt(config.c_ldap_bind_pw);
107 CtdlLogPrintf(CTDL_DEBUG, "LDAP bind DN: %s\n", config.c_ldap_bind_dn);
108 i = ldap_simple_bind_s(ldserver,
109 (!IsEmptyStr(config.c_ldap_bind_dn) ? config.c_ldap_bind_dn : NULL),
110 (!IsEmptyStr(config.c_ldap_bind_pw) ? config.c_ldap_bind_pw : NULL)
112 if (i != LDAP_SUCCESS) {
113 CtdlLogPrintf(CTDL_ALERT, "LDAP: Cannot bind: %s (%d)\n", ldap_err2string(i), i);
120 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
121 sprintf(searchstring, "(sAMAccountName=%s)", username);
124 sprintf(searchstring, "(&(objectclass=posixAccount)(uid=%s))", username);
127 /* Documentation of ldap_search_ext_s() is at http://tinyurl.com/y9c8a8l */
128 CtdlLogPrintf(CTDL_DEBUG, "LDAP search: %s\n", searchstring);
129 i = ldap_search_ext_s(ldserver, /* ld */
130 config.c_ldap_base_dn, /* base */
131 LDAP_SCOPE_SUBTREE, /* scope */
132 searchstring, /* filter */
133 NULL, /* attrs (all attributes) */
134 0, /* attrsonly (attrs + values) */
135 NULL, /* serverctrls (none) */
136 NULL, /* clientctrls (none) */
138 1, /* sizelimit (1 result max) */
139 &search_result /* res */
143 /* It appears that this is unnecessary, and returns an error even when the search succeeds? */
144 if (i != LDAP_SUCCESS) {
145 CtdlLogPrintf(CTDL_DEBUG, "LDAP search failed: %s (%d)\n", ldap_err2string(i), i);
146 ldap_unbind(ldserver);
147 if (search_result != NULL) {
148 /* this should never happen - warning memory leak! */
149 CtdlLogPrintf(CTDL_DEBUG, "search returned error but search_result is not null!\n");
155 if (search_result == NULL) {
156 CtdlLogPrintf(CTDL_DEBUG, "LDAP search: zero results were returned\n");
157 ldap_unbind(ldserver);
161 /* At this point we've got at least one result from our query. If there are multiple
162 * results, we still only look at the first one.
164 entry = ldap_first_entry(ldserver, search_result);
167 user_dn = ldap_get_dn(ldserver, entry);
169 CtdlLogPrintf(CTDL_DEBUG, "dn = %s\n", user_dn);
172 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
173 values = ldap_get_values(ldserver, search_result, "displayName");
176 if (fullname) safestrncpy(fullname, values[0], fullname_size);
177 CtdlLogPrintf(CTDL_DEBUG, "displayName = %s\n", values[0]);
179 ldap_value_free(values);
183 values = ldap_get_values(ldserver, search_result, "cn");
186 if (fullname) safestrncpy(fullname, values[0], fullname_size);
187 CtdlLogPrintf(CTDL_DEBUG, "cn = %s\n", values[0]);
189 ldap_value_free(values);
193 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
194 values = ldap_get_values(ldserver, search_result, "objectGUID");
198 *uid = abs(HashLittle(values[0], strlen(values[0])));
199 CtdlLogPrintf(CTDL_DEBUG, "uid hashed from objectGUID = %d\n", *uid);
202 ldap_value_free(values);
206 values = ldap_get_values(ldserver, search_result, "uidNumber");
209 CtdlLogPrintf(CTDL_DEBUG, "uidNumber = %s\n", values[0]);
211 *uid = atoi(values[0]);
214 ldap_value_free(values);
220 /* free the results */
221 ldap_msgfree(search_result);
223 /* unbind so we can go back in as the authenticating user */
224 ldap_unbind(ldserver);
227 CtdlLogPrintf(CTDL_DEBUG, "No such user was found.\n");
231 if (found_dn) safestrncpy(found_dn, user_dn, found_dn_size);
232 ldap_memfree(user_dn);
237 int CtdlTryPasswordLDAP(char *user_dn, char *password)
239 LDAP *ldserver = NULL;
242 ldserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
244 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
245 i = ldap_simple_bind_s(ldserver, user_dn, password);
246 if (i == LDAP_SUCCESS) {
247 CtdlLogPrintf(CTDL_DEBUG, "LDAP: bind succeeded\n");
250 CtdlLogPrintf(CTDL_DEBUG, "LDAP: Cannot bind: %s (%d)\n", ldap_err2string(i), i);
252 ldap_unbind(ldserver);
255 if (i == LDAP_SUCCESS) {
264 * Learn LDAP attributes and stuff them into the vCard.
265 * Returns nonzero if we changed anything.
267 int Ctdl_LDAP_to_vCard(char *ldap_dn, struct vCard *v)
269 if (!ldap_dn) return(0);
273 * FIXME LDAPSTUB this is a stub function
275 * ldap_dn will contain the DN of the user, and v will contain a pointer to
276 * the vCard that needs to be (re-)populated. Put the requisite LDAP code here.
278 vcard_set_prop(v, "email;internet", xxx, 0);
279 return(1); * return nonzero to tell the caller that we made changes that need to be saved *
283 return(0); /* return zero to tell the caller that we didn't make any changes */
286 #endif /* HAVE_LDAP */