3:smb2_setinfo: make use of file_fsp_smb2()
[ddiss/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                                                  struct files_struct *in_fsp,
33                                                  uint8_t in_info_type,
34                                                  uint8_t in_file_info_class,
35                                                  DATA_BLOB in_input_buffer,
36                                                  uint32_t in_additional_information);
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 files_struct *in_fsp;
54         struct tevent_req *subreq;
55
56         status = smbd_smb2_request_verify_sizes(req, 0x21);
57         if (!NT_STATUS_IS_OK(status)) {
58                 return smbd_smb2_request_error(req, status);
59         }
60         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
61
62         in_info_type                    = CVAL(inbody, 0x02);
63         in_file_info_class              = CVAL(inbody, 0x03);
64         in_input_buffer_length          = IVAL(inbody, 0x04);
65         in_input_buffer_offset          = SVAL(inbody, 0x08);
66         /* 0x0A 2 bytes reserved */
67         in_additional_information       = IVAL(inbody, 0x0C);
68         in_file_id_persistent           = BVAL(inbody, 0x10);
69         in_file_id_volatile             = BVAL(inbody, 0x18);
70
71         if (in_input_buffer_offset == 0 && in_input_buffer_length == 0) {
72                 /* This is ok */
73         } else if (in_input_buffer_offset !=
74                    (SMB2_HDR_BODY + req->in.vector[i+1].iov_len)) {
75                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
76         }
77
78         if (in_input_buffer_length > req->in.vector[i+2].iov_len) {
79                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
80         }
81
82         in_input_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
83         in_input_buffer.length = in_input_buffer_length;
84
85         if (in_input_buffer.length > req->sconn->smb2.max_trans) {
86                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
87         }
88
89         in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
90         if (in_fsp == NULL) {
91                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
92         }
93
94         subreq = smbd_smb2_setinfo_send(req, req->sconn->smb2.event_ctx,
95                                         req, in_fsp,
96                                         in_info_type,
97                                         in_file_info_class,
98                                         in_input_buffer,
99                                         in_additional_information);
100         if (subreq == NULL) {
101                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
102         }
103         tevent_req_set_callback(subreq, smbd_smb2_request_setinfo_done, req);
104
105         return smbd_smb2_request_pending_queue(req, subreq);
106 }
107
108 static void smbd_smb2_request_setinfo_done(struct tevent_req *subreq)
109 {
110         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
111                                         struct smbd_smb2_request);
112         DATA_BLOB outbody;
113         NTSTATUS status;
114         NTSTATUS error; /* transport error */
115
116         status = smbd_smb2_setinfo_recv(subreq);
117         TALLOC_FREE(subreq);
118         if (!NT_STATUS_IS_OK(status)) {
119                 error = smbd_smb2_request_error(req, status);
120                 if (!NT_STATUS_IS_OK(error)) {
121                         smbd_server_connection_terminate(req->sconn,
122                                                          nt_errstr(error));
123                         return;
124                 }
125                 return;
126         }
127
128         outbody = data_blob_talloc(req->out.vector, NULL, 0x02);
129         if (outbody.data == NULL) {
130                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
131                 if (!NT_STATUS_IS_OK(error)) {
132                         smbd_server_connection_terminate(req->sconn,
133                                                          nt_errstr(error));
134                         return;
135                 }
136                 return;
137         }
138
139         SSVAL(outbody.data, 0x00, 0x02);        /* struct size */
140
141         error = smbd_smb2_request_done(req, outbody, NULL);
142         if (!NT_STATUS_IS_OK(error)) {
143                 smbd_server_connection_terminate(req->sconn,
144                                                  nt_errstr(error));
145                 return;
146         }
147 }
148
149 struct smbd_smb2_setinfo_state {
150         struct smbd_smb2_request *smb2req;
151 };
152
153 static struct tevent_req *smbd_smb2_setinfo_send(TALLOC_CTX *mem_ctx,
154                                                  struct tevent_context *ev,
155                                                  struct smbd_smb2_request *smb2req,
156                                                  struct files_struct *fsp,
157                                                  uint8_t in_info_type,
158                                                  uint8_t in_file_info_class,
159                                                  DATA_BLOB in_input_buffer,
160                                                  uint32_t in_additional_information)
161 {
162         struct tevent_req *req = NULL;
163         struct smbd_smb2_setinfo_state *state = NULL;
164         struct smb_request *smbreq = NULL;
165         connection_struct *conn = smb2req->tcon->compat_conn;
166         NTSTATUS status;
167
168         req = tevent_req_create(mem_ctx, &state,
169                                 struct smbd_smb2_setinfo_state);
170         if (req == NULL) {
171                 return NULL;
172         }
173         state->smb2req = smb2req;
174
175         DEBUG(10,("smbd_smb2_setinfo_send: %s - fnum[%d]\n",
176                   fsp_str_dbg(fsp), fsp->fnum));
177
178         smbreq = smbd_smb2_fake_smb_request(smb2req);
179         if (tevent_req_nomem(smbreq, req)) {
180                 return tevent_req_post(req, ev);
181         }
182
183         if (IS_IPC(conn)) {
184                 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
185                 return tevent_req_post(req, ev);
186         }
187
188         switch (in_info_type) {
189         case 0x01:/* SMB2_SETINFO_FILE */
190         {
191                 uint16_t file_info_level;
192                 char *data;
193                 int data_size;
194                 int ret_size = 0;
195
196
197                 file_info_level = in_file_info_class + 1000;
198                 if (file_info_level == SMB_FILE_RENAME_INFORMATION) {
199                         /* SMB2_FILE_RENAME_INFORMATION_INTERNAL == 0xFF00 + in_file_info_class */
200                         file_info_level = SMB2_FILE_RENAME_INFORMATION_INTERNAL;
201                 }
202
203                 if (fsp->fh->fd == -1) {
204                         /*
205                          * This is actually a SETFILEINFO on a directory
206                          * handle (returned from an NT SMB). NT5.0 seems
207                          * to do this call. JRA.
208                          */
209                         if (INFO_LEVEL_IS_UNIX(file_info_level)) {
210                                 /* Always do lstat for UNIX calls. */
211                                 if (SMB_VFS_LSTAT(conn, fsp->fsp_name)) {
212                                         DEBUG(3,("smbd_smb2_setinfo_send: "
213                                                  "SMB_VFS_LSTAT of %s failed "
214                                                  "(%s)\n", fsp_str_dbg(fsp),
215                                                  strerror(errno)));
216                                         status = map_nt_error_from_unix(errno);
217                                         tevent_req_nterror(req, status);
218                                         return tevent_req_post(req, ev);
219                                 }
220                         } else {
221                                 if (SMB_VFS_STAT(conn, fsp->fsp_name) != 0) {
222                                         DEBUG(3,("smbd_smb2_setinfo_send: "
223                                                  "fileinfo of %s failed (%s)\n",
224                                                  fsp_str_dbg(fsp),
225                                                  strerror(errno)));
226                                         status = map_nt_error_from_unix(errno);
227                                         tevent_req_nterror(req, status);
228                                         return tevent_req_post(req, ev);
229                                 }
230                         }
231                 } else if (fsp->print_file) {
232                         /*
233                          * Doing a DELETE_ON_CLOSE should cancel a print job.
234                          */
235                         if ((file_info_level == SMB_SET_FILE_DISPOSITION_INFO)
236                             && in_input_buffer.length >= 1
237                             && CVAL(in_input_buffer.data,0)) {
238                                 fsp->fh->private_options |= NTCREATEX_OPTIONS_PRIVATE_DELETE_ON_CLOSE;
239
240                                 DEBUG(3,("smbd_smb2_setinfo_send: "
241                                          "Cancelling print job (%s)\n",
242                                          fsp_str_dbg(fsp)));
243
244                                 tevent_req_done(req);
245                                 return tevent_req_post(req, ev);
246                         } else {
247                                 tevent_req_nterror(req,
248                                         NT_STATUS_OBJECT_PATH_INVALID);
249                                 return tevent_req_post(req, ev);
250                         }
251                 } else {
252                         /*
253                          * Original code - this is an open file.
254                          */
255
256                         if (SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st) != 0) {
257                                 DEBUG(3,("smbd_smb2_setinfo_send: fstat "
258                                          "of fnum %d failed (%s)\n", fsp->fnum,
259                                          strerror(errno)));
260                                 status = map_nt_error_from_unix(errno);
261                                 tevent_req_nterror(req, status);
262                                 return tevent_req_post(req, ev);
263                         }
264                 }
265
266                 data = NULL;
267                 data_size = in_input_buffer.length;
268                 if (data_size > 0) {
269                         data = (char *)SMB_MALLOC_ARRAY(char, data_size);
270                         if (tevent_req_nomem(data, req)) {
271                                 return tevent_req_post(req, ev);
272                         }
273                         memcpy(data, in_input_buffer.data, data_size);
274                 }
275
276                 status = smbd_do_setfilepathinfo(conn, smbreq, state,
277                                                  file_info_level,
278                                                  fsp,
279                                                  fsp->fsp_name,
280                                                  &data,
281                                                  data_size,
282                                                  &ret_size);
283                 SAFE_FREE(data);
284                 if (!NT_STATUS_IS_OK(status)) {
285                         if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
286                                 status = NT_STATUS_INVALID_INFO_CLASS;
287                         }
288                         tevent_req_nterror(req, status);
289                         return tevent_req_post(req, ev);
290                 }
291                 break;
292         }
293
294         case 0x03:/* SMB2_SETINFO_SECURITY */
295         {
296                 if (!CAN_WRITE(conn)) {
297                         tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
298                         return tevent_req_post(req, ev);
299                 }
300
301                 status = set_sd(fsp,
302                                 in_input_buffer.data,
303                                 in_input_buffer.length,
304                                 in_additional_information);
305                 if (!NT_STATUS_IS_OK(status)) {
306                         tevent_req_nterror(req, status);
307                         return tevent_req_post(req, ev);
308                 }
309                 break;
310         }
311
312         default:
313                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
314                 return tevent_req_post(req, ev);
315         }
316
317         tevent_req_done(req);
318         return tevent_req_post(req, ev);
319 }
320
321 static NTSTATUS smbd_smb2_setinfo_recv(struct tevent_req *req)
322 {
323         NTSTATUS status;
324
325         if (tevent_req_is_nterror(req, &status)) {
326                 tevent_req_received(req);
327                 return status;
328         }
329
330         tevent_req_received(req);
331         return NT_STATUS_OK;
332 }