Merge branch 'v4-0-test' of git://git.samba.org/samba into 4-0-local
[kai/samba.git] / source / 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/smb2/smb2.h"
25 #include "libcli/smb2/smb2_calls.h"
26
27 /*
28   add a blob to a smb2_create attribute blob
29 */
30 NTSTATUS smb2_create_blob_add(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, 
31                               const char *tag,
32                               DATA_BLOB add, bool last)
33 {
34         uint32_t ofs = blob->length;
35         size_t tag_length = strlen(tag);
36         uint8_t pad = smb2_padding_size(add.length+tag_length, 8);
37         if (!data_blob_realloc(mem_ctx, blob, 
38                                blob->length + 0x14 + tag_length + add.length + pad))
39                 return NT_STATUS_NO_MEMORY;
40         
41         if (last) {
42                 SIVAL(blob->data, ofs+0x00, 0);
43         } else {
44                 SIVAL(blob->data, ofs+0x00, 0x14 + tag_length + add.length + pad);
45         }
46         SSVAL(blob->data, ofs+0x04, 0x10); /* offset of tag */
47         SIVAL(blob->data, ofs+0x06, tag_length); /* tag length */
48         SSVAL(blob->data, ofs+0x0A, 0x14 + tag_length); /* offset of data */
49         SIVAL(blob->data, ofs+0x0C, add.length);
50         memcpy(blob->data+ofs+0x10, tag, tag_length);
51         SIVAL(blob->data, ofs+0x10+tag_length, 0); /* pad? */
52         memcpy(blob->data+ofs+0x14+tag_length, add.data, add.length);
53         memset(blob->data+ofs+0x14+tag_length+add.length, 0, pad);
54
55         return NT_STATUS_OK;
56 }
57
58 /*
59   send a create request
60 */
61 struct smb2_request *smb2_create_send(struct smb2_tree *tree, struct smb2_create *io)
62 {
63         struct smb2_request *req;
64         NTSTATUS status;
65         DATA_BLOB blob = data_blob(NULL, 0);
66
67         req = smb2_request_init_tree(tree, SMB2_OP_CREATE, 0x38, true, 0);
68         if (req == NULL) return NULL;
69
70         SCVAL(req->out.body, 0x02, io->in.security_flags);
71         SCVAL(req->out.body, 0x03, io->in.oplock_level);
72         SIVAL(req->out.body, 0x04, io->in.impersonation_level);
73         SBVAL(req->out.body, 0x08, io->in.create_flags);
74         SBVAL(req->out.body, 0x10, io->in.reserved);
75         SIVAL(req->out.body, 0x18, io->in.desired_access);
76         SIVAL(req->out.body, 0x1C, io->in.file_attributes);
77         SIVAL(req->out.body, 0x20, io->in.share_access);
78         SIVAL(req->out.body, 0x24, io->in.create_disposition);
79         SIVAL(req->out.body, 0x28, io->in.create_options);
80
81         status = smb2_push_o16s16_string(&req->out, 0x2C, io->in.fname);
82         if (!NT_STATUS_IS_OK(status)) {
83                 talloc_free(req);
84                 return NULL;
85         }
86
87         if (io->in.eas.num_eas != 0) {
88                 DATA_BLOB b = data_blob_talloc(req, NULL, 
89                                                ea_list_size_chained(io->in.eas.num_eas, io->in.eas.eas));
90                 ea_put_list_chained(b.data, io->in.eas.num_eas, io->in.eas.eas);
91                 status = smb2_create_blob_add(req, &blob, SMB2_CREATE_TAG_EXTA, b, false);
92                 if (!NT_STATUS_IS_OK(status)) {
93                         talloc_free(req);
94                         return NULL;
95                 }
96                 data_blob_free(&b);
97         }
98
99         /* an empty MxAc tag seems to be used to ask the server to
100            return the maximum access mask allowed on the file */
101         status = smb2_create_blob_add(req, &blob, SMB2_CREATE_TAG_MXAC, 
102                                       data_blob(NULL, 0), true);
103
104         if (!NT_STATUS_IS_OK(status)) {
105                 talloc_free(req);
106                 return NULL;
107         }
108         status = smb2_push_o32s32_blob(&req->out, 0x30, blob);
109         if (!NT_STATUS_IS_OK(status)) {
110                 talloc_free(req);
111                 return NULL;
112         }
113
114         smb2_transport_send(req);
115
116         return req;
117 }
118
119
120 /*
121   recv a create reply
122 */
123 NTSTATUS smb2_create_recv(struct smb2_request *req, TALLOC_CTX *mem_ctx, struct smb2_create *io)
124 {
125         NTSTATUS status;
126
127         if (!smb2_request_receive(req) || 
128             !smb2_request_is_ok(req)) {
129                 return smb2_request_destroy(req);
130         }
131
132         SMB2_CHECK_PACKET_RECV(req, 0x58, true);
133
134         io->out.oplock_level   = CVAL(req->in.body, 0x02);
135         io->out.reserved       = CVAL(req->in.body, 0x03);
136         io->out.create_action  = IVAL(req->in.body, 0x04);
137         io->out.create_time    = smbcli_pull_nttime(req->in.body, 0x08);
138         io->out.access_time    = smbcli_pull_nttime(req->in.body, 0x10);
139         io->out.write_time     = smbcli_pull_nttime(req->in.body, 0x18);
140         io->out.change_time    = smbcli_pull_nttime(req->in.body, 0x20);
141         io->out.alloc_size     = BVAL(req->in.body, 0x28);
142         io->out.size           = BVAL(req->in.body, 0x30);
143         io->out.file_attr      = IVAL(req->in.body, 0x38);
144         io->out.reserved2      = IVAL(req->in.body, 0x3C);
145         smb2_pull_handle(req->in.body+0x40, &io->out.file.handle);
146         status = smb2_pull_o32s32_blob(&req->in, mem_ctx, req->in.body+0x50, &io->out.blob);
147         if (!NT_STATUS_IS_OK(status)) {
148                 smb2_request_destroy(req);
149                 return status;
150         }
151
152         return smb2_request_destroy(req);
153 }
154
155 /*
156   sync create request
157 */
158 NTSTATUS smb2_create(struct smb2_tree *tree, TALLOC_CTX *mem_ctx, struct smb2_create *io)
159 {
160         struct smb2_request *req = smb2_create_send(tree, io);
161         return smb2_create_recv(req, mem_ctx, io);
162 }