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/>.
22 #include "tldap_gensec_bind.h"
23 #include "auth/credentials/credentials.h"
24 #include "lib/util/tevent_unix.h"
25 #include "lib/util/talloc_stack.h"
26 #include "lib/util/samba_util.h"
27 #include "lib/util/debug.h"
28 #include "auth/gensec/gensec.h"
29 #include "lib/param/param.h"
30 #include "source4/auth/gensec/gensec_tstream.h"
32 struct tldap_gensec_bind_state
{
33 struct tevent_context
*ev
;
34 struct tldap_context
*ctx
;
35 struct cli_credentials
*creds
;
36 const char *target_service
;
37 const char *target_hostname
;
38 const char *target_principal
;
39 struct loadparm_context
*lp_ctx
;
40 uint32_t gensec_features
;
43 struct gensec_security
*gensec
;
44 NTSTATUS gensec_status
;
45 DATA_BLOB gensec_input
;
46 DATA_BLOB gensec_output
;
49 static void tldap_gensec_update_next(struct tevent_req
*req
);
50 static void tldap_gensec_update_done(struct tevent_req
*subreq
);
51 static void tldap_gensec_bind_done(struct tevent_req
*subreq
);
53 struct tevent_req
*tldap_gensec_bind_send(
54 TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
55 struct tldap_context
*ctx
, struct cli_credentials
*creds
,
56 const char *target_service
, const char *target_hostname
,
57 const char *target_principal
, struct loadparm_context
*lp_ctx
,
58 uint32_t gensec_features
)
60 struct tevent_req
*req
= NULL
;
61 struct tldap_gensec_bind_state
*state
= NULL
;
62 const DATA_BLOB
*tls_cb
= NULL
;
65 req
= tevent_req_create(mem_ctx
, &state
,
66 struct tldap_gensec_bind_state
);
73 state
->target_service
= target_service
;
74 state
->target_hostname
= target_hostname
;
75 state
->target_principal
= target_principal
;
76 state
->lp_ctx
= lp_ctx
;
77 state
->gensec_features
= gensec_features
;
82 status
= gensec_client_start(
83 state
, &state
->gensec
,
84 lpcfg_gensec_settings(state
, state
->lp_ctx
));
85 if (!NT_STATUS_IS_OK(status
)) {
86 DBG_DEBUG("gensec_client_start failed: %s\n",
88 tevent_req_ldap_error(req
, TLDAP_OPERATIONS_ERROR
);
89 return tevent_req_post(req
, ev
);
92 status
= gensec_set_credentials(state
->gensec
, state
->creds
);
93 if (!NT_STATUS_IS_OK(status
)) {
94 DBG_DEBUG("gensec_set_credentials failed: %s\n",
96 tevent_req_ldap_error(req
, TLDAP_OPERATIONS_ERROR
);
97 return tevent_req_post(req
, ev
);
100 status
= gensec_set_target_service(state
->gensec
,
101 state
->target_service
);
102 if (!NT_STATUS_IS_OK(status
)) {
103 DBG_DEBUG("gensec_set_target_service failed: %s\n",
105 tevent_req_ldap_error(req
, TLDAP_OPERATIONS_ERROR
);
106 return tevent_req_post(req
, ev
);
109 if (state
->target_hostname
!= NULL
) {
110 status
= gensec_set_target_hostname(state
->gensec
,
111 state
->target_hostname
);
112 if (!NT_STATUS_IS_OK(status
)) {
113 DBG_DEBUG("gensec_set_target_hostname failed: %s\n",
115 tevent_req_ldap_error(req
, TLDAP_OPERATIONS_ERROR
);
116 return tevent_req_post(req
, ev
);
120 if (state
->target_principal
!= NULL
) {
121 status
= gensec_set_target_principal(state
->gensec
,
122 state
->target_principal
);
123 if (!NT_STATUS_IS_OK(status
)) {
124 DBG_DEBUG("gensec_set_target_principal failed: %s\n",
126 tevent_req_ldap_error(req
, TLDAP_OPERATIONS_ERROR
);
127 return tevent_req_post(req
, ev
);
131 if (tldap_has_tls_tstream(state
->ctx
)) {
132 if (gensec_features
& (GENSEC_FEATURE_SIGN
|GENSEC_FEATURE_SEAL
)) {
133 DBG_WARNING("sign or seal not allowed over tls\n");
134 tevent_req_ldap_error(req
, TLDAP_OPERATIONS_ERROR
);
135 return tevent_req_post(req
, ev
);
138 tls_cb
= tldap_tls_channel_bindings(state
->ctx
);
141 if (tls_cb
!= NULL
) {
142 uint32_t initiator_addrtype
= 0;
143 const DATA_BLOB
*initiator_address
= NULL
;
144 uint32_t acceptor_addrtype
= 0;
145 const DATA_BLOB
*acceptor_address
= NULL
;
146 const DATA_BLOB
*application_data
= tls_cb
;
148 status
= gensec_set_channel_bindings(state
->gensec
,
154 if (!NT_STATUS_IS_OK(status
)) {
155 DBG_DEBUG("gensec_set_channel_bindings: %s\n",
157 tevent_req_ldap_error(req
, TLDAP_OPERATIONS_ERROR
);
158 return tevent_req_post(req
, ev
);
162 gensec_want_feature(state
->gensec
, state
->gensec_features
);
164 status
= gensec_start_mech_by_sasl_name(state
->gensec
, "GSS-SPNEGO");
165 if (!NT_STATUS_IS_OK(status
)) {
166 DBG_ERR("gensec_start_mech_by_sasl_name(GSS-SPNEGO) failed: %s\n",
168 tevent_req_ldap_error(req
, TLDAP_OPERATIONS_ERROR
);
169 return tevent_req_post(req
, ev
);
172 tldap_gensec_update_next(req
);
173 if (!tevent_req_is_in_progress(req
)) {
174 return tevent_req_post(req
, ev
);
180 static void tldap_gensec_update_next(struct tevent_req
*req
)
182 struct tldap_gensec_bind_state
*state
= tevent_req_data(
183 req
, struct tldap_gensec_bind_state
);
184 struct tevent_req
*subreq
= NULL
;
186 subreq
= gensec_update_send(state
,
189 state
->gensec_input
);
190 if (tevent_req_nomem(subreq
, req
)) {
193 tevent_req_set_callback(subreq
,
194 tldap_gensec_update_done
,
198 static void tldap_gensec_update_done(struct tevent_req
*subreq
)
200 struct tevent_req
*req
= tevent_req_callback_data(
201 subreq
, struct tevent_req
);
202 struct tldap_gensec_bind_state
*state
= tevent_req_data(
203 req
, struct tldap_gensec_bind_state
);
205 state
->gensec_status
= gensec_update_recv(subreq
,
207 &state
->gensec_output
);
209 data_blob_free(&state
->gensec_input
);
210 if (!NT_STATUS_IS_OK(state
->gensec_status
) &&
211 !NT_STATUS_EQUAL(state
->gensec_status
,
212 NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
213 DBG_DEBUG("gensec_update failed: %s\n",
214 nt_errstr(state
->gensec_status
));
215 tevent_req_ldap_error(req
, TLDAP_INVALID_CREDENTIALS
);
219 if (NT_STATUS_IS_OK(state
->gensec_status
) &&
220 (state
->gensec_output
.length
== 0)) {
223 tevent_req_ldap_error(req
, TLDAP_INVALID_CREDENTIALS
);
225 tevent_req_done(req
);
230 state
->first
= false;
232 subreq
= tldap_sasl_bind_send(state
,
237 &state
->gensec_output
,
242 if (tevent_req_nomem(subreq
, req
)) {
245 tevent_req_set_callback(subreq
, tldap_gensec_bind_done
, req
);
248 static void tldap_gensec_bind_done(struct tevent_req
*subreq
)
250 struct tevent_req
*req
= tevent_req_callback_data(
251 subreq
, struct tevent_req
);
252 struct tldap_gensec_bind_state
*state
= tevent_req_data(
253 req
, struct tldap_gensec_bind_state
);
256 rc
= tldap_sasl_bind_recv(subreq
, state
, &state
->gensec_input
);
258 data_blob_free(&state
->gensec_output
);
259 if (!TLDAP_RC_IS_SUCCESS(rc
) &&
260 !TLDAP_RC_EQUAL(rc
, TLDAP_SASL_BIND_IN_PROGRESS
)) {
261 tevent_req_ldap_error(req
, rc
);
265 if (TLDAP_RC_IS_SUCCESS(rc
) && NT_STATUS_IS_OK(state
->gensec_status
)) {
266 tevent_req_done(req
);
270 tldap_gensec_update_next(req
);
273 TLDAPRC
tldap_gensec_bind_recv(struct tevent_req
*req
)
275 struct tldap_gensec_bind_state
*state
= tevent_req_data(
276 req
, struct tldap_gensec_bind_state
);
277 struct tstream_context
*plain
, *sec
;
281 if (tevent_req_is_ldap_error(req
, &rc
)) {
285 if ((state
->gensec_features
& GENSEC_FEATURE_SIGN
) &&
286 !gensec_have_feature(state
->gensec
, GENSEC_FEATURE_SIGN
)) {
287 return TLDAP_OPERATIONS_ERROR
;
289 if ((state
->gensec_features
& GENSEC_FEATURE_SEAL
) &&
290 !gensec_have_feature(state
->gensec
, GENSEC_FEATURE_SEAL
)) {
291 return TLDAP_OPERATIONS_ERROR
;
294 if (!gensec_have_feature(state
->gensec
, GENSEC_FEATURE_SIGN
) &&
295 !gensec_have_feature(state
->gensec
, GENSEC_FEATURE_SEAL
)) {
296 return TLDAP_SUCCESS
;
300 * The gensec ctx needs to survive as long as the ldap context
303 talloc_steal(state
->ctx
, state
->gensec
);
305 plain
= tldap_get_plain_tstream(state
->ctx
);
307 status
= gensec_create_tstream(state
->ctx
, state
->gensec
,
309 if (!NT_STATUS_IS_OK(status
)) {
310 DBG_DEBUG("gensec_create_tstream failed: %s\n",
312 return TLDAP_OPERATIONS_ERROR
;
315 tldap_set_gensec_tstream(state
->ctx
, &sec
);
317 return TLDAP_SUCCESS
;
320 TLDAPRC
tldap_gensec_bind(
321 struct tldap_context
*ctx
, struct cli_credentials
*creds
,
322 const char *target_service
, const char *target_hostname
,
323 const char *target_principal
, struct loadparm_context
*lp_ctx
,
324 uint32_t gensec_features
)
326 TALLOC_CTX
*frame
= talloc_stackframe();
327 struct tevent_context
*ev
;
328 struct tevent_req
*req
;
329 TLDAPRC rc
= TLDAP_NO_MEMORY
;
331 ev
= samba_tevent_context_init(frame
);
335 req
= tldap_gensec_bind_send(frame
, ev
, ctx
, creds
, target_service
,
336 target_hostname
, target_principal
, lp_ctx
,
341 if (!tevent_req_poll(req
, ev
)) {
342 rc
= TLDAP_OPERATIONS_ERROR
;
345 rc
= tldap_gensec_bind_recv(req
);