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