docs: Remove references to default paramters in TOSHARG-PDC
[Samba.git] / libcli / smb / smb2cli_create.c
blob4d264efb575a85db226c4e3f046607d975bb9cc9
1 /*
2 Unix SMB/CIFS implementation.
3 smb2 lib
4 Copyright (C) Volker Lendecke 2011
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "system/network.h"
22 #include "lib/util/tevent_ntstatus.h"
23 #include "smb_common.h"
24 #include "smbXcli_base.h"
25 #include "smb2_create_blob.h"
27 struct smb2cli_create_state {
28 uint8_t fixed[56];
30 uint8_t oplock_level;
31 uint32_t create_action;
32 NTTIME creation_time;
33 NTTIME last_access_time;
34 NTTIME last_write_time;
35 NTTIME change_time;
36 uint64_t allocation_size;
37 uint64_t end_of_file;
38 uint32_t file_attributes;
39 uint64_t fid_persistent;
40 uint64_t fid_volatile;
41 struct smb2_create_blobs blobs;
44 static void smb2cli_create_done(struct tevent_req *subreq);
46 struct tevent_req *smb2cli_create_send(
47 TALLOC_CTX *mem_ctx,
48 struct tevent_context *ev,
49 struct smbXcli_conn *conn,
50 uint32_t timeout_msec,
51 struct smbXcli_session *session,
52 struct smbXcli_tcon *tcon,
53 const char *filename,
54 uint8_t oplock_level, /* SMB2_OPLOCK_LEVEL_* */
55 uint32_t impersonation_level, /* SMB2_IMPERSONATION_* */
56 uint32_t desired_access,
57 uint32_t file_attributes,
58 uint32_t share_access,
59 uint32_t create_disposition,
60 uint32_t create_options,
61 struct smb2_create_blobs *blobs)
63 struct tevent_req *req, *subreq;
64 struct smb2cli_create_state *state;
65 uint8_t *fixed;
66 uint8_t *name_utf16;
67 size_t name_utf16_len;
68 DATA_BLOB blob;
69 NTSTATUS status;
70 size_t blobs_offset;
71 uint8_t *dyn;
72 size_t dyn_len;
74 req = tevent_req_create(mem_ctx, &state,
75 struct smb2cli_create_state);
76 if (req == NULL) {
77 return NULL;
80 if (!convert_string_talloc(state, CH_UNIX, CH_UTF16,
81 filename, strlen(filename),
82 &name_utf16, &name_utf16_len)) {
83 tevent_req_oom(req);
84 return tevent_req_post(req, ev);
87 if (strlen(filename) == 0) {
88 TALLOC_FREE(name_utf16);
89 name_utf16_len = 0;
92 fixed = state->fixed;
94 SSVAL(fixed, 0, 57);
95 SCVAL(fixed, 3, oplock_level);
96 SIVAL(fixed, 4, impersonation_level);
97 SIVAL(fixed, 24, desired_access);
98 SIVAL(fixed, 28, file_attributes);
99 SIVAL(fixed, 32, share_access);
100 SIVAL(fixed, 36, create_disposition);
101 SIVAL(fixed, 40, create_options);
103 SSVAL(fixed, 44, SMB2_HDR_BODY + 56);
104 SSVAL(fixed, 46, name_utf16_len);
106 blob = data_blob_null;
108 if (blobs != NULL) {
109 status = smb2_create_blob_push(state, &blob, *blobs);
110 if (tevent_req_nterror(req, status)) {
111 return tevent_req_post(req, ev);
115 blobs_offset = name_utf16_len;
116 blobs_offset = ((blobs_offset + 3) & ~3);
118 if (blob.length > 0) {
119 SIVAL(fixed, 48, blobs_offset + SMB2_HDR_BODY + 56);
120 SIVAL(fixed, 52, blob.length);
123 dyn_len = MAX(1, blobs_offset + blob.length);
124 dyn = talloc_zero_array(state, uint8_t, dyn_len);
125 if (tevent_req_nomem(dyn, req)) {
126 return tevent_req_post(req, ev);
129 if (name_utf16) {
130 memcpy(dyn, name_utf16, name_utf16_len);
131 TALLOC_FREE(name_utf16);
134 if (blob.data != NULL) {
135 memcpy(dyn + blobs_offset,
136 blob.data, blob.length);
137 data_blob_free(&blob);
140 subreq = smb2cli_req_send(state, ev, conn, SMB2_OP_CREATE,
141 0, 0, /* flags */
142 timeout_msec,
143 tcon,
144 session,
145 state->fixed, sizeof(state->fixed),
146 dyn, dyn_len);
147 if (tevent_req_nomem(subreq, req)) {
148 return tevent_req_post(req, ev);
150 tevent_req_set_callback(subreq, smb2cli_create_done, req);
151 return req;
154 static void smb2cli_create_done(struct tevent_req *subreq)
156 struct tevent_req *req =
157 tevent_req_callback_data(subreq,
158 struct tevent_req);
159 struct smb2cli_create_state *state =
160 tevent_req_data(req,
161 struct smb2cli_create_state);
162 NTSTATUS status;
163 struct iovec *iov;
164 uint8_t *body;
165 uint32_t offset, length;
166 static const struct smb2cli_req_expected_response expected[] = {
168 .status = NT_STATUS_OK,
169 .body_size = 0x59
173 status = smb2cli_req_recv(subreq, state, &iov,
174 expected, ARRAY_SIZE(expected));
175 if (tevent_req_nterror(req, status)) {
176 return;
179 body = (uint8_t *)iov[1].iov_base;
181 state->oplock_level = CVAL(body, 2);
182 state->create_action = IVAL(body, 4);
183 state->creation_time = BVAL(body, 8);
184 state->last_access_time = BVAL(body, 16);
185 state->last_write_time = BVAL(body, 24);
186 state->change_time = BVAL(body, 32);
187 state->allocation_size = BVAL(body, 40);
188 state->end_of_file = BVAL(body, 48);
189 state->file_attributes = IVAL(body, 56);
190 state->fid_persistent = BVAL(body, 64);
191 state->fid_volatile = BVAL(body, 72);
193 offset = IVAL(body, 80);
194 length = IVAL(body, 84);
196 if ((offset != 0) && (length != 0)) {
197 if ((offset != SMB2_HDR_BODY + 88) ||
198 (length > iov[2].iov_len)) {
199 tevent_req_nterror(
200 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
201 return;
203 status = smb2_create_blob_parse(
204 state, data_blob_const(iov[2].iov_base, length),
205 &state->blobs);
206 if (tevent_req_nterror(req, status)) {
207 return;
210 tevent_req_done(req);
213 NTSTATUS smb2cli_create_recv(struct tevent_req *req,
214 uint64_t *fid_persistent,
215 uint64_t *fid_volatile)
217 struct smb2cli_create_state *state =
218 tevent_req_data(req,
219 struct smb2cli_create_state);
220 NTSTATUS status;
222 if (tevent_req_is_nterror(req, &status)) {
223 return status;
225 *fid_persistent = state->fid_persistent;
226 *fid_volatile = state->fid_volatile;
227 return NT_STATUS_OK;
230 NTSTATUS smb2cli_create(struct smbXcli_conn *conn,
231 uint32_t timeout_msec,
232 struct smbXcli_session *session,
233 struct smbXcli_tcon *tcon,
234 const char *filename,
235 uint8_t oplock_level, /* SMB2_OPLOCK_LEVEL_* */
236 uint32_t impersonation_level, /* SMB2_IMPERSONATION_* */
237 uint32_t desired_access,
238 uint32_t file_attributes,
239 uint32_t share_access,
240 uint32_t create_disposition,
241 uint32_t create_options,
242 struct smb2_create_blobs *blobs,
243 uint64_t *fid_persistent,
244 uint64_t *fid_volatile)
246 TALLOC_CTX *frame = talloc_stackframe();
247 struct tevent_context *ev;
248 struct tevent_req *req;
249 NTSTATUS status = NT_STATUS_NO_MEMORY;
251 if (smbXcli_conn_has_async_calls(conn)) {
253 * Can't use sync call while an async call is in flight
255 status = NT_STATUS_INVALID_PARAMETER;
256 goto fail;
258 ev = tevent_context_init(frame);
259 if (ev == NULL) {
260 goto fail;
262 req = smb2cli_create_send(frame, ev, conn, timeout_msec,
263 session, tcon,
264 filename, oplock_level,
265 impersonation_level, desired_access,
266 file_attributes, share_access,
267 create_disposition, create_options,
268 blobs);
269 if (req == NULL) {
270 goto fail;
272 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
273 goto fail;
275 status = smb2cli_create_recv(req, fid_persistent, fid_volatile);
276 fail:
277 TALLOC_FREE(frame);
278 return status;