s3:smbd: start SMB2 GetInfo support for File*Information levels
[ambi/samba-autobuild/.git] / source3 / smbd / smb2_getinfo.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_getinfo_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                                                  uint32_t in_output_buffer_length,
31                                                  DATA_BLOB in_input_buffer,
32                                                  uint32_t in_additional_information,
33                                                  uint32_t in_flags,
34                                                  uint64_t in_file_id_volatile);
35 static NTSTATUS smbd_smb2_getinfo_recv(struct tevent_req *req,
36                                        TALLOC_CTX *mem_ctx,
37                                        DATA_BLOB *out_output_buffer);
38
39 static void smbd_smb2_request_getinfo_done(struct tevent_req *subreq);
40 NTSTATUS smbd_smb2_request_process_getinfo(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 = 0x29;
46         size_t body_size;
47         uint8_t in_info_type;
48         uint8_t in_file_info_class;
49         uint32_t in_output_buffer_length;
50         uint16_t in_input_buffer_offset;
51         uint32_t in_input_buffer_length;
52         DATA_BLOB in_input_buffer;
53         uint32_t in_additional_information;
54         uint32_t in_flags;
55         uint64_t in_file_id_persistent;
56         uint64_t in_file_id_volatile;
57         struct tevent_req *subreq;
58
59         inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
60         if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
61                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
62         }
63
64         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
65
66         body_size = SVAL(inbody, 0x00);
67         if (body_size != expected_body_size) {
68                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
69         }
70
71         in_info_type                    = CVAL(inbody, 0x02);
72         in_file_info_class              = CVAL(inbody, 0x03);
73         in_output_buffer_length         = IVAL(inbody, 0x04);
74         in_input_buffer_offset          = SVAL(inbody, 0x08);
75         /* 0x0A 2 bytes reserved */
76         in_input_buffer_length          = IVAL(inbody, 0x0C);
77         in_additional_information       = IVAL(inbody, 0x10);
78         in_flags                        = IVAL(inbody, 0x14);
79         in_file_id_persistent           = BVAL(inbody, 0x18);
80         in_file_id_volatile             = BVAL(inbody, 0x20);
81
82         if (in_input_buffer_offset == 0 && in_input_buffer_length == 0) {
83                 /* This is ok */
84         } else if (in_input_buffer_offset !=
85                    (SMB2_HDR_BODY + (body_size & 0xFFFFFFFE))) {
86                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
87         }
88
89         if (in_input_buffer_length > req->in.vector[i+2].iov_len) {
90                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
91         }
92
93         in_input_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
94         in_input_buffer.length = in_input_buffer_length;
95
96         if (req->compat_chain_fsp) {
97                 /* skip check */
98         } else if (in_file_id_persistent != 0) {
99                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
100         }
101
102         subreq = smbd_smb2_getinfo_send(req,
103                                         req->conn->smb2.event_ctx,
104                                         req,
105                                         in_info_type,
106                                         in_file_info_class,
107                                         in_output_buffer_length,
108                                         in_input_buffer,
109                                         in_additional_information,
110                                         in_flags,
111                                         in_file_id_volatile);
112         if (subreq == NULL) {
113                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
114         }
115         tevent_req_set_callback(subreq, smbd_smb2_request_getinfo_done, req);
116
117         if (tevent_req_is_in_progress(subreq)) {
118                 return smbd_smb2_request_pending_queue(req);
119         }
120
121         return NT_STATUS_OK;
122 }
123
124 static void smbd_smb2_request_getinfo_done(struct tevent_req *subreq)
125 {
126         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
127                                         struct smbd_smb2_request);
128         int i = req->current_idx;
129         uint8_t *outhdr;
130         DATA_BLOB outbody;
131         DATA_BLOB outdyn;
132         uint16_t out_output_buffer_offset;
133         DATA_BLOB out_output_buffer;
134         NTSTATUS status;
135         NTSTATUS error; /* transport error */
136
137         status = smbd_smb2_getinfo_recv(subreq,
138                                         req,
139                                         &out_output_buffer);
140         TALLOC_FREE(subreq);
141         if (!NT_STATUS_IS_OK(status)) {
142                 error = smbd_smb2_request_error(req, status);
143                 if (!NT_STATUS_IS_OK(error)) {
144                         smbd_server_connection_terminate(req->conn,
145                                                          nt_errstr(error));
146                         return;
147                 }
148                 return;
149         }
150
151         out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
152
153         outhdr = (uint8_t *)req->out.vector[i].iov_base;
154
155         outbody = data_blob_talloc(req->out.vector, NULL, 0x08);
156         if (outbody.data == NULL) {
157                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
158                 if (!NT_STATUS_IS_OK(error)) {
159                         smbd_server_connection_terminate(req->conn,
160                                                          nt_errstr(error));
161                         return;
162                 }
163                 return;
164         }
165
166         SSVAL(outbody.data, 0x00, 0x08 + 1);    /* struct size */
167         SSVAL(outbody.data, 0x02,
168               out_output_buffer_offset);        /* output buffer offset */
169         SIVAL(outbody.data, 0x04,
170               out_output_buffer.length);        /* output buffer length */
171
172         outdyn = out_output_buffer;
173
174         error = smbd_smb2_request_done(req, outbody, &outdyn);
175         if (!NT_STATUS_IS_OK(error)) {
176                 smbd_server_connection_terminate(req->conn,
177                                                  nt_errstr(error));
178                 return;
179         }
180 }
181
182 struct smbd_smb2_getinfo_state {
183         struct smbd_smb2_request *smb2req;
184         NTSTATUS status;
185         DATA_BLOB out_output_buffer;
186 };
187
188 static struct tevent_req *smbd_smb2_getinfo_send(TALLOC_CTX *mem_ctx,
189                                                  struct tevent_context *ev,
190                                                  struct smbd_smb2_request *smb2req,
191                                                  uint8_t in_info_type,
192                                                  uint8_t in_file_info_class,
193                                                  uint32_t in_output_buffer_length,
194                                                  DATA_BLOB in_input_buffer,
195                                                  uint32_t in_additional_information,
196                                                  uint32_t in_flags,
197                                                  uint64_t in_file_id_volatile)
198 {
199         struct tevent_req *req;
200         struct smbd_smb2_getinfo_state *state;
201         struct smb_request *smbreq;
202         connection_struct *conn = smb2req->tcon->compat_conn;
203         files_struct *fsp;
204
205         req = tevent_req_create(mem_ctx, &state,
206                                 struct smbd_smb2_getinfo_state);
207         if (req == NULL) {
208                 return NULL;
209         }
210         state->smb2req = smb2req;
211         state->status = NT_STATUS_INTERNAL_ERROR;
212         state->out_output_buffer = data_blob_null;
213
214         DEBUG(10,("smbd_smb2_getinfo_send: file_id[0x%016llX]\n",
215                   (unsigned long long)in_file_id_volatile));
216
217         smbreq = smbd_smb2_fake_smb_request(smb2req);
218         if (tevent_req_nomem(smbreq, req)) {
219                 return tevent_req_post(req, ev);
220         }
221
222         fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
223         if (fsp == NULL) {
224                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
225                 return tevent_req_post(req, ev);
226         }
227         if (conn != fsp->conn) {
228                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
229                 return tevent_req_post(req, ev);
230         }
231         if (smb2req->session->vuid != fsp->vuid) {
232                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
233                 return tevent_req_post(req, ev);
234         }
235
236         if (IS_IPC(conn)) {
237                 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
238                 return tevent_req_post(req, ev);
239         }
240
241         switch (in_info_type) {
242         case 0x01:/* SMB2_GETINFO_FILE */
243         {
244                 uint16_t file_info_level;
245                 char *data = NULL;
246                 unsigned int data_size = 0;
247                 struct smb_filename *smb_fname = NULL;
248                 bool delete_pending = false;
249                 struct timespec write_time_ts;
250                 struct file_id fileid;
251                 struct ea_list *ea_list = NULL;
252                 int lock_data_count = 0;
253                 char *lock_data = NULL;
254                 bool ms_dfs_link = false;
255                 NTSTATUS status;
256
257                 ZERO_STRUCT(write_time_ts);
258
259                 switch (in_file_info_class) {
260                 case 0x0F:/* RAW_FILEINFO_SMB2_ALL_EAS */
261                         file_info_level = 0xFF00 | in_file_info_class;
262                         break;
263
264                 case 0x12:/* RAW_FILEINFO_SMB2_ALL_INFORMATION */
265                         file_info_level = 0xFF00 | in_file_info_class;
266                         break;
267
268                 default:
269                         /* the levels directly map to the passthru levels */
270                         file_info_level = in_file_info_class + 1000;
271                         break;
272                 }
273
274                 status = create_synthetic_smb_fname_split(state,
275                                                           fsp->fsp_name,
276                                                           NULL,
277                                                           &smb_fname);
278                 if (!NT_STATUS_IS_OK(status)) {
279                         tevent_req_nterror(req, status);
280                         return tevent_req_post(req, ev);
281                 }
282
283                 if (fsp->fake_file_handle) {
284                         /*
285                          * This is actually for the QUOTA_FAKE_FILE --metze
286                          */
287
288                         /* We know this name is ok, it's already passed the checks. */
289
290                 } else if (fsp && (fsp->is_directory || fsp->fh->fd == -1)) {
291                         /*
292                          * This is actually a QFILEINFO on a directory
293                          * handle (returned from an NT SMB). NT5.0 seems
294                          * to do this call. JRA.
295                          */
296
297                         if (INFO_LEVEL_IS_UNIX(file_info_level)) {
298                                 /* Always do lstat for UNIX calls. */
299                                 if (SMB_VFS_LSTAT(conn, smb_fname)) {
300                                         DEBUG(3,("call_trans2qfilepathinfo: "
301                                                  "SMB_VFS_LSTAT of %s failed "
302                                                  "(%s)\n",
303                                                  smb_fname_str_dbg(smb_fname),
304                                                  strerror(errno)));
305                                         status = map_nt_error_from_unix(errno);
306                                         tevent_req_nterror(req, status);
307                                         return tevent_req_post(req, ev);
308                                 }
309                         } else if (SMB_VFS_STAT(conn, smb_fname)) {
310                                 DEBUG(3,("call_trans2qfilepathinfo: "
311                                          "SMB_VFS_STAT of %s failed (%s)\n",
312                                          smb_fname_str_dbg(smb_fname),
313                                          strerror(errno)));
314                                 status = map_nt_error_from_unix(errno);
315                                 tevent_req_nterror(req, status);
316                                 return tevent_req_post(req, ev);
317                         }
318
319                         fileid = vfs_file_id_from_sbuf(conn, &smb_fname->st);
320                         get_file_infos(fileid, &delete_pending, &write_time_ts);
321                 } else {
322                         /*
323                          * Original code - this is an open file.
324                          */
325
326                         if (SMB_VFS_FSTAT(fsp, &smb_fname->st) != 0) {
327                                 DEBUG(3, ("fstat of fnum %d failed (%s)\n",
328                                           fsp->fnum, strerror(errno)));
329                                 status = map_nt_error_from_unix(errno);
330                                 tevent_req_nterror(req, status);
331                                 return tevent_req_post(req, ev);
332                         }
333                         fileid = vfs_file_id_from_sbuf(conn, &smb_fname->st);
334                         get_file_infos(fileid, &delete_pending, &write_time_ts);
335                 }
336
337                 status = smbd_do_qfilepathinfo(conn, state,
338                                                file_info_level,
339                                                fsp,
340                                                smb_fname,
341                                                delete_pending,
342                                                write_time_ts,
343                                                ms_dfs_link,
344                                                ea_list,
345                                                lock_data_count,
346                                                lock_data,
347                                                STR_UNICODE,
348                                                in_output_buffer_length,
349                                                &data,
350                                                &data_size);
351                 if (!NT_STATUS_IS_OK(status)) {
352                         SAFE_FREE(data);
353                         tevent_req_nterror(req, status);
354                         return tevent_req_post(req, ev);
355                 }
356                 if (data_size > 0) {
357                         state->out_output_buffer = data_blob_talloc(state,
358                                                                     data,
359                                                                     data_size);
360                         SAFE_FREE(data);
361                         if (tevent_req_nomem(state->out_output_buffer.data, req)) {
362                                 return tevent_req_post(req, ev);
363                         }
364                 }
365                 SAFE_FREE(data);
366                 break;
367         }
368
369         default:
370                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
371                 return tevent_req_post(req, ev);
372         }
373
374         tevent_req_done(req);
375         return tevent_req_post(req, ev);
376 }
377
378 static NTSTATUS smbd_smb2_getinfo_recv(struct tevent_req *req,
379                                        TALLOC_CTX *mem_ctx,
380                                        DATA_BLOB *out_output_buffer)
381 {
382         NTSTATUS status;
383         struct smbd_smb2_getinfo_state *state = tevent_req_data(req,
384                                                 struct smbd_smb2_getinfo_state);
385
386         if (tevent_req_is_nterror(req, &status)) {
387                 tevent_req_received(req);
388                 return status;
389         }
390
391         *out_output_buffer = state->out_output_buffer;
392         talloc_steal(mem_ctx, out_output_buffer->data);
393
394         tevent_req_received(req);
395         return NT_STATUS_OK;
396 }