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