selftest/Samba3: start samba_dcerpcd in clusteredmember
[samba.git] / librpc / rpc / binding.c
blobeaf3430c9d32207c08df757eb578e73ab5511c76
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
10 Copyright (C) Stefan Metzmacher 2014
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "includes.h"
27 #include "../../lib/util/util_net.h"
28 #include "librpc/gen_ndr/ndr_epmapper.h"
29 #include "librpc/gen_ndr/ndr_misc.h"
30 #include "librpc/rpc/dcerpc.h"
31 #include "rpc_common.h"
33 #undef strcasecmp
34 #undef strncasecmp
36 #define MAX_PROTSEQ 10
38 struct dcerpc_binding {
39 enum dcerpc_transport_t transport;
40 struct GUID object;
41 const char *object_string;
42 const char *host;
43 const char *target_hostname;
44 const char *target_principal;
45 const char *endpoint;
46 const char **options;
47 uint32_t flags;
48 uint32_t assoc_group_id;
49 char assoc_group_string[11]; /* 0x3456789a + '\0' */
52 static const struct {
53 const char *name;
54 enum dcerpc_transport_t transport;
55 int num_protocols;
56 enum epm_protocol protseq[MAX_PROTSEQ];
57 } transports[] = {
58 { "ncacn_np", NCACN_NP, 3,
59 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SMB, EPM_PROTOCOL_NETBIOS }},
60 { "ncacn_ip_tcp", NCACN_IP_TCP, 3,
61 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_TCP, EPM_PROTOCOL_IP } },
62 { "ncacn_http", NCACN_HTTP, 3,
63 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_HTTP, EPM_PROTOCOL_IP } },
64 { "ncadg_ip_udp", NCACN_IP_UDP, 3,
65 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UDP, EPM_PROTOCOL_IP } },
66 { "ncalrpc", NCALRPC, 2,
67 { EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_NAMED_PIPE } },
68 { "ncacn_unix_stream", NCACN_UNIX_STREAM, 2,
69 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_UNIX_DS } },
70 { "ncadg_unix_dgram", NCADG_UNIX_DGRAM, 2,
71 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UNIX_DS } },
72 { "ncacn_at_dsp", NCACN_AT_DSP, 3,
73 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DSP } },
74 { "ncadg_at_ddp", NCADG_AT_DDP, 3,
75 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DDP } },
76 { "ncacn_vns_ssp", NCACN_VNS_SPP, 3,
77 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_SPP } },
78 { "ncacn_vns_ipc", NCACN_VNS_IPC, 3,
79 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_IPC }, },
80 { "ncadg_ipx", NCADG_IPX, 2,
81 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_IPX },
83 { "ncacn_spx", NCACN_SPX, 3,
84 /* I guess some MS programmer confused the identifier for
85 * EPM_PROTOCOL_UUID (0x0D or 13) with the one for
86 * EPM_PROTOCOL_SPX (0x13) here. -- jelmer*/
87 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_UUID },
91 static const struct ncacn_option {
92 const char *name;
93 uint32_t flag;
94 } ncacn_options[] = {
95 {"sign", DCERPC_SIGN},
96 {"seal", DCERPC_SEAL},
97 {"connect", DCERPC_CONNECT},
98 {"spnego", DCERPC_AUTH_SPNEGO},
99 {"ntlm", DCERPC_AUTH_NTLM},
100 {"krb5", DCERPC_AUTH_KRB5},
101 {"schannel", DCERPC_SCHANNEL | DCERPC_SCHANNEL_AUTO},
102 {"validate", DCERPC_DEBUG_VALIDATE_BOTH},
103 {"print", DCERPC_DEBUG_PRINT_BOTH},
104 {"padcheck", DCERPC_DEBUG_PAD_CHECK},
105 {"bigendian", DCERPC_PUSH_BIGENDIAN},
106 {"smb1", DCERPC_SMB1},
107 {"smb2", DCERPC_SMB2},
108 {"ndr64", DCERPC_NDR64},
109 {"packet", DCERPC_PACKET},
112 static const struct ncacn_option *ncacn_option_by_name(const char *name)
114 size_t i;
116 for (i=0; i<ARRAY_SIZE(ncacn_options); i++) {
117 int ret;
119 ret = strcasecmp(ncacn_options[i].name, name);
120 if (ret != 0) {
121 continue;
124 return &ncacn_options[i];
127 return NULL;
130 const char *epm_floor_string(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
132 struct ndr_syntax_id syntax;
133 NTSTATUS status;
135 switch(epm_floor->lhs.protocol) {
136 case EPM_PROTOCOL_UUID:
137 status = dcerpc_floor_get_uuid_full(epm_floor, &syntax);
138 if (NT_STATUS_IS_OK(status)) {
139 /* lhs is used: UUID */
140 struct GUID_txt_buf buf;
142 if (GUID_equal(&syntax.uuid, &ndr_transfer_syntax_ndr.uuid)) {
143 return "NDR";
146 if (GUID_equal(&syntax.uuid, &ndr_transfer_syntax_ndr64.uuid)) {
147 return "NDR64";
150 return talloc_asprintf(
151 mem_ctx,
152 " uuid %s/0x%02x",
153 GUID_buf_string(&syntax.uuid, &buf),
154 syntax.if_version);
155 } else { /* IPX */
156 return talloc_asprintf(mem_ctx, "IPX:%s",
157 data_blob_hex_string_upper(mem_ctx, &epm_floor->rhs.uuid.unknown));
160 case EPM_PROTOCOL_NCACN:
161 return "RPC-C";
163 case EPM_PROTOCOL_NCADG:
164 return "RPC";
166 case EPM_PROTOCOL_NCALRPC:
167 return "NCALRPC";
169 case EPM_PROTOCOL_DNET_NSP:
170 return "DNET/NSP";
172 case EPM_PROTOCOL_IP:
173 return talloc_asprintf(mem_ctx, "IP:%s", epm_floor->rhs.ip.ipaddr);
175 case EPM_PROTOCOL_NAMED_PIPE:
176 return talloc_asprintf(mem_ctx, "NAMED-PIPE:%s", epm_floor->rhs.named_pipe.path);
178 case EPM_PROTOCOL_SMB:
179 return talloc_asprintf(mem_ctx, "SMB:%s", epm_floor->rhs.smb.unc);
181 case EPM_PROTOCOL_UNIX_DS:
182 return talloc_asprintf(mem_ctx, "Unix:%s", epm_floor->rhs.unix_ds.path);
184 case EPM_PROTOCOL_NETBIOS:
185 return talloc_asprintf(mem_ctx, "NetBIOS:%s", epm_floor->rhs.netbios.name);
187 case EPM_PROTOCOL_NETBEUI:
188 return "NETBeui";
190 case EPM_PROTOCOL_SPX:
191 return "SPX";
193 case EPM_PROTOCOL_NB_IPX:
194 return "NB_IPX";
196 case EPM_PROTOCOL_HTTP:
197 return talloc_asprintf(mem_ctx, "HTTP:%"PRIu16, epm_floor->rhs.http.port);
199 case EPM_PROTOCOL_TCP:
200 return talloc_asprintf(mem_ctx, "TCP:%"PRIu16, epm_floor->rhs.tcp.port);
202 case EPM_PROTOCOL_UDP:
203 return talloc_asprintf(mem_ctx, "UDP:%"PRIu16, epm_floor->rhs.udp.port);
205 default:
206 return talloc_asprintf(mem_ctx, "UNK(%02x):", epm_floor->lhs.protocol);
212 form a binding string from a binding structure
214 _PUBLIC_ char *dcerpc_binding_string(TALLOC_CTX *mem_ctx, const struct dcerpc_binding *b)
216 char *s = NULL;
217 size_t i;
218 const char *t_name = NULL;
219 bool option_section = false;
220 const char *target_hostname = NULL;
222 if (b->transport != NCA_UNKNOWN) {
223 t_name = derpc_transport_string_by_transport(b->transport);
224 if (!t_name) {
225 return NULL;
229 s = talloc_strdup(mem_ctx, "");
231 if (!GUID_all_zero(&b->object)) {
232 struct GUID_txt_buf buf;
233 talloc_asprintf_addbuf(
234 &s, "%s@", GUID_buf_string(&b->object, &buf));
237 if (t_name != NULL) {
238 talloc_asprintf_addbuf(&s, "%s:", t_name);
241 if (b->host) {
242 talloc_asprintf_addbuf(&s, "%s", b->host);
245 target_hostname = b->target_hostname;
246 if (target_hostname != NULL && b->host != NULL) {
247 if (strcmp(target_hostname, b->host) == 0) {
248 target_hostname = NULL;
252 option_section =
253 (b->endpoint != NULL) ||
254 (target_hostname != NULL) ||
255 (b->target_principal != NULL) ||
256 (b->assoc_group_id != 0) ||
257 (b->options != NULL) ||
258 (b->flags != 0);
260 if (!option_section) {
261 return s;
264 talloc_asprintf_addbuf(&s, "[");
266 if (b->endpoint) {
267 talloc_asprintf_addbuf(&s, "%s", b->endpoint);
270 for (i=0;i<ARRAY_SIZE(ncacn_options);i++) {
271 if (!(b->flags & ncacn_options[i].flag)) {
272 continue;
275 talloc_asprintf_addbuf(&s, ",%s", ncacn_options[i].name);
278 if (target_hostname) {
279 talloc_asprintf_addbuf(
280 &s, ",target_hostname=%s", b->target_hostname);
283 if (b->target_principal) {
284 talloc_asprintf_addbuf(
285 &s, ",target_principal=%s", b->target_principal);
288 if (b->assoc_group_id != 0) {
289 talloc_asprintf_addbuf(
290 &s, ",assoc_group_id=0x%08x", b->assoc_group_id);
293 for (i=0;b->options && b->options[i];i++) {
294 talloc_asprintf_addbuf(&s, ",%s", b->options[i]);
297 talloc_asprintf_addbuf(&s, "]");
299 return s;
303 parse a binding string into a dcerpc_binding structure
305 _PUBLIC_ NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *_s, struct dcerpc_binding **b_out)
307 char *_t;
308 struct dcerpc_binding *b;
309 char *s;
310 char *options = NULL;
311 char *p;
312 size_t i;
313 NTSTATUS status;
315 b = talloc_zero(mem_ctx, struct dcerpc_binding);
316 if (!b) {
317 return NT_STATUS_NO_MEMORY;
320 _t = talloc_strdup(b, _s);
321 if (_t == NULL) {
322 talloc_free(b);
323 return NT_STATUS_NO_MEMORY;
326 s = _t;
328 p = strchr(s, '[');
329 if (p) {
330 char *q = p + strlen(p) - 1;
331 if (*q != ']') {
332 talloc_free(b);
333 return NT_STATUS_INVALID_PARAMETER_MIX;
335 *p = '\0';
336 *q = '\0';
337 options = p + 1;
340 p = strchr(s, '@');
342 if (p && PTR_DIFF(p, s) == 36) { /* 36 is the length of a UUID */
343 *p = '\0';
345 status = dcerpc_binding_set_string_option(b, "object", s);
346 if (!NT_STATUS_IS_OK(status)) {
347 talloc_free(b);
348 return status;
351 s = p + 1;
354 p = strchr(s, ':');
356 if (p == NULL) {
357 b->transport = NCA_UNKNOWN;
358 } else if (is_ipaddress_v6(s)) {
359 b->transport = NCA_UNKNOWN;
360 } else {
361 *p = '\0';
363 status = dcerpc_binding_set_string_option(b, "transport", s);
364 if (!NT_STATUS_IS_OK(status)) {
365 talloc_free(b);
366 return status;
369 s = p + 1;
372 if (strlen(s) > 0) {
373 status = dcerpc_binding_set_string_option(b, "host", s);
374 if (!NT_STATUS_IS_OK(status)) {
375 talloc_free(b);
376 return status;
379 b->target_hostname = talloc_strdup(b, b->host);
380 if (b->target_hostname == NULL) {
381 talloc_free(b);
382 return NT_STATUS_NO_MEMORY;
386 for (i=0; options != NULL; i++) {
387 const char *name = options;
388 const char *value = NULL;
390 p = strchr(options, ',');
391 if (p != NULL) {
392 *p = '\0';
393 options = p+1;
394 } else {
395 options = NULL;
398 p = strchr(name, '=');
399 if (p != NULL) {
400 *p = '\0';
401 value = p + 1;
404 if (value == NULL) {
406 * If it's not a key=value pair
407 * it might be a ncacn_option
408 * or if it's the first option
409 * it's the endpoint.
411 const struct ncacn_option *no = NULL;
413 value = name;
415 no = ncacn_option_by_name(name);
416 if (no == NULL) {
417 if (i > 0) {
419 * we don't allow unknown options
421 return NT_STATUS_INVALID_PARAMETER_MIX;
425 * This is the endpoint
427 name = "endpoint";
428 if (strlen(value) == 0) {
429 value = NULL;
434 status = dcerpc_binding_set_string_option(b, name, value);
435 if (!NT_STATUS_IS_OK(status)) {
436 talloc_free(b);
437 return status;
441 talloc_free(_t);
442 *b_out = b;
443 return NT_STATUS_OK;
446 _PUBLIC_ struct GUID dcerpc_binding_get_object(const struct dcerpc_binding *b)
448 return b->object;
451 _PUBLIC_ NTSTATUS dcerpc_binding_set_object(struct dcerpc_binding *b,
452 struct GUID object)
454 char *tmp = discard_const_p(char, b->object_string);
456 if (GUID_all_zero(&object)) {
457 talloc_free(tmp);
458 b->object_string = NULL;
459 ZERO_STRUCT(b->object);
460 return NT_STATUS_OK;
463 b->object_string = GUID_string(b, &object);
464 if (b->object_string == NULL) {
465 b->object_string = tmp;
466 return NT_STATUS_NO_MEMORY;
468 talloc_free(tmp);
470 b->object = object;
471 return NT_STATUS_OK;
474 _PUBLIC_ enum dcerpc_transport_t dcerpc_binding_get_transport(const struct dcerpc_binding *b)
476 return b->transport;
479 _PUBLIC_ NTSTATUS dcerpc_binding_set_transport(struct dcerpc_binding *b,
480 enum dcerpc_transport_t transport)
482 NTSTATUS status;
485 * TODO: we may want to check the transport value is
486 * wellknown.
488 if (b->transport == transport) {
489 return NT_STATUS_OK;
493 * This implicitly resets the endpoint
494 * as the endpoint is transport specific.
496 * It also resets the assoc group as it's
497 * also endpoint specific.
499 * TODO: in future we may reset more options
500 * here.
502 status = dcerpc_binding_set_string_option(b, "endpoint", NULL);
503 if (!NT_STATUS_IS_OK(status)) {
504 return status;
507 b->assoc_group_id = 0;
509 b->transport = transport;
510 return NT_STATUS_OK;
513 _PUBLIC_ void dcerpc_binding_get_auth_info(const struct dcerpc_binding *b,
514 enum dcerpc_AuthType *_auth_type,
515 enum dcerpc_AuthLevel *_auth_level)
517 enum dcerpc_AuthType auth_type;
518 enum dcerpc_AuthLevel auth_level;
520 if (b->flags & DCERPC_AUTH_SPNEGO) {
521 auth_type = DCERPC_AUTH_TYPE_SPNEGO;
522 } else if (b->flags & DCERPC_AUTH_KRB5) {
523 auth_type = DCERPC_AUTH_TYPE_KRB5;
524 } else if (b->flags & DCERPC_SCHANNEL) {
525 auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
526 } else if (b->flags & DCERPC_AUTH_NTLM) {
527 auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
528 } else {
529 auth_type = DCERPC_AUTH_TYPE_NONE;
532 if (b->flags & DCERPC_SEAL) {
533 auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
534 } else if (b->flags & DCERPC_SIGN) {
535 auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
536 } else if (b->flags & DCERPC_CONNECT) {
537 auth_level = DCERPC_AUTH_LEVEL_CONNECT;
538 } else if (b->flags & DCERPC_PACKET) {
539 auth_level = DCERPC_AUTH_LEVEL_PACKET;
540 } else if (auth_type != DCERPC_AUTH_TYPE_NONE) {
541 auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
542 } else {
543 auth_level = DCERPC_AUTH_LEVEL_NONE;
546 if (_auth_type != NULL) {
547 *_auth_type = auth_type;
550 if (_auth_level != NULL) {
551 *_auth_level = auth_level;
555 _PUBLIC_ uint32_t dcerpc_binding_get_assoc_group_id(const struct dcerpc_binding *b)
557 return b->assoc_group_id;
560 _PUBLIC_ NTSTATUS dcerpc_binding_set_assoc_group_id(struct dcerpc_binding *b,
561 uint32_t assoc_group_id)
563 b->assoc_group_id = assoc_group_id;
564 return NT_STATUS_OK;
567 _PUBLIC_ struct ndr_syntax_id dcerpc_binding_get_abstract_syntax(const struct dcerpc_binding *b)
569 const char *s = dcerpc_binding_get_string_option(b, "abstract_syntax");
570 bool ok;
571 struct ndr_syntax_id id;
573 if (s == NULL) {
574 return ndr_syntax_id_null;
577 ok = ndr_syntax_id_from_string(s, &id);
578 if (!ok) {
579 return ndr_syntax_id_null;
582 return id;
585 _PUBLIC_ NTSTATUS dcerpc_binding_set_abstract_syntax(struct dcerpc_binding *b,
586 const struct ndr_syntax_id *syntax)
588 NTSTATUS status;
589 struct ndr_syntax_id_buf buf;
591 if (syntax == NULL) {
592 status = dcerpc_binding_set_string_option(b, "abstract_syntax", NULL);
593 return status;
596 if (ndr_syntax_id_equal(&ndr_syntax_id_null, syntax)) {
597 status = dcerpc_binding_set_string_option(b, "abstract_syntax", NULL);
598 return status;
601 status = dcerpc_binding_set_string_option(
602 b, "abstract_syntax", ndr_syntax_id_buf_string(syntax, &buf));
603 return status;
606 _PUBLIC_ const char *dcerpc_binding_get_string_option(const struct dcerpc_binding *b,
607 const char *name)
609 struct {
610 const char *name;
611 const char *value;
612 #define _SPECIAL(x) { .name = #x, .value = b->x, }
613 } specials[] = {
614 { .name = "object", .value = b->object_string, },
615 _SPECIAL(host),
616 _SPECIAL(endpoint),
617 _SPECIAL(target_hostname),
618 _SPECIAL(target_principal),
619 #undef _SPECIAL
621 const struct ncacn_option *no = NULL;
622 size_t name_len = strlen(name);
623 size_t i;
624 int ret;
626 ret = strcmp(name, "transport");
627 if (ret == 0) {
628 return derpc_transport_string_by_transport(b->transport);
631 ret = strcmp(name, "assoc_group_id");
632 if (ret == 0) {
633 char *tmp = discard_const_p(char, b->assoc_group_string);
635 if (b->assoc_group_id == 0) {
636 return NULL;
639 snprintf(tmp, sizeof(b->assoc_group_string),
640 "0x%08x", b->assoc_group_id);
641 return (const char *)b->assoc_group_string;
644 for (i=0; i < ARRAY_SIZE(specials); i++) {
645 ret = strcmp(specials[i].name, name);
646 if (ret != 0) {
647 continue;
650 return specials[i].value;
653 no = ncacn_option_by_name(name);
654 if (no != NULL) {
655 if (b->flags & no->flag) {
656 return no->name;
659 return NULL;
662 if (b->options == NULL) {
663 return NULL;
666 for (i=0; b->options[i]; i++) {
667 const char *o = b->options[i];
668 const char *vs = NULL;
670 ret = strncmp(name, o, name_len);
671 if (ret != 0) {
672 continue;
675 if (o[name_len] != '=') {
676 continue;
679 vs = &o[name_len + 1];
681 return vs;
684 return NULL;
687 _PUBLIC_ char *dcerpc_binding_copy_string_option(TALLOC_CTX *mem_ctx,
688 const struct dcerpc_binding *b,
689 const char *name)
691 const char *c = dcerpc_binding_get_string_option(b, name);
692 char *v;
694 if (c == NULL) {
695 errno = ENOENT;
696 return NULL;
699 v = talloc_strdup(mem_ctx, c);
700 if (v == NULL) {
701 errno = ENOMEM;
702 return NULL;
705 return v;
708 _PUBLIC_ NTSTATUS dcerpc_binding_set_string_option(struct dcerpc_binding *b,
709 const char *name,
710 const char *value)
712 struct {
713 const char *name;
714 const char **ptr;
715 #define _SPECIAL(x) { .name = #x, .ptr = &b->x, }
716 } specials[] = {
717 _SPECIAL(host),
718 _SPECIAL(endpoint),
719 _SPECIAL(target_hostname),
720 _SPECIAL(target_principal),
721 #undef _SPECIAL
723 const struct ncacn_option *no = NULL;
724 size_t name_len = strlen(name);
725 const char *opt = NULL;
726 char *tmp;
727 size_t i;
728 int ret;
731 * Note: value == NULL, means delete it.
732 * value != NULL means add or reset.
735 ret = strcmp(name, "transport");
736 if (ret == 0) {
737 enum dcerpc_transport_t t = dcerpc_transport_by_name(value);
739 if (t == NCA_UNKNOWN && value != NULL) {
740 return NT_STATUS_INVALID_PARAMETER_MIX;
743 return dcerpc_binding_set_transport(b, t);
746 ret = strcmp(name, "object");
747 if (ret == 0) {
748 NTSTATUS status;
749 struct GUID uuid = GUID_zero();
751 if (value != NULL) {
752 DATA_BLOB blob;
753 blob = data_blob_string_const(value);
754 if (blob.length != 36) {
755 return NT_STATUS_INVALID_PARAMETER_MIX;
758 status = GUID_from_data_blob(&blob, &uuid);
759 if (!NT_STATUS_IS_OK(status)) {
760 return status;
764 return dcerpc_binding_set_object(b, uuid);
767 ret = strcmp(name, "assoc_group_id");
768 if (ret == 0) {
769 uint32_t assoc_group_id = 0;
771 if (value != NULL) {
772 char c;
774 ret = sscanf(value, "0x%08x%c", &assoc_group_id, &c);
775 if (ret != 1) {
776 return NT_STATUS_INVALID_PARAMETER_MIX;
780 return dcerpc_binding_set_assoc_group_id(b, assoc_group_id);
783 for (i=0; i < ARRAY_SIZE(specials); i++) {
784 ret = strcmp(specials[i].name, name);
785 if (ret != 0) {
786 continue;
789 tmp = discard_const_p(char, *specials[i].ptr);
791 if (value == NULL) {
792 talloc_free(tmp);
793 *specials[i].ptr = NULL;
794 return NT_STATUS_OK;
797 if (value[0] == '\0') {
798 return NT_STATUS_INVALID_PARAMETER_MIX;
801 *specials[i].ptr = talloc_strdup(b, value);
802 if (*specials[i].ptr == NULL) {
803 *specials[i].ptr = tmp;
804 return NT_STATUS_NO_MEMORY;
806 talloc_free(tmp);
808 return NT_STATUS_OK;
811 no = ncacn_option_by_name(name);
812 if (no != NULL) {
813 if (value == NULL) {
814 b->flags &= ~no->flag;
815 return NT_STATUS_OK;
818 ret = strcasecmp(no->name, value);
819 if (ret != 0) {
820 return NT_STATUS_INVALID_PARAMETER_MIX;
823 b->flags |= no->flag;
824 return NT_STATUS_OK;
827 for (i=0; b->options && b->options[i]; i++) {
828 const char *o = b->options[i];
830 ret = strncmp(name, o, name_len);
831 if (ret != 0) {
832 continue;
835 if (o[name_len] != '=') {
836 continue;
839 opt = o;
840 break;
843 if (opt == NULL) {
844 const char **n;
846 if (value == NULL) {
847 return NT_STATUS_OK;
850 n = talloc_realloc(b, b->options, const char *, i + 2);
851 if (n == NULL) {
852 return NT_STATUS_NO_MEMORY;
854 n[i] = NULL;
855 n[i + 1] = NULL;
856 b->options = n;
859 tmp = discard_const_p(char, opt);
861 if (value == NULL) {
862 for (;b->options[i];i++) {
863 b->options[i] = b->options[i+1];
865 talloc_free(tmp);
866 return NT_STATUS_OK;
869 b->options[i] = talloc_asprintf(b->options, "%s=%s",
870 name, value);
871 if (b->options[i] == NULL) {
872 b->options[i] = tmp;
873 return NT_STATUS_NO_MEMORY;
876 return NT_STATUS_OK;
879 _PUBLIC_ uint32_t dcerpc_binding_get_flags(const struct dcerpc_binding *b)
881 return b->flags;
884 _PUBLIC_ NTSTATUS dcerpc_binding_set_flags(struct dcerpc_binding *b,
885 uint32_t additional,
886 uint32_t clear)
889 * TODO: in future we may want to reject invalid combinations
891 b->flags &= ~clear;
892 b->flags |= additional;
894 return NT_STATUS_OK;
897 _PUBLIC_ NTSTATUS dcerpc_floor_get_uuid_full(const struct epm_floor *epm_floor,
898 struct ndr_syntax_id *syntax)
900 TALLOC_CTX *mem_ctx = talloc_init("floor_get_lhs_data");
901 struct ndr_pull *ndr;
902 enum ndr_err_code ndr_err;
903 uint16_t if_version=0;
905 *syntax = (struct ndr_syntax_id) { .if_version = 0, };
907 if (epm_floor->lhs.protocol != EPM_PROTOCOL_UUID) {
908 talloc_free(mem_ctx);
909 return NT_STATUS_INVALID_PARAMETER;
912 ndr = ndr_pull_init_blob(&epm_floor->lhs.lhs_data, mem_ctx);
913 if (ndr == NULL) {
914 talloc_free(mem_ctx);
915 return NT_STATUS_NO_MEMORY;
917 ndr->flags |= LIBNDR_FLAG_NOALIGN;
919 ndr_err = ndr_pull_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
920 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
921 talloc_free(mem_ctx);
922 return ndr_map_error2ntstatus(ndr_err);
925 ndr_err = ndr_pull_uint16(ndr, NDR_SCALARS, &if_version);
926 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
927 talloc_free(mem_ctx);
928 return ndr_map_error2ntstatus(ndr_err);
931 syntax->if_version = if_version;
933 TALLOC_FREE(ndr);
935 ndr = ndr_pull_init_blob(&epm_floor->rhs.uuid.unknown, mem_ctx);
936 if (ndr == NULL) {
937 talloc_free(mem_ctx);
938 return NT_STATUS_NO_MEMORY;
940 ndr->flags |= LIBNDR_FLAG_NOALIGN;
942 ndr_err = ndr_pull_uint16(ndr, NDR_SCALARS, &if_version);
943 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
944 talloc_free(mem_ctx);
945 return ndr_map_error2ntstatus(ndr_err);
948 syntax->if_version |= (((uint32_t)if_version) << 16) & 0xffff0000;
950 talloc_free(mem_ctx);
952 return NT_STATUS_OK;
955 static DATA_BLOB dcerpc_floor_pack_lhs_data(TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax)
957 DATA_BLOB blob;
958 enum ndr_err_code ndr_err;
959 struct ndr_push *ndr;
961 ndr = ndr_push_init_ctx(mem_ctx);
962 if (ndr == NULL) {
963 return data_blob_null;
966 ndr->flags |= LIBNDR_FLAG_NOALIGN;
968 ndr_err = ndr_push_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
969 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
970 return data_blob_null;
972 ndr_err = ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version);
973 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
974 return data_blob_null;
977 blob = ndr_push_blob(ndr);
978 talloc_steal(mem_ctx, blob.data);
979 talloc_free(ndr);
980 return blob;
983 static bool dcerpc_floor_pack_rhs_if_version_data(
984 TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax,
985 DATA_BLOB *pblob)
987 DATA_BLOB blob;
988 struct ndr_push *ndr = ndr_push_init_ctx(mem_ctx);
989 enum ndr_err_code ndr_err;
991 if (ndr == NULL) {
992 return false;
995 ndr->flags |= LIBNDR_FLAG_NOALIGN;
997 ndr_err = ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version >> 16);
998 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
999 return false;
1002 blob = ndr_push_blob(ndr);
1003 talloc_steal(mem_ctx, blob.data);
1004 talloc_free(ndr);
1005 *pblob = blob;
1006 return true;
1009 static NTSTATUS dcerpc_floor_pack_uuid_full(TALLOC_CTX *mem_ctx,
1010 struct epm_floor *floor,
1011 const struct ndr_syntax_id *syntax)
1013 bool ok;
1015 floor->lhs.protocol = EPM_PROTOCOL_UUID;
1017 floor->lhs.lhs_data = dcerpc_floor_pack_lhs_data(mem_ctx, syntax);
1018 if (floor->lhs.lhs_data.data == NULL) {
1019 return NT_STATUS_NO_MEMORY;
1022 ok = dcerpc_floor_pack_rhs_if_version_data(mem_ctx, syntax,
1023 &floor->rhs.uuid.unknown);
1024 if (!ok) {
1025 data_blob_free(&floor->lhs.lhs_data);
1026 return NT_STATUS_NO_MEMORY;
1029 return NT_STATUS_OK;
1032 char *dcerpc_floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
1034 switch (epm_floor->lhs.protocol) {
1035 case EPM_PROTOCOL_TCP:
1036 if (epm_floor->rhs.tcp.port == 0) return NULL;
1037 return talloc_asprintf(mem_ctx, "%"PRIu16, epm_floor->rhs.tcp.port);
1039 case EPM_PROTOCOL_UDP:
1040 if (epm_floor->rhs.udp.port == 0) return NULL;
1041 return talloc_asprintf(mem_ctx, "%"PRIu16, epm_floor->rhs.udp.port);
1043 case EPM_PROTOCOL_HTTP:
1044 if (epm_floor->rhs.http.port == 0) return NULL;
1045 return talloc_asprintf(mem_ctx, "%"PRIu16, epm_floor->rhs.http.port);
1047 case EPM_PROTOCOL_IP:
1048 return talloc_strdup(mem_ctx, epm_floor->rhs.ip.ipaddr);
1050 case EPM_PROTOCOL_NCACN:
1051 return NULL;
1053 case EPM_PROTOCOL_NCADG:
1054 return NULL;
1056 case EPM_PROTOCOL_SMB:
1057 if (strlen(epm_floor->rhs.smb.unc) == 0) return NULL;
1058 return talloc_strdup(mem_ctx, epm_floor->rhs.smb.unc);
1060 case EPM_PROTOCOL_NAMED_PIPE:
1061 if (strlen(epm_floor->rhs.named_pipe.path) == 0) return NULL;
1062 return talloc_strdup(mem_ctx, epm_floor->rhs.named_pipe.path);
1064 case EPM_PROTOCOL_NETBIOS:
1065 if (strlen(epm_floor->rhs.netbios.name) == 0) return NULL;
1066 return talloc_strdup(mem_ctx, epm_floor->rhs.netbios.name);
1068 case EPM_PROTOCOL_NCALRPC:
1069 return NULL;
1071 case EPM_PROTOCOL_VINES_SPP:
1072 return talloc_asprintf(mem_ctx, "%"PRIu16, epm_floor->rhs.vines_spp.port);
1074 case EPM_PROTOCOL_VINES_IPC:
1075 return talloc_asprintf(mem_ctx, "%"PRIu16, epm_floor->rhs.vines_ipc.port);
1077 case EPM_PROTOCOL_STREETTALK:
1078 return talloc_strdup(mem_ctx, epm_floor->rhs.streettalk.streettalk);
1080 case EPM_PROTOCOL_UNIX_DS:
1081 if (strlen(epm_floor->rhs.unix_ds.path) == 0) return NULL;
1082 return talloc_strdup(mem_ctx, epm_floor->rhs.unix_ds.path);
1084 case EPM_PROTOCOL_NULL:
1085 return NULL;
1087 default:
1088 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
1089 break;
1092 return NULL;
1095 static NTSTATUS dcerpc_floor_set_rhs_data(TALLOC_CTX *mem_ctx,
1096 struct epm_floor *epm_floor,
1097 const char *data)
1099 if (data == NULL) {
1100 data = "";
1103 switch (epm_floor->lhs.protocol) {
1104 case EPM_PROTOCOL_TCP:
1105 epm_floor->rhs.tcp.port = atoi(data);
1106 return NT_STATUS_OK;
1108 case EPM_PROTOCOL_UDP:
1109 epm_floor->rhs.udp.port = atoi(data);
1110 return NT_STATUS_OK;
1112 case EPM_PROTOCOL_HTTP:
1113 epm_floor->rhs.http.port = atoi(data);
1114 return NT_STATUS_OK;
1116 case EPM_PROTOCOL_IP:
1117 if (!is_ipaddress_v4(data)) {
1118 data = "0.0.0.0";
1120 epm_floor->rhs.ip.ipaddr = talloc_strdup(mem_ctx, data);
1121 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.ip.ipaddr);
1122 return NT_STATUS_OK;
1124 case EPM_PROTOCOL_NCACN:
1125 epm_floor->rhs.ncacn.minor_version = 0;
1126 return NT_STATUS_OK;
1128 case EPM_PROTOCOL_NCADG:
1129 epm_floor->rhs.ncadg.minor_version = 0;
1130 return NT_STATUS_OK;
1132 case EPM_PROTOCOL_SMB:
1133 epm_floor->rhs.smb.unc = talloc_strdup(mem_ctx, data);
1134 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.smb.unc);
1135 return NT_STATUS_OK;
1137 case EPM_PROTOCOL_NAMED_PIPE:
1138 epm_floor->rhs.named_pipe.path = talloc_strdup(mem_ctx, data);
1139 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.named_pipe.path);
1140 return NT_STATUS_OK;
1142 case EPM_PROTOCOL_NETBIOS:
1143 epm_floor->rhs.netbios.name = talloc_strdup(mem_ctx, data);
1144 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.netbios.name);
1145 return NT_STATUS_OK;
1147 case EPM_PROTOCOL_NCALRPC:
1148 return NT_STATUS_OK;
1150 case EPM_PROTOCOL_VINES_SPP:
1151 epm_floor->rhs.vines_spp.port = atoi(data);
1152 return NT_STATUS_OK;
1154 case EPM_PROTOCOL_VINES_IPC:
1155 epm_floor->rhs.vines_ipc.port = atoi(data);
1156 return NT_STATUS_OK;
1158 case EPM_PROTOCOL_STREETTALK:
1159 epm_floor->rhs.streettalk.streettalk = talloc_strdup(mem_ctx, data);
1160 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.streettalk.streettalk);
1161 return NT_STATUS_OK;
1163 case EPM_PROTOCOL_UNIX_DS:
1164 epm_floor->rhs.unix_ds.path = talloc_strdup(mem_ctx, data);
1165 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.unix_ds.path);
1166 return NT_STATUS_OK;
1168 case EPM_PROTOCOL_NULL:
1169 return NT_STATUS_OK;
1171 default:
1172 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
1173 break;
1176 return NT_STATUS_NOT_SUPPORTED;
1179 enum dcerpc_transport_t dcerpc_transport_by_endpoint_protocol(int prot)
1181 size_t i;
1183 /* Find a transport that has 'prot' as 4th protocol */
1184 for (i=0;i<ARRAY_SIZE(transports);i++) {
1185 if (transports[i].num_protocols >= 2 &&
1186 transports[i].protseq[1] == prot) {
1187 return transports[i].transport;
1191 /* Unknown transport */
1192 return (unsigned int)-1;
1195 _PUBLIC_ enum dcerpc_transport_t dcerpc_transport_by_tower(const struct epm_tower *tower)
1197 size_t i;
1199 /* Find a transport that matches this tower */
1200 for (i=0;i<ARRAY_SIZE(transports);i++) {
1201 int j;
1202 if (transports[i].num_protocols != tower->num_floors - 2) {
1203 continue;
1206 for (j = 0; j < transports[i].num_protocols && j < MAX_PROTSEQ; j++) {
1207 if (transports[i].protseq[j] != tower->floors[j+2].lhs.protocol) {
1208 break;
1212 if (j == transports[i].num_protocols) {
1213 return transports[i].transport;
1217 /* Unknown transport */
1218 return (unsigned int)-1;
1221 _PUBLIC_ const char *derpc_transport_string_by_transport(enum dcerpc_transport_t t)
1223 size_t i;
1225 for (i=0; i<ARRAY_SIZE(transports); i++) {
1226 if (t == transports[i].transport) {
1227 return transports[i].name;
1230 return NULL;
1233 _PUBLIC_ enum dcerpc_transport_t dcerpc_transport_by_name(const char *name)
1235 size_t i;
1237 if (name == NULL) {
1238 return NCA_UNKNOWN;
1241 for (i=0; i<ARRAY_SIZE(transports);i++) {
1242 if (strcasecmp(name, transports[i].name) == 0) {
1243 return transports[i].transport;
1247 return NCA_UNKNOWN;
1250 _PUBLIC_ NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx,
1251 struct epm_tower *tower,
1252 struct dcerpc_binding **b_out)
1254 NTSTATUS status;
1255 struct dcerpc_binding *b;
1256 enum dcerpc_transport_t transport;
1257 struct ndr_syntax_id abstract_syntax;
1258 char *endpoint = NULL;
1259 char *host = NULL;
1262 * A tower needs to have at least 4 floors to carry useful
1263 * information. Floor 3 is the transport identifier which defines
1264 * how many floors are required at least.
1266 if (tower->num_floors < 4) {
1267 return NT_STATUS_INVALID_PARAMETER;
1270 status = dcerpc_parse_binding(mem_ctx, "", &b);
1271 if (!NT_STATUS_IS_OK(status)) {
1272 return status;
1275 transport = dcerpc_transport_by_tower(tower);
1276 if (transport == NCA_UNKNOWN) {
1277 talloc_free(b);
1278 return NT_STATUS_NOT_SUPPORTED;
1281 status = dcerpc_binding_set_transport(b, transport);
1282 if (!NT_STATUS_IS_OK(status)) {
1283 talloc_free(b);
1284 return status;
1287 /* Set abstract syntax */
1288 status = dcerpc_floor_get_uuid_full(&tower->floors[0], &abstract_syntax);
1289 if (!NT_STATUS_IS_OK(status)) {
1290 talloc_free(b);
1291 return status;
1294 status = dcerpc_binding_set_abstract_syntax(b, &abstract_syntax);
1295 if (!NT_STATUS_IS_OK(status)) {
1296 talloc_free(b);
1297 return status;
1300 /* Ignore floor 1, it contains the NDR version info */
1302 /* Set endpoint */
1303 errno = 0;
1304 if (tower->num_floors >= 4) {
1305 endpoint = dcerpc_floor_get_rhs_data(b, &tower->floors[3]);
1307 if (errno != 0) {
1308 int saved_errno = errno;
1309 talloc_free(b);
1310 return map_nt_error_from_unix_common(saved_errno);
1313 status = dcerpc_binding_set_string_option(b, "endpoint", endpoint);
1314 if (!NT_STATUS_IS_OK(status)) {
1315 talloc_free(b);
1316 return status;
1318 TALLOC_FREE(endpoint);
1320 /* Set network address */
1321 errno = 0;
1322 if (tower->num_floors >= 5) {
1323 host = dcerpc_floor_get_rhs_data(b, &tower->floors[4]);
1325 if (errno != 0) {
1326 int saved_errno = errno;
1327 talloc_free(b);
1328 return map_nt_error_from_unix_common(saved_errno);
1331 status = dcerpc_binding_set_string_option(b, "host", host);
1332 if (!NT_STATUS_IS_OK(status)) {
1333 talloc_free(b);
1334 return status;
1336 status = dcerpc_binding_set_string_option(b, "target_hostname", host);
1337 if (!NT_STATUS_IS_OK(status)) {
1338 talloc_free(b);
1339 return status;
1341 TALLOC_FREE(host);
1343 *b_out = b;
1344 return NT_STATUS_OK;
1347 _PUBLIC_ struct dcerpc_binding *dcerpc_binding_dup(TALLOC_CTX *mem_ctx,
1348 const struct dcerpc_binding *b)
1350 struct dcerpc_binding *n;
1351 uint32_t count;
1353 n = talloc_zero(mem_ctx, struct dcerpc_binding);
1354 if (n == NULL) {
1355 return NULL;
1358 n->transport = b->transport;
1359 n->object = b->object;
1360 n->flags = b->flags;
1361 n->assoc_group_id = b->assoc_group_id;
1363 if (b->object_string != NULL) {
1364 n->object_string = talloc_strdup(n, b->object_string);
1365 if (n->object_string == NULL) {
1366 goto nomem;
1369 if (b->host != NULL) {
1370 n->host = talloc_strdup(n, b->host);
1371 if (n->host == NULL) {
1372 goto nomem;
1376 if (b->target_hostname != NULL) {
1377 n->target_hostname = talloc_strdup(n, b->target_hostname);
1378 if (n->target_hostname == NULL) {
1379 goto nomem;
1383 if (b->target_principal != NULL) {
1384 n->target_principal = talloc_strdup(n, b->target_principal);
1385 if (n->target_principal == NULL) {
1386 goto nomem;
1390 if (b->endpoint != NULL) {
1391 n->endpoint = talloc_strdup(n, b->endpoint);
1392 if (n->endpoint == NULL) {
1393 goto nomem;
1397 for (count = 0; b->options && b->options[count]; count++);
1399 if (count > 0) {
1400 uint32_t i;
1402 n->options = talloc_array(n, const char *, count + 1);
1403 if (n->options == NULL) {
1404 goto nomem;
1407 for (i = 0; i < count; i++) {
1408 n->options[i] = talloc_strdup(n->options, b->options[i]);
1409 if (n->options[i] == NULL) {
1410 goto nomem;
1413 n->options[count] = NULL;
1416 return n;
1417 nomem:
1418 TALLOC_FREE(n);
1419 return NULL;
1422 _PUBLIC_ NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx,
1423 const struct dcerpc_binding *binding,
1424 struct epm_tower *tower)
1426 const enum epm_protocol *protseq = NULL;
1427 size_t i, num_protocols = 0;
1428 struct ndr_syntax_id abstract_syntax;
1429 NTSTATUS status;
1431 /* Find transport */
1432 for (i=0;i<ARRAY_SIZE(transports);i++) {
1433 if (transports[i].transport == binding->transport) {
1434 protseq = transports[i].protseq;
1435 num_protocols = transports[i].num_protocols;
1436 break;
1440 if (i == ARRAY_SIZE(transports)) {
1441 DEBUG(0, ("Unable to find transport with id '%d'\n", binding->transport));
1442 return NT_STATUS_UNSUCCESSFUL;
1445 tower->num_floors = 2 + num_protocols;
1446 tower->floors = talloc_array(mem_ctx, struct epm_floor, tower->num_floors);
1447 if (tower->floors == NULL) {
1448 return NT_STATUS_NO_MEMORY;
1451 /* Floor 0 */
1452 abstract_syntax = dcerpc_binding_get_abstract_syntax(binding);
1453 status = dcerpc_floor_pack_uuid_full(tower->floors,
1454 &tower->floors[0],
1455 &abstract_syntax);
1456 if (!NT_STATUS_IS_OK(status)) {
1457 return status;
1460 /* Floor 1 */
1461 status = dcerpc_floor_pack_uuid_full(tower->floors,
1462 &tower->floors[1],
1463 &ndr_transfer_syntax_ndr);
1464 if (!NT_STATUS_IS_OK(status)) {
1465 return status;
1468 /* Floor 2 to num_protocols */
1469 for (i = 0; i < num_protocols; i++) {
1470 tower->floors[2 + i].lhs.protocol = protseq[i];
1471 tower->floors[2 + i].lhs.lhs_data = data_blob_null;
1472 ZERO_STRUCT(tower->floors[2 + i].rhs);
1473 status = dcerpc_floor_set_rhs_data(tower->floors,
1474 &tower->floors[2 + i],
1475 NULL);
1476 if (!NT_STATUS_IS_OK(status)) {
1477 return status;
1481 /* The 4th floor contains the endpoint */
1482 if (num_protocols >= 2 && binding->endpoint) {
1483 status = dcerpc_floor_set_rhs_data(tower->floors,
1484 &tower->floors[3],
1485 binding->endpoint);
1486 if (!NT_STATUS_IS_OK(status)) {
1487 return status;
1491 /* The 5th contains the network address */
1492 if (num_protocols >= 3 && binding->host) {
1493 status = dcerpc_floor_set_rhs_data(tower->floors,
1494 &tower->floors[4],
1495 binding->host);
1496 if (!NT_STATUS_IS_OK(status)) {
1497 return status;
1501 return NT_STATUS_OK;