Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 : smb2 lib
4 : Copyright (C) Volker Lendecke 2011
5 :
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.
10 :
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.
15 :
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/>.
18 : */
19 :
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"
26 :
27 : struct smb2cli_create_state {
28 : uint8_t fixed[56];
29 :
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;
35 : };
36 :
37 : static void smb2cli_create_done(struct tevent_req *subreq);
38 : static bool smb2cli_create_cancel(struct tevent_req *req);
39 :
40 58959 : struct tevent_req *smb2cli_create_send(
41 : TALLOC_CTX *mem_ctx,
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,
47 : const char *filename,
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)
56 : {
57 : struct tevent_req *req, *subreq;
58 : struct smb2cli_create_state *state;
59 : uint8_t *fixed;
60 : uint8_t *name_utf16;
61 : size_t name_utf16_len;
62 : DATA_BLOB blob;
63 : NTSTATUS status;
64 : size_t blobs_offset;
65 : uint8_t *dyn;
66 : size_t dyn_len;
67 : size_t max_dyn_len;
68 58959 : uint32_t additional_flags = 0;
69 58959 : uint32_t clear_flags = 0;
70 :
71 58959 : req = tevent_req_create(mem_ctx, &state,
72 : struct smb2cli_create_state);
73 58959 : if (req == NULL) {
74 0 : return NULL;
75 : }
76 :
77 58959 : if (!convert_string_talloc(state, CH_UNIX, CH_UTF16,
78 : filename, strlen(filename),
79 : &name_utf16, &name_utf16_len)) {
80 0 : tevent_req_oom(req);
81 0 : return tevent_req_post(req, ev);
82 : }
83 :
84 58959 : if (strlen(filename) == 0) {
85 4173 : TALLOC_FREE(name_utf16);
86 4173 : name_utf16_len = 0;
87 : }
88 :
89 58959 : fixed = state->fixed;
90 :
91 58959 : SSVAL(fixed, 0, 57);
92 58959 : SCVAL(fixed, 3, oplock_level);
93 58959 : SIVAL(fixed, 4, impersonation_level);
94 58959 : SIVAL(fixed, 24, desired_access);
95 58959 : SIVAL(fixed, 28, file_attributes);
96 58959 : SIVAL(fixed, 32, share_access);
97 58959 : SIVAL(fixed, 36, create_disposition);
98 58959 : SIVAL(fixed, 40, create_options);
99 :
100 58959 : SSVAL(fixed, 44, SMB2_HDR_BODY + 56);
101 58959 : SSVAL(fixed, 46, name_utf16_len);
102 :
103 58959 : blob = data_blob_null;
104 :
105 58959 : if (blobs != NULL) {
106 41677 : status = smb2_create_blob_push(state, &blob, *blobs);
107 41677 : if (tevent_req_nterror(req, status)) {
108 0 : return tevent_req_post(req, ev);
109 : }
110 : }
111 :
112 58959 : blobs_offset = name_utf16_len;
113 58959 : blobs_offset = ((blobs_offset + 3) & ~3);
114 :
115 58959 : if (blob.length > 0) {
116 2322 : blobs_offset = ((blobs_offset + 7) & ~7);
117 2322 : SIVAL(fixed, 48, blobs_offset + SMB2_HDR_BODY + 56);
118 2322 : SIVAL(fixed, 52, blob.length);
119 : }
120 :
121 58959 : dyn_len = MAX(1, blobs_offset + blob.length);
122 58959 : dyn = talloc_zero_array(state, uint8_t, dyn_len);
123 58959 : if (tevent_req_nomem(dyn, req)) {
124 0 : return tevent_req_post(req, ev);
125 : }
126 :
127 58959 : if (name_utf16) {
128 55412 : memcpy(dyn, name_utf16, name_utf16_len);
129 54786 : TALLOC_FREE(name_utf16);
130 : }
131 :
132 58959 : if (blob.data != NULL) {
133 4644 : memcpy(dyn + blobs_offset,
134 2322 : blob.data, blob.length);
135 2322 : data_blob_free(&blob);
136 : }
137 :
138 114503 : if (smbXcli_conn_dfs_supported(conn) &&
139 55544 : smbXcli_tcon_is_dfs_share(tcon))
140 : {
141 8654 : additional_flags |= SMB2_HDR_FLAG_DFS;
142 : }
143 :
144 : /*
145 : * We use max_dyn_len = 0
146 : * as we don't explicitly ask for any output length.
147 : *
148 : * But it's still possible for the server to return
149 : * large create blobs.
150 : */
151 58959 : max_dyn_len = 0;
152 :
153 111863 : subreq = smb2cli_req_send(state, ev, conn, SMB2_OP_CREATE,
154 : additional_flags, clear_flags,
155 : timeout_msec,
156 : tcon,
157 : session,
158 58959 : state->fixed, sizeof(state->fixed),
159 : dyn, dyn_len,
160 : max_dyn_len);
161 58959 : if (tevent_req_nomem(subreq, req)) {
162 0 : return tevent_req_post(req, ev);
163 : }
164 58959 : tevent_req_set_callback(subreq, smb2cli_create_done, req);
165 :
166 58959 : state->subreq = subreq;
167 58959 : tevent_req_set_cancel_fn(req, smb2cli_create_cancel);
168 :
169 58959 : return req;
170 : }
171 :
172 2 : static bool smb2cli_create_cancel(struct tevent_req *req)
173 : {
174 2 : struct smb2cli_create_state *state = tevent_req_data(req,
175 : struct smb2cli_create_state);
176 2 : return tevent_req_cancel(state->subreq);
177 : }
178 :
179 58959 : static void smb2cli_create_done(struct tevent_req *subreq)
180 : {
181 53530 : struct tevent_req *req =
182 58959 : tevent_req_callback_data(subreq,
183 : struct tevent_req);
184 53530 : struct smb2cli_create_state *state =
185 58959 : tevent_req_data(req,
186 : struct smb2cli_create_state);
187 : NTSTATUS status;
188 : struct iovec *iov;
189 : uint8_t *body;
190 : uint32_t offset, length;
191 : static const struct smb2cli_req_expected_response expected[] = {
192 : {
193 : .status = NT_STATUS_OK,
194 : .body_size = 0x59
195 : }
196 : };
197 :
198 58959 : status = smb2cli_req_recv(subreq, state, &iov,
199 : expected, ARRAY_SIZE(expected));
200 58959 : TALLOC_FREE(subreq);
201 58959 : if (tevent_req_nterror(req, status)) {
202 9848 : return;
203 : }
204 :
205 53875 : body = (uint8_t *)iov[1].iov_base;
206 :
207 53875 : state->cr.oplock_level = CVAL(body, 2);
208 53875 : state->cr.create_action = IVAL(body, 4);
209 53875 : state->cr.creation_time = BVAL(body, 8);
210 53875 : state->cr.last_access_time = BVAL(body, 16);
211 53875 : state->cr.last_write_time = BVAL(body, 24);
212 53875 : state->cr.change_time = BVAL(body, 32);
213 53875 : state->cr.allocation_size = BVAL(body, 40);
214 53875 : state->cr.end_of_file = BVAL(body, 48);
215 53875 : state->cr.file_attributes = IVAL(body, 56);
216 53875 : state->fid_persistent = BVAL(body, 64);
217 53875 : state->fid_volatile = BVAL(body, 72);
218 :
219 53875 : offset = IVAL(body, 80);
220 53875 : length = IVAL(body, 84);
221 :
222 53875 : if ((offset != 0) && (length != 0)) {
223 8 : if ((offset != SMB2_HDR_BODY + 88) ||
224 4 : (length > iov[2].iov_len)) {
225 0 : tevent_req_nterror(
226 : req, NT_STATUS_INVALID_NETWORK_RESPONSE);
227 0 : return;
228 : }
229 8 : status = smb2_create_blob_parse(
230 4 : state, data_blob_const(iov[2].iov_base, length),
231 : &state->blobs);
232 4 : if (tevent_req_nterror(req, status)) {
233 0 : return;
234 : }
235 : }
236 53875 : tevent_req_done(req);
237 : }
238 :
239 58959 : NTSTATUS smb2cli_create_recv(struct tevent_req *req,
240 : uint64_t *fid_persistent,
241 : uint64_t *fid_volatile,
242 : struct smb_create_returns *cr,
243 : TALLOC_CTX *mem_ctx,
244 : struct smb2_create_blobs *blobs)
245 : {
246 53530 : struct smb2cli_create_state *state =
247 58959 : tevent_req_data(req,
248 : struct smb2cli_create_state);
249 : NTSTATUS status;
250 :
251 58959 : if (tevent_req_is_nterror(req, &status)) {
252 5084 : tevent_req_received(req);
253 5084 : return status;
254 : }
255 53875 : *fid_persistent = state->fid_persistent;
256 53875 : *fid_volatile = state->fid_volatile;
257 53875 : if (cr) {
258 36789 : *cr = state->cr;
259 : }
260 53875 : if (blobs) {
261 36789 : blobs->num_blobs = state->blobs.num_blobs;
262 36789 : blobs->blobs = talloc_move(mem_ctx, &state->blobs.blobs);
263 : }
264 53875 : tevent_req_received(req);
265 53875 : return NT_STATUS_OK;
266 : }
267 :
268 122 : NTSTATUS smb2cli_create(struct smbXcli_conn *conn,
269 : uint32_t timeout_msec,
270 : struct smbXcli_session *session,
271 : struct smbXcli_tcon *tcon,
272 : const char *filename,
273 : uint8_t oplock_level, /* SMB2_OPLOCK_LEVEL_* */
274 : uint32_t impersonation_level, /* SMB2_IMPERSONATION_* */
275 : uint32_t desired_access,
276 : uint32_t file_attributes,
277 : uint32_t share_access,
278 : uint32_t create_disposition,
279 : uint32_t create_options,
280 : struct smb2_create_blobs *blobs,
281 : uint64_t *fid_persistent,
282 : uint64_t *fid_volatile,
283 : struct smb_create_returns *cr,
284 : TALLOC_CTX *mem_ctx,
285 : struct smb2_create_blobs *ret_blobs)
286 : {
287 122 : TALLOC_CTX *frame = talloc_stackframe();
288 : struct tevent_context *ev;
289 : struct tevent_req *req;
290 122 : NTSTATUS status = NT_STATUS_NO_MEMORY;
291 :
292 122 : if (smbXcli_conn_has_async_calls(conn)) {
293 : /*
294 : * Can't use sync call while an async call is in flight
295 : */
296 0 : status = NT_STATUS_INVALID_PARAMETER;
297 0 : goto fail;
298 : }
299 122 : ev = samba_tevent_context_init(frame);
300 122 : if (ev == NULL) {
301 0 : goto fail;
302 : }
303 122 : req = smb2cli_create_send(frame, ev, conn, timeout_msec,
304 : session, tcon,
305 : filename, oplock_level,
306 : impersonation_level, desired_access,
307 : file_attributes, share_access,
308 : create_disposition, create_options,
309 : blobs);
310 122 : if (req == NULL) {
311 0 : goto fail;
312 : }
313 122 : if (!tevent_req_poll_ntstatus(req, ev, &status)) {
314 0 : goto fail;
315 : }
316 122 : status = smb2cli_create_recv(req, fid_persistent, fid_volatile, cr,
317 : mem_ctx, ret_blobs);
318 122 : fail:
319 122 : TALLOC_FREE(frame);
320 122 : return status;
321 : }
|