* Switch from ldap_search_st() to ldap_search_ext_s(). The latter is not deprecated.
[citadel.git] / citadel / ldap.c
1 /*
2  * $Id$
3  *
4  * These functions implement the portions of AUTHMODE_LDAP and AUTHMODE_LDAP_AD which
5  * actually speak to the LDAP server.
6  *
7  * Copyright (c) 2010 by Art Cancro and the citadel.org development team.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 int ctdl_require_ldap_version = 3;
25
26 #include "sysdep.h"
27 #include <errno.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <fcntl.h>
32 #include <signal.h>
33 #include <pwd.h>
34 #include <ctype.h>
35 #include <sys/types.h>
36 #include <sys/wait.h>
37 #ifdef HAVE_SYS_STAT_H
38 #include <sys/stat.h>
39 #endif
40
41 #if TIME_WITH_SYS_TIME
42 # include <sys/time.h>
43 # include <time.h>
44 #else
45 # if HAVE_SYS_TIME_H
46 #  include <sys/time.h>
47 # else
48 #  include <time.h>
49 # endif
50 #endif
51
52 #include <string.h>
53 #include <limits.h>
54 #include <libcitadel.h>
55 #include "auth.h"
56 #include "citadel.h"
57 #include "server.h"
58 #include "database.h"
59 #include "user_ops.h"
60 #include "sysdep_decls.h"
61 #include "support.h"
62 #include "room_ops.h"
63 #include "file_ops.h"
64 #include "control.h"
65 #include "msgbase.h"
66 #include "config.h"
67 #include "citserver.h"
68 #include "citadel_dirs.h"
69 #include "genstamp.h"
70 #include "threads.h"
71 #include "citadel_ldap.h"
72 #include "ctdl_module.h"
73
74 #ifdef HAVE_LDAP
75 #define LDAP_DEPRECATED 1       /* Suppress libldap's warning that we are using deprecated API calls */
76 #include <ldap.h>
77
78 int CtdlTryUserLDAP(char *username,
79                 char *found_dn, int found_dn_size,
80                 char *fullname, int fullname_size,
81                 uid_t *uid)
82 {
83         LDAP *ldserver = NULL;
84         int i;
85         LDAPMessage *search_result = NULL;
86         LDAPMessage *entry = NULL;
87         char searchstring[1024];
88         struct timeval tv;
89         char **values;
90         char *user_dn = NULL;
91
92         if (fullname) safestrncpy(fullname, username, fullname_size);
93
94         ldserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
95         if (ldserver == NULL) {
96                 CtdlLogPrintf(CTDL_ALERT, "LDAP: Could not connect to %s:%d : %s\n",
97                         config.c_ldap_host, config.c_ldap_port,
98                         strerror(errno)
99                 );
100                 return(errno);
101         }
102
103         ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
104
105         striplt(config.c_ldap_bind_dn);
106         striplt(config.c_ldap_bind_pw);
107         CtdlLogPrintf(CTDL_DEBUG, "LDAP bind DN: %s\n", config.c_ldap_bind_dn);
108         i = ldap_simple_bind_s(ldserver,
109                 (!IsEmptyStr(config.c_ldap_bind_dn) ? config.c_ldap_bind_dn : NULL),
110                 (!IsEmptyStr(config.c_ldap_bind_pw) ? config.c_ldap_bind_pw : NULL)
111         );
112         if (i != LDAP_SUCCESS) {
113                 CtdlLogPrintf(CTDL_ALERT, "LDAP: Cannot bind: %s (%d)\n", ldap_err2string(i), i);
114                 return(i);
115         }
116
117         tv.tv_sec = 10;
118         tv.tv_usec = 0;
119
120         if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
121                 sprintf(searchstring, "(sAMAccountName=%s)", username);
122         }
123         else {
124                 sprintf(searchstring, "(&(objectclass=posixAccount)(uid=%s))", username);
125         }
126
127         /* Documentation of ldap_search_ext_s() is at http://tinyurl.com/y9c8a8l */
128         CtdlLogPrintf(CTDL_DEBUG, "LDAP search: %s\n", searchstring);
129         i = ldap_search_ext_s(ldserver,                         /* ld                           */
130                 config.c_ldap_base_dn,                          /* base                         */
131                 LDAP_SCOPE_SUBTREE,                             /* scope                        */
132                 searchstring,                                   /* filter                       */
133                 NULL,                                           /* attrs (all attributes)       */
134                 0,                                              /* attrsonly (attrs + values)   */
135                 NULL,                                           /* serverctrls (none)           */
136                 NULL,                                           /* clientctrls (none)           */
137                 &tv,                                            /* timeout                      */
138                 1,                                              /* sizelimit (1 result max)     */
139                 &search_result                                  /* res                          */
140         );
141
142 #if 0
143         /* It appears that this is unnecessary, and returns an error even when the search succeeds? */
144         if (i != LDAP_SUCCESS) {
145                 CtdlLogPrintf(CTDL_DEBUG, "LDAP search failed: %s (%d)\n", ldap_err2string(i), i);
146                 ldap_unbind(ldserver);
147                 if (search_result != NULL) {
148                         /* this should never happen - warning memory leak! */
149                         CtdlLogPrintf(CTDL_DEBUG, "search returned error but search_result is not null!\n");
150                 }
151                 return(i);
152         }
153 #endif
154
155         if (search_result == NULL) {
156                 CtdlLogPrintf(CTDL_DEBUG, "LDAP search: zero results were returned\n");
157                 ldap_unbind(ldserver);
158                 return(2);
159         }
160
161         /* At this point we've got at least one result from our query.  If there are multiple
162          * results, we still only look at the first one.
163          */
164         entry = ldap_first_entry(ldserver, search_result);
165         if (entry) {
166
167                 user_dn = ldap_get_dn(ldserver, entry);
168                 if (user_dn) {
169                         CtdlLogPrintf(CTDL_DEBUG, "dn = %s\n", user_dn);
170                 }
171
172                 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
173                         values = ldap_get_values(ldserver, search_result, "displayName");
174                         if (values) {
175                                 if (values[0]) {
176                                         if (fullname) safestrncpy(fullname, values[0], fullname_size);
177                                         CtdlLogPrintf(CTDL_DEBUG, "displayName = %s\n", values[0]);
178                                 }
179                                 ldap_value_free(values);
180                         }
181                 }
182                 else {
183                         values = ldap_get_values(ldserver, search_result, "cn");
184                         if (values) {
185                                 if (values[0]) {
186                                         if (fullname) safestrncpy(fullname, values[0], fullname_size);
187                                         CtdlLogPrintf(CTDL_DEBUG, "cn = %s\n", values[0]);
188                                 }
189                                 ldap_value_free(values);
190                         }
191                 }
192
193                 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
194                         values = ldap_get_values(ldserver, search_result, "objectGUID");
195                         if (values) {
196                                 if (values[0]) {
197                                         if (uid != NULL) {
198                                                 *uid = abs(HashLittle(values[0], strlen(values[0])));
199                                                 CtdlLogPrintf(CTDL_DEBUG, "uid hashed from objectGUID = %d\n", *uid);
200                                         }
201                                 }
202                                 ldap_value_free(values);
203                         }
204                 }
205                 else {
206                         values = ldap_get_values(ldserver, search_result, "uidNumber");
207                         if (values) {
208                                 if (values[0]) {
209                                         CtdlLogPrintf(CTDL_DEBUG, "uidNumber = %s\n", values[0]);
210                                         if (uid != NULL) {
211                                                 *uid = atoi(values[0]);
212                                         }
213                                 }
214                                 ldap_value_free(values);
215                         }
216                 }
217
218         }
219
220         /* free the results */
221         ldap_msgfree(search_result);
222
223         /* unbind so we can go back in as the authenticating user */
224         ldap_unbind(ldserver);
225
226         if (!user_dn) {
227                 CtdlLogPrintf(CTDL_DEBUG, "No such user was found.\n");
228                 return(4);
229         }
230
231         if (found_dn) safestrncpy(found_dn, user_dn, found_dn_size);
232         ldap_memfree(user_dn);
233         return(0);
234 }
235
236
237 int CtdlTryPasswordLDAP(char *user_dn, char *password)
238 {
239         LDAP *ldserver = NULL;
240         int i = (-1);
241
242         ldserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
243         if (ldserver) {
244                 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
245                 i = ldap_simple_bind_s(ldserver, user_dn, password);
246                 if (i == LDAP_SUCCESS) {
247                         CtdlLogPrintf(CTDL_DEBUG, "LDAP: bind succeeded\n");
248                 }
249                 else {
250                         CtdlLogPrintf(CTDL_DEBUG, "LDAP: Cannot bind: %s (%d)\n", ldap_err2string(i), i);
251                 }
252                 ldap_unbind(ldserver);
253         }
254
255         if (i == LDAP_SUCCESS) {
256                 return(0);
257         }
258
259         return(1);
260 }
261
262
263 /*
264  * Learn LDAP attributes and stuff them into the vCard.
265  * Returns nonzero if we changed anything.
266  */
267 int Ctdl_LDAP_to_vCard(char *ldap_dn, struct vCard *v)
268 {
269         if (!ldap_dn) return(0);
270         if (!v) return(0);
271
272         /*
273          * FIXME LDAPSTUB this is a stub function
274          *
275          * ldap_dn will contain the DN of the user, and v will contain a pointer to
276          * the vCard that needs to be (re-)populated.  Put the requisite LDAP code here.
277          *
278         vcard_set_prop(v, "email;internet", xxx, 0);
279         return(1);      * return nonzero to tell the caller that we made changes that need to be saved *
280          *
281          */
282
283         return(0);      /* return zero to tell the caller that we didn't make any changes */
284 }
285
286 #endif /* HAVE_LDAP */