s3:smb2cli: SMB2_TCON needs one dyn byte to that the structure size check works.
[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 "smb2cli_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(talloc_tos(), "\\\\%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, talloc_get_size(tcon_share),
64                                    &dyn, &dyn_len)) {
65                 tevent_req_oom(req);
66                 return tevent_req_post(req, ev);
67         }
68
69         fixed = state->fixed;
70         SSVAL(fixed, 0, 9);
71         SSVAL(fixed, 4, SMB2_HDR_BODY + 8);
72         SSVAL(fixed, 6, dyn_len);
73
74         if (dyn_len == 0) {
75                 dyn = state->dyn_pad;;
76                 dyn_len = sizeof(state->dyn_pad);
77         }
78
79         subreq = smb2cli_req_send(state, ev, cli, SMB2_OP_TCON,
80                                   0, 0, /* flags */
81                                   cli->smb2.pid,
82                                   0, /* tid */
83                                   cli->smb2.uid,
84                                   state->fixed, sizeof(state->fixed),
85                                   dyn, dyn_len);
86         if (tevent_req_nomem(subreq, req)) {
87                 return tevent_req_post(req, ev);
88         }
89         tevent_req_set_callback(subreq, smb2cli_tcon_done, req);
90         return req;
91 }
92
93 static void smb2cli_tcon_done(struct tevent_req *subreq)
94 {
95         struct tevent_req *req = tevent_req_callback_data(
96                 subreq, struct tevent_req);
97         struct smb2cli_tcon_state *state = tevent_req_data(
98                 req, struct smb2cli_tcon_state);
99         struct cli_state *cli = state->cli;
100         NTSTATUS status;
101         struct iovec *iov;
102         uint8_t *body;
103
104         status = smb2cli_req_recv(subreq, talloc_tos(), &iov, 16);
105         if (!NT_STATUS_IS_OK(status)) {
106                 TALLOC_FREE(subreq);
107                 tevent_req_nterror(req, status);
108                 return;
109         }
110
111         cli->smb2.tid = IVAL(iov[0].iov_base, SMB2_HDR_TID);
112
113         body = (uint8_t *)iov[1].iov_base;
114         cli->smb2.share_type            = CVAL(body, 2);
115         cli->smb2.share_flags           = IVAL(body, 4);
116         cli->capabilities               = IVAL(body, 8);
117         cli->smb2.maximal_access        = IVAL(body, 12);
118
119         TALLOC_FREE(subreq);
120         tevent_req_done(req);
121 }
122
123 NTSTATUS smb2cli_tcon_recv(struct tevent_req *req)
124 {
125         return tevent_req_simple_recv_ntstatus(req);
126 }
127
128 NTSTATUS smb2cli_tcon(struct cli_state *cli, const char *share)
129 {
130         TALLOC_CTX *frame = talloc_stackframe();
131         struct event_context *ev;
132         struct tevent_req *req;
133         NTSTATUS status = NT_STATUS_NO_MEMORY;
134
135         if (cli_has_async_calls(cli)) {
136                 /*
137                  * Can't use sync call while an async call is in flight
138                  */
139                 status = NT_STATUS_INVALID_PARAMETER;
140                 goto fail;
141         }
142         ev = event_context_init(frame);
143         if (ev == NULL) {
144                 goto fail;
145         }
146         req = smb2cli_tcon_send(frame, ev, cli, share);
147         if (req == NULL) {
148                 goto fail;
149         }
150         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
151                 goto fail;
152         }
153         status = smb2cli_tcon_recv(req);
154  fail:
155         TALLOC_FREE(frame);
156         return status;
157 }
158
159 struct smb2cli_tdis_state {
160         uint8_t fixed[4];
161 };
162
163 static void smb2cli_tdis_done(struct tevent_req *subreq);
164
165 struct tevent_req *smb2cli_tdis_send(TALLOC_CTX *mem_ctx,
166                                       struct tevent_context *ev,
167                                       struct cli_state *cli)
168 {
169         struct tevent_req *req, *subreq;
170         struct smb2cli_tdis_state *state;
171
172         req = tevent_req_create(mem_ctx, &state,
173                                 struct smb2cli_tdis_state);
174         if (req == NULL) {
175                 return NULL;
176         }
177         SSVAL(state->fixed, 0, 4);
178
179         subreq = smb2cli_req_send(state, ev, cli, SMB2_OP_TDIS,
180                                   0, 0, /* flags */
181                                   cli->smb2.pid,
182                                   cli->smb2.tid,
183                                   cli->smb2.uid,
184                                   state->fixed, sizeof(state->fixed),
185                                   NULL, 0);
186         if (tevent_req_nomem(subreq, req)) {
187                 return tevent_req_post(req, ev);
188         }
189         tevent_req_set_callback(subreq, smb2cli_tdis_done, req);
190         return req;
191 }
192
193 static void smb2cli_tdis_done(struct tevent_req *subreq)
194 {
195         struct tevent_req *req =
196                 tevent_req_callback_data(subreq,
197                 struct tevent_req);
198         NTSTATUS status;
199         struct iovec *iov;
200
201         status = smb2cli_req_recv(subreq, talloc_tos(), &iov, 4);
202         TALLOC_FREE(subreq);
203         if (tevent_req_nterror(req, status)) {
204                 return;
205         }
206         tevent_req_done(req);
207 }
208
209 NTSTATUS smb2cli_tdis_recv(struct tevent_req *req)
210 {
211         return tevent_req_simple_recv_ntstatus(req);
212 }
213
214 NTSTATUS smb2cli_tdis(struct cli_state *cli)
215 {
216         TALLOC_CTX *frame = talloc_stackframe();
217         struct event_context *ev;
218         struct tevent_req *req;
219         NTSTATUS status = NT_STATUS_NO_MEMORY;
220
221         if (cli_has_async_calls(cli)) {
222                 /*
223                  * Can't use sync call while an async call is in flight
224                  */
225                 status = NT_STATUS_INVALID_PARAMETER;
226                 goto fail;
227         }
228         ev = event_context_init(frame);
229         if (ev == NULL) {
230                 goto fail;
231         }
232         req = smb2cli_tdis_send(frame, ev, cli);
233         if (req == NULL) {
234                 goto fail;
235         }
236         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
237                 goto fail;
238         }
239         status = smb2cli_tdis_recv(req);
240  fail:
241         TALLOC_FREE(frame);
242         return status;
243 }