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
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"
64 #define LDAP_DEPRECATED 1 /* Needed to suppress misleading warnings */
68 int CtdlTryUserLDAP(char *username,
69 char *found_dn, int found_dn_size,
70 char *fullname, int fullname_size,
73 LDAP *ldserver = NULL;
75 LDAPMessage *search_result = NULL;
76 LDAPMessage *entry = NULL;
77 char searchstring[1024];
82 if (fullname) safestrncpy(fullname, username, fullname_size);
84 ldserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
85 if (ldserver == NULL) {
86 CtdlLogPrintf(CTDL_ALERT, "LDAP: Could not connect to %s:%d : %s\n",
87 config.c_ldap_host, config.c_ldap_port,
92 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ldap_version);
94 striplt(config.c_ldap_bind_dn);
95 striplt(config.c_ldap_bind_pw);
96 i = ldap_simple_bind_s(ldserver,
97 (!IsEmptyStr(config.c_ldap_bind_dn) ? config.c_ldap_bind_dn : NULL),
98 (!IsEmptyStr(config.c_ldap_bind_pw) ? config.c_ldap_bind_pw : NULL)
100 if (i != LDAP_SUCCESS) {
101 CtdlLogPrintf(CTDL_ALERT, "LDAP: Cannot bind: %s (%d)\n", ldap_err2string(i), i);
108 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
109 sprintf(searchstring, "(sAMAccountName=%s)", username);
112 sprintf(searchstring, "(&(objectclass=posixAccount)(uid=%s))", username);
115 i = ldap_search_st(ldserver,
116 config.c_ldap_base_dn,
119 NULL, // return all attributes
120 0, // attributes + values
124 if (i != LDAP_SUCCESS) {
125 CtdlLogPrintf(CTDL_DEBUG,
126 "Couldn't find what I was looking for: %s (%d)\n", ldap_err2string(i), i);
127 ldap_unbind(ldserver);
131 if (search_result == NULL) {
132 CtdlLogPrintf(CTDL_DEBUG, "No results were returned\n");
133 ldap_unbind(ldserver);
137 /* At this point we've got at least one result from our query. If there are multiple
138 * results, we still only look at the first one.
140 entry = ldap_first_entry(ldserver, search_result);
143 user_dn = ldap_get_dn(ldserver, entry);
145 CtdlLogPrintf(CTDL_DEBUG, "dn = %s\n", user_dn);
148 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
149 values = ldap_get_values(ldserver, search_result, "displayName");
152 if (fullname) safestrncpy(fullname, values[0], fullname_size);
153 CtdlLogPrintf(CTDL_DEBUG, "displayName = %s\n", values[0]);
155 ldap_value_free(values);
159 values = ldap_get_values(ldserver, search_result, "cn");
162 if (fullname) safestrncpy(fullname, values[0], fullname_size);
163 CtdlLogPrintf(CTDL_DEBUG, "cn = %s\n", values[0]);
165 ldap_value_free(values);
169 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
170 values = ldap_get_values(ldserver, search_result, "objectGUID");
174 *uid = abs(HashLittle(values[0], strlen(values[0])));
175 CtdlLogPrintf(CTDL_DEBUG, "uid hashed from objectGUID = %d\n", *uid);
178 ldap_value_free(values);
182 values = ldap_get_values(ldserver, search_result, "uidNumber");
185 CtdlLogPrintf(CTDL_DEBUG, "uidNumber = %s\n", values[0]);
187 *uid = atoi(values[0]);
190 ldap_value_free(values);
196 /* free the results */
197 ldap_msgfree(search_result);
199 /* unbind so we can go back in as the authenticating user */
200 ldap_unbind(ldserver);
203 CtdlLogPrintf(CTDL_DEBUG, "No such user was found.\n");
207 if (found_dn) safestrncpy(found_dn, user_dn, found_dn_size);
208 ldap_memfree(user_dn);
213 int CtdlTryPasswordLDAP(char *user_dn, char *password)
215 LDAP *ldserver = NULL;
218 ldserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
220 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ldap_version);
221 i = ldap_simple_bind_s(ldserver, user_dn, password);
222 if (i == LDAP_SUCCESS) {
223 CtdlLogPrintf(CTDL_DEBUG, "LDAP: bind succeeded\n");
226 CtdlLogPrintf(CTDL_DEBUG, "LDAP: Cannot bind: %s (%d)\n", ldap_err2string(i), i);
228 ldap_unbind(ldserver);
231 if (i == LDAP_SUCCESS) {
241 #endif /* HAVE_LDAP */