python/samba: Another object.next() to next(object) py2/py3 converstion
[Samba.git] / source3 / lib / tldap_gensec_bind.c
blobc4092132d898a50414898a3869683c6b278ee0aa
1 /*
2 * Unix SMB/CIFS implementation.
3 * Gensec based tldap auth
4 * Copyright (C) Volker Lendecke 2015
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 3 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, see <http://www.gnu.org/licenses/>.
20 #include "tldap_gensec_bind.h"
21 #include "tldap_util.h"
22 #include "lib/util/tevent_unix.h"
23 #include "lib/util/talloc_stack.h"
24 #include "lib/util/samba_util.h"
25 #include "lib/util/debug.h"
26 #include "auth/gensec/gensec.h"
27 #include "auth/gensec/gensec_internal.h" /* TODO: remove this */
28 #include "lib/param/param.h"
29 #include "source4/auth/gensec/gensec_tstream.h"
31 struct tldap_gensec_bind_state {
32 struct tevent_context *ev;
33 struct tldap_context *ctx;
34 struct cli_credentials *creds;
35 const char *target_service;
36 const char *target_hostname;
37 const char *target_principal;
38 struct loadparm_context *lp_ctx;
39 uint32_t gensec_features;
41 bool first;
42 struct gensec_security *gensec;
43 NTSTATUS gensec_status;
44 DATA_BLOB gensec_output;
47 static void tldap_gensec_bind_got_mechs(struct tevent_req *subreq);
48 static void tldap_gensec_update_done(struct tldap_gensec_bind_state *state,
49 struct tevent_req *subreq);
50 static void tldap_gensec_bind_done(struct tevent_req *subreq);
52 static struct tevent_req *tldap_gensec_bind_send(
53 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
54 struct tldap_context *ctx, struct cli_credentials *creds,
55 const char *target_service, const char *target_hostname,
56 const char *target_principal, struct loadparm_context *lp_ctx,
57 uint32_t gensec_features)
59 struct tevent_req *req, *subreq;
60 struct tldap_gensec_bind_state *state;
62 const char *attrs[] = { "supportedSASLMechanisms" };
64 req = tevent_req_create(mem_ctx, &state,
65 struct tldap_gensec_bind_state);
66 if (req == NULL) {
67 return NULL;
69 state->ev = ev;
70 state->ctx = ctx;
71 state->creds = creds;
72 state->target_service = target_service;
73 state->target_hostname = target_hostname;
74 state->target_principal = target_principal;
75 state->lp_ctx = lp_ctx;
76 state->gensec_features = gensec_features;
77 state->first = true;
79 subreq = tldap_search_all_send(
80 state, state->ev, state->ctx, "", TLDAP_SCOPE_BASE,
81 "(objectclass=*)", attrs, ARRAY_SIZE(attrs),
82 false, NULL, 0, NULL, 0, 0, 1 /* sizelimit */, 0);
83 if (tevent_req_nomem(subreq, req)) {
84 return tevent_req_post(req, ev);
86 tevent_req_set_callback(subreq, tldap_gensec_bind_got_mechs, req);
87 return req;
90 static void tldap_gensec_bind_got_mechs(struct tevent_req *subreq)
92 struct tevent_req *req = tevent_req_callback_data(
93 subreq, struct tevent_req);
94 struct tldap_gensec_bind_state *state = tevent_req_data(
95 req, struct tldap_gensec_bind_state);
96 struct tldap_message **msgs, *msg, *result;
97 struct tldap_attribute *attribs, *attrib;
98 int num_attribs;
99 size_t num_msgs;
100 TLDAPRC rc;
101 int i;
102 bool ok;
103 const char **sasl_mechs;
104 NTSTATUS status;
106 rc = tldap_search_all_recv(subreq, state, &msgs, &result);
107 TALLOC_FREE(subreq);
108 if (tevent_req_ldap_error(req, rc)) {
109 return;
113 * TODO: Inspect "Result"
116 num_msgs = talloc_array_length(msgs);
117 if (num_msgs != 1) {
118 DBG_DEBUG("num_msgs = %zu\n", num_msgs);
119 tevent_req_ldap_error(req, TLDAP_OPERATIONS_ERROR);
120 return;
122 msg = msgs[0];
124 ok = tldap_entry_attributes(msg, &attribs, &num_attribs);
125 if (!ok) {
126 DBG_DEBUG("tldap_entry_attributes failed\n");
127 tevent_req_ldap_error(req, TLDAP_OPERATIONS_ERROR);
128 return;
131 if (num_attribs != 1) {
132 DBG_DEBUG("num_attribs = %d\n", num_attribs);
133 tevent_req_ldap_error(req, TLDAP_OPERATIONS_ERROR);
134 return;
136 attrib = &attribs[0];
138 sasl_mechs = talloc_array(state, const char *, attrib->num_values+1);
139 if (tevent_req_nomem(sasl_mechs, req)) {
140 return;
143 for (i=0; i<attrib->num_values; i++) {
144 DATA_BLOB *v = &attrib->values[i];
145 size_t len;
147 ok = convert_string_talloc(sasl_mechs, CH_UTF8, CH_UNIX,
148 v->data, v->length,
149 &sasl_mechs[i], &len);
150 if (!ok) {
151 DBG_DEBUG("convert_string_talloc failed\n");
152 tevent_req_ldap_error(req, TLDAP_OPERATIONS_ERROR);
153 return;
156 sasl_mechs[attrib->num_values] = NULL;
158 gensec_init();
160 status = gensec_client_start(
161 state, &state->gensec,
162 lpcfg_gensec_settings(state, state->lp_ctx));
163 if (!NT_STATUS_IS_OK(status)) {
164 DBG_DEBUG("gensec_client_start failed: %s\n",
165 nt_errstr(status));
166 tevent_req_ldap_error(req, TLDAP_OPERATIONS_ERROR);
167 return;
170 status = gensec_set_credentials(state->gensec, state->creds);
171 if (!NT_STATUS_IS_OK(status)) {
172 DBG_DEBUG("gensec_set_credentials failed: %s\n",
173 nt_errstr(status));
174 tevent_req_ldap_error(req, TLDAP_OPERATIONS_ERROR);
175 return;
178 status = gensec_set_target_service(state->gensec,
179 state->target_service);
180 if (!NT_STATUS_IS_OK(status)) {
181 DBG_DEBUG("gensec_set_target_service failed: %s\n",
182 nt_errstr(status));
183 tevent_req_ldap_error(req, TLDAP_OPERATIONS_ERROR);
184 return;
187 if (state->target_hostname != NULL) {
188 status = gensec_set_target_hostname(state->gensec,
189 state->target_hostname);
190 if (!NT_STATUS_IS_OK(status)) {
191 DBG_DEBUG("gensec_set_target_hostname failed: %s\n",
192 nt_errstr(status));
193 tevent_req_ldap_error(req, TLDAP_OPERATIONS_ERROR);
194 return;
198 if (state->target_principal != NULL) {
199 status = gensec_set_target_principal(state->gensec,
200 state->target_principal);
201 if (!NT_STATUS_IS_OK(status)) {
202 DBG_DEBUG("gensec_set_target_principal failed: %s\n",
203 nt_errstr(status));
204 tevent_req_ldap_error(req, TLDAP_OPERATIONS_ERROR);
205 return;
209 gensec_want_feature(state->gensec, state->gensec_features);
211 status = gensec_start_mech_by_sasl_list(state->gensec, sasl_mechs);
212 if (!NT_STATUS_IS_OK(status)) {
213 DBG_DEBUG("gensec_start_mech_by_sasl_list failed: %s\n",
214 nt_errstr(status));
215 tevent_req_ldap_error(req, TLDAP_OPERATIONS_ERROR);
216 return;
219 state->gensec_status = gensec_update(state->gensec, state,
220 data_blob_null,
221 &state->gensec_output);
222 tldap_gensec_update_done(state, req);
225 static void tldap_gensec_update_done(struct tldap_gensec_bind_state *state,
226 struct tevent_req *req)
228 struct tevent_req *subreq;
230 if (!NT_STATUS_IS_OK(state->gensec_status) &&
231 !NT_STATUS_EQUAL(state->gensec_status,
232 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
233 DBG_DEBUG("gensec_update failed: %s\n",
234 nt_errstr(state->gensec_status));
235 tevent_req_ldap_error(req, TLDAP_INVALID_CREDENTIALS);
236 return;
239 if (NT_STATUS_IS_OK(state->gensec_status) &&
240 (state->gensec_output.length == 0)) {
242 if (state->first) {
243 tevent_req_ldap_error(req, TLDAP_INVALID_CREDENTIALS);
244 } else {
245 tevent_req_done(req);
247 return;
250 state->first = false;
252 subreq = tldap_sasl_bind_send(
253 state, state->ev, state->ctx, "",
254 state->gensec->ops->sasl_name, &state->gensec_output,
255 NULL, 0, NULL, 0);
256 if (tevent_req_nomem(subreq, req)) {
257 return;
259 tevent_req_set_callback(subreq, tldap_gensec_bind_done, req);
262 static void tldap_gensec_bind_done(struct tevent_req *subreq)
264 struct tevent_req *req = tevent_req_callback_data(
265 subreq, struct tevent_req);
266 struct tldap_gensec_bind_state *state = tevent_req_data(
267 req, struct tldap_gensec_bind_state);
268 DATA_BLOB input;
269 TLDAPRC rc;
271 rc = tldap_sasl_bind_recv(subreq, state, &input);
272 TALLOC_FREE(subreq);
273 if (!TLDAP_RC_IS_SUCCESS(rc) &&
274 !TLDAP_RC_EQUAL(rc, TLDAP_SASL_BIND_IN_PROGRESS)) {
275 tevent_req_ldap_error(req, rc);
276 return;
279 if (TLDAP_RC_IS_SUCCESS(rc) && NT_STATUS_IS_OK(state->gensec_status)) {
280 tevent_req_done(req);
281 return;
284 state->gensec_status = gensec_update(state->gensec, state,
285 input,
286 &state->gensec_output);
287 tldap_gensec_update_done(state, req);
290 static TLDAPRC tldap_gensec_bind_recv(struct tevent_req *req)
292 struct tldap_gensec_bind_state *state = tevent_req_data(
293 req, struct tldap_gensec_bind_state);
294 struct tstream_context *plain, *sec;
295 NTSTATUS status;
296 TLDAPRC rc;
298 if (tevent_req_is_ldap_error(req, &rc)) {
299 return rc;
302 if ((state->gensec_features & GENSEC_FEATURE_SIGN) &&
303 !gensec_have_feature(state->gensec, GENSEC_FEATURE_SIGN)) {
304 return TLDAP_OPERATIONS_ERROR;
306 if ((state->gensec_features & GENSEC_FEATURE_SEAL) &&
307 !gensec_have_feature(state->gensec, GENSEC_FEATURE_SEAL)) {
308 return TLDAP_OPERATIONS_ERROR;
311 if (!gensec_have_feature(state->gensec, GENSEC_FEATURE_SIGN) &&
312 !gensec_have_feature(state->gensec, GENSEC_FEATURE_SEAL)) {
313 return TLDAP_SUCCESS;
317 * The gensec ctx needs to survive as long as the ldap context
318 * lives
320 talloc_steal(state->ctx, state->gensec);
322 plain = tldap_get_tstream(state->ctx);
324 status = gensec_create_tstream(state->ctx, state->gensec,
325 plain, &sec);
326 if (!NT_STATUS_IS_OK(status)) {
327 DBG_DEBUG("gensec_create_tstream failed: %s\n",
328 nt_errstr(status));
329 return TLDAP_OPERATIONS_ERROR;
332 tldap_set_tstream(state->ctx, sec);
334 return TLDAP_SUCCESS;
337 TLDAPRC tldap_gensec_bind(
338 struct tldap_context *ctx, struct cli_credentials *creds,
339 const char *target_service, const char *target_hostname,
340 const char *target_principal, struct loadparm_context *lp_ctx,
341 uint32_t gensec_features)
343 TALLOC_CTX *frame = talloc_stackframe();
344 struct tevent_context *ev;
345 struct tevent_req *req;
346 TLDAPRC rc = TLDAP_NO_MEMORY;
348 ev = samba_tevent_context_init(frame);
349 if (ev == NULL) {
350 goto fail;
352 req = tldap_gensec_bind_send(frame, ev, ctx, creds, target_service,
353 target_hostname, target_principal, lp_ctx,
354 gensec_features);
355 if (req == NULL) {
356 goto fail;
358 if (!tevent_req_poll(req, ev)) {
359 rc = TLDAP_OPERATIONS_ERROR;
360 goto fail;
362 rc = tldap_gensec_bind_recv(req);
363 fail:
364 TALLOC_FREE(frame);
365 return rc;