Add smb_composite_connectmulti: Send out multiple SYN packets at once, use the
[samba-svnmirror.git] / source / libcli / smb_composite / connect.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Andrew Tridgell 2005
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 /*
21   a composite API for making a full SMB connection
22 */
23
24 #include "includes.h"
25 #include "libcli/raw/libcliraw.h"
26 #include "libcli/composite/composite.h"
27 #include "libcli/smb_composite/smb_composite.h"
28 #include "lib/events/events.h"
29
30 /* the stages of this call */
31 enum connect_stage {CONNECT_RESOLVE, 
32                     CONNECT_SOCKET, 
33                     CONNECT_SESSION_REQUEST, 
34                     CONNECT_NEGPROT,
35                     CONNECT_SESSION_SETUP,
36                     CONNECT_TCON};
37
38 struct connect_state {
39         enum connect_stage stage;
40         struct smbcli_socket *sock;
41         struct smbcli_transport *transport;
42         struct smbcli_session *session;
43         struct smb_composite_connectmulti *conn;
44         struct smb_composite_connect *io;
45         union smb_tcon *io_tcon;
46         struct smb_composite_sesssetup *io_setup;
47         struct smbcli_request *req;
48         struct composite_context *creq;
49 };
50
51
52 static void request_handler(struct smbcli_request *);
53 static void composite_handler(struct composite_context *);
54
55 /*
56   setup a negprot send 
57 */
58 static NTSTATUS connect_send_negprot(struct composite_context *c, 
59                                      struct smb_composite_connect *io)
60 {
61         struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
62
63         state->req = smb_raw_negotiate_send(state->transport, lp_maxprotocol());
64         NT_STATUS_HAVE_NO_MEMORY(state->req);
65
66         state->req->async.fn = request_handler;
67         state->req->async.private = c;
68         state->stage = CONNECT_NEGPROT;
69         
70         return NT_STATUS_OK;
71 }
72
73
74 /*
75   a tree connect request has competed
76 */
77 static NTSTATUS connect_tcon(struct composite_context *c, 
78                              struct smb_composite_connect *io)
79 {
80         struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
81         NTSTATUS status;
82
83         status = smb_raw_tcon_recv(state->req, c, state->io_tcon);
84         NT_STATUS_NOT_OK_RETURN(status);
85
86         io->out.tree->tid = state->io_tcon->tconx.out.tid;
87         if (state->io_tcon->tconx.out.dev_type) {
88                 io->out.tree->device = talloc_strdup(io->out.tree, 
89                                                      state->io_tcon->tconx.out.dev_type);
90         }
91         if (state->io_tcon->tconx.out.fs_type) {
92                 io->out.tree->fs_type = talloc_strdup(io->out.tree, 
93                                                       state->io_tcon->tconx.out.fs_type);
94         }
95
96         /* all done! */
97         c->state = COMPOSITE_STATE_DONE;
98
99         return NT_STATUS_OK;
100 }
101
102
103 /*
104   a session setup request has competed
105 */
106 static NTSTATUS connect_session_setup(struct composite_context *c, 
107                                       struct smb_composite_connect *io)
108 {
109         struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
110         NTSTATUS status;
111
112         status = smb_composite_sesssetup_recv(state->creq);
113         NT_STATUS_NOT_OK_RETURN(status);
114         
115         state->session->vuid = state->io_setup->out.vuid;
116         
117         /* setup for a tconx */
118         io->out.tree = smbcli_tree_init(state->session, state, True);
119         NT_STATUS_HAVE_NO_MEMORY(io->out.tree);
120
121         state->io_tcon = talloc(c, union smb_tcon);
122         NT_STATUS_HAVE_NO_MEMORY(state->io_tcon);
123
124         /* connect to a share using a tree connect */
125         state->io_tcon->generic.level = RAW_TCON_TCONX;
126         state->io_tcon->tconx.in.flags = 0;
127         state->io_tcon->tconx.in.password = data_blob(NULL, 0); 
128         
129         state->io_tcon->tconx.in.path = talloc_asprintf(state->io_tcon, 
130                                                  "\\\\%s\\%s", 
131                                                  io->in.called_name, 
132                                                  io->in.service);
133         NT_STATUS_HAVE_NO_MEMORY(state->io_tcon->tconx.in.path);
134         if (!io->in.service_type) {
135                 state->io_tcon->tconx.in.device = "?????";
136         } else {
137                 state->io_tcon->tconx.in.device = io->in.service_type;
138         }
139
140         state->req = smb_raw_tcon_send(io->out.tree, state->io_tcon);
141         NT_STATUS_HAVE_NO_MEMORY(state->req);
142         if (state->req->state == SMBCLI_REQUEST_ERROR) {
143                 return state->req->status;
144         }
145
146         state->req->async.fn = request_handler;
147         state->req->async.private = c;
148         state->stage = CONNECT_TCON;
149
150         return NT_STATUS_OK;
151 }
152
153 /*
154   a negprot request has competed
155 */
156 static NTSTATUS connect_negprot(struct composite_context *c, 
157                                 struct smb_composite_connect *io)
158 {
159         struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
160         NTSTATUS status;
161
162         status = smb_raw_negotiate_recv(state->req);
163         NT_STATUS_NOT_OK_RETURN(status);
164
165         /* next step is a session setup */
166         state->session = smbcli_session_init(state->transport, state, True);
167         NT_STATUS_HAVE_NO_MEMORY(state->session);
168
169         state->io_setup = talloc(c, struct smb_composite_sesssetup);
170         NT_STATUS_HAVE_NO_MEMORY(state->io_setup);
171
172         /* prepare a session setup to establish a security context */
173         state->io_setup->in.sesskey      = state->transport->negotiate.sesskey;
174         state->io_setup->in.capabilities = state->transport->negotiate.capabilities;
175         state->io_setup->in.credentials  = io->in.credentials;
176         state->io_setup->in.workgroup    = io->in.workgroup;
177
178         state->creq = smb_composite_sesssetup_send(state->session, state->io_setup);
179         NT_STATUS_HAVE_NO_MEMORY(state->creq);
180         if (state->creq->state == COMPOSITE_STATE_ERROR) {
181                 return state->creq->status;
182         }
183
184         state->creq->async.fn = composite_handler;
185         state->creq->async.private_data = c;
186         state->stage = CONNECT_SESSION_SETUP;
187         
188         return NT_STATUS_OK;
189 }
190
191
192 /*
193   a session request operation has competed
194 */
195 static NTSTATUS connect_session_request(struct composite_context *c, 
196                                         struct smb_composite_connect *io)
197 {
198         struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
199         NTSTATUS status;
200
201         status = smbcli_transport_connect_recv(state->req);
202         NT_STATUS_NOT_OK_RETURN(status);
203
204         /* next step is a negprot */
205         return connect_send_negprot(c, io);
206 }
207
208 /*
209   a socket connection operation has competed
210 */
211 static NTSTATUS connect_socket(struct composite_context *c, 
212                                struct smb_composite_connect *io)
213 {
214         struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
215         NTSTATUS status;
216         struct nbt_name calling, called;
217
218         status = smb_composite_connectmulti_recv(state->creq, state);
219         NT_STATUS_NOT_OK_RETURN(status);
220
221         state->sock = state->conn->out.socket;
222
223         /* the socket is up - we can initialise the smbcli transport layer */
224         state->transport = smbcli_transport_init(state->sock, state, True);
225         NT_STATUS_HAVE_NO_MEMORY(state->transport);
226
227         make_nbt_name_client(&calling, cli_credentials_get_workstation(io->in.credentials));
228
229         nbt_choose_called_name(state, &called, io->in.called_name, NBT_NAME_SERVER);
230
231         /* we have a connected socket - next step is a session
232            request, if needed. Port 445 doesn't need it, so it goes
233            straight to the negprot */
234         if (state->sock->port == 445) {
235                 status = nbt_name_dup(state->transport, &called, 
236                                       &state->transport->called);
237                 NT_STATUS_NOT_OK_RETURN(status);
238                 return connect_send_negprot(c, io);
239         }
240
241         state->req = smbcli_transport_connect_send(state->transport, &calling, &called);
242         NT_STATUS_HAVE_NO_MEMORY(state->req);
243
244         state->req->async.fn = request_handler;
245         state->req->async.private = c;
246         state->stage = CONNECT_SESSION_REQUEST;
247
248         return NT_STATUS_OK;
249 }
250
251
252 /*
253   called when name resolution is finished
254 */
255 static NTSTATUS connect_resolve(struct composite_context *c, 
256                                 struct smb_composite_connect *io)
257 {
258         struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
259         NTSTATUS status;
260         const char *address;
261         struct smb_composite_connectmulti *conn;
262
263         status = resolve_name_recv(state->creq, state, &address);
264         NT_STATUS_NOT_OK_RETURN(status);
265
266         conn = talloc(state, struct smb_composite_connectmulti);
267         NT_STATUS_HAVE_NO_MEMORY(conn);
268         state->conn = conn;
269         conn->in.num_dests = 1;
270
271         conn->in.addresses = talloc_array(state->conn, const char *, 1);
272         NT_STATUS_HAVE_NO_MEMORY(conn->in.addresses);
273         conn->in.addresses[0] = address;
274
275         conn->in.hostnames = talloc_array(state->conn, const char *, 1);
276         NT_STATUS_HAVE_NO_MEMORY(conn->in.hostnames);
277         conn->in.hostnames[0] = state->io->in.dest_host;
278         
279         conn->in.ports = NULL;
280         if (state->io->in.port != 0) {
281                 conn->in.ports = talloc_array(state->conn, int, 1);
282                 NT_STATUS_HAVE_NO_MEMORY(conn->in.ports);
283                 conn->in.ports[0] = state->io->in.port;
284         }
285
286         state->creq = smb_composite_connectmulti_send(conn, state,
287                                                       c->event_ctx);
288         NT_STATUS_HAVE_NO_MEMORY(state->creq);
289
290         state->stage = CONNECT_SOCKET;
291         state->creq->async.private_data = c;
292         state->creq->async.fn = composite_handler;
293
294         return NT_STATUS_OK;
295 }
296
297
298 /*
299   handle and dispatch state transitions
300 */
301 static void state_handler(struct composite_context *c)
302 {
303         struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
304
305         switch (state->stage) {
306         case CONNECT_RESOLVE:
307                 c->status = connect_resolve(c, state->io);
308                 break;
309         case CONNECT_SOCKET:
310                 c->status = connect_socket(c, state->io);
311                 break;
312         case CONNECT_SESSION_REQUEST:
313                 c->status = connect_session_request(c, state->io);
314                 break;
315         case CONNECT_NEGPROT:
316                 c->status = connect_negprot(c, state->io);
317                 break;
318         case CONNECT_SESSION_SETUP:
319                 c->status = connect_session_setup(c, state->io);
320                 break;
321         case CONNECT_TCON:
322                 c->status = connect_tcon(c, state->io);
323                 break;
324         }
325
326         if (!NT_STATUS_IS_OK(c->status)) {
327                 c->state = COMPOSITE_STATE_ERROR;
328         }
329
330         if (c->state >= COMPOSITE_STATE_DONE &&
331             c->async.fn) {
332                 c->async.fn(c);
333         }
334 }
335
336
337 /*
338   handler for completion of a smbcli_request sub-request
339 */
340 static void request_handler(struct smbcli_request *req)
341 {
342         struct composite_context *c = talloc_get_type(req->async.private, 
343                                                      struct composite_context);
344         state_handler(c);
345 }
346
347 /*
348   handler for completion of a smbcli_composite sub-request
349 */
350 static void composite_handler(struct composite_context *creq)
351 {
352         struct composite_context *c = talloc_get_type(creq->async.private_data, 
353                                                      struct composite_context);
354         state_handler(c);
355 }
356
357 /*
358   a function to establish a smbcli_tree from scratch
359 */
360 struct composite_context *smb_composite_connect_send(struct smb_composite_connect *io,
361                                                      TALLOC_CTX *mem_ctx,
362                                                      struct event_context *event_ctx)
363 {
364         struct composite_context *c;
365         struct connect_state *state;
366         struct nbt_name name;
367
368         c = talloc_zero(mem_ctx, struct composite_context);
369         if (c == NULL) goto failed;
370
371         state = talloc(c, struct connect_state);
372         if (state == NULL) goto failed;
373
374         if (event_ctx == NULL) {
375                 event_ctx = event_context_init(mem_ctx);
376         }
377
378         state->io = io;
379
380         c->state = COMPOSITE_STATE_IN_PROGRESS;
381         c->event_ctx = talloc_reference(c, event_ctx);
382         c->private_data = state;
383
384         state->stage = CONNECT_RESOLVE;
385         make_nbt_name_server(&name, io->in.dest_host);
386         state->creq = resolve_name_send(&name, c->event_ctx, lp_name_resolve_order());
387
388         if (state->creq == NULL) goto failed;
389         state->creq->async.private_data = c;
390         state->creq->async.fn = composite_handler;
391
392         return c;
393 failed:
394         talloc_free(c);
395         return NULL;
396 }
397
398 /*
399   recv half of async composite connect code
400 */
401 NTSTATUS smb_composite_connect_recv(struct composite_context *c, TALLOC_CTX *mem_ctx)
402 {
403         NTSTATUS status;
404
405         status = composite_wait(c);
406
407         if (NT_STATUS_IS_OK(status)) {
408                 struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
409                 talloc_steal(mem_ctx, state->io->out.tree);
410         }
411
412         talloc_free(c);
413         return status;
414 }
415
416 /*
417   sync version of smb_composite_connect 
418 */
419 NTSTATUS smb_composite_connect(struct smb_composite_connect *io, TALLOC_CTX *mem_ctx,
420                                struct event_context *ev)
421 {
422         struct composite_context *c = smb_composite_connect_send(io, mem_ctx, ev);
423         return smb_composite_connect_recv(c, mem_ctx);
424 }