r12608: Remove some unused #include lines.
[ira/wip.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         } else if (!cli_credentials_is_anonymous(credentials) &&
894                 !(binding->transport == NCACN_NP &&
895                   !(binding->flags & DCERPC_SIGN) &&
896                   !(binding->flags & DCERPC_SEAL))) {   
897         
898                 /* Perform an authenticated DCE-RPC bind, except where
899                  * we ask for a connection on NCACN_NP, and that
900                  * connection is not signed or sealed.  For that case
901                  * we rely on the already authenicated CIFS connection
902                  */
903
904                 uint8_t auth_type;
905                 
906                 if ((p->conn->flags & (DCERPC_SIGN|DCERPC_SEAL)) == 0) {
907                         /*
908                           we are doing an authenticated connection,
909                           but not using sign or seal. We must force
910                           the CONNECT dcerpc auth type as a NONE auth
911                           type doesn't allow authentication
912                           information to be passed.
913                         */
914                         p->conn->flags |= DCERPC_CONNECT;
915                 }
916
917                 if (binding->flags & DCERPC_AUTH_SPNEGO) {
918                         auth_type = DCERPC_AUTH_TYPE_SPNEGO;
919                 } else if (binding->flags & DCERPC_AUTH_KRB5) {
920                         auth_type = DCERPC_AUTH_TYPE_KRB5;
921                 } else if (binding->flags & DCERPC_SCHANNEL) {
922                         auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
923                 } else {
924                         auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
925                 }
926
927                 status = dcerpc_bind_auth(p, table,
928                                           credentials, auth_type, table->authservices->names[0]);
929         } else {
930                 status = dcerpc_bind_auth_none(p, table);
931         }
932
933         if (!NT_STATUS_IS_OK(status)) {
934                 char *uuid_str = GUID_string(p, &table->uuid);
935                 DEBUG(0,("Failed to bind to uuid %s - %s\n", uuid_str, nt_errstr(status)));
936                 talloc_free(uuid_str);
937         }
938         talloc_free(tmp_ctx);
939         return status;
940 }
941
942
943 /* open a rpc connection to a rpc pipe on SMB using the binding
944    structure to determine the endpoint and options */
945 static NTSTATUS dcerpc_pipe_connect_ncacn_np(TALLOC_CTX *tmp_ctx, 
946                                              struct dcerpc_pipe_connect *io)
947 {
948         if (io->binding->flags & DCERPC_SMB2) {
949                 return dcerpc_pipe_connect_ncacn_np_smb2(tmp_ctx, io);
950
951         }
952         return dcerpc_pipe_connect_ncacn_np_smb(tmp_ctx, io);
953 }
954
955
956 /* open a rpc connection to a rpc pipe on SMP using the binding
957    structure to determine the endpoint and options */
958 static NTSTATUS dcerpc_pipe_connect_ncalrpc(TALLOC_CTX *tmp_ctx, 
959                                             struct dcerpc_pipe *p, 
960                                             struct dcerpc_binding *binding,
961                                                 const struct dcerpc_interface_table *table)
962 {
963         NTSTATUS status;
964
965         status = dcerpc_pipe_open_pipe(p->conn, binding->endpoint);
966         if (!NT_STATUS_IS_OK(status)) {
967                 DEBUG(0,("Failed to open ncalrpc pipe '%s' - %s\n", 
968                          binding->endpoint, nt_errstr(status)));
969                 return status;
970         }
971
972         return status;
973 }
974
975
976
977 /* open a rpc connection to a rpc pipe on SMP using the binding
978    structure to determine the endpoint and options */
979 static NTSTATUS dcerpc_pipe_connect_ncacn_unix_stream(TALLOC_CTX *tmp_ctx, 
980                                                       struct dcerpc_pipe *p, 
981                                                       struct dcerpc_binding *binding,
982                                                           const struct dcerpc_interface_table *table)
983 {
984         NTSTATUS status;
985
986         if (!binding->endpoint) {
987                 DEBUG(0, ("Path to unix socket not specified\n"));
988                 return NT_STATUS_INVALID_PARAMETER;
989         }
990
991         status = dcerpc_pipe_open_unix_stream(p->conn, binding->endpoint);
992         if (!NT_STATUS_IS_OK(status)) {
993                 DEBUG(0,("Failed to open unix socket %s - %s\n", 
994                          binding->endpoint, nt_errstr(status)));
995                 talloc_free(p);
996         return status;
997         }
998
999         return status;
1000 }
1001
1002 /* open a rpc connection to a rpc pipe on TCP/IP sockets using the binding
1003    structure to determine the endpoint and options */
1004 static NTSTATUS dcerpc_pipe_connect_ncacn_ip_tcp(TALLOC_CTX *tmp_ctx, 
1005                                                  struct dcerpc_pipe *p, 
1006                                                  struct dcerpc_binding *binding,
1007                                                  const struct dcerpc_interface_table *table)
1008 {
1009         NTSTATUS status;
1010         uint32_t port = 0;
1011
1012         port = atoi(binding->endpoint);
1013
1014         status = dcerpc_pipe_open_tcp(p->conn, binding->host, port);
1015         if (!NT_STATUS_IS_OK(status)) {
1016                 DEBUG(0,("Failed to connect to %s:%d - %s\n", 
1017                          binding->host, port, nt_errstr(status)));
1018                 return status;
1019         }
1020
1021         return status;
1022 }
1023
1024
1025 /* open a rpc connection to a rpc pipe, using the specified 
1026    binding structure to determine the endpoint and options */
1027 NTSTATUS dcerpc_pipe_connect_b(TALLOC_CTX *parent_ctx, 
1028                                struct dcerpc_pipe **pp, 
1029                                struct dcerpc_binding *binding,
1030                                    const struct dcerpc_interface_table *table,
1031                                struct cli_credentials *credentials,
1032                                struct event_context *ev)
1033 {
1034         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
1035         struct dcerpc_pipe *p; 
1036         struct dcerpc_pipe_connect pc;
1037         
1038         TALLOC_CTX *tmp_ctx;
1039
1040         (*pp) = NULL;
1041
1042         p = dcerpc_pipe_init(parent_ctx, ev);
1043         if (p == NULL) {
1044                 return NT_STATUS_NO_MEMORY;
1045         }
1046         tmp_ctx = talloc_named(p, 0, "dcerpc_pipe_connect_b tmp_ctx");
1047
1048         switch (binding->transport) {
1049         case NCACN_NP:
1050         case NCACN_IP_TCP:
1051         case NCALRPC:
1052                 if (!binding->endpoint) {
1053                         status = dcerpc_epm_map_binding(tmp_ctx, binding, table,
1054                                                         p->conn->event_ctx);
1055                         if (!NT_STATUS_IS_OK(status)) {
1056                                 DEBUG(0,("Failed to map DCERPC endpoint for '%s' - %s\n", 
1057                                          GUID_string(tmp_ctx, &table->uuid), nt_errstr(status)));
1058                                 talloc_free(tmp_ctx);
1059                                 return status;
1060                         }
1061                         DEBUG(2,("Mapped to DCERPC endpoint %s\n", binding->endpoint));
1062                 }
1063                 break;
1064
1065                 /* Fall through to next switch statement */
1066
1067         default:
1068                 break;
1069         }
1070
1071         pc.pipe          = p;
1072         pc.binding       = binding;
1073         pc.interface     = table;
1074         pc.creds         = credentials;
1075
1076         switch (binding->transport) {
1077         case NCACN_NP:
1078                 status = dcerpc_pipe_connect_ncacn_np(tmp_ctx, &pc);
1079                 break;
1080
1081         case NCACN_IP_TCP:
1082                 status = dcerpc_pipe_connect_ncacn_ip_tcp(tmp_ctx, 
1083                                                           p, binding, table);
1084                 break;
1085         case NCACN_UNIX_STREAM:
1086                 status = dcerpc_pipe_connect_ncacn_unix_stream(tmp_ctx, 
1087                                                                p, binding, table);
1088                 break;
1089         case NCALRPC:
1090                 status = dcerpc_pipe_connect_ncalrpc(tmp_ctx, 
1091                                                      p, binding, table);
1092                 break;
1093         default:
1094                 return NT_STATUS_NOT_SUPPORTED;
1095         }
1096
1097         if (!NT_STATUS_IS_OK(status)) {
1098                 talloc_free(p);
1099                 return status;
1100         }
1101
1102         status = dcerpc_pipe_auth(p, binding, table, credentials);
1103         if (!NT_STATUS_IS_OK(status)) {
1104                 talloc_free(p);
1105                 return status;
1106         }
1107         *pp = p;
1108         talloc_free(tmp_ctx);
1109
1110         return status;
1111 }
1112
1113
1114 /* open a rpc connection to a rpc pipe, using the specified string
1115    binding to determine the endpoint and options */
1116 NTSTATUS dcerpc_pipe_connect(TALLOC_CTX *parent_ctx, 
1117                              struct dcerpc_pipe **pp, 
1118                                  const char *binding,
1119                                  const struct dcerpc_interface_table *table,
1120                              struct cli_credentials *credentials,
1121                              struct event_context *ev)
1122 {
1123         struct dcerpc_binding *b;
1124         NTSTATUS status;
1125         TALLOC_CTX *tmp_ctx;
1126
1127         tmp_ctx = talloc_named(parent_ctx, 0, "dcerpc_pipe_connect tmp_ctx");
1128         if (!tmp_ctx) {
1129                 return NT_STATUS_NO_MEMORY;
1130         }
1131
1132         status = dcerpc_parse_binding(tmp_ctx, binding, &b);
1133         if (!NT_STATUS_IS_OK(status)) {
1134                 DEBUG(0,("Failed to parse dcerpc binding '%s'\n", binding));
1135                 talloc_free(tmp_ctx);
1136                 return status;
1137         }
1138
1139         DEBUG(3,("Using binding %s\n", dcerpc_binding_string(tmp_ctx, b)));
1140
1141         status = dcerpc_pipe_connect_b(tmp_ctx, pp, b, table, credentials, ev);
1142
1143         if (NT_STATUS_IS_OK(status)) {
1144                 *pp = talloc_steal(parent_ctx, *pp);
1145         }
1146         talloc_free(tmp_ctx);
1147
1148         return status;
1149 }
1150
1151
1152 /*
1153   create a secondary dcerpc connection from a primary connection
1154
1155   if the primary is a SMB connection then the secondary connection
1156   will be on the same SMB connection, but use a new fnum
1157 */
1158 NTSTATUS dcerpc_secondary_connection(struct dcerpc_pipe *p, struct dcerpc_pipe **p2,
1159                                      struct dcerpc_binding *b)
1160
1161
1162 {
1163         struct smbcli_tree *tree;
1164         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
1165
1166         (*p2) = dcerpc_pipe_init(p, p->conn->event_ctx);
1167         if (*p2 == NULL) {
1168                 return NT_STATUS_NO_MEMORY;
1169         }
1170         
1171         switch (p->conn->transport.transport) {
1172         case NCACN_NP:
1173                 tree = dcerpc_smb_tree(p->conn);
1174                 if (!tree) {
1175                         return NT_STATUS_INVALID_PARAMETER;
1176                 }
1177                 status = dcerpc_pipe_open_smb((*p2)->conn, tree, b->endpoint);
1178                 break;
1179
1180         case NCACN_IP_TCP:
1181                 status = dcerpc_pipe_open_tcp((*p2)->conn, b->host, atoi(b->endpoint));
1182                 break;
1183
1184         case NCALRPC:
1185                 status = dcerpc_pipe_open_pipe((*p2)->conn, b->endpoint);
1186                 break;
1187
1188         default:
1189                 return NT_STATUS_NOT_SUPPORTED;
1190         }
1191
1192         if (!NT_STATUS_IS_OK(status)) {
1193                 talloc_free(*p2);
1194                 return status;
1195         }
1196
1197         (*p2)->conn->flags = p->conn->flags;
1198
1199         return NT_STATUS_OK;
1200 }
1201
1202 NTSTATUS dcerpc_generic_session_key(struct dcerpc_connection *c,
1203                                     DATA_BLOB *session_key)
1204 {
1205         /* this took quite a few CPU cycles to find ... */
1206         session_key->data = discard_const_p(unsigned char, "SystemLibraryDTC");
1207         session_key->length = 16;
1208         return NT_STATUS_OK;
1209 }
1210
1211 /*
1212   fetch the user session key - may be default (above) or the SMB session key
1213 */
1214 NTSTATUS dcerpc_fetch_session_key(struct dcerpc_pipe *p,
1215                                   DATA_BLOB *session_key)
1216 {
1217         return p->conn->security_state.session_key(p->conn, session_key);
1218 }
1219
1220
1221 /*
1222   log a rpc packet in a format suitable for ndrdump. This is especially useful
1223   for sealed packets, where ethereal cannot easily see the contents
1224
1225   this triggers on a debug level of >= 10
1226 */
1227 void dcerpc_log_packet(const struct dcerpc_interface_table *ndr,
1228                        uint32_t opnum, uint32_t flags, DATA_BLOB *pkt)
1229 {
1230         const int num_examples = 20;
1231         int i;
1232
1233         if (DEBUGLEVEL < 10) return;
1234
1235         for (i=0;i<num_examples;i++) {
1236                 char *name=NULL;
1237                 asprintf(&name, "%s/rpclog/%s-%u.%d.%s", 
1238                          lp_lockdir(), ndr->name, opnum, i,
1239                          (flags&NDR_IN)?"in":"out");
1240                 if (name == NULL) {
1241                         return;
1242                 }
1243                 if (!file_exist(name)) {
1244                         if (file_save(name, pkt->data, pkt->length)) {
1245                                 DEBUG(10,("Logged rpc packet to %s\n", name));
1246                         }
1247                         free(name);
1248                         break;
1249                 }
1250                 free(name);
1251         }
1252 }
1253
1254
1255
1256 /*
1257   create a secondary context from a primary connection
1258
1259   this uses dcerpc_alter_context() to create a new dcerpc context_id
1260 */
1261 NTSTATUS dcerpc_secondary_context(struct dcerpc_pipe *p, 
1262                                   struct dcerpc_pipe **pp2,
1263                                   const struct dcerpc_interface_table *table)
1264 {
1265         NTSTATUS status;
1266         struct dcerpc_pipe *p2;
1267         
1268         p2 = talloc_zero(p, struct dcerpc_pipe);
1269         if (p2 == NULL) {
1270                 return NT_STATUS_NO_MEMORY;
1271         }
1272         p2->conn = talloc_reference(p2, p->conn);
1273         p2->request_timeout = p->request_timeout;
1274
1275         p2->context_id = ++p->conn->next_context_id;
1276
1277         p2->syntax.uuid = table->uuid;
1278         p2->syntax.if_version = table->if_version;
1279
1280         p2->transfer_syntax = ndr_transfer_syntax;
1281
1282         status = dcerpc_alter_context(p2, p2, &p2->syntax, &p2->transfer_syntax);
1283         if (!NT_STATUS_IS_OK(status)) {
1284                 talloc_free(p2);
1285                 return status;
1286         }
1287
1288         *pp2 = p2;
1289
1290         return status;
1291 }