s3:smbd: implement SMB2 Write
[ddiss/samba.git] / source3 / smbd / smb2_write.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
6
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.
11
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.
16
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/>.
19 */
20
21 #include "includes.h"
22 #include "smbd/globals.h"
23 #include "../source4/libcli/smb2/smb2_constants.h"
24
25 static NTSTATUS smbd_smb2_write(struct smbd_smb2_request *req,
26                                 uint32_t in_smbpid,
27                                 uint64_t in_file_id_volatile,
28                                 DATA_BLOB in_data,
29                                 uint64_t in_offset,
30                                 uint32_t in_flags,
31                                 uint32_t *out_count);
32
33 NTSTATUS smbd_smb2_request_process_write(struct smbd_smb2_request *req)
34 {
35         const uint8_t *inhdr;
36         const uint8_t *inbody;
37         int i = req->current_idx;
38         uint8_t *outhdr;
39         DATA_BLOB outbody;
40         DATA_BLOB outdyn;
41         size_t expected_body_size = 0x31;
42         size_t body_size;
43         uint32_t in_smbpid;
44         uint16_t in_data_offset;
45         uint32_t in_data_length;
46         DATA_BLOB in_data_buffer;
47         uint64_t in_offset;
48         uint64_t in_file_id_persistent;
49         uint64_t in_file_id_volatile;
50         uint32_t in_flags;
51         uint32_t out_count;
52         NTSTATUS status;
53
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);
57         }
58
59         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
60
61         body_size = SVAL(inbody, 0x00);
62         if (body_size != expected_body_size) {
63                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
64         }
65
66         in_smbpid = IVAL(inhdr, SMB2_HDR_PID);
67
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);
74
75         if (in_data_offset != (SMB2_HDR_BODY + (body_size & 0xFFFFFFFE))) {
76                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
77         }
78
79         if (in_data_length > req->in.vector[i+2].iov_len) {
80                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
81         }
82
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);
88         }
89
90         in_data_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
91         in_data_buffer.length = in_data_length;
92
93         if (in_file_id_persistent != 0) {
94                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
95         }
96
97         status = smbd_smb2_write(req,
98                                  in_smbpid,
99                                  in_file_id_volatile,
100                                  in_data_buffer,
101                                  in_offset,
102                                  in_flags,
103                                  &out_count);
104         if (!NT_STATUS_IS_OK(status)) {
105                 return smbd_smb2_request_error(req, status);
106         }
107
108         outhdr = (uint8_t *)req->out.vector[i].iov_base;
109
110         outbody = data_blob_talloc(req->out.vector, NULL, 0x10);
111         if (outbody.data == NULL) {
112                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
113         }
114
115         SSVAL(outbody.data, 0x00, 0x10 + 1);    /* struct size */
116         SSVAL(outbody.data, 0x02, 0);           /* reserved */
117         SIVAL(outbody.data, 0x04, out_count);   /* count */
118         SIVAL(outbody.data, 0x08, 0);           /* remaining */
119         SSVAL(outbody.data, 0x0C, 0);           /* write channel info offset */
120         SSVAL(outbody.data, 0x0E, 0);           /* write channel info length */
121
122         outdyn = data_blob_const(NULL, 0);
123
124         return smbd_smb2_request_done(req, outbody, &outdyn);
125 }
126
127 static NTSTATUS smbd_smb2_write(struct smbd_smb2_request *req,
128                                 uint32_t in_smbpid,
129                                 uint64_t in_file_id_volatile,
130                                 DATA_BLOB in_data,
131                                 uint64_t in_offset,
132                                 uint32_t in_flags,
133                                 uint32_t *out_count)
134 {
135         NTSTATUS status;
136         struct smb_request *smbreq;
137         connection_struct *conn = req->tcon->compat_conn;
138         files_struct *fsp;
139         ssize_t nwritten;
140         bool write_through = false;
141         struct lock_struct lock;
142
143         DEBUG(10,("smbd_smb2_write: file_id[0x%016llX]\n",
144                   (unsigned long long)in_file_id_volatile));
145
146         smbreq = smbd_smb2_fake_smb_request(req);
147         if (smbreq == NULL) {
148                 return NT_STATUS_NO_MEMORY;
149         }
150
151         /* If it's an IPC, pass off the pipe handler. */
152         if (IS_IPC(conn)) {
153                 return NT_STATUS_NOT_IMPLEMENTED;
154         }
155
156         fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
157         if (fsp == NULL) {
158                 return NT_STATUS_FILE_CLOSED;
159         }
160         if (conn != fsp->conn) {
161                 return NT_STATUS_FILE_CLOSED;
162         }
163         if (req->session->vuid != fsp->vuid) {
164                 return NT_STATUS_FILE_CLOSED;
165         }
166
167         if (!CHECK_WRITE(fsp)) {
168                 return NT_STATUS_ACCESS_DENIED;
169         }
170
171         init_strict_lock_struct(fsp,
172                                 in_smbpid,
173                                 in_offset,
174                                 in_data.length,
175                                 WRITE_LOCK,
176                                 &lock);
177
178         if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
179                 return NT_STATUS_FILE_LOCK_CONFLICT;
180         }
181
182         nwritten = write_file(smbreq, fsp,
183                               (const char *)in_data.data,
184                               in_offset,
185                               in_data.length);
186
187         if (((nwritten == 0) && (in_data.length != 0)) || (nwritten < 0)) {
188                 DEBUG(5,("smbd_smb2_write: write_file[%s] disk full\n",
189                         fsp->fsp_name));
190                 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
191                 return NT_STATUS_DISK_FULL;
192         }
193
194         DEBUG(3,("smbd_smb2_write: fnum=[%d/%s] length=%d offset=%d wrote=%d\n",
195                 fsp->fnum, fsp->fsp_name, (int)in_data.length,
196                 (int)in_offset, (int)nwritten));
197
198         if (in_flags & 0x00000001) {
199                 write_through = true;
200         }
201
202         status = sync_file(conn, fsp, write_through);
203         if (!NT_STATUS_IS_OK(status)) {
204                 DEBUG(5,("smbd_smb2_write: sync_file for %s returned %s\n",
205                         fsp->fsp_name, nt_errstr(status)));
206                 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
207                 return status;
208         }
209
210         SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
211
212         return NT_STATUS_OK;
213 }