s3:smb2_ioctl: Fix Coverity ID 701771 Uninitialized scalar variable
[samba.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 (in_input_buffer.length > req->sconn->smb2.max_trans) {
85                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
86         }
87
88         status = smbd_smb2_request_verify_creditcharge(req,
89                                                 in_input_buffer.length);
90         if (!NT_STATUS_IS_OK(status)) {
91                 return smbd_smb2_request_error(req, status);
92         }
93
94         if (req->compat_chain_fsp) {
95                 /* skip check */
96         } else if (in_file_id_persistent != in_file_id_volatile) {
97                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
98         }
99
100         subreq = smbd_smb2_setinfo_send(req,
101                                         req->sconn->ev_ctx,
102                                         req,
103                                         in_info_type,
104                                         in_file_info_class,
105                                         in_input_buffer,
106                                         in_additional_information,
107                                         in_file_id_volatile);
108         if (subreq == NULL) {
109                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
110         }
111         tevent_req_set_callback(subreq, smbd_smb2_request_setinfo_done, req);
112
113         return smbd_smb2_request_pending_queue(req, subreq, 500);
114 }
115
116 static void smbd_smb2_request_setinfo_done(struct tevent_req *subreq)
117 {
118         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
119                                         struct smbd_smb2_request);
120         DATA_BLOB outbody;
121         NTSTATUS status;
122         NTSTATUS error; /* transport error */
123
124         status = smbd_smb2_setinfo_recv(subreq);
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->sconn,
130                                                          nt_errstr(error));
131                         return;
132                 }
133                 return;
134         }
135
136         outbody = data_blob_talloc(req->out.vector, NULL, 0x02);
137         if (outbody.data == NULL) {
138                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
139                 if (!NT_STATUS_IS_OK(error)) {
140                         smbd_server_connection_terminate(req->sconn,
141                                                          nt_errstr(error));
142                         return;
143                 }
144                 return;
145         }
146
147         SSVAL(outbody.data, 0x00, 0x02);        /* struct size */
148
149         error = smbd_smb2_request_done(req, outbody, NULL);
150         if (!NT_STATUS_IS_OK(error)) {
151                 smbd_server_connection_terminate(req->sconn,
152                                                  nt_errstr(error));
153                 return;
154         }
155 }
156
157 struct smbd_smb2_setinfo_state {
158         struct smbd_smb2_request *smb2req;
159 };
160
161 static struct tevent_req *smbd_smb2_setinfo_send(TALLOC_CTX *mem_ctx,
162                                                  struct tevent_context *ev,
163                                                  struct smbd_smb2_request *smb2req,
164                                                  uint8_t in_info_type,
165                                                  uint8_t in_file_info_class,
166                                                  DATA_BLOB in_input_buffer,
167                                                  uint32_t in_additional_information,
168                                                  uint64_t in_file_id_volatile)
169 {
170         struct tevent_req *req = NULL;
171         struct smbd_smb2_setinfo_state *state = NULL;
172         struct smb_request *smbreq = NULL;
173         connection_struct *conn = smb2req->tcon->compat_conn;
174         files_struct *fsp = NULL;
175         NTSTATUS status;
176
177         req = tevent_req_create(mem_ctx, &state,
178                                 struct smbd_smb2_setinfo_state);
179         if (req == NULL) {
180                 return NULL;
181         }
182         state->smb2req = smb2req;
183
184         DEBUG(10,("smbd_smb2_setinfo_send: file_id[0x%016llX]\n",
185                   (unsigned long long)in_file_id_volatile));
186
187         smbreq = smbd_smb2_fake_smb_request(smb2req);
188         if (tevent_req_nomem(smbreq, req)) {
189                 return tevent_req_post(req, ev);
190         }
191
192         fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
193         if (fsp == NULL) {
194                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
195                 return tevent_req_post(req, ev);
196         }
197         if (conn != fsp->conn) {
198                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
199                 return tevent_req_post(req, ev);
200         }
201         if (smb2req->session->vuid != fsp->vuid) {
202                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
203                 return tevent_req_post(req, ev);
204         }
205
206         if (IS_IPC(conn)) {
207                 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
208                 return tevent_req_post(req, ev);
209         }
210
211         switch (in_info_type) {
212         case 0x01:/* SMB2_SETINFO_FILE */
213         {
214                 uint16_t file_info_level;
215                 char *data;
216                 int data_size;
217                 int ret_size = 0;
218
219
220                 file_info_level = in_file_info_class + 1000;
221                 if (file_info_level == SMB_FILE_RENAME_INFORMATION) {
222                         /* SMB2_FILE_RENAME_INFORMATION_INTERNAL == 0xFF00 + in_file_info_class */
223                         file_info_level = SMB2_FILE_RENAME_INFORMATION_INTERNAL;
224                 }
225
226                 if (fsp->fh->fd == -1) {
227                         /*
228                          * This is actually a SETFILEINFO on a directory
229                          * handle (returned from an NT SMB). NT5.0 seems
230                          * to do this call. JRA.
231                          */
232                         if (INFO_LEVEL_IS_UNIX(file_info_level)) {
233                                 /* Always do lstat for UNIX calls. */
234                                 if (SMB_VFS_LSTAT(conn, fsp->fsp_name)) {
235                                         DEBUG(3,("smbd_smb2_setinfo_send: "
236                                                  "SMB_VFS_LSTAT of %s failed "
237                                                  "(%s)\n", 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                         } else {
244                                 if (SMB_VFS_STAT(conn, fsp->fsp_name) != 0) {
245                                         DEBUG(3,("smbd_smb2_setinfo_send: "
246                                                  "fileinfo of %s failed (%s)\n",
247                                                  fsp_str_dbg(fsp),
248                                                  strerror(errno)));
249                                         status = map_nt_error_from_unix(errno);
250                                         tevent_req_nterror(req, status);
251                                         return tevent_req_post(req, ev);
252                                 }
253                         }
254                 } else if (fsp->print_file) {
255                         /*
256                          * Doing a DELETE_ON_CLOSE should cancel a print job.
257                          */
258                         if ((file_info_level == SMB_SET_FILE_DISPOSITION_INFO)
259                             && in_input_buffer.length >= 1
260                             && CVAL(in_input_buffer.data,0)) {
261                                 fsp->fh->private_options |= NTCREATEX_OPTIONS_PRIVATE_DELETE_ON_CLOSE;
262
263                                 DEBUG(3,("smbd_smb2_setinfo_send: "
264                                          "Cancelling print job (%s)\n",
265                                          fsp_str_dbg(fsp)));
266
267                                 tevent_req_done(req);
268                                 return tevent_req_post(req, ev);
269                         } else {
270                                 tevent_req_nterror(req,
271                                         NT_STATUS_OBJECT_PATH_INVALID);
272                                 return tevent_req_post(req, ev);
273                         }
274                 } else {
275                         /*
276                          * Original code - this is an open file.
277                          */
278
279                         if (SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st) != 0) {
280                                 DEBUG(3,("smbd_smb2_setinfo_send: fstat "
281                                          "of fnum %d failed (%s)\n", fsp->fnum,
282                                          strerror(errno)));
283                                 status = map_nt_error_from_unix(errno);
284                                 tevent_req_nterror(req, status);
285                                 return tevent_req_post(req, ev);
286                         }
287                 }
288
289                 data = NULL;
290                 data_size = in_input_buffer.length;
291                 if (data_size > 0) {
292                         data = (char *)SMB_MALLOC_ARRAY(char, data_size);
293                         if (tevent_req_nomem(data, req)) {
294                                 return tevent_req_post(req, ev);
295                         }
296                         memcpy(data, in_input_buffer.data, data_size);
297                 }
298
299                 status = smbd_do_setfilepathinfo(conn, smbreq, state,
300                                                  file_info_level,
301                                                  fsp,
302                                                  fsp->fsp_name,
303                                                  &data,
304                                                  data_size,
305                                                  &ret_size);
306                 SAFE_FREE(data);
307                 if (!NT_STATUS_IS_OK(status)) {
308                         if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
309                                 status = NT_STATUS_INVALID_INFO_CLASS;
310                         }
311                         tevent_req_nterror(req, status);
312                         return tevent_req_post(req, ev);
313                 }
314                 break;
315         }
316
317         case 0x03:/* SMB2_SETINFO_SECURITY */
318         {
319                 if (!CAN_WRITE(conn)) {
320                         tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
321                         return tevent_req_post(req, ev);
322                 }
323
324                 status = set_sd(fsp,
325                                 in_input_buffer.data,
326                                 in_input_buffer.length,
327                                 in_additional_information);
328                 if (!NT_STATUS_IS_OK(status)) {
329                         tevent_req_nterror(req, status);
330                         return tevent_req_post(req, ev);
331                 }
332                 break;
333         }
334
335         default:
336                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
337                 return tevent_req_post(req, ev);
338         }
339
340         tevent_req_done(req);
341         return tevent_req_post(req, ev);
342 }
343
344 static NTSTATUS smbd_smb2_setinfo_recv(struct tevent_req *req)
345 {
346         NTSTATUS status;
347
348         if (tevent_req_is_nterror(req, &status)) {
349                 tevent_req_received(req);
350                 return status;
351         }
352
353         tevent_req_received(req);
354         return NT_STATUS_OK;
355 }