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/>.
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"
36 #define MAX_PROTSEQ 10
38 struct dcerpc_binding {
39 enum dcerpc_transport_t transport;
40 struct ndr_syntax_id object;
41 const char *object_string;
43 const char *target_hostname;
44 const char *target_principal;
48 uint32_t assoc_group_id;
49 char assoc_group_string[11]; /* 0x3456789a + '\0' */
54 enum dcerpc_transport_t transport;
56 enum epm_protocol protseq[MAX_PROTSEQ];
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 {
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},
102 {"validate", DCERPC_DEBUG_VALIDATE_BOTH},
103 {"print", DCERPC_DEBUG_PRINT_BOTH},
104 {"padcheck", DCERPC_DEBUG_PAD_CHECK},
105 {"bigendian", DCERPC_PUSH_BIGENDIAN},
106 {"smb2", DCERPC_SMB2},
107 {"ndr64", DCERPC_NDR64},
110 static const struct ncacn_option *ncacn_option_by_name(const char *name)
114 for (i=0; i<ARRAY_SIZE(ncacn_options); i++) {
117 ret = strcasecmp(ncacn_options[i].name, name);
122 return &ncacn_options[i];
128 const char *epm_floor_string(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
130 struct ndr_syntax_id syntax;
133 switch(epm_floor->lhs.protocol) {
134 case EPM_PROTOCOL_UUID:
135 status = dcerpc_floor_get_lhs_data(epm_floor, &syntax);
136 if (NT_STATUS_IS_OK(status)) {
137 /* lhs is used: UUID */
140 if (GUID_equal(&syntax.uuid, &ndr_transfer_syntax_ndr.uuid)) {
144 if (GUID_equal(&syntax.uuid, &ndr_transfer_syntax_ndr64.uuid)) {
148 uuidstr = GUID_string(mem_ctx, &syntax.uuid);
150 return talloc_asprintf(mem_ctx, " uuid %s/0x%02x", uuidstr, syntax.if_version);
152 return talloc_asprintf(mem_ctx, "IPX:%s",
153 data_blob_hex_string_upper(mem_ctx, &epm_floor->rhs.uuid.unknown));
156 case EPM_PROTOCOL_NCACN:
159 case EPM_PROTOCOL_NCADG:
162 case EPM_PROTOCOL_NCALRPC:
165 case EPM_PROTOCOL_DNET_NSP:
168 case EPM_PROTOCOL_IP:
169 return talloc_asprintf(mem_ctx, "IP:%s", epm_floor->rhs.ip.ipaddr);
171 case EPM_PROTOCOL_NAMED_PIPE:
172 return talloc_asprintf(mem_ctx, "NAMED-PIPE:%s", epm_floor->rhs.named_pipe.path);
174 case EPM_PROTOCOL_SMB:
175 return talloc_asprintf(mem_ctx, "SMB:%s", epm_floor->rhs.smb.unc);
177 case EPM_PROTOCOL_UNIX_DS:
178 return talloc_asprintf(mem_ctx, "Unix:%s", epm_floor->rhs.unix_ds.path);
180 case EPM_PROTOCOL_NETBIOS:
181 return talloc_asprintf(mem_ctx, "NetBIOS:%s", epm_floor->rhs.netbios.name);
183 case EPM_PROTOCOL_NETBEUI:
186 case EPM_PROTOCOL_SPX:
189 case EPM_PROTOCOL_NB_IPX:
192 case EPM_PROTOCOL_HTTP:
193 return talloc_asprintf(mem_ctx, "HTTP:%d", epm_floor->rhs.http.port);
195 case EPM_PROTOCOL_TCP:
196 return talloc_asprintf(mem_ctx, "TCP:%d", epm_floor->rhs.tcp.port);
198 case EPM_PROTOCOL_UDP:
199 return talloc_asprintf(mem_ctx, "UDP:%d", epm_floor->rhs.udp.port);
202 return talloc_asprintf(mem_ctx, "UNK(%02x):", epm_floor->lhs.protocol);
208 form a binding string from a binding structure
210 _PUBLIC_ char *dcerpc_binding_string(TALLOC_CTX *mem_ctx, const struct dcerpc_binding *b)
212 char *s = talloc_strdup(mem_ctx, "");
215 const char *t_name = NULL;
216 bool option_section = false;
217 const char *target_hostname = NULL;
219 if (b->transport != NCA_UNKNOWN) {
220 t_name = derpc_transport_string_by_transport(b->transport);
227 if (!GUID_all_zero(&b->object.uuid)) {
229 s = talloc_asprintf_append_buffer(s, "%s@",
230 GUID_string(mem_ctx, &b->object.uuid));
237 if (t_name != NULL) {
239 s = talloc_asprintf_append_buffer(s, "%s:", t_name);
248 s = talloc_asprintf_append_buffer(s, "%s", b->host);
255 target_hostname = b->target_hostname;
256 if (target_hostname != NULL && b->host != NULL) {
257 if (strcmp(target_hostname, b->host) == 0) {
258 target_hostname = NULL;
263 option_section = true;
264 } else if (target_hostname) {
265 option_section = true;
266 } else if (b->target_principal) {
267 option_section = true;
268 } else if (b->assoc_group_id != 0) {
269 option_section = true;
270 } else if (b->options) {
271 option_section = true;
272 } else if (b->flags) {
273 option_section = true;
276 if (!option_section) {
281 s = talloc_asprintf_append_buffer(s, "[");
289 s = talloc_asprintf_append_buffer(s, "%s", b->endpoint);
296 for (i=0;i<ARRAY_SIZE(ncacn_options);i++) {
297 if (!(b->flags & ncacn_options[i].flag)) {
302 s = talloc_asprintf_append_buffer(s, ",%s", ncacn_options[i].name);
309 if (target_hostname) {
311 s = talloc_asprintf_append_buffer(s, ",target_hostname=%s",
319 if (b->target_principal) {
321 s = talloc_asprintf_append_buffer(s, ",target_principal=%s",
322 b->target_principal);
329 if (b->assoc_group_id != 0) {
331 s = talloc_asprintf_append_buffer(s, ",assoc_group_id=0x%08x",
339 for (i=0;b->options && b->options[i];i++) {
341 s = talloc_asprintf_append_buffer(s, ",%s", b->options[i]);
349 s = talloc_asprintf_append_buffer(s, "]");
359 parse a binding string into a dcerpc_binding structure
361 _PUBLIC_ NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *_s, struct dcerpc_binding **b_out)
364 struct dcerpc_binding *b;
366 char *options = NULL;
371 b = talloc_zero(mem_ctx, struct dcerpc_binding);
373 return NT_STATUS_NO_MEMORY;
376 _t = talloc_strdup(b, _s);
379 return NT_STATUS_NO_MEMORY;
388 if (options[strlen(options)-1] != ']') {
390 return NT_STATUS_INVALID_PARAMETER_MIX;
392 options[strlen(options)-1] = 0;
397 if (p && PTR_DIFF(p, s) == 36) { /* 36 is the length of a UUID */
400 status = dcerpc_binding_set_string_option(b, "object", s);
401 if (!NT_STATUS_IS_OK(status)) {
412 b->transport = NCA_UNKNOWN;
413 } else if (is_ipaddress_v6(s)) {
414 b->transport = NCA_UNKNOWN;
418 status = dcerpc_binding_set_string_option(b, "transport", s);
419 if (!NT_STATUS_IS_OK(status)) {
428 status = dcerpc_binding_set_string_option(b, "host", s);
429 if (!NT_STATUS_IS_OK(status)) {
434 b->target_hostname = talloc_strdup(b, b->host);
435 if (b->target_hostname == NULL) {
437 return NT_STATUS_NO_MEMORY;
441 for (i=0; options != NULL; i++) {
442 const char *name = options;
443 const char *value = NULL;
445 p = strchr(options, ',');
453 p = strchr(name, '=');
461 * If it's not a key=value pair
462 * it might be a ncacn_option
463 * or if it's the first option
466 const struct ncacn_option *no = NULL;
470 no = ncacn_option_by_name(name);
474 * we don't allow unknown options
476 return NT_STATUS_INVALID_PARAMETER_MIX;
480 * This is the endpoint
483 if (strlen(value) == 0) {
489 status = dcerpc_binding_set_string_option(b, name, value);
490 if (!NT_STATUS_IS_OK(status)) {
501 _PUBLIC_ struct GUID dcerpc_binding_get_object(const struct dcerpc_binding *b)
503 return b->object.uuid;
506 _PUBLIC_ NTSTATUS dcerpc_binding_set_object(struct dcerpc_binding *b,
509 char *tmp = discard_const_p(char, b->object_string);
511 if (GUID_all_zero(&object)) {
513 b->object_string = NULL;
514 ZERO_STRUCT(b->object);
518 b->object_string = GUID_string(b, &object);
519 if (b->object_string == NULL) {
520 b->object_string = tmp;
521 return NT_STATUS_NO_MEMORY;
525 ZERO_STRUCT(b->object);
526 b->object.uuid = object;
530 _PUBLIC_ enum dcerpc_transport_t dcerpc_binding_get_transport(const struct dcerpc_binding *b)
535 _PUBLIC_ NTSTATUS dcerpc_binding_set_transport(struct dcerpc_binding *b,
536 enum dcerpc_transport_t transport)
541 * TODO: we may want to check the transport value is
544 if (b->transport == transport) {
549 * This implicitly resets the endpoint
550 * as the endpoint is transport specific.
552 * It also resets the assoc group as it's
553 * also endpoint specific.
555 * TODO: in future we may reset more options
558 status = dcerpc_binding_set_string_option(b, "endpoint", NULL);
559 if (!NT_STATUS_IS_OK(status)) {
563 b->assoc_group_id = 0;
565 b->transport = transport;
569 _PUBLIC_ void dcerpc_binding_get_auth_info(const struct dcerpc_binding *b,
570 enum dcerpc_AuthType *_auth_type,
571 enum dcerpc_AuthLevel *_auth_level)
573 enum dcerpc_AuthType auth_type;
574 enum dcerpc_AuthLevel auth_level;
576 if (b->flags & DCERPC_AUTH_SPNEGO) {
577 auth_type = DCERPC_AUTH_TYPE_SPNEGO;
578 } else if (b->flags & DCERPC_AUTH_KRB5) {
579 auth_type = DCERPC_AUTH_TYPE_KRB5;
580 } else if (b->flags & DCERPC_SCHANNEL) {
581 auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
582 } else if (b->flags & DCERPC_AUTH_NTLM) {
583 auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
585 auth_type = DCERPC_AUTH_TYPE_NONE;
588 if (b->flags & DCERPC_SEAL) {
589 auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
590 } else if (b->flags & DCERPC_SIGN) {
591 auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
592 } else if (b->flags & DCERPC_CONNECT) {
593 auth_level = DCERPC_AUTH_LEVEL_CONNECT;
594 } else if (auth_type != DCERPC_AUTH_TYPE_NONE) {
595 auth_level = DCERPC_AUTH_LEVEL_CONNECT;
597 auth_level = DCERPC_AUTH_LEVEL_NONE;
600 if (_auth_type == NULL) {
601 *_auth_type = auth_type;
604 if (_auth_level == NULL) {
605 *_auth_level = auth_level;
609 _PUBLIC_ uint32_t dcerpc_binding_get_assoc_group_id(const struct dcerpc_binding *b)
611 return b->assoc_group_id;
614 _PUBLIC_ NTSTATUS dcerpc_binding_set_assoc_group_id(struct dcerpc_binding *b,
615 uint32_t assoc_group_id)
617 b->assoc_group_id = assoc_group_id;
621 _PUBLIC_ struct ndr_syntax_id dcerpc_binding_get_abstract_syntax(const struct dcerpc_binding *b)
624 * For now we just use object, until all callers are fixed.
629 _PUBLIC_ NTSTATUS dcerpc_binding_set_abstract_syntax(struct dcerpc_binding *b,
630 const struct ndr_syntax_id *syntax)
636 * For now we just use object, until all callers are fixed.
639 if (syntax != NULL) {
640 object = syntax->uuid;
642 object = GUID_zero();
646 * This sets also the string
648 status = dcerpc_binding_set_object(b, object);
649 if (!NT_STATUS_IS_OK(status)) {
653 if (syntax != NULL) {
655 * Here we need to reset the whole ndr_syntax_id
656 * structure including the .if_version
664 _PUBLIC_ const char *dcerpc_binding_get_string_option(const struct dcerpc_binding *b,
670 #define _SPECIAL(x) { .name = #x, .value = b->x, }
672 { .name = "object", .value = b->object_string, },
675 _SPECIAL(target_hostname),
676 _SPECIAL(target_principal),
679 const struct ncacn_option *no = NULL;
680 size_t name_len = strlen(name);
684 ret = strcmp(name, "transport");
686 return derpc_transport_string_by_transport(b->transport);
689 ret = strcmp(name, "assoc_group_id");
691 char *tmp = discard_const_p(char, b->assoc_group_string);
693 if (b->assoc_group_id == 0) {
697 snprintf(tmp, sizeof(b->assoc_group_string),
698 "0x%08x", b->assoc_group_id);
699 return (const char *)b->assoc_group_string;
702 for (i=0; i < ARRAY_SIZE(specials); i++) {
703 ret = strcmp(specials[i].name, name);
708 return specials[i].value;
711 no = ncacn_option_by_name(name);
713 if (b->flags & no->flag) {
720 if (b->options == NULL) {
724 for (i=0; b->options[i]; i++) {
725 const char *o = b->options[i];
726 const char *vs = NULL;
728 ret = strncmp(name, o, name_len);
733 if (o[name_len] != '=') {
737 vs = &o[name_len + 1];
745 _PUBLIC_ char *dcerpc_binding_copy_string_option(TALLOC_CTX *mem_ctx,
746 const struct dcerpc_binding *b,
749 const char *c = dcerpc_binding_get_string_option(b, name);
757 v = talloc_strdup(mem_ctx, c);
766 _PUBLIC_ NTSTATUS dcerpc_binding_set_string_option(struct dcerpc_binding *b,
773 #define _SPECIAL(x) { .name = #x, .ptr = &b->x, }
777 _SPECIAL(target_hostname),
778 _SPECIAL(target_principal),
781 const struct ncacn_option *no = NULL;
782 size_t name_len = strlen(name);
783 const char *opt = NULL;
789 * Note: value == NULL, means delete it.
790 * value != NULL means add or reset.
793 ret = strcmp(name, "transport");
795 enum dcerpc_transport_t t = dcerpc_transport_by_name(value);
797 if (t == NCA_UNKNOWN && value != NULL) {
798 return NT_STATUS_INVALID_PARAMETER_MIX;
801 return dcerpc_binding_set_transport(b, t);
804 ret = strcmp(name, "object");
807 struct GUID uuid = GUID_zero();
811 blob = data_blob_string_const(value);
812 if (blob.length != 36) {
813 return NT_STATUS_INVALID_PARAMETER_MIX;
816 status = GUID_from_data_blob(&blob, &uuid);
817 if (!NT_STATUS_IS_OK(status)) {
822 return dcerpc_binding_set_object(b, uuid);
825 ret = strcmp(name, "assoc_group_id");
827 uint32_t assoc_group_id = 0;
832 ret = sscanf(value, "0x%08x%c", &assoc_group_id, &c);
834 return NT_STATUS_INVALID_PARAMETER_MIX;
838 return dcerpc_binding_set_assoc_group_id(b, assoc_group_id);
841 for (i=0; i < ARRAY_SIZE(specials); i++) {
842 ret = strcmp(specials[i].name, name);
847 tmp = discard_const_p(char, *specials[i].ptr);
851 *specials[i].ptr = NULL;
855 if (value[0] == '\0') {
856 return NT_STATUS_INVALID_PARAMETER_MIX;
859 *specials[i].ptr = talloc_strdup(b, value);
860 if (*specials[i].ptr == NULL) {
861 *specials[i].ptr = tmp;
862 return NT_STATUS_NO_MEMORY;
869 no = ncacn_option_by_name(name);
872 b->flags &= ~no->flag;
876 ret = strcasecmp(no->name, value);
878 return NT_STATUS_INVALID_PARAMETER_MIX;
881 b->flags |= no->flag;
885 for (i=0; b->options && b->options[i]; i++) {
886 const char *o = b->options[i];
888 ret = strncmp(name, o, name_len);
893 if (o[name_len] != '=') {
908 n = talloc_realloc(b, b->options, const char *, i + 2);
910 return NT_STATUS_NO_MEMORY;
917 tmp = discard_const_p(char, opt);
920 for (;b->options[i];i++) {
921 b->options[i] = b->options[i+1];
927 b->options[i] = talloc_asprintf(b->options, "%s=%s",
929 if (b->options[i] == NULL) {
931 return NT_STATUS_NO_MEMORY;
937 _PUBLIC_ uint32_t dcerpc_binding_get_flags(const struct dcerpc_binding *b)
942 _PUBLIC_ NTSTATUS dcerpc_binding_set_flags(struct dcerpc_binding *b,
947 * TODO: in future we may want to reject invalid combinations
950 b->flags |= additional;
955 _PUBLIC_ NTSTATUS dcerpc_floor_get_lhs_data(const struct epm_floor *epm_floor,
956 struct ndr_syntax_id *syntax)
958 TALLOC_CTX *mem_ctx = talloc_init("floor_get_lhs_data");
959 struct ndr_pull *ndr;
960 enum ndr_err_code ndr_err;
961 uint16_t if_version=0;
963 ndr = ndr_pull_init_blob(&epm_floor->lhs.lhs_data, mem_ctx);
965 talloc_free(mem_ctx);
966 return NT_STATUS_NO_MEMORY;
968 ndr->flags |= LIBNDR_FLAG_NOALIGN;
970 ndr_err = ndr_pull_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
971 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
972 talloc_free(mem_ctx);
973 return ndr_map_error2ntstatus(ndr_err);
976 ndr_err = ndr_pull_uint16(ndr, NDR_SCALARS, &if_version);
977 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
978 talloc_free(mem_ctx);
979 return ndr_map_error2ntstatus(ndr_err);
982 syntax->if_version = if_version;
984 talloc_free(mem_ctx);
989 static DATA_BLOB dcerpc_floor_pack_lhs_data(TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax)
992 enum ndr_err_code ndr_err;
993 struct ndr_push *ndr;
995 ndr = ndr_push_init_ctx(mem_ctx);
997 return data_blob_null;
1000 ndr->flags |= LIBNDR_FLAG_NOALIGN;
1002 ndr_err = ndr_push_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
1003 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1004 return data_blob_null;
1006 ndr_err = ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version);
1007 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1008 return data_blob_null;
1011 blob = ndr_push_blob(ndr);
1012 talloc_steal(mem_ctx, blob.data);
1017 static bool dcerpc_floor_pack_rhs_if_version_data(
1018 TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax,
1022 struct ndr_push *ndr = ndr_push_init_ctx(mem_ctx);
1023 enum ndr_err_code ndr_err;
1029 ndr->flags |= LIBNDR_FLAG_NOALIGN;
1031 ndr_err = ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version >> 16);
1032 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1036 blob = ndr_push_blob(ndr);
1037 talloc_steal(mem_ctx, blob.data);
1043 char *dcerpc_floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
1045 switch (epm_floor->lhs.protocol) {
1046 case EPM_PROTOCOL_TCP:
1047 if (epm_floor->rhs.tcp.port == 0) return NULL;
1048 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.tcp.port);
1050 case EPM_PROTOCOL_UDP:
1051 if (epm_floor->rhs.udp.port == 0) return NULL;
1052 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.udp.port);
1054 case EPM_PROTOCOL_HTTP:
1055 if (epm_floor->rhs.http.port == 0) return NULL;
1056 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.http.port);
1058 case EPM_PROTOCOL_IP:
1059 return talloc_strdup(mem_ctx, epm_floor->rhs.ip.ipaddr);
1061 case EPM_PROTOCOL_NCACN:
1064 case EPM_PROTOCOL_NCADG:
1067 case EPM_PROTOCOL_SMB:
1068 if (strlen(epm_floor->rhs.smb.unc) == 0) return NULL;
1069 return talloc_strdup(mem_ctx, epm_floor->rhs.smb.unc);
1071 case EPM_PROTOCOL_NAMED_PIPE:
1072 if (strlen(epm_floor->rhs.named_pipe.path) == 0) return NULL;
1073 return talloc_strdup(mem_ctx, epm_floor->rhs.named_pipe.path);
1075 case EPM_PROTOCOL_NETBIOS:
1076 if (strlen(epm_floor->rhs.netbios.name) == 0) return NULL;
1077 return talloc_strdup(mem_ctx, epm_floor->rhs.netbios.name);
1079 case EPM_PROTOCOL_NCALRPC:
1082 case EPM_PROTOCOL_VINES_SPP:
1083 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_spp.port);
1085 case EPM_PROTOCOL_VINES_IPC:
1086 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_ipc.port);
1088 case EPM_PROTOCOL_STREETTALK:
1089 return talloc_strdup(mem_ctx, epm_floor->rhs.streettalk.streettalk);
1091 case EPM_PROTOCOL_UNIX_DS:
1092 if (strlen(epm_floor->rhs.unix_ds.path) == 0) return NULL;
1093 return talloc_strdup(mem_ctx, epm_floor->rhs.unix_ds.path);
1095 case EPM_PROTOCOL_NULL:
1099 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
1106 static NTSTATUS dcerpc_floor_set_rhs_data(TALLOC_CTX *mem_ctx,
1107 struct epm_floor *epm_floor,
1114 switch (epm_floor->lhs.protocol) {
1115 case EPM_PROTOCOL_TCP:
1116 epm_floor->rhs.tcp.port = atoi(data);
1117 return NT_STATUS_OK;
1119 case EPM_PROTOCOL_UDP:
1120 epm_floor->rhs.udp.port = atoi(data);
1121 return NT_STATUS_OK;
1123 case EPM_PROTOCOL_HTTP:
1124 epm_floor->rhs.http.port = atoi(data);
1125 return NT_STATUS_OK;
1127 case EPM_PROTOCOL_IP:
1128 if (!is_ipaddress_v4(data)) {
1131 epm_floor->rhs.ip.ipaddr = talloc_strdup(mem_ctx, data);
1132 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.ip.ipaddr);
1133 return NT_STATUS_OK;
1135 case EPM_PROTOCOL_NCACN:
1136 epm_floor->rhs.ncacn.minor_version = 0;
1137 return NT_STATUS_OK;
1139 case EPM_PROTOCOL_NCADG:
1140 epm_floor->rhs.ncadg.minor_version = 0;
1141 return NT_STATUS_OK;
1143 case EPM_PROTOCOL_SMB:
1144 epm_floor->rhs.smb.unc = talloc_strdup(mem_ctx, data);
1145 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.smb.unc);
1146 return NT_STATUS_OK;
1148 case EPM_PROTOCOL_NAMED_PIPE:
1149 epm_floor->rhs.named_pipe.path = talloc_strdup(mem_ctx, data);
1150 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.named_pipe.path);
1151 return NT_STATUS_OK;
1153 case EPM_PROTOCOL_NETBIOS:
1154 epm_floor->rhs.netbios.name = talloc_strdup(mem_ctx, data);
1155 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.netbios.name);
1156 return NT_STATUS_OK;
1158 case EPM_PROTOCOL_NCALRPC:
1159 return NT_STATUS_OK;
1161 case EPM_PROTOCOL_VINES_SPP:
1162 epm_floor->rhs.vines_spp.port = atoi(data);
1163 return NT_STATUS_OK;
1165 case EPM_PROTOCOL_VINES_IPC:
1166 epm_floor->rhs.vines_ipc.port = atoi(data);
1167 return NT_STATUS_OK;
1169 case EPM_PROTOCOL_STREETTALK:
1170 epm_floor->rhs.streettalk.streettalk = talloc_strdup(mem_ctx, data);
1171 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.streettalk.streettalk);
1172 return NT_STATUS_OK;
1174 case EPM_PROTOCOL_UNIX_DS:
1175 epm_floor->rhs.unix_ds.path = talloc_strdup(mem_ctx, data);
1176 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.unix_ds.path);
1177 return NT_STATUS_OK;
1179 case EPM_PROTOCOL_NULL:
1180 return NT_STATUS_OK;
1183 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
1187 return NT_STATUS_NOT_SUPPORTED;
1190 enum dcerpc_transport_t dcerpc_transport_by_endpoint_protocol(int prot)
1194 /* Find a transport that has 'prot' as 4th protocol */
1195 for (i=0;i<ARRAY_SIZE(transports);i++) {
1196 if (transports[i].num_protocols >= 2 &&
1197 transports[i].protseq[1] == prot) {
1198 return transports[i].transport;
1202 /* Unknown transport */
1203 return (unsigned int)-1;
1206 _PUBLIC_ enum dcerpc_transport_t dcerpc_transport_by_tower(const struct epm_tower *tower)
1210 /* Find a transport that matches this tower */
1211 for (i=0;i<ARRAY_SIZE(transports);i++) {
1213 if (transports[i].num_protocols != tower->num_floors - 2) {
1217 for (j = 0; j < transports[i].num_protocols; j++) {
1218 if (transports[i].protseq[j] != tower->floors[j+2].lhs.protocol) {
1223 if (j == transports[i].num_protocols) {
1224 return transports[i].transport;
1228 /* Unknown transport */
1229 return (unsigned int)-1;
1232 _PUBLIC_ const char *derpc_transport_string_by_transport(enum dcerpc_transport_t t)
1236 for (i=0; i<ARRAY_SIZE(transports); i++) {
1237 if (t == transports[i].transport) {
1238 return transports[i].name;
1244 _PUBLIC_ enum dcerpc_transport_t dcerpc_transport_by_name(const char *name)
1252 for (i=0; i<ARRAY_SIZE(transports);i++) {
1253 if (strcasecmp(name, transports[i].name) == 0) {
1254 return transports[i].transport;
1261 _PUBLIC_ NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx,
1262 struct epm_tower *tower,
1263 struct dcerpc_binding **b_out)
1266 struct dcerpc_binding *b;
1267 enum dcerpc_transport_t transport;
1268 struct ndr_syntax_id abstract_syntax;
1269 char *endpoint = NULL;
1273 * A tower needs to have at least 4 floors to carry useful
1274 * information. Floor 3 is the transport identifier which defines
1275 * how many floors are required at least.
1277 if (tower->num_floors < 4) {
1278 return NT_STATUS_INVALID_PARAMETER;
1281 status = dcerpc_parse_binding(mem_ctx, "", &b);
1282 if (!NT_STATUS_IS_OK(status)) {
1286 transport = dcerpc_transport_by_tower(tower);
1287 if (transport == NCA_UNKNOWN) {
1289 return NT_STATUS_NOT_SUPPORTED;
1292 status = dcerpc_binding_set_transport(b, transport);
1293 if (!NT_STATUS_IS_OK(status)) {
1298 /* Set abstract syntax */
1299 status = dcerpc_floor_get_lhs_data(&tower->floors[0], &abstract_syntax);
1300 if (!NT_STATUS_IS_OK(status)) {
1305 status = dcerpc_binding_set_abstract_syntax(b, &abstract_syntax);
1306 if (!NT_STATUS_IS_OK(status)) {
1311 /* Ignore floor 1, it contains the NDR version info */
1315 if (tower->num_floors >= 4) {
1316 endpoint = dcerpc_floor_get_rhs_data(b, &tower->floors[3]);
1319 int saved_errno = errno;
1321 return map_nt_error_from_unix_common(saved_errno);
1324 status = dcerpc_binding_set_string_option(b, "endpoint", endpoint);
1325 if (!NT_STATUS_IS_OK(status)) {
1329 TALLOC_FREE(endpoint);
1331 /* Set network address */
1333 if (tower->num_floors >= 5) {
1334 host = dcerpc_floor_get_rhs_data(b, &tower->floors[4]);
1337 int saved_errno = errno;
1339 return map_nt_error_from_unix_common(saved_errno);
1342 status = dcerpc_binding_set_string_option(b, "host", host);
1343 if (!NT_STATUS_IS_OK(status)) {
1347 status = dcerpc_binding_set_string_option(b, "target_hostname", host);
1348 if (!NT_STATUS_IS_OK(status)) {
1355 return NT_STATUS_OK;
1358 _PUBLIC_ struct dcerpc_binding *dcerpc_binding_dup(TALLOC_CTX *mem_ctx,
1359 const struct dcerpc_binding *b)
1361 struct dcerpc_binding *n;
1364 n = talloc_zero(mem_ctx, struct dcerpc_binding);
1369 n->transport = b->transport;
1370 n->object = b->object;
1371 n->flags = b->flags;
1372 n->assoc_group_id = b->assoc_group_id;
1374 if (b->object_string != NULL) {
1375 n->object_string = talloc_strdup(n, b->object_string);
1376 if (n->object_string == NULL) {
1381 if (b->host != NULL) {
1382 n->host = talloc_strdup(n, b->host);
1383 if (n->host == NULL) {
1389 if (b->target_hostname != NULL) {
1390 n->target_hostname = talloc_strdup(n, b->target_hostname);
1391 if (n->target_hostname == NULL) {
1397 if (b->target_principal != NULL) {
1398 n->target_principal = talloc_strdup(n, b->target_principal);
1399 if (n->target_principal == NULL) {
1405 if (b->endpoint != NULL) {
1406 n->endpoint = talloc_strdup(n, b->endpoint);
1407 if (n->endpoint == NULL) {
1413 for (count = 0; b->options && b->options[count]; count++);
1418 n->options = talloc_array(n, const char *, count + 1);
1419 if (n->options == NULL) {
1424 for (i = 0; i < count; i++) {
1425 n->options[i] = talloc_strdup(n->options, b->options[i]);
1426 if (n->options[i] == NULL) {
1431 n->options[count] = NULL;
1437 _PUBLIC_ NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx,
1438 const struct dcerpc_binding *binding,
1439 struct epm_tower *tower)
1441 const enum epm_protocol *protseq = NULL;
1442 int num_protocols = -1, i;
1443 struct ndr_syntax_id abstract_syntax;
1446 /* Find transport */
1447 for (i=0;i<ARRAY_SIZE(transports);i++) {
1448 if (transports[i].transport == binding->transport) {
1449 protseq = transports[i].protseq;
1450 num_protocols = transports[i].num_protocols;
1455 if (num_protocols == -1) {
1456 DEBUG(0, ("Unable to find transport with id '%d'\n", binding->transport));
1457 return NT_STATUS_UNSUCCESSFUL;
1460 tower->num_floors = 2 + num_protocols;
1461 tower->floors = talloc_array(mem_ctx, struct epm_floor, tower->num_floors);
1464 tower->floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
1466 abstract_syntax = dcerpc_binding_get_abstract_syntax(binding);
1467 tower->floors[0].lhs.lhs_data = dcerpc_floor_pack_lhs_data(tower->floors,
1470 if (!dcerpc_floor_pack_rhs_if_version_data(
1471 tower->floors, &abstract_syntax,
1472 &tower->floors[0].rhs.uuid.unknown)) {
1473 return NT_STATUS_NO_MEMORY;
1477 tower->floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
1479 tower->floors[1].lhs.lhs_data = dcerpc_floor_pack_lhs_data(tower->floors,
1480 &ndr_transfer_syntax_ndr);
1482 tower->floors[1].rhs.uuid.unknown = data_blob_talloc_zero(tower->floors, 2);
1484 /* Floor 2 to num_protocols */
1485 for (i = 0; i < num_protocols; i++) {
1486 tower->floors[2 + i].lhs.protocol = protseq[i];
1487 tower->floors[2 + i].lhs.lhs_data = data_blob_null;
1488 ZERO_STRUCT(tower->floors[2 + i].rhs);
1489 status = dcerpc_floor_set_rhs_data(tower->floors,
1490 &tower->floors[2 + i],
1492 if (!NT_STATUS_IS_OK(status)) {
1497 /* The 4th floor contains the endpoint */
1498 if (num_protocols >= 2 && binding->endpoint) {
1499 status = dcerpc_floor_set_rhs_data(tower->floors,
1502 if (!NT_STATUS_IS_OK(status)) {
1507 /* The 5th contains the network address */
1508 if (num_protocols >= 3 && binding->host) {
1509 status = dcerpc_floor_set_rhs_data(tower->floors,
1512 if (!NT_STATUS_IS_OK(status)) {
1517 return NT_STATUS_OK;