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"
31 static NTSTATUS ldapsrv_BindSimple(struct ldapsrv_call *call)
33 struct ldap_BindRequest *req = &call->request->r.BindRequest;
34 struct ldapsrv_reply *reply;
35 struct ldap_BindResponse *resp;
39 const char *nt4_domain, *nt4_account;
41 struct auth_session_info *session_info;
45 DEBUG(10, ("BindSimple dn: %s\n",req->dn));
47 status = crack_auto_name_to_nt4_name(call, req->dn, &nt4_domain, &nt4_account);
48 if (NT_STATUS_IS_OK(status)) {
49 status = authenticate_username_pw(call,
50 call->conn->connection->event.ctx,
51 call->conn->connection->msg_ctx,
52 nt4_domain, nt4_account,
57 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
59 return NT_STATUS_NO_MEMORY;
62 if (NT_STATUS_IS_OK(status)) {
63 result = LDAP_SUCCESS;
66 talloc_free(call->conn->session_info);
67 call->conn->session_info = session_info;
68 talloc_steal(call->conn, session_info);
70 /* don't leak the old LDB */
71 talloc_free(call->conn->ldb);
73 status = ldapsrv_backend_Init(call->conn);
75 if (!NT_STATUS_IS_OK(status)) {
76 result = LDAP_OPERATIONS_ERROR;
77 errstr = talloc_asprintf(reply, "Simple Bind: Failed to advise ldb new credentials: %s", nt_errstr(status));
80 status = auth_nt_status_squash(status);
82 result = LDAP_INVALID_CREDENTIALS;
83 errstr = talloc_asprintf(reply, "Simple Bind Failed: %s", nt_errstr(status));
86 resp = &reply->msg->r.BindResponse;
87 resp->response.resultcode = result;
88 resp->response.errormessage = errstr;
89 resp->response.dn = NULL;
90 resp->response.referral = NULL;
91 resp->SASL.secblob = NULL;
93 ldapsrv_queue_reply(call, reply);
97 struct ldapsrv_sasl_context {
98 struct ldapsrv_connection *conn;
99 struct socket_context *sasl_socket;
102 static void ldapsrv_set_sasl(void *private)
104 struct ldapsrv_sasl_context *ctx = talloc_get_type(private, struct ldapsrv_sasl_context);
105 talloc_steal(ctx->conn->connection, ctx->sasl_socket);
106 talloc_unlink(ctx->conn->connection, ctx->conn->connection->socket);
108 ctx->conn->sockets.sasl = ctx->sasl_socket;
109 ctx->conn->connection->socket = ctx->sasl_socket;
110 packet_set_socket(ctx->conn->packet, ctx->conn->connection->socket);
113 static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
115 struct ldap_BindRequest *req = &call->request->r.BindRequest;
116 struct ldapsrv_reply *reply;
117 struct ldap_BindResponse *resp;
118 struct ldapsrv_connection *conn;
120 const char *errstr=NULL;
121 NTSTATUS status = NT_STATUS_OK;
123 DEBUG(10, ("BindSASL dn: %s\n",req->dn));
125 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
127 return NT_STATUS_NO_MEMORY;
129 resp = &reply->msg->r.BindResponse;
134 * TODO: a SASL bind with a different mechanism
135 * should cancel an inprogress SASL bind.
140 conn->session_info = NULL;
142 status = gensec_server_start(conn,
143 conn->connection->event.ctx,
144 conn->connection->msg_ctx,
146 if (!NT_STATUS_IS_OK(status)) {
147 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
148 result = LDAP_OPERATIONS_ERROR;
149 errstr = talloc_asprintf(reply, "SASL: Failed to start authentication system: %s",
153 gensec_set_target_service(conn->gensec, "ldap");
155 gensec_set_credentials(conn->gensec, conn->server_credentials);
157 gensec_want_feature(conn->gensec, GENSEC_FEATURE_SIGN);
158 gensec_want_feature(conn->gensec, GENSEC_FEATURE_SEAL);
159 gensec_want_feature(conn->gensec, GENSEC_FEATURE_ASYNC_REPLIES);
161 status = gensec_start_mech_by_sasl_name(conn->gensec, req->creds.SASL.mechanism);
163 if (!NT_STATUS_IS_OK(status)) {
164 DEBUG(1, ("Failed to start GENSEC SASL[%s] server code: %s\n",
165 req->creds.SASL.mechanism, nt_errstr(status)));
166 result = LDAP_OPERATIONS_ERROR;
167 errstr = talloc_asprintf(reply, "SASL:[%s]: Failed to start authentication backend: %s",
168 req->creds.SASL.mechanism, nt_errstr(status));
173 if (NT_STATUS_IS_OK(status)) {
174 DATA_BLOB input = data_blob(NULL, 0);
175 DATA_BLOB output = data_blob(NULL, 0);
177 if (req->creds.SASL.secblob) {
178 input = *req->creds.SASL.secblob;
181 resp->SASL.secblob = talloc(reply, DATA_BLOB);
182 NT_STATUS_HAVE_NO_MEMORY(resp->SASL.secblob);
184 status = gensec_update(conn->gensec, reply,
187 /* Windows 2000 mmc doesn't like secblob == NULL and reports a decoding error */
188 resp->SASL.secblob = talloc(reply, DATA_BLOB);
189 NT_STATUS_HAVE_NO_MEMORY(resp->SASL.secblob);
190 *resp->SASL.secblob = output;
192 resp->SASL.secblob = NULL;
195 if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
196 result = LDAP_SASL_BIND_IN_PROGRESS;
198 } else if (NT_STATUS_IS_OK(status)) {
199 struct auth_session_info *old_session_info=NULL;
200 struct ldapsrv_sasl_context *ctx;
202 result = LDAP_SUCCESS;
205 ctx = talloc(call, struct ldapsrv_sasl_context);
208 status = NT_STATUS_NO_MEMORY;
211 status = gensec_socket_init(conn->gensec,
212 conn->connection->socket,
213 conn->connection->event.ctx,
214 stream_io_handler_callback,
219 if (!ctx || !NT_STATUS_IS_OK(status)) {
220 conn->session_info = old_session_info;
221 result = LDAP_OPERATIONS_ERROR;
222 errstr = talloc_asprintf(reply,
223 "SASL:[%s]: Failed to setup SASL socket: %s",
224 req->creds.SASL.mechanism, nt_errstr(status));
227 call->send_callback = ldapsrv_set_sasl;
228 call->send_private = ctx;
230 old_session_info = conn->session_info;
231 conn->session_info = NULL;
232 status = gensec_session_info(conn->gensec, &conn->session_info);
233 if (!NT_STATUS_IS_OK(status)) {
234 conn->session_info = old_session_info;
235 result = LDAP_OPERATIONS_ERROR;
236 errstr = talloc_asprintf(reply,
237 "SASL:[%s]: Failed to get session info: %s",
238 req->creds.SASL.mechanism, nt_errstr(status));
240 talloc_free(old_session_info);
241 talloc_steal(conn, conn->session_info);
243 /* don't leak the old LDB */
244 talloc_free(conn->ldb);
246 status = ldapsrv_backend_Init(conn);
248 if (!NT_STATUS_IS_OK(status)) {
249 result = LDAP_OPERATIONS_ERROR;
250 errstr = talloc_asprintf(reply,
251 "SASL:[%s]: Failed to advise samdb of new credentials: %s",
252 req->creds.SASL.mechanism,
258 status = auth_nt_status_squash(status);
260 result = LDAP_INVALID_CREDENTIALS;
261 errstr = talloc_asprintf(reply, "SASL:[%s]: %s", req->creds.SASL.mechanism, nt_errstr(status));
265 resp->response.resultcode = result;
266 resp->response.dn = NULL;
267 resp->response.errormessage = errstr;
268 resp->response.referral = NULL;
270 ldapsrv_queue_reply(call, reply);
274 NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
276 struct ldap_BindRequest *req = &call->request->r.BindRequest;
277 struct ldapsrv_reply *reply;
278 struct ldap_BindResponse *resp;
281 * TODO: we should fail the bind request
282 * if there're any pending requests.
284 * also a simple bind should cancel an
285 * inprogress SASL bind.
288 switch (req->mechanism) {
289 case LDAP_AUTH_MECH_SIMPLE:
290 return ldapsrv_BindSimple(call);
291 case LDAP_AUTH_MECH_SASL:
292 return ldapsrv_BindSASL(call);
295 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
297 return NT_STATUS_NO_MEMORY;
300 resp = &reply->msg->r.BindResponse;
301 resp->response.resultcode = 7;
302 resp->response.dn = NULL;
303 resp->response.errormessage = talloc_asprintf(reply, "Bad AuthenticationChoice [%d]", req->mechanism);
304 resp->response.referral = NULL;
305 resp->SASL.secblob = NULL;
307 ldapsrv_queue_reply(call, reply);
311 NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
313 DEBUG(10, ("UnbindRequest\n"));