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