s3:smbd: pass smbXsrv_connection to smbd_[un]lock_socket()
[Samba.git] / source4 / ldap_server / ldap_bind.c
blob69a6b6153b034d32c39c1865075c21262e73d291
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 NTSTATUS ldapsrv_BindSimple(struct ldapsrv_call *call)
34 struct ldap_BindRequest *req = &call->request->r.BindRequest;
35 struct ldapsrv_reply *reply;
36 struct ldap_BindResponse *resp;
38 int result;
39 const char *errstr;
40 const char *nt4_domain, *nt4_account;
42 struct auth_session_info *session_info;
44 NTSTATUS status;
46 DEBUG(10, ("BindSimple dn: %s\n",req->dn));
48 status = crack_auto_name_to_nt4_name(call, call->conn->connection->event.ctx, call->conn->lp_ctx, req->dn, &nt4_domain, &nt4_account);
49 if (NT_STATUS_IS_OK(status)) {
50 status = authenticate_username_pw(call,
51 call->conn->connection->event.ctx,
52 call->conn->connection->msg_ctx,
53 call->conn->lp_ctx,
54 nt4_domain, nt4_account,
55 req->creds.password,
56 MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT |
57 MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
58 &session_info);
61 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
62 if (!reply) {
63 return NT_STATUS_NO_MEMORY;
66 if (NT_STATUS_IS_OK(status)) {
67 result = LDAP_SUCCESS;
68 errstr = NULL;
70 talloc_unlink(call->conn, call->conn->session_info);
71 call->conn->session_info = talloc_steal(call->conn, session_info);
73 /* don't leak the old LDB */
74 talloc_unlink(call->conn, call->conn->ldb);
76 status = ldapsrv_backend_Init(call->conn);
78 if (!NT_STATUS_IS_OK(status)) {
79 result = LDAP_OPERATIONS_ERROR;
80 errstr = talloc_asprintf(reply, "Simple Bind: Failed to advise ldb new credentials: %s", nt_errstr(status));
82 } else {
83 status = nt_status_squash(status);
85 result = LDAP_INVALID_CREDENTIALS;
86 errstr = talloc_asprintf(reply, "Simple Bind Failed: %s", nt_errstr(status));
89 resp = &reply->msg->r.BindResponse;
90 resp->response.resultcode = result;
91 resp->response.errormessage = errstr;
92 resp->response.dn = NULL;
93 resp->response.referral = NULL;
94 resp->SASL.secblob = NULL;
96 ldapsrv_queue_reply(call, reply);
97 return NT_STATUS_OK;
100 struct ldapsrv_sasl_postprocess_context {
101 struct ldapsrv_connection *conn;
102 struct tstream_context *sasl;
105 struct ldapsrv_sasl_postprocess_state {
106 uint8_t dummy;
109 static struct tevent_req *ldapsrv_sasl_postprocess_send(TALLOC_CTX *mem_ctx,
110 struct tevent_context *ev,
111 void *private_data)
113 struct ldapsrv_sasl_postprocess_context *context =
114 talloc_get_type_abort(private_data,
115 struct ldapsrv_sasl_postprocess_context);
116 struct tevent_req *req;
117 struct ldapsrv_sasl_postprocess_state *state;
119 req = tevent_req_create(mem_ctx, &state,
120 struct ldapsrv_sasl_postprocess_state);
121 if (req == NULL) {
122 return NULL;
125 TALLOC_FREE(context->conn->sockets.sasl);
126 context->conn->sockets.sasl = talloc_move(context->conn, &context->sasl);
127 context->conn->sockets.active = context->conn->sockets.sasl;
129 tevent_req_done(req);
130 return tevent_req_post(req, ev);
133 static NTSTATUS ldapsrv_sasl_postprocess_recv(struct tevent_req *req)
135 return tevent_req_simple_recv_ntstatus(req);
138 static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
140 struct ldap_BindRequest *req = &call->request->r.BindRequest;
141 struct ldapsrv_reply *reply;
142 struct ldap_BindResponse *resp;
143 struct ldapsrv_connection *conn;
144 int result = 0;
145 const char *errstr=NULL;
146 NTSTATUS status = NT_STATUS_OK;
148 DEBUG(10, ("BindSASL dn: %s\n",req->dn));
150 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
151 if (!reply) {
152 return NT_STATUS_NO_MEMORY;
154 resp = &reply->msg->r.BindResponse;
156 conn = call->conn;
159 * TODO: a SASL bind with a different mechanism
160 * should cancel an inprogress SASL bind.
161 * (see RFC 4513)
164 if (!conn->gensec) {
165 conn->session_info = NULL;
167 status = samba_server_gensec_start(conn,
168 conn->connection->event.ctx,
169 conn->connection->msg_ctx,
170 conn->lp_ctx,
171 conn->server_credentials,
172 "ldap",
173 &conn->gensec);
174 if (!NT_STATUS_IS_OK(status)) {
175 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
176 result = LDAP_OPERATIONS_ERROR;
177 errstr = talloc_asprintf(reply, "SASL: Failed to start authentication system: %s",
178 nt_errstr(status));
179 } else {
181 gensec_want_feature(conn->gensec, GENSEC_FEATURE_SIGN);
182 gensec_want_feature(conn->gensec, GENSEC_FEATURE_SEAL);
183 gensec_want_feature(conn->gensec, GENSEC_FEATURE_ASYNC_REPLIES);
185 status = gensec_start_mech_by_sasl_name(conn->gensec, req->creds.SASL.mechanism);
187 if (!NT_STATUS_IS_OK(status)) {
188 DEBUG(1, ("Failed to start GENSEC SASL[%s] server code: %s\n",
189 req->creds.SASL.mechanism, nt_errstr(status)));
190 result = LDAP_OPERATIONS_ERROR;
191 errstr = talloc_asprintf(reply, "SASL:[%s]: Failed to start authentication backend: %s",
192 req->creds.SASL.mechanism, nt_errstr(status));
197 if (NT_STATUS_IS_OK(status)) {
198 DATA_BLOB input = data_blob(NULL, 0);
199 DATA_BLOB output = data_blob(NULL, 0);
201 if (req->creds.SASL.secblob) {
202 input = *req->creds.SASL.secblob;
205 status = gensec_update_ev(conn->gensec, reply, conn->connection->event.ctx,
206 input, &output);
208 /* Windows 2000 mmc doesn't like secblob == NULL and reports a decoding error */
209 resp->SASL.secblob = talloc(reply, DATA_BLOB);
210 NT_STATUS_HAVE_NO_MEMORY(resp->SASL.secblob);
211 *resp->SASL.secblob = output;
212 } else {
213 resp->SASL.secblob = NULL;
216 if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
217 result = LDAP_SASL_BIND_IN_PROGRESS;
218 errstr = NULL;
219 } else if (NT_STATUS_IS_OK(status)) {
220 struct auth_session_info *old_session_info=NULL;
221 struct ldapsrv_sasl_postprocess_context *context = NULL;
223 result = LDAP_SUCCESS;
224 errstr = NULL;
226 if (gensec_have_feature(conn->gensec, GENSEC_FEATURE_SIGN) ||
227 gensec_have_feature(conn->gensec, GENSEC_FEATURE_SEAL)) {
229 context = talloc(call, struct ldapsrv_sasl_postprocess_context);
231 if (!context) {
232 status = NT_STATUS_NO_MEMORY;
236 if (context && conn->sockets.tls) {
237 TALLOC_FREE(context);
238 status = NT_STATUS_NOT_SUPPORTED;
239 result = LDAP_UNWILLING_TO_PERFORM;
240 errstr = talloc_asprintf(reply,
241 "SASL:[%s]: Sign or Seal are not allowed if TLS is used",
242 req->creds.SASL.mechanism);
245 if (context && conn->sockets.sasl) {
246 TALLOC_FREE(context);
247 status = NT_STATUS_NOT_SUPPORTED;
248 result = LDAP_UNWILLING_TO_PERFORM;
249 errstr = talloc_asprintf(reply,
250 "SASL:[%s]: Sign or Seal are not allowed if SASL encryption has already been set up",
251 req->creds.SASL.mechanism);
254 if (context) {
255 context->conn = conn;
256 status = gensec_create_tstream(context,
257 context->conn->gensec,
258 context->conn->sockets.raw,
259 &context->sasl);
260 if (NT_STATUS_IS_OK(status)) {
261 if (!talloc_reference(context->sasl, conn->gensec)) {
262 status = NT_STATUS_NO_MEMORY;
267 if (result != LDAP_SUCCESS) {
268 conn->session_info = old_session_info;
269 } else if (!NT_STATUS_IS_OK(status)) {
270 conn->session_info = old_session_info;
271 result = LDAP_OPERATIONS_ERROR;
272 errstr = talloc_asprintf(reply,
273 "SASL:[%s]: Failed to setup SASL socket: %s",
274 req->creds.SASL.mechanism, nt_errstr(status));
275 } else {
277 old_session_info = conn->session_info;
278 conn->session_info = NULL;
279 status = gensec_session_info(conn->gensec, conn, &conn->session_info);
280 if (!NT_STATUS_IS_OK(status)) {
281 conn->session_info = old_session_info;
282 result = LDAP_OPERATIONS_ERROR;
283 errstr = talloc_asprintf(reply,
284 "SASL:[%s]: Failed to get session info: %s",
285 req->creds.SASL.mechanism, nt_errstr(status));
286 } else {
287 talloc_unlink(conn, old_session_info);
289 /* don't leak the old LDB */
290 talloc_unlink(conn, conn->ldb);
292 status = ldapsrv_backend_Init(conn);
294 if (!NT_STATUS_IS_OK(status)) {
295 result = LDAP_OPERATIONS_ERROR;
296 errstr = talloc_asprintf(reply,
297 "SASL:[%s]: Failed to advise samdb of new credentials: %s",
298 req->creds.SASL.mechanism,
299 nt_errstr(status));
304 if (NT_STATUS_IS_OK(status) && context) {
305 call->postprocess_send = ldapsrv_sasl_postprocess_send;
306 call->postprocess_recv = ldapsrv_sasl_postprocess_recv;
307 call->postprocess_private = context;
309 talloc_unlink(conn, conn->gensec);
310 conn->gensec = NULL;
311 } else {
312 status = nt_status_squash(status);
313 if (result == 0) {
314 result = LDAP_INVALID_CREDENTIALS;
315 errstr = talloc_asprintf(reply, "SASL:[%s]: %s", req->creds.SASL.mechanism, nt_errstr(status));
317 talloc_unlink(conn, conn->gensec);
318 conn->gensec = NULL;
321 resp->response.resultcode = result;
322 resp->response.dn = NULL;
323 resp->response.errormessage = errstr;
324 resp->response.referral = NULL;
326 ldapsrv_queue_reply(call, reply);
327 return NT_STATUS_OK;
330 NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
332 struct ldap_BindRequest *req = &call->request->r.BindRequest;
333 struct ldapsrv_reply *reply;
334 struct ldap_BindResponse *resp;
337 * TODO: we should fail the bind request
338 * if there're any pending requests.
340 * also a simple bind should cancel an
341 * inprogress SASL bind.
342 * (see RFC 4513)
344 switch (req->mechanism) {
345 case LDAP_AUTH_MECH_SIMPLE:
346 return ldapsrv_BindSimple(call);
347 case LDAP_AUTH_MECH_SASL:
348 return ldapsrv_BindSASL(call);
351 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
352 if (!reply) {
353 return NT_STATUS_NO_MEMORY;
356 resp = &reply->msg->r.BindResponse;
357 resp->response.resultcode = 7;
358 resp->response.dn = NULL;
359 resp->response.errormessage = talloc_asprintf(reply, "Bad AuthenticationChoice [%d]", req->mechanism);
360 resp->response.referral = NULL;
361 resp->SASL.secblob = NULL;
363 ldapsrv_queue_reply(call, reply);
364 return NT_STATUS_OK;
367 NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
369 DEBUG(10, ("UnbindRequest\n"));
370 return NT_STATUS_OK;