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/>.
21 #include "system/network.h"
22 #include "../lib/util/tevent_ntstatus.h"
23 #include "../libcli/smb/smb_common.h"
24 #include "../libcli/smb/smbXcli_base.h"
26 struct smb2cli_tcon_state
{
27 struct tevent_context
*ev
;
28 struct smbXcli_conn
*conn
;
29 uint32_t timeout_msec
;
30 struct smbXcli_session
*session
;
31 struct smbXcli_tcon
*tcon
;
36 static void smb2cli_tcon_done(struct tevent_req
*subreq
);
38 struct tevent_req
*smb2cli_tcon_send(TALLOC_CTX
*mem_ctx
,
39 struct tevent_context
*ev
,
40 struct smbXcli_conn
*conn
,
41 uint32_t timeout_msec
,
42 struct smbXcli_session
*session
,
43 struct smbXcli_tcon
*tcon
,
47 struct tevent_req
*req
, *subreq
;
48 struct smb2cli_tcon_state
*state
;
52 uint32_t additional_flags
= 0;
53 uint32_t clear_flags
= 0;
55 req
= tevent_req_create(mem_ctx
, &state
, struct smb2cli_tcon_state
);
61 state
->timeout_msec
= timeout_msec
;
62 state
->session
= session
;
65 if (!convert_string_talloc(state
, CH_UNIX
, CH_UTF16
,
69 return tevent_req_post(req
, ev
);
72 if (strlen(unc
) == 0) {
79 if (smbXcli_conn_protocol(conn
) >= PROTOCOL_SMB3_10
) {
80 SSVAL(fixed
, 2, flags
);
82 SSVAL(fixed
, 2, 0); /* Reserved */
84 SSVAL(fixed
, 4, SMB2_HDR_BODY
+ 8);
85 SSVAL(fixed
, 6, dyn_len
);
89 dyn_len
= sizeof(state
->dyn_pad
);
92 if (smbXcli_session_is_authenticated(state
->session
)) {
93 additional_flags
|= SMB2_HDR_FLAG_SIGNED
;
96 subreq
= smb2cli_req_send(state
, ev
, conn
, SMB2_OP_TCON
,
97 additional_flags
, clear_flags
,
101 state
->fixed
, sizeof(state
->fixed
),
103 0); /* max_dyn_len */
104 if (tevent_req_nomem(subreq
, req
)) {
105 return tevent_req_post(req
, ev
);
107 tevent_req_set_callback(subreq
, smb2cli_tcon_done
, req
);
112 static void smb2cli_tcon_validate(struct tevent_req
*subreq
);
114 static void smb2cli_tcon_done(struct tevent_req
*subreq
)
116 struct tevent_req
*req
= tevent_req_callback_data(
117 subreq
, struct tevent_req
);
118 struct smb2cli_tcon_state
*state
= tevent_req_data(
119 req
, struct smb2cli_tcon_state
);
125 uint32_t share_flags
;
126 uint32_t share_capabilities
;
127 uint32_t maximal_access
;
128 static const struct smb2cli_req_expected_response expected
[] = {
130 .status
= NT_STATUS_OK
,
135 status
= smb2cli_req_recv(subreq
, state
, &iov
,
136 expected
, ARRAY_SIZE(expected
));
138 if (!NT_STATUS_IS_OK(status
)) {
139 tevent_req_nterror(req
, status
);
143 tcon_id
= IVAL(iov
[0].iov_base
, SMB2_HDR_TID
);
145 body
= (uint8_t *)iov
[1].iov_base
;
146 share_type
= CVAL(body
, 0x02);
147 share_flags
= IVAL(body
, 0x04);
148 share_capabilities
= IVAL(body
, 0x08);
149 maximal_access
= IVAL(body
, 0x0C);
151 smb2cli_tcon_set_values(state
->tcon
,
159 if (!smbXcli_session_is_authenticated(state
->session
)) {
160 tevent_req_done(req
);
164 if (smbXcli_conn_protocol(state
->conn
) >= PROTOCOL_SMB3_10
) {
165 tevent_req_done(req
);
169 subreq
= smb2cli_validate_negotiate_info_send(state
, state
->ev
,
174 if (tevent_req_nomem(subreq
, req
)) {
177 tevent_req_set_callback(subreq
, smb2cli_tcon_validate
, req
);
180 static void smb2cli_tcon_validate(struct tevent_req
*subreq
)
182 struct tevent_req
*req
= tevent_req_callback_data(
183 subreq
, struct tevent_req
);
184 struct smb2cli_tcon_state
*state
= tevent_req_data(
185 req
, struct smb2cli_tcon_state
);
188 status
= smb2cli_validate_negotiate_info_recv(subreq
);
190 if (!NT_STATUS_IS_OK(status
)) {
191 smb2cli_tcon_set_values(state
->tcon
, NULL
,
192 UINT32_MAX
, 0, 0, 0, 0);
193 tevent_req_nterror(req
, status
);
197 tevent_req_done(req
);
200 NTSTATUS
smb2cli_tcon_recv(struct tevent_req
*req
)
202 return tevent_req_simple_recv_ntstatus(req
);
205 NTSTATUS
smb2cli_tcon(struct smbXcli_conn
*conn
,
206 uint32_t timeout_msec
,
207 struct smbXcli_session
*session
,
208 struct smbXcli_tcon
*tcon
,
212 TALLOC_CTX
*frame
= talloc_stackframe();
213 struct tevent_context
*ev
;
214 struct tevent_req
*req
;
215 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
217 if (smbXcli_conn_has_async_calls(conn
)) {
219 * Can't use sync call while an async call is in flight
221 status
= NT_STATUS_INVALID_PARAMETER
;
224 ev
= samba_tevent_context_init(frame
);
228 req
= smb2cli_tcon_send(frame
, ev
, conn
,
229 timeout_msec
, session
, tcon
,
234 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
237 status
= smb2cli_tcon_recv(req
);
243 struct smb2cli_tdis_state
{
244 struct smbXcli_tcon
*tcon
;
248 static void smb2cli_tdis_done(struct tevent_req
*subreq
);
250 struct tevent_req
*smb2cli_tdis_send(TALLOC_CTX
*mem_ctx
,
251 struct tevent_context
*ev
,
252 struct smbXcli_conn
*conn
,
253 uint32_t timeout_msec
,
254 struct smbXcli_session
*session
,
255 struct smbXcli_tcon
*tcon
)
257 struct tevent_req
*req
, *subreq
;
258 struct smb2cli_tdis_state
*state
;
260 req
= tevent_req_create(mem_ctx
, &state
,
261 struct smb2cli_tdis_state
);
267 SSVAL(state
->fixed
, 0, 4);
269 subreq
= smb2cli_req_send(state
, ev
, conn
, SMB2_OP_TDIS
,
273 state
->fixed
, sizeof(state
->fixed
),
275 0); /* max_dyn_len */
276 if (tevent_req_nomem(subreq
, req
)) {
277 return tevent_req_post(req
, ev
);
279 tevent_req_set_callback(subreq
, smb2cli_tdis_done
, req
);
283 static void smb2cli_tdis_done(struct tevent_req
*subreq
)
285 struct tevent_req
*req
=
286 tevent_req_callback_data(subreq
,
288 struct smb2cli_tdis_state
*state
=
290 struct smb2cli_tdis_state
);
292 static const struct smb2cli_req_expected_response expected
[] = {
294 .status
= NT_STATUS_OK
,
299 status
= smb2cli_req_recv(subreq
, NULL
, NULL
,
300 expected
, ARRAY_SIZE(expected
));
302 if (tevent_req_nterror(req
, status
)) {
305 smb2cli_tcon_set_values(state
->tcon
, NULL
,
306 UINT32_MAX
, 0, 0, 0, 0);
307 tevent_req_done(req
);
310 NTSTATUS
smb2cli_tdis_recv(struct tevent_req
*req
)
312 return tevent_req_simple_recv_ntstatus(req
);
315 NTSTATUS
smb2cli_tdis(struct smbXcli_conn
*conn
,
316 uint32_t timeout_msec
,
317 struct smbXcli_session
*session
,
318 struct smbXcli_tcon
*tcon
)
320 TALLOC_CTX
*frame
= talloc_stackframe();
321 struct tevent_context
*ev
;
322 struct tevent_req
*req
;
323 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
325 if (smbXcli_conn_has_async_calls(conn
)) {
327 * Can't use sync call while an async call is in flight
329 status
= NT_STATUS_INVALID_PARAMETER
;
332 ev
= samba_tevent_context_init(frame
);
336 req
= smb2cli_tdis_send(frame
, ev
, conn
,
337 timeout_msec
, session
, tcon
);
341 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
344 status
= smb2cli_tdis_recv(req
);