libcli/smb: pass smbXcli_tcon to smb2cli_query_info*()
[Samba/gebeck_regimport.git] / libcli / smb / smb2cli_query_info.c
blobb5d9e791aa1abefb07bd2bccf58ca99648faf268
1 /*
2 Unix SMB/CIFS implementation.
3 smb2 lib
4 Copyright (C) Stefan Metzmacher 2012
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.
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.
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/>.
20 #include "includes.h"
21 #include "system/network.h"
22 #include "lib/util/tevent_ntstatus.h"
23 #include "smb_common.h"
24 #include "smbXcli_base.h"
26 struct smb2cli_query_info_state {
27 uint8_t fixed[0x28];
28 uint8_t dyn_pad[1];
29 uint32_t max_output_length;
30 struct iovec *recv_iov;
31 DATA_BLOB out_output_buffer;
34 static void smb2cli_query_info_done(struct tevent_req *subreq);
36 struct tevent_req *smb2cli_query_info_send(TALLOC_CTX *mem_ctx,
37 struct tevent_context *ev,
38 struct smbXcli_conn *conn,
39 uint32_t timeout_msec,
40 struct smbXcli_session *session,
41 struct smbXcli_tcon *tcon,
42 uint8_t in_info_type,
43 uint8_t in_file_info_class,
44 uint32_t in_max_output_length,
45 const DATA_BLOB *in_input_buffer,
46 uint32_t in_additional_info,
47 uint32_t in_flags,
48 uint64_t in_fid_persistent,
49 uint64_t in_fid_volatile)
51 struct tevent_req *req, *subreq;
52 struct smb2cli_query_info_state *state;
53 uint8_t *fixed;
54 uint8_t *dyn;
55 size_t dyn_len;
56 uint16_t input_buffer_offset = 0;
57 uint32_t input_buffer_length = 0;
58 uint32_t tcon_id = 0;
60 req = tevent_req_create(mem_ctx, &state,
61 struct smb2cli_query_info_state);
62 if (req == NULL) {
63 return NULL;
65 state->max_output_length = in_max_output_length;
67 if (in_input_buffer) {
68 input_buffer_offset = SMB2_HDR_BODY+0x28;
69 input_buffer_length = in_input_buffer->length;
72 fixed = state->fixed;
74 SSVAL(fixed, 0x00, 0x29);
75 SCVAL(fixed, 0x02, in_info_type);
76 SCVAL(fixed, 0x03, in_file_info_class); /* reserved */
77 SIVAL(fixed, 0x04, in_max_output_length);
78 SSVAL(fixed, 0x08, input_buffer_offset);
79 SSVAL(fixed, 0x0A, 0); /* reserved */
80 SIVAL(fixed, 0x0C, input_buffer_length);
81 SIVAL(fixed, 0x10, in_additional_info);
82 SIVAL(fixed, 0x14, in_flags);
83 SBVAL(fixed, 0x18, in_fid_persistent);
84 SBVAL(fixed, 0x20, in_fid_volatile);
86 if (input_buffer_length > 0) {
87 dyn = in_input_buffer->data;
88 dyn_len = in_input_buffer->length;
89 } else {
90 dyn = state->dyn_pad;
91 dyn_len = sizeof(state->dyn_pad);
94 if (tcon) {
95 tcon_id = smb2cli_tcon_current_id(tcon);
98 subreq = smb2cli_req_send(state, ev, conn, SMB2_OP_GETINFO,
99 0, 0, /* flags */
100 timeout_msec,
101 0xFEFF, /* pid */
102 tcon_id,
103 session,
104 state->fixed, sizeof(state->fixed),
105 dyn, dyn_len);
106 if (tevent_req_nomem(subreq, req)) {
107 return tevent_req_post(req, ev);
109 tevent_req_set_callback(subreq, smb2cli_query_info_done, req);
110 return req;
113 static void smb2cli_query_info_done(struct tevent_req *subreq)
115 struct tevent_req *req =
116 tevent_req_callback_data(subreq,
117 struct tevent_req);
118 struct smb2cli_query_info_state *state =
119 tevent_req_data(req,
120 struct smb2cli_query_info_state);
121 NTSTATUS status;
122 struct iovec *iov;
123 uint8_t *fixed;
124 uint8_t *dyn;
125 size_t dyn_len;
126 uint32_t dyn_ofs = SMB2_HDR_BODY + 0x08;
127 uint32_t output_buffer_offset;
128 uint32_t output_buffer_length;
129 static const struct smb2cli_req_expected_response expected[] = {
131 .status = NT_STATUS_OK,
132 .body_size = 0x09
135 .status = STATUS_BUFFER_OVERFLOW,
136 .body_size = 0x09
140 status = smb2cli_req_recv(subreq, state, &iov,
141 expected, ARRAY_SIZE(expected));
142 if (tevent_req_nterror(req, status)) {
143 return;
146 state->recv_iov = iov;
147 fixed = (uint8_t *)iov[1].iov_base;
148 dyn = (uint8_t *)iov[2].iov_base;
149 dyn_len = iov[2].iov_len;
151 output_buffer_offset = SVAL(fixed, 0x02);
152 output_buffer_length = IVAL(fixed, 0x04);
154 if ((output_buffer_offset > 0) && (output_buffer_length > 0)) {
155 if (output_buffer_offset != dyn_ofs) {
156 tevent_req_nterror(
157 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
158 return;
161 if (output_buffer_length < dyn_len) {
162 tevent_req_nterror(
163 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
164 return;
167 if (output_buffer_length > state->max_output_length) {
168 tevent_req_nterror(
169 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
170 return;
173 state->out_output_buffer.data = dyn;
174 state->out_output_buffer.length = output_buffer_length;
177 tevent_req_done(req);
180 NTSTATUS smb2cli_query_info_recv(struct tevent_req *req,
181 TALLOC_CTX *mem_ctx,
182 DATA_BLOB *out_output_buffer)
184 struct smb2cli_query_info_state *state =
185 tevent_req_data(req,
186 struct smb2cli_query_info_state);
187 NTSTATUS status;
189 if (tevent_req_is_nterror(req, &status)) {
190 tevent_req_received(req);
191 return status;
194 talloc_steal(mem_ctx, state->recv_iov);
195 if (out_output_buffer) {
196 *out_output_buffer = state->out_output_buffer;
199 tevent_req_received(req);
200 return NT_STATUS_OK;
203 NTSTATUS smb2cli_query_info(struct smbXcli_conn *conn,
204 uint32_t timeout_msec,
205 struct smbXcli_session *session,
206 struct smbXcli_tcon *tcon,
207 uint8_t in_info_type,
208 uint8_t in_file_info_class,
209 uint32_t in_max_output_length,
210 const DATA_BLOB *in_input_buffer,
211 uint32_t in_additional_info,
212 uint32_t in_flags,
213 uint64_t in_fid_persistent,
214 uint64_t in_fid_volatile,
215 TALLOC_CTX *mem_ctx,
216 DATA_BLOB *out_output_buffer)
218 TALLOC_CTX *frame = talloc_stackframe();
219 struct tevent_context *ev;
220 struct tevent_req *req;
221 NTSTATUS status = NT_STATUS_NO_MEMORY;
223 if (smbXcli_conn_has_async_calls(conn)) {
225 * Can't use sync call while an async call is in flight
227 status = NT_STATUS_INVALID_PARAMETER_MIX;
228 goto fail;
230 ev = tevent_context_init(frame);
231 if (ev == NULL) {
232 goto fail;
234 req = smb2cli_query_info_send(frame, ev,
235 conn, timeout_msec,
236 session, tcon,
237 in_info_type,
238 in_file_info_class,
239 in_max_output_length,
240 in_input_buffer,
241 in_additional_info,
242 in_flags,
243 in_fid_persistent,
244 in_fid_volatile);
245 if (req == NULL) {
246 goto fail;
248 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
249 goto fail;
251 status = smb2cli_query_info_recv(req, mem_ctx,
252 out_output_buffer);
253 fail:
254 TALLOC_FREE(frame);
255 return status;