Add unique IP address binding for client connections (EPM and ncacn_ip_tcp levels)
[Samba.git] / librpc / rpc / binding.c
blobfeff5128ac77f3e1181052cbf73617c95931e419
1 /*
2 Unix SMB/CIFS implementation.
4 dcerpc utility functions
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
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.
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.
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/>.
25 #include "includes.h"
26 #include "../../lib/util/util_net.h"
27 #include "librpc/gen_ndr/ndr_epmapper.h"
28 #include "librpc/gen_ndr/ndr_misc.h"
29 #include "librpc/rpc/dcerpc.h"
30 #undef strcasecmp
32 #define MAX_PROTSEQ 10
34 static const struct {
35 const char *name;
36 enum dcerpc_transport_t transport;
37 int num_protocols;
38 enum epm_protocol protseq[MAX_PROTSEQ];
39 } transports[] = {
40 { "ncacn_np", NCACN_NP, 3,
41 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SMB, EPM_PROTOCOL_NETBIOS }},
42 { "ncacn_ip_tcp", NCACN_IP_TCP, 3,
43 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_TCP, EPM_PROTOCOL_IP } },
44 { "ncacn_http", NCACN_HTTP, 3,
45 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_HTTP, EPM_PROTOCOL_IP } },
46 { "ncadg_ip_udp", NCACN_IP_UDP, 3,
47 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UDP, EPM_PROTOCOL_IP } },
48 { "ncalrpc", NCALRPC, 2,
49 { EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_NAMED_PIPE } },
50 { "ncacn_unix_stream", NCACN_UNIX_STREAM, 2,
51 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_UNIX_DS } },
52 { "ncadg_unix_dgram", NCADG_UNIX_DGRAM, 2,
53 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UNIX_DS } },
54 { "ncacn_at_dsp", NCACN_AT_DSP, 3,
55 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DSP } },
56 { "ncadg_at_ddp", NCADG_AT_DDP, 3,
57 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DDP } },
58 { "ncacn_vns_ssp", NCACN_VNS_SPP, 3,
59 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_SPP } },
60 { "ncacn_vns_ipc", NCACN_VNS_IPC, 3,
61 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_IPC }, },
62 { "ncadg_ipx", NCADG_IPX, 2,
63 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_IPX },
65 { "ncacn_spx", NCACN_SPX, 3,
66 /* I guess some MS programmer confused the identifier for
67 * EPM_PROTOCOL_UUID (0x0D or 13) with the one for
68 * EPM_PROTOCOL_SPX (0x13) here. -- jelmer*/
69 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_UUID },
73 static const struct {
74 const char *name;
75 uint32_t flag;
76 } ncacn_options[] = {
77 {"sign", DCERPC_SIGN},
78 {"seal", DCERPC_SEAL},
79 {"connect", DCERPC_CONNECT},
80 {"spnego", DCERPC_AUTH_SPNEGO},
81 {"ntlm", DCERPC_AUTH_NTLM},
82 {"krb5", DCERPC_AUTH_KRB5},
83 {"validate", DCERPC_DEBUG_VALIDATE_BOTH},
84 {"print", DCERPC_DEBUG_PRINT_BOTH},
85 {"padcheck", DCERPC_DEBUG_PAD_CHECK},
86 {"bigendian", DCERPC_PUSH_BIGENDIAN},
87 {"smb2", DCERPC_SMB2},
88 {"hdrsign", DCERPC_HEADER_SIGNING},
89 {"ndr64", DCERPC_NDR64},
90 {"localaddress", DCERPC_LOCALADDRESS}
93 const char *epm_floor_string(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
95 struct ndr_syntax_id syntax;
96 NTSTATUS status;
98 switch(epm_floor->lhs.protocol) {
99 case EPM_PROTOCOL_UUID:
100 status = dcerpc_floor_get_lhs_data(epm_floor, &syntax);
101 if (NT_STATUS_IS_OK(status)) {
102 /* lhs is used: UUID */
103 char *uuidstr;
105 if (GUID_equal(&syntax.uuid, &ndr_transfer_syntax.uuid)) {
106 return "NDR";
109 if (GUID_equal(&syntax.uuid, &ndr64_transfer_syntax.uuid)) {
110 return "NDR64";
113 uuidstr = GUID_string(mem_ctx, &syntax.uuid);
115 return talloc_asprintf(mem_ctx, " uuid %s/0x%02x", uuidstr, syntax.if_version);
116 } else { /* IPX */
117 return talloc_asprintf(mem_ctx, "IPX:%s",
118 data_blob_hex_string_upper(mem_ctx, &epm_floor->rhs.uuid.unknown));
121 case EPM_PROTOCOL_NCACN:
122 return "RPC-C";
124 case EPM_PROTOCOL_NCADG:
125 return "RPC";
127 case EPM_PROTOCOL_NCALRPC:
128 return "NCALRPC";
130 case EPM_PROTOCOL_DNET_NSP:
131 return "DNET/NSP";
133 case EPM_PROTOCOL_IP:
134 return talloc_asprintf(mem_ctx, "IP:%s", epm_floor->rhs.ip.ipaddr);
136 case EPM_PROTOCOL_NAMED_PIPE:
137 return talloc_asprintf(mem_ctx, "NAMED-PIPE:%s", epm_floor->rhs.named_pipe.path);
139 case EPM_PROTOCOL_SMB:
140 return talloc_asprintf(mem_ctx, "SMB:%s", epm_floor->rhs.smb.unc);
142 case EPM_PROTOCOL_UNIX_DS:
143 return talloc_asprintf(mem_ctx, "Unix:%s", epm_floor->rhs.unix_ds.path);
145 case EPM_PROTOCOL_NETBIOS:
146 return talloc_asprintf(mem_ctx, "NetBIOS:%s", epm_floor->rhs.netbios.name);
148 case EPM_PROTOCOL_NETBEUI:
149 return "NETBeui";
151 case EPM_PROTOCOL_SPX:
152 return "SPX";
154 case EPM_PROTOCOL_NB_IPX:
155 return "NB_IPX";
157 case EPM_PROTOCOL_HTTP:
158 return talloc_asprintf(mem_ctx, "HTTP:%d", epm_floor->rhs.http.port);
160 case EPM_PROTOCOL_TCP:
161 return talloc_asprintf(mem_ctx, "TCP:%d", epm_floor->rhs.tcp.port);
163 case EPM_PROTOCOL_UDP:
164 return talloc_asprintf(mem_ctx, "UDP:%d", epm_floor->rhs.udp.port);
166 default:
167 return talloc_asprintf(mem_ctx, "UNK(%02x):", epm_floor->lhs.protocol);
173 form a binding string from a binding structure
175 _PUBLIC_ char *dcerpc_binding_string(TALLOC_CTX *mem_ctx, const struct dcerpc_binding *b)
177 char *s = talloc_strdup(mem_ctx, "");
178 int i;
179 const char *t_name = NULL;
181 if (b->transport != NCA_UNKNOWN) {
182 t_name = derpc_transport_string_by_transport(b->transport);
183 if (!t_name) {
184 return NULL;
188 if (!GUID_all_zero(&b->object.uuid)) {
189 s = talloc_asprintf(s, "%s@",
190 GUID_string(mem_ctx, &b->object.uuid));
193 if (t_name != NULL) {
194 s = talloc_asprintf_append_buffer(s, "%s:", t_name);
195 if (s == NULL) {
196 return NULL;
200 if (b->host) {
201 s = talloc_asprintf_append_buffer(s, "%s", b->host);
204 if (!b->endpoint && !b->options && !b->flags) {
205 return s;
208 s = talloc_asprintf_append_buffer(s, "[");
210 if (b->endpoint) {
211 s = talloc_asprintf_append_buffer(s, "%s", b->endpoint);
214 /* this is a *really* inefficent way of dealing with strings,
215 but this is rarely called and the strings are always short,
216 so I don't care */
217 for (i=0;b->options && b->options[i];i++) {
218 s = talloc_asprintf_append_buffer(s, ",%s", b->options[i]);
219 if (!s) return NULL;
222 for (i=0;i<ARRAY_SIZE(ncacn_options);i++) {
223 if (b->flags & ncacn_options[i].flag) {
224 if (ncacn_options[i].flag == DCERPC_LOCALADDRESS && b->localaddress) {
225 s = talloc_asprintf_append_buffer(s, ",%s=%s", ncacn_options[i].name,
226 b->localaddress);
227 } else {
228 s = talloc_asprintf_append_buffer(s, ",%s", ncacn_options[i].name);
230 if (!s) return NULL;
234 s = talloc_asprintf_append_buffer(s, "]");
236 return s;
240 parse a binding string into a dcerpc_binding structure
242 _PUBLIC_ NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *s, struct dcerpc_binding **b_out)
244 struct dcerpc_binding *b;
245 char *options;
246 char *p;
247 int i, j, comma_count;
249 b = talloc(mem_ctx, struct dcerpc_binding);
250 if (!b) {
251 return NT_STATUS_NO_MEMORY;
254 p = strchr(s, '@');
256 if (p && PTR_DIFF(p, s) == 36) { /* 36 is the length of a UUID */
257 NTSTATUS status;
258 DATA_BLOB blob = data_blob(s, 36);
259 status = GUID_from_data_blob(&blob, &b->object.uuid);
261 if (NT_STATUS_IS_ERR(status)) {
262 DEBUG(0, ("Failed parsing UUID\n"));
263 return status;
266 s = p + 1;
267 } else {
268 ZERO_STRUCT(b->object);
271 b->object.if_version = 0;
273 p = strchr(s, ':');
275 if (p == NULL) {
276 b->transport = NCA_UNKNOWN;
277 } else {
278 char *type = talloc_strndup(mem_ctx, s, PTR_DIFF(p, s));
279 if (!type) {
280 return NT_STATUS_NO_MEMORY;
283 for (i=0;i<ARRAY_SIZE(transports);i++) {
284 if (strcasecmp(type, transports[i].name) == 0) {
285 b->transport = transports[i].transport;
286 break;
290 if (i==ARRAY_SIZE(transports)) {
291 DEBUG(0,("Unknown dcerpc transport '%s'\n", type));
292 return NT_STATUS_INVALID_PARAMETER;
295 talloc_free(type);
297 s = p+1;
300 p = strchr(s, '[');
301 if (p) {
302 b->host = talloc_strndup(b, 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;
307 options[strlen(options)-1] = 0;
308 } else {
309 b->host = talloc_strdup(b, s);
310 options = NULL;
312 if (!b->host) {
313 return NT_STATUS_NO_MEMORY;
316 b->target_hostname = b->host;
318 b->options = NULL;
319 b->flags = 0;
320 b->assoc_group_id = 0;
321 b->endpoint = NULL;
322 b->localaddress = NULL;
324 if (!options) {
325 *b_out = b;
326 return NT_STATUS_OK;
329 comma_count = count_chars(options, ',');
331 b->options = talloc_array(b, const char *, comma_count+2);
332 if (!b->options) {
333 return NT_STATUS_NO_MEMORY;
336 for (i=0; (p = strchr(options, ',')); i++) {
337 b->options[i] = talloc_strndup(b, options, PTR_DIFF(p, options));
338 if (!b->options[i]) {
339 return NT_STATUS_NO_MEMORY;
341 options = p+1;
343 b->options[i] = options;
344 b->options[i+1] = NULL;
346 /* some options are pre-parsed for convenience */
347 for (i=0;b->options[i];i++) {
348 for (j=0;j<ARRAY_SIZE(ncacn_options);j++) {
349 size_t opt_len = strlen(ncacn_options[j].name);
350 if (strncasecmp(ncacn_options[j].name, b->options[i], opt_len) == 0) {
351 int k;
352 char c = b->options[i][opt_len];
354 if (ncacn_options[j].flag == DCERPC_LOCALADDRESS && c == '=') {
355 b->localaddress = talloc_strdup(b, &b->options[i][opt_len+1]);
356 } else if (c != 0) {
357 continue;
360 b->flags |= ncacn_options[j].flag;
361 for (k=i;b->options[k];k++) {
362 b->options[k] = b->options[k+1];
364 i--;
365 break;
370 if (b->options[0]) {
371 /* Endpoint is first option */
372 b->endpoint = b->options[0];
373 if (strlen(b->endpoint) == 0) b->endpoint = NULL;
375 for (i=0;b->options[i];i++) {
376 b->options[i] = b->options[i+1];
380 if (b->options[0] == NULL)
381 b->options = NULL;
383 *b_out = b;
384 return NT_STATUS_OK;
387 _PUBLIC_ NTSTATUS dcerpc_floor_get_lhs_data(const struct epm_floor *epm_floor,
388 struct ndr_syntax_id *syntax)
390 TALLOC_CTX *mem_ctx = talloc_init("floor_get_lhs_data");
391 struct ndr_pull *ndr;
392 enum ndr_err_code ndr_err;
393 uint16_t if_version=0;
395 ndr = ndr_pull_init_blob(&epm_floor->lhs.lhs_data, mem_ctx);
396 if (ndr == NULL) {
397 talloc_free(mem_ctx);
398 return NT_STATUS_NO_MEMORY;
400 ndr->flags |= LIBNDR_FLAG_NOALIGN;
402 ndr_err = ndr_pull_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
403 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
404 talloc_free(mem_ctx);
405 return ndr_map_error2ntstatus(ndr_err);
408 ndr_err = ndr_pull_uint16(ndr, NDR_SCALARS, &if_version);
409 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
410 talloc_free(mem_ctx);
411 return ndr_map_error2ntstatus(ndr_err);
414 syntax->if_version = if_version;
416 talloc_free(mem_ctx);
418 return NT_STATUS_OK;
421 static DATA_BLOB dcerpc_floor_pack_lhs_data(TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax)
423 DATA_BLOB blob;
424 struct ndr_push *ndr = ndr_push_init_ctx(mem_ctx);
426 ndr->flags |= LIBNDR_FLAG_NOALIGN;
428 ndr_push_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
429 ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version);
431 blob = ndr_push_blob(ndr);
432 talloc_steal(mem_ctx, blob.data);
433 talloc_free(ndr);
434 return blob;
437 static DATA_BLOB dcerpc_floor_pack_rhs_if_version_data(TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax)
439 DATA_BLOB blob;
440 struct ndr_push *ndr = ndr_push_init_ctx(mem_ctx);
442 ndr->flags |= LIBNDR_FLAG_NOALIGN;
444 ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version >> 16);
446 blob = ndr_push_blob(ndr);
447 talloc_steal(mem_ctx, blob.data);
448 talloc_free(ndr);
449 return blob;
452 const char *dcerpc_floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
454 switch (epm_floor->lhs.protocol) {
455 case EPM_PROTOCOL_TCP:
456 if (epm_floor->rhs.tcp.port == 0) return NULL;
457 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.tcp.port);
459 case EPM_PROTOCOL_UDP:
460 if (epm_floor->rhs.udp.port == 0) return NULL;
461 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.udp.port);
463 case EPM_PROTOCOL_HTTP:
464 if (epm_floor->rhs.http.port == 0) return NULL;
465 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.http.port);
467 case EPM_PROTOCOL_IP:
468 return talloc_strdup(mem_ctx, epm_floor->rhs.ip.ipaddr);
470 case EPM_PROTOCOL_NCACN:
471 return NULL;
473 case EPM_PROTOCOL_NCADG:
474 return NULL;
476 case EPM_PROTOCOL_SMB:
477 if (strlen(epm_floor->rhs.smb.unc) == 0) return NULL;
478 return talloc_strdup(mem_ctx, epm_floor->rhs.smb.unc);
480 case EPM_PROTOCOL_NAMED_PIPE:
481 if (strlen(epm_floor->rhs.named_pipe.path) == 0) return NULL;
482 return talloc_strdup(mem_ctx, epm_floor->rhs.named_pipe.path);
484 case EPM_PROTOCOL_NETBIOS:
485 if (strlen(epm_floor->rhs.netbios.name) == 0) return NULL;
486 return talloc_strdup(mem_ctx, epm_floor->rhs.netbios.name);
488 case EPM_PROTOCOL_NCALRPC:
489 return NULL;
491 case EPM_PROTOCOL_VINES_SPP:
492 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_spp.port);
494 case EPM_PROTOCOL_VINES_IPC:
495 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_ipc.port);
497 case EPM_PROTOCOL_STREETTALK:
498 return talloc_strdup(mem_ctx, epm_floor->rhs.streettalk.streettalk);
500 case EPM_PROTOCOL_UNIX_DS:
501 if (strlen(epm_floor->rhs.unix_ds.path) == 0) return NULL;
502 return talloc_strdup(mem_ctx, epm_floor->rhs.unix_ds.path);
504 case EPM_PROTOCOL_NULL:
505 return NULL;
507 default:
508 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
509 break;
512 return NULL;
515 static NTSTATUS dcerpc_floor_set_rhs_data(TALLOC_CTX *mem_ctx,
516 struct epm_floor *epm_floor,
517 const char *data)
519 switch (epm_floor->lhs.protocol) {
520 case EPM_PROTOCOL_TCP:
521 epm_floor->rhs.tcp.port = atoi(data);
522 return NT_STATUS_OK;
524 case EPM_PROTOCOL_UDP:
525 epm_floor->rhs.udp.port = atoi(data);
526 return NT_STATUS_OK;
528 case EPM_PROTOCOL_HTTP:
529 epm_floor->rhs.http.port = atoi(data);
530 return NT_STATUS_OK;
532 case EPM_PROTOCOL_IP:
533 epm_floor->rhs.ip.ipaddr = talloc_strdup(mem_ctx, data);
534 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.ip.ipaddr);
535 return NT_STATUS_OK;
537 case EPM_PROTOCOL_NCACN:
538 epm_floor->rhs.ncacn.minor_version = 0;
539 return NT_STATUS_OK;
541 case EPM_PROTOCOL_NCADG:
542 epm_floor->rhs.ncadg.minor_version = 0;
543 return NT_STATUS_OK;
545 case EPM_PROTOCOL_SMB:
546 epm_floor->rhs.smb.unc = talloc_strdup(mem_ctx, data);
547 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.smb.unc);
548 return NT_STATUS_OK;
550 case EPM_PROTOCOL_NAMED_PIPE:
551 epm_floor->rhs.named_pipe.path = talloc_strdup(mem_ctx, data);
552 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.named_pipe.path);
553 return NT_STATUS_OK;
555 case EPM_PROTOCOL_NETBIOS:
556 epm_floor->rhs.netbios.name = talloc_strdup(mem_ctx, data);
557 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.netbios.name);
558 return NT_STATUS_OK;
560 case EPM_PROTOCOL_NCALRPC:
561 return NT_STATUS_OK;
563 case EPM_PROTOCOL_VINES_SPP:
564 epm_floor->rhs.vines_spp.port = atoi(data);
565 return NT_STATUS_OK;
567 case EPM_PROTOCOL_VINES_IPC:
568 epm_floor->rhs.vines_ipc.port = atoi(data);
569 return NT_STATUS_OK;
571 case EPM_PROTOCOL_STREETTALK:
572 epm_floor->rhs.streettalk.streettalk = talloc_strdup(mem_ctx, data);
573 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.streettalk.streettalk);
574 return NT_STATUS_OK;
576 case EPM_PROTOCOL_UNIX_DS:
577 epm_floor->rhs.unix_ds.path = talloc_strdup(mem_ctx, data);
578 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.unix_ds.path);
579 return NT_STATUS_OK;
581 case EPM_PROTOCOL_NULL:
582 return NT_STATUS_OK;
584 default:
585 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
586 break;
589 return NT_STATUS_NOT_SUPPORTED;
592 enum dcerpc_transport_t dcerpc_transport_by_endpoint_protocol(int prot)
594 int i;
596 /* Find a transport that has 'prot' as 4th protocol */
597 for (i=0;i<ARRAY_SIZE(transports);i++) {
598 if (transports[i].num_protocols >= 2 &&
599 transports[i].protseq[1] == prot) {
600 return transports[i].transport;
604 /* Unknown transport */
605 return (unsigned int)-1;
608 _PUBLIC_ enum dcerpc_transport_t dcerpc_transport_by_tower(const struct epm_tower *tower)
610 int i;
612 /* Find a transport that matches this tower */
613 for (i=0;i<ARRAY_SIZE(transports);i++) {
614 int j;
615 if (transports[i].num_protocols != tower->num_floors - 2) {
616 continue;
619 for (j = 0; j < transports[i].num_protocols; j++) {
620 if (transports[i].protseq[j] != tower->floors[j+2].lhs.protocol) {
621 break;
625 if (j == transports[i].num_protocols) {
626 return transports[i].transport;
630 /* Unknown transport */
631 return (unsigned int)-1;
634 _PUBLIC_ const char *derpc_transport_string_by_transport(enum dcerpc_transport_t t)
636 int i;
638 for (i=0; i<ARRAY_SIZE(transports); i++) {
639 if (t == transports[i].transport) {
640 return transports[i].name;
643 return NULL;
646 _PUBLIC_ NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx,
647 struct epm_tower *tower,
648 struct dcerpc_binding **b_out)
650 NTSTATUS status;
651 struct dcerpc_binding *binding;
653 binding = talloc(mem_ctx, struct dcerpc_binding);
654 NT_STATUS_HAVE_NO_MEMORY(binding);
656 ZERO_STRUCT(binding->object);
657 binding->options = NULL;
658 binding->host = NULL;
659 binding->target_hostname = NULL;
660 binding->flags = 0;
661 binding->assoc_group_id = 0;
663 binding->transport = dcerpc_transport_by_tower(tower);
665 if (binding->transport == (unsigned int)-1) {
666 return NT_STATUS_NOT_SUPPORTED;
669 if (tower->num_floors < 1) {
670 return NT_STATUS_OK;
673 /* Set object uuid */
674 status = dcerpc_floor_get_lhs_data(&tower->floors[0], &binding->object);
676 if (!NT_STATUS_IS_OK(status)) {
677 DEBUG(1, ("Error pulling object uuid and version: %s", nt_errstr(status)));
678 return status;
681 /* Ignore floor 1, it contains the NDR version info */
683 binding->options = NULL;
685 /* Set endpoint */
686 if (tower->num_floors >= 4) {
687 binding->endpoint = dcerpc_floor_get_rhs_data(mem_ctx, &tower->floors[3]);
688 } else {
689 binding->endpoint = NULL;
692 /* Set network address */
693 if (tower->num_floors >= 5) {
694 binding->host = dcerpc_floor_get_rhs_data(mem_ctx, &tower->floors[4]);
695 NT_STATUS_HAVE_NO_MEMORY(binding->host);
696 binding->target_hostname = binding->host;
698 *b_out = binding;
699 return NT_STATUS_OK;
702 _PUBLIC_ NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx,
703 const struct dcerpc_binding *binding,
704 struct epm_tower *tower)
706 const enum epm_protocol *protseq = NULL;
707 int num_protocols = -1, i;
708 NTSTATUS status;
710 /* Find transport */
711 for (i=0;i<ARRAY_SIZE(transports);i++) {
712 if (transports[i].transport == binding->transport) {
713 protseq = transports[i].protseq;
714 num_protocols = transports[i].num_protocols;
715 break;
719 if (num_protocols == -1) {
720 DEBUG(0, ("Unable to find transport with id '%d'\n", binding->transport));
721 return NT_STATUS_UNSUCCESSFUL;
724 tower->num_floors = 2 + num_protocols;
725 tower->floors = talloc_array(mem_ctx, struct epm_floor, tower->num_floors);
727 /* Floor 0 */
728 tower->floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
730 tower->floors[0].lhs.lhs_data = dcerpc_floor_pack_lhs_data(tower->floors, &binding->object);
732 tower->floors[0].rhs.uuid.unknown = dcerpc_floor_pack_rhs_if_version_data(tower->floors, &binding->object);
734 /* Floor 1 */
735 tower->floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
737 tower->floors[1].lhs.lhs_data = dcerpc_floor_pack_lhs_data(tower->floors,
738 &ndr_transfer_syntax);
740 tower->floors[1].rhs.uuid.unknown = data_blob_talloc_zero(tower->floors, 2);
742 /* Floor 2 to num_protocols */
743 for (i = 0; i < num_protocols; i++) {
744 tower->floors[2 + i].lhs.protocol = protseq[i];
745 tower->floors[2 + i].lhs.lhs_data = data_blob_talloc(tower->floors, NULL, 0);
746 ZERO_STRUCT(tower->floors[2 + i].rhs);
747 dcerpc_floor_set_rhs_data(tower->floors, &tower->floors[2 + i], "");
750 /* The 4th floor contains the endpoint */
751 if (num_protocols >= 2 && binding->endpoint) {
752 status = dcerpc_floor_set_rhs_data(tower->floors, &tower->floors[3], binding->endpoint);
753 if (NT_STATUS_IS_ERR(status)) {
754 return status;
758 /* The 5th contains the network address */
759 if (num_protocols >= 3 && binding->host) {
760 if (is_ipaddress(binding->host)) {
761 status = dcerpc_floor_set_rhs_data(tower->floors, &tower->floors[4],
762 binding->host);
763 } else {
764 /* note that we don't attempt to resolve the
765 name here - when we get a hostname here we
766 are in the client code, and want to put in
767 a wildcard all-zeros IP for the server to
768 fill in */
769 status = dcerpc_floor_set_rhs_data(tower->floors, &tower->floors[4],
770 "0.0.0.0");
772 if (NT_STATUS_IS_ERR(status)) {
773 return status;
777 return NT_STATUS_OK;