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 {"validate", DCERPC_DEBUG_VALIDATE_BOTH},
102 {"print", DCERPC_DEBUG_PRINT_BOTH},
103 {"padcheck", DCERPC_DEBUG_PAD_CHECK},
104 {"bigendian", DCERPC_PUSH_BIGENDIAN},
105 {"smb2", DCERPC_SMB2},
106 {"ndr64", DCERPC_NDR64},
109 static const struct ncacn_option *ncacn_option_by_name(const char *name)
113 for (i=0; i<ARRAY_SIZE(ncacn_options); i++) {
116 ret = strcasecmp(ncacn_options[i].name, name);
121 return &ncacn_options[i];
127 const char *epm_floor_string(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
129 struct ndr_syntax_id syntax;
132 switch(epm_floor->lhs.protocol) {
133 case EPM_PROTOCOL_UUID:
134 status = dcerpc_floor_get_lhs_data(epm_floor, &syntax);
135 if (NT_STATUS_IS_OK(status)) {
136 /* lhs is used: UUID */
139 if (GUID_equal(&syntax.uuid, &ndr_transfer_syntax_ndr.uuid)) {
143 if (GUID_equal(&syntax.uuid, &ndr_transfer_syntax_ndr64.uuid)) {
147 uuidstr = GUID_string(mem_ctx, &syntax.uuid);
149 return talloc_asprintf(mem_ctx, " uuid %s/0x%02x", uuidstr, syntax.if_version);
151 return talloc_asprintf(mem_ctx, "IPX:%s",
152 data_blob_hex_string_upper(mem_ctx, &epm_floor->rhs.uuid.unknown));
155 case EPM_PROTOCOL_NCACN:
158 case EPM_PROTOCOL_NCADG:
161 case EPM_PROTOCOL_NCALRPC:
164 case EPM_PROTOCOL_DNET_NSP:
167 case EPM_PROTOCOL_IP:
168 return talloc_asprintf(mem_ctx, "IP:%s", epm_floor->rhs.ip.ipaddr);
170 case EPM_PROTOCOL_NAMED_PIPE:
171 return talloc_asprintf(mem_ctx, "NAMED-PIPE:%s", epm_floor->rhs.named_pipe.path);
173 case EPM_PROTOCOL_SMB:
174 return talloc_asprintf(mem_ctx, "SMB:%s", epm_floor->rhs.smb.unc);
176 case EPM_PROTOCOL_UNIX_DS:
177 return talloc_asprintf(mem_ctx, "Unix:%s", epm_floor->rhs.unix_ds.path);
179 case EPM_PROTOCOL_NETBIOS:
180 return talloc_asprintf(mem_ctx, "NetBIOS:%s", epm_floor->rhs.netbios.name);
182 case EPM_PROTOCOL_NETBEUI:
185 case EPM_PROTOCOL_SPX:
188 case EPM_PROTOCOL_NB_IPX:
191 case EPM_PROTOCOL_HTTP:
192 return talloc_asprintf(mem_ctx, "HTTP:%d", epm_floor->rhs.http.port);
194 case EPM_PROTOCOL_TCP:
195 return talloc_asprintf(mem_ctx, "TCP:%d", epm_floor->rhs.tcp.port);
197 case EPM_PROTOCOL_UDP:
198 return talloc_asprintf(mem_ctx, "UDP:%d", epm_floor->rhs.udp.port);
201 return talloc_asprintf(mem_ctx, "UNK(%02x):", epm_floor->lhs.protocol);
207 form a binding string from a binding structure
209 _PUBLIC_ char *dcerpc_binding_string(TALLOC_CTX *mem_ctx, const struct dcerpc_binding *b)
211 char *s = talloc_strdup(mem_ctx, "");
214 const char *t_name = NULL;
215 bool option_section = false;
216 const char *target_hostname = NULL;
218 if (b->transport != NCA_UNKNOWN) {
219 t_name = derpc_transport_string_by_transport(b->transport);
226 if (!GUID_all_zero(&b->object.uuid)) {
228 s = talloc_asprintf_append_buffer(s, "%s@",
229 GUID_string(mem_ctx, &b->object.uuid));
236 if (t_name != NULL) {
238 s = talloc_asprintf_append_buffer(s, "%s:", t_name);
247 s = talloc_asprintf_append_buffer(s, "%s", b->host);
254 target_hostname = b->target_hostname;
255 if (target_hostname != NULL && b->host != NULL) {
256 if (strcmp(target_hostname, b->host) == 0) {
257 target_hostname = NULL;
262 option_section = true;
263 } else if (target_hostname) {
264 option_section = true;
265 } else if (b->target_principal) {
266 option_section = true;
267 } else if (b->assoc_group_id != 0) {
268 option_section = true;
269 } else if (b->options) {
270 option_section = true;
271 } else if (b->flags) {
272 option_section = true;
275 if (!option_section) {
280 s = talloc_asprintf_append_buffer(s, "[");
288 s = talloc_asprintf_append_buffer(s, "%s", b->endpoint);
295 for (i=0;i<ARRAY_SIZE(ncacn_options);i++) {
296 if (!(b->flags & ncacn_options[i].flag)) {
301 s = talloc_asprintf_append_buffer(s, ",%s", ncacn_options[i].name);
308 if (target_hostname) {
310 s = talloc_asprintf_append_buffer(s, ",target_hostname=%s",
318 if (b->target_principal) {
320 s = talloc_asprintf_append_buffer(s, ",target_principal=%s",
321 b->target_principal);
328 if (b->assoc_group_id != 0) {
330 s = talloc_asprintf_append_buffer(s, ",assoc_group_id=0x%08x",
338 for (i=0;b->options && b->options[i];i++) {
340 s = talloc_asprintf_append_buffer(s, ",%s", b->options[i]);
348 s = talloc_asprintf_append_buffer(s, "]");
358 parse a binding string into a dcerpc_binding structure
360 _PUBLIC_ NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *_s, struct dcerpc_binding **b_out)
363 struct dcerpc_binding *b;
365 char *options = NULL;
370 b = talloc_zero(mem_ctx, struct dcerpc_binding);
372 return NT_STATUS_NO_MEMORY;
375 _t = talloc_strdup(b, _s);
378 return NT_STATUS_NO_MEMORY;
387 if (options[strlen(options)-1] != ']') {
389 return NT_STATUS_INVALID_PARAMETER_MIX;
391 options[strlen(options)-1] = 0;
396 if (p && PTR_DIFF(p, s) == 36) { /* 36 is the length of a UUID */
399 status = dcerpc_binding_set_string_option(b, "object", s);
400 if (!NT_STATUS_IS_OK(status)) {
411 b->transport = NCA_UNKNOWN;
415 status = dcerpc_binding_set_string_option(b, "transport", s);
416 if (!NT_STATUS_IS_OK(status)) {
425 status = dcerpc_binding_set_string_option(b, "host", s);
426 if (!NT_STATUS_IS_OK(status)) {
431 b->target_hostname = talloc_strdup(b, b->host);
432 if (b->target_hostname == NULL) {
434 return NT_STATUS_NO_MEMORY;
438 for (i=0; options != NULL; i++) {
439 const char *name = options;
440 const char *value = NULL;
442 p = strchr(options, ',');
450 p = strchr(name, '=');
458 * If it's not a key=value pair
459 * it might be a ncacn_option
460 * or if it's the first option
463 const struct ncacn_option *no = NULL;
467 no = ncacn_option_by_name(name);
471 * we don't allow unknown options
473 return NT_STATUS_INVALID_PARAMETER_MIX;
477 * This is the endpoint
480 if (strlen(value) == 0) {
486 status = dcerpc_binding_set_string_option(b, name, value);
487 if (!NT_STATUS_IS_OK(status)) {
498 _PUBLIC_ struct GUID dcerpc_binding_get_object(const struct dcerpc_binding *b)
500 return b->object.uuid;
503 _PUBLIC_ NTSTATUS dcerpc_binding_set_object(struct dcerpc_binding *b,
506 char *tmp = discard_const_p(char, b->object_string);
508 if (GUID_all_zero(&object)) {
510 b->object_string = NULL;
511 ZERO_STRUCT(b->object);
515 b->object_string = GUID_string(b, &object);
516 if (b->object_string == NULL) {
517 b->object_string = tmp;
518 return NT_STATUS_NO_MEMORY;
522 ZERO_STRUCT(b->object);
523 b->object.uuid = object;
527 _PUBLIC_ enum dcerpc_transport_t dcerpc_binding_get_transport(const struct dcerpc_binding *b)
532 _PUBLIC_ NTSTATUS dcerpc_binding_set_transport(struct dcerpc_binding *b,
533 enum dcerpc_transport_t transport)
535 char *tmp = discard_const_p(char, b->endpoint);
538 * TODO: we may want to check the transport value is
541 if (b->transport == transport) {
545 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
560 b->assoc_group_id = 0;
565 _PUBLIC_ void dcerpc_binding_get_auth_info(const struct dcerpc_binding *b,
566 enum dcerpc_AuthType *_auth_type,
567 enum dcerpc_AuthLevel *_auth_level)
569 enum dcerpc_AuthType auth_type;
570 enum dcerpc_AuthLevel auth_level;
572 if (b->flags & DCERPC_AUTH_SPNEGO) {
573 auth_type = DCERPC_AUTH_TYPE_SPNEGO;
574 } else if (b->flags & DCERPC_AUTH_KRB5) {
575 auth_type = DCERPC_AUTH_TYPE_KRB5;
576 } else if (b->flags & DCERPC_SCHANNEL) {
577 auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
578 } else if (b->flags & DCERPC_AUTH_NTLM) {
579 auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
581 auth_type = DCERPC_AUTH_TYPE_NONE;
584 if (b->flags & DCERPC_SEAL) {
585 auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
586 } else if (b->flags & DCERPC_SIGN) {
587 auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
588 } else if (b->flags & DCERPC_CONNECT) {
589 auth_level = DCERPC_AUTH_LEVEL_CONNECT;
590 } else if (auth_type != DCERPC_AUTH_TYPE_NONE) {
591 auth_level = DCERPC_AUTH_LEVEL_CONNECT;
593 auth_level = DCERPC_AUTH_LEVEL_NONE;
596 if (_auth_type == NULL) {
597 *_auth_type = auth_type;
600 if (_auth_level == NULL) {
601 *_auth_level = auth_level;
605 _PUBLIC_ uint32_t dcerpc_binding_get_assoc_group_id(const struct dcerpc_binding *b)
607 return b->assoc_group_id;
610 _PUBLIC_ NTSTATUS dcerpc_binding_set_assoc_group_id(struct dcerpc_binding *b,
611 uint32_t assoc_group_id)
613 b->assoc_group_id = assoc_group_id;
617 _PUBLIC_ struct ndr_syntax_id dcerpc_binding_get_abstract_syntax(const struct dcerpc_binding *b)
620 * For now we just use object, until all callers are fixed.
625 _PUBLIC_ NTSTATUS dcerpc_binding_set_abstract_syntax(struct dcerpc_binding *b,
626 const struct ndr_syntax_id *syntax)
632 * For now we just use object, until all callers are fixed.
635 if (syntax != NULL) {
636 object = syntax->uuid;
638 object = GUID_zero();
642 * This sets also the string
644 status = dcerpc_binding_set_object(b, object);
645 if (!NT_STATUS_IS_OK(status)) {
649 if (syntax != NULL) {
651 * Here we need to reset the whole ndr_syntax_id
652 * structure including the .if_version
660 _PUBLIC_ const char *dcerpc_binding_get_string_option(const struct dcerpc_binding *b,
666 #define _SPECIAL(x) { .name = #x, .value = b->x, }
668 { .name = "object", .value = b->object_string, },
671 _SPECIAL(target_hostname),
672 _SPECIAL(target_principal),
675 const struct ncacn_option *no = NULL;
676 size_t name_len = strlen(name);
680 ret = strcmp(name, "transport");
682 return derpc_transport_string_by_transport(b->transport);
685 ret = strcmp(name, "assoc_group_id");
687 char *tmp = discard_const_p(char, b->assoc_group_string);
689 if (b->assoc_group_id == 0) {
693 snprintf(tmp, sizeof(b->assoc_group_string),
694 "0x%08x", b->assoc_group_id);
695 return (const char *)b->assoc_group_string;
698 for (i=0; i < ARRAY_SIZE(specials); i++) {
699 ret = strcmp(specials[i].name, name);
704 return specials[i].value;
707 no = ncacn_option_by_name(name);
709 if (b->flags & no->flag) {
716 if (b->options == NULL) {
720 for (i=0; b->options[i]; i++) {
721 const char *o = b->options[i];
722 const char *vs = NULL;
724 ret = strncmp(name, o, name_len);
729 if (o[name_len] != '=') {
733 vs = &o[name_len + 1];
741 _PUBLIC_ char *dcerpc_binding_copy_string_option(TALLOC_CTX *mem_ctx,
742 const struct dcerpc_binding *b,
745 const char *c = dcerpc_binding_get_string_option(b, name);
753 v = talloc_strdup(mem_ctx, c);
762 _PUBLIC_ NTSTATUS dcerpc_binding_set_string_option(struct dcerpc_binding *b,
769 #define _SPECIAL(x) { .name = #x, .ptr = &b->x, }
773 _SPECIAL(target_hostname),
774 _SPECIAL(target_principal),
777 const struct ncacn_option *no = NULL;
778 size_t name_len = strlen(name);
779 const char *opt = NULL;
785 * Note: value == NULL, means delete it.
786 * value != NULL means add or reset.
789 ret = strcmp(name, "transport");
791 enum dcerpc_transport_t t = dcerpc_transport_by_name(value);
793 if (t == NCA_UNKNOWN && value != NULL) {
794 return NT_STATUS_INVALID_PARAMETER_MIX;
797 return dcerpc_binding_set_transport(b, t);
800 ret = strcmp(name, "object");
803 struct GUID uuid = GUID_zero();
807 blob = data_blob_string_const(value);
808 if (blob.length != 36) {
809 return NT_STATUS_INVALID_PARAMETER_MIX;
812 status = GUID_from_data_blob(&blob, &uuid);
813 if (!NT_STATUS_IS_OK(status)) {
818 return dcerpc_binding_set_object(b, uuid);
821 ret = strcmp(name, "assoc_group_id");
823 uint32_t assoc_group_id = 0;
828 ret = sscanf(value, "0x%08x%c", &assoc_group_id, &c);
830 return NT_STATUS_INVALID_PARAMETER_MIX;
834 return dcerpc_binding_set_assoc_group_id(b, assoc_group_id);
837 for (i=0; i < ARRAY_SIZE(specials); i++) {
838 ret = strcmp(specials[i].name, name);
843 tmp = discard_const_p(char, *specials[i].ptr);
847 *specials[i].ptr = NULL;
851 if (value[0] == '\0') {
852 return NT_STATUS_INVALID_PARAMETER_MIX;
855 *specials[i].ptr = talloc_strdup(b, value);
856 if (*specials[i].ptr == NULL) {
857 *specials[i].ptr = tmp;
858 return NT_STATUS_NO_MEMORY;
865 no = ncacn_option_by_name(name);
868 b->flags &= ~no->flag;
872 ret = strcasecmp(no->name, value);
874 return NT_STATUS_INVALID_PARAMETER_MIX;
877 b->flags |= no->flag;
881 for (i=0; b->options && b->options[i]; i++) {
882 const char *o = b->options[i];
884 ret = strncmp(name, o, name_len);
889 if (o[name_len] != '=') {
904 n = talloc_realloc(b, b->options, const char *, i + 2);
906 return NT_STATUS_NO_MEMORY;
913 tmp = discard_const_p(char, opt);
916 for (;b->options[i];i++) {
917 b->options[i] = b->options[i+1];
923 b->options[i] = talloc_asprintf(b->options, "%s=%s",
925 if (b->options[i] == NULL) {
927 return NT_STATUS_NO_MEMORY;
933 _PUBLIC_ uint32_t dcerpc_binding_get_flags(const struct dcerpc_binding *b)
938 _PUBLIC_ NTSTATUS dcerpc_binding_set_flags(struct dcerpc_binding *b,
943 * TODO: in future we may want to reject invalid combinations
946 b->flags |= additional;
951 _PUBLIC_ NTSTATUS dcerpc_floor_get_lhs_data(const struct epm_floor *epm_floor,
952 struct ndr_syntax_id *syntax)
954 TALLOC_CTX *mem_ctx = talloc_init("floor_get_lhs_data");
955 struct ndr_pull *ndr;
956 enum ndr_err_code ndr_err;
957 uint16_t if_version=0;
959 ndr = ndr_pull_init_blob(&epm_floor->lhs.lhs_data, mem_ctx);
961 talloc_free(mem_ctx);
962 return NT_STATUS_NO_MEMORY;
964 ndr->flags |= LIBNDR_FLAG_NOALIGN;
966 ndr_err = ndr_pull_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
967 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
968 talloc_free(mem_ctx);
969 return ndr_map_error2ntstatus(ndr_err);
972 ndr_err = ndr_pull_uint16(ndr, NDR_SCALARS, &if_version);
973 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
974 talloc_free(mem_ctx);
975 return ndr_map_error2ntstatus(ndr_err);
978 syntax->if_version = if_version;
980 talloc_free(mem_ctx);
985 static DATA_BLOB dcerpc_floor_pack_lhs_data(TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax)
988 enum ndr_err_code ndr_err;
989 struct ndr_push *ndr;
991 ndr = ndr_push_init_ctx(mem_ctx);
993 return data_blob_null;
996 ndr->flags |= LIBNDR_FLAG_NOALIGN;
998 ndr_err = ndr_push_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
999 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1000 return data_blob_null;
1002 ndr_err = ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version);
1003 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1004 return data_blob_null;
1007 blob = ndr_push_blob(ndr);
1008 talloc_steal(mem_ctx, blob.data);
1013 static bool dcerpc_floor_pack_rhs_if_version_data(
1014 TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax,
1018 struct ndr_push *ndr = ndr_push_init_ctx(mem_ctx);
1019 enum ndr_err_code ndr_err;
1025 ndr->flags |= LIBNDR_FLAG_NOALIGN;
1027 ndr_err = ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version >> 16);
1028 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1032 blob = ndr_push_blob(ndr);
1033 talloc_steal(mem_ctx, blob.data);
1039 char *dcerpc_floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
1041 switch (epm_floor->lhs.protocol) {
1042 case EPM_PROTOCOL_TCP:
1043 if (epm_floor->rhs.tcp.port == 0) return NULL;
1044 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.tcp.port);
1046 case EPM_PROTOCOL_UDP:
1047 if (epm_floor->rhs.udp.port == 0) return NULL;
1048 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.udp.port);
1050 case EPM_PROTOCOL_HTTP:
1051 if (epm_floor->rhs.http.port == 0) return NULL;
1052 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.http.port);
1054 case EPM_PROTOCOL_IP:
1055 return talloc_strdup(mem_ctx, epm_floor->rhs.ip.ipaddr);
1057 case EPM_PROTOCOL_NCACN:
1060 case EPM_PROTOCOL_NCADG:
1063 case EPM_PROTOCOL_SMB:
1064 if (strlen(epm_floor->rhs.smb.unc) == 0) return NULL;
1065 return talloc_strdup(mem_ctx, epm_floor->rhs.smb.unc);
1067 case EPM_PROTOCOL_NAMED_PIPE:
1068 if (strlen(epm_floor->rhs.named_pipe.path) == 0) return NULL;
1069 return talloc_strdup(mem_ctx, epm_floor->rhs.named_pipe.path);
1071 case EPM_PROTOCOL_NETBIOS:
1072 if (strlen(epm_floor->rhs.netbios.name) == 0) return NULL;
1073 return talloc_strdup(mem_ctx, epm_floor->rhs.netbios.name);
1075 case EPM_PROTOCOL_NCALRPC:
1078 case EPM_PROTOCOL_VINES_SPP:
1079 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_spp.port);
1081 case EPM_PROTOCOL_VINES_IPC:
1082 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_ipc.port);
1084 case EPM_PROTOCOL_STREETTALK:
1085 return talloc_strdup(mem_ctx, epm_floor->rhs.streettalk.streettalk);
1087 case EPM_PROTOCOL_UNIX_DS:
1088 if (strlen(epm_floor->rhs.unix_ds.path) == 0) return NULL;
1089 return talloc_strdup(mem_ctx, epm_floor->rhs.unix_ds.path);
1091 case EPM_PROTOCOL_NULL:
1095 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
1102 static NTSTATUS dcerpc_floor_set_rhs_data(TALLOC_CTX *mem_ctx,
1103 struct epm_floor *epm_floor,
1110 switch (epm_floor->lhs.protocol) {
1111 case EPM_PROTOCOL_TCP:
1112 epm_floor->rhs.tcp.port = atoi(data);
1113 return NT_STATUS_OK;
1115 case EPM_PROTOCOL_UDP:
1116 epm_floor->rhs.udp.port = atoi(data);
1117 return NT_STATUS_OK;
1119 case EPM_PROTOCOL_HTTP:
1120 epm_floor->rhs.http.port = atoi(data);
1121 return NT_STATUS_OK;
1123 case EPM_PROTOCOL_IP:
1124 if (!is_ipaddress_v4(data)) {
1127 epm_floor->rhs.ip.ipaddr = talloc_strdup(mem_ctx, data);
1128 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.ip.ipaddr);
1129 return NT_STATUS_OK;
1131 case EPM_PROTOCOL_NCACN:
1132 epm_floor->rhs.ncacn.minor_version = 0;
1133 return NT_STATUS_OK;
1135 case EPM_PROTOCOL_NCADG:
1136 epm_floor->rhs.ncadg.minor_version = 0;
1137 return NT_STATUS_OK;
1139 case EPM_PROTOCOL_SMB:
1140 epm_floor->rhs.smb.unc = talloc_strdup(mem_ctx, data);
1141 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.smb.unc);
1142 return NT_STATUS_OK;
1144 case EPM_PROTOCOL_NAMED_PIPE:
1145 epm_floor->rhs.named_pipe.path = talloc_strdup(mem_ctx, data);
1146 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.named_pipe.path);
1147 return NT_STATUS_OK;
1149 case EPM_PROTOCOL_NETBIOS:
1150 epm_floor->rhs.netbios.name = talloc_strdup(mem_ctx, data);
1151 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.netbios.name);
1152 return NT_STATUS_OK;
1154 case EPM_PROTOCOL_NCALRPC:
1155 return NT_STATUS_OK;
1157 case EPM_PROTOCOL_VINES_SPP:
1158 epm_floor->rhs.vines_spp.port = atoi(data);
1159 return NT_STATUS_OK;
1161 case EPM_PROTOCOL_VINES_IPC:
1162 epm_floor->rhs.vines_ipc.port = atoi(data);
1163 return NT_STATUS_OK;
1165 case EPM_PROTOCOL_STREETTALK:
1166 epm_floor->rhs.streettalk.streettalk = talloc_strdup(mem_ctx, data);
1167 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.streettalk.streettalk);
1168 return NT_STATUS_OK;
1170 case EPM_PROTOCOL_UNIX_DS:
1171 epm_floor->rhs.unix_ds.path = talloc_strdup(mem_ctx, data);
1172 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.unix_ds.path);
1173 return NT_STATUS_OK;
1175 case EPM_PROTOCOL_NULL:
1176 return NT_STATUS_OK;
1179 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
1183 return NT_STATUS_NOT_SUPPORTED;
1186 enum dcerpc_transport_t dcerpc_transport_by_endpoint_protocol(int prot)
1190 /* Find a transport that has 'prot' as 4th protocol */
1191 for (i=0;i<ARRAY_SIZE(transports);i++) {
1192 if (transports[i].num_protocols >= 2 &&
1193 transports[i].protseq[1] == prot) {
1194 return transports[i].transport;
1198 /* Unknown transport */
1199 return (unsigned int)-1;
1202 _PUBLIC_ enum dcerpc_transport_t dcerpc_transport_by_tower(const struct epm_tower *tower)
1206 /* Find a transport that matches this tower */
1207 for (i=0;i<ARRAY_SIZE(transports);i++) {
1209 if (transports[i].num_protocols != tower->num_floors - 2) {
1213 for (j = 0; j < transports[i].num_protocols; j++) {
1214 if (transports[i].protseq[j] != tower->floors[j+2].lhs.protocol) {
1219 if (j == transports[i].num_protocols) {
1220 return transports[i].transport;
1224 /* Unknown transport */
1225 return (unsigned int)-1;
1228 _PUBLIC_ const char *derpc_transport_string_by_transport(enum dcerpc_transport_t t)
1232 for (i=0; i<ARRAY_SIZE(transports); i++) {
1233 if (t == transports[i].transport) {
1234 return transports[i].name;
1240 _PUBLIC_ enum dcerpc_transport_t dcerpc_transport_by_name(const char *name)
1248 for (i=0; i<ARRAY_SIZE(transports);i++) {
1249 if (strcasecmp(name, transports[i].name) == 0) {
1250 return transports[i].transport;
1257 _PUBLIC_ NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx,
1258 struct epm_tower *tower,
1259 struct dcerpc_binding **b_out)
1262 struct dcerpc_binding *b;
1263 enum dcerpc_transport_t transport;
1264 struct ndr_syntax_id abstract_syntax;
1265 char *endpoint = NULL;
1269 * A tower needs to have at least 4 floors to carry useful
1270 * information. Floor 3 is the transport identifier which defines
1271 * how many floors are required at least.
1273 if (tower->num_floors < 4) {
1274 return NT_STATUS_INVALID_PARAMETER;
1277 status = dcerpc_parse_binding(mem_ctx, "", &b);
1278 if (!NT_STATUS_IS_OK(status)) {
1282 transport = dcerpc_transport_by_tower(tower);
1283 if (transport == NCA_UNKNOWN) {
1285 return NT_STATUS_NOT_SUPPORTED;
1288 status = dcerpc_binding_set_transport(b, transport);
1289 if (!NT_STATUS_IS_OK(status)) {
1294 /* Set abstract syntax */
1295 status = dcerpc_floor_get_lhs_data(&tower->floors[0], &abstract_syntax);
1296 if (!NT_STATUS_IS_OK(status)) {
1301 status = dcerpc_binding_set_abstract_syntax(b, &abstract_syntax);
1302 if (!NT_STATUS_IS_OK(status)) {
1307 /* Ignore floor 1, it contains the NDR version info */
1311 if (tower->num_floors >= 4) {
1312 endpoint = dcerpc_floor_get_rhs_data(b, &tower->floors[3]);
1315 int saved_errno = errno;
1317 return map_nt_error_from_unix_common(saved_errno);
1320 status = dcerpc_binding_set_string_option(b, "endpoint", endpoint);
1321 if (!NT_STATUS_IS_OK(status)) {
1325 TALLOC_FREE(endpoint);
1327 /* Set network address */
1329 if (tower->num_floors >= 5) {
1330 host = dcerpc_floor_get_rhs_data(b, &tower->floors[4]);
1333 int saved_errno = errno;
1335 return map_nt_error_from_unix_common(saved_errno);
1338 status = dcerpc_binding_set_string_option(b, "host", host);
1339 if (!NT_STATUS_IS_OK(status)) {
1343 status = dcerpc_binding_set_string_option(b, "target_hostname", host);
1344 if (!NT_STATUS_IS_OK(status)) {
1351 return NT_STATUS_OK;
1354 _PUBLIC_ struct dcerpc_binding *dcerpc_binding_dup(TALLOC_CTX *mem_ctx,
1355 const struct dcerpc_binding *b)
1357 struct dcerpc_binding *n;
1360 n = talloc_zero(mem_ctx, struct dcerpc_binding);
1365 n->transport = b->transport;
1366 n->object = b->object;
1367 n->flags = b->flags;
1368 n->assoc_group_id = b->assoc_group_id;
1370 if (b->object_string != NULL) {
1371 n->object_string = talloc_strdup(n, b->object_string);
1372 if (n->object_string == NULL) {
1377 if (b->host != NULL) {
1378 n->host = talloc_strdup(n, b->host);
1379 if (n->host == NULL) {
1385 if (b->target_hostname != NULL) {
1386 n->target_hostname = talloc_strdup(n, b->target_hostname);
1387 if (n->target_hostname == NULL) {
1393 if (b->target_principal != NULL) {
1394 n->target_principal = talloc_strdup(n, b->target_principal);
1395 if (n->target_principal == NULL) {
1401 if (b->endpoint != NULL) {
1402 n->endpoint = talloc_strdup(n, b->endpoint);
1403 if (n->endpoint == NULL) {
1409 for (count = 0; b->options && b->options[count]; count++);
1414 n->options = talloc_array(n, const char *, count + 1);
1415 if (n->options == NULL) {
1420 for (i = 0; i < count; i++) {
1421 n->options[i] = talloc_strdup(n->options, b->options[i]);
1422 if (n->options[i] == NULL) {
1427 n->options[count] = NULL;
1433 _PUBLIC_ NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx,
1434 const struct dcerpc_binding *binding,
1435 struct epm_tower *tower)
1437 const enum epm_protocol *protseq = NULL;
1438 int num_protocols = -1, i;
1439 struct ndr_syntax_id abstract_syntax;
1442 /* Find transport */
1443 for (i=0;i<ARRAY_SIZE(transports);i++) {
1444 if (transports[i].transport == binding->transport) {
1445 protseq = transports[i].protseq;
1446 num_protocols = transports[i].num_protocols;
1451 if (num_protocols == -1) {
1452 DEBUG(0, ("Unable to find transport with id '%d'\n", binding->transport));
1453 return NT_STATUS_UNSUCCESSFUL;
1456 tower->num_floors = 2 + num_protocols;
1457 tower->floors = talloc_array(mem_ctx, struct epm_floor, tower->num_floors);
1460 tower->floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
1462 abstract_syntax = dcerpc_binding_get_abstract_syntax(binding);
1463 tower->floors[0].lhs.lhs_data = dcerpc_floor_pack_lhs_data(tower->floors,
1466 if (!dcerpc_floor_pack_rhs_if_version_data(
1467 tower->floors, &abstract_syntax,
1468 &tower->floors[0].rhs.uuid.unknown)) {
1469 return NT_STATUS_NO_MEMORY;
1473 tower->floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
1475 tower->floors[1].lhs.lhs_data = dcerpc_floor_pack_lhs_data(tower->floors,
1476 &ndr_transfer_syntax_ndr);
1478 tower->floors[1].rhs.uuid.unknown = data_blob_talloc_zero(tower->floors, 2);
1480 /* Floor 2 to num_protocols */
1481 for (i = 0; i < num_protocols; i++) {
1482 tower->floors[2 + i].lhs.protocol = protseq[i];
1483 tower->floors[2 + i].lhs.lhs_data = data_blob_null;
1484 ZERO_STRUCT(tower->floors[2 + i].rhs);
1485 status = dcerpc_floor_set_rhs_data(tower->floors,
1486 &tower->floors[2 + i],
1488 if (!NT_STATUS_IS_OK(status)) {
1493 /* The 4th floor contains the endpoint */
1494 if (num_protocols >= 2 && binding->endpoint) {
1495 status = dcerpc_floor_set_rhs_data(tower->floors,
1498 if (!NT_STATUS_IS_OK(status)) {
1503 /* The 5th contains the network address */
1504 if (num_protocols >= 3 && binding->host) {
1505 status = dcerpc_floor_set_rhs_data(tower->floors,
1508 if (!NT_STATUS_IS_OK(status)) {
1513 return NT_STATUS_OK;