2 Unix SMB/CIFS implementation.
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/>.
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 {
30 uint64_t fid_persistent;
31 uint64_t fid_volatile;
32 struct smb_create_returns cr;
33 struct smb2_create_blobs blobs;
34 struct tevent_req *subreq;
37 static void smb2cli_create_done(struct tevent_req *subreq);
38 static bool smb2cli_create_cancel(struct tevent_req *req);
40 struct tevent_req *smb2cli_create_send(
42 struct tevent_context *ev,
43 struct smbXcli_conn *conn,
44 uint32_t timeout_msec,
45 struct smbXcli_session *session,
46 struct smbXcli_tcon *tcon,
48 uint8_t oplock_level, /* SMB2_OPLOCK_LEVEL_* */
49 uint32_t impersonation_level, /* SMB2_IMPERSONATION_* */
50 uint32_t desired_access,
51 uint32_t file_attributes,
52 uint32_t share_access,
53 uint32_t create_disposition,
54 uint32_t create_options,
55 struct smb2_create_blobs *blobs)
57 struct tevent_req *req, *subreq;
58 struct smb2cli_create_state *state;
61 size_t name_utf16_len;
68 uint32_t additional_flags = 0;
69 uint32_t clear_flags = 0;
71 req = tevent_req_create(mem_ctx, &state,
72 struct smb2cli_create_state);
77 if (!convert_string_talloc(state, CH_UNIX, CH_UTF16,
78 filename, strlen(filename),
79 &name_utf16, &name_utf16_len)) {
81 return tevent_req_post(req, ev);
84 if (strlen(filename) == 0) {
85 TALLOC_FREE(name_utf16);
92 SCVAL(fixed, 3, oplock_level);
93 SIVAL(fixed, 4, impersonation_level);
94 SIVAL(fixed, 24, desired_access);
95 SIVAL(fixed, 28, file_attributes);
96 SIVAL(fixed, 32, share_access);
97 SIVAL(fixed, 36, create_disposition);
98 SIVAL(fixed, 40, create_options);
100 SSVAL(fixed, 44, SMB2_HDR_BODY + 56);
101 SSVAL(fixed, 46, name_utf16_len);
103 blob = data_blob_null;
106 status = smb2_create_blob_push(state, &blob, *blobs);
107 if (tevent_req_nterror(req, status)) {
108 return tevent_req_post(req, ev);
112 blobs_offset = name_utf16_len;
113 blobs_offset = ((blobs_offset + 3) & ~3);
115 if (blob.length > 0) {
116 SIVAL(fixed, 48, blobs_offset + SMB2_HDR_BODY + 56);
117 SIVAL(fixed, 52, blob.length);
120 dyn_len = MAX(1, blobs_offset + blob.length);
121 dyn = talloc_zero_array(state, uint8_t, dyn_len);
122 if (tevent_req_nomem(dyn, req)) {
123 return tevent_req_post(req, ev);
127 memcpy(dyn, name_utf16, name_utf16_len);
128 TALLOC_FREE(name_utf16);
131 if (blob.data != NULL) {
132 memcpy(dyn + blobs_offset,
133 blob.data, blob.length);
134 data_blob_free(&blob);
137 if (smbXcli_conn_dfs_supported(conn) &&
138 smbXcli_tcon_is_dfs_share(tcon))
140 additional_flags |= SMB2_HDR_FLAG_DFS;
144 * We use max_dyn_len = 0
145 * as we don't explicitly ask for any output length.
147 * But it's still possible for the server to return
148 * large create blobs.
152 subreq = smb2cli_req_send(state, ev, conn, SMB2_OP_CREATE,
153 additional_flags, clear_flags,
157 state->fixed, sizeof(state->fixed),
160 if (tevent_req_nomem(subreq, req)) {
161 return tevent_req_post(req, ev);
163 tevent_req_set_callback(subreq, smb2cli_create_done, req);
165 state->subreq = subreq;
166 tevent_req_set_cancel_fn(req, smb2cli_create_cancel);
171 static bool smb2cli_create_cancel(struct tevent_req *req)
173 struct smb2cli_create_state *state = tevent_req_data(req,
174 struct smb2cli_create_state);
175 return tevent_req_cancel(state->subreq);
178 static void smb2cli_create_done(struct tevent_req *subreq)
180 struct tevent_req *req =
181 tevent_req_callback_data(subreq,
183 struct smb2cli_create_state *state =
185 struct smb2cli_create_state);
189 uint32_t offset, length;
190 static const struct smb2cli_req_expected_response expected[] = {
192 .status = NT_STATUS_OK,
197 status = smb2cli_req_recv(subreq, state, &iov,
198 expected, ARRAY_SIZE(expected));
200 if (tevent_req_nterror(req, status)) {
204 body = (uint8_t *)iov[1].iov_base;
206 state->cr.oplock_level = CVAL(body, 2);
207 state->cr.create_action = IVAL(body, 4);
208 state->cr.creation_time = BVAL(body, 8);
209 state->cr.last_access_time = BVAL(body, 16);
210 state->cr.last_write_time = BVAL(body, 24);
211 state->cr.change_time = BVAL(body, 32);
212 state->cr.allocation_size = BVAL(body, 40);
213 state->cr.end_of_file = BVAL(body, 48);
214 state->cr.file_attributes = IVAL(body, 56);
215 state->fid_persistent = BVAL(body, 64);
216 state->fid_volatile = BVAL(body, 72);
218 offset = IVAL(body, 80);
219 length = IVAL(body, 84);
221 if ((offset != 0) && (length != 0)) {
222 if ((offset != SMB2_HDR_BODY + 88) ||
223 (length > iov[2].iov_len)) {
225 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
228 status = smb2_create_blob_parse(
229 state, data_blob_const(iov[2].iov_base, length),
231 if (tevent_req_nterror(req, status)) {
235 tevent_req_done(req);
238 NTSTATUS smb2cli_create_recv(struct tevent_req *req,
239 uint64_t *fid_persistent,
240 uint64_t *fid_volatile,
241 struct smb_create_returns *cr,
243 struct smb2_create_blobs *blobs)
245 struct smb2cli_create_state *state =
247 struct smb2cli_create_state);
250 if (tevent_req_is_nterror(req, &status)) {
253 *fid_persistent = state->fid_persistent;
254 *fid_volatile = state->fid_volatile;
259 blobs->num_blobs = state->blobs.num_blobs;
260 blobs->blobs = talloc_move(mem_ctx, &state->blobs.blobs);
265 NTSTATUS smb2cli_create(struct smbXcli_conn *conn,
266 uint32_t timeout_msec,
267 struct smbXcli_session *session,
268 struct smbXcli_tcon *tcon,
269 const char *filename,
270 uint8_t oplock_level, /* SMB2_OPLOCK_LEVEL_* */
271 uint32_t impersonation_level, /* SMB2_IMPERSONATION_* */
272 uint32_t desired_access,
273 uint32_t file_attributes,
274 uint32_t share_access,
275 uint32_t create_disposition,
276 uint32_t create_options,
277 struct smb2_create_blobs *blobs,
278 uint64_t *fid_persistent,
279 uint64_t *fid_volatile,
280 struct smb_create_returns *cr,
282 struct smb2_create_blobs *ret_blobs)
284 TALLOC_CTX *frame = talloc_stackframe();
285 struct tevent_context *ev;
286 struct tevent_req *req;
287 NTSTATUS status = NT_STATUS_NO_MEMORY;
289 if (smbXcli_conn_has_async_calls(conn)) {
291 * Can't use sync call while an async call is in flight
293 status = NT_STATUS_INVALID_PARAMETER;
296 ev = samba_tevent_context_init(frame);
300 req = smb2cli_create_send(frame, ev, conn, timeout_msec,
302 filename, oplock_level,
303 impersonation_level, desired_access,
304 file_attributes, share_access,
305 create_disposition, create_options,
310 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
313 status = smb2cli_create_recv(req, fid_persistent, fid_volatile, cr,