s4-ldb_modules/acl: Get correct NTDSDSA objectGUID to check SPN for
[Samba.git] / librpc / rpc / binding.c
blobb7c3a40a0d14763d950adc9a8acace91bb48a9f7
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
31 #undef strncasecmp
33 #define MAX_PROTSEQ 10
35 static const struct {
36 const char *name;
37 enum dcerpc_transport_t transport;
38 int num_protocols;
39 enum epm_protocol protseq[MAX_PROTSEQ];
40 } transports[] = {
41 { "ncacn_np", NCACN_NP, 3,
42 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SMB, EPM_PROTOCOL_NETBIOS }},
43 { "ncacn_ip_tcp", NCACN_IP_TCP, 3,
44 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_TCP, EPM_PROTOCOL_IP } },
45 { "ncacn_http", NCACN_HTTP, 3,
46 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_HTTP, EPM_PROTOCOL_IP } },
47 { "ncadg_ip_udp", NCACN_IP_UDP, 3,
48 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UDP, EPM_PROTOCOL_IP } },
49 { "ncalrpc", NCALRPC, 2,
50 { EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_NAMED_PIPE } },
51 { "ncacn_unix_stream", NCACN_UNIX_STREAM, 2,
52 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_UNIX_DS } },
53 { "ncadg_unix_dgram", NCADG_UNIX_DGRAM, 2,
54 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UNIX_DS } },
55 { "ncacn_at_dsp", NCACN_AT_DSP, 3,
56 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DSP } },
57 { "ncadg_at_ddp", NCADG_AT_DDP, 3,
58 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DDP } },
59 { "ncacn_vns_ssp", NCACN_VNS_SPP, 3,
60 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_SPP } },
61 { "ncacn_vns_ipc", NCACN_VNS_IPC, 3,
62 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_IPC }, },
63 { "ncadg_ipx", NCADG_IPX, 2,
64 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_IPX },
66 { "ncacn_spx", NCACN_SPX, 3,
67 /* I guess some MS programmer confused the identifier for
68 * EPM_PROTOCOL_UUID (0x0D or 13) with the one for
69 * EPM_PROTOCOL_SPX (0x13) here. -- jelmer*/
70 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_UUID },
74 static const struct {
75 const char *name;
76 uint32_t flag;
77 } ncacn_options[] = {
78 {"sign", DCERPC_SIGN},
79 {"seal", DCERPC_SEAL},
80 {"connect", DCERPC_CONNECT},
81 {"spnego", DCERPC_AUTH_SPNEGO},
82 {"ntlm", DCERPC_AUTH_NTLM},
83 {"krb5", DCERPC_AUTH_KRB5},
84 {"validate", DCERPC_DEBUG_VALIDATE_BOTH},
85 {"print", DCERPC_DEBUG_PRINT_BOTH},
86 {"padcheck", DCERPC_DEBUG_PAD_CHECK},
87 {"bigendian", DCERPC_PUSH_BIGENDIAN},
88 {"smb2", DCERPC_SMB2},
89 {"hdrsign", DCERPC_HEADER_SIGNING},
90 {"ndr64", DCERPC_NDR64},
91 {"localaddress", DCERPC_LOCALADDRESS}
94 const char *epm_floor_string(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
96 struct ndr_syntax_id syntax;
97 NTSTATUS status;
99 switch(epm_floor->lhs.protocol) {
100 case EPM_PROTOCOL_UUID:
101 status = dcerpc_floor_get_lhs_data(epm_floor, &syntax);
102 if (NT_STATUS_IS_OK(status)) {
103 /* lhs is used: UUID */
104 char *uuidstr;
106 if (GUID_equal(&syntax.uuid, &ndr_transfer_syntax.uuid)) {
107 return "NDR";
110 if (GUID_equal(&syntax.uuid, &ndr64_transfer_syntax.uuid)) {
111 return "NDR64";
114 uuidstr = GUID_string(mem_ctx, &syntax.uuid);
116 return talloc_asprintf(mem_ctx, " uuid %s/0x%02x", uuidstr, syntax.if_version);
117 } else { /* IPX */
118 return talloc_asprintf(mem_ctx, "IPX:%s",
119 data_blob_hex_string_upper(mem_ctx, &epm_floor->rhs.uuid.unknown));
122 case EPM_PROTOCOL_NCACN:
123 return "RPC-C";
125 case EPM_PROTOCOL_NCADG:
126 return "RPC";
128 case EPM_PROTOCOL_NCALRPC:
129 return "NCALRPC";
131 case EPM_PROTOCOL_DNET_NSP:
132 return "DNET/NSP";
134 case EPM_PROTOCOL_IP:
135 return talloc_asprintf(mem_ctx, "IP:%s", epm_floor->rhs.ip.ipaddr);
137 case EPM_PROTOCOL_NAMED_PIPE:
138 return talloc_asprintf(mem_ctx, "NAMED-PIPE:%s", epm_floor->rhs.named_pipe.path);
140 case EPM_PROTOCOL_SMB:
141 return talloc_asprintf(mem_ctx, "SMB:%s", epm_floor->rhs.smb.unc);
143 case EPM_PROTOCOL_UNIX_DS:
144 return talloc_asprintf(mem_ctx, "Unix:%s", epm_floor->rhs.unix_ds.path);
146 case EPM_PROTOCOL_NETBIOS:
147 return talloc_asprintf(mem_ctx, "NetBIOS:%s", epm_floor->rhs.netbios.name);
149 case EPM_PROTOCOL_NETBEUI:
150 return "NETBeui";
152 case EPM_PROTOCOL_SPX:
153 return "SPX";
155 case EPM_PROTOCOL_NB_IPX:
156 return "NB_IPX";
158 case EPM_PROTOCOL_HTTP:
159 return talloc_asprintf(mem_ctx, "HTTP:%d", epm_floor->rhs.http.port);
161 case EPM_PROTOCOL_TCP:
162 return talloc_asprintf(mem_ctx, "TCP:%d", epm_floor->rhs.tcp.port);
164 case EPM_PROTOCOL_UDP:
165 return talloc_asprintf(mem_ctx, "UDP:%d", epm_floor->rhs.udp.port);
167 default:
168 return talloc_asprintf(mem_ctx, "UNK(%02x):", epm_floor->lhs.protocol);
174 form a binding string from a binding structure
176 _PUBLIC_ char *dcerpc_binding_string(TALLOC_CTX *mem_ctx, const struct dcerpc_binding *b)
178 char *s = talloc_strdup(mem_ctx, "");
179 int i;
180 const char *t_name = NULL;
182 if (b->transport != NCA_UNKNOWN) {
183 t_name = derpc_transport_string_by_transport(b->transport);
184 if (!t_name) {
185 return NULL;
189 if (!GUID_all_zero(&b->object.uuid)) {
190 s = talloc_asprintf(s, "%s@",
191 GUID_string(mem_ctx, &b->object.uuid));
194 if (t_name != NULL) {
195 s = talloc_asprintf_append_buffer(s, "%s:", t_name);
196 if (s == NULL) {
197 return NULL;
201 if (b->host) {
202 s = talloc_asprintf_append_buffer(s, "%s", b->host);
205 if (!b->endpoint && !b->options && !b->flags) {
206 return s;
209 s = talloc_asprintf_append_buffer(s, "[");
211 if (b->endpoint) {
212 s = talloc_asprintf_append_buffer(s, "%s", b->endpoint);
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;
223 for (i=0;i<ARRAY_SIZE(ncacn_options);i++) {
224 if (b->flags & ncacn_options[i].flag) {
225 if (ncacn_options[i].flag == DCERPC_LOCALADDRESS && b->localaddress) {
226 s = talloc_asprintf_append_buffer(s, ",%s=%s", ncacn_options[i].name,
227 b->localaddress);
228 } else {
229 s = talloc_asprintf_append_buffer(s, ",%s", ncacn_options[i].name);
231 if (!s) return NULL;
235 s = talloc_asprintf_append_buffer(s, "]");
237 return s;
241 parse a binding string into a dcerpc_binding structure
243 _PUBLIC_ NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *s, struct dcerpc_binding **b_out)
245 struct dcerpc_binding *b;
246 char *options;
247 char *p;
248 int i, j, comma_count;
250 b = talloc_zero(mem_ctx, struct dcerpc_binding);
251 if (!b) {
252 return NT_STATUS_NO_MEMORY;
255 p = strchr(s, '@');
257 if (p && PTR_DIFF(p, s) == 36) { /* 36 is the length of a UUID */
258 NTSTATUS status;
259 DATA_BLOB blob = data_blob(s, 36);
260 status = GUID_from_data_blob(&blob, &b->object.uuid);
262 if (NT_STATUS_IS_ERR(status)) {
263 DEBUG(0, ("Failed parsing UUID\n"));
264 return status;
267 s = p + 1;
268 } else {
269 ZERO_STRUCT(b->object);
272 b->object.if_version = 0;
274 p = strchr(s, ':');
276 if (p == NULL) {
277 b->transport = NCA_UNKNOWN;
278 } else {
279 char *type = talloc_strndup(mem_ctx, s, PTR_DIFF(p, s));
280 if (!type) {
281 return NT_STATUS_NO_MEMORY;
284 for (i=0;i<ARRAY_SIZE(transports);i++) {
285 if (strcasecmp(type, transports[i].name) == 0) {
286 b->transport = transports[i].transport;
287 break;
291 if (i==ARRAY_SIZE(transports)) {
292 DEBUG(0,("Unknown dcerpc transport '%s'\n", type));
293 return NT_STATUS_INVALID_PARAMETER;
296 talloc_free(type);
298 s = p+1;
301 p = strchr(s, '[');
302 if (p) {
303 b->host = talloc_strndup(b, s, PTR_DIFF(p, s));
304 options = talloc_strdup(mem_ctx, p+1);
305 if (options[strlen(options)-1] != ']') {
306 return NT_STATUS_INVALID_PARAMETER;
308 options[strlen(options)-1] = 0;
309 } else {
310 b->host = talloc_strdup(b, s);
311 options = NULL;
313 if (!b->host) {
314 return NT_STATUS_NO_MEMORY;
317 b->target_hostname = b->host;
319 b->options = NULL;
320 b->flags = 0;
321 b->assoc_group_id = 0;
322 b->endpoint = NULL;
323 b->localaddress = NULL;
325 if (!options) {
326 *b_out = b;
327 return NT_STATUS_OK;
330 comma_count = count_chars(options, ',');
332 b->options = talloc_array(b, const char *, comma_count+2);
333 if (!b->options) {
334 return NT_STATUS_NO_MEMORY;
337 for (i=0; (p = strchr(options, ',')); i++) {
338 b->options[i] = talloc_strndup(b, options, PTR_DIFF(p, options));
339 if (!b->options[i]) {
340 return NT_STATUS_NO_MEMORY;
342 options = p+1;
344 b->options[i] = options;
345 b->options[i+1] = NULL;
347 /* some options are pre-parsed for convenience */
348 for (i=0;b->options[i];i++) {
349 for (j=0;j<ARRAY_SIZE(ncacn_options);j++) {
350 size_t opt_len = strlen(ncacn_options[j].name);
351 if (strncasecmp(ncacn_options[j].name, b->options[i], opt_len) == 0) {
352 int k;
353 char c = b->options[i][opt_len];
355 if (ncacn_options[j].flag == DCERPC_LOCALADDRESS && c == '=') {
356 b->localaddress = talloc_strdup(b, &b->options[i][opt_len+1]);
357 } else if (c != 0) {
358 continue;
361 b->flags |= ncacn_options[j].flag;
362 for (k=i;b->options[k];k++) {
363 b->options[k] = b->options[k+1];
365 i--;
366 break;
371 if (b->options[0]) {
372 /* Endpoint is first option */
373 b->endpoint = b->options[0];
374 if (strlen(b->endpoint) == 0) b->endpoint = NULL;
376 for (i=0;b->options[i];i++) {
377 b->options[i] = b->options[i+1];
381 if (b->options[0] == NULL)
382 b->options = NULL;
384 *b_out = b;
385 return NT_STATUS_OK;
388 _PUBLIC_ NTSTATUS dcerpc_floor_get_lhs_data(const struct epm_floor *epm_floor,
389 struct ndr_syntax_id *syntax)
391 TALLOC_CTX *mem_ctx = talloc_init("floor_get_lhs_data");
392 struct ndr_pull *ndr;
393 enum ndr_err_code ndr_err;
394 uint16_t if_version=0;
396 ndr = ndr_pull_init_blob(&epm_floor->lhs.lhs_data, mem_ctx);
397 if (ndr == NULL) {
398 talloc_free(mem_ctx);
399 return NT_STATUS_NO_MEMORY;
401 ndr->flags |= LIBNDR_FLAG_NOALIGN;
403 ndr_err = ndr_pull_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
404 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
405 talloc_free(mem_ctx);
406 return ndr_map_error2ntstatus(ndr_err);
409 ndr_err = ndr_pull_uint16(ndr, NDR_SCALARS, &if_version);
410 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
411 talloc_free(mem_ctx);
412 return ndr_map_error2ntstatus(ndr_err);
415 syntax->if_version = if_version;
417 talloc_free(mem_ctx);
419 return NT_STATUS_OK;
422 static DATA_BLOB dcerpc_floor_pack_lhs_data(TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax)
424 DATA_BLOB blob;
425 struct ndr_push *ndr = ndr_push_init_ctx(mem_ctx);
427 ndr->flags |= LIBNDR_FLAG_NOALIGN;
429 ndr_push_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
430 ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version);
432 blob = ndr_push_blob(ndr);
433 talloc_steal(mem_ctx, blob.data);
434 talloc_free(ndr);
435 return blob;
438 static DATA_BLOB dcerpc_floor_pack_rhs_if_version_data(TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax)
440 DATA_BLOB blob;
441 struct ndr_push *ndr = ndr_push_init_ctx(mem_ctx);
443 ndr->flags |= LIBNDR_FLAG_NOALIGN;
445 ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version >> 16);
447 blob = ndr_push_blob(ndr);
448 talloc_steal(mem_ctx, blob.data);
449 talloc_free(ndr);
450 return blob;
453 const char *dcerpc_floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
455 switch (epm_floor->lhs.protocol) {
456 case EPM_PROTOCOL_TCP:
457 if (epm_floor->rhs.tcp.port == 0) return NULL;
458 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.tcp.port);
460 case EPM_PROTOCOL_UDP:
461 if (epm_floor->rhs.udp.port == 0) return NULL;
462 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.udp.port);
464 case EPM_PROTOCOL_HTTP:
465 if (epm_floor->rhs.http.port == 0) return NULL;
466 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.http.port);
468 case EPM_PROTOCOL_IP:
469 return talloc_strdup(mem_ctx, epm_floor->rhs.ip.ipaddr);
471 case EPM_PROTOCOL_NCACN:
472 return NULL;
474 case EPM_PROTOCOL_NCADG:
475 return NULL;
477 case EPM_PROTOCOL_SMB:
478 if (strlen(epm_floor->rhs.smb.unc) == 0) return NULL;
479 return talloc_strdup(mem_ctx, epm_floor->rhs.smb.unc);
481 case EPM_PROTOCOL_NAMED_PIPE:
482 if (strlen(epm_floor->rhs.named_pipe.path) == 0) return NULL;
483 return talloc_strdup(mem_ctx, epm_floor->rhs.named_pipe.path);
485 case EPM_PROTOCOL_NETBIOS:
486 if (strlen(epm_floor->rhs.netbios.name) == 0) return NULL;
487 return talloc_strdup(mem_ctx, epm_floor->rhs.netbios.name);
489 case EPM_PROTOCOL_NCALRPC:
490 return NULL;
492 case EPM_PROTOCOL_VINES_SPP:
493 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_spp.port);
495 case EPM_PROTOCOL_VINES_IPC:
496 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_ipc.port);
498 case EPM_PROTOCOL_STREETTALK:
499 return talloc_strdup(mem_ctx, epm_floor->rhs.streettalk.streettalk);
501 case EPM_PROTOCOL_UNIX_DS:
502 if (strlen(epm_floor->rhs.unix_ds.path) == 0) return NULL;
503 return talloc_strdup(mem_ctx, epm_floor->rhs.unix_ds.path);
505 case EPM_PROTOCOL_NULL:
506 return NULL;
508 default:
509 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
510 break;
513 return NULL;
516 static NTSTATUS dcerpc_floor_set_rhs_data(TALLOC_CTX *mem_ctx,
517 struct epm_floor *epm_floor,
518 const char *data)
520 switch (epm_floor->lhs.protocol) {
521 case EPM_PROTOCOL_TCP:
522 epm_floor->rhs.tcp.port = atoi(data);
523 return NT_STATUS_OK;
525 case EPM_PROTOCOL_UDP:
526 epm_floor->rhs.udp.port = atoi(data);
527 return NT_STATUS_OK;
529 case EPM_PROTOCOL_HTTP:
530 epm_floor->rhs.http.port = atoi(data);
531 return NT_STATUS_OK;
533 case EPM_PROTOCOL_IP:
534 epm_floor->rhs.ip.ipaddr = talloc_strdup(mem_ctx, data);
535 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.ip.ipaddr);
536 return NT_STATUS_OK;
538 case EPM_PROTOCOL_NCACN:
539 epm_floor->rhs.ncacn.minor_version = 0;
540 return NT_STATUS_OK;
542 case EPM_PROTOCOL_NCADG:
543 epm_floor->rhs.ncadg.minor_version = 0;
544 return NT_STATUS_OK;
546 case EPM_PROTOCOL_SMB:
547 epm_floor->rhs.smb.unc = talloc_strdup(mem_ctx, data);
548 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.smb.unc);
549 return NT_STATUS_OK;
551 case EPM_PROTOCOL_NAMED_PIPE:
552 epm_floor->rhs.named_pipe.path = talloc_strdup(mem_ctx, data);
553 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.named_pipe.path);
554 return NT_STATUS_OK;
556 case EPM_PROTOCOL_NETBIOS:
557 epm_floor->rhs.netbios.name = talloc_strdup(mem_ctx, data);
558 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.netbios.name);
559 return NT_STATUS_OK;
561 case EPM_PROTOCOL_NCALRPC:
562 return NT_STATUS_OK;
564 case EPM_PROTOCOL_VINES_SPP:
565 epm_floor->rhs.vines_spp.port = atoi(data);
566 return NT_STATUS_OK;
568 case EPM_PROTOCOL_VINES_IPC:
569 epm_floor->rhs.vines_ipc.port = atoi(data);
570 return NT_STATUS_OK;
572 case EPM_PROTOCOL_STREETTALK:
573 epm_floor->rhs.streettalk.streettalk = talloc_strdup(mem_ctx, data);
574 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.streettalk.streettalk);
575 return NT_STATUS_OK;
577 case EPM_PROTOCOL_UNIX_DS:
578 epm_floor->rhs.unix_ds.path = talloc_strdup(mem_ctx, data);
579 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.unix_ds.path);
580 return NT_STATUS_OK;
582 case EPM_PROTOCOL_NULL:
583 return NT_STATUS_OK;
585 default:
586 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
587 break;
590 return NT_STATUS_NOT_SUPPORTED;
593 enum dcerpc_transport_t dcerpc_transport_by_endpoint_protocol(int prot)
595 int i;
597 /* Find a transport that has 'prot' as 4th protocol */
598 for (i=0;i<ARRAY_SIZE(transports);i++) {
599 if (transports[i].num_protocols >= 2 &&
600 transports[i].protseq[1] == prot) {
601 return transports[i].transport;
605 /* Unknown transport */
606 return (unsigned int)-1;
609 _PUBLIC_ enum dcerpc_transport_t dcerpc_transport_by_tower(const struct epm_tower *tower)
611 int i;
613 /* Find a transport that matches this tower */
614 for (i=0;i<ARRAY_SIZE(transports);i++) {
615 int j;
616 if (transports[i].num_protocols != tower->num_floors - 2) {
617 continue;
620 for (j = 0; j < transports[i].num_protocols; j++) {
621 if (transports[i].protseq[j] != tower->floors[j+2].lhs.protocol) {
622 break;
626 if (j == transports[i].num_protocols) {
627 return transports[i].transport;
631 /* Unknown transport */
632 return (unsigned int)-1;
635 _PUBLIC_ const char *derpc_transport_string_by_transport(enum dcerpc_transport_t t)
637 int i;
639 for (i=0; i<ARRAY_SIZE(transports); i++) {
640 if (t == transports[i].transport) {
641 return transports[i].name;
644 return NULL;
647 _PUBLIC_ NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx,
648 struct epm_tower *tower,
649 struct dcerpc_binding **b_out)
651 NTSTATUS status;
652 struct dcerpc_binding *binding;
654 binding = talloc_zero(mem_ctx, struct dcerpc_binding);
655 NT_STATUS_HAVE_NO_MEMORY(binding);
657 ZERO_STRUCT(binding->object);
658 binding->options = NULL;
659 binding->host = NULL;
660 binding->target_hostname = NULL;
661 binding->flags = 0;
662 binding->assoc_group_id = 0;
664 binding->transport = dcerpc_transport_by_tower(tower);
666 if (binding->transport == (unsigned int)-1) {
667 return NT_STATUS_NOT_SUPPORTED;
670 if (tower->num_floors < 1) {
671 return NT_STATUS_OK;
674 /* Set object uuid */
675 status = dcerpc_floor_get_lhs_data(&tower->floors[0], &binding->object);
677 if (!NT_STATUS_IS_OK(status)) {
678 DEBUG(1, ("Error pulling object uuid and version: %s", nt_errstr(status)));
679 return status;
682 /* Ignore floor 1, it contains the NDR version info */
684 binding->options = NULL;
686 /* Set endpoint */
687 if (tower->num_floors >= 4) {
688 binding->endpoint = dcerpc_floor_get_rhs_data(binding, &tower->floors[3]);
689 } else {
690 binding->endpoint = NULL;
693 /* Set network address */
694 if (tower->num_floors >= 5) {
695 binding->host = dcerpc_floor_get_rhs_data(binding, &tower->floors[4]);
696 NT_STATUS_HAVE_NO_MEMORY(binding->host);
697 binding->target_hostname = binding->host;
699 *b_out = binding;
700 return NT_STATUS_OK;
703 _PUBLIC_ NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx,
704 const struct dcerpc_binding *binding,
705 struct epm_tower *tower)
707 const enum epm_protocol *protseq = NULL;
708 int num_protocols = -1, i;
709 NTSTATUS status;
711 /* Find transport */
712 for (i=0;i<ARRAY_SIZE(transports);i++) {
713 if (transports[i].transport == binding->transport) {
714 protseq = transports[i].protseq;
715 num_protocols = transports[i].num_protocols;
716 break;
720 if (num_protocols == -1) {
721 DEBUG(0, ("Unable to find transport with id '%d'\n", binding->transport));
722 return NT_STATUS_UNSUCCESSFUL;
725 tower->num_floors = 2 + num_protocols;
726 tower->floors = talloc_array(mem_ctx, struct epm_floor, tower->num_floors);
728 /* Floor 0 */
729 tower->floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
731 tower->floors[0].lhs.lhs_data = dcerpc_floor_pack_lhs_data(tower->floors, &binding->object);
733 tower->floors[0].rhs.uuid.unknown = dcerpc_floor_pack_rhs_if_version_data(tower->floors, &binding->object);
735 /* Floor 1 */
736 tower->floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
738 tower->floors[1].lhs.lhs_data = dcerpc_floor_pack_lhs_data(tower->floors,
739 &ndr_transfer_syntax);
741 tower->floors[1].rhs.uuid.unknown = data_blob_talloc_zero(tower->floors, 2);
743 /* Floor 2 to num_protocols */
744 for (i = 0; i < num_protocols; i++) {
745 tower->floors[2 + i].lhs.protocol = protseq[i];
746 tower->floors[2 + i].lhs.lhs_data = data_blob_talloc(tower->floors, NULL, 0);
747 ZERO_STRUCT(tower->floors[2 + i].rhs);
748 dcerpc_floor_set_rhs_data(tower->floors, &tower->floors[2 + i], "");
751 /* The 4th floor contains the endpoint */
752 if (num_protocols >= 2 && binding->endpoint) {
753 status = dcerpc_floor_set_rhs_data(tower->floors, &tower->floors[3], binding->endpoint);
754 if (NT_STATUS_IS_ERR(status)) {
755 return status;
759 /* The 5th contains the network address */
760 if (num_protocols >= 3 && binding->host) {
761 if (is_ipaddress(binding->host) ||
762 (binding->host[0] == '\\' && binding->host[1] == '\\')) {
763 status = dcerpc_floor_set_rhs_data(tower->floors, &tower->floors[4],
764 binding->host);
765 } else {
766 /* note that we don't attempt to resolve the
767 name here - when we get a hostname here we
768 are in the client code, and want to put in
769 a wildcard all-zeros IP for the server to
770 fill in */
771 status = dcerpc_floor_set_rhs_data(tower->floors, &tower->floors[4],
772 "0.0.0.0");
774 if (NT_STATUS_IS_ERR(status)) {
775 return status;
779 return NT_STATUS_OK;