summaryrefslogtreecommitdiff
path: root/source3/lib/tldap_gensec_bind.c
blob: c4092132d898a50414898a3869683c6b278ee0aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
 * Unix SMB/CIFS implementation.
 * Gensec based tldap auth
 * Copyright (C) Volker Lendecke 2015
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "tldap_gensec_bind.h"
#include "tldap_util.h"
#include "lib/util/tevent_unix.h"
#include "lib/util/talloc_stack.h"
#include "lib/util/samba_util.h"
#include "lib/util/debug.h"
#include "auth/gensec/gensec.h"
#include "auth/gensec/gensec_internal.h" /* TODO: remove this */
#include "lib/param/param.h"
#include "source4/auth/gensec/gensec_tstream.h"

struct tldap_gensec_bind_state {
	struct tevent_context *ev;
	struct tldap_context *ctx;
	struct cli_credentials *creds;
	const char *target_service;
	const char *target_hostname;
	const char *target_principal;
	struct loadparm_context *lp_ctx;
	uint32_t gensec_features;

	bool first;
	struct gensec_security *gensec;
	NTSTATUS gensec_status;
	DATA_BLOB gensec_output;
};

static void tldap_gensec_bind_got_mechs(struct tevent_req *subreq);
static void tldap_gensec_update_done(struct tldap_gensec_bind_state *state,
				struct tevent_req *subreq);
static void tldap_gensec_bind_done(struct tevent_req *subreq);

static struct tevent_req *tldap_gensec_bind_send(
	TALLOC_CTX *mem_ctx, struct tevent_context *ev,
	struct tldap_context *ctx, struct cli_credentials *creds,
	const char *target_service, const char *target_hostname,
	const char *target_principal, struct loadparm_context *lp_ctx,
	uint32_t gensec_features)
{
	struct tevent_req *req, *subreq;
	struct tldap_gensec_bind_state *state;

	const char *attrs[] = { "supportedSASLMechanisms" };

	req = tevent_req_create(mem_ctx, &state,
				struct tldap_gensec_bind_state);
	if (req == NULL) {
		return NULL;
	}
	state->ev = ev;
	state->ctx = ctx;
	state->creds = creds;
	state->target_service = target_service;
	state->target_hostname = target_hostname;
	state->target_principal = target_principal;
	state->lp_ctx = lp_ctx;
	state->gensec_features = gensec_features;
	state->first = true;

	subreq = tldap_search_all_send(
		state, state->ev, state->ctx, "", TLDAP_SCOPE_BASE,
		"(objectclass=*)", attrs, ARRAY_SIZE(attrs),
		false, NULL, 0, NULL, 0, 0, 1 /* sizelimit */, 0);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, tldap_gensec_bind_got_mechs, req);
	return req;
}

