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