* LDAP auth - do not permit empty passwords
[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 "sysdep_decls.h"
60 #include "support.h"
61 #include "room_ops.h"
62 #include "file_ops.h"
63 #include "control.h"
64 #include "msgbase.h"
65 #include "config.h"
66 #include "citserver.h"
67 #include "citadel_dirs.h"
68 #include "genstamp.h"
69 #include "threads.h"
70 #include "citadel_ldap.h"
71 #include "ctdl_module.h"
72 #include "user_ops.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         CtdlLogPrintf(CTDL_DEBUG, "LDAP search: %s\n", searchstring);
128         i = ldap_search_ext_s(ldserver,                         /* ld                           */
129                 config.c_ldap_base_dn,                          /* base                         */
130                 LDAP_SCOPE_SUBTREE,                             /* scope                        */
131                 searchstring,                                   /* filter                       */
132                 NULL,                                           /* attrs (all attributes)       */
133                 0,                                              /* attrsonly (attrs + values)   */
134                 NULL,                                           /* serverctrls (none)           */
135                 NULL,                                           /* clientctrls (none)           */
136                 &tv,                                            /* timeout                      */
137                 1,                                              /* sizelimit (1 result max)     */
138                 &search_result                                  /* res                          */
139         );
140
141         /* Ignore the return value of ldap_search_ext_s().  Sometimes it returns an error even when
142          * the search succeeds.  Instead, we check to see whether search_result is still NULL.
143          */
144         if (search_result == NULL) {
145                 CtdlLogPrintf(CTDL_DEBUG, "LDAP search: zero results were returned\n");
146                 ldap_unbind(ldserver);
147                 return(2);
148         }
149
150         /* At this point we've got at least one result from our query.  If there are multiple
151          * results, we still only look at the first one.
152          */
153         entry = ldap_first_entry(ldserver, search_result);
154         if (entry) {
155
156                 user_dn = ldap_get_dn(ldserver, entry);
157                 if (user_dn) {
158                         CtdlLogPrintf(CTDL_DEBUG, "dn = %s\n", user_dn);
159                 }
160
161                 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
162                         values = ldap_get_values(ldserver, search_result, "displayName");
163                         if (values) {
164                                 if (values[0]) {
165                                         if (fullname) safestrncpy(fullname, values[0], fullname_size);
166                                         CtdlLogPrintf(CTDL_DEBUG, "displayName = %s\n", values[0]);
167                                 }
168                                 ldap_value_free(values);
169                         }
170                 }
171                 else {
172                         values = ldap_get_values(ldserver, search_result, "cn");
173                         if (values) {
174                                 if (values[0]) {
175                                         if (fullname) safestrncpy(fullname, values[0], fullname_size);
176                                         CtdlLogPrintf(CTDL_DEBUG, "cn = %s\n", values[0]);
177                                 }
178                                 ldap_value_free(values);
179                         }
180                 }
181
182                 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
183                         values = ldap_get_values(ldserver, search_result, "objectGUID");
184                         if (values) {
185                                 if (values[0]) {
186                                         if (uid != NULL) {
187                                                 *uid = abs(HashLittle(values[0], strlen(values[0])));
188                                                 CtdlLogPrintf(CTDL_DEBUG, "uid hashed from objectGUID = %d\n", *uid);
189                                         }
190                                 }
191                                 ldap_value_free(values);
192                         }
193                 }
194                 else {
195                         values = ldap_get_values(ldserver, search_result, "uidNumber");
196                         if (values) {
197                                 if (values[0]) {
198                                         CtdlLogPrintf(CTDL_DEBUG, "uidNumber = %s\n", values[0]);
199                                         if (uid != NULL) {
200                                                 *uid = atoi(values[0]);
201                                         }
202                                 }
203                                 ldap_value_free(values);
204                         }
205                 }
206
207         }
208
209         /* free the results */
210         ldap_msgfree(search_result);
211
212         /* unbind so we can go back in as the authenticating user */
213         ldap_unbind(ldserver);
214
215         if (!user_dn) {
216                 CtdlLogPrintf(CTDL_DEBUG, "No such user was found.\n");
217                 return(4);
218         }
219
220         if (found_dn) safestrncpy(found_dn, user_dn, found_dn_size);
221         ldap_memfree(user_dn);
222         return(0);
223 }
224
225
226 int CtdlTryPasswordLDAP(char *user_dn, const char *password)
227 {
228         LDAP *ldserver = NULL;
229         int i = (-1);
230
231         if (IsEmptyStr(password)) {
232                 CtdlLogPrintf(CTDL_DEBUG, "LDAP: empty passwords are not permitted\n");
233                 return(1);
234         }
235
236         CtdlLogPrintf(CTDL_DEBUG, "LDAP: trying to bind as %s\n", user_dn);
237         ldserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
238         if (ldserver) {
239                 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
240                 i = ldap_simple_bind_s(ldserver, user_dn, password);
241                 if (i == LDAP_SUCCESS) {
242                         CtdlLogPrintf(CTDL_DEBUG, "LDAP: bind succeeded\n");
243                 }
244                 else {
245                         CtdlLogPrintf(CTDL_DEBUG, "LDAP: Cannot bind: %s (%d)\n", ldap_err2string(i), i);
246                 }
247                 ldap_unbind(ldserver);
248         }
249
250         if (i == LDAP_SUCCESS) {
251                 return(0);
252         }
253
254         return(1);
255 }
256
257
258 /*
259  * Learn LDAP attributes and stuff them into the vCard.
260  * Returns nonzero if we changed anything.
261  */
262 int Ctdl_LDAP_to_vCard(char *ldap_dn, struct vCard *v)
263 {
264         int changed_something = 0;
265
266         if (!ldap_dn) return(0);
267         if (!v) return(0);
268
269         /*
270          * FIXME this is a stub function
271          *
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.
274          *
275         vcard_set_prop(v, "email;internet", xxx, 0);
276          *
277          * return nonzero to tell the caller that we made changes that need to be saved
278         changed_something = 1;
279          *
280          */
281
282         return(changed_something);      /* tell the caller whether we made any changes */
283 }
284
285 #endif /* HAVE_LDAP */