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
;
36 static void smb2cli_create_done(struct tevent_req
*subreq
);
38 struct tevent_req
*smb2cli_create_send(
40 struct tevent_context
*ev
,
41 struct smbXcli_conn
*conn
,
42 uint32_t timeout_msec
,
43 struct smbXcli_session
*session
,
44 struct smbXcli_tcon
*tcon
,
46 uint8_t oplock_level
, /* SMB2_OPLOCK_LEVEL_* */
47 uint32_t impersonation_level
, /* SMB2_IMPERSONATION_* */
48 uint32_t desired_access
,
49 uint32_t file_attributes
,
50 uint32_t share_access
,
51 uint32_t create_disposition
,
52 uint32_t create_options
,
53 struct smb2_create_blobs
*blobs
)
55 struct tevent_req
*req
, *subreq
;
56 struct smb2cli_create_state
*state
;
59 size_t name_utf16_len
;
66 uint32_t additional_flags
= 0;
67 uint32_t clear_flags
= 0;
69 req
= tevent_req_create(mem_ctx
, &state
,
70 struct smb2cli_create_state
);
75 if (!convert_string_talloc(state
, CH_UNIX
, CH_UTF16
,
76 filename
, strlen(filename
),
77 &name_utf16
, &name_utf16_len
)) {
79 return tevent_req_post(req
, ev
);
82 if (strlen(filename
) == 0) {
83 TALLOC_FREE(name_utf16
);
90 SCVAL(fixed
, 3, oplock_level
);
91 SIVAL(fixed
, 4, impersonation_level
);
92 SIVAL(fixed
, 24, desired_access
);
93 SIVAL(fixed
, 28, file_attributes
);
94 SIVAL(fixed
, 32, share_access
);
95 SIVAL(fixed
, 36, create_disposition
);
96 SIVAL(fixed
, 40, create_options
);
98 SSVAL(fixed
, 44, SMB2_HDR_BODY
+ 56);
99 SSVAL(fixed
, 46, name_utf16_len
);
101 blob
= data_blob_null
;
104 status
= smb2_create_blob_push(state
, &blob
, *blobs
);
105 if (tevent_req_nterror(req
, status
)) {
106 return tevent_req_post(req
, ev
);
110 blobs_offset
= name_utf16_len
;
111 blobs_offset
= ((blobs_offset
+ 3) & ~3);
113 if (blob
.length
> 0) {
114 SIVAL(fixed
, 48, blobs_offset
+ SMB2_HDR_BODY
+ 56);
115 SIVAL(fixed
, 52, blob
.length
);
118 dyn_len
= MAX(1, blobs_offset
+ blob
.length
);
119 dyn
= talloc_zero_array(state
, uint8_t, dyn_len
);
120 if (tevent_req_nomem(dyn
, req
)) {
121 return tevent_req_post(req
, ev
);
125 memcpy(dyn
, name_utf16
, name_utf16_len
);
126 TALLOC_FREE(name_utf16
);
129 if (blob
.data
!= NULL
) {
130 memcpy(dyn
+ blobs_offset
,
131 blob
.data
, blob
.length
);
132 data_blob_free(&blob
);
135 if (smbXcli_conn_dfs_supported(conn
) &&
136 smbXcli_tcon_is_dfs_share(tcon
))
138 additional_flags
|= SMB2_HDR_FLAG_DFS
;
142 * We use max_dyn_len = 0
143 * as we don't explicitly ask for any output length.
145 * But it's still possible for the server to return
146 * large create blobs.
150 subreq
= smb2cli_req_send(state
, ev
, conn
, SMB2_OP_CREATE
,
151 additional_flags
, clear_flags
,
155 state
->fixed
, sizeof(state
->fixed
),
158 if (tevent_req_nomem(subreq
, req
)) {
159 return tevent_req_post(req
, ev
);
161 tevent_req_set_callback(subreq
, smb2cli_create_done
, req
);
165 static void smb2cli_create_done(struct tevent_req
*subreq
)
167 struct tevent_req
*req
=
168 tevent_req_callback_data(subreq
,
170 struct smb2cli_create_state
*state
=
172 struct smb2cli_create_state
);
176 uint32_t offset
, length
;
177 static const struct smb2cli_req_expected_response expected
[] = {
179 .status
= NT_STATUS_OK
,
184 status
= smb2cli_req_recv(subreq
, state
, &iov
,
185 expected
, ARRAY_SIZE(expected
));
187 if (tevent_req_nterror(req
, status
)) {
191 body
= (uint8_t *)iov
[1].iov_base
;
193 state
->cr
.oplock_level
= CVAL(body
, 2);
194 state
->cr
.create_action
= IVAL(body
, 4);
195 state
->cr
.creation_time
= BVAL(body
, 8);
196 state
->cr
.last_access_time
= BVAL(body
, 16);
197 state
->cr
.last_write_time
= BVAL(body
, 24);
198 state
->cr
.change_time
= BVAL(body
, 32);
199 state
->cr
.allocation_size
= BVAL(body
, 40);
200 state
->cr
.end_of_file
= BVAL(body
, 48);
201 state
->cr
.file_attributes
= IVAL(body
, 56);
202 state
->fid_persistent
= BVAL(body
, 64);
203 state
->fid_volatile
= BVAL(body
, 72);
205 offset
= IVAL(body
, 80);
206 length
= IVAL(body
, 84);
208 if ((offset
!= 0) && (length
!= 0)) {
209 if ((offset
!= SMB2_HDR_BODY
+ 88) ||
210 (length
> iov
[2].iov_len
)) {
212 req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
215 status
= smb2_create_blob_parse(
216 state
, data_blob_const(iov
[2].iov_base
, length
),
218 if (tevent_req_nterror(req
, status
)) {
222 tevent_req_done(req
);
225 NTSTATUS
smb2cli_create_recv(struct tevent_req
*req
,
226 uint64_t *fid_persistent
,
227 uint64_t *fid_volatile
,
228 struct smb_create_returns
*cr
)
230 struct smb2cli_create_state
*state
=
232 struct smb2cli_create_state
);
235 if (tevent_req_is_nterror(req
, &status
)) {
238 *fid_persistent
= state
->fid_persistent
;
239 *fid_volatile
= state
->fid_volatile
;
246 NTSTATUS
smb2cli_create(struct smbXcli_conn
*conn
,
247 uint32_t timeout_msec
,
248 struct smbXcli_session
*session
,
249 struct smbXcli_tcon
*tcon
,
250 const char *filename
,
251 uint8_t oplock_level
, /* SMB2_OPLOCK_LEVEL_* */
252 uint32_t impersonation_level
, /* SMB2_IMPERSONATION_* */
253 uint32_t desired_access
,
254 uint32_t file_attributes
,
255 uint32_t share_access
,
256 uint32_t create_disposition
,
257 uint32_t create_options
,
258 struct smb2_create_blobs
*blobs
,
259 uint64_t *fid_persistent
,
260 uint64_t *fid_volatile
,
261 struct smb_create_returns
*cr
)
263 TALLOC_CTX
*frame
= talloc_stackframe();
264 struct tevent_context
*ev
;
265 struct tevent_req
*req
;
266 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
268 if (smbXcli_conn_has_async_calls(conn
)) {
270 * Can't use sync call while an async call is in flight
272 status
= NT_STATUS_INVALID_PARAMETER
;
275 ev
= samba_tevent_context_init(frame
);
279 req
= smb2cli_create_send(frame
, ev
, conn
, timeout_msec
,
281 filename
, oplock_level
,
282 impersonation_level
, desired_access
,
283 file_attributes
, share_access
,
284 create_disposition
, create_options
,
289 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
292 status
= smb2cli_create_recv(req
, fid_persistent
, fid_volatile
, cr
);