Backport FSCTL codes from master
[Samba.git] / source4 / libcli / smb2 / request.c
blob358d13363f0248113ba6cb01787c234722fd13fc
1 /*
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/>.
23 #include "includes.h"
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;
39 } else {
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)
49 if (req->transport) {
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);
54 return 0;
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;
65 uint64_t seqnum;
66 uint32_t hdr_offset;
67 uint32_t flags = 0;
68 bool compound = false;
70 if (body_dynamic_present) {
71 if (body_dynamic_size == 0) {
72 body_dynamic_size = 1;
74 } else {
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;
84 } else {
85 transport->seqnum += 1;
88 req->state = SMB2_REQUEST_INIT;
89 req->transport = transport;
90 req->session = NULL;
91 req->tree = NULL;
92 req->seqnum = seqnum;
93 req->status = NT_STATUS_OK;
94 req->async.fn = NULL;
95 req->next = req->prev = NULL;
97 ZERO_STRUCT(req->cancel);
98 ZERO_STRUCT(req->in);
100 if (transport->compound.missing > 0) {
101 compound = true;
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;
108 } else {
109 ZERO_STRUCT(req->out);
112 if (req->out.size > 0) {
113 hdr_offset = req->out.size;
114 } else {
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) {
124 talloc_free(req);
125 return 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) {
156 req->out.size += 1;
157 SCVAL(req->out.dynamic, 0, 0);
160 talloc_set_destructor(req, smb2_request_destructor);
162 return req;
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,
174 body_dynamic_size);
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;
181 req->tree = tree;
183 return req;
186 /* destroy a request structure and return final status */
187 NTSTATUS smb2_request_destroy(struct smb2_request *req)
189 NTSTATUS status;
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;
198 } else {
199 status = req->status;
202 talloc_free(req);
203 return 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) {
218 return false;
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)
242 if (size == 0) {
243 /* zero bytes is never out of range */
244 return false;
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) {
251 return true;
253 return false;
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)) {
266 return 1;
269 return 0;
273 grow a SMB2 buffer by the specified amount
275 NTSTATUS smb2_grow_buffer(struct smb2_request_buffer *buf, size_t increase)
277 size_t hdr_ofs;
278 size_t dynamic_ofs;
279 uint8_t *buffer_ptr;
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;
299 return NT_STATUS_OK;
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)
308 uint16_t ofs, size;
309 if (smb2_oob(buf, ptr, 4)) {
310 return NT_STATUS_INVALID_PARAMETER;
312 ofs = SVAL(ptr, 0);
313 size = SVAL(ptr, 2);
314 if (ofs == 0) {
315 *blob = data_blob(NULL, 0);
316 return NT_STATUS_OK;
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);
323 return NT_STATUS_OK;
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
329 to the body start
331 NTSTATUS smb2_push_o16s16_blob(struct smb2_request_buffer *buf,
332 uint16_t ofs, DATA_BLOB blob)
334 NTSTATUS status;
335 size_t offset;
336 size_t padding_length;
337 size_t padding_fix;
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;
358 SSVAL(ptr, 0, 0);
359 SSVAL(ptr, 2, 0);
360 return NT_STATUS_OK;
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;
383 return NT_STATUS_OK;
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
390 to the body start
392 NTSTATUS smb2_push_o16s32_blob(struct smb2_request_buffer *buf,
393 uint16_t ofs, DATA_BLOB blob)
395 NTSTATUS status;
396 size_t offset;
397 size_t padding_length;
398 size_t padding_fix;
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;
414 SSVAL(ptr, 0, 0);
415 SIVAL(ptr, 2, 0);
416 return NT_STATUS_OK;
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;
439 return NT_STATUS_OK;
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
446 to the body start
448 NTSTATUS smb2_push_o32s32_blob(struct smb2_request_buffer *buf,
449 uint32_t ofs, DATA_BLOB blob)
451 NTSTATUS status;
452 size_t offset;
453 size_t padding_length;
454 size_t padding_fix;
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;
470 SIVAL(ptr, 0, 0);
471 SIVAL(ptr, 4, 0);
472 return NT_STATUS_OK;
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;
495 return NT_STATUS_OK;
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
502 to the body start
504 NTSTATUS smb2_push_s32o32_blob(struct smb2_request_buffer *buf,
505 uint32_t ofs, DATA_BLOB blob)
507 NTSTATUS status;
508 size_t offset;
509 size_t padding_length;
510 size_t padding_fix;
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;
526 SIVAL(ptr, 0, 0);
527 SIVAL(ptr, 4, 0);
528 return NT_STATUS_OK;
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;
551 return NT_STATUS_OK;
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)
560 uint16_t ofs;
561 uint32_t size;
563 if (smb2_oob(buf, ptr, 6)) {
564 return NT_STATUS_INVALID_PARAMETER;
566 ofs = SVAL(ptr, 0);
567 size = IVAL(ptr, 2);
568 if (ofs == 0) {
569 *blob = data_blob(NULL, 0);
570 return NT_STATUS_OK;
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);
577 return NT_STATUS_OK;
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)
586 uint32_t ofs, size;
587 if (smb2_oob(buf, ptr, 8)) {
588 return NT_STATUS_INVALID_PARAMETER;
590 ofs = IVAL(ptr, 0);
591 size = IVAL(ptr, 4);
592 if (ofs == 0) {
593 *blob = data_blob(NULL, 0);
594 return NT_STATUS_OK;
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);
601 return NT_STATUS_OK;
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)
613 uint32_t ofs, size;
614 if (smb2_oob(buf, ptr, 8)) {
615 return NT_STATUS_INVALID_PARAMETER;
617 ofs = SVAL(ptr, 0);
618 size = IVAL(ptr, 4);
619 if (ofs == 0) {
620 *blob = data_blob(NULL, 0);
621 return NT_STATUS_OK;
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);
628 return NT_STATUS_OK;
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)
637 uint32_t ofs, size;
638 if (smb2_oob(buf, ptr, 8)) {
639 return NT_STATUS_INVALID_PARAMETER;
641 size = IVAL(ptr, 0);
642 ofs = IVAL(ptr, 4);
643 if (ofs == 0) {
644 *blob = data_blob(NULL, 0);
645 return NT_STATUS_OK;
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);
652 return NT_STATUS_OK;
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)
661 uint32_t ofs, size;
662 if (smb2_oob(buf, ptr, 8)) {
663 return NT_STATUS_INVALID_PARAMETER;
665 size = IVAL(ptr, 0);
666 ofs = SVAL(ptr, 4);
667 if (ofs == 0) {
668 *blob = data_blob(NULL, 0);
669 return NT_STATUS_OK;
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);
676 return NT_STATUS_OK;
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)
686 DATA_BLOB blob;
687 NTSTATUS status;
688 void *vstr;
689 bool ret;
691 status = smb2_pull_o16s16_blob(buf, mem_ctx, ptr, &blob);
692 NT_STATUS_NOT_OK_RETURN(status);
694 if (blob.data == NULL) {
695 *str = NULL;
696 return NT_STATUS_OK;
699 if (blob.length == 0) {
700 char *s;
701 s = talloc_strdup(mem_ctx, "");
702 NT_STATUS_HAVE_NO_MEMORY(s);
703 *str = s;
704 return NT_STATUS_OK;
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;
711 if (!ret) {
712 return NT_STATUS_ILLEGAL_CHARACTER;
714 return NT_STATUS_OK;
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)
724 DATA_BLOB blob;
725 NTSTATUS status;
726 bool ret;
728 if (str == NULL) {
729 return smb2_push_o16s16_blob(buf, ofs, data_blob(NULL, 0));
732 if (*str == 0) {
733 blob.data = discard_const_p(uint8_t, str);
734 blob.length = 0;
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,
740 false);
741 if (!ret) {
742 return NT_STATUS_ILLEGAL_CHARACTER;
745 status = smb2_push_o16s16_blob(buf, ofs, blob);
746 data_blob_free(&blob);
747 return status;
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);