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
40 enum dcerpc_transport_t transport;
42 enum epm_protocol protseq[MAX_PROTSEQ];
44 { "ncacn_np", NCACN_NP, 3,
45 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SMB, EPM_PROTOCOL_NETBIOS }},
46 { "ncacn_ip_tcp", NCACN_IP_TCP, 3,
47 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_TCP, EPM_PROTOCOL_IP } },
48 { "ncacn_http", NCACN_HTTP, 3,
49 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_HTTP, EPM_PROTOCOL_IP } },
50 { "ncadg_ip_udp", NCACN_IP_UDP, 3,
51 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UDP, EPM_PROTOCOL_IP } },
52 { "ncalrpc", NCALRPC, 2,
53 { EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_NAMED_PIPE } },
54 { "ncacn_unix_stream", NCACN_UNIX_STREAM, 2,
55 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_UNIX_DS } },
56 { "ncadg_unix_dgram", NCADG_UNIX_DGRAM, 2,
57 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UNIX_DS } },
58 { "ncacn_at_dsp", NCACN_AT_DSP, 3,
59 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DSP } },
60 { "ncadg_at_ddp", NCADG_AT_DDP, 3,
61 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DDP } },
62 { "ncacn_vns_ssp", NCACN_VNS_SPP, 3,
63 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_SPP } },
64 { "ncacn_vns_ipc", NCACN_VNS_IPC, 3,
65 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_IPC }, },
66 { "ncadg_ipx", NCADG_IPX, 2,
67 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_IPX },
69 { "ncacn_spx", NCACN_SPX, 3,
70 /* I guess some MS programmer confused the identifier for
71 * EPM_PROTOCOL_UUID (0x0D or 13) with the one for
72 * EPM_PROTOCOL_SPX (0x13) here. -- jelmer*/
73 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_UUID },
77 static const struct ncacn_option {
81 {"sign", DCERPC_SIGN},
82 {"seal", DCERPC_SEAL},
83 {"connect", DCERPC_CONNECT},
84 {"spnego", DCERPC_AUTH_SPNEGO},
85 {"ntlm", DCERPC_AUTH_NTLM},
86 {"krb5", DCERPC_AUTH_KRB5},
87 {"validate", DCERPC_DEBUG_VALIDATE_BOTH},
88 {"print", DCERPC_DEBUG_PRINT_BOTH},
89 {"padcheck", DCERPC_DEBUG_PAD_CHECK},
90 {"bigendian", DCERPC_PUSH_BIGENDIAN},
91 {"smb2", DCERPC_SMB2},
92 {"ndr64", DCERPC_NDR64},
95 static const struct ncacn_option *ncacn_option_by_name(const char *name)
99 for (i=0; i<ARRAY_SIZE(ncacn_options); i++) {
102 ret = strcasecmp(ncacn_options[i].name, name);
107 return &ncacn_options[i];
113 const char *epm_floor_string(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
115 struct ndr_syntax_id syntax;
118 switch(epm_floor->lhs.protocol) {
119 case EPM_PROTOCOL_UUID:
120 status = dcerpc_floor_get_lhs_data(epm_floor, &syntax);
121 if (NT_STATUS_IS_OK(status)) {
122 /* lhs is used: UUID */
125 if (GUID_equal(&syntax.uuid, &ndr_transfer_syntax_ndr.uuid)) {
129 if (GUID_equal(&syntax.uuid, &ndr_transfer_syntax_ndr64.uuid)) {
133 uuidstr = GUID_string(mem_ctx, &syntax.uuid);
135 return talloc_asprintf(mem_ctx, " uuid %s/0x%02x", uuidstr, syntax.if_version);
137 return talloc_asprintf(mem_ctx, "IPX:%s",
138 data_blob_hex_string_upper(mem_ctx, &epm_floor->rhs.uuid.unknown));
141 case EPM_PROTOCOL_NCACN:
144 case EPM_PROTOCOL_NCADG:
147 case EPM_PROTOCOL_NCALRPC:
150 case EPM_PROTOCOL_DNET_NSP:
153 case EPM_PROTOCOL_IP:
154 return talloc_asprintf(mem_ctx, "IP:%s", epm_floor->rhs.ip.ipaddr);
156 case EPM_PROTOCOL_NAMED_PIPE:
157 return talloc_asprintf(mem_ctx, "NAMED-PIPE:%s", epm_floor->rhs.named_pipe.path);
159 case EPM_PROTOCOL_SMB:
160 return talloc_asprintf(mem_ctx, "SMB:%s", epm_floor->rhs.smb.unc);
162 case EPM_PROTOCOL_UNIX_DS:
163 return talloc_asprintf(mem_ctx, "Unix:%s", epm_floor->rhs.unix_ds.path);
165 case EPM_PROTOCOL_NETBIOS:
166 return talloc_asprintf(mem_ctx, "NetBIOS:%s", epm_floor->rhs.netbios.name);
168 case EPM_PROTOCOL_NETBEUI:
171 case EPM_PROTOCOL_SPX:
174 case EPM_PROTOCOL_NB_IPX:
177 case EPM_PROTOCOL_HTTP:
178 return talloc_asprintf(mem_ctx, "HTTP:%d", epm_floor->rhs.http.port);
180 case EPM_PROTOCOL_TCP:
181 return talloc_asprintf(mem_ctx, "TCP:%d", epm_floor->rhs.tcp.port);
183 case EPM_PROTOCOL_UDP:
184 return talloc_asprintf(mem_ctx, "UDP:%d", epm_floor->rhs.udp.port);
187 return talloc_asprintf(mem_ctx, "UNK(%02x):", epm_floor->lhs.protocol);
193 form a binding string from a binding structure
195 _PUBLIC_ char *dcerpc_binding_string(TALLOC_CTX *mem_ctx, const struct dcerpc_binding *b)
197 char *s = talloc_strdup(mem_ctx, "");
200 const char *t_name = NULL;
201 bool option_section = false;
202 const char *target_hostname = NULL;
204 if (b->transport != NCA_UNKNOWN) {
205 t_name = derpc_transport_string_by_transport(b->transport);
212 if (!GUID_all_zero(&b->object.uuid)) {
214 s = talloc_asprintf_append_buffer(s, "%s@",
215 GUID_string(mem_ctx, &b->object.uuid));
222 if (t_name != NULL) {
224 s = talloc_asprintf_append_buffer(s, "%s:", t_name);
233 s = talloc_asprintf_append_buffer(s, "%s", b->host);
240 target_hostname = b->target_hostname;
241 if (target_hostname != NULL && b->host != NULL) {
242 if (strcmp(target_hostname, b->host) == 0) {
243 target_hostname = NULL;
248 option_section = true;
249 } else if (target_hostname) {
250 option_section = true;
251 } else if (b->target_principal) {
252 option_section = true;
253 } else if (b->assoc_group_id != 0) {
254 option_section = true;
255 } else if (b->options) {
256 option_section = true;
257 } else if (b->flags) {
258 option_section = true;
261 if (!option_section) {
266 s = talloc_asprintf_append_buffer(s, "[");
274 s = talloc_asprintf_append_buffer(s, "%s", b->endpoint);
281 for (i=0;i<ARRAY_SIZE(ncacn_options);i++) {
282 if (!(b->flags & ncacn_options[i].flag)) {
287 s = talloc_asprintf_append_buffer(s, ",%s", ncacn_options[i].name);
294 if (target_hostname) {
296 s = talloc_asprintf_append_buffer(s, ",target_hostname=%s",
304 if (b->target_principal) {
306 s = talloc_asprintf_append_buffer(s, ",target_principal=%s",
307 b->target_principal);
314 if (b->assoc_group_id != 0) {
316 s = talloc_asprintf_append_buffer(s, ",assoc_group_id=0x%08x",
324 for (i=0;b->options && b->options[i];i++) {
326 s = talloc_asprintf_append_buffer(s, ",%s", b->options[i]);
334 s = talloc_asprintf_append_buffer(s, "]");
344 parse a binding string into a dcerpc_binding structure
346 _PUBLIC_ NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *_s, struct dcerpc_binding **b_out)
349 struct dcerpc_binding *b;
351 char *options = NULL;
356 b = talloc_zero(mem_ctx, struct dcerpc_binding);
358 return NT_STATUS_NO_MEMORY;
361 _t = talloc_strdup(b, _s);
364 return NT_STATUS_NO_MEMORY;
373 if (options[strlen(options)-1] != ']') {
375 return NT_STATUS_INVALID_PARAMETER_MIX;
377 options[strlen(options)-1] = 0;
382 if (p && PTR_DIFF(p, s) == 36) { /* 36 is the length of a UUID */
385 status = dcerpc_binding_set_string_option(b, "object", s);
386 if (!NT_STATUS_IS_OK(status)) {
397 b->transport = NCA_UNKNOWN;
401 status = dcerpc_binding_set_string_option(b, "transport", s);
402 if (!NT_STATUS_IS_OK(status)) {
411 status = dcerpc_binding_set_string_option(b, "host", s);
412 if (!NT_STATUS_IS_OK(status)) {
417 b->target_hostname = talloc_strdup(b, b->host);
418 if (b->target_hostname == NULL) {
420 return NT_STATUS_NO_MEMORY;
424 for (i=0; options != NULL; i++) {
425 const char *name = options;
426 const char *value = NULL;
428 p = strchr(options, ',');
436 p = strchr(name, '=');
444 * If it's not a key=value pair
445 * it might be a ncacn_option
446 * or if it's the first option
449 const struct ncacn_option *no = NULL;
453 no = ncacn_option_by_name(name);
457 * we don't allow unknown options
459 return NT_STATUS_INVALID_PARAMETER_MIX;
463 * This is the endpoint
466 if (strlen(value) == 0) {
472 status = dcerpc_binding_set_string_option(b, name, value);
473 if (!NT_STATUS_IS_OK(status)) {
484 _PUBLIC_ struct GUID dcerpc_binding_get_object(const struct dcerpc_binding *b)
486 return b->object.uuid;
489 _PUBLIC_ NTSTATUS dcerpc_binding_set_object(struct dcerpc_binding *b,
492 char *tmp = discard_const_p(char, b->object_string);
494 if (GUID_all_zero(&object)) {
496 b->object_string = NULL;
497 ZERO_STRUCT(b->object);
501 b->object_string = GUID_string(b, &object);
502 if (b->object_string == NULL) {
503 b->object_string = tmp;
504 return NT_STATUS_NO_MEMORY;
508 ZERO_STRUCT(b->object);
509 b->object.uuid = object;
513 _PUBLIC_ enum dcerpc_transport_t dcerpc_binding_get_transport(const struct dcerpc_binding *b)
518 _PUBLIC_ NTSTATUS dcerpc_binding_set_transport(struct dcerpc_binding *b,
519 enum dcerpc_transport_t transport)
521 char *tmp = discard_const_p(char, b->endpoint);
524 * TODO: we may want to check the transport value is
527 if (b->transport == transport) {
531 b->transport = transport;
534 * This implicitly resets the endpoint
535 * as the endpoint is transport specific.
537 * TODO: in future we may reset more options
546 _PUBLIC_ void dcerpc_binding_get_auth_info(const struct dcerpc_binding *b,
547 enum dcerpc_AuthType *_auth_type,
548 enum dcerpc_AuthLevel *_auth_level)
550 enum dcerpc_AuthType auth_type;
551 enum dcerpc_AuthLevel auth_level;
553 if (b->flags & DCERPC_AUTH_SPNEGO) {
554 auth_type = DCERPC_AUTH_TYPE_SPNEGO;
555 } else if (b->flags & DCERPC_AUTH_KRB5) {
556 auth_type = DCERPC_AUTH_TYPE_KRB5;
557 } else if (b->flags & DCERPC_SCHANNEL) {
558 auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
559 } else if (b->flags & DCERPC_AUTH_NTLM) {
560 auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
562 auth_type = DCERPC_AUTH_TYPE_NONE;
565 if (b->flags & DCERPC_SEAL) {
566 auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
567 } else if (b->flags & DCERPC_SIGN) {
568 auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
569 } else if (b->flags & DCERPC_CONNECT) {
570 auth_level = DCERPC_AUTH_LEVEL_CONNECT;
571 } else if (auth_type != DCERPC_AUTH_TYPE_NONE) {
572 auth_level = DCERPC_AUTH_LEVEL_CONNECT;
574 auth_level = DCERPC_AUTH_LEVEL_NONE;
577 if (_auth_type == NULL) {
578 *_auth_type = auth_type;
581 if (_auth_level == NULL) {
582 *_auth_level = auth_level;
586 _PUBLIC_ uint32_t dcerpc_binding_get_assoc_group_id(const struct dcerpc_binding *b)
588 return b->assoc_group_id;
591 _PUBLIC_ NTSTATUS dcerpc_binding_set_assoc_group_id(struct dcerpc_binding *b,
592 uint32_t assoc_group_id)
594 b->assoc_group_id = assoc_group_id;
598 _PUBLIC_ struct ndr_syntax_id dcerpc_binding_get_abstract_syntax(const struct dcerpc_binding *b)
601 * For now we just use object, until all callers are fixed.
606 _PUBLIC_ NTSTATUS dcerpc_binding_set_abstract_syntax(struct dcerpc_binding *b,
607 const struct ndr_syntax_id *syntax)
613 * For now we just use object, until all callers are fixed.
616 if (syntax != NULL) {
617 object = syntax->uuid;
619 object = GUID_zero();
623 * This sets also the string
625 status = dcerpc_binding_set_object(b, object);
626 if (!NT_STATUS_IS_OK(status)) {
630 if (syntax != NULL) {
632 * Here we need to reset the whole ndr_syntax_id
633 * structure including the .if_version
641 _PUBLIC_ const char *dcerpc_binding_get_string_option(const struct dcerpc_binding *b,
647 #define _SPECIAL(x) { .name = #x, .value = b->x, }
649 { .name = "object", .value = b->object_string, },
652 _SPECIAL(target_hostname),
653 _SPECIAL(target_principal),
656 const struct ncacn_option *no = NULL;
657 size_t name_len = strlen(name);
661 ret = strcmp(name, "transport");
663 return derpc_transport_string_by_transport(b->transport);
666 ret = strcmp(name, "assoc_group_id");
668 char *tmp = discard_const_p(char, b->assoc_group_string);
670 if (b->assoc_group_id == 0) {
674 snprintf(tmp, sizeof(b->assoc_group_string),
675 "0x%08x", b->assoc_group_id);
676 return (const char *)b->assoc_group_string;
679 for (i=0; i < ARRAY_SIZE(specials); i++) {
680 ret = strcmp(specials[i].name, name);
685 return specials[i].value;
688 no = ncacn_option_by_name(name);
690 if (b->flags & no->flag) {
697 if (b->options == NULL) {
701 for (i=0; b->options[i]; i++) {
702 const char *o = b->options[i];
703 const char *vs = NULL;
705 ret = strncmp(name, o, name_len);
710 if (o[name_len] != '=') {
714 vs = &o[name_len + 1];
722 _PUBLIC_ char *dcerpc_binding_copy_string_option(TALLOC_CTX *mem_ctx,
723 const struct dcerpc_binding *b,
726 const char *c = dcerpc_binding_get_string_option(b, name);
734 v = talloc_strdup(mem_ctx, c);
743 _PUBLIC_ NTSTATUS dcerpc_binding_set_string_option(struct dcerpc_binding *b,
750 #define _SPECIAL(x) { .name = #x, .ptr = &b->x, }
754 _SPECIAL(target_hostname),
755 _SPECIAL(target_principal),
758 const struct ncacn_option *no = NULL;
759 size_t name_len = strlen(name);
760 const char *opt = NULL;
766 * Note: value == NULL, means delete it.
767 * value != NULL means add or reset.
770 ret = strcmp(name, "transport");
772 enum dcerpc_transport_t t = dcerpc_transport_by_name(value);
774 if (t == NCA_UNKNOWN && value != NULL) {
775 return NT_STATUS_INVALID_PARAMETER_MIX;
778 return dcerpc_binding_set_transport(b, t);
781 ret = strcmp(name, "object");
784 struct GUID uuid = GUID_zero();
788 blob = data_blob_string_const(value);
789 if (blob.length != 36) {
790 return NT_STATUS_INVALID_PARAMETER_MIX;
793 status = GUID_from_data_blob(&blob, &uuid);
794 if (!NT_STATUS_IS_OK(status)) {
799 return dcerpc_binding_set_object(b, uuid);
802 ret = strcmp(name, "assoc_group_id");
804 uint32_t assoc_group_id = 0;
809 ret = sscanf(value, "0x%08x%c", &assoc_group_id, &c);
811 return NT_STATUS_INVALID_PARAMETER_MIX;
815 return dcerpc_binding_set_assoc_group_id(b, assoc_group_id);
818 for (i=0; i < ARRAY_SIZE(specials); i++) {
819 ret = strcmp(specials[i].name, name);
824 tmp = discard_const_p(char, *specials[i].ptr);
828 *specials[i].ptr = NULL;
832 if (value[0] == '\0') {
833 return NT_STATUS_INVALID_PARAMETER_MIX;
836 *specials[i].ptr = talloc_strdup(b, value);
837 if (*specials[i].ptr == NULL) {
838 *specials[i].ptr = tmp;
839 return NT_STATUS_NO_MEMORY;
846 no = ncacn_option_by_name(name);
849 b->flags &= ~no->flag;
853 ret = strcasecmp(no->name, value);
855 return NT_STATUS_INVALID_PARAMETER_MIX;
858 b->flags |= no->flag;
862 for (i=0; b->options && b->options[i]; i++) {
863 const char *o = b->options[i];
865 ret = strncmp(name, o, name_len);
870 if (o[name_len] != '=') {
885 n = talloc_realloc(b, b->options, const char *, i + 2);
887 return NT_STATUS_NO_MEMORY;
894 tmp = discard_const_p(char, opt);
897 for (;b->options[i];i++) {
898 b->options[i] = b->options[i+1];
904 b->options[i] = talloc_asprintf(b->options, "%s=%s",
906 if (b->options[i] == NULL) {
908 return NT_STATUS_NO_MEMORY;
914 _PUBLIC_ uint32_t dcerpc_binding_get_flags(const struct dcerpc_binding *b)
919 _PUBLIC_ NTSTATUS dcerpc_binding_set_flags(struct dcerpc_binding *b,
924 * TODO: in future we may want to reject invalid combinations
927 b->flags |= additional;
932 _PUBLIC_ NTSTATUS dcerpc_floor_get_lhs_data(const struct epm_floor *epm_floor,
933 struct ndr_syntax_id *syntax)
935 TALLOC_CTX *mem_ctx = talloc_init("floor_get_lhs_data");
936 struct ndr_pull *ndr;
937 enum ndr_err_code ndr_err;
938 uint16_t if_version=0;
940 ndr = ndr_pull_init_blob(&epm_floor->lhs.lhs_data, mem_ctx);
942 talloc_free(mem_ctx);
943 return NT_STATUS_NO_MEMORY;
945 ndr->flags |= LIBNDR_FLAG_NOALIGN;
947 ndr_err = ndr_pull_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
948 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
949 talloc_free(mem_ctx);
950 return ndr_map_error2ntstatus(ndr_err);
953 ndr_err = ndr_pull_uint16(ndr, NDR_SCALARS, &if_version);
954 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
955 talloc_free(mem_ctx);
956 return ndr_map_error2ntstatus(ndr_err);
959 syntax->if_version = if_version;
961 talloc_free(mem_ctx);
966 static DATA_BLOB dcerpc_floor_pack_lhs_data(TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax)
969 enum ndr_err_code ndr_err;
970 struct ndr_push *ndr;
972 ndr = ndr_push_init_ctx(mem_ctx);
974 return data_blob_null;
977 ndr->flags |= LIBNDR_FLAG_NOALIGN;
979 ndr_err = ndr_push_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
980 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
981 return data_blob_null;
983 ndr_err = ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version);
984 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
985 return data_blob_null;
988 blob = ndr_push_blob(ndr);
989 talloc_steal(mem_ctx, blob.data);
994 static bool dcerpc_floor_pack_rhs_if_version_data(
995 TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax,
999 struct ndr_push *ndr = ndr_push_init_ctx(mem_ctx);
1000 enum ndr_err_code ndr_err;
1006 ndr->flags |= LIBNDR_FLAG_NOALIGN;
1008 ndr_err = ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version >> 16);
1009 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1013 blob = ndr_push_blob(ndr);
1014 talloc_steal(mem_ctx, blob.data);
1020 char *dcerpc_floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
1022 switch (epm_floor->lhs.protocol) {
1023 case EPM_PROTOCOL_TCP:
1024 if (epm_floor->rhs.tcp.port == 0) return NULL;
1025 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.tcp.port);
1027 case EPM_PROTOCOL_UDP:
1028 if (epm_floor->rhs.udp.port == 0) return NULL;
1029 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.udp.port);
1031 case EPM_PROTOCOL_HTTP:
1032 if (epm_floor->rhs.http.port == 0) return NULL;
1033 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.http.port);
1035 case EPM_PROTOCOL_IP:
1036 return talloc_strdup(mem_ctx, epm_floor->rhs.ip.ipaddr);
1038 case EPM_PROTOCOL_NCACN:
1041 case EPM_PROTOCOL_NCADG:
1044 case EPM_PROTOCOL_SMB:
1045 if (strlen(epm_floor->rhs.smb.unc) == 0) return NULL;
1046 return talloc_strdup(mem_ctx, epm_floor->rhs.smb.unc);
1048 case EPM_PROTOCOL_NAMED_PIPE:
1049 if (strlen(epm_floor->rhs.named_pipe.path) == 0) return NULL;
1050 return talloc_strdup(mem_ctx, epm_floor->rhs.named_pipe.path);
1052 case EPM_PROTOCOL_NETBIOS:
1053 if (strlen(epm_floor->rhs.netbios.name) == 0) return NULL;
1054 return talloc_strdup(mem_ctx, epm_floor->rhs.netbios.name);
1056 case EPM_PROTOCOL_NCALRPC:
1059 case EPM_PROTOCOL_VINES_SPP:
1060 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_spp.port);
1062 case EPM_PROTOCOL_VINES_IPC:
1063 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_ipc.port);
1065 case EPM_PROTOCOL_STREETTALK:
1066 return talloc_strdup(mem_ctx, epm_floor->rhs.streettalk.streettalk);
1068 case EPM_PROTOCOL_UNIX_DS:
1069 if (strlen(epm_floor->rhs.unix_ds.path) == 0) return NULL;
1070 return talloc_strdup(mem_ctx, epm_floor->rhs.unix_ds.path);
1072 case EPM_PROTOCOL_NULL:
1076 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
1083 static NTSTATUS dcerpc_floor_set_rhs_data(TALLOC_CTX *mem_ctx,
1084 struct epm_floor *epm_floor,
1091 switch (epm_floor->lhs.protocol) {
1092 case EPM_PROTOCOL_TCP:
1093 epm_floor->rhs.tcp.port = atoi(data);
1094 return NT_STATUS_OK;
1096 case EPM_PROTOCOL_UDP:
1097 epm_floor->rhs.udp.port = atoi(data);
1098 return NT_STATUS_OK;
1100 case EPM_PROTOCOL_HTTP:
1101 epm_floor->rhs.http.port = atoi(data);
1102 return NT_STATUS_OK;
1104 case EPM_PROTOCOL_IP:
1105 if (!is_ipaddress_v4(data)) {
1108 epm_floor->rhs.ip.ipaddr = talloc_strdup(mem_ctx, data);
1109 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.ip.ipaddr);
1110 return NT_STATUS_OK;
1112 case EPM_PROTOCOL_NCACN:
1113 epm_floor->rhs.ncacn.minor_version = 0;
1114 return NT_STATUS_OK;
1116 case EPM_PROTOCOL_NCADG:
1117 epm_floor->rhs.ncadg.minor_version = 0;
1118 return NT_STATUS_OK;
1120 case EPM_PROTOCOL_SMB:
1121 epm_floor->rhs.smb.unc = talloc_strdup(mem_ctx, data);
1122 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.smb.unc);
1123 return NT_STATUS_OK;
1125 case EPM_PROTOCOL_NAMED_PIPE:
1126 epm_floor->rhs.named_pipe.path = talloc_strdup(mem_ctx, data);
1127 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.named_pipe.path);
1128 return NT_STATUS_OK;
1130 case EPM_PROTOCOL_NETBIOS:
1131 epm_floor->rhs.netbios.name = talloc_strdup(mem_ctx, data);
1132 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.netbios.name);
1133 return NT_STATUS_OK;
1135 case EPM_PROTOCOL_NCALRPC:
1136 return NT_STATUS_OK;
1138 case EPM_PROTOCOL_VINES_SPP:
1139 epm_floor->rhs.vines_spp.port = atoi(data);
1140 return NT_STATUS_OK;
1142 case EPM_PROTOCOL_VINES_IPC:
1143 epm_floor->rhs.vines_ipc.port = atoi(data);
1144 return NT_STATUS_OK;
1146 case EPM_PROTOCOL_STREETTALK:
1147 epm_floor->rhs.streettalk.streettalk = talloc_strdup(mem_ctx, data);
1148 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.streettalk.streettalk);
1149 return NT_STATUS_OK;
1151 case EPM_PROTOCOL_UNIX_DS:
1152 epm_floor->rhs.unix_ds.path = talloc_strdup(mem_ctx, data);
1153 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.unix_ds.path);
1154 return NT_STATUS_OK;
1156 case EPM_PROTOCOL_NULL:
1157 return NT_STATUS_OK;
1160 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
1164 return NT_STATUS_NOT_SUPPORTED;
1167 enum dcerpc_transport_t dcerpc_transport_by_endpoint_protocol(int prot)
1171 /* Find a transport that has 'prot' as 4th protocol */
1172 for (i=0;i<ARRAY_SIZE(transports);i++) {
1173 if (transports[i].num_protocols >= 2 &&
1174 transports[i].protseq[1] == prot) {
1175 return transports[i].transport;
1179 /* Unknown transport */
1180 return (unsigned int)-1;
1183 _PUBLIC_ enum dcerpc_transport_t dcerpc_transport_by_tower(const struct epm_tower *tower)
1187 /* Find a transport that matches this tower */
1188 for (i=0;i<ARRAY_SIZE(transports);i++) {
1190 if (transports[i].num_protocols != tower->num_floors - 2) {
1194 for (j = 0; j < transports[i].num_protocols; j++) {
1195 if (transports[i].protseq[j] != tower->floors[j+2].lhs.protocol) {
1200 if (j == transports[i].num_protocols) {
1201 return transports[i].transport;
1205 /* Unknown transport */
1206 return (unsigned int)-1;
1209 _PUBLIC_ const char *derpc_transport_string_by_transport(enum dcerpc_transport_t t)
1213 for (i=0; i<ARRAY_SIZE(transports); i++) {
1214 if (t == transports[i].transport) {
1215 return transports[i].name;
1221 _PUBLIC_ enum dcerpc_transport_t dcerpc_transport_by_name(const char *name)
1229 for (i=0; i<ARRAY_SIZE(transports);i++) {
1230 if (strcasecmp(name, transports[i].name) == 0) {
1231 return transports[i].transport;
1238 _PUBLIC_ NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx,
1239 struct epm_tower *tower,
1240 struct dcerpc_binding **b_out)
1243 struct dcerpc_binding *b;
1244 enum dcerpc_transport_t transport;
1245 struct ndr_syntax_id abstract_syntax;
1246 char *endpoint = NULL;
1250 * A tower needs to have at least 4 floors to carry useful
1251 * information. Floor 3 is the transport identifier which defines
1252 * how many floors are required at least.
1254 if (tower->num_floors < 4) {
1255 return NT_STATUS_INVALID_PARAMETER;
1258 status = dcerpc_parse_binding(mem_ctx, "", &b);
1259 if (!NT_STATUS_IS_OK(status)) {
1263 transport = dcerpc_transport_by_tower(tower);
1264 if (transport == NCA_UNKNOWN) {
1266 return NT_STATUS_NOT_SUPPORTED;
1269 status = dcerpc_binding_set_transport(b, transport);
1270 if (!NT_STATUS_IS_OK(status)) {
1275 /* Set abstract syntax */
1276 status = dcerpc_floor_get_lhs_data(&tower->floors[0], &abstract_syntax);
1277 if (!NT_STATUS_IS_OK(status)) {
1282 status = dcerpc_binding_set_abstract_syntax(b, &abstract_syntax);
1283 if (!NT_STATUS_IS_OK(status)) {
1288 /* Ignore floor 1, it contains the NDR version info */
1292 if (tower->num_floors >= 4) {
1293 endpoint = dcerpc_floor_get_rhs_data(b, &tower->floors[3]);
1296 int saved_errno = errno;
1298 return map_nt_error_from_unix_common(saved_errno);
1301 status = dcerpc_binding_set_string_option(b, "endpoint", endpoint);
1302 if (!NT_STATUS_IS_OK(status)) {
1306 TALLOC_FREE(endpoint);
1308 /* Set network address */
1310 if (tower->num_floors >= 5) {
1311 host = dcerpc_floor_get_rhs_data(b, &tower->floors[4]);
1314 int saved_errno = errno;
1316 return map_nt_error_from_unix_common(saved_errno);
1319 status = dcerpc_binding_set_string_option(b, "host", host);
1320 if (!NT_STATUS_IS_OK(status)) {
1324 status = dcerpc_binding_set_string_option(b, "target_hostname", host);
1325 if (!NT_STATUS_IS_OK(status)) {
1332 return NT_STATUS_OK;
1335 _PUBLIC_ struct dcerpc_binding *dcerpc_binding_dup(TALLOC_CTX *mem_ctx,
1336 const struct dcerpc_binding *b)
1338 struct dcerpc_binding *n;
1341 n = talloc_zero(mem_ctx, struct dcerpc_binding);
1346 n->transport = b->transport;
1347 n->object = b->object;
1348 n->flags = b->flags;
1349 n->assoc_group_id = b->assoc_group_id;
1351 if (b->object_string != NULL) {
1352 n->object_string = talloc_strdup(n, b->object_string);
1353 if (n->object_string == NULL) {
1358 if (b->host != NULL) {
1359 n->host = talloc_strdup(n, b->host);
1360 if (n->host == NULL) {
1366 if (b->target_hostname != NULL) {
1367 n->target_hostname = talloc_strdup(n, b->target_hostname);
1368 if (n->target_hostname == NULL) {
1374 if (b->target_principal != NULL) {
1375 n->target_principal = talloc_strdup(n, b->target_principal);
1376 if (n->target_principal == NULL) {
1382 if (b->endpoint != NULL) {
1383 n->endpoint = talloc_strdup(n, b->endpoint);
1384 if (n->endpoint == NULL) {
1390 for (count = 0; b->options && b->options[count]; count++);
1395 n->options = talloc_array(n, const char *, count + 1);
1396 if (n->options == NULL) {
1401 for (i = 0; i < count; i++) {
1402 n->options[i] = talloc_strdup(n->options, b->options[i]);
1403 if (n->options[i] == NULL) {
1408 n->options[count] = NULL;
1414 _PUBLIC_ NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx,
1415 const struct dcerpc_binding *binding,
1416 struct epm_tower *tower)
1418 const enum epm_protocol *protseq = NULL;
1419 int num_protocols = -1, i;
1420 struct ndr_syntax_id abstract_syntax;
1423 /* Find transport */
1424 for (i=0;i<ARRAY_SIZE(transports);i++) {
1425 if (transports[i].transport == binding->transport) {
1426 protseq = transports[i].protseq;
1427 num_protocols = transports[i].num_protocols;
1432 if (num_protocols == -1) {
1433 DEBUG(0, ("Unable to find transport with id '%d'\n", binding->transport));
1434 return NT_STATUS_UNSUCCESSFUL;
1437 tower->num_floors = 2 + num_protocols;
1438 tower->floors = talloc_array(mem_ctx, struct epm_floor, tower->num_floors);
1441 tower->floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
1443 abstract_syntax = dcerpc_binding_get_abstract_syntax(binding);
1444 tower->floors[0].lhs.lhs_data = dcerpc_floor_pack_lhs_data(tower->floors,
1447 if (!dcerpc_floor_pack_rhs_if_version_data(
1448 tower->floors, &abstract_syntax,
1449 &tower->floors[0].rhs.uuid.unknown)) {
1450 return NT_STATUS_NO_MEMORY;
1454 tower->floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
1456 tower->floors[1].lhs.lhs_data = dcerpc_floor_pack_lhs_data(tower->floors,
1457 &ndr_transfer_syntax_ndr);
1459 tower->floors[1].rhs.uuid.unknown = data_blob_talloc_zero(tower->floors, 2);
1461 /* Floor 2 to num_protocols */
1462 for (i = 0; i < num_protocols; i++) {
1463 tower->floors[2 + i].lhs.protocol = protseq[i];
1464 tower->floors[2 + i].lhs.lhs_data = data_blob_null;
1465 ZERO_STRUCT(tower->floors[2 + i].rhs);
1466 status = dcerpc_floor_set_rhs_data(tower->floors,
1467 &tower->floors[2 + i],
1469 if (!NT_STATUS_IS_OK(status)) {
1474 /* The 4th floor contains the endpoint */
1475 if (num_protocols >= 2 && binding->endpoint) {
1476 status = dcerpc_floor_set_rhs_data(tower->floors,
1479 if (!NT_STATUS_IS_OK(status)) {
1484 /* The 5th contains the network address */
1485 if (num_protocols >= 3 && binding->host) {
1486 status = dcerpc_floor_set_rhs_data(tower->floors,
1489 if (!NT_STATUS_IS_OK(status)) {
1494 return NT_STATUS_OK;