r4616: the first phase in the addition of proper support for
[kai/samba-autobuild/.git] / source4 / librpc / rpc / dcerpc_util.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    dcerpc utility functions
5
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) Jelmer Vernooij 2004
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "system/network.h"
26 #include "librpc/gen_ndr/ndr_epmapper.h"
27
28 /*
29   find the pipe name for a local IDL interface
30 */
31 const char *idl_pipe_name(const char *uuid, uint32_t if_version)
32 {
33         const struct dcerpc_interface_list *l;
34         for (l=librpc_dcerpc_pipes();l;l=l->next) {
35                 if (strcasecmp(l->table->uuid, uuid) == 0 &&
36                     l->table->if_version == if_version) {
37                         return l->table->name;
38                 }
39         }
40         return "UNKNOWN";
41 }
42
43 /*
44   find the number of calls defined by local IDL
45 */
46 int idl_num_calls(const char *uuid, uint32_t if_version)
47 {
48         const struct dcerpc_interface_list *l;
49         for (l=librpc_dcerpc_pipes();l;l=l->next){
50                 if (strcasecmp(l->table->uuid, uuid) == 0 &&
51                     l->table->if_version == if_version) {
52                         return l->table->num_calls;
53                 }
54         }
55         return -1;
56 }
57
58
59 /*
60   find a dcerpc interface by name
61 */
62 const struct dcerpc_interface_table *idl_iface_by_name(const char *name)
63 {
64         const struct dcerpc_interface_list *l;
65         for (l=librpc_dcerpc_pipes();l;l=l->next) {
66                 if (strcasecmp(l->table->name, name) == 0) {
67                         return l->table;
68                 }
69         }
70         return NULL;
71 }
72
73 /*
74   find a dcerpc interface by uuid
75 */
76 const struct dcerpc_interface_table *idl_iface_by_uuid(const char *uuid)
77 {
78         const struct dcerpc_interface_list *l;
79         for (l=librpc_dcerpc_pipes();l;l=l->next) {
80                 if (strcasecmp(l->table->uuid, uuid) == 0) {
81                         return l->table;
82                 }
83         }
84         return NULL;
85 }
86
87
88 /* 
89    push a dcerpc_packet into a blob, potentially with auth info
90 */
91 NTSTATUS dcerpc_push_auth(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, 
92                           struct dcerpc_packet *pkt,
93                           struct dcerpc_auth *auth_info)
94 {
95         NTSTATUS status;
96         struct ndr_push *ndr;
97
98         ndr = ndr_push_init_ctx(mem_ctx);
99         if (!ndr) {
100                 return NT_STATUS_NO_MEMORY;
101         }
102
103         if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
104                 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
105         }
106
107         if (pkt->pfc_flags & DCERPC_PFC_FLAG_ORPC) {
108                 ndr->flags |= LIBNDR_FLAG_OBJECT_PRESENT;
109         }
110
111         if (auth_info) {
112                 pkt->auth_length = auth_info->credentials.length;
113         } else {
114                 pkt->auth_length = 0;
115         }
116
117         status = ndr_push_dcerpc_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
118         if (!NT_STATUS_IS_OK(status)) {
119                 return status;
120         }
121
122         if (auth_info) {
123                 status = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, auth_info);
124         }
125
126         *blob = ndr_push_blob(ndr);
127
128         /* fill in the frag length */
129         dcerpc_set_frag_length(blob, blob->length);
130
131         return NT_STATUS_OK;
132 }
133
134 #define MAX_PROTSEQ             10
135
136 static const struct {
137         const char *name;
138         enum dcerpc_transport_t transport;
139         int num_protocols;
140         enum epm_protocol protseq[MAX_PROTSEQ];
141 } transports[] = {
142         { "ncacn_np",     NCACN_NP, 3, 
143                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SMB, EPM_PROTOCOL_NETBIOS }},
144         { "ncacn_ip_tcp", NCACN_IP_TCP, 3, 
145                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_TCP, EPM_PROTOCOL_IP } }, 
146         { "ncacn_http", NCACN_HTTP, 3, 
147                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_HTTP, EPM_PROTOCOL_IP } }, 
148         { "ncadg_ip_udp", NCACN_IP_UDP, 3, 
149                 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UDP, EPM_PROTOCOL_IP } },
150         { "ncalrpc", NCALRPC, 2, 
151                 { EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_PIPE } },
152         { "ncacn_unix_stream", NCACN_UNIX_STREAM, 2, 
153                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_UNIX_DS } },
154         { "ncadg_unix_dgram", NCADG_UNIX_DGRAM, 2, 
155                 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UNIX_DS } },
156         { "ncacn_at_dsp", NCACN_AT_DSP, 3, 
157                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DSP } },
158         { "ncadg_at_ddp", NCADG_AT_DDP, 3, 
159                 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DDP } },
160         { "ncacn_vns_ssp", NCACN_VNS_SPP, 3, 
161                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_SPP } },
162         { "ncacn_vns_ipc", NCACN_VNS_IPC, 3, 
163                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_IPC }, },
164         { "ncadg_ipx", NCADG_IPX, 2,
165                 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_IPX },
166         },
167         { "ncacn_spx", NCACN_SPX, 2,
168                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SPX },
169         },
170 };
171
172 static const struct {
173         const char *name;
174         uint32_t flag;
175 } ncacn_options[] = {
176         {"sign", DCERPC_SIGN},
177         {"seal", DCERPC_SEAL},
178         {"connect", DCERPC_CONNECT},
179         {"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 **pp, 
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         struct dcerpc_pipe *p;
786         NTSTATUS status;
787         BOOL retry;
788         struct smbcli_state *cli;
789         const char *pipe_name = NULL;
790         TALLOC_CTX *tmp_ctx;
791
792         *pp = NULL;
793
794         p = dcerpc_pipe_init(NULL);
795         if (p == NULL) {
796                 return NT_STATUS_NO_MEMORY;
797         }
798         tmp_ctx = talloc_new(p);
799         
800         /* Look up identifier using the epmapper */
801         if (!binding->endpoint) {
802                 status = dcerpc_epm_map_binding(tmp_ctx, binding, pipe_uuid, pipe_version);
803                 if (!NT_STATUS_IS_OK(status)) {
804                         DEBUG(0,("Failed to map DCERPC/TCP NCACN_NP pipe for '%s' - %s\n", 
805                                  pipe_uuid, nt_errstr(status)));
806                         talloc_free(p);
807                         return status;
808                 }
809                 DEBUG(1,("Mapped to DCERPC/NP pipe %s\n", binding->endpoint));
810         }
811
812         pipe_name = binding->endpoint;
813
814         if (!strncasecmp(pipe_name, "/pipe/", 6) || 
815             !strncasecmp(pipe_name, "\\pipe\\", 6)) {
816                 pipe_name += 6;
817         }
818
819         if (pipe_name[0] != '\\') {
820                 pipe_name = talloc_asprintf(tmp_ctx, "\\%s", pipe_name);
821         }
822         
823         if (!username || !username[0] || 
824             (binding->flags & DCERPC_SCHANNEL_ANY)) {
825                 status = smbcli_full_connection(p->conn, &cli, lp_netbios_name(),
826                                                 binding->host, NULL, 
827                                                 "ipc$", "?????", 
828                                                 "", "", NULL, 0, &retry);
829         } else {
830                 status = smbcli_full_connection(p->conn, &cli, lp_netbios_name(),
831                                                 binding->host, NULL, 
832                                                 "ipc$", "?????", 
833                                                 username, domain,
834                                                 password, 0, &retry);
835         }
836         if (!NT_STATUS_IS_OK(status)) {
837                 DEBUG(0,("Failed to connect to %s - %s\n", binding->host, nt_errstr(status)));
838                 talloc_free(p);
839                 return status;
840         }
841
842         status = dcerpc_pipe_open_smb(p->conn, cli->tree, pipe_name);
843         if (!NT_STATUS_IS_OK(status)) {
844                 DEBUG(0,("Failed to open pipe %s - %s\n", pipe_name, nt_errstr(status)));
845                 talloc_free(p);
846                 return status;
847         }
848         
849         p->conn->flags = binding->flags;
850
851         /* remember the binding string for possible secondary connections */
852         p->conn->binding_string = dcerpc_binding_string(p, binding);
853
854         if (username && username[0] && (binding->flags & DCERPC_SCHANNEL_ANY)) {
855                 status = dcerpc_bind_auth_schannel(p, pipe_uuid, pipe_version, 
856                                                    domain, username, password);
857         } else if (username && username[0] &&
858                    (binding->flags & (DCERPC_CONNECT|DCERPC_SIGN|DCERPC_SEAL))) {
859                 status = dcerpc_bind_auth_ntlm(p, pipe_uuid, pipe_version, domain, username, password);
860         } else {    
861                 status = dcerpc_bind_auth_none(p, pipe_uuid, pipe_version);
862
863         }
864
865         if (!NT_STATUS_IS_OK(status)) {
866                 DEBUG(0,("Failed to bind to uuid %s - %s\n", pipe_uuid, nt_errstr(status)));
867                 talloc_free(p);
868                 return status;
869         }
870
871         (*pp) = p;
872         talloc_free(tmp_ctx);
873
874         return NT_STATUS_OK;
875 }
876
877 /* open a rpc connection to a rpc pipe on SMP using the binding
878    structure to determine the endpoint and options */
879 static NTSTATUS dcerpc_pipe_connect_ncalrpc(struct dcerpc_pipe **pp, 
880                                             struct dcerpc_binding *binding,
881                                             const char *pipe_uuid, 
882                                             uint32_t pipe_version,
883                                             const char *domain,
884                                             const char *username,
885                                             const char *password)
886 {
887         NTSTATUS status;
888         struct dcerpc_pipe *p;
889         TALLOC_CTX *tmp_ctx;
890
891         (*pp) = NULL;
892
893         p = dcerpc_pipe_init(NULL);
894         if (p == NULL) {
895                 return NT_STATUS_NO_MEMORY;
896         }
897         tmp_ctx = talloc_new(p);
898
899         /* Look up identifier using the epmapper */
900         if (!binding->endpoint) {
901                 status = dcerpc_epm_map_binding(tmp_ctx, binding, pipe_uuid, pipe_version);
902                 if (!NT_STATUS_IS_OK(status)) {
903                         DEBUG(0,("Failed to map DCERPC/TCP NCALRPC identifier for '%s' - %s\n", 
904                                  pipe_uuid, nt_errstr(status)));
905                         talloc_free(p);
906                         return status;
907                 }
908                 DEBUG(1,("Mapped to DCERPC/LRPC identifier %s\n", binding->endpoint));
909         }
910
911         status = dcerpc_pipe_open_pipe(p->conn, binding->endpoint);
912         if (!NT_STATUS_IS_OK(status)) {
913                 DEBUG(0,("Failed to open ncalrpc pipe '%s' - %s\n", 
914                          binding->endpoint, nt_errstr(status)));
915                 talloc_free(p);
916                 return status;
917         }
918
919         p->conn->flags = binding->flags;
920
921         /* remember the binding string for possible secondary connections */
922         p->conn->binding_string = dcerpc_binding_string(p, binding);
923
924         if (username && username[0] && (binding->flags & DCERPC_SCHANNEL_ANY)) {
925                 status = dcerpc_bind_auth_schannel(p, pipe_uuid, pipe_version, 
926                                                    domain, username, password);
927         } else if (username && username[0]) {
928                 status = dcerpc_bind_auth_ntlm(p, pipe_uuid, pipe_version, domain, username, password);
929         } else {    
930                 status = dcerpc_bind_auth_none(p, pipe_uuid, pipe_version);
931         }
932
933         if (!NT_STATUS_IS_OK(status)) {
934                 DEBUG(0,("Failed to bind to uuid %s - %s\n", pipe_uuid, nt_errstr(status)));
935                 talloc_free(p);
936                 return status;
937         }
938
939         (*pp) = p;
940         talloc_free(tmp_ctx);
941
942         return status;
943 }
944
945
946
947 /* open a rpc connection to a rpc pipe on SMP using the binding
948    structure to determine the endpoint and options */
949 static NTSTATUS dcerpc_pipe_connect_ncacn_unix_stream(struct dcerpc_pipe **pp, 
950                                                       struct dcerpc_binding *binding,
951                                                       const char *pipe_uuid, 
952                                                       uint32_t pipe_version,
953                                                       const char *domain,
954                                                       const char *username,
955                                                       const char *password)
956 {
957         NTSTATUS status;
958         struct dcerpc_pipe *p;
959
960         (*pp) = NULL;
961
962         if (!binding->endpoint) {
963                 DEBUG(0, ("Path to unix socket not specified\n"));
964                 return NT_STATUS_INVALID_PARAMETER;
965         }
966
967         p = dcerpc_pipe_init(NULL);
968         if (p == NULL) {
969                 return NT_STATUS_NO_MEMORY;
970         }
971
972         status = dcerpc_pipe_open_unix_stream(p->conn, binding->endpoint);
973         if (!NT_STATUS_IS_OK(status)) {
974                 DEBUG(0,("Failed to open unix socket %s - %s\n", 
975                          binding->endpoint, nt_errstr(status)));
976                 talloc_free(p);
977                 return status;
978         }
979
980         p->conn->flags = binding->flags;
981
982         /* remember the binding string for possible secondary connections */
983         p->conn->binding_string = dcerpc_binding_string(p, binding);
984
985         if (username && username[0] && (binding->flags & DCERPC_SCHANNEL_ANY)) {
986                 status = dcerpc_bind_auth_schannel(p, pipe_uuid, pipe_version, 
987                                                    domain, username, password);
988         } else if (username && username[0]) {
989                 status = dcerpc_bind_auth_ntlm(p, pipe_uuid, pipe_version, domain, username, password);
990         } else {    
991                 status = dcerpc_bind_auth_none(p, pipe_uuid, pipe_version);
992         }
993
994         if (!NT_STATUS_IS_OK(status)) {
995                 DEBUG(0,("Failed to bind to uuid %s - %s\n", pipe_uuid, nt_errstr(status)));
996                 talloc_free(p);
997                 return status;
998         }
999
1000         (*pp) = p;
1001  
1002         return status;
1003 }
1004
1005 /* open a rpc connection to a rpc pipe on SMP using the binding
1006    structure to determine the endpoint and options */
1007 static NTSTATUS dcerpc_pipe_connect_ncacn_ip_tcp(struct dcerpc_pipe **pp, 
1008                                                  struct dcerpc_binding *binding,
1009                                                  const char *pipe_uuid, 
1010                                                  uint32_t pipe_version,
1011                                                  const char *domain,
1012                                                  const char *username,
1013                                                  const char *password)
1014 {
1015         NTSTATUS status;
1016         uint32_t port = 0;
1017         struct dcerpc_pipe *p;
1018         TALLOC_CTX *tmp_ctx;
1019
1020         (*pp) = NULL;
1021
1022         p = dcerpc_pipe_init(NULL);
1023         if (p == NULL) {
1024                 return NT_STATUS_NO_MEMORY;
1025         }
1026         tmp_ctx = talloc_new(p);
1027
1028         if (!binding->endpoint) {
1029                 status = dcerpc_epm_map_binding(tmp_ctx, binding, 
1030                                                 pipe_uuid, pipe_version);
1031                 if (!NT_STATUS_IS_OK(status)) {
1032                         DEBUG(0,("Failed to map DCERPC/TCP port for '%s' - %s\n", 
1033                                  pipe_uuid, nt_errstr(status)));
1034                         talloc_free(p);
1035                         return status;
1036                 }
1037                 DEBUG(1,("Mapped to DCERPC/TCP port %s\n", binding->endpoint));
1038         }
1039
1040         port = atoi(binding->endpoint);
1041
1042         status = dcerpc_pipe_open_tcp(p->conn, binding->host, port);
1043         if (!NT_STATUS_IS_OK(status)) {
1044                 DEBUG(0,("Failed to connect to %s:%d - %s\n", 
1045                          binding->host, port, nt_errstr(status)));
1046                 talloc_free(p);
1047                 return status;
1048         }
1049
1050         p->conn->flags = binding->flags;
1051
1052         /* remember the binding string for possible secondary connections */
1053         p->conn->binding_string = dcerpc_binding_string(p, binding);
1054
1055         if (username && username[0] && (binding->flags & DCERPC_SCHANNEL_ANY)) {
1056                 status = dcerpc_bind_auth_schannel(p, pipe_uuid, pipe_version, 
1057                                                    domain, username, password);
1058         } else if (username && username[0]) {
1059                 status = dcerpc_bind_auth_ntlm(p, pipe_uuid, pipe_version, domain, username, password);
1060         } else {    
1061                 status = dcerpc_bind_auth_none(p, pipe_uuid, pipe_version);
1062         }
1063
1064         if (!NT_STATUS_IS_OK(status)) {
1065                 DEBUG(0,("Failed to bind to uuid %s - %s\n", 
1066                          pipe_uuid, nt_errstr(status)));
1067                 talloc_free(p);
1068                 return status;
1069         }
1070
1071         (*pp) = p;
1072         talloc_free(tmp_ctx);
1073  
1074         return status;
1075 }
1076
1077
1078 /* open a rpc connection to a rpc pipe, using the specified 
1079    binding structure to determine the endpoint and options */
1080 NTSTATUS dcerpc_pipe_connect_b(struct dcerpc_pipe **pp, 
1081                                struct dcerpc_binding *binding,
1082                                const char *pipe_uuid, 
1083                                uint32_t pipe_version,
1084                                const char *domain,
1085                                const char *username,
1086                                const char *password)
1087 {
1088         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
1089
1090         switch (binding->transport) {
1091         case NCACN_NP:
1092                 status = dcerpc_pipe_connect_ncacn_np(pp, binding, pipe_uuid, pipe_version,
1093                                                       domain, username, password);
1094                 break;
1095         case NCACN_IP_TCP:
1096                 status = dcerpc_pipe_connect_ncacn_ip_tcp(pp, binding, pipe_uuid, pipe_version,
1097                                                           domain, username, password);
1098                 break;
1099         case NCACN_UNIX_STREAM:
1100                 status = dcerpc_pipe_connect_ncacn_unix_stream(pp, binding, pipe_uuid, pipe_version, 
1101                                                                domain, username, password);
1102                 break;
1103         case NCALRPC:
1104                 status = dcerpc_pipe_connect_ncalrpc(pp, binding, pipe_uuid, pipe_version, 
1105                                                      domain, username, password);
1106                 break;
1107         default:
1108                 return NT_STATUS_NOT_SUPPORTED;
1109         }
1110
1111         return status;
1112 }
1113
1114
1115 /* open a rpc connection to a rpc pipe, using the specified string
1116    binding to determine the endpoint and options */
1117 NTSTATUS dcerpc_pipe_connect(struct dcerpc_pipe **pp, 
1118                              const char *binding,
1119                              const char *pipe_uuid, 
1120                              uint32_t pipe_version,
1121                              const char *domain,
1122                              const char *username,
1123                              const char *password)
1124 {
1125         struct dcerpc_binding b;
1126         NTSTATUS status;
1127         TALLOC_CTX *tmp_ctx;
1128
1129         tmp_ctx = talloc_new(NULL);
1130
1131         status = dcerpc_parse_binding(tmp_ctx, binding, &b);
1132         if (!NT_STATUS_IS_OK(status)) {
1133                 DEBUG(0,("Failed to parse dcerpc binding '%s'\n", binding));
1134                 talloc_free(tmp_ctx);
1135                 return status;
1136         }
1137
1138         DEBUG(3,("Using binding %s\n", dcerpc_binding_string(tmp_ctx, &b)));
1139
1140         status = dcerpc_pipe_connect_b(pp, &b, pipe_uuid, pipe_version, domain, username, password);
1141
1142         talloc_free(tmp_ctx);
1143
1144         return status;
1145 }
1146
1147
1148 /*
1149   create a secondary dcerpc connection from a primary connection
1150
1151   if the primary is a SMB connection then the secondary connection
1152   will be on the same SMB connection, but use a new fnum
1153 */
1154 NTSTATUS dcerpc_secondary_connection(struct dcerpc_pipe *p, struct dcerpc_pipe **p2,
1155                                      const char *pipe_name,
1156                                      const char *pipe_uuid,
1157                                      uint32_t pipe_version)
1158 {
1159         struct smbcli_tree *tree;
1160         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
1161         struct dcerpc_binding b;
1162
1163         (*p2) = dcerpc_pipe_init(p);
1164         if (*p2 == NULL) {
1165                 return NT_STATUS_NO_MEMORY;
1166         }
1167         
1168         switch (p->conn->transport.transport) {
1169         case NCACN_NP:
1170                 tree = dcerpc_smb_tree(p->conn);
1171                 if (!tree) {
1172                         return NT_STATUS_INVALID_PARAMETER;
1173                 }
1174
1175                 status = dcerpc_pipe_open_smb((*p2)->conn, tree, pipe_name);
1176                 break;
1177
1178         case NCACN_IP_TCP:
1179                 status = dcerpc_parse_binding(p, p->conn->binding_string, &b);
1180                 if (!NT_STATUS_IS_OK(status)) {
1181                         return status;
1182                 }
1183                 b.flags &= ~DCERPC_AUTH_OPTIONS;
1184                 status = dcerpc_pipe_open_tcp((*p2)->conn, b.host, atoi(b.endpoint));
1185                 break;
1186
1187         case NCALRPC:
1188                 status = dcerpc_parse_binding(p, p->conn->binding_string, &b);
1189                 if (!NT_STATUS_IS_OK(status)) {
1190                         return status;
1191                 }
1192                 b.flags &= ~DCERPC_AUTH_OPTIONS;
1193                 status = dcerpc_pipe_open_pipe((*p2)->conn, b.endpoint);
1194                 break;
1195
1196         default:
1197                 return NT_STATUS_NOT_SUPPORTED;
1198         }
1199
1200         if (!NT_STATUS_IS_OK(status)) {
1201                 talloc_free(*p2);
1202                 return status;
1203         }
1204
1205         (*p2)->conn->flags = p->conn->flags;
1206
1207         status = dcerpc_bind_auth_none(*p2, pipe_uuid, pipe_version);
1208         if (!NT_STATUS_IS_OK(status)) {
1209                 talloc_free(*p2);
1210                 return status;
1211         }
1212
1213         return NT_STATUS_OK;
1214 }
1215
1216 NTSTATUS dcerpc_generic_session_key(struct dcerpc_connection *c,
1217                                     DATA_BLOB *session_key)
1218 {
1219         /* this took quite a few CPU cycles to find ... */
1220         session_key->data = discard_const_p(unsigned char, "SystemLibraryDTC");
1221         session_key->length = 16;
1222         return NT_STATUS_OK;
1223 }
1224
1225 /*
1226   fetch the user session key - may be default (above) or the SMB session key
1227 */
1228 NTSTATUS dcerpc_fetch_session_key(struct dcerpc_pipe *p,
1229                                   DATA_BLOB *session_key)
1230 {
1231         return p->conn->security_state.session_key(p->conn, session_key);
1232 }
1233
1234
1235 /*
1236   log a rpc packet in a format suitable for ndrdump. This is especially useful
1237   for sealed packets, where ethereal cannot easily see the contents
1238
1239   this triggers on a debug level of >= 10
1240 */
1241 void dcerpc_log_packet(const struct dcerpc_interface_table *ndr,
1242                        uint32_t opnum, uint32_t flags, DATA_BLOB *pkt)
1243 {
1244         const int num_examples = 20;
1245         int i;
1246
1247         if (DEBUGLEVEL < 10) return;
1248
1249         for (i=0;i<num_examples;i++) {
1250                 char *name=NULL;
1251                 asprintf(&name, "%s/rpclog/%s-%u.%d.%s", 
1252                          lp_lockdir(), ndr->name, opnum, i,
1253                          (flags&NDR_IN)?"in":"out");
1254                 if (name == NULL) {
1255                         return;
1256                 }
1257                 if (!file_exist(name, NULL)) {
1258                         if (file_save(name, pkt->data, pkt->length)) {
1259                                 DEBUG(10,("Logged rpc packet to %s\n", name));
1260                         }
1261                         free(name);
1262                         break;
1263                 }
1264                 free(name);
1265         }
1266 }
1267