s3:smb2_server: use smbd_smb2_request_verify_sizes() in smb2_setinfo.c
[metze/samba/wip.git] / source3 / smbd / smb2_setinfo.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
6    Copyright (C) Jeremy Allison 2010
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "../libcli/smb/smb_common.h"
26 #include "trans2.h"
27 #include "../lib/util/tevent_ntstatus.h"
28
29 static struct tevent_req *smbd_smb2_setinfo_send(TALLOC_CTX *mem_ctx,
30                                                  struct tevent_context *ev,
31                                                  struct smbd_smb2_request *smb2req,
32                                                  uint8_t in_info_type,
33                                                  uint8_t in_file_info_class,
34                                                  DATA_BLOB in_input_buffer,
35                                                  uint32_t in_additional_information,
36                                                  uint64_t in_file_id_volatile);
37 static NTSTATUS smbd_smb2_setinfo_recv(struct tevent_req *req);
38
39 static void smbd_smb2_request_setinfo_done(struct tevent_req *subreq);
40 NTSTATUS smbd_smb2_request_process_setinfo(struct smbd_smb2_request *req)
41 {
42         NTSTATUS status;
43         const uint8_t *inbody;
44         int i = req->current_idx;
45         uint8_t in_info_type;
46         uint8_t in_file_info_class;
47         uint16_t in_input_buffer_offset;
48         uint32_t in_input_buffer_length;
49         DATA_BLOB in_input_buffer;
50         uint32_t in_additional_information;
51         uint64_t in_file_id_persistent;
52         uint64_t in_file_id_volatile;
53         struct tevent_req *subreq;
54
55         status = smbd_smb2_request_verify_sizes(req, 0x21);
56         if (!NT_STATUS_IS_OK(status)) {
57                 return smbd_smb2_request_error(req, status);
58         }
59         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
60
61         in_info_type                    = CVAL(inbody, 0x02);
62         in_file_info_class              = CVAL(inbody, 0x03);
63         in_input_buffer_length          = IVAL(inbody, 0x04);
64         in_input_buffer_offset          = SVAL(inbody, 0x08);
65         /* 0x0A 2 bytes reserved */
66         in_additional_information       = IVAL(inbody, 0x0C);
67         in_file_id_persistent           = BVAL(inbody, 0x10);
68         in_file_id_volatile             = BVAL(inbody, 0x18);
69
70         if (in_input_buffer_offset == 0 && in_input_buffer_length == 0) {
71                 /* This is ok */
72         } else if (in_input_buffer_offset !=
73                    (SMB2_HDR_BODY + req->in.vector[i+1].iov_len)) {
74                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
75         }
76
77         if (in_input_buffer_length > req->in.vector[i+2].iov_len) {
78                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
79         }
80
81         in_input_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
82         in_input_buffer.length = in_input_buffer_length;
83
84         if (req->compat_chain_fsp) {
85                 /* skip check */
86         } else if (in_file_id_persistent != in_file_id_volatile) {
87                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
88         }
89
90         subreq = smbd_smb2_setinfo_send(req,
91                                         req->sconn->smb2.event_ctx,
92                                         req,
93                                         in_info_type,
94                                         in_file_info_class,
95                                         in_input_buffer,
96                                         in_additional_information,
97                                         in_file_id_volatile);
98         if (subreq == NULL) {
99                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
100         }
101         tevent_req_set_callback(subreq, smbd_smb2_request_setinfo_done, req);
102
103         return smbd_smb2_request_pending_queue(req, subreq);
104 }
105
106 static void smbd_smb2_request_setinfo_done(struct tevent_req *subreq)
107 {
108         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
109                                         struct smbd_smb2_request);
110         DATA_BLOB outbody;
111         NTSTATUS status;
112         NTSTATUS error; /* transport error */
113
114         status = smbd_smb2_setinfo_recv(subreq);
115         TALLOC_FREE(subreq);
116         if (!NT_STATUS_IS_OK(status)) {
117                 error = smbd_smb2_request_error(req, status);
118                 if (!NT_STATUS_IS_OK(error)) {
119                         smbd_server_connection_terminate(req->sconn,
120                                                          nt_errstr(error));
121                         return;
122                 }
123                 return;
124         }
125
126         outbody = data_blob_talloc(req->out.vector, NULL, 0x02);
127         if (outbody.data == NULL) {
128                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
129                 if (!NT_STATUS_IS_OK(error)) {
130                         smbd_server_connection_terminate(req->sconn,
131                                                          nt_errstr(error));
132                         return;
133                 }
134                 return;
135         }
136
137         SSVAL(outbody.data, 0x00, 0x02);        /* struct size */
138
139         error = smbd_smb2_request_done(req, outbody, NULL);
140         if (!NT_STATUS_IS_OK(error)) {
141                 smbd_server_connection_terminate(req->sconn,
142                                                  nt_errstr(error));
143                 return;
144         }
145 }
146
147 struct smbd_smb2_setinfo_state {
148         struct smbd_smb2_request *smb2req;
149 };
150
151 static struct tevent_req *smbd_smb2_setinfo_send(TALLOC_CTX *mem_ctx,
152                                                  struct tevent_context *ev,
153                                                  struct smbd_smb2_request *smb2req,
154                                                  uint8_t in_info_type,
155                                                  uint8_t in_file_info_class,
156                                                  DATA_BLOB in_input_buffer,
157                                                  uint32_t in_additional_information,
158                                                  uint64_t in_file_id_volatile)
159 {
160         struct tevent_req *req = NULL;
161         struct smbd_smb2_setinfo_state *state = NULL;
162         struct smb_request *smbreq = NULL;
163         connection_struct *conn = smb2req->tcon->compat_conn;
164         files_struct *fsp = NULL;
165         NTSTATUS status;
166
167         req = tevent_req_create(mem_ctx, &state,
168                                 struct smbd_smb2_setinfo_state);
169         if (req == NULL) {
170                 return NULL;
171         }
172         state->smb2req = smb2req;
173
174         DEBUG(10,("smbd_smb2_setinfo_send: file_id[0x%016llX]\n",
175                   (unsigned long long)in_file_id_volatile));
176
177         smbreq = smbd_smb2_fake_smb_request(smb2req);
178         if (tevent_req_nomem(smbreq, req)) {
179                 return tevent_req_post(req, ev);
180         }
181
182         fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
183         if (fsp == NULL) {
184                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
185                 return tevent_req_post(req, ev);
186         }
187         if (conn != fsp->conn) {
188                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
189                 return tevent_req_post(req, ev);
190         }
191         if (smb2req->session->vuid != fsp->vuid) {
192                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
193                 return tevent_req_post(req, ev);
194         }
195
196         if (IS_IPC(conn)) {
197                 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
198                 return tevent_req_post(req, ev);
199         }
200
201         switch (in_info_type) {
202         case 0x01:/* SMB2_SETINFO_FILE */
203         {
204                 uint16_t file_info_level;
205                 char *data;
206                 int data_size;
207                 int ret_size = 0;
208
209
210                 file_info_level = in_file_info_class + 1000;
211                 if (file_info_level == SMB_FILE_RENAME_INFORMATION) {
212                         /* SMB2_FILE_RENAME_INFORMATION_INTERNAL == 0xFF00 + in_file_info_class */
213                         file_info_level = SMB2_FILE_RENAME_INFORMATION_INTERNAL;
214                 }
215
216                 if (fsp->fh->fd == -1) {
217                         /*
218                          * This is actually a SETFILEINFO on a directory
219                          * handle (returned from an NT SMB). NT5.0 seems
220                          * to do this call. JRA.
221                          */
222                         if (INFO_LEVEL_IS_UNIX(file_info_level)) {
223                                 /* Always do lstat for UNIX calls. */
224                                 if (SMB_VFS_LSTAT(conn, fsp->fsp_name)) {
225                                         DEBUG(3,("smbd_smb2_setinfo_send: "
226                                                  "SMB_VFS_LSTAT of %s failed "
227                                                  "(%s)\n", fsp_str_dbg(fsp),
228                                                  strerror(errno)));
229                                         status = map_nt_error_from_unix(errno);
230                                         tevent_req_nterror(req, status);
231                                         return tevent_req_post(req, ev);
232                                 }
233                         } else {
234                                 if (SMB_VFS_STAT(conn, fsp->fsp_name) != 0) {
235                                         DEBUG(3,("smbd_smb2_setinfo_send: "
236                                                  "fileinfo of %s failed (%s)\n",
237                                                  fsp_str_dbg(fsp),
238                                                  strerror(errno)));
239                                         status = map_nt_error_from_unix(errno);
240                                         tevent_req_nterror(req, status);
241                                         return tevent_req_post(req, ev);
242                                 }
243                         }
244                 } else if (fsp->print_file) {
245                         /*
246                          * Doing a DELETE_ON_CLOSE should cancel a print job.
247                          */
248                         if ((file_info_level == SMB_SET_FILE_DISPOSITION_INFO)
249                             && in_input_buffer.length >= 1
250                             && CVAL(in_input_buffer.data,0)) {
251                                 fsp->fh->private_options |= NTCREATEX_OPTIONS_PRIVATE_DELETE_ON_CLOSE;
252
253                                 DEBUG(3,("smbd_smb2_setinfo_send: "
254                                          "Cancelling print job (%s)\n",
255                                          fsp_str_dbg(fsp)));
256
257                                 tevent_req_done(req);
258                                 return tevent_req_post(req, ev);
259                         } else {
260                                 tevent_req_nterror(req,
261                                         NT_STATUS_OBJECT_PATH_INVALID);
262                                 return tevent_req_post(req, ev);
263                         }
264                 } else {
265                         /*
266                          * Original code - this is an open file.
267                          */
268
269                         if (SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st) != 0) {
270                                 DEBUG(3,("smbd_smb2_setinfo_send: fstat "
271                                          "of fnum %d failed (%s)\n", fsp->fnum,
272                                          strerror(errno)));
273                                 status = map_nt_error_from_unix(errno);
274                                 tevent_req_nterror(req, status);
275                                 return tevent_req_post(req, ev);
276                         }
277                 }
278
279                 data = NULL;
280                 data_size = in_input_buffer.length;
281                 if (data_size > 0) {
282                         data = (char *)SMB_MALLOC_ARRAY(char, data_size);
283                         if (tevent_req_nomem(data, req)) {
284                                 return tevent_req_post(req, ev);
285                         }
286                         memcpy(data, in_input_buffer.data, data_size);
287                 }
288
289                 status = smbd_do_setfilepathinfo(conn, smbreq, state,
290                                                  file_info_level,
291                                                  fsp,
292                                                  fsp->fsp_name,
293                                                  &data,
294                                                  data_size,
295                                                  &ret_size);
296                 SAFE_FREE(data);
297                 if (!NT_STATUS_IS_OK(status)) {
298                         if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
299                                 status = NT_STATUS_INVALID_INFO_CLASS;
300                         }
301                         tevent_req_nterror(req, status);
302                         return tevent_req_post(req, ev);
303                 }
304                 break;
305         }
306
307         case 0x03:/* SMB2_SETINFO_SECURITY */
308         {
309                 if (!CAN_WRITE(conn)) {
310                         tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
311                         return tevent_req_post(req, ev);
312                 }
313
314                 status = set_sd(fsp,
315                                 in_input_buffer.data,
316                                 in_input_buffer.length,
317                                 in_additional_information);
318                 if (!NT_STATUS_IS_OK(status)) {
319                         tevent_req_nterror(req, status);
320                         return tevent_req_post(req, ev);
321                 }
322                 break;
323         }
324
325         default:
326                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
327                 return tevent_req_post(req, ev);
328         }
329
330         tevent_req_done(req);
331         return tevent_req_post(req, ev);
332 }
333
334 static NTSTATUS smbd_smb2_setinfo_recv(struct tevent_req *req)
335 {
336         NTSTATUS status;
337
338         if (tevent_req_is_nterror(req, &status)) {
339                 tevent_req_received(req);
340                 return status;
341         }
342
343         tevent_req_received(req);
344         return NT_STATUS_OK;
345 }