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