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