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 "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"
32 static char *ldapsrv_bind_error_msg(TALLOC_CTX *mem_ctx,
40 status = nt_status_squash(status);
41 werr = ntstatus_to_werror(status);
44 * There are 4 lower case hex digits following 'v' at the end,
45 * but different Windows Versions return different values:
47 * Windows 2008R2 uses 'v1db1'
48 * Windows 2012R2 uses 'v2580'
50 * We just match Windows 2008R2 as that's what was referenced
51 * in https://bugzilla.samba.org/show_bug.cgi?id=9048
53 msg = talloc_asprintf(mem_ctx, "%08X: LdapErr: DSID-%08X, comment: "
54 "AcceptSecurityContext error, data %x, v1db1",
55 (unsigned)HRES_ERROR_V(hresult),
57 (unsigned)W_ERROR_V(werr));
63 static NTSTATUS ldapsrv_BindSimple(struct ldapsrv_call *call)
65 struct ldap_BindRequest *req = &call->request->r.BindRequest;
66 struct ldapsrv_reply *reply;
67 struct ldap_BindResponse *resp;
72 struct auth_session_info *session_info;
76 bool using_tls = call->conn->sockets.active == call->conn->sockets.tls;
78 DEBUG(10, ("BindSimple dn: %s\n",req->dn));
80 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
82 return NT_STATUS_NO_MEMORY;
85 if (req->dn != NULL &&
86 strlen(req->dn) != 0 &&
87 call->conn->require_strong_auth > LDAP_SERVER_REQUIRE_STRONG_AUTH_NO &&
90 status = NT_STATUS_NETWORK_ACCESS_DENIED;
91 result = LDAP_STRONG_AUTH_REQUIRED;
92 errstr = talloc_asprintf(reply,
93 "BindSimple: Transport encryption required.");
97 status = authenticate_ldap_simple_bind(call,
98 call->conn->connection->event.ctx,
99 call->conn->connection->msg_ctx,
101 call->conn->connection->remote_address,
102 call->conn->connection->local_address,
108 if (NT_STATUS_IS_OK(status)) {
109 result = LDAP_SUCCESS;
112 talloc_unlink(call->conn, call->conn->session_info);
113 call->conn->session_info = talloc_steal(call->conn, session_info);
115 call->conn->authz_logged = true;
117 /* don't leak the old LDB */
118 talloc_unlink(call->conn, call->conn->ldb);
120 status = ldapsrv_backend_Init(call->conn);
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));
127 status = nt_status_squash(status);
129 result = LDAP_INVALID_CREDENTIALS;
130 errstr = ldapsrv_bind_error_msg(reply, HRES_SEC_E_INVALID_TOKEN,
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;
142 ldapsrv_queue_reply(call, reply);
146 struct ldapsrv_sasl_postprocess_context {
147 struct ldapsrv_connection *conn;
148 struct tstream_context *sasl;
151 struct ldapsrv_sasl_postprocess_state {
155 static struct tevent_req *ldapsrv_sasl_postprocess_send(TALLOC_CTX *mem_ctx,
156 struct tevent_context *ev,
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;
165 req = tevent_req_create(mem_ctx, &state,
166 struct ldapsrv_sasl_postprocess_state);
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;
175 tevent_req_done(req);
176 return tevent_req_post(req, ev);
179 static NTSTATUS ldapsrv_sasl_postprocess_recv(struct tevent_req *req)
181 return tevent_req_simple_recv_ntstatus(req);
184 static NTSTATUS ldapsrv_setup_gensec(struct ldapsrv_connection *conn,
185 const char *sasl_mech,
186 struct gensec_security **_gensec_security)
190 struct gensec_security *gensec_security;
192 status = samba_server_gensec_start(conn,
193 conn->connection->event.ctx,
194 conn->connection->msg_ctx,
196 conn->server_credentials,
199 if (!NT_STATUS_IS_OK(status)) {
203 status = gensec_set_target_service_description(gensec_security,
205 if (!NT_STATUS_IS_OK(status)) {
209 status = gensec_set_remote_address(gensec_security,
210 conn->connection->remote_address);
211 if (!NT_STATUS_IS_OK(status)) {
215 status = gensec_set_local_address(gensec_security,
216 conn->connection->local_address);
217 if (!NT_STATUS_IS_OK(status)) {
221 gensec_want_feature(gensec_security, GENSEC_FEATURE_ASYNC_REPLIES);
222 gensec_want_feature(gensec_security, GENSEC_FEATURE_LDAP_STYLE);
224 if (conn->sockets.active == conn->sockets.tls) {
225 gensec_want_feature(gensec_security, GENSEC_FEATURE_LDAPS_TRANSPORT);
228 status = gensec_start_mech_by_sasl_name(gensec_security, sasl_mech);
230 if (!NT_STATUS_IS_OK(status)) {
234 *_gensec_security = gensec_security;
238 static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
240 struct ldap_BindRequest *req = &call->request->r.BindRequest;
241 struct ldapsrv_reply *reply;
242 struct ldap_BindResponse *resp;
243 struct ldapsrv_connection *conn;
245 const char *errstr=NULL;
246 NTSTATUS status = NT_STATUS_OK;
248 DEBUG(10, ("BindSASL dn: %s\n",req->dn));
250 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
252 return NT_STATUS_NO_MEMORY;
254 resp = &reply->msg->r.BindResponse;
259 * TODO: a SASL bind with a different mechanism
260 * should cancel an inprogress SASL bind.
265 status = ldapsrv_setup_gensec(conn, req->creds.SASL.mechanism,
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),
271 result = LDAP_OPERATIONS_ERROR;
272 errstr = talloc_asprintf(reply, "SASL: Failed to start authentication system: %s",
277 if (NT_STATUS_IS_OK(status)) {
278 DATA_BLOB input = data_blob(NULL, 0);
279 DATA_BLOB output = data_blob(NULL, 0);
281 if (req->creds.SASL.secblob) {
282 input = *req->creds.SASL.secblob;
285 status = gensec_update_ev(conn->gensec, reply, conn->connection->event.ctx,
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;
293 resp->SASL.secblob = NULL;
296 if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
297 result = LDAP_SASL_BIND_IN_PROGRESS;
299 } else if (NT_STATUS_IS_OK(status)) {
300 struct ldapsrv_sasl_postprocess_context *context = NULL;
302 result = LDAP_SUCCESS;
305 if (gensec_have_feature(conn->gensec, GENSEC_FEATURE_SIGN) ||
306 gensec_have_feature(conn->gensec, GENSEC_FEATURE_SEAL)) {
308 context = talloc(call, struct ldapsrv_sasl_postprocess_context);
311 status = NT_STATUS_NO_MEMORY;
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);
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);
334 context->conn = conn;
335 status = gensec_create_tstream(context,
336 context->conn->gensec,
337 context->conn->sockets.raw,
339 if (NT_STATUS_IS_OK(status)) {
340 if (!talloc_reference(context->sasl, conn->gensec)) {
341 status = NT_STATUS_NO_MEMORY;
345 switch (call->conn->require_strong_auth) {
346 case LDAP_SERVER_REQUIRE_STRONG_AUTH_NO:
348 case LDAP_SERVER_REQUIRE_STRONG_AUTH_ALLOW_SASL_OVER_TLS:
349 if (call->conn->sockets.active == call->conn->sockets.tls) {
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);
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);
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));
375 struct auth_session_info *old_session_info=NULL;
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));
387 talloc_unlink(conn, old_session_info);
389 /* don't leak the old LDB */
390 talloc_unlink(conn, conn->ldb);
392 call->conn->authz_logged = true;
394 status = ldapsrv_backend_Init(conn);
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,
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;
411 talloc_unlink(conn, conn->gensec);
414 status = nt_status_squash(status);
416 result = LDAP_INVALID_CREDENTIALS;
417 errstr = ldapsrv_bind_error_msg(reply, HRES_SEC_E_LOGON_DENIED,
420 talloc_unlink(conn, conn->gensec);
424 resp->response.resultcode = result;
425 resp->response.dn = NULL;
426 resp->response.errormessage = errstr;
427 resp->response.referral = NULL;
429 ldapsrv_queue_reply(call, reply);
433 NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
435 struct ldap_BindRequest *req = &call->request->r.BindRequest;
436 struct ldapsrv_reply *reply;
437 struct ldap_BindResponse *resp;
439 if (call->conn->pending_calls != NULL) {
440 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
442 return NT_STATUS_NO_MEMORY;
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;
452 ldapsrv_queue_reply(call, reply);
457 * TODO: a simple bind should cancel an
458 * inprogress SASL bind.
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);
468 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
470 return NT_STATUS_NO_MEMORY;
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;
480 ldapsrv_queue_reply(call, reply);
484 NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
486 DEBUG(10, ("UnbindRequest\n"));