20c3a389f16fcc838c1240c8faeeacc2da0ed5ae
[abartlet/samba.git/.git] / librpc / rpc / binding.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    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
9    Copyright (C) Rafal Szczesniak 2006
10
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include "includes.h"
26 #include "librpc/gen_ndr/ndr_epmapper.h"
27 #include "librpc/gen_ndr/ndr_misc.h"
28 #include "librpc/rpc/dcerpc.h"
29 #undef strcasecmp
30
31 #define MAX_PROTSEQ             10
32
33 static const struct {
34         const char *name;
35         enum dcerpc_transport_t transport;
36         int num_protocols;
37         enum epm_protocol protseq[MAX_PROTSEQ];
38 } transports[] = {
39         { "ncacn_np",     NCACN_NP, 3, 
40                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SMB, EPM_PROTOCOL_NETBIOS }},
41         { "ncacn_ip_tcp", NCACN_IP_TCP, 3, 
42                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_TCP, EPM_PROTOCOL_IP } }, 
43         { "ncacn_http", NCACN_HTTP, 3, 
44                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_HTTP, EPM_PROTOCOL_IP } }, 
45         { "ncadg_ip_udp", NCACN_IP_UDP, 3, 
46                 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UDP, EPM_PROTOCOL_IP } },
47         { "ncalrpc", NCALRPC, 2, 
48                 { EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_NAMED_PIPE } },
49         { "ncacn_unix_stream", NCACN_UNIX_STREAM, 2, 
50                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_UNIX_DS } },
51         { "ncadg_unix_dgram", NCADG_UNIX_DGRAM, 2, 
52                 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UNIX_DS } },
53         { "ncacn_at_dsp", NCACN_AT_DSP, 3, 
54                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DSP } },
55         { "ncadg_at_ddp", NCADG_AT_DDP, 3, 
56                 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DDP } },
57         { "ncacn_vns_ssp", NCACN_VNS_SPP, 3, 
58                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_SPP } },
59         { "ncacn_vns_ipc", NCACN_VNS_IPC, 3, 
60                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_IPC }, },
61         { "ncadg_ipx", NCADG_IPX, 2,
62                 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_IPX },
63         },
64         { "ncacn_spx", NCACN_SPX, 3,
65                 /* I guess some MS programmer confused the identifier for 
66                  * EPM_PROTOCOL_UUID (0x0D or 13) with the one for 
67                  * EPM_PROTOCOL_SPX (0x13) here. -- jelmer*/
68                 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_UUID },
69         },
70 };
71
72 static const struct {
73         const char *name;
74         uint32_t flag;
75 } ncacn_options[] = {
76         {"sign", DCERPC_SIGN},
77         {"seal", DCERPC_SEAL},
78         {"connect", DCERPC_CONNECT},
79         {"spnego", DCERPC_AUTH_SPNEGO},
80         {"ntlm", DCERPC_AUTH_NTLM},
81         {"krb5", DCERPC_AUTH_KRB5},
82         {"validate", DCERPC_DEBUG_VALIDATE_BOTH},
83         {"print", DCERPC_DEBUG_PRINT_BOTH},
84         {"padcheck", DCERPC_DEBUG_PAD_CHECK},
85         {"bigendian", DCERPC_PUSH_BIGENDIAN},
86         {"smb2", DCERPC_SMB2},
87         {"hdrsign", DCERPC_HEADER_SIGNING},
88         {"ndr64", DCERPC_NDR64}
89 };
90
91 const char *epm_floor_string(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
92 {
93         struct ndr_syntax_id syntax;
94         NTSTATUS status;
95
96         switch(epm_floor->lhs.protocol) {
97                 case EPM_PROTOCOL_UUID:
98                         status = dcerpc_floor_get_lhs_data(epm_floor, &syntax);
99                         if (NT_STATUS_IS_OK(status)) {
100                                 /* lhs is used: UUID */
101                                 char *uuidstr;
102
103                                 if (GUID_equal(&syntax.uuid, &ndr_transfer_syntax.uuid)) {
104                                         return "NDR";
105                                 } 
106
107                                 if (GUID_equal(&syntax.uuid, &ndr64_transfer_syntax.uuid)) {
108                                         return "NDR64";
109                                 } 
110
111                                 uuidstr = GUID_string(mem_ctx, &syntax.uuid);
112
113                                 return talloc_asprintf(mem_ctx, " uuid %s/0x%02x", uuidstr, syntax.if_version);
114                         } else { /* IPX */
115                                 return talloc_asprintf(mem_ctx, "IPX:%s", 
116                                                 data_blob_hex_string_upper(mem_ctx, &epm_floor->rhs.uuid.unknown));
117                         }
118
119                 case EPM_PROTOCOL_NCACN:
120                         return "RPC-C";
121
122                 case EPM_PROTOCOL_NCADG:
123                         return "RPC";
124
125                 case EPM_PROTOCOL_NCALRPC:
126                         return "NCALRPC";
127
128                 case EPM_PROTOCOL_DNET_NSP:
129                         return "DNET/NSP";
130
131                 case EPM_PROTOCOL_IP:
132                         return talloc_asprintf(mem_ctx, "IP:%s", epm_floor->rhs.ip.ipaddr);
133
134                 case EPM_PROTOCOL_NAMED_PIPE:
135                         return talloc_asprintf(mem_ctx, "NAMED-PIPE:%s", epm_floor->rhs.named_pipe.path);
136
137                 case EPM_PROTOCOL_SMB:
138                         return talloc_asprintf(mem_ctx, "SMB:%s", epm_floor->rhs.smb.unc);
139
140                 case EPM_PROTOCOL_UNIX_DS:
141                         return talloc_asprintf(mem_ctx, "Unix:%s", epm_floor->rhs.unix_ds.path);
142
143                 case EPM_PROTOCOL_NETBIOS:
144                         return talloc_asprintf(mem_ctx, "NetBIOS:%s", epm_floor->rhs.netbios.name);
145
146                 case EPM_PROTOCOL_NETBEUI:
147                         return "NETBeui";
148
149                 case EPM_PROTOCOL_SPX:
150                         return "SPX";
151
152                 case EPM_PROTOCOL_NB_IPX:
153                         return "NB_IPX";
154
155                 case EPM_PROTOCOL_HTTP:
156                         return talloc_asprintf(mem_ctx, "HTTP:%d", epm_floor->rhs.http.port);
157
158                 case EPM_PROTOCOL_TCP:
159                         return talloc_asprintf(mem_ctx, "TCP:%d", epm_floor->rhs.tcp.port);
160
161                 case EPM_PROTOCOL_UDP:
162                         return talloc_asprintf(mem_ctx, "UDP:%d", epm_floor->rhs.udp.port);
163
164                 default:
165                         return talloc_asprintf(mem_ctx, "UNK(%02x):", epm_floor->lhs.protocol);
166         }
167 }
168
169
170 /*
171   form a binding string from a binding structure
172 */
173 _PUBLIC_ char *dcerpc_binding_string(TALLOC_CTX *mem_ctx, const struct dcerpc_binding *b)
174 {
175         char *s = talloc_strdup(mem_ctx, "");
176         int i;
177         const char *t_name = NULL;
178
179         if (b->transport != NCA_UNKNOWN) {
180                 t_name = derpc_transport_string_by_transport(b->transport);
181                 if (!t_name) {
182                         return NULL;
183                 }
184         }
185
186         if (!GUID_all_zero(&b->object.uuid)) { 
187                 s = talloc_asprintf(s, "%s@",
188                                     GUID_string(mem_ctx, &b->object.uuid));
189         }
190
191         if (t_name != NULL) {
192                 s = talloc_asprintf_append_buffer(s, "%s:", t_name);
193                 if (s == NULL) {
194                         return NULL;
195                 }
196         }
197
198         if (b->host) {
199                 s = talloc_asprintf_append_buffer(s, "%s", b->host);
200         }
201
202         if (!b->endpoint && !b->options && !b->flags) {
203                 return s;
204         }
205
206         s = talloc_asprintf_append_buffer(s, "[");
207
208         if (b->endpoint) {
209                 s = talloc_asprintf_append_buffer(s, "%s", b->endpoint);
210         }
211
212         /* this is a *really* inefficent way of dealing with strings,
213            but this is rarely called and the strings are always short,
214            so I don't care */
215         for (i=0;b->options && b->options[i];i++) {
216                 s = talloc_asprintf_append_buffer(s, ",%s", b->options[i]);
217                 if (!s) return NULL;
218         }
219
220         for (i=0;i<ARRAY_SIZE(ncacn_options);i++) {
221                 if (b->flags & ncacn_options[i].flag) {
222                         s = talloc_asprintf_append_buffer(s, ",%s", ncacn_options[i].name);
223                         if (!s) return NULL;
224                 }
225         }
226
227         s = talloc_asprintf_append_buffer(s, "]");
228
229         return s;
230 }
231
232 /*
233   parse a binding string into a dcerpc_binding structure
234 */
235 _PUBLIC_ NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *s, struct dcerpc_binding **b_out)
236 {
237         struct dcerpc_binding *b;
238         char *options;
239         char *p;
240         int i, j, comma_count;
241
242         b = talloc(mem_ctx, struct dcerpc_binding);
243         if (!b) {
244                 return NT_STATUS_NO_MEMORY;
245         }
246
247         p = strchr(s, '@');
248
249         if (p && PTR_DIFF(p, s) == 36) { /* 36 is the length of a UUID */
250                 NTSTATUS status;
251                 DATA_BLOB blob = data_blob(s, 36);
252                 status = GUID_from_data_blob(&blob, &b->object.uuid);
253
254                 if (NT_STATUS_IS_ERR(status)) {
255                         DEBUG(0, ("Failed parsing UUID\n"));
256                         return status;
257                 }
258
259                 s = p + 1;
260         } else {
261                 ZERO_STRUCT(b->object);
262         }
263
264         b->object.if_version = 0;
265
266         p = strchr(s, ':');
267
268         if (p == NULL) {
269                 b->transport = NCA_UNKNOWN;
270         } else {
271                 char *type = talloc_strndup(mem_ctx, s, PTR_DIFF(p, s));
272                 if (!type) {
273                         return NT_STATUS_NO_MEMORY;
274                 }
275
276                 for (i=0;i<ARRAY_SIZE(transports);i++) {
277                         if (strcasecmp(type, transports[i].name) == 0) {
278                                 b->transport = transports[i].transport;
279                                 break;
280                         }
281                 }
282
283                 if (i==ARRAY_SIZE(transports)) {
284                         DEBUG(0,("Unknown dcerpc transport '%s'\n", type));
285                         return NT_STATUS_INVALID_PARAMETER;
286                 }
287
288                 talloc_free(type);
289
290                 s = p+1;
291         }
292
293         p = strchr(s, '[');
294         if (p) {
295                 b->host = talloc_strndup(b, s, PTR_DIFF(p, s));
296                 options = talloc_strdup(mem_ctx, p+1);
297                 if (options[strlen(options)-1] != ']') {
298                         return NT_STATUS_INVALID_PARAMETER;
299                 }
300                 options[strlen(options)-1] = 0;
301         } else {
302                 b->host = talloc_strdup(b, s);
303                 options = NULL;
304         }
305         if (!b->host) {
306                 return NT_STATUS_NO_MEMORY;
307         }
308
309         b->target_hostname = b->host;
310
311         b->options = NULL;
312         b->flags = 0;
313         b->assoc_group_id = 0;
314         b->endpoint = NULL;
315
316         if (!options) {
317                 *b_out = b;
318                 return NT_STATUS_OK;
319         }
320
321         comma_count = count_chars(options, ',');
322
323         b->options = talloc_array(b, const char *, comma_count+2);
324         if (!b->options) {
325                 return NT_STATUS_NO_MEMORY;
326         }
327
328         for (i=0; (p = strchr(options, ',')); i++) {
329                 b->options[i] = talloc_strndup(b, options, PTR_DIFF(p, options));
330                 if (!b->options[i]) {
331                         return NT_STATUS_NO_MEMORY;
332                 }
333                 options = p+1;
334         }
335         b->options[i] = options;
336         b->options[i+1] = NULL;
337
338         /* some options are pre-parsed for convenience */
339         for (i=0;b->options[i];i++) {
340                 for (j=0;j<ARRAY_SIZE(ncacn_options);j++) {
341                         if (strcasecmp(ncacn_options[j].name, b->options[i]) == 0) {
342                                 int k;
343                                 b->flags |= ncacn_options[j].flag;
344                                 for (k=i;b->options[k];k++) {
345                                         b->options[k] = b->options[k+1];
346                                 }
347                                 i--;
348                                 break;
349                         }
350                 }
351         }
352
353         if (b->options[0]) {
354                 /* Endpoint is first option */
355                 b->endpoint = b->options[0];
356                 if (strlen(b->endpoint) == 0) b->endpoint = NULL;
357
358                 for (i=0;b->options[i];i++) {
359                         b->options[i] = b->options[i+1];
360                 }
361         }
362
363         if (b->options[0] == NULL)
364                 b->options = NULL;
365
366         *b_out = b;
367         return NT_STATUS_OK;
368 }
369
370 _PUBLIC_ NTSTATUS dcerpc_floor_get_lhs_data(const struct epm_floor *epm_floor,
371                                             struct ndr_syntax_id *syntax)
372 {
373         TALLOC_CTX *mem_ctx = talloc_init("floor_get_lhs_data");
374         struct ndr_pull *ndr;
375         enum ndr_err_code ndr_err;
376         uint16_t if_version=0;
377
378         ndr = ndr_pull_init_blob(&epm_floor->lhs.lhs_data, mem_ctx, NULL);
379         if (ndr == NULL) {
380                 talloc_free(mem_ctx);
381                 return NT_STATUS_NO_MEMORY;
382         }
383         ndr->flags |= LIBNDR_FLAG_NOALIGN;
384
385         ndr_err = ndr_pull_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
386         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
387                 talloc_free(mem_ctx);
388                 return ndr_map_error2ntstatus(ndr_err);
389         }
390
391         ndr_err = ndr_pull_uint16(ndr, NDR_SCALARS, &if_version);
392         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
393                 talloc_free(mem_ctx);
394                 return ndr_map_error2ntstatus(ndr_err);
395         }
396
397         syntax->if_version = if_version;
398
399         talloc_free(mem_ctx);
400
401         return NT_STATUS_OK;
402 }
403
404 static DATA_BLOB dcerpc_floor_pack_lhs_data(TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax)
405 {
406         DATA_BLOB blob;
407         struct ndr_push *ndr = ndr_push_init_ctx(mem_ctx, NULL);
408
409         ndr->flags |= LIBNDR_FLAG_NOALIGN;
410
411         ndr_push_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
412         ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version);
413
414         blob = ndr_push_blob(ndr);
415         talloc_steal(mem_ctx, blob.data);
416         talloc_free(ndr);
417         return blob;
418 }
419
420 const char *dcerpc_floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
421 {
422         switch (epm_floor->lhs.protocol) {
423         case EPM_PROTOCOL_TCP:
424                 if (epm_floor->rhs.tcp.port == 0) return NULL;
425                 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.tcp.port);
426
427         case EPM_PROTOCOL_UDP:
428                 if (epm_floor->rhs.udp.port == 0) return NULL;
429                 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.udp.port);
430
431         case EPM_PROTOCOL_HTTP:
432                 if (epm_floor->rhs.http.port == 0) return NULL;
433                 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.http.port);
434
435         case EPM_PROTOCOL_IP:
436                 return talloc_strdup(mem_ctx, epm_floor->rhs.ip.ipaddr);
437
438         case EPM_PROTOCOL_NCACN:
439                 return NULL;
440
441         case EPM_PROTOCOL_NCADG:
442                 return NULL;
443
444         case EPM_PROTOCOL_SMB:
445                 if (strlen(epm_floor->rhs.smb.unc) == 0) return NULL;
446                 return talloc_strdup(mem_ctx, epm_floor->rhs.smb.unc);
447
448         case EPM_PROTOCOL_NAMED_PIPE:
449                 if (strlen(epm_floor->rhs.named_pipe.path) == 0) return NULL;
450                 return talloc_strdup(mem_ctx, epm_floor->rhs.named_pipe.path);
451
452         case EPM_PROTOCOL_NETBIOS:
453                 if (strlen(epm_floor->rhs.netbios.name) == 0) return NULL;
454                 return talloc_strdup(mem_ctx, epm_floor->rhs.netbios.name);
455
456         case EPM_PROTOCOL_NCALRPC:
457                 return NULL;
458
459         case EPM_PROTOCOL_VINES_SPP:
460                 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_spp.port);
461
462         case EPM_PROTOCOL_VINES_IPC:
463                 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_ipc.port);
464
465         case EPM_PROTOCOL_STREETTALK:
466                 return talloc_strdup(mem_ctx, epm_floor->rhs.streettalk.streettalk);
467
468         case EPM_PROTOCOL_UNIX_DS:
469                 if (strlen(epm_floor->rhs.unix_ds.path) == 0) return NULL;
470                 return talloc_strdup(mem_ctx, epm_floor->rhs.unix_ds.path);
471
472         case EPM_PROTOCOL_NULL:
473                 return NULL;
474
475         default:
476                 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
477                 break;
478         }
479
480         return NULL;
481 }
482
483 static NTSTATUS dcerpc_floor_set_rhs_data(TALLOC_CTX *mem_ctx, 
484                                           struct epm_floor *epm_floor,  
485                                           const char *data)
486 {
487         switch (epm_floor->lhs.protocol) {
488         case EPM_PROTOCOL_TCP:
489                 epm_floor->rhs.tcp.port = atoi(data);
490                 return NT_STATUS_OK;
491
492         case EPM_PROTOCOL_UDP:
493                 epm_floor->rhs.udp.port = atoi(data);
494                 return NT_STATUS_OK;
495
496         case EPM_PROTOCOL_HTTP:
497                 epm_floor->rhs.http.port = atoi(data);
498                 return NT_STATUS_OK;
499
500         case EPM_PROTOCOL_IP:
501                 epm_floor->rhs.ip.ipaddr = talloc_strdup(mem_ctx, data);
502                 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.ip.ipaddr);
503                 return NT_STATUS_OK;
504
505         case EPM_PROTOCOL_NCACN:
506                 epm_floor->rhs.ncacn.minor_version = 0;
507                 return NT_STATUS_OK;
508
509         case EPM_PROTOCOL_NCADG:
510                 epm_floor->rhs.ncadg.minor_version = 0;
511                 return NT_STATUS_OK;
512
513         case EPM_PROTOCOL_SMB:
514                 epm_floor->rhs.smb.unc = talloc_strdup(mem_ctx, data);
515                 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.smb.unc);
516                 return NT_STATUS_OK;
517
518         case EPM_PROTOCOL_NAMED_PIPE:
519                 epm_floor->rhs.named_pipe.path = talloc_strdup(mem_ctx, data);
520                 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.named_pipe.path);
521                 return NT_STATUS_OK;
522
523         case EPM_PROTOCOL_NETBIOS:
524                 epm_floor->rhs.netbios.name = talloc_strdup(mem_ctx, data);
525                 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.netbios.name);
526                 return NT_STATUS_OK;
527
528         case EPM_PROTOCOL_NCALRPC:
529                 return NT_STATUS_OK;
530
531         case EPM_PROTOCOL_VINES_SPP:
532                 epm_floor->rhs.vines_spp.port = atoi(data);
533                 return NT_STATUS_OK;
534
535         case EPM_PROTOCOL_VINES_IPC:
536                 epm_floor->rhs.vines_ipc.port = atoi(data);
537                 return NT_STATUS_OK;
538
539         case EPM_PROTOCOL_STREETTALK:
540                 epm_floor->rhs.streettalk.streettalk = talloc_strdup(mem_ctx, data);
541                 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.streettalk.streettalk);
542                 return NT_STATUS_OK;
543
544         case EPM_PROTOCOL_UNIX_DS:
545                 epm_floor->rhs.unix_ds.path = talloc_strdup(mem_ctx, data);
546                 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.unix_ds.path);
547                 return NT_STATUS_OK;
548
549         case EPM_PROTOCOL_NULL:
550                 return NT_STATUS_OK;
551
552         default:
553                 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
554                 break;
555         }
556
557         return NT_STATUS_NOT_SUPPORTED;
558 }
559
560 enum dcerpc_transport_t dcerpc_transport_by_endpoint_protocol(int prot)
561 {
562         int i;
563
564         /* Find a transport that has 'prot' as 4th protocol */
565         for (i=0;i<ARRAY_SIZE(transports);i++) {
566                 if (transports[i].num_protocols >= 2 && 
567                         transports[i].protseq[1] == prot) {
568                         return transports[i].transport;
569                 }
570         }
571
572         /* Unknown transport */
573         return (unsigned int)-1;
574 }
575
576 _PUBLIC_ enum dcerpc_transport_t dcerpc_transport_by_tower(const struct epm_tower *tower)
577 {
578         int i;
579
580         /* Find a transport that matches this tower */
581         for (i=0;i<ARRAY_SIZE(transports);i++) {
582                 int j;
583                 if (transports[i].num_protocols != tower->num_floors - 2) {
584                         continue; 
585                 }
586
587                 for (j = 0; j < transports[i].num_protocols; j++) {
588                         if (transports[i].protseq[j] != tower->floors[j+2].lhs.protocol) {
589                                 break;
590                         }
591                 }
592
593                 if (j == transports[i].num_protocols) {
594                         return transports[i].transport;
595                 }
596         }
597
598         /* Unknown transport */
599         return (unsigned int)-1;
600 }
601
602 _PUBLIC_ const char *derpc_transport_string_by_transport(enum dcerpc_transport_t t)
603 {
604         int i;
605
606         for (i=0; i<ARRAY_SIZE(transports); i++) {
607                 if (t == transports[i].transport) {
608                         return transports[i].name;
609                 }
610         }
611         return NULL;
612 }
613
614 _PUBLIC_ NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx, 
615                                    struct epm_tower *tower, 
616                                    struct dcerpc_binding **b_out)
617 {
618         NTSTATUS status;
619         struct dcerpc_binding *binding;
620
621         binding = talloc(mem_ctx, struct dcerpc_binding);
622         NT_STATUS_HAVE_NO_MEMORY(binding);
623
624         ZERO_STRUCT(binding->object);
625         binding->options = NULL;
626         binding->host = NULL;
627         binding->target_hostname = NULL;
628         binding->flags = 0;
629         binding->assoc_group_id = 0;
630
631         binding->transport = dcerpc_transport_by_tower(tower);
632
633         if (binding->transport == (unsigned int)-1) {
634                 return NT_STATUS_NOT_SUPPORTED;
635         }
636
637         if (tower->num_floors < 1) {
638                 return NT_STATUS_OK;
639         }
640
641         /* Set object uuid */
642         status = dcerpc_floor_get_lhs_data(&tower->floors[0], &binding->object);
643
644         if (!NT_STATUS_IS_OK(status)) {
645                 DEBUG(1, ("Error pulling object uuid and version: %s", nt_errstr(status)));     
646                 return status;
647         }
648
649         /* Ignore floor 1, it contains the NDR version info */
650
651         binding->options = NULL;
652
653         /* Set endpoint */
654         if (tower->num_floors >= 4) {
655                 binding->endpoint = dcerpc_floor_get_rhs_data(mem_ctx, &tower->floors[3]);
656         } else {
657                 binding->endpoint = NULL;
658         }
659
660         /* Set network address */
661         if (tower->num_floors >= 5) {
662                 binding->host = dcerpc_floor_get_rhs_data(mem_ctx, &tower->floors[4]);
663                 NT_STATUS_HAVE_NO_MEMORY(binding->host);
664                 binding->target_hostname = binding->host;
665         }
666         *b_out = binding;
667         return NT_STATUS_OK;
668 }
669
670 _PUBLIC_ NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx,
671                                              const struct dcerpc_binding *binding,
672                                              struct epm_tower *tower)
673 {
674         const enum epm_protocol *protseq = NULL;
675         int num_protocols = -1, i;
676         NTSTATUS status;
677
678         /* Find transport */
679         for (i=0;i<ARRAY_SIZE(transports);i++) {
680                 if (transports[i].transport == binding->transport) {
681                         protseq = transports[i].protseq;
682                         num_protocols = transports[i].num_protocols;
683                         break;
684                 }
685         }
686
687         if (num_protocols == -1) {
688                 DEBUG(0, ("Unable to find transport with id '%d'\n", binding->transport));
689                 return NT_STATUS_UNSUCCESSFUL;
690         }
691
692         tower->num_floors = 2 + num_protocols;
693         tower->floors = talloc_array(mem_ctx, struct epm_floor, tower->num_floors);
694
695         /* Floor 0 */
696         tower->floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
697
698         tower->floors[0].lhs.lhs_data = dcerpc_floor_pack_lhs_data(tower->floors, &binding->object);
699
700         tower->floors[0].rhs.uuid.unknown = data_blob_talloc_zero(tower->floors, 2);
701
702         /* Floor 1 */
703         tower->floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
704
705         tower->floors[1].lhs.lhs_data = dcerpc_floor_pack_lhs_data(tower->floors, 
706                                                                 &ndr_transfer_syntax);
707
708         tower->floors[1].rhs.uuid.unknown = data_blob_talloc_zero(tower->floors, 2);
709
710         /* Floor 2 to num_protocols */
711         for (i = 0; i < num_protocols; i++) {
712                 tower->floors[2 + i].lhs.protocol = protseq[i];
713                 tower->floors[2 + i].lhs.lhs_data = data_blob_talloc(tower->floors, NULL, 0);
714                 ZERO_STRUCT(tower->floors[2 + i].rhs);
715                 dcerpc_floor_set_rhs_data(tower->floors, &tower->floors[2 + i], "");
716         }
717
718         /* The 4th floor contains the endpoint */
719         if (num_protocols >= 2 && binding->endpoint) {
720                 status = dcerpc_floor_set_rhs_data(tower->floors, &tower->floors[3], binding->endpoint);
721                 if (NT_STATUS_IS_ERR(status)) {
722                         return status;
723                 }
724         }
725
726         /* The 5th contains the network address */
727         if (num_protocols >= 3 && binding->host) {
728                 if (is_ipaddress(binding->host)) {
729                         status = dcerpc_floor_set_rhs_data(tower->floors, &tower->floors[4], 
730                                                            binding->host);
731                 } else {
732                         /* note that we don't attempt to resolve the
733                            name here - when we get a hostname here we
734                            are in the client code, and want to put in
735                            a wildcard all-zeros IP for the server to
736                            fill in */
737                         status = dcerpc_floor_set_rhs_data(tower->floors, &tower->floors[4], 
738                                                            "0.0.0.0");
739                 }
740                 if (NT_STATUS_IS_ERR(status)) {
741                         return status;
742                 }
743         }
744
745         return NT_STATUS_OK;
746 }