2 Unix SMB/CIFS mplementation.
6 Copyright (C) Andrew Tridgell 2005
7 Copyright (C) Volker Lendecke 2004
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "libcli/ldap/ldap.h"
27 #include "libcli/ldap/ldap_client.h"
28 #include "lib/tls/tls.h"
29 #include "auth/auth.h"
31 struct ldap_simple_creds {
36 NTSTATUS ldap_rebind(struct ldap_connection *conn)
39 struct ldap_simple_creds *creds;
41 switch (conn->bind.type) {
43 status = ldap_bind_sasl(conn, (struct cli_credentials *)conn->bind.creds);
46 case LDAP_BIND_SIMPLE:
47 creds = (struct ldap_simple_creds *)conn->bind.creds;
50 return NT_STATUS_UNSUCCESSFUL;
53 status = ldap_bind_simple(conn, creds->dn, creds->pw);
57 return NT_STATUS_UNSUCCESSFUL;
64 static struct ldap_message *new_ldap_simple_bind_msg(struct ldap_connection *conn,
65 const char *dn, const char *pw)
67 struct ldap_message *res;
69 res = new_ldap_message(conn);
74 res->type = LDAP_TAG_BindRequest;
75 res->r.BindRequest.version = 3;
76 res->r.BindRequest.dn = talloc_strdup(res, dn);
77 res->r.BindRequest.mechanism = LDAP_AUTH_MECH_SIMPLE;
78 res->r.BindRequest.creds.password = talloc_strdup(res, pw);
86 perform a simple username/password bind
88 NTSTATUS ldap_bind_simple(struct ldap_connection *conn,
89 const char *userdn, const char *password)
91 struct ldap_request *req;
92 struct ldap_message *msg;
97 return NT_STATUS_INVALID_CONNECTION;
113 if (conn->simple_pw) {
114 pw = conn->simple_pw;
120 msg = new_ldap_simple_bind_msg(conn, dn, pw);
121 NT_STATUS_HAVE_NO_MEMORY(msg);
123 /* send the request */
124 req = ldap_request_send(conn, msg);
126 NT_STATUS_HAVE_NO_MEMORY(req);
128 /* wait for replies */
129 status = ldap_request_wait(req);
130 if (!NT_STATUS_IS_OK(status)) {
135 /* check its a valid reply */
136 msg = req->replies[0];
137 if (msg->type != LDAP_TAG_BindResponse) {
139 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
142 status = ldap_check_response(conn, &msg->r.BindResponse.response);
146 if (NT_STATUS_IS_OK(status)) {
147 struct ldap_simple_creds *creds = talloc(conn, struct ldap_simple_creds);
149 return NT_STATUS_NO_MEMORY;
151 creds->dn = talloc_strdup(creds, dn);
152 creds->pw = talloc_strdup(creds, pw);
153 if (creds->dn == NULL || creds->pw == NULL) {
154 return NT_STATUS_NO_MEMORY;
156 conn->bind.type = LDAP_BIND_SIMPLE;
157 conn->bind.creds = creds;
164 static struct ldap_message *new_ldap_sasl_bind_msg(struct ldap_connection *conn,
165 const char *sasl_mechanism,
168 struct ldap_message *res;
170 res = new_ldap_message(conn);
175 res->type = LDAP_TAG_BindRequest;
176 res->r.BindRequest.version = 3;
177 res->r.BindRequest.dn = "";
178 res->r.BindRequest.mechanism = LDAP_AUTH_MECH_SASL;
179 res->r.BindRequest.creds.SASL.mechanism = talloc_strdup(res, sasl_mechanism);
181 res->r.BindRequest.creds.SASL.secblob = talloc(res, DATA_BLOB);
182 if (!res->r.BindRequest.creds.SASL.secblob) {
186 *res->r.BindRequest.creds.SASL.secblob = *secblob;
188 res->r.BindRequest.creds.SASL.secblob = NULL;
190 res->controls = NULL;
197 perform a sasl bind using the given credentials
199 NTSTATUS ldap_bind_sasl(struct ldap_connection *conn, struct cli_credentials *creds)
202 TALLOC_CTX *tmp_ctx = NULL;
204 DATA_BLOB input = data_blob(NULL, 0);
205 DATA_BLOB output = data_blob(NULL, 0);
207 struct ldap_message **sasl_mechs_msgs;
208 struct ldap_SearchResEntry *search;
211 const char **sasl_names;
213 static const char *supported_sasl_mech_attrs[] = {
214 "supportedSASLMechanisms",
218 status = gensec_client_start(conn, &conn->gensec, NULL);
219 if (!NT_STATUS_IS_OK(status)) {
220 DEBUG(0, ("Failed to start GENSEC engine (%s)\n", nt_errstr(status)));
224 /* require Kerberos SIGN/SEAL only if we don't use SSL
225 * Windows seem not to like double encryption */
226 if (!tls_enabled(conn->sock)) {
227 gensec_want_feature(conn->gensec, 0 | GENSEC_FEATURE_SIGN | GENSEC_FEATURE_SEAL);
230 status = gensec_set_credentials(conn->gensec, creds);
231 if (!NT_STATUS_IS_OK(status)) {
232 DEBUG(1, ("Failed to set GENSEC creds: %s\n",
237 status = gensec_set_target_hostname(conn->gensec, conn->host);
238 if (!NT_STATUS_IS_OK(status)) {
239 DEBUG(1, ("Failed to set GENSEC target hostname: %s\n",
244 status = gensec_set_target_service(conn->gensec, "ldap");
245 if (!NT_STATUS_IS_OK(status)) {
246 DEBUG(1, ("Failed to set GENSEC target service: %s\n",
251 status = ildap_search(conn, "", LDAP_SEARCH_SCOPE_BASE, "", supported_sasl_mech_attrs,
252 False, NULL, NULL, &sasl_mechs_msgs);
253 if (!NT_STATUS_IS_OK(status)) {
254 DEBUG(1, ("Failed to inquire of target's available sasl mechs in rootdse search: %s\n",
259 count = ildap_count_entries(conn, sasl_mechs_msgs);
261 DEBUG(1, ("Failed to inquire of target's available sasl mechs in rootdse search: wrong number of replies: %d\n",
266 tmp_ctx = talloc_new(conn);
267 if (tmp_ctx == NULL) goto failed;
269 search = &sasl_mechs_msgs[0]->r.SearchResultEntry;
270 if (search->num_attributes != 1) {
271 DEBUG(1, ("Failed to inquire of target's available sasl mechs in rootdse search: wrong number of attributes: %d\n",
272 search->num_attributes));
276 sasl_names = talloc_array(tmp_ctx, const char *, search->attributes[0].num_values + 1);
278 DEBUG(1, ("talloc_arry(char *, %d) failed\n",
283 for (i=0; i<search->attributes[0].num_values; i++) {
284 sasl_names[i] = (const char *)search->attributes[0].values[i].data;
286 sasl_names[i] = NULL;
288 status = gensec_start_mech_by_sasl_list(conn->gensec, sasl_names);
289 if (!NT_STATUS_IS_OK(status)) {
290 DEBUG(1, ("None of the %d proposed SASL mechs were acceptable: %s\n",
291 count, nt_errstr(status)));
296 NTSTATUS gensec_status;
297 struct ldap_message *response;
298 struct ldap_message *msg;
299 struct ldap_request *req;
300 int result = LDAP_OTHER;
302 status = gensec_update(conn->gensec, tmp_ctx,
305 /* The status value here, from GENSEC is vital to the security
306 * of the system. Even if the other end accepts, if GENSEC
307 * claims 'MORE_PROCESSING_REQUIRED' then you must keep
308 * feeding it blobs, or else the remote host/attacker might
309 * avoid mutal authentication requirements.
311 * Likewise, you must not feed GENSEC too much (after the OK),
312 * it doesn't like that either
315 gensec_status = status;
317 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
318 !NT_STATUS_IS_OK(status)) {
321 if (NT_STATUS_IS_OK(status) && output.length == 0) {
325 /* Perhaps we should make gensec_start_mech_by_sasl_list() return the name we got? */
326 msg = new_ldap_sasl_bind_msg(tmp_ctx, conn->gensec->ops->sasl_name, (output.data?&output:NULL));
328 status = NT_STATUS_NO_MEMORY;
332 req = ldap_request_send(conn, msg);
334 status = NT_STATUS_NO_MEMORY;
337 talloc_steal(tmp_ctx, req);
339 status = ldap_result_n(req, 0, &response);
340 if (!NT_STATUS_IS_OK(status)) {
344 if (response->type != LDAP_TAG_BindResponse) {
345 status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
349 result = response->r.BindResponse.response.resultcode;
351 if (result != LDAP_SUCCESS && result != LDAP_SASL_BIND_IN_PROGRESS) {
352 status = ldap_check_response(conn,
353 &response->r.BindResponse.response);
357 /* This is where we check if GENSEC wanted to be fed more data */
358 if (!NT_STATUS_EQUAL(gensec_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
361 if (response->r.BindResponse.SASL.secblob) {
362 input = *response->r.BindResponse.SASL.secblob;
364 input = data_blob(NULL, 0);
368 if (NT_STATUS_IS_OK(status) &&
369 (gensec_have_feature(conn->gensec, GENSEC_FEATURE_SEAL) ||
370 gensec_have_feature(conn->gensec, GENSEC_FEATURE_SIGN))) {
371 conn->enable_wrap = True;
374 talloc_free(tmp_ctx);
376 if (NT_STATUS_IS_OK(status)) {
377 conn->bind.type = LDAP_BIND_SASL;
378 conn->bind.creds = creds;
384 talloc_free(tmp_ctx);
385 talloc_free(conn->gensec);