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_ void dcerpc_binding_get_auth_info(const struct dcerpc_binding *b,
461 enum dcerpc_AuthType *_auth_type,
462 enum dcerpc_AuthLevel *_auth_level)
464 enum dcerpc_AuthType auth_type;
465 enum dcerpc_AuthLevel auth_level;
467 if (b->flags & DCERPC_AUTH_SPNEGO) {
468 auth_type = DCERPC_AUTH_TYPE_SPNEGO;
469 } else if (b->flags & DCERPC_AUTH_KRB5) {
470 auth_type = DCERPC_AUTH_TYPE_KRB5;
471 } else if (b->flags & DCERPC_SCHANNEL) {
472 auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
473 } else if (b->flags & DCERPC_AUTH_NTLM) {
474 auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
476 auth_type = DCERPC_AUTH_TYPE_NONE;
479 if (b->flags & DCERPC_SEAL) {
480 auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
481 } else if (b->flags & DCERPC_SIGN) {
482 auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
483 } else if (b->flags & DCERPC_CONNECT) {
484 auth_level = DCERPC_AUTH_LEVEL_CONNECT;
485 } else if (auth_type != DCERPC_AUTH_TYPE_NONE) {
486 auth_level = DCERPC_AUTH_LEVEL_CONNECT;
488 auth_level = DCERPC_AUTH_LEVEL_NONE;
491 if (_auth_type == NULL) {
492 *_auth_type = auth_type;
495 if (_auth_level == NULL) {
496 *_auth_level = auth_level;
500 _PUBLIC_ const char *dcerpc_binding_get_string_option(const struct dcerpc_binding *b,
506 #define _SPECIAL(x) { .name = #x, .value = b->x, }
508 { .name = "object", .value = b->object_string, },
511 _SPECIAL(target_hostname),
512 _SPECIAL(target_principal),
515 const struct ncacn_option *no = NULL;
516 size_t name_len = strlen(name);
520 ret = strcmp(name, "transport");
522 return derpc_transport_string_by_transport(b->transport);
525 for (i=0; i < ARRAY_SIZE(specials); i++) {
526 ret = strcmp(specials[i].name, name);
531 return specials[i].value;
534 no = ncacn_option_by_name(name);
536 if (b->flags & no->flag) {
543 if (b->options == NULL) {
547 for (i=0; b->options[i]; i++) {
548 const char *o = b->options[i];
549 const char *vs = NULL;
551 ret = strncmp(name, o, name_len);
556 if (o[name_len] != '=') {
560 vs = &o[name_len + 1];
568 _PUBLIC_ char *dcerpc_binding_copy_string_option(TALLOC_CTX *mem_ctx,
569 const struct dcerpc_binding *b,
572 const char *c = dcerpc_binding_get_string_option(b, name);
580 v = talloc_strdup(mem_ctx, c);
589 _PUBLIC_ NTSTATUS dcerpc_binding_set_string_option(struct dcerpc_binding *b,
596 #define _SPECIAL(x) { .name = #x, .ptr = &b->x, }
600 _SPECIAL(target_hostname),
601 _SPECIAL(target_principal),
604 const struct ncacn_option *no = NULL;
605 size_t name_len = strlen(name);
606 const char *opt = NULL;
612 * Note: value == NULL, means delete it.
613 * value != NULL means add or reset.
616 ret = strcmp(name, "transport");
618 enum dcerpc_transport_t t = dcerpc_transport_by_name(value);
620 if (t == NCA_UNKNOWN && value != NULL) {
621 return NT_STATUS_INVALID_PARAMETER_MIX;
628 ret = strcmp(name, "object");
631 struct GUID uuid = GUID_zero();
635 blob = data_blob_string_const(value);
636 if (blob.length != 36) {
637 return NT_STATUS_INVALID_PARAMETER_MIX;
640 status = GUID_from_data_blob(&blob, &uuid);
641 if (!NT_STATUS_IS_OK(status)) {
646 return dcerpc_binding_set_object(b, uuid);
649 for (i=0; i < ARRAY_SIZE(specials); i++) {
650 ret = strcmp(specials[i].name, name);
655 tmp = discard_const_p(char, *specials[i].ptr);
659 *specials[i].ptr = NULL;
663 if (value[0] == '\0') {
664 return NT_STATUS_INVALID_PARAMETER_MIX;
667 *specials[i].ptr = talloc_strdup(b, value);
668 if (*specials[i].ptr == NULL) {
669 *specials[i].ptr = tmp;
670 return NT_STATUS_NO_MEMORY;
677 no = ncacn_option_by_name(name);
680 b->flags &= ~no->flag;
684 ret = strcasecmp(no->name, value);
686 return NT_STATUS_INVALID_PARAMETER_MIX;
689 b->flags |= no->flag;
693 for (i=0; b->options && b->options[i]; i++) {
694 const char *o = b->options[i];
696 ret = strncmp(name, o, name_len);
701 if (o[name_len] != '=') {
716 n = talloc_realloc(b, b->options, const char *, i + 2);
718 return NT_STATUS_NO_MEMORY;
725 tmp = discard_const_p(char, opt);
728 for (;b->options[i];i++) {
729 b->options[i] = b->options[i+1];
735 b->options[i] = talloc_asprintf(b->options, "%s=%s",
737 if (b->options[i] == NULL) {
739 return NT_STATUS_NO_MEMORY;
745 _PUBLIC_ uint32_t dcerpc_binding_get_flags(const struct dcerpc_binding *b)
750 _PUBLIC_ NTSTATUS dcerpc_binding_set_flags(struct dcerpc_binding *b,
755 * TODO: in future we may want to reject invalid combinations
758 b->flags |= additional;
763 _PUBLIC_ NTSTATUS dcerpc_floor_get_lhs_data(const struct epm_floor *epm_floor,
764 struct ndr_syntax_id *syntax)
766 TALLOC_CTX *mem_ctx = talloc_init("floor_get_lhs_data");
767 struct ndr_pull *ndr;
768 enum ndr_err_code ndr_err;
769 uint16_t if_version=0;
771 ndr = ndr_pull_init_blob(&epm_floor->lhs.lhs_data, mem_ctx);
773 talloc_free(mem_ctx);
774 return NT_STATUS_NO_MEMORY;
776 ndr->flags |= LIBNDR_FLAG_NOALIGN;
778 ndr_err = ndr_pull_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
779 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
780 talloc_free(mem_ctx);
781 return ndr_map_error2ntstatus(ndr_err);
784 ndr_err = ndr_pull_uint16(ndr, NDR_SCALARS, &if_version);
785 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
786 talloc_free(mem_ctx);
787 return ndr_map_error2ntstatus(ndr_err);
790 syntax->if_version = if_version;
792 talloc_free(mem_ctx);
797 static DATA_BLOB dcerpc_floor_pack_lhs_data(TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax)
800 enum ndr_err_code ndr_err;
801 struct ndr_push *ndr;
803 ndr = ndr_push_init_ctx(mem_ctx);
805 return data_blob_null;
808 ndr->flags |= LIBNDR_FLAG_NOALIGN;
810 ndr_err = ndr_push_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
811 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
812 return data_blob_null;
814 ndr_err = ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version);
815 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
816 return data_blob_null;
819 blob = ndr_push_blob(ndr);
820 talloc_steal(mem_ctx, blob.data);
825 static bool dcerpc_floor_pack_rhs_if_version_data(
826 TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax,
830 struct ndr_push *ndr = ndr_push_init_ctx(mem_ctx);
831 enum ndr_err_code ndr_err;
837 ndr->flags |= LIBNDR_FLAG_NOALIGN;
839 ndr_err = ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version >> 16);
840 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
844 blob = ndr_push_blob(ndr);
845 talloc_steal(mem_ctx, blob.data);
851 char *dcerpc_floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
853 switch (epm_floor->lhs.protocol) {
854 case EPM_PROTOCOL_TCP:
855 if (epm_floor->rhs.tcp.port == 0) return NULL;
856 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.tcp.port);
858 case EPM_PROTOCOL_UDP:
859 if (epm_floor->rhs.udp.port == 0) return NULL;
860 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.udp.port);
862 case EPM_PROTOCOL_HTTP:
863 if (epm_floor->rhs.http.port == 0) return NULL;
864 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.http.port);
866 case EPM_PROTOCOL_IP:
867 return talloc_strdup(mem_ctx, epm_floor->rhs.ip.ipaddr);
869 case EPM_PROTOCOL_NCACN:
872 case EPM_PROTOCOL_NCADG:
875 case EPM_PROTOCOL_SMB:
876 if (strlen(epm_floor->rhs.smb.unc) == 0) return NULL;
877 return talloc_strdup(mem_ctx, epm_floor->rhs.smb.unc);
879 case EPM_PROTOCOL_NAMED_PIPE:
880 if (strlen(epm_floor->rhs.named_pipe.path) == 0) return NULL;
881 return talloc_strdup(mem_ctx, epm_floor->rhs.named_pipe.path);
883 case EPM_PROTOCOL_NETBIOS:
884 if (strlen(epm_floor->rhs.netbios.name) == 0) return NULL;
885 return talloc_strdup(mem_ctx, epm_floor->rhs.netbios.name);
887 case EPM_PROTOCOL_NCALRPC:
890 case EPM_PROTOCOL_VINES_SPP:
891 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_spp.port);
893 case EPM_PROTOCOL_VINES_IPC:
894 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_ipc.port);
896 case EPM_PROTOCOL_STREETTALK:
897 return talloc_strdup(mem_ctx, epm_floor->rhs.streettalk.streettalk);
899 case EPM_PROTOCOL_UNIX_DS:
900 if (strlen(epm_floor->rhs.unix_ds.path) == 0) return NULL;
901 return talloc_strdup(mem_ctx, epm_floor->rhs.unix_ds.path);
903 case EPM_PROTOCOL_NULL:
907 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
914 static NTSTATUS dcerpc_floor_set_rhs_data(TALLOC_CTX *mem_ctx,
915 struct epm_floor *epm_floor,
922 switch (epm_floor->lhs.protocol) {
923 case EPM_PROTOCOL_TCP:
924 epm_floor->rhs.tcp.port = atoi(data);
927 case EPM_PROTOCOL_UDP:
928 epm_floor->rhs.udp.port = atoi(data);
931 case EPM_PROTOCOL_HTTP:
932 epm_floor->rhs.http.port = atoi(data);
935 case EPM_PROTOCOL_IP:
936 if (!is_ipaddress_v4(data)) {
939 epm_floor->rhs.ip.ipaddr = talloc_strdup(mem_ctx, data);
940 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.ip.ipaddr);
943 case EPM_PROTOCOL_NCACN:
944 epm_floor->rhs.ncacn.minor_version = 0;
947 case EPM_PROTOCOL_NCADG:
948 epm_floor->rhs.ncadg.minor_version = 0;
951 case EPM_PROTOCOL_SMB:
952 epm_floor->rhs.smb.unc = talloc_strdup(mem_ctx, data);
953 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.smb.unc);
956 case EPM_PROTOCOL_NAMED_PIPE:
957 epm_floor->rhs.named_pipe.path = talloc_strdup(mem_ctx, data);
958 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.named_pipe.path);
961 case EPM_PROTOCOL_NETBIOS:
962 epm_floor->rhs.netbios.name = talloc_strdup(mem_ctx, data);
963 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.netbios.name);
966 case EPM_PROTOCOL_NCALRPC:
969 case EPM_PROTOCOL_VINES_SPP:
970 epm_floor->rhs.vines_spp.port = atoi(data);
973 case EPM_PROTOCOL_VINES_IPC:
974 epm_floor->rhs.vines_ipc.port = atoi(data);
977 case EPM_PROTOCOL_STREETTALK:
978 epm_floor->rhs.streettalk.streettalk = talloc_strdup(mem_ctx, data);
979 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.streettalk.streettalk);
982 case EPM_PROTOCOL_UNIX_DS:
983 epm_floor->rhs.unix_ds.path = talloc_strdup(mem_ctx, data);
984 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.unix_ds.path);
987 case EPM_PROTOCOL_NULL:
991 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
995 return NT_STATUS_NOT_SUPPORTED;
998 enum dcerpc_transport_t dcerpc_transport_by_endpoint_protocol(int prot)
1002 /* Find a transport that has 'prot' as 4th protocol */
1003 for (i=0;i<ARRAY_SIZE(transports);i++) {
1004 if (transports[i].num_protocols >= 2 &&
1005 transports[i].protseq[1] == prot) {
1006 return transports[i].transport;
1010 /* Unknown transport */
1011 return (unsigned int)-1;
1014 _PUBLIC_ enum dcerpc_transport_t dcerpc_transport_by_tower(const struct epm_tower *tower)
1018 /* Find a transport that matches this tower */
1019 for (i=0;i<ARRAY_SIZE(transports);i++) {
1021 if (transports[i].num_protocols != tower->num_floors - 2) {
1025 for (j = 0; j < transports[i].num_protocols; j++) {
1026 if (transports[i].protseq[j] != tower->floors[j+2].lhs.protocol) {
1031 if (j == transports[i].num_protocols) {
1032 return transports[i].transport;
1036 /* Unknown transport */
1037 return (unsigned int)-1;
1040 _PUBLIC_ const char *derpc_transport_string_by_transport(enum dcerpc_transport_t t)
1044 for (i=0; i<ARRAY_SIZE(transports); i++) {
1045 if (t == transports[i].transport) {
1046 return transports[i].name;
1052 _PUBLIC_ enum dcerpc_transport_t dcerpc_transport_by_name(const char *name)
1060 for (i=0; i<ARRAY_SIZE(transports);i++) {
1061 if (strcasecmp(name, transports[i].name) == 0) {
1062 return transports[i].transport;
1069 _PUBLIC_ NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx,
1070 struct epm_tower *tower,
1071 struct dcerpc_binding **b_out)
1074 struct dcerpc_binding *binding;
1077 * A tower needs to have at least 4 floors to carry useful
1078 * information. Floor 3 is the transport identifier which defines
1079 * how many floors are required at least.
1081 if (tower->num_floors < 4) {
1082 return NT_STATUS_INVALID_PARAMETER;
1085 binding = talloc_zero(mem_ctx, struct dcerpc_binding);
1086 NT_STATUS_HAVE_NO_MEMORY(binding);
1088 ZERO_STRUCT(binding->object);
1089 binding->options = NULL;
1090 binding->host = NULL;
1091 binding->target_hostname = NULL;
1093 binding->assoc_group_id = 0;
1095 binding->transport = dcerpc_transport_by_tower(tower);
1097 if (binding->transport == (unsigned int)-1) {
1098 talloc_free(binding);
1099 return NT_STATUS_NOT_SUPPORTED;
1102 /* Set object uuid */
1103 status = dcerpc_floor_get_lhs_data(&tower->floors[0], &binding->object);
1105 if (!NT_STATUS_IS_OK(status)) {
1106 DEBUG(1, ("Error pulling object uuid and version: %s", nt_errstr(status)));
1107 talloc_free(binding);
1111 /* Ignore floor 1, it contains the NDR version info */
1113 binding->options = NULL;
1117 if (tower->num_floors >= 4) {
1118 binding->endpoint = dcerpc_floor_get_rhs_data(binding, &tower->floors[3]);
1121 int saved_errno = errno;
1122 talloc_free(binding);
1123 return map_nt_error_from_unix_common(saved_errno);
1126 /* Set network address */
1128 if (tower->num_floors >= 5) {
1129 binding->host = dcerpc_floor_get_rhs_data(binding, &tower->floors[4]);
1132 int saved_errno = errno;
1133 talloc_free(binding);
1134 return map_nt_error_from_unix_common(saved_errno);
1136 binding->target_hostname = binding->host;
1139 return NT_STATUS_OK;
1142 _PUBLIC_ struct dcerpc_binding *dcerpc_binding_dup(TALLOC_CTX *mem_ctx,
1143 const struct dcerpc_binding *b)
1145 struct dcerpc_binding *n;
1148 n = talloc_zero(mem_ctx, struct dcerpc_binding);
1153 n->transport = b->transport;
1154 n->object = b->object;
1155 n->flags = b->flags;
1156 n->assoc_group_id = b->assoc_group_id;
1158 if (b->object_string != NULL) {
1159 n->object_string = talloc_strdup(n, b->object_string);
1160 if (n->object_string == NULL) {
1165 if (b->host != NULL) {
1166 n->host = talloc_strdup(n, b->host);
1167 if (n->host == NULL) {
1173 if (b->target_hostname != NULL) {
1174 n->target_hostname = talloc_strdup(n, b->target_hostname);
1175 if (n->target_hostname == NULL) {
1181 if (b->target_principal != NULL) {
1182 n->target_principal = talloc_strdup(n, b->target_principal);
1183 if (n->target_principal == NULL) {
1189 if (b->endpoint != NULL) {
1190 n->endpoint = talloc_strdup(n, b->endpoint);
1191 if (n->endpoint == NULL) {
1197 for (count = 0; b->options && b->options[count]; count++);
1202 n->options = talloc_array(n, const char *, count + 1);
1203 if (n->options == NULL) {
1208 for (i = 0; i < count; i++) {
1209 n->options[i] = talloc_strdup(n->options, b->options[i]);
1210 if (n->options[i] == NULL) {
1215 n->options[count] = NULL;
1221 _PUBLIC_ NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx,
1222 const struct dcerpc_binding *binding,
1223 struct epm_tower *tower)
1225 const enum epm_protocol *protseq = NULL;
1226 int num_protocols = -1, i;
1229 /* Find transport */
1230 for (i=0;i<ARRAY_SIZE(transports);i++) {
1231 if (transports[i].transport == binding->transport) {
1232 protseq = transports[i].protseq;
1233 num_protocols = transports[i].num_protocols;
1238 if (num_protocols == -1) {
1239 DEBUG(0, ("Unable to find transport with id '%d'\n", binding->transport));
1240 return NT_STATUS_UNSUCCESSFUL;
1243 tower->num_floors = 2 + num_protocols;
1244 tower->floors = talloc_array(mem_ctx, struct epm_floor, tower->num_floors);
1247 tower->floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
1249 tower->floors[0].lhs.lhs_data = dcerpc_floor_pack_lhs_data(tower->floors, &binding->object);
1251 if (!dcerpc_floor_pack_rhs_if_version_data(
1252 tower->floors, &binding->object,
1253 &tower->floors[0].rhs.uuid.unknown)) {
1254 return NT_STATUS_NO_MEMORY;
1258 tower->floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
1260 tower->floors[1].lhs.lhs_data = dcerpc_floor_pack_lhs_data(tower->floors,
1261 &ndr_transfer_syntax_ndr);
1263 tower->floors[1].rhs.uuid.unknown = data_blob_talloc_zero(tower->floors, 2);
1265 /* Floor 2 to num_protocols */
1266 for (i = 0; i < num_protocols; i++) {
1267 tower->floors[2 + i].lhs.protocol = protseq[i];
1268 tower->floors[2 + i].lhs.lhs_data = data_blob_null;
1269 ZERO_STRUCT(tower->floors[2 + i].rhs);
1270 status = dcerpc_floor_set_rhs_data(tower->floors,
1271 &tower->floors[2 + i],
1273 if (!NT_STATUS_IS_OK(status)) {
1278 /* The 4th floor contains the endpoint */
1279 if (num_protocols >= 2 && binding->endpoint) {
1280 status = dcerpc_floor_set_rhs_data(tower->floors,
1283 if (!NT_STATUS_IS_OK(status)) {
1288 /* The 5th contains the network address */
1289 if (num_protocols >= 3 && binding->host) {
1290 status = dcerpc_floor_set_rhs_data(tower->floors,
1293 if (!NT_STATUS_IS_OK(status)) {
1298 return NT_STATUS_OK;