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>
59 #include "sysdep_decls.h"
66 #include "citserver.h"
67 #include "citadel_dirs.h"
70 #include "citadel_ldap.h"
71 #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 CtdlLogPrintf(CTDL_DEBUG, "LDAP search: %s\n", searchstring);
128 i = ldap_search_ext_s(ldserver, /* ld */
129 config.c_ldap_base_dn, /* base */
130 LDAP_SCOPE_SUBTREE, /* scope */
131 searchstring, /* filter */
132 NULL, /* attrs (all attributes) */
133 0, /* attrsonly (attrs + values) */
134 NULL, /* serverctrls (none) */
135 NULL, /* clientctrls (none) */
137 1, /* sizelimit (1 result max) */
138 &search_result /* res */
141 /* Ignore the return value of ldap_search_ext_s(). Sometimes it returns an error even when
142 * the search succeeds. Instead, we check to see whether search_result is still NULL.
144 if (search_result == NULL) {
145 CtdlLogPrintf(CTDL_DEBUG, "LDAP search: zero results were returned\n");
146 ldap_unbind(ldserver);
150 /* At this point we've got at least one result from our query. If there are multiple
151 * results, we still only look at the first one.
153 entry = ldap_first_entry(ldserver, search_result);
156 user_dn = ldap_get_dn(ldserver, entry);
158 CtdlLogPrintf(CTDL_DEBUG, "dn = %s\n", user_dn);
161 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
162 values = ldap_get_values(ldserver, search_result, "displayName");
165 if (fullname) safestrncpy(fullname, values[0], fullname_size);
166 CtdlLogPrintf(CTDL_DEBUG, "displayName = %s\n", values[0]);
168 ldap_value_free(values);
172 values = ldap_get_values(ldserver, search_result, "cn");
175 if (fullname) safestrncpy(fullname, values[0], fullname_size);
176 CtdlLogPrintf(CTDL_DEBUG, "cn = %s\n", values[0]);
178 ldap_value_free(values);
182 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
183 values = ldap_get_values(ldserver, search_result, "objectGUID");
187 *uid = abs(HashLittle(values[0], strlen(values[0])));
188 CtdlLogPrintf(CTDL_DEBUG, "uid hashed from objectGUID = %d\n", *uid);
191 ldap_value_free(values);
195 values = ldap_get_values(ldserver, search_result, "uidNumber");
198 CtdlLogPrintf(CTDL_DEBUG, "uidNumber = %s\n", values[0]);
200 *uid = atoi(values[0]);
203 ldap_value_free(values);
209 /* free the results */
210 ldap_msgfree(search_result);
212 /* unbind so we can go back in as the authenticating user */
213 ldap_unbind(ldserver);
216 CtdlLogPrintf(CTDL_DEBUG, "No such user was found.\n");
220 if (found_dn) safestrncpy(found_dn, user_dn, found_dn_size);
221 ldap_memfree(user_dn);
226 int CtdlTryPasswordLDAP(char *user_dn, const char *password)
228 LDAP *ldserver = NULL;
231 if (IsEmptyStr(password)) {
232 CtdlLogPrintf(CTDL_DEBUG, "LDAP: empty passwords are not permitted\n");
236 CtdlLogPrintf(CTDL_DEBUG, "LDAP: trying to bind as %s\n", user_dn);
237 ldserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
239 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
240 i = ldap_simple_bind_s(ldserver, user_dn, password);
241 if (i == LDAP_SUCCESS) {
242 CtdlLogPrintf(CTDL_DEBUG, "LDAP: bind succeeded\n");
245 CtdlLogPrintf(CTDL_DEBUG, "LDAP: Cannot bind: %s (%d)\n", ldap_err2string(i), i);
247 ldap_unbind(ldserver);
250 if (i == LDAP_SUCCESS) {
259 * Learn LDAP attributes and stuff them into the vCard.
260 * Returns nonzero if we changed anything.
262 int Ctdl_LDAP_to_vCard(char *ldap_dn, struct vCard *v)
264 int changed_something = 0;
266 if (!ldap_dn) return(0);
270 * FIXME this is a stub function
272 * ldap_dn will contain the DN of the user, and v will contain a pointer to
273 * the vCard that needs to be (re-)populated. Put the requisite LDAP code here.
275 vcard_set_prop(v, "email;internet", xxx, 0);
277 * return nonzero to tell the caller that we made changes that need to be saved
278 changed_something = 1;
282 return(changed_something); /* tell the caller whether we made any changes */
285 #endif /* HAVE_LDAP */