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