r6178: fix ncacn_np connection without sign or seal against NT4
[bbaumbach/samba-autobuild/.git] / source4 / librpc / rpc / dcerpc_util.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    dcerpc utility functions
5
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) Jelmer Vernooij 2004
8    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 dcerpc_packet into a blob, potentially with auth info
91 */
92 NTSTATUS dcerpc_push_auth(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, 
93                           struct dcerpc_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_dcerpc_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 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_anonymous(anon_creds);
810         cli_credentials_guess(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_ANY) && 
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                 uint8_t auth_type;
944                 if (binding->flags & DCERPC_AUTH_SPNEGO) {
945                         auth_type = DCERPC_AUTH_TYPE_SPNEGO;
946                 } else if (binding->flags & DCERPC_AUTH_KRB5) {
947                         auth_type = DCERPC_AUTH_TYPE_KRB5;
948                 } else if (binding->flags & DCERPC_SCHANNEL_ANY) {
949                         auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
950                 } else {
951                         auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
952                 }
953
954                 status = dcerpc_bind_auth_password(p, pipe_uuid, pipe_version, 
955                                                    credentials, auth_type,
956                                                    binding->authservice);
957         } else {
958                 status = dcerpc_bind_auth_none(p, pipe_uuid, pipe_version);
959         }
960
961         if (!NT_STATUS_IS_OK(status)) {
962                 DEBUG(0,("Failed to bind to uuid %s - %s\n", pipe_uuid, nt_errstr(status)));
963         }
964         talloc_free(tmp_ctx);
965         return status;
966 }
967
968
969 /* open a rpc connection to a rpc pipe on SMB using the binding
970    structure to determine the endpoint and options */
971 static NTSTATUS dcerpc_pipe_connect_ncacn_np(TALLOC_CTX *tmp_ctx, 
972                                              struct dcerpc_pipe *p, 
973                                              struct dcerpc_binding *binding,
974                                              const char *pipe_uuid, 
975                                              uint32_t pipe_version,
976                                              struct cli_credentials *credentials)
977 {
978         NTSTATUS status;
979         struct smbcli_state *cli;
980         const char *pipe_name = NULL;
981
982         if (binding->flags & DCERPC_SCHANNEL_ANY) {
983                 struct cli_credentials *anon_creds
984                         = cli_credentials_init(tmp_ctx);
985                 cli_credentials_set_anonymous(anon_creds);
986                 cli_credentials_guess(anon_creds);
987                 status = smbcli_full_connection(p->conn, &cli, 
988                                                 binding->host, 
989                                                 "IPC$", NULL, 
990                                                 anon_creds);
991         } else {
992                 status = smbcli_full_connection(p->conn, &cli, 
993                                                 binding->host, 
994                                                 "IPC$", NULL,
995                                                 credentials);
996         }
997         if (!NT_STATUS_IS_OK(status)) {
998                 DEBUG(0,("Failed to connect to %s - %s\n", binding->host, nt_errstr(status)));
999                 return status;
1000         }
1001
1002         /* Look up identifier using the epmapper */
1003         if (!binding->endpoint) {
1004                 status = dcerpc_epm_map_binding(tmp_ctx, binding, pipe_uuid, pipe_version);
1005                 if (!NT_STATUS_IS_OK(status)) {
1006                         DEBUG(0,("Failed to map DCERPC/TCP NCACN_NP pipe for '%s' - %s\n", 
1007                                  pipe_uuid, nt_errstr(status)));
1008                         return status;
1009                 }
1010                 DEBUG(1,("Mapped to DCERPC/NP pipe %s\n", binding->endpoint));
1011         }
1012
1013         pipe_name = binding->endpoint;
1014
1015         status = dcerpc_pipe_open_smb(p->conn, cli->tree, pipe_name);
1016         if (!NT_STATUS_IS_OK(status)) {
1017                 DEBUG(0,("Failed to open pipe %s - %s\n", pipe_name, nt_errstr(status)));
1018                 return status;
1019         }
1020
1021         return NT_STATUS_OK;
1022 }
1023
1024 /* open a rpc connection to a rpc pipe on SMP using the binding
1025    structure to determine the endpoint and options */
1026 static NTSTATUS dcerpc_pipe_connect_ncalrpc(TALLOC_CTX *tmp_ctx, 
1027                                             struct dcerpc_pipe *p, 
1028                                             struct dcerpc_binding *binding,
1029                                             const char *pipe_uuid, 
1030                                             uint32_t pipe_version)
1031 {
1032         NTSTATUS status;
1033
1034         /* Look up identifier using the epmapper */
1035         if (!binding->endpoint) {
1036                 status = dcerpc_epm_map_binding(tmp_ctx, binding, pipe_uuid, pipe_version);
1037                 if (!NT_STATUS_IS_OK(status)) {
1038                         DEBUG(0,("Failed to map DCERPC/TCP NCALRPC identifier for '%s' - %s\n", 
1039                                  pipe_uuid, nt_errstr(status)));
1040                         return status;
1041                 }
1042                 DEBUG(1,("Mapped to DCERPC/LRPC identifier %s\n", binding->endpoint));
1043         }
1044
1045         status = dcerpc_pipe_open_pipe(p->conn, binding->endpoint);
1046         if (!NT_STATUS_IS_OK(status)) {
1047                 DEBUG(0,("Failed to open ncalrpc pipe '%s' - %s\n", 
1048                          binding->endpoint, nt_errstr(status)));
1049                 return status;
1050         }
1051
1052         return status;
1053 }
1054
1055
1056
1057 /* open a rpc connection to a rpc pipe on SMP using the binding
1058    structure to determine the endpoint and options */
1059 static NTSTATUS dcerpc_pipe_connect_ncacn_unix_stream(TALLOC_CTX *tmp_ctx, 
1060                                                       struct dcerpc_pipe *p, 
1061                                                       struct dcerpc_binding *binding,
1062                                                       const char *pipe_uuid, 
1063                                                       uint32_t pipe_version)
1064 {
1065         NTSTATUS status;
1066
1067         if (!binding->endpoint) {
1068                 DEBUG(0, ("Path to unix socket not specified\n"));
1069                 return NT_STATUS_INVALID_PARAMETER;
1070         }
1071
1072         status = dcerpc_pipe_open_unix_stream(p->conn, binding->endpoint);
1073         if (!NT_STATUS_IS_OK(status)) {
1074                 DEBUG(0,("Failed to open unix socket %s - %s\n", 
1075                          binding->endpoint, nt_errstr(status)));
1076                 talloc_free(p);
1077                 return status;
1078         }
1079
1080         return status;
1081 }
1082
1083 /* open a rpc connection to a rpc pipe on TCP/IP sockets using the binding
1084    structure to determine the endpoint and options */
1085 static NTSTATUS dcerpc_pipe_connect_ncacn_ip_tcp(TALLOC_CTX *tmp_ctx, 
1086                                                  struct dcerpc_pipe *p, 
1087                                                  struct dcerpc_binding *binding,
1088                                                  const char *pipe_uuid, 
1089                                                  uint32_t pipe_version)
1090 {
1091         NTSTATUS status;
1092         uint32_t port = 0;
1093
1094         if (!binding->endpoint) {
1095                 status = dcerpc_epm_map_binding(tmp_ctx, binding, 
1096                                                 pipe_uuid, pipe_version);
1097                 if (!NT_STATUS_IS_OK(status)) {
1098                         DEBUG(0,("Failed to map DCERPC/TCP port for '%s' - %s\n", 
1099                                  pipe_uuid, nt_errstr(status)));
1100                         return status;
1101                 }
1102                 DEBUG(1,("Mapped to DCERPC/TCP port %s\n", binding->endpoint));
1103         }
1104
1105         port = atoi(binding->endpoint);
1106
1107         status = dcerpc_pipe_open_tcp(p->conn, binding->host, port);
1108         if (!NT_STATUS_IS_OK(status)) {
1109                 DEBUG(0,("Failed to connect to %s:%d - %s\n", 
1110                          binding->host, port, nt_errstr(status)));
1111                 return status;
1112         }
1113
1114         return status;
1115 }
1116
1117
1118 /* open a rpc connection to a rpc pipe, using the specified 
1119    binding structure to determine the endpoint and options */
1120 NTSTATUS dcerpc_pipe_connect_b(TALLOC_CTX *parent_ctx, 
1121                                struct dcerpc_pipe **pp, 
1122                                struct dcerpc_binding *binding,
1123                                const char *pipe_uuid, 
1124                                uint32_t pipe_version,
1125                                struct cli_credentials *credentials)
1126 {
1127         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
1128         struct dcerpc_pipe *p; 
1129         
1130         TALLOC_CTX *tmp_ctx;
1131
1132         (*pp) = NULL;
1133
1134         p = dcerpc_pipe_init(parent_ctx);
1135         if (p == NULL) {
1136                 return NT_STATUS_NO_MEMORY;
1137         }
1138         tmp_ctx = talloc_named(p, 0, "dcerpc_pipe_connect_b tmp_ctx");
1139
1140         switch (binding->transport) {
1141         case NCACN_NP:
1142                 status = dcerpc_pipe_connect_ncacn_np(tmp_ctx, 
1143                                                       p, binding, pipe_uuid, pipe_version, credentials);
1144                 break;
1145         case NCACN_IP_TCP:
1146                 status = dcerpc_pipe_connect_ncacn_ip_tcp(tmp_ctx, 
1147                                                           p, binding, pipe_uuid, pipe_version);
1148                 break;
1149         case NCACN_UNIX_STREAM:
1150                 status = dcerpc_pipe_connect_ncacn_unix_stream(tmp_ctx, 
1151                                                                p, binding, pipe_uuid, pipe_version);
1152                 break;
1153         case NCALRPC:
1154                 status = dcerpc_pipe_connect_ncalrpc(tmp_ctx, 
1155                                                      p, binding, pipe_uuid, pipe_version);
1156                 break;
1157         default:
1158                 return NT_STATUS_NOT_SUPPORTED;
1159         }
1160
1161         if (!NT_STATUS_IS_OK(status)) {
1162                 talloc_free(p);
1163                 return status;
1164         }
1165
1166         status = dcerpc_pipe_auth(p, binding, pipe_uuid, pipe_version, credentials);
1167         if (!NT_STATUS_IS_OK(status)) {
1168                 talloc_free(p);
1169                 return status;
1170         }
1171         *pp = p;
1172         talloc_free(tmp_ctx);
1173
1174         return status;
1175 }
1176
1177
1178 /* open a rpc connection to a rpc pipe, using the specified string
1179    binding to determine the endpoint and options */
1180 NTSTATUS dcerpc_pipe_connect(TALLOC_CTX *parent_ctx, 
1181                              struct dcerpc_pipe **pp, 
1182                              const char *binding,
1183                              const char *pipe_uuid, 
1184                              uint32_t pipe_version,
1185                              struct cli_credentials *credentials)
1186 {
1187         struct dcerpc_binding *b;
1188         NTSTATUS status;
1189         TALLOC_CTX *tmp_ctx;
1190
1191         tmp_ctx = talloc_named(parent_ctx, 0, "dcerpc_pipe_connect tmp_ctx");
1192         if (!tmp_ctx) {
1193                 return NT_STATUS_NO_MEMORY;
1194         }
1195
1196         status = dcerpc_parse_binding(tmp_ctx, binding, &b);
1197         if (!NT_STATUS_IS_OK(status)) {
1198                 DEBUG(0,("Failed to parse dcerpc binding '%s'\n", binding));
1199                 talloc_free(tmp_ctx);
1200                 return status;
1201         }
1202
1203         DEBUG(3,("Using binding %s\n", dcerpc_binding_string(tmp_ctx, b)));
1204
1205         status = dcerpc_pipe_connect_b(tmp_ctx,
1206                                        pp, b, pipe_uuid, pipe_version, credentials);
1207
1208         if (NT_STATUS_IS_OK(status)) {
1209                 *pp = talloc_reference(parent_ctx, *pp);
1210         }
1211         talloc_free(tmp_ctx);
1212
1213         return status;
1214 }
1215
1216
1217 /*
1218   create a secondary dcerpc connection from a primary connection
1219
1220   if the primary is a SMB connection then the secondary connection
1221   will be on the same SMB connection, but use a new fnum
1222 */
1223 NTSTATUS dcerpc_secondary_connection(struct dcerpc_pipe *p, struct dcerpc_pipe **p2,
1224                                      struct dcerpc_binding *b)
1225
1226
1227 {
1228         struct smbcli_tree *tree;
1229         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
1230
1231         (*p2) = dcerpc_pipe_init(p);
1232         if (*p2 == NULL) {
1233                 return NT_STATUS_NO_MEMORY;
1234         }
1235         
1236         switch (p->conn->transport.transport) {
1237         case NCACN_NP:
1238                 tree = dcerpc_smb_tree(p->conn);
1239                 if (!tree) {
1240                         return NT_STATUS_INVALID_PARAMETER;
1241                 }
1242                 status = dcerpc_pipe_open_smb((*p2)->conn, tree, b->endpoint);
1243                 break;
1244
1245         case NCACN_IP_TCP:
1246                 status = dcerpc_pipe_open_tcp((*p2)->conn, b->host, atoi(b->endpoint));
1247                 break;
1248
1249         case NCALRPC:
1250                 status = dcerpc_pipe_open_pipe((*p2)->conn, b->endpoint);
1251                 break;
1252
1253         default:
1254                 return NT_STATUS_NOT_SUPPORTED;
1255         }
1256
1257         if (!NT_STATUS_IS_OK(status)) {
1258                 talloc_free(*p2);
1259                 return status;
1260         }
1261
1262         (*p2)->conn->flags = p->conn->flags;
1263
1264         return NT_STATUS_OK;
1265 }
1266
1267 NTSTATUS dcerpc_generic_session_key(struct dcerpc_connection *c,
1268                                     DATA_BLOB *session_key)
1269 {
1270         /* this took quite a few CPU cycles to find ... */
1271         session_key->data = discard_const_p(unsigned char, "SystemLibraryDTC");
1272         session_key->length = 16;
1273         return NT_STATUS_OK;
1274 }
1275
1276 /*
1277   fetch the user session key - may be default (above) or the SMB session key
1278 */
1279 NTSTATUS dcerpc_fetch_session_key(struct dcerpc_pipe *p,
1280                                   DATA_BLOB *session_key)
1281 {
1282         return p->conn->security_state.session_key(p->conn, session_key);
1283 }
1284
1285
1286 /*
1287   log a rpc packet in a format suitable for ndrdump. This is especially useful
1288   for sealed packets, where ethereal cannot easily see the contents
1289
1290   this triggers on a debug level of >= 10
1291 */
1292 void dcerpc_log_packet(const struct dcerpc_interface_table *ndr,
1293                        uint32_t opnum, uint32_t flags, DATA_BLOB *pkt)
1294 {
1295         const int num_examples = 20;
1296         int i;
1297
1298         if (DEBUGLEVEL < 10) return;
1299
1300         for (i=0;i<num_examples;i++) {
1301                 char *name=NULL;
1302                 asprintf(&name, "%s/rpclog/%s-%u.%d.%s", 
1303                          lp_lockdir(), ndr->name, opnum, i,
1304                          (flags&NDR_IN)?"in":"out");
1305                 if (name == NULL) {
1306                         return;
1307                 }
1308                 if (!file_exist(name)) {
1309                         if (file_save(name, pkt->data, pkt->length)) {
1310                                 DEBUG(10,("Logged rpc packet to %s\n", name));
1311                         }
1312                         free(name);
1313                         break;
1314                 }
1315                 free(name);
1316         }
1317 }
1318
1319
1320
1321 /*
1322   create a secondary context from a primary connection
1323
1324   this uses dcerpc_alter_context() to create a new dcerpc context_id
1325 */
1326 NTSTATUS dcerpc_secondary_context(struct dcerpc_pipe *p, 
1327                                   struct dcerpc_pipe **pp2,
1328                                   const char *pipe_uuid,
1329                                   uint32_t pipe_version)
1330 {
1331         NTSTATUS status;
1332         struct dcerpc_pipe *p2;
1333         
1334         p2 = talloc_zero(p, struct dcerpc_pipe);
1335         if (p2 == NULL) {
1336                 return NT_STATUS_NO_MEMORY;
1337         }
1338         p2->conn = talloc_reference(p2, p->conn);
1339
1340         p2->context_id = ++p->conn->next_context_id;
1341
1342         status = GUID_from_string(pipe_uuid, &p2->syntax.uuid);
1343         if (!NT_STATUS_IS_OK(status)) {
1344                 talloc_free(p2);
1345                 return status;
1346         }
1347         p2->syntax.if_version = pipe_version;
1348
1349         status = GUID_from_string(NDR_GUID, &p2->transfer_syntax.uuid);
1350         if (!NT_STATUS_IS_OK(status)) {
1351                 talloc_free(p2);
1352                 return status;
1353         }
1354         p2->transfer_syntax.if_version = NDR_GUID_VERSION;
1355
1356         status = dcerpc_alter_context(p2, p2, &p2->syntax, &p2->transfer_syntax);
1357         if (!NT_STATUS_IS_OK(status)) {
1358                 talloc_free(p2);
1359                 return status;
1360         }
1361
1362         *pp2 = p2;
1363
1364         return status;
1365 }