1901cb4977e4106a74e351a514a3a92287e68bc3
[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
28 /*
29   add a blob to a smb2_create attribute blob
30 */
31 static NTSTATUS smb2_create_blob_push_one(TALLOC_CTX *mem_ctx, DATA_BLOB *buffer,
32                                           const struct smb2_create_blob *blob,
33                                           bool last)
34 {
35         uint32_t ofs = buffer->length;
36         size_t tag_length = strlen(blob->tag);
37         uint8_t pad = smb2_padding_size(blob->data.length+tag_length, 4);
38
39         if (!data_blob_realloc(mem_ctx, buffer,
40                                buffer->length + 0x14 + tag_length + blob->data.length + pad))
41                 return NT_STATUS_NO_MEMORY;
42
43         if (last) {
44                 SIVAL(buffer->data, ofs+0x00, 0);
45         } else {
46                 SIVAL(buffer->data, ofs+0x00, 0x14 + tag_length + blob->data.length + pad);
47         }
48         SSVAL(buffer->data, ofs+0x04, 0x10); /* offset of tag */
49         SIVAL(buffer->data, ofs+0x06, tag_length); /* tag length */
50         SSVAL(buffer->data, ofs+0x0A, 0x14 + tag_length); /* offset of data */
51         SIVAL(buffer->data, ofs+0x0C, blob->data.length);
52         memcpy(buffer->data+ofs+0x10, blob->tag, tag_length);
53         SIVAL(buffer->data, ofs+0x10+tag_length, 0); /* pad? */
54         memcpy(buffer->data+ofs+0x14+tag_length, blob->data.data, blob->data.length);
55         memset(buffer->data+ofs+0x14+tag_length+blob->data.length, 0, pad);
56
57         return NT_STATUS_OK;
58 }
59
60 NTSTATUS smb2_create_blob_add(TALLOC_CTX *mem_ctx, struct smb2_create_blobs *b,
61                               const char *tag, DATA_BLOB data)
62 {
63         struct smb2_create_blob *array;
64
65         array = talloc_realloc(mem_ctx, b->blobs,
66                                struct smb2_create_blob,
67                                b->num_blobs + 1);
68         NT_STATUS_HAVE_NO_MEMORY(array);
69         b->blobs = array;
70
71         b->blobs[b->num_blobs].tag = talloc_strdup(b->blobs, tag);
72         NT_STATUS_HAVE_NO_MEMORY(b->blobs[b->num_blobs].tag);
73
74         if (data.data) {
75                 b->blobs[b->num_blobs].data = data_blob_talloc(b->blobs,
76                                                                data.data,
77                                                                data.length);
78                 NT_STATUS_HAVE_NO_MEMORY(b->blobs[b->num_blobs].data.data);
79         } else {
80                 b->blobs[b->num_blobs].data = data_blob(NULL, 0);
81         }
82
83         b->num_blobs += 1;
84
85         return NT_STATUS_OK;
86 }
87
88 /*
89   send a create request
90 */
91 struct smb2_request *smb2_create_send(struct smb2_tree *tree, struct smb2_create *io)
92 {
93         struct smb2_request *req;
94         NTSTATUS status;
95         DATA_BLOB blob = data_blob(NULL, 0);
96         uint32_t i;
97         struct smb2_create_blobs blobs = io->in.blobs;
98
99         req = smb2_request_init_tree(tree, SMB2_OP_CREATE, 0x38, true, 0);
100         if (req == NULL) return NULL;
101
102         SCVAL(req->out.body, 0x02, io->in.security_flags);
103         SCVAL(req->out.body, 0x03, io->in.oplock_level);
104         SIVAL(req->out.body, 0x04, io->in.impersonation_level);
105         SBVAL(req->out.body, 0x08, io->in.create_flags);
106         SBVAL(req->out.body, 0x10, io->in.reserved);
107         SIVAL(req->out.body, 0x18, io->in.desired_access);
108         SIVAL(req->out.body, 0x1C, io->in.file_attributes);
109         SIVAL(req->out.body, 0x20, io->in.share_access);
110         SIVAL(req->out.body, 0x24, io->in.create_disposition);
111         SIVAL(req->out.body, 0x28, io->in.create_options);
112
113         status = smb2_push_o16s16_string(&req->out, 0x2C, io->in.fname);
114         if (!NT_STATUS_IS_OK(status)) {
115                 talloc_free(req);
116                 return NULL;
117         }
118
119         if (io->in.eas.num_eas != 0) {
120                 DATA_BLOB b = data_blob_talloc(req, NULL, 
121                                                ea_list_size_chained(io->in.eas.num_eas, io->in.eas.eas, 4));
122                 ea_put_list_chained(b.data, io->in.eas.num_eas, io->in.eas.eas, 4);
123                 status = smb2_create_blob_add(req, &blobs,
124                                               SMB2_CREATE_TAG_EXTA, b);
125                 if (!NT_STATUS_IS_OK(status)) {
126                         talloc_free(req);
127                         return NULL;
128                 }
129                 data_blob_free(&b);
130         }
131
132         /* an empty MxAc tag seems to be used to ask the server to
133            return the maximum access mask allowed on the file */
134         status = smb2_create_blob_add(req, &blobs,
135                                       SMB2_CREATE_TAG_MXAC, data_blob(NULL, 0));
136         if (!NT_STATUS_IS_OK(status)) {
137                 talloc_free(req);
138                 return NULL;
139         }
140
141         for (i=0; i < blobs.num_blobs; i++) {
142                 bool last = false;
143                 const struct smb2_create_blob *c;
144
145                 if ((i + 1) == blobs.num_blobs) {
146                         last = true;
147                 }
148
149                 c = &blobs.blobs[i];
150                 status = smb2_create_blob_push_one(req, &blob,
151                                                    c, last);
152                 if (!NT_STATUS_IS_OK(status)) {
153                         talloc_free(req);
154                         return NULL;
155                 }
156         }
157
158         status = smb2_push_o32s32_blob(&req->out, 0x30, blob);
159         if (!NT_STATUS_IS_OK(status)) {
160                 talloc_free(req);
161                 return NULL;
162         }
163
164         smb2_transport_send(req);
165
166         return req;
167 }
168
169
170 /*
171   recv a create reply
172 */
173 NTSTATUS smb2_create_recv(struct smb2_request *req, TALLOC_CTX *mem_ctx, struct smb2_create *io)
174 {
175         NTSTATUS status;
176
177         if (!smb2_request_receive(req) || 
178             !smb2_request_is_ok(req)) {
179                 return smb2_request_destroy(req);
180         }
181
182         SMB2_CHECK_PACKET_RECV(req, 0x58, true);
183
184         io->out.oplock_level   = CVAL(req->in.body, 0x02);
185         io->out.reserved       = CVAL(req->in.body, 0x03);
186         io->out.create_action  = IVAL(req->in.body, 0x04);
187         io->out.create_time    = smbcli_pull_nttime(req->in.body, 0x08);
188         io->out.access_time    = smbcli_pull_nttime(req->in.body, 0x10);
189         io->out.write_time     = smbcli_pull_nttime(req->in.body, 0x18);
190         io->out.change_time    = smbcli_pull_nttime(req->in.body, 0x20);
191         io->out.alloc_size     = BVAL(req->in.body, 0x28);
192         io->out.size           = BVAL(req->in.body, 0x30);
193         io->out.file_attr      = IVAL(req->in.body, 0x38);
194         io->out.reserved2      = IVAL(req->in.body, 0x3C);
195         smb2_pull_handle(req->in.body+0x40, &io->out.file.handle);
196         status = smb2_pull_o32s32_blob(&req->in, mem_ctx, req->in.body+0x50, &io->out.blob);
197         if (!NT_STATUS_IS_OK(status)) {
198                 smb2_request_destroy(req);
199                 return status;
200         }
201
202         return smb2_request_destroy(req);
203 }
204
205 /*
206   sync create request
207 */
208 NTSTATUS smb2_create(struct smb2_tree *tree, TALLOC_CTX *mem_ctx, struct smb2_create *io)
209 {
210         struct smb2_request *req = smb2_create_send(tree, io);
211         return smb2_create_recv(req, mem_ctx, io);
212 }