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 status
= ldapsrv_setup_gensec(conn
, req
->creds
.SASL
.mechanism
,
256 if (!NT_STATUS_IS_OK(status
)) {
257 DEBUG(1, ("Failed to start GENSEC server for [%s] code: %s\n",
258 ldb_binary_encode_string(call
, req
->creds
.SASL
.mechanism
),
260 result
= LDAP_OPERATIONS_ERROR
;
261 errstr
= talloc_asprintf(reply
, "SASL: Failed to start authentication system: %s",
266 if (NT_STATUS_IS_OK(status
)) {
267 DATA_BLOB input
= data_blob(NULL
, 0);
268 DATA_BLOB output
= data_blob(NULL
, 0);
270 if (req
->creds
.SASL
.secblob
) {
271 input
= *req
->creds
.SASL
.secblob
;
274 status
= gensec_update_ev(conn
->gensec
, reply
, conn
->connection
->event
.ctx
,
277 /* Windows 2000 mmc doesn't like secblob == NULL and reports a decoding error */
278 resp
->SASL
.secblob
= talloc(reply
, DATA_BLOB
);
279 NT_STATUS_HAVE_NO_MEMORY(resp
->SASL
.secblob
);
280 *resp
->SASL
.secblob
= output
;
282 resp
->SASL
.secblob
= NULL
;
285 if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED
, status
)) {
286 result
= LDAP_SASL_BIND_IN_PROGRESS
;
288 } else if (NT_STATUS_IS_OK(status
)) {
289 struct ldapsrv_sasl_postprocess_context
*context
= NULL
;
291 result
= LDAP_SUCCESS
;
294 if (gensec_have_feature(conn
->gensec
, GENSEC_FEATURE_SIGN
) ||
295 gensec_have_feature(conn
->gensec
, GENSEC_FEATURE_SEAL
)) {
297 context
= talloc(call
, struct ldapsrv_sasl_postprocess_context
);
300 status
= NT_STATUS_NO_MEMORY
;
304 if (context
&& conn
->sockets
.tls
) {
305 TALLOC_FREE(context
);
306 status
= NT_STATUS_NOT_SUPPORTED
;
307 result
= LDAP_UNWILLING_TO_PERFORM
;
308 errstr
= talloc_asprintf(reply
,
309 "SASL:[%s]: Sign or Seal are not allowed if TLS is used",
310 req
->creds
.SASL
.mechanism
);
313 if (context
&& conn
->sockets
.sasl
) {
314 TALLOC_FREE(context
);
315 status
= NT_STATUS_NOT_SUPPORTED
;
316 result
= LDAP_UNWILLING_TO_PERFORM
;
317 errstr
= talloc_asprintf(reply
,
318 "SASL:[%s]: Sign or Seal are not allowed if SASL encryption has already been set up",
319 req
->creds
.SASL
.mechanism
);
323 context
->conn
= conn
;
324 status
= gensec_create_tstream(context
,
325 context
->conn
->gensec
,
326 context
->conn
->sockets
.raw
,
328 if (NT_STATUS_IS_OK(status
)) {
329 if (!talloc_reference(context
->sasl
, conn
->gensec
)) {
330 status
= NT_STATUS_NO_MEMORY
;
334 switch (call
->conn
->require_strong_auth
) {
335 case LDAP_SERVER_REQUIRE_STRONG_AUTH_NO
:
337 case LDAP_SERVER_REQUIRE_STRONG_AUTH_ALLOW_SASL_OVER_TLS
:
338 if (call
->conn
->sockets
.active
== call
->conn
->sockets
.tls
) {
341 status
= NT_STATUS_NETWORK_ACCESS_DENIED
;
342 result
= LDAP_STRONG_AUTH_REQUIRED
;
343 errstr
= talloc_asprintf(reply
,
344 "SASL:[%s]: not allowed if TLS is used.",
345 req
->creds
.SASL
.mechanism
);
347 case LDAP_SERVER_REQUIRE_STRONG_AUTH_YES
:
348 status
= NT_STATUS_NETWORK_ACCESS_DENIED
;
349 result
= LDAP_STRONG_AUTH_REQUIRED
;
350 errstr
= talloc_asprintf(reply
,
351 "SASL:[%s]: Sign or Seal are required.",
352 req
->creds
.SASL
.mechanism
);
357 if (result
!= LDAP_SUCCESS
) {
358 } else if (!NT_STATUS_IS_OK(status
)) {
359 result
= LDAP_OPERATIONS_ERROR
;
360 errstr
= talloc_asprintf(reply
,
361 "SASL:[%s]: Failed to setup SASL socket: %s",
362 req
->creds
.SASL
.mechanism
, nt_errstr(status
));
364 struct auth_session_info
*old_session_info
=NULL
;
366 old_session_info
= conn
->session_info
;
367 conn
->session_info
= NULL
;
368 status
= gensec_session_info(conn
->gensec
, conn
, &conn
->session_info
);
369 if (!NT_STATUS_IS_OK(status
)) {
370 conn
->session_info
= old_session_info
;
371 result
= LDAP_OPERATIONS_ERROR
;
372 errstr
= talloc_asprintf(reply
,
373 "SASL:[%s]: Failed to get session info: %s",
374 req
->creds
.SASL
.mechanism
, nt_errstr(status
));
376 talloc_unlink(conn
, old_session_info
);
378 /* don't leak the old LDB */
379 talloc_unlink(conn
, conn
->ldb
);
381 status
= ldapsrv_backend_Init(conn
);
383 if (!NT_STATUS_IS_OK(status
)) {
384 result
= LDAP_OPERATIONS_ERROR
;
385 errstr
= talloc_asprintf(reply
,
386 "SASL:[%s]: Failed to advise samdb of new credentials: %s",
387 req
->creds
.SASL
.mechanism
,
393 if (NT_STATUS_IS_OK(status
) && context
) {
394 call
->postprocess_send
= ldapsrv_sasl_postprocess_send
;
395 call
->postprocess_recv
= ldapsrv_sasl_postprocess_recv
;
396 call
->postprocess_private
= context
;
398 talloc_unlink(conn
, conn
->gensec
);
401 status
= nt_status_squash(status
);
403 result
= LDAP_INVALID_CREDENTIALS
;
404 errstr
= ldapsrv_bind_error_msg(reply
, HRES_SEC_E_LOGON_DENIED
,
407 talloc_unlink(conn
, conn
->gensec
);
411 resp
->response
.resultcode
= result
;
412 resp
->response
.dn
= NULL
;
413 resp
->response
.errormessage
= errstr
;
414 resp
->response
.referral
= NULL
;
416 ldapsrv_queue_reply(call
, reply
);
420 NTSTATUS
ldapsrv_BindRequest(struct ldapsrv_call
*call
)
422 struct ldap_BindRequest
*req
= &call
->request
->r
.BindRequest
;
423 struct ldapsrv_reply
*reply
;
424 struct ldap_BindResponse
*resp
;
426 if (call
->conn
->pending_calls
!= NULL
) {
427 reply
= ldapsrv_init_reply(call
, LDAP_TAG_BindResponse
);
429 return NT_STATUS_NO_MEMORY
;
432 resp
= &reply
->msg
->r
.BindResponse
;
433 resp
->response
.resultcode
= LDAP_BUSY
;
434 resp
->response
.dn
= NULL
;
435 resp
->response
.errormessage
= talloc_asprintf(reply
, "Pending requests on this LDAP session");
436 resp
->response
.referral
= NULL
;
437 resp
->SASL
.secblob
= NULL
;
439 ldapsrv_queue_reply(call
, reply
);
444 * TODO: a simple bind should cancel an
445 * inprogress SASL bind.
448 switch (req
->mechanism
) {
449 case LDAP_AUTH_MECH_SIMPLE
:
450 return ldapsrv_BindSimple(call
);
451 case LDAP_AUTH_MECH_SASL
:
452 return ldapsrv_BindSASL(call
);
455 reply
= ldapsrv_init_reply(call
, LDAP_TAG_BindResponse
);
457 return NT_STATUS_NO_MEMORY
;
460 resp
= &reply
->msg
->r
.BindResponse
;
461 resp
->response
.resultcode
= LDAP_AUTH_METHOD_NOT_SUPPORTED
;
462 resp
->response
.dn
= NULL
;
463 resp
->response
.errormessage
= talloc_asprintf(reply
, "Bad AuthenticationChoice [%d]", req
->mechanism
);
464 resp
->response
.referral
= NULL
;
465 resp
->SASL
.secblob
= NULL
;
467 ldapsrv_queue_reply(call
, reply
);
471 NTSTATUS
ldapsrv_UnbindRequest(struct ldapsrv_call
*call
)
473 DEBUG(10, ("UnbindRequest\n"));