s4-ldap_server: Do not set conn->session_info to NULL, keep valid at all times
[Samba.git] / source4 / ldap_server / ldap_bind.c
blobe0f13f2de2d85d9909c9790b445ae553bbf3636b
1 /*
2 Unix SMB/CIFS implementation.
3 LDAP server
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/>.
20 #include "includes.h"
21 #include "ldap_server/ldap_server.h"
22 #include "auth/auth.h"
23 #include "smbd/service.h"
24 #include <ldb.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,
33 HRESULT hresult,
34 uint32_t DSID,
35 NTSTATUS status)
37 WERROR werr;
38 char *msg = NULL;
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),
56 (unsigned)DSID,
57 (unsigned)W_ERROR_V(werr));
59 return msg;
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;
69 int result;
70 const char *errstr;
71 const char *nt4_domain, *nt4_account;
73 struct auth_session_info *session_info;
75 NTSTATUS status;
77 DEBUG(10, ("BindSimple dn: %s\n",req->dn));
79 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
80 if (!reply) {
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.");
93 goto do_reply;
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,
101 call->conn->lp_ctx,
102 nt4_domain, nt4_account,
103 req->creds.password,
104 MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT |
105 MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
106 &session_info);
109 if (NT_STATUS_IS_OK(status)) {
110 result = LDAP_SUCCESS;
111 errstr = NULL;
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));
125 } else {
126 status = nt_status_squash(status);
128 result = LDAP_INVALID_CREDENTIALS;
129 errstr = ldapsrv_bind_error_msg(reply, HRES_SEC_E_INVALID_TOKEN,
130 0x0C0903A9, status);
133 do_reply:
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);
142 return NT_STATUS_OK;
145 struct ldapsrv_sasl_postprocess_context {
146 struct ldapsrv_connection *conn;
147 struct tstream_context *sasl;
150 struct ldapsrv_sasl_postprocess_state {
151 uint8_t dummy;
154 static struct tevent_req *ldapsrv_sasl_postprocess_send(TALLOC_CTX *mem_ctx,
155 struct tevent_context *ev,
156 void *private_data)
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);
166 if (req == NULL) {
167 return NULL;
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)
187 NTSTATUS status;
189 struct gensec_security *gensec_security;
191 status = samba_server_gensec_start(conn,
192 conn->connection->event.ctx,
193 conn->connection->msg_ctx,
194 conn->lp_ctx,
195 conn->server_credentials,
196 "ldap",
197 &gensec_security);
198 if (!NT_STATUS_IS_OK(status)) {
199 return status;
202 status = gensec_set_remote_address(gensec_security,
203 conn->connection->remote_address);
204 if (!NT_STATUS_IS_OK(status)) {
205 return status;
208 status = gensec_set_local_address(gensec_security,
209 conn->connection->local_address);
210 if (!NT_STATUS_IS_OK(status)) {
211 return 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)) {
220 return status;
223 *_gensec_security = gensec_security;
224 return status;
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;
233 int result = 0;
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);
240 if (!reply) {
241 return NT_STATUS_NO_MEMORY;
243 resp = &reply->msg->r.BindResponse;
245 conn = call->conn;
248 * TODO: a SASL bind with a different mechanism
249 * should cancel an inprogress SASL bind.
250 * (see RFC 4513)
253 if (!conn->gensec) {
254 status = ldapsrv_setup_gensec(conn, req->creds.SASL.mechanism,
255 &conn->gensec);
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),
259 nt_errstr(status)));
260 result = LDAP_OPERATIONS_ERROR;
261 errstr = talloc_asprintf(reply, "SASL: Failed to start authentication system: %s",
262 nt_errstr(status));
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,
275 input, &output);
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;
281 } else {
282 resp->SASL.secblob = NULL;
285 if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
286 result = LDAP_SASL_BIND_IN_PROGRESS;
287 errstr = NULL;
288 } else if (NT_STATUS_IS_OK(status)) {
289 struct ldapsrv_sasl_postprocess_context *context = NULL;
291 result = LDAP_SUCCESS;
292 errstr = NULL;
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);
299 if (!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);
322 if (context) {
323 context->conn = conn;
324 status = gensec_create_tstream(context,
325 context->conn->gensec,
326 context->conn->sockets.raw,
327 &context->sasl);
328 if (NT_STATUS_IS_OK(status)) {
329 if (!talloc_reference(context->sasl, conn->gensec)) {
330 status = NT_STATUS_NO_MEMORY;
333 } else {
334 switch (call->conn->require_strong_auth) {
335 case LDAP_SERVER_REQUIRE_STRONG_AUTH_NO:
336 break;
337 case LDAP_SERVER_REQUIRE_STRONG_AUTH_ALLOW_SASL_OVER_TLS:
338 if (call->conn->sockets.active == call->conn->sockets.tls) {
339 break;
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);
346 break;
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);
353 break;
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));
363 } else {
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));
375 } else {
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,
388 nt_errstr(status));
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);
399 conn->gensec = NULL;
400 } else {
401 status = nt_status_squash(status);
402 if (result == 0) {
403 result = LDAP_INVALID_CREDENTIALS;
404 errstr = ldapsrv_bind_error_msg(reply, HRES_SEC_E_LOGON_DENIED,
405 0x0C0904DC, status);
407 talloc_unlink(conn, conn->gensec);
408 conn->gensec = NULL;
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);
417 return NT_STATUS_OK;
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);
428 if (!reply) {
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);
440 return NT_STATUS_OK;
444 * TODO: a simple bind should cancel an
445 * inprogress SASL bind.
446 * (see RFC 4513)
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);
456 if (!reply) {
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);
468 return NT_STATUS_OK;
471 NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
473 DEBUG(10, ("UnbindRequest\n"));
474 return NT_STATUS_OK;