lib/param: Mark source code generators as group build_source
[Samba.git] / librpc / rpc / binding.c
blob52122cfdae66e38f199aba5aa8631506965f8ca3
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 #include "rpc_common.h"
32 #undef strcasecmp
33 #undef strncasecmp
35 #define MAX_PROTSEQ 10
37 static const struct {
38 const char *name;
39 enum dcerpc_transport_t transport;
40 int num_protocols;
41 enum epm_protocol protseq[MAX_PROTSEQ];
42 } transports[] = {
43 { "ncacn_np", NCACN_NP, 3,
44 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SMB, EPM_PROTOCOL_NETBIOS }},
45 { "ncacn_ip_tcp", NCACN_IP_TCP, 3,
46 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_TCP, EPM_PROTOCOL_IP } },
47 { "ncacn_http", NCACN_HTTP, 3,
48 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_HTTP, EPM_PROTOCOL_IP } },
49 { "ncadg_ip_udp", NCACN_IP_UDP, 3,
50 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UDP, EPM_PROTOCOL_IP } },
51 { "ncalrpc", NCALRPC, 2,
52 { EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_NAMED_PIPE } },
53 { "ncacn_unix_stream", NCACN_UNIX_STREAM, 2,
54 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_UNIX_DS } },
55 { "ncadg_unix_dgram", NCADG_UNIX_DGRAM, 2,
56 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UNIX_DS } },
57 { "ncacn_at_dsp", NCACN_AT_DSP, 3,
58 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DSP } },
59 { "ncadg_at_ddp", NCADG_AT_DDP, 3,
60 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DDP } },
61 { "ncacn_vns_ssp", NCACN_VNS_SPP, 3,
62 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_SPP } },
63 { "ncacn_vns_ipc", NCACN_VNS_IPC, 3,
64 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_IPC }, },
65 { "ncadg_ipx", NCADG_IPX, 2,
66 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_IPX },
68 { "ncacn_spx", NCACN_SPX, 3,
69 /* I guess some MS programmer confused the identifier for
70 * EPM_PROTOCOL_UUID (0x0D or 13) with the one for
71 * EPM_PROTOCOL_SPX (0x13) here. -- jelmer*/
72 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_UUID },
76 static const struct {
77 const char *name;
78 uint32_t flag;
79 } ncacn_options[] = {
80 {"sign", DCERPC_SIGN},
81 {"seal", DCERPC_SEAL},
82 {"connect", DCERPC_CONNECT},
83 {"spnego", DCERPC_AUTH_SPNEGO},
84 {"ntlm", DCERPC_AUTH_NTLM},
85 {"krb5", DCERPC_AUTH_KRB5},
86 {"validate", DCERPC_DEBUG_VALIDATE_BOTH},
87 {"print", DCERPC_DEBUG_PRINT_BOTH},
88 {"padcheck", DCERPC_DEBUG_PAD_CHECK},
89 {"bigendian", DCERPC_PUSH_BIGENDIAN},
90 {"smb2", DCERPC_SMB2},
91 {"ndr64", DCERPC_NDR64},
92 {"localaddress", DCERPC_LOCALADDRESS}
95 const char *epm_floor_string(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
97 struct ndr_syntax_id syntax;
98 NTSTATUS status;
100 switch(epm_floor->lhs.protocol) {
101 case EPM_PROTOCOL_UUID:
102 status = dcerpc_floor_get_lhs_data(epm_floor, &syntax);
103 if (NT_STATUS_IS_OK(status)) {
104 /* lhs is used: UUID */
105 char *uuidstr;
107 if (GUID_equal(&syntax.uuid, &ndr_transfer_syntax_ndr.uuid)) {
108 return "NDR";
111 if (GUID_equal(&syntax.uuid, &ndr_transfer_syntax_ndr64.uuid)) {
112 return "NDR64";
115 uuidstr = GUID_string(mem_ctx, &syntax.uuid);
117 return talloc_asprintf(mem_ctx, " uuid %s/0x%02x", uuidstr, syntax.if_version);
118 } else { /* IPX */
119 return talloc_asprintf(mem_ctx, "IPX:%s",
120 data_blob_hex_string_upper(mem_ctx, &epm_floor->rhs.uuid.unknown));
123 case EPM_PROTOCOL_NCACN:
124 return "RPC-C";
126 case EPM_PROTOCOL_NCADG:
127 return "RPC";
129 case EPM_PROTOCOL_NCALRPC:
130 return "NCALRPC";
132 case EPM_PROTOCOL_DNET_NSP:
133 return "DNET/NSP";
135 case EPM_PROTOCOL_IP:
136 return talloc_asprintf(mem_ctx, "IP:%s", epm_floor->rhs.ip.ipaddr);
138 case EPM_PROTOCOL_NAMED_PIPE:
139 return talloc_asprintf(mem_ctx, "NAMED-PIPE:%s", epm_floor->rhs.named_pipe.path);
141 case EPM_PROTOCOL_SMB:
142 return talloc_asprintf(mem_ctx, "SMB:%s", epm_floor->rhs.smb.unc);
144 case EPM_PROTOCOL_UNIX_DS:
145 return talloc_asprintf(mem_ctx, "Unix:%s", epm_floor->rhs.unix_ds.path);
147 case EPM_PROTOCOL_NETBIOS:
148 return talloc_asprintf(mem_ctx, "NetBIOS:%s", epm_floor->rhs.netbios.name);
150 case EPM_PROTOCOL_NETBEUI:
151 return "NETBeui";
153 case EPM_PROTOCOL_SPX:
154 return "SPX";
156 case EPM_PROTOCOL_NB_IPX:
157 return "NB_IPX";
159 case EPM_PROTOCOL_HTTP:
160 return talloc_asprintf(mem_ctx, "HTTP:%d", epm_floor->rhs.http.port);
162 case EPM_PROTOCOL_TCP:
163 return talloc_asprintf(mem_ctx, "TCP:%d", epm_floor->rhs.tcp.port);
165 case EPM_PROTOCOL_UDP:
166 return talloc_asprintf(mem_ctx, "UDP:%d", epm_floor->rhs.udp.port);
168 default:
169 return talloc_asprintf(mem_ctx, "UNK(%02x):", epm_floor->lhs.protocol);
175 form a binding string from a binding structure
177 _PUBLIC_ char *dcerpc_binding_string(TALLOC_CTX *mem_ctx, const struct dcerpc_binding *b)
179 char *s = talloc_strdup(mem_ctx, "");
180 int i;
181 const char *t_name = NULL;
183 if (b->transport != NCA_UNKNOWN) {
184 t_name = derpc_transport_string_by_transport(b->transport);
185 if (!t_name) {
186 return NULL;
190 if (!GUID_all_zero(&b->object.uuid)) {
191 s = talloc_asprintf(s, "%s@",
192 GUID_string(mem_ctx, &b->object.uuid));
195 if (t_name != NULL) {
196 s = talloc_asprintf_append_buffer(s, "%s:", t_name);
197 if (s == NULL) {
198 return NULL;
202 if (b->host) {
203 s = talloc_asprintf_append_buffer(s, "%s", b->host);
206 if (!b->endpoint && !b->options && !b->flags) {
207 return s;
210 s = talloc_asprintf_append_buffer(s, "[");
212 if (b->endpoint) {
213 s = talloc_asprintf_append_buffer(s, "%s", b->endpoint);
216 /* this is a *really* inefficent way of dealing with strings,
217 but this is rarely called and the strings are always short,
218 so I don't care */
219 for (i=0;b->options && b->options[i];i++) {
220 s = talloc_asprintf_append_buffer(s, ",%s", b->options[i]);
221 if (!s) return NULL;
224 for (i=0;i<ARRAY_SIZE(ncacn_options);i++) {
225 if (b->flags & ncacn_options[i].flag) {
226 if (ncacn_options[i].flag == DCERPC_LOCALADDRESS && b->localaddress) {
227 s = talloc_asprintf_append_buffer(s, ",%s=%s", ncacn_options[i].name,
228 b->localaddress);
229 } else {
230 s = talloc_asprintf_append_buffer(s, ",%s", ncacn_options[i].name);
232 if (!s) return NULL;
236 s = talloc_asprintf_append_buffer(s, "]");
238 return s;
242 parse a binding string into a dcerpc_binding structure
244 _PUBLIC_ NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *s, struct dcerpc_binding **b_out)
246 struct dcerpc_binding *b;
247 char *options;
248 char *p;
249 int i, j, comma_count;
251 b = talloc_zero(mem_ctx, struct dcerpc_binding);
252 if (!b) {
253 return NT_STATUS_NO_MEMORY;
256 p = strchr(s, '@');
258 if (p && PTR_DIFF(p, s) == 36) { /* 36 is the length of a UUID */
259 NTSTATUS status;
260 DATA_BLOB blob = data_blob(s, 36);
261 status = GUID_from_data_blob(&blob, &b->object.uuid);
263 if (NT_STATUS_IS_ERR(status)) {
264 DEBUG(0, ("Failed parsing UUID\n"));
265 return status;
268 s = p + 1;
269 } else {
270 ZERO_STRUCT(b->object);
273 b->object.if_version = 0;
275 p = strchr(s, ':');
277 if (p == NULL) {
278 b->transport = NCA_UNKNOWN;
279 } else {
280 char *type = talloc_strndup(mem_ctx, s, PTR_DIFF(p, s));
281 if (!type) {
282 return NT_STATUS_NO_MEMORY;
285 for (i=0;i<ARRAY_SIZE(transports);i++) {
286 if (strcasecmp(type, transports[i].name) == 0) {
287 b->transport = transports[i].transport;
288 break;
292 if (i==ARRAY_SIZE(transports)) {
293 DEBUG(0,("Unknown dcerpc transport '%s'\n", type));
294 return NT_STATUS_INVALID_PARAMETER;
297 talloc_free(type);
299 s = p+1;
302 p = strchr(s, '[');
303 if (p) {
304 b->host = talloc_strndup(b, s, PTR_DIFF(p, s));
305 options = talloc_strdup(mem_ctx, p+1);
306 if (options[strlen(options)-1] != ']') {
307 return NT_STATUS_INVALID_PARAMETER;
309 options[strlen(options)-1] = 0;
310 } else {
311 b->host = talloc_strdup(b, s);
312 options = NULL;
314 if (!b->host) {
315 return NT_STATUS_NO_MEMORY;
318 b->target_hostname = b->host;
320 b->options = NULL;
321 b->flags = 0;
322 b->assoc_group_id = 0;
323 b->endpoint = NULL;
324 b->localaddress = NULL;
326 if (!options) {
327 *b_out = b;
328 return NT_STATUS_OK;
331 comma_count = count_chars(options, ',');
333 b->options = talloc_array(b, const char *, comma_count+2);
334 if (!b->options) {
335 return NT_STATUS_NO_MEMORY;
338 for (i=0; (p = strchr(options, ',')); i++) {
339 b->options[i] = talloc_strndup(b, options, PTR_DIFF(p, options));
340 if (!b->options[i]) {
341 return NT_STATUS_NO_MEMORY;
343 options = p+1;
345 b->options[i] = options;
346 b->options[i+1] = NULL;
348 /* some options are pre-parsed for convenience */
349 for (i=0;b->options[i];i++) {
350 for (j=0;j<ARRAY_SIZE(ncacn_options);j++) {
351 size_t opt_len = strlen(ncacn_options[j].name);
352 if (strncasecmp(ncacn_options[j].name, b->options[i], opt_len) == 0) {
353 int k;
354 char c = b->options[i][opt_len];
356 if (ncacn_options[j].flag == DCERPC_LOCALADDRESS && c == '=') {
357 b->localaddress = talloc_strdup(b, &b->options[i][opt_len+1]);
358 } else if (c != 0) {
359 continue;
362 b->flags |= ncacn_options[j].flag;
363 for (k=i;b->options[k];k++) {
364 b->options[k] = b->options[k+1];
366 i--;
367 break;
372 if (b->options[0]) {
373 /* Endpoint is first option */
374 b->endpoint = b->options[0];
375 if (strlen(b->endpoint) == 0) b->endpoint = NULL;
377 for (i=0;b->options[i];i++) {
378 b->options[i] = b->options[i+1];
382 if (b->options[0] == NULL)
383 b->options = NULL;
385 *b_out = b;
386 return NT_STATUS_OK;
389 _PUBLIC_ NTSTATUS dcerpc_floor_get_lhs_data(const struct epm_floor *epm_floor,
390 struct ndr_syntax_id *syntax)
392 TALLOC_CTX *mem_ctx = talloc_init("floor_get_lhs_data");
393 struct ndr_pull *ndr;
394 enum ndr_err_code ndr_err;
395 uint16_t if_version=0;
397 ndr = ndr_pull_init_blob(&epm_floor->lhs.lhs_data, mem_ctx);
398 if (ndr == NULL) {
399 talloc_free(mem_ctx);
400 return NT_STATUS_NO_MEMORY;
402 ndr->flags |= LIBNDR_FLAG_NOALIGN;
404 ndr_err = ndr_pull_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
405 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
406 talloc_free(mem_ctx);
407 return ndr_map_error2ntstatus(ndr_err);
410 ndr_err = ndr_pull_uint16(ndr, NDR_SCALARS, &if_version);
411 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
412 talloc_free(mem_ctx);
413 return ndr_map_error2ntstatus(ndr_err);
416 syntax->if_version = if_version;
418 talloc_free(mem_ctx);
420 return NT_STATUS_OK;
423 static DATA_BLOB dcerpc_floor_pack_lhs_data(TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax)
425 DATA_BLOB blob;
426 enum ndr_err_code ndr_err;
427 struct ndr_push *ndr;
429 ndr = ndr_push_init_ctx(mem_ctx);
430 if (ndr == NULL) {
431 return data_blob_null;
434 ndr->flags |= LIBNDR_FLAG_NOALIGN;
436 ndr_err = ndr_push_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
437 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
438 return data_blob_null;
440 ndr_err = ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version);
441 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
442 return data_blob_null;
445 blob = ndr_push_blob(ndr);
446 talloc_steal(mem_ctx, blob.data);
447 talloc_free(ndr);
448 return blob;
451 static bool dcerpc_floor_pack_rhs_if_version_data(
452 TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax,
453 DATA_BLOB *pblob)
455 DATA_BLOB blob;
456 struct ndr_push *ndr = ndr_push_init_ctx(mem_ctx);
457 enum ndr_err_code ndr_err;
459 if (ndr == NULL) {
460 return false;
463 ndr->flags |= LIBNDR_FLAG_NOALIGN;
465 ndr_err = ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version >> 16);
466 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
467 return false;
470 blob = ndr_push_blob(ndr);
471 talloc_steal(mem_ctx, blob.data);
472 talloc_free(ndr);
473 *pblob = blob;
474 return true;
477 const char *dcerpc_floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
479 switch (epm_floor->lhs.protocol) {
480 case EPM_PROTOCOL_TCP:
481 if (epm_floor->rhs.tcp.port == 0) return NULL;
482 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.tcp.port);
484 case EPM_PROTOCOL_UDP:
485 if (epm_floor->rhs.udp.port == 0) return NULL;
486 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.udp.port);
488 case EPM_PROTOCOL_HTTP:
489 if (epm_floor->rhs.http.port == 0) return NULL;
490 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.http.port);
492 case EPM_PROTOCOL_IP:
493 return talloc_strdup(mem_ctx, epm_floor->rhs.ip.ipaddr);
495 case EPM_PROTOCOL_NCACN:
496 return NULL;
498 case EPM_PROTOCOL_NCADG:
499 return NULL;
501 case EPM_PROTOCOL_SMB:
502 if (strlen(epm_floor->rhs.smb.unc) == 0) return NULL;
503 return talloc_strdup(mem_ctx, epm_floor->rhs.smb.unc);
505 case EPM_PROTOCOL_NAMED_PIPE:
506 if (strlen(epm_floor->rhs.named_pipe.path) == 0) return NULL;
507 return talloc_strdup(mem_ctx, epm_floor->rhs.named_pipe.path);
509 case EPM_PROTOCOL_NETBIOS:
510 if (strlen(epm_floor->rhs.netbios.name) == 0) return NULL;
511 return talloc_strdup(mem_ctx, epm_floor->rhs.netbios.name);
513 case EPM_PROTOCOL_NCALRPC:
514 return NULL;
516 case EPM_PROTOCOL_VINES_SPP:
517 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_spp.port);
519 case EPM_PROTOCOL_VINES_IPC:
520 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_ipc.port);
522 case EPM_PROTOCOL_STREETTALK:
523 return talloc_strdup(mem_ctx, epm_floor->rhs.streettalk.streettalk);
525 case EPM_PROTOCOL_UNIX_DS:
526 if (strlen(epm_floor->rhs.unix_ds.path) == 0) return NULL;
527 return talloc_strdup(mem_ctx, epm_floor->rhs.unix_ds.path);
529 case EPM_PROTOCOL_NULL:
530 return NULL;
532 default:
533 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
534 break;
537 return NULL;
540 static NTSTATUS dcerpc_floor_set_rhs_data(TALLOC_CTX *mem_ctx,
541 struct epm_floor *epm_floor,
542 const char *data)
544 switch (epm_floor->lhs.protocol) {
545 case EPM_PROTOCOL_TCP:
546 epm_floor->rhs.tcp.port = atoi(data);
547 return NT_STATUS_OK;
549 case EPM_PROTOCOL_UDP:
550 epm_floor->rhs.udp.port = atoi(data);
551 return NT_STATUS_OK;
553 case EPM_PROTOCOL_HTTP:
554 epm_floor->rhs.http.port = atoi(data);
555 return NT_STATUS_OK;
557 case EPM_PROTOCOL_IP:
558 epm_floor->rhs.ip.ipaddr = talloc_strdup(mem_ctx, data);
559 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.ip.ipaddr);
560 return NT_STATUS_OK;
562 case EPM_PROTOCOL_NCACN:
563 epm_floor->rhs.ncacn.minor_version = 0;
564 return NT_STATUS_OK;
566 case EPM_PROTOCOL_NCADG:
567 epm_floor->rhs.ncadg.minor_version = 0;
568 return NT_STATUS_OK;
570 case EPM_PROTOCOL_SMB:
571 epm_floor->rhs.smb.unc = talloc_strdup(mem_ctx, data);
572 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.smb.unc);
573 return NT_STATUS_OK;
575 case EPM_PROTOCOL_NAMED_PIPE:
576 epm_floor->rhs.named_pipe.path = talloc_strdup(mem_ctx, data);
577 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.named_pipe.path);
578 return NT_STATUS_OK;
580 case EPM_PROTOCOL_NETBIOS:
581 epm_floor->rhs.netbios.name = talloc_strdup(mem_ctx, data);
582 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.netbios.name);
583 return NT_STATUS_OK;
585 case EPM_PROTOCOL_NCALRPC:
586 return NT_STATUS_OK;
588 case EPM_PROTOCOL_VINES_SPP:
589 epm_floor->rhs.vines_spp.port = atoi(data);
590 return NT_STATUS_OK;
592 case EPM_PROTOCOL_VINES_IPC:
593 epm_floor->rhs.vines_ipc.port = atoi(data);
594 return NT_STATUS_OK;
596 case EPM_PROTOCOL_STREETTALK:
597 epm_floor->rhs.streettalk.streettalk = talloc_strdup(mem_ctx, data);
598 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.streettalk.streettalk);
599 return NT_STATUS_OK;
601 case EPM_PROTOCOL_UNIX_DS:
602 epm_floor->rhs.unix_ds.path = talloc_strdup(mem_ctx, data);
603 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.unix_ds.path);
604 return NT_STATUS_OK;
606 case EPM_PROTOCOL_NULL:
607 return NT_STATUS_OK;
609 default:
610 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
611 break;
614 return NT_STATUS_NOT_SUPPORTED;
617 enum dcerpc_transport_t dcerpc_transport_by_endpoint_protocol(int prot)
619 int i;
621 /* Find a transport that has 'prot' as 4th protocol */
622 for (i=0;i<ARRAY_SIZE(transports);i++) {
623 if (transports[i].num_protocols >= 2 &&
624 transports[i].protseq[1] == prot) {
625 return transports[i].transport;
629 /* Unknown transport */
630 return (unsigned int)-1;
633 _PUBLIC_ enum dcerpc_transport_t dcerpc_transport_by_tower(const struct epm_tower *tower)
635 int i;
637 /* Find a transport that matches this tower */
638 for (i=0;i<ARRAY_SIZE(transports);i++) {
639 int j;
640 if (transports[i].num_protocols != tower->num_floors - 2) {
641 continue;
644 for (j = 0; j < transports[i].num_protocols; j++) {
645 if (transports[i].protseq[j] != tower->floors[j+2].lhs.protocol) {
646 break;
650 if (j == transports[i].num_protocols) {
651 return transports[i].transport;
655 /* Unknown transport */
656 return (unsigned int)-1;
659 _PUBLIC_ const char *derpc_transport_string_by_transport(enum dcerpc_transport_t t)
661 int i;
663 for (i=0; i<ARRAY_SIZE(transports); i++) {
664 if (t == transports[i].transport) {
665 return transports[i].name;
668 return NULL;
671 _PUBLIC_ NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx,
672 struct epm_tower *tower,
673 struct dcerpc_binding **b_out)
675 NTSTATUS status;
676 struct dcerpc_binding *binding;
679 * A tower needs to have at least 4 floors to carry useful
680 * information. Floor 3 is the transport identifier which defines
681 * how many floors are required at least.
683 if (tower->num_floors < 4) {
684 return NT_STATUS_INVALID_PARAMETER;
687 binding = talloc_zero(mem_ctx, struct dcerpc_binding);
688 NT_STATUS_HAVE_NO_MEMORY(binding);
690 ZERO_STRUCT(binding->object);
691 binding->options = NULL;
692 binding->host = NULL;
693 binding->target_hostname = NULL;
694 binding->flags = 0;
695 binding->assoc_group_id = 0;
697 binding->transport = dcerpc_transport_by_tower(tower);
699 if (binding->transport == (unsigned int)-1) {
700 return NT_STATUS_NOT_SUPPORTED;
703 /* Set object uuid */
704 status = dcerpc_floor_get_lhs_data(&tower->floors[0], &binding->object);
706 if (!NT_STATUS_IS_OK(status)) {
707 DEBUG(1, ("Error pulling object uuid and version: %s", nt_errstr(status)));
708 return status;
711 /* Ignore floor 1, it contains the NDR version info */
713 binding->options = NULL;
715 /* Set endpoint */
716 if (tower->num_floors >= 4) {
717 binding->endpoint = dcerpc_floor_get_rhs_data(binding, &tower->floors[3]);
718 } else {
719 binding->endpoint = NULL;
722 /* Set network address */
723 if (tower->num_floors >= 5) {
724 binding->host = dcerpc_floor_get_rhs_data(binding, &tower->floors[4]);
725 NT_STATUS_HAVE_NO_MEMORY(binding->host);
726 binding->target_hostname = binding->host;
728 *b_out = binding;
729 return NT_STATUS_OK;
732 _PUBLIC_ struct dcerpc_binding *dcerpc_binding_dup(TALLOC_CTX *mem_ctx,
733 const struct dcerpc_binding *b)
735 struct dcerpc_binding *n;
736 uint32_t count;
738 n = talloc_zero(mem_ctx, struct dcerpc_binding);
739 if (n == NULL) {
740 return NULL;
743 n->transport = b->transport;
744 n->object = b->object;
745 n->flags = b->flags;
746 n->assoc_group_id = b->assoc_group_id;
748 if (b->host != NULL) {
749 n->host = talloc_strdup(n, b->host);
750 if (n->host == NULL) {
751 talloc_free(n);
752 return NULL;
756 if (b->target_hostname != NULL) {
757 n->target_hostname = talloc_strdup(n, b->target_hostname);
758 if (n->target_hostname == NULL) {
759 talloc_free(n);
760 return NULL;
764 if (b->target_principal != NULL) {
765 n->target_principal = talloc_strdup(n, b->target_principal);
766 if (n->target_principal == NULL) {
767 talloc_free(n);
768 return NULL;
772 if (b->localaddress != NULL) {
773 n->localaddress = talloc_strdup(n, b->localaddress);
774 if (n->localaddress == NULL) {
775 talloc_free(n);
776 return NULL;
780 if (b->endpoint != NULL) {
781 n->endpoint = talloc_strdup(n, b->endpoint);
782 if (n->endpoint == NULL) {
783 talloc_free(n);
784 return NULL;
788 for (count = 0; b->options && b->options[count]; count++);
790 if (count > 0) {
791 uint32_t i;
793 n->options = talloc_array(n, const char *, count + 1);
794 if (n->options == NULL) {
795 talloc_free(n);
796 return NULL;
799 for (i = 0; i < count; i++) {
800 n->options[i] = talloc_strdup(n->options, b->options[i]);
801 if (n->options[i] == NULL) {
802 talloc_free(n);
803 return NULL;
806 n->options[count] = NULL;
809 return n;
812 _PUBLIC_ NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx,
813 const struct dcerpc_binding *binding,
814 struct epm_tower *tower)
816 const enum epm_protocol *protseq = NULL;
817 int num_protocols = -1, i;
818 NTSTATUS status;
820 /* Find transport */
821 for (i=0;i<ARRAY_SIZE(transports);i++) {
822 if (transports[i].transport == binding->transport) {
823 protseq = transports[i].protseq;
824 num_protocols = transports[i].num_protocols;
825 break;
829 if (num_protocols == -1) {
830 DEBUG(0, ("Unable to find transport with id '%d'\n", binding->transport));
831 return NT_STATUS_UNSUCCESSFUL;
834 tower->num_floors = 2 + num_protocols;
835 tower->floors = talloc_array(mem_ctx, struct epm_floor, tower->num_floors);
837 /* Floor 0 */
838 tower->floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
840 tower->floors[0].lhs.lhs_data = dcerpc_floor_pack_lhs_data(tower->floors, &binding->object);
842 if (!dcerpc_floor_pack_rhs_if_version_data(
843 tower->floors, &binding->object,
844 &tower->floors[0].rhs.uuid.unknown)) {
845 return NT_STATUS_NO_MEMORY;
848 /* Floor 1 */
849 tower->floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
851 tower->floors[1].lhs.lhs_data = dcerpc_floor_pack_lhs_data(tower->floors,
852 &ndr_transfer_syntax_ndr);
854 tower->floors[1].rhs.uuid.unknown = data_blob_talloc_zero(tower->floors, 2);
856 /* Floor 2 to num_protocols */
857 for (i = 0; i < num_protocols; i++) {
858 tower->floors[2 + i].lhs.protocol = protseq[i];
859 tower->floors[2 + i].lhs.lhs_data = data_blob_talloc(tower->floors, NULL, 0);
860 ZERO_STRUCT(tower->floors[2 + i].rhs);
861 dcerpc_floor_set_rhs_data(tower->floors, &tower->floors[2 + i], "");
864 /* The 4th floor contains the endpoint */
865 if (num_protocols >= 2 && binding->endpoint) {
866 status = dcerpc_floor_set_rhs_data(tower->floors, &tower->floors[3], binding->endpoint);
867 if (NT_STATUS_IS_ERR(status)) {
868 return status;
872 /* The 5th contains the network address */
873 if (num_protocols >= 3 && binding->host) {
874 if (is_ipaddress(binding->host) ||
875 (binding->host[0] == '\\' && binding->host[1] == '\\')) {
876 status = dcerpc_floor_set_rhs_data(tower->floors, &tower->floors[4],
877 binding->host);
878 } else {
879 /* note that we don't attempt to resolve the
880 name here - when we get a hostname here we
881 are in the client code, and want to put in
882 a wildcard all-zeros IP for the server to
883 fill in */
884 status = dcerpc_floor_set_rhs_data(tower->floors, &tower->floors[4],
885 "0.0.0.0");
887 if (NT_STATUS_IS_ERR(status)) {
888 return status;
892 return NT_STATUS_OK;