smbd: Rename lck1->lock in brl_conflict_other
[Samba.git] / source3 / libsmb / smb2cli_tcon.c
blob2467ce58214c8d3c107fbad616e0646d02cd766d
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 const char *tcon_share;
45 uint8_t *dyn;
46 size_t dyn_len;
48 req = tevent_req_create(mem_ctx, &state, struct smb2cli_tcon_state);
49 if (req == NULL) {
50 return NULL;
52 state->cli = cli;
54 tcon_share = talloc_asprintf(state, "\\\\%s\\%s",
55 smbXcli_conn_remote_name(cli->conn),
56 share);
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),
62 &dyn, &dyn_len)) {
63 tevent_req_oom(req);
64 return tevent_req_post(req, ev);
67 if (strlen(tcon_share) == 0) {
68 TALLOC_FREE(dyn);
69 dyn_len = 0;
72 fixed = state->fixed;
73 SSVAL(fixed, 0, 9);
74 SSVAL(fixed, 4, SMB2_HDR_BODY + 8);
75 SSVAL(fixed, 6, dyn_len);
77 if (dyn_len == 0) {
78 dyn = state->dyn_pad;;
79 dyn_len = sizeof(state->dyn_pad);
82 subreq = smb2cli_req_send(state, ev, cli->conn, SMB2_OP_TCON,
83 0, 0, /* flags */
84 cli->timeout,
85 NULL, /* tcon */
86 cli->smb2.session,
87 state->fixed, sizeof(state->fixed),
88 dyn, dyn_len,
89 0); /* max_dyn_len */
90 if (tevent_req_nomem(subreq, req)) {
91 return tevent_req_post(req, ev);
93 tevent_req_set_callback(subreq, smb2cli_tcon_done, req);
94 return req;
97 static void smb2cli_tcon_done(struct tevent_req *subreq)
99 struct tevent_req *req = tevent_req_callback_data(
100 subreq, struct tevent_req);
101 struct smb2cli_tcon_state *state = tevent_req_data(
102 req, struct smb2cli_tcon_state);
103 struct cli_state *cli = state->cli;
104 NTSTATUS status;
105 struct iovec *iov;
106 uint8_t *body;
107 uint32_t tcon_id;
108 uint8_t share_type;
109 uint32_t share_flags;
110 uint32_t share_capabilities;
111 uint32_t maximal_access;
112 static const struct smb2cli_req_expected_response expected[] = {
114 .status = NT_STATUS_OK,
115 .body_size = 0x10
119 status = smb2cli_req_recv(subreq, state, &iov,
120 expected, ARRAY_SIZE(expected));
121 TALLOC_FREE(subreq);
122 if (!NT_STATUS_IS_OK(status)) {
123 tevent_req_nterror(req, status);
124 return;
127 tcon_id = IVAL(iov[0].iov_base, SMB2_HDR_TID);
129 body = (uint8_t *)iov[1].iov_base;
130 share_type = CVAL(body, 0x02);
131 share_flags = IVAL(body, 0x04);
132 share_capabilities = IVAL(body, 0x08);
133 maximal_access = IVAL(body, 0x0C);
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 cli->smb2.session,
142 tcon_id,
143 share_type,
144 share_flags,
145 share_capabilities,
146 maximal_access);
148 tevent_req_done(req);
151 NTSTATUS smb2cli_tcon_recv(struct tevent_req *req)
153 return tevent_req_simple_recv_ntstatus(req);
156 NTSTATUS smb2cli_tcon(struct cli_state *cli, const char *share)
158 TALLOC_CTX *frame = talloc_stackframe();
159 struct tevent_context *ev;
160 struct tevent_req *req;
161 NTSTATUS status = NT_STATUS_NO_MEMORY;
163 if (smbXcli_conn_has_async_calls(cli->conn)) {
165 * Can't use sync call while an async call is in flight
167 status = NT_STATUS_INVALID_PARAMETER;
168 goto fail;
170 ev = samba_tevent_context_init(frame);
171 if (ev == NULL) {
172 goto fail;
174 req = smb2cli_tcon_send(frame, ev, cli, share);
175 if (req == NULL) {
176 goto fail;
178 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
179 goto fail;
181 status = smb2cli_tcon_recv(req);
182 fail:
183 TALLOC_FREE(frame);
184 return status;
187 struct smb2cli_tdis_state {
188 struct cli_state *cli;
189 uint8_t fixed[4];
192 static void smb2cli_tdis_done(struct tevent_req *subreq);
194 struct tevent_req *smb2cli_tdis_send(TALLOC_CTX *mem_ctx,
195 struct tevent_context *ev,
196 struct cli_state *cli)
198 struct tevent_req *req, *subreq;
199 struct smb2cli_tdis_state *state;
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 subreq = smb2cli_req_send(state, ev, cli->conn, SMB2_OP_TDIS,
210 0, 0, /* flags */
211 cli->timeout,
212 cli->smb2.tcon,
213 cli->smb2.session,
214 state->fixed, sizeof(state->fixed),
215 NULL, 0, /* dyn* */
216 0); /* max_dyn_len */
217 if (tevent_req_nomem(subreq, req)) {
218 return tevent_req_post(req, ev);
220 tevent_req_set_callback(subreq, smb2cli_tdis_done, req);
221 return req;
224 static void smb2cli_tdis_done(struct tevent_req *subreq)
226 struct tevent_req *req =
227 tevent_req_callback_data(subreq,
228 struct tevent_req);
229 struct smb2cli_tdis_state *state =
230 tevent_req_data(req,
231 struct smb2cli_tdis_state);
232 NTSTATUS status;
233 static const struct smb2cli_req_expected_response expected[] = {
235 .status = NT_STATUS_OK,
236 .body_size = 0x04
240 status = smb2cli_req_recv(subreq, NULL, NULL,
241 expected, ARRAY_SIZE(expected));
242 TALLOC_FREE(subreq);
243 if (tevent_req_nterror(req, status)) {
244 return;
246 smb2cli_tcon_set_values(state->cli->smb2.tcon, NULL,
247 UINT32_MAX, 0, 0, 0, 0);
248 tevent_req_done(req);
251 NTSTATUS smb2cli_tdis_recv(struct tevent_req *req)
253 return tevent_req_simple_recv_ntstatus(req);
256 NTSTATUS smb2cli_tdis(struct cli_state *cli)
258 TALLOC_CTX *frame = talloc_stackframe();
259 struct tevent_context *ev;
260 struct tevent_req *req;
261 NTSTATUS status = NT_STATUS_NO_MEMORY;
263 if (smbXcli_conn_has_async_calls(cli->conn)) {
265 * Can't use sync call while an async call is in flight
267 status = NT_STATUS_INVALID_PARAMETER;
268 goto fail;
270 ev = samba_tevent_context_init(frame);
271 if (ev == NULL) {
272 goto fail;
274 req = smb2cli_tdis_send(frame, ev, cli);
275 if (req == NULL) {
276 goto fail;
278 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
279 goto fail;
281 status = smb2cli_tdis_recv(req);
282 fail:
283 TALLOC_FREE(frame);
284 return status;