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
{
31 uint32_t create_action
;
33 NTTIME last_access_time
;
34 NTTIME last_write_time
;
36 uint64_t allocation_size
;
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(
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
,
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
;
67 size_t name_utf16_len
;
74 req
= tevent_req_create(mem_ctx
, &state
,
75 struct smb2cli_create_state
);
80 if (!convert_string_talloc(state
, CH_UNIX
, CH_UTF16
,
81 filename
, strlen(filename
),
82 &name_utf16
, &name_utf16_len
)) {
84 return tevent_req_post(req
, ev
);
87 if (strlen(filename
) == 0) {
88 TALLOC_FREE(name_utf16
);
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
;
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
);
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
,
145 state
->fixed
, sizeof(state
->fixed
),
147 if (tevent_req_nomem(subreq
, req
)) {
148 return tevent_req_post(req
, ev
);
150 tevent_req_set_callback(subreq
, smb2cli_create_done
, req
);
154 static void smb2cli_create_done(struct tevent_req
*subreq
)
156 struct tevent_req
*req
=
157 tevent_req_callback_data(subreq
,
159 struct smb2cli_create_state
*state
=
161 struct smb2cli_create_state
);
165 uint32_t offset
, length
;
166 static const struct smb2cli_req_expected_response expected
[] = {
168 .status
= NT_STATUS_OK
,
173 status
= smb2cli_req_recv(subreq
, state
, &iov
,
174 expected
, ARRAY_SIZE(expected
));
176 if (tevent_req_nterror(req
, status
)) {
180 body
= (uint8_t *)iov
[1].iov_base
;
182 state
->oplock_level
= CVAL(body
, 2);
183 state
->create_action
= IVAL(body
, 4);
184 state
->creation_time
= BVAL(body
, 8);
185 state
->last_access_time
= BVAL(body
, 16);
186 state
->last_write_time
= BVAL(body
, 24);
187 state
->change_time
= BVAL(body
, 32);
188 state
->allocation_size
= BVAL(body
, 40);
189 state
->end_of_file
= BVAL(body
, 48);
190 state
->file_attributes
= IVAL(body
, 56);
191 state
->fid_persistent
= BVAL(body
, 64);
192 state
->fid_volatile
= BVAL(body
, 72);
194 offset
= IVAL(body
, 80);
195 length
= IVAL(body
, 84);
197 if ((offset
!= 0) && (length
!= 0)) {
198 if ((offset
!= SMB2_HDR_BODY
+ 88) ||
199 (length
> iov
[2].iov_len
)) {
201 req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
204 status
= smb2_create_blob_parse(
205 state
, data_blob_const(iov
[2].iov_base
, length
),
207 if (tevent_req_nterror(req
, status
)) {
211 tevent_req_done(req
);
214 NTSTATUS
smb2cli_create_recv(struct tevent_req
*req
,
215 uint64_t *fid_persistent
,
216 uint64_t *fid_volatile
)
218 struct smb2cli_create_state
*state
=
220 struct smb2cli_create_state
);
223 if (tevent_req_is_nterror(req
, &status
)) {
226 *fid_persistent
= state
->fid_persistent
;
227 *fid_volatile
= state
->fid_volatile
;
231 NTSTATUS
smb2cli_create(struct smbXcli_conn
*conn
,
232 uint32_t timeout_msec
,
233 struct smbXcli_session
*session
,
234 struct smbXcli_tcon
*tcon
,
235 const char *filename
,
236 uint8_t oplock_level
, /* SMB2_OPLOCK_LEVEL_* */
237 uint32_t impersonation_level
, /* SMB2_IMPERSONATION_* */
238 uint32_t desired_access
,
239 uint32_t file_attributes
,
240 uint32_t share_access
,
241 uint32_t create_disposition
,
242 uint32_t create_options
,
243 struct smb2_create_blobs
*blobs
,
244 uint64_t *fid_persistent
,
245 uint64_t *fid_volatile
)
247 TALLOC_CTX
*frame
= talloc_stackframe();
248 struct tevent_context
*ev
;
249 struct tevent_req
*req
;
250 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
252 if (smbXcli_conn_has_async_calls(conn
)) {
254 * Can't use sync call while an async call is in flight
256 status
= NT_STATUS_INVALID_PARAMETER
;
259 ev
= samba_tevent_context_init(frame
);
263 req
= smb2cli_create_send(frame
, ev
, conn
, timeout_msec
,
265 filename
, oplock_level
,
266 impersonation_level
, desired_access
,
267 file_attributes
, share_access
,
268 create_disposition
, create_options
,
273 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
276 status
= smb2cli_create_recv(req
, fid_persistent
, fid_volatile
);