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 const char *tcon_share;
48 req = tevent_req_create(mem_ctx, &state, struct smb2cli_tcon_state);
54 tcon_share = talloc_asprintf(state, "\\\\%s\\%s",
55 smbXcli_conn_remote_name(cli->conn),
57 if (tevent_req_nomem(tcon_share, req)) {
58 return tevent_req_post(req, ev);
60 if (!convert_string_talloc(state, CH_UNIX, CH_UTF16,
61 tcon_share, strlen(tcon_share),
64 return tevent_req_post(req, ev);
67 if (strlen(tcon_share) == 0) {
74 SSVAL(fixed, 4, SMB2_HDR_BODY + 8);
75 SSVAL(fixed, 6, dyn_len);
78 dyn = state->dyn_pad;;
79 dyn_len = sizeof(state->dyn_pad);
82 subreq = smb2cli_req_send(state, ev, cli->conn, SMB2_OP_TCON,
87 state->fixed, sizeof(state->fixed),
89 if (tevent_req_nomem(subreq, req)) {
90 return tevent_req_post(req, ev);
92 tevent_req_set_callback(subreq, smb2cli_tcon_done, req);
96 static void smb2cli_tcon_done(struct tevent_req *subreq)
98 struct tevent_req *req = tevent_req_callback_data(
99 subreq, struct tevent_req);
100 struct smb2cli_tcon_state *state = tevent_req_data(
101 req, struct smb2cli_tcon_state);
102 struct cli_state *cli = state->cli;
108 uint32_t share_flags;
109 uint32_t share_capabilities;
110 uint32_t maximal_access;
111 static const struct smb2cli_req_expected_response expected[] = {
113 .status = NT_STATUS_OK,
118 status = smb2cli_req_recv(subreq, state, &iov,
119 expected, ARRAY_SIZE(expected));
121 if (!NT_STATUS_IS_OK(status)) {
122 tevent_req_nterror(req, status);
126 tcon_id = IVAL(iov[0].iov_base, SMB2_HDR_TID);
128 body = (uint8_t *)iov[1].iov_base;
129 share_type = CVAL(body, 0x02);
130 share_flags = IVAL(body, 0x04);
131 share_capabilities = IVAL(body, 0x08);
132 maximal_access = IVAL(body, 0x0C);
134 cli->smb2.tcon = smbXcli_tcon_create(cli);
135 if (tevent_req_nomem(cli->smb2.tcon, req)) {
139 smb2cli_tcon_set_values(cli->smb2.tcon,
147 tevent_req_done(req);
150 NTSTATUS smb2cli_tcon_recv(struct tevent_req *req)
152 return tevent_req_simple_recv_ntstatus(req);
155 NTSTATUS smb2cli_tcon(struct cli_state *cli, const char *share)
157 TALLOC_CTX *frame = talloc_stackframe();
158 struct tevent_context *ev;
159 struct tevent_req *req;
160 NTSTATUS status = NT_STATUS_NO_MEMORY;
162 if (smbXcli_conn_has_async_calls(cli->conn)) {
164 * Can't use sync call while an async call is in flight
166 status = NT_STATUS_INVALID_PARAMETER;
169 ev = tevent_context_init(frame);
173 req = smb2cli_tcon_send(frame, ev, cli, share);
177 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
180 status = smb2cli_tcon_recv(req);
186 struct smb2cli_tdis_state {
187 struct cli_state *cli;
191 static void smb2cli_tdis_done(struct tevent_req *subreq);
193 struct tevent_req *smb2cli_tdis_send(TALLOC_CTX *mem_ctx,
194 struct tevent_context *ev,
195 struct cli_state *cli)
197 struct tevent_req *req, *subreq;
198 struct smb2cli_tdis_state *state;
200 req = tevent_req_create(mem_ctx, &state,
201 struct smb2cli_tdis_state);
206 SSVAL(state->fixed, 0, 4);
208 subreq = smb2cli_req_send(state, ev, cli->conn, SMB2_OP_TDIS,
213 state->fixed, sizeof(state->fixed),
215 if (tevent_req_nomem(subreq, req)) {
216 return tevent_req_post(req, ev);
218 tevent_req_set_callback(subreq, smb2cli_tdis_done, req);
222 static void smb2cli_tdis_done(struct tevent_req *subreq)
224 struct tevent_req *req =
225 tevent_req_callback_data(subreq,
227 struct smb2cli_tdis_state *state =
229 struct smb2cli_tdis_state);
231 static const struct smb2cli_req_expected_response expected[] = {
233 .status = NT_STATUS_OK,
238 status = smb2cli_req_recv(subreq, NULL, NULL,
239 expected, ARRAY_SIZE(expected));
241 if (tevent_req_nterror(req, status)) {
244 TALLOC_FREE(state->cli->smb2.tcon);
245 tevent_req_done(req);
248 NTSTATUS smb2cli_tdis_recv(struct tevent_req *req)
250 return tevent_req_simple_recv_ntstatus(req);
253 NTSTATUS smb2cli_tdis(struct cli_state *cli)
255 TALLOC_CTX *frame = talloc_stackframe();
256 struct tevent_context *ev;
257 struct tevent_req *req;
258 NTSTATUS status = NT_STATUS_NO_MEMORY;
260 if (smbXcli_conn_has_async_calls(cli->conn)) {
262 * Can't use sync call while an async call is in flight
264 status = NT_STATUS_INVALID_PARAMETER;
267 ev = tevent_context_init(frame);
271 req = smb2cli_tdis_send(frame, ev, cli);
275 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
278 status = smb2cli_tdis_recv(req);