18 #include <sys/types.h>
20 #ifdef HAVE_SYS_STAT_H
24 #if TIME_WITH_SYS_TIME
25 # include <sys/time.h>
29 # include <sys/time.h>
37 #include <libcitadel.h>
43 #include "sysdep_decls.h"
50 #include "citserver.h"
51 #include "citadel_dirs.h"
54 #include "citadel_ldap.h"
58 #define LDAP_DEPRECATED 1 /* Needed to suppress misleading warnings */
62 int CtdlTryUserLDAP(char *username,
63 char *found_dn, int found_dn_size,
64 char *fullname, int fullname_size,
67 LDAP *ldserver = NULL;
69 LDAPMessage *search_result = NULL;
70 LDAPMessage *entry = NULL;
71 char searchstring[1024];
76 safestrncpy(fullname, username, fullname_size);
78 ldserver = ldap_init(CTDL_LDAP_HOST, CTDL_LDAP_PORT);
79 if (ldserver == NULL) {
80 CtdlLogPrintf(CTDL_ALERT, "LDAP: Could not connect to %s:%d : %s\n",
81 CTDL_LDAP_HOST, CTDL_LDAP_PORT,
86 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ldap_version);
88 i = ldap_simple_bind_s(ldserver, BIND_DN, BIND_PW);
89 if (i != LDAP_SUCCESS) {
90 CtdlLogPrintf(CTDL_ALERT, "LDAP: Cannot bind: %s (%d)\n", ldap_err2string(i), i);
97 sprintf(searchstring, SEARCH_STRING, username);
99 i = ldap_search_st(ldserver,
103 NULL, // return all attributes
104 0, // attributes + values
108 if (i != LDAP_SUCCESS) {
109 CtdlLogPrintf(CTDL_DEBUG,
110 "Couldn't find what I was looking for: %s (%d)\n", ldap_err2string(i), i);
111 ldap_unbind(ldserver);
115 if (search_result == NULL) {
116 CtdlLogPrintf(CTDL_DEBUG, "No results were returned\n");
117 ldap_unbind(ldserver);
121 /* At this point we've got at least one result from our query. If there are multiple
122 * results, we still only look at the first one.
124 entry = ldap_first_entry(ldserver, search_result);
127 user_dn = ldap_get_dn(ldserver, entry);
129 CtdlLogPrintf(CTDL_DEBUG, "dn = %s\n", user_dn);
132 values = ldap_get_values(ldserver, search_result, "cn");
135 safestrncpy(fullname, values[0], fullname_size);
136 CtdlLogPrintf(CTDL_DEBUG, "cn = %s\n", values[0]);
138 ldap_value_free(values);
141 values = ldap_get_values(ldserver, search_result, "uidNumber");
144 CtdlLogPrintf(CTDL_DEBUG, "uidNumber = %s\n", values[0]);
146 *uid = atoi(values[0]);
149 ldap_value_free(values);
152 values = ldap_get_values(ldserver, search_result, "objectGUID");
155 CtdlLogPrintf(CTDL_DEBUG, "objectGUID = (%d characers)\n", strlen(values[0]));
157 ldap_value_free(values);
162 /* free the results */
163 ldap_msgfree(search_result);
165 /* unbind so we can go back in as the authenticating user */
166 ldap_unbind(ldserver);
169 CtdlLogPrintf(CTDL_DEBUG, "No such user was found.\n");
173 safestrncpy(found_dn, user_dn, found_dn_size);
174 ldap_memfree(user_dn);
179 int CtdlTryPasswordLDAP(char *user_dn, char *password)
181 LDAP *ldserver = NULL;
184 ldserver = ldap_init(CTDL_LDAP_HOST, CTDL_LDAP_PORT);
186 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ldap_version);
187 i = ldap_simple_bind_s(ldserver, user_dn, password);
188 if (i == LDAP_SUCCESS) {
189 CtdlLogPrintf(CTDL_DEBUG, "LDAP: bind succeeded\n");
192 CtdlLogPrintf(CTDL_DEBUG, "LDAP: Cannot bind: %s (%d)\n", ldap_err2string(i), i);
194 ldap_unbind(ldserver);
197 if (i == LDAP_SUCCESS) {
207 #endif /* HAVE_LDAP */