2 * These functions implement the portions of AUTHMODE_LDAP and AUTHMODE_LDAP_AD which
3 * actually speak to the LDAP server.
5 * Copyright (c) 2011 by Art Cancro and the citadel.org development team.
7 * This program is open source software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License, version 3.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 int ctdl_require_ldap_version = 3;
27 #include <sys/types.h>
29 #ifdef HAVE_SYS_STAT_H
33 #if TIME_WITH_SYS_TIME
34 # include <sys/time.h>
38 # include <sys/time.h>
46 #include <libcitadel.h>
51 #include "sysdep_decls.h"
58 #include "citserver.h"
59 #include "citadel_dirs.h"
62 #include "citadel_ldap.h"
63 #include "ctdl_module.h"
67 #define LDAP_DEPRECATED 1 /* Suppress libldap's warning that we are using deprecated API calls */
70 int CtdlTryUserLDAP(char *username,
71 char *found_dn, int found_dn_size,
72 char *fullname, int fullname_size,
75 LDAP *ldserver = NULL;
77 LDAPMessage *search_result = NULL;
78 LDAPMessage *entry = NULL;
79 char searchstring[1024];
84 if (fullname) safestrncpy(fullname, username, fullname_size);
86 ldserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
87 if (ldserver == NULL) {
88 syslog(LOG_ALERT, "LDAP: Could not connect to %s:%d : %s\n",
89 config.c_ldap_host, config.c_ldap_port,
95 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
96 ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
98 striplt(config.c_ldap_bind_dn);
99 striplt(config.c_ldap_bind_pw);
100 syslog(LOG_DEBUG, "LDAP bind DN: %s\n", config.c_ldap_bind_dn);
101 i = ldap_simple_bind_s(ldserver,
102 (!IsEmptyStr(config.c_ldap_bind_dn) ? config.c_ldap_bind_dn : NULL),
103 (!IsEmptyStr(config.c_ldap_bind_pw) ? config.c_ldap_bind_pw : NULL)
105 if (i != LDAP_SUCCESS) {
106 syslog(LOG_ALERT, "LDAP: Cannot bind: %s (%d)\n", ldap_err2string(i), i);
113 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
114 sprintf(searchstring, "(sAMAccountName=%s)", username);
117 sprintf(searchstring, "(&(objectclass=posixAccount)(uid=%s))", username);
120 syslog(LOG_DEBUG, "LDAP search: %s\n", searchstring);
121 (void) ldap_search_ext_s(
123 config.c_ldap_base_dn, /* base */
124 LDAP_SCOPE_SUBTREE, /* scope */
125 searchstring, /* filter */
126 NULL, /* attrs (all attributes) */
127 0, /* attrsonly (attrs + values) */
128 NULL, /* serverctrls (none) */
129 NULL, /* clientctrls (none) */
131 1, /* sizelimit (1 result max) */
132 &search_result /* res */
135 /* Ignore the return value of ldap_search_ext_s(). Sometimes it returns an error even when
136 * the search succeeds. Instead, we check to see whether search_result is still NULL.
138 if (search_result == NULL) {
139 syslog(LOG_DEBUG, "LDAP search: zero results were returned\n");
140 ldap_unbind(ldserver);
144 /* At this point we've got at least one result from our query. If there are multiple
145 * results, we still only look at the first one.
147 entry = ldap_first_entry(ldserver, search_result);
150 user_dn = ldap_get_dn(ldserver, entry);
152 syslog(LOG_DEBUG, "dn = %s\n", user_dn);
155 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
156 values = ldap_get_values(ldserver, search_result, "displayName");
159 if (fullname) safestrncpy(fullname, values[0], fullname_size);
160 syslog(LOG_DEBUG, "displayName = %s\n", values[0]);
162 ldap_value_free(values);
166 values = ldap_get_values(ldserver, search_result, "cn");
169 if (fullname) safestrncpy(fullname, values[0], fullname_size);
170 syslog(LOG_DEBUG, "cn = %s\n", values[0]);
172 ldap_value_free(values);
176 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
177 values = ldap_get_values(ldserver, search_result, "objectGUID");
181 *uid = abs(HashLittle(values[0], strlen(values[0])));
182 syslog(LOG_DEBUG, "uid hashed from objectGUID = %d\n", *uid);
185 ldap_value_free(values);
189 values = ldap_get_values(ldserver, search_result, "uidNumber");
192 syslog(LOG_DEBUG, "uidNumber = %s\n", values[0]);
194 *uid = atoi(values[0]);
197 ldap_value_free(values);
203 /* free the results */
204 ldap_msgfree(search_result);
206 /* unbind so we can go back in as the authenticating user */
207 ldap_unbind(ldserver);
210 syslog(LOG_DEBUG, "No such user was found.\n");
214 if (found_dn) safestrncpy(found_dn, user_dn, found_dn_size);
215 ldap_memfree(user_dn);
220 int CtdlTryPasswordLDAP(char *user_dn, const char *password)
222 LDAP *ldserver = NULL;
225 if (IsEmptyStr(password)) {
226 syslog(LOG_DEBUG, "LDAP: empty passwords are not permitted\n");
230 syslog(LOG_DEBUG, "LDAP: trying to bind as %s\n", user_dn);
231 ldserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
233 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
234 i = ldap_simple_bind_s(ldserver, user_dn, password);
235 if (i == LDAP_SUCCESS) {
236 syslog(LOG_DEBUG, "LDAP: bind succeeded\n");
239 syslog(LOG_DEBUG, "LDAP: Cannot bind: %s (%d)\n", ldap_err2string(i), i);
241 ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
242 ldap_unbind(ldserver);
245 if (i == LDAP_SUCCESS) {
254 * Learn LDAP attributes and stuff them into the vCard.
255 * Returns nonzero if we changed anything.
257 int Ctdl_LDAP_to_vCard(char *ldap_dn, struct vCard *v)
259 int changed_something = 0;
261 if (!ldap_dn) return(0);
265 * FIXME this is a stub function
267 * ldap_dn will contain the DN of the user, and v will contain a pointer to
268 * the vCard that needs to be (re-)populated. Put the requisite LDAP code here.
270 vcard_set_prop(v, "email;internet", xxx, 0);
272 * return nonzero to tell the caller that we made changes that need to be saved
273 changed_something = 1;
277 return(changed_something); /* tell the caller whether we made any changes */
280 #endif /* HAVE_LDAP */