2 Unix SMB/CIFS implementation.
4 SMB2 client request handling
6 Copyright (C) Andrew Tridgell 2005
7 Copyright (C) Stefan Metzmacher 2005
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/smb2/smb2.h"
26 #include "../lib/util/dlinklist.h"
27 #include "lib/events/events.h"
28 #include "libcli/smb2/smb2_calls.h"
30 /* fill in the bufinfo */
31 void smb2_setup_bufinfo(struct smb2_request
*req
)
33 req
->in
.bufinfo
.mem_ctx
= req
;
34 req
->in
.bufinfo
.flags
= BUFINFO_FLAG_UNICODE
| BUFINFO_FLAG_SMB2
;
35 req
->in
.bufinfo
.align_base
= req
->in
.buffer
;
36 if (req
->in
.dynamic
) {
37 req
->in
.bufinfo
.data
= req
->in
.dynamic
;
38 req
->in
.bufinfo
.data_size
= req
->in
.body_size
- req
->in
.body_fixed
;
40 req
->in
.bufinfo
.data
= NULL
;
41 req
->in
.bufinfo
.data_size
= 0;
46 /* destroy a request structure */
47 static int smb2_request_destructor(struct smb2_request
*req
)
50 /* remove it from the list of pending requests (a null op if
51 its not in the list) */
52 DLIST_REMOVE(req
->transport
->pending_recv
, req
);
58 initialise a smb2 request
60 struct smb2_request
*smb2_request_init(struct smb2_transport
*transport
, uint16_t opcode
,
61 uint16_t body_fixed_size
, bool body_dynamic_present
,
62 uint32_t body_dynamic_size
)
64 struct smb2_request
*req
;
68 bool compound
= false;
70 if (body_dynamic_present
) {
71 if (body_dynamic_size
== 0) {
72 body_dynamic_size
= 1;
75 body_dynamic_size
= 0;
78 req
= talloc(transport
, struct smb2_request
);
79 if (req
== NULL
) return NULL
;
81 seqnum
= transport
->seqnum
;
82 if (transport
->credits
.charge
> 0) {
83 transport
->seqnum
+= transport
->credits
.charge
;
85 transport
->seqnum
+= 1;
88 req
->state
= SMB2_REQUEST_INIT
;
89 req
->transport
= transport
;
93 req
->status
= NT_STATUS_OK
;
95 req
->next
= req
->prev
= NULL
;
97 ZERO_STRUCT(req
->cancel
);
100 if (transport
->compound
.missing
> 0) {
102 transport
->compound
.missing
-= 1;
103 req
->out
= transport
->compound
.buffer
;
104 ZERO_STRUCT(transport
->compound
.buffer
);
105 if (transport
->compound
.related
) {
106 flags
|= SMB2_HDR_FLAG_CHAINED
;
109 ZERO_STRUCT(req
->out
);
112 if (req
->out
.size
> 0) {
113 hdr_offset
= req
->out
.size
;
115 hdr_offset
= NBT_HDR_SIZE
;
118 req
->out
.size
= hdr_offset
+ SMB2_HDR_BODY
+ body_fixed_size
;
119 req
->out
.allocated
= req
->out
.size
+ body_dynamic_size
;
121 req
->out
.buffer
= talloc_realloc(req
, req
->out
.buffer
,
122 uint8_t, req
->out
.allocated
);
123 if (req
->out
.buffer
== NULL
) {
128 req
->out
.hdr
= req
->out
.buffer
+ hdr_offset
;
129 req
->out
.body
= req
->out
.hdr
+ SMB2_HDR_BODY
;
130 req
->out
.body_fixed
= body_fixed_size
;
131 req
->out
.body_size
= body_fixed_size
;
132 req
->out
.dynamic
= (body_dynamic_size
? req
->out
.body
+ body_fixed_size
: NULL
);
134 SIVAL(req
->out
.hdr
, 0, SMB2_MAGIC
);
135 SSVAL(req
->out
.hdr
, SMB2_HDR_LENGTH
, SMB2_HDR_BODY
);
136 SSVAL(req
->out
.hdr
, SMB2_HDR_EPOCH
, transport
->credits
.charge
);
137 SIVAL(req
->out
.hdr
, SMB2_HDR_STATUS
, 0);
138 SSVAL(req
->out
.hdr
, SMB2_HDR_OPCODE
, opcode
);
139 SSVAL(req
->out
.hdr
, SMB2_HDR_CREDIT
, transport
->credits
.ask_num
);
140 SIVAL(req
->out
.hdr
, SMB2_HDR_FLAGS
, flags
);
141 SIVAL(req
->out
.hdr
, SMB2_HDR_NEXT_COMMAND
, 0);
142 SBVAL(req
->out
.hdr
, SMB2_HDR_MESSAGE_ID
, req
->seqnum
);
143 SIVAL(req
->out
.hdr
, SMB2_HDR_PID
, 0);
144 SIVAL(req
->out
.hdr
, SMB2_HDR_TID
, 0);
145 SBVAL(req
->out
.hdr
, SMB2_HDR_SESSION_ID
, 0);
146 memset(req
->out
.hdr
+SMB2_HDR_SIGNATURE
, 0, 16);
148 /* set the length of the fixed body part and +1 if there's a dynamic part also */
149 SSVAL(req
->out
.body
, 0, body_fixed_size
+ (body_dynamic_size
?1:0));
152 * if we have a dynamic part, make sure the first byte
153 * which is always be part of the packet is initialized
155 if (body_dynamic_size
&& !compound
) {
157 SCVAL(req
->out
.dynamic
, 0, 0);
160 talloc_set_destructor(req
, smb2_request_destructor
);
166 initialise a smb2 request for tree operations
168 struct smb2_request
*smb2_request_init_tree(struct smb2_tree
*tree
, uint16_t opcode
,
169 uint16_t body_fixed_size
, bool body_dynamic_present
,
170 uint32_t body_dynamic_size
)
172 struct smb2_request
*req
= smb2_request_init(tree
->session
->transport
, opcode
,
173 body_fixed_size
, body_dynamic_present
,
175 if (req
== NULL
) return NULL
;
177 SBVAL(req
->out
.hdr
, SMB2_HDR_SESSION_ID
, tree
->session
->uid
);
178 SIVAL(req
->out
.hdr
, SMB2_HDR_PID
, tree
->session
->pid
);
179 SIVAL(req
->out
.hdr
, SMB2_HDR_TID
, tree
->tid
);
180 req
->session
= tree
->session
;
186 /* destroy a request structure and return final status */
187 NTSTATUS
smb2_request_destroy(struct smb2_request
*req
)
191 /* this is the error code we give the application for when a
192 _send() call fails completely */
193 if (!req
) return NT_STATUS_UNSUCCESSFUL
;
195 if (req
->state
== SMB2_REQUEST_ERROR
&&
196 NT_STATUS_IS_OK(req
->status
)) {
197 status
= NT_STATUS_INTERNAL_ERROR
;
199 status
= req
->status
;
207 receive a response to a packet
209 bool smb2_request_receive(struct smb2_request
*req
)
211 /* req can be NULL when a send has failed. This eliminates lots of NULL
212 checks in each module */
213 if (!req
) return false;
215 /* keep receiving packets until this one is replied to */
216 while (req
->state
<= SMB2_REQUEST_RECV
) {
217 if (event_loop_once(req
->transport
->socket
->event
.ctx
) != 0) {
222 return req
->state
== SMB2_REQUEST_DONE
;
225 /* Return true if the last packet was in error */
226 bool smb2_request_is_error(struct smb2_request
*req
)
228 return NT_STATUS_IS_ERR(req
->status
);
231 /* Return true if the last packet was OK */
232 bool smb2_request_is_ok(struct smb2_request
*req
)
234 return NT_STATUS_IS_OK(req
->status
);
238 check if a range in the reply body is out of bounds
240 bool smb2_oob(struct smb2_request_buffer
*buf
, const uint8_t *ptr
, size_t size
)
243 /* zero bytes is never out of range */
246 /* be careful with wraparound! */
247 if ((uintptr_t)ptr
< (uintptr_t)buf
->body
||
248 (uintptr_t)ptr
>= (uintptr_t)buf
->body
+ buf
->body_size
||
249 size
> buf
->body_size
||
250 (uintptr_t)ptr
+ size
> (uintptr_t)buf
->body
+ buf
->body_size
) {
256 size_t smb2_padding_size(uint32_t offset
, size_t n
)
258 if ((offset
& (n
-1)) == 0) return 0;
259 return n
- (offset
& (n
-1));
262 static size_t smb2_padding_fix(struct smb2_request_buffer
*buf
)
264 if (buf
->dynamic
== (buf
->body
+ buf
->body_fixed
)) {
265 if (buf
->dynamic
!= (buf
->buffer
+ buf
->size
)) {
273 grow a SMB2 buffer by the specified amount
275 NTSTATUS
smb2_grow_buffer(struct smb2_request_buffer
*buf
, size_t increase
)
280 uint32_t newsize
= buf
->size
+ increase
;
282 /* a packet size should be limited a bit */
283 if (newsize
>= 0x00FFFFFF) return NT_STATUS_MARSHALL_OVERFLOW
;
285 if (newsize
<= buf
->allocated
) return NT_STATUS_OK
;
287 hdr_ofs
= buf
->hdr
- buf
->buffer
;
288 dynamic_ofs
= buf
->dynamic
- buf
->buffer
;
290 buffer_ptr
= talloc_realloc(buf
, buf
->buffer
, uint8_t, newsize
);
291 NT_STATUS_HAVE_NO_MEMORY(buffer_ptr
);
293 buf
->buffer
= buffer_ptr
;
294 buf
->hdr
= buf
->buffer
+ hdr_ofs
;
295 buf
->body
= buf
->hdr
+ SMB2_HDR_BODY
;
296 buf
->dynamic
= buf
->buffer
+ dynamic_ofs
;
297 buf
->allocated
= newsize
;
303 pull a uint16_t ofs/ uint16_t length/blob triple from a data blob
304 the ptr points to the start of the offset/length pair
306 NTSTATUS
smb2_pull_o16s16_blob(struct smb2_request_buffer
*buf
, TALLOC_CTX
*mem_ctx
, uint8_t *ptr
, DATA_BLOB
*blob
)
309 if (smb2_oob(buf
, ptr
, 4)) {
310 return NT_STATUS_INVALID_PARAMETER
;
315 *blob
= data_blob(NULL
, 0);
318 if (smb2_oob(buf
, buf
->hdr
+ ofs
, size
)) {
319 return NT_STATUS_INVALID_PARAMETER
;
321 *blob
= data_blob_talloc(mem_ctx
, buf
->hdr
+ ofs
, size
);
322 NT_STATUS_HAVE_NO_MEMORY(blob
->data
);
327 push a uint16_t ofs/ uint16_t length/blob triple into a data blob
328 the ofs points to the start of the offset/length pair, and is relative
331 NTSTATUS
smb2_push_o16s16_blob(struct smb2_request_buffer
*buf
,
332 uint16_t ofs
, DATA_BLOB blob
)
336 size_t padding_length
;
338 uint8_t *ptr
= buf
->body
+ofs
;
340 if (buf
->dynamic
== NULL
) {
341 return NT_STATUS_INVALID_PARAMETER
;
344 /* we have only 16 bit for the size */
345 if (blob
.length
> 0xFFFF) {
346 return NT_STATUS_INVALID_PARAMETER
;
349 /* check if there're enough room for ofs and size */
350 if (smb2_oob(buf
, ptr
, 4)) {
351 return NT_STATUS_INVALID_PARAMETER
;
354 if (blob
.data
== NULL
) {
355 if (blob
.length
!= 0) {
356 return NT_STATUS_INTERNAL_ERROR
;
363 offset
= buf
->dynamic
- buf
->hdr
;
364 padding_length
= smb2_padding_size(offset
, 2);
365 offset
+= padding_length
;
366 padding_fix
= smb2_padding_fix(buf
);
368 SSVAL(ptr
, 0, offset
);
369 SSVAL(ptr
, 2, blob
.length
);
371 status
= smb2_grow_buffer(buf
, blob
.length
+ padding_length
- padding_fix
);
372 NT_STATUS_NOT_OK_RETURN(status
);
374 memset(buf
->dynamic
, 0, padding_length
);
375 buf
->dynamic
+= padding_length
;
377 memcpy(buf
->dynamic
, blob
.data
, blob
.length
);
378 buf
->dynamic
+= blob
.length
;
380 buf
->size
+= blob
.length
+ padding_length
- padding_fix
;
381 buf
->body_size
+= blob
.length
+ padding_length
;
388 push a uint16_t ofs/ uint32_t length/blob triple into a data blob
389 the ofs points to the start of the offset/length pair, and is relative
392 NTSTATUS
smb2_push_o16s32_blob(struct smb2_request_buffer
*buf
,
393 uint16_t ofs
, DATA_BLOB blob
)
397 size_t padding_length
;
399 uint8_t *ptr
= buf
->body
+ofs
;
401 if (buf
->dynamic
== NULL
) {
402 return NT_STATUS_INVALID_PARAMETER
;
405 /* check if there're enough room for ofs and size */
406 if (smb2_oob(buf
, ptr
, 6)) {
407 return NT_STATUS_INVALID_PARAMETER
;
410 if (blob
.data
== NULL
) {
411 if (blob
.length
!= 0) {
412 return NT_STATUS_INTERNAL_ERROR
;
419 offset
= buf
->dynamic
- buf
->hdr
;
420 padding_length
= smb2_padding_size(offset
, 2);
421 offset
+= padding_length
;
422 padding_fix
= smb2_padding_fix(buf
);
424 SSVAL(ptr
, 0, offset
);
425 SIVAL(ptr
, 2, blob
.length
);
427 status
= smb2_grow_buffer(buf
, blob
.length
+ padding_length
- padding_fix
);
428 NT_STATUS_NOT_OK_RETURN(status
);
430 memset(buf
->dynamic
, 0, padding_length
);
431 buf
->dynamic
+= padding_length
;
433 memcpy(buf
->dynamic
, blob
.data
, blob
.length
);
434 buf
->dynamic
+= blob
.length
;
436 buf
->size
+= blob
.length
+ padding_length
- padding_fix
;
437 buf
->body_size
+= blob
.length
+ padding_length
;
444 push a uint32_t ofs/ uint32_t length/blob triple into a data blob
445 the ofs points to the start of the offset/length pair, and is relative
448 NTSTATUS
smb2_push_o32s32_blob(struct smb2_request_buffer
*buf
,
449 uint32_t ofs
, DATA_BLOB blob
)
453 size_t padding_length
;
455 uint8_t *ptr
= buf
->body
+ofs
;
457 if (buf
->dynamic
== NULL
) {
458 return NT_STATUS_INVALID_PARAMETER
;
461 /* check if there're enough room for ofs and size */
462 if (smb2_oob(buf
, ptr
, 8)) {
463 return NT_STATUS_INVALID_PARAMETER
;
466 if (blob
.data
== NULL
) {
467 if (blob
.length
!= 0) {
468 return NT_STATUS_INTERNAL_ERROR
;
475 offset
= buf
->dynamic
- buf
->hdr
;
476 padding_length
= smb2_padding_size(offset
, 8);
477 offset
+= padding_length
;
478 padding_fix
= smb2_padding_fix(buf
);
480 SIVAL(ptr
, 0, offset
);
481 SIVAL(ptr
, 4, blob
.length
);
483 status
= smb2_grow_buffer(buf
, blob
.length
+ padding_length
- padding_fix
);
484 NT_STATUS_NOT_OK_RETURN(status
);
486 memset(buf
->dynamic
, 0, padding_length
);
487 buf
->dynamic
+= padding_length
;
489 memcpy(buf
->dynamic
, blob
.data
, blob
.length
);
490 buf
->dynamic
+= blob
.length
;
492 buf
->size
+= blob
.length
+ padding_length
- padding_fix
;
493 buf
->body_size
+= blob
.length
+ padding_length
;
500 push a uint32_t length/ uint32_t ofs/blob triple into a data blob
501 the ofs points to the start of the length/offset pair, and is relative
504 NTSTATUS
smb2_push_s32o32_blob(struct smb2_request_buffer
*buf
,
505 uint32_t ofs
, DATA_BLOB blob
)
509 size_t padding_length
;
511 uint8_t *ptr
= buf
->body
+ofs
;
513 if (buf
->dynamic
== NULL
) {
514 return NT_STATUS_INVALID_PARAMETER
;
517 /* check if there're enough room for ofs and size */
518 if (smb2_oob(buf
, ptr
, 8)) {
519 return NT_STATUS_INVALID_PARAMETER
;
522 if (blob
.data
== NULL
) {
523 if (blob
.length
!= 0) {
524 return NT_STATUS_INTERNAL_ERROR
;
531 offset
= buf
->dynamic
- buf
->hdr
;
532 padding_length
= smb2_padding_size(offset
, 8);
533 offset
+= padding_length
;
534 padding_fix
= smb2_padding_fix(buf
);
536 SIVAL(ptr
, 0, blob
.length
);
537 SIVAL(ptr
, 4, offset
);
539 status
= smb2_grow_buffer(buf
, blob
.length
+ padding_length
- padding_fix
);
540 NT_STATUS_NOT_OK_RETURN(status
);
542 memset(buf
->dynamic
, 0, padding_length
);
543 buf
->dynamic
+= padding_length
;
545 memcpy(buf
->dynamic
, blob
.data
, blob
.length
);
546 buf
->dynamic
+= blob
.length
;
548 buf
->size
+= blob
.length
+ padding_length
- padding_fix
;
549 buf
->body_size
+= blob
.length
+ padding_length
;
555 pull a uint16_t ofs/ uint32_t length/blob triple from a data blob
556 the ptr points to the start of the offset/length pair
558 NTSTATUS
smb2_pull_o16s32_blob(struct smb2_request_buffer
*buf
, TALLOC_CTX
*mem_ctx
, uint8_t *ptr
, DATA_BLOB
*blob
)
563 if (smb2_oob(buf
, ptr
, 6)) {
564 return NT_STATUS_INVALID_PARAMETER
;
569 *blob
= data_blob(NULL
, 0);
572 if (smb2_oob(buf
, buf
->hdr
+ ofs
, size
)) {
573 return NT_STATUS_INVALID_PARAMETER
;
575 *blob
= data_blob_talloc(mem_ctx
, buf
->hdr
+ ofs
, size
);
576 NT_STATUS_HAVE_NO_MEMORY(blob
->data
);
581 pull a uint32_t ofs/ uint32_t length/blob triple from a data blob
582 the ptr points to the start of the offset/length pair
584 NTSTATUS
smb2_pull_o32s32_blob(struct smb2_request_buffer
*buf
, TALLOC_CTX
*mem_ctx
, uint8_t *ptr
, DATA_BLOB
*blob
)
587 if (smb2_oob(buf
, ptr
, 8)) {
588 return NT_STATUS_INVALID_PARAMETER
;
593 *blob
= data_blob(NULL
, 0);
596 if (smb2_oob(buf
, buf
->hdr
+ ofs
, size
)) {
597 return NT_STATUS_INVALID_PARAMETER
;
599 *blob
= data_blob_talloc(mem_ctx
, buf
->hdr
+ ofs
, size
);
600 NT_STATUS_HAVE_NO_MEMORY(blob
->data
);
605 pull a uint16_t ofs/ uint32_t length/blob triple from a data blob
606 the ptr points to the start of the offset/length pair
608 In this varient the uint16_t is padded by an extra 2 bytes, making
609 the size aligned on 4 byte boundary
611 NTSTATUS
smb2_pull_o16As32_blob(struct smb2_request_buffer
*buf
, TALLOC_CTX
*mem_ctx
, uint8_t *ptr
, DATA_BLOB
*blob
)
614 if (smb2_oob(buf
, ptr
, 8)) {
615 return NT_STATUS_INVALID_PARAMETER
;
620 *blob
= data_blob(NULL
, 0);
623 if (smb2_oob(buf
, buf
->hdr
+ ofs
, size
)) {
624 return NT_STATUS_INVALID_PARAMETER
;
626 *blob
= data_blob_talloc(mem_ctx
, buf
->hdr
+ ofs
, size
);
627 NT_STATUS_HAVE_NO_MEMORY(blob
->data
);
632 pull a uint32_t length/ uint32_t ofs/blob triple from a data blob
633 the ptr points to the start of the offset/length pair
635 NTSTATUS
smb2_pull_s32o32_blob(struct smb2_request_buffer
*buf
, TALLOC_CTX
*mem_ctx
, uint8_t *ptr
, DATA_BLOB
*blob
)
638 if (smb2_oob(buf
, ptr
, 8)) {
639 return NT_STATUS_INVALID_PARAMETER
;
644 *blob
= data_blob(NULL
, 0);
647 if (smb2_oob(buf
, buf
->hdr
+ ofs
, size
)) {
648 return NT_STATUS_INVALID_PARAMETER
;
650 *blob
= data_blob_talloc(mem_ctx
, buf
->hdr
+ ofs
, size
);
651 NT_STATUS_HAVE_NO_MEMORY(blob
->data
);
656 pull a uint32_t length/ uint16_t ofs/blob triple from a data blob
657 the ptr points to the start of the offset/length pair
659 NTSTATUS
smb2_pull_s32o16_blob(struct smb2_request_buffer
*buf
, TALLOC_CTX
*mem_ctx
, uint8_t *ptr
, DATA_BLOB
*blob
)
662 if (smb2_oob(buf
, ptr
, 8)) {
663 return NT_STATUS_INVALID_PARAMETER
;
668 *blob
= data_blob(NULL
, 0);
671 if (smb2_oob(buf
, buf
->hdr
+ ofs
, size
)) {
672 return NT_STATUS_INVALID_PARAMETER
;
674 *blob
= data_blob_talloc(mem_ctx
, buf
->hdr
+ ofs
, size
);
675 NT_STATUS_HAVE_NO_MEMORY(blob
->data
);
680 pull a string in a uint16_t ofs/ uint16_t length/blob format
681 UTF-16 without termination
683 NTSTATUS
smb2_pull_o16s16_string(struct smb2_request_buffer
*buf
, TALLOC_CTX
*mem_ctx
,
684 uint8_t *ptr
, const char **str
)
691 status
= smb2_pull_o16s16_blob(buf
, mem_ctx
, ptr
, &blob
);
692 NT_STATUS_NOT_OK_RETURN(status
);
694 if (blob
.data
== NULL
) {
699 if (blob
.length
== 0) {
701 s
= talloc_strdup(mem_ctx
, "");
702 NT_STATUS_HAVE_NO_MEMORY(s
);
707 ret
= convert_string_talloc(mem_ctx
, CH_UTF16
, CH_UNIX
,
708 blob
.data
, blob
.length
, &vstr
, NULL
, false);
709 data_blob_free(&blob
);
710 (*str
) = (char *)vstr
;
712 return NT_STATUS_ILLEGAL_CHARACTER
;
718 push a string in a uint16_t ofs/ uint16_t length/blob format
719 UTF-16 without termination
721 NTSTATUS
smb2_push_o16s16_string(struct smb2_request_buffer
*buf
,
722 uint16_t ofs
, const char *str
)
729 return smb2_push_o16s16_blob(buf
, ofs
, data_blob(NULL
, 0));
733 blob
.data
= discard_const_p(uint8_t, str
);
735 return smb2_push_o16s16_blob(buf
, ofs
, blob
);
738 ret
= convert_string_talloc(buf
->buffer
, CH_UNIX
, CH_UTF16
,
739 str
, strlen(str
), (void **)&blob
.data
, &blob
.length
,
742 return NT_STATUS_ILLEGAL_CHARACTER
;
745 status
= smb2_push_o16s16_blob(buf
, ofs
, blob
);
746 data_blob_free(&blob
);
751 push a file handle into a buffer
753 void smb2_push_handle(uint8_t *data
, struct smb2_handle
*h
)
755 SBVAL(data
, 0, h
->data
[0]);
756 SBVAL(data
, 8, h
->data
[1]);
760 pull a file handle from a buffer
762 void smb2_pull_handle(uint8_t *ptr
, struct smb2_handle
*h
)
764 h
->data
[0] = BVAL(ptr
, 0);
765 h
->data
[1] = BVAL(ptr
, 8);