9d280186487556576427f29861f72846533a586b
[citadel.git] / citadel / ldap.c
1 /*
2  * These functions implement the portions of AUTHMODE_LDAP and AUTHMODE_LDAP_AD which
3  * actually speak to the LDAP server.
4  *
5  * Copyright (c) 2011 by Art Cancro and the citadel.org development team.
6  *
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 as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
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.
16  *
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
20  */
21
22 int ctdl_require_ldap_version = 3;
23
24 #include "sysdep.h"
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <stdio.h>
29 #include <fcntl.h>
30 #include <signal.h>
31 #include <pwd.h>
32 #include <ctype.h>
33 #include <sys/types.h>
34 #include <sys/wait.h>
35 #ifdef HAVE_SYS_STAT_H
36 #include <sys/stat.h>
37 #endif
38
39 #if TIME_WITH_SYS_TIME
40 # include <sys/time.h>
41 # include <time.h>
42 #else
43 # if HAVE_SYS_TIME_H
44 #  include <sys/time.h>
45 # else
46 #  include <time.h>
47 # endif
48 #endif
49
50 #include <string.h>
51 #include <limits.h>
52 #include <libcitadel.h>
53 #include "auth.h"
54 #include "citadel.h"
55 #include "server.h"
56 #include "database.h"
57 #include "sysdep_decls.h"
58 #include "support.h"
59 #include "room_ops.h"
60 #include "file_ops.h"
61 #include "control.h"
62 #include "msgbase.h"
63 #include "config.h"
64 #include "citserver.h"
65 #include "citadel_dirs.h"
66 #include "genstamp.h"
67 #include "threads.h"
68 #include "citadel_ldap.h"
69 #include "ctdl_module.h"
70 #include "user_ops.h"
71
72 #ifdef HAVE_LDAP
73 #define LDAP_DEPRECATED 1       /* Suppress libldap's warning that we are using deprecated API calls */
74 #include <ldap.h>
75
76 int CtdlTryUserLDAP(char *username,
77                 char *found_dn, int found_dn_size,
78                 char *fullname, int fullname_size,
79                 uid_t *uid)
80 {
81         LDAP *ldserver = NULL;
82         int i;
83         LDAPMessage *search_result = NULL;
84         LDAPMessage *entry = NULL;
85         char searchstring[1024];
86         struct timeval tv;
87         char **values;
88         char *user_dn = NULL;
89
90         if (fullname) safestrncpy(fullname, username, fullname_size);
91
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,
96                         strerror(errno)
97                 );
98                 return(errno);
99         }
100
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);
103
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)
110         );
111         if (i != LDAP_SUCCESS) {
112                 syslog(LOG_ALERT, "LDAP: Cannot bind: %s (%d)\n", ldap_err2string(i), i);
113                 return(i);
114         }
115
116         tv.tv_sec = 10;
117         tv.tv_usec = 0;
118
119         if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
120                 sprintf(searchstring, "(sAMAccountName=%s)", username);
121         }
122         else {
123                 sprintf(searchstring, "(&(objectclass=posixAccount)(uid=%s))", username);
124         }
125
126         syslog(LOG_DEBUG, "LDAP search: %s\n", searchstring);
127         (void) ldap_search_ext_s(
128                 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                 syslog(LOG_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                         syslog(LOG_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                                         syslog(LOG_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                                         syslog(LOG_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                                                 syslog(LOG_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                                         syslog(LOG_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                 syslog(LOG_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                 syslog(LOG_DEBUG, "LDAP: empty passwords are not permitted\n");
233                 return(1);
234         }
235
236         syslog(LOG_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                         syslog(LOG_DEBUG, "LDAP: bind succeeded\n");
243                 }
244                 else {
245                         syslog(LOG_DEBUG, "LDAP: Cannot bind: %s (%d)\n", ldap_err2string(i), i);
246                 }
247                 ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
248                 ldap_unbind(ldserver);
249         }
250
251         if (i == LDAP_SUCCESS) {
252                 return(0);
253         }
254
255         return(1);
256 }
257
258
259 /*
260  * Learn LDAP attributes and stuff them into the vCard.
261  * Returns nonzero if we changed anything.
262  */
263 int Ctdl_LDAP_to_vCard(char *ldap_dn, struct vCard *v)
264 {
265         int changed_something = 0;
266
267         if (!ldap_dn) return(0);
268         if (!v) return(0);
269
270         /*
271          * FIXME this is a stub function
272          *
273          * ldap_dn will contain the DN of the user, and v will contain a pointer to
274          * the vCard that needs to be (re-)populated.  Put the requisite LDAP code here.
275          *
276         vcard_set_prop(v, "email;internet", xxx, 0);
277          *
278          * return nonzero to tell the caller that we made changes that need to be saved
279         changed_something = 1;
280          *
281          */
282
283         return(changed_something);      /* tell the caller whether we made any changes */
284 }
285
286 #endif /* HAVE_LDAP */