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 3 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, see <http://www.gnu.org/licenses/>.
25 #include "libcli/ldap/ldap.h"
26 #include "libcli/ldap/ldap_client.h"
27 #include "lib/tls/tls.h"
28 #include "auth/gensec/gensec.h"
29 #include "auth/credentials/credentials.h"
30 #include "lib/stream/packet.h"
31 #include "param/param.h"
33 struct ldap_simple_creds {
38 NTSTATUS ldap_rebind(struct ldap_connection *conn)
41 struct ldap_simple_creds *creds;
43 switch (conn->bind.type) {
45 status = ldap_bind_sasl(conn, (struct cli_credentials *)conn->bind.creds,
49 case LDAP_BIND_SIMPLE:
50 creds = (struct ldap_simple_creds *)conn->bind.creds;
53 return NT_STATUS_UNSUCCESSFUL;
56 status = ldap_bind_simple(conn, creds->dn, creds->pw);
60 return NT_STATUS_UNSUCCESSFUL;
67 static struct ldap_message *new_ldap_simple_bind_msg(struct ldap_connection *conn,
68 const char *dn, const char *pw)
70 struct ldap_message *res;
72 res = new_ldap_message(conn);
77 res->type = LDAP_TAG_BindRequest;
78 res->r.BindRequest.version = 3;
79 res->r.BindRequest.dn = talloc_strdup(res, dn);
80 res->r.BindRequest.mechanism = LDAP_AUTH_MECH_SIMPLE;
81 res->r.BindRequest.creds.password = talloc_strdup(res, pw);
89 perform a simple username/password bind
91 NTSTATUS ldap_bind_simple(struct ldap_connection *conn,
92 const char *userdn, const char *password)
94 struct ldap_request *req;
95 struct ldap_message *msg;
100 return NT_STATUS_INVALID_CONNECTION;
116 if (conn->simple_pw) {
117 pw = conn->simple_pw;
123 msg = new_ldap_simple_bind_msg(conn, dn, pw);
124 NT_STATUS_HAVE_NO_MEMORY(msg);
126 /* send the request */
127 req = ldap_request_send(conn, msg);
129 NT_STATUS_HAVE_NO_MEMORY(req);
131 /* wait for replies */
132 status = ldap_request_wait(req);
133 if (!NT_STATUS_IS_OK(status)) {
138 /* check its a valid reply */
139 msg = req->replies[0];
140 if (msg->type != LDAP_TAG_BindResponse) {
142 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
145 status = ldap_check_response(conn, &msg->r.BindResponse.response);
149 if (NT_STATUS_IS_OK(status)) {
150 struct ldap_simple_creds *creds = talloc(conn, struct ldap_simple_creds);
152 return NT_STATUS_NO_MEMORY;
154 creds->dn = talloc_strdup(creds, dn);
155 creds->pw = talloc_strdup(creds, pw);
156 if (creds->dn == NULL || creds->pw == NULL) {
157 return NT_STATUS_NO_MEMORY;
159 conn->bind.type = LDAP_BIND_SIMPLE;
160 conn->bind.creds = creds;
167 static struct ldap_message *new_ldap_sasl_bind_msg(struct ldap_connection *conn,
168 const char *sasl_mechanism,
171 struct ldap_message *res;
173 res = new_ldap_message(conn);
178 res->type = LDAP_TAG_BindRequest;
179 res->r.BindRequest.version = 3;
180 res->r.BindRequest.dn = "";
181 res->r.BindRequest.mechanism = LDAP_AUTH_MECH_SASL;
182 res->r.BindRequest.creds.SASL.mechanism = talloc_strdup(res, sasl_mechanism);
184 res->r.BindRequest.creds.SASL.secblob = talloc(res, DATA_BLOB);
185 if (!res->r.BindRequest.creds.SASL.secblob) {
189 *res->r.BindRequest.creds.SASL.secblob = *secblob;
191 res->r.BindRequest.creds.SASL.secblob = NULL;
193 res->controls = NULL;
200 perform a sasl bind using the given credentials
202 NTSTATUS ldap_bind_sasl(struct ldap_connection *conn,
203 struct cli_credentials *creds,
204 struct loadparm_context *lp_ctx)
207 TALLOC_CTX *tmp_ctx = NULL;
209 DATA_BLOB input = data_blob(NULL, 0);
210 DATA_BLOB output = data_blob(NULL, 0);
212 struct ldap_message **sasl_mechs_msgs;
213 struct ldap_SearchResEntry *search;
216 const char **sasl_names;
217 uint32_t old_gensec_features;
218 static const char *supported_sasl_mech_attrs[] = {
219 "supportedSASLMechanisms",
225 status = gensec_client_start(conn, &conn->gensec, NULL, lp_ctx);
226 if (!NT_STATUS_IS_OK(status)) {
227 DEBUG(0, ("Failed to start GENSEC engine (%s)\n", nt_errstr(status)));
231 /* require Kerberos SIGN/SEAL only if we don't use SSL
232 * Windows seem not to like double encryption */
233 old_gensec_features = cli_credentials_get_gensec_features(creds);
234 if (tls_enabled(conn->sock)) {
235 cli_credentials_set_gensec_features(creds, 0);
238 /* this call also sets the gensec_want_features */
239 status = gensec_set_credentials(conn->gensec, creds);
240 if (!NT_STATUS_IS_OK(status)) {
241 DEBUG(1, ("Failed to set GENSEC creds: %s\n",
246 /* reset the original gensec_features */
247 cli_credentials_set_gensec_features(creds, old_gensec_features);
250 status = gensec_set_target_hostname(conn->gensec, conn->host);
251 if (!NT_STATUS_IS_OK(status)) {
252 DEBUG(1, ("Failed to set GENSEC target hostname: %s\n",
258 status = gensec_set_target_service(conn->gensec, "ldap");
259 if (!NT_STATUS_IS_OK(status)) {
260 DEBUG(1, ("Failed to set GENSEC target service: %s\n",
265 status = ildap_search(conn, "", LDAP_SEARCH_SCOPE_BASE, "", supported_sasl_mech_attrs,
266 false, NULL, NULL, &sasl_mechs_msgs);
267 if (!NT_STATUS_IS_OK(status)) {
268 DEBUG(1, ("Failed to inquire of target's available sasl mechs in rootdse search: %s\n",
273 count = ildap_count_entries(conn, sasl_mechs_msgs);
275 DEBUG(1, ("Failed to inquire of target's available sasl mechs in rootdse search: wrong number of replies: %d\n",
280 tmp_ctx = talloc_new(conn);
281 if (tmp_ctx == NULL) goto failed;
283 search = &sasl_mechs_msgs[0]->r.SearchResultEntry;
284 if (search->num_attributes != 1) {
285 DEBUG(1, ("Failed to inquire of target's available sasl mechs in rootdse search: wrong number of attributes: %d\n",
286 search->num_attributes));
290 sasl_names = talloc_array(tmp_ctx, const char *, search->attributes[0].num_values + 1);
292 DEBUG(1, ("talloc_arry(char *, %d) failed\n",
297 for (i=0; i<search->attributes[0].num_values; i++) {
298 sasl_names[i] = (const char *)search->attributes[0].values[i].data;
300 sasl_names[i] = NULL;
302 status = gensec_start_mech_by_sasl_list(conn->gensec, sasl_names);
303 if (!NT_STATUS_IS_OK(status)) {
304 DEBUG(1, ("None of the %d proposed SASL mechs were acceptable: %s\n",
305 count, nt_errstr(status)));
310 NTSTATUS gensec_status;
311 struct ldap_message *response;
312 struct ldap_message *msg;
313 struct ldap_request *req;
314 int result = LDAP_OTHER;
316 status = gensec_update(conn->gensec, tmp_ctx,
319 /* The status value here, from GENSEC is vital to the security
320 * of the system. Even if the other end accepts, if GENSEC
321 * claims 'MORE_PROCESSING_REQUIRED' then you must keep
322 * feeding it blobs, or else the remote host/attacker might
323 * avoid mutal authentication requirements.
325 * Likewise, you must not feed GENSEC too much (after the OK),
326 * it doesn't like that either
329 gensec_status = status;
331 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
332 !NT_STATUS_IS_OK(status)) {
335 if (NT_STATUS_IS_OK(status) && output.length == 0) {
339 /* Perhaps we should make gensec_start_mech_by_sasl_list() return the name we got? */
340 msg = new_ldap_sasl_bind_msg(tmp_ctx, conn->gensec->ops->sasl_name, (output.data?&output:NULL));
342 status = NT_STATUS_NO_MEMORY;
346 req = ldap_request_send(conn, msg);
348 status = NT_STATUS_NO_MEMORY;
351 talloc_steal(tmp_ctx, req);
353 status = ldap_result_n(req, 0, &response);
354 if (!NT_STATUS_IS_OK(status)) {
358 if (response->type != LDAP_TAG_BindResponse) {
359 status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
363 result = response->r.BindResponse.response.resultcode;
365 if (result != LDAP_SUCCESS && result != LDAP_SASL_BIND_IN_PROGRESS) {
366 status = ldap_check_response(conn,
367 &response->r.BindResponse.response);
371 /* This is where we check if GENSEC wanted to be fed more data */
372 if (!NT_STATUS_EQUAL(gensec_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
375 if (response->r.BindResponse.SASL.secblob) {
376 input = *response->r.BindResponse.SASL.secblob;
378 input = data_blob(NULL, 0);
382 talloc_free(tmp_ctx);
384 if (NT_STATUS_IS_OK(status)) {
385 struct socket_context *sasl_socket;
386 status = gensec_socket_init(conn->gensec,
388 conn->event.event_ctx,
389 ldap_read_io_handler,
392 if (!NT_STATUS_IS_OK(status)) goto failed;
394 talloc_steal(conn->sock, sasl_socket);
395 talloc_unlink(conn, conn->sock);
396 conn->sock = sasl_socket;
397 packet_set_socket(conn->packet, conn->sock);
399 conn->bind.type = LDAP_BIND_SASL;
400 conn->bind.creds = creds;
406 talloc_free(tmp_ctx);
407 talloc_free(conn->gensec);