s3:libsmb: don't mix smb2 share capabilities with smb1 capabilities
[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, 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, SMB2_OP_TCON,
85                                   0, 0, /* flags */
86                                   cli->smb2.pid,
87                                   0, /* tid */
88                                   cli->smb2.uid,
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
109         status = smb2cli_req_recv(subreq, talloc_tos(), &iov, 16);
110         if (!NT_STATUS_IS_OK(status)) {
111                 TALLOC_FREE(subreq);
112                 tevent_req_nterror(req, status);
113                 return;
114         }
115
116         cli->smb2.tid = IVAL(iov[0].iov_base, SMB2_HDR_TID);
117
118         body = (uint8_t *)iov[1].iov_base;
119         cli->smb2.share_type            = CVAL(body, 2);
120         cli->smb2.share_flags           = IVAL(body, 4);
121         cli->smb2.share_capabilities    = IVAL(body, 8);
122         cli->smb2.maximal_access        = IVAL(body, 12);
123
124         TALLOC_FREE(subreq);
125         tevent_req_done(req);
126 }
127
128 NTSTATUS smb2cli_tcon_recv(struct tevent_req *req)
129 {
130         return tevent_req_simple_recv_ntstatus(req);
131 }
132
133 NTSTATUS smb2cli_tcon(struct cli_state *cli, const char *share)
134 {
135         TALLOC_CTX *frame = talloc_stackframe();
136         struct event_context *ev;
137         struct tevent_req *req;
138         NTSTATUS status = NT_STATUS_NO_MEMORY;
139
140         if (cli_has_async_calls(cli)) {
141                 /*
142                  * Can't use sync call while an async call is in flight
143                  */
144                 status = NT_STATUS_INVALID_PARAMETER;
145                 goto fail;
146         }
147         ev = event_context_init(frame);
148         if (ev == NULL) {
149                 goto fail;
150         }
151         req = smb2cli_tcon_send(frame, ev, cli, share);
152         if (req == NULL) {
153                 goto fail;
154         }
155         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
156                 goto fail;
157         }
158         status = smb2cli_tcon_recv(req);
159  fail:
160         TALLOC_FREE(frame);
161         return status;
162 }
163
164 struct smb2cli_tdis_state {
165         uint8_t fixed[4];
166 };
167
168 static void smb2cli_tdis_done(struct tevent_req *subreq);
169
170 struct tevent_req *smb2cli_tdis_send(TALLOC_CTX *mem_ctx,
171                                       struct tevent_context *ev,
172                                       struct cli_state *cli)
173 {
174         struct tevent_req *req, *subreq;
175         struct smb2cli_tdis_state *state;
176
177         req = tevent_req_create(mem_ctx, &state,
178                                 struct smb2cli_tdis_state);
179         if (req == NULL) {
180                 return NULL;
181         }
182         SSVAL(state->fixed, 0, 4);
183
184         subreq = smb2cli_req_send(state, ev, cli, SMB2_OP_TDIS,
185                                   0, 0, /* flags */
186                                   cli->smb2.pid,
187                                   cli->smb2.tid,
188                                   cli->smb2.uid,
189                                   state->fixed, sizeof(state->fixed),
190                                   NULL, 0);
191         if (tevent_req_nomem(subreq, req)) {
192                 return tevent_req_post(req, ev);
193         }
194         tevent_req_set_callback(subreq, smb2cli_tdis_done, req);
195         return req;
196 }
197
198 static void smb2cli_tdis_done(struct tevent_req *subreq)
199 {
200         struct tevent_req *req =
201                 tevent_req_callback_data(subreq,
202                 struct tevent_req);
203         NTSTATUS status;
204         struct iovec *iov;
205
206         status = smb2cli_req_recv(subreq, talloc_tos(), &iov, 4);
207         TALLOC_FREE(subreq);
208         if (tevent_req_nterror(req, status)) {
209                 return;
210         }
211         tevent_req_done(req);
212 }
213
214 NTSTATUS smb2cli_tdis_recv(struct tevent_req *req)
215 {
216         return tevent_req_simple_recv_ntstatus(req);
217 }
218
219 NTSTATUS smb2cli_tdis(struct cli_state *cli)
220 {
221         TALLOC_CTX *frame = talloc_stackframe();
222         struct event_context *ev;
223         struct tevent_req *req;
224         NTSTATUS status = NT_STATUS_NO_MEMORY;
225
226         if (cli_has_async_calls(cli)) {
227                 /*
228                  * Can't use sync call while an async call is in flight
229                  */
230                 status = NT_STATUS_INVALID_PARAMETER;
231                 goto fail;
232         }
233         ev = event_context_init(frame);
234         if (ev == NULL) {
235                 goto fail;
236         }
237         req = smb2cli_tdis_send(frame, ev, cli);
238         if (req == NULL) {
239                 goto fail;
240         }
241         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
242                 goto fail;
243         }
244         status = smb2cli_tdis_recv(req);
245  fail:
246         TALLOC_FREE(frame);
247         return status;
248 }