Fix bug 8433, segfault in iconv.c
[Samba.git] / source4 / libcli / smb_composite / sesssetup.c
blobe1159a4cd22582d938ea5d78b9d7e18ce4f780b2
1 /*
2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 2005
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 a composite API for making handling a generic async session setup
23 #include "includes.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/raw/raw_proto.h"
26 #include "libcli/composite/composite.h"
27 #include "libcli/smb_composite/smb_composite.h"
28 #include "libcli/smb_composite/proto.h"
29 #include "libcli/auth/libcli_auth.h"
30 #include "auth/auth.h"
31 #include "auth/gensec/gensec.h"
32 #include "auth/credentials/credentials.h"
33 #include "version.h"
34 #include "param/param.h"
36 struct sesssetup_state {
37 union smb_sesssetup setup;
38 NTSTATUS remote_status;
39 NTSTATUS gensec_status;
40 struct smb_composite_sesssetup *io;
41 struct smbcli_request *req;
44 static int sesssetup_state_destructor(struct sesssetup_state *state)
46 if (state->req) {
47 talloc_free(state->req);
48 state->req = NULL;
51 return 0;
54 static NTSTATUS session_setup_old(struct composite_context *c,
55 struct smbcli_session *session,
56 struct smb_composite_sesssetup *io,
57 struct smbcli_request **req);
58 static NTSTATUS session_setup_nt1(struct composite_context *c,
59 struct smbcli_session *session,
60 struct smb_composite_sesssetup *io,
61 struct smbcli_request **req);
62 static NTSTATUS session_setup_spnego(struct composite_context *c,
63 struct smbcli_session *session,
64 struct smb_composite_sesssetup *io,
65 struct smbcli_request **req);
68 store the user session key for a transport
70 static void set_user_session_key(struct smbcli_session *session,
71 const DATA_BLOB *session_key)
73 session->user_session_key = data_blob_talloc(session,
74 session_key->data,
75 session_key->length);
79 handler for completion of a smbcli_request sub-request
81 static void request_handler(struct smbcli_request *req)
83 struct composite_context *c = (struct composite_context *)req->async.private_data;
84 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
85 struct smbcli_session *session = req->session;
86 DATA_BLOB session_key = data_blob(NULL, 0);
87 DATA_BLOB null_data_blob = data_blob(NULL, 0);
88 NTSTATUS session_key_err, nt_status;
89 struct smbcli_request *check_req = NULL;
90 const char *os = NULL;
91 const char *lanman = NULL;
93 if (req->sign_caller_checks) {
94 req->do_not_free = true;
95 check_req = req;
98 state->remote_status = smb_raw_sesssetup_recv(req, state, &state->setup);
99 c->status = state->remote_status;
100 state->req = NULL;
103 * we only need to check the signature if the
104 * NT_STATUS_OK is returned
106 if (!NT_STATUS_IS_OK(state->remote_status)) {
107 talloc_free(check_req);
108 check_req = NULL;
111 switch (state->setup.old.level) {
112 case RAW_SESSSETUP_OLD:
113 state->io->out.vuid = state->setup.old.out.vuid;
114 /* This doesn't work, as this only happens on old
115 * protocols, where this comparison won't match. */
116 if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
117 /* we neet to reset the vuid for a new try */
118 session->vuid = 0;
119 if (cli_credentials_wrong_password(state->io->in.credentials)) {
120 nt_status = session_setup_old(c, session,
121 state->io,
122 &state->req);
123 if (NT_STATUS_IS_OK(nt_status)) {
124 talloc_free(check_req);
125 c->status = nt_status;
126 composite_continue_smb(c, state->req, request_handler, c);
127 return;
131 os = state->setup.old.out.os;
132 lanman = state->setup.old.out.lanman;
133 break;
135 case RAW_SESSSETUP_NT1:
136 state->io->out.vuid = state->setup.nt1.out.vuid;
137 if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
138 /* we neet to reset the vuid for a new try */
139 session->vuid = 0;
140 if (cli_credentials_wrong_password(state->io->in.credentials)) {
141 nt_status = session_setup_nt1(c, session,
142 state->io,
143 &state->req);
144 if (NT_STATUS_IS_OK(nt_status)) {
145 talloc_free(check_req);
146 c->status = nt_status;
147 composite_continue_smb(c, state->req, request_handler, c);
148 return;
152 os = state->setup.nt1.out.os;
153 lanman = state->setup.nt1.out.lanman;
154 break;
156 case RAW_SESSSETUP_SPNEGO:
157 state->io->out.vuid = state->setup.spnego.out.vuid;
158 if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
159 /* we need to reset the vuid for a new try */
160 session->vuid = 0;
161 if (cli_credentials_wrong_password(state->io->in.credentials)) {
162 nt_status = session_setup_spnego(c, session,
163 state->io,
164 &state->req);
165 if (NT_STATUS_IS_OK(nt_status)) {
166 talloc_free(check_req);
167 c->status = nt_status;
168 composite_continue_smb(c, state->req, request_handler, c);
169 return;
173 if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
174 !NT_STATUS_IS_OK(c->status)) {
175 break;
177 if (NT_STATUS_EQUAL(state->gensec_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
179 /* The status value here, from the earlier pass at GENSEC is
180 * vital to the security of the system. Even if the other end
181 * accepts, if GENSEC claims 'MORE_PROCESSING_REQUIRED' then
182 * you must keep feeding it blobs, or else the remote
183 * host/attacker might avoid mutal authentication
184 * requirements */
186 state->gensec_status = gensec_update(session->gensec, state,
187 state->setup.spnego.out.secblob,
188 &state->setup.spnego.in.secblob);
189 c->status = state->gensec_status;
190 if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
191 !NT_STATUS_IS_OK(c->status)) {
192 break;
194 } else {
195 state->setup.spnego.in.secblob = data_blob(NULL, 0);
198 if (NT_STATUS_IS_OK(state->remote_status)) {
199 if (state->setup.spnego.in.secblob.length) {
200 c->status = NT_STATUS_INTERNAL_ERROR;
201 break;
203 session_key_err = gensec_session_key(session->gensec, &session_key);
204 if (NT_STATUS_IS_OK(session_key_err)) {
205 set_user_session_key(session, &session_key);
206 smbcli_transport_simple_set_signing(session->transport, session_key, null_data_blob);
210 if (state->setup.spnego.in.secblob.length) {
212 * set the session->vuid value only for calling
213 * smb_raw_sesssetup_send()
215 uint16_t vuid = session->vuid;
216 session->vuid = state->io->out.vuid;
217 state->req = smb_raw_sesssetup_send(session, &state->setup);
218 session->vuid = vuid;
219 if (state->req) {
220 state->req->sign_caller_checks = true;
222 composite_continue_smb(c, state->req, request_handler, c);
223 return;
225 os = state->setup.spnego.out.os;
226 lanman = state->setup.spnego.out.lanman;
227 break;
229 case RAW_SESSSETUP_SMB2:
230 c->status = NT_STATUS_INTERNAL_ERROR;
231 break;
234 if (check_req) {
235 check_req->sign_caller_checks = false;
236 if (!smbcli_request_check_sign_mac(check_req)) {
237 c->status = NT_STATUS_ACCESS_DENIED;
239 talloc_free(check_req);
240 check_req = NULL;
243 /* enforce the local signing required flag */
244 if (NT_STATUS_IS_OK(c->status) && !cli_credentials_is_anonymous(state->io->in.credentials)) {
245 if (!session->transport->negotiate.sign_info.doing_signing
246 && session->transport->negotiate.sign_info.mandatory_signing) {
247 DEBUG(0, ("SMB signing required, but server does not support it\n"));
248 c->status = NT_STATUS_ACCESS_DENIED;
252 if (!NT_STATUS_IS_OK(c->status)) {
253 composite_error(c, c->status);
254 return;
257 if (os) {
258 session->os = talloc_strdup(session, os);
259 if (composite_nomem(session->os, c)) return;
260 } else {
261 session->os = NULL;
263 if (lanman) {
264 session->lanman = talloc_strdup(session, lanman);
265 if (composite_nomem(session->lanman, c)) return;
266 } else {
267 session->lanman = NULL;
270 composite_done(c);
275 send a nt1 style session setup
277 static NTSTATUS session_setup_nt1(struct composite_context *c,
278 struct smbcli_session *session,
279 struct smb_composite_sesssetup *io,
280 struct smbcli_request **req)
282 NTSTATUS nt_status = NT_STATUS_INTERNAL_ERROR;
283 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
284 DATA_BLOB names_blob = NTLMv2_generate_names_blob(state, session->transport->socket->hostname, cli_credentials_get_domain(io->in.credentials));
285 DATA_BLOB session_key = data_blob(NULL, 0);
286 int flags = CLI_CRED_NTLM_AUTH;
288 smbcli_temp_set_signing(session->transport);
290 if (session->options.lanman_auth) {
291 flags |= CLI_CRED_LANMAN_AUTH;
294 if (session->options.ntlmv2_auth) {
295 flags |= CLI_CRED_NTLMv2_AUTH;
298 state->setup.nt1.level = RAW_SESSSETUP_NT1;
299 state->setup.nt1.in.bufsize = session->transport->options.max_xmit;
300 state->setup.nt1.in.mpx_max = session->transport->options.max_mux;
301 state->setup.nt1.in.vc_num = 1;
302 state->setup.nt1.in.sesskey = io->in.sesskey;
303 state->setup.nt1.in.capabilities = io->in.capabilities;
304 state->setup.nt1.in.os = "Unix";
305 state->setup.nt1.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
307 cli_credentials_get_ntlm_username_domain(io->in.credentials, state,
308 &state->setup.nt1.in.user,
309 &state->setup.nt1.in.domain);
312 if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
313 nt_status = cli_credentials_get_ntlm_response(io->in.credentials, state,
314 &flags,
315 session->transport->negotiate.secblob,
316 names_blob,
317 &state->setup.nt1.in.password1,
318 &state->setup.nt1.in.password2,
319 NULL, &session_key);
320 NT_STATUS_NOT_OK_RETURN(nt_status);
321 } else if (session->options.plaintext_auth) {
322 const char *password = cli_credentials_get_password(io->in.credentials);
323 state->setup.nt1.in.password1 = data_blob_talloc(state, password, strlen(password));
324 state->setup.nt1.in.password2 = data_blob(NULL, 0);
325 } else {
326 /* could match windows client and return 'cannot logon from this workstation', but it just confuses everybody */
327 return NT_STATUS_INVALID_PARAMETER;
330 *req = smb_raw_sesssetup_send(session, &state->setup);
331 if (!*req) {
332 return NT_STATUS_NO_MEMORY;
335 if (NT_STATUS_IS_OK(nt_status)) {
336 smbcli_transport_simple_set_signing(session->transport, session_key,
337 state->setup.nt1.in.password2);
338 set_user_session_key(session, &session_key);
340 data_blob_free(&session_key);
343 return (*req)->status;
348 old style session setup (pre NT1 protocol level)
350 static NTSTATUS session_setup_old(struct composite_context *c,
351 struct smbcli_session *session,
352 struct smb_composite_sesssetup *io,
353 struct smbcli_request **req)
355 NTSTATUS nt_status;
356 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
357 const char *password = cli_credentials_get_password(io->in.credentials);
358 DATA_BLOB names_blob = NTLMv2_generate_names_blob(state, session->transport->socket->hostname, cli_credentials_get_domain(io->in.credentials));
359 DATA_BLOB session_key;
360 int flags = 0;
361 if (session->options.lanman_auth) {
362 flags |= CLI_CRED_LANMAN_AUTH;
365 if (session->options.ntlmv2_auth) {
366 flags |= CLI_CRED_NTLMv2_AUTH;
369 state->setup.old.level = RAW_SESSSETUP_OLD;
370 state->setup.old.in.bufsize = session->transport->options.max_xmit;
371 state->setup.old.in.mpx_max = session->transport->options.max_mux;
372 state->setup.old.in.vc_num = 1;
373 state->setup.old.in.sesskey = io->in.sesskey;
374 state->setup.old.in.os = "Unix";
375 state->setup.old.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
376 cli_credentials_get_ntlm_username_domain(io->in.credentials, state,
377 &state->setup.old.in.user,
378 &state->setup.old.in.domain);
380 if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
381 nt_status = cli_credentials_get_ntlm_response(io->in.credentials, state,
382 &flags,
383 session->transport->negotiate.secblob,
384 names_blob,
385 &state->setup.old.in.password,
386 NULL,
387 NULL, &session_key);
388 NT_STATUS_NOT_OK_RETURN(nt_status);
389 set_user_session_key(session, &session_key);
391 data_blob_free(&session_key);
392 } else if (session->options.plaintext_auth) {
393 state->setup.old.in.password = data_blob_talloc(state, password, strlen(password));
394 } else {
395 /* could match windows client and return 'cannot logon from this workstation', but it just confuses everybody */
396 return NT_STATUS_INVALID_PARAMETER;
399 *req = smb_raw_sesssetup_send(session, &state->setup);
400 if (!*req) {
401 return NT_STATUS_NO_MEMORY;
403 return (*req)->status;
408 Modern, all singing, all dancing extended security (and possibly SPNEGO) request
410 static NTSTATUS session_setup_spnego(struct composite_context *c,
411 struct smbcli_session *session,
412 struct smb_composite_sesssetup *io,
413 struct smbcli_request **req)
415 struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
416 NTSTATUS status;
417 const char *chosen_oid = NULL;
419 state->setup.spnego.level = RAW_SESSSETUP_SPNEGO;
420 state->setup.spnego.in.bufsize = session->transport->options.max_xmit;
421 state->setup.spnego.in.mpx_max = session->transport->options.max_mux;
422 state->setup.spnego.in.vc_num = 1;
423 state->setup.spnego.in.sesskey = io->in.sesskey;
424 state->setup.spnego.in.capabilities = io->in.capabilities;
425 state->setup.spnego.in.os = "Unix";
426 state->setup.spnego.in.lanman = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
427 state->setup.spnego.in.workgroup = io->in.workgroup;
429 smbcli_temp_set_signing(session->transport);
431 status = gensec_client_start(session, &session->gensec, c->event_ctx,
432 io->in.gensec_settings);
433 if (!NT_STATUS_IS_OK(status)) {
434 DEBUG(1, ("Failed to start GENSEC client mode: %s\n", nt_errstr(status)));
435 return status;
438 gensec_want_feature(session->gensec, GENSEC_FEATURE_SESSION_KEY);
440 status = gensec_set_credentials(session->gensec, io->in.credentials);
441 if (!NT_STATUS_IS_OK(status)) {
442 DEBUG(1, ("Failed to start set GENSEC client credentials: %s\n",
443 nt_errstr(status)));
444 return status;
447 status = gensec_set_target_hostname(session->gensec, session->transport->socket->hostname);
448 if (!NT_STATUS_IS_OK(status)) {
449 DEBUG(1, ("Failed to start set GENSEC target hostname: %s\n",
450 nt_errstr(status)));
451 return status;
454 status = gensec_set_target_service(session->gensec, "cifs");
455 if (!NT_STATUS_IS_OK(status)) {
456 DEBUG(1, ("Failed to start set GENSEC target service: %s\n",
457 nt_errstr(status)));
458 return status;
461 if (session->transport->negotiate.secblob.length) {
462 chosen_oid = GENSEC_OID_SPNEGO;
463 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
464 if (!NT_STATUS_IS_OK(status)) {
465 DEBUG(1, ("Failed to start set GENSEC client mechanism %s: %s\n",
466 gensec_get_name_by_oid(session->gensec, chosen_oid), nt_errstr(status)));
467 chosen_oid = GENSEC_OID_NTLMSSP;
468 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
469 if (!NT_STATUS_IS_OK(status)) {
470 DEBUG(1, ("Failed to start set (fallback) GENSEC client mechanism %s: %s\n",
471 gensec_get_name_by_oid(session->gensec, chosen_oid),
472 nt_errstr(status)));
473 return status;
476 } else {
477 /* without a sec blob, means raw NTLMSSP */
478 chosen_oid = GENSEC_OID_NTLMSSP;
479 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
480 if (!NT_STATUS_IS_OK(status)) {
481 DEBUG(1, ("Failed to start set GENSEC client mechanism %s: %s\n",
482 gensec_get_name_by_oid(session->gensec, chosen_oid), nt_errstr(status)));
486 if ((const void *)chosen_oid == (const void *)GENSEC_OID_SPNEGO) {
487 status = gensec_update(session->gensec, state,
488 session->transport->negotiate.secblob,
489 &state->setup.spnego.in.secblob);
490 } else {
491 status = gensec_update(session->gensec, state,
492 data_blob(NULL, 0),
493 &state->setup.spnego.in.secblob);
497 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
498 !NT_STATUS_IS_OK(status)) {
499 DEBUG(1, ("Failed initial gensec_update with mechanism %s: %s\n",
500 gensec_get_name_by_oid(session->gensec, chosen_oid),
501 nt_errstr(status)));
502 return status;
504 state->gensec_status = status;
506 *req = smb_raw_sesssetup_send(session, &state->setup);
507 if (!*req) {
508 return NT_STATUS_NO_MEMORY;
512 * we need to check the signature ourself
513 * as the session key might be the acceptor subkey
514 * which comes within the response itself
516 (*req)->sign_caller_checks = true;
518 return (*req)->status;
523 composite session setup function that hides the details of all the
524 different session setup varients, including the multi-pass nature of
525 the spnego varient
527 struct composite_context *smb_composite_sesssetup_send(struct smbcli_session *session,
528 struct smb_composite_sesssetup *io)
530 struct composite_context *c;
531 struct sesssetup_state *state;
532 NTSTATUS status;
534 c = composite_create(session, session->transport->socket->event.ctx);
535 if (c == NULL) return NULL;
537 state = talloc_zero(c, struct sesssetup_state);
538 if (composite_nomem(state, c)) return c;
539 c->private_data = state;
541 state->io = io;
543 talloc_set_destructor(state, sesssetup_state_destructor);
545 /* no session setup at all in earliest protocol varients */
546 if (session->transport->negotiate.protocol < PROTOCOL_LANMAN1) {
547 ZERO_STRUCT(io->out);
548 composite_done(c);
549 return c;
552 /* see what session setup interface we will use */
553 if (session->transport->negotiate.protocol < PROTOCOL_NT1) {
554 status = session_setup_old(c, session, io, &state->req);
555 } else if (!session->transport->options.use_spnego ||
556 !(io->in.capabilities & CAP_EXTENDED_SECURITY)) {
557 status = session_setup_nt1(c, session, io, &state->req);
558 } else {
559 status = session_setup_spnego(c, session, io, &state->req);
562 if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) ||
563 NT_STATUS_IS_OK(status)) {
564 composite_continue_smb(c, state->req, request_handler, c);
565 return c;
568 composite_error(c, status);
569 return c;
574 receive a composite session setup reply
576 NTSTATUS smb_composite_sesssetup_recv(struct composite_context *c)
578 NTSTATUS status;
579 status = composite_wait(c);
580 talloc_free(c);
581 return status;
585 sync version of smb_composite_sesssetup
587 NTSTATUS smb_composite_sesssetup(struct smbcli_session *session, struct smb_composite_sesssetup *io)
589 struct composite_context *c = smb_composite_sesssetup_send(session, io);
590 return smb_composite_sesssetup_recv(c);