Move lp_*() calls a bit higher up the calls tack.
[kai/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
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         struct smbcli_options options;
37         struct smb2_negprot negprot;
38         struct smb2_tree_connect tcon;
39         struct smb2_session *session;
40         struct smb2_tree *tree;
41 };
42
43 /*
44   continue after tcon reply
45 */
46 static void continue_tcon(struct smb2_request *req)
47 {
48         struct composite_context *c = talloc_get_type(req->async.private_data, 
49                                                       struct composite_context);
50         struct smb2_connect_state *state = talloc_get_type(c->private_data, 
51                                                            struct smb2_connect_state);
52
53         c->status = smb2_tree_connect_recv(req, &state->tcon);
54         if (!composite_is_ok(c)) return;
55         
56         state->tree->tid = state->tcon.out.tid;
57
58         composite_done(c);
59 }
60
61 /*
62   continue after a session setup
63 */
64 static void continue_session(struct composite_context *creq)
65 {
66         struct composite_context *c = talloc_get_type(creq->async.private_data, 
67                                                       struct composite_context);
68         struct smb2_connect_state *state = talloc_get_type(c->private_data, 
69                                                            struct smb2_connect_state);
70         struct smb2_request *req;
71
72         c->status = smb2_session_setup_spnego_recv(creq);
73         if (!composite_is_ok(c)) return;
74
75         state->tree = smb2_tree_init(state->session, state, true);
76         if (composite_nomem(state->tree, c)) return;
77
78         state->tcon.in.reserved = 0;
79         state->tcon.in.path     = talloc_asprintf(state, "\\\\%s\\%s", 
80                                                   state->host, state->share);
81         if (composite_nomem(state->tcon.in.path, c)) return;
82         
83         req = smb2_tree_connect_send(state->tree, &state->tcon);
84         if (composite_nomem(req, c)) return;
85
86         req->async.fn = continue_tcon;
87         req->async.private_data = c;    
88 }
89
90 /*
91   continue after negprot reply
92 */
93 static void continue_negprot(struct smb2_request *req)
94 {
95         struct composite_context *c = talloc_get_type(req->async.private_data, 
96                                                       struct composite_context);
97         struct smb2_connect_state *state = talloc_get_type(c->private_data, 
98                                                            struct smb2_connect_state);
99         struct smb2_transport *transport = req->transport;
100         struct composite_context *creq;
101
102         c->status = smb2_negprot_recv(req, c, &state->negprot);
103         if (!composite_is_ok(c)) return;
104
105         transport->negotiate.system_time = state->negprot.out.system_time;
106         transport->negotiate.server_start_time = state->negprot.out.server_start_time;
107         transport->negotiate.security_mode = state->negprot.out.security_mode;
108
109         switch (transport->options.signing) {
110         case SMB_SIGNING_OFF:
111                 if (transport->negotiate.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
112                         composite_error(c, NT_STATUS_ACCESS_DENIED);
113                         return;
114                 }
115                 transport->signing_required = false;
116                 break;
117         case SMB_SIGNING_SUPPORTED:
118                 if (transport->negotiate.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
119                         transport->signing_required = true;
120                 } else {
121                         transport->signing_required = false;
122                 }
123                 break;
124         case SMB_SIGNING_AUTO:
125                 if (transport->negotiate.security_mode & SMB2_NEGOTIATE_SIGNING_ENABLED) {
126                         transport->signing_required = true;
127                 } else {
128                         transport->signing_required = false;
129                 }
130                 break;
131         case SMB_SIGNING_REQUIRED:
132                 if (transport->negotiate.security_mode & SMB2_NEGOTIATE_SIGNING_ENABLED) {
133                         transport->signing_required = true;
134                 } else {
135                         composite_error(c, NT_STATUS_ACCESS_DENIED);
136                         return;
137                 }
138                 break;
139         }
140
141         state->session = smb2_session_init(transport, global_loadparm, state, true);
142         if (composite_nomem(state->session, c)) return;
143
144         creq = smb2_session_setup_spnego_send(state->session, state->credentials);
145
146         composite_continue(c, creq, continue_session, c);
147 }
148
149 /*
150   continue after a socket connect completes
151 */
152 static void continue_socket(struct composite_context *creq)
153 {
154         struct composite_context *c = talloc_get_type(creq->async.private_data, 
155                                                       struct composite_context);
156         struct smb2_connect_state *state = talloc_get_type(c->private_data, 
157                                                            struct smb2_connect_state);
158         struct smbcli_socket *sock;
159         struct smb2_transport *transport;
160         struct smb2_request *req;
161         uint16_t dialects[2];
162
163         c->status = smbcli_sock_connect_recv(creq, state, &sock);
164         if (!composite_is_ok(c)) return;
165
166         transport = smb2_transport_init(sock, state, &state->options);
167         if (composite_nomem(transport, c)) return;
168
169         ZERO_STRUCT(state->negprot);
170         state->negprot.in.dialect_count = 2;
171         switch (transport->options.signing) {
172         case SMB_SIGNING_OFF:
173                 state->negprot.in.security_mode = 0;
174                 break;
175         case SMB_SIGNING_SUPPORTED:
176         case SMB_SIGNING_AUTO:
177                 state->negprot.in.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
178                 break;
179         case SMB_SIGNING_REQUIRED:
180                 state->negprot.in.security_mode = 
181                         SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED;
182                 break;
183         }
184         state->negprot.in.capabilities  = 0;
185         unix_to_nt_time(&state->negprot.in.start_time, time(NULL));
186         dialects[0] = SMB2_DIALECT_REVISION;
187         dialects[1] = 0;
188         state->negprot.in.dialects = dialects;
189
190         req = smb2_negprot_send(transport, &state->negprot);
191         if (composite_nomem(req, c)) return;
192
193         req->async.fn = continue_negprot;
194         req->async.private_data = c;
195 }
196
197
198 /*
199   continue after a resolve finishes
200 */
201 static void continue_resolve(struct composite_context *creq)
202 {
203         struct composite_context *c = talloc_get_type(creq->async.private_data, 
204                                                       struct composite_context);
205         struct smb2_connect_state *state = talloc_get_type(c->private_data, 
206                                                            struct smb2_connect_state);
207         const char *addr;
208         const char **ports;
209         const char *default_ports[] = { "445", NULL };
210
211         ports = lp_parm_string_list(state, global_loadparm, NULL, "smb2", "ports", NULL);
212         if (ports == NULL) {
213                 ports = default_ports;
214         }
215
216         c->status = resolve_name_recv(creq, state, &addr);
217         if (!composite_is_ok(c)) return;
218
219         creq = smbcli_sock_connect_send(state, addr, ports, state->host, state->resolve_ctx, c->event_ctx, lp_socket_options(global_loadparm));
220
221         composite_continue(c, creq, continue_socket, c);
222 }
223
224 /*
225   a composite function that does a full negprot/sesssetup/tcon, returning
226   a connected smb2_tree
227  */
228 struct composite_context *smb2_connect_send(TALLOC_CTX *mem_ctx,
229                                             const char *host,
230                                             const char *share,
231                                             struct resolve_context *resolve_ctx,
232                                             struct cli_credentials *credentials,
233                                             struct event_context *ev,
234                                             struct smbcli_options *options)
235 {
236         struct composite_context *c;
237         struct smb2_connect_state *state;
238         struct nbt_name name;
239         struct composite_context *creq;
240
241         c = composite_create(mem_ctx, ev);
242         if (c == NULL) return NULL;
243
244         state = talloc(c, struct smb2_connect_state);
245         if (composite_nomem(state, c)) return c;
246         c->private_data = state;
247
248         state->credentials = credentials;
249         state->options = *options;
250         state->host = talloc_strdup(c, host);
251         if (composite_nomem(state->host, c)) return c;
252         state->share = talloc_strdup(c, share);
253         if (composite_nomem(state->share, c)) return c;
254         state->resolve_ctx = talloc_reference(state, resolve_ctx);
255
256         ZERO_STRUCT(name);
257         name.name = host;
258
259         creq = resolve_name_send(resolve_ctx, &name, c->event_ctx);
260         composite_continue(c, creq, continue_resolve, c);
261         return c;
262 }
263
264 /*
265   receive a connect reply
266 */
267 NTSTATUS smb2_connect_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
268                            struct smb2_tree **tree)
269 {
270         NTSTATUS status;
271         struct smb2_connect_state *state = talloc_get_type(c->private_data, 
272                                                            struct smb2_connect_state);
273         status = composite_wait(c);
274         if (NT_STATUS_IS_OK(status)) {
275                 *tree = talloc_steal(mem_ctx, state->tree);
276         }
277         talloc_free(c);
278         return status;
279 }
280
281 /*
282   sync version of smb2_connect
283 */
284 NTSTATUS smb2_connect(TALLOC_CTX *mem_ctx, 
285                       const char *host, const char *share,
286                       struct resolve_context *resolve_ctx,
287                       struct cli_credentials *credentials,
288                       struct smb2_tree **tree,
289                       struct event_context *ev,
290                       struct smbcli_options *options)
291 {
292         struct composite_context *c = smb2_connect_send(mem_ctx, host, share, 
293                                                         resolve_ctx,
294                                                         credentials, ev, options);
295         return smb2_connect_recv(c, mem_ctx, tree);
296 }