s3:lib/events: make use of tevent_common_loop_timer_delay()
[Samba/gebeck_regimport.git] / libcli / smb / smb2cli_query_info.c
blob9ec16b5c990b66c317dccc4675b5b2f9bb675790
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;
59 req = tevent_req_create(mem_ctx, &state,
60 struct smb2cli_query_info_state);
61 if (req == NULL) {
62 return NULL;
64 state->max_output_length = in_max_output_length;
66 if (in_input_buffer) {
67 input_buffer_offset = SMB2_HDR_BODY+0x28;
68 input_buffer_length = in_input_buffer->length;
71 fixed = state->fixed;
73 SSVAL(fixed, 0x00, 0x29);
74 SCVAL(fixed, 0x02, in_info_type);
75 SCVAL(fixed, 0x03, in_file_info_class); /* reserved */
76 SIVAL(fixed, 0x04, in_max_output_length);
77 SSVAL(fixed, 0x08, input_buffer_offset);
78 SSVAL(fixed, 0x0A, 0); /* reserved */
79 SIVAL(fixed, 0x0C, input_buffer_length);
80 SIVAL(fixed, 0x10, in_additional_info);
81 SIVAL(fixed, 0x14, in_flags);
82 SBVAL(fixed, 0x18, in_fid_persistent);
83 SBVAL(fixed, 0x20, in_fid_volatile);
85 if (input_buffer_length > 0) {
86 dyn = in_input_buffer->data;
87 dyn_len = in_input_buffer->length;
88 } else {
89 dyn = state->dyn_pad;
90 dyn_len = sizeof(state->dyn_pad);
93 subreq = smb2cli_req_send(state, ev, conn, SMB2_OP_GETINFO,
94 0, 0, /* flags */
95 timeout_msec,
96 tcon,
97 session,
98 state->fixed, sizeof(state->fixed),
99 dyn, dyn_len);
100 if (tevent_req_nomem(subreq, req)) {
101 return tevent_req_post(req, ev);
103 tevent_req_set_callback(subreq, smb2cli_query_info_done, req);
104 return req;
107 static void smb2cli_query_info_done(struct tevent_req *subreq)
109 struct tevent_req *req =
110 tevent_req_callback_data(subreq,
111 struct tevent_req);
112 struct smb2cli_query_info_state *state =
113 tevent_req_data(req,
114 struct smb2cli_query_info_state);
115 NTSTATUS status;
116 struct iovec *iov;
117 uint8_t *fixed;
118 uint8_t *dyn;
119 size_t dyn_len;
120 uint32_t dyn_ofs = SMB2_HDR_BODY + 0x08;
121 uint32_t output_buffer_offset;
122 uint32_t output_buffer_length;
123 static const struct smb2cli_req_expected_response expected[] = {
125 .status = NT_STATUS_OK,
126 .body_size = 0x09
129 .status = STATUS_BUFFER_OVERFLOW,
130 .body_size = 0x09
134 status = smb2cli_req_recv(subreq, state, &iov,
135 expected, ARRAY_SIZE(expected));
136 TALLOC_FREE(subreq);
137 if (tevent_req_nterror(req, status)) {
138 return;
141 state->recv_iov = iov;
142 fixed = (uint8_t *)iov[1].iov_base;
143 dyn = (uint8_t *)iov[2].iov_base;
144 dyn_len = iov[2].iov_len;
146 output_buffer_offset = SVAL(fixed, 0x02);
147 output_buffer_length = IVAL(fixed, 0x04);
149 if ((output_buffer_offset > 0) && (output_buffer_length > 0)) {
150 if (output_buffer_offset != dyn_ofs) {
151 tevent_req_nterror(
152 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
153 return;
156 if (output_buffer_length < dyn_len) {
157 tevent_req_nterror(
158 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
159 return;
162 if (output_buffer_length > state->max_output_length) {
163 tevent_req_nterror(
164 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
165 return;
168 state->out_output_buffer.data = dyn;
169 state->out_output_buffer.length = output_buffer_length;
172 tevent_req_done(req);
175 NTSTATUS smb2cli_query_info_recv(struct tevent_req *req,
176 TALLOC_CTX *mem_ctx,
177 DATA_BLOB *out_output_buffer)
179 struct smb2cli_query_info_state *state =
180 tevent_req_data(req,
181 struct smb2cli_query_info_state);
182 NTSTATUS status;
184 if (tevent_req_is_nterror(req, &status)) {
185 tevent_req_received(req);
186 return status;
189 talloc_steal(mem_ctx, state->recv_iov);
190 if (out_output_buffer) {
191 *out_output_buffer = state->out_output_buffer;
194 tevent_req_received(req);
195 return NT_STATUS_OK;
198 NTSTATUS smb2cli_query_info(struct smbXcli_conn *conn,
199 uint32_t timeout_msec,
200 struct smbXcli_session *session,
201 struct smbXcli_tcon *tcon,
202 uint8_t in_info_type,
203 uint8_t in_file_info_class,
204 uint32_t in_max_output_length,
205 const DATA_BLOB *in_input_buffer,
206 uint32_t in_additional_info,
207 uint32_t in_flags,
208 uint64_t in_fid_persistent,
209 uint64_t in_fid_volatile,
210 TALLOC_CTX *mem_ctx,
211 DATA_BLOB *out_output_buffer)
213 TALLOC_CTX *frame = talloc_stackframe();
214 struct tevent_context *ev;
215 struct tevent_req *req;
216 NTSTATUS status = NT_STATUS_NO_MEMORY;
218 if (smbXcli_conn_has_async_calls(conn)) {
220 * Can't use sync call while an async call is in flight
222 status = NT_STATUS_INVALID_PARAMETER_MIX;
223 goto fail;
225 ev = samba_tevent_context_init(frame);
226 if (ev == NULL) {
227 goto fail;
229 req = smb2cli_query_info_send(frame, ev,
230 conn, timeout_msec,
231 session, tcon,
232 in_info_type,
233 in_file_info_class,
234 in_max_output_length,
235 in_input_buffer,
236 in_additional_info,
237 in_flags,
238 in_fid_persistent,
239 in_fid_volatile);
240 if (req == NULL) {
241 goto fail;
243 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
244 goto fail;
246 status = smb2cli_query_info_recv(req, mem_ctx,
247 out_output_buffer);
248 fail:
249 TALLOC_FREE(frame);
250 return status;