s3:libsmb: remove cli_state->smb2.tid
[Samba/gebeck_regimport.git] / source3 / libsmb / smb2cli_tcon.c
blobed5b00b329917844c438245331531f233160dd06
1 /*
2 Unix SMB/CIFS implementation.
3 smb2 lib
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/>.
20 #include "includes.h"
21 #include "client.h"
22 #include "async_smb.h"
23 #include "../libcli/smb/smbXcli_base.h"
24 #include "smb2cli.h"
25 #include "libsmb/proto.h"
26 #include "lib/util/tevent_ntstatus.h"
28 struct smb2cli_tcon_state {
29 struct cli_state *cli;
30 uint8_t fixed[8];
31 uint8_t dyn_pad[1];
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,
39 const char *share)
41 struct tevent_req *req, *subreq;
42 struct smb2cli_tcon_state *state;
43 uint8_t *fixed;
44 char srv_ip[INET6_ADDRSTRLEN];
45 const char *tcon_share;
46 uint8_t *dyn;
47 size_t dyn_len;
49 req = tevent_req_create(mem_ctx, &state, struct smb2cli_tcon_state);
50 if (req == NULL) {
51 return NULL;
53 state->cli = cli;
55 print_sockaddr(srv_ip, sizeof(srv_ip), smbXcli_conn_remote_sockaddr(cli->conn));
57 tcon_share = talloc_asprintf(state, "\\\\%s\\%s",
58 srv_ip, share);
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),
64 &dyn, &dyn_len)) {
65 tevent_req_oom(req);
66 return tevent_req_post(req, ev);
69 if (strlen(tcon_share) == 0) {
70 TALLOC_FREE(dyn);
71 dyn_len = 0;
74 fixed = state->fixed;
75 SSVAL(fixed, 0, 9);
76 SSVAL(fixed, 4, SMB2_HDR_BODY + 8);
77 SSVAL(fixed, 6, dyn_len);
79 if (dyn_len == 0) {
80 dyn = state->dyn_pad;;
81 dyn_len = sizeof(state->dyn_pad);
84 subreq = smb2cli_req_send(state, ev, cli->conn, SMB2_OP_TCON,
85 0, 0, /* flags */
86 cli->timeout,
87 cli->smb2.pid,
88 0, /* tid */
89 cli->smb2.session,
90 state->fixed, sizeof(state->fixed),
91 dyn, dyn_len);
92 if (tevent_req_nomem(subreq, req)) {
93 return tevent_req_post(req, ev);
95 tevent_req_set_callback(subreq, smb2cli_tcon_done, req);
96 return 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;
106 NTSTATUS status;
107 struct iovec *iov;
108 uint8_t *body;
109 uint32_t tcon_id;
110 static const struct smb2cli_req_expected_response expected[] = {
112 .status = NT_STATUS_OK,
113 .body_size = 0x10
117 status = smb2cli_req_recv(subreq, state, &iov,
118 expected, ARRAY_SIZE(expected));
119 if (!NT_STATUS_IS_OK(status)) {
120 TALLOC_FREE(subreq);
121 tevent_req_nterror(req, status);
122 return;
125 tcon_id = IVAL(iov[0].iov_base, SMB2_HDR_TID);
127 body = (uint8_t *)iov[1].iov_base;
128 cli->smb2.share_type = CVAL(body, 2);
129 cli->smb2.share_flags = IVAL(body, 4);
130 cli->smb2.share_capabilities = IVAL(body, 8);
131 cli->smb2.maximal_access = IVAL(body, 12);
133 TALLOC_FREE(subreq);
135 cli->smb2.tcon = smbXcli_tcon_create(cli);
136 if (tevent_req_nomem(cli->smb2.tcon, req)) {
137 return;
140 smb2cli_tcon_set_values(cli->smb2.tcon,
141 tcon_id,
142 cli->smb2.share_type,
143 cli->smb2.share_flags,
144 cli->smb2.share_capabilities,
145 cli->smb2.maximal_access);
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;
167 goto fail;
169 ev = tevent_context_init(frame);
170 if (ev == NULL) {
171 goto fail;
173 req = smb2cli_tcon_send(frame, ev, cli, share);
174 if (req == NULL) {
175 goto fail;
177 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
178 goto fail;
180 status = smb2cli_tcon_recv(req);
181 fail:
182 TALLOC_FREE(frame);
183 return status;
186 struct smb2cli_tdis_state {
187 struct cli_state *cli;
188 uint8_t fixed[4];
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;
199 uint32_t tcon_id = 0;
201 req = tevent_req_create(mem_ctx, &state,
202 struct smb2cli_tdis_state);
203 if (req == NULL) {
204 return NULL;
206 state->cli = cli;
207 SSVAL(state->fixed, 0, 4);
209 if (cli->smb2.tcon) {
210 tcon_id = smb2cli_tcon_current_id(cli->smb2.tcon);
213 subreq = smb2cli_req_send(state, ev, cli->conn, SMB2_OP_TDIS,
214 0, 0, /* flags */
215 cli->timeout,
216 cli->smb2.pid,
217 tcon_id,
218 cli->smb2.session,
219 state->fixed, sizeof(state->fixed),
220 NULL, 0);
221 if (tevent_req_nomem(subreq, req)) {
222 return tevent_req_post(req, ev);
224 tevent_req_set_callback(subreq, smb2cli_tdis_done, req);
225 return req;
228 static void smb2cli_tdis_done(struct tevent_req *subreq)
230 struct tevent_req *req =
231 tevent_req_callback_data(subreq,
232 struct tevent_req);
233 struct smb2cli_tdis_state *state =
234 tevent_req_data(req,
235 struct smb2cli_tdis_state);
236 NTSTATUS status;
237 static const struct smb2cli_req_expected_response expected[] = {
239 .status = NT_STATUS_OK,
240 .body_size = 0x04
244 status = smb2cli_req_recv(subreq, NULL, NULL,
245 expected, ARRAY_SIZE(expected));
246 TALLOC_FREE(subreq);
247 if (tevent_req_nterror(req, status)) {
248 return;
250 TALLOC_FREE(state->cli->smb2.tcon);
251 tevent_req_done(req);
254 NTSTATUS smb2cli_tdis_recv(struct tevent_req *req)
256 return tevent_req_simple_recv_ntstatus(req);
259 NTSTATUS smb2cli_tdis(struct cli_state *cli)
261 TALLOC_CTX *frame = talloc_stackframe();
262 struct tevent_context *ev;
263 struct tevent_req *req;
264 NTSTATUS status = NT_STATUS_NO_MEMORY;
266 if (smbXcli_conn_has_async_calls(cli->conn)) {
268 * Can't use sync call while an async call is in flight
270 status = NT_STATUS_INVALID_PARAMETER;
271 goto fail;
273 ev = tevent_context_init(frame);
274 if (ev == NULL) {
275 goto fail;
277 req = smb2cli_tdis_send(frame, ev, cli);
278 if (req == NULL) {
279 goto fail;
281 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
282 goto fail;
284 status = smb2cli_tdis_recv(req);
285 fail:
286 TALLOC_FREE(frame);
287 return status;