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