8aa2a31296d0a69e0fc390054a0723d182c206cc
[mat/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), cli_state_remote_sockaddr(cli));
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                                   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);
94         }
95         tevent_req_set_callback(subreq, smb2cli_tcon_done, req);
96         return req;
97 }
98
99 static void smb2cli_tcon_done(struct tevent_req *subreq)
100 {
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         static const struct smb2cli_req_expected_response expected[] = {
110         {
111                 .status = NT_STATUS_OK,
112                 .body_size = 0x10
113         }
114         };
115
116         status = smb2cli_req_recv(subreq, state, &iov,
117                                   expected, ARRAY_SIZE(expected));
118         if (!NT_STATUS_IS_OK(status)) {
119                 TALLOC_FREE(subreq);
120                 tevent_req_nterror(req, status);
121                 return;
122         }
123
124         cli->smb2.tid = IVAL(iov[0].iov_base, SMB2_HDR_TID);
125
126         body = (uint8_t *)iov[1].iov_base;
127         cli->smb2.share_type            = CVAL(body, 2);
128         cli->smb2.share_flags           = IVAL(body, 4);
129         cli->smb2.share_capabilities    = IVAL(body, 8);
130         cli->smb2.maximal_access        = IVAL(body, 12);
131
132         TALLOC_FREE(subreq);
133         tevent_req_done(req);
134 }
135
136 NTSTATUS smb2cli_tcon_recv(struct tevent_req *req)
137 {
138         return tevent_req_simple_recv_ntstatus(req);
139 }
140
141 NTSTATUS smb2cli_tcon(struct cli_state *cli, const char *share)
142 {
143         TALLOC_CTX *frame = talloc_stackframe();
144         struct tevent_context *ev;
145         struct tevent_req *req;
146         NTSTATUS status = NT_STATUS_NO_MEMORY;
147
148         if (cli_has_async_calls(cli)) {
149                 /*
150                  * Can't use sync call while an async call is in flight
151                  */
152                 status = NT_STATUS_INVALID_PARAMETER;
153                 goto fail;
154         }
155         ev = tevent_context_init(frame);
156         if (ev == NULL) {
157                 goto fail;
158         }
159         req = smb2cli_tcon_send(frame, ev, cli, share);
160         if (req == NULL) {
161                 goto fail;
162         }
163         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
164                 goto fail;
165         }
166         status = smb2cli_tcon_recv(req);
167  fail:
168         TALLOC_FREE(frame);
169         return status;
170 }
171
172 struct smb2cli_tdis_state {
173         struct cli_state *cli;
174         uint8_t fixed[4];
175 };
176
177 static void smb2cli_tdis_done(struct tevent_req *subreq);
178
179 struct tevent_req *smb2cli_tdis_send(TALLOC_CTX *mem_ctx,
180                                       struct tevent_context *ev,
181                                       struct cli_state *cli)
182 {
183         struct tevent_req *req, *subreq;
184         struct smb2cli_tdis_state *state;
185
186         req = tevent_req_create(mem_ctx, &state,
187                                 struct smb2cli_tdis_state);
188         if (req == NULL) {
189                 return NULL;
190         }
191         state->cli = cli;
192         SSVAL(state->fixed, 0, 4);
193
194         subreq = smb2cli_req_send(state, ev, cli->conn, SMB2_OP_TDIS,
195                                   0, 0, /* flags */
196                                   cli->timeout,
197                                   cli->smb2.pid,
198                                   cli->smb2.tid,
199                                   cli->smb2.session,
200                                   state->fixed, sizeof(state->fixed),
201                                   NULL, 0);
202         if (tevent_req_nomem(subreq, req)) {
203                 return tevent_req_post(req, ev);
204         }
205         tevent_req_set_callback(subreq, smb2cli_tdis_done, req);
206         return req;
207 }
208
209 static void smb2cli_tdis_done(struct tevent_req *subreq)
210 {
211         struct tevent_req *req =
212                 tevent_req_callback_data(subreq,
213                 struct tevent_req);
214         struct smb2cli_tdis_state *state =
215                 tevent_req_data(req,
216                 struct smb2cli_tdis_state);
217         NTSTATUS status;
218         static const struct smb2cli_req_expected_response expected[] = {
219         {
220                 .status = NT_STATUS_OK,
221                 .body_size = 0x04
222         }
223         };
224
225         status = smb2cli_req_recv(subreq, NULL, NULL,
226                                   expected, ARRAY_SIZE(expected));
227         TALLOC_FREE(subreq);
228         if (tevent_req_nterror(req, status)) {
229                 return;
230         }
231         state->cli->smb2.tid = 0;
232         tevent_req_done(req);
233 }
234
235 NTSTATUS smb2cli_tdis_recv(struct tevent_req *req)
236 {
237         return tevent_req_simple_recv_ntstatus(req);
238 }
239
240 NTSTATUS smb2cli_tdis(struct cli_state *cli)
241 {
242         TALLOC_CTX *frame = talloc_stackframe();
243         struct tevent_context *ev;
244         struct tevent_req *req;
245         NTSTATUS status = NT_STATUS_NO_MEMORY;
246
247         if (cli_has_async_calls(cli)) {
248                 /*
249                  * Can't use sync call while an async call is in flight
250                  */
251                 status = NT_STATUS_INVALID_PARAMETER;
252                 goto fail;
253         }
254         ev = tevent_context_init(frame);
255         if (ev == NULL) {
256                 goto fail;
257         }
258         req = smb2cli_tdis_send(frame, ev, cli);
259         if (req == NULL) {
260                 goto fail;
261         }
262         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
263                 goto fail;
264         }
265         status = smb2cli_tdis_recv(req);
266  fail:
267         TALLOC_FREE(frame);
268         return status;
269 }