r7313: Prefix a few functions with ncacn_ rather then dcerpc_ because they are
[kai/samba.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    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26 #include "system/network.h"
27 #include "librpc/gen_ndr/ndr_epmapper.h"
28
29 /*
30   find the pipe name for a local IDL interface
31 */
32 const char *idl_pipe_name(const char *uuid, uint32_t if_version)
33 {
34         const struct dcerpc_interface_list *l;
35         for (l=librpc_dcerpc_pipes();l;l=l->next) {
36                 if (strcasecmp(l->table->uuid, uuid) == 0 &&
37                     l->table->if_version == if_version) {
38                         return l->table->name;
39                 }
40         }
41         return "UNKNOWN";
42 }
43
44 /*
45   find the number of calls defined by local IDL
46 */
47 int idl_num_calls(const char *uuid, uint32_t if_version)
48 {
49         const struct dcerpc_interface_list *l;
50         for (l=librpc_dcerpc_pipes();l;l=l->next){
51                 if (strcasecmp(l->table->uuid, uuid) == 0 &&
52                     l->table->if_version == if_version) {
53                         return l->table->num_calls;
54                 }
55         }
56         return -1;
57 }
58
59
60 /*
61   find a dcerpc interface by name
62 */
63 const struct dcerpc_interface_table *idl_iface_by_name(const char *name)
64 {
65         const struct dcerpc_interface_list *l;
66         for (l=librpc_dcerpc_pipes();l;l=l->next) {
67                 if (strcasecmp(l->table->name, name) == 0) {
68                         return l->table;
69                 }
70         }
71         return NULL;
72 }
73
74 /*
75   find a dcerpc interface by uuid
76 */
77 const struct dcerpc_interface_table *idl_iface_by_uuid(const char *uuid)
78 {
79         const struct dcerpc_interface_list *l;
80         for (l=librpc_dcerpc_pipes();l;l=l->next) {
81                 if (strcasecmp(l->table->uuid, uuid) == 0) {
82                         return l->table;
83                 }
84         }
85         return NULL;
86 }
87
88
89 /* 
90    push a ncacn_packet into a blob, potentially with auth info
91 */
92 NTSTATUS ncacn_push_auth(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, 
93                           struct ncacn_packet *pkt,
94                           struct dcerpc_auth *auth_info)
95 {
96         NTSTATUS status;
97         struct ndr_push *ndr;
98
99         ndr = ndr_push_init_ctx(mem_ctx);
100         if (!ndr) {
101                 return NT_STATUS_NO_MEMORY;
102         }
103
104         if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
105                 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
106         }
107
108         if (pkt->pfc_flags & DCERPC_PFC_FLAG_ORPC) {
109                 ndr->flags |= LIBNDR_FLAG_OBJECT_PRESENT;
110         }
111
112         if (auth_info) {
113                 pkt->auth_length = auth_info->credentials.length;
114         } else {
115                 pkt->auth_length = 0;
116         }
117
118         status = ndr_push_ncacn_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
119         if (!NT_STATUS_IS_OK(status)) {
120                 return status;
121         }
122
123         if (auth_info) {
124                 status = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, auth_info);
125         }
126
127         *blob = ndr_push_blob(ndr);
128
129         /* fill in the frag length */
130         dcerpc_set_frag_length(blob, blob->length);
131
132         return NT_STATUS_OK;
133 }
134
135 #define MAX_PROTSEQ             10
136
137 static const struct {
138         const char *name;
139         enum dcerpc_transport_t transport;
140         int num_protocols;
141         enum epm_protocol protseq[MAX_PROTSEQ];
142 } transports[] = {
143         { "ncacn_np",     NCACN_NP, 3, 
144                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SMB, EPM_PROTOCOL_NETBIOS }},
145         { "ncacn_ip_tcp", NCACN_IP_TCP, 3, 
146                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_TCP, EPM_PROTOCOL_IP } }, 
147         { "ncacn_http", NCACN_HTTP, 3, 
148                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_HTTP, EPM_PROTOCOL_IP } }, 
149         { "ncadg_ip_udp", NCACN_IP_UDP, 3, 
150                 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UDP, EPM_PROTOCOL_IP } },
151         { "ncalrpc", NCALRPC, 2, 
152                 { EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_PIPE } },
153         { "ncacn_unix_stream", NCACN_UNIX_STREAM, 2, 
154                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_UNIX_DS } },
155         { "ncadg_unix_dgram", NCADG_UNIX_DGRAM, 2, 
156                 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UNIX_DS } },
157         { "ncacn_at_dsp", NCACN_AT_DSP, 3, 
158                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DSP } },
159         { "ncadg_at_ddp", NCADG_AT_DDP, 3, 
160                 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DDP } },
161         { "ncacn_vns_ssp", NCACN_VNS_SPP, 3, 
162                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_SPP } },
163         { "ncacn_vns_ipc", NCACN_VNS_IPC, 3, 
164                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_IPC }, },
165         { "ncadg_ipx", NCADG_IPX, 2,
166                 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_IPX },
167         },
168         { "ncacn_spx", NCACN_SPX, 3,
169                 /* I guess some MS programmer confused the identifier for 
170                  * EPM_PROTOCOL_UUID (0x0D or 13) with the one for 
171                  * EPM_PROTOCOL_SPX (0x13) here. -- jelmer*/
172                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_UUID },
173         },
174 };
175
176 static const struct {
177         const char *name;
178         uint32_t flag;
179 } ncacn_options[] = {
180         {"sign", DCERPC_SIGN},
181         {"seal", DCERPC_SEAL},
182         {"connect", DCERPC_CONNECT},
183         {"spnego", DCERPC_AUTH_SPNEGO},
184         {"krb5", DCERPC_AUTH_KRB5},
185         {"validate", DCERPC_DEBUG_VALIDATE_BOTH},
186         {"print", DCERPC_DEBUG_PRINT_BOTH},
187         {"padcheck", DCERPC_DEBUG_PAD_CHECK},
188         {"bigendian", DCERPC_PUSH_BIGENDIAN}
189 };
190
191 const char *epm_floor_string(TALLOC_CTX *mem_ctx, struct epm_floor *fl)
192 {
193         struct GUID uuid;
194         uint16_t if_version;
195         NTSTATUS status;
196
197         switch(fl->lhs.protocol) {
198                 case EPM_PROTOCOL_UUID:
199                         status = dcerpc_floor_get_lhs_data(fl, &uuid, &if_version);
200                         if (NT_STATUS_IS_OK(status)) {
201                                 /* lhs is used: UUID */
202                                 char *uuidstr;
203
204                                 uuidstr = GUID_string(mem_ctx, &uuid);
205
206                                 if (strcasecmp(uuidstr, NDR_GUID) == 0) {
207                                         return "NDR";
208                                 } 
209
210                                 return talloc_asprintf(mem_ctx, " uuid %s/0x%02x", uuidstr, if_version);
211                         } else { /* IPX */
212                                 return talloc_asprintf(mem_ctx, "IPX:%s", 
213                                                 data_blob_hex_string(mem_ctx, &fl->rhs.uuid.unknown));
214                         }
215
216                 case EPM_PROTOCOL_NCACN:
217                         return "RPC-C";
218
219                 case EPM_PROTOCOL_NCADG:
220                         return "RPC";
221
222                 case EPM_PROTOCOL_NCALRPC:
223                         return "NCALRPC";
224
225                 case EPM_PROTOCOL_DNET_NSP:
226                         return "DNET/NSP";
227
228                 case EPM_PROTOCOL_IP:
229                         return talloc_asprintf(mem_ctx, "IP:%s", fl->rhs.ip.ipaddr);
230
231                 case EPM_PROTOCOL_PIPE:
232                         return talloc_asprintf(mem_ctx, "PIPE:%s", fl->rhs.pipe.path);
233
234                 case EPM_PROTOCOL_SMB:
235                         return talloc_asprintf(mem_ctx, "SMB:%s", fl->rhs.smb.unc);
236
237                 case EPM_PROTOCOL_UNIX_DS:
238                         return talloc_asprintf(mem_ctx, "Unix:%s", fl->rhs.unix_ds.path);
239
240                 case EPM_PROTOCOL_NETBIOS:
241                         return talloc_asprintf(mem_ctx, "NetBIOS:%s", fl->rhs.netbios.name);
242
243                 case EPM_PROTOCOL_NETBEUI:
244                         return "NETBeui";
245
246                 case EPM_PROTOCOL_SPX:
247                         return "SPX";
248
249                 case EPM_PROTOCOL_NB_IPX:
250                         return "NB_IPX";
251
252                 case EPM_PROTOCOL_HTTP:
253                         return talloc_asprintf(mem_ctx, "HTTP:%d", fl->rhs.http.port);
254
255                 case EPM_PROTOCOL_TCP:
256                         return talloc_asprintf(mem_ctx, "TCP:%d", fl->rhs.tcp.port);
257
258                 case EPM_PROTOCOL_UDP:
259                         return talloc_asprintf(mem_ctx, "UDP:%d", fl->rhs.udp.port);
260
261                 default:
262                         return talloc_asprintf(mem_ctx, "UNK(%02x):", fl->lhs.protocol);
263         }
264 }
265
266
267 /*
268   form a binding string from a binding structure
269 */
270 const char *dcerpc_binding_string(TALLOC_CTX *mem_ctx, const struct dcerpc_binding *b)
271 {
272         char *s = talloc_strdup(mem_ctx, "");
273         int i;
274         const char *t_name=NULL;
275
276         for (i=0;i<ARRAY_SIZE(transports);i++) {
277                 if (transports[i].transport == b->transport) {
278                         t_name = transports[i].name;
279                 }
280         }
281         if (!t_name) {
282                 return NULL;
283         }
284
285         if (!GUID_all_zero(&b->object)) { 
286                 s = talloc_asprintf(s, "%s@",
287                                     GUID_string(mem_ctx, &b->object));
288         }
289
290         s = talloc_asprintf_append(s, "%s:", t_name);
291         if (!s) return NULL;
292
293         if (b->host) {
294                 s = talloc_asprintf_append(s, "%s", b->host);
295         }
296
297         if (!b->endpoint && !b->options && !b->flags) {
298                 return s;
299         }
300
301         s = talloc_asprintf_append(s, "[");
302
303         if (b->endpoint) {
304                 s = talloc_asprintf_append(s, "%s", b->endpoint);
305         }
306
307         /* this is a *really* inefficent way of dealing with strings,
308            but this is rarely called and the strings are always short,
309            so I don't care */
310         for (i=0;b->options && b->options[i];i++) {
311                 s = talloc_asprintf_append(s, ",%s", b->options[i]);
312                 if (!s) return NULL;
313         }
314
315         for (i=0;i<ARRAY_SIZE(ncacn_options);i++) {
316                 if (b->flags & ncacn_options[i].flag) {
317                         s = talloc_asprintf_append(s, ",%s", ncacn_options[i].name);
318                         if (!s) return NULL;
319                 }
320         }
321
322         s = talloc_asprintf_append(s, "]");
323
324         return s;
325 }
326
327 /*
328   parse a binding string into a dcerpc_binding structure
329 */
330 NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *s, struct dcerpc_binding **b_out)
331 {
332         struct dcerpc_binding *b;
333         char *options, *type;
334         char *p;
335         int i, j, comma_count;
336
337         b = talloc(mem_ctx, struct dcerpc_binding);
338         if (!b) {
339                 return NT_STATUS_NO_MEMORY;
340         }
341
342         b->authservice = NULL;
343
344         p = strchr(s, '@');
345
346         if (p && PTR_DIFF(p, s) == 36) { /* 36 is the length of a UUID */
347                 NTSTATUS status;
348
349                 status = GUID_from_string(s, &b->object);
350
351                 if (NT_STATUS_IS_ERR(status)) {
352                         DEBUG(0, ("Failed parsing UUID\n"));
353                         return status;
354                 }
355
356                 s = p + 1;
357         } else {
358                 ZERO_STRUCT(b->object);
359         }
360
361         b->object_version = 0;
362
363         p = strchr(s, ':');
364         if (!p) {
365                 return NT_STATUS_INVALID_PARAMETER;
366         }
367
368         type = talloc_strndup(mem_ctx, s, PTR_DIFF(p, s));
369         if (!type) {
370                 return NT_STATUS_NO_MEMORY;
371         }
372
373         for (i=0;i<ARRAY_SIZE(transports);i++) {
374                 if (strcasecmp(type, transports[i].name) == 0) {
375                         b->transport = transports[i].transport;
376                         break;
377                 }
378         }
379         if (i==ARRAY_SIZE(transports)) {
380                 DEBUG(0,("Unknown dcerpc transport '%s'\n", type));
381                 return NT_STATUS_INVALID_PARAMETER;
382         }
383         
384         s = p+1;
385
386         p = strchr(s, '[');
387         if (p) {
388                 b->host = talloc_strndup(b, s, PTR_DIFF(p, s));
389                 options = talloc_strdup(mem_ctx, p+1);
390                 if (options[strlen(options)-1] != ']') {
391                         return NT_STATUS_INVALID_PARAMETER;
392                 }
393                 options[strlen(options)-1] = 0;
394         } else {
395                 b->host = talloc_strdup(b, s);
396                 options = NULL;
397         }
398
399         if (!b->host) {
400                 return NT_STATUS_NO_MEMORY;
401         }
402
403         b->options = NULL;
404         b->flags = 0;
405         b->endpoint = NULL;
406
407         if (!options) {
408                 *b_out = b;
409                 return NT_STATUS_OK;
410         }
411
412         comma_count = count_chars(options, ',');
413
414         b->options = talloc_array(b, const char *, comma_count+2);
415         if (!b->options) {
416                 return NT_STATUS_NO_MEMORY;
417         }
418
419         for (i=0; (p = strchr(options, ',')); i++) {
420                 b->options[i] = talloc_strndup(b, options, PTR_DIFF(p, options));
421                 if (!b->options[i]) {
422                         return NT_STATUS_NO_MEMORY;
423                 }
424                 options = p+1;
425         }
426         b->options[i] = options;
427         b->options[i+1] = NULL;
428
429         /* some options are pre-parsed for convenience */
430         for (i=0;b->options[i];i++) {
431                 for (j=0;j<ARRAY_SIZE(ncacn_options);j++) {
432                         if (strcasecmp(ncacn_options[j].name, b->options[i]) == 0) {
433                                 int k;
434                                 b->flags |= ncacn_options[j].flag;
435                                 for (k=i;b->options[k];k++) {
436                                         b->options[k] = b->options[k+1];
437                                 }
438                                 i--;
439                                 break;
440                         }
441                 }
442         }
443
444         if (b->options[0]) {
445                 /* Endpoint is first option */
446                 b->endpoint = b->options[0];
447                 if (strlen(b->endpoint) == 0) b->endpoint = NULL;
448
449                 for (i=0;b->options[i];i++) {
450                         b->options[i] = b->options[i+1];
451                 }
452         }
453
454         if (b->options[0] == NULL)
455                 b->options = NULL;
456         
457         *b_out = b;
458         return NT_STATUS_OK;
459 }
460
461 NTSTATUS dcerpc_floor_get_lhs_data(struct epm_floor *floor, struct GUID *uuid, uint16_t *if_version)
462 {
463         TALLOC_CTX *mem_ctx = talloc_init("floor_get_lhs_data");
464         struct ndr_pull *ndr = ndr_pull_init_blob(&floor->lhs.lhs_data, mem_ctx);
465         NTSTATUS status;
466         
467         ndr->flags |= LIBNDR_FLAG_NOALIGN;
468
469         status = ndr_pull_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, uuid);
470         if (NT_STATUS_IS_ERR(status)) {
471                 talloc_free(mem_ctx);
472                 return status;
473         }
474
475         status = ndr_pull_uint16(ndr, NDR_SCALARS, if_version);
476
477         talloc_free(mem_ctx);
478
479         return status;
480 }
481
482 static DATA_BLOB dcerpc_floor_pack_lhs_data(TALLOC_CTX *mem_ctx, struct GUID *uuid, uint32_t if_version)
483 {
484         struct ndr_push *ndr = ndr_push_init_ctx(mem_ctx);
485
486         ndr->flags |= LIBNDR_FLAG_NOALIGN;
487
488         ndr_push_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, uuid);
489         ndr_push_uint16(ndr, NDR_SCALARS, if_version);
490
491         return ndr_push_blob(ndr);
492 }
493
494 const char *dcerpc_floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *floor)
495 {
496         switch (floor->lhs.protocol) {
497         case EPM_PROTOCOL_TCP:
498                 if (floor->rhs.tcp.port == 0) return NULL;
499                 return talloc_asprintf(mem_ctx, "%d", floor->rhs.tcp.port);
500                 
501         case EPM_PROTOCOL_UDP:
502                 if (floor->rhs.udp.port == 0) return NULL;
503                 return talloc_asprintf(mem_ctx, "%d", floor->rhs.udp.port);
504
505         case EPM_PROTOCOL_HTTP:
506                 if (floor->rhs.http.port == 0) return NULL;
507                 return talloc_asprintf(mem_ctx, "%d", floor->rhs.http.port);
508
509         case EPM_PROTOCOL_IP:
510                 return talloc_strdup(mem_ctx, floor->rhs.ip.ipaddr);
511
512         case EPM_PROTOCOL_NCACN:
513                 return NULL;
514
515         case EPM_PROTOCOL_NCADG:
516                 return NULL;
517
518         case EPM_PROTOCOL_SMB:
519                 if (strlen(floor->rhs.smb.unc) == 0) return NULL;
520                 return talloc_strdup(mem_ctx, floor->rhs.smb.unc);
521
522         case EPM_PROTOCOL_PIPE:
523                 if (strlen(floor->rhs.pipe.path) == 0) return NULL;
524                 return talloc_strdup(mem_ctx, floor->rhs.pipe.path);
525
526         case EPM_PROTOCOL_NETBIOS:
527                 if (strlen(floor->rhs.netbios.name) == 0) return NULL;
528                 return talloc_strdup(mem_ctx, floor->rhs.netbios.name);
529
530         case EPM_PROTOCOL_NCALRPC:
531                 return NULL;
532                 
533         case EPM_PROTOCOL_VINES_SPP:
534                 return talloc_asprintf(mem_ctx, "%d", floor->rhs.vines_spp.port);
535                 
536         case EPM_PROTOCOL_VINES_IPC:
537                 return talloc_asprintf(mem_ctx, "%d", floor->rhs.vines_ipc.port);
538                 
539         case EPM_PROTOCOL_STREETTALK:
540                 return talloc_strdup(mem_ctx, floor->rhs.streettalk.streettalk);
541                 
542         case EPM_PROTOCOL_UNIX_DS:
543                 if (strlen(floor->rhs.unix_ds.path) == 0) return NULL;
544                 return talloc_strdup(mem_ctx, floor->rhs.unix_ds.path);
545                 
546         case EPM_PROTOCOL_NULL:
547                 return NULL;
548
549         default:
550                 DEBUG(0,("Unsupported lhs protocol %d\n", floor->lhs.protocol));
551                 break;
552         }
553
554         return NULL;
555 }
556
557 static NTSTATUS dcerpc_floor_set_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *floor,  const char *data)
558 {
559         switch (floor->lhs.protocol) {
560         case EPM_PROTOCOL_TCP:
561                 floor->rhs.tcp.port = atoi(data);
562                 return NT_STATUS_OK;
563                 
564         case EPM_PROTOCOL_UDP:
565                 floor->rhs.udp.port = atoi(data);
566                 return NT_STATUS_OK;
567
568         case EPM_PROTOCOL_HTTP:
569                 floor->rhs.http.port = atoi(data);
570                 return NT_STATUS_OK;
571
572         case EPM_PROTOCOL_IP:
573                 floor->rhs.ip.ipaddr = talloc_strdup(mem_ctx, data);
574                 NT_STATUS_HAVE_NO_MEMORY(floor->rhs.ip.ipaddr);
575                 return NT_STATUS_OK;
576
577         case EPM_PROTOCOL_NCACN:
578                 floor->rhs.ncacn.minor_version = 0;
579                 return NT_STATUS_OK;
580
581         case EPM_PROTOCOL_NCADG:
582                 floor->rhs.ncadg.minor_version = 0;
583                 return NT_STATUS_OK;
584
585         case EPM_PROTOCOL_SMB:
586                 floor->rhs.smb.unc = talloc_strdup(mem_ctx, data);
587                 NT_STATUS_HAVE_NO_MEMORY(floor->rhs.smb.unc);
588                 return NT_STATUS_OK;
589
590         case EPM_PROTOCOL_PIPE:
591                 floor->rhs.pipe.path = talloc_strdup(mem_ctx, data);
592                 NT_STATUS_HAVE_NO_MEMORY(floor->rhs.pipe.path);
593                 return NT_STATUS_OK;
594
595         case EPM_PROTOCOL_NETBIOS:
596                 floor->rhs.netbios.name = talloc_strdup(mem_ctx, data);
597                 NT_STATUS_HAVE_NO_MEMORY(floor->rhs.netbios.name);
598                 return NT_STATUS_OK;
599
600         case EPM_PROTOCOL_NCALRPC:
601                 return NT_STATUS_OK;
602                 
603         case EPM_PROTOCOL_VINES_SPP:
604                 floor->rhs.vines_spp.port = atoi(data);
605                 return NT_STATUS_OK;
606                 
607         case EPM_PROTOCOL_VINES_IPC:
608                 floor->rhs.vines_ipc.port = atoi(data);
609                 return NT_STATUS_OK;
610                 
611         case EPM_PROTOCOL_STREETTALK:
612                 floor->rhs.streettalk.streettalk = talloc_strdup(mem_ctx, data);
613                 NT_STATUS_HAVE_NO_MEMORY(floor->rhs.streettalk.streettalk);
614                 return NT_STATUS_OK;
615                 
616         case EPM_PROTOCOL_UNIX_DS:
617                 floor->rhs.unix_ds.path = talloc_strdup(mem_ctx, data);
618                 NT_STATUS_HAVE_NO_MEMORY(floor->rhs.unix_ds.path);
619                 return NT_STATUS_OK;
620                 
621         case EPM_PROTOCOL_NULL:
622                 return NT_STATUS_OK;
623
624         default:
625                 DEBUG(0,("Unsupported lhs protocol %d\n", floor->lhs.protocol));
626                 break;
627         }
628
629         return NT_STATUS_NOT_SUPPORTED;
630 }
631
632 enum dcerpc_transport_t dcerpc_transport_by_endpoint_protocol(int prot)
633 {
634         int i;
635
636         /* Find a transport that has 'prot' as 4th protocol */
637         for (i=0;i<ARRAY_SIZE(transports);i++) {
638                 if (transports[i].num_protocols >= 2 && 
639                         transports[i].protseq[1] == prot) {
640                         return transports[i].transport;
641                 }
642         }
643         
644         /* Unknown transport */
645         return -1;
646 }
647
648 enum dcerpc_transport_t dcerpc_transport_by_tower(struct epm_tower *tower)
649 {
650         int i;
651
652         /* Find a transport that matches this tower */
653         for (i=0;i<ARRAY_SIZE(transports);i++) {
654                 int j;
655                 if (transports[i].num_protocols != tower->num_floors - 2) {
656                         continue; 
657                 }
658
659                 for (j = 0; j < transports[i].num_protocols; j++) {
660                         if (transports[i].protseq[j] != tower->floors[j+2].lhs.protocol) {
661                                 break;
662                         }
663                 }
664
665                 if (j == transports[i].num_protocols) {
666                         return transports[i].transport;
667                 }
668         }
669         
670         /* Unknown transport */
671         return -1;
672 }
673
674 NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx, struct epm_tower *tower, struct dcerpc_binding **b_out)
675 {
676         NTSTATUS status;
677         struct dcerpc_binding *binding;
678
679         binding = talloc(mem_ctx, struct dcerpc_binding);
680         NT_STATUS_HAVE_NO_MEMORY(binding);
681
682         ZERO_STRUCT(binding->object);
683         binding->options = NULL;
684         binding->host = NULL;
685         binding->flags = 0;
686
687         binding->transport = dcerpc_transport_by_tower(tower);
688
689         if (binding->transport == -1) {
690                 return NT_STATUS_NOT_SUPPORTED;
691         }
692
693         if (tower->num_floors < 1) {
694                 return NT_STATUS_OK;
695         }
696
697         /* Set object uuid */
698         status = dcerpc_floor_get_lhs_data(&tower->floors[0], &binding->object, &binding->object_version);
699         
700         if (!NT_STATUS_IS_OK(status)) {
701                 DEBUG(1, ("Error pulling object uuid and version: %s", nt_errstr(status)));     
702                 return status;
703         }
704
705         /* Ignore floor 1, it contains the NDR version info */
706         
707         binding->options = NULL;
708
709         /* Set endpoint */
710         if (tower->num_floors >= 4) {
711                 binding->endpoint = dcerpc_floor_get_rhs_data(mem_ctx, &tower->floors[3]);
712         } else {
713                 binding->endpoint = NULL;
714         }
715
716         /* Set network address */
717         if (tower->num_floors >= 5) {
718                 binding->host = dcerpc_floor_get_rhs_data(mem_ctx, &tower->floors[4]);
719         }
720         *b_out = binding;
721         return NT_STATUS_OK;
722 }
723
724 NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx, struct dcerpc_binding *binding, struct epm_tower *tower)
725 {
726         const enum epm_protocol *protseq = NULL;
727         int num_protocols = -1, i;
728         struct GUID ndr_guid;
729         NTSTATUS status;
730         
731         /* Find transport */
732         for (i=0;i<ARRAY_SIZE(transports);i++) {
733                 if (transports[i].transport == binding->transport) {
734                         protseq = transports[i].protseq;
735                         num_protocols = transports[i].num_protocols;
736                         break;
737                 }
738         }
739
740         if (num_protocols == -1) {
741                 DEBUG(0, ("Unable to find transport with id '%d'\n", binding->transport));
742                 return NT_STATUS_UNSUCCESSFUL;
743         }
744
745         tower->num_floors = 2 + num_protocols;
746         tower->floors = talloc_array(mem_ctx, struct epm_floor, tower->num_floors);
747
748         /* Floor 0 */
749         tower->floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
750
751         tower->floors[0].lhs.lhs_data = dcerpc_floor_pack_lhs_data(mem_ctx, &binding->object, binding->object_version);
752
753         tower->floors[0].rhs.uuid.unknown = data_blob_talloc_zero(mem_ctx, 2);
754         
755         /* Floor 1 */
756         tower->floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
757
758         status = GUID_from_string(NDR_GUID, &ndr_guid);
759         if (NT_STATUS_IS_ERR(status)) {
760                 return status;
761         }
762
763         tower->floors[1].lhs.lhs_data = dcerpc_floor_pack_lhs_data(mem_ctx, &ndr_guid, NDR_GUID_VERSION);
764         
765         tower->floors[1].rhs.uuid.unknown = data_blob_talloc_zero(mem_ctx, 2);
766         
767         /* Floor 2 to num_protocols */
768         for (i = 0; i < num_protocols; i++) {
769                 tower->floors[2 + i].lhs.protocol = protseq[i];
770                 tower->floors[2 + i].lhs.lhs_data = data_blob_talloc(mem_ctx, NULL, 0);
771                 ZERO_STRUCT(tower->floors[2 + i].rhs);
772                 dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[2 + i], "");
773         }
774
775         /* The 4th floor contains the endpoint */
776         if (num_protocols >= 2 && binding->endpoint) {
777                 status = dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[3], binding->endpoint);
778                 if (NT_STATUS_IS_ERR(status)) {
779                         return status;
780                 }
781         }
782         
783         /* The 5th contains the network address */
784         if (num_protocols >= 3 && binding->host) {
785                 status = dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[4], binding->host);
786                 if (NT_STATUS_IS_ERR(status)) {
787                         return status;
788                 }
789         }
790
791         return NT_STATUS_OK;
792 }
793
794 NTSTATUS dcerpc_epm_map_binding(TALLOC_CTX *mem_ctx, struct dcerpc_binding *binding,
795                                 const char *uuid, uint_t version)
796 {
797         struct dcerpc_pipe *p;
798         NTSTATUS status;
799         struct epm_Map r;
800         struct policy_handle handle;
801         struct GUID guid;
802         struct epm_twr_t twr, *twr_r;
803         struct dcerpc_binding *epmapper_binding;
804         const struct dcerpc_interface_table *table = idl_iface_by_uuid(uuid);
805         int i;
806
807         struct cli_credentials *anon_creds
808                 = cli_credentials_init(mem_ctx);
809         cli_credentials_set_conf(anon_creds);
810         cli_credentials_set_anonymous(anon_creds);
811
812         /* First, check if there is a default endpoint specified in the IDL */
813
814         if (table) {
815                 struct dcerpc_binding *default_binding;
816
817                 binding->authservice = talloc_strdup(binding, table->authservices->names[0]);
818
819                 /* Find one of the default pipes for this interface */
820                 for (i = 0; i < table->endpoints->count; i++) {
821                         status = dcerpc_parse_binding(mem_ctx, table->endpoints->names[i], &default_binding);
822
823                         if (NT_STATUS_IS_OK(status)) {
824                                 if (default_binding->transport == binding->transport && default_binding->endpoint) {
825                                         binding->endpoint = talloc_reference(binding, default_binding->endpoint);
826                                         talloc_free(default_binding);
827                                         return NT_STATUS_OK;
828                                 } else {
829                                         talloc_free(default_binding);
830                                 }
831                         }
832                 }
833         }
834
835         epmapper_binding = talloc_zero(mem_ctx, struct dcerpc_binding);
836         if (!epmapper_binding) {
837                 return NT_STATUS_NO_MEMORY;
838         }
839
840         epmapper_binding->transport = binding->transport;
841         epmapper_binding->host = talloc_reference(epmapper_binding, 
842                                                   binding->host);
843         epmapper_binding->options = NULL;
844         epmapper_binding->flags = 0;
845         epmapper_binding->endpoint = NULL;
846         epmapper_binding->authservice = NULL;
847         
848         status = dcerpc_pipe_connect_b(mem_ctx, 
849                                        &p,
850                                        epmapper_binding,
851                                        DCERPC_EPMAPPER_UUID,
852                                        DCERPC_EPMAPPER_VERSION,
853                                        anon_creds);
854
855         if (!NT_STATUS_IS_OK(status)) {
856                 return status;
857         }
858
859         ZERO_STRUCT(handle);
860         ZERO_STRUCT(guid);
861
862         status = GUID_from_string(uuid, &binding->object);
863         if (NT_STATUS_IS_ERR(status)) {
864                 return status;
865         }
866
867         binding->object_version = version;
868
869         status = dcerpc_binding_build_tower(p, binding, &twr.tower);
870         if (NT_STATUS_IS_ERR(status)) {
871                 return status;
872         }
873
874         /* with some nice pretty paper around it of course */
875         r.in.object = &guid;
876         r.in.map_tower = &twr;
877         r.in.entry_handle = &handle;
878         r.in.max_towers = 1;
879         r.out.entry_handle = &handle;
880
881         status = dcerpc_epm_Map(p, p, &r);
882         if (!NT_STATUS_IS_OK(status)) {
883                 talloc_free(p);
884                 return status;
885         }
886         if (r.out.result != 0 || r.out.num_towers != 1) {
887                 talloc_free(p);
888                 return NT_STATUS_PORT_UNREACHABLE;
889         }
890
891         twr_r = r.out.towers[0].twr;
892         if (!twr_r) {
893                 talloc_free(p);
894                 return NT_STATUS_PORT_UNREACHABLE;
895         }
896
897         if (twr_r->tower.num_floors != twr.tower.num_floors ||
898             twr_r->tower.floors[3].lhs.protocol != twr.tower.floors[3].lhs.protocol) {
899                 talloc_free(p);
900                 return NT_STATUS_PORT_UNREACHABLE;
901         }
902
903         binding->endpoint = talloc_reference(binding, dcerpc_floor_get_rhs_data(mem_ctx, &twr_r->tower.floors[3]));
904
905         talloc_free(p);
906
907         return NT_STATUS_OK;
908 }
909
910
911 /* 
912    perform an authenticated bind if needed
913 */
914 NTSTATUS dcerpc_pipe_auth(struct dcerpc_pipe *p, 
915                           struct dcerpc_binding *binding,
916                           const char *pipe_uuid, 
917                           uint32_t pipe_version,
918                           struct cli_credentials *credentials)
919 {
920         NTSTATUS status;
921         TALLOC_CTX *tmp_ctx;
922         tmp_ctx = talloc_new(p);
923
924         p->conn->flags = binding->flags;
925
926         /* remember the binding string for possible secondary connections */
927         p->conn->binding_string = dcerpc_binding_string(p, binding);
928
929         if (!cli_credentials_is_anonymous(credentials) &&
930                 (binding->flags & DCERPC_SCHANNEL) && 
931                 !cli_credentials_get_netlogon_creds(credentials)) {
932                 
933                 /* If we don't already have netlogon credentials for
934                  * the schannel bind, then we have to get these
935                  * first */
936                 status = dcerpc_bind_auth_schannel(tmp_ctx, 
937                                                    p, pipe_uuid, pipe_version, 
938                                                    credentials);
939         } else if (!cli_credentials_is_anonymous(credentials) &&
940                 !(binding->transport == NCACN_NP &&
941                   !(binding->flags & DCERPC_SIGN) &&
942                   !(binding->flags & DCERPC_SEAL))) {   
943         
944                 /* Perform an authenticated DCE-RPC bind, except where
945                  * we ask for a connection on NCACN_NP, and that
946                  * connection is not signed or sealed.  For that case
947                  * we rely on the already authenicated CIFS connection
948                  */
949
950                 uint8_t auth_type;
951                 if (binding->flags & DCERPC_AUTH_SPNEGO) {
952                         auth_type = DCERPC_AUTH_TYPE_SPNEGO;
953                 } else if (binding->flags & DCERPC_AUTH_KRB5) {
954                         auth_type = DCERPC_AUTH_TYPE_KRB5;
955                 } else if (binding->flags & DCERPC_SCHANNEL) {
956                         auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
957                 } else {
958                         auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
959                 }
960
961                 status = dcerpc_bind_auth_password(p, pipe_uuid, pipe_version, 
962                                                    credentials, auth_type,
963                                                    binding->authservice);
964         } else {
965                 status = dcerpc_bind_auth_none(p, pipe_uuid, pipe_version);
966         }
967
968         if (!NT_STATUS_IS_OK(status)) {
969                 DEBUG(0,("Failed to bind to uuid %s - %s\n", pipe_uuid, nt_errstr(status)));
970         }
971         talloc_free(tmp_ctx);
972         return status;
973 }
974
975
976 /* open a rpc connection to a rpc pipe on SMB using the binding
977    structure to determine the endpoint and options */
978 static NTSTATUS dcerpc_pipe_connect_ncacn_np(TALLOC_CTX *tmp_ctx, 
979                                              struct dcerpc_pipe *p, 
980                                              struct dcerpc_binding *binding,
981                                              const char *pipe_uuid, 
982                                              uint32_t pipe_version,
983                                              struct cli_credentials *credentials)
984 {
985         NTSTATUS status;
986         struct smbcli_state *cli;
987         const char *pipe_name = NULL;
988
989         if (binding->flags & DCERPC_SCHANNEL) {
990                 struct cli_credentials *anon_creds
991                         = cli_credentials_init(tmp_ctx);
992                 cli_credentials_set_anonymous(anon_creds);
993                 cli_credentials_guess(anon_creds);
994                 status = smbcli_full_connection(p->conn, &cli, 
995                                                 binding->host, 
996                                                 "IPC$", NULL, 
997                                                 anon_creds);
998         } else {
999                 status = smbcli_full_connection(p->conn, &cli, 
1000                                                 binding->host, 
1001                                                 "IPC$", NULL,
1002                                                 credentials);
1003         }
1004         if (!NT_STATUS_IS_OK(status)) {
1005                 DEBUG(0,("Failed to connect to %s - %s\n", binding->host, nt_errstr(status)));
1006                 return status;
1007         }
1008
1009         /* Look up identifier using the epmapper */
1010         if (!binding->endpoint) {
1011                 status = dcerpc_epm_map_binding(tmp_ctx, binding, pipe_uuid, pipe_version);
1012                 if (!NT_STATUS_IS_OK(status)) {
1013                         DEBUG(0,("Failed to map DCERPC/TCP NCACN_NP pipe for '%s' - %s\n", 
1014                                  pipe_uuid, nt_errstr(status)));
1015                         return status;
1016                 }
1017                 DEBUG(1,("Mapped to DCERPC/NP pipe %s\n", binding->endpoint));
1018         }
1019
1020         pipe_name = binding->endpoint;
1021
1022         status = dcerpc_pipe_open_smb(p->conn, cli->tree, pipe_name);
1023         if (!NT_STATUS_IS_OK(status)) {
1024                 DEBUG(0,("Failed to open pipe %s - %s\n", pipe_name, nt_errstr(status)));
1025                 return status;
1026         }
1027
1028         return NT_STATUS_OK;
1029 }
1030
1031 /* open a rpc connection to a rpc pipe on SMP using the binding
1032    structure to determine the endpoint and options */
1033 static NTSTATUS dcerpc_pipe_connect_ncalrpc(TALLOC_CTX *tmp_ctx, 
1034                                             struct dcerpc_pipe *p, 
1035                                             struct dcerpc_binding *binding,
1036                                             const char *pipe_uuid, 
1037                                             uint32_t pipe_version)
1038 {
1039         NTSTATUS status;
1040
1041         /* Look up identifier using the epmapper */
1042         if (!binding->endpoint) {
1043                 status = dcerpc_epm_map_binding(tmp_ctx, binding, pipe_uuid, pipe_version);
1044                 if (!NT_STATUS_IS_OK(status)) {
1045                         DEBUG(0,("Failed to map DCERPC/TCP NCALRPC identifier for '%s' - %s\n", 
1046                                  pipe_uuid, nt_errstr(status)));
1047                         return status;
1048                 }
1049                 DEBUG(1,("Mapped to DCERPC/LRPC identifier %s\n", binding->endpoint));
1050         }
1051
1052         status = dcerpc_pipe_open_pipe(p->conn, binding->endpoint);
1053         if (!NT_STATUS_IS_OK(status)) {
1054                 DEBUG(0,("Failed to open ncalrpc pipe '%s' - %s\n", 
1055                          binding->endpoint, nt_errstr(status)));
1056                 return status;
1057         }
1058
1059         return status;
1060 }
1061
1062
1063
1064 /* open a rpc connection to a rpc pipe on SMP using the binding
1065    structure to determine the endpoint and options */
1066 static NTSTATUS dcerpc_pipe_connect_ncacn_unix_stream(TALLOC_CTX *tmp_ctx, 
1067                                                       struct dcerpc_pipe *p, 
1068                                                       struct dcerpc_binding *binding,
1069                                                       const char *pipe_uuid, 
1070                                                       uint32_t pipe_version)
1071 {
1072         NTSTATUS status;
1073
1074         if (!binding->endpoint) {
1075                 DEBUG(0, ("Path to unix socket not specified\n"));
1076                 return NT_STATUS_INVALID_PARAMETER;
1077         }
1078
1079         status = dcerpc_pipe_open_unix_stream(p->conn, binding->endpoint);
1080         if (!NT_STATUS_IS_OK(status)) {
1081                 DEBUG(0,("Failed to open unix socket %s - %s\n", 
1082                          binding->endpoint, nt_errstr(status)));
1083                 talloc_free(p);
1084                 return status;
1085         }
1086
1087         return status;
1088 }
1089
1090 /* open a rpc connection to a rpc pipe on TCP/IP sockets using the binding
1091    structure to determine the endpoint and options */
1092 static NTSTATUS dcerpc_pipe_connect_ncacn_ip_tcp(TALLOC_CTX *tmp_ctx, 
1093                                                  struct dcerpc_pipe *p, 
1094                                                  struct dcerpc_binding *binding,
1095                                                  const char *pipe_uuid, 
1096                                                  uint32_t pipe_version)
1097 {
1098         NTSTATUS status;
1099         uint32_t port = 0;
1100
1101         if (!binding->endpoint) {
1102                 status = dcerpc_epm_map_binding(tmp_ctx, binding, 
1103                                                 pipe_uuid, pipe_version);
1104                 if (!NT_STATUS_IS_OK(status)) {
1105                         DEBUG(0,("Failed to map DCERPC/TCP port for '%s' - %s\n", 
1106                                  pipe_uuid, nt_errstr(status)));
1107                         return status;
1108                 }
1109                 DEBUG(1,("Mapped to DCERPC/TCP port %s\n", binding->endpoint));
1110         }
1111
1112         port = atoi(binding->endpoint);
1113
1114         status = dcerpc_pipe_open_tcp(p->conn, binding->host, port);
1115         if (!NT_STATUS_IS_OK(status)) {
1116                 DEBUG(0,("Failed to connect to %s:%d - %s\n", 
1117                          binding->host, port, nt_errstr(status)));
1118                 return status;
1119         }
1120
1121         return status;
1122 }
1123
1124
1125 /* open a rpc connection to a rpc pipe, using the specified 
1126    binding structure to determine the endpoint and options */
1127 NTSTATUS dcerpc_pipe_connect_b(TALLOC_CTX *parent_ctx, 
1128                                struct dcerpc_pipe **pp, 
1129                                struct dcerpc_binding *binding,
1130                                const char *pipe_uuid, 
1131                                uint32_t pipe_version,
1132                                struct cli_credentials *credentials)
1133 {
1134         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
1135         struct dcerpc_pipe *p; 
1136         
1137         TALLOC_CTX *tmp_ctx;
1138
1139         (*pp) = NULL;
1140
1141         p = dcerpc_pipe_init(parent_ctx);
1142         if (p == NULL) {
1143                 return NT_STATUS_NO_MEMORY;
1144         }
1145         tmp_ctx = talloc_named(p, 0, "dcerpc_pipe_connect_b tmp_ctx");
1146
1147         switch (binding->transport) {
1148         case NCACN_NP:
1149                 status = dcerpc_pipe_connect_ncacn_np(tmp_ctx, 
1150                                                       p, binding, pipe_uuid, pipe_version, credentials);
1151                 break;
1152         case NCACN_IP_TCP:
1153                 status = dcerpc_pipe_connect_ncacn_ip_tcp(tmp_ctx, 
1154                                                           p, binding, pipe_uuid, pipe_version);
1155                 break;
1156         case NCACN_UNIX_STREAM:
1157                 status = dcerpc_pipe_connect_ncacn_unix_stream(tmp_ctx, 
1158                                                                p, binding, pipe_uuid, pipe_version);
1159                 break;
1160         case NCALRPC:
1161                 status = dcerpc_pipe_connect_ncalrpc(tmp_ctx, 
1162                                                      p, binding, pipe_uuid, pipe_version);
1163                 break;
1164         default:
1165                 return NT_STATUS_NOT_SUPPORTED;
1166         }
1167
1168         if (!NT_STATUS_IS_OK(status)) {
1169                 talloc_free(p);
1170                 return status;
1171         }
1172
1173         status = dcerpc_pipe_auth(p, binding, pipe_uuid, pipe_version, credentials);
1174         if (!NT_STATUS_IS_OK(status)) {
1175                 talloc_free(p);
1176                 return status;
1177         }
1178         *pp = p;
1179         talloc_free(tmp_ctx);
1180
1181         return status;
1182 }
1183
1184
1185 /* open a rpc connection to a rpc pipe, using the specified string
1186    binding to determine the endpoint and options */
1187 NTSTATUS dcerpc_pipe_connect(TALLOC_CTX *parent_ctx, 
1188                              struct dcerpc_pipe **pp, 
1189                              const char *binding,
1190                              const char *pipe_uuid, 
1191                              uint32_t pipe_version,
1192                              struct cli_credentials *credentials)
1193 {
1194         struct dcerpc_binding *b;
1195         NTSTATUS status;
1196         TALLOC_CTX *tmp_ctx;
1197
1198         tmp_ctx = talloc_named(parent_ctx, 0, "dcerpc_pipe_connect tmp_ctx");
1199         if (!tmp_ctx) {
1200                 return NT_STATUS_NO_MEMORY;
1201         }
1202
1203         status = dcerpc_parse_binding(tmp_ctx, binding, &b);
1204         if (!NT_STATUS_IS_OK(status)) {
1205                 DEBUG(0,("Failed to parse dcerpc binding '%s'\n", binding));
1206                 talloc_free(tmp_ctx);
1207                 return status;
1208         }
1209
1210         DEBUG(3,("Using binding %s\n", dcerpc_binding_string(tmp_ctx, b)));
1211
1212         status = dcerpc_pipe_connect_b(tmp_ctx,
1213                                        pp, b, pipe_uuid, pipe_version, credentials);
1214
1215         if (NT_STATUS_IS_OK(status)) {
1216                 *pp = talloc_reference(parent_ctx, *pp);
1217         }
1218         talloc_free(tmp_ctx);
1219
1220         return status;
1221 }
1222
1223
1224 /*
1225   create a secondary dcerpc connection from a primary connection
1226
1227   if the primary is a SMB connection then the secondary connection
1228   will be on the same SMB connection, but use a new fnum
1229 */
1230 NTSTATUS dcerpc_secondary_connection(struct dcerpc_pipe *p, struct dcerpc_pipe **p2,
1231                                      struct dcerpc_binding *b)
1232
1233
1234 {
1235         struct smbcli_tree *tree;
1236         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
1237
1238         (*p2) = dcerpc_pipe_init(p);
1239         if (*p2 == NULL) {
1240                 return NT_STATUS_NO_MEMORY;
1241         }
1242         
1243         switch (p->conn->transport.transport) {
1244         case NCACN_NP:
1245                 tree = dcerpc_smb_tree(p->conn);
1246                 if (!tree) {
1247                         return NT_STATUS_INVALID_PARAMETER;
1248                 }
1249                 status = dcerpc_pipe_open_smb((*p2)->conn, tree, b->endpoint);
1250                 break;
1251
1252         case NCACN_IP_TCP:
1253                 status = dcerpc_pipe_open_tcp((*p2)->conn, b->host, atoi(b->endpoint));
1254                 break;
1255
1256         case NCALRPC:
1257                 status = dcerpc_pipe_open_pipe((*p2)->conn, b->endpoint);
1258                 break;
1259
1260         default:
1261                 return NT_STATUS_NOT_SUPPORTED;
1262         }
1263
1264         if (!NT_STATUS_IS_OK(status)) {
1265                 talloc_free(*p2);
1266                 return status;
1267         }
1268
1269         (*p2)->conn->flags = p->conn->flags;
1270
1271         return NT_STATUS_OK;
1272 }
1273
1274 NTSTATUS dcerpc_generic_session_key(struct dcerpc_connection *c,
1275                                     DATA_BLOB *session_key)
1276 {
1277         /* this took quite a few CPU cycles to find ... */
1278         session_key->data = discard_const_p(unsigned char, "SystemLibraryDTC");
1279         session_key->length = 16;
1280         return NT_STATUS_OK;
1281 }
1282
1283 /*
1284   fetch the user session key - may be default (above) or the SMB session key
1285 */
1286 NTSTATUS dcerpc_fetch_session_key(struct dcerpc_pipe *p,
1287                                   DATA_BLOB *session_key)
1288 {
1289         return p->conn->security_state.session_key(p->conn, session_key);
1290 }
1291
1292
1293 /*
1294   log a rpc packet in a format suitable for ndrdump. This is especially useful
1295   for sealed packets, where ethereal cannot easily see the contents
1296
1297   this triggers on a debug level of >= 10
1298 */
1299 void dcerpc_log_packet(const struct dcerpc_interface_table *ndr,
1300                        uint32_t opnum, uint32_t flags, DATA_BLOB *pkt)
1301 {
1302         const int num_examples = 20;
1303         int i;
1304
1305         if (DEBUGLEVEL < 10) return;
1306
1307         for (i=0;i<num_examples;i++) {
1308                 char *name=NULL;
1309                 asprintf(&name, "%s/rpclog/%s-%u.%d.%s", 
1310                          lp_lockdir(), ndr->name, opnum, i,
1311                          (flags&NDR_IN)?"in":"out");
1312                 if (name == NULL) {
1313                         return;
1314                 }
1315                 if (!file_exist(name)) {
1316                         if (file_save(name, pkt->data, pkt->length)) {
1317                                 DEBUG(10,("Logged rpc packet to %s\n", name));
1318                         }
1319                         free(name);
1320                         break;
1321                 }
1322                 free(name);
1323         }
1324 }
1325
1326
1327
1328 /*
1329   create a secondary context from a primary connection
1330
1331   this uses dcerpc_alter_context() to create a new dcerpc context_id
1332 */
1333 NTSTATUS dcerpc_secondary_context(struct dcerpc_pipe *p, 
1334                                   struct dcerpc_pipe **pp2,
1335                                   const char *pipe_uuid,
1336                                   uint32_t pipe_version)
1337 {
1338         NTSTATUS status;
1339         struct dcerpc_pipe *p2;
1340         
1341         p2 = talloc_zero(p, struct dcerpc_pipe);
1342         if (p2 == NULL) {
1343                 return NT_STATUS_NO_MEMORY;
1344         }
1345         p2->conn = talloc_reference(p2, p->conn);
1346
1347         p2->context_id = ++p->conn->next_context_id;
1348
1349         status = GUID_from_string(pipe_uuid, &p2->syntax.uuid);
1350         if (!NT_STATUS_IS_OK(status)) {
1351                 talloc_free(p2);
1352                 return status;
1353         }
1354         p2->syntax.if_version = pipe_version;
1355
1356         status = GUID_from_string(NDR_GUID, &p2->transfer_syntax.uuid);
1357         if (!NT_STATUS_IS_OK(status)) {
1358                 talloc_free(p2);
1359                 return status;
1360         }
1361         p2->transfer_syntax.if_version = NDR_GUID_VERSION;
1362
1363         status = dcerpc_alter_context(p2, p2, &p2->syntax, &p2->transfer_syntax);
1364         if (!NT_STATUS_IS_OK(status)) {
1365                 talloc_free(p2);
1366                 return status;
1367         }
1368
1369         *pp2 = p2;
1370
1371         return status;
1372 }