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;
71 const char *nt4_domain, *nt4_account;
73 struct auth_session_info *session_info;
77 DEBUG(10, ("BindSimple dn: %s\n",req->dn));
79 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
81 return NT_STATUS_NO_MEMORY;
84 if (req->dn != NULL &&
85 strlen(req->dn) != 0 &&
86 call->conn->require_strong_auth > LDAP_SERVER_REQUIRE_STRONG_AUTH_NO &&
87 call->conn->sockets.active != call->conn->sockets.tls)
89 status = NT_STATUS_NETWORK_ACCESS_DENIED;
90 result = LDAP_STRONG_AUTH_REQUIRED;
91 errstr = talloc_asprintf(reply,
92 "BindSimple: Transport encryption required.");
96 status = crack_auto_name_to_nt4_name(call, call->conn->connection->event.ctx, call->conn->lp_ctx, req->dn, &nt4_domain, &nt4_account);
97 if (NT_STATUS_IS_OK(status)) {
98 status = authenticate_username_pw(call,
99 call->conn->connection->event.ctx,
100 call->conn->connection->msg_ctx,
102 nt4_domain, nt4_account,
104 MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT |
105 MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
109 if (NT_STATUS_IS_OK(status)) {
110 result = LDAP_SUCCESS;
113 talloc_unlink(call->conn, call->conn->session_info);
114 call->conn->session_info = talloc_steal(call->conn, session_info);
116 /* don't leak the old LDB */
117 talloc_unlink(call->conn, call->conn->ldb);
119 status = ldapsrv_backend_Init(call->conn);
121 if (!NT_STATUS_IS_OK(status)) {
122 result = LDAP_OPERATIONS_ERROR;
123 errstr = talloc_asprintf(reply, "Simple Bind: Failed to advise ldb new credentials: %s", nt_errstr(status));
126 status = nt_status_squash(status);
128 result = LDAP_INVALID_CREDENTIALS;
129 errstr = ldapsrv_bind_error_msg(reply, HRES_SEC_E_INVALID_TOKEN,
134 resp = &reply->msg->r.BindResponse;
135 resp->response.resultcode = result;
136 resp->response.errormessage = errstr;
137 resp->response.dn = NULL;
138 resp->response.referral = NULL;
139 resp->SASL.secblob = NULL;
141 ldapsrv_queue_reply(call, reply);
145 struct ldapsrv_sasl_postprocess_context {
146 struct ldapsrv_connection *conn;
147 struct tstream_context *sasl;
150 struct ldapsrv_sasl_postprocess_state {
154 static struct tevent_req *ldapsrv_sasl_postprocess_send(TALLOC_CTX *mem_ctx,
155 struct tevent_context *ev,
158 struct ldapsrv_sasl_postprocess_context *context =
159 talloc_get_type_abort(private_data,
160 struct ldapsrv_sasl_postprocess_context);
161 struct tevent_req *req;
162 struct ldapsrv_sasl_postprocess_state *state;
164 req = tevent_req_create(mem_ctx, &state,
165 struct ldapsrv_sasl_postprocess_state);
170 TALLOC_FREE(context->conn->sockets.sasl);
171 context->conn->sockets.sasl = talloc_move(context->conn, &context->sasl);
172 context->conn->sockets.active = context->conn->sockets.sasl;
174 tevent_req_done(req);
175 return tevent_req_post(req, ev);
178 static NTSTATUS ldapsrv_sasl_postprocess_recv(struct tevent_req *req)
180 return tevent_req_simple_recv_ntstatus(req);
183 static NTSTATUS ldapsrv_setup_gensec(struct ldapsrv_connection *conn,
184 const char *sasl_mech,
185 struct gensec_security **_gensec_security)
189 struct gensec_security *gensec_security;
191 status = samba_server_gensec_start(conn,
192 conn->connection->event.ctx,
193 conn->connection->msg_ctx,
195 conn->server_credentials,
198 if (!NT_STATUS_IS_OK(status)) {
202 status = gensec_set_remote_address(gensec_security,
203 conn->connection->remote_address);
204 if (!NT_STATUS_IS_OK(status)) {
208 status = gensec_set_local_address(gensec_security,
209 conn->connection->local_address);
210 if (!NT_STATUS_IS_OK(status)) {
214 gensec_want_feature(gensec_security, GENSEC_FEATURE_ASYNC_REPLIES);
215 gensec_want_feature(gensec_security, GENSEC_FEATURE_LDAP_STYLE);
217 status = gensec_start_mech_by_sasl_name(gensec_security, sasl_mech);
219 if (!NT_STATUS_IS_OK(status)) {
223 *_gensec_security = gensec_security;
227 static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
229 struct ldap_BindRequest *req = &call->request->r.BindRequest;
230 struct ldapsrv_reply *reply;
231 struct ldap_BindResponse *resp;
232 struct ldapsrv_connection *conn;
234 const char *errstr=NULL;
235 NTSTATUS status = NT_STATUS_OK;
237 DEBUG(10, ("BindSASL dn: %s\n",req->dn));
239 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
241 return NT_STATUS_NO_MEMORY;
243 resp = &reply->msg->r.BindResponse;
248 * TODO: a SASL bind with a different mechanism
249 * should cancel an inprogress SASL bind.
254 conn->session_info = NULL;
256 status = ldapsrv_setup_gensec(conn, req->creds.SASL.mechanism,
258 if (!NT_STATUS_IS_OK(status)) {
259 DEBUG(1, ("Failed to start GENSEC server for [%s] code: %s\n",
260 ldb_binary_encode_string(call, req->creds.SASL.mechanism),
262 result = LDAP_OPERATIONS_ERROR;
263 errstr = talloc_asprintf(reply, "SASL: Failed to start authentication system: %s",
268 if (NT_STATUS_IS_OK(status)) {
269 DATA_BLOB input = data_blob(NULL, 0);
270 DATA_BLOB output = data_blob(NULL, 0);
272 if (req->creds.SASL.secblob) {
273 input = *req->creds.SASL.secblob;
276 status = gensec_update_ev(conn->gensec, reply, conn->connection->event.ctx,
279 /* Windows 2000 mmc doesn't like secblob == NULL and reports a decoding error */
280 resp->SASL.secblob = talloc(reply, DATA_BLOB);
281 NT_STATUS_HAVE_NO_MEMORY(resp->SASL.secblob);
282 *resp->SASL.secblob = output;
284 resp->SASL.secblob = NULL;
287 if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
288 result = LDAP_SASL_BIND_IN_PROGRESS;
290 } else if (NT_STATUS_IS_OK(status)) {
291 struct ldapsrv_sasl_postprocess_context *context = NULL;
293 result = LDAP_SUCCESS;
296 if (gensec_have_feature(conn->gensec, GENSEC_FEATURE_SIGN) ||
297 gensec_have_feature(conn->gensec, GENSEC_FEATURE_SEAL)) {
299 context = talloc(call, struct ldapsrv_sasl_postprocess_context);
302 status = NT_STATUS_NO_MEMORY;
306 if (context && conn->sockets.tls) {
307 TALLOC_FREE(context);
308 status = NT_STATUS_NOT_SUPPORTED;
309 result = LDAP_UNWILLING_TO_PERFORM;
310 errstr = talloc_asprintf(reply,
311 "SASL:[%s]: Sign or Seal are not allowed if TLS is used",
312 req->creds.SASL.mechanism);
315 if (context && conn->sockets.sasl) {
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 SASL encryption has already been set up",
321 req->creds.SASL.mechanism);
325 context->conn = conn;
326 status = gensec_create_tstream(context,
327 context->conn->gensec,
328 context->conn->sockets.raw,
330 if (NT_STATUS_IS_OK(status)) {
331 if (!talloc_reference(context->sasl, conn->gensec)) {
332 status = NT_STATUS_NO_MEMORY;
336 switch (call->conn->require_strong_auth) {
337 case LDAP_SERVER_REQUIRE_STRONG_AUTH_NO:
339 case LDAP_SERVER_REQUIRE_STRONG_AUTH_ALLOW_SASL_OVER_TLS:
340 if (call->conn->sockets.active == call->conn->sockets.tls) {
343 status = NT_STATUS_NETWORK_ACCESS_DENIED;
344 result = LDAP_STRONG_AUTH_REQUIRED;
345 errstr = talloc_asprintf(reply,
346 "SASL:[%s]: not allowed if TLS is used.",
347 req->creds.SASL.mechanism);
349 case LDAP_SERVER_REQUIRE_STRONG_AUTH_YES:
350 status = NT_STATUS_NETWORK_ACCESS_DENIED;
351 result = LDAP_STRONG_AUTH_REQUIRED;
352 errstr = talloc_asprintf(reply,
353 "SASL:[%s]: Sign or Seal are required.",
354 req->creds.SASL.mechanism);
359 if (result != LDAP_SUCCESS) {
360 } else if (!NT_STATUS_IS_OK(status)) {
361 result = LDAP_OPERATIONS_ERROR;
362 errstr = talloc_asprintf(reply,
363 "SASL:[%s]: Failed to setup SASL socket: %s",
364 req->creds.SASL.mechanism, nt_errstr(status));
366 struct auth_session_info *old_session_info=NULL;
368 old_session_info = conn->session_info;
369 conn->session_info = NULL;
370 status = gensec_session_info(conn->gensec, conn, &conn->session_info);
371 if (!NT_STATUS_IS_OK(status)) {
372 conn->session_info = old_session_info;
373 result = LDAP_OPERATIONS_ERROR;
374 errstr = talloc_asprintf(reply,
375 "SASL:[%s]: Failed to get session info: %s",
376 req->creds.SASL.mechanism, nt_errstr(status));
378 talloc_unlink(conn, old_session_info);
380 /* don't leak the old LDB */
381 talloc_unlink(conn, conn->ldb);
383 status = ldapsrv_backend_Init(conn);
385 if (!NT_STATUS_IS_OK(status)) {
386 result = LDAP_OPERATIONS_ERROR;
387 errstr = talloc_asprintf(reply,
388 "SASL:[%s]: Failed to advise samdb of new credentials: %s",
389 req->creds.SASL.mechanism,
395 if (NT_STATUS_IS_OK(status) && context) {
396 call->postprocess_send = ldapsrv_sasl_postprocess_send;
397 call->postprocess_recv = ldapsrv_sasl_postprocess_recv;
398 call->postprocess_private = context;
400 talloc_unlink(conn, conn->gensec);
403 status = nt_status_squash(status);
405 result = LDAP_INVALID_CREDENTIALS;
406 errstr = ldapsrv_bind_error_msg(reply, HRES_SEC_E_LOGON_DENIED,
409 talloc_unlink(conn, conn->gensec);
413 resp->response.resultcode = result;
414 resp->response.dn = NULL;
415 resp->response.errormessage = errstr;
416 resp->response.referral = NULL;
418 ldapsrv_queue_reply(call, reply);
422 NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
424 struct ldap_BindRequest *req = &call->request->r.BindRequest;
425 struct ldapsrv_reply *reply;
426 struct ldap_BindResponse *resp;
428 if (call->conn->pending_calls != NULL) {
429 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
431 return NT_STATUS_NO_MEMORY;
434 resp = &reply->msg->r.BindResponse;
435 resp->response.resultcode = LDAP_BUSY;
436 resp->response.dn = NULL;
437 resp->response.errormessage = talloc_asprintf(reply, "Pending requests on this LDAP session");
438 resp->response.referral = NULL;
439 resp->SASL.secblob = NULL;
441 ldapsrv_queue_reply(call, reply);
446 * TODO: a simple bind should cancel an
447 * inprogress SASL bind.
450 switch (req->mechanism) {
451 case LDAP_AUTH_MECH_SIMPLE:
452 return ldapsrv_BindSimple(call);
453 case LDAP_AUTH_MECH_SASL:
454 return ldapsrv_BindSASL(call);
457 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
459 return NT_STATUS_NO_MEMORY;
462 resp = &reply->msg->r.BindResponse;
463 resp->response.resultcode = LDAP_AUTH_METHOD_NOT_SUPPORTED;
464 resp->response.dn = NULL;
465 resp->response.errormessage = talloc_asprintf(reply, "Bad AuthenticationChoice [%d]", req->mechanism);
466 resp->response.referral = NULL;
467 resp->SASL.secblob = NULL;
469 ldapsrv_queue_reply(call, reply);
473 NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
475 DEBUG(10, ("UnbindRequest\n"));