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