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 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.
22 #include "ldap_server/ldap_server.h"
23 #include "auth/auth.h"
24 #include "libcli/ldap/ldap.h"
25 #include "smbd/service_stream.h"
26 #include "lib/ldb/include/ldb.h"
27 #include "lib/ldb/include/ldb_errors.h"
28 #include "dsdb/samdb/samdb.h"
30 static NTSTATUS ldapsrv_BindSimple(struct ldapsrv_call *call)
32 struct ldap_BindRequest *req = &call->request->r.BindRequest;
33 struct ldapsrv_reply *reply;
34 struct ldap_BindResponse *resp;
38 const char *nt4_domain, *nt4_account;
40 struct auth_session_info *session_info;
44 DEBUG(10, ("BindSimple dn: %s\n",req->dn));
46 status = crack_dn_to_nt4_name(call, req->dn, &nt4_domain, &nt4_account);
47 if (NT_STATUS_IS_OK(status)) {
48 status = authenticate_username_pw(call, nt4_domain, nt4_account,
49 req->creds.password, &session_info);
52 /* When we add authentication here, we also need to handle telling the backends */
54 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
56 return NT_STATUS_NO_MEMORY;
59 if (NT_STATUS_IS_OK(status)) {
60 result = LDAP_SUCCESS;
63 talloc_free(call->conn->session_info);
64 call->conn->session_info = session_info;
66 /* don't leak the old LDB */
67 talloc_free(call->conn->ldb);
69 status = ldapsrv_backend_Init(call->conn);
71 if (!NT_STATUS_IS_OK(status)) {
72 result = LDAP_OPERATIONS_ERROR;
73 errstr = talloc_asprintf(reply, "Simple Bind: Failed to advise ldb new credentials: %s", nt_errstr(status));
76 status = auth_nt_status_squash(status);
78 result = LDAP_INVALID_CREDENTIALS;
79 errstr = talloc_asprintf(reply, "Simple Bind Failed: %s", nt_errstr(status));
82 resp = &reply->msg->r.BindResponse;
83 resp->response.resultcode = result;
84 resp->response.errormessage = errstr;
85 resp->response.dn = NULL;
86 resp->response.referral = NULL;
88 /* This looks wrong... */
89 resp->SASL.secblob = data_blob(NULL, 0);
91 ldapsrv_queue_reply(call, reply);
95 static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
97 struct ldap_BindRequest *req = &call->request->r.BindRequest;
98 struct ldapsrv_reply *reply;
99 struct ldap_BindResponse *resp;
100 struct ldapsrv_connection *conn;
103 NTSTATUS status = NT_STATUS_OK;
105 DEBUG(10, ("BindSASL dn: %s\n",req->dn));
107 if (!call->conn->gensec) {
108 call->conn->session_info = NULL;
110 status = gensec_server_start(call->conn, &call->conn->gensec,
111 call->conn->connection->event.ctx);
112 if (!NT_STATUS_IS_OK(status)) {
113 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
117 gensec_set_target_service(call->conn->gensec, "ldap");
119 gensec_set_credentials(call->conn->gensec, call->conn->server_credentials);
121 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_SIGN);
122 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_SEAL);
123 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_ASYNC_REPLIES);
125 status = gensec_start_mech_by_sasl_name(call->conn->gensec, req->creds.SASL.mechanism);
126 if (!NT_STATUS_IS_OK(status)) {
127 DEBUG(1, ("Failed to start GENSEC SASL[%s] server code: %s\n",
128 req->creds.SASL.mechanism, nt_errstr(status)));
132 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
134 return NT_STATUS_NO_MEMORY;
136 resp = &reply->msg->r.BindResponse;
140 if (NT_STATUS_IS_OK(status)) {
141 status = gensec_update(call->conn->gensec, reply,
142 req->creds.SASL.secblob, &resp->SASL.secblob);
145 if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
146 result = LDAP_SASL_BIND_IN_PROGRESS;
148 } else if (NT_STATUS_IS_OK(status)) {
149 struct auth_session_info *old_session_info;
151 result = LDAP_SUCCESS;
153 if (gensec_have_feature(call->conn->gensec, GENSEC_FEATURE_SEAL) ||
154 gensec_have_feature(call->conn->gensec, GENSEC_FEATURE_SIGN)) {
155 call->conn->enable_wrap = True;
157 old_session_info = call->conn->session_info;
158 call->conn->session_info = NULL;
159 status = gensec_session_info(call->conn->gensec, &call->conn->session_info);
160 if (!NT_STATUS_IS_OK(status)) {
161 call->conn->session_info = old_session_info;
162 result = LDAP_OPERATIONS_ERROR;
163 errstr = talloc_asprintf(reply, "SASL:[%s]: Failed to get session info: %s", req->creds.SASL.mechanism, nt_errstr(status));
165 talloc_free(old_session_info);
167 /* don't leak the old LDB */
168 talloc_free(call->conn->ldb);
170 status = ldapsrv_backend_Init(call->conn);
172 if (!NT_STATUS_IS_OK(status)) {
173 result = LDAP_OPERATIONS_ERROR;
174 errstr = talloc_asprintf(reply, "SASL:[%s]: Failed to advise samdb of new credentials: %s", req->creds.SASL.mechanism, nt_errstr(status));
178 status = auth_nt_status_squash(status);
179 result = LDAP_INVALID_CREDENTIALS;
180 errstr = talloc_asprintf(reply, "SASL:[%s]: %s", req->creds.SASL.mechanism, nt_errstr(status));
183 resp->response.resultcode = result;
184 resp->response.dn = NULL;
185 resp->response.errormessage = errstr;
186 resp->response.referral = NULL;
188 ldapsrv_queue_reply(call, reply);
192 NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
194 struct ldap_BindRequest *req = &call->request->r.BindRequest;
195 struct ldapsrv_reply *reply;
196 struct ldap_BindResponse *resp;
198 switch (req->mechanism) {
199 case LDAP_AUTH_MECH_SIMPLE:
200 return ldapsrv_BindSimple(call);
201 case LDAP_AUTH_MECH_SASL:
202 return ldapsrv_BindSASL(call);
205 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
207 return NT_STATUS_NO_MEMORY;
210 resp = &reply->msg->r.BindResponse;
211 resp->response.resultcode = 7;
212 resp->response.dn = NULL;
213 resp->response.errormessage = talloc_asprintf(reply, "Bad AuthenticationChoice [%d]", req->mechanism);
214 resp->response.referral = NULL;
215 resp->SASL.secblob = data_blob(NULL, 0);
217 ldapsrv_queue_reply(call, reply);
221 NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
223 DEBUG(10, ("UnbindRequest\n"));