Revert "Fix bug #7781 (Samba transforms "ShareName" to lowercase when adding new...
[samba.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 "../libcli/smb/smb_common.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->sconn->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         return smbd_smb2_request_pending_queue(req, subreq);
118 }
119
120 static void smbd_smb2_request_getinfo_done(struct tevent_req *subreq)
121 {
122         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
123                                         struct smbd_smb2_request);
124         int i = req->current_idx;
125         uint8_t *outhdr;
126         DATA_BLOB outbody;
127         DATA_BLOB outdyn;
128         uint16_t out_output_buffer_offset;
129         DATA_BLOB out_output_buffer = data_blob_null;
130         NTSTATUS status;
131         NTSTATUS error; /* transport error */
132
133         status = smbd_smb2_getinfo_recv(subreq,
134                                         req,
135                                         &out_output_buffer);
136         TALLOC_FREE(subreq);
137         if (!NT_STATUS_IS_OK(status)) {
138                 error = smbd_smb2_request_error(req, status);
139                 if (!NT_STATUS_IS_OK(error)) {
140                         smbd_server_connection_terminate(req->sconn,
141                                                          nt_errstr(error));
142                         return;
143                 }
144                 return;
145         }
146
147         out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
148
149         outhdr = (uint8_t *)req->out.vector[i].iov_base;
150
151         outbody = data_blob_talloc(req->out.vector, NULL, 0x08);
152         if (outbody.data == NULL) {
153                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
154                 if (!NT_STATUS_IS_OK(error)) {
155                         smbd_server_connection_terminate(req->sconn,
156                                                          nt_errstr(error));
157                         return;
158                 }
159                 return;
160         }
161
162         SSVAL(outbody.data, 0x00, 0x08 + 1);    /* struct size */
163         SSVAL(outbody.data, 0x02,
164               out_output_buffer_offset);        /* output buffer offset */
165         SIVAL(outbody.data, 0x04,
166               out_output_buffer.length);        /* output buffer length */
167
168         outdyn = out_output_buffer;
169
170         error = smbd_smb2_request_done(req, outbody, &outdyn);
171         if (!NT_STATUS_IS_OK(error)) {
172                 smbd_server_connection_terminate(req->sconn,
173                                                  nt_errstr(error));
174                 return;
175         }
176 }
177
178 struct smbd_smb2_getinfo_state {
179         struct smbd_smb2_request *smb2req;
180         NTSTATUS status;
181         DATA_BLOB out_output_buffer;
182 };
183
184 static struct tevent_req *smbd_smb2_getinfo_send(TALLOC_CTX *mem_ctx,
185                                                  struct tevent_context *ev,
186                                                  struct smbd_smb2_request *smb2req,
187                                                  uint8_t in_info_type,
188                                                  uint8_t in_file_info_class,
189                                                  uint32_t in_output_buffer_length,
190                                                  DATA_BLOB in_input_buffer,
191                                                  uint32_t in_additional_information,
192                                                  uint32_t in_flags,
193                                                  uint64_t in_file_id_volatile)
194 {
195         struct tevent_req *req;
196         struct smbd_smb2_getinfo_state *state;
197         struct smb_request *smbreq;
198         connection_struct *conn = smb2req->tcon->compat_conn;
199         files_struct *fsp;
200
201         req = tevent_req_create(mem_ctx, &state,
202                                 struct smbd_smb2_getinfo_state);
203         if (req == NULL) {
204                 return NULL;
205         }
206         state->smb2req = smb2req;
207         state->status = NT_STATUS_INTERNAL_ERROR;
208         state->out_output_buffer = data_blob_null;
209
210         DEBUG(10,("smbd_smb2_getinfo_send: file_id[0x%016llX]\n",
211                   (unsigned long long)in_file_id_volatile));
212
213         smbreq = smbd_smb2_fake_smb_request(smb2req);
214         if (tevent_req_nomem(smbreq, req)) {
215                 return tevent_req_post(req, ev);
216         }
217
218         fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
219         if (fsp == NULL) {
220                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
221                 return tevent_req_post(req, ev);
222         }
223         if (conn != fsp->conn) {
224                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
225                 return tevent_req_post(req, ev);
226         }
227         if (smb2req->session->vuid != fsp->vuid) {
228                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
229                 return tevent_req_post(req, ev);
230         }
231
232         if (IS_IPC(conn)) {
233                 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
234                 return tevent_req_post(req, ev);
235         }
236
237         switch (in_info_type) {
238         case 0x01:/* SMB2_GETINFO_FILE */
239         {
240                 uint16_t file_info_level;
241                 char *data = NULL;
242                 unsigned int data_size = 0;
243                 bool delete_pending = false;
244                 struct timespec write_time_ts;
245                 struct file_id fileid;
246                 struct ea_list *ea_list = NULL;
247                 int lock_data_count = 0;
248                 char *lock_data = NULL;
249                 bool ms_dfs_link = false;
250                 NTSTATUS status;
251
252                 ZERO_STRUCT(write_time_ts);
253
254                 switch (in_file_info_class) {
255                 case 0x0F:/* RAW_FILEINFO_SMB2_ALL_EAS */
256                         file_info_level = 0xFF00 | in_file_info_class;
257                         break;
258
259                 case 0x12:/* RAW_FILEINFO_SMB2_ALL_INFORMATION */
260                         file_info_level = 0xFF00 | in_file_info_class;
261                         break;
262
263                 default:
264                         /* the levels directly map to the passthru levels */
265                         file_info_level = in_file_info_class + 1000;
266                         break;
267                 }
268
269                 if (fsp->fake_file_handle) {
270                         /*
271                          * This is actually for the QUOTA_FAKE_FILE --metze
272                          */
273
274                         /* We know this name is ok, it's already passed the checks. */
275
276                 } else if (fsp && (fsp->is_directory || fsp->fh->fd == -1)) {
277                         /*
278                          * This is actually a QFILEINFO on a directory
279                          * handle (returned from an NT SMB). NT5.0 seems
280                          * to do this call. JRA.
281                          */
282
283                         if (INFO_LEVEL_IS_UNIX(file_info_level)) {
284                                 /* Always do lstat for UNIX calls. */
285                                 if (SMB_VFS_LSTAT(conn, fsp->fsp_name)) {
286                                         DEBUG(3,("smbd_smb2_getinfo_send: "
287                                                  "SMB_VFS_LSTAT of %s failed "
288                                                  "(%s)\n", fsp_str_dbg(fsp),
289                                                  strerror(errno)));
290                                         status = map_nt_error_from_unix(errno);
291                                         tevent_req_nterror(req, status);
292                                         return tevent_req_post(req, ev);
293                                 }
294                         } else if (SMB_VFS_STAT(conn, fsp->fsp_name)) {
295                                 DEBUG(3,("smbd_smb2_getinfo_send: "
296                                          "SMB_VFS_STAT of %s failed (%s)\n",
297                                          fsp_str_dbg(fsp),
298                                          strerror(errno)));
299                                 status = map_nt_error_from_unix(errno);
300                                 tevent_req_nterror(req, status);
301                                 return tevent_req_post(req, ev);
302                         }
303
304                         fileid = vfs_file_id_from_sbuf(conn,
305                                                        &fsp->fsp_name->st);
306                         get_file_infos(fileid, &delete_pending, &write_time_ts);
307                 } else {
308                         /*
309                          * Original code - this is an open file.
310                          */
311
312                         if (SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st) != 0) {
313                                 DEBUG(3, ("smbd_smb2_getinfo_send: "
314                                           "fstat of fnum %d failed (%s)\n",
315                                           fsp->fnum, strerror(errno)));
316                                 status = map_nt_error_from_unix(errno);
317                                 tevent_req_nterror(req, status);
318                                 return tevent_req_post(req, ev);
319                         }
320                         fileid = vfs_file_id_from_sbuf(conn,
321                                                        &fsp->fsp_name->st);
322                         get_file_infos(fileid, &delete_pending, &write_time_ts);
323                 }
324
325                 status = smbd_do_qfilepathinfo(conn, state,
326                                                file_info_level,
327                                                fsp,
328                                                fsp->fsp_name,
329                                                delete_pending,
330                                                write_time_ts,
331                                                ms_dfs_link,
332                                                ea_list,
333                                                lock_data_count,
334                                                lock_data,
335                                                STR_UNICODE,
336                                                in_output_buffer_length,
337                                                &data,
338                                                &data_size);
339                 if (!NT_STATUS_IS_OK(status)) {
340                         SAFE_FREE(data);
341                         if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
342                                 status = NT_STATUS_INVALID_INFO_CLASS;
343                         }
344                         tevent_req_nterror(req, status);
345                         return tevent_req_post(req, ev);
346                 }
347                 if (data_size > 0) {
348                         state->out_output_buffer = data_blob_talloc(state,
349                                                                     data,
350                                                                     data_size);
351                         SAFE_FREE(data);
352                         if (tevent_req_nomem(state->out_output_buffer.data, req)) {
353                                 return tevent_req_post(req, ev);
354                         }
355                 }
356                 SAFE_FREE(data);
357                 break;
358         }
359
360         case 0x02:/* SMB2_GETINFO_FS */
361         {
362                 uint16_t file_info_level;
363                 char *data = NULL;
364                 int data_size = 0;
365                 NTSTATUS status;
366
367                 /* the levels directly map to the passthru levels */
368                 file_info_level = in_file_info_class + 1000;
369
370                 status = smbd_do_qfsinfo(conn, state,
371                                          file_info_level,
372                                          STR_UNICODE,
373                                          in_output_buffer_length,
374                                          &data,
375                                          &data_size);
376                 if (!NT_STATUS_IS_OK(status)) {
377                         SAFE_FREE(data);
378                         if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
379                                 status = NT_STATUS_INVALID_INFO_CLASS;
380                         }
381                         tevent_req_nterror(req, status);
382                         return tevent_req_post(req, ev);
383                 }
384                 if (data_size > 0) {
385                         state->out_output_buffer = data_blob_talloc(state,
386                                                                     data,
387                                                                     data_size);
388                         SAFE_FREE(data);
389                         if (tevent_req_nomem(state->out_output_buffer.data, req)) {
390                                 return tevent_req_post(req, ev);
391                         }
392                 }
393                 SAFE_FREE(data);
394                 break;
395         }
396
397         default:
398                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
399                 return tevent_req_post(req, ev);
400         }
401
402         tevent_req_done(req);
403         return tevent_req_post(req, ev);
404 }
405
406 static NTSTATUS smbd_smb2_getinfo_recv(struct tevent_req *req,
407                                        TALLOC_CTX *mem_ctx,
408                                        DATA_BLOB *out_output_buffer)
409 {
410         NTSTATUS status;
411         struct smbd_smb2_getinfo_state *state = tevent_req_data(req,
412                                                 struct smbd_smb2_getinfo_state);
413
414         if (tevent_req_is_nterror(req, &status)) {
415                 tevent_req_received(req);
416                 return status;
417         }
418
419         *out_output_buffer = state->out_output_buffer;
420         talloc_steal(mem_ctx, out_output_buffer->data);
421
422         tevent_req_received(req);
423         return NT_STATUS_OK;
424 }