updated SMB2 code for getinfo according to WSPP docs
[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         SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x08, true, op->info->out.blob.length));
57
58         /* TODO: this is maybe a o16s32_blob */
59         SMB2SRV_CHECK(smb2_push_o16s16_blob(&req->out, 0x02, op->info->out.blob));
60         SSVAL(req->out.body,    0x06,   0);
61
62         smb2srv_send_reply(req);
63 }
64
65 static NTSTATUS smb2srv_getinfo_file_send(struct smb2srv_getinfo_op *op)
66 {
67         union smb_fileinfo *io = talloc_get_type(op->io_ptr, union smb_fileinfo);
68         NTSTATUS status;
69
70         status = smbsrv_push_passthru_fileinfo(op->req,
71                                                &op->info->out.blob,
72                                                io->generic.level, io,
73                                                STR_UNICODE);
74         NT_STATUS_NOT_OK_RETURN(status);
75
76         return NT_STATUS_OK;
77 }
78
79 static NTSTATUS smb2srv_getinfo_file(struct smb2srv_getinfo_op *op, uint8_t smb2_level)
80 {
81         union smb_fileinfo *io;
82         uint16_t level;
83
84         io = talloc(op, union smb_fileinfo);
85         NT_STATUS_HAVE_NO_MEMORY(io);
86
87         level = op->info->in.info_type | (op->info->in.info_class << 8);
88         switch (level) {
89         case RAW_FILEINFO_SMB2_ALL_EAS:
90                 io->all_eas.level               = level;
91                 io->all_eas.in.file.ntvfs       = op->info->in.file.ntvfs;
92                 io->all_eas.in.continue_flags   = op->info->in.getinfo_flags;
93                 break;
94
95         case RAW_FILEINFO_SMB2_ALL_INFORMATION:
96                 io->all_info2.level             = level;
97                 io->all_info2.in.file.ntvfs     = op->info->in.file.ntvfs;
98                 break;
99
100         default:
101                 /* the rest directly maps to the passthru levels */
102                 io->generic.level               = smb2_level + 1000;
103                 io->generic.in.file.ntvfs       = op->info->in.file.ntvfs;
104                 break;
105         }
106
107         op->io_ptr      = io;
108         op->send_fn     = smb2srv_getinfo_file_send;
109
110         return ntvfs_qfileinfo(op->req->ntvfs, io);
111 }
112
113 static NTSTATUS smb2srv_getinfo_fs_send(struct smb2srv_getinfo_op *op)
114 {
115         union smb_fsinfo *io = talloc_get_type(op->io_ptr, union smb_fsinfo);
116         NTSTATUS status;
117
118         status = smbsrv_push_passthru_fsinfo(op->req,
119                                              &op->info->out.blob,
120                                              io->generic.level, io,
121                                              STR_UNICODE);
122         NT_STATUS_NOT_OK_RETURN(status);
123
124         return NT_STATUS_OK;
125 }
126
127 static NTSTATUS smb2srv_getinfo_fs(struct smb2srv_getinfo_op *op, uint8_t smb2_level)
128 {
129         union smb_fsinfo *io;
130
131         io = talloc(op, union smb_fsinfo);
132         NT_STATUS_HAVE_NO_MEMORY(io);
133
134         /* the rest directly maps to the passthru levels */
135         io->generic.level       = smb2_level + 1000;
136
137         /* TODO: allow qfsinfo only the share root directory handle */
138
139         op->io_ptr      = io;
140         op->send_fn     = smb2srv_getinfo_fs_send;
141
142         return ntvfs_fsinfo(op->req->ntvfs, io);
143 }
144
145 static NTSTATUS smb2srv_getinfo_security_send(struct smb2srv_getinfo_op *op)
146 {
147         union smb_fileinfo *io = talloc_get_type(op->io_ptr, union smb_fileinfo);
148         enum ndr_err_code ndr_err;
149
150         ndr_err = ndr_push_struct_blob(&op->info->out.blob, op->req, NULL,
151                                        io->query_secdesc.out.sd,
152                                        (ndr_push_flags_fn_t)ndr_push_security_descriptor);
153         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
154                 return ndr_map_error2ntstatus(ndr_err);
155         }
156
157         return NT_STATUS_OK;
158 }
159
160 static NTSTATUS smb2srv_getinfo_security(struct smb2srv_getinfo_op *op, uint8_t smb2_level)
161 {
162         union smb_fileinfo *io;
163
164         switch (smb2_level) {
165         case 0x00:
166                 io = talloc(op, union smb_fileinfo);
167                 NT_STATUS_HAVE_NO_MEMORY(io);
168
169                 io->query_secdesc.level                 = RAW_FILEINFO_SEC_DESC;
170                 io->query_secdesc.in.file.ntvfs         = op->info->in.file.ntvfs;
171                 io->query_secdesc.in.secinfo_flags      = op->info->in.additional_information;
172
173                 op->io_ptr      = io;
174                 op->send_fn     = smb2srv_getinfo_security_send;
175
176                 return ntvfs_qfileinfo(op->req->ntvfs, io);
177         }
178
179         return NT_STATUS_INVALID_PARAMETER;
180 }
181
182 static NTSTATUS smb2srv_getinfo_backend(struct smb2srv_getinfo_op *op)
183 {
184         switch (op->info->in.info_type) {
185         case SMB2_GETINFO_FILE:
186                 return smb2srv_getinfo_file(op, op->info->in.info_class);
187
188         case SMB2_GETINFO_FS:
189                 return smb2srv_getinfo_fs(op, op->info->in.info_class);
190
191         case SMB2_GETINFO_SECURITY:
192                 return smb2srv_getinfo_security(op, op->info->in.info_class);
193
194         case SMB2_GETINFO_QUOTA:
195                 return NT_STATUS_NOT_SUPPORTED;
196         }
197
198         return NT_STATUS_INVALID_PARAMETER;
199 }
200
201 void smb2srv_getinfo_recv(struct smb2srv_request *req)
202 {
203         struct smb2_getinfo *info;
204         struct smb2srv_getinfo_op *op;
205
206         SMB2SRV_CHECK_BODY_SIZE(req, 0x28, true);
207         SMB2SRV_TALLOC_IO_PTR(info, struct smb2_getinfo);
208         /* this overwrites req->io_ptr !*/
209         SMB2SRV_TALLOC_IO_PTR(op, struct smb2srv_getinfo_op);
210         op->req         = req;
211         op->info        = info;
212         op->io_ptr      = NULL;
213         op->send_fn     = NULL;
214         SMB2SRV_SETUP_NTVFS_REQUEST(smb2srv_getinfo_send, NTVFS_ASYNC_STATE_MAY_ASYNC);
215
216         info->in.info_type              = CVAL(req->in.body, 0x02);
217         info->in.info_class             = CVAL(req->in.body, 0x03);
218         info->in.output_buffer_length   = IVAL(req->in.body, 0x04);
219         info->in.reserved               = IVAL(req->in.body, 0x0C);
220         info->in.additional_information = IVAL(req->in.body, 0x10);
221         info->in.getinfo_flags          = IVAL(req->in.body, 0x14);
222         info->in.file.ntvfs             = smb2srv_pull_handle(req, req->in.body, 0x18);
223         SMB2SRV_CHECK(smb2_pull_o16As32_blob(&req->in, op, 
224                                             req->in.body+0x08, &info->in.blob));
225
226         SMB2SRV_CHECK_FILE_HANDLE(info->in.file.ntvfs);
227         SMB2SRV_CALL_NTVFS_BACKEND(smb2srv_getinfo_backend(op));
228 }
229
230 struct smb2srv_setinfo_op {
231         struct smb2srv_request *req;
232         struct smb2_setinfo *info;
233 };
234
235 static void smb2srv_setinfo_send(struct ntvfs_request *ntvfs)
236 {
237         struct smb2srv_setinfo_op *op;
238         struct smb2srv_request *req;
239
240         /*
241          * SMB2 uses NT_STATUS_INVALID_INFO_CLASS
242          * so we need to translated it here
243          */
244         if (NT_STATUS_EQUAL(NT_STATUS_INVALID_LEVEL, ntvfs->async_states->status)) {
245                 ntvfs->async_states->status = NT_STATUS_INVALID_INFO_CLASS;
246         }
247
248         SMB2SRV_CHECK_ASYNC_STATUS(op, struct smb2srv_setinfo_op);
249
250         SMB2SRV_CHECK(smb2srv_setup_reply(req, 0x02, false, 0));
251
252         smb2srv_send_reply(req);
253 }
254
255 static NTSTATUS smb2srv_setinfo_file(struct smb2srv_setinfo_op *op, uint8_t smb2_level)
256 {
257         union smb_setfileinfo *io;
258         NTSTATUS status;
259
260         io = talloc(op, union smb_setfileinfo);
261         NT_STATUS_HAVE_NO_MEMORY(io);
262
263         /* the levels directly map to the passthru levels */
264         io->generic.level               = smb2_level + 1000;
265         io->generic.in.file.ntvfs       = op->info->in.file.ntvfs;
266
267         /* handle cases that don't map directly */
268         if (io->generic.level == RAW_SFILEINFO_RENAME_INFORMATION) {
269                 io->generic.level = RAW_SFILEINFO_RENAME_INFORMATION_SMB2;
270         }
271
272         status = smbsrv_pull_passthru_sfileinfo(io, io->generic.level, io,
273                                                 &op->info->in.blob,
274                                                 STR_UNICODE, &op->req->in.bufinfo);
275         NT_STATUS_NOT_OK_RETURN(status);
276
277         return ntvfs_setfileinfo(op->req->ntvfs, io);
278 }
279
280 static NTSTATUS smb2srv_setinfo_fs(struct smb2srv_setinfo_op *op, uint8_t smb2_level)
281 {
282         switch (smb2_level) {
283         case 0x02:
284                 return NT_STATUS_NOT_IMPLEMENTED;
285
286         case 0x06:
287                 return NT_STATUS_ACCESS_DENIED;
288
289         case 0x08:
290                 return NT_STATUS_ACCESS_DENIED;
291
292         case 0x0A:
293                 return NT_STATUS_ACCESS_DENIED;
294         }
295
296         return NT_STATUS_INVALID_INFO_CLASS;
297 }
298
299 static NTSTATUS smb2srv_setinfo_security(struct smb2srv_setinfo_op *op, uint8_t smb2_level)
300 {
301         union smb_setfileinfo *io;
302         enum ndr_err_code ndr_err;
303
304         switch (smb2_level) {
305         case 0x00:
306                 io = talloc(op, union smb_setfileinfo);
307                 NT_STATUS_HAVE_NO_MEMORY(io);
308
309                 io->set_secdesc.level            = RAW_SFILEINFO_SEC_DESC;
310                 io->set_secdesc.in.file.ntvfs    = op->info->in.file.ntvfs;
311                 io->set_secdesc.in.secinfo_flags = op->info->in.flags;
312
313                 io->set_secdesc.in.sd = talloc(io, struct security_descriptor);
314                 NT_STATUS_HAVE_NO_MEMORY(io->set_secdesc.in.sd);
315
316                 ndr_err = ndr_pull_struct_blob(&op->info->in.blob, io, NULL,
317                                                io->set_secdesc.in.sd,
318                                                (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
319                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
320                         return ndr_map_error2ntstatus(ndr_err);
321                 }
322
323                 return ntvfs_setfileinfo(op->req->ntvfs, io);
324         }
325
326         return NT_STATUS_INVALID_INFO_CLASS;
327 }
328
329 static NTSTATUS smb2srv_setinfo_backend(struct smb2srv_setinfo_op *op)
330 {
331         uint8_t smb2_class;
332         uint8_t smb2_level;
333
334         smb2_class = 0xFF & op->info->in.level;
335         smb2_level = 0xFF & (op->info->in.level>>8);
336
337         switch (smb2_class) {
338         case SMB2_GETINFO_FILE:
339                 return smb2srv_setinfo_file(op, smb2_level);
340
341         case SMB2_GETINFO_FS:
342                 return smb2srv_setinfo_fs(op, smb2_level);
343
344         case SMB2_GETINFO_SECURITY:
345                 return smb2srv_setinfo_security(op, smb2_level);
346
347         case 0x04:
348                 return NT_STATUS_NOT_SUPPORTED;
349         }
350
351         return NT_STATUS_INVALID_PARAMETER;
352 }
353
354 void smb2srv_setinfo_recv(struct smb2srv_request *req)
355 {
356         struct smb2_setinfo *info;
357         struct smb2srv_setinfo_op *op;
358
359         SMB2SRV_CHECK_BODY_SIZE(req, 0x20, true);
360         SMB2SRV_TALLOC_IO_PTR(info, struct smb2_setinfo);
361         /* this overwrites req->io_ptr !*/
362         SMB2SRV_TALLOC_IO_PTR(op, struct smb2srv_setinfo_op);
363         op->req         = req;
364         op->info        = info;
365         SMB2SRV_SETUP_NTVFS_REQUEST(smb2srv_setinfo_send, NTVFS_ASYNC_STATE_MAY_ASYNC);
366
367         info->in.level                  = SVAL(req->in.body, 0x02);
368         SMB2SRV_CHECK(smb2_pull_s32o32_blob(&req->in, info, req->in.body+0x04, &info->in.blob));
369         info->in.flags                  = IVAL(req->in.body, 0x0C);
370         info->in.file.ntvfs             = smb2srv_pull_handle(req, req->in.body, 0x10);
371
372         SMB2SRV_CHECK_FILE_HANDLE(info->in.file.ntvfs);
373         SMB2SRV_CALL_NTVFS_BACKEND(smb2srv_setinfo_backend(op));
374 }