r11775: added support for creating files on SMB2 with initial EA lists and an ACL
[ira/wip.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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/smb2/smb2.h"
26 #include "libcli/smb2/smb2_calls.h"
27
28 #define CREATE_TAG_EA 0x41747845 /* "ExtA" */
29 #define CREATE_TAG_SD 0x6341784D /* "MxAc" */
30
31 /*
32   add a blob to a smb2_create attribute blob
33 */
34 static NTSTATUS smb2_create_blob_add(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, 
35                                      uint32_t tag,
36                                      DATA_BLOB add, BOOL last)
37 {
38         NTSTATUS status;
39         uint32_t ofs = blob->length;
40         status = data_blob_realloc(mem_ctx, blob, blob->length + 0x18 + add.length);
41         NT_STATUS_NOT_OK_RETURN(status);
42         
43         if (last) {
44                 SIVAL(blob->data, ofs+0x00, 0);
45         } else {
46                 SIVAL(blob->data, ofs+0x00, 0x18 + add.length);
47         }
48         SSVAL(blob->data, ofs+0x04, 0x10); /* offset of tag */
49         SIVAL(blob->data, ofs+0x06, 0x04); /* tag length */
50         SSVAL(blob->data, ofs+0x0A, 0x18); /* offset of data */
51         SIVAL(blob->data, ofs+0x0C, add.length);
52         SIVAL(blob->data, ofs+0x10, tag);
53         SIVAL(blob->data, ofs+0x14, 0); /* pad? */
54         memcpy(blob->data+ofs+0x18, add.data, add.length);
55
56         return NT_STATUS_OK;
57 }
58
59 /*
60   send a create request
61 */
62 struct smb2_request *smb2_create_send(struct smb2_tree *tree, struct smb2_create *io)
63 {
64         struct smb2_request *req;
65         NTSTATUS status;
66         DATA_BLOB blob = data_blob(NULL, 0);
67
68         req = smb2_request_init_tree(tree, SMB2_OP_CREATE, 0x38, 1);
69         if (req == NULL) return NULL;
70
71         SSVAL(req->out.body, 0x02, io->in.oplock_flags);
72         SIVAL(req->out.body, 0x04, io->in.unknown2);
73         SIVAL(req->out.body, 0x08, io->in.unknown3[0]);
74         SIVAL(req->out.body, 0x0C, io->in.unknown3[1]);
75         SIVAL(req->out.body, 0x10, io->in.unknown3[2]);
76         SIVAL(req->out.body, 0x14, io->in.unknown3[3]);
77         SIVAL(req->out.body, 0x18, io->in.access_mask);
78         SIVAL(req->out.body, 0x1C, io->in.file_attr);
79         SIVAL(req->out.body, 0x20, io->in.share_access);
80         SIVAL(req->out.body, 0x24, io->in.open_disposition);
81         SIVAL(req->out.body, 0x28, io->in.create_options);
82
83         status = smb2_push_o16s16_string(&req->out, 0x2C, io->in.fname);
84         if (!NT_STATUS_IS_OK(status)) {
85                 talloc_free(req);
86                 return NULL;
87         }
88
89         if (io->in.eas.num_eas != 0) {
90                 DATA_BLOB b = data_blob_talloc(req, NULL, 
91                                                ea_list_size_chained(io->in.eas.num_eas, io->in.eas.eas));
92                 ea_put_list_chained(b.data, io->in.eas.num_eas, io->in.eas.eas);
93                 status = smb2_create_blob_add(req, &blob, CREATE_TAG_EA, b, False);
94                 if (!NT_STATUS_IS_OK(status)) {
95                         talloc_free(req);
96                         return NULL;
97                 }
98                 data_blob_free(&b);
99         }
100
101         if (io->in.sd != NULL) {
102                 DATA_BLOB b;
103                 status = ndr_push_struct_blob(&b, req, io->in.sd,
104                                               (ndr_push_flags_fn_t)ndr_push_security_descriptor);
105                 if (!NT_STATUS_IS_OK(status)) {
106                         talloc_free(req);
107                         return NULL;
108                 }
109                 status = smb2_create_blob_add(req, &blob, CREATE_TAG_SD, b, True);
110         } else {
111                 status = smb2_create_blob_add(req, &blob, CREATE_TAG_SD, data_blob(NULL, 0), True);
112         }
113
114         if (!NT_STATUS_IS_OK(status)) {
115                 talloc_free(req);
116                 return NULL;
117         }
118         status = smb2_push_o32s32_blob(&req->out, 0x30, blob);
119         if (!NT_STATUS_IS_OK(status)) {
120                 talloc_free(req);
121                 return NULL;
122         }
123
124         smb2_transport_send(req);
125
126         return req;
127 }
128
129
130 /*
131   recv a create reply
132 */
133 NTSTATUS smb2_create_recv(struct smb2_request *req, TALLOC_CTX *mem_ctx, struct smb2_create *io)
134 {
135         NTSTATUS status;
136
137         if (!smb2_request_receive(req) || 
138             !smb2_request_is_ok(req)) {
139                 return smb2_request_destroy(req);
140         }
141
142         SMB2_CHECK_PACKET_RECV(req, 0x58, True);
143
144         io->out.oplock_flags   = SVAL(req->in.body, 0x02);
145         io->out.create_action  = IVAL(req->in.body, 0x04);
146         io->out.create_time    = smbcli_pull_nttime(req->in.body, 0x08);
147         io->out.access_time    = smbcli_pull_nttime(req->in.body, 0x10);
148         io->out.write_time     = smbcli_pull_nttime(req->in.body, 0x18);
149         io->out.change_time    = smbcli_pull_nttime(req->in.body, 0x20);
150         io->out.alloc_size     = BVAL(req->in.body, 0x28);
151         io->out.size           = BVAL(req->in.body, 0x30);
152         io->out.file_attr      = IVAL(req->in.body, 0x38);
153         io->out._pad           = IVAL(req->in.body, 0x3C);
154         smb2_pull_handle(req->in.body+0x40, &io->out.handle);
155         status = smb2_pull_o32s32_blob(&req->in, mem_ctx, req->in.body+0x50, &io->out.blob);
156         if (!NT_STATUS_IS_OK(status)) {
157                 smb2_request_destroy(req);
158                 return status;
159         }
160
161         return smb2_request_destroy(req);
162 }
163
164 /*
165   sync create request
166 */
167 NTSTATUS smb2_create(struct smb2_tree *tree, TALLOC_CTX *mem_ctx, struct smb2_create *io)
168 {
169         struct smb2_request *req = smb2_create_send(tree, io);
170         return smb2_create_recv(req, mem_ctx, io);
171 }