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