2 Unix SMB/CIFS implementation.
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/>.
22 #include "async_smb.h"
23 #include "smb2cli_base.h"
25 #include "libsmb/proto.h"
26 #include "lib/util/tevent_ntstatus.h"
28 struct smb2cli_query_directory_state
{
30 struct iovec
*recv_iov
;
35 static void smb2cli_query_directory_done(struct tevent_req
*subreq
);
37 struct tevent_req
*smb2cli_query_directory_send(TALLOC_CTX
*mem_ctx
,
38 struct tevent_context
*ev
,
39 struct cli_state
*cli
,
43 uint64_t fid_persistent
,
44 uint64_t fid_volatile
,
48 struct tevent_req
*req
, *subreq
;
49 struct smb2cli_query_directory_state
*state
;
54 req
= tevent_req_create(mem_ctx
, &state
,
55 struct smb2cli_query_directory_state
);
60 if (!convert_string_talloc(state
, CH_UNIX
, CH_UTF16
,
64 return tevent_req_post(req
, ev
);
69 SCVAL(fixed
, 2, level
);
70 SCVAL(fixed
, 3, flags
);
71 SIVAL(fixed
, 4, file_index
);
72 SBVAL(fixed
, 8, fid_persistent
);
73 SBVAL(fixed
, 16, fid_volatile
);
74 SSVAL(fixed
, 24, SMB2_HDR_BODY
+ 32);
75 SSVAL(fixed
, 26, dyn_len
);
76 SSVAL(fixed
, 28, outbuf_len
);
78 subreq
= smb2cli_req_send(state
, ev
, cli
, SMB2_OP_FIND
, 0,
79 state
->fixed
, sizeof(state
->fixed
),
81 if (tevent_req_nomem(subreq
, req
)) {
82 return tevent_req_post(req
, ev
);
84 tevent_req_set_callback(subreq
, smb2cli_query_directory_done
, req
);
88 static void smb2cli_query_directory_done(struct tevent_req
*subreq
)
90 struct tevent_req
*req
=
91 tevent_req_callback_data(subreq
,
93 struct smb2cli_query_directory_state
*state
=
95 struct smb2cli_query_directory_state
);
100 status
= smb2cli_req_recv(subreq
, state
, &iov
, 9);
101 if (tevent_req_nterror(req
, status
)) {
105 data_offset
= SVAL(iov
[1].iov_base
, 2);
106 state
->data_length
= IVAL(iov
[1].iov_base
, 4);
108 if ((data_offset
!= SMB2_HDR_BODY
+ 8) ||
109 (state
->data_length
> iov
[2].iov_len
)) {
110 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
114 state
->recv_iov
= iov
;
115 state
->data
= (uint8_t *)iov
[2].iov_base
;
116 tevent_req_done(req
);
119 NTSTATUS
smb2cli_query_directory_recv(struct tevent_req
*req
,
122 uint32_t *data_length
)
124 struct smb2cli_query_directory_state
*state
=
126 struct smb2cli_query_directory_state
);
129 if (tevent_req_is_nterror(req
, &status
)) {
132 talloc_steal(mem_ctx
, state
->recv_iov
);
133 *data_length
= state
->data_length
;
138 NTSTATUS
smb2cli_query_directory(struct cli_state
*cli
,
142 uint64_t fid_persistent
,
143 uint64_t fid_volatile
,
148 uint32_t *data_length
)
150 TALLOC_CTX
*frame
= talloc_stackframe();
151 struct event_context
*ev
;
152 struct tevent_req
*req
;
153 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
155 if (cli_has_async_calls(cli
)) {
157 * Can't use sync call while an async call is in flight
159 status
= NT_STATUS_INVALID_PARAMETER
;
162 ev
= event_context_init(frame
);
166 req
= smb2cli_query_directory_send(frame
, ev
, cli
, level
, flags
,
167 file_index
, fid_persistent
,
168 fid_volatile
, mask
, outbuf_len
);
172 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
175 status
= smb2cli_query_directory_recv(req
, mem_ctx
,