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;
69 if (body_dynamic_present) {
70 if (body_dynamic_size == 0) {
71 body_dynamic_size = 1;
74 body_dynamic_size = 0;
77 req = talloc(transport, struct smb2_request);
78 if (req == NULL) return NULL;
80 seqnum = transport->seqnum++;
81 if (seqnum == UINT64_MAX) {
82 seqnum = transport->seqnum++;
85 req->state = SMB2_REQUEST_INIT;
86 req->transport = transport;
90 req->status = NT_STATUS_OK;
92 req->next = req->prev = NULL;
94 ZERO_STRUCT(req->cancel);
97 if (transport->compound.missing > 0) {
98 transport->compound.missing -= 1;
99 req->out = transport->compound.buffer;
100 ZERO_STRUCT(transport->compound.buffer);
101 if (transport->compound.related) {
102 flags |= SMB2_HDR_FLAG_CHAINED;
105 ZERO_STRUCT(req->out);
108 if (req->out.size > 0) {
109 hdr_offset = req->out.size;
111 hdr_offset = NBT_HDR_SIZE;
114 req->out.size = hdr_offset + SMB2_HDR_BODY + body_fixed_size;
115 req->out.allocated = req->out.size + body_dynamic_size;
117 req->out.buffer = talloc_realloc(req, req->out.buffer,
118 uint8_t, req->out.allocated);
119 if (req->out.buffer == NULL) {
124 req->out.hdr = req->out.buffer + hdr_offset;
125 req->out.body = req->out.hdr + SMB2_HDR_BODY;
126 req->out.body_fixed= body_fixed_size;
127 req->out.body_size = body_fixed_size;
128 req->out.dynamic = (body_dynamic_size ? req->out.body + body_fixed_size : NULL);
130 SIVAL(req->out.hdr, 0, SMB2_MAGIC);
131 SSVAL(req->out.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
132 SSVAL(req->out.hdr, SMB2_HDR_EPOCH, 0);
133 SIVAL(req->out.hdr, SMB2_HDR_STATUS, 0);
134 SSVAL(req->out.hdr, SMB2_HDR_OPCODE, opcode);
135 SSVAL(req->out.hdr, SMB2_HDR_CREDIT, 0);
136 SIVAL(req->out.hdr, SMB2_HDR_FLAGS, flags);
137 SIVAL(req->out.hdr, SMB2_HDR_NEXT_COMMAND, 0);
138 SBVAL(req->out.hdr, SMB2_HDR_MESSAGE_ID, req->seqnum);
139 SIVAL(req->out.hdr, SMB2_HDR_PID, 0);
140 SIVAL(req->out.hdr, SMB2_HDR_TID, 0);
141 SBVAL(req->out.hdr, SMB2_HDR_SESSION_ID, 0);
142 memset(req->out.hdr+SMB2_HDR_SIGNATURE, 0, 16);
144 /* set the length of the fixed body part and +1 if there's a dynamic part also */
145 SSVAL(req->out.body, 0, body_fixed_size + (body_dynamic_size?1:0));
148 * if we have a dynamic part, make sure the first byte
149 * which is always be part of the packet is initialized
151 if (body_dynamic_size) {
153 SCVAL(req->out.dynamic, 0, 0);
156 talloc_set_destructor(req, smb2_request_destructor);
162 initialise a smb2 request for tree operations
164 struct smb2_request *smb2_request_init_tree(struct smb2_tree *tree, uint16_t opcode,
165 uint16_t body_fixed_size, bool body_dynamic_present,
166 uint32_t body_dynamic_size)
168 struct smb2_request *req = smb2_request_init(tree->session->transport, opcode,
169 body_fixed_size, body_dynamic_present,
171 if (req == NULL) return NULL;
173 SBVAL(req->out.hdr, SMB2_HDR_SESSION_ID, tree->session->uid);
174 SIVAL(req->out.hdr, SMB2_HDR_TID, tree->tid);
175 req->session = tree->session;
181 /* destroy a request structure and return final status */
182 NTSTATUS smb2_request_destroy(struct smb2_request *req)
186 /* this is the error code we give the application for when a
187 _send() call fails completely */
188 if (!req) return NT_STATUS_UNSUCCESSFUL;
190 if (req->state == SMB2_REQUEST_ERROR &&
191 NT_STATUS_IS_OK(req->status)) {
192 status = NT_STATUS_INTERNAL_ERROR;
194 status = req->status;
202 receive a response to a packet
204 bool smb2_request_receive(struct smb2_request *req)
206 /* req can be NULL when a send has failed. This eliminates lots of NULL
207 checks in each module */
208 if (!req) return false;
210 /* keep receiving packets until this one is replied to */
211 while (req->state <= SMB2_REQUEST_RECV) {
212 if (event_loop_once(req->transport->socket->event.ctx) != 0) {
217 return req->state == SMB2_REQUEST_DONE;
220 /* Return true if the last packet was in error */
221 bool smb2_request_is_error(struct smb2_request *req)
223 return NT_STATUS_IS_ERR(req->status);
226 /* Return true if the last packet was OK */
227 bool smb2_request_is_ok(struct smb2_request *req)
229 return NT_STATUS_IS_OK(req->status);
233 check if a range in the reply body is out of bounds
235 bool smb2_oob(struct smb2_request_buffer *buf, const uint8_t *ptr, size_t size)
238 /* zero bytes is never out of range */
241 /* be careful with wraparound! */
242 if ((uintptr_t)ptr < (uintptr_t)buf->body ||
243 (uintptr_t)ptr >= (uintptr_t)buf->body + buf->body_size ||
244 size > buf->body_size ||
245 (uintptr_t)ptr + size > (uintptr_t)buf->body + buf->body_size) {
251 size_t smb2_padding_size(uint32_t offset, size_t n)
253 if ((offset & (n-1)) == 0) return 0;
254 return n - (offset & (n-1));
257 static size_t smb2_padding_fix(struct smb2_request_buffer *buf)
259 if (buf->dynamic == (buf->body + buf->body_fixed)) {
266 grow a SMB2 buffer by the specified amount
268 NTSTATUS smb2_grow_buffer(struct smb2_request_buffer *buf, size_t increase)
273 uint32_t newsize = buf->size + increase;
275 /* a packet size should be limited a bit */
276 if (newsize >= 0x00FFFFFF) return NT_STATUS_MARSHALL_OVERFLOW;
278 if (newsize <= buf->allocated) return NT_STATUS_OK;
280 hdr_ofs = buf->hdr - buf->buffer;
281 dynamic_ofs = buf->dynamic - buf->buffer;
283 buffer_ptr = talloc_realloc(buf, buf->buffer, uint8_t, newsize);
284 NT_STATUS_HAVE_NO_MEMORY(buffer_ptr);
286 buf->buffer = buffer_ptr;
287 buf->hdr = buf->buffer + hdr_ofs;
288 buf->body = buf->hdr + SMB2_HDR_BODY;
289 buf->dynamic = buf->buffer + dynamic_ofs;
290 buf->allocated = newsize;
296 pull a uint16_t ofs/ uint16_t length/blob triple from a data blob
297 the ptr points to the start of the offset/length pair
299 NTSTATUS smb2_pull_o16s16_blob(struct smb2_request_buffer *buf, TALLOC_CTX *mem_ctx, uint8_t *ptr, DATA_BLOB *blob)
302 if (smb2_oob(buf, ptr, 4)) {
303 return NT_STATUS_INVALID_PARAMETER;
308 *blob = data_blob(NULL, 0);
311 if (smb2_oob(buf, buf->hdr + ofs, size)) {
312 return NT_STATUS_INVALID_PARAMETER;
314 *blob = data_blob_talloc(mem_ctx, buf->hdr + ofs, size);
315 NT_STATUS_HAVE_NO_MEMORY(blob->data);
320 push a uint16_t ofs/ uint16_t length/blob triple into a data blob
321 the ofs points to the start of the offset/length pair, and is relative
324 NTSTATUS smb2_push_o16s16_blob(struct smb2_request_buffer *buf,
325 uint16_t ofs, DATA_BLOB blob)
329 size_t padding_length;
331 uint8_t *ptr = buf->body+ofs;
333 if (buf->dynamic == NULL) {
334 return NT_STATUS_INVALID_PARAMETER;
337 /* we have only 16 bit for the size */
338 if (blob.length > 0xFFFF) {
339 return NT_STATUS_INVALID_PARAMETER;
342 /* check if there're enough room for ofs and size */
343 if (smb2_oob(buf, ptr, 4)) {
344 return NT_STATUS_INVALID_PARAMETER;
347 if (blob.data == NULL) {
348 if (blob.length != 0) {
349 return NT_STATUS_INTERNAL_ERROR;
356 offset = buf->dynamic - buf->hdr;
357 padding_length = smb2_padding_size(offset, 2);
358 offset += padding_length;
359 padding_fix = smb2_padding_fix(buf);
361 SSVAL(ptr, 0, offset);
362 SSVAL(ptr, 2, blob.length);
364 status = smb2_grow_buffer(buf, blob.length + padding_length - padding_fix);
365 NT_STATUS_NOT_OK_RETURN(status);
367 memset(buf->dynamic, 0, padding_length);
368 buf->dynamic += padding_length;
370 memcpy(buf->dynamic, blob.data, blob.length);
371 buf->dynamic += blob.length;
373 buf->size += blob.length + padding_length - padding_fix;
374 buf->body_size += blob.length + padding_length;
381 push a uint16_t ofs/ uint32_t length/blob triple into a data blob
382 the ofs points to the start of the offset/length pair, and is relative
385 NTSTATUS smb2_push_o16s32_blob(struct smb2_request_buffer *buf,
386 uint16_t ofs, DATA_BLOB blob)
390 size_t padding_length;
392 uint8_t *ptr = buf->body+ofs;
394 if (buf->dynamic == NULL) {
395 return NT_STATUS_INVALID_PARAMETER;
398 /* check if there're enough room for ofs and size */
399 if (smb2_oob(buf, ptr, 6)) {
400 return NT_STATUS_INVALID_PARAMETER;
403 if (blob.data == NULL) {
404 if (blob.length != 0) {
405 return NT_STATUS_INTERNAL_ERROR;
412 offset = buf->dynamic - buf->hdr;
413 padding_length = smb2_padding_size(offset, 2);
414 offset += padding_length;
415 padding_fix = smb2_padding_fix(buf);
417 SSVAL(ptr, 0, offset);
418 SIVAL(ptr, 2, blob.length);
420 status = smb2_grow_buffer(buf, blob.length + padding_length - padding_fix);
421 NT_STATUS_NOT_OK_RETURN(status);
423 memset(buf->dynamic, 0, padding_length);
424 buf->dynamic += padding_length;
426 memcpy(buf->dynamic, blob.data, blob.length);
427 buf->dynamic += blob.length;
429 buf->size += blob.length + padding_length - padding_fix;
430 buf->body_size += blob.length + padding_length;
437 push a uint32_t ofs/ uint32_t length/blob triple into a data blob
438 the ofs points to the start of the offset/length pair, and is relative
441 NTSTATUS smb2_push_o32s32_blob(struct smb2_request_buffer *buf,
442 uint32_t ofs, DATA_BLOB blob)
446 size_t padding_length;
448 uint8_t *ptr = buf->body+ofs;
450 if (buf->dynamic == NULL) {
451 return NT_STATUS_INVALID_PARAMETER;
454 /* check if there're enough room for ofs and size */
455 if (smb2_oob(buf, ptr, 8)) {
456 return NT_STATUS_INVALID_PARAMETER;
459 if (blob.data == NULL) {
460 if (blob.length != 0) {
461 return NT_STATUS_INTERNAL_ERROR;
468 offset = buf->dynamic - buf->hdr;
469 padding_length = smb2_padding_size(offset, 8);
470 offset += padding_length;
471 padding_fix = smb2_padding_fix(buf);
473 SIVAL(ptr, 0, offset);
474 SIVAL(ptr, 4, blob.length);
476 status = smb2_grow_buffer(buf, blob.length + padding_length - padding_fix);
477 NT_STATUS_NOT_OK_RETURN(status);
479 memset(buf->dynamic, 0, padding_length);
480 buf->dynamic += padding_length;
482 memcpy(buf->dynamic, blob.data, blob.length);
483 buf->dynamic += blob.length;
485 buf->size += blob.length + padding_length - padding_fix;
486 buf->body_size += blob.length + padding_length;
493 push a uint32_t length/ uint32_t ofs/blob triple into a data blob
494 the ofs points to the start of the length/offset pair, and is relative
497 NTSTATUS smb2_push_s32o32_blob(struct smb2_request_buffer *buf,
498 uint32_t ofs, DATA_BLOB blob)
502 size_t padding_length;
504 uint8_t *ptr = buf->body+ofs;
506 if (buf->dynamic == NULL) {
507 return NT_STATUS_INVALID_PARAMETER;
510 /* check if there're enough room for ofs and size */
511 if (smb2_oob(buf, ptr, 8)) {
512 return NT_STATUS_INVALID_PARAMETER;
515 if (blob.data == NULL) {
516 if (blob.length != 0) {
517 return NT_STATUS_INTERNAL_ERROR;
524 offset = buf->dynamic - buf->hdr;
525 padding_length = smb2_padding_size(offset, 8);
526 offset += padding_length;
527 padding_fix = smb2_padding_fix(buf);
529 SIVAL(ptr, 0, blob.length);
530 SIVAL(ptr, 4, offset);
532 status = smb2_grow_buffer(buf, blob.length + padding_length - padding_fix);
533 NT_STATUS_NOT_OK_RETURN(status);
535 memset(buf->dynamic, 0, padding_length);
536 buf->dynamic += padding_length;
538 memcpy(buf->dynamic, blob.data, blob.length);
539 buf->dynamic += blob.length;
541 buf->size += blob.length + padding_length - padding_fix;
542 buf->body_size += blob.length + padding_length;
548 pull a uint16_t ofs/ uint32_t length/blob triple from a data blob
549 the ptr points to the start of the offset/length pair
551 NTSTATUS smb2_pull_o16s32_blob(struct smb2_request_buffer *buf, TALLOC_CTX *mem_ctx, uint8_t *ptr, DATA_BLOB *blob)
556 if (smb2_oob(buf, ptr, 6)) {
557 return NT_STATUS_INVALID_PARAMETER;
562 *blob = data_blob(NULL, 0);
565 if (smb2_oob(buf, buf->hdr + ofs, size)) {
566 return NT_STATUS_INVALID_PARAMETER;
568 *blob = data_blob_talloc(mem_ctx, buf->hdr + ofs, size);
569 NT_STATUS_HAVE_NO_MEMORY(blob->data);
574 pull a uint32_t ofs/ uint32_t length/blob triple from a data blob
575 the ptr points to the start of the offset/length pair
577 NTSTATUS smb2_pull_o32s32_blob(struct smb2_request_buffer *buf, TALLOC_CTX *mem_ctx, uint8_t *ptr, DATA_BLOB *blob)
580 if (smb2_oob(buf, ptr, 8)) {
581 return NT_STATUS_INVALID_PARAMETER;
586 *blob = data_blob(NULL, 0);
589 if (smb2_oob(buf, buf->hdr + ofs, size)) {
590 return NT_STATUS_INVALID_PARAMETER;
592 *blob = data_blob_talloc(mem_ctx, buf->hdr + ofs, size);
593 NT_STATUS_HAVE_NO_MEMORY(blob->data);
598 pull a uint16_t ofs/ uint32_t length/blob triple from a data blob
599 the ptr points to the start of the offset/length pair
601 In this varient the uint16_t is padded by an extra 2 bytes, making
602 the size aligned on 4 byte boundary
604 NTSTATUS smb2_pull_o16As32_blob(struct smb2_request_buffer *buf, TALLOC_CTX *mem_ctx, uint8_t *ptr, DATA_BLOB *blob)
607 if (smb2_oob(buf, ptr, 8)) {
608 return NT_STATUS_INVALID_PARAMETER;
613 *blob = data_blob(NULL, 0);
616 if (smb2_oob(buf, buf->hdr + ofs, size)) {
617 return NT_STATUS_INVALID_PARAMETER;
619 *blob = data_blob_talloc(mem_ctx, buf->hdr + ofs, size);
620 NT_STATUS_HAVE_NO_MEMORY(blob->data);
625 pull a uint32_t length/ uint32_t ofs/blob triple from a data blob
626 the ptr points to the start of the offset/length pair
628 NTSTATUS smb2_pull_s32o32_blob(struct smb2_request_buffer *buf, TALLOC_CTX *mem_ctx, uint8_t *ptr, DATA_BLOB *blob)
631 if (smb2_oob(buf, ptr, 8)) {
632 return NT_STATUS_INVALID_PARAMETER;
637 *blob = data_blob(NULL, 0);
640 if (smb2_oob(buf, buf->hdr + ofs, size)) {
641 return NT_STATUS_INVALID_PARAMETER;
643 *blob = data_blob_talloc(mem_ctx, buf->hdr + ofs, size);
644 NT_STATUS_HAVE_NO_MEMORY(blob->data);
649 pull a uint32_t length/ uint16_t ofs/blob triple from a data blob
650 the ptr points to the start of the offset/length pair
652 NTSTATUS smb2_pull_s32o16_blob(struct smb2_request_buffer *buf, TALLOC_CTX *mem_ctx, uint8_t *ptr, DATA_BLOB *blob)
655 if (smb2_oob(buf, ptr, 8)) {
656 return NT_STATUS_INVALID_PARAMETER;
661 *blob = data_blob(NULL, 0);
664 if (smb2_oob(buf, buf->hdr + ofs, size)) {
665 return NT_STATUS_INVALID_PARAMETER;
667 *blob = data_blob_talloc(mem_ctx, buf->hdr + ofs, size);
668 NT_STATUS_HAVE_NO_MEMORY(blob->data);
673 pull a string in a uint16_t ofs/ uint16_t length/blob format
674 UTF-16 without termination
676 NTSTATUS smb2_pull_o16s16_string(struct smb2_request_buffer *buf, TALLOC_CTX *mem_ctx,
677 uint8_t *ptr, const char **str)
684 status = smb2_pull_o16s16_blob(buf, mem_ctx, ptr, &blob);
685 NT_STATUS_NOT_OK_RETURN(status);
687 if (blob.data == NULL) {
692 if (blob.length == 0) {
694 s = talloc_strdup(mem_ctx, "");
695 NT_STATUS_HAVE_NO_MEMORY(s);
700 ret = convert_string_talloc(mem_ctx, CH_UTF16, CH_UNIX,
701 blob.data, blob.length, &vstr, NULL, false);
702 data_blob_free(&blob);
703 (*str) = (char *)vstr;
705 return NT_STATUS_ILLEGAL_CHARACTER;
711 push a string in a uint16_t ofs/ uint16_t length/blob format
712 UTF-16 without termination
714 NTSTATUS smb2_push_o16s16_string(struct smb2_request_buffer *buf,
715 uint16_t ofs, const char *str)
722 return smb2_push_o16s16_blob(buf, ofs, data_blob(NULL, 0));
726 blob.data = discard_const_p(uint8_t, str);
728 return smb2_push_o16s16_blob(buf, ofs, blob);
731 ret = convert_string_talloc(buf->buffer, CH_UNIX, CH_UTF16,
732 str, strlen(str), (void **)&blob.data, &blob.length,
735 return NT_STATUS_ILLEGAL_CHARACTER;
738 status = smb2_push_o16s16_blob(buf, ofs, blob);
739 data_blob_free(&blob);
744 push a file handle into a buffer
746 void smb2_push_handle(uint8_t *data, struct smb2_handle *h)
748 SBVAL(data, 0, h->data[0]);
749 SBVAL(data, 8, h->data[1]);
753 pull a file handle from a buffer
755 void smb2_pull_handle(uint8_t *ptr, struct smb2_handle *h)
757 h->data[0] = BVAL(ptr, 0);
758 h->data[1] = BVAL(ptr, 8);