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