Remove restrictions on number of DN components in LDAP server
[kai/samba.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_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 };
89
90 const char *epm_floor_string(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
91 {
92         struct ndr_syntax_id syntax;
93         NTSTATUS status;
94
95         switch(epm_floor->lhs.protocol) {
96                 case EPM_PROTOCOL_UUID:
97                         status = dcerpc_floor_get_lhs_data(epm_floor, &syntax);
98                         if (NT_STATUS_IS_OK(status)) {
99                                 /* lhs is used: UUID */
100                                 char *uuidstr;
101
102                                 if (GUID_equal(&syntax.uuid, &ndr_transfer_syntax.uuid)) {
103                                         return "NDR";
104                                 } 
105
106                                 if (GUID_equal(&syntax.uuid, &ndr64_transfer_syntax.uuid)) {
107                                         return "NDR64";
108                                 } 
109
110                                 uuidstr = GUID_string(mem_ctx, &syntax.uuid);
111
112                                 return talloc_asprintf(mem_ctx, " uuid %s/0x%02x", uuidstr, syntax.if_version);
113                         } else { /* IPX */
114                                 return talloc_asprintf(mem_ctx, "IPX:%s", 
115                                                 data_blob_hex_string(mem_ctx, &epm_floor->rhs.uuid.unknown));
116                         }
117
118                 case EPM_PROTOCOL_NCACN:
119                         return "RPC-C";
120
121                 case EPM_PROTOCOL_NCADG:
122                         return "RPC";
123
124                 case EPM_PROTOCOL_NCALRPC:
125                         return "NCALRPC";
126
127                 case EPM_PROTOCOL_DNET_NSP:
128                         return "DNET/NSP";
129
130                 case EPM_PROTOCOL_IP:
131                         return talloc_asprintf(mem_ctx, "IP:%s", epm_floor->rhs.ip.ipaddr);
132
133                 case EPM_PROTOCOL_PIPE:
134                         return talloc_asprintf(mem_ctx, "PIPE:%s", epm_floor->rhs.pipe.path);
135
136                 case EPM_PROTOCOL_SMB:
137                         return talloc_asprintf(mem_ctx, "SMB:%s", epm_floor->rhs.smb.unc);
138
139                 case EPM_PROTOCOL_UNIX_DS:
140                         return talloc_asprintf(mem_ctx, "Unix:%s", epm_floor->rhs.unix_ds.path);
141
142                 case EPM_PROTOCOL_NETBIOS:
143                         return talloc_asprintf(mem_ctx, "NetBIOS:%s", epm_floor->rhs.netbios.name);
144
145                 case EPM_PROTOCOL_NETBEUI:
146                         return "NETBeui";
147
148                 case EPM_PROTOCOL_SPX:
149                         return "SPX";
150
151                 case EPM_PROTOCOL_NB_IPX:
152                         return "NB_IPX";
153
154                 case EPM_PROTOCOL_HTTP:
155                         return talloc_asprintf(mem_ctx, "HTTP:%d", epm_floor->rhs.http.port);
156
157                 case EPM_PROTOCOL_TCP:
158                         return talloc_asprintf(mem_ctx, "TCP:%d", epm_floor->rhs.tcp.port);
159
160                 case EPM_PROTOCOL_UDP:
161                         return talloc_asprintf(mem_ctx, "UDP:%d", epm_floor->rhs.udp.port);
162
163                 default:
164                         return talloc_asprintf(mem_ctx, "UNK(%02x):", epm_floor->lhs.protocol);
165         }
166 }
167
168
169 /*
170   form a binding string from a binding structure
171 */
172 _PUBLIC_ char *dcerpc_binding_string(TALLOC_CTX *mem_ctx, const struct dcerpc_binding *b)
173 {
174         char *s = talloc_strdup(mem_ctx, "");
175         int i;
176         const char *t_name = NULL;
177
178         if (b->transport != NCA_UNKNOWN) {
179                 for (i=0;i<ARRAY_SIZE(transports);i++) {
180                         if (transports[i].transport == b->transport) {
181                                 t_name = transports[i].name;
182                         }
183                 }
184                 if (!t_name) {
185                         return NULL;
186                 }
187         }
188
189         if (!GUID_all_zero(&b->object.uuid)) { 
190                 s = talloc_asprintf(s, "%s@",
191                                     GUID_string(mem_ctx, &b->object.uuid));
192         }
193
194         if (t_name != NULL) {
195                 s = talloc_asprintf_append_buffer(s, "%s:", t_name);
196                 if (s == NULL) {
197                         return NULL;
198                 }
199         }
200
201         if (b->host) {
202                 s = talloc_asprintf_append_buffer(s, "%s", b->host);
203         }
204
205         if (!b->endpoint && !b->options && !b->flags) {
206                 return s;
207         }
208
209         s = talloc_asprintf_append_buffer(s, "[");
210
211         if (b->endpoint) {
212                 s = talloc_asprintf_append_buffer(s, "%s", b->endpoint);
213         }
214
215         /* this is a *really* inefficent way of dealing with strings,
216            but this is rarely called and the strings are always short,
217            so I don't care */
218         for (i=0;b->options && b->options[i];i++) {
219                 s = talloc_asprintf_append_buffer(s, ",%s", b->options[i]);
220                 if (!s) return NULL;
221         }
222
223         for (i=0;i<ARRAY_SIZE(ncacn_options);i++) {
224                 if (b->flags & ncacn_options[i].flag) {
225                         s = talloc_asprintf_append_buffer(s, ",%s", ncacn_options[i].name);
226                         if (!s) return NULL;
227                 }
228         }
229
230         s = talloc_asprintf_append_buffer(s, "]");
231
232         return s;
233 }
234
235 /*
236   parse a binding string into a dcerpc_binding structure
237 */
238 _PUBLIC_ NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *s, struct dcerpc_binding **b_out)
239 {
240         struct dcerpc_binding *b;
241         char *options;
242         char *p;
243         int i, j, comma_count;
244
245         b = talloc(mem_ctx, struct dcerpc_binding);
246         if (!b) {
247                 return NT_STATUS_NO_MEMORY;
248         }
249
250         p = strchr(s, '@');
251
252         if (p && PTR_DIFF(p, s) == 36) { /* 36 is the length of a UUID */
253                 NTSTATUS status;
254
255                 status = GUID_from_string(s, &b->object.uuid);
256
257                 if (NT_STATUS_IS_ERR(status)) {
258                         DEBUG(0, ("Failed parsing UUID\n"));
259                         return status;
260                 }
261
262                 s = p + 1;
263         } else {
264                 ZERO_STRUCT(b->object);
265         }
266
267         b->object.if_version = 0;
268
269         p = strchr(s, ':');
270
271         if (p == NULL) {
272                 b->transport = NCA_UNKNOWN;
273         } else {
274                 char *type = talloc_strndup(mem_ctx, s, PTR_DIFF(p, s));
275                 if (!type) {
276                         return NT_STATUS_NO_MEMORY;
277                 }
278
279                 for (i=0;i<ARRAY_SIZE(transports);i++) {
280                         if (strcasecmp(type, transports[i].name) == 0) {
281                                 b->transport = transports[i].transport;
282                                 break;
283                         }
284                 }
285
286                 if (i==ARRAY_SIZE(transports)) {
287                         DEBUG(0,("Unknown dcerpc transport '%s'\n", type));
288                         return NT_STATUS_INVALID_PARAMETER;
289                 }
290
291                 talloc_free(type);
292         
293                 s = p+1;
294         }
295
296         p = strchr(s, '[');
297         if (p) {
298                 b->host = talloc_strndup(b, s, PTR_DIFF(p, s));
299                 options = talloc_strdup(mem_ctx, p+1);
300                 if (options[strlen(options)-1] != ']') {
301                         return NT_STATUS_INVALID_PARAMETER;
302                 }
303                 options[strlen(options)-1] = 0;
304         } else {
305                 b->host = talloc_strdup(b, s);
306                 options = NULL;
307         }
308         if (!b->host) {
309                 return NT_STATUS_NO_MEMORY;
310         }
311
312         b->target_hostname = b->host;
313
314         b->options = NULL;
315         b->flags = 0;
316         b->assoc_group_id = 0;
317         b->endpoint = NULL;
318
319         if (!options) {
320                 *b_out = b;
321                 return NT_STATUS_OK;
322         }
323
324         comma_count = count_chars(options, ',');
325
326         b->options = talloc_array(b, 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(b, 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         *b_out = b;
370         return NT_STATUS_OK;
371 }
372
373 _PUBLIC_ NTSTATUS dcerpc_floor_get_lhs_data(struct epm_floor *epm_floor, struct ndr_syntax_id *syntax)
374 {
375         TALLOC_CTX *mem_ctx = talloc_init("floor_get_lhs_data");
376         struct ndr_pull *ndr = ndr_pull_init_blob(&epm_floor->lhs.lhs_data, mem_ctx, NULL);
377         enum ndr_err_code ndr_err;
378         uint16_t if_version=0;
379
380         ndr->flags |= LIBNDR_FLAG_NOALIGN;
381
382         ndr_err = ndr_pull_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
383         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
384                 talloc_free(mem_ctx);
385                 return ndr_map_error2ntstatus(ndr_err);
386         }
387
388         ndr_err = ndr_pull_uint16(ndr, NDR_SCALARS, &if_version);
389         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
390                 talloc_free(mem_ctx);
391                 return ndr_map_error2ntstatus(ndr_err);
392         }
393
394         syntax->if_version = if_version;
395
396         talloc_free(mem_ctx);
397
398         return NT_STATUS_OK;
399 }
400
401 static DATA_BLOB dcerpc_floor_pack_lhs_data(TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax)
402 {
403         struct ndr_push *ndr = ndr_push_init_ctx(mem_ctx, NULL);
404
405         ndr->flags |= LIBNDR_FLAG_NOALIGN;
406
407         ndr_push_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
408         ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version);
409
410         return ndr_push_blob(ndr);
411 }
412
413 const char *dcerpc_floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
414 {
415         switch (epm_floor->lhs.protocol) {
416         case EPM_PROTOCOL_TCP:
417                 if (epm_floor->rhs.tcp.port == 0) return NULL;
418                 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.tcp.port);
419                 
420         case EPM_PROTOCOL_UDP:
421                 if (epm_floor->rhs.udp.port == 0) return NULL;
422                 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.udp.port);
423
424         case EPM_PROTOCOL_HTTP:
425                 if (epm_floor->rhs.http.port == 0) return NULL;
426                 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.http.port);
427
428         case EPM_PROTOCOL_IP:
429                 return talloc_strdup(mem_ctx, epm_floor->rhs.ip.ipaddr);
430
431         case EPM_PROTOCOL_NCACN:
432                 return NULL;
433
434         case EPM_PROTOCOL_NCADG:
435                 return NULL;
436
437         case EPM_PROTOCOL_SMB:
438                 if (strlen(epm_floor->rhs.smb.unc) == 0) return NULL;
439                 return talloc_strdup(mem_ctx, epm_floor->rhs.smb.unc);
440
441         case EPM_PROTOCOL_PIPE:
442                 if (strlen(epm_floor->rhs.pipe.path) == 0) return NULL;
443                 return talloc_strdup(mem_ctx, epm_floor->rhs.pipe.path);
444
445         case EPM_PROTOCOL_NETBIOS:
446                 if (strlen(epm_floor->rhs.netbios.name) == 0) return NULL;
447                 return talloc_strdup(mem_ctx, epm_floor->rhs.netbios.name);
448
449         case EPM_PROTOCOL_NCALRPC:
450                 return NULL;
451                 
452         case EPM_PROTOCOL_VINES_SPP:
453                 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_spp.port);
454                 
455         case EPM_PROTOCOL_VINES_IPC:
456                 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_ipc.port);
457                 
458         case EPM_PROTOCOL_STREETTALK:
459                 return talloc_strdup(mem_ctx, epm_floor->rhs.streettalk.streettalk);
460                 
461         case EPM_PROTOCOL_UNIX_DS:
462                 if (strlen(epm_floor->rhs.unix_ds.path) == 0) return NULL;
463                 return talloc_strdup(mem_ctx, epm_floor->rhs.unix_ds.path);
464                 
465         case EPM_PROTOCOL_NULL:
466                 return NULL;
467
468         default:
469                 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
470                 break;
471         }
472
473         return NULL;
474 }
475
476 static NTSTATUS dcerpc_floor_set_rhs_data(TALLOC_CTX *mem_ctx, 
477                                           struct epm_floor *epm_floor,  
478                                           const char *data)
479 {
480         switch (epm_floor->lhs.protocol) {
481         case EPM_PROTOCOL_TCP:
482                 epm_floor->rhs.tcp.port = atoi(data);
483                 return NT_STATUS_OK;
484                 
485         case EPM_PROTOCOL_UDP:
486                 epm_floor->rhs.udp.port = atoi(data);
487                 return NT_STATUS_OK;
488
489         case EPM_PROTOCOL_HTTP:
490                 epm_floor->rhs.http.port = atoi(data);
491                 return NT_STATUS_OK;
492
493         case EPM_PROTOCOL_IP:
494                 epm_floor->rhs.ip.ipaddr = talloc_strdup(mem_ctx, data);
495                 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.ip.ipaddr);
496                 return NT_STATUS_OK;
497
498         case EPM_PROTOCOL_NCACN:
499                 epm_floor->rhs.ncacn.minor_version = 0;
500                 return NT_STATUS_OK;
501
502         case EPM_PROTOCOL_NCADG:
503                 epm_floor->rhs.ncadg.minor_version = 0;
504                 return NT_STATUS_OK;
505
506         case EPM_PROTOCOL_SMB:
507                 epm_floor->rhs.smb.unc = talloc_strdup(mem_ctx, data);
508                 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.smb.unc);
509                 return NT_STATUS_OK;
510
511         case EPM_PROTOCOL_PIPE:
512                 epm_floor->rhs.pipe.path = talloc_strdup(mem_ctx, data);
513                 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.pipe.path);
514                 return NT_STATUS_OK;
515
516         case EPM_PROTOCOL_NETBIOS:
517                 epm_floor->rhs.netbios.name = talloc_strdup(mem_ctx, data);
518                 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.netbios.name);
519                 return NT_STATUS_OK;
520
521         case EPM_PROTOCOL_NCALRPC:
522                 return NT_STATUS_OK;
523                 
524         case EPM_PROTOCOL_VINES_SPP:
525                 epm_floor->rhs.vines_spp.port = atoi(data);
526                 return NT_STATUS_OK;
527                 
528         case EPM_PROTOCOL_VINES_IPC:
529                 epm_floor->rhs.vines_ipc.port = atoi(data);
530                 return NT_STATUS_OK;
531                 
532         case EPM_PROTOCOL_STREETTALK:
533                 epm_floor->rhs.streettalk.streettalk = talloc_strdup(mem_ctx, data);
534                 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.streettalk.streettalk);
535                 return NT_STATUS_OK;
536                 
537         case EPM_PROTOCOL_UNIX_DS:
538                 epm_floor->rhs.unix_ds.path = talloc_strdup(mem_ctx, data);
539                 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.unix_ds.path);
540                 return NT_STATUS_OK;
541                 
542         case EPM_PROTOCOL_NULL:
543                 return NT_STATUS_OK;
544
545         default:
546                 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
547                 break;
548         }
549
550         return NT_STATUS_NOT_SUPPORTED;
551 }
552
553 enum dcerpc_transport_t dcerpc_transport_by_endpoint_protocol(int prot)
554 {
555         int i;
556
557         /* Find a transport that has 'prot' as 4th protocol */
558         for (i=0;i<ARRAY_SIZE(transports);i++) {
559                 if (transports[i].num_protocols >= 2 && 
560                         transports[i].protseq[1] == prot) {
561                         return transports[i].transport;
562                 }
563         }
564         
565         /* Unknown transport */
566         return (unsigned int)-1;
567 }
568
569 _PUBLIC_ enum dcerpc_transport_t dcerpc_transport_by_tower(struct epm_tower *tower)
570 {
571         int i;
572
573         /* Find a transport that matches this tower */
574         for (i=0;i<ARRAY_SIZE(transports);i++) {
575                 int j;
576                 if (transports[i].num_protocols != tower->num_floors - 2) {
577                         continue; 
578                 }
579
580                 for (j = 0; j < transports[i].num_protocols; j++) {
581                         if (transports[i].protseq[j] != tower->floors[j+2].lhs.protocol) {
582                                 break;
583                         }
584                 }
585
586                 if (j == transports[i].num_protocols) {
587                         return transports[i].transport;
588                 }
589         }
590         
591         /* Unknown transport */
592         return (unsigned int)-1;
593 }
594
595 _PUBLIC_ NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx, 
596                                    struct epm_tower *tower, 
597                                    struct dcerpc_binding **b_out)
598 {
599         NTSTATUS status;
600         struct dcerpc_binding *binding;
601
602         binding = talloc(mem_ctx, struct dcerpc_binding);
603         NT_STATUS_HAVE_NO_MEMORY(binding);
604
605         ZERO_STRUCT(binding->object);
606         binding->options = NULL;
607         binding->host = NULL;
608         binding->target_hostname = NULL;
609         binding->flags = 0;
610         binding->assoc_group_id = 0;
611
612         binding->transport = dcerpc_transport_by_tower(tower);
613
614         if (binding->transport == (unsigned int)-1) {
615                 return NT_STATUS_NOT_SUPPORTED;
616         }
617
618         if (tower->num_floors < 1) {
619                 return NT_STATUS_OK;
620         }
621
622         /* Set object uuid */
623         status = dcerpc_floor_get_lhs_data(&tower->floors[0], &binding->object);
624         
625         if (!NT_STATUS_IS_OK(status)) {
626                 DEBUG(1, ("Error pulling object uuid and version: %s", nt_errstr(status)));     
627                 return status;
628         }
629
630         /* Ignore floor 1, it contains the NDR version info */
631         
632         binding->options = NULL;
633
634         /* Set endpoint */
635         if (tower->num_floors >= 4) {
636                 binding->endpoint = dcerpc_floor_get_rhs_data(mem_ctx, &tower->floors[3]);
637         } else {
638                 binding->endpoint = NULL;
639         }
640
641         /* Set network address */
642         if (tower->num_floors >= 5) {
643                 binding->host = dcerpc_floor_get_rhs_data(mem_ctx, &tower->floors[4]);
644                 NT_STATUS_HAVE_NO_MEMORY(binding->host);
645                 binding->target_hostname = binding->host;
646         }
647         *b_out = binding;
648         return NT_STATUS_OK;
649 }
650
651 _PUBLIC_ NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx, struct dcerpc_binding *binding, struct epm_tower *tower)
652 {
653         const enum epm_protocol *protseq = NULL;
654         int num_protocols = -1, i;
655         NTSTATUS status;
656         
657         /* Find transport */
658         for (i=0;i<ARRAY_SIZE(transports);i++) {
659                 if (transports[i].transport == binding->transport) {
660                         protseq = transports[i].protseq;
661                         num_protocols = transports[i].num_protocols;
662                         break;
663                 }
664         }
665
666         if (num_protocols == -1) {
667                 DEBUG(0, ("Unable to find transport with id '%d'\n", binding->transport));
668                 return NT_STATUS_UNSUCCESSFUL;
669         }
670
671         tower->num_floors = 2 + num_protocols;
672         tower->floors = talloc_array(mem_ctx, struct epm_floor, tower->num_floors);
673
674         /* Floor 0 */
675         tower->floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
676
677         tower->floors[0].lhs.lhs_data = dcerpc_floor_pack_lhs_data(mem_ctx, &binding->object);
678
679         tower->floors[0].rhs.uuid.unknown = data_blob_talloc_zero(mem_ctx, 2);
680         
681         /* Floor 1 */
682         tower->floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
683
684         tower->floors[1].lhs.lhs_data = dcerpc_floor_pack_lhs_data(mem_ctx, 
685                                                                 &ndr_transfer_syntax);
686         
687         tower->floors[1].rhs.uuid.unknown = data_blob_talloc_zero(mem_ctx, 2);
688         
689         /* Floor 2 to num_protocols */
690         for (i = 0; i < num_protocols; i++) {
691                 tower->floors[2 + i].lhs.protocol = protseq[i];
692                 tower->floors[2 + i].lhs.lhs_data = data_blob_talloc(mem_ctx, NULL, 0);
693                 ZERO_STRUCT(tower->floors[2 + i].rhs);
694                 dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[2 + i], "");
695         }
696
697         /* The 4th floor contains the endpoint */
698         if (num_protocols >= 2 && binding->endpoint) {
699                 status = dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[3], binding->endpoint);
700                 if (NT_STATUS_IS_ERR(status)) {
701                         return status;
702                 }
703         }
704         
705         /* The 5th contains the network address */
706         if (num_protocols >= 3 && binding->host) {
707                 if (is_ipaddress(binding->host)) {
708                         status = dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[4], 
709                                                            binding->host);
710                 } else {
711                         /* note that we don't attempt to resolve the
712                            name here - when we get a hostname here we
713                            are in the client code, and want to put in
714                            a wildcard all-zeros IP for the server to
715                            fill in */
716                         status = dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[4], 
717                                                            "0.0.0.0");
718                 }
719                 if (NT_STATUS_IS_ERR(status)) {
720                         return status;
721                 }
722         }
723
724         return NT_STATUS_OK;
725 }