r3157: Add ncacn_http
[gd/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
26 /*
27   work out what TCP port to use for a given interface on a given host
28 */
29 NTSTATUS dcerpc_epm_map_tcp_port(const char *server, 
30                                  const char *uuid, uint_t version,
31                                  uint32_t *port)
32 {
33         struct dcerpc_pipe *p;
34         NTSTATUS status;
35         struct epm_Map r;
36         struct policy_handle handle;
37         struct GUID guid;
38         struct epm_twr_t twr, *twr_r;
39
40         if (strcasecmp(uuid, DCERPC_EPMAPPER_UUID) == 0 ||
41             strcasecmp(uuid, DCERPC_MGMT_UUID) == 0) {
42                 /* don't lookup epmapper via epmapper! */
43                 *port = EPMAPPER_PORT;
44                 return NT_STATUS_OK;
45         }
46
47         status = dcerpc_pipe_open_tcp(&p, server, EPMAPPER_PORT, AF_UNSPEC );
48         if (!NT_STATUS_IS_OK(status)) {
49                 return status;
50         }
51
52         /* we can use the pipes memory context here as we will have a short
53            lived connection */
54         status = dcerpc_bind_byuuid(p, p, 
55                                     DCERPC_EPMAPPER_UUID,
56                                     DCERPC_EPMAPPER_VERSION);
57         if (!NT_STATUS_IS_OK(status)) {
58                 dcerpc_pipe_close(p);
59                 return status;
60         }
61
62         ZERO_STRUCT(handle);
63         ZERO_STRUCT(guid);
64
65         twr.tower.num_floors = 5;
66         twr.tower.floors = talloc(p, sizeof(twr.tower.floors[0]) * 5);
67
68         /* what I'd like for christmas ... */
69
70         /* an RPC interface ... */
71         twr.tower.floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
72         GUID_from_string(uuid, &twr.tower.floors[0].lhs.info.uuid.uuid);
73         twr.tower.floors[0].lhs.info.uuid.version = version;
74         twr.tower.floors[0].rhs.uuid.unknown = 0;
75
76         /* encoded with NDR ... */
77         twr.tower.floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
78         GUID_from_string(NDR_GUID, &twr.tower.floors[1].lhs.info.uuid.uuid);
79         twr.tower.floors[1].lhs.info.uuid.version = NDR_GUID_VERSION;
80         twr.tower.floors[1].rhs.uuid.unknown = 0;
81
82         /* on an RPC connection ... */
83         twr.tower.floors[2].lhs.protocol = EPM_PROTOCOL_NCACN;
84         twr.tower.floors[2].lhs.info.lhs_data = data_blob(NULL, 0);
85         twr.tower.floors[2].rhs.ncacn.minor_version = 0;
86
87         /* on a TCP port ... */
88         twr.tower.floors[3].lhs.protocol = EPM_PROTOCOL_TCP;
89         twr.tower.floors[3].lhs.info.lhs_data = data_blob(NULL, 0);
90         twr.tower.floors[3].rhs.tcp.port = 0;
91
92         /* on an IP link ... */
93         twr.tower.floors[4].lhs.protocol = EPM_PROTOCOL_IP;
94         twr.tower.floors[4].lhs.info.lhs_data = data_blob(NULL, 0);
95         twr.tower.floors[4].rhs.ip.address = 0;
96
97         /* with some nice pretty paper around it of course */
98         r.in.object = &guid;
99         r.in.map_tower = &twr;
100         r.in.entry_handle = &handle;
101         r.in.max_towers = 1;
102         r.out.entry_handle = &handle;
103
104         status = dcerpc_epm_Map(p, p, &r);
105         if (!NT_STATUS_IS_OK(status)) {
106                 dcerpc_pipe_close(p);
107                 return status;
108         }
109         if (r.out.result != 0 || r.out.num_towers != 1) {
110                 dcerpc_pipe_close(p);
111                 return NT_STATUS_PORT_UNREACHABLE;
112         }
113
114         twr_r = r.out.towers[0].twr;
115         if (!twr_r) {
116                 dcerpc_pipe_close(p);
117                 return NT_STATUS_PORT_UNREACHABLE;
118         }
119
120         if (twr_r->tower.num_floors != 5 ||
121             twr_r->tower.floors[3].lhs.protocol != twr.tower.floors[3].lhs.protocol) {
122                 dcerpc_pipe_close(p);
123                 return NT_STATUS_PORT_UNREACHABLE;
124         }
125
126         *port = twr_r->tower.floors[3].rhs.tcp.port;
127
128         dcerpc_pipe_close(p);
129
130         return NT_STATUS_OK;
131 }
132
133 /*
134   find the pipe name for a local IDL interface
135 */
136 const char *idl_pipe_name(const char *uuid, uint32_t if_version)
137 {
138         int i;
139         for (i=0;dcerpc_pipes[i];i++) {
140                 if (strcasecmp(dcerpc_pipes[i]->uuid, uuid) == 0 &&
141                     dcerpc_pipes[i]->if_version == if_version) {
142                         return dcerpc_pipes[i]->name;
143                 }
144         }
145         return "UNKNOWN";
146 }
147
148 /*
149   find the number of calls defined by local IDL
150 */
151 int idl_num_calls(const char *uuid, uint32_t if_version)
152 {
153         int i;
154         for (i=0;dcerpc_pipes[i];i++) {
155                 if (strcasecmp(dcerpc_pipes[i]->uuid, uuid) == 0 &&
156                     dcerpc_pipes[i]->if_version == if_version) {
157                         return dcerpc_pipes[i]->num_calls;
158                 }
159         }
160         return -1;
161 }
162
163
164 /*
165   find a dcerpc interface by name
166 */
167 const struct dcerpc_interface_table *idl_iface_by_name(const char *name)
168 {
169         int i;
170         for (i=0;dcerpc_pipes[i];i++) {
171                 if (strcasecmp(dcerpc_pipes[i]->name, name) == 0) {
172                         return dcerpc_pipes[i];
173                 }
174         }
175         return NULL;
176 }
177
178 /*
179   find a dcerpc interface by uuid
180 */
181 const struct dcerpc_interface_table *idl_iface_by_uuid(const char *uuid)
182 {
183         int i;
184         for (i=0;dcerpc_pipes[i];i++) {
185                 if (strcasecmp(dcerpc_pipes[i]->uuid, uuid) == 0) {
186                         return dcerpc_pipes[i];
187                 }
188         }
189         return NULL;
190 }
191
192
193
194 /* 
195    push a dcerpc_packet into a blob, potentially with auth info
196 */
197 NTSTATUS dcerpc_push_auth(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, 
198                           struct dcerpc_packet *pkt,
199                           struct dcerpc_auth *auth_info)
200 {
201         NTSTATUS status;
202         struct ndr_push *ndr;
203
204         ndr = ndr_push_init_ctx(mem_ctx);
205         if (!ndr) {
206                 return NT_STATUS_NO_MEMORY;
207         }
208
209         if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
210                 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
211         }
212
213         if (auth_info) {
214                 pkt->auth_length = auth_info->credentials.length;
215         } else {
216                 pkt->auth_length = 0;
217         }
218
219         status = ndr_push_dcerpc_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
220         if (!NT_STATUS_IS_OK(status)) {
221                 return status;
222         }
223
224         if (auth_info) {
225                 status = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, auth_info);
226         }
227
228         *blob = ndr_push_blob(ndr);
229
230         /* fill in the frag length */
231         dcerpc_set_frag_length(blob, blob->length);
232
233         return NT_STATUS_OK;
234 }
235
236 #define MAX_PROTSEQ             10
237
238 static const struct {
239         const char *name;
240         enum dcerpc_transport_t transport;
241         int num_protocols;
242         enum epm_protocols protseq[MAX_PROTSEQ];
243 } transports[] = {
244         { "ncacn_np",     NCACN_NP, 3, 
245                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SMB, EPM_PROTOCOL_NETBIOS }},
246         { "ncacn_ip_tcp", NCACN_IP_TCP, 3, 
247                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_TCP, EPM_PROTOCOL_IP } }, 
248         { "ncacn_http", NCACN_HTTP, 3, 
249                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_HTTP, EPM_PROTOCOL_IP } }, 
250         { "ncadg_ip_udp", NCACN_IP_UDP, 3, 
251                 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UDP, EPM_PROTOCOL_IP } },
252         { "ncalrpc", NCALRPC, 2, 
253                 { EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_PIPE } },
254         { "ncacn_unix_stream", NCACN_UNIX_STREAM, 2, 
255                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_UNIX_DS } },
256         { "ncadg_unix_dgram", NCADG_UNIX_DGRAM, 2, 
257                 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UNIX_DS } },
258         { "ncacn_at_dsp", NCACN_AT_DSP, 3, 
259                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DSP } },
260         { "ncadg_at_ddp", NCADG_AT_DDP, 3, 
261                 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DDP } },
262         { "ncacn_vns_ssp", NCACN_VNS_SPP, 3, 
263                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_SPP } },
264         { "ncacn_vns_ipc", NCACN_VNS_IPC, 3, 
265                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_IPC }, },
266 };
267
268 static const struct {
269         const char *name;
270         uint32_t flag;
271 } ncacn_options[] = {
272         {"sign", DCERPC_SIGN},
273         {"seal", DCERPC_SEAL},
274         {"connect", DCERPC_CONNECT},
275         {"validate", DCERPC_DEBUG_VALIDATE_BOTH},
276         {"print", DCERPC_DEBUG_PRINT_BOTH},
277         {"padcheck", DCERPC_DEBUG_PAD_CHECK},
278         {"bigendian", DCERPC_PUSH_BIGENDIAN}
279 };
280
281
282
283 /*
284   form a binding string from a binding structure
285 */
286 const char *dcerpc_binding_string(TALLOC_CTX *mem_ctx, const struct dcerpc_binding *b)
287 {
288         char *s = NULL;
289         int i;
290         const char *t_name=NULL;
291
292         for (i=0;i<ARRAY_SIZE(transports);i++) {
293                 if (transports[i].transport == b->transport) {
294                         t_name = transports[i].name;
295                 }
296         }
297         if (!t_name) {
298                 return NULL;
299         }
300
301         if (!uuid_all_zero(&b->object)) { 
302                 s = talloc_asprintf(mem_ctx, "%s@", GUID_string(mem_ctx, &b->object));
303         }
304
305         s = talloc_asprintf_append(s, "%s:", t_name);
306         if (!s) return NULL;
307
308         if (b->host) {
309                 s = talloc_asprintf_append(s, "%s", b->host);
310         }
311
312         if ((!b->options || !b->options[0]) && !b->flags) {
313                 return s;
314         }
315
316         s = talloc_asprintf_append(s, "[");
317
318         /* this is a *really* inefficent way of dealing with strings,
319            but this is rarely called and the strings are always short,
320            so I don't care */
321         for (i=0;b->options && b->options[i];i++) {
322                 s = talloc_asprintf_append(s, "%s,", b->options[i]);
323                 if (!s) return NULL;
324         }
325         for (i=0;i<ARRAY_SIZE(ncacn_options);i++) {
326                 if (b->flags & ncacn_options[i].flag) {
327                         s = talloc_asprintf_append(s, "%s,", ncacn_options[i].name);
328                         if (!s) return NULL;
329                 }
330         }
331
332         s[strlen(s)-1] = 0;
333         s = talloc_asprintf_append(s, "]");
334
335         return s;
336 }
337
338 /*
339   parse a binding string into a dcerpc_binding structure
340 */
341 NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *s, struct dcerpc_binding *b)
342 {
343         char *options, *type;
344         char *p;
345         int i, j, comma_count;
346
347         p = strchr(s, '@');
348
349         if (p && PTR_DIFF(p, s) == 36) { /* 36 is the length of a UUID */
350                 NTSTATUS status;
351
352                 status = GUID_from_string(s, &b->object);
353
354                 if (NT_STATUS_IS_ERR(status)) {
355                         DEBUG(0, ("Failed parsing UUID\n"));
356                         return status;
357                 }
358
359                 s = p + 1;
360         } else {
361                 ZERO_STRUCT(b->object);
362         }
363
364         b->object_version = 0;
365
366         p = strchr(s, ':');
367         if (!p) {
368                 return NT_STATUS_INVALID_PARAMETER;
369         }
370
371         type = talloc_strndup(mem_ctx, s, PTR_DIFF(p, s));
372         if (!type) {
373                 return NT_STATUS_NO_MEMORY;
374         }
375
376         for (i=0;i<ARRAY_SIZE(transports);i++) {
377                 if (strcasecmp(type, transports[i].name) == 0) {
378                         b->transport = transports[i].transport;
379                         break;
380                 }
381         }
382         if (i==ARRAY_SIZE(transports)) {
383                 DEBUG(0,("Unknown dcerpc transport '%s'\n", type));
384                 return NT_STATUS_INVALID_PARAMETER;
385         }
386         
387         s = p+1;
388
389         p = strchr(s, '[');
390         if (p) {
391                 b->host = talloc_strndup(mem_ctx, s, PTR_DIFF(p, s));
392                 options = talloc_strdup(mem_ctx, p+1);
393                 if (options[strlen(options)-1] != ']') {
394                         return NT_STATUS_INVALID_PARAMETER;
395                 }
396                 options[strlen(options)-1] = 0;
397         } else {
398                 b->host = talloc_strdup(mem_ctx, s);
399                 options = NULL;
400         }
401
402         if (!b->host) {
403                 return NT_STATUS_NO_MEMORY;
404         }
405
406         b->options = NULL;
407         b->flags = 0;
408
409         if (!options) {
410                 return NT_STATUS_OK;
411         }
412
413         comma_count = count_chars(options, ',');
414         b->options = talloc_array_p(mem_ctx, const char *, comma_count+2);
415         if (!b->options) {
416                 return NT_STATUS_NO_MEMORY;
417         }
418
419         for (i=0; (p = strchr(options, ',')); i++) {
420                 b->options[i] = talloc_strndup(mem_ctx, options, PTR_DIFF(p, options));
421                 if (!b->options[i]) {
422                         return NT_STATUS_NO_MEMORY;
423                 }
424                 options = p+1;
425         }
426         b->options[i] = options;
427         b->options[i+1] = NULL;
428
429         /* some options are pre-parsed for convenience */
430         for (i=0;b->options[i];i++) {
431                 for (j=0;j<ARRAY_SIZE(ncacn_options);j++) {
432                         if (strcasecmp(ncacn_options[j].name, b->options[i]) == 0) {
433                                 int k;
434                                 b->flags |= ncacn_options[j].flag;
435                                 for (k=i;b->options[k];k++) {
436                                         b->options[k] = b->options[k+1];
437                                 }
438                                 i--;
439                                 break;
440                         }
441                 }
442         }
443         
444         return NT_STATUS_OK;
445 }
446
447 static const char *floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *floor)
448 {
449         switch (floor->lhs.protocol) {
450         case EPM_PROTOCOL_TCP:
451                 if (floor->rhs.tcp.port == 0) return NULL;
452                 return talloc_asprintf(mem_ctx, "%d", floor->rhs.tcp.port);
453                 
454         case EPM_PROTOCOL_UDP:
455                 if (floor->rhs.udp.port == 0) return NULL;
456                 return talloc_asprintf(mem_ctx, "%d", floor->rhs.udp.port);
457
458         case EPM_PROTOCOL_HTTP:
459                 if (floor->rhs.http.port == 0) return NULL;
460                 return talloc_asprintf(mem_ctx, "%d", floor->rhs.http.port);
461
462         case EPM_PROTOCOL_IP:
463                 if (floor->rhs.ip.address == 0) {
464                         return NULL; 
465                 }
466
467                 {
468                 struct in_addr in;
469                         in.s_addr = htonl(floor->rhs.ip.address);
470             return talloc_strdup(mem_ctx, inet_ntoa(in));
471                 }
472
473         case EPM_PROTOCOL_NCACN:
474                 return NULL;
475
476         case EPM_PROTOCOL_NCADG:
477                 return NULL;
478
479         case EPM_PROTOCOL_SMB:
480                 return talloc_strdup(mem_ctx, floor->rhs.smb.unc);
481
482         case EPM_PROTOCOL_PIPE:
483                 return talloc_strdup(mem_ctx, floor->rhs.pipe.path);
484
485         case EPM_PROTOCOL_NETBIOS:
486                 return talloc_strdup(mem_ctx, floor->rhs.netbios.name);
487
488         case EPM_PROTOCOL_NCALRPC:
489                 return NULL;
490                 
491         case EPM_PROTOCOL_VINES_SPP:
492                 return talloc_asprintf(mem_ctx, "%d", floor->rhs.vines_spp.port);
493                 
494         case EPM_PROTOCOL_VINES_IPC:
495                 return talloc_asprintf(mem_ctx, "%d", floor->rhs.vines_ipc.port);
496                 
497         case EPM_PROTOCOL_STREETTALK:
498                 return talloc_strdup(mem_ctx, floor->rhs.streettalk.streettalk);
499                 
500         case EPM_PROTOCOL_UNIX_DS:
501                 return talloc_strdup(mem_ctx, floor->rhs.unix_ds.path);
502                 
503         case EPM_PROTOCOL_NULL:
504                 return NULL;
505         }
506
507         return NULL;
508 }
509
510 static NTSTATUS floor_set_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *floor,  const char *data)
511 {
512         switch (floor->lhs.protocol) {
513         case EPM_PROTOCOL_TCP:
514                 floor->rhs.tcp.port = atoi(data);
515                 return NT_STATUS_OK;
516                 
517         case EPM_PROTOCOL_UDP:
518                 floor->rhs.udp.port = atoi(data);
519                 return NT_STATUS_OK;
520
521         case EPM_PROTOCOL_HTTP:
522                 floor->rhs.http.port = atoi(data);
523                 return NT_STATUS_OK;
524
525         case EPM_PROTOCOL_IP:
526                 floor->rhs.ip.address = 0;
527
528                 /* Only try to put in a IPv4 address. Windows 2003 just returns 
529                  * 0.0.0.0 for IPv6 addresses */
530                 if (strlen(data) > 0) {
531                     struct addrinfo hints, *res;
532
533                         memset(&hints, 0, sizeof(struct addrinfo));
534
535                         hints.ai_family = AF_INET;
536                         hints.ai_protocol = PF_INET;
537
538                         if (getaddrinfo(data, NULL, &hints, &res) < 0) {
539                                 return NT_STATUS_BAD_NETWORK_NAME;
540                         }
541
542                         floor->rhs.ip.address = ntohl(((struct sockaddr_in *)res->ai_addr)->sin_addr.s_addr);
543
544                         freeaddrinfo(res);
545                 }
546                 return NT_STATUS_OK;
547
548         case EPM_PROTOCOL_NCACN:
549                 floor->rhs.ncacn.minor_version = 0;
550                 return NT_STATUS_OK;
551
552         case EPM_PROTOCOL_NCADG:
553                 floor->rhs.ncadg.minor_version = 0;
554                 return NT_STATUS_OK;
555
556         case EPM_PROTOCOL_SMB:
557                 floor->rhs.smb.unc = talloc_strdup(mem_ctx, data);
558                 if (!floor->rhs.smb.unc) {
559                         return NT_STATUS_NO_MEMORY;
560                 }
561                 return NT_STATUS_OK;
562
563         case EPM_PROTOCOL_PIPE:
564                 floor->rhs.pipe.path = talloc_strdup(mem_ctx, data);
565                 if (!floor->rhs.pipe.path) {
566                         return NT_STATUS_NO_MEMORY;
567                 }
568                 return NT_STATUS_OK;
569
570         case EPM_PROTOCOL_NETBIOS:
571                 floor->rhs.netbios.name = talloc_strdup(mem_ctx, data);
572                 if (!floor->rhs.netbios.name) {
573                         return NT_STATUS_NO_MEMORY;
574                 }
575                 return NT_STATUS_OK;
576
577         case EPM_PROTOCOL_NCALRPC:
578                 return NT_STATUS_OK;
579                 
580         case EPM_PROTOCOL_VINES_SPP:
581                 floor->rhs.vines_spp.port = atoi(data);
582                 return NT_STATUS_OK;
583                 
584         case EPM_PROTOCOL_VINES_IPC:
585                 floor->rhs.vines_ipc.port = atoi(data);
586                 return NT_STATUS_OK;
587                 
588         case EPM_PROTOCOL_STREETTALK:
589                 floor->rhs.streettalk.streettalk = talloc_strdup(mem_ctx, data);
590                 if (!floor->rhs.streettalk.streettalk) {
591                         return NT_STATUS_NO_MEMORY;
592                 }
593                 return NT_STATUS_OK;
594                 
595         case EPM_PROTOCOL_UNIX_DS:
596                 floor->rhs.unix_ds.path = talloc_strdup(mem_ctx, data);
597                 if (!floor->rhs.unix_ds.path) {
598                         return NT_STATUS_NO_MEMORY;
599                 }
600                 return NT_STATUS_OK;
601                 
602         case EPM_PROTOCOL_NULL:
603                 return NT_STATUS_OK;
604         }
605
606         return NT_STATUS_NOT_SUPPORTED;
607 }
608
609 enum dcerpc_transport_t dcerpc_transport_by_tower(struct epm_tower *tower)
610 {
611         int i;
612
613         /* Find a transport that matches this tower */
614         for (i=0;i<ARRAY_SIZE(transports);i++) {
615                 int j;
616                 if (transports[i].num_protocols != tower->num_floors - 2) {
617                         continue; 
618                 }
619
620                 for (j = 0; j < transports[i].num_protocols; j++) {
621                         if (transports[i].protseq[j] != tower->floors[j+2].lhs.protocol) {
622                                 break;
623                         }
624                 }
625
626                 if (j == transports[i].num_protocols) {
627                         return transports[i].transport;
628                 }
629         }
630         
631         /* Unknown transport */
632         return -1;
633 }
634
635 NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx, struct epm_tower *tower, struct dcerpc_binding *binding)
636 {
637         ZERO_STRUCT(binding->object);
638         binding->options = NULL;
639         binding->host = NULL;
640         binding->flags = 0;
641
642         binding->transport = dcerpc_transport_by_tower(tower);
643
644         if (binding->transport == -1) {
645                 return NT_STATUS_NOT_SUPPORTED;
646         }
647
648         if (tower->num_floors < 1) {
649                 return NT_STATUS_OK;
650         }
651
652         /* Set object uuid */
653         binding->object = tower->floors[0].lhs.info.uuid.uuid;
654         binding->object_version = tower->floors[0].lhs.info.uuid.version;
655
656         /* Ignore floor 1, it contains the NDR version info */
657         
658         binding->options = talloc_array_p(mem_ctx, const char *, 2);
659
660         /* Set endpoint */
661         if (tower->num_floors >= 4) {
662                 binding->options[0] = floor_get_rhs_data(mem_ctx, &tower->floors[3]);
663         } else {
664                 binding->options[0] = NULL;
665         }
666         binding->options[1] = NULL;
667
668         /* Set network address */
669         if (tower->num_floors >= 5) {
670                 binding->host = floor_get_rhs_data(mem_ctx, &tower->floors[4]);
671         }
672         return NT_STATUS_OK;
673 }
674
675 NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx, struct dcerpc_binding *binding, struct epm_tower *tower)
676 {
677         const enum epm_protocols *protseq;
678         int num_protocols = -1, i;
679         NTSTATUS status;
680         
681         /* Find transport */
682         for (i=0;i<ARRAY_SIZE(transports);i++) {
683                 if (transports[i].transport == binding->transport) {
684                         protseq = transports[i].protseq;
685                         num_protocols = transports[i].num_protocols;
686                         break;
687                 }
688         }
689
690         if (num_protocols == -1) {
691                 DEBUG(0, ("Unable to find transport with id '%d'\n", binding->transport));
692                 return NT_STATUS_UNSUCCESSFUL;
693         }
694
695         tower->num_floors = 2 + num_protocols;
696         tower->floors = talloc_array_p(mem_ctx, struct epm_floor, tower->num_floors);
697
698         /* Floor 0 */
699         tower->floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
700         tower->floors[0].lhs.info.uuid.uuid = binding->object;
701         tower->floors[0].lhs.info.uuid.version = binding->object_version;
702         tower->floors[0].rhs.uuid.unknown = 0;
703         
704         /* Floor 1 */
705         tower->floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
706         tower->floors[1].lhs.info.uuid.version = NDR_GUID_VERSION;
707         tower->floors[1].rhs.uuid.unknown = 0;
708         status = GUID_from_string(NDR_GUID, &tower->floors[1].lhs.info.uuid.uuid);
709         if (NT_STATUS_IS_ERR(status)) {
710                 return status;
711         }
712
713         /* Floor 2 to num_protocols */
714         for (i = 0; i < num_protocols; i++) {
715                 tower->floors[2 + i].lhs.protocol = protseq[i];
716                 tower->floors[2 + i].lhs.info.lhs_data = data_blob_talloc(mem_ctx, NULL, 0);
717                 ZERO_STRUCT(tower->floors[2 + i].rhs);
718         }
719
720         /* The 4th floor contains the endpoint */
721         if (num_protocols >= 2 && binding->options && binding->options[0]) {
722                 status = floor_set_rhs_data(mem_ctx, &tower->floors[3], binding->options[0]);
723                 if (NT_STATUS_IS_ERR(status)) {
724                         return status;
725                 }
726         }
727         /* The 5th contains the network address */
728         if (num_protocols >= 3 && binding->host) {
729                 status = floor_set_rhs_data(mem_ctx, &tower->floors[4], binding->host);
730                 if (NT_STATUS_IS_ERR(status)) {
731                         return status;
732                 }
733         }
734
735         return NT_STATUS_OK;
736 }
737
738
739 /* open a rpc connection to a rpc pipe on SMB using the binding
740    structure to determine the endpoint and options */
741 static NTSTATUS dcerpc_pipe_connect_ncacn_np(struct dcerpc_pipe **p, 
742                                              struct dcerpc_binding *binding,
743                                              const char *pipe_uuid, 
744                                              uint32_t pipe_version,
745                                              const char *domain,
746                                              const char *username,
747                                              const char *password)
748 {
749         NTSTATUS status;
750         BOOL retry;
751         struct smbcli_state *cli;
752         const char *pipe_name;
753         TALLOC_CTX *mem_ctx = talloc_init("dcerpc_pipe_connect_ncacn_np");
754         
755         if (!binding->options || !binding->options[0] || !strlen(binding->options[0])) {
756                 const struct dcerpc_interface_table *table = idl_iface_by_uuid(pipe_uuid);
757                 struct dcerpc_binding default_binding;
758                 int i;
759
760                 if (!table) {
761                         DEBUG(0,("Unknown interface endpoint '%s'\n", pipe_uuid));
762                         talloc_destroy(mem_ctx);
763                         return NT_STATUS_INVALID_PARAMETER;
764                 }
765
766                 /* Find one of the default pipes for this interface */
767                 for (i = 0; i < table->endpoints->count; i++) {
768                         status = dcerpc_parse_binding(mem_ctx, table->endpoints->names[i], &default_binding);
769
770                         if (NT_STATUS_IS_OK(status) && default_binding.transport == NCACN_NP) {
771                                 pipe_name = default_binding.options[0]; 
772                                 break;
773                                 
774                         }
775                 }
776         } else {
777                 pipe_name = binding->options[0];
778         }
779
780         if (!strncasecmp(pipe_name, "/pipe/", 6) || 
781                 !strncasecmp(pipe_name, "\\pipe\\", 6)) {
782                 pipe_name+=6;
783         }
784
785         if (pipe_name[0] != '\\') {
786                 pipe_name = talloc_asprintf(mem_ctx, "\\%s", pipe_name);
787         }
788         
789         if (!username || !username[0]) {
790                 status = smbcli_full_connection(NULL, &cli, lp_netbios_name(),
791                                              binding->host, NULL, 
792                                              "ipc$", "?????", 
793                                              "", "", NULL, 0, &retry);
794         } else {
795                 status = smbcli_full_connection(NULL, &cli, lp_netbios_name(),
796                                              binding->host, NULL, 
797                                              "ipc$", "?????", 
798                                              username, domain,
799                                              password, 0, &retry);
800         }
801         if (!NT_STATUS_IS_OK(status)) {
802                 DEBUG(0,("Failed to connect to %s - %s\n", binding->host, nt_errstr(status)));
803                 talloc_destroy(mem_ctx);
804                 return status;
805         }
806
807         status = dcerpc_pipe_open_smb(p, cli->tree, pipe_name);
808         if (!NT_STATUS_IS_OK(status)) {
809                 DEBUG(0,("Failed to open pipe %s - %s\n", pipe_name, nt_errstr(status)));
810                 smbcli_tdis(cli);
811                 smbcli_shutdown(cli);
812                 talloc_destroy(mem_ctx);
813         return status;
814     }   
815
816         talloc_destroy(mem_ctx);
817         
818         /* this ensures that the reference count is decremented so
819            a pipe close will really close the link */
820         talloc_steal(*p, cli);
821
822         (*p)->flags = binding->flags;
823
824         /* remember the binding string for possible secondary connections */
825         (*p)->binding_string = dcerpc_binding_string((*p), binding);
826
827         if (username && username[0] && (binding->flags & DCERPC_SCHANNEL_ANY)) {
828                 status = dcerpc_bind_auth_schannel(*p, pipe_uuid, pipe_version, 
829                                                    domain, username, password);
830         } else if (username && username[0] &&
831                    (binding->flags & (DCERPC_CONNECT|DCERPC_SIGN|DCERPC_SEAL))) {
832                 status = dcerpc_bind_auth_ntlm(*p, pipe_uuid, pipe_version, domain, username, password);
833         } else {    
834                 status = dcerpc_bind_auth_none(*p, pipe_uuid, pipe_version);
835
836         }
837
838         if (!NT_STATUS_IS_OK(status)) {
839                 DEBUG(0,("Failed to bind to uuid %s - %s\n", pipe_uuid, nt_errstr(status)));
840                 dcerpc_pipe_close(*p);
841                 *p = NULL;
842                 return status;
843         }
844
845         return NT_STATUS_OK;
846 }
847
848
849 /* open a rpc connection to a rpc pipe on SMP using the binding
850    structure to determine the endpoint and options */
851 static NTSTATUS dcerpc_pipe_connect_ncacn_ip_tcp(struct dcerpc_pipe **p, 
852                                                  struct dcerpc_binding *binding,
853                                                  const char *pipe_uuid, 
854                                                  uint32_t pipe_version,
855                                                  const char *domain,
856                                                  const char *username,
857                                                  const char *password)
858 {
859         NTSTATUS status;
860         uint32_t port = 0;
861
862         if (binding->options && binding->options[0] && strlen(binding->options[0])) {
863                 port = atoi(binding->options[0]);
864         }
865
866         if (port == 0) {
867                 status = dcerpc_epm_map_tcp_port(binding->host, 
868                                                  pipe_uuid, pipe_version,
869                                                  &port);
870                 if (!NT_STATUS_IS_OK(status)) {
871                         DEBUG(0,("Failed to map DCERPC/TCP port for '%s' - %s\n", 
872                                  pipe_uuid, nt_errstr(status)));
873                         return status;
874                 }
875                 DEBUG(1,("Mapped to DCERPC/TCP port %u\n", port));
876         }
877
878         status = dcerpc_pipe_open_tcp(p, binding->host, port, AF_UNSPEC);
879         if (!NT_STATUS_IS_OK(status)) {
880                 DEBUG(0,("Failed to connect to %s:%d\n", binding->host, port));
881                 return status;
882         }
883
884         (*p)->flags = binding->flags;
885
886         /* remember the binding string for possible secondary connections */
887         (*p)->binding_string = dcerpc_binding_string((*p), binding);
888
889         if (username && username[0] && (binding->flags & DCERPC_SCHANNEL_ANY)) {
890                 status = dcerpc_bind_auth_schannel(*p, pipe_uuid, pipe_version, 
891                                                    domain, username, password);
892         } else if (username && username[0]) {
893                 status = dcerpc_bind_auth_ntlm(*p, pipe_uuid, pipe_version, domain, username, password);
894         } else {    
895                 status = dcerpc_bind_auth_none(*p, pipe_uuid, pipe_version);
896         }
897
898         if (!NT_STATUS_IS_OK(status)) {
899                 DEBUG(0,("Failed to bind to uuid %s - %s\n", 
900                          pipe_uuid, nt_errstr(status)));
901                 dcerpc_pipe_close(*p);
902                 *p = NULL;
903                 return status;
904         }
905  
906         return status;
907 }
908
909
910 /* open a rpc connection to a rpc pipe, using the specified 
911    binding structure to determine the endpoint and options */
912 NTSTATUS dcerpc_pipe_connect_b(struct dcerpc_pipe **p, 
913                                struct dcerpc_binding *binding,
914                                const char *pipe_uuid, 
915                                uint32_t pipe_version,
916                                const char *domain,
917                                const char *username,
918                                const char *password)
919 {
920         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
921
922         switch (binding->transport) {
923         case NCACN_NP:
924                 status = dcerpc_pipe_connect_ncacn_np(p, binding, pipe_uuid, pipe_version,
925                                                       domain, username, password);
926                 break;
927         case NCACN_IP_TCP:
928                 status = dcerpc_pipe_connect_ncacn_ip_tcp(p, binding, pipe_uuid, pipe_version,
929                                                           domain, username, password);
930                 break;
931         default:
932                 return NT_STATUS_NOT_SUPPORTED;
933         }
934
935         return status;
936 }
937
938
939 /* open a rpc connection to a rpc pipe, using the specified string
940    binding to determine the endpoint and options */
941 NTSTATUS dcerpc_pipe_connect(struct dcerpc_pipe **p, 
942                              const char *binding,
943                              const char *pipe_uuid, 
944                              uint32_t pipe_version,
945                              const char *domain,
946                              const char *username,
947                              const char *password)
948 {
949         struct dcerpc_binding b;
950         NTSTATUS status;
951         TALLOC_CTX *mem_ctx;
952
953         mem_ctx = talloc_init("dcerpc_pipe_connect");
954         if (!mem_ctx) return NT_STATUS_NO_MEMORY;
955
956         status = dcerpc_parse_binding(mem_ctx, binding, &b);
957         if (!NT_STATUS_IS_OK(status)) {
958                 DEBUG(0,("Failed to parse dcerpc binding '%s'\n", binding));
959                 talloc_destroy(mem_ctx);
960                 return status;
961         }
962
963         DEBUG(3,("Using binding %s\n", dcerpc_binding_string(mem_ctx, &b)));
964
965         status = dcerpc_pipe_connect_b(p, &b, pipe_uuid, pipe_version, domain, username, password);
966
967         talloc_destroy(mem_ctx);
968         return status;
969 }
970
971
972 /*
973   create a secondary dcerpc connection from a primary connection
974
975   if the primary is a SMB connection then the secondary connection
976   will be on the same SMB connection, but use a new fnum
977 */
978 NTSTATUS dcerpc_secondary_connection(struct dcerpc_pipe *p, struct dcerpc_pipe **p2,
979                                      const char *pipe_name,
980                                      const char *pipe_uuid,
981                                      uint32_t pipe_version)
982 {
983         struct smbcli_tree *tree;
984         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
985         struct dcerpc_binding b;
986
987         switch (p->transport.transport) {
988         case NCACN_NP:
989                 tree = dcerpc_smb_tree(p);
990                 if (!tree) {
991                         return NT_STATUS_INVALID_PARAMETER;
992                 }
993
994                 status = dcerpc_pipe_open_smb(p2, tree, pipe_name);
995                 break;
996
997         case NCACN_IP_TCP:
998                 status = dcerpc_parse_binding(p, p->binding_string, &b);
999                 if (!NT_STATUS_IS_OK(status)) {
1000                         return status;
1001                 }
1002                 b.flags &= ~DCERPC_AUTH_OPTIONS;
1003                 status = dcerpc_pipe_connect_ncacn_ip_tcp(p2, &b, pipe_uuid,
1004                                                           pipe_version, NULL, 
1005                                                           NULL, NULL);
1006                 break;
1007         default:
1008                 return NT_STATUS_NOT_SUPPORTED;
1009         }
1010
1011         if (!NT_STATUS_IS_OK(status)) {
1012                 return status;
1013         }
1014
1015         (*p2)->flags = p->flags;
1016
1017         status = dcerpc_bind_auth_none(*p2, pipe_uuid, pipe_version);
1018         if (!NT_STATUS_IS_OK(status)) {
1019                 return status;
1020         }
1021
1022         return NT_STATUS_OK;
1023 }
1024
1025 NTSTATUS dcerpc_generic_session_key(struct dcerpc_pipe *p,
1026                                     DATA_BLOB *session_key)
1027 {
1028         /* this took quite a few CPU cycles to find ... */
1029         session_key->data = discard_const_p(unsigned char, "SystemLibraryDTC");
1030         session_key->length = 16;
1031         return NT_STATUS_OK;
1032 }
1033
1034 /*
1035   fetch the user session key - may be default (above) or the SMB session key
1036 */
1037 NTSTATUS dcerpc_fetch_session_key(struct dcerpc_pipe *p,
1038                                   DATA_BLOB *session_key)
1039 {
1040         return p->security_state.session_key(p, session_key);
1041 }
1042
1043
1044 /*
1045   log a rpc packet in a format suitable for ndrdump. This is especially useful
1046   for sealed packets, where ethereal cannot easily see the contents
1047
1048   this triggers on a debug level of >= 10
1049 */
1050 void dcerpc_log_packet(const struct dcerpc_interface_table *ndr,
1051                        uint32_t opnum, uint32_t flags, DATA_BLOB *pkt)
1052 {
1053         const int num_examples = 20;
1054         int i;
1055
1056         if (DEBUGLEVEL < 10) return;
1057
1058         for (i=0;i<num_examples;i++) {
1059                 char *name=NULL;
1060                 asprintf(&name, "%s/rpclog/%s-%u.%d.%s", 
1061                          lp_lockdir(), ndr->name, opnum, i,
1062                          (flags&NDR_IN)?"in":"out");
1063                 if (name == NULL) {
1064                         return;
1065                 }
1066                 if (!file_exist(name, NULL)) {
1067                         if (file_save(name, pkt->data, pkt->length)) {
1068                                 DEBUG(10,("Logged rpc packet to %s\n", name));
1069                         }
1070                         free(name);
1071                         break;
1072                 }
1073                 free(name);
1074         }
1075 }
1076