static void tldap_gensec_bind_got_mechs(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct tldap_gensec_bind_state *state = tevent_req_data(
		req, struct tldap_gensec_bind_state);
	struct tldap_message **msgs, *msg, *result;
	struct tldap_attribute *attribs, *attrib;
	int num_attribs;
	size_t num_msgs;
	TLDAPRC rc;
	int i;
	bool ok;
	const char **sasl_mechs;
	NTSTATUS status;

	rc = tldap_search_all_recv(subreq, state, &msgs, &result);
	TALLOC_FREE(subreq);
	if (tevent_req_ldap_error(req, rc)) {
		return;
	}

	/*
	 * TODO: Inspect "Result"
	 */

	num_msgs = talloc_array_length(msgs);
	if (num_msgs != 1) {
		DBG_DEBUG("num_msgs = %zu\n", num_msgs);
		tevent_req_ldap_error(req, TLDAP_OPERATIONS_ERROR);
		return;
	}
	msg = msgs[0];

	ok = tldap_entry_attributes(msg, &attribs, &num_attribs);
	if (!ok) {
		DBG_DEBUG("tldap_entry_attributes failed\n");
		tevent_req_ldap_error(req, TLDAP_OPERATIONS_ERROR);
		return;
	}

	if (num_attribs != 1) {
		DBG_DEBUG("num_attribs = %d\n", num_attribs);
		tevent_req_ldap_error(req, TLDAP_OPERATIONS_ERROR);
		return;
	}
	attrib = &attribs[0];

	sasl_mechs = talloc_array(state, const char *, attrib->num_values+1);
	if (tevent_req_nomem(sasl_mechs, req)) {
		return;
	}

	for (i=0; i<attrib->num_values; i++) {
		DATA_BLOB *v = &attrib->values[i];
		size_t len;

		ok = convert_string_talloc(sasl_mechs, CH_UTF8, CH_UNIX,
					   v->data, v->length,
					   &sasl_mechs[i], &len);
		if (!ok) {
			DBG_DEBUG("convert_string_talloc failed\n");
			tevent_req_ldap_error(req, TLDAP_OPERATIONS_ERROR);
			return;
		}
	}
	sasl_mechs[attrib->num_values] = NULL;

	gensec_init();

	status = gensec_client_start(
		state, &state->gensec,
		lpcfg_gensec_settings(state, state->lp_ctx));
	if (!NT_STATUS_IS_OK(status)) {
		DBG_DEBUG("gensec_client_start failed: %s\n",
			  nt_errstr(status));
		tevent_req_ldap_error(req, TLDAP_OPERATIONS_ERROR);
		return;
	}

	status = gensec_set_credentials(state->gensec, state->creds);
	if (!NT_STATUS_IS_OK(status)) {
		DBG_DEBUG("gensec_set_credentials failed: %s\n",
			  nt_errstr(status));
		tevent_req_ldap_error(req, TLDAP_OPERATIONS_ERROR);
		return;
	}

	status = gensec_set_target_service(state->gensec,
					   state->target_service);
	if (!NT_STATUS_IS_OK(status)) {
		DBG_DEBUG("gensec_set_target_service failed: %s\n",
			  nt_errstr(status));
		tevent_req_ldap_error(req, TLDAP_OPERATIONS_ERROR);
		return;
	}

	if (state->target_hostname != NULL) {
		status = gensec_set_target_hostname(state->gensec,
						    state->target_hostname);
		if (!NT_STATUS_IS_OK(status)) {
			DBG_DEBUG("gensec_set_target_hostname failed: %s\n",
				  nt_errstr(status));
			tevent_req_ldap_error(req, TLDAP_OPERATIONS_ERROR);
			return;
		}
	}

	if (state->target_principal != NULL) {
		status = gensec_set_target_principal(state->gensec,
						     state->target_principal);
		if (!NT_STATUS_IS_OK(status)) {
			DBG_DEBUG("gensec_set_target_principal failed: %s\n",
				  nt_errstr(status));
			tevent_req_ldap_error(req, TLDAP_OPERATIONS_ERROR);
			return;
		}
	}

	gensec_want_feature(state->gensec, state->gensec_features);

	status = gensec_start_mech_by_sasl_list(state->gensec, sasl_mechs);
	if (!NT_STATUS_IS_OK(status)) {
		DBG_DEBUG("gensec_start_mech_by_sasl_list failed: %s\n",
			  nt_errstr(status));
		tevent_req_ldap_error(req, TLDAP_OPERATIONS_ERROR);
		return;
	}

	state->gensec_status = gensec_update(state->gensec, state,
						data_blob_null,
						&state->gensec_output);
	tldap_gensec_update_done(state, req);
}

static void tldap_gensec_update_done(struct tldap_gensec_bind_state *state,
					struct tevent_req *req)
{
	struct tevent_req *subreq;

	if (!NT_STATUS_IS_OK(state->gensec_status) &&
	    !NT_STATUS_EQUAL(state->gensec_status,
			     NT_STATUS_MORE_PROCESSING_REQUIRED)) {
		DBG_DEBUG("gensec_update failed: %s\n",
			  nt_errstr(state->gensec_status));
		tevent_req_ldap_error(req, TLDAP_INVALID_CREDENTIALS);
		return;
	}

	if (NT_STATUS_IS_OK(state->gensec_status) &&
	    (state->gensec_output.length == 0)) {

		if (state->first) {
			tevent_req_ldap_error(req, TLDAP_INVALID_CREDENTIALS);
		} else {
			tevent_req_done(req);
		}
		return;
	}

