2 Unix SMB/CIFS implementation.
4 SMB2 client tree handling
6 Copyright (C) Andrew Tridgell 2005
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 "libcli/raw/libcliraw.h"
24 #include "libcli/raw/raw_proto.h"
25 #include "libcli/smb2/smb2.h"
26 #include "libcli/smb2/smb2_calls.h"
27 #include "librpc/gen_ndr/ndr_security.h"
31 parse a set of SMB2 create blobs
33 NTSTATUS smb2_create_blob_parse(TALLOC_CTX *mem_ctx, const DATA_BLOB buffer,
34 struct smb2_create_blobs *blobs)
36 const uint8_t *data = buffer.data;
37 uint32_t remaining = buffer.length;
39 while (remaining > 0) {
41 uint32_t name_offset, name_length;
42 uint32_t reserved, data_offset;
49 return NT_STATUS_INVALID_PARAMETER;
52 name_offset = SVAL(data, 4);
53 name_length = SVAL(data, 6);
54 reserved = SVAL(data, 8);
55 data_offset = SVAL(data, 10);
56 data_length = IVAL(data, 12);
58 if ((next & 0x7) != 0 ||
61 name_offset > remaining ||
62 name_offset + name_length > remaining ||
63 data_offset < name_offset + name_length ||
64 data_offset > remaining ||
65 data_offset + (uint64_t)data_length > remaining) {
66 return NT_STATUS_INVALID_PARAMETER;
69 tag = talloc_strndup(mem_ctx, (const char *)data + name_offset, name_length);
71 return NT_STATUS_NO_MEMORY;
74 b = data_blob_const(data+data_offset, data_length);
75 status = smb2_create_blob_add(mem_ctx, blobs, tag, b);
76 if (!NT_STATUS_IS_OK(status)) {
88 return NT_STATUS_INVALID_PARAMETER;
97 add a blob to a smb2_create attribute blob
99 static NTSTATUS smb2_create_blob_push_one(TALLOC_CTX *mem_ctx, DATA_BLOB *buffer,
100 const struct smb2_create_blob *blob,
103 uint32_t ofs = buffer->length;
104 size_t tag_length = strlen(blob->tag);
105 uint8_t pad = smb2_padding_size(blob->data.length+tag_length, 4);
107 if (!data_blob_realloc(mem_ctx, buffer,
108 buffer->length + 0x14 + tag_length + blob->data.length + pad))
109 return NT_STATUS_NO_MEMORY;
112 SIVAL(buffer->data, ofs+0x00, 0);
114 SIVAL(buffer->data, ofs+0x00, 0x14 + tag_length + blob->data.length + pad);
116 SSVAL(buffer->data, ofs+0x04, 0x10); /* offset of tag */
117 SIVAL(buffer->data, ofs+0x06, tag_length); /* tag length */
118 SSVAL(buffer->data, ofs+0x0A, 0x14 + tag_length); /* offset of data */
119 SIVAL(buffer->data, ofs+0x0C, blob->data.length);
120 memcpy(buffer->data+ofs+0x10, blob->tag, tag_length);
121 SIVAL(buffer->data, ofs+0x10+tag_length, 0); /* pad? */
122 memcpy(buffer->data+ofs+0x14+tag_length, blob->data.data, blob->data.length);
123 memset(buffer->data+ofs+0x14+tag_length+blob->data.length, 0, pad);
130 create a buffer of a set of create blobs
132 NTSTATUS smb2_create_blob_push(TALLOC_CTX *mem_ctx, DATA_BLOB *buffer,
133 const struct smb2_create_blobs blobs)
138 *buffer = data_blob(NULL, 0);
139 for (i=0; i < blobs.num_blobs; i++) {
141 const struct smb2_create_blob *c;
143 if ((i + 1) == blobs.num_blobs) {
148 status = smb2_create_blob_push_one(mem_ctx, buffer, c, last);
149 if (!NT_STATUS_IS_OK(status)) {
157 NTSTATUS smb2_create_blob_add(TALLOC_CTX *mem_ctx, struct smb2_create_blobs *b,
158 const char *tag, DATA_BLOB data)
160 struct smb2_create_blob *array;
162 array = talloc_realloc(mem_ctx, b->blobs,
163 struct smb2_create_blob,
165 NT_STATUS_HAVE_NO_MEMORY(array);
168 b->blobs[b->num_blobs].tag = talloc_strdup(b->blobs, tag);
169 NT_STATUS_HAVE_NO_MEMORY(b->blobs[b->num_blobs].tag);
172 b->blobs[b->num_blobs].data = data_blob_talloc(b->blobs,
175 NT_STATUS_HAVE_NO_MEMORY(b->blobs[b->num_blobs].data.data);
177 b->blobs[b->num_blobs].data = data_blob(NULL, 0);
186 send a create request
188 struct smb2_request *smb2_create_send(struct smb2_tree *tree, struct smb2_create *io)
190 struct smb2_request *req;
193 struct smb2_create_blobs blobs = io->in.blobs;
195 req = smb2_request_init_tree(tree, SMB2_OP_CREATE, 0x38, true, 0);
196 if (req == NULL) return NULL;
198 SCVAL(req->out.body, 0x02, io->in.security_flags);
199 SCVAL(req->out.body, 0x03, io->in.oplock_level);
200 SIVAL(req->out.body, 0x04, io->in.impersonation_level);
201 SBVAL(req->out.body, 0x08, io->in.create_flags);
202 SBVAL(req->out.body, 0x10, io->in.reserved);
203 SIVAL(req->out.body, 0x18, io->in.desired_access);
204 SIVAL(req->out.body, 0x1C, io->in.file_attributes);
205 SIVAL(req->out.body, 0x20, io->in.share_access);
206 SIVAL(req->out.body, 0x24, io->in.create_disposition);
207 SIVAL(req->out.body, 0x28, io->in.create_options);
209 status = smb2_push_o16s16_string(&req->out, 0x2C, io->in.fname);
210 if (!NT_STATUS_IS_OK(status)) {
215 /* now add all the optional blobs */
216 if (io->in.eas.num_eas != 0) {
217 DATA_BLOB b = data_blob_talloc(req, NULL,
218 ea_list_size_chained(io->in.eas.num_eas, io->in.eas.eas, 4));
219 ea_put_list_chained(b.data, io->in.eas.num_eas, io->in.eas.eas, 4);
220 status = smb2_create_blob_add(req, &blobs,
221 SMB2_CREATE_TAG_EXTA, b);
222 if (!NT_STATUS_IS_OK(status)) {
229 /* an empty MxAc tag seems to be used to ask the server to
230 return the maximum access mask allowed on the file */
231 if (io->in.query_maximal_access) {
232 /* TODO: MS-SMB2 2.2.13.2.5 says this can contain a timestamp? What to do
233 with that if it doesn't match? */
234 status = smb2_create_blob_add(req, &blobs,
235 SMB2_CREATE_TAG_MXAC, data_blob(NULL, 0));
236 if (!NT_STATUS_IS_OK(status)) {
242 if (io->in.alloc_size != 0) {
244 SBVAL(data, 0, io->in.alloc_size);
245 status = smb2_create_blob_add(req, &blobs,
246 SMB2_CREATE_TAG_ALSI, data_blob_const(data, 8));
247 if (!NT_STATUS_IS_OK(status)) {
253 if (io->in.durable_open) {
254 status = smb2_create_blob_add(req, &blobs,
255 SMB2_CREATE_TAG_DHNQ, data_blob_talloc_zero(req, 16));
256 if (!NT_STATUS_IS_OK(status)) {
262 if (io->in.durable_handle) {
264 smb2_push_handle(data, io->in.durable_handle);
265 status = smb2_create_blob_add(req, &blobs,
266 SMB2_CREATE_TAG_DHNC, data_blob_const(data, 16));
267 if (!NT_STATUS_IS_OK(status)) {
273 if (io->in.timewarp) {
275 SBVAL(data, 0, io->in.timewarp);
276 status = smb2_create_blob_add(req, &blobs,
277 SMB2_CREATE_TAG_TWRP, data_blob_const(data, 8));
278 if (!NT_STATUS_IS_OK(status)) {
284 if (io->in.sec_desc) {
285 enum ndr_err_code ndr_err;
287 ndr_err = ndr_push_struct_blob(&sd_blob, req, NULL,
289 (ndr_push_flags_fn_t)ndr_push_security_descriptor);
290 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
294 status = smb2_create_blob_add(req, &blobs,
295 SMB2_CREATE_TAG_SECD, sd_blob);
296 if (!NT_STATUS_IS_OK(status)) {
302 if (io->in.query_on_disk_id) {
303 status = smb2_create_blob_add(req, &blobs,
304 SMB2_CREATE_TAG_QFID, data_blob(NULL, 0));
305 if (!NT_STATUS_IS_OK(status)) {
311 /* and any custom blobs */
312 status = smb2_create_blob_push(req, &blob, blobs);
313 if (!NT_STATUS_IS_OK(status)) {
318 status = smb2_push_o32s32_blob(&req->out, 0x30, blob);
319 if (!NT_STATUS_IS_OK(status)) {
324 data_blob_free(&blob);
326 smb2_transport_send(req);
335 NTSTATUS smb2_create_recv(struct smb2_request *req, TALLOC_CTX *mem_ctx, struct smb2_create *io)
341 if (!smb2_request_receive(req) ||
342 !smb2_request_is_ok(req)) {
343 return smb2_request_destroy(req);
346 SMB2_CHECK_PACKET_RECV(req, 0x58, true);
347 ZERO_STRUCT(io->out);
348 io->out.oplock_level = CVAL(req->in.body, 0x02);
349 io->out.reserved = CVAL(req->in.body, 0x03);
350 io->out.create_action = IVAL(req->in.body, 0x04);
351 io->out.create_time = smbcli_pull_nttime(req->in.body, 0x08);
352 io->out.access_time = smbcli_pull_nttime(req->in.body, 0x10);
353 io->out.write_time = smbcli_pull_nttime(req->in.body, 0x18);
354 io->out.change_time = smbcli_pull_nttime(req->in.body, 0x20);
355 io->out.alloc_size = BVAL(req->in.body, 0x28);
356 io->out.size = BVAL(req->in.body, 0x30);
357 io->out.file_attr = IVAL(req->in.body, 0x38);
358 io->out.reserved2 = IVAL(req->in.body, 0x3C);
359 smb2_pull_handle(req->in.body+0x40, &io->out.file.handle);
360 status = smb2_pull_o32s32_blob(&req->in, mem_ctx, req->in.body+0x50, &blob);
361 if (!NT_STATUS_IS_OK(status)) {
362 smb2_request_destroy(req);
366 status = smb2_create_blob_parse(mem_ctx, blob, &io->out.blobs);
367 if (!NT_STATUS_IS_OK(status)) {
368 smb2_request_destroy(req);
372 /* pull out the parsed blobs */
373 for (i=0;i<io->out.blobs.num_blobs;i++) {
374 if (strcmp(io->out.blobs.blobs[i].tag, SMB2_CREATE_TAG_MXAC) == 0) {
375 /* why 8 bytes not 4?? */
376 if (io->out.blobs.blobs[i].data.length != 8) {
377 smb2_request_destroy(req);
378 return NT_STATUS_INVALID_NETWORK_RESPONSE;
380 io->out.maximal_access = IVAL(io->out.blobs.blobs[i].data.data, 0);
382 if (strcmp(io->out.blobs.blobs[i].tag, SMB2_CREATE_TAG_QFID) == 0) {
383 if (io->out.blobs.blobs[i].data.length != 32) {
384 smb2_request_destroy(req);
385 return NT_STATUS_INVALID_NETWORK_RESPONSE;
387 memcpy(io->out.on_disk_id, io->out.blobs.blobs[i].data.data, 32);
391 data_blob_free(&blob);
393 return smb2_request_destroy(req);
399 NTSTATUS smb2_create(struct smb2_tree *tree, TALLOC_CTX *mem_ctx, struct smb2_create *io)
401 struct smb2_request *req = smb2_create_send(tree, io);
402 return smb2_create_recv(req, mem_ctx, io);