s3-libnet: avoid using lp_dns_hostname() in join code
[samba4-gss.git] / libcli / smb / smb2cli_read.c
blob6efaa8da5ee1b1d840589980f2aca8ed70452d26
1 /*
2 Unix SMB/CIFS implementation.
3 smb2 lib
4 Copyright (C) Volker Lendecke 2011
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_read_state {
27 uint8_t fixed[48];
28 uint8_t dyn_pad[1];
29 struct iovec *recv_iov;
30 uint8_t *data;
31 uint32_t data_length;
32 bool out_valid;
33 struct tevent_req *subreq;
34 bool notify_async;
35 bool report_pending;
38 static bool smb2cli_read_cancel(struct tevent_req *req);
39 static void smb2cli_read_done(struct tevent_req *subreq);
41 struct tevent_req *smb2cli_read_send(TALLOC_CTX *mem_ctx,
42 struct tevent_context *ev,
43 struct smbXcli_conn *conn,
44 uint32_t timeout_msec,
45 struct smbXcli_session *session,
46 struct smbXcli_tcon *tcon,
47 uint32_t length,
48 uint64_t offset,
49 uint64_t fid_persistent,
50 uint64_t fid_volatile,
51 uint64_t minimum_count,
52 uint64_t remaining_bytes)
54 struct tevent_req *req, *subreq;
55 struct smb2cli_read_state *state;
56 uint8_t *fixed;
58 req = tevent_req_create(mem_ctx, &state,
59 struct smb2cli_read_state);
60 if (req == NULL) {
61 return NULL;
64 fixed = state->fixed;
66 SSVAL(fixed, 0, 49);
67 SIVAL(fixed, 4, length);
68 SBVAL(fixed, 8, offset);
69 SBVAL(fixed, 16, fid_persistent);
70 SBVAL(fixed, 24, fid_volatile);
71 SBVAL(fixed, 32, minimum_count);
72 SBVAL(fixed, 40, remaining_bytes);
74 subreq = smb2cli_req_send(state, ev, conn, SMB2_OP_READ,
75 0, 0, /* flags */
76 timeout_msec,
77 tcon,
78 session,
79 state->fixed, sizeof(state->fixed),
80 state->dyn_pad, sizeof(state->dyn_pad),
81 length); /* max_dyn_len */
82 if (tevent_req_nomem(subreq, req)) {
83 return tevent_req_post(req, ev);
85 tevent_req_set_callback(subreq, smb2cli_read_done, req);
86 state->subreq = subreq;
87 tevent_req_set_cancel_fn(req, smb2cli_read_cancel);
89 return req;
92 static bool smb2cli_read_cancel(struct tevent_req *req)
94 struct smb2cli_read_state *state = tevent_req_data(
95 req, struct smb2cli_read_state);
96 bool ok;
98 ok = tevent_req_cancel(state->subreq);
99 return ok;
102 void smb2cli_read_set_notify_async(struct tevent_req *req)
104 struct smb2cli_read_state *state =
105 tevent_req_data(req,
106 struct smb2cli_read_state);
108 smb2cli_req_set_notify_async(state->subreq);
109 state->notify_async = true;
112 static void smb2cli_read_done(struct tevent_req *subreq)
114 struct tevent_req *req = tevent_req_callback_data(
115 subreq, struct tevent_req);
116 struct smb2cli_read_state *state =
117 tevent_req_data(req,
118 struct smb2cli_read_state);
119 NTSTATUS status;
120 NTSTATUS error;
121 struct iovec *iov;
122 const uint8_t dyn_ofs = SMB2_HDR_BODY + 0x10;
123 DATA_BLOB dyn_buffer = data_blob_null;
124 uint8_t data_offset;
125 DATA_BLOB data_buffer = data_blob_null;
126 uint32_t next_offset = 0; /* this variable is completely ignored */
127 static const struct smb2cli_req_expected_response expected[] = {
129 .status = STATUS_BUFFER_OVERFLOW,
130 .body_size = 0x11
133 .status = NT_STATUS_OK,
134 .body_size = 0x11
138 SMB_ASSERT(state->subreq == subreq);
140 status = smb2cli_req_recv(subreq, state, &iov,
141 expected, ARRAY_SIZE(expected));
142 if (NT_STATUS_EQUAL(status, NT_STATUS_PENDING) && state->notify_async) {
143 state->notify_async = false;
144 state->report_pending = true;
145 tevent_req_notify_callback(req);
146 return;
148 state->notify_async = false;
149 state->report_pending = false;
150 state->subreq = NULL;
151 TALLOC_FREE(subreq);
152 if (NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
153 /* no error */
154 } else {
155 if (tevent_req_nterror(req, status)) {
156 return;
160 data_offset = CVAL(iov[1].iov_base, 2);
161 state->data_length = IVAL(iov[1].iov_base, 4);
163 dyn_buffer = data_blob_const((uint8_t *)iov[2].iov_base,
164 iov[2].iov_len);
166 error = smb2cli_parse_dyn_buffer(dyn_ofs,
167 dyn_buffer,
168 dyn_ofs, /* min_offset */
169 data_offset,
170 state->data_length,
171 dyn_buffer.length, /* max_length */
172 &next_offset,
173 &data_buffer);
174 if (tevent_req_nterror(req, error)) {
175 return;
178 state->recv_iov = iov;
179 state->data = data_buffer.data;
181 state->out_valid = true;
183 if (tevent_req_nterror(req, status)) {
184 return;
187 tevent_req_done(req);
190 NTSTATUS smb2cli_read_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
191 uint8_t **data, uint32_t *data_length)
193 struct smb2cli_read_state *state =
194 tevent_req_data(req,
195 struct smb2cli_read_state);
196 NTSTATUS status = NT_STATUS_OK;
198 if (state->report_pending) {
199 *data_length = 0;
200 *data = NULL;
201 return NT_STATUS_PENDING;
204 if (tevent_req_is_nterror(req, &status) && !state->out_valid) {
205 *data_length = 0;
206 *data = NULL;
207 tevent_req_received(req);
208 return status;
210 talloc_steal(mem_ctx, state->recv_iov);
211 *data_length = state->data_length;
212 *data = state->data;
213 tevent_req_received(req);
214 return status;
217 NTSTATUS smb2cli_read(struct smbXcli_conn *conn,
218 uint32_t timeout_msec,
219 struct smbXcli_session *session,
220 struct smbXcli_tcon *tcon,
221 uint32_t length,
222 uint64_t offset,
223 uint64_t fid_persistent,
224 uint64_t fid_volatile,
225 uint64_t minimum_count,
226 uint64_t remaining_bytes,
227 TALLOC_CTX *mem_ctx,
228 uint8_t **data,
229 uint32_t *data_length)
231 TALLOC_CTX *frame = talloc_stackframe();
232 struct tevent_context *ev;
233 struct tevent_req *req;
234 NTSTATUS status = NT_STATUS_NO_MEMORY;
236 if (smbXcli_conn_has_async_calls(conn)) {
238 * Can't use sync call while an async call is in flight
240 status = NT_STATUS_INVALID_PARAMETER;
241 goto fail;
243 ev = samba_tevent_context_init(frame);
244 if (ev == NULL) {
245 goto fail;
247 req = smb2cli_read_send(frame, ev,
248 conn, timeout_msec, session, tcon,
249 length, offset,
250 fid_persistent, fid_volatile,
251 minimum_count, remaining_bytes);
252 if (req == NULL) {
253 goto fail;
255 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
256 goto fail;
258 status = smb2cli_read_recv(req, mem_ctx, data, data_length);
259 fail:
260 TALLOC_FREE(frame);
261 return status;