s3-utils/net_rpc_printer.c: print more info on write error
[Samba/gebeck_regimport.git] / source3 / libsmb / smb2cli_create.c
blob794b1f1bff80437a1161a31397afc0693906b794
1 /*
2 Unix SMB/CIFS implementation.
3 smb2 lib
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/>.
20 #include "includes.h"
21 #include "client.h"
22 #include "async_smb.h"
23 #include "smb2cli_base.h"
24 #include "smb2cli.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 {
30 uint8_t fixed[56];
32 uint8_t oplock_level;
33 uint32_t create_action;
34 struct timespec creation_time;
35 struct timespec last_access_time;
36 struct timespec last_write_time;
37 struct timespec change_time;
38 uint64_t allocation_size;
39 uint64_t end_of_file;
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(
49 TALLOC_CTX *mem_ctx,
50 struct tevent_context *ev,
51 struct cli_state *cli,
52 const char *filename,
53 uint8_t oplock_level, /* SMB2_OPLOCK_LEVEL_* */
54 uint32_t impersonation_level, /* SMB2_IMPERSONATION_* */
55 uint32_t desired_access,
56 uint32_t file_attributes,
57 uint32_t share_access,
58 uint32_t create_disposition,
59 uint32_t create_options,
60 struct smb2_create_blobs *blobs)
62 struct tevent_req *req, *subreq;
63 struct smb2cli_create_state *state;
64 uint8_t *fixed;
65 uint8_t *name_utf16;
66 size_t name_utf16_len;
67 DATA_BLOB blob;
68 NTSTATUS status;
69 size_t blobs_offset;
70 uint8_t *dyn;
71 size_t dyn_len;
73 req = tevent_req_create(mem_ctx, &state,
74 struct smb2cli_create_state);
75 if (req == NULL) {
76 return NULL;
79 if (!convert_string_talloc(state, CH_UNIX, CH_UTF16,
80 filename, strlen(filename)+1,
81 &name_utf16, &name_utf16_len)) {
82 tevent_req_oom(req);
83 return tevent_req_post(req, ev);
86 fixed = state->fixed;
88 SSVAL(fixed, 0, 57);
89 SCVAL(fixed, 3, oplock_level);
90 SIVAL(fixed, 4, impersonation_level);
91 SIVAL(fixed, 24, desired_access);
92 SIVAL(fixed, 28, file_attributes);
93 SIVAL(fixed, 32, share_access);
94 SIVAL(fixed, 36, create_disposition);
95 SIVAL(fixed, 40, create_options);
97 SSVAL(fixed, 44, SMB2_HDR_BODY + 56);
98 SSVAL(fixed, 46, name_utf16_len);
100 blob = data_blob_null;
102 if (blobs != NULL) {
103 status = smb2_create_blob_push(talloc_tos(), &blob, *blobs);
104 if (tevent_req_nterror(req, status)) {
105 return tevent_req_post(req, ev);
109 blobs_offset = name_utf16_len;
110 blobs_offset = ((blobs_offset + 3) & ~3);
112 SIVAL(fixed, 48, blobs_offset + SMB2_HDR_BODY + 56);
113 SIVAL(fixed, 52, blob.length);
115 dyn_len = blobs_offset + blob.length;
116 dyn = talloc_zero_array(state, uint8_t, dyn_len);
117 if (tevent_req_nomem(dyn, req)) {
118 return tevent_req_post(req, ev);
121 memcpy(dyn, name_utf16, name_utf16_len);
122 TALLOC_FREE(name_utf16);
124 if (blob.data != NULL) {
125 memcpy(dyn + blobs_offset - (SMB2_HDR_BODY + 56),
126 blob.data, blob.length);
127 data_blob_free(&blob);
130 subreq = smb2cli_req_send(state, ev, cli, SMB2_OP_CREATE, 0,
131 state->fixed, sizeof(state->fixed),
132 dyn, dyn_len);
133 if (tevent_req_nomem(subreq, req)) {
134 return tevent_req_post(req, ev);
136 tevent_req_set_callback(subreq, smb2cli_create_done, req);
137 return req;
140 static void smb2cli_create_done(struct tevent_req *subreq)
142 struct tevent_req *req =
143 tevent_req_callback_data(subreq,
144 struct tevent_req);
145 struct smb2cli_create_state *state =
146 tevent_req_data(req,
147 struct smb2cli_create_state);
148 NTSTATUS status;
149 struct iovec *iov;
150 uint8_t *body;
151 uint32_t offset, length;
153 status = smb2cli_req_recv(subreq, talloc_tos(), &iov, 89);
154 if (tevent_req_nterror(req, status)) {
155 return;
158 body = (uint8_t *)iov[1].iov_base;
160 state->oplock_level = CVAL(body, 2);
161 state->create_action = IVAL(body, 4);
162 state->creation_time = interpret_long_date((char *)body + 8);
163 state->last_access_time = interpret_long_date((char *)body + 16);
164 state->last_write_time = interpret_long_date((char *)body + 24);
165 state->change_time = interpret_long_date((char *)body + 32);
166 state->allocation_size = BVAL(body, 40);
167 state->end_of_file = BVAL(body, 48);
168 state->file_attributes = IVAL(body, 56);
169 state->fid_persistent = BVAL(body, 64);
170 state->fid_volatile = BVAL(body, 72);
172 offset = IVAL(body, 80);
173 length = IVAL(body, 84);
175 if ((offset != 0) && (length != 0)) {
176 if ((offset != SMB2_HDR_BODY + 88) ||
177 (length > iov[2].iov_len)) {
178 tevent_req_nterror(
179 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
180 return;
182 status = smb2_create_blob_parse(
183 state, data_blob_const(iov[2].iov_base, length),
184 &state->blobs);
185 if (tevent_req_nterror(req, status)) {
186 return;
189 tevent_req_done(req);
192 NTSTATUS smb2cli_create_recv(struct tevent_req *req,
193 uint64_t *fid_persistent,
194 uint64_t *fid_volatile)
196 struct smb2cli_create_state *state =
197 tevent_req_data(req,
198 struct smb2cli_create_state);
199 NTSTATUS status;
201 if (tevent_req_is_nterror(req, &status)) {
202 return status;
204 *fid_persistent = state->fid_persistent;
205 *fid_volatile = state->fid_volatile;
206 return NT_STATUS_OK;
209 NTSTATUS smb2cli_create(struct cli_state *cli,
210 const char *filename,
211 uint8_t oplock_level, /* SMB2_OPLOCK_LEVEL_* */
212 uint32_t impersonation_level, /* SMB2_IMPERSONATION_* */
213 uint32_t desired_access,
214 uint32_t file_attributes,
215 uint32_t share_access,
216 uint32_t create_disposition,
217 uint32_t create_options,
218 struct smb2_create_blobs *blobs,
219 uint64_t *fid_persistent,
220 uint64_t *fid_volatile)
222 TALLOC_CTX *frame = talloc_stackframe();
223 struct event_context *ev;
224 struct tevent_req *req;
225 NTSTATUS status = NT_STATUS_NO_MEMORY;
227 if (cli_has_async_calls(cli)) {
229 * Can't use sync call while an async call is in flight
231 status = NT_STATUS_INVALID_PARAMETER;
232 goto fail;
234 ev = event_context_init(frame);
235 if (ev == NULL) {
236 goto fail;
238 req = smb2cli_create_send(frame, ev, cli, filename, oplock_level,
239 impersonation_level, desired_access,
240 file_attributes, share_access,
241 create_disposition, create_options,
242 blobs);
243 if (req == NULL) {
244 goto fail;
246 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
247 goto fail;
249 status = smb2cli_create_recv(req, fid_persistent, fid_volatile);
250 fail:
251 TALLOC_FREE(frame);
252 return status;