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