3ee3445ede05379d56958eccc1f66cea94c62018
[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, 3,
168                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_UUID },
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         b->authservice = NULL;
339
340         p = strchr(s, '@');
341
342         if (p && PTR_DIFF(p, s) == 36) { /* 36 is the length of a UUID */
343                 NTSTATUS status;
344
345                 status = GUID_from_string(s, &b->object);
346
347                 if (NT_STATUS_IS_ERR(status)) {
348                         DEBUG(0, ("Failed parsing UUID\n"));
349                         return status;
350                 }
351
352                 s = p + 1;
353         } else {
354                 ZERO_STRUCT(b->object);
355         }
356
357         b->object_version = 0;
358
359         p = strchr(s, ':');
360         if (!p) {
361                 return NT_STATUS_INVALID_PARAMETER;
362         }
363
364         type = talloc_strndup(mem_ctx, s, PTR_DIFF(p, s));
365         if (!type) {
366                 return NT_STATUS_NO_MEMORY;
367         }
368
369         for (i=0;i<ARRAY_SIZE(transports);i++) {
370                 if (strcasecmp(type, transports[i].name) == 0) {
371                         b->transport = transports[i].transport;
372                         break;
373                 }
374         }
375         if (i==ARRAY_SIZE(transports)) {
376                 DEBUG(0,("Unknown dcerpc transport '%s'\n", type));
377                 return NT_STATUS_INVALID_PARAMETER;
378         }
379         
380         s = p+1;
381
382         p = strchr(s, '[');
383         if (p) {
384                 b->host = talloc_strndup(b, s, PTR_DIFF(p, s));
385                 options = talloc_strdup(mem_ctx, p+1);
386                 if (options[strlen(options)-1] != ']') {
387                         return NT_STATUS_INVALID_PARAMETER;
388                 }
389                 options[strlen(options)-1] = 0;
390         } else {
391                 b->host = talloc_strdup(b, s);
392                 options = NULL;
393         }
394
395         if (!b->host) {
396                 return NT_STATUS_NO_MEMORY;
397         }
398
399         b->options = NULL;
400         b->flags = 0;
401         b->endpoint = NULL;
402
403         if (!options) {
404                 *b_out = b;
405                 return NT_STATUS_OK;
406         }
407
408         comma_count = count_chars(options, ',');
409
410         b->options = talloc_array(b, const char *, comma_count+2);
411         if (!b->options) {
412                 return NT_STATUS_NO_MEMORY;
413         }
414
415         for (i=0; (p = strchr(options, ',')); i++) {
416                 b->options[i] = talloc_strndup(b, options, PTR_DIFF(p, options));
417                 if (!b->options[i]) {
418                         return NT_STATUS_NO_MEMORY;
419                 }
420                 options = p+1;
421         }
422         b->options[i] = options;
423         b->options[i+1] = NULL;
424
425         /* some options are pre-parsed for convenience */
426         for (i=0;b->options[i];i++) {
427                 for (j=0;j<ARRAY_SIZE(ncacn_options);j++) {
428                         if (strcasecmp(ncacn_options[j].name, b->options[i]) == 0) {
429                                 int k;
430                                 b->flags |= ncacn_options[j].flag;
431                                 for (k=i;b->options[k];k++) {
432                                         b->options[k] = b->options[k+1];
433                                 }
434                                 i--;
435                                 break;
436                         }
437                 }
438         }
439
440         if (b->options[0]) {
441                 /* Endpoint is first option */
442                 b->endpoint = b->options[0];
443                 if (strlen(b->endpoint) == 0) b->endpoint = NULL;
444
445                 for (i=0;b->options[i];i++) {
446                         b->options[i] = b->options[i+1];
447                 }
448         }
449
450         if (b->options[0] == NULL)
451                 b->options = NULL;
452         
453         *b_out = b;
454         return NT_STATUS_OK;
455 }
456
457 NTSTATUS dcerpc_floor_get_lhs_data(struct epm_floor *floor, struct GUID *uuid, uint16_t *if_version)
458 {
459         TALLOC_CTX *mem_ctx = talloc_init("floor_get_lhs_data");
460         struct ndr_pull *ndr = ndr_pull_init_blob(&floor->lhs.lhs_data, mem_ctx);
461         NTSTATUS status;
462         
463         ndr->flags |= LIBNDR_FLAG_NOALIGN;
464
465         status = ndr_pull_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, uuid);
466         if (NT_STATUS_IS_ERR(status)) {
467                 talloc_free(mem_ctx);
468                 return status;
469         }
470
471         status = ndr_pull_uint16(ndr, NDR_SCALARS, if_version);
472
473         talloc_free(mem_ctx);
474
475         return status;
476 }
477
478 DATA_BLOB dcerpc_floor_pack_lhs_data(TALLOC_CTX *mem_ctx, struct GUID *uuid, uint32_t if_version)
479 {
480         struct ndr_push *ndr = ndr_push_init_ctx(mem_ctx);
481
482         ndr->flags |= LIBNDR_FLAG_NOALIGN;
483
484         ndr_push_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, uuid);
485         ndr_push_uint16(ndr, NDR_SCALARS, if_version);
486
487         return ndr_push_blob(ndr);
488 }
489
490 const char *dcerpc_floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *floor)
491 {
492         switch (floor->lhs.protocol) {
493         case EPM_PROTOCOL_TCP:
494                 if (floor->rhs.tcp.port == 0) return NULL;
495                 return talloc_asprintf(mem_ctx, "%d", floor->rhs.tcp.port);
496                 
497         case EPM_PROTOCOL_UDP:
498                 if (floor->rhs.udp.port == 0) return NULL;
499                 return talloc_asprintf(mem_ctx, "%d", floor->rhs.udp.port);
500
501         case EPM_PROTOCOL_HTTP:
502                 if (floor->rhs.http.port == 0) return NULL;
503                 return talloc_asprintf(mem_ctx, "%d", floor->rhs.http.port);
504
505         case EPM_PROTOCOL_IP:
506                 return talloc_strdup(mem_ctx, floor->rhs.ip.ipaddr);
507
508         case EPM_PROTOCOL_NCACN:
509                 return NULL;
510
511         case EPM_PROTOCOL_NCADG:
512                 return NULL;
513
514         case EPM_PROTOCOL_SMB:
515                 if (strlen(floor->rhs.smb.unc) == 0) return NULL;
516                 return talloc_strdup(mem_ctx, floor->rhs.smb.unc);
517
518         case EPM_PROTOCOL_PIPE:
519                 if (strlen(floor->rhs.pipe.path) == 0) return NULL;
520                 return talloc_strdup(mem_ctx, floor->rhs.pipe.path);
521
522         case EPM_PROTOCOL_NETBIOS:
523                 if (strlen(floor->rhs.netbios.name) == 0) return NULL;
524                 return talloc_strdup(mem_ctx, floor->rhs.netbios.name);
525
526         case EPM_PROTOCOL_NCALRPC:
527                 return NULL;
528                 
529         case EPM_PROTOCOL_VINES_SPP:
530                 return talloc_asprintf(mem_ctx, "%d", floor->rhs.vines_spp.port);
531                 
532         case EPM_PROTOCOL_VINES_IPC:
533                 return talloc_asprintf(mem_ctx, "%d", floor->rhs.vines_ipc.port);
534                 
535         case EPM_PROTOCOL_STREETTALK:
536                 return talloc_strdup(mem_ctx, floor->rhs.streettalk.streettalk);
537                 
538         case EPM_PROTOCOL_UNIX_DS:
539                 if (strlen(floor->rhs.unix_ds.path) == 0) return NULL;
540                 return talloc_strdup(mem_ctx, floor->rhs.unix_ds.path);
541                 
542         case EPM_PROTOCOL_NULL:
543                 return NULL;
544
545         default:
546                 DEBUG(0,("Unsupported lhs protocol %d\n", floor->lhs.protocol));
547                 break;
548         }
549
550         return NULL;
551 }
552
553 static NTSTATUS dcerpc_floor_set_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *floor,  const char *data)
554 {
555         switch (floor->lhs.protocol) {
556         case EPM_PROTOCOL_TCP:
557                 floor->rhs.tcp.port = atoi(data);
558                 return NT_STATUS_OK;
559                 
560         case EPM_PROTOCOL_UDP:
561                 floor->rhs.udp.port = atoi(data);
562                 return NT_STATUS_OK;
563
564         case EPM_PROTOCOL_HTTP:
565                 floor->rhs.http.port = atoi(data);
566                 return NT_STATUS_OK;
567
568         case EPM_PROTOCOL_IP:
569                 floor->rhs.ip.ipaddr = talloc_strdup(mem_ctx, data);
570                 NT_STATUS_HAVE_NO_MEMORY(floor->rhs.ip.ipaddr);
571                 return NT_STATUS_OK;
572
573         case EPM_PROTOCOL_NCACN:
574                 floor->rhs.ncacn.minor_version = 0;
575                 return NT_STATUS_OK;
576
577         case EPM_PROTOCOL_NCADG:
578                 floor->rhs.ncadg.minor_version = 0;
579                 return NT_STATUS_OK;
580
581         case EPM_PROTOCOL_SMB:
582                 floor->rhs.smb.unc = talloc_strdup(mem_ctx, data);
583                 NT_STATUS_HAVE_NO_MEMORY(floor->rhs.smb.unc);
584                 return NT_STATUS_OK;
585
586         case EPM_PROTOCOL_PIPE:
587                 floor->rhs.pipe.path = talloc_strdup(mem_ctx, data);
588                 NT_STATUS_HAVE_NO_MEMORY(floor->rhs.pipe.path);
589                 return NT_STATUS_OK;
590
591         case EPM_PROTOCOL_NETBIOS:
592                 floor->rhs.netbios.name = talloc_strdup(mem_ctx, data);
593                 NT_STATUS_HAVE_NO_MEMORY(floor->rhs.netbios.name);
594                 return NT_STATUS_OK;
595
596         case EPM_PROTOCOL_NCALRPC:
597                 return NT_STATUS_OK;
598                 
599         case EPM_PROTOCOL_VINES_SPP:
600                 floor->rhs.vines_spp.port = atoi(data);
601                 return NT_STATUS_OK;
602                 
603         case EPM_PROTOCOL_VINES_IPC:
604                 floor->rhs.vines_ipc.port = atoi(data);
605                 return NT_STATUS_OK;
606                 
607         case EPM_PROTOCOL_STREETTALK:
608                 floor->rhs.streettalk.streettalk = talloc_strdup(mem_ctx, data);
609                 NT_STATUS_HAVE_NO_MEMORY(floor->rhs.streettalk.streettalk);
610                 return NT_STATUS_OK;
611                 
612         case EPM_PROTOCOL_UNIX_DS:
613                 floor->rhs.unix_ds.path = talloc_strdup(mem_ctx, data);
614                 NT_STATUS_HAVE_NO_MEMORY(floor->rhs.unix_ds.path);
615                 return NT_STATUS_OK;
616                 
617         case EPM_PROTOCOL_NULL:
618                 return NT_STATUS_OK;
619
620         default:
621                 DEBUG(0,("Unsupported lhs protocol %d\n", floor->lhs.protocol));
622                 break;
623         }
624
625         return NT_STATUS_NOT_SUPPORTED;
626 }
627
628 enum dcerpc_transport_t dcerpc_transport_by_endpoint_protocol(int prot)
629 {
630         int i;
631
632         /* Find a transport that has 'prot' as 4th protocol */
633         for (i=0;i<ARRAY_SIZE(transports);i++) {
634                 if (transports[i].num_protocols >= 2 && 
635                         transports[i].protseq[1] == prot) {
636                         return transports[i].transport;
637                 }
638         }
639         
640         /* Unknown transport */
641         return -1;
642 }
643
644 enum dcerpc_transport_t dcerpc_transport_by_tower(struct epm_tower *tower)
645 {
646         int i;
647
648         /* Find a transport that matches this tower */
649         for (i=0;i<ARRAY_SIZE(transports);i++) {
650                 int j;
651                 if (transports[i].num_protocols != tower->num_floors - 2) {
652                         continue; 
653                 }
654
655                 for (j = 0; j < transports[i].num_protocols; j++) {
656                         if (transports[i].protseq[j] != tower->floors[j+2].lhs.protocol) {
657                                 break;
658                         }
659                 }
660
661                 if (j == transports[i].num_protocols) {
662                         return transports[i].transport;
663                 }
664         }
665         
666         /* Unknown transport */
667         return -1;
668 }
669
670 NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx, struct epm_tower *tower, struct dcerpc_binding **b_out)
671 {
672         NTSTATUS status;
673         struct dcerpc_binding *binding;
674
675         binding = talloc(mem_ctx, struct dcerpc_binding);
676         NT_STATUS_HAVE_NO_MEMORY(binding);
677
678         ZERO_STRUCT(binding->object);
679         binding->options = NULL;
680         binding->host = NULL;
681         binding->flags = 0;
682
683         binding->transport = dcerpc_transport_by_tower(tower);
684
685         if (binding->transport == -1) {
686                 return NT_STATUS_NOT_SUPPORTED;
687         }
688
689         if (tower->num_floors < 1) {
690                 return NT_STATUS_OK;
691         }
692
693         /* Set object uuid */
694         status = dcerpc_floor_get_lhs_data(&tower->floors[0], &binding->object, &binding->object_version);
695         
696         if (!NT_STATUS_IS_OK(status)) {
697                 DEBUG(1, ("Error pulling object uuid and version: %s", nt_errstr(status)));     
698                 return status;
699         }
700
701         /* Ignore floor 1, it contains the NDR version info */
702         
703         binding->options = NULL;
704
705         /* Set endpoint */
706         if (tower->num_floors >= 4) {
707                 binding->endpoint = dcerpc_floor_get_rhs_data(mem_ctx, &tower->floors[3]);
708         } else {
709                 binding->endpoint = NULL;
710         }
711
712         /* Set network address */
713         if (tower->num_floors >= 5) {
714                 binding->host = dcerpc_floor_get_rhs_data(mem_ctx, &tower->floors[4]);
715         }
716         *b_out = binding;
717         return NT_STATUS_OK;
718 }
719
720 NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx, struct dcerpc_binding *binding, struct epm_tower *tower)
721 {
722         const enum epm_protocol *protseq = NULL;
723         int num_protocols = -1, i;
724         struct GUID ndr_guid;
725         NTSTATUS status;
726         
727         /* Find transport */
728         for (i=0;i<ARRAY_SIZE(transports);i++) {
729                 if (transports[i].transport == binding->transport) {
730                         protseq = transports[i].protseq;
731                         num_protocols = transports[i].num_protocols;
732                         break;
733                 }
734         }
735
736         if (num_protocols == -1) {
737                 DEBUG(0, ("Unable to find transport with id '%d'\n", binding->transport));
738                 return NT_STATUS_UNSUCCESSFUL;
739         }
740
741         tower->num_floors = 2 + num_protocols;
742         tower->floors = talloc_array(mem_ctx, struct epm_floor, tower->num_floors);
743
744         /* Floor 0 */
745         tower->floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
746
747         tower->floors[0].lhs.lhs_data = dcerpc_floor_pack_lhs_data(mem_ctx, &binding->object, binding->object_version);
748
749         tower->floors[0].rhs.uuid.unknown = data_blob_talloc_zero(mem_ctx, 2);
750         
751         /* Floor 1 */
752         tower->floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
753
754         status = GUID_from_string(NDR_GUID, &ndr_guid);
755         if (NT_STATUS_IS_ERR(status)) {
756                 return status;
757         }
758
759         tower->floors[1].lhs.lhs_data = dcerpc_floor_pack_lhs_data(mem_ctx, &ndr_guid, NDR_GUID_VERSION);
760         
761         tower->floors[1].rhs.uuid.unknown = data_blob_talloc_zero(mem_ctx, 2);
762         
763         /* Floor 2 to num_protocols */
764         for (i = 0; i < num_protocols; i++) {
765                 tower->floors[2 + i].lhs.protocol = protseq[i];
766                 tower->floors[2 + i].lhs.lhs_data = data_blob_talloc(mem_ctx, NULL, 0);
767                 ZERO_STRUCT(tower->floors[2 + i].rhs);
768                 dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[2 + i], "");
769         }
770
771         /* The 4th floor contains the endpoint */
772         if (num_protocols >= 2 && binding->endpoint) {
773                 status = dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[3], binding->endpoint);
774                 if (NT_STATUS_IS_ERR(status)) {
775                         return status;
776                 }
777         }
778         
779         /* The 5th contains the network address */
780         if (num_protocols >= 3 && binding->host) {
781                 status = dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[4], binding->host);
782                 if (NT_STATUS_IS_ERR(status)) {
783                         return status;
784                 }
785         }
786
787         return NT_STATUS_OK;
788 }
789
790 NTSTATUS dcerpc_epm_map_binding(TALLOC_CTX *mem_ctx, struct dcerpc_binding *binding,
791                                 const char *uuid, uint_t version)
792 {
793         struct dcerpc_pipe *p;
794         NTSTATUS status;
795         struct epm_Map r;
796         struct policy_handle handle;
797         struct GUID guid;
798         struct epm_twr_t twr, *twr_r;
799         struct dcerpc_binding *epmapper_binding;
800         const struct dcerpc_interface_table *table = idl_iface_by_uuid(uuid);
801         int i;
802
803         struct cli_credentials *anon_creds
804                 = cli_credentials_init(mem_ctx);
805         cli_credentials_set_anonymous(anon_creds);
806         cli_credentials_guess(anon_creds);
807
808         /* First, check if there is a default endpoint specified in the IDL */
809
810         if (table) {
811                 struct dcerpc_binding *default_binding;
812
813                 binding->authservice = talloc_strdup(binding, table->authservices->names[0]);
814
815                 /* Find one of the default pipes for this interface */
816                 for (i = 0; i < table->endpoints->count; i++) {
817                         status = dcerpc_parse_binding(mem_ctx, table->endpoints->names[i], &default_binding);
818
819                         if (NT_STATUS_IS_OK(status)) {
820                                 if (default_binding->transport == binding->transport && default_binding->endpoint) {
821                                         binding->endpoint = talloc_reference(binding, default_binding->endpoint);
822                                         talloc_free(default_binding);
823                                         return NT_STATUS_OK;
824                                 } else {
825                                         talloc_free(default_binding);
826                                 }
827                         }
828                 }
829         }
830
831         epmapper_binding = talloc_zero(mem_ctx, struct dcerpc_binding);
832         if (!epmapper_binding) {
833                 return NT_STATUS_NO_MEMORY;
834         }
835
836         epmapper_binding->transport = binding->transport;
837         epmapper_binding->host = talloc_reference(epmapper_binding, 
838                                                   binding->host);
839         epmapper_binding->options = NULL;
840         epmapper_binding->flags = 0;
841         epmapper_binding->endpoint = NULL;
842         epmapper_binding->authservice = NULL;
843         
844         status = dcerpc_pipe_connect_b(mem_ctx, 
845                                        &p,
846                                        epmapper_binding,
847                                        DCERPC_EPMAPPER_UUID,
848                                        DCERPC_EPMAPPER_VERSION,
849                                        anon_creds);
850
851         if (!NT_STATUS_IS_OK(status)) {
852                 return status;
853         }
854
855         ZERO_STRUCT(handle);
856         ZERO_STRUCT(guid);
857
858         status = GUID_from_string(uuid, &binding->object);
859         if (NT_STATUS_IS_ERR(status)) {
860                 return status;
861         }
862
863         binding->object_version = version;
864
865         status = dcerpc_binding_build_tower(p, binding, &twr.tower);
866         if (NT_STATUS_IS_ERR(status)) {
867                 return status;
868         }
869
870         /* with some nice pretty paper around it of course */
871         r.in.object = &guid;
872         r.in.map_tower = &twr;
873         r.in.entry_handle = &handle;
874         r.in.max_towers = 1;
875         r.out.entry_handle = &handle;
876
877         status = dcerpc_epm_Map(p, p, &r);
878         if (!NT_STATUS_IS_OK(status)) {
879                 talloc_free(p);
880                 return status;
881         }
882         if (r.out.result != 0 || r.out.num_towers != 1) {
883                 talloc_free(p);
884                 return NT_STATUS_PORT_UNREACHABLE;
885         }
886
887         twr_r = r.out.towers[0].twr;
888         if (!twr_r) {
889                 talloc_free(p);
890                 return NT_STATUS_PORT_UNREACHABLE;
891         }
892
893         if (twr_r->tower.num_floors != twr.tower.num_floors ||
894             twr_r->tower.floors[3].lhs.protocol != twr.tower.floors[3].lhs.protocol) {
895                 talloc_free(p);
896                 return NT_STATUS_PORT_UNREACHABLE;
897         }
898
899         binding->endpoint = talloc_reference(binding, dcerpc_floor_get_rhs_data(mem_ctx, &twr_r->tower.floors[3]));
900
901         talloc_free(p);
902
903         return NT_STATUS_OK;
904 }
905
906
907 /* 
908    perform an authenticated bind if needed
909 */
910 NTSTATUS dcerpc_pipe_auth(struct dcerpc_pipe *p, 
911                           struct dcerpc_binding *binding,
912                           const char *pipe_uuid, 
913                           uint32_t pipe_version,
914                           struct cli_credentials *credentials)
915 {
916         NTSTATUS status;
917         TALLOC_CTX *tmp_ctx;
918         tmp_ctx = talloc_new(p);
919
920         p->conn->flags = binding->flags;
921
922         /* remember the binding string for possible secondary connections */
923         p->conn->binding_string = dcerpc_binding_string(p, binding);
924
925         if (!cli_credentials_is_anonymous(credentials) &&
926                 (binding->flags & DCERPC_SCHANNEL_ANY)) {
927                 status = dcerpc_bind_auth_schannel(tmp_ctx, 
928                                                    p, pipe_uuid, pipe_version, 
929                                                    credentials);
930         } else if (!cli_credentials_is_anonymous(credentials)) {
931                 uint8_t auth_type;
932                 if (binding->flags & DCERPC_AUTH_SPNEGO) {
933                         auth_type = DCERPC_AUTH_TYPE_SPNEGO;
934                 } else if (binding->flags & DCERPC_AUTH_KRB5) {
935                         auth_type = DCERPC_AUTH_TYPE_KRB5;
936                 } else {
937                         auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
938                 }
939
940                 status = dcerpc_bind_auth_password(p, pipe_uuid, pipe_version, 
941                                                    credentials, auth_type,
942                                                    binding->authservice);
943         } else {
944                 status = dcerpc_bind_auth_none(p, pipe_uuid, pipe_version);
945         }
946
947         if (!NT_STATUS_IS_OK(status)) {
948                 DEBUG(0,("Failed to bind to uuid %s - %s\n", pipe_uuid, nt_errstr(status)));
949         }
950         talloc_free(tmp_ctx);
951         return status;
952 }
953
954
955 /* open a rpc connection to a rpc pipe on SMB using the binding
956    structure to determine the endpoint and options */
957 static NTSTATUS dcerpc_pipe_connect_ncacn_np(TALLOC_CTX *tmp_ctx, 
958                                              struct dcerpc_pipe *p, 
959                                              struct dcerpc_binding *binding,
960                                              const char *pipe_uuid, 
961                                              uint32_t pipe_version,
962                                              struct cli_credentials *credentials)
963 {
964         NTSTATUS status;
965         struct smbcli_state *cli;
966         const char *pipe_name = NULL;
967
968         if (binding->flags & DCERPC_SCHANNEL_ANY) {
969                 struct cli_credentials *anon_creds
970                         = cli_credentials_init(tmp_ctx);
971                 cli_credentials_set_anonymous(anon_creds);
972                 cli_credentials_guess(anon_creds);
973                 status = smbcli_full_connection(p->conn, &cli, 
974                                                 cli_credentials_get_workstation(credentials),
975                                                 binding->host, 
976                                                 "IPC$", NULL, 
977                                                 anon_creds);
978         } else {
979                 status = smbcli_full_connection(p->conn, &cli, 
980                                                 cli_credentials_get_workstation(credentials),
981                                                 binding->host, 
982                                                 "IPC$", NULL,
983                                                 credentials);
984         }
985         if (!NT_STATUS_IS_OK(status)) {
986                 DEBUG(0,("Failed to connect to %s - %s\n", binding->host, nt_errstr(status)));
987                 return status;
988         }
989
990         /* Look up identifier using the epmapper */
991         if (!binding->endpoint) {
992                 status = dcerpc_epm_map_binding(tmp_ctx, binding, pipe_uuid, pipe_version);
993                 if (!NT_STATUS_IS_OK(status)) {
994                         DEBUG(0,("Failed to map DCERPC/TCP NCACN_NP pipe for '%s' - %s\n", 
995                                  pipe_uuid, nt_errstr(status)));
996                         return status;
997                 }
998                 DEBUG(1,("Mapped to DCERPC/NP pipe %s\n", binding->endpoint));
999         }
1000
1001         pipe_name = binding->endpoint;
1002
1003         status = dcerpc_pipe_open_smb(p->conn, cli->tree, pipe_name);
1004         if (!NT_STATUS_IS_OK(status)) {
1005                 DEBUG(0,("Failed to open pipe %s - %s\n", pipe_name, nt_errstr(status)));
1006                 return status;
1007         }
1008
1009         return NT_STATUS_OK;
1010 }
1011
1012 /* open a rpc connection to a rpc pipe on SMP using the binding
1013    structure to determine the endpoint and options */
1014 static NTSTATUS dcerpc_pipe_connect_ncalrpc(TALLOC_CTX *tmp_ctx, 
1015                                             struct dcerpc_pipe *p, 
1016                                             struct dcerpc_binding *binding,
1017                                             const char *pipe_uuid, 
1018                                             uint32_t pipe_version)
1019 {
1020         NTSTATUS status;
1021
1022         /* Look up identifier using the epmapper */
1023         if (!binding->endpoint) {
1024                 status = dcerpc_epm_map_binding(tmp_ctx, binding, pipe_uuid, pipe_version);
1025                 if (!NT_STATUS_IS_OK(status)) {
1026                         DEBUG(0,("Failed to map DCERPC/TCP NCALRPC identifier for '%s' - %s\n", 
1027                                  pipe_uuid, nt_errstr(status)));
1028                         return status;
1029                 }
1030                 DEBUG(1,("Mapped to DCERPC/LRPC identifier %s\n", binding->endpoint));
1031         }
1032
1033         status = dcerpc_pipe_open_pipe(p->conn, binding->endpoint);
1034         if (!NT_STATUS_IS_OK(status)) {
1035                 DEBUG(0,("Failed to open ncalrpc pipe '%s' - %s\n", 
1036                          binding->endpoint, nt_errstr(status)));
1037                 return status;
1038         }
1039
1040         return status;
1041 }
1042
1043
1044
1045 /* open a rpc connection to a rpc pipe on SMP using the binding
1046    structure to determine the endpoint and options */
1047 static NTSTATUS dcerpc_pipe_connect_ncacn_unix_stream(TALLOC_CTX *tmp_ctx, 
1048                                                       struct dcerpc_pipe *p, 
1049                                                       struct dcerpc_binding *binding,
1050                                                       const char *pipe_uuid, 
1051                                                       uint32_t pipe_version)
1052 {
1053         NTSTATUS status;
1054
1055         if (!binding->endpoint) {
1056                 DEBUG(0, ("Path to unix socket not specified\n"));
1057                 return NT_STATUS_INVALID_PARAMETER;
1058         }
1059
1060         status = dcerpc_pipe_open_unix_stream(p->conn, binding->endpoint);
1061         if (!NT_STATUS_IS_OK(status)) {
1062                 DEBUG(0,("Failed to open unix socket %s - %s\n", 
1063                          binding->endpoint, nt_errstr(status)));
1064                 talloc_free(p);
1065                 return status;
1066         }
1067
1068         return status;
1069 }
1070
1071 /* open a rpc connection to a rpc pipe on TCP/IP sockets using the binding
1072    structure to determine the endpoint and options */
1073 static NTSTATUS dcerpc_pipe_connect_ncacn_ip_tcp(TALLOC_CTX *tmp_ctx, 
1074                                                  struct dcerpc_pipe *p, 
1075                                                  struct dcerpc_binding *binding,
1076                                                  const char *pipe_uuid, 
1077                                                  uint32_t pipe_version)
1078 {
1079         NTSTATUS status;
1080         uint32_t port = 0;
1081
1082         if (!binding->endpoint) {
1083                 status = dcerpc_epm_map_binding(tmp_ctx, binding, 
1084                                                 pipe_uuid, pipe_version);
1085                 if (!NT_STATUS_IS_OK(status)) {
1086                         DEBUG(0,("Failed to map DCERPC/TCP port for '%s' - %s\n", 
1087                                  pipe_uuid, nt_errstr(status)));
1088                         return status;
1089                 }
1090                 DEBUG(1,("Mapped to DCERPC/TCP port %s\n", binding->endpoint));
1091         }
1092
1093         port = atoi(binding->endpoint);
1094
1095         status = dcerpc_pipe_open_tcp(p->conn, binding->host, port);
1096         if (!NT_STATUS_IS_OK(status)) {
1097                 DEBUG(0,("Failed to connect to %s:%d - %s\n", 
1098                          binding->host, port, nt_errstr(status)));
1099                 return status;
1100         }
1101
1102         return status;
1103 }
1104
1105
1106 /* open a rpc connection to a rpc pipe, using the specified 
1107    binding structure to determine the endpoint and options */
1108 NTSTATUS dcerpc_pipe_connect_b(TALLOC_CTX *parent_ctx, 
1109                                struct dcerpc_pipe **pp, 
1110                                struct dcerpc_binding *binding,
1111                                const char *pipe_uuid, 
1112                                uint32_t pipe_version,
1113                                struct cli_credentials *credentials)
1114 {
1115         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
1116         struct dcerpc_pipe *p; 
1117         
1118         TALLOC_CTX *tmp_ctx;
1119
1120         (*pp) = NULL;
1121
1122         p = dcerpc_pipe_init(parent_ctx);
1123         if (p == NULL) {
1124                 return NT_STATUS_NO_MEMORY;
1125         }
1126         tmp_ctx = talloc_named(p, 0, "dcerpc_pipe_connect_b tmp_ctx");
1127
1128         switch (binding->transport) {
1129         case NCACN_NP:
1130                 status = dcerpc_pipe_connect_ncacn_np(tmp_ctx, 
1131                                                       p, binding, pipe_uuid, pipe_version, credentials);
1132                 break;
1133         case NCACN_IP_TCP:
1134                 status = dcerpc_pipe_connect_ncacn_ip_tcp(tmp_ctx, 
1135                                                           p, binding, pipe_uuid, pipe_version);
1136                 break;
1137         case NCACN_UNIX_STREAM:
1138                 status = dcerpc_pipe_connect_ncacn_unix_stream(tmp_ctx, 
1139                                                                p, binding, pipe_uuid, pipe_version);
1140                 break;
1141         case NCALRPC:
1142                 status = dcerpc_pipe_connect_ncalrpc(tmp_ctx, 
1143                                                      p, binding, pipe_uuid, pipe_version);
1144                 break;
1145         default:
1146                 return NT_STATUS_NOT_SUPPORTED;
1147         }
1148
1149         if (!NT_STATUS_IS_OK(status)) {
1150                 talloc_free(p);
1151                 return status;
1152         }
1153
1154         status = dcerpc_pipe_auth(p, binding, pipe_uuid, pipe_version, credentials);
1155         if (!NT_STATUS_IS_OK(status)) {
1156                 talloc_free(p);
1157                 return status;
1158         }
1159         *pp = p;
1160         talloc_free(tmp_ctx);
1161
1162         return status;
1163 }
1164
1165
1166 /* open a rpc connection to a rpc pipe, using the specified string
1167    binding to determine the endpoint and options */
1168 NTSTATUS dcerpc_pipe_connect(TALLOC_CTX *parent_ctx, 
1169                              struct dcerpc_pipe **pp, 
1170                              const char *binding,
1171                              const char *pipe_uuid, 
1172                              uint32_t pipe_version,
1173                              struct cli_credentials *credentials)
1174 {
1175         struct dcerpc_binding *b;
1176         NTSTATUS status;
1177         TALLOC_CTX *tmp_ctx;
1178
1179         tmp_ctx = talloc_named(parent_ctx, 0, "dcerpc_pipe_connect tmp_ctx");
1180         if (!tmp_ctx) {
1181                 return NT_STATUS_NO_MEMORY;
1182         }
1183
1184         status = dcerpc_parse_binding(tmp_ctx, binding, &b);
1185         if (!NT_STATUS_IS_OK(status)) {
1186                 DEBUG(0,("Failed to parse dcerpc binding '%s'\n", binding));
1187                 talloc_free(tmp_ctx);
1188                 return status;
1189         }
1190
1191         DEBUG(3,("Using binding %s\n", dcerpc_binding_string(tmp_ctx, b)));
1192
1193         status = dcerpc_pipe_connect_b(tmp_ctx,
1194                                        pp, b, pipe_uuid, pipe_version, credentials);
1195
1196         if (NT_STATUS_IS_OK(status)) {
1197                 *pp = talloc_reference(parent_ctx, *pp);
1198         }
1199         talloc_free(tmp_ctx);
1200
1201         return status;
1202 }
1203
1204
1205 /*
1206   create a secondary dcerpc connection from a primary connection
1207
1208   if the primary is a SMB connection then the secondary connection
1209   will be on the same SMB connection, but use a new fnum
1210 */
1211 NTSTATUS dcerpc_secondary_connection(struct dcerpc_pipe *p, struct dcerpc_pipe **p2,
1212                                      struct dcerpc_binding *b)
1213
1214
1215 {
1216         struct smbcli_tree *tree;
1217         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
1218
1219         (*p2) = dcerpc_pipe_init(p);
1220         if (*p2 == NULL) {
1221                 return NT_STATUS_NO_MEMORY;
1222         }
1223         
1224         switch (p->conn->transport.transport) {
1225         case NCACN_NP:
1226                 tree = dcerpc_smb_tree(p->conn);
1227                 if (!tree) {
1228                         return NT_STATUS_INVALID_PARAMETER;
1229                 }
1230                 status = dcerpc_pipe_open_smb((*p2)->conn, tree, b->endpoint);
1231                 break;
1232
1233         case NCACN_IP_TCP:
1234                 status = dcerpc_pipe_open_tcp((*p2)->conn, b->host, atoi(b->endpoint));
1235                 break;
1236
1237         case NCALRPC:
1238                 status = dcerpc_pipe_open_pipe((*p2)->conn, b->endpoint);
1239                 break;
1240
1241         default:
1242                 return NT_STATUS_NOT_SUPPORTED;
1243         }
1244
1245         if (!NT_STATUS_IS_OK(status)) {
1246                 talloc_free(*p2);
1247                 return status;
1248         }
1249
1250         (*p2)->conn->flags = p->conn->flags;
1251
1252         return NT_STATUS_OK;
1253 }
1254
1255 NTSTATUS dcerpc_generic_session_key(struct dcerpc_connection *c,
1256                                     DATA_BLOB *session_key)
1257 {
1258         /* this took quite a few CPU cycles to find ... */
1259         session_key->data = discard_const_p(unsigned char, "SystemLibraryDTC");
1260         session_key->length = 16;
1261         return NT_STATUS_OK;
1262 }
1263
1264 /*
1265   fetch the user session key - may be default (above) or the SMB session key
1266 */
1267 NTSTATUS dcerpc_fetch_session_key(struct dcerpc_pipe *p,
1268                                   DATA_BLOB *session_key)
1269 {
1270         return p->conn->security_state.session_key(p->conn, session_key);
1271 }
1272
1273
1274 /*
1275   log a rpc packet in a format suitable for ndrdump. This is especially useful
1276   for sealed packets, where ethereal cannot easily see the contents
1277
1278   this triggers on a debug level of >= 10
1279 */
1280 void dcerpc_log_packet(const struct dcerpc_interface_table *ndr,
1281                        uint32_t opnum, uint32_t flags, DATA_BLOB *pkt)
1282 {
1283         const int num_examples = 20;
1284         int i;
1285
1286         if (DEBUGLEVEL < 10) return;
1287
1288         for (i=0;i<num_examples;i++) {
1289                 char *name=NULL;
1290                 asprintf(&name, "%s/rpclog/%s-%u.%d.%s", 
1291                          lp_lockdir(), ndr->name, opnum, i,
1292                          (flags&NDR_IN)?"in":"out");
1293                 if (name == NULL) {
1294                         return;
1295                 }
1296                 if (!file_exist(name)) {
1297                         if (file_save(name, pkt->data, pkt->length)) {
1298                                 DEBUG(10,("Logged rpc packet to %s\n", name));
1299                         }
1300                         free(name);
1301                         break;
1302                 }
1303                 free(name);
1304         }
1305 }
1306
1307
1308
1309 /*
1310   create a secondary context from a primary connection
1311
1312   this uses dcerpc_alter_context() to create a new dcerpc context_id
1313 */
1314 NTSTATUS dcerpc_secondary_context(struct dcerpc_pipe *p, 
1315                                   struct dcerpc_pipe **pp2,
1316                                   const char *pipe_uuid,
1317                                   uint32_t pipe_version)
1318 {
1319         NTSTATUS status;
1320         struct dcerpc_pipe *p2;
1321         
1322         p2 = talloc_zero(p, struct dcerpc_pipe);
1323         if (p2 == NULL) {
1324                 return NT_STATUS_NO_MEMORY;
1325         }
1326         p2->conn = talloc_reference(p2, p->conn);
1327
1328         p2->context_id = ++p->conn->next_context_id;
1329
1330         status = GUID_from_string(pipe_uuid, &p2->syntax.uuid);
1331         if (!NT_STATUS_IS_OK(status)) {
1332                 talloc_free(p2);
1333                 return status;
1334         }
1335         p2->syntax.if_version = pipe_version;
1336
1337         status = GUID_from_string(NDR_GUID, &p2->transfer_syntax.uuid);
1338         if (!NT_STATUS_IS_OK(status)) {
1339                 talloc_free(p2);
1340                 return status;
1341         }
1342         p2->transfer_syntax.if_version = NDR_GUID_VERSION;
1343
1344         status = dcerpc_alter_context(p2, p2, &p2->syntax, &p2->transfer_syntax);
1345         if (!NT_STATUS_IS_OK(status)) {
1346                 talloc_free(p2);
1347                 return status;
1348         }
1349
1350         *pp2 = p2;
1351
1352         return status;
1353 }