9125f9e017259e97f63363f36ee1c65a87fc9ced
[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         { "ncadg_ip_udp", NCACN_IP_UDP, 3, 
249                 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UDP, EPM_PROTOCOL_IP } },
250         { "ncalrpc", NCALRPC, 2, 
251                 { EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_PIPE } },
252         { "ncacn_unix_stream", NCACN_UNIX_STREAM, 2, 
253                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_UNIX_DS } },
254         { "ncadg_unix_dgram", NCADG_UNIX_DGRAM, 2, 
255                 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UNIX_DS } },
256         { "ncacn_at_dsp", NCACN_AT_DSP, 3, 
257                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DSP } },
258         { "ncadg_at_ddp", NCADG_AT_DDP, 3, 
259                 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DDP } },
260         { "ncacn_vns_ssp", NCACN_VNS_SPP, 3, 
261                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_SPP } },
262         { "ncacn_vns_ipc", NCACN_VNS_IPC, 3, 
263                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_IPC }, },
264 };
265
266 static const struct {
267         const char *name;
268         uint32_t flag;
269 } ncacn_options[] = {
270         {"sign", DCERPC_SIGN},
271         {"seal", DCERPC_SEAL},
272         {"connect", DCERPC_CONNECT},
273         {"validate", DCERPC_DEBUG_VALIDATE_BOTH},
274         {"print", DCERPC_DEBUG_PRINT_BOTH},
275         {"padcheck", DCERPC_DEBUG_PAD_CHECK},
276         {"bigendian", DCERPC_PUSH_BIGENDIAN}
277 };
278
279
280
281 /*
282   form a binding string from a binding structure
283 */
284 const char *dcerpc_binding_string(TALLOC_CTX *mem_ctx, const struct dcerpc_binding *b)
285 {
286         char *s = NULL;
287         int i;
288         const char *t_name=NULL;
289
290         for (i=0;i<ARRAY_SIZE(transports);i++) {
291                 if (transports[i].transport == b->transport) {
292                         t_name = transports[i].name;
293                 }
294         }
295         if (!t_name) {
296                 return NULL;
297         }
298
299         if (!uuid_all_zero(&b->object)) { 
300                 s = talloc_asprintf(mem_ctx, "%s@", GUID_string(mem_ctx, &b->object));
301         }
302
303         s = talloc_asprintf_append(s, "%s:", t_name);
304         if (!s) return NULL;
305
306         if (b->host) {
307                 s = talloc_asprintf_append(s, "%s", b->host);
308         }
309
310         if ((!b->options || !b->options[0]) && !b->flags) {
311                 return s;
312         }
313
314         s = talloc_asprintf_append(s, "[");
315
316         /* this is a *really* inefficent way of dealing with strings,
317            but this is rarely called and the strings are always short,
318            so I don't care */
319         for (i=0;b->options && b->options[i];i++) {
320                 s = talloc_asprintf_append(s, "%s,", b->options[i]);
321                 if (!s) return NULL;
322         }
323         for (i=0;i<ARRAY_SIZE(ncacn_options);i++) {
324                 if (b->flags & ncacn_options[i].flag) {
325                         s = talloc_asprintf_append(s, "%s,", ncacn_options[i].name);
326                         if (!s) return NULL;
327                 }
328         }
329
330         s[strlen(s)-1] = 0;
331         s = talloc_asprintf_append(s, "]");
332
333         return s;
334 }
335
336 /*
337   parse a binding string into a dcerpc_binding structure
338 */
339 NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *s, struct dcerpc_binding *b)
340 {
341         char *options, *type;
342         char *p;
343         int i, j, comma_count;
344
345         p = strchr(s, '@');
346
347         if (p && PTR_DIFF(p, s) == 36) { /* 36 is the length of a UUID */
348                 NTSTATUS status;
349
350                 status = GUID_from_string(s, &b->object);
351
352                 if (NT_STATUS_IS_ERR(status)) {
353                         DEBUG(0, ("Failed parsing UUID\n"));
354                         return status;
355                 }
356
357                 s = p + 1;
358         } else {
359                 ZERO_STRUCT(b->object);
360         }
361
362         b->object_version = 0;
363
364         p = strchr(s, ':');
365         if (!p) {
366                 return NT_STATUS_INVALID_PARAMETER;
367         }
368
369         type = talloc_strndup(mem_ctx, s, PTR_DIFF(p, s));
370         if (!type) {
371                 return NT_STATUS_NO_MEMORY;
372         }
373
374         for (i=0;i<ARRAY_SIZE(transports);i++) {
375                 if (strcasecmp(type, transports[i].name) == 0) {
376                         b->transport = transports[i].transport;
377                         break;
378                 }
379         }
380         if (i==ARRAY_SIZE(transports)) {
381                 DEBUG(0,("Unknown dcerpc transport '%s'\n", type));
382                 return NT_STATUS_INVALID_PARAMETER;
383         }
384         
385         s = p+1;
386
387         p = strchr(s, '[');
388         if (p) {
389                 b->host = talloc_strndup(mem_ctx, s, PTR_DIFF(p, s));
390                 options = talloc_strdup(mem_ctx, p+1);
391                 if (options[strlen(options)-1] != ']') {
392                         return NT_STATUS_INVALID_PARAMETER;
393                 }
394                 options[strlen(options)-1] = 0;
395         } else {
396                 b->host = talloc_strdup(mem_ctx, s);
397                 options = NULL;
398         }
399
400         if (!b->host) {
401                 return NT_STATUS_NO_MEMORY;
402         }
403
404         b->options = NULL;
405         b->flags = 0;
406
407         if (!options) {
408                 return NT_STATUS_OK;
409         }
410
411         comma_count = count_chars(options, ',');
412         b->options = talloc_array_p(mem_ctx, const char *, comma_count+2);
413         if (!b->options) {
414                 return NT_STATUS_NO_MEMORY;
415         }
416
417         for (i=0; (p = strchr(options, ',')); i++) {
418                 b->options[i] = talloc_strndup(mem_ctx, options, PTR_DIFF(p, options));
419                 if (!b->options[i]) {
420                         return NT_STATUS_NO_MEMORY;
421                 }
422                 options = p+1;
423         }
424         b->options[i] = options;
425         b->options[i+1] = NULL;
426
427         /* some options are pre-parsed for convenience */
428         for (i=0;b->options[i];i++) {
429                 for (j=0;j<ARRAY_SIZE(ncacn_options);j++) {
430                         if (strcasecmp(ncacn_options[j].name, b->options[i]) == 0) {
431                                 int k;
432                                 b->flags |= ncacn_options[j].flag;
433                                 for (k=i;b->options[k];k++) {
434                                         b->options[k] = b->options[k+1];
435                                 }
436                                 i--;
437                                 break;
438                         }
439                 }
440         }
441         
442         return NT_STATUS_OK;
443 }
444
445 static const char *floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *floor)
446 {
447         switch (floor->lhs.protocol) {
448         case EPM_PROTOCOL_TCP:
449                 if (floor->rhs.tcp.port == 0) return NULL;
450                 return talloc_asprintf(mem_ctx, "%d", floor->rhs.tcp.port);
451                 
452         case EPM_PROTOCOL_UDP:
453                 if (floor->rhs.udp.port == 0) return NULL;
454                 return talloc_asprintf(mem_ctx, "%d", floor->rhs.udp.port);
455
456         case EPM_PROTOCOL_HTTP:
457                 if (floor->rhs.http.port == 0) return NULL;
458                 return talloc_asprintf(mem_ctx, "%d", floor->rhs.http.port);
459
460         case EPM_PROTOCOL_IP:
461                 if (floor->rhs.ip.address == 0) {
462                         return NULL; 
463                 }
464
465                 {
466                 struct in_addr in;
467                         in.s_addr = htonl(floor->rhs.ip.address);
468             return talloc_strdup(mem_ctx, inet_ntoa(in));
469                 }
470
471         case EPM_PROTOCOL_NCACN:
472                 return NULL;
473
474         case EPM_PROTOCOL_NCADG:
475                 return NULL;
476
477         case EPM_PROTOCOL_SMB:
478                 return talloc_strdup(mem_ctx, floor->rhs.smb.unc);
479
480         case EPM_PROTOCOL_PIPE:
481                 return talloc_strdup(mem_ctx, floor->rhs.pipe.path);
482
483         case EPM_PROTOCOL_NETBIOS:
484                 return talloc_strdup(mem_ctx, floor->rhs.netbios.name);
485
486         case EPM_PROTOCOL_NCALRPC:
487                 return NULL;
488                 
489         case EPM_PROTOCOL_VINES_SPP:
490                 return talloc_asprintf(mem_ctx, "%d", floor->rhs.vines_spp.port);
491                 
492         case EPM_PROTOCOL_VINES_IPC:
493                 return talloc_asprintf(mem_ctx, "%d", floor->rhs.vines_ipc.port);
494                 
495         case EPM_PROTOCOL_STREETTALK:
496                 return talloc_strdup(mem_ctx, floor->rhs.streettalk.streettalk);
497                 
498         case EPM_PROTOCOL_UNIX_DS:
499                 return talloc_strdup(mem_ctx, floor->rhs.unix_ds.path);
500                 
501         case EPM_PROTOCOL_NULL:
502                 return NULL;
503         }
504
505         return NULL;
506 }
507
508 static NTSTATUS floor_set_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *floor,  const char *data)
509 {
510         switch (floor->lhs.protocol) {
511         case EPM_PROTOCOL_TCP:
512                 floor->rhs.tcp.port = atoi(data);
513                 return NT_STATUS_OK;
514                 
515         case EPM_PROTOCOL_UDP:
516                 floor->rhs.udp.port = atoi(data);
517                 return NT_STATUS_OK;
518
519         case EPM_PROTOCOL_HTTP:
520                 floor->rhs.http.port = atoi(data);
521                 return NT_STATUS_OK;
522
523         case EPM_PROTOCOL_IP:
524                 floor->rhs.ip.address = 0;
525
526                 /* Only try to put in a IPv4 address. Windows 2003 just returns 
527                  * 0.0.0.0 for IPv6 addresses */
528                 if (strlen(data) > 0) {
529                     struct addrinfo hints, *res;
530
531                         memset(&hints, 0, sizeof(struct addrinfo));
532
533                         hints.ai_family = AF_INET;
534                         hints.ai_protocol = PF_INET;
535
536                         if (getaddrinfo(data, NULL, &hints, &res) < 0) {
537                                 return NT_STATUS_BAD_NETWORK_NAME;
538                         }
539
540                         floor->rhs.ip.address = ntohl(((struct sockaddr_in *)res->ai_addr)->sin_addr.s_addr);
541
542                         freeaddrinfo(res);
543                 }
544                 return NT_STATUS_OK;
545
546         case EPM_PROTOCOL_NCACN:
547                 floor->rhs.ncacn.minor_version = 0;
548                 return NT_STATUS_OK;
549
550         case EPM_PROTOCOL_NCADG:
551                 floor->rhs.ncadg.minor_version = 0;
552                 return NT_STATUS_OK;
553
554         case EPM_PROTOCOL_SMB:
555                 floor->rhs.smb.unc = talloc_strdup(mem_ctx, data);
556                 if (!floor->rhs.smb.unc) {
557                         return NT_STATUS_NO_MEMORY;
558                 }
559                 return NT_STATUS_OK;
560
561         case EPM_PROTOCOL_PIPE:
562                 floor->rhs.pipe.path = talloc_strdup(mem_ctx, data);
563                 if (!floor->rhs.pipe.path) {
564                         return NT_STATUS_NO_MEMORY;
565                 }
566                 return NT_STATUS_OK;
567
568         case EPM_PROTOCOL_NETBIOS:
569                 floor->rhs.netbios.name = talloc_strdup(mem_ctx, data);
570                 if (!floor->rhs.netbios.name) {
571                         return NT_STATUS_NO_MEMORY;
572                 }
573                 return NT_STATUS_OK;
574
575         case EPM_PROTOCOL_NCALRPC:
576                 return NT_STATUS_OK;
577                 
578         case EPM_PROTOCOL_VINES_SPP:
579                 floor->rhs.vines_spp.port = atoi(data);
580                 return NT_STATUS_OK;
581                 
582         case EPM_PROTOCOL_VINES_IPC:
583                 floor->rhs.vines_ipc.port = atoi(data);
584                 return NT_STATUS_OK;
585                 
586         case EPM_PROTOCOL_STREETTALK:
587                 floor->rhs.streettalk.streettalk = talloc_strdup(mem_ctx, data);
588                 if (!floor->rhs.streettalk.streettalk) {
589                         return NT_STATUS_NO_MEMORY;
590                 }
591                 return NT_STATUS_OK;
592                 
593         case EPM_PROTOCOL_UNIX_DS:
594                 floor->rhs.unix_ds.path = talloc_strdup(mem_ctx, data);
595                 if (!floor->rhs.unix_ds.path) {
596                         return NT_STATUS_NO_MEMORY;
597                 }
598                 return NT_STATUS_OK;
599                 
600         case EPM_PROTOCOL_NULL:
601                 return NT_STATUS_OK;
602         }
603
604         return NT_STATUS_NOT_SUPPORTED;
605 }
606
607 enum dcerpc_transport_t dcerpc_transport_by_tower(struct epm_tower *tower)
608 {
609         int i;
610
611         /* Find a transport that matches this tower */
612         for (i=0;i<ARRAY_SIZE(transports);i++) {
613                 int j;
614                 if (transports[i].num_protocols != tower->num_floors - 2) {
615                         continue; 
616                 }
617
618                 for (j = 0; j < transports[i].num_protocols; j++) {
619                         if (transports[i].protseq[j] != tower->floors[j+2].lhs.protocol) {
620                                 break;
621                         }
622                 }
623
624                 if (j == transports[i].num_protocols) {
625                         return transports[i].transport;
626                 }
627         }
628         
629         /* Unknown transport */
630         return -1;
631 }
632
633 NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx, struct epm_tower *tower, struct dcerpc_binding *binding)
634 {
635         ZERO_STRUCT(binding->object);
636         binding->options = NULL;
637         binding->host = NULL;
638         binding->flags = 0;
639
640         binding->transport = dcerpc_transport_by_tower(tower);
641
642         if (binding->transport == -1) {
643                 return NT_STATUS_NOT_SUPPORTED;
644         }
645
646         if (tower->num_floors < 1) {
647                 return NT_STATUS_OK;
648         }
649
650         /* Set object uuid */
651         binding->object = tower->floors[0].lhs.info.uuid.uuid;
652         binding->object_version = tower->floors[0].lhs.info.uuid.version;
653
654         /* Ignore floor 1, it contains the NDR version info */
655         
656         binding->options = talloc_array_p(mem_ctx, const char *, 2);
657
658         /* Set endpoint */
659         if (tower->num_floors >= 4) {
660                 binding->options[0] = floor_get_rhs_data(mem_ctx, &tower->floors[3]);
661         } else {
662                 binding->options[0] = NULL;
663         }
664         binding->options[1] = NULL;
665
666         /* Set network address */
667         if (tower->num_floors >= 5) {
668                 binding->host = floor_get_rhs_data(mem_ctx, &tower->floors[4]);
669         }
670         return NT_STATUS_OK;
671 }
672
673 NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx, struct dcerpc_binding *binding, struct epm_tower *tower)
674 {
675         const enum epm_protocols *protseq;
676         int num_protocols = -1, i;
677         NTSTATUS status;
678         
679         /* Find transport */
680         for (i=0;i<ARRAY_SIZE(transports);i++) {
681                 if (transports[i].transport == binding->transport) {
682                         protseq = transports[i].protseq;
683                         num_protocols = transports[i].num_protocols;
684                         break;
685                 }
686         }
687
688         if (num_protocols == -1) {
689                 DEBUG(0, ("Unable to find transport with id '%d'\n", binding->transport));
690                 return NT_STATUS_UNSUCCESSFUL;
691         }
692
693         tower->num_floors = 2 + num_protocols;
694         tower->floors = talloc_array_p(mem_ctx, struct epm_floor, tower->num_floors);
695
696         /* Floor 0 */
697         tower->floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
698         tower->floors[0].lhs.info.uuid.uuid = binding->object;
699         tower->floors[0].lhs.info.uuid.version = binding->object_version;
700         tower->floors[0].rhs.uuid.unknown = 0;
701         
702         /* Floor 1 */
703         tower->floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
704         tower->floors[1].lhs.info.uuid.version = NDR_GUID_VERSION;
705         tower->floors[1].rhs.uuid.unknown = 0;
706         status = GUID_from_string(NDR_GUID, &tower->floors[1].lhs.info.uuid.uuid);
707         if (NT_STATUS_IS_ERR(status)) {
708                 return status;
709         }
710
711         /* Floor 2 to num_protocols */
712         for (i = 0; i < num_protocols; i++) {
713                 tower->floors[2 + i].lhs.protocol = protseq[i];
714                 tower->floors[2 + i].lhs.info.lhs_data = data_blob_talloc(mem_ctx, NULL, 0);
715                 ZERO_STRUCT(tower->floors[2 + i].rhs);
716         }
717
718         /* The 4th floor contains the endpoint */
719         if (num_protocols >= 2 && binding->options && binding->options[0]) {
720                 status = floor_set_rhs_data(mem_ctx, &tower->floors[3], binding->options[0]);
721                 if (NT_STATUS_IS_ERR(status)) {
722                         return status;
723                 }
724         }
725         /* The 5th contains the network address */
726         if (num_protocols >= 3 && binding->host) {
727                 status = floor_set_rhs_data(mem_ctx, &tower->floors[4], binding->host);
728                 if (NT_STATUS_IS_ERR(status)) {
729                         return status;
730                 }
731         }
732
733         return NT_STATUS_OK;
734 }
735
736
737 /* open a rpc connection to a rpc pipe on SMB using the binding
738    structure to determine the endpoint and options */
739 static NTSTATUS dcerpc_pipe_connect_ncacn_np(struct dcerpc_pipe **p, 
740                                              struct dcerpc_binding *binding,
741                                              const char *pipe_uuid, 
742                                              uint32_t pipe_version,
743                                              const char *domain,
744                                              const char *username,
745                                              const char *password)
746 {
747         NTSTATUS status;
748         BOOL retry;
749         struct smbcli_state *cli;
750         const char *pipe_name;
751         TALLOC_CTX *mem_ctx = talloc_init("dcerpc_pipe_connect_ncacn_np");
752         
753         if (!binding->options || !binding->options[0] || !strlen(binding->options[0])) {
754                 const struct dcerpc_interface_table *table = idl_iface_by_uuid(pipe_uuid);
755                 struct dcerpc_binding default_binding;
756                 int i;
757
758                 if (!table) {
759                         DEBUG(0,("Unknown interface endpoint '%s'\n", pipe_uuid));
760                         talloc_destroy(mem_ctx);
761                         return NT_STATUS_INVALID_PARAMETER;
762                 }
763
764                 /* Find one of the default pipes for this interface */
765                 for (i = 0; i < table->endpoints->count; i++) {
766                         status = dcerpc_parse_binding(mem_ctx, table->endpoints->names[i], &default_binding);
767
768                         if (NT_STATUS_IS_OK(status) && default_binding.transport == NCACN_NP) {
769                                 pipe_name = default_binding.options[0]; 
770                                 break;
771                                 
772                         }
773                 }
774         } else {
775                 pipe_name = binding->options[0];
776         }
777
778         if (!strncasecmp(pipe_name, "/pipe/", 6) || 
779                 !strncasecmp(pipe_name, "\\pipe\\", 6)) {
780                 pipe_name+=6;
781         }
782
783         if (pipe_name[0] != '\\') {
784                 pipe_name = talloc_asprintf(mem_ctx, "\\%s", pipe_name);
785         }
786         
787         if (!username || !username[0]) {
788                 status = smbcli_full_connection(NULL, &cli, lp_netbios_name(),
789                                              binding->host, NULL, 
790                                              "ipc$", "?????", 
791                                              "", "", NULL, 0, &retry);
792         } else {
793                 status = smbcli_full_connection(NULL, &cli, lp_netbios_name(),
794                                              binding->host, NULL, 
795                                              "ipc$", "?????", 
796                                              username, domain,
797                                              password, 0, &retry);
798         }
799         if (!NT_STATUS_IS_OK(status)) {
800                 DEBUG(0,("Failed to connect to %s - %s\n", binding->host, nt_errstr(status)));
801                 talloc_destroy(mem_ctx);
802                 return status;
803         }
804
805         status = dcerpc_pipe_open_smb(p, cli->tree, pipe_name);
806         if (!NT_STATUS_IS_OK(status)) {
807                 DEBUG(0,("Failed to open pipe %s - %s\n", pipe_name, nt_errstr(status)));
808                 smbcli_tdis(cli);
809                 smbcli_shutdown(cli);
810                 talloc_destroy(mem_ctx);
811         return status;
812     }   
813
814         talloc_destroy(mem_ctx);
815         
816         /* this ensures that the reference count is decremented so
817            a pipe close will really close the link */
818         talloc_steal(*p, cli);
819
820         (*p)->flags = binding->flags;
821
822         /* remember the binding string for possible secondary connections */
823         (*p)->binding_string = dcerpc_binding_string((*p), binding);
824
825         if (username && username[0] && (binding->flags & DCERPC_SCHANNEL_ANY)) {
826                 status = dcerpc_bind_auth_schannel(*p, pipe_uuid, pipe_version, 
827                                                    domain, username, password);
828         } else if (username && username[0] &&
829                    (binding->flags & (DCERPC_CONNECT|DCERPC_SIGN|DCERPC_SEAL))) {
830                 status = dcerpc_bind_auth_ntlm(*p, pipe_uuid, pipe_version, domain, username, password);
831         } else {    
832                 status = dcerpc_bind_auth_none(*p, pipe_uuid, pipe_version);
833
834         }
835
836         if (!NT_STATUS_IS_OK(status)) {
837                 DEBUG(0,("Failed to bind to uuid %s - %s\n", pipe_uuid, nt_errstr(status)));
838                 dcerpc_pipe_close(*p);
839                 *p = NULL;
840                 return status;
841         }
842
843         return NT_STATUS_OK;
844 }
845
846
847 /* open a rpc connection to a rpc pipe on SMP using the binding
848    structure to determine the endpoint and options */
849 static NTSTATUS dcerpc_pipe_connect_ncacn_ip_tcp(struct dcerpc_pipe **p, 
850                                                  struct dcerpc_binding *binding,
851                                                  const char *pipe_uuid, 
852                                                  uint32_t pipe_version,
853                                                  const char *domain,
854                                                  const char *username,
855                                                  const char *password)
856 {
857         NTSTATUS status;
858         uint32_t port = 0;
859
860         if (binding->options && binding->options[0] && strlen(binding->options[0])) {
861                 port = atoi(binding->options[0]);
862         }
863
864         if (port == 0) {
865                 status = dcerpc_epm_map_tcp_port(binding->host, 
866                                                  pipe_uuid, pipe_version,
867                                                  &port);
868                 if (!NT_STATUS_IS_OK(status)) {
869                         DEBUG(0,("Failed to map DCERPC/TCP port for '%s' - %s\n", 
870                                  pipe_uuid, nt_errstr(status)));
871                         return status;
872                 }
873                 DEBUG(1,("Mapped to DCERPC/TCP port %u\n", port));
874         }
875
876         status = dcerpc_pipe_open_tcp(p, binding->host, port, AF_UNSPEC);
877         if (!NT_STATUS_IS_OK(status)) {
878                 DEBUG(0,("Failed to connect to %s:%d\n", binding->host, port));
879                 return status;
880         }
881
882         (*p)->flags = binding->flags;
883
884         /* remember the binding string for possible secondary connections */
885         (*p)->binding_string = dcerpc_binding_string((*p), binding);
886
887         if (username && username[0] && (binding->flags & DCERPC_SCHANNEL_ANY)) {
888                 status = dcerpc_bind_auth_schannel(*p, pipe_uuid, pipe_version, 
889                                                    domain, username, password);
890         } else if (username && username[0]) {
891                 status = dcerpc_bind_auth_ntlm(*p, pipe_uuid, pipe_version, domain, username, password);
892         } else {    
893                 status = dcerpc_bind_auth_none(*p, pipe_uuid, pipe_version);
894         }
895
896         if (!NT_STATUS_IS_OK(status)) {
897                 DEBUG(0,("Failed to bind to uuid %s - %s\n", 
898                          pipe_uuid, nt_errstr(status)));
899                 dcerpc_pipe_close(*p);
900                 *p = NULL;
901                 return status;
902         }
903  
904         return status;
905 }
906
907
908 /* open a rpc connection to a rpc pipe, using the specified 
909    binding structure to determine the endpoint and options */
910 NTSTATUS dcerpc_pipe_connect_b(struct dcerpc_pipe **p, 
911                                struct dcerpc_binding *binding,
912                                const char *pipe_uuid, 
913                                uint32_t pipe_version,
914                                const char *domain,
915                                const char *username,
916                                const char *password)
917 {
918         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
919
920         switch (binding->transport) {
921         case NCACN_NP:
922                 status = dcerpc_pipe_connect_ncacn_np(p, binding, pipe_uuid, pipe_version,
923                                                       domain, username, password);
924                 break;
925         case NCACN_IP_TCP:
926                 status = dcerpc_pipe_connect_ncacn_ip_tcp(p, binding, pipe_uuid, pipe_version,
927                                                           domain, username, password);
928                 break;
929         default:
930                 return NT_STATUS_NOT_SUPPORTED;
931         }
932
933         return status;
934 }
935
936
937 /* open a rpc connection to a rpc pipe, using the specified string
938    binding to determine the endpoint and options */
939 NTSTATUS dcerpc_pipe_connect(struct dcerpc_pipe **p, 
940                              const char *binding,
941                              const char *pipe_uuid, 
942                              uint32_t pipe_version,
943                              const char *domain,
944                              const char *username,
945                              const char *password)
946 {
947         struct dcerpc_binding b;
948         NTSTATUS status;
949         TALLOC_CTX *mem_ctx;
950
951         mem_ctx = talloc_init("dcerpc_pipe_connect");
952         if (!mem_ctx) return NT_STATUS_NO_MEMORY;
953
954         status = dcerpc_parse_binding(mem_ctx, binding, &b);
955         if (!NT_STATUS_IS_OK(status)) {
956                 DEBUG(0,("Failed to parse dcerpc binding '%s'\n", binding));
957                 talloc_destroy(mem_ctx);
958                 return status;
959         }
960
961         DEBUG(3,("Using binding %s\n", dcerpc_binding_string(mem_ctx, &b)));
962
963         status = dcerpc_pipe_connect_b(p, &b, pipe_uuid, pipe_version, domain, username, password);
964
965         talloc_destroy(mem_ctx);
966         return status;
967 }
968
969
970 /*
971   create a secondary dcerpc connection from a primary connection
972
973   if the primary is a SMB connection then the secondary connection
974   will be on the same SMB connection, but use a new fnum
975 */
976 NTSTATUS dcerpc_secondary_connection(struct dcerpc_pipe *p, struct dcerpc_pipe **p2,
977                                      const char *pipe_name,
978                                      const char *pipe_uuid,
979                                      uint32_t pipe_version)
980 {
981         struct smbcli_tree *tree;
982         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
983         struct dcerpc_binding b;
984
985         switch (p->transport.transport) {
986         case NCACN_NP:
987                 tree = dcerpc_smb_tree(p);
988                 if (!tree) {
989                         return NT_STATUS_INVALID_PARAMETER;
990                 }
991
992                 status = dcerpc_pipe_open_smb(p2, tree, pipe_name);
993                 break;
994
995         case NCACN_IP_TCP:
996                 status = dcerpc_parse_binding(p, p->binding_string, &b);
997                 if (!NT_STATUS_IS_OK(status)) {
998                         return status;
999                 }
1000                 b.flags &= ~DCERPC_AUTH_OPTIONS;
1001                 status = dcerpc_pipe_connect_ncacn_ip_tcp(p2, &b, pipe_uuid,
1002                                                           pipe_version, NULL, 
1003                                                           NULL, NULL);
1004                 break;
1005         default:
1006                 return NT_STATUS_NOT_SUPPORTED;
1007         }
1008
1009         if (!NT_STATUS_IS_OK(status)) {
1010                 return status;
1011         }
1012
1013         (*p2)->flags = p->flags;
1014
1015         status = dcerpc_bind_auth_none(*p2, pipe_uuid, pipe_version);
1016         if (!NT_STATUS_IS_OK(status)) {
1017                 return status;
1018         }
1019
1020         return NT_STATUS_OK;
1021 }
1022
1023 NTSTATUS dcerpc_generic_session_key(struct dcerpc_pipe *p,
1024                                     DATA_BLOB *session_key)
1025 {
1026         /* this took quite a few CPU cycles to find ... */
1027         session_key->data = discard_const_p(unsigned char, "SystemLibraryDTC");
1028         session_key->length = 16;
1029         return NT_STATUS_OK;
1030 }
1031
1032 /*
1033   fetch the user session key - may be default (above) or the SMB session key
1034 */
1035 NTSTATUS dcerpc_fetch_session_key(struct dcerpc_pipe *p,
1036                                   DATA_BLOB *session_key)
1037 {
1038         return p->security_state.session_key(p, session_key);
1039 }
1040
1041
1042 /*
1043   log a rpc packet in a format suitable for ndrdump. This is especially useful
1044   for sealed packets, where ethereal cannot easily see the contents
1045
1046   this triggers on a debug level of >= 10
1047 */
1048 void dcerpc_log_packet(const struct dcerpc_interface_table *ndr,
1049                        uint32_t opnum, uint32_t flags, DATA_BLOB *pkt)
1050 {
1051         const int num_examples = 20;
1052         int i;
1053
1054         if (DEBUGLEVEL < 10) return;
1055
1056         for (i=0;i<num_examples;i++) {
1057                 char *name=NULL;
1058                 asprintf(&name, "%s/rpclog/%s-%u.%d.%s", 
1059                          lp_lockdir(), ndr->name, opnum, i,
1060                          (flags&NDR_IN)?"in":"out");
1061                 if (name == NULL) {
1062                         return;
1063                 }
1064                 if (!file_exist(name, NULL)) {
1065                         if (file_save(name, pkt->data, pkt->length)) {
1066                                 DEBUG(10,("Logged rpc packet to %s\n", name));
1067                         }
1068                         free(name);
1069                         break;
1070                 }
1071                 free(name);
1072         }
1073 }
1074