wtf
[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-2014 by 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, version 3.
9  *
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.
14  */
15
16 int ctdl_require_ldap_version = 3;
17
18 #define _GNU_SOURCE             // Needed to suppress warning about vasprintf() when running on Linux/Linux
19 #include <stdio.h>
20 #include <libcitadel.h>
21 #include "citserver.h"
22 #include "citadel_ldap.h"
23 #include "ctdl_module.h"
24 #include "user_ops.h"
25
26 #ifdef HAVE_LDAP
27 #define LDAP_DEPRECATED 1       /* Suppress libldap's warning that we are using deprecated API calls */
28 #include <ldap.h>
29
30 int CtdlTryUserLDAP(char *username,
31                 char *found_dn, int found_dn_size,
32                 char *fullname, int fullname_size,
33                 uid_t *uid)
34 {
35         LDAP *ldserver = NULL;
36         int i;
37         LDAPMessage *search_result = NULL;
38         LDAPMessage *entry = NULL;
39         char searchstring[1024];
40         struct timeval tv;
41         char **values;
42         char *user_dn = NULL;
43
44         if (fullname) safestrncpy(fullname, username, fullname_size);
45
46         ldserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
47         if (ldserver == NULL) {
48                 syslog(LOG_ALERT, "LDAP: Could not connect to %s:%d : %s",
49                         config.c_ldap_host, config.c_ldap_port,
50                         strerror(errno)
51                 );
52                 return(errno);
53         }
54
55         ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
56         ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
57
58         striplt(config.c_ldap_bind_dn);
59         striplt(config.c_ldap_bind_pw);
60         syslog(LOG_DEBUG, "LDAP bind DN: %s", config.c_ldap_bind_dn);
61         i = ldap_simple_bind_s(ldserver,
62                 (!IsEmptyStr(config.c_ldap_bind_dn) ? config.c_ldap_bind_dn : NULL),
63                 (!IsEmptyStr(config.c_ldap_bind_pw) ? config.c_ldap_bind_pw : NULL)
64         );
65         if (i != LDAP_SUCCESS) {
66                 syslog(LOG_ALERT, "LDAP: Cannot bind: %s (%d)", ldap_err2string(i), i);
67                 return(i);
68         }
69
70         tv.tv_sec = 10;
71         tv.tv_usec = 0;
72
73         if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
74                 snprintf(searchstring, sizeof(searchstring), "(sAMAccountName=%s)", username);
75         }
76         else {
77                 snprintf(searchstring, sizeof(searchstring), "(&(objectclass=posixAccount)(uid=%s))", username);
78         }
79
80         syslog(LOG_DEBUG, "LDAP search: %s", searchstring);
81         (void) ldap_search_ext_s(
82                 ldserver,                                       /* ld                           */
83                 config.c_ldap_base_dn,                          /* base                         */
84                 LDAP_SCOPE_SUBTREE,                             /* scope                        */
85                 searchstring,                                   /* filter                       */
86                 NULL,                                           /* attrs (all attributes)       */
87                 0,                                              /* attrsonly (attrs + values)   */
88                 NULL,                                           /* serverctrls (none)           */
89                 NULL,                                           /* clientctrls (none)           */
90                 &tv,                                            /* timeout                      */
91                 1,                                              /* sizelimit (1 result max)     */
92                 &search_result                                  /* res                          */
93         );
94
95         /* Ignore the return value of ldap_search_ext_s().  Sometimes it returns an error even when
96          * the search succeeds.  Instead, we check to see whether search_result is still NULL.
97          */
98         if (search_result == NULL) {
99                 syslog(LOG_DEBUG, "LDAP search: zero results were returned");
100                 ldap_unbind(ldserver);
101                 return(2);
102         }
103
104         /* At this point we've got at least one result from our query.  If there are multiple
105          * results, we still only look at the first one.
106          */
107         entry = ldap_first_entry(ldserver, search_result);
108         if (entry) {
109
110                 user_dn = ldap_get_dn(ldserver, entry);
111                 if (user_dn) {
112                         syslog(LOG_DEBUG, "dn = %s", user_dn);
113                 }
114
115                 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
116                         values = ldap_get_values(ldserver, search_result, "displayName");
117                         if (values) {
118                                 if (values[0]) {
119                                         if (fullname) safestrncpy(fullname, values[0], fullname_size);
120                                         syslog(LOG_DEBUG, "displayName = %s", values[0]);
121                                 }
122                                 ldap_value_free(values);
123                         }
124                 }
125                 else {
126                         values = ldap_get_values(ldserver, search_result, "cn");
127                         if (values) {
128                                 if (values[0]) {
129                                         if (fullname) safestrncpy(fullname, values[0], fullname_size);
130                                         syslog(LOG_DEBUG, "cn = %s", values[0]);
131                                 }
132                                 ldap_value_free(values);
133                         }
134                 }
135
136                 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
137                         values = ldap_get_values(ldserver, search_result, "objectGUID");
138                         if (values) {
139                                 if (values[0]) {
140                                         if (uid != NULL) {
141                                                 *uid = abs(HashLittle(values[0], strlen(values[0])));
142                                                 syslog(LOG_DEBUG, "uid hashed from objectGUID = %d", *uid);
143                                         }
144                                 }
145                                 ldap_value_free(values);
146                         }
147                 }
148                 else {
149                         values = ldap_get_values(ldserver, search_result, "uidNumber");
150                         if (values) {
151                                 if (values[0]) {
152                                         syslog(LOG_DEBUG, "uidNumber = %s", values[0]);
153                                         if (uid != NULL) {
154                                                 *uid = atoi(values[0]);
155                                         }
156                                 }
157                                 ldap_value_free(values);
158                         }
159                 }
160
161         }
162
163         /* free the results */
164         ldap_msgfree(search_result);
165
166         /* unbind so we can go back in as the authenticating user */
167         ldap_unbind(ldserver);
168
169         if (!user_dn) {
170                 syslog(LOG_DEBUG, "No such user was found.");
171                 return(4);
172         }
173
174         if (found_dn) safestrncpy(found_dn, user_dn, found_dn_size);
175         ldap_memfree(user_dn);
176         return(0);
177 }
178
179
180 int CtdlTryPasswordLDAP(char *user_dn, const char *password)
181 {
182         LDAP *ldserver = NULL;
183         int i = (-1);
184
185         if (IsEmptyStr(password)) {
186                 syslog(LOG_DEBUG, "LDAP: empty passwords are not permitted");
187                 return(1);
188         }
189
190         syslog(LOG_DEBUG, "LDAP: trying to bind as %s", user_dn);
191         ldserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
192         if (ldserver) {
193                 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
194                 i = ldap_simple_bind_s(ldserver, user_dn, password);
195                 if (i == LDAP_SUCCESS) {
196                         syslog(LOG_DEBUG, "LDAP: bind succeeded");
197                 }
198                 else {
199                         syslog(LOG_DEBUG, "LDAP: Cannot bind: %s (%d)", ldap_err2string(i), i);
200                 }
201                 ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
202                 ldap_unbind(ldserver);
203         }
204
205         if (i == LDAP_SUCCESS) {
206                 return(0);
207         }
208
209         return(1);
210 }
211
212 //return !0 iff property changed.
213 int vcard_set_props_iff_different(struct vCard *v,char *propname,int numvals, char **vals) {
214         int i;
215         char *oldval;
216         for(i=0;i<numvals;i++) {
217           oldval = vcard_get_prop(v,propname,0,i,0);
218           if (oldval == NULL) break;
219           if (strcmp(vals[i],oldval)) break;
220         }
221         if (i!=numvals) {
222                 for(i=0;i<numvals;i++) vcard_set_prop(v,propname,vals[i],(i==0) ? 0 : 1);
223                 return 1;
224         }
225         return 0;
226 }
227
228
229 //return !0 iff property changed.
230 int vcard_set_one_prop_iff_different(struct vCard *v,char *propname, char *newfmt, ...) {
231         va_list args;
232         char *newvalue;
233         int changed_something;
234         va_start(args,newfmt);
235         if (-1==vasprintf(&newvalue,newfmt,args)) {
236                 syslog(LOG_ALERT, "Out of memory!\n");
237                 return 0;
238         }
239         changed_something = vcard_set_props_iff_different(v,propname,1,&newvalue);
240         va_end(args);
241         free(newvalue);
242         return changed_something;
243 }
244
245 /*
246  * Learn LDAP attributes and stuff them into the vCard.
247  * Returns nonzero if we changed anything.
248  */
249 int Ctdl_LDAP_to_vCard(char *ldap_dn, struct vCard *v)
250 {
251         int changed_something = 0;
252         LDAP *ldserver = NULL;
253         int i;
254         struct timeval tv;
255         LDAPMessage *search_result = NULL;
256         LDAPMessage *entry = NULL;
257         char **givenName;
258         char **sn;
259         char **cn;
260         char **initials;
261         char **o;
262         char **street;
263         char **l;
264         char **st;
265         char **postalCode;
266         char **telephoneNumber;
267         char **mobile;
268         char **homePhone;
269         char **facsimileTelephoneNumber;
270         char **mail;
271         char **uid;
272         char **homeDirectory;
273         char **uidNumber;
274         char **loginShell;
275         char **gidNumber;
276         char **c;
277         char **title;
278         char **uuid;
279         char *attrs[] = { "*","+",NULL};
280
281         if (!ldap_dn) return(0);
282         if (!v) return(0);
283         ldserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
284         if (ldserver == NULL) {
285                 syslog(LOG_ALERT, "LDAP: Could not connect to %s:%d : %s",
286                         config.c_ldap_host, config.c_ldap_port,
287                         strerror(errno)
288                 );
289                 return(0);
290         }
291
292         ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
293         ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
294
295         striplt(config.c_ldap_bind_dn);
296         striplt(config.c_ldap_bind_pw);
297         syslog(LOG_DEBUG, "LDAP bind DN: %s", config.c_ldap_bind_dn);
298         i = ldap_simple_bind_s(ldserver,
299                 (!IsEmptyStr(config.c_ldap_bind_dn) ? config.c_ldap_bind_dn : NULL),
300                 (!IsEmptyStr(config.c_ldap_bind_pw) ? config.c_ldap_bind_pw : NULL)
301         );
302         if (i != LDAP_SUCCESS) {
303                 syslog(LOG_ALERT, "LDAP: Cannot bind: %s (%d)", ldap_err2string(i), i);
304                 return(0);
305         }
306
307         tv.tv_sec = 10;
308         tv.tv_usec = 0;
309
310         syslog(LOG_DEBUG, "LDAP search: %s", ldap_dn);
311         (void) ldap_search_ext_s(
312                 ldserver,                               /* ld                           */
313                 ldap_dn,                                /* base                         */
314                 LDAP_SCOPE_SUBTREE,             /* scope                        */
315                 NULL,                                   /* filter                       */
316                 attrs,                                  /* attrs (all attributes)       */
317                 0,                                              /* attrsonly (attrs + values)   */
318                 NULL,                                   /* serverctrls (none)           */
319                 NULL,                                   /* clientctrls (none)           */
320                 &tv,                                    /* timeout                      */
321                 1,                                              /* sizelimit (1 result max)     */
322                 &search_result                  /* res                          */
323         );
324         
325         /* Ignore the return value of ldap_search_ext_s().  Sometimes it returns an error even when
326          * the search succeeds.  Instead, we check to see whether search_result is still NULL.
327          */
328          
329         if (search_result == NULL) {
330                 syslog(LOG_DEBUG, "LDAP search: zero results were returned");
331                 ldap_unbind(ldserver);
332                 return(0);
333         }
334
335         /* At this point we've got at least one result from our query.  If there are multiple
336          * results, we still only look at the first one.
337          */
338
339         entry = ldap_first_entry(ldserver, search_result);
340         if (entry) {
341                 syslog(LOG_DEBUG, "LDAP search, got user details for vcard.");
342                 givenName=ldap_get_values(ldserver, search_result, "givenName");
343                 sn=ldap_get_values(ldserver, search_result, "sn");
344                 cn=ldap_get_values(ldserver, search_result, "cn");
345                 initials=ldap_get_values(ldserver, search_result, "initials");
346                 title=ldap_get_values(ldserver, search_result, "title");
347                 o=ldap_get_values(ldserver, search_result, "o");
348                 street=ldap_get_values(ldserver, search_result, "street");
349                 l=ldap_get_values(ldserver, search_result, "l");
350                 st=ldap_get_values(ldserver, search_result, "st");
351                 postalCode=ldap_get_values(ldserver, search_result, "postalCode");
352                 telephoneNumber=ldap_get_values(ldserver, search_result, "telephoneNumber");
353                 mobile=ldap_get_values(ldserver, search_result, "mobile");
354                 homePhone=ldap_get_values(ldserver, search_result, "homePhone");
355                 facsimileTelephoneNumber=ldap_get_values(ldserver, search_result, "facsimileTelephoneNumber");
356                 mail=ldap_get_values(ldserver, search_result, "mail");
357                 uid=ldap_get_values(ldserver, search_result, "uid");
358                 homeDirectory=ldap_get_values(ldserver, search_result, "homeDirectory");
359                 uidNumber=ldap_get_values(ldserver, search_result, "uidNumber");
360                 loginShell=ldap_get_values(ldserver, search_result, "loginShell");
361                 gidNumber=ldap_get_values(ldserver, search_result, "gidNumber");
362                 c=ldap_get_values(ldserver, search_result, "c");
363                 uuid=ldap_get_values(ldserver, search_result, "entryUUID");
364
365                 if (street && l && st && postalCode && c) changed_something |= vcard_set_one_prop_iff_different(v,"adr",";;%s;%s;%s;%s;%s",street[0],l[0],st[0],postalCode[0],c[0]);
366                 if (telephoneNumber) changed_something |= vcard_set_one_prop_iff_different(v,"tel;work","%s",telephoneNumber[0]);
367                 if (facsimileTelephoneNumber) changed_something |= vcard_set_one_prop_iff_different(v,"tel;fax","%s",facsimileTelephoneNumber[0]);
368                 if (mobile) changed_something |= vcard_set_one_prop_iff_different(v,"tel;cell","%s",mobile[0]);
369                 if (homePhone) changed_something |= vcard_set_one_prop_iff_different(v,"tel;home","%s",homePhone[0]);
370                 if (givenName && sn) {
371                         if (initials) {
372                                 changed_something |= vcard_set_one_prop_iff_different(v,"n","%s;%s;%s",sn[0],givenName[0],initials[0]);
373                         }
374                         else {
375                                 changed_something |= vcard_set_one_prop_iff_different(v,"n","%s;%s",sn[0],givenName[0]);
376                         }
377                 }
378                 if (mail) {
379                         changed_something |= vcard_set_props_iff_different(v,"email;internet",ldap_count_values(mail),mail);
380                 }
381                 if (uuid) changed_something |= vcard_set_one_prop_iff_different(v,"uid","%s",uuid[0]);
382                 if (o) changed_something |= vcard_set_one_prop_iff_different(v,"org","%s",o[0]);
383                 if (cn) changed_something |= vcard_set_one_prop_iff_different(v,"fn","%s",cn[0]);
384                 if (title) changed_something |= vcard_set_one_prop_iff_different(v,"title","%s",title[0]);
385                 
386                 if (givenName) ldap_value_free(givenName);
387                 if (initials) ldap_value_free(initials);
388                 if (sn) ldap_value_free(sn);
389                 if (cn) ldap_value_free(cn);
390                 if (o) ldap_value_free(o);
391                 if (street) ldap_value_free(street);
392                 if (l) ldap_value_free(l);
393                 if (st) ldap_value_free(st);
394                 if (postalCode) ldap_value_free(postalCode);
395                 if (telephoneNumber) ldap_value_free(telephoneNumber);
396                 if (mobile) ldap_value_free(mobile);
397                 if (homePhone) ldap_value_free(homePhone);
398                 if (facsimileTelephoneNumber) ldap_value_free(facsimileTelephoneNumber);
399                 if (mail) ldap_value_free(mail);
400                 if (uid) ldap_value_free(uid);
401                 if (homeDirectory) ldap_value_free(homeDirectory);
402                 if (uidNumber) ldap_value_free(uidNumber);
403                 if (loginShell) ldap_value_free(loginShell);
404                 if (gidNumber) ldap_value_free(gidNumber);
405                 if (c) ldap_value_free(c);
406                 if (title) ldap_value_free(title);
407                 if (uuid) ldap_value_free(uuid);
408         }
409         /* free the results */
410         ldap_msgfree(search_result);
411
412         /* unbind so we can go back in as the authenticating user */
413         ldap_unbind(ldserver);
414         
415         return(changed_something);      /* tell the caller whether we made any changes */
416 }
417
418 #endif /* HAVE_LDAP */