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
;
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
);
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
;
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
);
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
;
103 const char **sasl_mechs
;
106 rc
= tldap_search_all_recv(subreq
, state
, &msgs
, &result
);
108 if (tevent_req_ldap_error(req
, rc
)) {
113 * TODO: Inspect "Result"
116 num_msgs
= talloc_array_length(msgs
);
118 DBG_DEBUG("num_msgs = %zu\n", num_msgs
);
119 tevent_req_ldap_error(req
, TLDAP_OPERATIONS_ERROR
);
124 ok
= tldap_entry_attributes(msg
, &attribs
, &num_attribs
);
126 DBG_DEBUG("tldap_entry_attributes failed\n");
127 tevent_req_ldap_error(req
, TLDAP_OPERATIONS_ERROR
);
131 if (num_attribs
!= 1) {
132 DBG_DEBUG("num_attribs = %d\n", num_attribs
);
133 tevent_req_ldap_error(req
, TLDAP_OPERATIONS_ERROR
);
136 attrib
= &attribs
[0];
138 sasl_mechs
= talloc_array(state
, const char *, attrib
->num_values
+1);
139 if (tevent_req_nomem(sasl_mechs
, req
)) {
143 for (i
=0; i
<attrib
->num_values
; i
++) {
144 DATA_BLOB
*v
= &attrib
->values
[i
];
147 ok
= convert_string_talloc(sasl_mechs
, CH_UTF8
, CH_UNIX
,
149 &sasl_mechs
[i
], &len
);
151 DBG_DEBUG("convert_string_talloc failed\n");
152 tevent_req_ldap_error(req
, TLDAP_OPERATIONS_ERROR
);
156 sasl_mechs
[attrib
->num_values
] = NULL
;
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",
166 tevent_req_ldap_error(req
, TLDAP_OPERATIONS_ERROR
);
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",
174 tevent_req_ldap_error(req
, TLDAP_OPERATIONS_ERROR
);
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",
183 tevent_req_ldap_error(req
, TLDAP_OPERATIONS_ERROR
);
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",
193 tevent_req_ldap_error(req
, TLDAP_OPERATIONS_ERROR
);
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",
204 tevent_req_ldap_error(req
, TLDAP_OPERATIONS_ERROR
);
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",
215 tevent_req_ldap_error(req
, TLDAP_OPERATIONS_ERROR
);
219 state
->gensec_status
= gensec_update(state
->gensec
, state
,
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
);
239 if (NT_STATUS_IS_OK(state
->gensec_status
) &&
240 (state
->gensec_output
.length
== 0)) {
243 tevent_req_ldap_error(req
, TLDAP_INVALID_CREDENTIALS
);
245 tevent_req_done(req
);
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
,
256 if (tevent_req_nomem(subreq
, req
)) {
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
);
271 rc
= tldap_sasl_bind_recv(subreq
, state
, &input
);
273 if (!TLDAP_RC_IS_SUCCESS(rc
) &&
274 !TLDAP_RC_EQUAL(rc
, TLDAP_SASL_BIND_IN_PROGRESS
)) {
275 tevent_req_ldap_error(req
, rc
);
279 if (TLDAP_RC_IS_SUCCESS(rc
) && NT_STATUS_IS_OK(state
->gensec_status
)) {
280 tevent_req_done(req
);
284 state
->gensec_status
= gensec_update(state
->gensec
, state
,
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
;
298 if (tevent_req_is_ldap_error(req
, &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
320 talloc_steal(state
->ctx
, state
->gensec
);
322 plain
= tldap_get_tstream(state
->ctx
);
324 status
= gensec_create_tstream(state
->ctx
, state
->gensec
,
326 if (!NT_STATUS_IS_OK(status
)) {
327 DBG_DEBUG("gensec_create_tstream failed: %s\n",
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
);
352 req
= tldap_gensec_bind_send(frame
, ev
, ctx
, creds
, target_service
,
353 target_hostname
, target_principal
, lp_ctx
,
358 if (!tevent_req_poll(req
, ev
)) {
359 rc
= TLDAP_OPERATIONS_ERROR
;
362 rc
= tldap_gensec_bind_recv(req
);