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 "auth/auth.h"
30 static struct ldap_message *new_ldap_simple_bind_msg(struct ldap_connection *conn,
31 const char *dn, const char *pw)
33 struct ldap_message *res;
35 res = new_ldap_message(conn);
40 res->type = LDAP_TAG_BindRequest;
41 res->r.BindRequest.version = 3;
42 res->r.BindRequest.dn = talloc_strdup(res, dn);
43 res->r.BindRequest.mechanism = LDAP_AUTH_MECH_SIMPLE;
44 res->r.BindRequest.creds.password = talloc_strdup(res, pw);
52 perform a simple username/password bind
54 NTSTATUS ldap_bind_simple(struct ldap_connection *conn,
55 const char *userdn, const char *password)
57 struct ldap_request *req;
58 struct ldap_message *msg;
63 return NT_STATUS_INVALID_CONNECTION;
79 if (conn->simple_pw) {
86 msg = new_ldap_simple_bind_msg(conn, dn, pw);
87 NT_STATUS_HAVE_NO_MEMORY(msg);
89 /* send the request */
90 req = ldap_request_send(conn, msg);
92 NT_STATUS_HAVE_NO_MEMORY(req);
94 /* wait for replies */
95 status = ldap_request_wait(req);
96 if (!NT_STATUS_IS_OK(status)) {
101 /* check its a valid reply */
102 msg = req->replies[0];
103 if (msg->type != LDAP_TAG_BindResponse) {
105 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
108 status = ldap_check_response(conn, &msg->r.BindResponse.response);
116 static struct ldap_message *new_ldap_sasl_bind_msg(struct ldap_connection *conn,
117 const char *sasl_mechanism,
120 struct ldap_message *res;
122 res = new_ldap_message(conn);
127 res->type = LDAP_TAG_BindRequest;
128 res->r.BindRequest.version = 3;
129 res->r.BindRequest.dn = "";
130 res->r.BindRequest.mechanism = LDAP_AUTH_MECH_SASL;
131 res->r.BindRequest.creds.SASL.mechanism = talloc_strdup(res, sasl_mechanism);
132 res->r.BindRequest.creds.SASL.secblob = *secblob;
133 res->controls = NULL;
140 perform a sasl bind using the given credentials
142 NTSTATUS ldap_bind_sasl(struct ldap_connection *conn, struct cli_credentials *creds)
145 TALLOC_CTX *tmp_ctx = NULL;
147 DATA_BLOB input = data_blob(NULL, 0);
148 DATA_BLOB output = data_blob(NULL, 0);
150 struct ldap_message **sasl_mechs_msgs;
151 struct ldap_SearchResEntry *search;
154 const char **sasl_names;
156 static const char *supported_sasl_mech_attrs[] = {
157 "supportedSASLMechanisms",
161 status = gensec_client_start(conn, &conn->gensec, NULL);
162 if (!NT_STATUS_IS_OK(status)) {
163 DEBUG(0, ("Failed to start GENSEC engine (%s)\n", nt_errstr(status)));
167 gensec_want_feature(conn->gensec, 0 | GENSEC_FEATURE_SIGN | GENSEC_FEATURE_SEAL);
169 status = gensec_set_credentials(conn->gensec, creds);
170 if (!NT_STATUS_IS_OK(status)) {
171 DEBUG(1, ("Failed to set GENSEC creds: %s\n",
176 status = gensec_set_target_hostname(conn->gensec, conn->host);
177 if (!NT_STATUS_IS_OK(status)) {
178 DEBUG(1, ("Failed to set GENSEC target hostname: %s\n",
183 status = gensec_set_target_service(conn->gensec, "ldap");
184 if (!NT_STATUS_IS_OK(status)) {
185 DEBUG(1, ("Failed to set GENSEC target service: %s\n",
190 status = ildap_search(conn, "", LDAP_SEARCH_SCOPE_BASE, "", supported_sasl_mech_attrs,
191 False, NULL, NULL, &sasl_mechs_msgs);
192 if (!NT_STATUS_IS_OK(status)) {
193 DEBUG(1, ("Failed to inquire of target's available sasl mechs in rootdse search: %s\n",
198 count = ildap_count_entries(conn, sasl_mechs_msgs);
200 DEBUG(1, ("Failed to inquire of target's available sasl mechs in rootdse search: wrong number of replies: %d\n",
205 tmp_ctx = talloc_new(conn);
206 if (tmp_ctx == NULL) goto failed;
208 search = &sasl_mechs_msgs[0]->r.SearchResultEntry;
209 if (search->num_attributes != 1) {
210 DEBUG(1, ("Failed to inquire of target's available sasl mechs in rootdse search: wrong number of attributes: %d\n",
211 search->num_attributes));
215 sasl_names = talloc_array(tmp_ctx, const char *, search->attributes[0].num_values + 1);
217 DEBUG(1, ("talloc_arry(char *, %d) failed\n",
222 for (i=0; i<search->attributes[0].num_values; i++) {
223 sasl_names[i] = (const char *)search->attributes[0].values[i].data;
225 sasl_names[i] = NULL;
227 status = gensec_start_mech_by_sasl_list(conn->gensec, sasl_names);
228 if (!NT_STATUS_IS_OK(status)) {
229 DEBUG(1, ("None of the %d proposed SASL mechs were acceptable: %s\n",
230 count, nt_errstr(status)));
235 NTSTATUS gensec_status;
236 struct ldap_message *response;
237 struct ldap_message *msg;
238 struct ldap_request *req;
239 int result = LDAP_OTHER;
241 status = gensec_update(conn->gensec, tmp_ctx,
244 /* The status value here, from GENSEC is vital to the security
245 * of the system. Even if the other end accepts, if GENSEC
246 * claims 'MORE_PROCESSING_REQUIRED' then you must keep
247 * feeding it blobs, or else the remote host/attacker might
248 * avoid mutal authentication requirements.
250 * Likewise, you must not feed GENSEC too much (after the OK),
251 * it doesn't like that either
254 gensec_status = status;
256 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
257 !NT_STATUS_IS_OK(status)) {
260 if (NT_STATUS_IS_OK(status) && output.length == 0) {
264 /* Perhaps we should make gensec_start_mech_by_sasl_list() return the name we got? */
265 msg = new_ldap_sasl_bind_msg(tmp_ctx, conn->gensec->ops->sasl_name, &output);
267 status = NT_STATUS_NO_MEMORY;
271 req = ldap_request_send(conn, msg);
273 status = NT_STATUS_NO_MEMORY;
276 talloc_steal(tmp_ctx, req);
278 status = ldap_result_n(req, 0, &response);
279 if (!NT_STATUS_IS_OK(status)) {
283 if (response->type != LDAP_TAG_BindResponse) {
284 status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
288 result = response->r.BindResponse.response.resultcode;
290 if (result != LDAP_SUCCESS && result != LDAP_SASL_BIND_IN_PROGRESS) {
291 status = ldap_check_response(conn,
292 &response->r.BindResponse.response);
296 /* This is where we check if GENSEC wanted to be fed more data */
297 if (!NT_STATUS_EQUAL(gensec_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
300 input = response->r.BindResponse.SASL.secblob;
303 if (NT_STATUS_IS_OK(status) &&
304 (gensec_have_feature(conn->gensec, GENSEC_FEATURE_SEAL) ||
305 gensec_have_feature(conn->gensec, GENSEC_FEATURE_SIGN))) {
306 conn->enable_wrap = True;
309 talloc_free(tmp_ctx);
313 talloc_free(tmp_ctx);
314 talloc_free(conn->gensec);