38a707725db30b4825cdf0082f434556b82812ec
[samba.git] / source4 / librpc / rpc / dcerpc_connect.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    dcerpc connect functions
5
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) Jelmer Vernooij 2004
8    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
9    Copyright (C) Rafal Szczesniak  2005
10    
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2 of the License, or
14    (at your option) any later version.
15    
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20    
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26
27 #include "includes.h"
28 #include "libcli/composite/composite.h"
29 #include "libcli/smb_composite/smb_composite.h"
30 #include "lib/events/events.h"
31 #include "libcli/smb2/smb2.h"
32 #include "libcli/smb2/smb2_calls.h"
33 #include "librpc/rpc/dcerpc.h"
34 #include "auth/credentials/credentials.h"
35 #include "auth/credentials/credentials_krb5.h"
36
37
38 struct pipe_np_smb_state {
39         struct smb_composite_connect conn;
40         struct smbcli_tree *tree;
41         struct dcerpc_pipe_connect io;
42 };
43
44
45 /*
46   Stage 3 of ncacn_np_smb: Named pipe opened (or not)
47 */
48 static void continue_pipe_open_smb(struct composite_context *ctx)
49 {
50         struct composite_context *c = talloc_get_type(ctx->async.private_data,
51                                                       struct composite_context);
52
53         /* receive result of named pipe open request on smb */
54         c->status = dcerpc_pipe_open_smb_recv(ctx);
55         if (!composite_is_ok(c)) return;
56
57         composite_done(c);
58 }
59
60
61 /*
62   Stage 2 of ncacn_np_smb: Open a named pipe after successful smb connection
63 */
64 static void continue_smb_connect(struct composite_context *ctx)
65 {
66         struct composite_context *open_ctx;
67         struct composite_context *c = talloc_get_type(ctx->async.private_data,
68                                                       struct composite_context);
69         struct pipe_np_smb_state *s = talloc_get_type(c->private_data,
70                                                       struct pipe_np_smb_state);
71         
72         /* receive result of smb connect request */
73         c->status = smb_composite_connect_recv(ctx, c);
74         if (!composite_is_ok(c)) return;
75
76         /* prepare named pipe open parameters */
77         s->tree         = s->conn.out.tree;
78         s->io.pipe_name = s->io.binding->endpoint;
79
80         /* send named pipe open request */
81         open_ctx = dcerpc_pipe_open_smb_send(s->io.pipe->conn, s->tree, s->io.pipe_name);
82         if (composite_nomem(open_ctx, c)) return;
83
84         composite_continue(c, open_ctx, continue_pipe_open_smb, c);
85 }
86
87
88 /*
89   Initiate async open of a rpc connection to a rpc pipe on SMB using
90   the binding structure to determine the endpoint and options
91 */
92 static struct composite_context *dcerpc_pipe_connect_ncacn_np_smb_send(TALLOC_CTX *mem_ctx, 
93                                                                 struct dcerpc_pipe_connect *io)
94 {
95         struct composite_context *c;
96         struct pipe_np_smb_state *s;
97         struct composite_context *conn_req;
98         struct smb_composite_connect *conn;
99
100         /* composite context allocation and setup */
101         c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
102         if (c == NULL) return NULL;
103
104         s = talloc_zero(c, struct pipe_np_smb_state);
105         if (composite_nomem(s, c)) return c;
106         c->private_data = s;
107
108         s->io  = *io;
109         conn   = &s->conn;
110
111         /* prepare smb connection parameters: we're connecting to IPC$ share on
112            remote rpc server */
113         conn->in.dest_host              = s->io.binding->host;
114         conn->in.port                   = 0;
115         conn->in.called_name            = s->io.binding->target_hostname;
116         conn->in.service                = "IPC$";
117         conn->in.service_type           = NULL;
118         conn->in.workgroup              = lp_workgroup();
119
120         /*
121          * provide proper credentials - user supplied, but allow a
122          * fallback to anonymous if this is an schannel connection
123          * (might be NT4 not allowing machine logins at session
124          * setup).
125          */
126         s->conn.in.credentials = s->io.creds;
127         if (s->io.binding->flags & DCERPC_SCHANNEL) {
128                 conn->in.fallback_to_anonymous  = True;
129         } else {
130                 conn->in.fallback_to_anonymous  = False;
131         }
132
133         /* send smb connect request */
134         conn_req = smb_composite_connect_send(conn, s->io.pipe->conn, s->io.pipe->conn->event_ctx);
135         if (composite_nomem(conn_req, c)) return c;
136
137         composite_continue(c, conn_req, continue_smb_connect, c);
138         return c;
139 }
140
141
142 /*
143   Receive result of a rpc connection to a rpc pipe on SMB
144 */
145 static NTSTATUS dcerpc_pipe_connect_ncacn_np_smb_recv(struct composite_context *c)
146 {
147         NTSTATUS status = composite_wait(c);
148
149         talloc_free(c);
150         return status;
151 }
152
153
154 struct pipe_np_smb2_state {
155         struct smb2_tree *tree;
156         struct dcerpc_pipe_connect io;
157 };
158
159
160 /*
161   Stage 3 of ncacn_np_smb: Named pipe opened (or not)
162 */
163 static void continue_pipe_open_smb2(struct composite_context *ctx)
164 {
165         struct composite_context *c = talloc_get_type(ctx->async.private_data,
166                                                       struct composite_context);
167
168         /* receive result of named pipe open request on smb2 */
169         c->status = dcerpc_pipe_open_smb2_recv(ctx);
170         if (!composite_is_ok(c)) return;
171
172         composite_done(c);
173 }
174
175
176 /*
177   Stage 2 of ncacn_np_smb2: Open a named pipe after successful smb2 connection
178 */
179 static void continue_smb2_connect(struct composite_context *ctx)
180 {
181         struct composite_context *open_req;
182         struct composite_context *c = talloc_get_type(ctx->async.private_data,
183                                                       struct composite_context);
184         struct pipe_np_smb2_state *s = talloc_get_type(c->private_data,
185                                                        struct pipe_np_smb2_state);
186
187         /* receive result of smb2 connect request */
188         c->status = smb2_connect_recv(ctx, c, &s->tree);
189         if (!composite_is_ok(c)) return;
190
191         /* prepare named pipe open parameters */
192         s->io.pipe_name = s->io.binding->endpoint;
193
194         /* send named pipe open request */
195         open_req = dcerpc_pipe_open_smb2_send(s->io.pipe->conn, s->tree, s->io.pipe_name);
196         if (composite_nomem(open_req, c)) return;
197
198         composite_continue(c, open_req, continue_pipe_open_smb2, c);
199 }
200
201
202 /* 
203    Initiate async open of a rpc connection request on SMB2 using
204    the binding structure to determine the endpoint and options
205 */
206 static struct composite_context *dcerpc_pipe_connect_ncacn_np_smb2_send(TALLOC_CTX *mem_ctx,
207                                                                         struct dcerpc_pipe_connect *io)
208 {
209         struct composite_context *c;
210         struct pipe_np_smb2_state *s;
211         struct composite_context *conn_req;
212
213         /* composite context allocation and setup */
214         c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
215         if (c == NULL) return NULL;
216
217         s = talloc_zero(c, struct pipe_np_smb2_state);
218         if (composite_nomem(s, c)) return c;
219         c->private_data = s;
220
221         s->io = *io;
222
223         /*
224          * provide proper credentials - user supplied or anonymous in case this is
225          * schannel connection
226          */
227         if (s->io.binding->flags & DCERPC_SCHANNEL) {
228                 s->io.creds = cli_credentials_init(mem_ctx);
229                 if (composite_nomem(s->io.creds, c)) return c;
230
231                 cli_credentials_set_anonymous(s->io.creds);
232                 cli_credentials_guess(s->io.creds);
233         }
234
235         /* send smb2 connect request */
236         conn_req = smb2_connect_send(mem_ctx, s->io.binding->host, "IPC$", s->io.creds,
237                                      c->event_ctx);
238         composite_continue(c, conn_req, continue_smb2_connect, c);
239         return c;
240 }
241
242
243 /*
244   Receive result of a rpc connection to a rpc pipe on SMB2
245 */
246 static NTSTATUS dcerpc_pipe_connect_ncacn_np_smb2_recv(struct composite_context *c)
247 {
248         NTSTATUS status = composite_wait(c);
249         
250         talloc_free(c);
251         return status;
252 }
253
254
255 struct pipe_ip_tcp_state {
256         struct dcerpc_pipe_connect io;
257         const char *host;
258         const char *target_hostname;
259         uint32_t port;
260 };
261
262
263 /*
264   Stage 2 of ncacn_ip_tcp: rpc pipe opened (or not)
265 */
266 static void continue_pipe_open_ncacn_ip_tcp(struct composite_context *ctx)
267 {
268         struct composite_context *c = talloc_get_type(ctx->async.private_data,
269                                                       struct composite_context);
270
271         /* receive result of named pipe open request on tcp/ip */
272         c->status = dcerpc_pipe_open_tcp_recv(ctx);
273         if (!composite_is_ok(c)) return;
274
275         composite_done(c);
276 }
277
278
279 /*
280   Initiate async open of a rpc connection to a rpc pipe on TCP/IP using
281   the binding structure to determine the endpoint and options
282 */
283 static struct composite_context* dcerpc_pipe_connect_ncacn_ip_tcp_send(TALLOC_CTX *mem_ctx,
284                                                                        struct dcerpc_pipe_connect *io)
285 {
286         struct composite_context *c;
287         struct pipe_ip_tcp_state *s;
288         struct composite_context *pipe_req;
289
290         /* composite context allocation and setup */
291         c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
292         if (c == NULL) return NULL;
293
294         s = talloc_zero(c, struct pipe_ip_tcp_state);
295         if (composite_nomem(s, c)) return c;
296         c->private_data = s;
297
298         /* store input parameters in state structure */
299         s->io               = *io;
300         s->host             = talloc_reference(c, io->binding->host);
301         s->target_hostname  = talloc_reference(c, io->binding->target_hostname);
302                              /* port number is a binding endpoint here */
303         s->port             = atoi(io->binding->endpoint);   
304
305         /* send pipe open request on tcp/ip */
306         pipe_req = dcerpc_pipe_open_tcp_send(s->io.pipe->conn, s->host, s->target_hostname, 
307                                              s->port);
308         composite_continue(c, pipe_req, continue_pipe_open_ncacn_ip_tcp, c);
309         return c;
310 }
311
312
313 /*
314   Receive result of a rpc connection to a rpc pipe on TCP/IP
315 */
316 static NTSTATUS dcerpc_pipe_connect_ncacn_ip_tcp_recv(struct composite_context *c)
317 {
318         NTSTATUS status = composite_wait(c);
319         
320         talloc_free(c);
321         return status;
322 }
323
324
325 struct pipe_unix_state {
326         struct dcerpc_pipe_connect io;
327         const char *path;
328 };
329
330
331 /*
332   Stage 2 of ncacn_unix: rpc pipe opened (or not)
333 */
334 static void continue_pipe_open_ncacn_unix_stream(struct composite_context *ctx)
335 {
336         struct composite_context *c = talloc_get_type(ctx->async.private_data,
337                                                       struct composite_context);
338
339         /* receive result of pipe open request on unix socket */
340         c->status = dcerpc_pipe_open_unix_stream_recv(ctx);
341         if (!composite_is_ok(c)) return;
342
343         composite_done(c);
344 }
345
346
347 /*
348   Initiate async open of a rpc connection to a rpc pipe on unix socket using
349   the binding structure to determine the endpoint and options
350 */
351 static struct composite_context* dcerpc_pipe_connect_ncacn_unix_stream_send(TALLOC_CTX *mem_ctx,
352                                                                             struct dcerpc_pipe_connect *io)
353 {
354         struct composite_context *c;
355         struct pipe_unix_state *s;
356         struct composite_context *pipe_req;
357
358         /* composite context allocation and setup */
359         c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
360         if (c == NULL) return NULL;
361
362         s = talloc_zero(c, struct pipe_unix_state);
363         if (composite_nomem(s, c)) return c;
364         c->private_data = s;
365
366         /* prepare pipe open parameters and store them in state structure
367            also, verify whether biding endpoint is not null */
368         s->io = *io;
369         
370         if (!io->binding->endpoint) {
371                 DEBUG(0, ("Path to unix socket not specified\n"));
372                 composite_error(c, NT_STATUS_INVALID_PARAMETER);
373                 return c;
374         }
375
376         s->path  = talloc_strdup(c, io->binding->endpoint);  /* path is a binding endpoint here */
377         if (composite_nomem(s->path, c)) return c;
378
379         /* send pipe open request on unix socket */
380         pipe_req = dcerpc_pipe_open_unix_stream_send(s->io.pipe->conn, s->path);
381         composite_continue(c, pipe_req, continue_pipe_open_ncacn_unix_stream, c);
382         return c;
383 }
384
385
386 /*
387   Receive result of a rpc connection to a pipe on unix socket
388 */
389 static NTSTATUS dcerpc_pipe_connect_ncacn_unix_stream_recv(struct composite_context *c)
390 {
391         NTSTATUS status = composite_wait(c);
392
393         talloc_free(c);
394         return status;
395 }
396
397
398 struct pipe_ncalrpc_state {
399         struct dcerpc_pipe_connect io;
400 };
401
402 static NTSTATUS dcerpc_pipe_connect_ncalrpc_recv(struct composite_context *c);
403
404 /*
405   Stage 2 of ncalrpc: rpc pipe opened (or not)
406 */
407 static void continue_pipe_open_ncalrpc(struct composite_context *ctx)
408 {
409         struct composite_context *c = talloc_get_type(ctx->async.private_data,
410                                                       struct composite_context);
411
412         /* receive result of pipe open request on ncalrpc */
413         c->status = dcerpc_pipe_connect_ncalrpc_recv(ctx);
414         if (!composite_is_ok(c)) return;
415
416         composite_done(c);
417 }
418
419
420 /* 
421    Initiate async open of a rpc connection request on NCALRPC using
422    the binding structure to determine the endpoint and options
423 */
424 static struct composite_context* dcerpc_pipe_connect_ncalrpc_send(TALLOC_CTX *mem_ctx,
425                                                                   struct dcerpc_pipe_connect *io)
426 {
427         struct composite_context *c;
428         struct pipe_ncalrpc_state *s;
429         struct composite_context *pipe_req;
430
431         /* composite context allocation and setup */
432         c = composite_create(mem_ctx, io->pipe->conn->event_ctx);
433         if (c == NULL) return NULL;
434
435         s = talloc_zero(c, struct pipe_ncalrpc_state);
436         if (composite_nomem(s, c)) return c;
437         c->private_data = s;
438         
439         /* store input parameters in state structure */
440         s->io  = *io;
441
442         /* send pipe open request */
443         pipe_req = dcerpc_pipe_open_pipe_send(s->io.pipe->conn, s->io.binding->endpoint);
444         composite_continue(c, pipe_req, continue_pipe_open_ncalrpc, c);
445         return c;
446 }
447
448
449 /*
450   Receive result of a rpc connection to a rpc pipe on NCALRPC
451 */
452 static NTSTATUS dcerpc_pipe_connect_ncalrpc_recv(struct composite_context *c)
453 {
454         NTSTATUS status = composite_wait(c);
455         
456         talloc_free(c);
457         return status;
458 }
459
460
461 struct pipe_connect_state {
462         struct dcerpc_pipe *pipe;
463         struct dcerpc_binding *binding;
464         const struct dcerpc_interface_table *table;
465         struct cli_credentials *credentials;
466 };
467
468
469 static void continue_map_binding(struct composite_context *ctx);
470 static void continue_connect(struct composite_context *c, struct pipe_connect_state *s);
471 static void continue_pipe_connect_ncacn_np_smb2(struct composite_context *ctx);
472 static void continue_pipe_connect_ncacn_np_smb(struct composite_context *ctx);
473 static void continue_pipe_connect_ncacn_ip_tcp(struct composite_context *ctx);
474 static void continue_pipe_connect_ncacn_unix(struct composite_context *ctx);
475 static void continue_pipe_connect_ncalrpc(struct composite_context *ctx);
476 static void continue_pipe_connect(struct composite_context *c, struct pipe_connect_state *s);
477 static void continue_pipe_auth(struct composite_context *ctx);
478
479
480 /*
481   Stage 2 of pipe_connect_b: Receive result of endpoint mapping
482 */
483 static void continue_map_binding(struct composite_context *ctx)
484 {
485         struct composite_context *c = talloc_get_type(ctx->async.private_data,
486                                                       struct composite_context);
487         struct pipe_connect_state *s = talloc_get_type(c->private_data,
488                                                        struct pipe_connect_state);
489         
490         c->status = dcerpc_epm_map_binding_recv(ctx);
491         if (!composite_is_ok(c)) return;
492
493         DEBUG(2,("Mapped to DCERPC endpoint %s\n", s->binding->endpoint));
494         
495         continue_connect(c, s);
496 }
497
498
499 /*
500   Stage 2 of pipe_connect_b: Continue connection after endpoint is known
501 */
502 static void continue_connect(struct composite_context *c, struct pipe_connect_state *s)
503 {
504         struct dcerpc_pipe_connect pc;
505
506         /* potential exits to another stage by sending an async request */
507         struct composite_context *ncacn_np_smb2_req;
508         struct composite_context *ncacn_np_smb_req;
509         struct composite_context *ncacn_ip_tcp_req;
510         struct composite_context *ncacn_unix_req;
511         struct composite_context *ncalrpc_req;
512
513         /* dcerpc pipe connect input parameters */
514         pc.pipe         = s->pipe;
515         pc.binding      = s->binding;
516         pc.interface    = s->table;
517         pc.creds        = s->credentials;
518
519         /* connect dcerpc pipe depending on required transport */
520         switch (s->binding->transport) {
521         case NCACN_NP:
522                 if (pc.binding->flags & DCERPC_SMB2) {
523                         /* new varient of SMB a.k.a. SMB2 */
524                         ncacn_np_smb2_req = dcerpc_pipe_connect_ncacn_np_smb2_send(c, &pc);
525                         composite_continue(c, ncacn_np_smb2_req, continue_pipe_connect_ncacn_np_smb2, c);
526                         return;
527
528                 } else {
529                         /* good old ordinary SMB */
530                         ncacn_np_smb_req = dcerpc_pipe_connect_ncacn_np_smb_send(c, &pc);
531                         composite_continue(c, ncacn_np_smb_req, continue_pipe_connect_ncacn_np_smb, c);
532                         return;
533                 }
534                 break;
535
536         case NCACN_IP_TCP:
537                 ncacn_ip_tcp_req = dcerpc_pipe_connect_ncacn_ip_tcp_send(c, &pc);
538                 composite_continue(c, ncacn_ip_tcp_req, continue_pipe_connect_ncacn_ip_tcp, c);
539                 return;
540
541         case NCACN_UNIX_STREAM:
542                 ncacn_unix_req = dcerpc_pipe_connect_ncacn_unix_stream_send(c, &pc);
543                 composite_continue(c, ncacn_unix_req, continue_pipe_connect_ncacn_unix, c);
544                 return;
545
546         case NCALRPC:
547                 ncalrpc_req = dcerpc_pipe_connect_ncalrpc_send(c, &pc);
548                 composite_continue(c, ncalrpc_req, continue_pipe_connect_ncalrpc, c);
549                 return;
550
551         default:
552                 /* looks like a transport we don't support now */
553                 composite_error(c, NT_STATUS_NOT_SUPPORTED);
554         }
555 }
556
557
558 /*
559   Stage 3 of pipe_connect_b: Receive result of pipe connect request on
560   named pipe on smb2
561 */
562 static void continue_pipe_connect_ncacn_np_smb2(struct composite_context *ctx)
563 {
564         struct composite_context *c = talloc_get_type(ctx->async.private_data,
565                                                       struct composite_context);
566         struct pipe_connect_state *s = talloc_get_type(c->private_data,
567                                                        struct pipe_connect_state);
568
569         c->status = dcerpc_pipe_connect_ncacn_np_smb2_recv(ctx);
570         if (!composite_is_ok(c)) return;
571
572         continue_pipe_connect(c, s);
573 }
574
575
576 /*
577   Stage 3 of pipe_connect_b: Receive result of pipe connect request on
578   named pipe on smb
579 */
580 static void continue_pipe_connect_ncacn_np_smb(struct composite_context *ctx)
581 {
582         struct composite_context *c = talloc_get_type(ctx->async.private_data,
583                                                       struct composite_context);
584         struct pipe_connect_state *s = talloc_get_type(c->private_data,
585                                                        struct pipe_connect_state);
586
587         c->status = dcerpc_pipe_connect_ncacn_np_smb_recv(ctx);
588         if (!composite_is_ok(c)) return;
589         
590         continue_pipe_connect(c, s);
591 }
592
593
594 /*
595   Stage 3 of pipe_connect_b: Receive result of pipe connect request on tcp/ip
596 */
597 static void continue_pipe_connect_ncacn_ip_tcp(struct composite_context *ctx)
598 {
599         struct composite_context *c = talloc_get_type(ctx->async.private_data,
600                                                       struct composite_context);
601         struct pipe_connect_state *s = talloc_get_type(c->private_data,
602                                                        struct pipe_connect_state);
603
604         c->status = dcerpc_pipe_connect_ncacn_ip_tcp_recv(ctx);
605         if (!composite_is_ok(c)) return;
606
607         continue_pipe_connect(c, s);
608 }
609
610
611 /*
612   Stage 3 of pipe_connect_b: Receive result of pipe connect request on unix socket
613 */
614 static void continue_pipe_connect_ncacn_unix(struct composite_context *ctx)
615 {
616         struct composite_context *c = talloc_get_type(ctx->async.private_data,
617                                                       struct composite_context);
618         struct pipe_connect_state *s = talloc_get_type(c->private_data,
619                                                        struct pipe_connect_state);
620         
621         c->status = dcerpc_pipe_connect_ncacn_unix_stream_recv(ctx);
622         if (!composite_is_ok(c)) return;
623         
624         continue_pipe_connect(c, s);
625 }
626
627
628 /*
629   Stage 3 of pipe_connect_b: Receive result of pipe connect request on local rpc
630 */
631 static void continue_pipe_connect_ncalrpc(struct composite_context *ctx)
632 {
633         struct composite_context *c = talloc_get_type(ctx->async.private_data,
634                                                       struct composite_context);
635         struct pipe_connect_state *s = talloc_get_type(c->private_data,
636                                                        struct pipe_connect_state);
637         
638         c->status = dcerpc_pipe_connect_ncalrpc_recv(ctx);
639         if (!composite_is_ok(c)) return;
640
641         continue_pipe_connect(c, s);
642 }
643
644
645 /*
646   Stage 4 of pipe_connect_b: Start an authentication on connected dcerpc pipe
647   depending on credentials and binding flags passed.
648 */
649 static void continue_pipe_connect(struct composite_context *c, struct pipe_connect_state *s)
650 {
651         struct composite_context *auth_bind_req;
652
653         s->pipe->binding = s->binding;
654         if (!talloc_reference(s->pipe, s->binding)) {
655                 composite_error(c, NT_STATUS_NO_MEMORY);
656                 return;
657         }
658
659         auth_bind_req = dcerpc_pipe_auth_send(s->pipe, s->binding, s->table,
660                                               s->credentials);
661         composite_continue(c, auth_bind_req, continue_pipe_auth, c);
662 }
663
664
665 /*
666   Stage 5 of pipe_connect_b: Receive result of pipe authentication request
667   and say if all went ok
668 */
669 static void continue_pipe_auth(struct composite_context *ctx)
670 {
671         struct composite_context *c = talloc_get_type(ctx->async.private_data,
672                                                       struct composite_context);
673         struct pipe_connect_state *s = talloc_get_type(c->private_data, struct pipe_connect_state);
674
675         c->status = dcerpc_pipe_auth_recv(ctx, s, &s->pipe);
676         if (!composite_is_ok(c)) return;
677
678         composite_done(c);
679 }
680
681
682 /*
683   handle timeouts of a dcerpc connect
684 */
685 static void dcerpc_connect_timeout_handler(struct event_context *ev, struct timed_event *te, 
686                                            struct timeval t, void *private)
687 {
688         struct composite_context *c = talloc_get_type(private, struct composite_context);
689         composite_error(c, NT_STATUS_IO_TIMEOUT);
690 }
691
692 /*
693   start a request to open a rpc connection to a rpc pipe, using
694   specified binding structure to determine the endpoint and options
695 */
696 struct composite_context* dcerpc_pipe_connect_b_send(TALLOC_CTX *parent_ctx,
697                                                      struct dcerpc_binding *binding,
698                                                      const struct dcerpc_interface_table *table,
699                                                      struct cli_credentials *credentials,
700                                                      struct event_context *ev)
701 {
702         struct composite_context *c;
703         struct pipe_connect_state *s;
704         struct event_context *new_ev = NULL;
705
706         if (ev == NULL) {
707                 new_ev = event_context_init(parent_ctx);
708                 if (new_ev == NULL) return NULL;
709                 ev = new_ev;
710         }
711
712         /* composite context allocation and setup */
713         c = composite_create(parent_ctx, ev);
714         if (c == NULL) {
715                 talloc_free(new_ev);
716                 return NULL;
717         }
718         talloc_steal(c, new_ev);
719
720         s = talloc_zero(c, struct pipe_connect_state);
721         if (composite_nomem(s, c)) return c;
722         c->private_data = s;
723
724         /* initialise dcerpc pipe structure */
725         s->pipe = dcerpc_pipe_init(c, ev);
726         if (composite_nomem(s->pipe, c)) return c;
727
728         /* store parameters in state structure */
729         s->binding      = binding;
730         s->table        = table;
731         s->credentials  = credentials;
732
733         event_add_timed(c->event_ctx, c,
734                         timeval_current_ofs(DCERPC_REQUEST_TIMEOUT, 0),
735                         dcerpc_connect_timeout_handler, c);
736         
737         switch (s->binding->transport) {
738         case NCACN_NP:
739         case NCACN_IP_TCP:
740         case NCALRPC:
741                 if (!s->binding->endpoint) {
742                         struct composite_context *binding_req;
743                         binding_req = dcerpc_epm_map_binding_send(c, s->binding, s->table,
744                                                                   s->pipe->conn->event_ctx);
745                         composite_continue(c, binding_req, continue_map_binding, c);
746                         return c;
747                 }
748
749         default:
750                 break;
751         }
752
753         continue_connect(c, s);
754         return c;
755 }
756
757
758 /*
759   receive result of a request to open a rpc connection to a rpc pipe
760 */
761 NTSTATUS dcerpc_pipe_connect_b_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
762                                     struct dcerpc_pipe **p)
763 {
764         NTSTATUS status;
765         struct pipe_connect_state *s;
766         
767         status = composite_wait(c);
768         
769         if (NT_STATUS_IS_OK(status)) {
770                 s = talloc_get_type(c->private_data, struct pipe_connect_state);
771                 talloc_steal(mem_ctx, s->pipe);
772                 *p = s->pipe;
773         }
774         talloc_free(c);
775         return status;
776 }
777
778
779 /*
780   open a rpc connection to a rpc pipe, using the specified 
781   binding structure to determine the endpoint and options - sync version
782 */
783 NTSTATUS dcerpc_pipe_connect_b(TALLOC_CTX *parent_ctx,
784                                struct dcerpc_pipe **pp,
785                                struct dcerpc_binding *binding,
786                                const struct dcerpc_interface_table *table,
787                                struct cli_credentials *credentials,
788                                struct event_context *ev)
789 {
790         struct composite_context *c;
791         
792         c = dcerpc_pipe_connect_b_send(parent_ctx, binding, table,
793                                        credentials, ev);
794         return dcerpc_pipe_connect_b_recv(c, parent_ctx, pp);
795 }
796
797
798 struct pipe_conn_state {
799         struct dcerpc_pipe *pipe;
800 };
801
802
803 static void continue_pipe_connect_b(struct composite_context *ctx);
804
805
806 /*
807   Initiate rpc connection to a rpc pipe, using the specified string
808   binding to determine the endpoint and options.
809   The string is to be parsed to a binding structure first.
810 */
811 struct composite_context* dcerpc_pipe_connect_send(TALLOC_CTX *parent_ctx,
812                                                    const char *binding,
813                                                    const struct dcerpc_interface_table *table,
814                                                    struct cli_credentials *credentials,
815                                                    struct event_context *ev)
816 {
817         struct composite_context *c;
818         struct pipe_conn_state *s;
819         struct dcerpc_binding *b;
820         struct composite_context *pipe_conn_req;
821         struct event_context *new_ev = NULL;
822
823         if (ev == NULL) {
824                 new_ev = event_context_init(parent_ctx);
825                 if (new_ev == NULL) return NULL;
826                 ev = new_ev;
827         }
828
829         /* composite context allocation and setup */
830         c = composite_create(parent_ctx, ev);
831         if (c == NULL) {
832                 talloc_free(new_ev);
833                 return NULL;
834         }
835         talloc_steal(c, new_ev);
836
837         s = talloc_zero(c, struct pipe_conn_state);
838         if (composite_nomem(s, c)) return c;
839         c->private_data = s;
840
841         /* parse binding string to the structure */
842         c->status = dcerpc_parse_binding(c, binding, &b);
843         if (!NT_STATUS_IS_OK(c->status)) {
844                 DEBUG(0, ("Failed to parse dcerpc binding '%s'\n", binding));
845                 composite_error(c, c->status);
846                 return c;
847         }
848
849         DEBUG(3, ("Using binding %s\n", dcerpc_binding_string(c, b)));
850
851         /* 
852            start connecting to a rpc pipe after binding structure
853            is established
854          */
855         pipe_conn_req = dcerpc_pipe_connect_b_send(c, b, table,
856                                                    credentials, ev);
857         composite_continue(c, pipe_conn_req, continue_pipe_connect_b, c);
858         return c;
859 }
860
861
862 /*
863   Stage 2 of pipe_connect: Receive result of actual pipe connect request
864   and say if we're done ok
865 */
866 static void continue_pipe_connect_b(struct composite_context *ctx)
867 {
868         struct composite_context *c = talloc_get_type(ctx->async.private_data,
869                                                       struct composite_context);
870         struct pipe_conn_state *s = talloc_get_type(c->private_data,
871                                                     struct pipe_conn_state);
872
873         c->status = dcerpc_pipe_connect_b_recv(ctx, c, &s->pipe);
874         talloc_steal(s, s->pipe);
875         if (!composite_is_ok(c)) return;
876
877         composite_done(c);
878 }
879
880
881 /*
882   Receive result of pipe connect (using binding string) request
883   and return connected pipe structure.
884 */
885 NTSTATUS dcerpc_pipe_connect_recv(struct composite_context *c,
886                                   TALLOC_CTX *mem_ctx,
887                                   struct dcerpc_pipe **pp)
888 {
889         NTSTATUS status;
890         struct pipe_conn_state *s;
891
892         status = composite_wait(c);
893         s = talloc_get_type(c->private_data, struct pipe_conn_state);
894         *pp = talloc_steal(mem_ctx, s->pipe);
895
896         talloc_free(c);
897         return status;
898 }
899
900
901 /*
902   Open a rpc connection to a rpc pipe, using the specified string
903   binding to determine the endpoint and options - sync version
904 */
905 NTSTATUS dcerpc_pipe_connect(TALLOC_CTX *parent_ctx, 
906                              struct dcerpc_pipe **pp, 
907                              const char *binding,
908                              const struct dcerpc_interface_table *table,
909                              struct cli_credentials *credentials,
910                              struct event_context *ev)
911 {
912         struct composite_context *c;
913         c = dcerpc_pipe_connect_send(parent_ctx, binding, 
914                                      table,
915                                      credentials, ev);
916         return dcerpc_pipe_connect_recv(c, parent_ctx, pp);
917 }
918
919
920 struct sec_conn_state {
921         struct dcerpc_pipe *pipe;
922         struct dcerpc_pipe *pipe2;
923         struct dcerpc_binding *binding;
924         struct smbcli_tree *tree;
925 };
926
927
928 static void continue_open_smb(struct composite_context *ctx);
929 static void continue_open_tcp(struct composite_context *ctx);
930 static void continue_open_pipe(struct composite_context *ctx);
931 static void continue_pipe_open(struct composite_context *c);
932
933
934 /*
935   Send request to create a secondary dcerpc connection from a primary
936   connection
937 */
938 struct composite_context* dcerpc_secondary_connection_send(struct dcerpc_pipe *p,
939                                                            struct dcerpc_binding *b)
940 {
941         struct composite_context *c;
942         struct sec_conn_state *s;
943         struct composite_context *pipe_smb_req;
944         struct composite_context *pipe_tcp_req;
945         struct composite_context *pipe_ncalrpc_req;
946         
947         /* composite context allocation and setup */
948         c = composite_create(p, p->conn->event_ctx);
949         if (c == NULL) return NULL;
950
951         s = talloc_zero(c, struct sec_conn_state);
952         if (composite_nomem(s, c)) return c;
953         c->private_data = s;
954
955         s->pipe     = p;
956         s->binding  = b;
957
958         /* initialise second dcerpc pipe based on primary pipe's event context */
959         s->pipe2 = dcerpc_pipe_init(c, s->pipe->conn->event_ctx);
960         if (composite_nomem(s->pipe2, c)) return c;
961
962         /* open second dcerpc pipe using the same transport as for primary pipe */
963         switch (s->pipe->conn->transport.transport) {
964         case NCACN_NP:
965                 /* get smb tree of primary dcerpc pipe opened on smb */
966                 s->tree = dcerpc_smb_tree(s->pipe->conn);
967                 if (!s->tree) {
968                         composite_error(c, NT_STATUS_INVALID_PARAMETER);
969                         return c;
970                 }
971
972                 pipe_smb_req = dcerpc_pipe_open_smb_send(s->pipe2->conn, s->tree,
973                                                          s->binding->endpoint);
974                 composite_continue(c, pipe_smb_req, continue_open_smb, c);
975                 return c;
976
977         case NCACN_IP_TCP:
978                 pipe_tcp_req = dcerpc_pipe_open_tcp_send(s->pipe2->conn,
979                                                          s->binding->host,
980                                                          s->binding->target_hostname,
981                                                          atoi(s->binding->endpoint));
982                 composite_continue(c, pipe_tcp_req, continue_open_tcp, c);
983                 return c;
984
985         case NCALRPC:
986                 pipe_ncalrpc_req = dcerpc_pipe_open_pipe_send(s->pipe2->conn,
987                                                               s->binding->endpoint);
988                 composite_continue(c, pipe_ncalrpc_req, continue_open_pipe, c);
989                 return c;
990
991         default:
992                 /* looks like a transport we don't support */
993                 composite_error(c, NT_STATUS_NOT_SUPPORTED);
994         }
995
996         return c;
997 }
998
999
1000 /*
1001   Stage 2 of secondary_connection: Receive result of pipe open request on smb
1002 */
1003 static void continue_open_smb(struct composite_context *ctx)
1004 {
1005         struct composite_context *c = talloc_get_type(ctx->async.private_data,
1006                                                       struct composite_context);
1007         
1008         c->status = dcerpc_pipe_open_smb_recv(ctx);
1009         if (!composite_is_ok(c)) return;
1010
1011         continue_pipe_open(c);
1012 }
1013
1014
1015 /*
1016   Stage 2 of secondary_connection: Receive result of pipe open request on tcp/ip
1017 */
1018 static void continue_open_tcp(struct composite_context *ctx)
1019 {
1020         struct composite_context *c = talloc_get_type(ctx->async.private_data,
1021                                                       struct composite_context);
1022         
1023         c->status = dcerpc_pipe_open_tcp_recv(ctx);
1024         if (!composite_is_ok(c)) return;
1025
1026         continue_pipe_open(c);
1027 }
1028
1029
1030 /*
1031   Stage 2 of secondary_connection: Receive result of pipe open request on ncalrpc
1032 */
1033 static void continue_open_pipe(struct composite_context *ctx)
1034 {
1035         struct composite_context *c = talloc_get_type(ctx->async.private_data,
1036                                                       struct composite_context);
1037
1038         c->status = dcerpc_pipe_open_pipe_recv(ctx);
1039         if (!composite_is_ok(c)) return;
1040
1041         continue_pipe_open(c);
1042 }
1043
1044
1045 /*
1046   Stage 3 of secondary_connection: Get binding data and flags from primary pipe
1047   and say if we're done ok.
1048 */
1049 static void continue_pipe_open(struct composite_context *c)
1050 {
1051         struct sec_conn_state *s;
1052
1053         s = talloc_get_type(c->private_data, struct sec_conn_state);
1054
1055         s->pipe2->conn->flags = s->pipe->conn->flags;
1056         s->pipe2->binding     = s->binding;
1057         if (!talloc_reference(s->pipe2, s->binding)) {
1058                 composite_error(c, NT_STATUS_NO_MEMORY);
1059                 return;
1060         }
1061
1062         composite_done(c);
1063 }
1064
1065
1066 /*
1067   Receive result of secondary rpc connection request and return
1068   second dcerpc pipe.
1069 */
1070 NTSTATUS dcerpc_secondary_connection_recv(struct composite_context *c,
1071                                           struct dcerpc_pipe **p2)
1072 {
1073         NTSTATUS status = composite_wait(c);
1074         struct sec_conn_state *s;
1075
1076         s = talloc_get_type(c->private_data, struct sec_conn_state);
1077
1078         if (NT_STATUS_IS_OK(status)) {
1079                 *p2 = talloc_steal(s->pipe, s->pipe2);
1080         }
1081
1082         talloc_free(c);
1083         return status;
1084 }
1085
1086 /*
1087   Create a secondary dcerpc connection from a primary connection
1088   - sync version
1089
1090   If the primary is a SMB connection then the secondary connection
1091   will be on the same SMB connection, but using a new fnum
1092 */
1093 NTSTATUS dcerpc_secondary_connection(struct dcerpc_pipe *p,
1094                                      struct dcerpc_pipe **p2,
1095                                      struct dcerpc_binding *b)
1096 {
1097         struct composite_context *c;
1098         
1099         c = dcerpc_secondary_connection_send(p, b);
1100         return dcerpc_secondary_connection_recv(c, p2);
1101 }