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