2bb0f838ead9c69fdb3201977617d27baaefc44e
[kai/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 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, 0));
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_INFO_CLASS;
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
198         return NT_STATUS_FOOBAR;
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.level                  = SVAL(req->in.body, 0x02);
217         info->in.max_response_size      = IVAL(req->in.body, 0x04);
218         info->in.unknown1               = IVAL(req->in.body, 0x08);
219         info->in.unknown2               = IVAL(req->in.body, 0x0C);
220         info->in.flags                  = IVAL(req->in.body, 0x10);
221         info->in.flags2                 = IVAL(req->in.body, 0x14);
222         info->in.file.ntvfs             = smb2srv_pull_handle(req, req->in.body, 0x18);
223
224         SMB2SRV_CHECK_FILE_HANDLE(info->in.file.ntvfs);
225         SMB2SRV_CALL_NTVFS_BACKEND(smb2srv_getinfo_backend(op));
226 }
227
228 void smb2srv_setinfo_recv(struct smb2srv_request *req)
229 {
230         smb2srv_send_error(req, NT_STATUS_NOT_IMPLEMENTED);
231 }