s4-cldap: return domainFunctionality from SAM
[Samba/gebeck_regimport.git] / source3 / smbd / smb2_write.c
blobfa209fafc73170c006f1de6a1ee718365ea096a7
1 /*
2 Unix SMB/CIFS implementation.
3 Core SMB2 server
5 Copyright (C) Stefan Metzmacher 2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "smbd/globals.h"
23 #include "../libcli/smb/smb_common.h"
25 static struct tevent_req *smbd_smb2_write_send(TALLOC_CTX *mem_ctx,
26 struct tevent_context *ev,
27 struct smbd_smb2_request *smb2req,
28 uint32_t in_smbpid,
29 uint64_t in_file_id_volatile,
30 DATA_BLOB in_data,
31 uint64_t in_offset,
32 uint32_t in_flags);
33 static NTSTATUS smbd_smb2_write_recv(struct tevent_req *req,
34 uint32_t *out_count);
36 static void smbd_smb2_request_write_done(struct tevent_req *subreq);
37 NTSTATUS smbd_smb2_request_process_write(struct smbd_smb2_request *req)
39 const uint8_t *inhdr;
40 const uint8_t *inbody;
41 int i = req->current_idx;
42 size_t expected_body_size = 0x31;
43 size_t body_size;
44 uint32_t in_smbpid;
45 uint16_t in_data_offset;
46 uint32_t in_data_length;
47 DATA_BLOB in_data_buffer;
48 uint64_t in_offset;
49 uint64_t in_file_id_persistent;
50 uint64_t in_file_id_volatile;
51 uint32_t in_flags;
52 struct tevent_req *subreq;
54 inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
55 if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
56 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
59 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
61 body_size = SVAL(inbody, 0x00);
62 if (body_size != expected_body_size) {
63 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
66 in_smbpid = IVAL(inhdr, SMB2_HDR_PID);
68 in_data_offset = SVAL(inbody, 0x02);
69 in_data_length = IVAL(inbody, 0x04);
70 in_offset = BVAL(inbody, 0x08);
71 in_file_id_persistent = BVAL(inbody, 0x10);
72 in_file_id_volatile = BVAL(inbody, 0x18);
73 in_flags = IVAL(inbody, 0x2C);
75 if (in_data_offset != (SMB2_HDR_BODY + (body_size & 0xFFFFFFFE))) {
76 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
79 if (in_data_length > req->in.vector[i+2].iov_len) {
80 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
83 /* check the max write size */
84 if (in_data_length > 0x00010000) {
85 DEBUG(0,("here:%s: 0x%08X: 0x%08X\n",
86 __location__, in_data_length, 0x00010000));
87 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
90 in_data_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
91 in_data_buffer.length = in_data_length;
93 if (req->compat_chain_fsp) {
94 /* skip check */
95 } else if (in_file_id_persistent != 0) {
96 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
99 subreq = smbd_smb2_write_send(req,
100 req->sconn->smb2.event_ctx,
101 req,
102 in_smbpid,
103 in_file_id_volatile,
104 in_data_buffer,
105 in_offset,
106 in_flags);
107 if (subreq == NULL) {
108 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
110 tevent_req_set_callback(subreq, smbd_smb2_request_write_done, req);
112 return smbd_smb2_request_pending_queue(req, subreq);
115 static void smbd_smb2_request_write_done(struct tevent_req *subreq)
117 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
118 struct smbd_smb2_request);
119 int i = req->current_idx;
120 uint8_t *outhdr;
121 DATA_BLOB outbody;
122 DATA_BLOB outdyn;
123 uint32_t out_count = 0;
124 NTSTATUS status;
125 NTSTATUS error; /* transport error */
127 status = smbd_smb2_write_recv(subreq, &out_count);
128 TALLOC_FREE(subreq);
129 if (!NT_STATUS_IS_OK(status)) {
130 error = smbd_smb2_request_error(req, status);
131 if (!NT_STATUS_IS_OK(error)) {
132 smbd_server_connection_terminate(req->sconn,
133 nt_errstr(error));
134 return;
136 return;
139 outhdr = (uint8_t *)req->out.vector[i].iov_base;
141 outbody = data_blob_talloc(req->out.vector, NULL, 0x10);
142 if (outbody.data == NULL) {
143 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
144 if (!NT_STATUS_IS_OK(error)) {
145 smbd_server_connection_terminate(req->sconn,
146 nt_errstr(error));
147 return;
149 return;
152 SSVAL(outbody.data, 0x00, 0x10 + 1); /* struct size */
153 SSVAL(outbody.data, 0x02, 0); /* reserved */
154 SIVAL(outbody.data, 0x04, out_count); /* count */
155 SIVAL(outbody.data, 0x08, 0); /* remaining */
156 SSVAL(outbody.data, 0x0C, 0); /* write channel info offset */
157 SSVAL(outbody.data, 0x0E, 0); /* write channel info length */
159 outdyn = data_blob_const(NULL, 0);
161 error = smbd_smb2_request_done(req, outbody, &outdyn);
162 if (!NT_STATUS_IS_OK(error)) {
163 smbd_server_connection_terminate(req->sconn, nt_errstr(error));
164 return;
168 struct smbd_smb2_write_state {
169 struct smbd_smb2_request *smb2req;
170 uint32_t in_length;
171 uint32_t out_count;
174 static void smbd_smb2_write_pipe_done(struct tevent_req *subreq);
176 static struct tevent_req *smbd_smb2_write_send(TALLOC_CTX *mem_ctx,
177 struct tevent_context *ev,
178 struct smbd_smb2_request *smb2req,
179 uint32_t in_smbpid,
180 uint64_t in_file_id_volatile,
181 DATA_BLOB in_data,
182 uint64_t in_offset,
183 uint32_t in_flags)
185 NTSTATUS status;
186 struct tevent_req *req;
187 struct smbd_smb2_write_state *state;
188 struct smb_request *smbreq;
189 connection_struct *conn = smb2req->tcon->compat_conn;
190 files_struct *fsp;
191 ssize_t nwritten;
192 bool write_through = false;
193 struct lock_struct lock;
195 req = tevent_req_create(mem_ctx, &state,
196 struct smbd_smb2_write_state);
197 if (req == NULL) {
198 return NULL;
200 state->smb2req = smb2req;
201 state->in_length = in_data.length;
202 state->out_count = 0;
204 DEBUG(10,("smbd_smb2_write: file_id[0x%016llX]\n",
205 (unsigned long long)in_file_id_volatile));
207 smbreq = smbd_smb2_fake_smb_request(smb2req);
208 if (tevent_req_nomem(smbreq, req)) {
209 return tevent_req_post(req, ev);
212 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
213 if (fsp == NULL) {
214 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
215 return tevent_req_post(req, ev);
217 if (conn != fsp->conn) {
218 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
219 return tevent_req_post(req, ev);
221 if (smb2req->session->vuid != fsp->vuid) {
222 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
223 return tevent_req_post(req, ev);
226 if (IS_IPC(smbreq->conn)) {
227 struct tevent_req *subreq;
229 if (!fsp_is_np(fsp)) {
230 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
231 return tevent_req_post(req, ev);
234 subreq = np_write_send(state, smbd_event_context(),
235 fsp->fake_file_handle,
236 in_data.data,
237 in_data.length);
238 if (tevent_req_nomem(subreq, req)) {
239 return tevent_req_post(req, ev);
241 tevent_req_set_callback(subreq,
242 smbd_smb2_write_pipe_done,
243 req);
244 return req;
247 if (!CHECK_WRITE(fsp)) {
248 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
249 return tevent_req_post(req, ev);
252 init_strict_lock_struct(fsp,
253 in_smbpid,
254 in_offset,
255 in_data.length,
256 WRITE_LOCK,
257 &lock);
259 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
260 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
261 return tevent_req_post(req, ev);
264 nwritten = write_file(smbreq, fsp,
265 (const char *)in_data.data,
266 in_offset,
267 in_data.length);
269 if (((nwritten == 0) && (in_data.length != 0)) || (nwritten < 0)) {
270 DEBUG(5,("smbd_smb2_write: write_file[%s] disk full\n",
271 fsp_str_dbg(fsp)));
272 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
273 tevent_req_nterror(req, NT_STATUS_DISK_FULL);
274 return tevent_req_post(req, ev);
277 DEBUG(3,("smbd_smb2_write: fnum=[%d/%s] length=%d offset=%d wrote=%d\n",
278 fsp->fnum, fsp_str_dbg(fsp), (int)in_data.length,
279 (int)in_offset, (int)nwritten));
281 if (in_flags & 0x00000001) {
282 write_through = true;
285 status = sync_file(conn, fsp, write_through);
286 if (!NT_STATUS_IS_OK(status)) {
287 DEBUG(5,("smbd_smb2_write: sync_file for %s returned %s\n",
288 fsp_str_dbg(fsp), nt_errstr(status)));
289 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
290 tevent_req_nterror(req, status);
291 return tevent_req_post(req, ev);
294 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
296 state->out_count = nwritten;
298 tevent_req_done(req);
299 return tevent_req_post(req, ev);
302 static void smbd_smb2_write_pipe_done(struct tevent_req *subreq)
304 struct tevent_req *req = tevent_req_callback_data(subreq,
305 struct tevent_req);
306 struct smbd_smb2_write_state *state = tevent_req_data(req,
307 struct smbd_smb2_write_state);
308 NTSTATUS status;
309 ssize_t nwritten = -1;
311 status = np_write_recv(subreq, &nwritten);
312 TALLOC_FREE(subreq);
313 if (!NT_STATUS_IS_OK(status)) {
314 tevent_req_nterror(req, status);
315 return;
318 if ((nwritten == 0 && state->in_length != 0) || (nwritten < 0)) {
319 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
320 return;
323 state->out_count = nwritten;
325 tevent_req_done(req);
328 static NTSTATUS smbd_smb2_write_recv(struct tevent_req *req,
329 uint32_t *out_count)
331 NTSTATUS status;
332 struct smbd_smb2_write_state *state = tevent_req_data(req,
333 struct smbd_smb2_write_state);
335 if (tevent_req_is_nterror(req, &status)) {
336 tevent_req_received(req);
337 return status;
340 *out_count = state->out_count;
342 tevent_req_received(req);
343 return NT_STATUS_OK;