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;
202 if (b->transport != NCA_UNKNOWN) {
203 t_name = derpc_transport_string_by_transport(b->transport);
210 if (!GUID_all_zero(&b->object.uuid)) {
212 s = talloc_asprintf_append_buffer(s, "%s@",
213 GUID_string(mem_ctx, &b->object.uuid));
220 if (t_name != NULL) {
222 s = talloc_asprintf_append_buffer(s, "%s:", t_name);
231 s = talloc_asprintf_append_buffer(s, "%s", b->host);
238 if (!b->endpoint && !b->options && !b->flags) {
243 s = talloc_asprintf_append_buffer(s, "[");
251 s = talloc_asprintf_append_buffer(s, "%s", b->endpoint);
258 for (i=0;i<ARRAY_SIZE(ncacn_options);i++) {
259 if (!(b->flags & ncacn_options[i].flag)) {
264 s = talloc_asprintf_append_buffer(s, ",%s", ncacn_options[i].name);
271 for (i=0;b->options && b->options[i];i++) {
273 s = talloc_asprintf_append_buffer(s, ",%s", b->options[i]);
281 s = talloc_asprintf_append_buffer(s, "]");
291 parse a binding string into a dcerpc_binding structure
293 _PUBLIC_ NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *_s, struct dcerpc_binding **b_out)
296 struct dcerpc_binding *b;
298 char *options = NULL;
303 b = talloc_zero(mem_ctx, struct dcerpc_binding);
305 return NT_STATUS_NO_MEMORY;
308 _t = talloc_strdup(b, _s);
311 return NT_STATUS_NO_MEMORY;
320 if (options[strlen(options)-1] != ']') {
322 return NT_STATUS_INVALID_PARAMETER_MIX;
324 options[strlen(options)-1] = 0;
329 if (p && PTR_DIFF(p, s) == 36) { /* 36 is the length of a UUID */
332 status = dcerpc_binding_set_string_option(b, "object", s);
333 if (!NT_STATUS_IS_OK(status)) {
344 b->transport = NCA_UNKNOWN;
348 status = dcerpc_binding_set_string_option(b, "transport", s);
349 if (!NT_STATUS_IS_OK(status)) {
358 status = dcerpc_binding_set_string_option(b, "host", s);
359 if (!NT_STATUS_IS_OK(status)) {
364 b->target_hostname = talloc_strdup(b, b->host);
365 if (b->target_hostname == NULL) {
367 return NT_STATUS_NO_MEMORY;
371 for (i=0; options != NULL; i++) {
372 const char *name = options;
373 const char *value = NULL;
375 p = strchr(options, ',');
383 p = strchr(name, '=');
391 * If it's not a key=value pair
392 * it might be a ncacn_option
393 * or if it's the first option
396 const struct ncacn_option *no = NULL;
400 no = ncacn_option_by_name(name);
404 * we don't allow unknown options
406 return NT_STATUS_INVALID_PARAMETER_MIX;
410 * This is the endpoint
413 if (strlen(value) == 0) {
419 status = dcerpc_binding_set_string_option(b, name, value);
420 if (!NT_STATUS_IS_OK(status)) {
431 _PUBLIC_ struct GUID dcerpc_binding_get_object(const struct dcerpc_binding *b)
433 return b->object.uuid;
436 _PUBLIC_ NTSTATUS dcerpc_binding_set_object(struct dcerpc_binding *b,
439 char *tmp = discard_const_p(char, b->object_string);
441 if (GUID_all_zero(&object)) {
443 b->object_string = NULL;
444 ZERO_STRUCT(b->object);
448 b->object_string = GUID_string(b, &object);
449 if (b->object_string == NULL) {
450 b->object_string = tmp;
451 return NT_STATUS_NO_MEMORY;
455 ZERO_STRUCT(b->object);
456 b->object.uuid = object;
460 _PUBLIC_ enum dcerpc_transport_t dcerpc_binding_get_transport(const struct dcerpc_binding *b)
465 _PUBLIC_ NTSTATUS dcerpc_binding_set_transport(struct dcerpc_binding *b,
466 enum dcerpc_transport_t transport)
468 char *tmp = discard_const_p(char, b->endpoint);
471 * TODO: we may want to check the transport value is
474 if (b->transport == transport) {
478 b->transport = transport;
481 * This implicitly resets the endpoint
482 * as the endpoint is transport specific.
484 * TODO: in future we may reset more options
493 _PUBLIC_ void dcerpc_binding_get_auth_info(const struct dcerpc_binding *b,
494 enum dcerpc_AuthType *_auth_type,
495 enum dcerpc_AuthLevel *_auth_level)
497 enum dcerpc_AuthType auth_type;
498 enum dcerpc_AuthLevel auth_level;
500 if (b->flags & DCERPC_AUTH_SPNEGO) {
501 auth_type = DCERPC_AUTH_TYPE_SPNEGO;
502 } else if (b->flags & DCERPC_AUTH_KRB5) {
503 auth_type = DCERPC_AUTH_TYPE_KRB5;
504 } else if (b->flags & DCERPC_SCHANNEL) {
505 auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
506 } else if (b->flags & DCERPC_AUTH_NTLM) {
507 auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
509 auth_type = DCERPC_AUTH_TYPE_NONE;
512 if (b->flags & DCERPC_SEAL) {
513 auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
514 } else if (b->flags & DCERPC_SIGN) {
515 auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
516 } else if (b->flags & DCERPC_CONNECT) {
517 auth_level = DCERPC_AUTH_LEVEL_CONNECT;
518 } else if (auth_type != DCERPC_AUTH_TYPE_NONE) {
519 auth_level = DCERPC_AUTH_LEVEL_CONNECT;
521 auth_level = DCERPC_AUTH_LEVEL_NONE;
524 if (_auth_type == NULL) {
525 *_auth_type = auth_type;
528 if (_auth_level == NULL) {
529 *_auth_level = auth_level;
533 _PUBLIC_ uint32_t dcerpc_binding_get_assoc_group_id(const struct dcerpc_binding *b)
535 return b->assoc_group_id;
538 _PUBLIC_ NTSTATUS dcerpc_binding_set_assoc_group_id(struct dcerpc_binding *b,
539 uint32_t assoc_group_id)
541 b->assoc_group_id = assoc_group_id;
545 _PUBLIC_ const char *dcerpc_binding_get_string_option(const struct dcerpc_binding *b,
551 #define _SPECIAL(x) { .name = #x, .value = b->x, }
553 { .name = "object", .value = b->object_string, },
556 _SPECIAL(target_hostname),
557 _SPECIAL(target_principal),
560 const struct ncacn_option *no = NULL;
561 size_t name_len = strlen(name);
565 ret = strcmp(name, "transport");
567 return derpc_transport_string_by_transport(b->transport);
570 ret = strcmp(name, "assoc_group_id");
572 char *tmp = discard_const_p(char, b->assoc_group_string);
574 if (b->assoc_group_id == 0) {
578 snprintf(tmp, sizeof(b->assoc_group_string),
579 "0x%08x", b->assoc_group_id);
580 return (const char *)b->assoc_group_string;
583 for (i=0; i < ARRAY_SIZE(specials); i++) {
584 ret = strcmp(specials[i].name, name);
589 return specials[i].value;
592 no = ncacn_option_by_name(name);
594 if (b->flags & no->flag) {
601 if (b->options == NULL) {
605 for (i=0; b->options[i]; i++) {
606 const char *o = b->options[i];
607 const char *vs = NULL;
609 ret = strncmp(name, o, name_len);
614 if (o[name_len] != '=') {
618 vs = &o[name_len + 1];
626 _PUBLIC_ char *dcerpc_binding_copy_string_option(TALLOC_CTX *mem_ctx,
627 const struct dcerpc_binding *b,
630 const char *c = dcerpc_binding_get_string_option(b, name);
638 v = talloc_strdup(mem_ctx, c);
647 _PUBLIC_ NTSTATUS dcerpc_binding_set_string_option(struct dcerpc_binding *b,
654 #define _SPECIAL(x) { .name = #x, .ptr = &b->x, }
658 _SPECIAL(target_hostname),
659 _SPECIAL(target_principal),
662 const struct ncacn_option *no = NULL;
663 size_t name_len = strlen(name);
664 const char *opt = NULL;
670 * Note: value == NULL, means delete it.
671 * value != NULL means add or reset.
674 ret = strcmp(name, "transport");
676 enum dcerpc_transport_t t = dcerpc_transport_by_name(value);
678 if (t == NCA_UNKNOWN && value != NULL) {
679 return NT_STATUS_INVALID_PARAMETER_MIX;
682 return dcerpc_binding_set_transport(b, t);
685 ret = strcmp(name, "object");
688 struct GUID uuid = GUID_zero();
692 blob = data_blob_string_const(value);
693 if (blob.length != 36) {
694 return NT_STATUS_INVALID_PARAMETER_MIX;
697 status = GUID_from_data_blob(&blob, &uuid);
698 if (!NT_STATUS_IS_OK(status)) {
703 return dcerpc_binding_set_object(b, uuid);
706 ret = strcmp(name, "assoc_group_id");
708 uint32_t assoc_group_id = 0;
713 ret = sscanf(value, "0x%08x%c", &assoc_group_id, &c);
715 return NT_STATUS_INVALID_PARAMETER_MIX;
719 return dcerpc_binding_set_assoc_group_id(b, assoc_group_id);
722 for (i=0; i < ARRAY_SIZE(specials); i++) {
723 ret = strcmp(specials[i].name, name);
728 tmp = discard_const_p(char, *specials[i].ptr);
732 *specials[i].ptr = NULL;
736 if (value[0] == '\0') {
737 return NT_STATUS_INVALID_PARAMETER_MIX;
740 *specials[i].ptr = talloc_strdup(b, value);
741 if (*specials[i].ptr == NULL) {
742 *specials[i].ptr = tmp;
743 return NT_STATUS_NO_MEMORY;
750 no = ncacn_option_by_name(name);
753 b->flags &= ~no->flag;
757 ret = strcasecmp(no->name, value);
759 return NT_STATUS_INVALID_PARAMETER_MIX;
762 b->flags |= no->flag;
766 for (i=0; b->options && b->options[i]; i++) {
767 const char *o = b->options[i];
769 ret = strncmp(name, o, name_len);
774 if (o[name_len] != '=') {
789 n = talloc_realloc(b, b->options, const char *, i + 2);
791 return NT_STATUS_NO_MEMORY;
798 tmp = discard_const_p(char, opt);
801 for (;b->options[i];i++) {
802 b->options[i] = b->options[i+1];
808 b->options[i] = talloc_asprintf(b->options, "%s=%s",
810 if (b->options[i] == NULL) {
812 return NT_STATUS_NO_MEMORY;
818 _PUBLIC_ uint32_t dcerpc_binding_get_flags(const struct dcerpc_binding *b)
823 _PUBLIC_ NTSTATUS dcerpc_binding_set_flags(struct dcerpc_binding *b,
828 * TODO: in future we may want to reject invalid combinations
831 b->flags |= additional;
836 _PUBLIC_ NTSTATUS dcerpc_floor_get_lhs_data(const struct epm_floor *epm_floor,
837 struct ndr_syntax_id *syntax)
839 TALLOC_CTX *mem_ctx = talloc_init("floor_get_lhs_data");
840 struct ndr_pull *ndr;
841 enum ndr_err_code ndr_err;
842 uint16_t if_version=0;
844 ndr = ndr_pull_init_blob(&epm_floor->lhs.lhs_data, mem_ctx);
846 talloc_free(mem_ctx);
847 return NT_STATUS_NO_MEMORY;
849 ndr->flags |= LIBNDR_FLAG_NOALIGN;
851 ndr_err = ndr_pull_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
852 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
853 talloc_free(mem_ctx);
854 return ndr_map_error2ntstatus(ndr_err);
857 ndr_err = ndr_pull_uint16(ndr, NDR_SCALARS, &if_version);
858 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
859 talloc_free(mem_ctx);
860 return ndr_map_error2ntstatus(ndr_err);
863 syntax->if_version = if_version;
865 talloc_free(mem_ctx);
870 static DATA_BLOB dcerpc_floor_pack_lhs_data(TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax)
873 enum ndr_err_code ndr_err;
874 struct ndr_push *ndr;
876 ndr = ndr_push_init_ctx(mem_ctx);
878 return data_blob_null;
881 ndr->flags |= LIBNDR_FLAG_NOALIGN;
883 ndr_err = ndr_push_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
884 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
885 return data_blob_null;
887 ndr_err = ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version);
888 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
889 return data_blob_null;
892 blob = ndr_push_blob(ndr);
893 talloc_steal(mem_ctx, blob.data);
898 static bool dcerpc_floor_pack_rhs_if_version_data(
899 TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax,
903 struct ndr_push *ndr = ndr_push_init_ctx(mem_ctx);
904 enum ndr_err_code ndr_err;
910 ndr->flags |= LIBNDR_FLAG_NOALIGN;
912 ndr_err = ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version >> 16);
913 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
917 blob = ndr_push_blob(ndr);
918 talloc_steal(mem_ctx, blob.data);
924 char *dcerpc_floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
926 switch (epm_floor->lhs.protocol) {
927 case EPM_PROTOCOL_TCP:
928 if (epm_floor->rhs.tcp.port == 0) return NULL;
929 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.tcp.port);
931 case EPM_PROTOCOL_UDP:
932 if (epm_floor->rhs.udp.port == 0) return NULL;
933 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.udp.port);
935 case EPM_PROTOCOL_HTTP:
936 if (epm_floor->rhs.http.port == 0) return NULL;
937 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.http.port);
939 case EPM_PROTOCOL_IP:
940 return talloc_strdup(mem_ctx, epm_floor->rhs.ip.ipaddr);
942 case EPM_PROTOCOL_NCACN:
945 case EPM_PROTOCOL_NCADG:
948 case EPM_PROTOCOL_SMB:
949 if (strlen(epm_floor->rhs.smb.unc) == 0) return NULL;
950 return talloc_strdup(mem_ctx, epm_floor->rhs.smb.unc);
952 case EPM_PROTOCOL_NAMED_PIPE:
953 if (strlen(epm_floor->rhs.named_pipe.path) == 0) return NULL;
954 return talloc_strdup(mem_ctx, epm_floor->rhs.named_pipe.path);
956 case EPM_PROTOCOL_NETBIOS:
957 if (strlen(epm_floor->rhs.netbios.name) == 0) return NULL;
958 return talloc_strdup(mem_ctx, epm_floor->rhs.netbios.name);
960 case EPM_PROTOCOL_NCALRPC:
963 case EPM_PROTOCOL_VINES_SPP:
964 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_spp.port);
966 case EPM_PROTOCOL_VINES_IPC:
967 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_ipc.port);
969 case EPM_PROTOCOL_STREETTALK:
970 return talloc_strdup(mem_ctx, epm_floor->rhs.streettalk.streettalk);
972 case EPM_PROTOCOL_UNIX_DS:
973 if (strlen(epm_floor->rhs.unix_ds.path) == 0) return NULL;
974 return talloc_strdup(mem_ctx, epm_floor->rhs.unix_ds.path);
976 case EPM_PROTOCOL_NULL:
980 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
987 static NTSTATUS dcerpc_floor_set_rhs_data(TALLOC_CTX *mem_ctx,
988 struct epm_floor *epm_floor,
995 switch (epm_floor->lhs.protocol) {
996 case EPM_PROTOCOL_TCP:
997 epm_floor->rhs.tcp.port = atoi(data);
1000 case EPM_PROTOCOL_UDP:
1001 epm_floor->rhs.udp.port = atoi(data);
1002 return NT_STATUS_OK;
1004 case EPM_PROTOCOL_HTTP:
1005 epm_floor->rhs.http.port = atoi(data);
1006 return NT_STATUS_OK;
1008 case EPM_PROTOCOL_IP:
1009 if (!is_ipaddress_v4(data)) {
1012 epm_floor->rhs.ip.ipaddr = talloc_strdup(mem_ctx, data);
1013 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.ip.ipaddr);
1014 return NT_STATUS_OK;
1016 case EPM_PROTOCOL_NCACN:
1017 epm_floor->rhs.ncacn.minor_version = 0;
1018 return NT_STATUS_OK;
1020 case EPM_PROTOCOL_NCADG:
1021 epm_floor->rhs.ncadg.minor_version = 0;
1022 return NT_STATUS_OK;
1024 case EPM_PROTOCOL_SMB:
1025 epm_floor->rhs.smb.unc = talloc_strdup(mem_ctx, data);
1026 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.smb.unc);
1027 return NT_STATUS_OK;
1029 case EPM_PROTOCOL_NAMED_PIPE:
1030 epm_floor->rhs.named_pipe.path = talloc_strdup(mem_ctx, data);
1031 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.named_pipe.path);
1032 return NT_STATUS_OK;
1034 case EPM_PROTOCOL_NETBIOS:
1035 epm_floor->rhs.netbios.name = talloc_strdup(mem_ctx, data);
1036 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.netbios.name);
1037 return NT_STATUS_OK;
1039 case EPM_PROTOCOL_NCALRPC:
1040 return NT_STATUS_OK;
1042 case EPM_PROTOCOL_VINES_SPP:
1043 epm_floor->rhs.vines_spp.port = atoi(data);
1044 return NT_STATUS_OK;
1046 case EPM_PROTOCOL_VINES_IPC:
1047 epm_floor->rhs.vines_ipc.port = atoi(data);
1048 return NT_STATUS_OK;
1050 case EPM_PROTOCOL_STREETTALK:
1051 epm_floor->rhs.streettalk.streettalk = talloc_strdup(mem_ctx, data);
1052 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.streettalk.streettalk);
1053 return NT_STATUS_OK;
1055 case EPM_PROTOCOL_UNIX_DS:
1056 epm_floor->rhs.unix_ds.path = talloc_strdup(mem_ctx, data);
1057 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.unix_ds.path);
1058 return NT_STATUS_OK;
1060 case EPM_PROTOCOL_NULL:
1061 return NT_STATUS_OK;
1064 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
1068 return NT_STATUS_NOT_SUPPORTED;
1071 enum dcerpc_transport_t dcerpc_transport_by_endpoint_protocol(int prot)
1075 /* Find a transport that has 'prot' as 4th protocol */
1076 for (i=0;i<ARRAY_SIZE(transports);i++) {
1077 if (transports[i].num_protocols >= 2 &&
1078 transports[i].protseq[1] == prot) {
1079 return transports[i].transport;
1083 /* Unknown transport */
1084 return (unsigned int)-1;
1087 _PUBLIC_ enum dcerpc_transport_t dcerpc_transport_by_tower(const struct epm_tower *tower)
1091 /* Find a transport that matches this tower */
1092 for (i=0;i<ARRAY_SIZE(transports);i++) {
1094 if (transports[i].num_protocols != tower->num_floors - 2) {
1098 for (j = 0; j < transports[i].num_protocols; j++) {
1099 if (transports[i].protseq[j] != tower->floors[j+2].lhs.protocol) {
1104 if (j == transports[i].num_protocols) {
1105 return transports[i].transport;
1109 /* Unknown transport */
1110 return (unsigned int)-1;
1113 _PUBLIC_ const char *derpc_transport_string_by_transport(enum dcerpc_transport_t t)
1117 for (i=0; i<ARRAY_SIZE(transports); i++) {
1118 if (t == transports[i].transport) {
1119 return transports[i].name;
1125 _PUBLIC_ enum dcerpc_transport_t dcerpc_transport_by_name(const char *name)
1133 for (i=0; i<ARRAY_SIZE(transports);i++) {
1134 if (strcasecmp(name, transports[i].name) == 0) {
1135 return transports[i].transport;
1142 _PUBLIC_ NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx,
1143 struct epm_tower *tower,
1144 struct dcerpc_binding **b_out)
1147 struct dcerpc_binding *binding;
1150 * A tower needs to have at least 4 floors to carry useful
1151 * information. Floor 3 is the transport identifier which defines
1152 * how many floors are required at least.
1154 if (tower->num_floors < 4) {
1155 return NT_STATUS_INVALID_PARAMETER;
1158 binding = talloc_zero(mem_ctx, struct dcerpc_binding);
1159 NT_STATUS_HAVE_NO_MEMORY(binding);
1161 ZERO_STRUCT(binding->object);
1162 binding->options = NULL;
1163 binding->host = NULL;
1164 binding->target_hostname = NULL;
1166 binding->assoc_group_id = 0;
1168 binding->transport = dcerpc_transport_by_tower(tower);
1170 if (binding->transport == (unsigned int)-1) {
1171 talloc_free(binding);
1172 return NT_STATUS_NOT_SUPPORTED;
1175 /* Set object uuid */
1176 status = dcerpc_floor_get_lhs_data(&tower->floors[0], &binding->object);
1178 if (!NT_STATUS_IS_OK(status)) {
1179 DEBUG(1, ("Error pulling object uuid and version: %s", nt_errstr(status)));
1180 talloc_free(binding);
1184 /* Ignore floor 1, it contains the NDR version info */
1186 binding->options = NULL;
1190 if (tower->num_floors >= 4) {
1191 binding->endpoint = dcerpc_floor_get_rhs_data(binding, &tower->floors[3]);
1194 int saved_errno = errno;
1195 talloc_free(binding);
1196 return map_nt_error_from_unix_common(saved_errno);
1199 /* Set network address */
1201 if (tower->num_floors >= 5) {
1202 binding->host = dcerpc_floor_get_rhs_data(binding, &tower->floors[4]);
1205 int saved_errno = errno;
1206 talloc_free(binding);
1207 return map_nt_error_from_unix_common(saved_errno);
1209 binding->target_hostname = binding->host;
1212 return NT_STATUS_OK;
1215 _PUBLIC_ struct dcerpc_binding *dcerpc_binding_dup(TALLOC_CTX *mem_ctx,
1216 const struct dcerpc_binding *b)
1218 struct dcerpc_binding *n;
1221 n = talloc_zero(mem_ctx, struct dcerpc_binding);
1226 n->transport = b->transport;
1227 n->object = b->object;
1228 n->flags = b->flags;
1229 n->assoc_group_id = b->assoc_group_id;
1231 if (b->object_string != NULL) {
1232 n->object_string = talloc_strdup(n, b->object_string);
1233 if (n->object_string == NULL) {
1238 if (b->host != NULL) {
1239 n->host = talloc_strdup(n, b->host);
1240 if (n->host == NULL) {
1246 if (b->target_hostname != NULL) {
1247 n->target_hostname = talloc_strdup(n, b->target_hostname);
1248 if (n->target_hostname == NULL) {
1254 if (b->target_principal != NULL) {
1255 n->target_principal = talloc_strdup(n, b->target_principal);
1256 if (n->target_principal == NULL) {
1262 if (b->endpoint != NULL) {
1263 n->endpoint = talloc_strdup(n, b->endpoint);
1264 if (n->endpoint == NULL) {
1270 for (count = 0; b->options && b->options[count]; count++);
1275 n->options = talloc_array(n, const char *, count + 1);
1276 if (n->options == NULL) {
1281 for (i = 0; i < count; i++) {
1282 n->options[i] = talloc_strdup(n->options, b->options[i]);
1283 if (n->options[i] == NULL) {
1288 n->options[count] = NULL;
1294 _PUBLIC_ NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx,
1295 const struct dcerpc_binding *binding,
1296 struct epm_tower *tower)
1298 const enum epm_protocol *protseq = NULL;
1299 int num_protocols = -1, i;
1302 /* Find transport */
1303 for (i=0;i<ARRAY_SIZE(transports);i++) {
1304 if (transports[i].transport == binding->transport) {
1305 protseq = transports[i].protseq;
1306 num_protocols = transports[i].num_protocols;
1311 if (num_protocols == -1) {
1312 DEBUG(0, ("Unable to find transport with id '%d'\n", binding->transport));
1313 return NT_STATUS_UNSUCCESSFUL;
1316 tower->num_floors = 2 + num_protocols;
1317 tower->floors = talloc_array(mem_ctx, struct epm_floor, tower->num_floors);
1320 tower->floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
1322 tower->floors[0].lhs.lhs_data = dcerpc_floor_pack_lhs_data(tower->floors, &binding->object);
1324 if (!dcerpc_floor_pack_rhs_if_version_data(
1325 tower->floors, &binding->object,
1326 &tower->floors[0].rhs.uuid.unknown)) {
1327 return NT_STATUS_NO_MEMORY;
1331 tower->floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
1333 tower->floors[1].lhs.lhs_data = dcerpc_floor_pack_lhs_data(tower->floors,
1334 &ndr_transfer_syntax_ndr);
1336 tower->floors[1].rhs.uuid.unknown = data_blob_talloc_zero(tower->floors, 2);
1338 /* Floor 2 to num_protocols */
1339 for (i = 0; i < num_protocols; i++) {
1340 tower->floors[2 + i].lhs.protocol = protseq[i];
1341 tower->floors[2 + i].lhs.lhs_data = data_blob_null;
1342 ZERO_STRUCT(tower->floors[2 + i].rhs);
1343 status = dcerpc_floor_set_rhs_data(tower->floors,
1344 &tower->floors[2 + i],
1346 if (!NT_STATUS_IS_OK(status)) {
1351 /* The 4th floor contains the endpoint */
1352 if (num_protocols >= 2 && binding->endpoint) {
1353 status = dcerpc_floor_set_rhs_data(tower->floors,
1356 if (!NT_STATUS_IS_OK(status)) {
1361 /* The 5th contains the network address */
1362 if (num_protocols >= 3 && binding->host) {
1363 status = dcerpc_floor_set_rhs_data(tower->floors,
1366 if (!NT_STATUS_IS_OK(status)) {
1371 return NT_STATUS_OK;