r25554: Convert last instances of BOOL, True and False to the standard types.
[kai/samba-autobuild/.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 3 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, see <http://www.gnu.org/licenses/>.
21    
22 */
23
24 #include "includes.h"
25 #include "libcli/ldap/ldap.h"
26 #include "libcli/ldap/ldap_client.h"
27 #include "lib/tls/tls.h"
28 #include "auth/gensec/gensec.h"
29 #include "auth/gensec/socket.h"
30 #include "auth/credentials/credentials.h"
31 #include "lib/stream/packet.h"
32
33 struct ldap_simple_creds {
34         const char *dn;
35         const char *pw;
36 };
37
38 NTSTATUS ldap_rebind(struct ldap_connection *conn)
39 {
40         NTSTATUS status;
41         struct ldap_simple_creds *creds;
42
43         switch (conn->bind.type) {
44         case LDAP_BIND_SASL:
45                 status = ldap_bind_sasl(conn, (struct cli_credentials *)conn->bind.creds);
46                 break;
47                 
48         case LDAP_BIND_SIMPLE:
49                 creds = (struct ldap_simple_creds *)conn->bind.creds;
50
51                 if (creds == NULL) {
52                         return NT_STATUS_UNSUCCESSFUL;
53                 }
54
55                 status = ldap_bind_simple(conn, creds->dn, creds->pw);
56                 break;
57
58         default:
59                 return NT_STATUS_UNSUCCESSFUL;
60         }
61
62         return status;
63 }
64
65
66 static struct ldap_message *new_ldap_simple_bind_msg(struct ldap_connection *conn, 
67                                                      const char *dn, const char *pw)
68 {
69         struct ldap_message *res;
70
71         res = new_ldap_message(conn);
72         if (!res) {
73                 return NULL;
74         }
75
76         res->type = LDAP_TAG_BindRequest;
77         res->r.BindRequest.version = 3;
78         res->r.BindRequest.dn = talloc_strdup(res, dn);
79         res->r.BindRequest.mechanism = LDAP_AUTH_MECH_SIMPLE;
80         res->r.BindRequest.creds.password = talloc_strdup(res, pw);
81         res->controls = NULL;
82
83         return res;
84 }
85
86
87 /*
88   perform a simple username/password bind
89 */
90 NTSTATUS ldap_bind_simple(struct ldap_connection *conn, 
91                           const char *userdn, const char *password)
92 {
93         struct ldap_request *req;
94         struct ldap_message *msg;
95         const char *dn, *pw;
96         NTSTATUS status;
97
98         if (conn == NULL) {
99                 return NT_STATUS_INVALID_CONNECTION;
100         }
101
102         if (userdn) {
103                 dn = userdn;
104         } else {
105                 if (conn->auth_dn) {
106                         dn = conn->auth_dn;
107                 } else {
108                         dn = "";
109                 }
110         }
111
112         if (password) {
113                 pw = password;
114         } else {
115                 if (conn->simple_pw) {
116                         pw = conn->simple_pw;
117                 } else {
118                         pw = "";
119                 }
120         }
121
122         msg = new_ldap_simple_bind_msg(conn, dn, pw);
123         NT_STATUS_HAVE_NO_MEMORY(msg);
124
125         /* send the request */
126         req = ldap_request_send(conn, msg);
127         talloc_free(msg);
128         NT_STATUS_HAVE_NO_MEMORY(req);
129
130         /* wait for replies */
131         status = ldap_request_wait(req);
132         if (!NT_STATUS_IS_OK(status)) {
133                 talloc_free(req);
134                 return status;
135         }
136
137         /* check its a valid reply */
138         msg = req->replies[0];
139         if (msg->type != LDAP_TAG_BindResponse) {
140                 talloc_free(req);
141                 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
142         }
143
144         status = ldap_check_response(conn, &msg->r.BindResponse.response);
145
146         talloc_free(req);
147
148         if (NT_STATUS_IS_OK(status)) {
149                 struct ldap_simple_creds *creds = talloc(conn, struct ldap_simple_creds);
150                 if (creds == NULL) {
151                         return NT_STATUS_NO_MEMORY;
152                 }
153                 creds->dn = talloc_strdup(creds, dn);
154                 creds->pw = talloc_strdup(creds, pw);
155                 if (creds->dn == NULL || creds->pw == NULL) {
156                         return NT_STATUS_NO_MEMORY;
157                 }
158                 conn->bind.type = LDAP_BIND_SIMPLE;
159                 conn->bind.creds = creds;
160         }
161
162         return status;
163 }
164
165
166 static struct ldap_message *new_ldap_sasl_bind_msg(struct ldap_connection *conn, 
167                                                    const char *sasl_mechanism, 
168                                                    DATA_BLOB *secblob)
169 {
170         struct ldap_message *res;
171
172         res = new_ldap_message(conn);
173         if (!res) {
174                 return NULL;
175         }
176
177         res->type = LDAP_TAG_BindRequest;
178         res->r.BindRequest.version = 3;
179         res->r.BindRequest.dn = "";
180         res->r.BindRequest.mechanism = LDAP_AUTH_MECH_SASL;
181         res->r.BindRequest.creds.SASL.mechanism = talloc_strdup(res, sasl_mechanism);
182         if (secblob) {
183                 res->r.BindRequest.creds.SASL.secblob = talloc(res, DATA_BLOB);
184                 if (!res->r.BindRequest.creds.SASL.secblob) {
185                         talloc_free(res);
186                         return NULL;
187                 }
188                 *res->r.BindRequest.creds.SASL.secblob = *secblob;
189         } else {
190                 res->r.BindRequest.creds.SASL.secblob = NULL;
191         }
192         res->controls = NULL;
193
194         return res;
195 }
196
197
198 /*
199   perform a sasl bind using the given credentials
200 */
201 NTSTATUS ldap_bind_sasl(struct ldap_connection *conn, struct cli_credentials *creds)
202 {
203         NTSTATUS status;
204         TALLOC_CTX *tmp_ctx = NULL;
205
206         DATA_BLOB input = data_blob(NULL, 0);
207         DATA_BLOB output = data_blob(NULL, 0);
208
209         struct ldap_message **sasl_mechs_msgs;
210         struct ldap_SearchResEntry *search;
211         int count, i;
212
213         const char **sasl_names;
214         uint32_t old_gensec_features;
215         static const char *supported_sasl_mech_attrs[] = {
216                 "supportedSASLMechanisms", 
217                 NULL 
218         };
219
220         status = gensec_client_start(conn, &conn->gensec, NULL);
221         if (!NT_STATUS_IS_OK(status)) {
222                 DEBUG(0, ("Failed to start GENSEC engine (%s)\n", nt_errstr(status)));
223                 goto failed;
224         }
225
226         /* require Kerberos SIGN/SEAL only if we don't use SSL
227          * Windows seem not to like double encryption */
228         old_gensec_features = cli_credentials_get_gensec_features(creds);
229         if (tls_enabled(conn->sock)) {
230                 cli_credentials_set_gensec_features(creds, 0);
231         }
232
233         /* this call also sets the gensec_want_features */
234         status = gensec_set_credentials(conn->gensec, creds);
235         if (!NT_STATUS_IS_OK(status)) {
236                 DEBUG(1, ("Failed to set GENSEC creds: %s\n", 
237                           nt_errstr(status)));
238                 goto failed;
239         }
240
241         /* reset the original gensec_features */
242         cli_credentials_set_gensec_features(creds, old_gensec_features);
243
244         if (conn->host) {
245                 status = gensec_set_target_hostname(conn->gensec, conn->host);
246                 if (!NT_STATUS_IS_OK(status)) {
247                         DEBUG(1, ("Failed to set GENSEC target hostname: %s\n", 
248                                   nt_errstr(status)));
249                         goto failed;
250                 }
251         }
252
253         status = gensec_set_target_service(conn->gensec, "ldap");
254         if (!NT_STATUS_IS_OK(status)) {
255                 DEBUG(1, ("Failed to set GENSEC target service: %s\n", 
256                           nt_errstr(status)));
257                 goto failed;
258         }
259
260         status = ildap_search(conn, "", LDAP_SEARCH_SCOPE_BASE, "", supported_sasl_mech_attrs, 
261                               false, NULL, NULL, &sasl_mechs_msgs);
262         if (!NT_STATUS_IS_OK(status)) {
263                 DEBUG(1, ("Failed to inquire of target's available sasl mechs in rootdse search: %s\n", 
264                           nt_errstr(status)));
265                 goto failed;
266         }
267         
268         count = ildap_count_entries(conn, sasl_mechs_msgs);
269         if (count != 1) {
270                 DEBUG(1, ("Failed to inquire of target's available sasl mechs in rootdse search: wrong number of replies: %d\n",
271                           count));
272                 goto failed;
273         }
274
275         tmp_ctx = talloc_new(conn);
276         if (tmp_ctx == NULL) goto failed;
277
278         search = &sasl_mechs_msgs[0]->r.SearchResultEntry;
279         if (search->num_attributes != 1) {
280                 DEBUG(1, ("Failed to inquire of target's available sasl mechs in rootdse search: wrong number of attributes: %d\n",
281                           search->num_attributes));
282                 goto failed;
283         }
284
285         sasl_names = talloc_array(tmp_ctx, const char *, search->attributes[0].num_values + 1);
286         if (!sasl_names) {
287                 DEBUG(1, ("talloc_arry(char *, %d) failed\n",
288                           count));
289                 goto failed;
290         }
291                 
292         for (i=0; i<search->attributes[0].num_values; i++) {
293                 sasl_names[i] = (const char *)search->attributes[0].values[i].data;
294         }
295         sasl_names[i] = NULL;
296         
297         status = gensec_start_mech_by_sasl_list(conn->gensec, sasl_names);
298         if (!NT_STATUS_IS_OK(status)) {
299                 DEBUG(1, ("None of the %d proposed SASL mechs were acceptable: %s\n",
300                           count, nt_errstr(status)));
301                 goto failed;
302         }
303
304         while (1) {
305                 NTSTATUS gensec_status;
306                 struct ldap_message *response;
307                 struct ldap_message *msg;
308                 struct ldap_request *req;
309                 int result = LDAP_OTHER;
310         
311                 status = gensec_update(conn->gensec, tmp_ctx,
312                                        input,
313                                        &output);
314                 /* The status value here, from GENSEC is vital to the security
315                  * of the system.  Even if the other end accepts, if GENSEC
316                  * claims 'MORE_PROCESSING_REQUIRED' then you must keep
317                  * feeding it blobs, or else the remote host/attacker might
318                  * avoid mutal authentication requirements.
319                  *
320                  * Likewise, you must not feed GENSEC too much (after the OK),
321                  * it doesn't like that either
322                  */
323
324                 gensec_status = status;
325
326                 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) && 
327                     !NT_STATUS_IS_OK(status)) {
328                         break;
329                 }
330                 if (NT_STATUS_IS_OK(status) && output.length == 0) {
331                         break;
332                 }
333
334                 /* Perhaps we should make gensec_start_mech_by_sasl_list() return the name we got? */
335                 msg = new_ldap_sasl_bind_msg(tmp_ctx, conn->gensec->ops->sasl_name, (output.data?&output:NULL));
336                 if (msg == NULL) {
337                         status = NT_STATUS_NO_MEMORY;
338                         goto failed;
339                 }
340
341                 req = ldap_request_send(conn, msg);
342                 if (req == NULL) {
343                         status = NT_STATUS_NO_MEMORY;
344                         goto failed;
345                 }
346                 talloc_steal(tmp_ctx, req);
347
348                 status = ldap_result_n(req, 0, &response);
349                 if (!NT_STATUS_IS_OK(status)) {
350                         goto failed;
351                 }
352                 
353                 if (response->type != LDAP_TAG_BindResponse) {
354                         status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
355                         goto failed;
356                 }
357
358                 result = response->r.BindResponse.response.resultcode;
359
360                 if (result != LDAP_SUCCESS && result != LDAP_SASL_BIND_IN_PROGRESS) {
361                         status = ldap_check_response(conn, 
362                                                      &response->r.BindResponse.response);
363                         break;
364                 }
365
366                 /* This is where we check if GENSEC wanted to be fed more data */
367                 if (!NT_STATUS_EQUAL(gensec_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
368                         break;
369                 }
370                 if (response->r.BindResponse.SASL.secblob) {
371                         input = *response->r.BindResponse.SASL.secblob;
372                 } else {
373                         input = data_blob(NULL, 0);
374                 }
375         }
376
377         talloc_free(tmp_ctx);
378
379         if (NT_STATUS_IS_OK(status)) {
380                 struct socket_context *sasl_socket;
381                 status = gensec_socket_init(conn->gensec, 
382                                             conn->sock,
383                                             conn->event.event_ctx, 
384                                             ldap_read_io_handler,
385                                             conn,
386                                             &sasl_socket);
387                 if (!NT_STATUS_IS_OK(status)) goto failed;
388
389                 talloc_steal(conn->sock, sasl_socket);
390                 talloc_unlink(conn, conn->sock);
391                 conn->sock = sasl_socket;
392                 packet_set_socket(conn->packet, conn->sock);
393
394                 conn->bind.type = LDAP_BIND_SASL;
395                 conn->bind.creds = creds;
396         }
397
398         return status;
399
400 failed:
401         talloc_free(tmp_ctx);
402         talloc_free(conn->gensec);
403         conn->gensec = NULL;
404         return status;
405 }