dc18544bfff192bea1237b235680b598ee6c247b
[metze/samba/wip.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         const char *tcon_share;
45         uint8_t *dyn;
46         size_t dyn_len;
47
48         req = tevent_req_create(mem_ctx, &state, struct smb2cli_tcon_state);
49         if (req == NULL) {
50                 return NULL;
51         }
52         state->cli = cli;
53
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);
59         }
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);
65         }
66
67         if (strlen(tcon_share) == 0) {
68                 TALLOC_FREE(dyn);
69                 dyn_len = 0;
70         }
71
72         fixed = state->fixed;
73         SSVAL(fixed, 0, 9);
74         SSVAL(fixed, 4, SMB2_HDR_BODY + 8);
75         SSVAL(fixed, 6, dyn_len);
76
77         if (dyn_len == 0) {
78                 dyn = state->dyn_pad;;
79                 dyn_len = sizeof(state->dyn_pad);
80         }
81
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         if (tevent_req_nomem(subreq, req)) {
90                 return tevent_req_post(req, ev);
91         }
92         tevent_req_set_callback(subreq, smb2cli_tcon_done, req);
93         return req;
94 }
95
96 static void smb2cli_tcon_done(struct tevent_req *subreq)
97 {
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;
103         NTSTATUS status;
104         struct iovec *iov;
105         uint8_t *body;
106         uint32_t tcon_id;
107         uint8_t share_type;
108         uint32_t share_flags;
109         uint32_t share_capabilities;
110         uint32_t maximal_access;
111         static const struct smb2cli_req_expected_response expected[] = {
112         {
113                 .status = NT_STATUS_OK,
114                 .body_size = 0x10
115         }
116         };
117
118         status = smb2cli_req_recv(subreq, state, &iov,
119                                   expected, ARRAY_SIZE(expected));
120         TALLOC_FREE(subreq);
121         if (!NT_STATUS_IS_OK(status)) {
122                 tevent_req_nterror(req, status);
123                 return;
124         }
125
126         tcon_id = IVAL(iov[0].iov_base, SMB2_HDR_TID);
127
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);
133
134         cli->smb2.tcon = smbXcli_tcon_create(cli);
135         if (tevent_req_nomem(cli->smb2.tcon, req)) {
136                 return;
137         }
138
139         smb2cli_tcon_set_values(cli->smb2.tcon,
140                                 cli->smb2.session,
141                                 tcon_id,
142                                 share_type,
143                                 share_flags,
144                                 share_capabilities,
145                                 maximal_access);
146
147         tevent_req_done(req);
148 }
149
150 NTSTATUS smb2cli_tcon_recv(struct tevent_req *req)
151 {
152         return tevent_req_simple_recv_ntstatus(req);
153 }
154
155 NTSTATUS smb2cli_tcon(struct cli_state *cli, const char *share)
156 {
157         TALLOC_CTX *frame = talloc_stackframe();
158         struct tevent_context *ev;
159         struct tevent_req *req;
160         NTSTATUS status = NT_STATUS_NO_MEMORY;
161
162         if (smbXcli_conn_has_async_calls(cli->conn)) {
163                 /*
164                  * Can't use sync call while an async call is in flight
165                  */
166                 status = NT_STATUS_INVALID_PARAMETER;
167                 goto fail;
168         }
169         ev = tevent_context_init(frame);
170         if (ev == NULL) {
171                 goto fail;
172         }
173         req = smb2cli_tcon_send(frame, ev, cli, share);
174         if (req == NULL) {
175                 goto fail;
176         }
177         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
178                 goto fail;
179         }
180         status = smb2cli_tcon_recv(req);
181  fail:
182         TALLOC_FREE(frame);
183         return status;
184 }
185
186 struct smb2cli_tdis_state {
187         struct cli_state *cli;
188         uint8_t fixed[4];
189 };
190
191 static void smb2cli_tdis_done(struct tevent_req *subreq);
192
193 struct tevent_req *smb2cli_tdis_send(TALLOC_CTX *mem_ctx,
194                                       struct tevent_context *ev,
195                                       struct cli_state *cli)
196 {
197         struct tevent_req *req, *subreq;
198         struct smb2cli_tdis_state *state;
199
200         req = tevent_req_create(mem_ctx, &state,
201                                 struct smb2cli_tdis_state);
202         if (req == NULL) {
203                 return NULL;
204         }
205         state->cli = cli;
206         SSVAL(state->fixed, 0, 4);
207
208         subreq = smb2cli_req_send(state, ev, cli->conn, SMB2_OP_TDIS,
209                                   0, 0, /* flags */
210                                   cli->timeout,
211                                   cli->smb2.tcon,
212                                   cli->smb2.session,
213                                   state->fixed, sizeof(state->fixed),
214                                   NULL, 0);
215         if (tevent_req_nomem(subreq, req)) {
216                 return tevent_req_post(req, ev);
217         }
218         tevent_req_set_callback(subreq, smb2cli_tdis_done, req);
219         return req;
220 }
221
222 static void smb2cli_tdis_done(struct tevent_req *subreq)
223 {
224         struct tevent_req *req =
225                 tevent_req_callback_data(subreq,
226                 struct tevent_req);
227         struct smb2cli_tdis_state *state =
228                 tevent_req_data(req,
229                 struct smb2cli_tdis_state);
230         NTSTATUS status;
231         static const struct smb2cli_req_expected_response expected[] = {
232         {
233                 .status = NT_STATUS_OK,
234                 .body_size = 0x04
235         }
236         };
237
238         status = smb2cli_req_recv(subreq, NULL, NULL,
239                                   expected, ARRAY_SIZE(expected));
240         TALLOC_FREE(subreq);
241         if (tevent_req_nterror(req, status)) {
242                 return;
243         }
244         TALLOC_FREE(state->cli->smb2.tcon);
245         tevent_req_done(req);
246 }
247
248 NTSTATUS smb2cli_tdis_recv(struct tevent_req *req)
249 {
250         return tevent_req_simple_recv_ntstatus(req);
251 }
252
253 NTSTATUS smb2cli_tdis(struct cli_state *cli)
254 {
255         TALLOC_CTX *frame = talloc_stackframe();
256         struct tevent_context *ev;
257         struct tevent_req *req;
258         NTSTATUS status = NT_STATUS_NO_MEMORY;
259
260         if (smbXcli_conn_has_async_calls(cli->conn)) {
261                 /*
262                  * Can't use sync call while an async call is in flight
263                  */
264                 status = NT_STATUS_INVALID_PARAMETER;
265                 goto fail;
266         }
267         ev = tevent_context_init(frame);
268         if (ev == NULL) {
269                 goto fail;
270         }
271         req = smb2cli_tdis_send(frame, ev, cli);
272         if (req == NULL) {
273                 goto fail;
274         }
275         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
276                 goto fail;
277         }
278         status = smb2cli_tdis_recv(req);
279  fail:
280         TALLOC_FREE(frame);
281         return status;
282 }