s4:ldap_server: introduce a ldapsrv_call_destructor()
[samba.git] / source4 / ldap_server / ldap_bind.c
1 /* 
2    Unix SMB/CIFS implementation.
3    LDAP server
4    Copyright (C) Stefan Metzmacher 2004
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "ldap_server/ldap_server.h"
22 #include "auth/auth.h"
23 #include "smbd/service.h"
24 #include <ldb.h>
25 #include <ldb_errors.h>
26 #include "dsdb/samdb/samdb.h"
27 #include "auth/gensec/gensec.h"
28 #include "auth/gensec/gensec_tstream.h"
29 #include "param/param.h"
30 #include "../lib/util/tevent_ntstatus.h"
31
32 static char *ldapsrv_bind_error_msg(TALLOC_CTX *mem_ctx,
33                                     HRESULT hresult,
34                                     uint32_t DSID,
35                                     NTSTATUS status)
36 {
37         WERROR werr;
38         char *msg = NULL;
39
40         status = nt_status_squash(status);
41         werr = ntstatus_to_werror(status);
42
43         /*
44          * There are 4 lower case hex digits following 'v' at the end,
45          * but different Windows Versions return different values:
46          *
47          * Windows 2008R2 uses 'v1db1'
48          * Windows 2012R2 uses 'v2580'
49          *
50          * We just match Windows 2008R2 as that's what was referenced
51          * in https://bugzilla.samba.org/show_bug.cgi?id=9048
52          */
53         msg = talloc_asprintf(mem_ctx, "%08X: LdapErr: DSID-%08X, comment: "
54                               "AcceptSecurityContext error, data %x, v1db1",
55                               (unsigned)HRES_ERROR_V(hresult),
56                               (unsigned)DSID,
57                               (unsigned)W_ERROR_V(werr));
58
59         return msg;
60 }
61
62
63 static NTSTATUS ldapsrv_BindSimple(struct ldapsrv_call *call)
64 {
65         struct ldap_BindRequest *req = &call->request->r.BindRequest;
66         struct ldapsrv_reply *reply;
67         struct ldap_BindResponse *resp;
68
69         int result;
70         const char *errstr;
71
72         struct auth_session_info *session_info;
73
74         NTSTATUS status;
75
76         bool using_tls = call->conn->sockets.active == call->conn->sockets.tls;
77
78         DEBUG(10, ("BindSimple dn: %s\n",req->dn));
79
80         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
81         if (!reply) {
82                 return NT_STATUS_NO_MEMORY;
83         }
84
85         if (req->dn != NULL &&
86             strlen(req->dn) != 0 &&
87             call->conn->require_strong_auth > LDAP_SERVER_REQUIRE_STRONG_AUTH_NO &&
88             !using_tls)
89         {
90                 status = NT_STATUS_NETWORK_ACCESS_DENIED;
91                 result = LDAP_STRONG_AUTH_REQUIRED;
92                 errstr = talloc_asprintf(reply,
93                                          "BindSimple: Transport encryption required.");
94                 goto do_reply;
95         }
96
97         status = authenticate_ldap_simple_bind(call,
98                                                call->conn->connection->event.ctx,
99                                                call->conn->connection->msg_ctx,
100                                                call->conn->lp_ctx,
101                                                call->conn->connection->remote_address,
102                                                call->conn->connection->local_address,
103                                                using_tls,
104                                                req->dn,
105                                                req->creds.password,
106                                                &session_info);
107
108         if (NT_STATUS_IS_OK(status)) {
109                 result = LDAP_SUCCESS;
110                 errstr = NULL;
111
112                 talloc_unlink(call->conn, call->conn->session_info);
113                 call->conn->session_info = talloc_steal(call->conn, session_info);
114
115                 call->conn->authz_logged = true;
116
117                 /* don't leak the old LDB */
118                 talloc_unlink(call->conn, call->conn->ldb);
119
120                 status = ldapsrv_backend_Init(call->conn);              
121                 
122                 if (!NT_STATUS_IS_OK(status)) {
123                         result = LDAP_OPERATIONS_ERROR;
124                         errstr = talloc_asprintf(reply, "Simple Bind: Failed to advise ldb new credentials: %s", nt_errstr(status));
125                 }
126         } else {
127                 status = nt_status_squash(status);
128
129                 result = LDAP_INVALID_CREDENTIALS;
130                 errstr = ldapsrv_bind_error_msg(reply, HRES_SEC_E_INVALID_TOKEN,
131                                                 0x0C0903A9, status);
132         }
133
134 do_reply:
135         resp = &reply->msg->r.BindResponse;
136         resp->response.resultcode = result;
137         resp->response.errormessage = errstr;
138         resp->response.dn = NULL;
139         resp->response.referral = NULL;
140         resp->SASL.secblob = NULL;
141
142         ldapsrv_queue_reply(call, reply);
143         return NT_STATUS_OK;
144 }
145
146 struct ldapsrv_sasl_postprocess_context {
147         struct ldapsrv_connection *conn;
148         struct tstream_context *sasl;
149 };
150
151 struct ldapsrv_sasl_postprocess_state {
152         uint8_t dummy;
153 };
154
155 static struct tevent_req *ldapsrv_sasl_postprocess_send(TALLOC_CTX *mem_ctx,
156                                                 struct tevent_context *ev,
157                                                 void *private_data)
158 {
159         struct ldapsrv_sasl_postprocess_context *context =
160                 talloc_get_type_abort(private_data,
161                 struct ldapsrv_sasl_postprocess_context);
162         struct tevent_req *req;
163         struct ldapsrv_sasl_postprocess_state *state;
164
165         req = tevent_req_create(mem_ctx, &state,
166                                 struct ldapsrv_sasl_postprocess_state);
167         if (req == NULL) {
168                 return NULL;
169         }
170
171         TALLOC_FREE(context->conn->sockets.sasl);
172         context->conn->sockets.sasl = talloc_move(context->conn, &context->sasl);
173         context->conn->sockets.active = context->conn->sockets.sasl;
174
175         tevent_req_done(req);
176         return tevent_req_post(req, ev);
177 }
178
179 static NTSTATUS ldapsrv_sasl_postprocess_recv(struct tevent_req *req)
180 {
181         return tevent_req_simple_recv_ntstatus(req);
182 }
183
184 static NTSTATUS ldapsrv_setup_gensec(struct ldapsrv_connection *conn,
185                                      const char *sasl_mech,
186                                      struct gensec_security **_gensec_security)
187 {
188         NTSTATUS status;
189
190         struct gensec_security *gensec_security;
191
192         status = samba_server_gensec_start(conn,
193                                            conn->connection->event.ctx,
194                                            conn->connection->msg_ctx,
195                                            conn->lp_ctx,
196                                            conn->server_credentials,
197                                            "ldap",
198                                            &gensec_security);
199         if (!NT_STATUS_IS_OK(status)) {
200                 return status;
201         }
202
203         status = gensec_set_target_service_description(gensec_security,
204                                                        "LDAP");
205         if (!NT_STATUS_IS_OK(status)) {
206                 return status;
207         }
208
209         status = gensec_set_remote_address(gensec_security,
210                                            conn->connection->remote_address);
211         if (!NT_STATUS_IS_OK(status)) {
212                 return status;
213         }
214
215         status = gensec_set_local_address(gensec_security,
216                                           conn->connection->local_address);
217         if (!NT_STATUS_IS_OK(status)) {
218                 return status;
219         }
220
221         gensec_want_feature(gensec_security, GENSEC_FEATURE_ASYNC_REPLIES);
222         gensec_want_feature(gensec_security, GENSEC_FEATURE_LDAP_STYLE);
223
224         if (conn->sockets.active == conn->sockets.tls) {
225                 gensec_want_feature(gensec_security, GENSEC_FEATURE_LDAPS_TRANSPORT);
226         }
227
228         status = gensec_start_mech_by_sasl_name(gensec_security, sasl_mech);
229
230         if (!NT_STATUS_IS_OK(status)) {
231                 return status;
232         }
233
234         *_gensec_security = gensec_security;
235         return status;
236 }
237
238 static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
239 {
240         struct ldap_BindRequest *req = &call->request->r.BindRequest;
241         struct ldapsrv_reply *reply;
242         struct ldap_BindResponse *resp;
243         struct ldapsrv_connection *conn;
244         int result = 0;
245         const char *errstr=NULL;
246         NTSTATUS status = NT_STATUS_OK;
247
248         DEBUG(10, ("BindSASL dn: %s\n",req->dn));
249
250         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
251         if (!reply) {
252                 return NT_STATUS_NO_MEMORY;
253         }
254         resp = &reply->msg->r.BindResponse;
255         
256         conn = call->conn;
257
258         /* 
259          * TODO: a SASL bind with a different mechanism
260          *       should cancel an inprogress SASL bind.
261          *       (see RFC 4513)
262          */
263
264         if (!conn->gensec) {
265                 status = ldapsrv_setup_gensec(conn, req->creds.SASL.mechanism,
266                                               &conn->gensec);
267                 if (!NT_STATUS_IS_OK(status)) {
268                         DEBUG(1, ("Failed to start GENSEC server for [%s] code: %s\n",
269                                   ldb_binary_encode_string(call, req->creds.SASL.mechanism),
270                                   nt_errstr(status)));
271                         result = LDAP_OPERATIONS_ERROR;
272                         errstr = talloc_asprintf(reply, "SASL: Failed to start authentication system: %s", 
273                                                  nt_errstr(status));
274                 }
275         }
276
277         if (NT_STATUS_IS_OK(status)) {
278                 DATA_BLOB input = data_blob(NULL, 0);
279                 DATA_BLOB output = data_blob(NULL, 0);
280
281                 if (req->creds.SASL.secblob) {
282                         input = *req->creds.SASL.secblob;
283                 }
284
285                 status = gensec_update_ev(conn->gensec, reply, conn->connection->event.ctx,
286                                           input, &output);
287
288                 /* Windows 2000 mmc doesn't like secblob == NULL and reports a decoding error */
289                 resp->SASL.secblob = talloc(reply, DATA_BLOB);
290                 NT_STATUS_HAVE_NO_MEMORY(resp->SASL.secblob);
291                 *resp->SASL.secblob = output;
292         } else {
293                 resp->SASL.secblob = NULL;
294         }
295
296         if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
297                 result = LDAP_SASL_BIND_IN_PROGRESS;
298                 errstr = NULL;
299         } else if (NT_STATUS_IS_OK(status)) {
300                 struct ldapsrv_sasl_postprocess_context *context = NULL;
301
302                 result = LDAP_SUCCESS;
303                 errstr = NULL;
304
305                 if (gensec_have_feature(conn->gensec, GENSEC_FEATURE_SIGN) ||
306                     gensec_have_feature(conn->gensec, GENSEC_FEATURE_SEAL)) {
307
308                         context = talloc(call, struct ldapsrv_sasl_postprocess_context);
309
310                         if (!context) {
311                                 status = NT_STATUS_NO_MEMORY;
312                         }
313                 }
314
315                 if (context && conn->sockets.tls) {
316                         TALLOC_FREE(context);
317                         status = NT_STATUS_NOT_SUPPORTED;
318                         result = LDAP_UNWILLING_TO_PERFORM;
319                         errstr = talloc_asprintf(reply,
320                                                  "SASL:[%s]: Sign or Seal are not allowed if TLS is used",
321                                                  req->creds.SASL.mechanism);
322                 }
323
324                 if (context && conn->sockets.sasl) {
325                         TALLOC_FREE(context);
326                         status = NT_STATUS_NOT_SUPPORTED;
327                         result = LDAP_UNWILLING_TO_PERFORM;
328                         errstr = talloc_asprintf(reply,
329                                                  "SASL:[%s]: Sign or Seal are not allowed if SASL encryption has already been set up",
330                                                  req->creds.SASL.mechanism);
331                 }
332
333                 if (context) {
334                         context->conn = conn;
335                         status = gensec_create_tstream(context,
336                                                        context->conn->gensec,
337                                                        context->conn->sockets.raw,
338                                                        &context->sasl);
339                         if (NT_STATUS_IS_OK(status)) {
340                                 if (!talloc_reference(context->sasl, conn->gensec)) {
341                                         status = NT_STATUS_NO_MEMORY;
342                                 }
343                         }
344                 } else {
345                         switch (call->conn->require_strong_auth) {
346                         case LDAP_SERVER_REQUIRE_STRONG_AUTH_NO:
347                                 break;
348                         case LDAP_SERVER_REQUIRE_STRONG_AUTH_ALLOW_SASL_OVER_TLS:
349                                 if (call->conn->sockets.active == call->conn->sockets.tls) {
350                                         break;
351                                 }
352                                 status = NT_STATUS_NETWORK_ACCESS_DENIED;
353                                 result = LDAP_STRONG_AUTH_REQUIRED;
354                                 errstr = talloc_asprintf(reply,
355                                                 "SASL:[%s]: not allowed if TLS is used.",
356                                                  req->creds.SASL.mechanism);
357                                 break;
358                         case LDAP_SERVER_REQUIRE_STRONG_AUTH_YES:
359                                 status = NT_STATUS_NETWORK_ACCESS_DENIED;
360                                 result = LDAP_STRONG_AUTH_REQUIRED;
361                                 errstr = talloc_asprintf(reply,
362                                                  "SASL:[%s]: Sign or Seal are required.",
363                                                  req->creds.SASL.mechanism);
364                                 break;
365                         }
366                 }
367
368                 if (result != LDAP_SUCCESS) {
369                 } else if (!NT_STATUS_IS_OK(status)) {
370                         result = LDAP_OPERATIONS_ERROR;
371                         errstr = talloc_asprintf(reply, 
372                                                  "SASL:[%s]: Failed to setup SASL socket: %s", 
373                                                  req->creds.SASL.mechanism, nt_errstr(status));
374                 } else {
375                         struct auth_session_info *old_session_info=NULL;
376
377                         old_session_info = conn->session_info;
378                         conn->session_info = NULL;
379                         status = gensec_session_info(conn->gensec, conn, &conn->session_info);
380                         if (!NT_STATUS_IS_OK(status)) {
381                                 conn->session_info = old_session_info;
382                                 result = LDAP_OPERATIONS_ERROR;
383                                 errstr = talloc_asprintf(reply, 
384                                                          "SASL:[%s]: Failed to get session info: %s", 
385                                                          req->creds.SASL.mechanism, nt_errstr(status));
386                         } else {
387                                 talloc_unlink(conn, old_session_info);
388                                 
389                                 /* don't leak the old LDB */
390                                 talloc_unlink(conn, conn->ldb);
391
392                                 call->conn->authz_logged = true;
393
394                                 status = ldapsrv_backend_Init(conn);            
395                                 
396                                 if (!NT_STATUS_IS_OK(status)) {
397                                         result = LDAP_OPERATIONS_ERROR;
398                                         errstr = talloc_asprintf(reply, 
399                                                                  "SASL:[%s]: Failed to advise samdb of new credentials: %s", 
400                                                                  req->creds.SASL.mechanism, 
401                                                                  nt_errstr(status));
402                                 }
403                         }
404                 }
405
406                 if (NT_STATUS_IS_OK(status) && context) {
407                         call->postprocess_send = ldapsrv_sasl_postprocess_send;
408                         call->postprocess_recv = ldapsrv_sasl_postprocess_recv;
409                         call->postprocess_private = context;
410                 }
411                 talloc_unlink(conn, conn->gensec);
412                 conn->gensec = NULL;
413         } else {
414                 status = nt_status_squash(status);
415                 if (result == 0) {
416                         result = LDAP_INVALID_CREDENTIALS;
417                         errstr = ldapsrv_bind_error_msg(reply, HRES_SEC_E_LOGON_DENIED,
418                                                         0x0C0904DC, status);
419                 }
420                 talloc_unlink(conn, conn->gensec);
421                 conn->gensec = NULL;
422         }
423
424         resp->response.resultcode = result;
425         resp->response.dn = NULL;
426         resp->response.errormessage = errstr;
427         resp->response.referral = NULL;
428
429         ldapsrv_queue_reply(call, reply);
430         return NT_STATUS_OK;
431 }
432
433 NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
434 {
435         struct ldap_BindRequest *req = &call->request->r.BindRequest;
436         struct ldapsrv_reply *reply;
437         struct ldap_BindResponse *resp;
438
439         if (call->conn->pending_calls != NULL) {
440                 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
441                 if (!reply) {
442                         return NT_STATUS_NO_MEMORY;
443                 }
444
445                 resp = &reply->msg->r.BindResponse;
446                 resp->response.resultcode = LDAP_BUSY;
447                 resp->response.dn = NULL;
448                 resp->response.errormessage = talloc_asprintf(reply, "Pending requests on this LDAP session");
449                 resp->response.referral = NULL;
450                 resp->SASL.secblob = NULL;
451
452                 ldapsrv_queue_reply(call, reply);
453                 return NT_STATUS_OK;
454         }
455
456         /* 
457          * TODO: a simple bind should cancel an
458          *       inprogress SASL bind.
459          *       (see RFC 4513)
460          */
461         switch (req->mechanism) {
462                 case LDAP_AUTH_MECH_SIMPLE:
463                         return ldapsrv_BindSimple(call);
464                 case LDAP_AUTH_MECH_SASL:
465                         return ldapsrv_BindSASL(call);
466         }
467
468         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
469         if (!reply) {
470                 return NT_STATUS_NO_MEMORY;
471         }
472
473         resp = &reply->msg->r.BindResponse;
474         resp->response.resultcode = LDAP_AUTH_METHOD_NOT_SUPPORTED;
475         resp->response.dn = NULL;
476         resp->response.errormessage = talloc_asprintf(reply, "Bad AuthenticationChoice [%d]", req->mechanism);
477         resp->response.referral = NULL;
478         resp->SASL.secblob = NULL;
479
480         ldapsrv_queue_reply(call, reply);
481         return NT_STATUS_OK;
482 }
483
484 NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
485 {
486         DEBUG(10, ("UnbindRequest\n"));
487         return NT_STATUS_OK;
488 }