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