r3123: Add dcerpc_binding_build_tower()
[kai/samba-autobuild/.git] / source4 / librpc / rpc / dcerpc_util.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    dcerpc utility functions
5
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) Jelmer Vernooij 2004
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25
26 /*
27   work out what TCP port to use for a given interface on a given host
28 */
29 NTSTATUS dcerpc_epm_map_tcp_port(const char *server, 
30                                  const char *uuid, uint_t version,
31                                  uint32_t *port)
32 {
33         struct dcerpc_pipe *p;
34         NTSTATUS status;
35         struct epm_Map r;
36         struct policy_handle handle;
37         struct GUID guid;
38         struct epm_twr_t twr, *twr_r;
39
40         if (strcasecmp(uuid, DCERPC_EPMAPPER_UUID) == 0 ||
41             strcasecmp(uuid, DCERPC_MGMT_UUID) == 0) {
42                 /* don't lookup epmapper via epmapper! */
43                 *port = EPMAPPER_PORT;
44                 return NT_STATUS_OK;
45         }
46
47         status = dcerpc_pipe_open_tcp(&p, server, EPMAPPER_PORT, AF_UNSPEC );
48         if (!NT_STATUS_IS_OK(status)) {
49                 return status;
50         }
51
52         /* we can use the pipes memory context here as we will have a short
53            lived connection */
54         status = dcerpc_bind_byuuid(p, p, 
55                                     DCERPC_EPMAPPER_UUID,
56                                     DCERPC_EPMAPPER_VERSION);
57         if (!NT_STATUS_IS_OK(status)) {
58                 dcerpc_pipe_close(p);
59                 return status;
60         }
61
62         ZERO_STRUCT(handle);
63         ZERO_STRUCT(guid);
64
65         twr.tower.num_floors = 5;
66         twr.tower.floors = talloc(p, sizeof(twr.tower.floors[0]) * 5);
67
68         /* what I'd like for christmas ... */
69
70         /* an RPC interface ... */
71         twr.tower.floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
72         GUID_from_string(uuid, &twr.tower.floors[0].lhs.info.uuid.uuid);
73         twr.tower.floors[0].lhs.info.uuid.version = version;
74         twr.tower.floors[0].rhs.uuid.unknown = 0;
75
76         /* encoded with NDR ... */
77         twr.tower.floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
78         GUID_from_string(NDR_GUID, &twr.tower.floors[1].lhs.info.uuid.uuid);
79         twr.tower.floors[1].lhs.info.uuid.version = NDR_GUID_VERSION;
80         twr.tower.floors[1].rhs.uuid.unknown = 0;
81
82         /* on an RPC connection ... */
83         twr.tower.floors[2].lhs.protocol = EPM_PROTOCOL_NCACN;
84         twr.tower.floors[2].lhs.info.lhs_data = data_blob(NULL, 0);
85         twr.tower.floors[2].rhs.ncacn.minor_version = 0;
86
87         /* on a TCP port ... */
88         twr.tower.floors[3].lhs.protocol = EPM_PROTOCOL_TCP;
89         twr.tower.floors[3].lhs.info.lhs_data = data_blob(NULL, 0);
90         twr.tower.floors[3].rhs.tcp.port = 0;
91
92         /* on an IP link ... */
93         twr.tower.floors[4].lhs.protocol = EPM_PROTOCOL_IP;
94         twr.tower.floors[4].lhs.info.lhs_data = data_blob(NULL, 0);
95         twr.tower.floors[4].rhs.ip.address = 0;
96
97         /* with some nice pretty paper around it of course */
98         r.in.object = &guid;
99         r.in.map_tower = &twr;
100         r.in.entry_handle = &handle;
101         r.in.max_towers = 1;
102         r.out.entry_handle = &handle;
103
104         status = dcerpc_epm_Map(p, p, &r);
105         if (!NT_STATUS_IS_OK(status)) {
106                 dcerpc_pipe_close(p);
107                 return status;
108         }
109         if (r.out.result != 0 || r.out.num_towers != 1) {
110                 dcerpc_pipe_close(p);
111                 return NT_STATUS_PORT_UNREACHABLE;
112         }
113
114         twr_r = r.out.towers[0].twr;
115         if (!twr_r) {
116                 dcerpc_pipe_close(p);
117                 return NT_STATUS_PORT_UNREACHABLE;
118         }
119
120         if (twr_r->tower.num_floors != 5 ||
121             twr_r->tower.floors[3].lhs.protocol != twr.tower.floors[3].lhs.protocol) {
122                 dcerpc_pipe_close(p);
123                 return NT_STATUS_PORT_UNREACHABLE;
124         }
125
126         *port = twr_r->tower.floors[3].rhs.tcp.port;
127
128         dcerpc_pipe_close(p);
129
130         return NT_STATUS_OK;
131 }
132
133 /*
134   find the pipe name for a local IDL interface
135 */
136 const char *idl_pipe_name(const char *uuid, uint32_t if_version)
137 {
138         int i;
139         for (i=0;dcerpc_pipes[i];i++) {
140                 if (strcasecmp(dcerpc_pipes[i]->uuid, uuid) == 0 &&
141                     dcerpc_pipes[i]->if_version == if_version) {
142                         return dcerpc_pipes[i]->name;
143                 }
144         }
145         return "UNKNOWN";
146 }
147
148 /*
149   find the number of calls defined by local IDL
150 */
151 int idl_num_calls(const char *uuid, uint32_t if_version)
152 {
153         int i;
154         for (i=0;dcerpc_pipes[i];i++) {
155                 if (strcasecmp(dcerpc_pipes[i]->uuid, uuid) == 0 &&
156                     dcerpc_pipes[i]->if_version == if_version) {
157                         return dcerpc_pipes[i]->num_calls;
158                 }
159         }
160         return -1;
161 }
162
163
164 /*
165   find a dcerpc interface by name
166 */
167 const struct dcerpc_interface_table *idl_iface_by_name(const char *name)
168 {
169         int i;
170         for (i=0;dcerpc_pipes[i];i++) {
171                 if (strcasecmp(dcerpc_pipes[i]->name, name) == 0) {
172                         return dcerpc_pipes[i];
173                 }
174         }
175         return NULL;
176 }
177
178 /*
179   find a dcerpc interface by uuid
180 */
181 const struct dcerpc_interface_table *idl_iface_by_uuid(const char *uuid)
182 {
183         int i;
184         for (i=0;dcerpc_pipes[i];i++) {
185                 if (strcasecmp(dcerpc_pipes[i]->uuid, uuid) == 0) {
186                         return dcerpc_pipes[i];
187                 }
188         }
189         return NULL;
190 }
191
192
193
194 /* 
195    push a dcerpc_packet into a blob, potentially with auth info
196 */
197 NTSTATUS dcerpc_push_auth(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, 
198                           struct dcerpc_packet *pkt,
199                           struct dcerpc_auth *auth_info)
200 {
201         NTSTATUS status;
202         struct ndr_push *ndr;
203
204         ndr = ndr_push_init_ctx(mem_ctx);
205         if (!ndr) {
206                 return NT_STATUS_NO_MEMORY;
207         }
208
209         if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
210                 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
211         }
212
213         if (auth_info) {
214                 pkt->auth_length = auth_info->credentials.length;
215         } else {
216                 pkt->auth_length = 0;
217         }
218
219         status = ndr_push_dcerpc_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
220         if (!NT_STATUS_IS_OK(status)) {
221                 return status;
222         }
223
224         if (auth_info) {
225                 status = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, auth_info);
226         }
227
228         *blob = ndr_push_blob(ndr);
229
230         /* fill in the frag length */
231         dcerpc_set_frag_length(blob, blob->length);
232
233         return NT_STATUS_OK;
234 }
235
236 #define MAX_PROTSEQ             10
237
238 static const struct {
239         const char *name;
240         enum dcerpc_transport_t transport;
241         int num_protocols;
242         enum epm_protocols protseq[MAX_PROTSEQ];
243 } transports[] = {
244         { "ncacn_np",     NCACN_NP, 3, 
245                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SMB, EPM_PROTOCOL_PIPE }},
246         { "ncacn_ip_tcp", NCACN_IP_TCP, 3, 
247                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_IP, EPM_PROTOCOL_TCP } }, 
248         { "ncadg_ip_udp", NCACN_IP_UDP, 3, 
249                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_IP, EPM_PROTOCOL_UDP } },
250         { "ncalrpc", NCALRPC, 2, 
251                 { EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_PIPE } },
252         { "ncacn_unix_stream", NCACN_UNIX_STREAM, 2, 
253                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_UNIX_DS } },
254         { "ncadg_unix_dgram", NCADG_UNIX_DGRAM, 2, 
255                 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UNIX_DS } },
256         { "ncacn_at_dsp", NCACN_AT_DSP, 3, 
257                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DSP } },
258         { "ncadg_at_ddp", NCADG_AT_DDP, 3, 
259                 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DDP } },
260         { "ncacn_vns_ssp", NCACN_VNS_SPP, 3, 
261                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_SPP } },
262         { "ncacn_vns_ipc", NCACN_VNS_IPC, 3, 
263                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_IPC }, },
264 };
265
266 static const struct {
267         const char *name;
268         uint32_t flag;
269 } ncacn_options[] = {
270         {"sign", DCERPC_SIGN},
271         {"seal", DCERPC_SEAL},
272         {"connect", DCERPC_CONNECT},
273         {"validate", DCERPC_DEBUG_VALIDATE_BOTH},
274         {"print", DCERPC_DEBUG_PRINT_BOTH},
275         {"padcheck", DCERPC_DEBUG_PAD_CHECK},
276         {"bigendian", DCERPC_PUSH_BIGENDIAN}
277 };
278
279
280
281 /*
282   form a binding string from a binding structure
283 */
284 const char *dcerpc_binding_string(TALLOC_CTX *mem_ctx, const struct dcerpc_binding *b)
285 {
286         char *s = NULL;
287         int i;
288         const char *t_name=NULL;
289
290         for (i=0;i<ARRAY_SIZE(transports);i++) {
291                 if (transports[i].transport == b->transport) {
292                         t_name = transports[i].name;
293                 }
294         }
295         if (!t_name) {
296                 return NULL;
297         }
298
299         if (b->object) { 
300                 s = talloc_asprintf(mem_ctx, "%s@", GUID_string(mem_ctx, b->object));
301         }
302
303         s = talloc_asprintf_append(s, "%s:%s", t_name, b->host);
304         if (!s) return NULL;
305
306         if ((!b->options || !b->options[0]) && !b->flags) {
307                 return s;
308         }
309
310         s = talloc_asprintf_append(s, "[");
311
312         /* this is a *really* inefficent way of dealing with strings,
313            but this is rarely called and the strings are always short,
314            so I don't care */
315         for (i=0;b->options && b->options[i];i++) {
316                 s = talloc_asprintf_append(s, "%s,", b->options[i]);
317                 if (!s) return NULL;
318         }
319         for (i=0;i<ARRAY_SIZE(ncacn_options);i++) {
320                 if (b->flags & ncacn_options[i].flag) {
321                         s = talloc_asprintf_append(s, "%s,", ncacn_options[i].name);
322                         if (!s) return NULL;
323                 }
324         }
325
326         s[strlen(s)-1] = 0;
327         s = talloc_asprintf_append(s, "]");
328
329         return s;
330 }
331
332 /*
333   parse a binding string into a dcerpc_binding structure
334 */
335 NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *s, struct dcerpc_binding *b)
336 {
337         char *options, *type;
338         char *p;
339         int i, j, comma_count;
340
341         p = strchr(s, '@');
342
343         if (p && PTR_DIFF(p, s) == 36) { /* 36 is the length of a UUID */
344                 NTSTATUS status;
345
346                 b->object = talloc_p(mem_ctx, struct GUID);
347                 
348                 status = GUID_from_string(s, b->object);
349
350                 if (NT_STATUS_IS_ERR(status)) {
351                         DEBUG(0, ("Failed parsing UUID\n"));
352                         return status;
353                 }
354
355                 s = p + 1;
356         } else {
357                 b->object = NULL;
358         }
359
360         p = strchr(s, ':');
361         if (!p) {
362                 return NT_STATUS_INVALID_PARAMETER;
363         }
364
365         type = talloc_strndup(mem_ctx, s, PTR_DIFF(p, s));
366         if (!type) {
367                 return NT_STATUS_NO_MEMORY;
368         }
369
370         for (i=0;i<ARRAY_SIZE(transports);i++) {
371                 if (strcasecmp(type, transports[i].name) == 0) {
372                         b->transport = transports[i].transport;
373                         break;
374                 }
375         }
376         if (i==ARRAY_SIZE(transports)) {
377                 DEBUG(0,("Unknown dcerpc transport '%s'\n", type));
378                 return NT_STATUS_INVALID_PARAMETER;
379         }
380         
381         s = p+1;
382
383         p = strchr(s, '[');
384         if (p) {
385                 b->host = talloc_strndup(mem_ctx, s, PTR_DIFF(p, s));
386                 options = talloc_strdup(mem_ctx, p+1);
387                 if (options[strlen(options)-1] != ']') {
388                         return NT_STATUS_INVALID_PARAMETER;
389                 }
390                 options[strlen(options)-1] = 0;
391         } else {
392                 b->host = talloc_strdup(mem_ctx, s);
393                 options = NULL;
394         }
395
396         if (!b->host) {
397                 return NT_STATUS_NO_MEMORY;
398         }
399
400         b->options = NULL;
401         b->flags = 0;
402
403         if (!options) {
404                 return NT_STATUS_OK;
405         }
406
407         comma_count = count_chars(options, ',');
408         b->options = talloc_array_p(mem_ctx, const char *, comma_count+2);
409         if (!b->options) {
410                 return NT_STATUS_NO_MEMORY;
411         }
412
413         for (i=0; (p = strchr(options, ',')); i++) {
414                 b->options[i] = talloc_strndup(mem_ctx, options, PTR_DIFF(p, options));
415                 if (!b->options[i]) {
416                         return NT_STATUS_NO_MEMORY;
417                 }
418                 options = p+1;
419         }
420         b->options[i] = options;
421         b->options[i+1] = NULL;
422
423         /* some options are pre-parsed for convenience */
424         for (i=0;b->options[i];i++) {
425                 for (j=0;j<ARRAY_SIZE(ncacn_options);j++) {
426                         if (strcasecmp(ncacn_options[j].name, b->options[i]) == 0) {
427                                 int k;
428                                 b->flags |= ncacn_options[j].flag;
429                                 for (k=i;b->options[k];k++) {
430                                         b->options[k] = b->options[k+1];
431                                 }
432                                 i--;
433                                 break;
434                         }
435                 }
436         }
437         
438         return NT_STATUS_OK;
439 }
440
441 static NTSTATUS floor_set_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *floor,  const char *data)
442 {
443         switch (floor->lhs.protocol) {
444         case EPM_PROTOCOL_TCP:
445                 floor->rhs.tcp.port = atoi(data);
446                 return NT_STATUS_OK;
447                 
448         case EPM_PROTOCOL_UDP:
449                 floor->rhs.udp.port = atoi(data);
450                 return NT_STATUS_OK;
451
452         case EPM_PROTOCOL_HTTP:
453                 floor->rhs.http.port = atoi(data);
454                 return NT_STATUS_OK;
455
456         case EPM_PROTOCOL_IP:
457                 floor->rhs.ip.address = 0;
458
459                 /* Only try to put in a IPv4 address. Windows 2003 just returns 
460                  * 0.0.0.0 for IPv6 addresses */
461                 if (strlen(data) > 0) {
462                     struct addrinfo hints, *res;
463
464                         memset(&hints, 0, sizeof(struct addrinfo));
465
466                         hints.ai_family = AF_INET;
467
468                         if (getaddrinfo(data, NULL, &hints, &res) < 0) {
469                                 return NT_STATUS_BAD_NETWORK_NAME;
470                         }
471
472                         floor->rhs.ip.address = ntohl(((struct in_addr *)res->ai_addr)->s_addr);
473
474                         freeaddrinfo(res);
475                 }
476                 return NT_STATUS_OK;
477
478         case EPM_PROTOCOL_NCACN:
479                 floor->rhs.ncacn.minor_version = 0;
480                 return NT_STATUS_OK;
481
482         case EPM_PROTOCOL_NCADG:
483                 floor->rhs.ncadg.minor_version = 0;
484                 return NT_STATUS_OK;
485
486         case EPM_PROTOCOL_SMB:
487                 floor->rhs.smb.unc = talloc_strdup(mem_ctx, data);
488                 if (!floor->rhs.smb.unc) {
489                         return NT_STATUS_NO_MEMORY;
490                 }
491                 return NT_STATUS_OK;
492
493         case EPM_PROTOCOL_PIPE:
494                 floor->rhs.pipe.path = talloc_strdup(mem_ctx, data);
495                 if (!floor->rhs.pipe.path) {
496                         return NT_STATUS_NO_MEMORY;
497                 }
498                 return NT_STATUS_OK;
499
500         case EPM_PROTOCOL_NETBIOS:
501                 floor->rhs.netbios.name = talloc_strdup(mem_ctx, data);
502                 if (!floor->rhs.netbios.name) {
503                         return NT_STATUS_NO_MEMORY;
504                 }
505                 return NT_STATUS_OK;
506
507         case EPM_PROTOCOL_NCALRPC:
508                 return NT_STATUS_OK;
509                 
510         case EPM_PROTOCOL_VINES_SPP:
511                 floor->rhs.vines_spp.port = atoi(data);
512                 return NT_STATUS_OK;
513                 
514         case EPM_PROTOCOL_VINES_IPC:
515                 floor->rhs.vines_ipc.port = atoi(data);
516                 return NT_STATUS_OK;
517                 
518         case EPM_PROTOCOL_STREETTALK:
519                 floor->rhs.streettalk.streettalk = talloc_strdup(mem_ctx, data);
520                 if (!floor->rhs.streettalk.streettalk) {
521                         return NT_STATUS_NO_MEMORY;
522                 }
523                 return NT_STATUS_OK;
524                 
525         case EPM_PROTOCOL_UNIX_DS:
526                 floor->rhs.unix_ds.path = talloc_strdup(mem_ctx, data);
527                 if (!floor->rhs.unix_ds.path) {
528                         return NT_STATUS_NO_MEMORY;
529                 }
530                 return NT_STATUS_OK;
531                 
532         case EPM_PROTOCOL_NULL:
533                 return NT_STATUS_OK;
534         }
535
536         return NT_STATUS_NOT_SUPPORTED;
537 }
538
539
540 NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx, struct dcerpc_binding *binding, struct epm_tower **tower)
541 {
542         const enum epm_protocols *protseq;
543         int num_protocols = -1, i;
544         NTSTATUS status;
545         
546         *tower = talloc_p(mem_ctx, struct epm_tower);
547
548         if (!(*tower)) {
549                 return NT_STATUS_NO_MEMORY;
550         }
551
552         /* Find transport */
553         for (i=0;i<ARRAY_SIZE(transports);i++) {
554                 if (transports[i].transport == binding->transport) {
555                         protseq = transports[i].protseq;
556                         num_protocols = transports[i].num_protocols;
557                         break;
558                 }
559         }
560
561         if (num_protocols == -1) {
562                 DEBUG(0, ("Unable to find transport with id '%d'\n", binding->transport));
563                 return NT_STATUS_UNSUCCESSFUL;
564         }
565
566         (*tower)->num_floors = 2 + num_protocols;
567         (*tower)->floors = talloc_array_p(mem_ctx, struct epm_floor, (*tower)->num_floors);
568
569         /* Floor 0 */
570         (*tower)->floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
571         if (binding->object) {
572                 (*tower)->floors[0].lhs.info.uuid.uuid = *binding->object;
573         }
574         (*tower)->floors[0].lhs.info.uuid.version = 0;
575         
576         /* Floor 1 */
577         (*tower)->floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
578         (*tower)->floors[1].lhs.info.uuid.version = NDR_GUID_VERSION;
579         status = GUID_from_string(NDR_GUID, &(*tower)->floors[1].lhs.info.uuid.uuid);
580         if (NT_STATUS_IS_ERR(status)) {
581                 return status;
582         }
583
584         /* Floor 2 to num_protocols */
585         for (i = 0; i < num_protocols; i++) {
586                 (*tower)->floors[2 + i].lhs.protocol = protseq[i];
587         }
588
589         /* The top floor contains the endpoint */
590         if (num_protocols >= 1 && binding->options && binding->options[0]) {
591                 status = floor_set_rhs_data(mem_ctx, &(*tower)->floors[2 + num_protocols - 1], binding->options[0]);
592                 if (NT_STATUS_IS_ERR(status)) {
593                         return status;
594                 }
595         }
596
597         /* The second-to-top floor contains the network address */
598         if (num_protocols >= 2 && binding->host) {
599                 status = floor_set_rhs_data(mem_ctx, &(*tower)->floors[2 + num_protocols - 2], binding->host);
600                 if (NT_STATUS_IS_ERR(status)) {
601                         return status;
602                 }
603         }
604
605         return NT_STATUS_OK;
606 }
607
608
609 /* open a rpc connection to a rpc pipe on SMB using the binding
610    structure to determine the endpoint and options */
611 static NTSTATUS dcerpc_pipe_connect_ncacn_np(struct dcerpc_pipe **p, 
612                                              struct dcerpc_binding *binding,
613                                              const char *pipe_uuid, 
614                                              uint32_t pipe_version,
615                                              const char *domain,
616                                              const char *username,
617                                              const char *password)
618 {
619         NTSTATUS status;
620         BOOL retry;
621         struct smbcli_state *cli;
622         const char *pipe_name;
623         TALLOC_CTX *mem_ctx = talloc_init("dcerpc_pipe_connect_ncacn_np");
624         
625         if (!binding->options || !binding->options[0] || !strlen(binding->options[0])) {
626                 const struct dcerpc_interface_table *table = idl_iface_by_uuid(pipe_uuid);
627                 struct dcerpc_binding default_binding;
628                 int i;
629
630                 if (!table) {
631                         DEBUG(0,("Unknown interface endpoint '%s'\n", pipe_uuid));
632                         talloc_destroy(mem_ctx);
633                         return NT_STATUS_INVALID_PARAMETER;
634                 }
635
636                 /* Find one of the default pipes for this interface */
637                 for (i = 0; i < table->endpoints->count; i++) {
638                         status = dcerpc_parse_binding(mem_ctx, table->endpoints->names[i], &default_binding);
639
640                         if (NT_STATUS_IS_OK(status) && default_binding.transport == NCACN_NP) {
641                                 pipe_name = default_binding.options[0]; 
642                                 break;
643                                 
644                         }
645                 }
646         } else {
647                 pipe_name = binding->options[0];
648         }
649
650         if (!strncasecmp(pipe_name, "/pipe/", 6) || 
651                 !strncasecmp(pipe_name, "\\pipe\\", 6)) {
652                 pipe_name+=6;
653         }
654
655         if (pipe_name[0] != '\\') {
656                 pipe_name = talloc_asprintf(mem_ctx, "\\%s", pipe_name);
657         }
658         
659         if (!username || !username[0]) {
660                 status = smbcli_full_connection(NULL, &cli, lp_netbios_name(),
661                                              binding->host, NULL, 
662                                              "ipc$", "?????", 
663                                              "", "", NULL, 0, &retry);
664         } else {
665                 status = smbcli_full_connection(NULL, &cli, lp_netbios_name(),
666                                              binding->host, NULL, 
667                                              "ipc$", "?????", 
668                                              username, domain,
669                                              password, 0, &retry);
670         }
671         if (!NT_STATUS_IS_OK(status)) {
672                 DEBUG(0,("Failed to connect to %s - %s\n", binding->host, nt_errstr(status)));
673                 talloc_destroy(mem_ctx);
674                 return status;
675         }
676
677         status = dcerpc_pipe_open_smb(p, cli->tree, pipe_name);
678         if (!NT_STATUS_IS_OK(status)) {
679                 DEBUG(0,("Failed to open pipe %s - %s\n", pipe_name, nt_errstr(status)));
680                 smbcli_tdis(cli);
681                 smbcli_shutdown(cli);
682                 talloc_destroy(mem_ctx);
683         return status;
684     }   
685
686         talloc_destroy(mem_ctx);
687         
688         /* this ensures that the reference count is decremented so
689            a pipe close will really close the link */
690         talloc_steal(*p, cli);
691
692         (*p)->flags = binding->flags;
693
694         /* remember the binding string for possible secondary connections */
695         (*p)->binding_string = dcerpc_binding_string((*p), binding);
696
697         if (username && username[0] && (binding->flags & DCERPC_SCHANNEL_ANY)) {
698                 status = dcerpc_bind_auth_schannel(*p, pipe_uuid, pipe_version, 
699                                                    domain, username, password);
700         } else if (username && username[0] &&
701                    (binding->flags & (DCERPC_CONNECT|DCERPC_SIGN|DCERPC_SEAL))) {
702                 status = dcerpc_bind_auth_ntlm(*p, pipe_uuid, pipe_version, domain, username, password);
703         } else {    
704                 status = dcerpc_bind_auth_none(*p, pipe_uuid, pipe_version);
705
706         }
707
708         if (!NT_STATUS_IS_OK(status)) {
709                 DEBUG(0,("Failed to bind to uuid %s - %s\n", pipe_uuid, nt_errstr(status)));
710                 dcerpc_pipe_close(*p);
711                 *p = NULL;
712                 return status;
713         }
714
715         return NT_STATUS_OK;
716 }
717
718
719 /* open a rpc connection to a rpc pipe on SMP using the binding
720    structure to determine the endpoint and options */
721 static NTSTATUS dcerpc_pipe_connect_ncacn_ip_tcp(struct dcerpc_pipe **p, 
722                                                  struct dcerpc_binding *binding,
723                                                  const char *pipe_uuid, 
724                                                  uint32_t pipe_version,
725                                                  const char *domain,
726                                                  const char *username,
727                                                  const char *password)
728 {
729         NTSTATUS status;
730         uint32_t port = 0;
731
732         if (binding->options && binding->options[0] && strlen(binding->options[0])) {
733                 port = atoi(binding->options[0]);
734         }
735
736         if (port == 0) {
737                 status = dcerpc_epm_map_tcp_port(binding->host, 
738                                                  pipe_uuid, pipe_version,
739                                                  &port);
740                 if (!NT_STATUS_IS_OK(status)) {
741                         DEBUG(0,("Failed to map DCERPC/TCP port for '%s' - %s\n", 
742                                  pipe_uuid, nt_errstr(status)));
743                         return status;
744                 }
745                 DEBUG(1,("Mapped to DCERPC/TCP port %u\n", port));
746         }
747
748         status = dcerpc_pipe_open_tcp(p, binding->host, port, AF_UNSPEC);
749         if (!NT_STATUS_IS_OK(status)) {
750                 DEBUG(0,("Failed to connect to %s:%d\n", binding->host, port));
751                 return status;
752         }
753
754         (*p)->flags = binding->flags;
755
756         /* remember the binding string for possible secondary connections */
757         (*p)->binding_string = dcerpc_binding_string((*p), binding);
758
759         if (username && username[0] && (binding->flags & DCERPC_SCHANNEL_ANY)) {
760                 status = dcerpc_bind_auth_schannel(*p, pipe_uuid, pipe_version, 
761                                                    domain, username, password);
762         } else if (username && username[0]) {
763                 status = dcerpc_bind_auth_ntlm(*p, pipe_uuid, pipe_version, domain, username, password);
764         } else {    
765                 status = dcerpc_bind_auth_none(*p, pipe_uuid, pipe_version);
766         }
767
768         if (!NT_STATUS_IS_OK(status)) {
769                 DEBUG(0,("Failed to bind to uuid %s - %s\n", 
770                          pipe_uuid, nt_errstr(status)));
771                 dcerpc_pipe_close(*p);
772                 *p = NULL;
773                 return status;
774         }
775  
776         return status;
777 }
778
779
780 /* open a rpc connection to a rpc pipe, using the specified 
781    binding structure to determine the endpoint and options */
782 NTSTATUS dcerpc_pipe_connect_b(struct dcerpc_pipe **p, 
783                                struct dcerpc_binding *binding,
784                                const char *pipe_uuid, 
785                                uint32_t pipe_version,
786                                const char *domain,
787                                const char *username,
788                                const char *password)
789 {
790         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
791
792         switch (binding->transport) {
793         case NCACN_NP:
794                 status = dcerpc_pipe_connect_ncacn_np(p, binding, pipe_uuid, pipe_version,
795                                                       domain, username, password);
796                 break;
797         case NCACN_IP_TCP:
798                 status = dcerpc_pipe_connect_ncacn_ip_tcp(p, binding, pipe_uuid, pipe_version,
799                                                           domain, username, password);
800                 break;
801         default:
802                 return NT_STATUS_NOT_SUPPORTED;
803         }
804
805         return status;
806 }
807
808
809 /* open a rpc connection to a rpc pipe, using the specified string
810    binding to determine the endpoint and options */
811 NTSTATUS dcerpc_pipe_connect(struct dcerpc_pipe **p, 
812                              const char *binding,
813                              const char *pipe_uuid, 
814                              uint32_t pipe_version,
815                              const char *domain,
816                              const char *username,
817                              const char *password)
818 {
819         struct dcerpc_binding b;
820         NTSTATUS status;
821         TALLOC_CTX *mem_ctx;
822
823         mem_ctx = talloc_init("dcerpc_pipe_connect");
824         if (!mem_ctx) return NT_STATUS_NO_MEMORY;
825
826         status = dcerpc_parse_binding(mem_ctx, binding, &b);
827         if (!NT_STATUS_IS_OK(status)) {
828                 DEBUG(0,("Failed to parse dcerpc binding '%s'\n", binding));
829                 talloc_destroy(mem_ctx);
830                 return status;
831         }
832
833         DEBUG(3,("Using binding %s\n", dcerpc_binding_string(mem_ctx, &b)));
834
835         status = dcerpc_pipe_connect_b(p, &b, pipe_uuid, pipe_version, domain, username, password);
836
837         talloc_destroy(mem_ctx);
838         return status;
839 }
840
841
842 /*
843   create a secondary dcerpc connection from a primary connection
844
845   if the primary is a SMB connection then the secondary connection
846   will be on the same SMB connection, but use a new fnum
847 */
848 NTSTATUS dcerpc_secondary_connection(struct dcerpc_pipe *p, struct dcerpc_pipe **p2,
849                                      const char *pipe_name,
850                                      const char *pipe_uuid,
851                                      uint32_t pipe_version)
852 {
853         struct smbcli_tree *tree;
854         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
855         struct dcerpc_binding b;
856
857         switch (p->transport.transport) {
858         case NCACN_NP:
859                 tree = dcerpc_smb_tree(p);
860                 if (!tree) {
861                         return NT_STATUS_INVALID_PARAMETER;
862                 }
863
864                 status = dcerpc_pipe_open_smb(p2, tree, pipe_name);
865                 break;
866
867         case NCACN_IP_TCP:
868                 status = dcerpc_parse_binding(p, p->binding_string, &b);
869                 if (!NT_STATUS_IS_OK(status)) {
870                         return status;
871                 }
872                 b.flags &= ~DCERPC_AUTH_OPTIONS;
873                 status = dcerpc_pipe_connect_ncacn_ip_tcp(p2, &b, pipe_uuid,
874                                                           pipe_version, NULL, 
875                                                           NULL, NULL);
876                 break;
877         default:
878                 return NT_STATUS_NOT_SUPPORTED;
879         }
880
881         if (!NT_STATUS_IS_OK(status)) {
882                 return status;
883         }
884
885         (*p2)->flags = p->flags;
886
887         status = dcerpc_bind_auth_none(*p2, pipe_uuid, pipe_version);
888         if (!NT_STATUS_IS_OK(status)) {
889                 return status;
890         }
891
892         return NT_STATUS_OK;
893 }
894
895 NTSTATUS dcerpc_generic_session_key(struct dcerpc_pipe *p,
896                                     DATA_BLOB *session_key)
897 {
898         /* this took quite a few CPU cycles to find ... */
899         session_key->data = discard_const_p(unsigned char, "SystemLibraryDTC");
900         session_key->length = 16;
901         return NT_STATUS_OK;
902 }
903
904 /*
905   fetch the user session key - may be default (above) or the SMB session key
906 */
907 NTSTATUS dcerpc_fetch_session_key(struct dcerpc_pipe *p,
908                                   DATA_BLOB *session_key)
909 {
910         return p->security_state.session_key(p, session_key);
911 }
912
913
914 /*
915   log a rpc packet in a format suitable for ndrdump. This is especially useful
916   for sealed packets, where ethereal cannot easily see the contents
917
918   this triggers on a debug level of >= 10
919 */
920 void dcerpc_log_packet(const struct dcerpc_interface_table *ndr,
921                        uint32_t opnum, uint32_t flags, DATA_BLOB *pkt)
922 {
923         const int num_examples = 20;
924         int i;
925
926         if (DEBUGLEVEL < 10) return;
927
928         for (i=0;i<num_examples;i++) {
929                 char *name=NULL;
930                 asprintf(&name, "%s/rpclog/%s-%u.%d.%s", 
931                          lp_lockdir(), ndr->name, opnum, i,
932                          (flags&NDR_IN)?"in":"out");
933                 if (name == NULL) {
934                         return;
935                 }
936                 if (!file_exist(name, NULL)) {
937                         if (file_save(name, pkt->data, pkt->length)) {
938                                 DEBUG(10,("Logged rpc packet to %s\n", name));
939                         }
940                         free(name);
941                         break;
942                 }
943                 free(name);
944         }
945 }
946