2 * These functions implement the portions of AUTHMODE_LDAP and AUTHMODE_LDAP_AD which
3 * actually speak to the LDAP server.
5 * Copyright (c) 2010 by Art Cancro and the citadel.org development team.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 int ctdl_require_ldap_version = 3;
33 #include <sys/types.h>
35 #ifdef HAVE_SYS_STAT_H
39 #if TIME_WITH_SYS_TIME
40 # include <sys/time.h>
44 # include <sys/time.h>
52 #include <libcitadel.h>
57 #include "sysdep_decls.h"
64 #include "citserver.h"
65 #include "citadel_dirs.h"
68 #include "citadel_ldap.h"
69 #include "ctdl_module.h"
73 #define LDAP_DEPRECATED 1 /* Suppress libldap's warning that we are using deprecated API calls */
76 int CtdlTryUserLDAP(char *username,
77 char *found_dn, int found_dn_size,
78 char *fullname, int fullname_size,
81 LDAP *ldserver = NULL;
83 LDAPMessage *search_result = NULL;
84 LDAPMessage *entry = NULL;
85 char searchstring[1024];
90 if (fullname) safestrncpy(fullname, username, fullname_size);
92 ldserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
93 if (ldserver == NULL) {
94 syslog(LOG_ALERT, "LDAP: Could not connect to %s:%d : %s\n",
95 config.c_ldap_host, config.c_ldap_port,
101 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
102 ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
104 striplt(config.c_ldap_bind_dn);
105 striplt(config.c_ldap_bind_pw);
106 syslog(LOG_DEBUG, "LDAP bind DN: %s\n", config.c_ldap_bind_dn);
107 i = ldap_simple_bind_s(ldserver,
108 (!IsEmptyStr(config.c_ldap_bind_dn) ? config.c_ldap_bind_dn : NULL),
109 (!IsEmptyStr(config.c_ldap_bind_pw) ? config.c_ldap_bind_pw : NULL)
111 if (i != LDAP_SUCCESS) {
112 syslog(LOG_ALERT, "LDAP: Cannot bind: %s (%d)\n", ldap_err2string(i), i);
119 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
120 sprintf(searchstring, "(sAMAccountName=%s)", username);
123 sprintf(searchstring, "(&(objectclass=posixAccount)(uid=%s))", username);
126 syslog(LOG_DEBUG, "LDAP search: %s\n", searchstring);
127 i = ldap_search_ext_s(ldserver, /* ld */
128 config.c_ldap_base_dn, /* base */
129 LDAP_SCOPE_SUBTREE, /* scope */
130 searchstring, /* filter */
131 NULL, /* attrs (all attributes) */
132 0, /* attrsonly (attrs + values) */
133 NULL, /* serverctrls (none) */
134 NULL, /* clientctrls (none) */
136 1, /* sizelimit (1 result max) */
137 &search_result /* res */
140 /* Ignore the return value of ldap_search_ext_s(). Sometimes it returns an error even when
141 * the search succeeds. Instead, we check to see whether search_result is still NULL.
143 if (search_result == NULL) {
144 syslog(LOG_DEBUG, "LDAP search: zero results were returned\n");
145 ldap_unbind(ldserver);
149 /* At this point we've got at least one result from our query. If there are multiple
150 * results, we still only look at the first one.
152 entry = ldap_first_entry(ldserver, search_result);
155 user_dn = ldap_get_dn(ldserver, entry);
157 syslog(LOG_DEBUG, "dn = %s\n", user_dn);
160 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
161 values = ldap_get_values(ldserver, search_result, "displayName");
164 if (fullname) safestrncpy(fullname, values[0], fullname_size);
165 syslog(LOG_DEBUG, "displayName = %s\n", values[0]);
167 ldap_value_free(values);
171 values = ldap_get_values(ldserver, search_result, "cn");
174 if (fullname) safestrncpy(fullname, values[0], fullname_size);
175 syslog(LOG_DEBUG, "cn = %s\n", values[0]);
177 ldap_value_free(values);
181 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
182 values = ldap_get_values(ldserver, search_result, "objectGUID");
186 *uid = abs(HashLittle(values[0], strlen(values[0])));
187 syslog(LOG_DEBUG, "uid hashed from objectGUID = %d\n", *uid);
190 ldap_value_free(values);
194 values = ldap_get_values(ldserver, search_result, "uidNumber");
197 syslog(LOG_DEBUG, "uidNumber = %s\n", values[0]);
199 *uid = atoi(values[0]);
202 ldap_value_free(values);
208 /* free the results */
209 ldap_msgfree(search_result);
211 /* unbind so we can go back in as the authenticating user */
212 ldap_unbind(ldserver);
215 syslog(LOG_DEBUG, "No such user was found.\n");
219 if (found_dn) safestrncpy(found_dn, user_dn, found_dn_size);
220 ldap_memfree(user_dn);
225 int CtdlTryPasswordLDAP(char *user_dn, const char *password)
227 LDAP *ldserver = NULL;
230 if (IsEmptyStr(password)) {
231 syslog(LOG_DEBUG, "LDAP: empty passwords are not permitted\n");
235 syslog(LOG_DEBUG, "LDAP: trying to bind as %s\n", user_dn);
236 ldserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
238 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
239 i = ldap_simple_bind_s(ldserver, user_dn, password);
240 if (i == LDAP_SUCCESS) {
241 syslog(LOG_DEBUG, "LDAP: bind succeeded\n");
244 syslog(LOG_DEBUG, "LDAP: Cannot bind: %s (%d)\n", ldap_err2string(i), i);
246 ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
247 ldap_unbind(ldserver);
250 if (i == LDAP_SUCCESS) {
259 * Learn LDAP attributes and stuff them into the vCard.
260 * Returns nonzero if we changed anything.
262 int Ctdl_LDAP_to_vCard(char *ldap_dn, struct vCard *v)
264 int changed_something = 0;
266 if (!ldap_dn) return(0);
270 * FIXME this is a stub function
272 * ldap_dn will contain the DN of the user, and v will contain a pointer to
273 * the vCard that needs to be (re-)populated. Put the requisite LDAP code here.
275 vcard_set_prop(v, "email;internet", xxx, 0);
277 * return nonzero to tell the caller that we made changes that need to be saved
278 changed_something = 1;
282 return(changed_something); /* tell the caller whether we made any changes */
285 #endif /* HAVE_LDAP */