2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2004
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.
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.
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/>.
21 #include "ldap_server/ldap_server.h"
22 #include "auth/auth.h"
23 #include "smbd/service.h"
25 #include <ldb_errors.h>
26 #include "../lib/util/dlinklist.h"
27 #include "dsdb/samdb/samdb.h"
28 #include "auth/gensec/gensec.h"
29 #include "auth/gensec/gensec_tstream.h"
30 #include "param/param.h"
31 #include "../lib/util/tevent_ntstatus.h"
33 static char *ldapsrv_bind_error_msg(TALLOC_CTX *mem_ctx,
41 status = nt_status_squash(status);
42 werr = ntstatus_to_werror(status);
45 * There are 4 lower case hex digits following 'v' at the end,
46 * but different Windows Versions return different values:
48 * Windows 2008R2 uses 'v1db1'
49 * Windows 2012R2 uses 'v2580'
51 * We just match Windows 2008R2 as that's what was referenced
52 * in https://bugzilla.samba.org/show_bug.cgi?id=9048
54 msg = talloc_asprintf(mem_ctx, "%08X: LdapErr: DSID-%08X, comment: "
55 "AcceptSecurityContext error, data %x, v1db1",
56 (unsigned)HRES_ERROR_V(hresult),
58 (unsigned)W_ERROR_V(werr));
63 struct ldapsrv_bind_wait_context {
64 struct ldapsrv_reply *reply;
65 struct tevent_req *req;
70 struct ldapsrv_bind_wait_state {
74 static struct tevent_req *ldapsrv_bind_wait_send(TALLOC_CTX *mem_ctx,
75 struct tevent_context *ev,
78 struct ldapsrv_bind_wait_context *bind_wait =
79 talloc_get_type_abort(private_data,
80 struct ldapsrv_bind_wait_context);
81 struct tevent_req *req;
82 struct ldapsrv_bind_wait_state *state;
84 req = tevent_req_create(mem_ctx, &state,
85 struct ldapsrv_bind_wait_state);
91 tevent_req_defer_callback(req, ev);
93 if (!bind_wait->done) {
97 if (tevent_req_nterror(req, bind_wait->status)) {
98 return tevent_req_post(req, ev);
101 tevent_req_done(req);
102 return tevent_req_post(req, ev);
105 static NTSTATUS ldapsrv_bind_wait_recv(struct tevent_req *req)
107 return tevent_req_simple_recv_ntstatus(req);
110 static NTSTATUS ldapsrv_bind_wait_setup(struct ldapsrv_call *call,
111 struct ldapsrv_reply *reply)
113 struct ldapsrv_bind_wait_context *bind_wait = NULL;
115 if (call->wait_private != NULL) {
116 return NT_STATUS_INTERNAL_ERROR;
119 bind_wait = talloc_zero(call, struct ldapsrv_bind_wait_context);
120 if (bind_wait == NULL) {
121 return NT_STATUS_NO_MEMORY;
123 bind_wait->reply = reply;
125 call->wait_private = bind_wait;
126 call->wait_send = ldapsrv_bind_wait_send;
127 call->wait_recv = ldapsrv_bind_wait_recv;
131 static void ldapsrv_bind_wait_finished(struct ldapsrv_call *call,
134 struct ldapsrv_bind_wait_context *bind_wait =
135 talloc_get_type_abort(call->wait_private,
136 struct ldapsrv_bind_wait_context);
138 bind_wait->done = true;
139 bind_wait->status = status;
141 if (bind_wait->req == NULL) {
145 if (tevent_req_nterror(bind_wait->req, status)) {
149 tevent_req_done(bind_wait->req);
152 static void ldapsrv_BindSimple_done(struct tevent_req *subreq);
154 static NTSTATUS ldapsrv_BindSimple(struct ldapsrv_call *call)
156 struct ldap_BindRequest *req = &call->request->r.BindRequest;
157 struct ldapsrv_reply *reply = NULL;
158 struct ldap_BindResponse *resp = NULL;
160 const char *errstr = NULL;
162 bool using_tls = call->conn->sockets.active == call->conn->sockets.tls;
163 struct tevent_req *subreq = NULL;
165 DEBUG(10, ("BindSimple dn: %s\n",req->dn));
167 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
169 return NT_STATUS_NO_MEMORY;
172 if (req->dn != NULL &&
173 strlen(req->dn) != 0 &&
174 call->conn->require_strong_auth > LDAP_SERVER_REQUIRE_STRONG_AUTH_NO &&
177 status = NT_STATUS_NETWORK_ACCESS_DENIED;
178 result = LDAP_STRONG_AUTH_REQUIRED;
179 errstr = talloc_asprintf(reply,
180 "BindSimple: Transport encryption required.");
184 subreq = authenticate_ldap_simple_bind_send(call,
185 call->conn->connection->event.ctx,
186 call->conn->connection->msg_ctx,
188 call->conn->connection->remote_address,
189 call->conn->connection->local_address,
192 req->creds.password);
193 if (subreq == NULL) {
194 return NT_STATUS_NO_MEMORY;
196 tevent_req_set_callback(subreq, ldapsrv_BindSimple_done, call);
198 status = ldapsrv_bind_wait_setup(call, reply);
199 if (!NT_STATUS_IS_OK(status)) {
205 * The rest will be async.
210 resp = &reply->msg->r.BindResponse;
211 resp->response.resultcode = result;
212 resp->response.errormessage = errstr;
213 resp->response.dn = NULL;
214 resp->response.referral = NULL;
215 resp->SASL.secblob = NULL;
217 ldapsrv_queue_reply(call, reply);
221 static void ldapsrv_BindSimple_done(struct tevent_req *subreq)
223 struct ldapsrv_call *call =
224 tevent_req_callback_data(subreq,
225 struct ldapsrv_call);
226 struct ldapsrv_bind_wait_context *bind_wait =
227 talloc_get_type_abort(call->wait_private,
228 struct ldapsrv_bind_wait_context);
229 struct ldapsrv_reply *reply = bind_wait->reply;
230 struct auth_session_info *session_info = NULL;
232 struct ldap_BindResponse *resp = NULL;
234 const char *errstr = NULL;
236 status = authenticate_ldap_simple_bind_recv(subreq,
239 if (NT_STATUS_IS_OK(status)) {
240 result = LDAP_SUCCESS;
243 talloc_unlink(call->conn, call->conn->session_info);
244 call->conn->session_info = talloc_steal(call->conn, session_info);
246 call->conn->authz_logged = true;
248 /* don't leak the old LDB */
249 talloc_unlink(call->conn, call->conn->ldb);
251 status = ldapsrv_backend_Init(call->conn);
253 if (!NT_STATUS_IS_OK(status)) {
254 result = LDAP_OPERATIONS_ERROR;
255 errstr = talloc_asprintf(reply, "Simple Bind: Failed to advise ldb new credentials: %s", nt_errstr(status));
258 status = nt_status_squash(status);
260 result = LDAP_INVALID_CREDENTIALS;
261 errstr = ldapsrv_bind_error_msg(reply, HRES_SEC_E_INVALID_TOKEN,
265 resp = &reply->msg->r.BindResponse;
266 resp->response.resultcode = result;
267 resp->response.errormessage = errstr;
268 resp->response.dn = NULL;
269 resp->response.referral = NULL;
270 resp->SASL.secblob = NULL;
272 ldapsrv_queue_reply(call, reply);
273 ldapsrv_bind_wait_finished(call, NT_STATUS_OK);
276 struct ldapsrv_sasl_postprocess_context {
277 struct ldapsrv_connection *conn;
278 struct tstream_context *sasl;
281 struct ldapsrv_sasl_postprocess_state {
285 static struct tevent_req *ldapsrv_sasl_postprocess_send(TALLOC_CTX *mem_ctx,
286 struct tevent_context *ev,
289 struct ldapsrv_sasl_postprocess_context *context =
290 talloc_get_type_abort(private_data,
291 struct ldapsrv_sasl_postprocess_context);
292 struct tevent_req *req;
293 struct ldapsrv_sasl_postprocess_state *state;
295 req = tevent_req_create(mem_ctx, &state,
296 struct ldapsrv_sasl_postprocess_state);
301 TALLOC_FREE(context->conn->sockets.sasl);
302 context->conn->sockets.sasl = talloc_move(context->conn, &context->sasl);
303 context->conn->sockets.active = context->conn->sockets.sasl;
305 tevent_req_done(req);
306 return tevent_req_post(req, ev);
309 static NTSTATUS ldapsrv_sasl_postprocess_recv(struct tevent_req *req)
311 return tevent_req_simple_recv_ntstatus(req);
314 static NTSTATUS ldapsrv_setup_gensec(struct ldapsrv_connection *conn,
315 const char *sasl_mech,
316 struct gensec_security **_gensec_security)
320 struct gensec_security *gensec_security;
322 status = samba_server_gensec_start(conn,
323 conn->connection->event.ctx,
324 conn->connection->msg_ctx,
326 conn->server_credentials,
329 if (!NT_STATUS_IS_OK(status)) {
333 status = gensec_set_target_service_description(gensec_security,
335 if (!NT_STATUS_IS_OK(status)) {
339 status = gensec_set_remote_address(gensec_security,
340 conn->connection->remote_address);
341 if (!NT_STATUS_IS_OK(status)) {
345 status = gensec_set_local_address(gensec_security,
346 conn->connection->local_address);
347 if (!NT_STATUS_IS_OK(status)) {
351 gensec_want_feature(gensec_security, GENSEC_FEATURE_ASYNC_REPLIES);
352 gensec_want_feature(gensec_security, GENSEC_FEATURE_LDAP_STYLE);
354 if (conn->sockets.active == conn->sockets.tls) {
355 gensec_want_feature(gensec_security, GENSEC_FEATURE_LDAPS_TRANSPORT);
358 status = gensec_start_mech_by_sasl_name(gensec_security, sasl_mech);
360 if (!NT_STATUS_IS_OK(status)) {
364 *_gensec_security = gensec_security;
368 static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
370 struct ldap_BindRequest *req = &call->request->r.BindRequest;
371 struct ldapsrv_reply *reply;
372 struct ldap_BindResponse *resp;
373 struct ldapsrv_connection *conn;
374 struct ldapsrv_sasl_postprocess_context *context = NULL;
376 const char *errstr=NULL;
377 NTSTATUS status = NT_STATUS_OK;
378 DATA_BLOB input = data_blob_null;
379 DATA_BLOB output = data_blob_null;
381 DEBUG(10, ("BindSASL dn: %s\n",req->dn));
383 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
385 return NT_STATUS_NO_MEMORY;
387 resp = &reply->msg->r.BindResponse;
388 /* Windows 2000 mmc doesn't like secblob == NULL and reports a decoding error */
389 resp->SASL.secblob = talloc_zero(reply, DATA_BLOB);
390 if (resp->SASL.secblob == NULL) {
391 return NT_STATUS_NO_MEMORY;
397 * TODO: a SASL bind with a different mechanism
398 * should cancel an inprogress SASL bind.
403 status = ldapsrv_setup_gensec(conn, req->creds.SASL.mechanism,
405 if (!NT_STATUS_IS_OK(status)) {
406 DEBUG(1, ("Failed to start GENSEC server for [%s] code: %s\n",
407 ldb_binary_encode_string(call, req->creds.SASL.mechanism),
409 result = LDAP_OPERATIONS_ERROR;
410 errstr = talloc_asprintf(reply, "SASL: Failed to start authentication system: %s",
416 if (req->creds.SASL.secblob) {
417 input = *req->creds.SASL.secblob;
420 status = gensec_update_ev(conn->gensec, reply, conn->connection->event.ctx,
423 if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
424 *resp->SASL.secblob = output;
425 result = LDAP_SASL_BIND_IN_PROGRESS;
430 if (!NT_STATUS_IS_OK(status)) {
431 status = nt_status_squash(status);
432 result = LDAP_INVALID_CREDENTIALS;
433 errstr = ldapsrv_bind_error_msg(reply, HRES_SEC_E_LOGON_DENIED,
438 result = LDAP_SUCCESS;
441 if (gensec_have_feature(conn->gensec, GENSEC_FEATURE_SIGN) ||
442 gensec_have_feature(conn->gensec, GENSEC_FEATURE_SEAL)) {
444 context = talloc_zero(call, struct ldapsrv_sasl_postprocess_context);
445 if (context == NULL) {
446 return NT_STATUS_NO_MEMORY;
450 if (context && conn->sockets.tls) {
451 TALLOC_FREE(context);
452 status = NT_STATUS_NOT_SUPPORTED;
453 result = LDAP_UNWILLING_TO_PERFORM;
454 errstr = talloc_asprintf(reply,
455 "SASL:[%s]: Sign or Seal are not allowed if TLS is used",
456 req->creds.SASL.mechanism);
460 if (context && conn->sockets.sasl) {
461 TALLOC_FREE(context);
462 status = NT_STATUS_NOT_SUPPORTED;
463 result = LDAP_UNWILLING_TO_PERFORM;
464 errstr = talloc_asprintf(reply,
465 "SASL:[%s]: Sign or Seal are not allowed if SASL encryption has already been set up",
466 req->creds.SASL.mechanism);
470 if (context == NULL) {
471 switch (call->conn->require_strong_auth) {
472 case LDAP_SERVER_REQUIRE_STRONG_AUTH_NO:
474 case LDAP_SERVER_REQUIRE_STRONG_AUTH_ALLOW_SASL_OVER_TLS:
475 if (call->conn->sockets.active == call->conn->sockets.tls) {
478 status = NT_STATUS_NETWORK_ACCESS_DENIED;
479 result = LDAP_STRONG_AUTH_REQUIRED;
480 errstr = talloc_asprintf(reply,
481 "SASL:[%s]: not allowed if TLS is used.",
482 req->creds.SASL.mechanism);
485 case LDAP_SERVER_REQUIRE_STRONG_AUTH_YES:
486 status = NT_STATUS_NETWORK_ACCESS_DENIED;
487 result = LDAP_STRONG_AUTH_REQUIRED;
488 errstr = talloc_asprintf(reply,
489 "SASL:[%s]: Sign or Seal are required.",
490 req->creds.SASL.mechanism);
495 if (context != NULL) {
496 context->conn = conn;
497 status = gensec_create_tstream(context,
498 context->conn->gensec,
499 context->conn->sockets.raw,
501 if (NT_STATUS_IS_OK(status)) {
502 if (!talloc_reference(context->sasl, conn->gensec)) {
503 status = NT_STATUS_NO_MEMORY;
508 if (!NT_STATUS_IS_OK(status)) {
509 result = LDAP_OPERATIONS_ERROR;
510 errstr = talloc_asprintf(reply,
511 "SASL:[%s]: Failed to setup SASL socket: %s",
512 req->creds.SASL.mechanism, nt_errstr(status));
515 struct auth_session_info *old_session_info=NULL;
517 old_session_info = conn->session_info;
518 conn->session_info = NULL;
519 status = gensec_session_info(conn->gensec, conn, &conn->session_info);
520 if (!NT_STATUS_IS_OK(status)) {
521 conn->session_info = old_session_info;
522 result = LDAP_OPERATIONS_ERROR;
523 errstr = talloc_asprintf(reply,
524 "SASL:[%s]: Failed to get session info: %s",
525 req->creds.SASL.mechanism, nt_errstr(status));
528 talloc_unlink(conn, old_session_info);
530 /* don't leak the old LDB */
531 talloc_unlink(conn, conn->ldb);
533 call->conn->authz_logged = true;
535 status = ldapsrv_backend_Init(conn);
537 if (!NT_STATUS_IS_OK(status)) {
538 result = LDAP_OPERATIONS_ERROR;
539 errstr = talloc_asprintf(reply,
540 "SASL:[%s]: Failed to advise samdb of new credentials: %s",
541 req->creds.SASL.mechanism,
548 if (NT_STATUS_IS_OK(status) && context) {
549 call->postprocess_send = ldapsrv_sasl_postprocess_send;
550 call->postprocess_recv = ldapsrv_sasl_postprocess_recv;
551 call->postprocess_private = context;
553 talloc_unlink(conn, conn->gensec);
556 *resp->SASL.secblob = output;
559 if (result != LDAP_SASL_BIND_IN_PROGRESS) {
561 * We should destroy the gensec context
562 * when we hit a fatal error.
564 * Note: conn->gensec is already cleared
565 * for the LDAP_SUCCESS case.
567 talloc_unlink(conn, conn->gensec);
571 resp->response.resultcode = result;
572 resp->response.dn = NULL;
573 resp->response.errormessage = errstr;
574 resp->response.referral = NULL;
576 ldapsrv_queue_reply(call, reply);
580 NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
582 struct ldap_BindRequest *req = &call->request->r.BindRequest;
583 struct ldapsrv_reply *reply;
584 struct ldap_BindResponse *resp;
586 if (call->conn->pending_calls != NULL) {
587 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
589 return NT_STATUS_NO_MEMORY;
592 resp = &reply->msg->r.BindResponse;
593 resp->response.resultcode = LDAP_BUSY;
594 resp->response.dn = NULL;
595 resp->response.errormessage = talloc_asprintf(reply, "Pending requests on this LDAP session");
596 resp->response.referral = NULL;
597 resp->SASL.secblob = NULL;
599 ldapsrv_queue_reply(call, reply);
604 * TODO: a simple bind should cancel an
605 * inprogress SASL bind.
608 switch (req->mechanism) {
609 case LDAP_AUTH_MECH_SIMPLE:
610 return ldapsrv_BindSimple(call);
611 case LDAP_AUTH_MECH_SASL:
612 return ldapsrv_BindSASL(call);
615 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
617 return NT_STATUS_NO_MEMORY;
620 resp = &reply->msg->r.BindResponse;
621 resp->response.resultcode = LDAP_AUTH_METHOD_NOT_SUPPORTED;
622 resp->response.dn = NULL;
623 resp->response.errormessage = talloc_asprintf(reply, "Bad AuthenticationChoice [%d]", req->mechanism);
624 resp->response.referral = NULL;
625 resp->SASL.secblob = NULL;
627 ldapsrv_queue_reply(call, reply);
631 struct ldapsrv_unbind_wait_context {
635 struct ldapsrv_unbind_wait_state {
639 static struct tevent_req *ldapsrv_unbind_wait_send(TALLOC_CTX *mem_ctx,
640 struct tevent_context *ev,
643 struct ldapsrv_unbind_wait_context *unbind_wait =
644 talloc_get_type_abort(private_data,
645 struct ldapsrv_unbind_wait_context);
646 struct tevent_req *req;
647 struct ldapsrv_unbind_wait_state *state;
649 req = tevent_req_create(mem_ctx, &state,
650 struct ldapsrv_unbind_wait_state);
657 tevent_req_nterror(req, NT_STATUS_LOCAL_DISCONNECT);
658 return tevent_req_post(req, ev);
661 static NTSTATUS ldapsrv_unbind_wait_recv(struct tevent_req *req)
663 return tevent_req_simple_recv_ntstatus(req);
666 static NTSTATUS ldapsrv_unbind_wait_setup(struct ldapsrv_call *call)
668 struct ldapsrv_unbind_wait_context *unbind_wait = NULL;
670 if (call->wait_private != NULL) {
671 return NT_STATUS_INTERNAL_ERROR;
674 unbind_wait = talloc_zero(call, struct ldapsrv_unbind_wait_context);
675 if (unbind_wait == NULL) {
676 return NT_STATUS_NO_MEMORY;
679 call->wait_private = unbind_wait;
680 call->wait_send = ldapsrv_unbind_wait_send;
681 call->wait_recv = ldapsrv_unbind_wait_recv;
685 NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
687 struct ldapsrv_call *c = NULL;
688 struct ldapsrv_call *n = NULL;
690 DEBUG(10, ("UnbindRequest\n"));
692 for (c = call->conn->pending_calls; c != NULL; c = n) {
695 DLIST_REMOVE(call->conn->pending_calls, c);
699 return ldapsrv_unbind_wait_setup(call);