a6f3db3c275f035e4a570b7664a2a18d6acfc008
[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, 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         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\n",
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\n", 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)\n", ldap_err2string(i), i);
68                 return(i);
69         }
70
71         tv.tv_sec = 10;
72         tv.tv_usec = 0;
73
74         if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
75                 sprintf(searchstring, "(sAMAccountName=%s)", username);
76         }
77         else {
78                 sprintf(searchstring, "(&(objectclass=posixAccount)(uid=%s))", username);
79         }
80
81         syslog(LOG_DEBUG, "LDAP search: %s\n", searchstring);
82         (void) ldap_search_ext_s(
83                 ldserver,                                       /* ld                           */
84                 config.c_ldap_base_dn,                          /* base                         */
85                 LDAP_SCOPE_SUBTREE,                             /* scope                        */
86                 searchstring,                                   /* filter                       */
87                 NULL,                                           /* attrs (all attributes)       */
88                 0,                                              /* attrsonly (attrs + values)   */
89                 NULL,                                           /* serverctrls (none)           */
90                 NULL,                                           /* clientctrls (none)           */
91                 &tv,                                            /* timeout                      */
92                 1,                                              /* sizelimit (1 result max)     */
93                 &search_result                                  /* res                          */
94         );
95
96         /* Ignore the return value of ldap_search_ext_s().  Sometimes it returns an error even when
97          * the search succeeds.  Instead, we check to see whether search_result is still NULL.
98          */
99         if (search_result == NULL) {
100                 syslog(LOG_DEBUG, "LDAP search: zero results were returned\n");
101                 ldap_unbind(ldserver);
102                 return(2);
103         }
104
105         /* At this point we've got at least one result from our query.  If there are multiple
106          * results, we still only look at the first one.
107          */
108         entry = ldap_first_entry(ldserver, search_result);
109         if (entry) {
110
111                 user_dn = ldap_get_dn(ldserver, entry);
112                 if (user_dn) {
113                         syslog(LOG_DEBUG, "dn = %s\n", user_dn);
114                 }
115
116                 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
117                         values = ldap_get_values(ldserver, search_result, "displayName");
118                         if (values) {
119                                 if (values[0]) {
120                                         if (fullname) safestrncpy(fullname, values[0], fullname_size);
121                                         syslog(LOG_DEBUG, "displayName = %s\n", values[0]);
122                                 }
123                                 ldap_value_free(values);
124                         }
125                 }
126                 else {
127                         values = ldap_get_values(ldserver, search_result, "cn");
128                         if (values) {
129                                 if (values[0]) {
130                                         if (fullname) safestrncpy(fullname, values[0], fullname_size);
131                                         syslog(LOG_DEBUG, "cn = %s\n", values[0]);
132                                 }
133                                 ldap_value_free(values);
134                         }
135                 }
136
137                 if (config.c_auth_mode == AUTHMODE_LDAP_AD) {
138                         values = ldap_get_values(ldserver, search_result, "objectGUID");
139                         if (values) {
140                                 if (values[0]) {
141                                         if (uid != NULL) {
142                                                 *uid = abs(HashLittle(values[0], strlen(values[0])));
143                                                 syslog(LOG_DEBUG, "uid hashed from objectGUID = %d\n", *uid);
144                                         }
145                                 }
146                                 ldap_value_free(values);
147                         }
148                 }
149                 else {
150                         values = ldap_get_values(ldserver, search_result, "uidNumber");
151                         if (values) {
152                                 if (values[0]) {
153                                         syslog(LOG_DEBUG, "uidNumber = %s\n", values[0]);
154                                         if (uid != NULL) {
155                                                 *uid = atoi(values[0]);
156                                         }
157                                 }
158                                 ldap_value_free(values);
159                         }
160                 }
161
162         }
163
164         /* free the results */
165         ldap_msgfree(search_result);
166
167         /* unbind so we can go back in as the authenticating user */
168         ldap_unbind(ldserver);
169
170         if (!user_dn) {
171                 syslog(LOG_DEBUG, "No such user was found.\n");
172                 return(4);
173         }
174
175         if (found_dn) safestrncpy(found_dn, user_dn, found_dn_size);
176         ldap_memfree(user_dn);
177         return(0);
178 }
179
180
181 int CtdlTryPasswordLDAP(char *user_dn, const char *password)
182 {
183         LDAP *ldserver = NULL;
184         int i = (-1);
185
186         if (IsEmptyStr(password)) {
187                 syslog(LOG_DEBUG, "LDAP: empty passwords are not permitted\n");
188                 return(1);
189         }
190
191         syslog(LOG_DEBUG, "LDAP: trying to bind as %s\n", user_dn);
192         ldserver = ldap_init(config.c_ldap_host, config.c_ldap_port);
193         if (ldserver) {
194                 ldap_set_option(ldserver, LDAP_OPT_PROTOCOL_VERSION, &ctdl_require_ldap_version);
195                 i = ldap_simple_bind_s(ldserver, user_dn, password);
196                 if (i == LDAP_SUCCESS) {
197                         syslog(LOG_DEBUG, "LDAP: bind succeeded\n");
198                 }
199                 else {
200                         syslog(LOG_DEBUG, "LDAP: Cannot bind: %s (%d)\n", ldap_err2string(i), i);
201                 }
202                 ldap_set_option(ldserver, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_OFF);
203                 ldap_unbind(ldserver);
204         }
205
206         if (i == LDAP_SUCCESS) {
207                 return(0);
208         }
209
210         return(1);
211 }
212
213
214 /*
215  * Learn LDAP attributes and stuff them into the vCard.
216  * Returns nonzero if we changed anything.
217  */
218 int Ctdl_LDAP_to_vCard(char *ldap_dn, struct vCard *v)
219 {
220         int changed_something = 0;
221
222         if (!ldap_dn) return(0);
223         if (!v) return(0);
224
225         /*
226          * FIXME this is a stub function
227          *
228          * ldap_dn will contain the DN of the user, and v will contain a pointer to
229          * the vCard that needs to be (re-)populated.  Put the requisite LDAP code here.
230          *
231         vcard_set_prop(v, "email;internet", xxx, 0);
232          *
233          * return nonzero to tell the caller that we made changes that need to be saved
234         changed_something = 1;
235          *
236          */
237
238         return(changed_something);      /* tell the caller whether we made any changes */
239 }
240
241 #endif /* HAVE_LDAP */