s4:libcli/smb2: add support for SMB2 LEASES v2
[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   send a create request
31 */
32 struct smb2_request *smb2_create_send(struct smb2_tree *tree, struct smb2_create *io)
33 {
34         struct smb2_request *req;
35         NTSTATUS status;
36         DATA_BLOB blob;
37         struct smb2_create_blobs blobs;
38         int i;
39
40         ZERO_STRUCT(blobs);
41
42         req = smb2_request_init_tree(tree, SMB2_OP_CREATE, 0x38, true, 0);
43         if (req == NULL) return NULL;
44
45         SCVAL(req->out.body, 0x02, io->in.security_flags);
46         SCVAL(req->out.body, 0x03, io->in.oplock_level);
47         SIVAL(req->out.body, 0x04, io->in.impersonation_level);
48         SBVAL(req->out.body, 0x08, io->in.create_flags);
49         SBVAL(req->out.body, 0x10, io->in.reserved);
50         SIVAL(req->out.body, 0x18, io->in.desired_access);
51         SIVAL(req->out.body, 0x1C, io->in.file_attributes);
52         SIVAL(req->out.body, 0x20, io->in.share_access);
53         SIVAL(req->out.body, 0x24, io->in.create_disposition);
54         SIVAL(req->out.body, 0x28, io->in.create_options);
55
56         status = smb2_push_o16s16_string(&req->out, 0x2C, io->in.fname);
57         if (!NT_STATUS_IS_OK(status)) {
58                 talloc_free(req);
59                 return NULL;
60         }
61
62         /* now add all the optional blobs */
63         if (io->in.eas.num_eas != 0) {
64                 DATA_BLOB b = data_blob_talloc(req, NULL, 
65                                                ea_list_size_chained(io->in.eas.num_eas, io->in.eas.eas, 4));
66                 ea_put_list_chained(b.data, io->in.eas.num_eas, io->in.eas.eas, 4);
67                 status = smb2_create_blob_add(req, &blobs,
68                                               SMB2_CREATE_TAG_EXTA, b);
69                 if (!NT_STATUS_IS_OK(status)) {
70                         talloc_free(req);
71                         return NULL;
72                 }
73                 data_blob_free(&b);
74         }
75
76         /* an empty MxAc tag seems to be used to ask the server to
77            return the maximum access mask allowed on the file */
78         if (io->in.query_maximal_access) {
79                 /* TODO: MS-SMB2 2.2.13.2.5 says this can contain a timestamp? What to do
80                    with that if it doesn't match? */
81                 status = smb2_create_blob_add(req, &blobs,
82                                               SMB2_CREATE_TAG_MXAC, data_blob(NULL, 0));
83                 if (!NT_STATUS_IS_OK(status)) {
84                         talloc_free(req);
85                         return NULL;
86                 }
87         }
88
89         if (io->in.alloc_size != 0) {
90                 uint8_t data[8];
91                 SBVAL(data, 0, io->in.alloc_size);
92                 status = smb2_create_blob_add(req, &blobs,
93                                               SMB2_CREATE_TAG_ALSI, data_blob_const(data, 8));
94                 if (!NT_STATUS_IS_OK(status)) {
95                         talloc_free(req);
96                         return NULL;
97                 }
98         }
99
100         if (io->in.durable_open) {
101                 status = smb2_create_blob_add(req, &blobs,
102                                               SMB2_CREATE_TAG_DHNQ, data_blob_talloc_zero(req, 16));
103                 if (!NT_STATUS_IS_OK(status)) {
104                         talloc_free(req);
105                         return NULL;
106                 }
107         }
108
109         if (io->in.durable_open_v2) {
110                 uint8_t data[32];
111                 uint32_t flags = 0;
112                 DATA_BLOB guid_blob;
113
114                 SIVAL(data, 0, io->in.timeout);
115                 if (io->in.persistent_open) {
116                         flags = SMB2_DHANDLE_FLAG_PERSISTENT;
117                 }
118                 SIVAL(data, 4, flags);
119                 SBVAL(data, 8, 0x0); /* reserved */
120                 status = GUID_to_ndr_blob(&io->in.create_guid, req, /* TALLOC_CTX */
121                                           &guid_blob);
122                 if (!NT_STATUS_IS_OK(status)) {
123                         talloc_free(req);
124                         return NULL;
125                 }
126                 memcpy(data+16, guid_blob.data, 16);
127
128                 status = smb2_create_blob_add(req, &blobs,
129                                               SMB2_CREATE_TAG_DH2Q,
130                                               data_blob_const(data, 32));
131                 if (!NT_STATUS_IS_OK(status)) {
132                         talloc_free(req);
133                         return NULL;
134                 }
135         }
136
137         if (io->in.durable_handle) {
138                 uint8_t data[16];
139                 smb2_push_handle(data, io->in.durable_handle);
140                 status = smb2_create_blob_add(req, &blobs,
141                                               SMB2_CREATE_TAG_DHNC, data_blob_const(data, 16));
142                 if (!NT_STATUS_IS_OK(status)) {
143                         talloc_free(req);
144                         return NULL;
145                 }
146         }
147
148         if (io->in.durable_handle_v2) {
149                 uint8_t data[36];
150                 DATA_BLOB guid_blob;
151                 uint32_t flags = 0;
152
153                 smb2_push_handle(data, io->in.durable_handle_v2);
154                 status = GUID_to_ndr_blob(&io->in.create_guid, req, /* TALLOC_CTX */
155                                           &guid_blob);
156                 if (!NT_STATUS_IS_OK(status)) {
157                         talloc_free(req);
158                         return NULL;
159                 }
160                 memcpy(data+16, guid_blob.data, 16);
161                 if (io->in.persistent_open) {
162                         flags = SMB2_DHANDLE_FLAG_PERSISTENT;
163                 }
164                 SIVAL(data, 32, flags);
165
166                 status = smb2_create_blob_add(req, &blobs,
167                                               SMB2_CREATE_TAG_DH2C,
168                                               data_blob_const(data, 36));
169                 if (!NT_STATUS_IS_OK(status)) {
170                         talloc_free(req);
171                         return NULL;
172                 }
173         }
174
175         if (io->in.timewarp) {
176                 uint8_t data[8];
177                 SBVAL(data, 0, io->in.timewarp);                
178                 status = smb2_create_blob_add(req, &blobs,
179                                               SMB2_CREATE_TAG_TWRP, data_blob_const(data, 8));
180                 if (!NT_STATUS_IS_OK(status)) {
181                         talloc_free(req);
182                         return NULL;
183                 }
184         }
185
186         if (io->in.sec_desc) {
187                 enum ndr_err_code ndr_err;
188                 DATA_BLOB sd_blob;
189                 ndr_err = ndr_push_struct_blob(&sd_blob, req, io->in.sec_desc,
190                                                (ndr_push_flags_fn_t)ndr_push_security_descriptor);
191                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
192                         talloc_free(req);
193                         return NULL;
194                 }
195                 status = smb2_create_blob_add(req, &blobs,
196                                               SMB2_CREATE_TAG_SECD, sd_blob);
197                 if (!NT_STATUS_IS_OK(status)) {
198                         talloc_free(req);
199                         return NULL;
200                 }
201         }
202
203         if (io->in.query_on_disk_id) {
204                 status = smb2_create_blob_add(req, &blobs,
205                                               SMB2_CREATE_TAG_QFID, data_blob(NULL, 0));
206                 if (!NT_STATUS_IS_OK(status)) {
207                         talloc_free(req);
208                         return NULL;
209                 }
210         }
211
212         if (io->in.lease_request) {
213                 uint8_t data[32];
214
215                 memcpy(&data[0], &io->in.lease_request->lease_key, 16);
216                 SIVAL(data, 16, io->in.lease_request->lease_state);
217                 SIVAL(data, 20, io->in.lease_request->lease_flags);
218                 SBVAL(data, 24, io->in.lease_request->lease_duration);
219
220                 status = smb2_create_blob_add(req, &blobs,
221                                               SMB2_CREATE_TAG_RQLS,
222                                               data_blob_const(data, 32));
223                 if (!NT_STATUS_IS_OK(status)) {
224                         talloc_free(req);
225                         return NULL;
226                 }
227         }
228
229         if (io->in.lease_request_v2) {
230                 struct smb2_lease *ls = &io->in.lease_request_v2;
231                 uint8_t data[52];
232
233                 memcpy(&data[0], &ls->lease_key, 16);
234                 SIVAL(data, 16, ls->lease_state);
235                 SIVAL(data, 20, ls->lease_flags);
236                 SBVAL(data, 24, ls->lease_duration);
237                 memcpy(&data[32], &ls->parent_lease_key, 16);
238                 SSVAL(data, 48, ls->lease_epoch);
239                 SSVAL(data, 50, 0); /* reserved */
240
241                 status = smb2_create_blob_add(req, &blobs,
242                                               SMB2_CREATE_TAG_RQLS,
243                                               data_blob_const(data, 52));
244                 if (!NT_STATUS_IS_OK(status)) {
245                         talloc_free(req);
246                         return NULL;
247                 }
248         }
249
250         if (io->in.app_instance_id) {
251                 uint8_t data[20];
252                 DATA_BLOB guid_blob;
253
254                 SSVAL(data, 0, 20); /* structure size */
255                 SSVAL(data, 2, 0);  /* reserved */
256
257                 status = GUID_to_ndr_blob(io->in.app_instance_id,
258                                           req, /* TALLOC_CTX */
259                                           &guid_blob);
260                 if (!NT_STATUS_IS_OK(status)) {
261                         talloc_free(req);
262                         return NULL;
263                 }
264                 memcpy(data+4, guid_blob.data, 16);
265
266                 status = smb2_create_blob_add(req, &blobs,
267                                               SMB2_CREATE_TAG_APP_INSTANCE_ID,
268                                               data_blob_const(data, 20));
269                 if (!NT_STATUS_IS_OK(status)) {
270                         talloc_free(req);
271                         return NULL;
272                 }
273         }
274
275         /* and any custom blobs */
276         for (i=0;i<io->in.blobs.num_blobs;i++) {
277                 status = smb2_create_blob_add(req, &blobs,
278                                               io->in.blobs.blobs[i].tag, 
279                                               io->in.blobs.blobs[i].data);
280                 if (!NT_STATUS_IS_OK(status)) {
281                         talloc_free(req);
282                         return NULL;
283                 }
284         }
285
286
287         status = smb2_create_blob_push(req, &blob, blobs);
288         if (!NT_STATUS_IS_OK(status)) {
289                 talloc_free(req);
290                 return NULL;
291         }
292
293         status = smb2_push_o32s32_blob(&req->out, 0x30, blob);
294         if (!NT_STATUS_IS_OK(status)) {
295                 talloc_free(req);
296                 return NULL;
297         }
298
299         data_blob_free(&blob);
300
301         smb2_transport_send(req);
302
303         return req;
304 }
305
306
307 /*
308   recv a create reply
309 */
310 NTSTATUS smb2_create_recv(struct smb2_request *req, TALLOC_CTX *mem_ctx, struct smb2_create *io)
311 {
312         NTSTATUS status;
313         DATA_BLOB blob;
314         int i;
315
316         if (!smb2_request_receive(req) || 
317             !smb2_request_is_ok(req)) {
318                 return smb2_request_destroy(req);
319         }
320
321         SMB2_CHECK_PACKET_RECV(req, 0x58, true);
322         ZERO_STRUCT(io->out);
323         io->out.oplock_level   = CVAL(req->in.body, 0x02);
324         io->out.reserved       = CVAL(req->in.body, 0x03);
325         io->out.create_action  = IVAL(req->in.body, 0x04);
326         io->out.create_time    = smbcli_pull_nttime(req->in.body, 0x08);
327         io->out.access_time    = smbcli_pull_nttime(req->in.body, 0x10);
328         io->out.write_time     = smbcli_pull_nttime(req->in.body, 0x18);
329         io->out.change_time    = smbcli_pull_nttime(req->in.body, 0x20);
330         io->out.alloc_size     = BVAL(req->in.body, 0x28);
331         io->out.size           = BVAL(req->in.body, 0x30);
332         io->out.file_attr      = IVAL(req->in.body, 0x38);
333         io->out.reserved2      = IVAL(req->in.body, 0x3C);
334         smb2_pull_handle(req->in.body+0x40, &io->out.file.handle);
335         status = smb2_pull_o32s32_blob(&req->in, mem_ctx, req->in.body+0x50, &blob);
336         if (!NT_STATUS_IS_OK(status)) {
337                 smb2_request_destroy(req);
338                 return status;
339         }
340
341         status = smb2_create_blob_parse(mem_ctx, blob, &io->out.blobs);
342         if (!NT_STATUS_IS_OK(status)) {
343                 smb2_request_destroy(req);
344                 return status;
345         }
346
347         /* pull out the parsed blobs */
348         for (i=0;i<io->out.blobs.num_blobs;i++) {
349                 if (strcmp(io->out.blobs.blobs[i].tag, SMB2_CREATE_TAG_MXAC) == 0) {
350                         /* TODO: this also contains a status field in
351                            first 4 bytes */
352                         if (io->out.blobs.blobs[i].data.length != 8) {
353                                 smb2_request_destroy(req);
354                                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
355                         }
356                         io->out.maximal_access = IVAL(io->out.blobs.blobs[i].data.data, 4);
357                 }
358                 if (strcmp(io->out.blobs.blobs[i].tag, SMB2_CREATE_TAG_QFID) == 0) {
359                         if (io->out.blobs.blobs[i].data.length != 32) {
360                                 smb2_request_destroy(req);
361                                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
362                         }
363                         memcpy(io->out.on_disk_id, io->out.blobs.blobs[i].data.data, 32);
364                 }
365                 if (strcmp(io->out.blobs.blobs[i].tag, SMB2_CREATE_TAG_RQLS) == 0) {
366                         struct smb2_lease *ls = NULL;
367                         uint8_t *data;
368
369                         ZERO_STRUCT(io->out.lease_response);
370                         ZERO_STRUCT(io->out.lease_response_v2);
371
372                         switch (io->out.blobs.blobs[i].data.length) {
373                         case 32:
374                                 ls = &io->out.lease_response;
375                                 break;
376                         case 52:
377                                 ls = &io->out.lease_response_v2;
378                                 break;
379                         default:
380                                 smb2_request_destroy(req);
381                                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
382                         }
383
384                         data = io->out.blobs.blobs[i].data.data;
385                         memcpy(&ls->lease_key, data, 16);
386                         ls->lease_state = IVAL(data, 16);
387                         ls->lease_flags = IVAL(data, 20);
388                         ls->lease_duration = BVAL(data, 24);
389
390                         if (io->out.blobs.blobs[i].data.length == 52) {
391                                 memcpy(&ls->parent_lease_key, data+32, 16);
392                                 ls->lease_epoch = SVAL(data, 48);
393                         }
394                 }
395                 if (strcmp(io->out.blobs.blobs[i].tag, SMB2_CREATE_TAG_DHNQ) == 0) {
396                         if (io->out.blobs.blobs[i].data.length != 8) {
397                                 smb2_request_destroy(req);
398                                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
399                         }
400                         io->out.durable_open = true;
401                 }
402                 if (strcmp(io->out.blobs.blobs[i].tag, SMB2_CREATE_TAG_DH2Q) == 0) {
403                         uint32_t flags;
404                         uint8_t *data;
405
406                         if (io->out.blobs.blobs[i].data.length != 8) {
407                                 smb2_request_destroy(req);
408                                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
409                         }
410
411                         io->out.durable_open = false;
412                         io->out.durable_open_v2 = true;
413
414                         data = io->out.blobs.blobs[i].data.data;
415                         io->out.timeout = IVAL(data, 0);
416                         flags = IVAL(data, 4);
417                         if ((flags & SMB2_DHANDLE_FLAG_PERSISTENT) != 0) {
418                                 io->out.persistent_open = true;
419                         }
420                 }
421         }
422
423         data_blob_free(&blob);
424
425         return smb2_request_destroy(req);
426 }
427
428 /*
429   sync create request
430 */
431 NTSTATUS smb2_create(struct smb2_tree *tree, TALLOC_CTX *mem_ctx, struct smb2_create *io)
432 {
433         struct smb2_request *req = smb2_create_send(tree, io);
434         return smb2_create_recv(req, mem_ctx, io);
435 }