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 "dsdb/samdb/samdb.h"
28 static NTSTATUS ldapsrv_BindSimple(struct ldapsrv_call *call)
30 struct ldap_BindRequest *req = &call->request->r.BindRequest;
31 struct ldapsrv_reply *reply;
32 struct ldap_BindResponse *resp;
36 const char *nt4_domain, *nt4_account;
38 struct auth_session_info *session_info;
42 DEBUG(10, ("BindSimple dn: %s\n",req->dn));
44 status = crack_dn_to_nt4_name(call, req->dn, &nt4_domain, &nt4_account);
45 if (NT_STATUS_IS_OK(status)) {
46 status = authenticate_username_pw(call, nt4_domain, nt4_account,
47 req->creds.password, &session_info);
50 /* When we add authentication here, we also need to handle telling the backends */
52 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
54 return NT_STATUS_NO_MEMORY;
57 if (NT_STATUS_IS_OK(status)) {
58 struct ldapsrv_partition *part;
59 result = LDAP_SUCCESS;
62 talloc_free(call->conn->session_info);
63 call->conn->session_info = session_info;
64 for (part = call->conn->partitions; part; part = part->next) {
65 if (!part->ops->Bind) {
68 status = part->ops->Bind(part, call->conn);
69 if (!NT_STATUS_IS_OK(status)) {
70 result = LDAP_OPERATIONS_ERROR;
71 errstr = talloc_asprintf(reply, "Simple Bind: Failed to advise partition %s of new credentials: %s", part->base_dn, nt_errstr(status));
75 status = auth_nt_status_squash(status);
77 result = LDAP_INVALID_CREDENTIALS;
78 errstr = talloc_asprintf(reply, "Simple Bind Failed: %s", nt_errstr(status));
81 resp = &reply->msg->r.BindResponse;
82 resp->response.resultcode = result;
83 resp->response.errormessage = errstr;
84 resp->response.dn = NULL;
85 resp->response.referral = NULL;
87 /* This looks wrong... */
88 resp->SASL.secblob = data_blob(NULL, 0);
90 ldapsrv_queue_reply(call, reply);
94 static NTSTATUS ldapsrv_BindSASL(struct ldapsrv_call *call)
96 struct ldap_BindRequest *req = &call->request->r.BindRequest;
97 struct ldapsrv_reply *reply;
98 struct ldap_BindResponse *resp;
99 struct ldapsrv_connection *conn;
102 NTSTATUS status = NT_STATUS_OK;
104 DEBUG(10, ("BindSASL dn: %s\n",req->dn));
106 if (!call->conn->gensec) {
107 struct cli_credentials *server_credentials;
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");
120 = cli_credentials_init(call);
121 if (!server_credentials) {
122 DEBUG(1, ("Failed to init server credentials\n"));
123 return NT_STATUS_NO_MEMORY;
126 cli_credentials_set_conf(server_credentials);
127 status = cli_credentials_set_machine_account(server_credentials);
128 if (!NT_STATUS_IS_OK(status)) {
129 DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(status)));
130 talloc_free(server_credentials);
131 server_credentials = NULL;
134 gensec_set_credentials(call->conn->gensec, server_credentials);
136 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_SIGN);
137 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_SEAL);
138 gensec_want_feature(call->conn->gensec, GENSEC_FEATURE_ASYNC_REPLIES);
140 status = gensec_start_mech_by_sasl_name(call->conn->gensec, req->creds.SASL.mechanism);
141 if (!NT_STATUS_IS_OK(status)) {
142 DEBUG(1, ("Failed to start GENSEC SASL[%s] server code: %s\n",
143 req->creds.SASL.mechanism, nt_errstr(status)));
147 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
149 return NT_STATUS_NO_MEMORY;
151 resp = &reply->msg->r.BindResponse;
155 if (NT_STATUS_IS_OK(status)) {
156 status = gensec_update(call->conn->gensec, reply,
157 req->creds.SASL.secblob, &resp->SASL.secblob);
160 if (NT_STATUS_EQUAL(NT_STATUS_MORE_PROCESSING_REQUIRED, status)) {
161 result = LDAP_SASL_BIND_IN_PROGRESS;
163 } else if (NT_STATUS_IS_OK(status)) {
164 struct ldapsrv_partition *part;
165 struct auth_session_info *old_session_info;
167 result = LDAP_SUCCESS;
169 if (gensec_have_feature(call->conn->gensec, GENSEC_FEATURE_SEAL) ||
170 gensec_have_feature(call->conn->gensec, GENSEC_FEATURE_SIGN)) {
171 call->conn->enable_wrap = True;
173 old_session_info = call->conn->session_info;
174 call->conn->session_info = NULL;
175 status = gensec_session_info(call->conn->gensec, &call->conn->session_info);
176 if (!NT_STATUS_IS_OK(status)) {
177 call->conn->session_info = old_session_info;
178 result = LDAP_OPERATIONS_ERROR;
179 errstr = talloc_asprintf(reply, "SASL:[%s]: Failed to get session info: %s", req->creds.SASL.mechanism, nt_errstr(status));
181 talloc_free(old_session_info);
182 for (part = call->conn->partitions; part; part = part->next) {
183 if (!part->ops->Bind) {
186 status = part->ops->Bind(part, conn);
187 if (!NT_STATUS_IS_OK(status)) {
188 result = LDAP_OPERATIONS_ERROR;
189 errstr = talloc_asprintf(reply, "SASL:[%s]: Failed to advise partition %s of new credentials: %s", req->creds.SASL.mechanism, part->base_dn, nt_errstr(status));
194 status = auth_nt_status_squash(status);
195 result = LDAP_INVALID_CREDENTIALS;
196 errstr = talloc_asprintf(reply, "SASL:[%s]: %s", req->creds.SASL.mechanism, nt_errstr(status));
199 resp->response.resultcode = result;
200 resp->response.dn = NULL;
201 resp->response.errormessage = errstr;
202 resp->response.referral = NULL;
204 ldapsrv_queue_reply(call, reply);
208 NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
210 struct ldap_BindRequest *req = &call->request->r.BindRequest;
211 struct ldapsrv_reply *reply;
212 struct ldap_BindResponse *resp;
214 switch (req->mechanism) {
215 case LDAP_AUTH_MECH_SIMPLE:
216 return ldapsrv_BindSimple(call);
217 case LDAP_AUTH_MECH_SASL:
218 return ldapsrv_BindSASL(call);
221 reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
223 return NT_STATUS_NO_MEMORY;
226 resp = &reply->msg->r.BindResponse;
227 resp->response.resultcode = 7;
228 resp->response.dn = NULL;
229 resp->response.errormessage = talloc_asprintf(reply, "Bad AuthenticationChoice [%d]", req->mechanism);
230 resp->response.referral = NULL;
231 resp->SASL.secblob = data_blob(NULL, 0);
233 ldapsrv_queue_reply(call, reply);
237 NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
239 DEBUG(10, ("UnbindRequest\n"));