a9a681ea53edaea2cf524883777c8679c15e0210
[kai/samba-autobuild/.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
34         req = smb2_request_init_tree(tree, SMB2_OP_GETINFO, 0x28, False, 0);
35         if (req == NULL) return NULL;
36
37         /* this seems to be a bug, they use 0x29 but only send 0x28 bytes */
38         SSVAL(req->out.body, 0x00, 0x29);
39
40         SSVAL(req->out.body, 0x02, io->in.level);
41         SIVAL(req->out.body, 0x04, io->in.max_response_size);
42         SIVAL(req->out.body, 0x08, io->in.unknown1);
43         SIVAL(req->out.body, 0x0C, io->in.unknown2);
44         SIVAL(req->out.body, 0x10, io->in.flags);
45         SIVAL(req->out.body, 0x14, io->in.flags2);
46         smb2_push_handle(req->out.body+0x18, &io->in.file.handle);
47
48         smb2_transport_send(req);
49
50         return req;
51 }
52
53
54 /*
55   recv a getinfo reply
56 */
57 NTSTATUS smb2_getinfo_recv(struct smb2_request *req, TALLOC_CTX *mem_ctx,
58                            struct smb2_getinfo *io)
59 {
60         NTSTATUS status;
61
62         if (!smb2_request_receive(req) || 
63             smb2_request_is_error(req)) {
64                 return smb2_request_destroy(req);
65         }
66
67         SMB2_CHECK_PACKET_RECV(req, 0x08, True);
68
69         status = smb2_pull_o16s16_blob(&req->in, mem_ctx, req->in.body+0x02, &io->out.blob);
70         if (!NT_STATUS_IS_OK(status)) {
71                 return status;
72         }
73
74         return smb2_request_destroy(req);
75 }
76
77 /*
78   sync getinfo request
79 */
80 NTSTATUS smb2_getinfo(struct smb2_tree *tree, TALLOC_CTX *mem_ctx,
81                       struct smb2_getinfo *io)
82 {
83         struct smb2_request *req = smb2_getinfo_send(tree, io);
84         return smb2_getinfo_recv(req, mem_ctx, io);
85 }
86
87
88 /*
89   map a generic info level to a SMB2 info level
90 */
91 uint16_t smb2_getinfo_map_level(uint16_t level, uint8_t class)
92 {
93         if (class == SMB2_GETINFO_FILE && 
94             level == RAW_FILEINFO_SEC_DESC) {
95                 return SMB2_GETINFO_SECURITY;
96         }
97         if ((level & 0xFF) == class) {
98                 return level;
99         } else if (level > 1000) {
100                 return ((level-1000)<<8) | class;
101         }
102         DEBUG(0,("Unable to map SMB2 info level 0x%04x of class %d\n", level, class));
103         return 0;       
104 }
105
106 /*
107   level specific getinfo call - async send
108 */
109 struct smb2_request *smb2_getinfo_file_send(struct smb2_tree *tree, union smb_fileinfo *io)
110 {
111         struct smb2_getinfo b;
112         uint16_t smb2_level = smb2_getinfo_map_level(io->generic.level, SMB2_GETINFO_FILE);
113         
114         if (smb2_level == 0) {
115                 return NULL;
116         }
117
118         ZERO_STRUCT(b);
119         b.in.max_response_size = 0x10000;
120         b.in.file.handle       = io->generic.in.file.handle;
121         b.in.level             = smb2_level;
122
123         if (io->generic.level == RAW_FILEINFO_SEC_DESC) {
124                 b.in.flags = io->query_secdesc.in.secinfo_flags;
125         }
126         if (io->generic.level == RAW_FILEINFO_SMB2_ALL_EAS) {
127                 b.in.flags2 = io->all_eas.in.continue_flags;
128         }
129
130         return smb2_getinfo_send(tree, &b);
131 }
132
133 /*
134   recv a getinfo reply and parse the level info
135 */
136 NTSTATUS smb2_getinfo_file_recv(struct smb2_request *req, TALLOC_CTX *mem_ctx,
137                                 union smb_fileinfo *io)
138 {
139         struct smb2_getinfo b;
140         NTSTATUS status;
141
142         status = smb2_getinfo_recv(req, mem_ctx, &b);
143         NT_STATUS_NOT_OK_RETURN(status);
144
145         status = smb_raw_fileinfo_passthru_parse(&b.out.blob, mem_ctx, io->generic.level, io);
146         data_blob_free(&b.out.blob);
147
148         return status;
149 }
150
151 /*
152   level specific getinfo call
153 */
154 NTSTATUS smb2_getinfo_file(struct smb2_tree *tree, TALLOC_CTX *mem_ctx, 
155                            union smb_fileinfo *io)
156 {
157         struct smb2_request *req = smb2_getinfo_file_send(tree, io);
158         return smb2_getinfo_file_recv(req, mem_ctx, io);
159 }
160
161
162 /*
163   level specific getinfo call - async send
164 */
165 struct smb2_request *smb2_getinfo_fs_send(struct smb2_tree *tree, union smb_fsinfo *io)
166 {
167         struct smb2_getinfo b;
168         uint16_t smb2_level = smb2_getinfo_map_level(io->generic.level, SMB2_GETINFO_FS);
169         
170         if (smb2_level == 0) {
171                 return NULL;
172         }
173         
174         ZERO_STRUCT(b);
175         b.in.max_response_size = 0x10000;
176         b.in.file.handle       = io->generic.handle;
177         b.in.level             = smb2_level;
178
179         return smb2_getinfo_send(tree, &b);
180 }
181
182 /*
183   recv a getinfo reply and parse the level info
184 */
185 NTSTATUS smb2_getinfo_fs_recv(struct smb2_request *req, TALLOC_CTX *mem_ctx,
186                                 union smb_fsinfo *io)
187 {
188         struct smb2_getinfo b;
189         NTSTATUS status;
190
191         status = smb2_getinfo_recv(req, mem_ctx, &b);
192         NT_STATUS_NOT_OK_RETURN(status);
193
194         status = smb_raw_fsinfo_passthru_parse(b.out.blob, mem_ctx, io->generic.level, io);
195         data_blob_free(&b.out.blob);
196
197         return status;
198 }
199
200 /*
201   level specific getinfo call
202 */
203 NTSTATUS smb2_getinfo_fs(struct smb2_tree *tree, TALLOC_CTX *mem_ctx, 
204                            union smb_fsinfo *io)
205 {
206         struct smb2_request *req = smb2_getinfo_fs_send(tree, io);
207         return smb2_getinfo_fs_recv(req, mem_ctx, io);
208 }
209