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/>.
22 #include "async_smb.h"
23 #include "../libcli/smb/smbXcli_base.h"
25 #include "libsmb/proto.h"
26 #include "lib/util/tevent_ntstatus.h"
27 #include "libcli/smb/smb2_create_blob.h"
29 struct smb2cli_create_state {
33 uint32_t create_action;
35 NTTIME last_access_time;
36 NTTIME last_write_time;
38 uint64_t allocation_size;
40 uint32_t file_attributes;
41 uint64_t fid_persistent;
42 uint64_t fid_volatile;
43 struct smb2_create_blobs blobs;
46 static void smb2cli_create_done(struct tevent_req *subreq);
48 struct tevent_req *smb2cli_create_send(
50 struct tevent_context *ev,
51 struct smbXcli_conn *conn,
52 uint32_t timeout_msec,
53 struct smbXcli_session *session,
56 uint8_t oplock_level, /* SMB2_OPLOCK_LEVEL_* */
57 uint32_t impersonation_level, /* SMB2_IMPERSONATION_* */
58 uint32_t desired_access,
59 uint32_t file_attributes,
60 uint32_t share_access,
61 uint32_t create_disposition,
62 uint32_t create_options,
63 struct smb2_create_blobs *blobs)
65 struct tevent_req *req, *subreq;
66 struct smb2cli_create_state *state;
69 size_t name_utf16_len;
76 req = tevent_req_create(mem_ctx, &state,
77 struct smb2cli_create_state);
82 if (!convert_string_talloc(state, CH_UNIX, CH_UTF16,
83 filename, strlen(filename),
84 &name_utf16, &name_utf16_len)) {
86 return tevent_req_post(req, ev);
89 if (strlen(filename) == 0) {
90 TALLOC_FREE(name_utf16);
97 SCVAL(fixed, 3, oplock_level);
98 SIVAL(fixed, 4, impersonation_level);
99 SIVAL(fixed, 24, desired_access);
100 SIVAL(fixed, 28, file_attributes);
101 SIVAL(fixed, 32, share_access);
102 SIVAL(fixed, 36, create_disposition);
103 SIVAL(fixed, 40, create_options);
105 SSVAL(fixed, 44, SMB2_HDR_BODY + 56);
106 SSVAL(fixed, 46, name_utf16_len);
108 blob = data_blob_null;
111 status = smb2_create_blob_push(state, &blob, *blobs);
112 if (tevent_req_nterror(req, status)) {
113 return tevent_req_post(req, ev);
117 blobs_offset = name_utf16_len;
118 blobs_offset = ((blobs_offset + 3) & ~3);
120 if (blob.length > 0) {
121 SIVAL(fixed, 48, blobs_offset + SMB2_HDR_BODY + 56);
122 SIVAL(fixed, 52, blob.length);
125 dyn_len = MAX(1, blobs_offset + blob.length);
126 dyn = talloc_zero_array(state, uint8_t, dyn_len);
127 if (tevent_req_nomem(dyn, req)) {
128 return tevent_req_post(req, ev);
132 memcpy(dyn, name_utf16, name_utf16_len);
133 TALLOC_FREE(name_utf16);
136 if (blob.data != NULL) {
137 memcpy(dyn + blobs_offset,
138 blob.data, blob.length);
139 data_blob_free(&blob);
142 subreq = smb2cli_req_send(state, ev, conn, SMB2_OP_CREATE,
148 state->fixed, sizeof(state->fixed),
150 if (tevent_req_nomem(subreq, req)) {
151 return tevent_req_post(req, ev);
153 tevent_req_set_callback(subreq, smb2cli_create_done, req);
157 static void smb2cli_create_done(struct tevent_req *subreq)
159 struct tevent_req *req =
160 tevent_req_callback_data(subreq,
162 struct smb2cli_create_state *state =
164 struct smb2cli_create_state);
168 uint32_t offset, length;
169 static const struct smb2cli_req_expected_response expected[] = {
171 .status = NT_STATUS_OK,
176 status = smb2cli_req_recv(subreq, state, &iov,
177 expected, ARRAY_SIZE(expected));
178 if (tevent_req_nterror(req, status)) {
182 body = (uint8_t *)iov[1].iov_base;
184 state->oplock_level = CVAL(body, 2);
185 state->create_action = IVAL(body, 4);
186 state->creation_time = BVAL(body, 8);
187 state->last_access_time = BVAL(body, 16);
188 state->last_write_time = BVAL(body, 24);
189 state->change_time = BVAL(body, 32);
190 state->allocation_size = BVAL(body, 40);
191 state->end_of_file = BVAL(body, 48);
192 state->file_attributes = IVAL(body, 56);
193 state->fid_persistent = BVAL(body, 64);
194 state->fid_volatile = BVAL(body, 72);
196 offset = IVAL(body, 80);
197 length = IVAL(body, 84);
199 if ((offset != 0) && (length != 0)) {
200 if ((offset != SMB2_HDR_BODY + 88) ||
201 (length > iov[2].iov_len)) {
203 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
206 status = smb2_create_blob_parse(
207 state, data_blob_const(iov[2].iov_base, length),
209 if (tevent_req_nterror(req, status)) {
213 tevent_req_done(req);
216 NTSTATUS smb2cli_create_recv(struct tevent_req *req,
217 uint64_t *fid_persistent,
218 uint64_t *fid_volatile)
220 struct smb2cli_create_state *state =
222 struct smb2cli_create_state);
225 if (tevent_req_is_nterror(req, &status)) {
228 *fid_persistent = state->fid_persistent;
229 *fid_volatile = state->fid_volatile;
233 NTSTATUS smb2cli_create(struct smbXcli_conn *conn,
234 uint32_t timeout_msec,
235 struct smbXcli_session *session,
237 const char *filename,
238 uint8_t oplock_level, /* SMB2_OPLOCK_LEVEL_* */
239 uint32_t impersonation_level, /* SMB2_IMPERSONATION_* */
240 uint32_t desired_access,
241 uint32_t file_attributes,
242 uint32_t share_access,
243 uint32_t create_disposition,
244 uint32_t create_options,
245 struct smb2_create_blobs *blobs,
246 uint64_t *fid_persistent,
247 uint64_t *fid_volatile)
249 TALLOC_CTX *frame = talloc_stackframe();
250 struct tevent_context *ev;
251 struct tevent_req *req;
252 NTSTATUS status = NT_STATUS_NO_MEMORY;
254 if (smbXcli_conn_has_async_calls(conn)) {
256 * Can't use sync call while an async call is in flight
258 status = NT_STATUS_INVALID_PARAMETER;
261 ev = tevent_context_init(frame);
265 req = smb2cli_create_send(frame, ev, conn, timeout_msec, session,
266 tcon_id, filename, oplock_level,
267 impersonation_level, desired_access,
268 file_attributes, share_access,
269 create_disposition, create_options,
274 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
277 status = smb2cli_create_recv(req, fid_persistent, fid_volatile);