Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into v4-0-wsgi
[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 smb2_negprot negprot;
37         struct smb2_tree_connect tcon;
38         struct smb2_session *session;
39         struct smb2_tree *tree;
40 };
41
42 /*
43   continue after tcon reply
44 */
45 static void continue_tcon(struct smb2_request *req)
46 {
47         struct composite_context *c = talloc_get_type(req->async.private_data, 
48                                                       struct composite_context);
49         struct smb2_connect_state *state = talloc_get_type(c->private_data, 
50                                                            struct smb2_connect_state);
51
52         c->status = smb2_tree_connect_recv(req, &state->tcon);
53         if (!composite_is_ok(c)) return;
54         
55         state->tree->tid = state->tcon.out.tid;
56
57         composite_done(c);
58 }
59
60 /*
61   continue after a session setup
62 */
63 static void continue_session(struct composite_context *creq)
64 {
65         struct composite_context *c = talloc_get_type(creq->async.private_data, 
66                                                       struct composite_context);
67         struct smb2_connect_state *state = talloc_get_type(c->private_data, 
68                                                            struct smb2_connect_state);
69         struct smb2_request *req;
70
71         c->status = smb2_session_setup_spnego_recv(creq);
72         if (!composite_is_ok(c)) return;
73
74         state->tree = smb2_tree_init(state->session, state, true);
75         if (composite_nomem(state->tree, c)) return;
76
77         state->tcon.in.reserved = 0;
78         state->tcon.in.path     = talloc_asprintf(state, "\\\\%s\\%s", 
79                                                   state->host, state->share);
80         if (composite_nomem(state->tcon.in.path, c)) return;
81         
82         req = smb2_tree_connect_send(state->tree, &state->tcon);
83         if (composite_nomem(req, c)) return;
84
85         req->async.fn = continue_tcon;
86         req->async.private_data = c;    
87 }
88
89 /*
90   continue after negprot reply
91 */
92 static void continue_negprot(struct smb2_request *req)
93 {
94         struct composite_context *c = talloc_get_type(req->async.private_data, 
95                                                       struct composite_context);
96         struct smb2_connect_state *state = talloc_get_type(c->private_data, 
97                                                            struct smb2_connect_state);
98         struct smb2_transport *transport = req->transport;
99         struct composite_context *creq;
100
101         c->status = smb2_negprot_recv(req, c, &state->negprot);
102         if (!composite_is_ok(c)) return;
103
104         transport->negotiate.system_time = state->negprot.out.system_time;
105         transport->negotiate.server_start_time = state->negprot.out.server_start_time;
106
107         state->session = smb2_session_init(transport, global_loadparm, state, true);
108         if (composite_nomem(state->session, c)) return;
109
110         creq = smb2_session_setup_spnego_send(state->session, state->credentials);
111
112         composite_continue(c, creq, continue_session, c);
113 }
114
115 /*
116   continue after a socket connect completes
117 */
118 static void continue_socket(struct composite_context *creq)
119 {
120         struct composite_context *c = talloc_get_type(creq->async.private_data, 
121                                                       struct composite_context);
122         struct smb2_connect_state *state = talloc_get_type(c->private_data, 
123                                                            struct smb2_connect_state);
124         struct smbcli_socket *sock;
125         struct smb2_transport *transport;
126         struct smb2_request *req;
127         uint16_t dialects[2];
128
129         c->status = smbcli_sock_connect_recv(creq, state, &sock);
130         if (!composite_is_ok(c)) return;
131
132         transport = smb2_transport_init(sock, state);
133         if (composite_nomem(transport, c)) return;
134
135         ZERO_STRUCT(state->negprot);
136         state->negprot.in.dialect_count = 2;
137         state->negprot.in.security_mode = 0;
138         state->negprot.in.capabilities  = 0;
139         unix_to_nt_time(&state->negprot.in.start_time, time(NULL));
140         dialects[0] = 0;
141         dialects[1] = SMB2_DIALECT_REVISION;
142         state->negprot.in.dialects = dialects;
143
144         req = smb2_negprot_send(transport, &state->negprot);
145         if (composite_nomem(req, c)) return;
146
147         req->async.fn = continue_negprot;
148         req->async.private_data = c;
149 }
150
151
152 /*
153   continue after a resolve finishes
154 */
155 static void continue_resolve(struct composite_context *creq)
156 {
157         struct composite_context *c = talloc_get_type(creq->async.private_data, 
158                                                       struct composite_context);
159         struct smb2_connect_state *state = talloc_get_type(c->private_data, 
160                                                            struct smb2_connect_state);
161         const char *addr;
162         const char *ports[2] = { "445", NULL };
163
164         c->status = resolve_name_recv(creq, state, &addr);
165         if (!composite_is_ok(c)) return;
166
167         creq = smbcli_sock_connect_send(state, addr, ports, state->host, state->resolve_ctx, c->event_ctx);
168
169         composite_continue(c, creq, continue_socket, c);
170 }
171
172 /*
173   a composite function that does a full negprot/sesssetup/tcon, returning
174   a connected smb2_tree
175  */
176 struct composite_context *smb2_connect_send(TALLOC_CTX *mem_ctx,
177                                             const char *host,
178                                             const char *share,
179                                             struct resolve_context *resolve_ctx,
180                                             struct cli_credentials *credentials,
181                                             struct event_context *ev)
182 {
183         struct composite_context *c;
184         struct smb2_connect_state *state;
185         struct nbt_name name;
186         struct composite_context *creq;
187
188         c = composite_create(mem_ctx, ev);
189         if (c == NULL) return NULL;
190
191         state = talloc(c, struct smb2_connect_state);
192         if (composite_nomem(state, c)) return c;
193         c->private_data = state;
194
195         state->credentials = credentials;
196         state->host = talloc_strdup(c, host);
197         if (composite_nomem(state->host, c)) return c;
198         state->share = talloc_strdup(c, share);
199         if (composite_nomem(state->share, c)) return c;
200         state->resolve_ctx = talloc_reference(state, resolve_ctx);
201
202         ZERO_STRUCT(name);
203         name.name = host;
204
205         creq = resolve_name_send(resolve_ctx, &name, c->event_ctx);
206         composite_continue(c, creq, continue_resolve, c);
207         return c;
208 }
209
210 /*
211   receive a connect reply
212 */
213 NTSTATUS smb2_connect_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
214                            struct smb2_tree **tree)
215 {
216         NTSTATUS status;
217         struct smb2_connect_state *state = talloc_get_type(c->private_data, 
218                                                            struct smb2_connect_state);
219         status = composite_wait(c);
220         if (NT_STATUS_IS_OK(status)) {
221                 *tree = talloc_steal(mem_ctx, state->tree);
222         }
223         talloc_free(c);
224         return status;
225 }
226
227 /*
228   sync version of smb2_connect
229 */
230 NTSTATUS smb2_connect(TALLOC_CTX *mem_ctx, 
231                       const char *host, const char *share,
232                       struct resolve_context *resolve_ctx,
233                       struct cli_credentials *credentials,
234                       struct smb2_tree **tree,
235                       struct event_context *ev)
236 {
237         struct composite_context *c = smb2_connect_send(mem_ctx, host, share, 
238                                                         resolve_ctx,
239                                                         credentials, ev);
240         return smb2_connect_recv(c, mem_ctx, tree);
241 }