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 | 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)
116 for (i=0; i<ARRAY_SIZE(ncacn_options); i++) {
119 ret = strcasecmp(ncacn_options[i].name, name);
124 return &ncacn_options[i];
130 const char *epm_floor_string(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
132 struct ndr_syntax_id syntax;
135 switch(epm_floor->lhs.protocol) {
136 case EPM_PROTOCOL_UUID:
137 status = dcerpc_floor_get_lhs_data(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)) {
146 if (GUID_equal(&syntax.uuid, &ndr_transfer_syntax_ndr64.uuid)) {
150 return talloc_asprintf(
153 GUID_buf_string(&syntax.uuid, &buf),
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:
163 case EPM_PROTOCOL_NCADG:
166 case EPM_PROTOCOL_NCALRPC:
169 case EPM_PROTOCOL_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:
190 case EPM_PROTOCOL_SPX:
193 case EPM_PROTOCOL_NB_IPX:
196 case EPM_PROTOCOL_HTTP:
197 return talloc_asprintf(mem_ctx, "HTTP:%d", epm_floor->rhs.http.port);
199 case EPM_PROTOCOL_TCP:
200 return talloc_asprintf(mem_ctx, "TCP:%d", epm_floor->rhs.tcp.port);
202 case EPM_PROTOCOL_UDP:
203 return talloc_asprintf(mem_ctx, "UDP:%d", epm_floor->rhs.udp.port);
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 = talloc_strdup(mem_ctx, "");
219 const char *t_name = NULL;
220 bool option_section = false;
221 const char *target_hostname = NULL;
223 if (b->transport != NCA_UNKNOWN) {
224 t_name = derpc_transport_string_by_transport(b->transport);
231 if (!GUID_all_zero(&b->object)) {
232 struct GUID_txt_buf buf;
235 s = talloc_asprintf_append_buffer(
236 s, "%s@", GUID_buf_string(&b->object, &buf));
243 if (t_name != NULL) {
245 s = talloc_asprintf_append_buffer(s, "%s:", t_name);
254 s = talloc_asprintf_append_buffer(s, "%s", b->host);
261 target_hostname = b->target_hostname;
262 if (target_hostname != NULL && b->host != NULL) {
263 if (strcmp(target_hostname, b->host) == 0) {
264 target_hostname = NULL;
269 (b->endpoint != NULL) ||
270 (target_hostname != NULL) ||
271 (b->target_principal != NULL) ||
272 (b->assoc_group_id != 0) ||
273 (b->options != NULL) ||
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;
386 char *q = p + strlen(p) - 1;
389 return NT_STATUS_INVALID_PARAMETER_MIX;
398 if (p && PTR_DIFF(p, s) == 36) { /* 36 is the length of a UUID */
401 status = dcerpc_binding_set_string_option(b, "object", s);
402 if (!NT_STATUS_IS_OK(status)) {
413 b->transport = NCA_UNKNOWN;
414 } else if (is_ipaddress_v6(s)) {
415 b->transport = NCA_UNKNOWN;
419 status = dcerpc_binding_set_string_option(b, "transport", s);
420 if (!NT_STATUS_IS_OK(status)) {
429 status = dcerpc_binding_set_string_option(b, "host", s);
430 if (!NT_STATUS_IS_OK(status)) {
435 b->target_hostname = talloc_strdup(b, b->host);
436 if (b->target_hostname == NULL) {
438 return NT_STATUS_NO_MEMORY;
442 for (i=0; options != NULL; i++) {
443 const char *name = options;
444 const char *value = NULL;
446 p = strchr(options, ',');
454 p = strchr(name, '=');
462 * If it's not a key=value pair
463 * it might be a ncacn_option
464 * or if it's the first option
467 const struct ncacn_option *no = NULL;
471 no = ncacn_option_by_name(name);
475 * we don't allow unknown options
477 return NT_STATUS_INVALID_PARAMETER_MIX;
481 * This is the endpoint
484 if (strlen(value) == 0) {
490 status = dcerpc_binding_set_string_option(b, name, value);
491 if (!NT_STATUS_IS_OK(status)) {
502 _PUBLIC_ struct GUID dcerpc_binding_get_object(const struct dcerpc_binding *b)
507 _PUBLIC_ NTSTATUS dcerpc_binding_set_object(struct dcerpc_binding *b,
510 char *tmp = discard_const_p(char, b->object_string);
512 if (GUID_all_zero(&object)) {
514 b->object_string = NULL;
515 ZERO_STRUCT(b->object);
519 b->object_string = GUID_string(b, &object);
520 if (b->object_string == NULL) {
521 b->object_string = tmp;
522 return NT_STATUS_NO_MEMORY;
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 (b->flags & DCERPC_PACKET) {
595 auth_level = DCERPC_AUTH_LEVEL_PACKET;
596 } else if (auth_type != DCERPC_AUTH_TYPE_NONE) {
597 auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
599 auth_level = DCERPC_AUTH_LEVEL_NONE;
602 if (_auth_type != NULL) {
603 *_auth_type = auth_type;
606 if (_auth_level != NULL) {
607 *_auth_level = auth_level;
611 _PUBLIC_ uint32_t dcerpc_binding_get_assoc_group_id(const struct dcerpc_binding *b)
613 return b->assoc_group_id;
616 _PUBLIC_ NTSTATUS dcerpc_binding_set_assoc_group_id(struct dcerpc_binding *b,
617 uint32_t assoc_group_id)
619 b->assoc_group_id = assoc_group_id;
623 _PUBLIC_ struct ndr_syntax_id dcerpc_binding_get_abstract_syntax(const struct dcerpc_binding *b)
625 const char *s = dcerpc_binding_get_string_option(b, "abstract_syntax");
627 struct ndr_syntax_id id;
630 return ndr_syntax_id_null;
633 ok = ndr_syntax_id_from_string(s, &id);
635 return ndr_syntax_id_null;
641 _PUBLIC_ NTSTATUS dcerpc_binding_set_abstract_syntax(struct dcerpc_binding *b,
642 const struct ndr_syntax_id *syntax)
645 struct ndr_syntax_id_buf buf;
647 if (syntax == NULL) {
648 status = dcerpc_binding_set_string_option(b, "abstract_syntax", NULL);
652 if (ndr_syntax_id_equal(&ndr_syntax_id_null, syntax)) {
653 status = dcerpc_binding_set_string_option(b, "abstract_syntax", NULL);
657 status = dcerpc_binding_set_string_option(
658 b, "abstract_syntax", ndr_syntax_id_buf_string(syntax, &buf));
662 _PUBLIC_ const char *dcerpc_binding_get_string_option(const struct dcerpc_binding *b,
668 #define _SPECIAL(x) { .name = #x, .value = b->x, }
670 { .name = "object", .value = b->object_string, },
673 _SPECIAL(target_hostname),
674 _SPECIAL(target_principal),
677 const struct ncacn_option *no = NULL;
678 size_t name_len = strlen(name);
682 ret = strcmp(name, "transport");
684 return derpc_transport_string_by_transport(b->transport);
687 ret = strcmp(name, "assoc_group_id");
689 char *tmp = discard_const_p(char, b->assoc_group_string);
691 if (b->assoc_group_id == 0) {
695 snprintf(tmp, sizeof(b->assoc_group_string),
696 "0x%08x", b->assoc_group_id);
697 return (const char *)b->assoc_group_string;
700 for (i=0; i < ARRAY_SIZE(specials); i++) {
701 ret = strcmp(specials[i].name, name);
706 return specials[i].value;
709 no = ncacn_option_by_name(name);
711 if (b->flags & no->flag) {
718 if (b->options == NULL) {
722 for (i=0; b->options[i]; i++) {
723 const char *o = b->options[i];
724 const char *vs = NULL;
726 ret = strncmp(name, o, name_len);
731 if (o[name_len] != '=') {
735 vs = &o[name_len + 1];
743 _PUBLIC_ char *dcerpc_binding_copy_string_option(TALLOC_CTX *mem_ctx,
744 const struct dcerpc_binding *b,
747 const char *c = dcerpc_binding_get_string_option(b, name);
755 v = talloc_strdup(mem_ctx, c);
764 _PUBLIC_ NTSTATUS dcerpc_binding_set_string_option(struct dcerpc_binding *b,
771 #define _SPECIAL(x) { .name = #x, .ptr = &b->x, }
775 _SPECIAL(target_hostname),
776 _SPECIAL(target_principal),
779 const struct ncacn_option *no = NULL;
780 size_t name_len = strlen(name);
781 const char *opt = NULL;
787 * Note: value == NULL, means delete it.
788 * value != NULL means add or reset.
791 ret = strcmp(name, "transport");
793 enum dcerpc_transport_t t = dcerpc_transport_by_name(value);
795 if (t == NCA_UNKNOWN && value != NULL) {
796 return NT_STATUS_INVALID_PARAMETER_MIX;
799 return dcerpc_binding_set_transport(b, t);
802 ret = strcmp(name, "object");
805 struct GUID uuid = GUID_zero();
809 blob = data_blob_string_const(value);
810 if (blob.length != 36) {
811 return NT_STATUS_INVALID_PARAMETER_MIX;
814 status = GUID_from_data_blob(&blob, &uuid);
815 if (!NT_STATUS_IS_OK(status)) {
820 return dcerpc_binding_set_object(b, uuid);
823 ret = strcmp(name, "assoc_group_id");
825 uint32_t assoc_group_id = 0;
830 ret = sscanf(value, "0x%08x%c", &assoc_group_id, &c);
832 return NT_STATUS_INVALID_PARAMETER_MIX;
836 return dcerpc_binding_set_assoc_group_id(b, assoc_group_id);
839 for (i=0; i < ARRAY_SIZE(specials); i++) {
840 ret = strcmp(specials[i].name, name);
845 tmp = discard_const_p(char, *specials[i].ptr);
849 *specials[i].ptr = NULL;
853 if (value[0] == '\0') {
854 return NT_STATUS_INVALID_PARAMETER_MIX;
857 *specials[i].ptr = talloc_strdup(b, value);
858 if (*specials[i].ptr == NULL) {
859 *specials[i].ptr = tmp;
860 return NT_STATUS_NO_MEMORY;
867 no = ncacn_option_by_name(name);
870 b->flags &= ~no->flag;
874 ret = strcasecmp(no->name, value);
876 return NT_STATUS_INVALID_PARAMETER_MIX;
879 b->flags |= no->flag;
883 for (i=0; b->options && b->options[i]; i++) {
884 const char *o = b->options[i];
886 ret = strncmp(name, o, name_len);
891 if (o[name_len] != '=') {
906 n = talloc_realloc(b, b->options, const char *, i + 2);
908 return NT_STATUS_NO_MEMORY;
915 tmp = discard_const_p(char, opt);
918 for (;b->options[i];i++) {
919 b->options[i] = b->options[i+1];
925 b->options[i] = talloc_asprintf(b->options, "%s=%s",
927 if (b->options[i] == NULL) {
929 return NT_STATUS_NO_MEMORY;
935 _PUBLIC_ uint32_t dcerpc_binding_get_flags(const struct dcerpc_binding *b)
940 _PUBLIC_ NTSTATUS dcerpc_binding_set_flags(struct dcerpc_binding *b,
945 * TODO: in future we may want to reject invalid combinations
948 b->flags |= additional;
953 _PUBLIC_ NTSTATUS dcerpc_floor_get_lhs_data(const struct epm_floor *epm_floor,
954 struct ndr_syntax_id *syntax)
956 TALLOC_CTX *mem_ctx = talloc_init("floor_get_lhs_data");
957 struct ndr_pull *ndr;
958 enum ndr_err_code ndr_err;
959 uint16_t if_version=0;
961 ndr = ndr_pull_init_blob(&epm_floor->lhs.lhs_data, mem_ctx);
963 talloc_free(mem_ctx);
964 return NT_STATUS_NO_MEMORY;
966 ndr->flags |= LIBNDR_FLAG_NOALIGN;
968 ndr_err = ndr_pull_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
969 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
970 talloc_free(mem_ctx);
971 return ndr_map_error2ntstatus(ndr_err);
974 ndr_err = ndr_pull_uint16(ndr, NDR_SCALARS, &if_version);
975 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
976 talloc_free(mem_ctx);
977 return ndr_map_error2ntstatus(ndr_err);
980 syntax->if_version = if_version;
982 talloc_free(mem_ctx);
987 static DATA_BLOB dcerpc_floor_pack_lhs_data(TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax)
990 enum ndr_err_code ndr_err;
991 struct ndr_push *ndr;
993 ndr = ndr_push_init_ctx(mem_ctx);
995 return data_blob_null;
998 ndr->flags |= LIBNDR_FLAG_NOALIGN;
1000 ndr_err = ndr_push_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
1001 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1002 return data_blob_null;
1004 ndr_err = ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version);
1005 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1006 return data_blob_null;
1009 blob = ndr_push_blob(ndr);
1010 talloc_steal(mem_ctx, blob.data);
1015 static bool dcerpc_floor_pack_rhs_if_version_data(
1016 TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax,
1020 struct ndr_push *ndr = ndr_push_init_ctx(mem_ctx);
1021 enum ndr_err_code ndr_err;
1027 ndr->flags |= LIBNDR_FLAG_NOALIGN;
1029 ndr_err = ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version >> 16);
1030 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1034 blob = ndr_push_blob(ndr);
1035 talloc_steal(mem_ctx, blob.data);
1041 char *dcerpc_floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
1043 switch (epm_floor->lhs.protocol) {
1044 case EPM_PROTOCOL_TCP:
1045 if (epm_floor->rhs.tcp.port == 0) return NULL;
1046 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.tcp.port);
1048 case EPM_PROTOCOL_UDP:
1049 if (epm_floor->rhs.udp.port == 0) return NULL;
1050 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.udp.port);
1052 case EPM_PROTOCOL_HTTP:
1053 if (epm_floor->rhs.http.port == 0) return NULL;
1054 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.http.port);
1056 case EPM_PROTOCOL_IP:
1057 return talloc_strdup(mem_ctx, epm_floor->rhs.ip.ipaddr);
1059 case EPM_PROTOCOL_NCACN:
1062 case EPM_PROTOCOL_NCADG:
1065 case EPM_PROTOCOL_SMB:
1066 if (strlen(epm_floor->rhs.smb.unc) == 0) return NULL;
1067 return talloc_strdup(mem_ctx, epm_floor->rhs.smb.unc);
1069 case EPM_PROTOCOL_NAMED_PIPE:
1070 if (strlen(epm_floor->rhs.named_pipe.path) == 0) return NULL;
1071 return talloc_strdup(mem_ctx, epm_floor->rhs.named_pipe.path);
1073 case EPM_PROTOCOL_NETBIOS:
1074 if (strlen(epm_floor->rhs.netbios.name) == 0) return NULL;
1075 return talloc_strdup(mem_ctx, epm_floor->rhs.netbios.name);
1077 case EPM_PROTOCOL_NCALRPC:
1080 case EPM_PROTOCOL_VINES_SPP:
1081 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_spp.port);
1083 case EPM_PROTOCOL_VINES_IPC:
1084 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_ipc.port);
1086 case EPM_PROTOCOL_STREETTALK:
1087 return talloc_strdup(mem_ctx, epm_floor->rhs.streettalk.streettalk);
1089 case EPM_PROTOCOL_UNIX_DS:
1090 if (strlen(epm_floor->rhs.unix_ds.path) == 0) return NULL;
1091 return talloc_strdup(mem_ctx, epm_floor->rhs.unix_ds.path);
1093 case EPM_PROTOCOL_NULL:
1097 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
1104 static NTSTATUS dcerpc_floor_set_rhs_data(TALLOC_CTX *mem_ctx,
1105 struct epm_floor *epm_floor,
1112 switch (epm_floor->lhs.protocol) {
1113 case EPM_PROTOCOL_TCP:
1114 epm_floor->rhs.tcp.port = atoi(data);
1115 return NT_STATUS_OK;
1117 case EPM_PROTOCOL_UDP:
1118 epm_floor->rhs.udp.port = atoi(data);
1119 return NT_STATUS_OK;
1121 case EPM_PROTOCOL_HTTP:
1122 epm_floor->rhs.http.port = atoi(data);
1123 return NT_STATUS_OK;
1125 case EPM_PROTOCOL_IP:
1126 if (!is_ipaddress_v4(data)) {
1129 epm_floor->rhs.ip.ipaddr = talloc_strdup(mem_ctx, data);
1130 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.ip.ipaddr);
1131 return NT_STATUS_OK;
1133 case EPM_PROTOCOL_NCACN:
1134 epm_floor->rhs.ncacn.minor_version = 0;
1135 return NT_STATUS_OK;
1137 case EPM_PROTOCOL_NCADG:
1138 epm_floor->rhs.ncadg.minor_version = 0;
1139 return NT_STATUS_OK;
1141 case EPM_PROTOCOL_SMB:
1142 epm_floor->rhs.smb.unc = talloc_strdup(mem_ctx, data);
1143 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.smb.unc);
1144 return NT_STATUS_OK;
1146 case EPM_PROTOCOL_NAMED_PIPE:
1147 epm_floor->rhs.named_pipe.path = talloc_strdup(mem_ctx, data);
1148 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.named_pipe.path);
1149 return NT_STATUS_OK;
1151 case EPM_PROTOCOL_NETBIOS:
1152 epm_floor->rhs.netbios.name = talloc_strdup(mem_ctx, data);
1153 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.netbios.name);
1154 return NT_STATUS_OK;
1156 case EPM_PROTOCOL_NCALRPC:
1157 return NT_STATUS_OK;
1159 case EPM_PROTOCOL_VINES_SPP:
1160 epm_floor->rhs.vines_spp.port = atoi(data);
1161 return NT_STATUS_OK;
1163 case EPM_PROTOCOL_VINES_IPC:
1164 epm_floor->rhs.vines_ipc.port = atoi(data);
1165 return NT_STATUS_OK;
1167 case EPM_PROTOCOL_STREETTALK:
1168 epm_floor->rhs.streettalk.streettalk = talloc_strdup(mem_ctx, data);
1169 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.streettalk.streettalk);
1170 return NT_STATUS_OK;
1172 case EPM_PROTOCOL_UNIX_DS:
1173 epm_floor->rhs.unix_ds.path = talloc_strdup(mem_ctx, data);
1174 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.unix_ds.path);
1175 return NT_STATUS_OK;
1177 case EPM_PROTOCOL_NULL:
1178 return NT_STATUS_OK;
1181 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
1185 return NT_STATUS_NOT_SUPPORTED;
1188 enum dcerpc_transport_t dcerpc_transport_by_endpoint_protocol(int prot)
1192 /* Find a transport that has 'prot' as 4th protocol */
1193 for (i=0;i<ARRAY_SIZE(transports);i++) {
1194 if (transports[i].num_protocols >= 2 &&
1195 transports[i].protseq[1] == prot) {
1196 return transports[i].transport;
1200 /* Unknown transport */
1201 return (unsigned int)-1;
1204 _PUBLIC_ enum dcerpc_transport_t dcerpc_transport_by_tower(const struct epm_tower *tower)
1208 /* Find a transport that matches this tower */
1209 for (i=0;i<ARRAY_SIZE(transports);i++) {
1211 if (transports[i].num_protocols != tower->num_floors - 2) {
1215 for (j = 0; j < transports[i].num_protocols && j < MAX_PROTSEQ; j++) {
1216 if (transports[i].protseq[j] != tower->floors[j+2].lhs.protocol) {
1221 if (j == transports[i].num_protocols) {
1222 return transports[i].transport;
1226 /* Unknown transport */
1227 return (unsigned int)-1;
1230 _PUBLIC_ const char *derpc_transport_string_by_transport(enum dcerpc_transport_t t)
1234 for (i=0; i<ARRAY_SIZE(transports); i++) {
1235 if (t == transports[i].transport) {
1236 return transports[i].name;
1242 _PUBLIC_ enum dcerpc_transport_t dcerpc_transport_by_name(const char *name)
1250 for (i=0; i<ARRAY_SIZE(transports);i++) {
1251 if (strcasecmp(name, transports[i].name) == 0) {
1252 return transports[i].transport;
1259 _PUBLIC_ NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx,
1260 struct epm_tower *tower,
1261 struct dcerpc_binding **b_out)
1264 struct dcerpc_binding *b;
1265 enum dcerpc_transport_t transport;
1266 struct ndr_syntax_id abstract_syntax;
1267 char *endpoint = NULL;
1271 * A tower needs to have at least 4 floors to carry useful
1272 * information. Floor 3 is the transport identifier which defines
1273 * how many floors are required at least.
1275 if (tower->num_floors < 4) {
1276 return NT_STATUS_INVALID_PARAMETER;
1279 status = dcerpc_parse_binding(mem_ctx, "", &b);
1280 if (!NT_STATUS_IS_OK(status)) {
1284 transport = dcerpc_transport_by_tower(tower);
1285 if (transport == NCA_UNKNOWN) {
1287 return NT_STATUS_NOT_SUPPORTED;
1290 status = dcerpc_binding_set_transport(b, transport);
1291 if (!NT_STATUS_IS_OK(status)) {
1296 /* Set abstract syntax */
1297 status = dcerpc_floor_get_lhs_data(&tower->floors[0], &abstract_syntax);
1298 if (!NT_STATUS_IS_OK(status)) {
1303 status = dcerpc_binding_set_abstract_syntax(b, &abstract_syntax);
1304 if (!NT_STATUS_IS_OK(status)) {
1309 /* Ignore floor 1, it contains the NDR version info */
1313 if (tower->num_floors >= 4) {
1314 endpoint = dcerpc_floor_get_rhs_data(b, &tower->floors[3]);
1317 int saved_errno = errno;
1319 return map_nt_error_from_unix_common(saved_errno);
1322 status = dcerpc_binding_set_string_option(b, "endpoint", endpoint);
1323 if (!NT_STATUS_IS_OK(status)) {
1327 TALLOC_FREE(endpoint);
1329 /* Set network address */
1331 if (tower->num_floors >= 5) {
1332 host = dcerpc_floor_get_rhs_data(b, &tower->floors[4]);
1335 int saved_errno = errno;
1337 return map_nt_error_from_unix_common(saved_errno);
1340 status = dcerpc_binding_set_string_option(b, "host", host);
1341 if (!NT_STATUS_IS_OK(status)) {
1345 status = dcerpc_binding_set_string_option(b, "target_hostname", host);
1346 if (!NT_STATUS_IS_OK(status)) {
1353 return NT_STATUS_OK;
1356 _PUBLIC_ struct dcerpc_binding *dcerpc_binding_dup(TALLOC_CTX *mem_ctx,
1357 const struct dcerpc_binding *b)
1359 struct dcerpc_binding *n;
1362 n = talloc_zero(mem_ctx, struct dcerpc_binding);
1367 n->transport = b->transport;
1368 n->object = b->object;
1369 n->flags = b->flags;
1370 n->assoc_group_id = b->assoc_group_id;
1372 if (b->object_string != NULL) {
1373 n->object_string = talloc_strdup(n, b->object_string);
1374 if (n->object_string == NULL) {
1379 if (b->host != NULL) {
1380 n->host = talloc_strdup(n, b->host);
1381 if (n->host == NULL) {
1387 if (b->target_hostname != NULL) {
1388 n->target_hostname = talloc_strdup(n, b->target_hostname);
1389 if (n->target_hostname == NULL) {
1395 if (b->target_principal != NULL) {
1396 n->target_principal = talloc_strdup(n, b->target_principal);
1397 if (n->target_principal == NULL) {
1403 if (b->endpoint != NULL) {
1404 n->endpoint = talloc_strdup(n, b->endpoint);
1405 if (n->endpoint == NULL) {
1411 for (count = 0; b->options && b->options[count]; count++);
1416 n->options = talloc_array(n, const char *, count + 1);
1417 if (n->options == NULL) {
1422 for (i = 0; i < count; i++) {
1423 n->options[i] = talloc_strdup(n->options, b->options[i]);
1424 if (n->options[i] == NULL) {
1429 n->options[count] = NULL;
1435 _PUBLIC_ NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx,
1436 const struct dcerpc_binding *binding,
1437 struct epm_tower *tower)
1439 const enum epm_protocol *protseq = NULL;
1440 size_t i, num_protocols;
1441 struct ndr_syntax_id abstract_syntax;
1444 /* Find transport */
1445 for (i=0;i<ARRAY_SIZE(transports);i++) {
1446 if (transports[i].transport == binding->transport) {
1447 protseq = transports[i].protseq;
1448 num_protocols = transports[i].num_protocols;
1453 if (i == ARRAY_SIZE(transports)) {
1454 DEBUG(0, ("Unable to find transport with id '%d'\n", binding->transport));
1455 return NT_STATUS_UNSUCCESSFUL;
1458 tower->num_floors = 2 + num_protocols;
1459 tower->floors = talloc_array(mem_ctx, struct epm_floor, tower->num_floors);
1460 if (tower->floors == NULL) {
1461 return NT_STATUS_NO_MEMORY;
1465 tower->floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
1467 abstract_syntax = dcerpc_binding_get_abstract_syntax(binding);
1468 tower->floors[0].lhs.lhs_data = dcerpc_floor_pack_lhs_data(tower->floors,
1471 if (!dcerpc_floor_pack_rhs_if_version_data(
1472 tower->floors, &abstract_syntax,
1473 &tower->floors[0].rhs.uuid.unknown)) {
1474 return NT_STATUS_NO_MEMORY;
1478 tower->floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
1480 tower->floors[1].lhs.lhs_data = dcerpc_floor_pack_lhs_data(tower->floors,
1481 &ndr_transfer_syntax_ndr);
1483 tower->floors[1].rhs.uuid.unknown = data_blob_talloc_zero(tower->floors, 2);
1485 /* Floor 2 to num_protocols */
1486 for (i = 0; i < num_protocols; i++) {
1487 tower->floors[2 + i].lhs.protocol = protseq[i];
1488 tower->floors[2 + i].lhs.lhs_data = data_blob_null;
1489 ZERO_STRUCT(tower->floors[2 + i].rhs);
1490 status = dcerpc_floor_set_rhs_data(tower->floors,
1491 &tower->floors[2 + i],
1493 if (!NT_STATUS_IS_OK(status)) {
1498 /* The 4th floor contains the endpoint */
1499 if (num_protocols >= 2 && binding->endpoint) {
1500 status = dcerpc_floor_set_rhs_data(tower->floors,
1503 if (!NT_STATUS_IS_OK(status)) {
1508 /* The 5th contains the network address */
1509 if (num_protocols >= 3 && binding->host) {
1510 status = dcerpc_floor_set_rhs_data(tower->floors,
1513 if (!NT_STATUS_IS_OK(status)) {
1518 return NT_STATUS_OK;