2 Unix SMB/CIFS implementation.
5 Copyright (C) Stefan Metzmacher 2009
6 Copyright (C) Jeremy Allison 2010
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "../libcli/smb/smb_common.h"
26 #include "../auth/gensec/gensec.h"
28 #include "../lib/tsocket/tsocket.h"
29 #include "../libcli/security/security.h"
30 #include "../lib/util/tevent_ntstatus.h"
32 static struct tevent_req
*smbd_smb2_session_setup_wrap_send(TALLOC_CTX
*mem_ctx
,
33 struct tevent_context
*ev
,
34 struct smbd_smb2_request
*smb2req
,
35 uint64_t in_session_id
,
37 uint8_t in_security_mode
,
38 uint64_t in_previous_session_id
,
39 DATA_BLOB in_security_buffer
);
40 static NTSTATUS
smbd_smb2_session_setup_wrap_recv(struct tevent_req
*req
,
41 uint16_t *out_session_flags
,
43 DATA_BLOB
*out_security_buffer
,
44 uint64_t *out_session_id
);
46 static void smbd_smb2_request_sesssetup_done(struct tevent_req
*subreq
);
48 NTSTATUS
smbd_smb2_request_process_sesssetup(struct smbd_smb2_request
*smb2req
)
51 const uint8_t *inbody
;
52 uint64_t in_session_id
;
54 uint8_t in_security_mode
;
55 uint64_t in_previous_session_id
;
56 uint16_t in_security_offset
;
57 uint16_t in_security_length
;
58 DATA_BLOB in_security_buffer
;
60 struct tevent_req
*subreq
;
62 status
= smbd_smb2_request_verify_sizes(smb2req
, 0x19);
63 if (!NT_STATUS_IS_OK(status
)) {
64 return smbd_smb2_request_error(smb2req
, status
);
66 inhdr
= SMBD_SMB2_IN_HDR_PTR(smb2req
);
67 inbody
= SMBD_SMB2_IN_BODY_PTR(smb2req
);
69 in_session_id
= BVAL(inhdr
, SMB2_HDR_SESSION_ID
);
71 in_flags
= CVAL(inbody
, 0x02);
72 in_security_mode
= CVAL(inbody
, 0x03);
73 /* Capabilities = IVAL(inbody, 0x04) */
74 /* Channel = IVAL(inbody, 0x08) */
75 in_security_offset
= SVAL(inbody
, 0x0C);
76 in_security_length
= SVAL(inbody
, 0x0E);
77 in_previous_session_id
= BVAL(inbody
, 0x10);
79 if (in_security_offset
!= (SMB2_HDR_BODY
+ SMBD_SMB2_IN_BODY_LEN(smb2req
))) {
80 return smbd_smb2_request_error(smb2req
, NT_STATUS_INVALID_PARAMETER
);
83 if (in_security_length
> SMBD_SMB2_IN_DYN_LEN(smb2req
)) {
84 return smbd_smb2_request_error(smb2req
, NT_STATUS_INVALID_PARAMETER
);
87 in_security_buffer
.data
= SMBD_SMB2_IN_DYN_PTR(smb2req
);
88 in_security_buffer
.length
= in_security_length
;
90 subreq
= smbd_smb2_session_setup_wrap_send(smb2req
,
91 smb2req
->sconn
->ev_ctx
,
96 in_previous_session_id
,
99 return smbd_smb2_request_error(smb2req
, NT_STATUS_NO_MEMORY
);
101 tevent_req_set_callback(subreq
, smbd_smb2_request_sesssetup_done
, smb2req
);
103 return smbd_smb2_request_pending_queue(smb2req
, subreq
, 500);
106 static void smbd_smb2_request_sesssetup_done(struct tevent_req
*subreq
)
108 struct smbd_smb2_request
*smb2req
=
109 tevent_req_callback_data(subreq
,
110 struct smbd_smb2_request
);
114 uint16_t out_session_flags
;
115 uint64_t out_session_id
;
116 uint16_t out_security_offset
;
117 DATA_BLOB out_security_buffer
= data_blob_null
;
119 NTSTATUS error
; /* transport error */
121 status
= smbd_smb2_session_setup_wrap_recv(subreq
,
124 &out_security_buffer
,
127 if (!NT_STATUS_IS_OK(status
) &&
128 !NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
129 status
= nt_status_squash(status
);
130 error
= smbd_smb2_request_error(smb2req
, status
);
131 if (!NT_STATUS_IS_OK(error
)) {
132 smbd_server_connection_terminate(smb2req
->sconn
,
139 out_security_offset
= SMB2_HDR_BODY
+ 0x08;
141 outhdr
= SMBD_SMB2_OUT_HDR_PTR(smb2req
);
143 outbody
= data_blob_talloc(smb2req
->out
.vector
, NULL
, 0x08);
144 if (outbody
.data
== NULL
) {
145 error
= smbd_smb2_request_error(smb2req
, NT_STATUS_NO_MEMORY
);
146 if (!NT_STATUS_IS_OK(error
)) {
147 smbd_server_connection_terminate(smb2req
->sconn
,
154 SBVAL(outhdr
, SMB2_HDR_SESSION_ID
, out_session_id
);
156 SSVAL(outbody
.data
, 0x00, 0x08 + 1); /* struct size */
157 SSVAL(outbody
.data
, 0x02,
158 out_session_flags
); /* session flags */
159 SSVAL(outbody
.data
, 0x04,
160 out_security_offset
); /* security buffer offset */
161 SSVAL(outbody
.data
, 0x06,
162 out_security_buffer
.length
); /* security buffer length */
164 outdyn
= out_security_buffer
;
166 error
= smbd_smb2_request_done_ex(smb2req
, status
, outbody
, &outdyn
,
168 if (!NT_STATUS_IS_OK(error
)) {
169 smbd_server_connection_terminate(smb2req
->sconn
,
175 static NTSTATUS
smbd_smb2_auth_generic_return(struct smbXsrv_session
*session
,
176 struct smbd_smb2_request
*smb2req
,
177 uint8_t in_security_mode
,
178 struct auth_session_info
*session_info
,
179 uint16_t *out_session_flags
,
180 uint64_t *out_session_id
)
184 uint8_t session_key
[16];
185 struct smbXsrv_session
*x
= session
;
186 struct smbXsrv_connection
*conn
= session
->connection
;
188 if ((in_security_mode
& SMB2_NEGOTIATE_SIGNING_REQUIRED
) ||
189 lp_server_signing() == SMB_SIGNING_REQUIRED
) {
190 x
->global
->signing_required
= true;
193 if ((lp_smb_encrypt(-1) >= SMB_SIGNING_DESIRED
) &&
194 (conn
->smb2
.client
.capabilities
& SMB2_CAP_ENCRYPTION
)) {
195 x
->encryption_desired
= true;
198 if (lp_smb_encrypt(-1) == SMB_SIGNING_REQUIRED
) {
199 x
->encryption_desired
= true;
200 x
->global
->encryption_required
= true;
203 if (security_session_user_level(session_info
, NULL
) < SECURITY_USER
) {
204 /* we map anonymous to guest internally */
205 *out_session_flags
|= SMB2_SESSION_FLAG_IS_GUEST
;
206 *out_session_flags
|= SMB2_SESSION_FLAG_IS_NULL
;
207 /* force no signing */
208 x
->global
->signing_required
= false;
212 if (guest
&& x
->global
->encryption_required
) {
213 DEBUG(1,("reject guest session as encryption is required\n"));
214 return NT_STATUS_ACCESS_DENIED
;
217 if (!(conn
->smb2
.server
.capabilities
& SMB2_CAP_ENCRYPTION
)) {
218 if (x
->global
->encryption_required
) {
219 DEBUG(1,("reject session with dialect[0x%04X] "
220 "as encryption is required\n",
221 conn
->smb2
.server
.dialect
));
222 return NT_STATUS_ACCESS_DENIED
;
226 if (x
->encryption_desired
) {
227 *out_session_flags
|= SMB2_SESSION_FLAG_ENCRYPT_DATA
;
230 ZERO_STRUCT(session_key
);
231 memcpy(session_key
, session_info
->session_key
.data
,
232 MIN(session_info
->session_key
.length
, sizeof(session_key
)));
234 x
->global
->signing_key
= data_blob_talloc(x
->global
,
236 sizeof(session_key
));
237 if (x
->global
->signing_key
.data
== NULL
) {
238 ZERO_STRUCT(session_key
);
239 return NT_STATUS_NO_MEMORY
;
242 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
243 const DATA_BLOB label
= data_blob_string_const_null("SMB2AESCMAC");
244 const DATA_BLOB context
= data_blob_string_const_null("SmbSign");
246 smb2_key_derivation(session_key
, sizeof(session_key
),
247 label
.data
, label
.length
,
248 context
.data
, context
.length
,
249 x
->global
->signing_key
.data
);
252 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
253 const DATA_BLOB label
= data_blob_string_const_null("SMB2AESCCM");
254 const DATA_BLOB context
= data_blob_string_const_null("ServerIn ");
256 x
->global
->decryption_key
= data_blob_talloc(x
->global
,
258 sizeof(session_key
));
259 if (x
->global
->decryption_key
.data
== NULL
) {
260 ZERO_STRUCT(session_key
);
261 return NT_STATUS_NO_MEMORY
;
264 smb2_key_derivation(session_key
, sizeof(session_key
),
265 label
.data
, label
.length
,
266 context
.data
, context
.length
,
267 x
->global
->decryption_key
.data
);
270 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
271 const DATA_BLOB label
= data_blob_string_const_null("SMB2AESCCM");
272 const DATA_BLOB context
= data_blob_string_const_null("ServerOut");
274 x
->global
->encryption_key
= data_blob_talloc(x
->global
,
276 sizeof(session_key
));
277 if (x
->global
->encryption_key
.data
== NULL
) {
278 ZERO_STRUCT(session_key
);
279 return NT_STATUS_NO_MEMORY
;
282 smb2_key_derivation(session_key
, sizeof(session_key
),
283 label
.data
, label
.length
,
284 context
.data
, context
.length
,
285 x
->global
->encryption_key
.data
);
287 generate_random_buffer((uint8_t *)&x
->nonce_high
, sizeof(x
->nonce_high
));
291 x
->global
->application_key
= data_blob_dup_talloc(x
->global
,
292 x
->global
->signing_key
);
293 if (x
->global
->application_key
.data
== NULL
) {
294 ZERO_STRUCT(session_key
);
295 return NT_STATUS_NO_MEMORY
;
298 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
299 const DATA_BLOB label
= data_blob_string_const_null("SMB2APP");
300 const DATA_BLOB context
= data_blob_string_const_null("SmbRpc");
302 smb2_key_derivation(session_key
, sizeof(session_key
),
303 label
.data
, label
.length
,
304 context
.data
, context
.length
,
305 x
->global
->application_key
.data
);
307 ZERO_STRUCT(session_key
);
309 x
->global
->channels
[0].signing_key
= data_blob_dup_talloc(x
->global
->channels
,
310 x
->global
->signing_key
);
311 if (x
->global
->channels
[0].signing_key
.data
== NULL
) {
312 return NT_STATUS_NO_MEMORY
;
315 data_blob_clear_free(&session_info
->session_key
);
316 session_info
->session_key
= data_blob_dup_talloc(session_info
,
317 x
->global
->application_key
);
318 if (session_info
->session_key
.data
== NULL
) {
319 return NT_STATUS_NO_MEMORY
;
322 session
->compat
= talloc_zero(session
, struct user_struct
);
323 if (session
->compat
== NULL
) {
324 return NT_STATUS_NO_MEMORY
;
326 session
->compat
->session
= session
;
327 session
->compat
->homes_snum
= -1;
328 session
->compat
->session_info
= session_info
;
329 session
->compat
->session_keystr
= NULL
;
330 session
->compat
->vuid
= session
->global
->session_wire_id
;
331 DLIST_ADD(smb2req
->sconn
->users
, session
->compat
);
332 smb2req
->sconn
->num_users
++;
334 if (security_session_user_level(session_info
, NULL
) >= SECURITY_USER
) {
335 session
->compat
->homes_snum
=
336 register_homes_share(session_info
->unix_info
->unix_name
);
339 set_current_user_info(session_info
->unix_info
->sanitized_username
,
340 session_info
->unix_info
->unix_name
,
341 session_info
->info
->domain_name
);
343 reload_services(smb2req
->sconn
, conn_snum_used
, true);
345 session
->status
= NT_STATUS_OK
;
346 session
->global
->auth_session_info
= session_info
;
347 session
->global
->auth_session_info_seqnum
+= 1;
348 session
->global
->channels
[0].auth_session_info_seqnum
=
349 session
->global
->auth_session_info_seqnum
;
350 session
->global
->expiration_time
= gensec_expire_time(session
->gensec
);
352 if (!session_claim(session
)) {
353 DEBUG(1, ("smb2: Failed to claim session "
355 (unsigned long long)session
->compat
->vuid
));
356 return NT_STATUS_LOGON_FAILURE
;
359 status
= smbXsrv_session_update(session
);
360 if (!NT_STATUS_IS_OK(status
)) {
361 DEBUG(0, ("smb2: Failed to update session for vuid=%llu - %s\n",
362 (unsigned long long)session
->compat
->vuid
,
364 return NT_STATUS_LOGON_FAILURE
;
368 * we attach the session to the request
369 * so that the response can be signed
372 smb2req
->do_signing
= true;
375 global_client_caps
|= (CAP_LEVEL_II_OPLOCKS
|CAP_STATUS32
);
377 *out_session_id
= session
->global
->session_wire_id
;
382 static NTSTATUS
smbd_smb2_reauth_generic_return(struct smbXsrv_session
*session
,
383 struct smbd_smb2_request
*smb2req
,
384 struct auth_session_info
*session_info
,
385 uint16_t *out_session_flags
,
386 uint64_t *out_session_id
)
389 struct smbXsrv_session
*x
= session
;
390 struct smbXsrv_connection
*conn
= session
->connection
;
392 data_blob_clear_free(&session_info
->session_key
);
393 session_info
->session_key
= data_blob_dup_talloc(session_info
,
394 x
->global
->application_key
);
395 if (session_info
->session_key
.data
== NULL
) {
396 return NT_STATUS_NO_MEMORY
;
399 session
->compat
->session_info
= session_info
;
400 session
->compat
->vuid
= session
->global
->session_wire_id
;
402 session
->compat
->homes_snum
=
403 register_homes_share(session_info
->unix_info
->unix_name
);
405 set_current_user_info(session_info
->unix_info
->sanitized_username
,
406 session_info
->unix_info
->unix_name
,
407 session_info
->info
->domain_name
);
409 reload_services(smb2req
->sconn
, conn_snum_used
, true);
411 session
->status
= NT_STATUS_OK
;
412 TALLOC_FREE(session
->global
->auth_session_info
);
413 session
->global
->auth_session_info
= session_info
;
414 session
->global
->auth_session_info_seqnum
+= 1;
415 session
->global
->channels
[0].auth_session_info_seqnum
=
416 session
->global
->auth_session_info_seqnum
;
417 session
->global
->expiration_time
= gensec_expire_time(session
->gensec
);
419 status
= smbXsrv_session_update(session
);
420 if (!NT_STATUS_IS_OK(status
)) {
421 DEBUG(0, ("smb2: Failed to update session for vuid=%llu - %s\n",
422 (unsigned long long)session
->compat
->vuid
,
424 return NT_STATUS_LOGON_FAILURE
;
427 conn_clear_vuid_caches(conn
->sconn
, session
->compat
->vuid
);
429 if (security_session_user_level(session_info
, NULL
) >= SECURITY_USER
) {
430 smb2req
->do_signing
= true;
433 *out_session_id
= session
->global
->session_wire_id
;
438 struct smbd_smb2_session_setup_state
{
439 struct tevent_context
*ev
;
440 struct smbd_smb2_request
*smb2req
;
441 uint64_t in_session_id
;
443 uint8_t in_security_mode
;
444 uint64_t in_previous_session_id
;
445 DATA_BLOB in_security_buffer
;
446 struct smbXsrv_session
*session
;
447 struct auth_session_info
*session_info
;
448 uint16_t out_session_flags
;
449 DATA_BLOB out_security_buffer
;
450 uint64_t out_session_id
;
453 static void smbd_smb2_session_setup_gensec_done(struct tevent_req
*subreq
);
454 static void smbd_smb2_session_setup_previous_done(struct tevent_req
*subreq
);
456 static struct tevent_req
*smbd_smb2_session_setup_send(TALLOC_CTX
*mem_ctx
,
457 struct tevent_context
*ev
,
458 struct smbd_smb2_request
*smb2req
,
459 uint64_t in_session_id
,
461 uint8_t in_security_mode
,
462 uint64_t in_previous_session_id
,
463 DATA_BLOB in_security_buffer
)
465 struct tevent_req
*req
;
466 struct smbd_smb2_session_setup_state
*state
;
468 NTTIME now
= timeval_to_nttime(&smb2req
->request_time
);
469 struct tevent_req
*subreq
;
471 req
= tevent_req_create(mem_ctx
, &state
,
472 struct smbd_smb2_session_setup_state
);
477 state
->smb2req
= smb2req
;
478 state
->in_session_id
= in_session_id
;
479 state
->in_flags
= in_flags
;
480 state
->in_security_mode
= in_security_mode
;
481 state
->in_previous_session_id
= in_previous_session_id
;
482 state
->in_security_buffer
= in_security_buffer
;
484 if (in_flags
& SMB2_SESSION_FLAG_BINDING
) {
485 if (smb2req
->sconn
->conn
->protocol
< PROTOCOL_SMB2_22
) {
486 tevent_req_nterror(req
, NT_STATUS_REQUEST_NOT_ACCEPTED
);
487 return tevent_req_post(req
, ev
);
491 * We do not support multi channel.
493 tevent_req_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
494 return tevent_req_post(req
, ev
);
497 if (state
->in_session_id
== 0) {
498 /* create a new session */
499 status
= smbXsrv_session_create(state
->smb2req
->sconn
->conn
,
500 now
, &state
->session
);
501 if (tevent_req_nterror(req
, status
)) {
502 return tevent_req_post(req
, ev
);
504 smb2req
->session
= state
->session
;
506 status
= smb2srv_session_lookup(state
->smb2req
->sconn
->conn
,
507 state
->in_session_id
, now
,
509 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_SESSION_EXPIRED
)) {
510 status
= NT_STATUS_OK
;
512 if (NT_STATUS_IS_OK(status
)) {
513 state
->session
->status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
514 status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
515 TALLOC_FREE(state
->session
->gensec
);
517 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
518 tevent_req_nterror(req
, status
);
519 return tevent_req_post(req
, ev
);
523 if (state
->session
->gensec
== NULL
) {
524 status
= auth_generic_prepare(state
->session
,
525 state
->session
->connection
->remote_address
,
526 &state
->session
->gensec
);
527 if (tevent_req_nterror(req
, status
)) {
528 return tevent_req_post(req
, ev
);
531 gensec_want_feature(state
->session
->gensec
, GENSEC_FEATURE_SESSION_KEY
);
532 gensec_want_feature(state
->session
->gensec
, GENSEC_FEATURE_UNIX_TOKEN
);
534 status
= gensec_start_mech_by_oid(state
->session
->gensec
,
536 if (tevent_req_nterror(req
, status
)) {
537 return tevent_req_post(req
, ev
);
542 subreq
= gensec_update_send(state
, state
->ev
,
543 state
->session
->gensec
,
544 state
->in_security_buffer
);
546 if (tevent_req_nomem(subreq
, req
)) {
547 return tevent_req_post(req
, ev
);
549 tevent_req_set_callback(subreq
, smbd_smb2_session_setup_gensec_done
, req
);
554 static void smbd_smb2_session_setup_gensec_done(struct tevent_req
*subreq
)
556 struct tevent_req
*req
=
557 tevent_req_callback_data(subreq
,
559 struct smbd_smb2_session_setup_state
*state
=
561 struct smbd_smb2_session_setup_state
);
565 status
= gensec_update_recv(subreq
, state
,
566 &state
->out_security_buffer
);
569 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
) &&
570 !NT_STATUS_IS_OK(status
)) {
571 tevent_req_nterror(req
, status
);
575 if (NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
576 state
->out_session_id
= state
->session
->global
->session_wire_id
;
577 tevent_req_nterror(req
, status
);
581 status
= gensec_session_info(state
->session
->gensec
,
582 state
->session
->global
,
583 &state
->session_info
);
584 if (tevent_req_nterror(req
, status
)) {
588 if ((state
->in_previous_session_id
!= 0) &&
589 (state
->session
->global
->session_wire_id
!=
590 state
->in_previous_session_id
))
592 subreq
= smb2srv_session_close_previous_send(state
, state
->ev
,
593 state
->session
->connection
,
595 state
->in_previous_session_id
,
596 state
->session
->global
->session_wire_id
);
597 if (tevent_req_nomem(subreq
, req
)) {
600 tevent_req_set_callback(subreq
,
601 smbd_smb2_session_setup_previous_done
,
606 if (state
->session
->global
->auth_session_info
!= NULL
) {
607 status
= smbd_smb2_reauth_generic_return(state
->session
,
610 &state
->out_session_flags
,
611 &state
->out_session_id
);
612 if (tevent_req_nterror(req
, status
)) {
615 tevent_req_done(req
);
619 status
= smbd_smb2_auth_generic_return(state
->session
,
621 state
->in_security_mode
,
623 &state
->out_session_flags
,
624 &state
->out_session_id
);
625 if (tevent_req_nterror(req
, status
)) {
629 tevent_req_done(req
);
633 static void smbd_smb2_session_setup_previous_done(struct tevent_req
*subreq
)
635 struct tevent_req
*req
=
636 tevent_req_callback_data(subreq
,
638 struct smbd_smb2_session_setup_state
*state
=
640 struct smbd_smb2_session_setup_state
);
643 status
= smb2srv_session_close_previous_recv(subreq
);
645 if (tevent_req_nterror(req
, status
)) {
649 if (state
->session
->global
->auth_session_info
!= NULL
) {
650 status
= smbd_smb2_reauth_generic_return(state
->session
,
653 &state
->out_session_flags
,
654 &state
->out_session_id
);
655 if (tevent_req_nterror(req
, status
)) {
658 tevent_req_done(req
);
662 status
= smbd_smb2_auth_generic_return(state
->session
,
664 state
->in_security_mode
,
666 &state
->out_session_flags
,
667 &state
->out_session_id
);
668 if (tevent_req_nterror(req
, status
)) {
672 tevent_req_done(req
);
676 static NTSTATUS
smbd_smb2_session_setup_recv(struct tevent_req
*req
,
677 uint16_t *out_session_flags
,
679 DATA_BLOB
*out_security_buffer
,
680 uint64_t *out_session_id
)
682 struct smbd_smb2_session_setup_state
*state
=
684 struct smbd_smb2_session_setup_state
);
687 if (tevent_req_is_nterror(req
, &status
)) {
688 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
689 tevent_req_received(req
);
690 return nt_status_squash(status
);
693 status
= NT_STATUS_OK
;
696 *out_session_flags
= state
->out_session_flags
;
697 *out_security_buffer
= state
->out_security_buffer
;
698 *out_session_id
= state
->out_session_id
;
700 talloc_steal(mem_ctx
, out_security_buffer
->data
);
701 tevent_req_received(req
);
705 struct smbd_smb2_session_setup_wrap_state
{
706 struct tevent_context
*ev
;
707 struct smbd_smb2_request
*smb2req
;
708 uint64_t in_session_id
;
710 uint8_t in_security_mode
;
711 uint64_t in_previous_session_id
;
712 DATA_BLOB in_security_buffer
;
713 uint16_t out_session_flags
;
714 DATA_BLOB out_security_buffer
;
715 uint64_t out_session_id
;
719 static void smbd_smb2_session_setup_wrap_setup_done(struct tevent_req
*subreq
);
720 static void smbd_smb2_session_setup_wrap_shutdown_done(struct tevent_req
*subreq
);
722 static struct tevent_req
*smbd_smb2_session_setup_wrap_send(TALLOC_CTX
*mem_ctx
,
723 struct tevent_context
*ev
,
724 struct smbd_smb2_request
*smb2req
,
725 uint64_t in_session_id
,
727 uint8_t in_security_mode
,
728 uint64_t in_previous_session_id
,
729 DATA_BLOB in_security_buffer
)
731 struct tevent_req
*req
;
732 struct smbd_smb2_session_setup_wrap_state
*state
;
733 struct tevent_req
*subreq
;
735 req
= tevent_req_create(mem_ctx
, &state
,
736 struct smbd_smb2_session_setup_wrap_state
);
741 state
->smb2req
= smb2req
;
742 state
->in_session_id
= in_session_id
;
743 state
->in_flags
= in_flags
;
744 state
->in_security_mode
= in_security_mode
;
745 state
->in_previous_session_id
= in_previous_session_id
;
746 state
->in_security_buffer
= in_security_buffer
;
748 subreq
= smbd_smb2_session_setup_send(state
, state
->ev
,
750 state
->in_session_id
,
752 state
->in_security_mode
,
753 state
->in_previous_session_id
,
754 state
->in_security_buffer
);
755 if (tevent_req_nomem(subreq
, req
)) {
756 return tevent_req_post(req
, ev
);
758 tevent_req_set_callback(subreq
,
759 smbd_smb2_session_setup_wrap_setup_done
, req
);
764 static void smbd_smb2_session_setup_wrap_setup_done(struct tevent_req
*subreq
)
766 struct tevent_req
*req
=
767 tevent_req_callback_data(subreq
,
769 struct smbd_smb2_session_setup_wrap_state
*state
=
771 struct smbd_smb2_session_setup_wrap_state
);
774 status
= smbd_smb2_session_setup_recv(subreq
,
775 &state
->out_session_flags
,
777 &state
->out_security_buffer
,
778 &state
->out_session_id
);
780 if (NT_STATUS_IS_OK(status
)) {
781 tevent_req_done(req
);
784 if (NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
785 tevent_req_nterror(req
, status
);
789 if (state
->smb2req
->session
== NULL
) {
790 tevent_req_nterror(req
, status
);
794 state
->error
= status
;
796 subreq
= smb2srv_session_shutdown_send(state
, state
->ev
,
797 state
->smb2req
->session
,
799 if (tevent_req_nomem(subreq
, req
)) {
802 tevent_req_set_callback(subreq
,
803 smbd_smb2_session_setup_wrap_shutdown_done
,
807 static void smbd_smb2_session_setup_wrap_shutdown_done(struct tevent_req
*subreq
)
809 struct tevent_req
*req
=
810 tevent_req_callback_data(subreq
,
812 struct smbd_smb2_session_setup_wrap_state
*state
=
814 struct smbd_smb2_session_setup_wrap_state
);
817 status
= smb2srv_session_shutdown_recv(subreq
);
819 if (tevent_req_nterror(req
, status
)) {
824 * we may need to sign the response, so we need to keep
825 * the session until the response is sent to the wire.
827 talloc_steal(state
->smb2req
, state
->smb2req
->session
);
829 tevent_req_nterror(req
, state
->error
);
832 static NTSTATUS
smbd_smb2_session_setup_wrap_recv(struct tevent_req
*req
,
833 uint16_t *out_session_flags
,
835 DATA_BLOB
*out_security_buffer
,
836 uint64_t *out_session_id
)
838 struct smbd_smb2_session_setup_wrap_state
*state
=
840 struct smbd_smb2_session_setup_wrap_state
);
843 if (tevent_req_is_nterror(req
, &status
)) {
844 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
845 tevent_req_received(req
);
846 return nt_status_squash(status
);
849 status
= NT_STATUS_OK
;
852 *out_session_flags
= state
->out_session_flags
;
853 *out_security_buffer
= state
->out_security_buffer
;
854 *out_session_id
= state
->out_session_id
;
856 talloc_steal(mem_ctx
, out_security_buffer
->data
);
857 tevent_req_received(req
);
861 static struct tevent_req
*smbd_smb2_logoff_send(TALLOC_CTX
*mem_ctx
,
862 struct tevent_context
*ev
,
863 struct smbd_smb2_request
*smb2req
);
864 static NTSTATUS
smbd_smb2_logoff_recv(struct tevent_req
*req
);
865 static void smbd_smb2_request_logoff_done(struct tevent_req
*subreq
);
867 NTSTATUS
smbd_smb2_request_process_logoff(struct smbd_smb2_request
*req
)
870 struct tevent_req
*subreq
= NULL
;
872 status
= smbd_smb2_request_verify_sizes(req
, 0x04);
873 if (!NT_STATUS_IS_OK(status
)) {
874 return smbd_smb2_request_error(req
, status
);
877 subreq
= smbd_smb2_logoff_send(req
, req
->sconn
->ev_ctx
, req
);
878 if (subreq
== NULL
) {
879 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
881 tevent_req_set_callback(subreq
, smbd_smb2_request_logoff_done
, req
);
884 * Wait a long time before going async on this to allow
885 * requests we're waiting on to finish. Set timeout to 10 secs.
887 return smbd_smb2_request_pending_queue(req
, subreq
, 10000000);
890 static void smbd_smb2_request_logoff_done(struct tevent_req
*subreq
)
892 struct smbd_smb2_request
*smb2req
=
893 tevent_req_callback_data(subreq
,
894 struct smbd_smb2_request
);
899 status
= smbd_smb2_logoff_recv(subreq
);
901 if (!NT_STATUS_IS_OK(status
)) {
902 error
= smbd_smb2_request_error(smb2req
, status
);
903 if (!NT_STATUS_IS_OK(error
)) {
904 smbd_server_connection_terminate(smb2req
->sconn
,
911 outbody
= data_blob_talloc(smb2req
->out
.vector
, NULL
, 0x04);
912 if (outbody
.data
== NULL
) {
913 error
= smbd_smb2_request_error(smb2req
, NT_STATUS_NO_MEMORY
);
914 if (!NT_STATUS_IS_OK(error
)) {
915 smbd_server_connection_terminate(smb2req
->sconn
,
922 SSVAL(outbody
.data
, 0x00, 0x04); /* struct size */
923 SSVAL(outbody
.data
, 0x02, 0); /* reserved */
925 error
= smbd_smb2_request_done(smb2req
, outbody
, NULL
);
926 if (!NT_STATUS_IS_OK(error
)) {
927 smbd_server_connection_terminate(smb2req
->sconn
,
933 struct smbd_smb2_logoff_state
{
934 struct smbd_smb2_request
*smb2req
;
937 static void smbd_smb2_logoff_shutdown_done(struct tevent_req
*subreq
);
939 static struct tevent_req
*smbd_smb2_logoff_send(TALLOC_CTX
*mem_ctx
,
940 struct tevent_context
*ev
,
941 struct smbd_smb2_request
*smb2req
)
943 struct tevent_req
*req
;
944 struct smbd_smb2_logoff_state
*state
;
945 struct tevent_req
*subreq
;
947 req
= tevent_req_create(mem_ctx
, &state
,
948 struct smbd_smb2_logoff_state
);
952 state
->smb2req
= smb2req
;
954 subreq
= smb2srv_session_shutdown_send(state
, ev
,
957 if (tevent_req_nomem(subreq
, req
)) {
958 return tevent_req_post(req
, ev
);
960 tevent_req_set_callback(subreq
, smbd_smb2_logoff_shutdown_done
, req
);
965 static void smbd_smb2_logoff_shutdown_done(struct tevent_req
*subreq
)
967 struct tevent_req
*req
= tevent_req_callback_data(
968 subreq
, struct tevent_req
);
969 struct smbd_smb2_logoff_state
*state
= tevent_req_data(
970 req
, struct smbd_smb2_logoff_state
);
973 status
= smb2srv_session_shutdown_recv(subreq
);
974 if (tevent_req_nterror(req
, status
)) {
980 * As we've been awoken, we may have changed
981 * uid in the meantime. Ensure we're still
982 * root (SMB2_OP_LOGOFF has .as_root = true).
984 change_to_root_user();
986 status
= smbXsrv_session_logoff(state
->smb2req
->session
);
987 if (tevent_req_nterror(req
, status
)) {
992 * we may need to sign the response, so we need to keep
993 * the session until the response is sent to the wire.
995 talloc_steal(state
->smb2req
, state
->smb2req
->session
);
997 tevent_req_done(req
);
1000 static NTSTATUS
smbd_smb2_logoff_recv(struct tevent_req
*req
)
1002 return tevent_req_simple_recv_ntstatus(req
);