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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "libcli/raw/libcliraw.h"
26 #include "libcli/smb2/smb2.h"
27 #include "include/dlinklist.h"
28 #include "lib/events/events.h"
31 initialise a smb2 request
33 struct smb2_request *smb2_request_init(struct smb2_transport *transport, uint16_t opcode,
34 uint16_t body_fixed_size, uint32_t body_dynamic_size)
36 struct smb2_request *req;
38 req = talloc(transport, struct smb2_request);
39 if (req == NULL) return NULL;
41 req->state = SMB2_REQUEST_INIT;
42 req->transport = transport;
45 req->seqnum = transport->seqnum++;
46 req->status = NT_STATUS_OK;
48 req->next = req->prev = NULL;
52 req->out.size = SMB2_HDR_BODY+NBT_HDR_SIZE+body_fixed_size;
54 req->out.allocated = req->out.size + body_dynamic_size;
55 req->out.buffer = talloc_size(req, req->out.allocated);
56 if (req->out.buffer == NULL) {
61 req->out.hdr = req->out.buffer + NBT_HDR_SIZE;
62 req->out.body = req->out.hdr + SMB2_HDR_BODY;
63 req->out.body_size = body_fixed_size;
64 req->out.dynamic = (body_dynamic_size ? req->out.body + body_fixed_size : NULL);
66 SIVAL(req->out.hdr, 0, SMB2_MAGIC);
67 SSVAL(req->out.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
68 SSVAL(req->out.hdr, SMB2_HDR_PAD1, 0);
69 SIVAL(req->out.hdr, SMB2_HDR_STATUS, 0);
70 SSVAL(req->out.hdr, SMB2_HDR_OPCODE, opcode);
71 SSVAL(req->out.hdr, SMB2_HDR_PAD2, 0);
72 SIVAL(req->out.hdr, SMB2_HDR_FLAGS, 0);
73 SIVAL(req->out.hdr, SMB2_HDR_UNKNOWN, 0);
74 SBVAL(req->out.hdr, SMB2_HDR_SEQNUM, req->seqnum);
75 SIVAL(req->out.hdr, SMB2_HDR_PID, 0);
76 SIVAL(req->out.hdr, SMB2_HDR_TID, 0);
77 SBVAL(req->out.hdr, SMB2_HDR_UID, 0);
78 memset(req->out.hdr+SMB2_HDR_SIG, 0, 16);
80 /* set the length of the fixed body part and +1 if there's a dynamic part also */
81 SSVAL(req->out.body, 0, body_fixed_size + (body_dynamic_size?1:0));
84 * if we have a dynamic part, make sure the first byte
85 * which is always be part of the packet is initialized
87 if (body_dynamic_size) {
88 SCVAL(req->out.dynamic, 0, 0);
95 initialise a smb2 request for tree operations
97 struct smb2_request *smb2_request_init_tree(struct smb2_tree *tree, uint16_t opcode,
98 uint16_t body_fixed_size, uint32_t body_dynamic_size)
100 struct smb2_request *req = smb2_request_init(tree->session->transport, opcode,
101 body_fixed_size, body_dynamic_size);
102 if (req == NULL) return NULL;
104 SBVAL(req->out.hdr, SMB2_HDR_UID, tree->session->uid);
105 SIVAL(req->out.hdr, SMB2_HDR_TID, tree->tid);
106 req->session = tree->session;
112 /* destroy a request structure and return final status */
113 NTSTATUS smb2_request_destroy(struct smb2_request *req)
117 /* this is the error code we give the application for when a
118 _send() call fails completely */
119 if (!req) return NT_STATUS_UNSUCCESSFUL;
121 if (req->transport) {
122 /* remove it from the list of pending requests (a null op if
123 its not in the list) */
124 DLIST_REMOVE(req->transport->pending_recv, req);
127 if (req->state == SMB2_REQUEST_ERROR &&
128 NT_STATUS_IS_OK(req->status)) {
129 req->status = NT_STATUS_INTERNAL_ERROR;
132 status = req->status;
138 receive a response to a packet
140 BOOL smb2_request_receive(struct smb2_request *req)
142 /* req can be NULL when a send has failed. This eliminates lots of NULL
143 checks in each module */
144 if (!req) return False;
146 /* keep receiving packets until this one is replied to */
147 while (req->state <= SMB2_REQUEST_RECV) {
148 if (event_loop_once(req->transport->socket->event.ctx) != 0) {
153 return req->state == SMB2_REQUEST_DONE;
156 /* Return true if the last packet was in error */
157 BOOL smb2_request_is_error(struct smb2_request *req)
159 return NT_STATUS_IS_ERR(req->status);
162 /* Return true if the last packet was OK */
163 BOOL smb2_request_is_ok(struct smb2_request *req)
165 return NT_STATUS_IS_OK(req->status);
169 check if a range in the reply body is out of bounds
171 BOOL smb2_oob(struct smb2_request_buffer *buf, const uint8_t *ptr, size_t size)
173 /* be careful with wraparound! */
174 if (ptr < buf->body ||
175 ptr >= buf->body + buf->body_size ||
176 size > buf->body_size ||
177 ptr + size > buf->body + buf->body_size) {
183 size_t smb2_padding_size(uint32_t offset, size_t n)
185 if ((offset & (n-1)) == 0) return 0;
186 return n - (offset & (n-1));
190 grow a SMB2 buffer by the specified amount
192 static NTSTATUS smb2_grow_buffer(struct smb2_request_buffer *buf, size_t increase)
196 uint32_t newsize = buf->size + increase;
198 /* a packet size should be limited a bit */
199 if (newsize >= 0x00FFFFFF) return NT_STATUS_MARSHALL_OVERFLOW;
201 if (newsize <= buf->allocated) return NT_STATUS_OK;
203 dynamic_ofs = buf->dynamic - buf->buffer;
205 buffer_ptr = talloc_realloc_size(buf, buf->buffer, newsize);
206 NT_STATUS_HAVE_NO_MEMORY(buffer_ptr);
208 buf->buffer = buffer_ptr;
209 buf->hdr = buf->buffer + NBT_HDR_SIZE;
210 buf->body = buf->hdr + SMB2_HDR_BODY;
211 buf->dynamic = buf->buffer + dynamic_ofs;
212 buf->allocated = newsize;
218 pull a uint16_t ofs/ uint16_t length/blob triple from a data blob
219 the ptr points to the start of the offset/length pair
221 NTSTATUS smb2_pull_o16s16_blob(struct smb2_request_buffer *buf, TALLOC_CTX *mem_ctx, uint8_t *ptr, DATA_BLOB *blob)
224 if (smb2_oob(buf, ptr, 4)) {
225 return NT_STATUS_BUFFER_TOO_SMALL;
229 if (ofs == 0 || size == 0) {
230 *blob = data_blob(NULL, 0);
233 if (smb2_oob(buf, buf->hdr + ofs, size)) {
234 return NT_STATUS_BUFFER_TOO_SMALL;
236 *blob = data_blob_talloc(mem_ctx, buf->hdr + ofs, size);
237 NT_STATUS_HAVE_NO_MEMORY(blob->data);
242 push a uint16_t ofs/ uint16_t length/blob triple into a data blob
243 the ofs points to the start of the offset/length pair, and is relative
246 NTSTATUS smb2_push_o16s16_blob(struct smb2_request_buffer *buf,
247 uint16_t ofs, DATA_BLOB blob)
251 size_t padding_length;
252 uint8_t *ptr = buf->body+ofs;
254 if (buf->dynamic == NULL) {
255 return NT_STATUS_INVALID_PARAMETER;
258 /* we have only 16 bit for the size */
259 if (blob.length > 0xFFFF) {
260 return NT_STATUS_BUFFER_TOO_SMALL;
263 /* check if there're enough room for ofs and size */
264 if (smb2_oob(buf, ptr, 4)) {
265 return NT_STATUS_BUFFER_TOO_SMALL;
268 if (blob.length == 0) {
274 offset = buf->dynamic - buf->hdr;
275 padding_length = smb2_padding_size(offset, 2);
276 offset += padding_length;
278 SSVAL(ptr, 0, offset);
279 SSVAL(ptr, 2, blob.length);
281 status = smb2_grow_buffer(buf, padding_length + blob.length);
282 NT_STATUS_NOT_OK_RETURN(status);
284 memset(buf->dynamic, 0, padding_length);
285 buf->dynamic += padding_length;
287 memcpy(buf->dynamic, blob.data, blob.length);
288 buf->dynamic += blob.length;
290 buf->size += blob.length + padding_length;
291 buf->body_size += blob.length + padding_length;
298 push a uint16_t ofs/ uint32_t length/blob triple into a data blob
299 the ofs points to the start of the offset/length pair, and is relative
302 NTSTATUS smb2_push_o16s32_blob(struct smb2_request_buffer *buf,
303 uint16_t ofs, DATA_BLOB blob)
307 size_t padding_length;
308 uint8_t *ptr = buf->body+ofs;
310 if (buf->dynamic == NULL) {
311 return NT_STATUS_INVALID_PARAMETER;
314 /* check if there're enough room for ofs and size */
315 if (smb2_oob(buf, ptr, 6)) {
316 return NT_STATUS_BUFFER_TOO_SMALL;
319 if (blob.length == 0) {
325 offset = buf->dynamic - buf->hdr;
326 padding_length = smb2_padding_size(offset, 2);
327 offset += padding_length;
329 SSVAL(ptr, 0, offset);
330 SIVAL(ptr, 2, blob.length);
332 status = smb2_grow_buffer(buf, padding_length + blob.length);
333 NT_STATUS_NOT_OK_RETURN(status);
335 memset(buf->dynamic, 0, padding_length);
336 buf->dynamic += padding_length;
338 memcpy(buf->dynamic, blob.data, blob.length);
339 buf->dynamic += blob.length;
341 buf->size += blob.length + padding_length;
342 buf->body_size += blob.length + padding_length;
349 push a uint32_t ofs/ uint32_t length/blob triple into a data blob
350 the ofs points to the start of the offset/length pair, and is relative
353 NTSTATUS smb2_push_o32s32_blob(struct smb2_request_buffer *buf,
354 uint32_t ofs, DATA_BLOB blob)
358 size_t padding_length;
359 uint8_t *ptr = buf->body+ofs;
361 if (buf->dynamic == NULL) {
362 return NT_STATUS_INVALID_PARAMETER;
365 /* check if there're enough room for ofs and size */
366 if (smb2_oob(buf, ptr, 8)) {
367 return NT_STATUS_BUFFER_TOO_SMALL;
370 if (blob.length == 0) {
376 offset = buf->dynamic - buf->hdr;
377 padding_length = smb2_padding_size(offset, 8);
378 offset += padding_length;
380 SIVAL(ptr, 0, offset);
381 SIVAL(ptr, 4, blob.length);
383 status = smb2_grow_buffer(buf, padding_length + blob.length);
384 NT_STATUS_NOT_OK_RETURN(status);
386 memset(buf->dynamic, 0, padding_length);
387 buf->dynamic += padding_length;
389 memcpy(buf->dynamic, blob.data, blob.length);
390 buf->dynamic += blob.length;
392 buf->size += blob.length + padding_length;
393 buf->body_size += blob.length + padding_length;
400 push a uint32_t length/ uint32_t ofs/blob triple into a data blob
401 the ofs points to the start of the length/offset pair, and is relative
404 NTSTATUS smb2_push_s32o32_blob(struct smb2_request_buffer *buf,
405 uint32_t ofs, DATA_BLOB blob)
409 size_t padding_length;
410 uint8_t *ptr = buf->body+ofs;
412 if (buf->dynamic == NULL) {
413 return NT_STATUS_INVALID_PARAMETER;
416 /* check if there're enough room for ofs and size */
417 if (smb2_oob(buf, ptr, 8)) {
418 return NT_STATUS_BUFFER_TOO_SMALL;
421 if (blob.length == 0) {
427 offset = buf->dynamic - buf->hdr;
428 padding_length = smb2_padding_size(offset, 8);
429 offset += padding_length;
431 SIVAL(ptr, 0, blob.length);
432 SIVAL(ptr, 4, offset);
434 status = smb2_grow_buffer(buf, padding_length + blob.length);
435 NT_STATUS_NOT_OK_RETURN(status);
437 memset(buf->dynamic, 0, padding_length);
438 buf->dynamic += padding_length;
440 memcpy(buf->dynamic, blob.data, blob.length);
441 buf->dynamic += blob.length;
443 buf->size += blob.length + padding_length;
444 buf->body_size += blob.length + padding_length;
450 pull a uint16_t ofs/ uint32_t length/blob triple from a data blob
451 the ptr points to the start of the offset/length pair
453 NTSTATUS smb2_pull_o16s32_blob(struct smb2_request_buffer *buf, TALLOC_CTX *mem_ctx, uint8_t *ptr, DATA_BLOB *blob)
458 if (smb2_oob(buf, ptr, 6)) {
459 return NT_STATUS_BUFFER_TOO_SMALL;
463 if (ofs == 0 || size == 0) {
464 *blob = data_blob(NULL, 0);
467 if (smb2_oob(buf, buf->hdr + ofs, size)) {
468 return NT_STATUS_BUFFER_TOO_SMALL;
470 *blob = data_blob_talloc(mem_ctx, buf->hdr + ofs, size);
471 NT_STATUS_HAVE_NO_MEMORY(blob->data);
476 pull a uint32_t ofs/ uint32_t length/blob triple from a data blob
477 the ptr points to the start of the offset/length pair
479 NTSTATUS smb2_pull_o32s32_blob(struct smb2_request_buffer *buf, TALLOC_CTX *mem_ctx, uint8_t *ptr, DATA_BLOB *blob)
482 if (smb2_oob(buf, ptr, 8)) {
483 return NT_STATUS_BUFFER_TOO_SMALL;
487 if (ofs == 0 || size == 0) {
488 *blob = data_blob(NULL, 0);
491 if (smb2_oob(buf, buf->hdr + ofs, size)) {
492 return NT_STATUS_BUFFER_TOO_SMALL;
494 *blob = data_blob_talloc(mem_ctx, buf->hdr + ofs, size);
495 NT_STATUS_HAVE_NO_MEMORY(blob->data);
500 pull a string in a uint16_t ofs/ uint16_t length/blob format
501 UTF-16 without termination
503 NTSTATUS smb2_pull_o16s16_string(struct smb2_request_buffer *buf, TALLOC_CTX *mem_ctx,
504 uint8_t *ptr, const char **str)
511 status = smb2_pull_o16s16_blob(buf, mem_ctx, ptr, &blob);
512 NT_STATUS_NOT_OK_RETURN(status);
514 size = convert_string_talloc(mem_ctx, CH_UTF16, CH_UNIX,
515 blob.data, blob.length, &vstr);
516 data_blob_free(&blob);
519 return NT_STATUS_ILLEGAL_CHARACTER;
525 push a string in a uint16_t ofs/ uint16_t length/blob format
526 UTF-16 without termination
528 NTSTATUS smb2_push_o16s16_string(struct smb2_request_buffer *buf,
529 uint16_t ofs, const char *str)
535 if (strcmp("", str) == 0) {
536 return smb2_push_o16s16_blob(buf, ofs, data_blob(NULL, 0));
539 size = convert_string_talloc(buf->buffer, CH_UNIX, CH_UTF16,
540 str, strlen(str), (void **)&blob.data);
542 return NT_STATUS_ILLEGAL_CHARACTER;
546 status = smb2_push_o16s16_blob(buf, ofs, blob);
547 data_blob_free(&blob);
552 push a file handle into a buffer
554 void smb2_push_handle(uint8_t *data, struct smb2_handle *h)
556 SBVAL(data, 0, h->data[0]);
557 SBVAL(data, 8, h->data[1]);
561 pull a file handle from a buffer
563 void smb2_pull_handle(uint8_t *ptr, struct smb2_handle *h)
565 h->data[0] = BVAL(ptr, 0);
566 h->data[1] = BVAL(ptr, 8);