r13508: some ASN.1 element in LDAP are optional,
[metze/samba/wip.git] / source4 / libcli / ldap / ldap_bind.c
1 /* 
2    Unix SMB/CIFS mplementation.
3
4    LDAP bind calls
5    
6    Copyright (C) Andrew Tridgell  2005
7    Copyright (C) Volker Lendecke  2004
8     
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22    
23 */
24
25 #include "includes.h"
26 #include "libcli/ldap/ldap.h"
27 #include "libcli/ldap/ldap_client.h"
28 #include "auth/auth.h"
29
30 static struct ldap_message *new_ldap_simple_bind_msg(struct ldap_connection *conn, 
31                                                      const char *dn, const char *pw)
32 {
33         struct ldap_message *res;
34
35         res = new_ldap_message(conn);
36         if (!res) {
37                 return NULL;
38         }
39
40         res->type = LDAP_TAG_BindRequest;
41         res->r.BindRequest.version = 3;
42         res->r.BindRequest.dn = talloc_strdup(res, dn);
43         res->r.BindRequest.mechanism = LDAP_AUTH_MECH_SIMPLE;
44         res->r.BindRequest.creds.password = talloc_strdup(res, pw);
45         res->controls = NULL;
46
47         return res;
48 }
49
50
51 /*
52   perform a simple username/password bind
53 */
54 NTSTATUS ldap_bind_simple(struct ldap_connection *conn, 
55                           const char *userdn, const char *password)
56 {
57         struct ldap_request *req;
58         struct ldap_message *msg;
59         const char *dn, *pw;
60         NTSTATUS status;
61
62         if (conn == NULL) {
63                 return NT_STATUS_INVALID_CONNECTION;
64         }
65
66         if (userdn) {
67                 dn = userdn;
68         } else {
69                 if (conn->auth_dn) {
70                         dn = conn->auth_dn;
71                 } else {
72                         dn = "";
73                 }
74         }
75
76         if (password) {
77                 pw = password;
78         } else {
79                 if (conn->simple_pw) {
80                         pw = conn->simple_pw;
81                 } else {
82                         pw = "";
83                 }
84         }
85
86         msg = new_ldap_simple_bind_msg(conn, dn, pw);
87         NT_STATUS_HAVE_NO_MEMORY(msg);
88
89         /* send the request */
90         req = ldap_request_send(conn, msg);
91         talloc_free(msg);
92         NT_STATUS_HAVE_NO_MEMORY(req);
93
94         /* wait for replies */
95         status = ldap_request_wait(req);
96         if (!NT_STATUS_IS_OK(status)) {
97                 talloc_free(req);
98                 return status;
99         }
100
101         /* check its a valid reply */
102         msg = req->replies[0];
103         if (msg->type != LDAP_TAG_BindResponse) {
104                 talloc_free(req);
105                 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
106         }
107
108         status = ldap_check_response(conn, &msg->r.BindResponse.response);
109
110         talloc_free(req);
111
112         return status;
113 }
114
115
116 static struct ldap_message *new_ldap_sasl_bind_msg(struct ldap_connection *conn, 
117                                                    const char *sasl_mechanism, 
118                                                    DATA_BLOB *secblob)
119 {
120         struct ldap_message *res;
121
122         res = new_ldap_message(conn);
123         if (!res) {
124                 return NULL;
125         }
126
127         res->type = LDAP_TAG_BindRequest;
128         res->r.BindRequest.version = 3;
129         res->r.BindRequest.dn = "";
130         res->r.BindRequest.mechanism = LDAP_AUTH_MECH_SASL;
131         res->r.BindRequest.creds.SASL.mechanism = talloc_strdup(res, sasl_mechanism);
132         if (secblob) {
133                 res->r.BindRequest.creds.SASL.secblob = talloc(res, DATA_BLOB);
134                 if (!res->r.BindRequest.creds.SASL.secblob) {
135                         talloc_free(res);
136                         return NULL;
137                 }
138                 *res->r.BindRequest.creds.SASL.secblob = *secblob;
139         } else {
140                 res->r.BindRequest.creds.SASL.secblob = NULL;
141         }
142         res->controls = NULL;
143
144         return res;
145 }
146
147
148 /*
149   perform a sasl bind using the given credentials
150 */
151 NTSTATUS ldap_bind_sasl(struct ldap_connection *conn, struct cli_credentials *creds)
152 {
153         NTSTATUS status;
154         TALLOC_CTX *tmp_ctx = NULL;
155
156         DATA_BLOB input = data_blob(NULL, 0);
157         DATA_BLOB output = data_blob(NULL, 0);
158
159         struct ldap_message **sasl_mechs_msgs;
160         struct ldap_SearchResEntry *search;
161         int count, i;
162
163         const char **sasl_names;
164         
165         static const char *supported_sasl_mech_attrs[] = {
166                 "supportedSASLMechanisms", 
167                 NULL 
168         };
169
170         status = gensec_client_start(conn, &conn->gensec, NULL);
171         if (!NT_STATUS_IS_OK(status)) {
172                 DEBUG(0, ("Failed to start GENSEC engine (%s)\n", nt_errstr(status)));
173                 goto failed;
174         }
175
176         gensec_want_feature(conn->gensec, 0 | GENSEC_FEATURE_SIGN | GENSEC_FEATURE_SEAL);
177
178         status = gensec_set_credentials(conn->gensec, creds);
179         if (!NT_STATUS_IS_OK(status)) {
180                 DEBUG(1, ("Failed to set GENSEC creds: %s\n", 
181                           nt_errstr(status)));
182                 goto failed;
183         }
184
185         status = gensec_set_target_hostname(conn->gensec, conn->host);
186         if (!NT_STATUS_IS_OK(status)) {
187                 DEBUG(1, ("Failed to set GENSEC target hostname: %s\n", 
188                           nt_errstr(status)));
189                 goto failed;
190         }
191
192         status = gensec_set_target_service(conn->gensec, "ldap");
193         if (!NT_STATUS_IS_OK(status)) {
194                 DEBUG(1, ("Failed to set GENSEC target service: %s\n", 
195                           nt_errstr(status)));
196                 goto failed;
197         }
198
199         status = ildap_search(conn, "", LDAP_SEARCH_SCOPE_BASE, "", supported_sasl_mech_attrs, 
200                               False, NULL, NULL, &sasl_mechs_msgs);
201         if (!NT_STATUS_IS_OK(status)) {
202                 DEBUG(1, ("Failed to inquire of target's available sasl mechs in rootdse search: %s\n", 
203                           nt_errstr(status)));
204                 goto failed;
205         }
206         
207         count = ildap_count_entries(conn, sasl_mechs_msgs);
208         if (count != 1) {
209                 DEBUG(1, ("Failed to inquire of target's available sasl mechs in rootdse search: wrong number of replies: %d\n",
210                           count));
211                 goto failed;
212         }
213
214         tmp_ctx = talloc_new(conn);
215         if (tmp_ctx == NULL) goto failed;
216
217         search = &sasl_mechs_msgs[0]->r.SearchResultEntry;
218         if (search->num_attributes != 1) {
219                 DEBUG(1, ("Failed to inquire of target's available sasl mechs in rootdse search: wrong number of attributes: %d\n",
220                           search->num_attributes));
221                 goto failed;
222         }
223
224         sasl_names = talloc_array(tmp_ctx, const char *, search->attributes[0].num_values + 1);
225         if (!sasl_names) {
226                 DEBUG(1, ("talloc_arry(char *, %d) failed\n",
227                           count));
228                 goto failed;
229         }
230                 
231         for (i=0; i<search->attributes[0].num_values; i++) {
232                 sasl_names[i] = (const char *)search->attributes[0].values[i].data;
233         }
234         sasl_names[i] = NULL;
235         
236         status = gensec_start_mech_by_sasl_list(conn->gensec, sasl_names);
237         if (!NT_STATUS_IS_OK(status)) {
238                 DEBUG(1, ("None of the %d proposed SASL mechs were acceptable: %s\n",
239                           count, nt_errstr(status)));
240                 goto failed;
241         }
242
243         while (1) {
244                 NTSTATUS gensec_status;
245                 struct ldap_message *response;
246                 struct ldap_message *msg;
247                 struct ldap_request *req;
248                 int result = LDAP_OTHER;
249         
250                 status = gensec_update(conn->gensec, tmp_ctx,
251                                        input,
252                                        &output);
253                 /* The status value here, from GENSEC is vital to the security
254                  * of the system.  Even if the other end accepts, if GENSEC
255                  * claims 'MORE_PROCESSING_REQUIRED' then you must keep
256                  * feeding it blobs, or else the remote host/attacker might
257                  * avoid mutal authentication requirements.
258                  *
259                  * Likewise, you must not feed GENSEC too much (after the OK),
260                  * it doesn't like that either
261                  */
262
263                 gensec_status = status;
264
265                 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) && 
266                     !NT_STATUS_IS_OK(status)) {
267                         break;
268                 }
269                 if (NT_STATUS_IS_OK(status) && output.length == 0) {
270                         break;
271                 }
272
273                 /* Perhaps we should make gensec_start_mech_by_sasl_list() return the name we got? */
274                 msg = new_ldap_sasl_bind_msg(tmp_ctx, conn->gensec->ops->sasl_name, (output.data?&output:NULL));
275                 if (msg == NULL) {
276                         status = NT_STATUS_NO_MEMORY;
277                         goto failed;
278                 }
279
280                 req = ldap_request_send(conn, msg);
281                 if (req == NULL) {
282                         status = NT_STATUS_NO_MEMORY;
283                         goto failed;
284                 }
285                 talloc_steal(tmp_ctx, req);
286
287                 status = ldap_result_n(req, 0, &response);
288                 if (!NT_STATUS_IS_OK(status)) {
289                         goto failed;
290                 }
291                 
292                 if (response->type != LDAP_TAG_BindResponse) {
293                         status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
294                         goto failed;
295                 }
296
297                 result = response->r.BindResponse.response.resultcode;
298
299                 if (result != LDAP_SUCCESS && result != LDAP_SASL_BIND_IN_PROGRESS) {
300                         status = ldap_check_response(conn, 
301                                                      &response->r.BindResponse.response);
302                         break;
303                 }
304
305                 /* This is where we check if GENSEC wanted to be fed more data */
306                 if (!NT_STATUS_EQUAL(gensec_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
307                         break;
308                 }
309                 if (response->r.BindResponse.SASL.secblob) {
310                         input = *response->r.BindResponse.SASL.secblob;
311                 } else {
312                         input = data_blob(NULL, 0);
313                 }
314         }
315
316         if (NT_STATUS_IS_OK(status) &&
317             (gensec_have_feature(conn->gensec, GENSEC_FEATURE_SEAL) ||
318              gensec_have_feature(conn->gensec, GENSEC_FEATURE_SIGN))) {
319                 conn->enable_wrap = True;
320         }
321
322         talloc_free(tmp_ctx);
323         return status;
324
325 failed:
326         talloc_free(tmp_ctx);
327         talloc_free(conn->gensec);
328         conn->gensec = NULL;
329         return status;
330 }