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 "samba/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"
32 #include "lib/util/time_basic.h"
34 static char *ldapsrv_bind_error_msg(TALLOC_CTX *mem_ctx,
42 status = nt_status_squash(status);
43 werr = ntstatus_to_werror(status);
46 * There are 4 lower case hex digits following 'v' at the end,
47 * but different Windows Versions return different values:
49 * Windows 2008R2 uses 'v1db1'
50 * Windows 2012R2 uses 'v2580'
52 * We just match Windows 2008R2 as that's what was referenced
53 * in https://bugzilla.samba.org/show_bug.cgi?id=9048
55 msg = talloc_asprintf(mem_ctx, "%08X: LdapErr: DSID-%08X, comment: "
56 "AcceptSecurityContext error, data %x, v1db1",
57 (unsigned)HRES_ERROR_V(hresult),
59 (unsigned)W_ERROR_V(werr));
64 struct ldapsrv_bind_wait_context {
65 struct ldapsrv_reply *reply;
66 struct tevent_req *req;
71 struct ldapsrv_bind_wait_state {
75 static struct tevent_req *ldapsrv_bind_wait_send(TALLOC_CTX *mem_ctx,
76 struct tevent_context *ev,
79 struct ldapsrv_bind_wait_context *bind_wait =
80 talloc_get_type_abort(private_data,
81 struct ldapsrv_bind_wait_context);
82 struct tevent_req *req;
83 struct ldapsrv_bind_wait_state *state;
85 req = tevent_req_create(mem_ctx, &state,
86 struct ldapsrv_bind_wait_state);
92 tevent_req_defer_callback(req, ev);
94 if (!bind_wait->done) {
98 if (tevent_req_nterror(req, bind_wait->status)) {
99 return tevent_req_post(req, ev);
102 tevent_req_done(req);
103 return tevent_req_post(req, ev);
106 static NTSTATUS ldapsrv_bind_wait_recv(struct tevent_req *req)
108 return tevent_req_simple_recv_ntstatus(req);
111 static NTSTATUS ldapsrv_bind_wait_setup(struct ldapsrv_call *call,
112 struct ldapsrv_reply *reply)
114 struct ldapsrv_bind_wait_context *bind_wait = NULL;
116 if (call->wait_private != NULL) {
117 return NT_STATUS_INTERNAL_ERROR;
120 bind_wait = talloc_zero(call, struct ldapsrv_bind_wait_context);
121 if (bind_wait == NULL) {
122 return NT_STATUS_NO_MEMORY;
124 bind_wait->reply = reply;
126 call->wait_private = bind_wait;
127 call->wait_send = ldapsrv_bind_wait_send;
128 call->wait_recv = ldapsrv_bind_wait_recv;
132 static void ldapsrv_bind_wait_finished(struct ldapsrv_call *call,
135 struct ldapsrv_bind_wait_context *bind_wait =
136 talloc_get_type_abort(call->wait_private,
137 struct ldapsrv_bind_wait_context);
139 bind_wait->done = true;
140 bind_wait->status = status;
142 if (bind_wait->req == NULL) {
146 if (tevent_req_nterror(bind_wait->req, status)) {
150 tevent_req_done(bind_wait->req);
153 static void ldapsrv_BindSimple_done(struct tevent_req *subreq);
155 static NTSTATUS ldapsrv_BindSimple(struct ldapsrv_call *call)
157 struct ldap_BindRequest *req = &call->request->r.BindRequest;
158 struct ldapsrv_reply *reply = NULL;
159 struct ldap_BindResponse *resp = NULL;
161 const char *errstr = NULL;
163 bool using_tls = call->conn->sockets.active == call->conn->sockets.tls;
164 struct tevent_req *subreq = NULL;
166 DEBUG(10, ("BindSimple dn: %s\n",req->dn));
168 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
170 return NT_STATUS_NO_MEMORY;
173 if (req->dn != NULL &&
174 strlen(req->dn) != 0 &&
175 call->conn->require_strong_auth > LDAP_SERVER_REQUIRE_STRONG_AUTH_NO &&
178 status = NT_STATUS_NETWORK_ACCESS_DENIED;
179 result = LDAP_STRONG_AUTH_REQUIRED;
180 errstr = talloc_asprintf(reply,
181 "BindSimple: Transport encryption required.");
185 subreq = authenticate_ldap_simple_bind_send(call,
186 call->conn->connection->event.ctx,
187 call->conn->connection->msg_ctx,
189 call->conn->connection->remote_address,
190 call->conn->connection->local_address,
193 req->creds.password);
194 if (subreq == NULL) {
195 return NT_STATUS_NO_MEMORY;
197 tevent_req_set_callback(subreq, ldapsrv_BindSimple_done, call);
199 status = ldapsrv_bind_wait_setup(call, reply);
200 if (!NT_STATUS_IS_OK(status)) {
206 * The rest will be async.
211 resp = &reply->msg->r.BindResponse;
212 resp->response.resultcode = result;
213 resp->response.errormessage = errstr;
214 resp->response.dn = NULL;
215 resp->response.referral = NULL;
216 resp->SASL.secblob = NULL;
218 ldapsrv_queue_reply(call, reply);
222 static void ldapsrv_BindSimple_done(struct tevent_req *subreq)
224 struct ldapsrv_call *call =
225 tevent_req_callback_data(subreq,
226 struct ldapsrv_call);
227 struct ldapsrv_bind_wait_context *bind_wait =
228 talloc_get_type_abort(call->wait_private,
229 struct ldapsrv_bind_wait_context);
230 struct ldapsrv_reply *reply = bind_wait->reply;
231 struct auth_session_info *session_info = NULL;
233 struct ldap_BindResponse *resp = NULL;
235 const char *errstr = NULL;
237 status = authenticate_ldap_simple_bind_recv(subreq,
240 if (NT_STATUS_IS_OK(status)) {
241 char *ldb_errstring = NULL;
242 result = LDAP_SUCCESS;
245 talloc_unlink(call->conn, call->conn->session_info);
246 call->conn->session_info = talloc_steal(call->conn, session_info);
248 call->conn->authz_logged = true;
250 /* don't leak the old LDB */
251 talloc_unlink(call->conn, call->conn->ldb);
253 result = ldapsrv_backend_Init(call->conn, &ldb_errstring);
255 if (result != LDB_SUCCESS) {
256 /* Only put the detailed error in DEBUG() */
257 DBG_ERR("ldapsrv_backend_Init failed: %s: %s",
258 ldb_errstring, ldb_strerror(result));
259 errstr = talloc_strdup(reply,
260 "Simple Bind: Failed to advise "
261 "ldb new credentials");
262 result = LDB_ERR_OPERATIONS_ERROR;
265 status = nt_status_squash(status);
267 result = LDAP_INVALID_CREDENTIALS;
268 errstr = ldapsrv_bind_error_msg(reply, HRES_SEC_E_INVALID_TOKEN,
272 resp = &reply->msg->r.BindResponse;
273 resp->response.resultcode = result;
274 resp->response.errormessage = errstr;
275 resp->response.dn = NULL;
276 resp->response.referral = NULL;
277 resp->SASL.secblob = NULL;
279 ldapsrv_queue_reply(call, reply);
280 ldapsrv_bind_wait_finished(call, NT_STATUS_OK);
283 struct ldapsrv_sasl_postprocess_context {
284 struct ldapsrv_connection *conn;
285 struct tstream_context *sasl;
288 struct ldapsrv_sasl_postprocess_state {
292 static struct tevent_req *ldapsrv_sasl_postprocess_send(TALLOC_CTX *mem_ctx,
293 struct tevent_context *ev,
296 struct ldapsrv_sasl_postprocess_context *context =
297 talloc_get_type_abort(private_data,
298 struct ldapsrv_sasl_postprocess_context);
299 struct tevent_req *req;
300 struct ldapsrv_sasl_postprocess_state *state;
302 req = tevent_req_create(mem_ctx, &state,
303 struct ldapsrv_sasl_postprocess_state);
308 TALLOC_FREE(context->conn->sockets.sasl);
309 context->conn->sockets.sasl = talloc_move(context->conn, &context->sasl);
310 context->conn->sockets.active = context->conn->sockets.sasl;
312 tevent_req_done(req);
313 return tevent_req_post(req, ev);
316 static NTSTATUS ldapsrv_sasl_postprocess_recv(struct tevent_req *req)
318 return tevent_req_simple_recv_ntstatus(req);
321 static NTSTATUS ldapsrv_setup_gensec(struct ldapsrv_connection *conn,
322 const char *sasl_mech,
323 struct gensec_security **_gensec_security)
327 struct gensec_security *gensec_security;
329 status = samba_server_gensec_start(conn,
330 conn->connection->event.ctx,
331 conn->connection->msg_ctx,
333 conn->server_credentials,
336 if (!NT_STATUS_IS_OK(status)) {
340 status = gensec_set_target_service_description(gensec_security,
342 if (!NT_STATUS_IS_OK(status)) {
346 status = gensec_set_remote_address(gensec_security,
347 conn->connection->remote_address);
348 if (!NT_STATUS_IS_OK(status)) {
352 status = gensec_set_local_address(gensec_security,
353 conn->connection->local_address);
354 if (!NT_STATUS_IS_OK(status)) {
358 gensec_want_feature(gensec_security, GENSEC_FEATURE_ASYNC_REPLIES);
359 gensec_want_feature(gensec_security, GENSEC_FEATURE_LDAP_STYLE);
361 if (conn->sockets.active == conn->sockets.tls) {
362 gensec_want_feature(gensec_security, GENSEC_FEATURE_LDAPS_TRANSPORT);
365 status = gensec_start_mech_by_sasl_name(gensec_security, sasl_mech);
367 if (!NT_STATUS_IS_OK(status)) {
371 *_gensec_security = gensec_security;
375 static void ldapsrv_BindSASL_done(struct tevent_req *subreq);
377 static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
379 struct ldap_BindRequest *req = &call->request->r.BindRequest;
380 struct ldapsrv_reply *reply;
381 struct ldap_BindResponse *resp;
382 struct ldapsrv_connection *conn;
384 const char *errstr=NULL;
385 NTSTATUS status = NT_STATUS_OK;
386 DATA_BLOB input = data_blob_null;
387 struct tevent_req *subreq = NULL;
389 DEBUG(10, ("BindSASL dn: %s\n",req->dn));
391 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
393 return NT_STATUS_NO_MEMORY;
395 resp = &reply->msg->r.BindResponse;
396 /* Windows 2000 mmc doesn't like secblob == NULL and reports a decoding error */
397 resp->SASL.secblob = talloc_zero(reply, DATA_BLOB);
398 if (resp->SASL.secblob == NULL) {
399 return NT_STATUS_NO_MEMORY;
405 * TODO: a SASL bind with a different mechanism
406 * should cancel an inprogress SASL bind.
411 status = ldapsrv_setup_gensec(conn, req->creds.SASL.mechanism,
413 if (!NT_STATUS_IS_OK(status)) {
414 DEBUG(1, ("Failed to start GENSEC server for [%s] code: %s\n",
415 ldb_binary_encode_string(call, req->creds.SASL.mechanism),
417 result = LDAP_OPERATIONS_ERROR;
418 errstr = talloc_asprintf(reply, "SASL: Failed to start authentication system: %s",
424 if (req->creds.SASL.secblob) {
425 input = *req->creds.SASL.secblob;
428 subreq = gensec_update_send(call, conn->connection->event.ctx,
429 conn->gensec, input);
430 if (subreq == NULL) {
431 return NT_STATUS_NO_MEMORY;
433 tevent_req_set_callback(subreq, ldapsrv_BindSASL_done, call);
435 status = ldapsrv_bind_wait_setup(call, reply);
436 if (!NT_STATUS_IS_OK(status)) {
442 * The rest will be async.
447 if (result != LDAP_SASL_BIND_IN_PROGRESS) {
449 * We should destroy the gensec context
450 * when we hit a fatal error.
452 * Note: conn->gensec is already cleared
453 * for the LDAP_SUCCESS case.
455 talloc_unlink(conn, conn->gensec);
459 resp->response.resultcode = result;
460 resp->response.dn = NULL;
461 resp->response.errormessage = errstr;
462 resp->response.referral = NULL;
464 ldapsrv_queue_reply(call, reply);
468 static void ldapsrv_BindSASL_done(struct tevent_req *subreq)
470 struct ldapsrv_call *call =
471 tevent_req_callback_data(subreq,
472 struct ldapsrv_call);
473 struct ldapsrv_bind_wait_context *bind_wait =
474 talloc_get_type_abort(call->wait_private,
475 struct ldapsrv_bind_wait_context);
476 struct ldap_BindRequest *req = &call->request->r.BindRequest;
477 struct ldapsrv_reply *reply = bind_wait->reply;
478 struct ldap_BindResponse *resp = &reply->msg->r.BindResponse;
479 struct ldapsrv_connection *conn = call->conn;
480 struct auth_session_info *session_info = NULL;
481 struct ldapsrv_sasl_postprocess_context *context = NULL;
484 const char *errstr = NULL;
485 char *ldb_errstring = NULL;
486 DATA_BLOB output = data_blob_null;
487 NTTIME expire_time_nt;
489 status = gensec_update_recv(subreq, call, &output);
492 if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
493 *resp->SASL.secblob = output;
494 result = LDAP_SASL_BIND_IN_PROGRESS;
499 if (!NT_STATUS_IS_OK(status)) {
500 status = nt_status_squash(status);
501 result = LDAP_INVALID_CREDENTIALS;
502 errstr = ldapsrv_bind_error_msg(reply, HRES_SEC_E_LOGON_DENIED,
507 if (gensec_have_feature(conn->gensec, GENSEC_FEATURE_SIGN) ||
508 gensec_have_feature(conn->gensec, GENSEC_FEATURE_SEAL)) {
510 context = talloc_zero(call, struct ldapsrv_sasl_postprocess_context);
511 if (context == NULL) {
512 ldapsrv_bind_wait_finished(call, NT_STATUS_NO_MEMORY);
517 if (context && conn->sockets.tls) {
518 TALLOC_FREE(context);
519 status = NT_STATUS_NOT_SUPPORTED;
520 result = LDAP_UNWILLING_TO_PERFORM;
521 errstr = talloc_asprintf(reply,
522 "SASL:[%s]: Sign or Seal are not allowed if TLS is used",
523 req->creds.SASL.mechanism);
527 if (context && conn->sockets.sasl) {
528 TALLOC_FREE(context);
529 status = NT_STATUS_NOT_SUPPORTED;
530 result = LDAP_UNWILLING_TO_PERFORM;
531 errstr = talloc_asprintf(reply,
532 "SASL:[%s]: Sign or Seal are not allowed if SASL encryption has already been set up",
533 req->creds.SASL.mechanism);
537 if (context == NULL) {
538 switch (call->conn->require_strong_auth) {
539 case LDAP_SERVER_REQUIRE_STRONG_AUTH_NO:
541 case LDAP_SERVER_REQUIRE_STRONG_AUTH_ALLOW_SASL_OVER_TLS:
542 if (call->conn->sockets.active == call->conn->sockets.tls) {
545 status = NT_STATUS_NETWORK_ACCESS_DENIED;
546 result = LDAP_STRONG_AUTH_REQUIRED;
547 errstr = talloc_asprintf(reply,
548 "SASL:[%s]: not allowed if TLS is used.",
549 req->creds.SASL.mechanism);
552 case LDAP_SERVER_REQUIRE_STRONG_AUTH_YES:
553 status = NT_STATUS_NETWORK_ACCESS_DENIED;
554 result = LDAP_STRONG_AUTH_REQUIRED;
555 errstr = talloc_asprintf(reply,
556 "SASL:[%s]: Sign or Seal are required.",
557 req->creds.SASL.mechanism);
562 if (context != NULL) {
563 context->conn = conn;
564 status = gensec_create_tstream(context,
565 context->conn->gensec,
566 context->conn->sockets.raw,
568 if (!NT_STATUS_IS_OK(status)) {
569 result = LDAP_OPERATIONS_ERROR;
570 errstr = talloc_asprintf(reply,
571 "SASL:[%s]: Failed to setup SASL socket: %s",
572 req->creds.SASL.mechanism, nt_errstr(status));
577 status = gensec_session_info(conn->gensec, call, &session_info);
578 if (!NT_STATUS_IS_OK(status)) {
579 result = LDAP_OPERATIONS_ERROR;
580 errstr = talloc_asprintf(reply,
581 "SASL:[%s]: Failed to get session info: %s",
582 req->creds.SASL.mechanism, nt_errstr(status));
586 talloc_unlink(conn, conn->session_info);
587 conn->session_info = talloc_steal(conn, session_info);
589 /* don't leak the old LDB */
590 talloc_unlink(conn, conn->ldb);
592 call->conn->authz_logged = true;
594 result = ldapsrv_backend_Init(call->conn, &ldb_errstring);
596 if (result != LDB_SUCCESS) {
597 /* Only put the detailed error in DEBUG() */
598 DBG_ERR("ldapsrv_backend_Init failed: %s: %s",
599 ldb_errstring, ldb_strerror(result));
600 errstr = talloc_strdup(reply,
601 "SASL Bind: Failed to advise "
602 "ldb new credentials");
603 result = LDB_ERR_OPERATIONS_ERROR;
607 expire_time_nt = gensec_expire_time(conn->gensec);
608 if (expire_time_nt != GENSEC_EXPIRE_TIME_INFINITY) {
609 struct timeval_buf buf;
611 nttime_to_timeval(&conn->limits.expire_time, expire_time_nt);
613 DBG_DEBUG("Setting connection expire_time to %s\n",
614 timeval_str_buf(&conn->limits.expire_time,
620 if (context != NULL) {
621 const void *ptr = NULL;
623 ptr = talloc_reparent(conn, context->sasl, conn->gensec);
625 ldapsrv_bind_wait_finished(call, NT_STATUS_NO_MEMORY);
629 call->postprocess_send = ldapsrv_sasl_postprocess_send;
630 call->postprocess_recv = ldapsrv_sasl_postprocess_recv;
631 call->postprocess_private = context;
633 talloc_unlink(conn, conn->gensec);
637 *resp->SASL.secblob = output;
638 result = LDAP_SUCCESS;
642 if (result != LDAP_SASL_BIND_IN_PROGRESS) {
644 * We should destroy the gensec context
645 * when we hit a fatal error.
647 * Note: conn->gensec is already cleared
648 * for the LDAP_SUCCESS case.
650 talloc_unlink(conn, conn->gensec);
654 resp->response.resultcode = result;
655 resp->response.dn = NULL;
656 resp->response.errormessage = errstr;
657 resp->response.referral = NULL;
659 ldapsrv_queue_reply(call, reply);
660 ldapsrv_bind_wait_finished(call, NT_STATUS_OK);
663 NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
665 struct ldap_BindRequest *req = &call->request->r.BindRequest;
666 struct ldapsrv_reply *reply;
667 struct ldap_BindResponse *resp;
669 if (call->conn->pending_calls != NULL) {
670 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
672 return NT_STATUS_NO_MEMORY;
675 resp = &reply->msg->r.BindResponse;
676 resp->response.resultcode = LDAP_BUSY;
677 resp->response.dn = NULL;
678 resp->response.errormessage = talloc_asprintf(reply, "Pending requests on this LDAP session");
679 resp->response.referral = NULL;
680 resp->SASL.secblob = NULL;
682 ldapsrv_queue_reply(call, reply);
687 * TODO: a simple bind should cancel an
688 * inprogress SASL bind.
691 switch (req->mechanism) {
692 case LDAP_AUTH_MECH_SIMPLE:
693 return ldapsrv_BindSimple(call);
694 case LDAP_AUTH_MECH_SASL:
695 return ldapsrv_BindSASL(call);
698 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
700 return NT_STATUS_NO_MEMORY;
703 resp = &reply->msg->r.BindResponse;
704 resp->response.resultcode = LDAP_AUTH_METHOD_NOT_SUPPORTED;
705 resp->response.dn = NULL;
706 resp->response.errormessage = talloc_asprintf(reply, "Bad AuthenticationChoice [%d]", req->mechanism);
707 resp->response.referral = NULL;
708 resp->SASL.secblob = NULL;
710 ldapsrv_queue_reply(call, reply);
714 struct ldapsrv_unbind_wait_context {
718 struct ldapsrv_unbind_wait_state {
722 static struct tevent_req *ldapsrv_unbind_wait_send(TALLOC_CTX *mem_ctx,
723 struct tevent_context *ev,
726 struct ldapsrv_unbind_wait_context *unbind_wait =
727 talloc_get_type_abort(private_data,
728 struct ldapsrv_unbind_wait_context);
729 struct tevent_req *req;
730 struct ldapsrv_unbind_wait_state *state;
732 req = tevent_req_create(mem_ctx, &state,
733 struct ldapsrv_unbind_wait_state);
740 tevent_req_nterror(req, NT_STATUS_LOCAL_DISCONNECT);
741 return tevent_req_post(req, ev);
744 static NTSTATUS ldapsrv_unbind_wait_recv(struct tevent_req *req)
746 return tevent_req_simple_recv_ntstatus(req);
749 static NTSTATUS ldapsrv_unbind_wait_setup(struct ldapsrv_call *call)
751 struct ldapsrv_unbind_wait_context *unbind_wait = NULL;
753 if (call->wait_private != NULL) {
754 return NT_STATUS_INTERNAL_ERROR;
757 unbind_wait = talloc_zero(call, struct ldapsrv_unbind_wait_context);
758 if (unbind_wait == NULL) {
759 return NT_STATUS_NO_MEMORY;
762 call->wait_private = unbind_wait;
763 call->wait_send = ldapsrv_unbind_wait_send;
764 call->wait_recv = ldapsrv_unbind_wait_recv;
768 NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
770 struct ldapsrv_call *c = NULL;
771 struct ldapsrv_call *n = NULL;
773 DEBUG(10, ("UnbindRequest\n"));
775 for (c = call->conn->pending_calls; c != NULL; c = n) {
778 DLIST_REMOVE(call->conn->pending_calls, c);
782 return ldapsrv_unbind_wait_setup(call);