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 /* don't leak the old LDB */
113 talloc_unlink(call
->conn
, call
->conn
->ldb
);
115 status
= ldapsrv_backend_Init(call
->conn
);
117 if (!NT_STATUS_IS_OK(status
)) {
118 result
= LDAP_OPERATIONS_ERROR
;
119 errstr
= talloc_asprintf(reply
, "Simple Bind: Failed to advise ldb new credentials: %s", nt_errstr(status
));
122 status
= nt_status_squash(status
);
124 result
= LDAP_INVALID_CREDENTIALS
;
125 errstr
= ldapsrv_bind_error_msg(reply
, HRES_SEC_E_INVALID_TOKEN
,
130 resp
= &reply
->msg
->r
.BindResponse
;
131 resp
->response
.resultcode
= result
;
132 resp
->response
.errormessage
= errstr
;
133 resp
->response
.dn
= NULL
;
134 resp
->response
.referral
= NULL
;
135 resp
->SASL
.secblob
= NULL
;
137 ldapsrv_queue_reply(call
, reply
);
141 struct ldapsrv_sasl_postprocess_context
{
142 struct ldapsrv_connection
*conn
;
143 struct tstream_context
*sasl
;
146 struct ldapsrv_sasl_postprocess_state
{
150 static struct tevent_req
*ldapsrv_sasl_postprocess_send(TALLOC_CTX
*mem_ctx
,
151 struct tevent_context
*ev
,
154 struct ldapsrv_sasl_postprocess_context
*context
=
155 talloc_get_type_abort(private_data
,
156 struct ldapsrv_sasl_postprocess_context
);
157 struct tevent_req
*req
;
158 struct ldapsrv_sasl_postprocess_state
*state
;
160 req
= tevent_req_create(mem_ctx
, &state
,
161 struct ldapsrv_sasl_postprocess_state
);
166 TALLOC_FREE(context
->conn
->sockets
.sasl
);
167 context
->conn
->sockets
.sasl
= talloc_move(context
->conn
, &context
->sasl
);
168 context
->conn
->sockets
.active
= context
->conn
->sockets
.sasl
;
170 tevent_req_done(req
);
171 return tevent_req_post(req
, ev
);
174 static NTSTATUS
ldapsrv_sasl_postprocess_recv(struct tevent_req
*req
)
176 return tevent_req_simple_recv_ntstatus(req
);
179 static NTSTATUS
ldapsrv_setup_gensec(struct ldapsrv_connection
*conn
,
180 const char *sasl_mech
,
181 struct gensec_security
**_gensec_security
)
185 struct gensec_security
*gensec_security
;
187 status
= samba_server_gensec_start(conn
,
188 conn
->connection
->event
.ctx
,
189 conn
->connection
->msg_ctx
,
191 conn
->server_credentials
,
194 if (!NT_STATUS_IS_OK(status
)) {
198 status
= gensec_set_target_service_description(gensec_security
,
200 if (!NT_STATUS_IS_OK(status
)) {
204 status
= gensec_set_remote_address(gensec_security
,
205 conn
->connection
->remote_address
);
206 if (!NT_STATUS_IS_OK(status
)) {
210 status
= gensec_set_local_address(gensec_security
,
211 conn
->connection
->local_address
);
212 if (!NT_STATUS_IS_OK(status
)) {
216 gensec_want_feature(gensec_security
, GENSEC_FEATURE_ASYNC_REPLIES
);
217 gensec_want_feature(gensec_security
, GENSEC_FEATURE_LDAP_STYLE
);
219 status
= gensec_start_mech_by_sasl_name(gensec_security
, sasl_mech
);
221 if (!NT_STATUS_IS_OK(status
)) {
225 *_gensec_security
= gensec_security
;
229 static NTSTATUS
ldapsrv_BindSASL(struct ldapsrv_call
*call
)
231 struct ldap_BindRequest
*req
= &call
->request
->r
.BindRequest
;
232 struct ldapsrv_reply
*reply
;
233 struct ldap_BindResponse
*resp
;
234 struct ldapsrv_connection
*conn
;
236 const char *errstr
=NULL
;
237 NTSTATUS status
= NT_STATUS_OK
;
239 DEBUG(10, ("BindSASL dn: %s\n",req
->dn
));
241 reply
= ldapsrv_init_reply(call
, LDAP_TAG_BindResponse
);
243 return NT_STATUS_NO_MEMORY
;
245 resp
= &reply
->msg
->r
.BindResponse
;
250 * TODO: a SASL bind with a different mechanism
251 * should cancel an inprogress SASL bind.
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"));