- the buffer code (first 2 bytes in the SMB2 body) seem to be the length
[samba-svnmirror.git] / source / 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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/smb2/smb2.h"
26 #include "libcli/smb2/smb2_calls.h"
27 #include "libcli/composite/composite.h"
28
29 struct smb2_connect_state {
30         struct smb2_request *req;
31         struct composite_context *creq;
32         struct cli_credentials *credentials;
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 (!NT_STATUS_IS_OK(c->status)) {
53                 composite_error(c, c->status);
54                 return;
55         }
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
72         c->status = smb2_session_setup_spnego_recv(creq);
73         if (!NT_STATUS_IS_OK(c->status)) {
74                 composite_error(c, c->status);
75                 return;
76         }
77
78         state->tree = smb2_tree_init(state->session, state, True);
79         if (state->tree == NULL) {
80                 composite_error(c, NT_STATUS_NO_MEMORY);
81                 return;
82         }
83
84         state->tcon.in.unknown1 = 0x09;
85         state->tcon.in.path     = talloc_asprintf(state, "\\\\%s\\%s", 
86                                                   state->host, state->share);
87         if (state->tcon.in.path == NULL) {
88                 composite_error(c, NT_STATUS_NO_MEMORY);
89                 return;
90         }
91         
92         state->req = smb2_tree_connect_send(state->tree, &state->tcon);
93         if (state->req == NULL) {
94                 composite_error(c, NT_STATUS_NO_MEMORY);
95                 return;
96         }
97
98         state->req->async.fn = continue_tcon;
99         state->req->async.private = c;  
100 }
101
102 /*
103   continue after negprot reply
104 */
105 static void continue_negprot(struct smb2_request *req)
106 {
107         struct composite_context *c = talloc_get_type(req->async.private, 
108                                                       struct composite_context);
109         struct smb2_connect_state *state = talloc_get_type(c->private_data, 
110                                                            struct smb2_connect_state);
111         struct smb2_transport *transport = req->transport;
112
113         c->status = smb2_negprot_recv(req, c, &state->negprot);
114         if (!NT_STATUS_IS_OK(c->status)) {
115                 composite_error(c, c->status);
116                 return;
117         }
118
119         state->session = smb2_session_init(transport, state, True);
120         if (state->session == NULL) {
121                 composite_error(c, NT_STATUS_NO_MEMORY);
122                 return;
123         }
124
125         state->creq = smb2_session_setup_spnego_send(state->session, state->credentials);
126         if (state->creq == NULL) {
127                 composite_error(c, NT_STATUS_NO_MEMORY);
128                 return;
129         }
130
131         state->creq->async.fn = continue_session;
132         state->creq->async.private_data = c;
133 }
134
135 /*
136   continue after a socket connect completes
137 */
138 static void continue_socket(struct composite_context *creq)
139 {
140         struct composite_context *c = talloc_get_type(creq->async.private_data, 
141                                                       struct composite_context);
142         struct smb2_connect_state *state = talloc_get_type(c->private_data, 
143                                                            struct smb2_connect_state);
144         struct smbcli_socket *sock;
145         struct smb2_transport *transport;
146
147         c->status = smbcli_sock_connect_recv(creq, state, &sock);
148         if (!NT_STATUS_IS_OK(c->status)) {
149                 composite_error(c, c->status);
150                 return;
151         }
152
153         transport = smb2_transport_init(sock, state);
154         if (transport == NULL) {
155                 composite_error(c, NT_STATUS_NO_MEMORY);
156                 return;
157         }
158
159         ZERO_STRUCT(state->negprot);
160         state->negprot.in.unknown1 = 0x0001;
161
162         state->req = smb2_negprot_send(transport, &state->negprot);
163         if (state->req == NULL) {
164                 composite_error(c, NT_STATUS_NO_MEMORY);
165                 return;
166         }
167
168         state->req->async.fn = continue_negprot;
169         state->req->async.private = c;
170 }
171
172
173 /*
174   continue after a resolve finishes
175 */
176 static void continue_resolve(struct composite_context *creq)
177 {
178         struct composite_context *c = talloc_get_type(creq->async.private_data, 
179                                                       struct composite_context);
180         struct smb2_connect_state *state = talloc_get_type(c->private_data, 
181                                                            struct smb2_connect_state);
182         const char *addr;
183         
184         c->status = resolve_name_recv(creq, state, &addr);
185         if (!NT_STATUS_IS_OK(c->status)) {
186                 composite_error(c, c->status);
187                 return;
188         }
189
190         state->creq = smbcli_sock_connect_send(state, addr, 445, state->host, c->event_ctx);
191         if (state->creq == NULL) {
192                 composite_error(c, NT_STATUS_NO_MEMORY);
193                 return;
194         }
195
196         state->creq->async.private_data = c;
197         state->creq->async.fn = continue_socket;
198 }
199
200 /*
201   a composite function that does a full negprot/sesssetup/tcon, returning
202   a connected smb2_tree
203  */
204 struct composite_context *smb2_connect_send(TALLOC_CTX *mem_ctx,
205                                             const char *host,
206                                             const char *share,
207                                             struct cli_credentials *credentials,
208                                             struct event_context *ev)
209 {
210         struct composite_context *c;
211         struct smb2_connect_state *state;
212         struct nbt_name name;
213
214         c = talloc_zero(mem_ctx, struct composite_context);
215         if (c == NULL) return NULL;
216
217         state = talloc(c, struct smb2_connect_state);
218         if (state == NULL) {
219                 c->status = NT_STATUS_NO_MEMORY;
220                 goto failed;
221         }
222
223         c->state = COMPOSITE_STATE_IN_PROGRESS;
224         c->private_data = state;
225         c->event_ctx = ev;
226
227         state->credentials = credentials;
228         state->host = talloc_strdup(c, host);
229         state->share = talloc_strdup(c, share);
230         if (state->host == NULL || state->share == NULL) {
231                 c->status = NT_STATUS_NO_MEMORY;
232                 goto failed;
233         }
234
235         ZERO_STRUCT(name);
236         name.name = host;
237
238         state->creq = resolve_name_send(&name, c->event_ctx, lp_name_resolve_order());
239         if (state->creq == NULL) goto failed;
240
241         state->creq->async.private_data = c;
242         state->creq->async.fn = continue_resolve;
243
244         return c;
245
246 failed:
247         composite_trigger_error(c);
248         return c;
249 }
250
251 /*
252   receive a connect reply
253 */
254 NTSTATUS smb2_connect_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
255                            struct smb2_tree **tree)
256 {
257         NTSTATUS status;
258         struct smb2_connect_state *state = talloc_get_type(c->private_data, 
259                                                            struct smb2_connect_state);
260         status = composite_wait(c);
261         if (NT_STATUS_IS_OK(status)) {
262                 *tree = talloc_steal(mem_ctx, state->tree);
263         }
264         talloc_free(c);
265         return status;
266 }
267
268 /*
269   sync version of smb2_connect
270 */
271 NTSTATUS smb2_connect(TALLOC_CTX *mem_ctx, 
272                       const char *host, const char *share,
273                       struct cli_credentials *credentials,
274                       struct smb2_tree **tree,
275                       struct event_context *ev)
276 {
277         struct composite_context *c = smb2_connect_send(mem_ctx, host, share, 
278                                                         credentials, ev);
279         return smb2_connect_recv(c, mem_ctx, tree);
280 }