- use the current dialect first, for servers that only look at the
[tridge/samba.git] / source4 / libcli / smb2 / connect.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    SMB2 composite connection setup
5
6    Copyright (C) Andrew Tridgell 2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "libcli/raw/libcliraw.h"
24 #include "libcli/raw/raw_proto.h"
25 #include "libcli/smb2/smb2.h"
26 #include "libcli/smb2/smb2_calls.h"
27 #include "libcli/composite/composite.h"
28 #include "libcli/resolve/resolve.h"
29 #include "param/param.h"
30 #include "lib/cmdline/popt_common.h"
31
32 struct smb2_connect_state {
33         struct cli_credentials *credentials;
34         struct resolve_context *resolve_ctx;
35         const char *host;
36         const char *share;
37         struct smbcli_options options;
38         struct smb2_negprot negprot;
39         struct smb2_tree_connect tcon;
40         struct smb2_session *session;
41         struct smb2_tree *tree;
42 };
43
44 /*
45   continue after tcon reply
46 */
47 static void continue_tcon(struct smb2_request *req)
48 {
49         struct composite_context *c = talloc_get_type(req->async.private_data, 
50                                                       struct composite_context);
51         struct smb2_connect_state *state = talloc_get_type(c->private_data, 
52                                                            struct smb2_connect_state);
53
54         c->status = smb2_tree_connect_recv(req, &state->tcon);
55         if (!composite_is_ok(c)) return;
56         
57         state->tree->tid = state->tcon.out.tid;
58
59         composite_done(c);
60 }
61
62 /*
63   continue after a session setup
64 */
65 static void continue_session(struct composite_context *creq)
66 {
67         struct composite_context *c = talloc_get_type(creq->async.private_data, 
68                                                       struct composite_context);
69         struct smb2_connect_state *state = talloc_get_type(c->private_data, 
70                                                            struct smb2_connect_state);
71         struct smb2_request *req;
72
73         c->status = smb2_session_setup_spnego_recv(creq);
74         if (!composite_is_ok(c)) return;
75
76         state->tree = smb2_tree_init(state->session, state, true);
77         if (composite_nomem(state->tree, c)) return;
78
79         state->tcon.in.reserved = 0;
80         state->tcon.in.path     = talloc_asprintf(state, "\\\\%s\\%s", 
81                                                   state->host, state->share);
82         if (composite_nomem(state->tcon.in.path, c)) return;
83         
84         req = smb2_tree_connect_send(state->tree, &state->tcon);
85         if (composite_nomem(req, c)) return;
86
87         req->async.fn = continue_tcon;
88         req->async.private_data = c;    
89 }
90
91 /*
92   continue after negprot reply
93 */
94 static void continue_negprot(struct smb2_request *req)
95 {
96         struct composite_context *c = talloc_get_type(req->async.private_data, 
97                                                       struct composite_context);
98         struct smb2_connect_state *state = talloc_get_type(c->private_data, 
99                                                            struct smb2_connect_state);
100         struct smb2_transport *transport = req->transport;
101         struct composite_context *creq;
102
103         c->status = smb2_negprot_recv(req, c, &state->negprot);
104         if (!composite_is_ok(c)) return;
105
106         transport->negotiate.system_time = state->negprot.out.system_time;
107         transport->negotiate.server_start_time = state->negprot.out.server_start_time;
108         transport->negotiate.security_mode = state->negprot.out.security_mode;
109
110         switch (transport->options.signing) {
111         case SMB_SIGNING_OFF:
112                 if (transport->negotiate.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
113                         composite_error(c, NT_STATUS_ACCESS_DENIED);
114                         return;
115                 }
116                 transport->signing_required = false;
117                 break;
118         case SMB_SIGNING_SUPPORTED:
119                 if (transport->negotiate.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
120                         transport->signing_required = true;
121                 } else {
122                         transport->signing_required = false;
123                 }
124                 break;
125         case SMB_SIGNING_AUTO:
126                 if (transport->negotiate.security_mode & SMB2_NEGOTIATE_SIGNING_ENABLED) {
127                         transport->signing_required = true;
128                 } else {
129                         transport->signing_required = false;
130                 }
131                 break;
132         case SMB_SIGNING_REQUIRED:
133                 if (transport->negotiate.security_mode & SMB2_NEGOTIATE_SIGNING_ENABLED) {
134                         transport->signing_required = true;
135                 } else {
136                         composite_error(c, NT_STATUS_ACCESS_DENIED);
137                         return;
138                 }
139                 break;
140         }
141         
142
143         state->session = smb2_session_init(transport, global_loadparm, state, true);
144         if (composite_nomem(state->session, c)) return;
145
146         creq = smb2_session_setup_spnego_send(state->session, state->credentials);
147
148         composite_continue(c, creq, continue_session, c);
149 }
150
151 /*
152   continue after a socket connect completes
153 */
154 static void continue_socket(struct composite_context *creq)
155 {
156         struct composite_context *c = talloc_get_type(creq->async.private_data, 
157                                                       struct composite_context);
158         struct smb2_connect_state *state = talloc_get_type(c->private_data, 
159                                                            struct smb2_connect_state);
160         struct smbcli_socket *sock;
161         struct smb2_transport *transport;
162         struct smb2_request *req;
163         uint16_t dialects[2];
164
165         c->status = smbcli_sock_connect_recv(creq, state, &sock);
166         if (!composite_is_ok(c)) return;
167
168         transport = smb2_transport_init(sock, state, &state->options);
169         if (composite_nomem(transport, c)) return;
170
171         ZERO_STRUCT(state->negprot);
172         state->negprot.in.dialect_count = 2;
173         switch (transport->options.signing) {
174         case SMB_SIGNING_OFF:
175                 state->negprot.in.security_mode = 0;
176                 break;
177         case SMB_SIGNING_SUPPORTED:
178         case SMB_SIGNING_AUTO:
179                 state->negprot.in.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
180                 break;
181         case SMB_SIGNING_REQUIRED:
182                 state->negprot.in.security_mode = 
183                         SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED;
184                 break;
185         }
186         state->negprot.in.capabilities  = 0;
187         unix_to_nt_time(&state->negprot.in.start_time, time(NULL));
188         dialects[0] = SMB2_DIALECT_REVISION;
189         dialects[1] = 0;
190         state->negprot.in.dialects = dialects;
191
192         req = smb2_negprot_send(transport, &state->negprot);
193         if (composite_nomem(req, c)) return;
194
195         req->async.fn = continue_negprot;
196         req->async.private_data = c;
197 }
198
199
200 /*
201   continue after a resolve finishes
202 */
203 static void continue_resolve(struct composite_context *creq)
204 {
205         struct composite_context *c = talloc_get_type(creq->async.private_data, 
206                                                       struct composite_context);
207         struct smb2_connect_state *state = talloc_get_type(c->private_data, 
208                                                            struct smb2_connect_state);
209         const char *addr;
210         const char **ports;
211         const char *default_ports[] = { "445", NULL };
212
213         ports = lp_parm_string_list(state, cmdline_lp_ctx, NULL, "smb2", "ports", NULL);
214         if (ports == NULL) {
215                 ports = default_ports;
216         }
217
218         c->status = resolve_name_recv(creq, state, &addr);
219         if (!composite_is_ok(c)) return;
220
221         creq = smbcli_sock_connect_send(state, addr, ports, state->host, state->resolve_ctx, c->event_ctx);
222
223         composite_continue(c, creq, continue_socket, c);
224 }
225
226 /*
227   a composite function that does a full negprot/sesssetup/tcon, returning
228   a connected smb2_tree
229  */
230 struct composite_context *smb2_connect_send(TALLOC_CTX *mem_ctx,
231                                             const char *host,
232                                             const char *share,
233                                             struct resolve_context *resolve_ctx,
234                                             struct cli_credentials *credentials,
235                                             struct event_context *ev,
236                                             struct smbcli_options *options)
237 {
238         struct composite_context *c;
239         struct smb2_connect_state *state;
240         struct nbt_name name;
241         struct composite_context *creq;
242
243         c = composite_create(mem_ctx, ev);
244         if (c == NULL) return NULL;
245
246         state = talloc(c, struct smb2_connect_state);
247         if (composite_nomem(state, c)) return c;
248         c->private_data = state;
249
250         state->credentials = credentials;
251         state->options = *options;
252         state->host = talloc_strdup(c, host);
253         if (composite_nomem(state->host, c)) return c;
254         state->share = talloc_strdup(c, share);
255         if (composite_nomem(state->share, c)) return c;
256         state->resolve_ctx = talloc_reference(state, resolve_ctx);
257
258         ZERO_STRUCT(name);
259         name.name = host;
260
261         creq = resolve_name_send(resolve_ctx, &name, c->event_ctx);
262         composite_continue(c, creq, continue_resolve, c);
263         return c;
264 }
265
266 /*
267   receive a connect reply
268 */
269 NTSTATUS smb2_connect_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
270                            struct smb2_tree **tree)
271 {
272         NTSTATUS status;
273         struct smb2_connect_state *state = talloc_get_type(c->private_data, 
274                                                            struct smb2_connect_state);
275         status = composite_wait(c);
276         if (NT_STATUS_IS_OK(status)) {
277                 *tree = talloc_steal(mem_ctx, state->tree);
278         }
279         talloc_free(c);
280         return status;
281 }
282
283 /*
284   sync version of smb2_connect
285 */
286 NTSTATUS smb2_connect(TALLOC_CTX *mem_ctx, 
287                       const char *host, const char *share,
288                       struct resolve_context *resolve_ctx,
289                       struct cli_credentials *credentials,
290                       struct smb2_tree **tree,
291                       struct event_context *ev,
292                       struct smbcli_options *options)
293 {
294         struct composite_context *c = smb2_connect_send(mem_ctx, host, share, 
295                                                         resolve_ctx,
296                                                         credentials, ev, options);
297         return smb2_connect_recv(c, mem_ctx, tree);
298 }