s3:smbd: add missing return statements to the SMB2 write error cases
[Samba.git] / source3 / smbd / smb2_write.c
blob1f19a66a4636a7363be737df4243fc1718ab1d31
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 "../source4/libcli/smb2/smb2_constants.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 (in_file_id_persistent != 0) {
94 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
97 subreq = smbd_smb2_write_send(req,
98 req->conn->smb2.event_ctx,
99 req,
100 in_smbpid,
101 in_file_id_volatile,
102 in_data_buffer,
103 in_offset,
104 in_flags);
105 if (subreq == NULL) {
106 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
108 tevent_req_set_callback(subreq, smbd_smb2_request_write_done, req);
109 return NT_STATUS_OK;
112 static void smbd_smb2_request_write_done(struct tevent_req *subreq)
114 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
115 struct smbd_smb2_request);
116 int i = req->current_idx;
117 uint8_t *outhdr;
118 DATA_BLOB outbody;
119 DATA_BLOB outdyn;
120 uint32_t out_count;
121 NTSTATUS status;
122 NTSTATUS error; /* transport error */
124 status = smbd_smb2_write_recv(subreq, &out_count);
125 TALLOC_FREE(subreq);
126 if (!NT_STATUS_IS_OK(status)) {
127 error = smbd_smb2_request_error(req, status);
128 if (!NT_STATUS_IS_OK(error)) {
129 smbd_server_connection_terminate(req->conn,
130 nt_errstr(error));
131 return;
133 return;
136 outhdr = (uint8_t *)req->out.vector[i].iov_base;
138 outbody = data_blob_talloc(req->out.vector, NULL, 0x10);
139 if (outbody.data == NULL) {
140 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
141 if (!NT_STATUS_IS_OK(error)) {
142 smbd_server_connection_terminate(req->conn,
143 nt_errstr(error));
144 return;
146 return;
149 SSVAL(outbody.data, 0x00, 0x10 + 1); /* struct size */
150 SSVAL(outbody.data, 0x02, 0); /* reserved */
151 SIVAL(outbody.data, 0x04, out_count); /* count */
152 SIVAL(outbody.data, 0x08, 0); /* remaining */
153 SSVAL(outbody.data, 0x0C, 0); /* write channel info offset */
154 SSVAL(outbody.data, 0x0E, 0); /* write channel info length */
156 outdyn = data_blob_const(NULL, 0);
158 error = smbd_smb2_request_done(req, outbody, &outdyn);
159 if (!NT_STATUS_IS_OK(error)) {
160 smbd_server_connection_terminate(req->conn, nt_errstr(error));
161 return;
165 struct smbd_smb2_write_state {
166 struct smbd_smb2_request *smb2req;
167 uint32_t in_length;
168 uint32_t out_count;
171 static struct tevent_req *smbd_smb2_write_send(TALLOC_CTX *mem_ctx,
172 struct tevent_context *ev,
173 struct smbd_smb2_request *smb2req,
174 uint32_t in_smbpid,
175 uint64_t in_file_id_volatile,
176 DATA_BLOB in_data,
177 uint64_t in_offset,
178 uint32_t in_flags)
180 NTSTATUS status;
181 struct tevent_req *req;
182 struct smbd_smb2_write_state *state;
183 struct smb_request *smbreq;
184 connection_struct *conn = smb2req->tcon->compat_conn;
185 files_struct *fsp;
186 ssize_t nwritten;
187 bool write_through = false;
188 struct lock_struct lock;
190 req = tevent_req_create(mem_ctx, &state,
191 struct smbd_smb2_write_state);
192 if (req == NULL) {
193 return NULL;
195 state->smb2req = smb2req;
196 state->in_length = in_data.length;
197 state->out_count = 0;
199 DEBUG(10,("smbd_smb2_write: file_id[0x%016llX]\n",
200 (unsigned long long)in_file_id_volatile));
202 smbreq = smbd_smb2_fake_smb_request(smb2req);
203 if (tevent_req_nomem(smbreq, req)) {
204 return tevent_req_post(req, ev);
207 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
208 if (fsp == NULL) {
209 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
210 return tevent_req_post(req, ev);
212 if (conn != fsp->conn) {
213 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
214 return tevent_req_post(req, ev);
216 if (smb2req->session->vuid != fsp->vuid) {
217 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
218 return tevent_req_post(req, ev);
221 if (!CHECK_WRITE(fsp)) {
222 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
223 return tevent_req_post(req, ev);
226 if (IS_IPC(smbreq->conn)) {
227 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
228 return tevent_req_post(req, ev);
231 init_strict_lock_struct(fsp,
232 in_smbpid,
233 in_offset,
234 in_data.length,
235 WRITE_LOCK,
236 &lock);
238 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
239 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
240 return tevent_req_post(req, ev);
243 nwritten = write_file(smbreq, fsp,
244 (const char *)in_data.data,
245 in_offset,
246 in_data.length);
248 if (((nwritten == 0) && (in_data.length != 0)) || (nwritten < 0)) {
249 DEBUG(5,("smbd_smb2_write: write_file[%s] disk full\n",
250 fsp->fsp_name));
251 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
252 tevent_req_nterror(req, NT_STATUS_DISK_FULL);
253 return tevent_req_post(req, ev);
256 DEBUG(3,("smbd_smb2_write: fnum=[%d/%s] length=%d offset=%d wrote=%d\n",
257 fsp->fnum, fsp->fsp_name, (int)in_data.length,
258 (int)in_offset, (int)nwritten));
260 if (in_flags & 0x00000001) {
261 write_through = true;
264 status = sync_file(conn, fsp, write_through);
265 if (!NT_STATUS_IS_OK(status)) {
266 DEBUG(5,("smbd_smb2_write: sync_file for %s returned %s\n",
267 fsp->fsp_name, nt_errstr(status)));
268 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
269 tevent_req_nterror(req, status);
270 return tevent_req_post(req, ev);
273 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
275 state->out_count = nwritten;
277 tevent_req_done(req);
278 return tevent_req_post(req, ev);
281 static NTSTATUS smbd_smb2_write_recv(struct tevent_req *req,
282 uint32_t *out_count)
284 NTSTATUS status;
285 struct smbd_smb2_write_state *state = tevent_req_data(req,
286 struct smbd_smb2_write_state);
288 if (tevent_req_is_nterror(req, &status)) {
289 tevent_req_received(req);
290 return status;
293 *out_count = state->out_count;
295 tevent_req_received(req);
296 return NT_STATUS_OK;