winreg: Use the ntstatus return code for client side errors
[Samba/gebeck_regimport.git] / source3 / libsmb / smb2cli_read.c
blob79224e517a45a72fb4c0b596481bdbc958c515ca
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 "client.h"
22 #include "async_smb.h"
23 #include "smb2cli_base.h"
24 #include "smb2cli.h"
25 #include "libsmb/proto.h"
26 #include "lib/util/tevent_ntstatus.h"
28 struct smb2cli_read_state {
29 uint8_t fixed[48];
30 uint8_t *inbuf;
31 uint8_t *data;
32 uint32_t data_length;
35 static void smb2cli_read_done(struct tevent_req *subreq);
37 struct tevent_req *smb2cli_read_send(TALLOC_CTX *mem_ctx,
38 struct tevent_context *ev,
39 struct cli_state *cli,
40 uint32_t length,
41 uint64_t offset,
42 uint64_t fid_persistent,
43 uint64_t fid_volatile,
44 uint64_t minimum_count,
45 uint64_t remaining_bytes)
47 struct tevent_req *req, *subreq;
48 struct smb2cli_read_state *state;
49 uint8_t *fixed;
51 req = tevent_req_create(mem_ctx, &state,
52 struct smb2cli_read_state);
53 if (req == NULL) {
54 return NULL;
57 fixed = state->fixed;
59 SSVAL(fixed, 0, 49);
60 SIVAL(fixed, 4, length);
61 SBVAL(fixed, 8, offset);
62 SBVAL(fixed, 16, fid_persistent);
63 SBVAL(fixed, 24, fid_volatile);
64 SBVAL(fixed, 32, minimum_count);
65 SBVAL(fixed, 40, remaining_bytes);
67 subreq = smb2cli_req_send(state, ev, cli, SMB2_OP_READ, 0,
68 state->fixed, sizeof(state->fixed),
69 NULL, 0);
70 if (tevent_req_nomem(subreq, req)) {
71 return tevent_req_post(req, ev);
73 tevent_req_set_callback(subreq, smb2cli_read_done, req);
74 return req;
77 static void smb2cli_read_done(struct tevent_req *subreq)
79 struct tevent_req *req = tevent_req_callback_data(
80 subreq, struct tevent_req);
81 struct smb2cli_read_state *state =
82 tevent_req_data(req,
83 struct smb2cli_read_state);
84 NTSTATUS status;
85 struct iovec *iov;
86 uint8_t data_offset;
88 status = smb2cli_req_recv(subreq, talloc_tos(), &iov, 17);
89 if (tevent_req_nterror(req, status)) {
90 return;
93 data_offset = CVAL(iov[1].iov_base, 2);
94 state->data_length = IVAL(iov[1].iov_base, 4);
96 if ((data_offset != SMB2_HDR_BODY + 16) ||
97 (state->data_length > iov[2].iov_len)) {
98 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
99 return;
101 state->inbuf = smb2cli_req_inbuf(subreq);
102 state->data = (uint8_t *)iov[2].iov_base;
103 tevent_req_done(req);
106 NTSTATUS smb2cli_read_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
107 uint8_t **data, uint32_t *data_length)
109 struct smb2cli_read_state *state =
110 tevent_req_data(req,
111 struct smb2cli_read_state);
112 NTSTATUS status;
114 if (tevent_req_is_nterror(req, &status)) {
115 return status;
117 talloc_steal(mem_ctx, state->inbuf);
118 *data_length = state->data_length;
119 *data = state->data;
120 return NT_STATUS_OK;
123 NTSTATUS smb2cli_read(struct cli_state *cli,
124 uint32_t length,
125 uint64_t offset,
126 uint64_t fid_persistent,
127 uint64_t fid_volatile,
128 uint64_t minimum_count,
129 uint64_t remaining_bytes,
130 TALLOC_CTX *mem_ctx,
131 uint8_t **data,
132 uint32_t *data_length)
134 TALLOC_CTX *frame = talloc_stackframe();
135 struct event_context *ev;
136 struct tevent_req *req;
137 NTSTATUS status = NT_STATUS_NO_MEMORY;
139 if (cli_has_async_calls(cli)) {
141 * Can't use sync call while an async call is in flight
143 status = NT_STATUS_INVALID_PARAMETER;
144 goto fail;
146 ev = event_context_init(frame);
147 if (ev == NULL) {
148 goto fail;
150 req = smb2cli_read_send(frame, ev, cli, length, offset,
151 fid_persistent, fid_volatile,
152 minimum_count, remaining_bytes);
153 if (req == NULL) {
154 goto fail;
156 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
157 goto fail;
159 status = smb2cli_read_recv(req, mem_ctx, data, data_length);
160 fail:
161 TALLOC_FREE(frame);
162 return status;