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 "../libcli/smb/smbXcli_base.h"
25 #include "libsmb/proto.h"
26 #include "lib/util/tevent_ntstatus.h"
28 struct smb2cli_tcon_state
{
29 struct cli_state
*cli
;
34 static void smb2cli_tcon_done(struct tevent_req
*subreq
);
36 struct tevent_req
*smb2cli_tcon_send(TALLOC_CTX
*mem_ctx
,
37 struct tevent_context
*ev
,
38 struct cli_state
*cli
,
41 struct tevent_req
*req
, *subreq
;
42 struct smb2cli_tcon_state
*state
;
44 char srv_ip
[INET6_ADDRSTRLEN
];
45 const char *tcon_share
;
49 req
= tevent_req_create(mem_ctx
, &state
, struct smb2cli_tcon_state
);
55 print_sockaddr(srv_ip
, sizeof(srv_ip
), cli_state_remote_sockaddr(cli
));
57 tcon_share
= talloc_asprintf(talloc_tos(), "\\\\%s\\%s",
59 if (tevent_req_nomem(tcon_share
, req
)) {
60 return tevent_req_post(req
, ev
);
62 if (!convert_string_talloc(state
, CH_UNIX
, CH_UTF16
,
63 tcon_share
, strlen(tcon_share
),
66 return tevent_req_post(req
, ev
);
69 if (strlen(tcon_share
) == 0) {
76 SSVAL(fixed
, 4, SMB2_HDR_BODY
+ 8);
77 SSVAL(fixed
, 6, dyn_len
);
80 dyn
= state
->dyn_pad
;;
81 dyn_len
= sizeof(state
->dyn_pad
);
84 subreq
= smb2cli_req_send(state
, ev
, cli
->conn
, SMB2_OP_TCON
,
90 state
->fixed
, sizeof(state
->fixed
),
92 if (tevent_req_nomem(subreq
, req
)) {
93 return tevent_req_post(req
, ev
);
95 tevent_req_set_callback(subreq
, smb2cli_tcon_done
, req
);
99 static void smb2cli_tcon_done(struct tevent_req
*subreq
)
101 struct tevent_req
*req
= tevent_req_callback_data(
102 subreq
, struct tevent_req
);
103 struct smb2cli_tcon_state
*state
= tevent_req_data(
104 req
, struct smb2cli_tcon_state
);
105 struct cli_state
*cli
= state
->cli
;
109 static const struct smb2cli_req_expected_response expected
[] = {
111 .status
= NT_STATUS_OK
,
116 status
= smb2cli_req_recv(subreq
, talloc_tos(), &iov
,
117 expected
, ARRAY_SIZE(expected
));
118 if (!NT_STATUS_IS_OK(status
)) {
120 tevent_req_nterror(req
, status
);
124 cli
->smb2
.tid
= IVAL(iov
[0].iov_base
, SMB2_HDR_TID
);
126 body
= (uint8_t *)iov
[1].iov_base
;
127 cli
->smb2
.share_type
= CVAL(body
, 2);
128 cli
->smb2
.share_flags
= IVAL(body
, 4);
129 cli
->smb2
.share_capabilities
= IVAL(body
, 8);
130 cli
->smb2
.maximal_access
= IVAL(body
, 12);
133 tevent_req_done(req
);
136 NTSTATUS
smb2cli_tcon_recv(struct tevent_req
*req
)
138 return tevent_req_simple_recv_ntstatus(req
);
141 NTSTATUS
smb2cli_tcon(struct cli_state
*cli
, const char *share
)
143 TALLOC_CTX
*frame
= talloc_stackframe();
144 struct event_context
*ev
;
145 struct tevent_req
*req
;
146 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
148 if (cli_has_async_calls(cli
)) {
150 * Can't use sync call while an async call is in flight
152 status
= NT_STATUS_INVALID_PARAMETER
;
155 ev
= event_context_init(frame
);
159 req
= smb2cli_tcon_send(frame
, ev
, cli
, share
);
163 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
166 status
= smb2cli_tcon_recv(req
);
172 struct smb2cli_tdis_state
{
173 struct cli_state
*cli
;
177 static void smb2cli_tdis_done(struct tevent_req
*subreq
);
179 struct tevent_req
*smb2cli_tdis_send(TALLOC_CTX
*mem_ctx
,
180 struct tevent_context
*ev
,
181 struct cli_state
*cli
)
183 struct tevent_req
*req
, *subreq
;
184 struct smb2cli_tdis_state
*state
;
186 req
= tevent_req_create(mem_ctx
, &state
,
187 struct smb2cli_tdis_state
);
192 SSVAL(state
->fixed
, 0, 4);
194 subreq
= smb2cli_req_send(state
, ev
, cli
->conn
, SMB2_OP_TDIS
,
200 state
->fixed
, sizeof(state
->fixed
),
202 if (tevent_req_nomem(subreq
, req
)) {
203 return tevent_req_post(req
, ev
);
205 tevent_req_set_callback(subreq
, smb2cli_tdis_done
, req
);
209 static void smb2cli_tdis_done(struct tevent_req
*subreq
)
211 struct tevent_req
*req
=
212 tevent_req_callback_data(subreq
,
214 struct smb2cli_tdis_state
*state
=
216 struct smb2cli_tdis_state
);
219 static const struct smb2cli_req_expected_response expected
[] = {
221 .status
= NT_STATUS_OK
,
226 status
= smb2cli_req_recv(subreq
, talloc_tos(), &iov
,
227 expected
, ARRAY_SIZE(expected
));
229 if (tevent_req_nterror(req
, status
)) {
232 state
->cli
->smb2
.tid
= 0;
233 tevent_req_done(req
);
236 NTSTATUS
smb2cli_tdis_recv(struct tevent_req
*req
)
238 return tevent_req_simple_recv_ntstatus(req
);
241 NTSTATUS
smb2cli_tdis(struct cli_state
*cli
)
243 TALLOC_CTX
*frame
= talloc_stackframe();
244 struct event_context
*ev
;
245 struct tevent_req
*req
;
246 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
248 if (cli_has_async_calls(cli
)) {
250 * Can't use sync call while an async call is in flight
252 status
= NT_STATUS_INVALID_PARAMETER
;
255 ev
= event_context_init(frame
);
259 req
= smb2cli_tdis_send(frame
, ev
, cli
);
263 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
266 status
= smb2cli_tdis_recv(req
);