e9f47140f50322c1369fcc22e43539e0fef40b15
[kai/samba.git] / source4 / libcli / smb2 / getinfo.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    SMB2 client getinfo calls
5
6    Copyright (C) Andrew Tridgell 2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "libcli/raw/libcliraw.h"
24 #include "libcli/smb2/smb2.h"
25 #include "libcli/smb2/smb2_calls.h"
26
27 /*
28   send a getinfo request
29 */
30 struct smb2_request *smb2_getinfo_send(struct smb2_tree *tree, struct smb2_getinfo *io)
31 {
32         struct smb2_request *req;
33         NTSTATUS status;
34
35         req = smb2_request_init_tree(tree, SMB2_OP_GETINFO, 0x28, true, 
36                                      io->in.blob.length);
37         if (req == NULL) return NULL;
38
39         SCVAL(req->out.body, 0x02, io->in.info_type);
40         SCVAL(req->out.body, 0x03, io->in.info_class);
41         SIVAL(req->out.body, 0x04, io->in.output_buffer_length);
42         SIVAL(req->out.body, 0x0C, io->in.reserved);
43         SIVAL(req->out.body, 0x08, io->in.input_buffer_length);
44         SIVAL(req->out.body, 0x10, io->in.additional_information);
45         SIVAL(req->out.body, 0x14, io->in.getinfo_flags);
46         smb2_push_handle(req->out.body+0x18, &io->in.file.handle);
47
48         /* this blob is used for quota queries */
49         status = smb2_push_o32s32_blob(&req->out, 0x08, io->in.blob);
50         if (!NT_STATUS_IS_OK(status)) {
51                 talloc_free(req);
52                 return NULL;
53         }
54         smb2_transport_send(req);
55
56         return req;
57 }
58
59
60 /*
61   recv a getinfo reply
62 */
63 NTSTATUS smb2_getinfo_recv(struct smb2_request *req, TALLOC_CTX *mem_ctx,
64                            struct smb2_getinfo *io)
65 {
66         NTSTATUS status;
67
68         if (!smb2_request_receive(req) || 
69             smb2_request_is_error(req)) {
70                 return smb2_request_destroy(req);
71         }
72
73         SMB2_CHECK_PACKET_RECV(req, 0x08, true);
74
75         status = smb2_pull_o16s16_blob(&req->in, mem_ctx, req->in.body+0x02, &io->out.blob);
76         if (!NT_STATUS_IS_OK(status)) {
77                 return status;
78         }
79
80         return smb2_request_destroy(req);
81 }
82
83 /*
84   sync getinfo request
85 */
86 NTSTATUS smb2_getinfo(struct smb2_tree *tree, TALLOC_CTX *mem_ctx,
87                       struct smb2_getinfo *io)
88 {
89         struct smb2_request *req = smb2_getinfo_send(tree, io);
90         return smb2_getinfo_recv(req, mem_ctx, io);
91 }
92
93
94 /*
95   map a generic info level to a SMB2 info level
96 */
97 uint16_t smb2_getinfo_map_level(uint16_t level, uint8_t class)
98 {
99         if (class == SMB2_GETINFO_FILE && 
100             level == RAW_FILEINFO_SEC_DESC) {
101                 return SMB2_GETINFO_SECURITY;
102         }
103         if ((level & 0xFF) == class) {
104                 return level;
105         } else if (level > 1000) {
106                 return ((level-1000)<<8) | class;
107         }
108         DEBUG(0,("Unable to map SMB2 info level 0x%04x of class %d\n", level, class));
109         return 0;       
110 }
111
112 /*
113   level specific getinfo call - async send
114 */
115 struct smb2_request *smb2_getinfo_file_send(struct smb2_tree *tree, union smb_fileinfo *io)
116 {
117         struct smb2_getinfo b;
118         uint16_t smb2_level = smb2_getinfo_map_level(io->generic.level, SMB2_GETINFO_FILE);
119         
120         if (smb2_level == 0) {
121                 return NULL;
122         }
123
124         ZERO_STRUCT(b);
125         b.in.info_type            = smb2_level & 0xFF;
126         b.in.info_class           = smb2_level >> 8;
127         b.in.output_buffer_length = 0x10000;
128         b.in.input_buffer_length  = 0;
129         b.in.file.handle          = io->generic.in.file.handle;
130
131         if (io->generic.level == RAW_FILEINFO_SEC_DESC) {
132                 b.in.additional_information = io->query_secdesc.in.secinfo_flags;
133         }
134         if (io->generic.level == RAW_FILEINFO_SMB2_ALL_EAS) {
135                 b.in.getinfo_flags = io->all_eas.in.continue_flags;
136         }
137
138         return smb2_getinfo_send(tree, &b);
139 }
140
141 /*
142   recv a getinfo reply and parse the level info
143 */
144 NTSTATUS smb2_getinfo_file_recv(struct smb2_request *req, TALLOC_CTX *mem_ctx,
145                                 union smb_fileinfo *io)
146 {
147         struct smb2_getinfo b;
148         NTSTATUS status;
149
150         status = smb2_getinfo_recv(req, mem_ctx, &b);
151         NT_STATUS_NOT_OK_RETURN(status);
152
153         status = smb_raw_fileinfo_passthru_parse(&b.out.blob, mem_ctx, io->generic.level, io);
154         data_blob_free(&b.out.blob);
155
156         return status;
157 }
158
159 /*
160   level specific getinfo call
161 */
162 NTSTATUS smb2_getinfo_file(struct smb2_tree *tree, TALLOC_CTX *mem_ctx, 
163                            union smb_fileinfo *io)
164 {
165         struct smb2_request *req = smb2_getinfo_file_send(tree, io);
166         return smb2_getinfo_file_recv(req, mem_ctx, io);
167 }
168
169
170 /*
171   level specific getinfo call - async send
172 */
173 struct smb2_request *smb2_getinfo_fs_send(struct smb2_tree *tree, union smb_fsinfo *io)
174 {
175         struct smb2_getinfo b;
176         uint16_t smb2_level = smb2_getinfo_map_level(io->generic.level, SMB2_GETINFO_FS);
177         
178         if (smb2_level == 0) {
179                 return NULL;
180         }
181         
182         ZERO_STRUCT(b);
183         b.in.output_buffer_length = 0x10000;
184         b.in.file.handle          = io->generic.handle;
185         b.in.info_type            = smb2_level & 0xFF;
186         b.in.info_class           = smb2_level >> 8;
187
188         return smb2_getinfo_send(tree, &b);
189 }
190
191 /*
192   recv a getinfo reply and parse the level info
193 */
194 NTSTATUS smb2_getinfo_fs_recv(struct smb2_request *req, TALLOC_CTX *mem_ctx,
195                                 union smb_fsinfo *io)
196 {
197         struct smb2_getinfo b;
198         NTSTATUS status;
199
200         status = smb2_getinfo_recv(req, mem_ctx, &b);
201         NT_STATUS_NOT_OK_RETURN(status);
202
203         status = smb_raw_fsinfo_passthru_parse(b.out.blob, mem_ctx, io->generic.level, io);
204         data_blob_free(&b.out.blob);
205
206         return status;
207 }
208
209 /*
210   level specific getinfo call
211 */
212 NTSTATUS smb2_getinfo_fs(struct smb2_tree *tree, TALLOC_CTX *mem_ctx, 
213                            union smb_fsinfo *io)
214 {
215         struct smb2_request *req = smb2_getinfo_fs_send(tree, io);
216         return smb2_getinfo_fs_recv(req, mem_ctx, io);
217 }
218