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