dbwrap: determine basename once instead of three times
[Samba.git] / librpc / rpc / binding.c
blob63ba682a577e88be16ce5aa2af742262ef57b98d
1 /*
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/>.
26 #include "includes.h"
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"
33 #undef strcasecmp
34 #undef strncasecmp
36 #define MAX_PROTSEQ 10
38 struct dcerpc_binding {
39 enum dcerpc_transport_t transport;
40 struct GUID object;
41 const char *object_string;
42 const char *host;
43 const char *target_hostname;
44 const char *target_principal;
45 const char *endpoint;
46 const char **options;
47 uint32_t flags;
48 uint32_t assoc_group_id;
49 char assoc_group_string[11]; /* 0x3456789a + '\0' */
52 static const struct {
53 const char *name;
54 enum dcerpc_transport_t transport;
55 int num_protocols;
56 enum epm_protocol protseq[MAX_PROTSEQ];
57 } transports[] = {
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 {
92 const char *name;
93 uint32_t flag;
94 } ncacn_options[] = {
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 {"smb2", DCERPC_SMB2},
107 {"ndr64", DCERPC_NDR64},
108 {"packet", DCERPC_PACKET},
111 static const struct ncacn_option *ncacn_option_by_name(const char *name)
113 size_t i;
115 for (i=0; i<ARRAY_SIZE(ncacn_options); i++) {
116 int ret;
118 ret = strcasecmp(ncacn_options[i].name, name);
119 if (ret != 0) {
120 continue;
123 return &ncacn_options[i];
126 return NULL;
129 const char *epm_floor_string(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
131 struct ndr_syntax_id syntax;
132 NTSTATUS status;
134 switch(epm_floor->lhs.protocol) {
135 case EPM_PROTOCOL_UUID:
136 status = dcerpc_floor_get_lhs_data(epm_floor, &syntax);
137 if (NT_STATUS_IS_OK(status)) {
138 /* lhs is used: UUID */
139 char *uuidstr;
141 if (GUID_equal(&syntax.uuid, &ndr_transfer_syntax_ndr.uuid)) {
142 return "NDR";
145 if (GUID_equal(&syntax.uuid, &ndr_transfer_syntax_ndr64.uuid)) {
146 return "NDR64";
149 uuidstr = GUID_string(mem_ctx, &syntax.uuid);
151 return talloc_asprintf(mem_ctx, " uuid %s/0x%02x", uuidstr, syntax.if_version);
152 } else { /* IPX */
153 return talloc_asprintf(mem_ctx, "IPX:%s",
154 data_blob_hex_string_upper(mem_ctx, &epm_floor->rhs.uuid.unknown));
157 case EPM_PROTOCOL_NCACN:
158 return "RPC-C";
160 case EPM_PROTOCOL_NCADG:
161 return "RPC";
163 case EPM_PROTOCOL_NCALRPC:
164 return "NCALRPC";
166 case EPM_PROTOCOL_DNET_NSP:
167 return "DNET/NSP";
169 case EPM_PROTOCOL_IP:
170 return talloc_asprintf(mem_ctx, "IP:%s", epm_floor->rhs.ip.ipaddr);
172 case EPM_PROTOCOL_NAMED_PIPE:
173 return talloc_asprintf(mem_ctx, "NAMED-PIPE:%s", epm_floor->rhs.named_pipe.path);
175 case EPM_PROTOCOL_SMB:
176 return talloc_asprintf(mem_ctx, "SMB:%s", epm_floor->rhs.smb.unc);
178 case EPM_PROTOCOL_UNIX_DS:
179 return talloc_asprintf(mem_ctx, "Unix:%s", epm_floor->rhs.unix_ds.path);
181 case EPM_PROTOCOL_NETBIOS:
182 return talloc_asprintf(mem_ctx, "NetBIOS:%s", epm_floor->rhs.netbios.name);
184 case EPM_PROTOCOL_NETBEUI:
185 return "NETBeui";
187 case EPM_PROTOCOL_SPX:
188 return "SPX";
190 case EPM_PROTOCOL_NB_IPX:
191 return "NB_IPX";
193 case EPM_PROTOCOL_HTTP:
194 return talloc_asprintf(mem_ctx, "HTTP:%d", epm_floor->rhs.http.port);
196 case EPM_PROTOCOL_TCP:
197 return talloc_asprintf(mem_ctx, "TCP:%d", epm_floor->rhs.tcp.port);
199 case EPM_PROTOCOL_UDP:
200 return talloc_asprintf(mem_ctx, "UDP:%d", epm_floor->rhs.udp.port);
202 default:
203 return talloc_asprintf(mem_ctx, "UNK(%02x):", epm_floor->lhs.protocol);
209 form a binding string from a binding structure
211 _PUBLIC_ char *dcerpc_binding_string(TALLOC_CTX *mem_ctx, const struct dcerpc_binding *b)
213 char *s = talloc_strdup(mem_ctx, "");
214 char *o = s;
215 int i;
216 const char *t_name = NULL;
217 bool option_section = false;
218 const char *target_hostname = NULL;
220 if (b->transport != NCA_UNKNOWN) {
221 t_name = derpc_transport_string_by_transport(b->transport);
222 if (!t_name) {
223 talloc_free(o);
224 return NULL;
228 if (!GUID_all_zero(&b->object)) {
229 o = s;
230 s = talloc_asprintf_append_buffer(s, "%s@",
231 GUID_string(mem_ctx, &b->object));
232 if (s == NULL) {
233 talloc_free(o);
234 return NULL;
238 if (t_name != NULL) {
239 o = s;
240 s = talloc_asprintf_append_buffer(s, "%s:", t_name);
241 if (s == NULL) {
242 talloc_free(o);
243 return NULL;
247 if (b->host) {
248 o = s;
249 s = talloc_asprintf_append_buffer(s, "%s", b->host);
250 if (s == NULL) {
251 talloc_free(o);
252 return NULL;
256 target_hostname = b->target_hostname;
257 if (target_hostname != NULL && b->host != NULL) {
258 if (strcmp(target_hostname, b->host) == 0) {
259 target_hostname = NULL;
263 if (b->endpoint) {
264 option_section = true;
265 } else if (target_hostname) {
266 option_section = true;
267 } else if (b->target_principal) {
268 option_section = true;
269 } else if (b->assoc_group_id != 0) {
270 option_section = true;
271 } else if (b->options) {
272 option_section = true;
273 } else if (b->flags) {
274 option_section = true;
277 if (!option_section) {
278 return s;
281 o = s;
282 s = talloc_asprintf_append_buffer(s, "[");
283 if (s == NULL) {
284 talloc_free(o);
285 return NULL;
288 if (b->endpoint) {
289 o = s;
290 s = talloc_asprintf_append_buffer(s, "%s", b->endpoint);
291 if (s == NULL) {
292 talloc_free(o);
293 return NULL;
297 for (i=0;i<ARRAY_SIZE(ncacn_options);i++) {
298 if (!(b->flags & ncacn_options[i].flag)) {
299 continue;
302 o = s;
303 s = talloc_asprintf_append_buffer(s, ",%s", ncacn_options[i].name);
304 if (s == NULL) {
305 talloc_free(o);
306 return NULL;
310 if (target_hostname) {
311 o = s;
312 s = talloc_asprintf_append_buffer(s, ",target_hostname=%s",
313 b->target_hostname);
314 if (s == NULL) {
315 talloc_free(o);
316 return NULL;
320 if (b->target_principal) {
321 o = s;
322 s = talloc_asprintf_append_buffer(s, ",target_principal=%s",
323 b->target_principal);
324 if (s == NULL) {
325 talloc_free(o);
326 return NULL;
330 if (b->assoc_group_id != 0) {
331 o = s;
332 s = talloc_asprintf_append_buffer(s, ",assoc_group_id=0x%08x",
333 b->assoc_group_id);
334 if (s == NULL) {
335 talloc_free(o);
336 return NULL;
340 for (i=0;b->options && b->options[i];i++) {
341 o = s;
342 s = talloc_asprintf_append_buffer(s, ",%s", b->options[i]);
343 if (s == NULL) {
344 talloc_free(o);
345 return NULL;
349 o = s;
350 s = talloc_asprintf_append_buffer(s, "]");
351 if (s == NULL) {
352 talloc_free(o);
353 return NULL;
356 return s;
360 parse a binding string into a dcerpc_binding structure
362 _PUBLIC_ NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *_s, struct dcerpc_binding **b_out)
364 char *_t;
365 struct dcerpc_binding *b;
366 char *s;
367 char *options = NULL;
368 char *p;
369 size_t i;
370 NTSTATUS status;
372 b = talloc_zero(mem_ctx, struct dcerpc_binding);
373 if (!b) {
374 return NT_STATUS_NO_MEMORY;
377 _t = talloc_strdup(b, _s);
378 if (_t == NULL) {
379 talloc_free(b);
380 return NT_STATUS_NO_MEMORY;
383 s = _t;
385 p = strchr(s, '[');
386 if (p) {
387 *p = '\0';
388 options = p + 1;
389 if (options[strlen(options)-1] != ']') {
390 talloc_free(b);
391 return NT_STATUS_INVALID_PARAMETER_MIX;
393 options[strlen(options)-1] = 0;
396 p = strchr(s, '@');
398 if (p && PTR_DIFF(p, s) == 36) { /* 36 is the length of a UUID */
399 *p = '\0';
401 status = dcerpc_binding_set_string_option(b, "object", s);
402 if (!NT_STATUS_IS_OK(status)) {
403 talloc_free(b);
404 return status;
407 s = p + 1;
410 p = strchr(s, ':');
412 if (p == NULL) {
413 b->transport = NCA_UNKNOWN;
414 } else if (is_ipaddress_v6(s)) {
415 b->transport = NCA_UNKNOWN;
416 } else {
417 *p = '\0';
419 status = dcerpc_binding_set_string_option(b, "transport", s);
420 if (!NT_STATUS_IS_OK(status)) {
421 talloc_free(b);
422 return status;
425 s = p + 1;
428 if (strlen(s) > 0) {
429 status = dcerpc_binding_set_string_option(b, "host", s);
430 if (!NT_STATUS_IS_OK(status)) {
431 talloc_free(b);
432 return status;
435 b->target_hostname = talloc_strdup(b, b->host);
436 if (b->target_hostname == NULL) {
437 talloc_free(b);
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, ',');
447 if (p != NULL) {
448 *p = '\0';
449 options = p+1;
450 } else {
451 options = NULL;
454 p = strchr(name, '=');
455 if (p != NULL) {
456 *p = '\0';
457 value = p + 1;
460 if (value == NULL) {
462 * If it's not a key=value pair
463 * it might be a ncacn_option
464 * or if it's the first option
465 * it's the endpoint.
467 const struct ncacn_option *no = NULL;
469 value = name;
471 no = ncacn_option_by_name(name);
472 if (no == NULL) {
473 if (i > 0) {
475 * we don't allow unknown options
477 return NT_STATUS_INVALID_PARAMETER_MIX;
481 * This is the endpoint
483 name = "endpoint";
484 if (strlen(value) == 0) {
485 value = NULL;
490 status = dcerpc_binding_set_string_option(b, name, value);
491 if (!NT_STATUS_IS_OK(status)) {
492 talloc_free(b);
493 return status;
497 talloc_free(_t);
498 *b_out = b;
499 return NT_STATUS_OK;
502 _PUBLIC_ struct GUID dcerpc_binding_get_object(const struct dcerpc_binding *b)
504 return b->object;
507 _PUBLIC_ NTSTATUS dcerpc_binding_set_object(struct dcerpc_binding *b,
508 struct GUID object)
510 char *tmp = discard_const_p(char, b->object_string);
512 if (GUID_all_zero(&object)) {
513 talloc_free(tmp);
514 b->object_string = NULL;
515 ZERO_STRUCT(b->object);
516 return NT_STATUS_OK;
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;
524 talloc_free(tmp);
526 b->object = object;
527 return NT_STATUS_OK;
530 _PUBLIC_ enum dcerpc_transport_t dcerpc_binding_get_transport(const struct dcerpc_binding *b)
532 return b->transport;
535 _PUBLIC_ NTSTATUS dcerpc_binding_set_transport(struct dcerpc_binding *b,
536 enum dcerpc_transport_t transport)
538 NTSTATUS status;
541 * TODO: we may want to check the transport value is
542 * wellknown.
544 if (b->transport == transport) {
545 return NT_STATUS_OK;
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
556 * here.
558 status = dcerpc_binding_set_string_option(b, "endpoint", NULL);
559 if (!NT_STATUS_IS_OK(status)) {
560 return status;
563 b->assoc_group_id = 0;
565 b->transport = transport;
566 return NT_STATUS_OK;
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;
584 } else {
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;
598 } else {
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;
620 return NT_STATUS_OK;
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");
626 bool ok;
627 struct ndr_syntax_id id;
629 if (s == NULL) {
630 return ndr_syntax_id_null;
633 ok = ndr_syntax_id_from_string(s, &id);
634 if (!ok) {
635 return ndr_syntax_id_null;
638 return id;
641 _PUBLIC_ NTSTATUS dcerpc_binding_set_abstract_syntax(struct dcerpc_binding *b,
642 const struct ndr_syntax_id *syntax)
644 NTSTATUS status;
645 char *s = NULL;
647 if (syntax == NULL) {
648 status = dcerpc_binding_set_string_option(b, "abstract_syntax", NULL);
649 if (!NT_STATUS_IS_OK(status)) {
650 return status;
653 return NT_STATUS_OK;
656 if (ndr_syntax_id_equal(&ndr_syntax_id_null, syntax)) {
657 status = dcerpc_binding_set_string_option(b, "abstract_syntax", NULL);
658 if (!NT_STATUS_IS_OK(status)) {
659 return status;
662 return NT_STATUS_OK;
665 s = ndr_syntax_id_to_string(b, syntax);
666 if (s == NULL) {
667 return NT_STATUS_NO_MEMORY;
670 status = dcerpc_binding_set_string_option(b, "abstract_syntax", s);
671 TALLOC_FREE(s);
672 if (!NT_STATUS_IS_OK(status)) {
673 return status;
676 return NT_STATUS_OK;
679 _PUBLIC_ const char *dcerpc_binding_get_string_option(const struct dcerpc_binding *b,
680 const char *name)
682 struct {
683 const char *name;
684 const char *value;
685 #define _SPECIAL(x) { .name = #x, .value = b->x, }
686 } specials[] = {
687 { .name = "object", .value = b->object_string, },
688 _SPECIAL(host),
689 _SPECIAL(endpoint),
690 _SPECIAL(target_hostname),
691 _SPECIAL(target_principal),
692 #undef _SPECIAL
694 const struct ncacn_option *no = NULL;
695 size_t name_len = strlen(name);
696 size_t i;
697 int ret;
699 ret = strcmp(name, "transport");
700 if (ret == 0) {
701 return derpc_transport_string_by_transport(b->transport);
704 ret = strcmp(name, "assoc_group_id");
705 if (ret == 0) {
706 char *tmp = discard_const_p(char, b->assoc_group_string);
708 if (b->assoc_group_id == 0) {
709 return NULL;
712 snprintf(tmp, sizeof(b->assoc_group_string),
713 "0x%08x", b->assoc_group_id);
714 return (const char *)b->assoc_group_string;
717 for (i=0; i < ARRAY_SIZE(specials); i++) {
718 ret = strcmp(specials[i].name, name);
719 if (ret != 0) {
720 continue;
723 return specials[i].value;
726 no = ncacn_option_by_name(name);
727 if (no != NULL) {
728 if (b->flags & no->flag) {
729 return no->name;
732 return NULL;
735 if (b->options == NULL) {
736 return NULL;
739 for (i=0; b->options[i]; i++) {
740 const char *o = b->options[i];
741 const char *vs = NULL;
743 ret = strncmp(name, o, name_len);
744 if (ret != 0) {
745 continue;
748 if (o[name_len] != '=') {
749 continue;
752 vs = &o[name_len + 1];
754 return vs;
757 return NULL;
760 _PUBLIC_ char *dcerpc_binding_copy_string_option(TALLOC_CTX *mem_ctx,
761 const struct dcerpc_binding *b,
762 const char *name)
764 const char *c = dcerpc_binding_get_string_option(b, name);
765 char *v;
767 if (c == NULL) {
768 errno = ENOENT;
769 return NULL;
772 v = talloc_strdup(mem_ctx, c);
773 if (v == NULL) {
774 errno = ENOMEM;
775 return NULL;
778 return v;
781 _PUBLIC_ NTSTATUS dcerpc_binding_set_string_option(struct dcerpc_binding *b,
782 const char *name,
783 const char *value)
785 struct {
786 const char *name;
787 const char **ptr;
788 #define _SPECIAL(x) { .name = #x, .ptr = &b->x, }
789 } specials[] = {
790 _SPECIAL(host),
791 _SPECIAL(endpoint),
792 _SPECIAL(target_hostname),
793 _SPECIAL(target_principal),
794 #undef _SPECIAL
796 const struct ncacn_option *no = NULL;
797 size_t name_len = strlen(name);
798 const char *opt = NULL;
799 char *tmp;
800 size_t i;
801 int ret;
804 * Note: value == NULL, means delete it.
805 * value != NULL means add or reset.
808 ret = strcmp(name, "transport");
809 if (ret == 0) {
810 enum dcerpc_transport_t t = dcerpc_transport_by_name(value);
812 if (t == NCA_UNKNOWN && value != NULL) {
813 return NT_STATUS_INVALID_PARAMETER_MIX;
816 return dcerpc_binding_set_transport(b, t);
819 ret = strcmp(name, "object");
820 if (ret == 0) {
821 NTSTATUS status;
822 struct GUID uuid = GUID_zero();
824 if (value != NULL) {
825 DATA_BLOB blob;
826 blob = data_blob_string_const(value);
827 if (blob.length != 36) {
828 return NT_STATUS_INVALID_PARAMETER_MIX;
831 status = GUID_from_data_blob(&blob, &uuid);
832 if (!NT_STATUS_IS_OK(status)) {
833 return status;
837 return dcerpc_binding_set_object(b, uuid);
840 ret = strcmp(name, "assoc_group_id");
841 if (ret == 0) {
842 uint32_t assoc_group_id = 0;
844 if (value != NULL) {
845 char c;
847 ret = sscanf(value, "0x%08x%c", &assoc_group_id, &c);
848 if (ret != 1) {
849 return NT_STATUS_INVALID_PARAMETER_MIX;
853 return dcerpc_binding_set_assoc_group_id(b, assoc_group_id);
856 for (i=0; i < ARRAY_SIZE(specials); i++) {
857 ret = strcmp(specials[i].name, name);
858 if (ret != 0) {
859 continue;
862 tmp = discard_const_p(char, *specials[i].ptr);
864 if (value == NULL) {
865 talloc_free(tmp);
866 *specials[i].ptr = NULL;
867 return NT_STATUS_OK;
870 if (value[0] == '\0') {
871 return NT_STATUS_INVALID_PARAMETER_MIX;
874 *specials[i].ptr = talloc_strdup(b, value);
875 if (*specials[i].ptr == NULL) {
876 *specials[i].ptr = tmp;
877 return NT_STATUS_NO_MEMORY;
879 talloc_free(tmp);
881 return NT_STATUS_OK;
884 no = ncacn_option_by_name(name);
885 if (no != NULL) {
886 if (value == NULL) {
887 b->flags &= ~no->flag;
888 return NT_STATUS_OK;
891 ret = strcasecmp(no->name, value);
892 if (ret != 0) {
893 return NT_STATUS_INVALID_PARAMETER_MIX;
896 b->flags |= no->flag;
897 return NT_STATUS_OK;
900 for (i=0; b->options && b->options[i]; i++) {
901 const char *o = b->options[i];
903 ret = strncmp(name, o, name_len);
904 if (ret != 0) {
905 continue;
908 if (o[name_len] != '=') {
909 continue;
912 opt = o;
913 break;
916 if (opt == NULL) {
917 const char **n;
919 if (value == NULL) {
920 return NT_STATUS_OK;
923 n = talloc_realloc(b, b->options, const char *, i + 2);
924 if (n == NULL) {
925 return NT_STATUS_NO_MEMORY;
927 n[i] = NULL;
928 n[i + 1] = NULL;
929 b->options = n;
932 tmp = discard_const_p(char, opt);
934 if (value == NULL) {
935 for (;b->options[i];i++) {
936 b->options[i] = b->options[i+1];
938 talloc_free(tmp);
939 return NT_STATUS_OK;
942 b->options[i] = talloc_asprintf(b->options, "%s=%s",
943 name, value);
944 if (b->options[i] == NULL) {
945 b->options[i] = tmp;
946 return NT_STATUS_NO_MEMORY;
949 return NT_STATUS_OK;
952 _PUBLIC_ uint32_t dcerpc_binding_get_flags(const struct dcerpc_binding *b)
954 return b->flags;
957 _PUBLIC_ NTSTATUS dcerpc_binding_set_flags(struct dcerpc_binding *b,
958 uint32_t additional,
959 uint32_t clear)
962 * TODO: in future we may want to reject invalid combinations
964 b->flags &= ~clear;
965 b->flags |= additional;
967 return NT_STATUS_OK;
970 _PUBLIC_ NTSTATUS dcerpc_floor_get_lhs_data(const struct epm_floor *epm_floor,
971 struct ndr_syntax_id *syntax)
973 TALLOC_CTX *mem_ctx = talloc_init("floor_get_lhs_data");
974 struct ndr_pull *ndr;
975 enum ndr_err_code ndr_err;
976 uint16_t if_version=0;
978 ndr = ndr_pull_init_blob(&epm_floor->lhs.lhs_data, mem_ctx);
979 if (ndr == NULL) {
980 talloc_free(mem_ctx);
981 return NT_STATUS_NO_MEMORY;
983 ndr->flags |= LIBNDR_FLAG_NOALIGN;
985 ndr_err = ndr_pull_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
986 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
987 talloc_free(mem_ctx);
988 return ndr_map_error2ntstatus(ndr_err);
991 ndr_err = ndr_pull_uint16(ndr, NDR_SCALARS, &if_version);
992 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
993 talloc_free(mem_ctx);
994 return ndr_map_error2ntstatus(ndr_err);
997 syntax->if_version = if_version;
999 talloc_free(mem_ctx);
1001 return NT_STATUS_OK;
1004 static DATA_BLOB dcerpc_floor_pack_lhs_data(TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax)
1006 DATA_BLOB blob;
1007 enum ndr_err_code ndr_err;
1008 struct ndr_push *ndr;
1010 ndr = ndr_push_init_ctx(mem_ctx);
1011 if (ndr == NULL) {
1012 return data_blob_null;
1015 ndr->flags |= LIBNDR_FLAG_NOALIGN;
1017 ndr_err = ndr_push_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
1018 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1019 return data_blob_null;
1021 ndr_err = ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version);
1022 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1023 return data_blob_null;
1026 blob = ndr_push_blob(ndr);
1027 talloc_steal(mem_ctx, blob.data);
1028 talloc_free(ndr);
1029 return blob;
1032 static bool dcerpc_floor_pack_rhs_if_version_data(
1033 TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax,
1034 DATA_BLOB *pblob)
1036 DATA_BLOB blob;
1037 struct ndr_push *ndr = ndr_push_init_ctx(mem_ctx);
1038 enum ndr_err_code ndr_err;
1040 if (ndr == NULL) {
1041 return false;
1044 ndr->flags |= LIBNDR_FLAG_NOALIGN;
1046 ndr_err = ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version >> 16);
1047 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1048 return false;
1051 blob = ndr_push_blob(ndr);
1052 talloc_steal(mem_ctx, blob.data);
1053 talloc_free(ndr);
1054 *pblob = blob;
1055 return true;
1058 char *dcerpc_floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
1060 switch (epm_floor->lhs.protocol) {
1061 case EPM_PROTOCOL_TCP:
1062 if (epm_floor->rhs.tcp.port == 0) return NULL;
1063 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.tcp.port);
1065 case EPM_PROTOCOL_UDP:
1066 if (epm_floor->rhs.udp.port == 0) return NULL;
1067 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.udp.port);
1069 case EPM_PROTOCOL_HTTP:
1070 if (epm_floor->rhs.http.port == 0) return NULL;
1071 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.http.port);
1073 case EPM_PROTOCOL_IP:
1074 return talloc_strdup(mem_ctx, epm_floor->rhs.ip.ipaddr);
1076 case EPM_PROTOCOL_NCACN:
1077 return NULL;
1079 case EPM_PROTOCOL_NCADG:
1080 return NULL;
1082 case EPM_PROTOCOL_SMB:
1083 if (strlen(epm_floor->rhs.smb.unc) == 0) return NULL;
1084 return talloc_strdup(mem_ctx, epm_floor->rhs.smb.unc);
1086 case EPM_PROTOCOL_NAMED_PIPE:
1087 if (strlen(epm_floor->rhs.named_pipe.path) == 0) return NULL;
1088 return talloc_strdup(mem_ctx, epm_floor->rhs.named_pipe.path);
1090 case EPM_PROTOCOL_NETBIOS:
1091 if (strlen(epm_floor->rhs.netbios.name) == 0) return NULL;
1092 return talloc_strdup(mem_ctx, epm_floor->rhs.netbios.name);
1094 case EPM_PROTOCOL_NCALRPC:
1095 return NULL;
1097 case EPM_PROTOCOL_VINES_SPP:
1098 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_spp.port);
1100 case EPM_PROTOCOL_VINES_IPC:
1101 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_ipc.port);
1103 case EPM_PROTOCOL_STREETTALK:
1104 return talloc_strdup(mem_ctx, epm_floor->rhs.streettalk.streettalk);
1106 case EPM_PROTOCOL_UNIX_DS:
1107 if (strlen(epm_floor->rhs.unix_ds.path) == 0) return NULL;
1108 return talloc_strdup(mem_ctx, epm_floor->rhs.unix_ds.path);
1110 case EPM_PROTOCOL_NULL:
1111 return NULL;
1113 default:
1114 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
1115 break;
1118 return NULL;
1121 static NTSTATUS dcerpc_floor_set_rhs_data(TALLOC_CTX *mem_ctx,
1122 struct epm_floor *epm_floor,
1123 const char *data)
1125 if (data == NULL) {
1126 data = "";
1129 switch (epm_floor->lhs.protocol) {
1130 case EPM_PROTOCOL_TCP:
1131 epm_floor->rhs.tcp.port = atoi(data);
1132 return NT_STATUS_OK;
1134 case EPM_PROTOCOL_UDP:
1135 epm_floor->rhs.udp.port = atoi(data);
1136 return NT_STATUS_OK;
1138 case EPM_PROTOCOL_HTTP:
1139 epm_floor->rhs.http.port = atoi(data);
1140 return NT_STATUS_OK;
1142 case EPM_PROTOCOL_IP:
1143 if (!is_ipaddress_v4(data)) {
1144 data = "0.0.0.0";
1146 epm_floor->rhs.ip.ipaddr = talloc_strdup(mem_ctx, data);
1147 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.ip.ipaddr);
1148 return NT_STATUS_OK;
1150 case EPM_PROTOCOL_NCACN:
1151 epm_floor->rhs.ncacn.minor_version = 0;
1152 return NT_STATUS_OK;
1154 case EPM_PROTOCOL_NCADG:
1155 epm_floor->rhs.ncadg.minor_version = 0;
1156 return NT_STATUS_OK;
1158 case EPM_PROTOCOL_SMB:
1159 epm_floor->rhs.smb.unc = talloc_strdup(mem_ctx, data);
1160 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.smb.unc);
1161 return NT_STATUS_OK;
1163 case EPM_PROTOCOL_NAMED_PIPE:
1164 epm_floor->rhs.named_pipe.path = talloc_strdup(mem_ctx, data);
1165 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.named_pipe.path);
1166 return NT_STATUS_OK;
1168 case EPM_PROTOCOL_NETBIOS:
1169 epm_floor->rhs.netbios.name = talloc_strdup(mem_ctx, data);
1170 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.netbios.name);
1171 return NT_STATUS_OK;
1173 case EPM_PROTOCOL_NCALRPC:
1174 return NT_STATUS_OK;
1176 case EPM_PROTOCOL_VINES_SPP:
1177 epm_floor->rhs.vines_spp.port = atoi(data);
1178 return NT_STATUS_OK;
1180 case EPM_PROTOCOL_VINES_IPC:
1181 epm_floor->rhs.vines_ipc.port = atoi(data);
1182 return NT_STATUS_OK;
1184 case EPM_PROTOCOL_STREETTALK:
1185 epm_floor->rhs.streettalk.streettalk = talloc_strdup(mem_ctx, data);
1186 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.streettalk.streettalk);
1187 return NT_STATUS_OK;
1189 case EPM_PROTOCOL_UNIX_DS:
1190 epm_floor->rhs.unix_ds.path = talloc_strdup(mem_ctx, data);
1191 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.unix_ds.path);
1192 return NT_STATUS_OK;
1194 case EPM_PROTOCOL_NULL:
1195 return NT_STATUS_OK;
1197 default:
1198 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
1199 break;
1202 return NT_STATUS_NOT_SUPPORTED;
1205 enum dcerpc_transport_t dcerpc_transport_by_endpoint_protocol(int prot)
1207 int i;
1209 /* Find a transport that has 'prot' as 4th protocol */
1210 for (i=0;i<ARRAY_SIZE(transports);i++) {
1211 if (transports[i].num_protocols >= 2 &&
1212 transports[i].protseq[1] == prot) {
1213 return transports[i].transport;
1217 /* Unknown transport */
1218 return (unsigned int)-1;
1221 _PUBLIC_ enum dcerpc_transport_t dcerpc_transport_by_tower(const struct epm_tower *tower)
1223 int i;
1225 /* Find a transport that matches this tower */
1226 for (i=0;i<ARRAY_SIZE(transports);i++) {
1227 int j;
1228 if (transports[i].num_protocols != tower->num_floors - 2) {
1229 continue;
1232 for (j = 0; j < transports[i].num_protocols && j < MAX_PROTSEQ; j++) {
1233 if (transports[i].protseq[j] != tower->floors[j+2].lhs.protocol) {
1234 break;
1238 if (j == transports[i].num_protocols) {
1239 return transports[i].transport;
1243 /* Unknown transport */
1244 return (unsigned int)-1;
1247 _PUBLIC_ const char *derpc_transport_string_by_transport(enum dcerpc_transport_t t)
1249 int i;
1251 for (i=0; i<ARRAY_SIZE(transports); i++) {
1252 if (t == transports[i].transport) {
1253 return transports[i].name;
1256 return NULL;
1259 _PUBLIC_ enum dcerpc_transport_t dcerpc_transport_by_name(const char *name)
1261 size_t i;
1263 if (name == NULL) {
1264 return NCA_UNKNOWN;
1267 for (i=0; i<ARRAY_SIZE(transports);i++) {
1268 if (strcasecmp(name, transports[i].name) == 0) {
1269 return transports[i].transport;
1273 return NCA_UNKNOWN;
1276 _PUBLIC_ NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx,
1277 struct epm_tower *tower,
1278 struct dcerpc_binding **b_out)
1280 NTSTATUS status;
1281 struct dcerpc_binding *b;
1282 enum dcerpc_transport_t transport;
1283 struct ndr_syntax_id abstract_syntax;
1284 char *endpoint = NULL;
1285 char *host = NULL;
1288 * A tower needs to have at least 4 floors to carry useful
1289 * information. Floor 3 is the transport identifier which defines
1290 * how many floors are required at least.
1292 if (tower->num_floors < 4) {
1293 return NT_STATUS_INVALID_PARAMETER;
1296 status = dcerpc_parse_binding(mem_ctx, "", &b);
1297 if (!NT_STATUS_IS_OK(status)) {
1298 return status;
1301 transport = dcerpc_transport_by_tower(tower);
1302 if (transport == NCA_UNKNOWN) {
1303 talloc_free(b);
1304 return NT_STATUS_NOT_SUPPORTED;
1307 status = dcerpc_binding_set_transport(b, transport);
1308 if (!NT_STATUS_IS_OK(status)) {
1309 talloc_free(b);
1310 return status;
1313 /* Set abstract syntax */
1314 status = dcerpc_floor_get_lhs_data(&tower->floors[0], &abstract_syntax);
1315 if (!NT_STATUS_IS_OK(status)) {
1316 talloc_free(b);
1317 return status;
1320 status = dcerpc_binding_set_abstract_syntax(b, &abstract_syntax);
1321 if (!NT_STATUS_IS_OK(status)) {
1322 talloc_free(b);
1323 return status;
1326 /* Ignore floor 1, it contains the NDR version info */
1328 /* Set endpoint */
1329 errno = 0;
1330 if (tower->num_floors >= 4) {
1331 endpoint = dcerpc_floor_get_rhs_data(b, &tower->floors[3]);
1333 if (errno != 0) {
1334 int saved_errno = errno;
1335 talloc_free(b);
1336 return map_nt_error_from_unix_common(saved_errno);
1339 status = dcerpc_binding_set_string_option(b, "endpoint", endpoint);
1340 if (!NT_STATUS_IS_OK(status)) {
1341 talloc_free(b);
1342 return status;
1344 TALLOC_FREE(endpoint);
1346 /* Set network address */
1347 errno = 0;
1348 if (tower->num_floors >= 5) {
1349 host = dcerpc_floor_get_rhs_data(b, &tower->floors[4]);
1351 if (errno != 0) {
1352 int saved_errno = errno;
1353 talloc_free(b);
1354 return map_nt_error_from_unix_common(saved_errno);
1357 status = dcerpc_binding_set_string_option(b, "host", host);
1358 if (!NT_STATUS_IS_OK(status)) {
1359 talloc_free(b);
1360 return status;
1362 status = dcerpc_binding_set_string_option(b, "target_hostname", host);
1363 if (!NT_STATUS_IS_OK(status)) {
1364 talloc_free(b);
1365 return status;
1367 TALLOC_FREE(host);
1369 *b_out = b;
1370 return NT_STATUS_OK;
1373 _PUBLIC_ struct dcerpc_binding *dcerpc_binding_dup(TALLOC_CTX *mem_ctx,
1374 const struct dcerpc_binding *b)
1376 struct dcerpc_binding *n;
1377 uint32_t count;
1379 n = talloc_zero(mem_ctx, struct dcerpc_binding);
1380 if (n == NULL) {
1381 return NULL;
1384 n->transport = b->transport;
1385 n->object = b->object;
1386 n->flags = b->flags;
1387 n->assoc_group_id = b->assoc_group_id;
1389 if (b->object_string != NULL) {
1390 n->object_string = talloc_strdup(n, b->object_string);
1391 if (n->object_string == NULL) {
1392 talloc_free(n);
1393 return NULL;
1396 if (b->host != NULL) {
1397 n->host = talloc_strdup(n, b->host);
1398 if (n->host == NULL) {
1399 talloc_free(n);
1400 return NULL;
1404 if (b->target_hostname != NULL) {
1405 n->target_hostname = talloc_strdup(n, b->target_hostname);
1406 if (n->target_hostname == NULL) {
1407 talloc_free(n);
1408 return NULL;
1412 if (b->target_principal != NULL) {
1413 n->target_principal = talloc_strdup(n, b->target_principal);
1414 if (n->target_principal == NULL) {
1415 talloc_free(n);
1416 return NULL;
1420 if (b->endpoint != NULL) {
1421 n->endpoint = talloc_strdup(n, b->endpoint);
1422 if (n->endpoint == NULL) {
1423 talloc_free(n);
1424 return NULL;
1428 for (count = 0; b->options && b->options[count]; count++);
1430 if (count > 0) {
1431 uint32_t i;
1433 n->options = talloc_array(n, const char *, count + 1);
1434 if (n->options == NULL) {
1435 talloc_free(n);
1436 return NULL;
1439 for (i = 0; i < count; i++) {
1440 n->options[i] = talloc_strdup(n->options, b->options[i]);
1441 if (n->options[i] == NULL) {
1442 talloc_free(n);
1443 return NULL;
1446 n->options[count] = NULL;
1449 return n;
1452 _PUBLIC_ NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx,
1453 const struct dcerpc_binding *binding,
1454 struct epm_tower *tower)
1456 const enum epm_protocol *protseq = NULL;
1457 int num_protocols = -1, i;
1458 struct ndr_syntax_id abstract_syntax;
1459 NTSTATUS status;
1461 /* Find transport */
1462 for (i=0;i<ARRAY_SIZE(transports);i++) {
1463 if (transports[i].transport == binding->transport) {
1464 protseq = transports[i].protseq;
1465 num_protocols = transports[i].num_protocols;
1466 break;
1470 if (num_protocols == -1) {
1471 DEBUG(0, ("Unable to find transport with id '%d'\n", binding->transport));
1472 return NT_STATUS_UNSUCCESSFUL;
1475 tower->num_floors = 2 + num_protocols;
1476 tower->floors = talloc_array(mem_ctx, struct epm_floor, tower->num_floors);
1478 /* Floor 0 */
1479 tower->floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
1481 abstract_syntax = dcerpc_binding_get_abstract_syntax(binding);
1482 tower->floors[0].lhs.lhs_data = dcerpc_floor_pack_lhs_data(tower->floors,
1483 &abstract_syntax);
1485 if (!dcerpc_floor_pack_rhs_if_version_data(
1486 tower->floors, &abstract_syntax,
1487 &tower->floors[0].rhs.uuid.unknown)) {
1488 return NT_STATUS_NO_MEMORY;
1491 /* Floor 1 */
1492 tower->floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
1494 tower->floors[1].lhs.lhs_data = dcerpc_floor_pack_lhs_data(tower->floors,
1495 &ndr_transfer_syntax_ndr);
1497 tower->floors[1].rhs.uuid.unknown = data_blob_talloc_zero(tower->floors, 2);
1499 /* Floor 2 to num_protocols */
1500 for (i = 0; i < num_protocols; i++) {
1501 tower->floors[2 + i].lhs.protocol = protseq[i];
1502 tower->floors[2 + i].lhs.lhs_data = data_blob_null;
1503 ZERO_STRUCT(tower->floors[2 + i].rhs);
1504 status = dcerpc_floor_set_rhs_data(tower->floors,
1505 &tower->floors[2 + i],
1506 NULL);
1507 if (!NT_STATUS_IS_OK(status)) {
1508 return status;
1512 /* The 4th floor contains the endpoint */
1513 if (num_protocols >= 2 && binding->endpoint) {
1514 status = dcerpc_floor_set_rhs_data(tower->floors,
1515 &tower->floors[3],
1516 binding->endpoint);
1517 if (!NT_STATUS_IS_OK(status)) {
1518 return status;
1522 /* The 5th contains the network address */
1523 if (num_protocols >= 3 && binding->host) {
1524 status = dcerpc_floor_set_rhs_data(tower->floors,
1525 &tower->floors[4],
1526 binding->host);
1527 if (!NT_STATUS_IS_OK(status)) {
1528 return status;
1532 return NT_STATUS_OK;