Fix the build.
[ira/wip.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
31 struct smb2_connect_state {
32         struct cli_credentials *credentials;
33         struct resolve_context *resolve_ctx;
34         const char *host;
35         const char *share;
36         const char **ports;
37         const char *socket_options;
38         struct smbcli_options options;
39         struct smb2_negprot negprot;
40         struct smb2_tree_connect tcon;
41         struct smb2_session *session;
42         struct smb2_tree *tree;
43 };
44
45 /*
46   continue after tcon reply
47 */
48 static void continue_tcon(struct smb2_request *req)
49 {
50         struct composite_context *c = talloc_get_type(req->async.private_data, 
51                                                       struct composite_context);
52         struct smb2_connect_state *state = talloc_get_type(c->private_data, 
53                                                            struct smb2_connect_state);
54
55         c->status = smb2_tree_connect_recv(req, &state->tcon);
56         if (!composite_is_ok(c)) return;
57         
58         state->tree->tid = state->tcon.out.tid;
59
60         composite_done(c);
61 }
62
63 /*
64   continue after a session setup
65 */
66 static void continue_session(struct composite_context *creq)
67 {
68         struct composite_context *c = talloc_get_type(creq->async.private_data, 
69                                                       struct composite_context);
70         struct smb2_connect_state *state = talloc_get_type(c->private_data, 
71                                                            struct smb2_connect_state);
72         struct smb2_request *req;
73
74         c->status = smb2_session_setup_spnego_recv(creq);
75         if (!composite_is_ok(c)) return;
76
77         state->tree = smb2_tree_init(state->session, state, true);
78         if (composite_nomem(state->tree, c)) return;
79
80         state->tcon.in.reserved = 0;
81         state->tcon.in.path     = talloc_asprintf(state, "\\\\%s\\%s", 
82                                                   state->host, state->share);
83         if (composite_nomem(state->tcon.in.path, c)) return;
84         
85         req = smb2_tree_connect_send(state->tree, &state->tcon);
86         if (composite_nomem(req, c)) return;
87
88         req->async.fn = continue_tcon;
89         req->async.private_data = c;    
90 }
91
92 /*
93   continue after negprot reply
94 */
95 static void continue_negprot(struct smb2_request *req)
96 {
97         struct composite_context *c = talloc_get_type(req->async.private_data, 
98                                                       struct composite_context);
99         struct smb2_connect_state *state = talloc_get_type(c->private_data, 
100                                                            struct smb2_connect_state);
101         struct smb2_transport *transport = req->transport;
102         struct composite_context *creq;
103
104         c->status = smb2_negprot_recv(req, c, &state->negprot);
105         if (!composite_is_ok(c)) return;
106
107         transport->negotiate.system_time = state->negprot.out.system_time;
108         transport->negotiate.server_start_time = state->negprot.out.server_start_time;
109         transport->negotiate.security_mode = state->negprot.out.security_mode;
110
111         switch (transport->options.signing) {
112         case SMB_SIGNING_OFF:
113                 if (transport->negotiate.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
114                         composite_error(c, NT_STATUS_ACCESS_DENIED);
115                         return;
116                 }
117                 transport->signing_required = false;
118                 break;
119         case SMB_SIGNING_SUPPORTED:
120                 if (transport->negotiate.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
121                         transport->signing_required = true;
122                 } else {
123                         transport->signing_required = false;
124                 }
125                 break;
126         case SMB_SIGNING_AUTO:
127                 if (transport->negotiate.security_mode & SMB2_NEGOTIATE_SIGNING_ENABLED) {
128                         transport->signing_required = true;
129                 } else {
130                         transport->signing_required = false;
131                 }
132                 break;
133         case SMB_SIGNING_REQUIRED:
134                 if (transport->negotiate.security_mode & SMB2_NEGOTIATE_SIGNING_ENABLED) {
135                         transport->signing_required = true;
136                 } else {
137                         composite_error(c, NT_STATUS_ACCESS_DENIED);
138                         return;
139                 }
140                 break;
141         }
142
143         state->session = smb2_session_init(transport, lp_gensec_settings(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         c->status = resolve_name_recv(creq, state, &addr);
214         if (!composite_is_ok(c)) return;
215
216         if (state->ports == NULL) {
217                 ports = default_ports;
218         } else {
219                 ports = state->ports;
220         }
221
222         creq = smbcli_sock_connect_send(state, addr, ports, state->host, state->resolve_ctx, c->event_ctx, state->socket_options);
223
224         composite_continue(c, creq, continue_socket, c);
225 }
226
227 /*
228   a composite function that does a full negprot/sesssetup/tcon, returning
229   a connected smb2_tree
230  */
231 struct composite_context *smb2_connect_send(TALLOC_CTX *mem_ctx,
232                                             const char *host,
233                                                 const char **ports,
234                                             const char *share,
235                                             struct resolve_context *resolve_ctx,
236                                             struct cli_credentials *credentials,
237                                             struct event_context *ev,
238                                             struct smbcli_options *options,
239                                                 const char *socket_options)
240 {
241         struct composite_context *c;
242         struct smb2_connect_state *state;
243         struct nbt_name name;
244         struct composite_context *creq;
245
246         c = composite_create(mem_ctx, ev);
247         if (c == NULL) return NULL;
248
249         state = talloc(c, struct smb2_connect_state);
250         if (composite_nomem(state, c)) return c;
251         c->private_data = state;
252
253         state->credentials = credentials;
254         state->options = *options;
255         state->host = talloc_strdup(c, host);
256         if (composite_nomem(state->host, c)) return c;
257         state->ports = talloc_reference(state, ports);
258         state->share = talloc_strdup(c, share);
259         if (composite_nomem(state->share, c)) return c;
260         state->resolve_ctx = talloc_reference(state, resolve_ctx);
261         state->socket_options = talloc_reference(state, socket_options);
262         if (composite_nomem(state->socket_options, c)) return c;
263
264         ZERO_STRUCT(name);
265         name.name = host;
266
267         creq = resolve_name_send(resolve_ctx, &name, c->event_ctx);
268         composite_continue(c, creq, continue_resolve, c);
269         return c;
270 }
271
272 /*
273   receive a connect reply
274 */
275 NTSTATUS smb2_connect_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
276                            struct smb2_tree **tree)
277 {
278         NTSTATUS status;
279         struct smb2_connect_state *state = talloc_get_type(c->private_data, 
280                                                            struct smb2_connect_state);
281         status = composite_wait(c);
282         if (NT_STATUS_IS_OK(status)) {
283                 *tree = talloc_steal(mem_ctx, state->tree);
284         }
285         talloc_free(c);
286         return status;
287 }
288
289 /*
290   sync version of smb2_connect
291 */
292 NTSTATUS smb2_connect(TALLOC_CTX *mem_ctx, 
293                       const char *host, const char **ports, 
294                           const char *share,
295                       struct resolve_context *resolve_ctx,
296                       struct cli_credentials *credentials,
297                       struct smb2_tree **tree,
298                       struct event_context *ev,
299                       struct smbcli_options *options,
300                           const char *socket_options)
301 {
302         struct composite_context *c = smb2_connect_send(mem_ctx, host, ports, 
303                                                                                                         share, resolve_ctx, 
304                                                                                                         credentials, ev, options,
305                                                                                                         socket_options);
306         return smb2_connect_recv(c, mem_ctx, tree);
307 }