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 "libcli/ldap/ldap.h"
24 #include "smbd/service.h"
25 #include "lib/ldb/include/ldb.h"
26 #include "lib/ldb/include/ldb_errors.h"
27 #include "dsdb/samdb/samdb.h"
28 #include "auth/gensec/gensec.h"
29 #include "auth/gensec/socket.h"
30 #include "param/param.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;
40 const char *nt4_domain, *nt4_account;
42 struct auth_session_info *session_info;
46 DEBUG(10, ("BindSimple dn: %s\n",req->dn));
48 status = crack_auto_name_to_nt4_name(call, global_loadparm, 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,
54 nt4_domain, nt4_account,
59 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
61 return NT_STATUS_NO_MEMORY;
64 if (NT_STATUS_IS_OK(status)) {
65 result = LDAP_SUCCESS;
68 talloc_free(call->conn->session_info);
69 call->conn->session_info = session_info;
70 talloc_steal(call->conn, session_info);
72 /* don't leak the old LDB */
73 talloc_free(call->conn->ldb);
75 status = ldapsrv_backend_Init(call->conn);
77 if (!NT_STATUS_IS_OK(status)) {
78 result = LDAP_OPERATIONS_ERROR;
79 errstr = talloc_asprintf(reply, "Simple Bind: Failed to advise ldb new credentials: %s", nt_errstr(status));
82 status = auth_nt_status_squash(status);
84 result = LDAP_INVALID_CREDENTIALS;
85 errstr = talloc_asprintf(reply, "Simple Bind Failed: %s", nt_errstr(status));
88 resp = &reply->msg->r.BindResponse;
89 resp->response.resultcode = result;
90 resp->response.errormessage = errstr;
91 resp->response.dn = NULL;
92 resp->response.referral = NULL;
93 resp->SASL.secblob = NULL;
95 ldapsrv_queue_reply(call, reply);
99 struct ldapsrv_sasl_context {
100 struct ldapsrv_connection *conn;
101 struct socket_context *sasl_socket;
104 static void ldapsrv_set_sasl(void *private)
106 struct ldapsrv_sasl_context *ctx = talloc_get_type(private, struct ldapsrv_sasl_context);
107 talloc_steal(ctx->conn->connection, ctx->sasl_socket);
108 talloc_unlink(ctx->conn->connection, ctx->conn->connection->socket);
110 ctx->conn->sockets.sasl = ctx->sasl_socket;
111 ctx->conn->connection->socket = ctx->sasl_socket;
112 packet_set_socket(ctx->conn->packet, ctx->conn->connection->socket);
115 static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
117 struct ldap_BindRequest *req = &call->request->r.BindRequest;
118 struct ldapsrv_reply *reply;
119 struct ldap_BindResponse *resp;
120 struct ldapsrv_connection *conn;
122 const char *errstr=NULL;
123 NTSTATUS status = NT_STATUS_OK;
125 DEBUG(10, ("BindSASL dn: %s\n",req->dn));
127 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
129 return NT_STATUS_NO_MEMORY;
131 resp = &reply->msg->r.BindResponse;
136 * TODO: a SASL bind with a different mechanism
137 * should cancel an inprogress SASL bind.
142 conn->session_info = NULL;
144 status = gensec_server_start(conn,
145 conn->connection->event.ctx,
147 conn->connection->msg_ctx,
149 if (!NT_STATUS_IS_OK(status)) {
150 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
151 result = LDAP_OPERATIONS_ERROR;
152 errstr = talloc_asprintf(reply, "SASL: Failed to start authentication system: %s",
156 gensec_set_target_service(conn->gensec, "ldap");
158 gensec_set_credentials(conn->gensec, conn->server_credentials);
160 gensec_want_feature(conn->gensec, GENSEC_FEATURE_SIGN);
161 gensec_want_feature(conn->gensec, GENSEC_FEATURE_SEAL);
162 gensec_want_feature(conn->gensec, GENSEC_FEATURE_ASYNC_REPLIES);
164 status = gensec_start_mech_by_sasl_name(conn->gensec, req->creds.SASL.mechanism);
166 if (!NT_STATUS_IS_OK(status)) {
167 DEBUG(1, ("Failed to start GENSEC SASL[%s] server code: %s\n",
168 req->creds.SASL.mechanism, nt_errstr(status)));
169 result = LDAP_OPERATIONS_ERROR;
170 errstr = talloc_asprintf(reply, "SASL:[%s]: Failed to start authentication backend: %s",
171 req->creds.SASL.mechanism, nt_errstr(status));
176 if (NT_STATUS_IS_OK(status)) {
177 DATA_BLOB input = data_blob(NULL, 0);
178 DATA_BLOB output = data_blob(NULL, 0);
180 if (req->creds.SASL.secblob) {
181 input = *req->creds.SASL.secblob;
184 resp->SASL.secblob = talloc(reply, DATA_BLOB);
185 NT_STATUS_HAVE_NO_MEMORY(resp->SASL.secblob);
187 status = gensec_update(conn->gensec, reply,
190 /* Windows 2000 mmc doesn't like secblob == NULL and reports a decoding error */
191 resp->SASL.secblob = talloc(reply, DATA_BLOB);
192 NT_STATUS_HAVE_NO_MEMORY(resp->SASL.secblob);
193 *resp->SASL.secblob = output;
195 resp->SASL.secblob = NULL;
198 if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
199 result = LDAP_SASL_BIND_IN_PROGRESS;
201 } else if (NT_STATUS_IS_OK(status)) {
202 struct auth_session_info *old_session_info=NULL;
203 struct ldapsrv_sasl_context *ctx;
205 result = LDAP_SUCCESS;
208 ctx = talloc(call, struct ldapsrv_sasl_context);
211 status = NT_STATUS_NO_MEMORY;
214 status = gensec_socket_init(conn->gensec,
215 conn->connection->socket,
216 conn->connection->event.ctx,
217 stream_io_handler_callback,
222 if (!ctx || !NT_STATUS_IS_OK(status)) {
223 conn->session_info = old_session_info;
224 result = LDAP_OPERATIONS_ERROR;
225 errstr = talloc_asprintf(reply,
226 "SASL:[%s]: Failed to setup SASL socket: %s",
227 req->creds.SASL.mechanism, nt_errstr(status));
230 call->send_callback = ldapsrv_set_sasl;
231 call->send_private = ctx;
233 old_session_info = conn->session_info;
234 conn->session_info = NULL;
235 status = gensec_session_info(conn->gensec, &conn->session_info);
236 if (!NT_STATUS_IS_OK(status)) {
237 conn->session_info = old_session_info;
238 result = LDAP_OPERATIONS_ERROR;
239 errstr = talloc_asprintf(reply,
240 "SASL:[%s]: Failed to get session info: %s",
241 req->creds.SASL.mechanism, nt_errstr(status));
243 talloc_free(old_session_info);
244 talloc_steal(conn, conn->session_info);
246 /* don't leak the old LDB */
247 talloc_free(conn->ldb);
249 status = ldapsrv_backend_Init(conn);
251 if (!NT_STATUS_IS_OK(status)) {
252 result = LDAP_OPERATIONS_ERROR;
253 errstr = talloc_asprintf(reply,
254 "SASL:[%s]: Failed to advise samdb of new credentials: %s",
255 req->creds.SASL.mechanism,
261 status = auth_nt_status_squash(status);
263 result = LDAP_INVALID_CREDENTIALS;
264 errstr = talloc_asprintf(reply, "SASL:[%s]: %s", req->creds.SASL.mechanism, nt_errstr(status));
268 resp->response.resultcode = result;
269 resp->response.dn = NULL;
270 resp->response.errormessage = errstr;
271 resp->response.referral = NULL;
273 ldapsrv_queue_reply(call, reply);
277 NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
279 struct ldap_BindRequest *req = &call->request->r.BindRequest;
280 struct ldapsrv_reply *reply;
281 struct ldap_BindResponse *resp;
284 * TODO: we should fail the bind request
285 * if there're any pending requests.
287 * also a simple bind should cancel an
288 * inprogress SASL bind.
291 switch (req->mechanism) {
292 case LDAP_AUTH_MECH_SIMPLE:
293 return ldapsrv_BindSimple(call);
294 case LDAP_AUTH_MECH_SASL:
295 return ldapsrv_BindSASL(call);
298 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
300 return NT_STATUS_NO_MEMORY;
303 resp = &reply->msg->r.BindResponse;
304 resp->response.resultcode = 7;
305 resp->response.dn = NULL;
306 resp->response.errormessage = talloc_asprintf(reply, "Bad AuthenticationChoice [%d]", req->mechanism);
307 resp->response.referral = NULL;
308 resp->SASL.secblob = NULL;
310 ldapsrv_queue_reply(call, reply);
314 NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
316 DEBUG(10, ("UnbindRequest\n"));