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;
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)) {
229 s = talloc_asprintf_append_buffer(s, "%s@",
230 GUID_string(mem_ctx, &b->object));
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)
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;
529 _PUBLIC_ enum dcerpc_transport_t dcerpc_binding_get_transport(const struct dcerpc_binding *b)
534 _PUBLIC_ NTSTATUS dcerpc_binding_set_transport(struct dcerpc_binding *b,
535 enum dcerpc_transport_t transport)
540 * TODO: we may want to check the transport value is
543 if (b->transport == transport) {
548 * This implicitly resets the endpoint
549 * as the endpoint is transport specific.
551 * It also resets the assoc group as it's
552 * also endpoint specific.
554 * TODO: in future we may reset more options
557 status = dcerpc_binding_set_string_option(b, "endpoint", NULL);
558 if (!NT_STATUS_IS_OK(status)) {
562 b->assoc_group_id = 0;
564 b->transport = transport;
568 _PUBLIC_ void dcerpc_binding_get_auth_info(const struct dcerpc_binding *b,
569 enum dcerpc_AuthType *_auth_type,
570 enum dcerpc_AuthLevel *_auth_level)
572 enum dcerpc_AuthType auth_type;
573 enum dcerpc_AuthLevel auth_level;
575 if (b->flags & DCERPC_AUTH_SPNEGO) {
576 auth_type = DCERPC_AUTH_TYPE_SPNEGO;
577 } else if (b->flags & DCERPC_AUTH_KRB5) {
578 auth_type = DCERPC_AUTH_TYPE_KRB5;
579 } else if (b->flags & DCERPC_SCHANNEL) {
580 auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
581 } else if (b->flags & DCERPC_AUTH_NTLM) {
582 auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
584 auth_type = DCERPC_AUTH_TYPE_NONE;
587 if (b->flags & DCERPC_SEAL) {
588 auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
589 } else if (b->flags & DCERPC_SIGN) {
590 auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
591 } else if (b->flags & DCERPC_CONNECT) {
592 auth_level = DCERPC_AUTH_LEVEL_CONNECT;
593 } else if (auth_type != DCERPC_AUTH_TYPE_NONE) {
594 auth_level = DCERPC_AUTH_LEVEL_CONNECT;
596 auth_level = DCERPC_AUTH_LEVEL_NONE;
599 if (_auth_type != NULL) {
600 *_auth_type = auth_type;
603 if (_auth_level != NULL) {
604 *_auth_level = auth_level;
608 _PUBLIC_ uint32_t dcerpc_binding_get_assoc_group_id(const struct dcerpc_binding *b)
610 return b->assoc_group_id;
613 _PUBLIC_ NTSTATUS dcerpc_binding_set_assoc_group_id(struct dcerpc_binding *b,
614 uint32_t assoc_group_id)
616 b->assoc_group_id = assoc_group_id;
620 _PUBLIC_ struct ndr_syntax_id dcerpc_binding_get_abstract_syntax(const struct dcerpc_binding *b)
622 const char *s = dcerpc_binding_get_string_option(b, "abstract_syntax");
624 struct ndr_syntax_id id;
627 return ndr_syntax_id_null;
630 ok = ndr_syntax_id_from_string(s, &id);
632 return ndr_syntax_id_null;
638 _PUBLIC_ NTSTATUS dcerpc_binding_set_abstract_syntax(struct dcerpc_binding *b,
639 const struct ndr_syntax_id *syntax)
644 if (syntax == NULL) {
645 status = dcerpc_binding_set_string_option(b, "abstract_syntax", NULL);
646 if (!NT_STATUS_IS_OK(status)) {
653 if (ndr_syntax_id_equal(&ndr_syntax_id_null, syntax)) {
654 status = dcerpc_binding_set_string_option(b, "abstract_syntax", NULL);
655 if (!NT_STATUS_IS_OK(status)) {
662 s = ndr_syntax_id_to_string(b, syntax);
664 return NT_STATUS_NO_MEMORY;
667 status = dcerpc_binding_set_string_option(b, "abstract_syntax", s);
669 if (!NT_STATUS_IS_OK(status)) {
676 _PUBLIC_ const char *dcerpc_binding_get_string_option(const struct dcerpc_binding *b,
682 #define _SPECIAL(x) { .name = #x, .value = b->x, }
684 { .name = "object", .value = b->object_string, },
687 _SPECIAL(target_hostname),
688 _SPECIAL(target_principal),
691 const struct ncacn_option *no = NULL;
692 size_t name_len = strlen(name);
696 ret = strcmp(name, "transport");
698 return derpc_transport_string_by_transport(b->transport);
701 ret = strcmp(name, "assoc_group_id");
703 char *tmp = discard_const_p(char, b->assoc_group_string);
705 if (b->assoc_group_id == 0) {
709 snprintf(tmp, sizeof(b->assoc_group_string),
710 "0x%08x", b->assoc_group_id);
711 return (const char *)b->assoc_group_string;
714 for (i=0; i < ARRAY_SIZE(specials); i++) {
715 ret = strcmp(specials[i].name, name);
720 return specials[i].value;
723 no = ncacn_option_by_name(name);
725 if (b->flags & no->flag) {
732 if (b->options == NULL) {
736 for (i=0; b->options[i]; i++) {
737 const char *o = b->options[i];
738 const char *vs = NULL;
740 ret = strncmp(name, o, name_len);
745 if (o[name_len] != '=') {
749 vs = &o[name_len + 1];
757 _PUBLIC_ char *dcerpc_binding_copy_string_option(TALLOC_CTX *mem_ctx,
758 const struct dcerpc_binding *b,
761 const char *c = dcerpc_binding_get_string_option(b, name);
769 v = talloc_strdup(mem_ctx, c);
778 _PUBLIC_ NTSTATUS dcerpc_binding_set_string_option(struct dcerpc_binding *b,
785 #define _SPECIAL(x) { .name = #x, .ptr = &b->x, }
789 _SPECIAL(target_hostname),
790 _SPECIAL(target_principal),
793 const struct ncacn_option *no = NULL;
794 size_t name_len = strlen(name);
795 const char *opt = NULL;
801 * Note: value == NULL, means delete it.
802 * value != NULL means add or reset.
805 ret = strcmp(name, "transport");
807 enum dcerpc_transport_t t = dcerpc_transport_by_name(value);
809 if (t == NCA_UNKNOWN && value != NULL) {
810 return NT_STATUS_INVALID_PARAMETER_MIX;
813 return dcerpc_binding_set_transport(b, t);
816 ret = strcmp(name, "object");
819 struct GUID uuid = GUID_zero();
823 blob = data_blob_string_const(value);
824 if (blob.length != 36) {
825 return NT_STATUS_INVALID_PARAMETER_MIX;
828 status = GUID_from_data_blob(&blob, &uuid);
829 if (!NT_STATUS_IS_OK(status)) {
834 return dcerpc_binding_set_object(b, uuid);
837 ret = strcmp(name, "assoc_group_id");
839 uint32_t assoc_group_id = 0;
844 ret = sscanf(value, "0x%08x%c", &assoc_group_id, &c);
846 return NT_STATUS_INVALID_PARAMETER_MIX;
850 return dcerpc_binding_set_assoc_group_id(b, assoc_group_id);
853 for (i=0; i < ARRAY_SIZE(specials); i++) {
854 ret = strcmp(specials[i].name, name);
859 tmp = discard_const_p(char, *specials[i].ptr);
863 *specials[i].ptr = NULL;
867 if (value[0] == '\0') {
868 return NT_STATUS_INVALID_PARAMETER_MIX;
871 *specials[i].ptr = talloc_strdup(b, value);
872 if (*specials[i].ptr == NULL) {
873 *specials[i].ptr = tmp;
874 return NT_STATUS_NO_MEMORY;
881 no = ncacn_option_by_name(name);
884 b->flags &= ~no->flag;
888 ret = strcasecmp(no->name, value);
890 return NT_STATUS_INVALID_PARAMETER_MIX;
893 b->flags |= no->flag;
897 for (i=0; b->options && b->options[i]; i++) {
898 const char *o = b->options[i];
900 ret = strncmp(name, o, name_len);
905 if (o[name_len] != '=') {
920 n = talloc_realloc(b, b->options, const char *, i + 2);
922 return NT_STATUS_NO_MEMORY;
929 tmp = discard_const_p(char, opt);
932 for (;b->options[i];i++) {
933 b->options[i] = b->options[i+1];
939 b->options[i] = talloc_asprintf(b->options, "%s=%s",
941 if (b->options[i] == NULL) {
943 return NT_STATUS_NO_MEMORY;
949 _PUBLIC_ uint32_t dcerpc_binding_get_flags(const struct dcerpc_binding *b)
954 _PUBLIC_ NTSTATUS dcerpc_binding_set_flags(struct dcerpc_binding *b,
959 * TODO: in future we may want to reject invalid combinations
962 b->flags |= additional;
967 _PUBLIC_ NTSTATUS dcerpc_floor_get_lhs_data(const struct epm_floor *epm_floor,
968 struct ndr_syntax_id *syntax)
970 TALLOC_CTX *mem_ctx = talloc_init("floor_get_lhs_data");
971 struct ndr_pull *ndr;
972 enum ndr_err_code ndr_err;
973 uint16_t if_version=0;
975 ndr = ndr_pull_init_blob(&epm_floor->lhs.lhs_data, mem_ctx);
977 talloc_free(mem_ctx);
978 return NT_STATUS_NO_MEMORY;
980 ndr->flags |= LIBNDR_FLAG_NOALIGN;
982 ndr_err = ndr_pull_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
983 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
984 talloc_free(mem_ctx);
985 return ndr_map_error2ntstatus(ndr_err);
988 ndr_err = ndr_pull_uint16(ndr, NDR_SCALARS, &if_version);
989 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
990 talloc_free(mem_ctx);
991 return ndr_map_error2ntstatus(ndr_err);
994 syntax->if_version = if_version;
996 talloc_free(mem_ctx);
1001 static DATA_BLOB dcerpc_floor_pack_lhs_data(TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax)
1004 enum ndr_err_code ndr_err;
1005 struct ndr_push *ndr;
1007 ndr = ndr_push_init_ctx(mem_ctx);
1009 return data_blob_null;
1012 ndr->flags |= LIBNDR_FLAG_NOALIGN;
1014 ndr_err = ndr_push_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
1015 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1016 return data_blob_null;
1018 ndr_err = ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version);
1019 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1020 return data_blob_null;
1023 blob = ndr_push_blob(ndr);
1024 talloc_steal(mem_ctx, blob.data);
1029 static bool dcerpc_floor_pack_rhs_if_version_data(
1030 TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax,
1034 struct ndr_push *ndr = ndr_push_init_ctx(mem_ctx);
1035 enum ndr_err_code ndr_err;
1041 ndr->flags |= LIBNDR_FLAG_NOALIGN;
1043 ndr_err = ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version >> 16);
1044 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1048 blob = ndr_push_blob(ndr);
1049 talloc_steal(mem_ctx, blob.data);
1055 char *dcerpc_floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
1057 switch (epm_floor->lhs.protocol) {
1058 case EPM_PROTOCOL_TCP:
1059 if (epm_floor->rhs.tcp.port == 0) return NULL;
1060 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.tcp.port);
1062 case EPM_PROTOCOL_UDP:
1063 if (epm_floor->rhs.udp.port == 0) return NULL;
1064 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.udp.port);
1066 case EPM_PROTOCOL_HTTP:
1067 if (epm_floor->rhs.http.port == 0) return NULL;
1068 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.http.port);
1070 case EPM_PROTOCOL_IP:
1071 return talloc_strdup(mem_ctx, epm_floor->rhs.ip.ipaddr);
1073 case EPM_PROTOCOL_NCACN:
1076 case EPM_PROTOCOL_NCADG:
1079 case EPM_PROTOCOL_SMB:
1080 if (strlen(epm_floor->rhs.smb.unc) == 0) return NULL;
1081 return talloc_strdup(mem_ctx, epm_floor->rhs.smb.unc);
1083 case EPM_PROTOCOL_NAMED_PIPE:
1084 if (strlen(epm_floor->rhs.named_pipe.path) == 0) return NULL;
1085 return talloc_strdup(mem_ctx, epm_floor->rhs.named_pipe.path);
1087 case EPM_PROTOCOL_NETBIOS:
1088 if (strlen(epm_floor->rhs.netbios.name) == 0) return NULL;
1089 return talloc_strdup(mem_ctx, epm_floor->rhs.netbios.name);
1091 case EPM_PROTOCOL_NCALRPC:
1094 case EPM_PROTOCOL_VINES_SPP:
1095 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_spp.port);
1097 case EPM_PROTOCOL_VINES_IPC:
1098 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_ipc.port);
1100 case EPM_PROTOCOL_STREETTALK:
1101 return talloc_strdup(mem_ctx, epm_floor->rhs.streettalk.streettalk);
1103 case EPM_PROTOCOL_UNIX_DS:
1104 if (strlen(epm_floor->rhs.unix_ds.path) == 0) return NULL;
1105 return talloc_strdup(mem_ctx, epm_floor->rhs.unix_ds.path);
1107 case EPM_PROTOCOL_NULL:
1111 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
1118 static NTSTATUS dcerpc_floor_set_rhs_data(TALLOC_CTX *mem_ctx,
1119 struct epm_floor *epm_floor,
1126 switch (epm_floor->lhs.protocol) {
1127 case EPM_PROTOCOL_TCP:
1128 epm_floor->rhs.tcp.port = atoi(data);
1129 return NT_STATUS_OK;
1131 case EPM_PROTOCOL_UDP:
1132 epm_floor->rhs.udp.port = atoi(data);
1133 return NT_STATUS_OK;
1135 case EPM_PROTOCOL_HTTP:
1136 epm_floor->rhs.http.port = atoi(data);
1137 return NT_STATUS_OK;
1139 case EPM_PROTOCOL_IP:
1140 if (!is_ipaddress_v4(data)) {
1143 epm_floor->rhs.ip.ipaddr = talloc_strdup(mem_ctx, data);
1144 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.ip.ipaddr);
1145 return NT_STATUS_OK;
1147 case EPM_PROTOCOL_NCACN:
1148 epm_floor->rhs.ncacn.minor_version = 0;
1149 return NT_STATUS_OK;
1151 case EPM_PROTOCOL_NCADG:
1152 epm_floor->rhs.ncadg.minor_version = 0;
1153 return NT_STATUS_OK;
1155 case EPM_PROTOCOL_SMB:
1156 epm_floor->rhs.smb.unc = talloc_strdup(mem_ctx, data);
1157 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.smb.unc);
1158 return NT_STATUS_OK;
1160 case EPM_PROTOCOL_NAMED_PIPE:
1161 epm_floor->rhs.named_pipe.path = talloc_strdup(mem_ctx, data);
1162 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.named_pipe.path);
1163 return NT_STATUS_OK;
1165 case EPM_PROTOCOL_NETBIOS:
1166 epm_floor->rhs.netbios.name = talloc_strdup(mem_ctx, data);
1167 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.netbios.name);
1168 return NT_STATUS_OK;
1170 case EPM_PROTOCOL_NCALRPC:
1171 return NT_STATUS_OK;
1173 case EPM_PROTOCOL_VINES_SPP:
1174 epm_floor->rhs.vines_spp.port = atoi(data);
1175 return NT_STATUS_OK;
1177 case EPM_PROTOCOL_VINES_IPC:
1178 epm_floor->rhs.vines_ipc.port = atoi(data);
1179 return NT_STATUS_OK;
1181 case EPM_PROTOCOL_STREETTALK:
1182 epm_floor->rhs.streettalk.streettalk = talloc_strdup(mem_ctx, data);
1183 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.streettalk.streettalk);
1184 return NT_STATUS_OK;
1186 case EPM_PROTOCOL_UNIX_DS:
1187 epm_floor->rhs.unix_ds.path = talloc_strdup(mem_ctx, data);
1188 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.unix_ds.path);
1189 return NT_STATUS_OK;
1191 case EPM_PROTOCOL_NULL:
1192 return NT_STATUS_OK;
1195 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
1199 return NT_STATUS_NOT_SUPPORTED;
1202 enum dcerpc_transport_t dcerpc_transport_by_endpoint_protocol(int prot)
1206 /* Find a transport that has 'prot' as 4th protocol */
1207 for (i=0;i<ARRAY_SIZE(transports);i++) {
1208 if (transports[i].num_protocols >= 2 &&
1209 transports[i].protseq[1] == prot) {
1210 return transports[i].transport;
1214 /* Unknown transport */
1215 return (unsigned int)-1;
1218 _PUBLIC_ enum dcerpc_transport_t dcerpc_transport_by_tower(const struct epm_tower *tower)
1222 /* Find a transport that matches this tower */
1223 for (i=0;i<ARRAY_SIZE(transports);i++) {
1225 if (transports[i].num_protocols != tower->num_floors - 2) {
1229 for (j = 0; j < transports[i].num_protocols; j++) {
1230 if (transports[i].protseq[j] != tower->floors[j+2].lhs.protocol) {
1235 if (j == transports[i].num_protocols) {
1236 return transports[i].transport;
1240 /* Unknown transport */
1241 return (unsigned int)-1;
1244 _PUBLIC_ const char *derpc_transport_string_by_transport(enum dcerpc_transport_t t)
1248 for (i=0; i<ARRAY_SIZE(transports); i++) {
1249 if (t == transports[i].transport) {
1250 return transports[i].name;
1256 _PUBLIC_ enum dcerpc_transport_t dcerpc_transport_by_name(const char *name)
1264 for (i=0; i<ARRAY_SIZE(transports);i++) {
1265 if (strcasecmp(name, transports[i].name) == 0) {
1266 return transports[i].transport;
1273 _PUBLIC_ NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx,
1274 struct epm_tower *tower,
1275 struct dcerpc_binding **b_out)
1278 struct dcerpc_binding *b;
1279 enum dcerpc_transport_t transport;
1280 struct ndr_syntax_id abstract_syntax;
1281 char *endpoint = NULL;
1285 * A tower needs to have at least 4 floors to carry useful
1286 * information. Floor 3 is the transport identifier which defines
1287 * how many floors are required at least.
1289 if (tower->num_floors < 4) {
1290 return NT_STATUS_INVALID_PARAMETER;
1293 status = dcerpc_parse_binding(mem_ctx, "", &b);
1294 if (!NT_STATUS_IS_OK(status)) {
1298 transport = dcerpc_transport_by_tower(tower);
1299 if (transport == NCA_UNKNOWN) {
1301 return NT_STATUS_NOT_SUPPORTED;
1304 status = dcerpc_binding_set_transport(b, transport);
1305 if (!NT_STATUS_IS_OK(status)) {
1310 /* Set abstract syntax */
1311 status = dcerpc_floor_get_lhs_data(&tower->floors[0], &abstract_syntax);
1312 if (!NT_STATUS_IS_OK(status)) {
1317 status = dcerpc_binding_set_abstract_syntax(b, &abstract_syntax);
1318 if (!NT_STATUS_IS_OK(status)) {
1323 /* Ignore floor 1, it contains the NDR version info */
1327 if (tower->num_floors >= 4) {
1328 endpoint = dcerpc_floor_get_rhs_data(b, &tower->floors[3]);
1331 int saved_errno = errno;
1333 return map_nt_error_from_unix_common(saved_errno);
1336 status = dcerpc_binding_set_string_option(b, "endpoint", endpoint);
1337 if (!NT_STATUS_IS_OK(status)) {
1341 TALLOC_FREE(endpoint);
1343 /* Set network address */
1345 if (tower->num_floors >= 5) {
1346 host = dcerpc_floor_get_rhs_data(b, &tower->floors[4]);
1349 int saved_errno = errno;
1351 return map_nt_error_from_unix_common(saved_errno);
1354 status = dcerpc_binding_set_string_option(b, "host", host);
1355 if (!NT_STATUS_IS_OK(status)) {
1359 status = dcerpc_binding_set_string_option(b, "target_hostname", host);
1360 if (!NT_STATUS_IS_OK(status)) {
1367 return NT_STATUS_OK;
1370 _PUBLIC_ struct dcerpc_binding *dcerpc_binding_dup(TALLOC_CTX *mem_ctx,
1371 const struct dcerpc_binding *b)
1373 struct dcerpc_binding *n;
1376 n = talloc_zero(mem_ctx, struct dcerpc_binding);
1381 n->transport = b->transport;
1382 n->object = b->object;
1383 n->flags = b->flags;
1384 n->assoc_group_id = b->assoc_group_id;
1386 if (b->object_string != NULL) {
1387 n->object_string = talloc_strdup(n, b->object_string);
1388 if (n->object_string == NULL) {
1393 if (b->host != NULL) {
1394 n->host = talloc_strdup(n, b->host);
1395 if (n->host == NULL) {
1401 if (b->target_hostname != NULL) {
1402 n->target_hostname = talloc_strdup(n, b->target_hostname);
1403 if (n->target_hostname == NULL) {
1409 if (b->target_principal != NULL) {
1410 n->target_principal = talloc_strdup(n, b->target_principal);
1411 if (n->target_principal == NULL) {
1417 if (b->endpoint != NULL) {
1418 n->endpoint = talloc_strdup(n, b->endpoint);
1419 if (n->endpoint == NULL) {
1425 for (count = 0; b->options && b->options[count]; count++);
1430 n->options = talloc_array(n, const char *, count + 1);
1431 if (n->options == NULL) {
1436 for (i = 0; i < count; i++) {
1437 n->options[i] = talloc_strdup(n->options, b->options[i]);
1438 if (n->options[i] == NULL) {
1443 n->options[count] = NULL;
1449 _PUBLIC_ NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx,
1450 const struct dcerpc_binding *binding,
1451 struct epm_tower *tower)
1453 const enum epm_protocol *protseq = NULL;
1454 int num_protocols = -1, i;
1455 struct ndr_syntax_id abstract_syntax;
1458 /* Find transport */
1459 for (i=0;i<ARRAY_SIZE(transports);i++) {
1460 if (transports[i].transport == binding->transport) {
1461 protseq = transports[i].protseq;
1462 num_protocols = transports[i].num_protocols;
1467 if (num_protocols == -1) {
1468 DEBUG(0, ("Unable to find transport with id '%d'\n", binding->transport));
1469 return NT_STATUS_UNSUCCESSFUL;
1472 tower->num_floors = 2 + num_protocols;
1473 tower->floors = talloc_array(mem_ctx, struct epm_floor, tower->num_floors);
1476 tower->floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
1478 abstract_syntax = dcerpc_binding_get_abstract_syntax(binding);
1479 tower->floors[0].lhs.lhs_data = dcerpc_floor_pack_lhs_data(tower->floors,
1482 if (!dcerpc_floor_pack_rhs_if_version_data(
1483 tower->floors, &abstract_syntax,
1484 &tower->floors[0].rhs.uuid.unknown)) {
1485 return NT_STATUS_NO_MEMORY;
1489 tower->floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
1491 tower->floors[1].lhs.lhs_data = dcerpc_floor_pack_lhs_data(tower->floors,
1492 &ndr_transfer_syntax_ndr);
1494 tower->floors[1].rhs.uuid.unknown = data_blob_talloc_zero(tower->floors, 2);
1496 /* Floor 2 to num_protocols */
1497 for (i = 0; i < num_protocols; i++) {
1498 tower->floors[2 + i].lhs.protocol = protseq[i];
1499 tower->floors[2 + i].lhs.lhs_data = data_blob_null;
1500 ZERO_STRUCT(tower->floors[2 + i].rhs);
1501 status = dcerpc_floor_set_rhs_data(tower->floors,
1502 &tower->floors[2 + i],
1504 if (!NT_STATUS_IS_OK(status)) {
1509 /* The 4th floor contains the endpoint */
1510 if (num_protocols >= 2 && binding->endpoint) {
1511 status = dcerpc_floor_set_rhs_data(tower->floors,
1514 if (!NT_STATUS_IS_OK(status)) {
1519 /* The 5th contains the network address */
1520 if (num_protocols >= 3 && binding->host) {
1521 status = dcerpc_floor_set_rhs_data(tower->floors,
1524 if (!NT_STATUS_IS_OK(status)) {
1529 return NT_STATUS_OK;