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