2 Unix SMB/CIFS implementation.
3 Infrastructure for async SMB client requests
4 Copyright (C) Volker Lendecke 2008
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/>.
21 #include "libsmb/libsmb.h"
22 #include "../lib/util/tevent_ntstatus.h"
23 #include "async_smb.h"
24 #include "../libcli/smb/smbXcli_base.h"
26 struct cli_smb_req_state
{
27 struct cli_state
*cli
;
29 struct tevent_req
*req
;
30 struct cli_smb_req_state
**ptr
;
33 static int cli_smb_req_state_destructor(struct cli_smb_req_state
*state
)
35 talloc_set_destructor(state
->ptr
, NULL
);
36 talloc_free(state
->ptr
);
40 static int cli_smb_req_state_ptr_destructor(struct cli_smb_req_state
**ptr
)
42 struct cli_smb_req_state
*state
= *ptr
;
43 void *parent
= talloc_parent(state
);
45 talloc_set_destructor(state
, NULL
);
47 talloc_reparent(state
, parent
, state
->req
);
52 struct tevent_req
*cli_smb_req_create(TALLOC_CTX
*mem_ctx
,
53 struct tevent_context
*ev
,
54 struct cli_state
*cli
,
56 uint8_t additional_flags
,
57 uint16_t additional_flags2
,
58 uint8_t wct
, uint16_t *vwv
,
60 struct iovec
*bytes_iov
)
62 struct cli_smb_req_state
*state
;
63 uint8_t clear_flags
= 0;
64 uint16_t clear_flags2
= 0;
66 state
= talloc_zero(mem_ctx
, struct cli_smb_req_state
);
71 state
->smb_command
= smb_command
;
72 state
->ptr
= talloc(state
, struct cli_smb_req_state
*);
73 if (state
->ptr
== NULL
) {
79 state
->req
= smb1cli_req_create(state
, ev
, cli
->conn
, smb_command
,
80 additional_flags
, clear_flags
,
81 additional_flags2
, clear_flags2
,
86 wct
, vwv
, iov_count
, bytes_iov
);
87 if (state
->req
== NULL
) {
92 talloc_reparent(state
, state
->req
, state
->ptr
);
93 talloc_set_destructor(state
, cli_smb_req_state_destructor
);
94 talloc_set_destructor(state
->ptr
, cli_smb_req_state_ptr_destructor
);
99 struct tevent_req
*cli_smb_send(TALLOC_CTX
*mem_ctx
,
100 struct tevent_context
*ev
,
101 struct cli_state
*cli
,
103 uint8_t additional_flags
,
104 uint16_t additional_flags2
,
105 uint8_t wct
, uint16_t *vwv
,
107 const uint8_t *bytes
)
109 struct cli_smb_req_state
*state
;
110 uint8_t clear_flags
= 0;
111 uint16_t clear_flags2
= 0;
113 state
= talloc_zero(mem_ctx
, struct cli_smb_req_state
);
118 state
->smb_command
= smb_command
;
119 state
->ptr
= talloc(state
, struct cli_smb_req_state
*);
120 if (state
->ptr
== NULL
) {
126 state
->req
= smb1cli_req_send(state
, ev
, cli
->conn
, smb_command
,
127 additional_flags
, clear_flags
,
128 additional_flags2
, clear_flags2
,
133 wct
, vwv
, num_bytes
, bytes
);
134 if (state
->req
== NULL
) {
139 talloc_reparent(state
, state
->req
, state
->ptr
);
140 talloc_set_destructor(state
, cli_smb_req_state_destructor
);
141 talloc_set_destructor(state
->ptr
, cli_smb_req_state_ptr_destructor
);
146 NTSTATUS
cli_smb_recv(struct tevent_req
*req
,
147 TALLOC_CTX
*mem_ctx
, uint8_t **pinbuf
,
148 uint8_t min_wct
, uint8_t *pwct
, uint16_t **pvwv
,
149 uint32_t *pnum_bytes
, uint8_t **pbytes
)
152 void *parent
= talloc_parent(req
);
153 struct cli_smb_req_state
*state
=
154 talloc_get_type(parent
,
155 struct cli_smb_req_state
);
156 struct iovec
*recv_iov
= NULL
;
158 uint16_t *vwv
= NULL
;
160 uint8_t *bytes
= NULL
;
162 bool is_expected
= false;
163 bool map_dos_errors
= true;
165 if (pinbuf
!= NULL
) {
174 if (pnum_bytes
!= NULL
) {
177 if (pbytes
!= NULL
) {
181 status
= smb1cli_req_recv(req
, req
,
186 NULL
, /* pvwv_offset */
189 NULL
, /* pbytes_offset */
191 NULL
, 0); /* expected */
194 if ((state
->smb_command
== SMBsesssetupX
) &&
195 NT_STATUS_EQUAL(status
,
196 NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
198 * NT_STATUS_MORE_PROCESSING_REQUIRED is a
199 * valid return code for session setup
204 if ((state
->smb_command
== SMBntcreateX
) &&
205 NT_STATUS_EQUAL(status
, NT_STATUS_STOPPED_ON_SYMLINK
)) {
209 map_dos_errors
= state
->cli
->map_dos_errors
;
210 state
->cli
->raw_status
= status
;
211 talloc_free(state
->ptr
);
215 if (NT_STATUS_IS_DOS(status
) && map_dos_errors
) {
216 uint8_t eclass
= NT_STATUS_DOS_CLASS(status
);
217 uint16_t ecode
= NT_STATUS_DOS_CODE(status
);
219 * TODO: is it really a good idea to do a mapping here?
221 * The old cli_pull_error() also does it, so I do not change
224 status
= dos_to_ntstatus(eclass
, ecode
);
227 if (!NT_STATUS_IS_ERR(status
)) {
232 TALLOC_FREE(recv_iov
);
237 TALLOC_FREE(recv_iov
);
238 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
247 if (pnum_bytes
!= NULL
) {
248 *pnum_bytes
= num_bytes
;
250 if (pbytes
!= NULL
) {
254 if (pinbuf
!= NULL
&& mem_ctx
!= NULL
) {
255 if (talloc_reference_count(inbuf
) == 0) {
256 *pinbuf
= talloc_move(mem_ctx
, &inbuf
);
257 TALLOC_FREE(recv_iov
);
261 } else if (mem_ctx
!= NULL
) {
262 if (talloc_reference_count(inbuf
) == 0) {
263 (void)talloc_move(mem_ctx
, &inbuf
);
264 TALLOC_FREE(recv_iov
);