r19598: Ahead of a merge to current lorikeet-heimdal:
[Samba.git] / source / ldap_server / ldap_bind.c
blobf88d08e822db2bd7f0129b6e1c6eb9ef82e87f5d
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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "includes.h"
22 #include "ldap_server/ldap_server.h"
23 #include "auth/auth.h"
24 #include "libcli/ldap/ldap.h"
25 #include "smbd/service.h"
26 #include "lib/ldb/include/ldb.h"
27 #include "lib/ldb/include/ldb_errors.h"
28 #include "dsdb/samdb/samdb.h"
29 #include "auth/gensec/gensec.h"
30 #include "auth/gensec/socket.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_dn_to_nt4_name(call, 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 nt4_domain, nt4_account,
54 req->creds.password,
55 &session_info);
58 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
59 if (!reply) {
60 return NT_STATUS_NO_MEMORY;
63 if (NT_STATUS_IS_OK(status)) {
64 result = LDAP_SUCCESS;
65 errstr = NULL;
67 talloc_free(call->conn->session_info);
68 call->conn->session_info = session_info;
69 talloc_steal(call->conn, session_info);
71 /* don't leak the old LDB */
72 talloc_free(call->conn->ldb);
74 status = ldapsrv_backend_Init(call->conn);
76 if (!NT_STATUS_IS_OK(status)) {
77 result = LDAP_OPERATIONS_ERROR;
78 errstr = talloc_asprintf(reply, "Simple Bind: Failed to advise ldb new credentials: %s", nt_errstr(status));
80 } else {
81 status = auth_nt_status_squash(status);
83 result = LDAP_INVALID_CREDENTIALS;
84 errstr = talloc_asprintf(reply, "Simple Bind Failed: %s", nt_errstr(status));
87 resp = &reply->msg->r.BindResponse;
88 resp->response.resultcode = result;
89 resp->response.errormessage = errstr;
90 resp->response.dn = NULL;
91 resp->response.referral = NULL;
92 resp->SASL.secblob = NULL;
94 ldapsrv_queue_reply(call, reply);
95 return NT_STATUS_OK;
98 struct ldapsrv_sasl_context {
99 struct ldapsrv_connection *conn;
100 struct socket_context *sasl_socket;
103 static void ldapsrv_set_sasl(void *private)
105 struct ldapsrv_sasl_context *ctx = talloc_get_type(private, struct ldapsrv_sasl_context);
106 talloc_steal(ctx->conn->connection, ctx->sasl_socket);
107 talloc_unlink(ctx->conn->connection, ctx->conn->connection->socket);
109 ctx->conn->sockets.sasl = ctx->sasl_socket;
110 ctx->conn->connection->socket = ctx->sasl_socket;
111 packet_set_socket(ctx->conn->packet, ctx->conn->connection->socket);
114 static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
116 struct ldap_BindRequest *req = &call->request->r.BindRequest;
117 struct ldapsrv_reply *reply;
118 struct ldap_BindResponse *resp;
119 struct ldapsrv_connection *conn;
120 int result = 0;
121 const char *errstr=NULL;
122 NTSTATUS status = NT_STATUS_OK;
124 DEBUG(10, ("BindSASL dn: %s\n",req->dn));
126 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
127 if (!reply) {
128 return NT_STATUS_NO_MEMORY;
130 resp = &reply->msg->r.BindResponse;
132 conn = call->conn;
135 * TODO: a SASL bind with a different mechanism
136 * should cancel an inprogress SASL bind.
137 * (see RFC 4513)
140 if (!conn->gensec) {
141 conn->session_info = NULL;
143 status = gensec_server_start(conn,
144 conn->connection->event.ctx,
145 conn->connection->msg_ctx,
146 &conn->gensec);
147 if (!NT_STATUS_IS_OK(status)) {
148 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
149 result = LDAP_OPERATIONS_ERROR;
150 errstr = talloc_asprintf(reply, "SASL: Failed to start authentication system: %s",
151 nt_errstr(status));
152 } else {
154 gensec_set_target_service(conn->gensec, "ldap");
156 gensec_set_credentials(conn->gensec, conn->server_credentials);
158 gensec_want_feature(conn->gensec, GENSEC_FEATURE_SIGN);
159 gensec_want_feature(conn->gensec, GENSEC_FEATURE_SEAL);
160 gensec_want_feature(conn->gensec, GENSEC_FEATURE_ASYNC_REPLIES);
162 status = gensec_start_mech_by_sasl_name(conn->gensec, req->creds.SASL.mechanism);
164 if (!NT_STATUS_IS_OK(status)) {
165 DEBUG(1, ("Failed to start GENSEC SASL[%s] server code: %s\n",
166 req->creds.SASL.mechanism, nt_errstr(status)));
167 result = LDAP_OPERATIONS_ERROR;
168 errstr = talloc_asprintf(reply, "SASL:[%s]: Failed to start authentication backend: %s",
169 req->creds.SASL.mechanism, nt_errstr(status));
174 if (NT_STATUS_IS_OK(status)) {
175 DATA_BLOB input = data_blob(NULL, 0);
176 DATA_BLOB output = data_blob(NULL, 0);
178 if (req->creds.SASL.secblob) {
179 input = *req->creds.SASL.secblob;
182 resp->SASL.secblob = talloc(reply, DATA_BLOB);
183 NT_STATUS_HAVE_NO_MEMORY(resp->SASL.secblob);
185 status = gensec_update(conn->gensec, reply,
186 input, &output);
188 /* TODO: gensec should really handle the difference between NULL and length=0 better! */
189 if (output.data) {
190 resp->SASL.secblob = talloc(reply, DATA_BLOB);
191 NT_STATUS_HAVE_NO_MEMORY(resp->SASL.secblob);
192 *resp->SASL.secblob = output;
193 } else {
194 resp->SASL.secblob = NULL;
196 } else {
197 resp->SASL.secblob = NULL;
200 if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
201 result = LDAP_SASL_BIND_IN_PROGRESS;
202 errstr = NULL;
203 } else if (NT_STATUS_IS_OK(status)) {
204 struct auth_session_info *old_session_info=NULL;
205 struct ldapsrv_sasl_context *ctx;
207 result = LDAP_SUCCESS;
208 errstr = NULL;
210 ctx = talloc(call, struct ldapsrv_sasl_context);
212 if (!ctx) {
213 status = NT_STATUS_NO_MEMORY;
214 } else {
215 ctx->conn = conn;
216 status = gensec_socket_init(conn->gensec,
217 conn->connection->socket,
218 conn->connection->event.ctx,
219 stream_io_handler_callback,
220 conn->connection,
221 &ctx->sasl_socket);
224 if (!ctx || !NT_STATUS_IS_OK(status)) {
225 conn->session_info = old_session_info;
226 result = LDAP_OPERATIONS_ERROR;
227 errstr = talloc_asprintf(reply,
228 "SASL:[%s]: Failed to setup SASL socket: %s",
229 req->creds.SASL.mechanism, nt_errstr(status));
230 } else {
232 call->send_callback = ldapsrv_set_sasl;
233 call->send_private = ctx;
235 old_session_info = conn->session_info;
236 conn->session_info = NULL;
237 status = gensec_session_info(conn->gensec, &conn->session_info);
238 if (!NT_STATUS_IS_OK(status)) {
239 conn->session_info = old_session_info;
240 result = LDAP_OPERATIONS_ERROR;
241 errstr = talloc_asprintf(reply,
242 "SASL:[%s]: Failed to get session info: %s",
243 req->creds.SASL.mechanism, nt_errstr(status));
244 } else {
245 talloc_free(old_session_info);
246 talloc_steal(conn, conn->session_info);
248 /* don't leak the old LDB */
249 talloc_free(conn->ldb);
251 status = ldapsrv_backend_Init(conn);
253 if (!NT_STATUS_IS_OK(status)) {
254 result = LDAP_OPERATIONS_ERROR;
255 errstr = talloc_asprintf(reply,
256 "SASL:[%s]: Failed to advise samdb of new credentials: %s",
257 req->creds.SASL.mechanism,
258 nt_errstr(status));
262 } else {
263 status = auth_nt_status_squash(status);
264 if (result == 0) {
265 result = LDAP_INVALID_CREDENTIALS;
266 errstr = talloc_asprintf(reply, "SASL:[%s]: %s", req->creds.SASL.mechanism, nt_errstr(status));
270 resp->response.resultcode = result;
271 resp->response.dn = NULL;
272 resp->response.errormessage = errstr;
273 resp->response.referral = NULL;
275 ldapsrv_queue_reply(call, reply);
276 return NT_STATUS_OK;
279 NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
281 struct ldap_BindRequest *req = &call->request->r.BindRequest;
282 struct ldapsrv_reply *reply;
283 struct ldap_BindResponse *resp;
286 * TODO: we should fail the bind request
287 * if there're any pending requests.
289 * also a simple bind should cancel an
290 * inprogress SASL bind.
291 * (see RFC 4513)
293 switch (req->mechanism) {
294 case LDAP_AUTH_MECH_SIMPLE:
295 return ldapsrv_BindSimple(call);
296 case LDAP_AUTH_MECH_SASL:
297 return ldapsrv_BindSASL(call);
300 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
301 if (!reply) {
302 return NT_STATUS_NO_MEMORY;
305 resp = &reply->msg->r.BindResponse;
306 resp->response.resultcode = 7;
307 resp->response.dn = NULL;
308 resp->response.errormessage = talloc_asprintf(reply, "Bad AuthenticationChoice [%d]", req->mechanism);
309 resp->response.referral = NULL;
310 resp->SASL.secblob = NULL;
312 ldapsrv_queue_reply(call, reply);
313 return NT_STATUS_OK;
316 NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
318 DEBUG(10, ("UnbindRequest\n"));
319 return NT_STATUS_OK;