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 DEBUG(10, ("BindSimple dn: %s\n",req->dn));
78 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
80 return NT_STATUS_NO_MEMORY;
83 if (req->dn != NULL &&
84 strlen(req->dn) != 0 &&
85 call->conn->require_strong_auth > LDAP_SERVER_REQUIRE_STRONG_AUTH_NO &&
86 call->conn->sockets.active != call->conn->sockets.tls)
88 status = NT_STATUS_NETWORK_ACCESS_DENIED;
89 result = LDAP_STRONG_AUTH_REQUIRED;
90 errstr = talloc_asprintf(reply,
91 "BindSimple: Transport encryption required.");
95 status = authenticate_ldap_simple_bind(call,
96 call->conn->connection->event.ctx,
97 call->conn->connection->msg_ctx,
99 call->conn->connection->remote_address,
100 call->conn->connection->local_address,
105 if (NT_STATUS_IS_OK(status)) {
106 result = LDAP_SUCCESS;
109 talloc_unlink(call->conn, call->conn->session_info);
110 call->conn->session_info = talloc_steal(call->conn, session_info);
112 call->conn->authz_logged = true;
114 /* don't leak the old LDB */
115 talloc_unlink(call->conn, call->conn->ldb);
117 status = ldapsrv_backend_Init(call->conn);
119 if (!NT_STATUS_IS_OK(status)) {
120 result = LDAP_OPERATIONS_ERROR;
121 errstr = talloc_asprintf(reply, "Simple Bind: Failed to advise ldb new credentials: %s", nt_errstr(status));
124 status = nt_status_squash(status);
126 result = LDAP_INVALID_CREDENTIALS;
127 errstr = ldapsrv_bind_error_msg(reply, HRES_SEC_E_INVALID_TOKEN,
132 resp = &reply->msg->r.BindResponse;
133 resp->response.resultcode = result;
134 resp->response.errormessage = errstr;
135 resp->response.dn = NULL;
136 resp->response.referral = NULL;
137 resp->SASL.secblob = NULL;
139 ldapsrv_queue_reply(call, reply);
143 struct ldapsrv_sasl_postprocess_context {
144 struct ldapsrv_connection *conn;
145 struct tstream_context *sasl;
148 struct ldapsrv_sasl_postprocess_state {
152 static struct tevent_req *ldapsrv_sasl_postprocess_send(TALLOC_CTX *mem_ctx,
153 struct tevent_context *ev,
156 struct ldapsrv_sasl_postprocess_context *context =
157 talloc_get_type_abort(private_data,
158 struct ldapsrv_sasl_postprocess_context);
159 struct tevent_req *req;
160 struct ldapsrv_sasl_postprocess_state *state;
162 req = tevent_req_create(mem_ctx, &state,
163 struct ldapsrv_sasl_postprocess_state);
168 TALLOC_FREE(context->conn->sockets.sasl);
169 context->conn->sockets.sasl = talloc_move(context->conn, &context->sasl);
170 context->conn->sockets.active = context->conn->sockets.sasl;
172 tevent_req_done(req);
173 return tevent_req_post(req, ev);
176 static NTSTATUS ldapsrv_sasl_postprocess_recv(struct tevent_req *req)
178 return tevent_req_simple_recv_ntstatus(req);
181 static NTSTATUS ldapsrv_setup_gensec(struct ldapsrv_connection *conn,
182 const char *sasl_mech,
183 struct gensec_security **_gensec_security)
187 struct gensec_security *gensec_security;
189 status = samba_server_gensec_start(conn,
190 conn->connection->event.ctx,
191 conn->connection->msg_ctx,
193 conn->server_credentials,
196 if (!NT_STATUS_IS_OK(status)) {
200 status = gensec_set_target_service_description(gensec_security,
202 if (!NT_STATUS_IS_OK(status)) {
206 status = gensec_set_remote_address(gensec_security,
207 conn->connection->remote_address);
208 if (!NT_STATUS_IS_OK(status)) {
212 status = gensec_set_local_address(gensec_security,
213 conn->connection->local_address);
214 if (!NT_STATUS_IS_OK(status)) {
218 gensec_want_feature(gensec_security, GENSEC_FEATURE_ASYNC_REPLIES);
219 gensec_want_feature(gensec_security, GENSEC_FEATURE_LDAP_STYLE);
221 status = gensec_start_mech_by_sasl_name(gensec_security, sasl_mech);
223 if (!NT_STATUS_IS_OK(status)) {
227 *_gensec_security = gensec_security;
231 static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
233 struct ldap_BindRequest *req = &call->request->r.BindRequest;
234 struct ldapsrv_reply *reply;
235 struct ldap_BindResponse *resp;
236 struct ldapsrv_connection *conn;
238 const char *errstr=NULL;
239 NTSTATUS status = NT_STATUS_OK;
241 DEBUG(10, ("BindSASL dn: %s\n",req->dn));
243 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
245 return NT_STATUS_NO_MEMORY;
247 resp = &reply->msg->r.BindResponse;
252 * TODO: a SASL bind with a different mechanism
253 * should cancel an inprogress SASL bind.
258 status = ldapsrv_setup_gensec(conn, req->creds.SASL.mechanism,
260 if (!NT_STATUS_IS_OK(status)) {
261 DEBUG(1, ("Failed to start GENSEC server for [%s] code: %s\n",
262 ldb_binary_encode_string(call, req->creds.SASL.mechanism),
264 result = LDAP_OPERATIONS_ERROR;
265 errstr = talloc_asprintf(reply, "SASL: Failed to start authentication system: %s",
270 if (NT_STATUS_IS_OK(status)) {
271 DATA_BLOB input = data_blob(NULL, 0);
272 DATA_BLOB output = data_blob(NULL, 0);
274 if (req->creds.SASL.secblob) {
275 input = *req->creds.SASL.secblob;
278 status = gensec_update_ev(conn->gensec, reply, conn->connection->event.ctx,
281 /* Windows 2000 mmc doesn't like secblob == NULL and reports a decoding error */
282 resp->SASL.secblob = talloc(reply, DATA_BLOB);
283 NT_STATUS_HAVE_NO_MEMORY(resp->SASL.secblob);
284 *resp->SASL.secblob = output;
286 resp->SASL.secblob = NULL;
289 if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
290 result = LDAP_SASL_BIND_IN_PROGRESS;
292 } else if (NT_STATUS_IS_OK(status)) {
293 struct ldapsrv_sasl_postprocess_context *context = NULL;
295 result = LDAP_SUCCESS;
298 if (gensec_have_feature(conn->gensec, GENSEC_FEATURE_SIGN) ||
299 gensec_have_feature(conn->gensec, GENSEC_FEATURE_SEAL)) {
301 context = talloc(call, struct ldapsrv_sasl_postprocess_context);
304 status = NT_STATUS_NO_MEMORY;
308 if (context && conn->sockets.tls) {
309 TALLOC_FREE(context);
310 status = NT_STATUS_NOT_SUPPORTED;
311 result = LDAP_UNWILLING_TO_PERFORM;
312 errstr = talloc_asprintf(reply,
313 "SASL:[%s]: Sign or Seal are not allowed if TLS is used",
314 req->creds.SASL.mechanism);
317 if (context && conn->sockets.sasl) {
318 TALLOC_FREE(context);
319 status = NT_STATUS_NOT_SUPPORTED;
320 result = LDAP_UNWILLING_TO_PERFORM;
321 errstr = talloc_asprintf(reply,
322 "SASL:[%s]: Sign or Seal are not allowed if SASL encryption has already been set up",
323 req->creds.SASL.mechanism);
327 context->conn = conn;
328 status = gensec_create_tstream(context,
329 context->conn->gensec,
330 context->conn->sockets.raw,
332 if (NT_STATUS_IS_OK(status)) {
333 if (!talloc_reference(context->sasl, conn->gensec)) {
334 status = NT_STATUS_NO_MEMORY;
338 switch (call->conn->require_strong_auth) {
339 case LDAP_SERVER_REQUIRE_STRONG_AUTH_NO:
341 case LDAP_SERVER_REQUIRE_STRONG_AUTH_ALLOW_SASL_OVER_TLS:
342 if (call->conn->sockets.active == call->conn->sockets.tls) {
345 status = NT_STATUS_NETWORK_ACCESS_DENIED;
346 result = LDAP_STRONG_AUTH_REQUIRED;
347 errstr = talloc_asprintf(reply,
348 "SASL:[%s]: not allowed if TLS is used.",
349 req->creds.SASL.mechanism);
351 case LDAP_SERVER_REQUIRE_STRONG_AUTH_YES:
352 status = NT_STATUS_NETWORK_ACCESS_DENIED;
353 result = LDAP_STRONG_AUTH_REQUIRED;
354 errstr = talloc_asprintf(reply,
355 "SASL:[%s]: Sign or Seal are required.",
356 req->creds.SASL.mechanism);
361 if (result != LDAP_SUCCESS) {
362 } else if (!NT_STATUS_IS_OK(status)) {
363 result = LDAP_OPERATIONS_ERROR;
364 errstr = talloc_asprintf(reply,
365 "SASL:[%s]: Failed to setup SASL socket: %s",
366 req->creds.SASL.mechanism, nt_errstr(status));
368 struct auth_session_info *old_session_info=NULL;
370 old_session_info = conn->session_info;
371 conn->session_info = NULL;
372 status = gensec_session_info(conn->gensec, conn, &conn->session_info);
373 if (!NT_STATUS_IS_OK(status)) {
374 conn->session_info = old_session_info;
375 result = LDAP_OPERATIONS_ERROR;
376 errstr = talloc_asprintf(reply,
377 "SASL:[%s]: Failed to get session info: %s",
378 req->creds.SASL.mechanism, nt_errstr(status));
380 talloc_unlink(conn, old_session_info);
382 /* don't leak the old LDB */
383 talloc_unlink(conn, conn->ldb);
385 call->conn->authz_logged = true;
387 status = ldapsrv_backend_Init(conn);
389 if (!NT_STATUS_IS_OK(status)) {
390 result = LDAP_OPERATIONS_ERROR;
391 errstr = talloc_asprintf(reply,
392 "SASL:[%s]: Failed to advise samdb of new credentials: %s",
393 req->creds.SASL.mechanism,
399 if (NT_STATUS_IS_OK(status) && context) {
400 call->postprocess_send = ldapsrv_sasl_postprocess_send;
401 call->postprocess_recv = ldapsrv_sasl_postprocess_recv;
402 call->postprocess_private = context;
404 talloc_unlink(conn, conn->gensec);
407 status = nt_status_squash(status);
409 result = LDAP_INVALID_CREDENTIALS;
410 errstr = ldapsrv_bind_error_msg(reply, HRES_SEC_E_LOGON_DENIED,
413 talloc_unlink(conn, conn->gensec);
417 resp->response.resultcode = result;
418 resp->response.dn = NULL;
419 resp->response.errormessage = errstr;
420 resp->response.referral = NULL;
422 ldapsrv_queue_reply(call, reply);
426 NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
428 struct ldap_BindRequest *req = &call->request->r.BindRequest;
429 struct ldapsrv_reply *reply;
430 struct ldap_BindResponse *resp;
432 if (call->conn->pending_calls != NULL) {
433 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
435 return NT_STATUS_NO_MEMORY;
438 resp = &reply->msg->r.BindResponse;
439 resp->response.resultcode = LDAP_BUSY;
440 resp->response.dn = NULL;
441 resp->response.errormessage = talloc_asprintf(reply, "Pending requests on this LDAP session");
442 resp->response.referral = NULL;
443 resp->SASL.secblob = NULL;
445 ldapsrv_queue_reply(call, reply);
450 * TODO: a simple bind should cancel an
451 * inprogress SASL bind.
454 switch (req->mechanism) {
455 case LDAP_AUTH_MECH_SIMPLE:
456 return ldapsrv_BindSimple(call);
457 case LDAP_AUTH_MECH_SASL:
458 return ldapsrv_BindSASL(call);
461 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
463 return NT_STATUS_NO_MEMORY;
466 resp = &reply->msg->r.BindResponse;
467 resp->response.resultcode = LDAP_AUTH_METHOD_NOT_SUPPORTED;
468 resp->response.dn = NULL;
469 resp->response.errormessage = talloc_asprintf(reply, "Bad AuthenticationChoice [%d]", req->mechanism);
470 resp->response.referral = NULL;
471 resp->SASL.secblob = NULL;
473 ldapsrv_queue_reply(call, reply);
477 NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
479 DEBUG(10, ("UnbindRequest\n"));