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