s3-tevent: only include ../lib/util/tevent wrappers where needed.
[nivanova/samba-autobuild/.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         const uint8_t *inhdr;
43         const uint8_t *inbody;
44         int i = req->current_idx;
45         size_t expected_body_size = 0x21;
46         size_t body_size;
47         uint8_t in_info_type;
48         uint8_t in_file_info_class;
49         uint16_t in_input_buffer_offset;
50         uint32_t in_input_buffer_length;
51         DATA_BLOB in_input_buffer;
52         uint32_t in_additional_information;
53         uint64_t in_file_id_persistent;
54         uint64_t in_file_id_volatile;
55         struct tevent_req *subreq;
56
57         inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
58         if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
59                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
60         }
61
62         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
63
64         body_size = SVAL(inbody, 0x00);
65         if (body_size != expected_body_size) {
66                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
67         }
68
69         in_info_type                    = CVAL(inbody, 0x02);
70         in_file_info_class              = CVAL(inbody, 0x03);
71         in_input_buffer_length          = IVAL(inbody, 0x04);
72         in_input_buffer_offset          = SVAL(inbody, 0x08);
73         /* 0x0A 2 bytes reserved */
74         in_additional_information       = IVAL(inbody, 0x0C);
75         in_file_id_persistent           = BVAL(inbody, 0x10);
76         in_file_id_volatile             = BVAL(inbody, 0x18);
77
78         if (in_input_buffer_offset == 0 && in_input_buffer_length == 0) {
79                 /* This is ok */
80         } else if (in_input_buffer_offset !=
81                    (SMB2_HDR_BODY + (body_size & 0xFFFFFFFE))) {
82                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
83         }
84
85         if (in_input_buffer_length > req->in.vector[i+2].iov_len) {
86                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
87         }
88
89         in_input_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
90         in_input_buffer.length = in_input_buffer_length;
91
92         if (req->compat_chain_fsp) {
93                 /* skip check */
94         } else if (in_file_id_persistent != in_file_id_volatile) {
95                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
96         }
97
98         subreq = smbd_smb2_setinfo_send(req,
99                                         req->sconn->smb2.event_ctx,
100                                         req,
101                                         in_info_type,
102                                         in_file_info_class,
103                                         in_input_buffer,
104                                         in_additional_information,
105                                         in_file_id_volatile);
106         if (subreq == NULL) {
107                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
108         }
109         tevent_req_set_callback(subreq, smbd_smb2_request_setinfo_done, req);
110
111         return smbd_smb2_request_pending_queue(req, subreq);
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->sconn,
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->sconn,
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->sconn,
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 = NULL;
169         struct smbd_smb2_setinfo_state *state = NULL;
170         struct smb_request *smbreq = NULL;
171         connection_struct *conn = smb2req->tcon->compat_conn;
172         files_struct *fsp = NULL;
173         NTSTATUS status;
174
175         req = tevent_req_create(mem_ctx, &state,
176                                 struct smbd_smb2_setinfo_state);
177         if (req == NULL) {
178                 return NULL;
179         }
180         state->smb2req = smb2req;
181
182         DEBUG(10,("smbd_smb2_setinfo_send: file_id[0x%016llX]\n",
183                   (unsigned long long)in_file_id_volatile));
184
185         smbreq = smbd_smb2_fake_smb_request(smb2req);
186         if (tevent_req_nomem(smbreq, req)) {
187                 return tevent_req_post(req, ev);
188         }
189
190         fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
191         if (fsp == NULL) {
192                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
193                 return tevent_req_post(req, ev);
194         }
195         if (conn != fsp->conn) {
196                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
197                 return tevent_req_post(req, ev);
198         }
199         if (smb2req->session->vuid != fsp->vuid) {
200                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
201                 return tevent_req_post(req, ev);
202         }
203
204         if (IS_IPC(conn)) {
205                 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
206                 return tevent_req_post(req, ev);
207         }
208
209         switch (in_info_type) {
210         case 0x01:/* SMB2_SETINFO_FILE */
211         {
212                 uint16_t file_info_level;
213                 char *data;
214                 int data_size;
215                 int ret_size = 0;
216
217
218                 file_info_level = in_file_info_class + 1000;
219                 if (file_info_level == SMB_FILE_RENAME_INFORMATION) {
220                         /* SMB2_FILE_RENAME_INFORMATION_INTERNAL == 0xFF00 + in_file_info_class */
221                         file_info_level = SMB2_FILE_RENAME_INFORMATION_INTERNAL;
222                         if (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK &&
223                             fsp->oplock_type != NO_OPLOCK) {
224                                 /* No break, but error. */
225                                 tevent_req_nterror(req, NT_STATUS_SHARING_VIOLATION);
226                                 return tevent_req_post(req, ev);
227                         }
228                 }
229
230                 if (fsp->fh->fd == -1) {
231                         /*
232                          * This is actually a SETFILEINFO on a directory
233                          * handle (returned from an NT SMB). NT5.0 seems
234                          * to do this call. JRA.
235                          */
236                         if (INFO_LEVEL_IS_UNIX(file_info_level)) {
237                                 /* Always do lstat for UNIX calls. */
238                                 if (SMB_VFS_LSTAT(conn, fsp->fsp_name)) {
239                                         DEBUG(3,("smbd_smb2_setinfo_send: "
240                                                  "SMB_VFS_LSTAT of %s failed "
241                                                  "(%s)\n", fsp_str_dbg(fsp),
242                                                  strerror(errno)));
243                                         status = map_nt_error_from_unix(errno);
244                                         tevent_req_nterror(req, status);
245                                         return tevent_req_post(req, ev);
246                                 }
247                         } else {
248                                 if (SMB_VFS_STAT(conn, fsp->fsp_name) != 0) {
249                                         DEBUG(3,("smbd_smb2_setinfo_send: "
250                                                  "fileinfo of %s failed (%s)\n",
251                                                  fsp_str_dbg(fsp),
252                                                  strerror(errno)));
253                                         status = map_nt_error_from_unix(errno);
254                                         tevent_req_nterror(req, status);
255                                         return tevent_req_post(req, ev);
256                                 }
257                         }
258                 } else if (fsp->print_file) {
259                         /*
260                          * Doing a DELETE_ON_CLOSE should cancel a print job.
261                          */
262                         if ((file_info_level == SMB_SET_FILE_DISPOSITION_INFO)
263                             && in_input_buffer.length >= 1
264                             && CVAL(in_input_buffer.data,0)) {
265                                 fsp->fh->private_options |= NTCREATEX_OPTIONS_PRIVATE_DELETE_ON_CLOSE;
266
267                                 DEBUG(3,("smbd_smb2_setinfo_send: "
268                                          "Cancelling print job (%s)\n",
269                                          fsp_str_dbg(fsp)));
270
271                                 tevent_req_done(req);
272                                 return tevent_req_post(req, ev);
273                         } else {
274                                 tevent_req_nterror(req,
275                                         NT_STATUS_OBJECT_PATH_INVALID);
276                                 return tevent_req_post(req, ev);
277                         }
278                 } else {
279                         /*
280                          * Original code - this is an open file.
281                          */
282
283                         if (SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st) != 0) {
284                                 DEBUG(3,("smbd_smb2_setinfo_send: fstat "
285                                          "of fnum %d failed (%s)\n", fsp->fnum,
286                                          strerror(errno)));
287                                 status = map_nt_error_from_unix(errno);
288                                 tevent_req_nterror(req, status);
289                                 return tevent_req_post(req, ev);
290                         }
291                 }
292
293                 data = NULL;
294                 data_size = in_input_buffer.length;
295                 if (data_size > 0) {
296                         data = (char *)SMB_MALLOC_ARRAY(char, data_size);
297                         if (tevent_req_nomem(data, req)) {
298                                 return tevent_req_post(req, ev);
299                         }
300                         memcpy(data, in_input_buffer.data, data_size);
301                 }
302
303                 status = smbd_do_setfilepathinfo(conn, smbreq, state,
304                                                  file_info_level,
305                                                  fsp,
306                                                  fsp->fsp_name,
307                                                  &data,
308                                                  data_size,
309                                                  &ret_size);
310                 SAFE_FREE(data);
311                 if (!NT_STATUS_IS_OK(status)) {
312                         if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
313                                 status = NT_STATUS_INVALID_INFO_CLASS;
314                         }
315                         tevent_req_nterror(req, status);
316                         return tevent_req_post(req, ev);
317                 }
318                 break;
319         }
320
321         case 0x03:/* SMB2_SETINFO_SECURITY */
322         {
323                 if (!CAN_WRITE(conn)) {
324                         tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
325                         return tevent_req_post(req, ev);
326                 }
327
328                 status = set_sd(fsp,
329                                 in_input_buffer.data,
330                                 in_input_buffer.length,
331                                 in_additional_information);
332                 if (!NT_STATUS_IS_OK(status)) {
333                         tevent_req_nterror(req, status);
334                         return tevent_req_post(req, ev);
335                 }
336                 break;
337         }
338
339         default:
340                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
341                 return tevent_req_post(req, ev);
342         }
343
344         tevent_req_done(req);
345         return tevent_req_post(req, ev);
346 }
347
348 static NTSTATUS smbd_smb2_setinfo_recv(struct tevent_req *req)
349 {
350         NTSTATUS status;
351
352         if (tevent_req_is_nterror(req, &status)) {
353                 tevent_req_received(req);
354                 return status;
355         }
356
357         tevent_req_received(req);
358         return NT_STATUS_OK;
359 }