s3:smbd: make use of smbXsrv_tcon and smbXsrv_session for smb2
[kai/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                 DEBUG(2,("smbd_smb2_request_process_setinfo: "
87                          "client ignored max trans: %s: 0x%08X: 0x%08X\n",
88                          __location__, (unsigned)in_input_buffer.length,
89                          (unsigned)req->sconn->smb2.max_trans));
90                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
91         }
92
93         status = smbd_smb2_request_verify_creditcharge(req,
94                                                 in_input_buffer.length);
95         if (!NT_STATUS_IS_OK(status)) {
96                 return smbd_smb2_request_error(req, status);
97         }
98
99         in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
100         if (in_fsp == NULL) {
101                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
102         }
103
104         subreq = smbd_smb2_setinfo_send(req, req->sconn->ev_ctx,
105                                         req, in_fsp,
106                                         in_info_type,
107                                         in_file_info_class,
108                                         in_input_buffer,
109                                         in_additional_information);
110         if (subreq == NULL) {
111                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
112         }
113         tevent_req_set_callback(subreq, smbd_smb2_request_setinfo_done, req);
114
115         return smbd_smb2_request_pending_queue(req, subreq, 500);
116 }
117
118 static void smbd_smb2_request_setinfo_done(struct tevent_req *subreq)
119 {
120         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
121                                         struct smbd_smb2_request);
122         DATA_BLOB outbody;
123         NTSTATUS status;
124         NTSTATUS error; /* transport error */
125
126         status = smbd_smb2_setinfo_recv(subreq);
127         TALLOC_FREE(subreq);
128         if (!NT_STATUS_IS_OK(status)) {
129                 error = smbd_smb2_request_error(req, status);
130                 if (!NT_STATUS_IS_OK(error)) {
131                         smbd_server_connection_terminate(req->sconn,
132                                                          nt_errstr(error));
133                         return;
134                 }
135                 return;
136         }
137
138         outbody = data_blob_talloc(req->out.vector, NULL, 0x02);
139         if (outbody.data == NULL) {
140                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
141                 if (!NT_STATUS_IS_OK(error)) {
142                         smbd_server_connection_terminate(req->sconn,
143                                                          nt_errstr(error));
144                         return;
145                 }
146                 return;
147         }
148
149         SSVAL(outbody.data, 0x00, 0x02);        /* struct size */
150
151         error = smbd_smb2_request_done(req, outbody, NULL);
152         if (!NT_STATUS_IS_OK(error)) {
153                 smbd_server_connection_terminate(req->sconn,
154                                                  nt_errstr(error));
155                 return;
156         }
157 }
158
159 struct smbd_smb2_setinfo_state {
160         struct smbd_smb2_request *smb2req;
161 };
162
163 static struct tevent_req *smbd_smb2_setinfo_send(TALLOC_CTX *mem_ctx,
164                                                  struct tevent_context *ev,
165                                                  struct smbd_smb2_request *smb2req,
166                                                  struct files_struct *fsp,
167                                                  uint8_t in_info_type,
168                                                  uint8_t in_file_info_class,
169                                                  DATA_BLOB in_input_buffer,
170                                                  uint32_t in_additional_information)
171 {
172         struct tevent_req *req = NULL;
173         struct smbd_smb2_setinfo_state *state = NULL;
174         struct smb_request *smbreq = NULL;
175         connection_struct *conn = smb2req->tcon->compat;
176         NTSTATUS status;
177
178         req = tevent_req_create(mem_ctx, &state,
179                                 struct smbd_smb2_setinfo_state);
180         if (req == NULL) {
181                 return NULL;
182         }
183         state->smb2req = smb2req;
184
185         DEBUG(10,("smbd_smb2_setinfo_send: %s - %s\n",
186                   fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
187
188         smbreq = smbd_smb2_fake_smb_request(smb2req);
189         if (tevent_req_nomem(smbreq, req)) {
190                 return tevent_req_post(req, ev);
191         }
192
193         if (IS_IPC(conn)) {
194                 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
195                 return tevent_req_post(req, ev);
196         }
197
198         switch (in_info_type) {
199         case 0x01:/* SMB2_SETINFO_FILE */
200         {
201                 uint16_t file_info_level;
202                 char *data;
203                 int data_size;
204                 int ret_size = 0;
205
206
207                 file_info_level = in_file_info_class + 1000;
208                 if (file_info_level == SMB_FILE_RENAME_INFORMATION) {
209                         /* SMB2_FILE_RENAME_INFORMATION_INTERNAL == 0xFF00 + in_file_info_class */
210                         file_info_level = SMB2_FILE_RENAME_INFORMATION_INTERNAL;
211                 }
212
213                 if (fsp->fh->fd == -1) {
214                         /*
215                          * This is actually a SETFILEINFO on a directory
216                          * handle (returned from an NT SMB). NT5.0 seems
217                          * to do this call. JRA.
218                          */
219                         if (INFO_LEVEL_IS_UNIX(file_info_level)) {
220                                 /* Always do lstat for UNIX calls. */
221                                 if (SMB_VFS_LSTAT(conn, fsp->fsp_name)) {
222                                         DEBUG(3,("smbd_smb2_setinfo_send: "
223                                                  "SMB_VFS_LSTAT of %s failed "
224                                                  "(%s)\n", 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                         } else {
231                                 if (SMB_VFS_STAT(conn, fsp->fsp_name) != 0) {
232                                         DEBUG(3,("smbd_smb2_setinfo_send: "
233                                                  "fileinfo of %s failed (%s)\n",
234                                                  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                         }
241                 } else if (fsp->print_file) {
242                         /*
243                          * Doing a DELETE_ON_CLOSE should cancel a print job.
244                          */
245                         if ((file_info_level == SMB_SET_FILE_DISPOSITION_INFO)
246                             && in_input_buffer.length >= 1
247                             && CVAL(in_input_buffer.data,0)) {
248                                 fsp->fh->private_options |= NTCREATEX_OPTIONS_PRIVATE_DELETE_ON_CLOSE;
249
250                                 DEBUG(3,("smbd_smb2_setinfo_send: "
251                                          "Cancelling print job (%s)\n",
252                                          fsp_str_dbg(fsp)));
253
254                                 tevent_req_done(req);
255                                 return tevent_req_post(req, ev);
256                         } else {
257                                 tevent_req_nterror(req,
258                                         NT_STATUS_OBJECT_PATH_INVALID);
259                                 return tevent_req_post(req, ev);
260                         }
261                 } else {
262                         /*
263                          * Original code - this is an open file.
264                          */
265
266                         if (SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st) != 0) {
267                                 DEBUG(3,("smbd_smb2_setinfo_send: fstat "
268                                          "of %s failed (%s)\n",
269                                          fsp_fnum_dbg(fsp),
270                                          strerror(errno)));
271                                 status = map_nt_error_from_unix(errno);
272                                 tevent_req_nterror(req, status);
273                                 return tevent_req_post(req, ev);
274                         }
275                 }
276
277                 data = NULL;
278                 data_size = in_input_buffer.length;
279                 if (data_size > 0) {
280                         data = (char *)SMB_MALLOC_ARRAY(char, data_size);
281                         if (tevent_req_nomem(data, req)) {
282                                 return tevent_req_post(req, ev);
283                         }
284                         memcpy(data, in_input_buffer.data, data_size);
285                 }
286
287                 status = smbd_do_setfilepathinfo(conn, smbreq, state,
288                                                  file_info_level,
289                                                  fsp,
290                                                  fsp->fsp_name,
291                                                  &data,
292                                                  data_size,
293                                                  &ret_size);
294                 SAFE_FREE(data);
295                 if (!NT_STATUS_IS_OK(status)) {
296                         if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
297                                 status = NT_STATUS_INVALID_INFO_CLASS;
298                         }
299                         tevent_req_nterror(req, status);
300                         return tevent_req_post(req, ev);
301                 }
302                 break;
303         }
304
305         case 0x03:/* SMB2_SETINFO_SECURITY */
306         {
307                 if (!CAN_WRITE(conn)) {
308                         tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
309                         return tevent_req_post(req, ev);
310                 }
311
312                 status = set_sd(fsp,
313                                 in_input_buffer.data,
314                                 in_input_buffer.length,
315                                 in_additional_information);
316                 if (!NT_STATUS_IS_OK(status)) {
317                         tevent_req_nterror(req, status);
318                         return tevent_req_post(req, ev);
319                 }
320                 break;
321         }
322
323         default:
324                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
325                 return tevent_req_post(req, ev);
326         }
327
328         tevent_req_done(req);
329         return tevent_req_post(req, ev);
330 }
331
332 static NTSTATUS smbd_smb2_setinfo_recv(struct tevent_req *req)
333 {
334         NTSTATUS status;
335
336         if (tevent_req_is_nterror(req, &status)) {
337                 tevent_req_received(req);
338                 return status;
339         }
340
341         tevent_req_received(req);
342         return NT_STATUS_OK;
343 }