b976b528f1386f33f376175f6b5fbb261a39de61
[kai/samba.git] / source4 / libcli / smb2 / create.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    SMB2 client tree handling
5
6    Copyright (C) Andrew Tridgell 2005
7    
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.
12    
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.
17    
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/>.
20 */
21
22 #include "includes.h"
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"
28
29
30 /*
31   parse a set of SMB2 create blobs
32 */
33 NTSTATUS smb2_create_blob_parse(TALLOC_CTX *mem_ctx, const DATA_BLOB buffer,
34                                 struct smb2_create_blobs *blobs)
35 {
36         const uint8_t *data = buffer.data;
37         uint32_t remaining = buffer.length;
38
39         while (remaining > 0) {
40                 uint32_t next;
41                 uint32_t name_offset, name_length;
42                 uint32_t reserved, data_offset;
43                 uint32_t data_length;
44                 char *tag;
45                 DATA_BLOB b;
46                 NTSTATUS status;
47
48                 if (remaining < 16) {
49                         return NT_STATUS_INVALID_PARAMETER;
50                 }
51                 next        = IVAL(data, 0);
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);
57
58                 if ((next & 0x7) != 0 ||
59                     next > remaining ||
60                     name_offset < 16 ||
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;
67                 }
68
69                 tag = talloc_strndup(mem_ctx, (const char *)data + name_offset, name_length);
70                 if (tag == NULL) {
71                         return NT_STATUS_NO_MEMORY;
72                 }
73
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)) {
77                         return status;
78                 }
79
80                 talloc_free(tag);
81
82                 if (next == 0) break;
83
84                 remaining -= next;
85                 data += next;
86
87                 if (remaining < 16) {
88                         return NT_STATUS_INVALID_PARAMETER;
89                 }
90         }
91
92         return NT_STATUS_OK;
93 }
94
95
96 /*
97   add a blob to a smb2_create attribute blob
98 */
99 static NTSTATUS smb2_create_blob_push_one(TALLOC_CTX *mem_ctx, DATA_BLOB *buffer,
100                                           const struct smb2_create_blob *blob,
101                                           bool last)
102 {
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);
106
107         if (!data_blob_realloc(mem_ctx, buffer,
108                                buffer->length + 0x14 + tag_length + blob->data.length + pad))
109                 return NT_STATUS_NO_MEMORY;
110
111         if (last) {
112                 SIVAL(buffer->data, ofs+0x00, 0);
113         } else {
114                 SIVAL(buffer->data, ofs+0x00, 0x14 + tag_length + blob->data.length + pad);
115         }
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);
124
125         return NT_STATUS_OK;
126 }
127
128
129 /*
130   create a buffer of a set of create blobs
131 */
132 NTSTATUS smb2_create_blob_push(TALLOC_CTX *mem_ctx, DATA_BLOB *buffer,
133                                const struct smb2_create_blobs blobs)
134 {
135         int i;
136         NTSTATUS status;
137
138         *buffer = data_blob(NULL, 0);
139         for (i=0; i < blobs.num_blobs; i++) {
140                 bool last = false;
141                 const struct smb2_create_blob *c;
142
143                 if ((i + 1) == blobs.num_blobs) {
144                         last = true;
145                 }
146
147                 c = &blobs.blobs[i];
148                 status = smb2_create_blob_push_one(mem_ctx, buffer, c, last);
149                 if (!NT_STATUS_IS_OK(status)) {
150                         return status;
151                 }
152         }
153         return NT_STATUS_OK;
154 }
155
156
157 NTSTATUS smb2_create_blob_add(TALLOC_CTX *mem_ctx, struct smb2_create_blobs *b,
158                               const char *tag, DATA_BLOB data)
159 {
160         struct smb2_create_blob *array;
161
162         array = talloc_realloc(mem_ctx, b->blobs,
163                                struct smb2_create_blob,
164                                b->num_blobs + 1);
165         NT_STATUS_HAVE_NO_MEMORY(array);
166         b->blobs = array;
167
168         b->blobs[b->num_blobs].tag = talloc_strdup(b->blobs, tag);
169         NT_STATUS_HAVE_NO_MEMORY(b->blobs[b->num_blobs].tag);
170
171         if (data.data) {
172                 b->blobs[b->num_blobs].data = data_blob_talloc(b->blobs,
173                                                                data.data,
174                                                                data.length);
175                 NT_STATUS_HAVE_NO_MEMORY(b->blobs[b->num_blobs].data.data);
176         } else {
177                 b->blobs[b->num_blobs].data = data_blob(NULL, 0);
178         }
179
180         b->num_blobs += 1;
181
182         return NT_STATUS_OK;
183 }
184
185 /*
186   send a create request
187 */
188 struct smb2_request *smb2_create_send(struct smb2_tree *tree, struct smb2_create *io)
189 {
190         struct smb2_request *req;
191         NTSTATUS status;
192         DATA_BLOB blob;
193         struct smb2_create_blobs blobs = io->in.blobs;
194
195         req = smb2_request_init_tree(tree, SMB2_OP_CREATE, 0x38, true, 0);
196         if (req == NULL) return NULL;
197
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);
208
209         status = smb2_push_o16s16_string(&req->out, 0x2C, io->in.fname);
210         if (!NT_STATUS_IS_OK(status)) {
211                 talloc_free(req);
212                 return NULL;
213         }
214
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)) {
223                         talloc_free(req);
224                         return NULL;
225                 }
226                 data_blob_free(&b);
227         }
228
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)) {
237                         talloc_free(req);
238                         return NULL;
239                 }
240         }
241
242         if (io->in.alloc_size != 0) {
243                 uint8_t data[8];
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)) {
248                         talloc_free(req);
249                         return NULL;
250                 }
251         }
252
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)) {
257                         talloc_free(req);
258                         return NULL;
259                 }
260         }
261
262         if (io->in.durable_handle) {
263                 uint8_t data[16];
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)) {
268                         talloc_free(req);
269                         return NULL;
270                 }
271         }
272
273         if (io->in.timewarp) {
274                 uint8_t data[8];
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)) {
279                         talloc_free(req);
280                         return NULL;
281                 }
282         }
283
284         if (io->in.sec_desc) {
285                 enum ndr_err_code ndr_err;
286                 DATA_BLOB sd_blob;
287                 ndr_err = ndr_push_struct_blob(&sd_blob, req, NULL,
288                                                io->in.sec_desc,
289                                                (ndr_push_flags_fn_t)ndr_push_security_descriptor);
290                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
291                         talloc_free(req);
292                         return NULL;
293                 }
294                 status = smb2_create_blob_add(req, &blobs,
295                                               SMB2_CREATE_TAG_SECD, sd_blob);
296                 if (!NT_STATUS_IS_OK(status)) {
297                         talloc_free(req);
298                         return NULL;
299                 }
300         }
301
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)) {
306                         talloc_free(req);
307                         return NULL;
308                 }
309         }
310
311         /* and any custom blobs */
312         status = smb2_create_blob_push(req, &blob, blobs);
313         if (!NT_STATUS_IS_OK(status)) {
314                 talloc_free(req);
315                 return NULL;
316         }
317
318         status = smb2_push_o32s32_blob(&req->out, 0x30, blob);
319         if (!NT_STATUS_IS_OK(status)) {
320                 talloc_free(req);
321                 return NULL;
322         }
323
324         data_blob_free(&blob);
325
326         smb2_transport_send(req);
327
328         return req;
329 }
330
331
332 /*
333   recv a create reply
334 */
335 NTSTATUS smb2_create_recv(struct smb2_request *req, TALLOC_CTX *mem_ctx, struct smb2_create *io)
336 {
337         NTSTATUS status;
338         DATA_BLOB blob;
339         int i;
340
341         if (!smb2_request_receive(req) || 
342             !smb2_request_is_ok(req)) {
343                 return smb2_request_destroy(req);
344         }
345
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);
363                 return status;
364         }
365
366         status = smb2_create_blob_parse(mem_ctx, blob, &io->out.blobs);
367         if (!NT_STATUS_IS_OK(status)) {
368                 smb2_request_destroy(req);
369                 return status;
370         }
371
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;
379                         }
380                         io->out.maximal_access = IVAL(io->out.blobs.blobs[i].data.data, 0);
381                 }
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;
386                         }
387                         memcpy(io->out.on_disk_id, io->out.blobs.blobs[i].data.data, 32);
388                 }
389         }
390
391         data_blob_free(&blob);
392
393         return smb2_request_destroy(req);
394 }
395
396 /*
397   sync create request
398 */
399 NTSTATUS smb2_create(struct smb2_tree *tree, TALLOC_CTX *mem_ctx, struct smb2_create *io)
400 {
401         struct smb2_request *req = smb2_create_send(tree, io);
402         return smb2_create_recv(req, mem_ctx, io);
403 }