s3:libsmb: remove unused cli_state->user_session_key
[kai/samba.git] / source3 / libsmb / smb2cli_tcon.c
1 /*
2    Unix SMB/CIFS implementation.
3    smb2 lib
4    Copyright (C) Volker Lendecke 2011
5
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.
10
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.
15
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/>.
18 */
19
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"
27
28 struct smb2cli_tcon_state {
29         struct cli_state *cli;
30         uint8_t fixed[8];
31         uint8_t dyn_pad[1];
32 };
33
34 static void smb2cli_tcon_done(struct tevent_req *subreq);
35
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)
40 {
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;
48
49         req = tevent_req_create(mem_ctx, &state, struct smb2cli_tcon_state);
50         if (req == NULL) {
51                 return NULL;
52         }
53         state->cli = cli;
54
55         print_sockaddr(srv_ip, sizeof(srv_ip), smbXcli_conn_remote_sockaddr(cli->conn));
56
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);
61         }
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);
67         }
68
69         if (strlen(tcon_share) == 0) {
70                 TALLOC_FREE(dyn);
71                 dyn_len = 0;
72         }
73
74         fixed = state->fixed;
75         SSVAL(fixed, 0, 9);
76         SSVAL(fixed, 4, SMB2_HDR_BODY + 8);
77         SSVAL(fixed, 6, dyn_len);
78
79         if (dyn_len == 0) {
80                 dyn = state->dyn_pad;;
81                 dyn_len = sizeof(state->dyn_pad);
82         }
83
84         subreq = smb2cli_req_send(state, ev, cli->conn, SMB2_OP_TCON,
85                                   0, 0, /* flags */
86                                   cli->timeout,
87                                   NULL, /* tcon */
88                                   cli->smb2.session,
89                                   state->fixed, sizeof(state->fixed),
90                                   dyn, dyn_len);
91         if (tevent_req_nomem(subreq, req)) {
92                 return tevent_req_post(req, ev);
93         }
94         tevent_req_set_callback(subreq, smb2cli_tcon_done, req);
95         return req;
96 }
97
98 static void smb2cli_tcon_done(struct tevent_req *subreq)
99 {
100         struct tevent_req *req = tevent_req_callback_data(
101                 subreq, struct tevent_req);
102         struct smb2cli_tcon_state *state = tevent_req_data(
103                 req, struct smb2cli_tcon_state);
104         struct cli_state *cli = state->cli;
105         NTSTATUS status;
106         struct iovec *iov;
107         uint8_t *body;
108         uint32_t tcon_id;
109         uint8_t share_type;
110         uint32_t share_flags;
111         uint32_t share_capabilities;
112         uint32_t maximal_access;
113         static const struct smb2cli_req_expected_response expected[] = {
114         {
115                 .status = NT_STATUS_OK,
116                 .body_size = 0x10
117         }
118         };
119
120         status = smb2cli_req_recv(subreq, state, &iov,
121                                   expected, ARRAY_SIZE(expected));
122         TALLOC_FREE(subreq);
123         if (!NT_STATUS_IS_OK(status)) {
124                 tevent_req_nterror(req, status);
125                 return;
126         }
127
128         tcon_id = IVAL(iov[0].iov_base, SMB2_HDR_TID);
129
130         body = (uint8_t *)iov[1].iov_base;
131         share_type              = CVAL(body, 0x02);
132         share_flags             = IVAL(body, 0x04);
133         share_capabilities      = IVAL(body, 0x08);
134         maximal_access          = IVAL(body, 0x0C);
135
136         cli->smb2.tcon = smbXcli_tcon_create(cli);
137         if (tevent_req_nomem(cli->smb2.tcon, req)) {
138                 return;
139         }
140
141         smb2cli_tcon_set_values(cli->smb2.tcon,
142                                 cli->smb2.session,
143                                 tcon_id,
144                                 share_type,
145                                 share_flags,
146                                 share_capabilities,
147                                 maximal_access);
148
149         tevent_req_done(req);
150 }
151
152 NTSTATUS smb2cli_tcon_recv(struct tevent_req *req)
153 {
154         return tevent_req_simple_recv_ntstatus(req);
155 }
156
157 NTSTATUS smb2cli_tcon(struct cli_state *cli, const char *share)
158 {
159         TALLOC_CTX *frame = talloc_stackframe();
160         struct tevent_context *ev;
161         struct tevent_req *req;
162         NTSTATUS status = NT_STATUS_NO_MEMORY;
163
164         if (smbXcli_conn_has_async_calls(cli->conn)) {
165                 /*
166                  * Can't use sync call while an async call is in flight
167                  */
168                 status = NT_STATUS_INVALID_PARAMETER;
169                 goto fail;
170         }
171         ev = tevent_context_init(frame);
172         if (ev == NULL) {
173                 goto fail;
174         }
175         req = smb2cli_tcon_send(frame, ev, cli, share);
176         if (req == NULL) {
177                 goto fail;
178         }
179         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
180                 goto fail;
181         }
182         status = smb2cli_tcon_recv(req);
183  fail:
184         TALLOC_FREE(frame);
185         return status;
186 }
187
188 struct smb2cli_tdis_state {
189         struct cli_state *cli;
190         uint8_t fixed[4];
191 };
192
193 static void smb2cli_tdis_done(struct tevent_req *subreq);
194
195 struct tevent_req *smb2cli_tdis_send(TALLOC_CTX *mem_ctx,
196                                       struct tevent_context *ev,
197                                       struct cli_state *cli)
198 {
199         struct tevent_req *req, *subreq;
200         struct smb2cli_tdis_state *state;
201
202         req = tevent_req_create(mem_ctx, &state,
203                                 struct smb2cli_tdis_state);
204         if (req == NULL) {
205                 return NULL;
206         }
207         state->cli = cli;
208         SSVAL(state->fixed, 0, 4);
209
210         subreq = smb2cli_req_send(state, ev, cli->conn, SMB2_OP_TDIS,
211                                   0, 0, /* flags */
212                                   cli->timeout,
213                                   cli->smb2.tcon,
214                                   cli->smb2.session,
215                                   state->fixed, sizeof(state->fixed),
216                                   NULL, 0);
217         if (tevent_req_nomem(subreq, req)) {
218                 return tevent_req_post(req, ev);
219         }
220         tevent_req_set_callback(subreq, smb2cli_tdis_done, req);
221         return req;
222 }
223
224 static void smb2cli_tdis_done(struct tevent_req *subreq)
225 {
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[] = {
234         {
235                 .status = NT_STATUS_OK,
236                 .body_size = 0x04
237         }
238         };
239
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;
245         }
246         TALLOC_FREE(state->cli->smb2.tcon);
247         tevent_req_done(req);
248 }
249
250 NTSTATUS smb2cli_tdis_recv(struct tevent_req *req)
251 {
252         return tevent_req_simple_recv_ntstatus(req);
253 }
254
255 NTSTATUS smb2cli_tdis(struct cli_state *cli)
256 {
257         TALLOC_CTX *frame = talloc_stackframe();
258         struct tevent_context *ev;
259         struct tevent_req *req;
260         NTSTATUS status = NT_STATUS_NO_MEMORY;
261
262         if (smbXcli_conn_has_async_calls(cli->conn)) {
263                 /*
264                  * Can't use sync call while an async call is in flight
265                  */
266                 status = NT_STATUS_INVALID_PARAMETER;
267                 goto fail;
268         }
269         ev = tevent_context_init(frame);
270         if (ev == NULL) {
271                 goto fail;
272         }
273         req = smb2cli_tdis_send(frame, ev, cli);
274         if (req == NULL) {
275                 goto fail;
276         }
277         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
278                 goto fail;
279         }
280         status = smb2cli_tdis_recv(req);
281  fail:
282         TALLOC_FREE(frame);
283         return status;
284 }