4 * These functions implement the portions of AUTHMODE_LDAP and AUTHMODE_LDAP_AD which
5 * actually speak to the LDAP server.
7 * Copyright (c) 2009 by Art Cancro and the citadel.org development team.
8 * This program is released under the terms of the GNU General Public License v3
12 int ctdl_require_ldap_version = 3;
24 #include <sys/types.h>
26 #ifdef HAVE_SYS_STAT_H
30 #if TIME_WITH_SYS_TIME
31 # include <sys/time.h>
35 # include <sys/time.h>
43 #include <libcitadel.h>
49 #include "sysdep_decls.h"
56 #include "citserver.h"
57 #include "citadel_dirs.h"
60 #include "citadel_ldap.h"
62 #include "ctdl_module.h"
67 #define LDAP_DEPRECATED 1 /* Needed to suppress misleading warnings */
71 int CtdlTryUserLDAP(char *username,
72 char *found_dn, int found_dn_size,
73 char *fullname, int fullname_size,
76 LDAP *ldserver = NULL;
78 LDAPMessage *search_result = NULL;
79 LDAPMessage *entry = NULL;
80 char searchstring[1024];
85 if (fullname) safestrncpy(fullname, username, fullname_size);
87 ldserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
88 if (ldserver == NULL) {
89 CtdlLogPrintf(CTDL_ALERT, "LDAP: Could not connect to %s:%d : %s\n",
90 config.c_ldap_host, config.c_ldap_port,
95 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
97 striplt(config.c_ldap_bind_dn);
98 striplt(config.c_ldap_bind_pw);
99 i = ldap_simple_bind_s(ldserver,
100 (!IsEmptyStr(config.c_ldap_bind_dn) ? config.c_ldap_bind_dn : NULL),
101 (!IsEmptyStr(config.c_ldap_bind_pw) ? config.c_ldap_bind_pw : NULL)
103 if (i != LDAP_SUCCESS) {
104 CtdlLogPrintf(CTDL_ALERT, "LDAP: Cannot bind: %s (%d)\n", ldap_err2string(i), i);
111 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
112 sprintf(searchstring, "(sAMAccountName=%s)", username);
115 sprintf(searchstring, "(&(objectclass=posixAccount)(uid=%s))", username);
118 i = ldap_search_st(ldserver,
119 config.c_ldap_base_dn,
122 NULL, // return all attributes
123 0, // attributes + values
127 if (i != LDAP_SUCCESS) {
128 CtdlLogPrintf(CTDL_DEBUG,
129 "Couldn't find what I was looking for: %s (%d)\n", ldap_err2string(i), i);
130 ldap_unbind(ldserver);
134 if (search_result == NULL) {
135 CtdlLogPrintf(CTDL_DEBUG, "No results were returned\n");
136 ldap_unbind(ldserver);
140 /* At this point we've got at least one result from our query. If there are multiple
141 * results, we still only look at the first one.
143 entry = ldap_first_entry(ldserver, search_result);
146 user_dn = ldap_get_dn(ldserver, entry);
148 CtdlLogPrintf(CTDL_DEBUG, "dn = %s\n", user_dn);
151 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
152 values = ldap_get_values(ldserver, search_result, "displayName");
155 if (fullname) safestrncpy(fullname, values[0], fullname_size);
156 CtdlLogPrintf(CTDL_DEBUG, "displayName = %s\n", values[0]);
158 ldap_value_free(values);
162 values = ldap_get_values(ldserver, search_result, "cn");
165 if (fullname) safestrncpy(fullname, values[0], fullname_size);
166 CtdlLogPrintf(CTDL_DEBUG, "cn = %s\n", values[0]);
168 ldap_value_free(values);
172 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
173 values = ldap_get_values(ldserver, search_result, "objectGUID");
177 *uid = abs(HashLittle(values[0], strlen(values[0])));
178 CtdlLogPrintf(CTDL_DEBUG, "uid hashed from objectGUID = %d\n", *uid);
181 ldap_value_free(values);
185 values = ldap_get_values(ldserver, search_result, "uidNumber");
188 CtdlLogPrintf(CTDL_DEBUG, "uidNumber = %s\n", values[0]);
190 *uid = atoi(values[0]);
193 ldap_value_free(values);
199 /* free the results */
200 ldap_msgfree(search_result);
202 /* unbind so we can go back in as the authenticating user */
203 ldap_unbind(ldserver);
206 CtdlLogPrintf(CTDL_DEBUG, "No such user was found.\n");
210 if (found_dn) safestrncpy(found_dn, user_dn, found_dn_size);
211 ldap_memfree(user_dn);
216 int CtdlTryPasswordLDAP(char *user_dn, char *password)
218 LDAP *ldserver = NULL;
221 ldserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
223 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
224 i = ldap_simple_bind_s(ldserver, user_dn, password);
225 if (i == LDAP_SUCCESS) {
226 CtdlLogPrintf(CTDL_DEBUG, "LDAP: bind succeeded\n");
229 CtdlLogPrintf(CTDL_DEBUG, "LDAP: Cannot bind: %s (%d)\n", ldap_err2string(i), i);
231 ldap_unbind(ldserver);
234 if (i == LDAP_SUCCESS) {
243 * Learn LDAP attributes and stuff them into the vCard.
244 * Returns nonzero if we changed anything.
246 int Ctdl_LDAP_to_vCard(char *ldap_dn, struct vCard *v)
248 if (!ldap_dn) return(0);
252 * FIXME LDAPSTUB 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);
258 return(1); * return nonzero to tell the caller that we made changes that need to be saved *
262 return(0); /* return zero to tell the caller that we didn't make any changes */
265 #endif /* HAVE_LDAP */