Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into v4-0-test
[samba.git] / source4 / smb_server / smb2 / fileinfo.c
1 /* 
2    Unix SMB2 implementation.
3    
4    Copyright (C) Stefan Metzmacher      2006
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "libcli/smb2/smb2.h"
22 #include "libcli/smb2/smb2_calls.h"
23 #include "smb_server/smb_server.h"
24 #include "smb_server/service_smb_proto.h"
25 #include "smb_server/smb2/smb2_server.h"
26 #include "ntvfs/ntvfs.h"
27 #include "librpc/gen_ndr/ndr_security.h"
28
29 struct smb2srv_getinfo_op {
30         struct smb2srv_request *req;
31         struct smb2_getinfo *info;
32         void *io_ptr;
33         NTSTATUS (*send_fn)(struct smb2srv_getinfo_op *op);
34 };
35
36 static void smb2srv_getinfo_send(struct ntvfs_request *ntvfs)
37 {
38         struct smb2srv_getinfo_op *op;
39         struct smb2srv_request *req;
40
41         /*
42          * SMB2 uses NT_STATUS_INVALID_INFO_CLASS
43          * so we need to translated it here
44          */
45         if (NT_STATUS_EQUAL(NT_STATUS_INVALID_LEVEL, ntvfs->async_states->status)) {
46                 ntvfs->async_states->status = NT_STATUS_INVALID_INFO_CLASS;
47         }
48
49         SMB2SRV_CHECK_ASYNC_STATUS(op, struct smb2srv_getinfo_op);
50
51         ZERO_STRUCT(op->info->out);
52         if (op->send_fn) {
53                 SMB2SRV_CHECK(op->send_fn(op));
54         }
55
56         if (op->info->in.output_buffer_length < op->info->out.blob.length) {
57                 smb2srv_send_error(req,  NT_STATUS_INFO_LENGTH_MISMATCH);
58                 return;
59         }
60
61         SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x08, true, op->info->out.blob.length));
62
63         SMB2SRV_CHECK(smb2_push_o16s32_blob(&req->out, 0x02, op->info->out.blob));
64         SSVAL(req->out.body,    0x06,   0);
65
66         smb2srv_send_reply(req);
67 }
68
69 static NTSTATUS smb2srv_getinfo_file_send(struct smb2srv_getinfo_op *op)
70 {
71         union smb_fileinfo *io = talloc_get_type(op->io_ptr, union smb_fileinfo);
72         NTSTATUS status;
73
74         status = smbsrv_push_passthru_fileinfo(op->req,
75                                                &op->info->out.blob,
76                                                io->generic.level, io,
77                                                STR_UNICODE);
78         NT_STATUS_NOT_OK_RETURN(status);
79
80         return NT_STATUS_OK;
81 }
82
83 static NTSTATUS smb2srv_getinfo_file(struct smb2srv_getinfo_op *op, uint8_t smb2_level)
84 {
85         union smb_fileinfo *io;
86         uint16_t level;
87
88         io = talloc(op, union smb_fileinfo);
89         NT_STATUS_HAVE_NO_MEMORY(io);
90
91         level = op->info->in.info_type | (op->info->in.info_class << 8);
92         switch (level) {
93         case RAW_FILEINFO_SMB2_ALL_EAS:
94                 io->all_eas.level               = level;
95                 io->all_eas.in.file.ntvfs       = op->info->in.file.ntvfs;
96                 io->all_eas.in.continue_flags   = op->info->in.getinfo_flags;
97                 break;
98
99         case RAW_FILEINFO_SMB2_ALL_INFORMATION:
100                 io->all_info2.level             = level;
101                 io->all_info2.in.file.ntvfs     = op->info->in.file.ntvfs;
102                 break;
103
104         default:
105                 /* the rest directly maps to the passthru levels */
106                 io->generic.level               = smb2_level + 1000;
107                 io->generic.in.file.ntvfs       = op->info->in.file.ntvfs;
108                 break;
109         }
110
111         op->io_ptr      = io;
112         op->send_fn     = smb2srv_getinfo_file_send;
113
114         return ntvfs_qfileinfo(op->req->ntvfs, io);
115 }
116
117 static NTSTATUS smb2srv_getinfo_fs_send(struct smb2srv_getinfo_op *op)
118 {
119         union smb_fsinfo *io = talloc_get_type(op->io_ptr, union smb_fsinfo);
120         NTSTATUS status;
121
122         status = smbsrv_push_passthru_fsinfo(op->req,
123                                              &op->info->out.blob,
124                                              io->generic.level, io,
125                                              STR_UNICODE);
126         NT_STATUS_NOT_OK_RETURN(status);
127
128         return NT_STATUS_OK;
129 }
130
131 static NTSTATUS smb2srv_getinfo_fs(struct smb2srv_getinfo_op *op, uint8_t smb2_level)
132 {
133         union smb_fsinfo *io;
134
135         io = talloc(op, union smb_fsinfo);
136         NT_STATUS_HAVE_NO_MEMORY(io);
137
138         /* the rest directly maps to the passthru levels */
139         io->generic.level       = smb2_level + 1000;
140
141         /* TODO: allow qfsinfo only the share root directory handle */
142
143         op->io_ptr      = io;
144         op->send_fn     = smb2srv_getinfo_fs_send;
145
146         return ntvfs_fsinfo(op->req->ntvfs, io);
147 }
148
149 static NTSTATUS smb2srv_getinfo_security_send(struct smb2srv_getinfo_op *op)
150 {
151         union smb_fileinfo *io = talloc_get_type(op->io_ptr, union smb_fileinfo);
152         enum ndr_err_code ndr_err;
153
154         ndr_err = ndr_push_struct_blob(&op->info->out.blob, op->req, NULL,
155                                        io->query_secdesc.out.sd,
156                                        (ndr_push_flags_fn_t)ndr_push_security_descriptor);
157         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
158                 return ndr_map_error2ntstatus(ndr_err);
159         }
160
161         return NT_STATUS_OK;
162 }
163
164 static NTSTATUS smb2srv_getinfo_security(struct smb2srv_getinfo_op *op, uint8_t smb2_level)
165 {
166         union smb_fileinfo *io;
167
168         switch (smb2_level) {
169         case 0x00:
170                 io = talloc(op, union smb_fileinfo);
171                 NT_STATUS_HAVE_NO_MEMORY(io);
172
173                 io->query_secdesc.level                 = RAW_FILEINFO_SEC_DESC;
174                 io->query_secdesc.in.file.ntvfs         = op->info->in.file.ntvfs;
175                 io->query_secdesc.in.secinfo_flags      = op->info->in.additional_information;
176
177                 op->io_ptr      = io;
178                 op->send_fn     = smb2srv_getinfo_security_send;
179
180                 return ntvfs_qfileinfo(op->req->ntvfs, io);
181         }
182
183         return NT_STATUS_INVALID_PARAMETER;
184 }
185
186 static NTSTATUS smb2srv_getinfo_backend(struct smb2srv_getinfo_op *op)
187 {
188         switch (op->info->in.info_type) {
189         case SMB2_GETINFO_FILE:
190                 return smb2srv_getinfo_file(op, op->info->in.info_class);
191
192         case SMB2_GETINFO_FS:
193                 return smb2srv_getinfo_fs(op, op->info->in.info_class);
194
195         case SMB2_GETINFO_SECURITY:
196                 return smb2srv_getinfo_security(op, op->info->in.info_class);
197
198         case SMB2_GETINFO_QUOTA:
199                 return NT_STATUS_NOT_SUPPORTED;
200         }
201
202         return NT_STATUS_INVALID_PARAMETER;
203 }
204
205 void smb2srv_getinfo_recv(struct smb2srv_request *req)
206 {
207         struct smb2_getinfo *info;
208         struct smb2srv_getinfo_op *op;
209
210         SMB2SRV_CHECK_BODY_SIZE(req, 0x28, true);
211         SMB2SRV_TALLOC_IO_PTR(info, struct smb2_getinfo);
212         /* this overwrites req->io_ptr !*/
213         SMB2SRV_TALLOC_IO_PTR(op, struct smb2srv_getinfo_op);
214         op->req         = req;
215         op->info        = info;
216         op->io_ptr      = NULL;
217         op->send_fn     = NULL;
218         SMB2SRV_SETUP_NTVFS_REQUEST(smb2srv_getinfo_send, NTVFS_ASYNC_STATE_MAY_ASYNC);
219
220         info->in.info_type              = CVAL(req->in.body, 0x02);
221         info->in.info_class             = CVAL(req->in.body, 0x03);
222         info->in.output_buffer_length   = IVAL(req->in.body, 0x04);
223         info->in.reserved               = IVAL(req->in.body, 0x0C);
224         info->in.additional_information = IVAL(req->in.body, 0x10);
225         info->in.getinfo_flags          = IVAL(req->in.body, 0x14);
226         info->in.file.ntvfs             = smb2srv_pull_handle(req, req->in.body, 0x18);
227         SMB2SRV_CHECK(smb2_pull_o16As32_blob(&req->in, op, 
228                                             req->in.body+0x08, &info->in.blob));
229
230         SMB2SRV_CHECK_FILE_HANDLE(info->in.file.ntvfs);
231         SMB2SRV_CALL_NTVFS_BACKEND(smb2srv_getinfo_backend(op));
232 }
233
234 struct smb2srv_setinfo_op {
235         struct smb2srv_request *req;
236         struct smb2_setinfo *info;
237 };
238
239 static void smb2srv_setinfo_send(struct ntvfs_request *ntvfs)
240 {
241         struct smb2srv_setinfo_op *op;
242         struct smb2srv_request *req;
243
244         /*
245          * SMB2 uses NT_STATUS_INVALID_INFO_CLASS
246          * so we need to translated it here
247          */
248         if (NT_STATUS_EQUAL(NT_STATUS_INVALID_LEVEL, ntvfs->async_states->status)) {
249                 ntvfs->async_states->status = NT_STATUS_INVALID_INFO_CLASS;
250         }
251
252         SMB2SRV_CHECK_ASYNC_STATUS(op, struct smb2srv_setinfo_op);
253
254         SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x02, false, 0));
255
256         smb2srv_send_reply(req);
257 }
258
259 static NTSTATUS smb2srv_setinfo_file(struct smb2srv_setinfo_op *op, uint8_t smb2_level)
260 {
261         union smb_setfileinfo *io;
262         NTSTATUS status;
263
264         io = talloc(op, union smb_setfileinfo);
265         NT_STATUS_HAVE_NO_MEMORY(io);
266
267         /* the levels directly map to the passthru levels */
268         io->generic.level               = smb2_level + 1000;
269         io->generic.in.file.ntvfs       = op->info->in.file.ntvfs;
270
271         /* handle cases that don't map directly */
272         if (io->generic.level == RAW_SFILEINFO_RENAME_INFORMATION) {
273                 io->generic.level = RAW_SFILEINFO_RENAME_INFORMATION_SMB2;
274         }
275
276         status = smbsrv_pull_passthru_sfileinfo(io, io->generic.level, io,
277                                                 &op->info->in.blob,
278                                                 STR_UNICODE, &op->req->in.bufinfo);
279         NT_STATUS_NOT_OK_RETURN(status);
280
281         return ntvfs_setfileinfo(op->req->ntvfs, io);
282 }
283
284 static NTSTATUS smb2srv_setinfo_fs(struct smb2srv_setinfo_op *op, uint8_t smb2_level)
285 {
286         switch (smb2_level) {
287         case 0x02:
288                 return NT_STATUS_NOT_IMPLEMENTED;
289
290         case 0x06:
291                 return NT_STATUS_ACCESS_DENIED;
292
293         case 0x08:
294                 return NT_STATUS_ACCESS_DENIED;
295
296         case 0x0A:
297                 return NT_STATUS_ACCESS_DENIED;
298         }
299
300         return NT_STATUS_INVALID_INFO_CLASS;
301 }
302
303 static NTSTATUS smb2srv_setinfo_security(struct smb2srv_setinfo_op *op, uint8_t smb2_level)
304 {
305         union smb_setfileinfo *io;
306         enum ndr_err_code ndr_err;
307
308         switch (smb2_level) {
309         case 0x00:
310                 io = talloc(op, union smb_setfileinfo);
311                 NT_STATUS_HAVE_NO_MEMORY(io);
312
313                 io->set_secdesc.level            = RAW_SFILEINFO_SEC_DESC;
314                 io->set_secdesc.in.file.ntvfs    = op->info->in.file.ntvfs;
315                 io->set_secdesc.in.secinfo_flags = op->info->in.flags;
316
317                 io->set_secdesc.in.sd = talloc(io, struct security_descriptor);
318                 NT_STATUS_HAVE_NO_MEMORY(io->set_secdesc.in.sd);
319
320                 ndr_err = ndr_pull_struct_blob(&op->info->in.blob, io, NULL,
321                                                io->set_secdesc.in.sd,
322                                                (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
323                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
324                         return ndr_map_error2ntstatus(ndr_err);
325                 }
326
327                 return ntvfs_setfileinfo(op->req->ntvfs, io);
328         }
329
330         return NT_STATUS_INVALID_INFO_CLASS;
331 }
332
333 static NTSTATUS smb2srv_setinfo_backend(struct smb2srv_setinfo_op *op)
334 {
335         uint8_t smb2_class;
336         uint8_t smb2_level;
337
338         smb2_class = 0xFF & op->info->in.level;
339         smb2_level = 0xFF & (op->info->in.level>>8);
340
341         switch (smb2_class) {
342         case SMB2_GETINFO_FILE:
343                 return smb2srv_setinfo_file(op, smb2_level);
344
345         case SMB2_GETINFO_FS:
346                 return smb2srv_setinfo_fs(op, smb2_level);
347
348         case SMB2_GETINFO_SECURITY:
349                 return smb2srv_setinfo_security(op, smb2_level);
350
351         case 0x04:
352                 return NT_STATUS_NOT_SUPPORTED;
353         }
354
355         return NT_STATUS_INVALID_PARAMETER;
356 }
357
358 void smb2srv_setinfo_recv(struct smb2srv_request *req)
359 {
360         struct smb2_setinfo *info;
361         struct smb2srv_setinfo_op *op;
362
363         SMB2SRV_CHECK_BODY_SIZE(req, 0x20, true);
364         SMB2SRV_TALLOC_IO_PTR(info, struct smb2_setinfo);
365         /* this overwrites req->io_ptr !*/
366         SMB2SRV_TALLOC_IO_PTR(op, struct smb2srv_setinfo_op);
367         op->req         = req;
368         op->info        = info;
369         SMB2SRV_SETUP_NTVFS_REQUEST(smb2srv_setinfo_send, NTVFS_ASYNC_STATE_MAY_ASYNC);
370
371         info->in.level                  = SVAL(req->in.body, 0x02);
372         SMB2SRV_CHECK(smb2_pull_s32o32_blob(&req->in, info, req->in.body+0x04, &info->in.blob));
373         info->in.flags                  = IVAL(req->in.body, 0x0C);
374         info->in.file.ntvfs             = smb2srv_pull_handle(req, req->in.body, 0x10);
375
376         SMB2SRV_CHECK_FILE_HANDLE(info->in.file.ntvfs);
377         SMB2SRV_CALL_NTVFS_BACKEND(smb2srv_setinfo_backend(op));
378 }