19 #include <sys/types.h>
21 #ifdef HAVE_SYS_STAT_H
25 #if TIME_WITH_SYS_TIME
26 # include <sys/time.h>
30 # include <sys/time.h>
38 #include <libcitadel.h>
44 #include "sysdep_decls.h"
51 #include "citserver.h"
52 #include "citadel_dirs.h"
55 #include "citadel_ldap.h"
57 #define LDAP_DEPRECATED 1 /* Needed to suppress misleading warnings */
61 int CtdlTryUserLDAP(char *username, char *found_dn, int found_dn_size, char *fullname, int fullname_size)
63 LDAP *ldserver = NULL;
65 LDAPMessage *search_result = NULL;
66 LDAPMessage *entry = NULL;
67 char searchstring[1024];
72 safestrncpy(fullname, username, fullname_size);
74 ldserver = ldap_init(CTDL_LDAP_HOST, CTDL_LDAP_PORT);
75 if (ldserver == NULL) {
76 CtdlLogPrintf(CTDL_ALERT, "LDAP: Could not connect to %s:%d : %s\n",
77 CTDL_LDAP_HOST, CTDL_LDAP_PORT,
82 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ldap_version);
84 i = ldap_simple_bind_s(ldserver, BIND_DN, BIND_PW);
85 if (i != LDAP_SUCCESS) {
86 CtdlLogPrintf(CTDL_ALERT, "LDAP: Cannot bind: %s (%d)\n", ldap_err2string(i), i);
93 sprintf(searchstring, SEARCH_STRING, username);
95 i = ldap_search_st(ldserver,
99 NULL, // return all attributes
100 0, // attributes + values
104 if (i != LDAP_SUCCESS) {
105 CtdlLogPrintf(CTDL_DEBUG,
106 "Couldn't find what I was looking for: %s (%d)\n", ldap_err2string(i), i);
107 ldap_unbind(ldserver);
111 if (search_result == NULL) {
112 CtdlLogPrintf(CTDL_DEBUG, "No results were returned\n");
113 ldap_unbind(ldserver);
117 /* At this point we've got at least one result from our query. If there are multiple
118 * results, we still only look at the first one.
120 entry = ldap_first_entry(ldserver, search_result);
123 user_dn = ldap_get_dn(ldserver, entry);
125 CtdlLogPrintf(CTDL_DEBUG, "dn = %s\n", user_dn);
128 values = ldap_get_values(ldserver, search_result, "cn");
131 safestrncpy(fullname, values[0], fullname_size);
132 CtdlLogPrintf(CTDL_DEBUG, "cn = %s\n", values[0]);
134 ldap_value_free(values);
137 values = ldap_get_values(ldserver, search_result, "uidNumber");
140 CtdlLogPrintf(CTDL_DEBUG, "uidNumber = %s\n", values[0]);
142 ldap_value_free(values);
145 values = ldap_get_values(ldserver, search_result, "objectGUID");
148 CtdlLogPrintf(CTDL_DEBUG, "objectGUID = (%d characers)\n", strlen(values[0]));
150 ldap_value_free(values);
155 /* free the results */
156 ldap_msgfree(search_result);
158 /* unbind so we can go back in as the authenticating user */
159 ldap_unbind(ldserver);
162 CtdlLogPrintf(CTDL_DEBUG, "No such user was found.\n");
166 safestrncpy(found_dn, user_dn, found_dn_size);
167 ldap_memfree(user_dn);
172 int CtdlTryPasswordLDAP(char *user_dn, char *password)
174 LDAP *ldserver = NULL;
177 ldserver = ldap_init(CTDL_LDAP_HOST, CTDL_LDAP_PORT);
179 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ldap_version);
180 i = ldap_simple_bind_s(ldserver, user_dn, password);
181 if (i == LDAP_SUCCESS) {
182 CtdlLogPrintf(CTDL_DEBUG, "LDAP: bind succeeded\n");
185 CtdlLogPrintf(CTDL_DEBUG, "LDAP: Cannot bind: %s (%d)\n", ldap_err2string(i), i);
187 ldap_unbind(ldserver);
190 return((i == LDAP_SUCCESS) ? 0 : 1);
196 #endif /* HAVE_LDAP */