	state->first = false;

	subreq = tldap_sasl_bind_send(
		state, state->ev, state->ctx, "",
		state->gensec->ops->sasl_name, &state->gensec_output,
		NULL, 0, NULL, 0);
	if (tevent_req_nomem(subreq, req)) {
		return;
	}
	tevent_req_set_callback(subreq, tldap_gensec_bind_done, req);
}

static void tldap_gensec_bind_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct tldap_gensec_bind_state *state = tevent_req_data(
		req, struct tldap_gensec_bind_state);
	DATA_BLOB input;
	TLDAPRC rc;

	rc = tldap_sasl_bind_recv(subreq, state, &input);
	TALLOC_FREE(subreq);
	if (!TLDAP_RC_IS_SUCCESS(rc) &&
	    !TLDAP_RC_EQUAL(rc, TLDAP_SASL_BIND_IN_PROGRESS)) {
		tevent_req_ldap_error(req, rc);
		return;
	}

	if (TLDAP_RC_IS_SUCCESS(rc) && NT_STATUS_IS_OK(state->gensec_status)) {
		tevent_req_done(req);
		return;
	}

	state->gensec_status = gensec_update(state->gensec, state,
						input,
						&state->gensec_output);
	tldap_gensec_update_done(state, req);
}

static TLDAPRC tldap_gensec_bind_recv(struct tevent_req *req)
{
	struct tldap_gensec_bind_state *state = tevent_req_data(
		req, struct tldap_gensec_bind_state);
	struct tstream_context *plain, *sec;
	NTSTATUS status;
	TLDAPRC rc;

	if (tevent_req_is_ldap_error(req, &rc)) {
		return rc;
	}

	if ((state->gensec_features & GENSEC_FEATURE_SIGN) &&
	    !gensec_have_feature(state->gensec, GENSEC_FEATURE_SIGN)) {
		return TLDAP_OPERATIONS_ERROR;
	}
	if ((state->gensec_features & GENSEC_FEATURE_SEAL) &&
	    !gensec_have_feature(state->gensec, GENSEC_FEATURE_SEAL)) {
		return TLDAP_OPERATIONS_ERROR;
	}

	if (!gensec_have_feature(state->gensec, GENSEC_FEATURE_SIGN) &&
	    !gensec_have_feature(state->gensec, GENSEC_FEATURE_SEAL)) {
		return TLDAP_SUCCESS;
	}

	/*
	 * The gensec ctx needs to survive as long as the ldap context
	 * lives
	 */
	talloc_steal(state->ctx, state->gensec);

	plain = tldap_get_tstream(state->ctx);

	status = gensec_create_tstream(state->ctx, state->gensec,
				       plain, &sec);
	if (!NT_STATUS_IS_OK(status)) {
		DBG_DEBUG("gensec_create_tstream failed: %s\n",
			  nt_errstr(status));
		return TLDAP_OPERATIONS_ERROR;
	}

	tldap_set_tstream(state->ctx, sec);

	return TLDAP_SUCCESS;
}

TLDAPRC tldap_gensec_bind(
	struct tldap_context *ctx, struct cli_credentials *creds,
	const char *target_service, const char *target_hostname,
	const char *target_principal, struct loadparm_context *lp_ctx,
	uint32_t gensec_features)
{
	TALLOC_CTX *frame = talloc_stackframe();
	struct tevent_context *ev;
	struct tevent_req *req;
	TLDAPRC rc = TLDAP_NO_MEMORY;

	ev = samba_tevent_context_init(frame);
	if (ev == NULL) {
		goto fail;
	}
	req = tldap_gensec_bind_send(frame, ev, ctx, creds, target_service,
				     target_hostname, target_principal, lp_ctx,
				     gensec_features);
	if (req == NULL) {
		goto fail;
	}
	if (!tevent_req_poll(req, ev)) {
		rc = TLDAP_OPERATIONS_ERROR;
		goto fail;
	}
	rc = tldap_gensec_bind_recv(req);
 fail:
	TALLOC_FREE(frame);
	return rc;
}