smbXsrv_open.c: Initialize local variable.
[Samba.git] / librpc / rpc / binding.c
blob53309163a40b1352ec8d404d10f56e26d81cfbfe
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 static const struct {
39 const char *name;
40 enum dcerpc_transport_t transport;
41 int num_protocols;
42 enum epm_protocol protseq[MAX_PROTSEQ];
43 } transports[] = {
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 {
78 const char *name;
79 uint32_t flag;
80 } ncacn_options[] = {
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},
93 {"localaddress", DCERPC_LOCALADDRESS}
96 const char *epm_floor_string(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
98 struct ndr_syntax_id syntax;
99 NTSTATUS status;
101 switch(epm_floor->lhs.protocol) {
102 case EPM_PROTOCOL_UUID:
103 status = dcerpc_floor_get_lhs_data(epm_floor, &syntax);
104 if (NT_STATUS_IS_OK(status)) {
105 /* lhs is used: UUID */
106 char *uuidstr;
108 if (GUID_equal(&syntax.uuid, &ndr_transfer_syntax_ndr.uuid)) {
109 return "NDR";
112 if (GUID_equal(&syntax.uuid, &ndr_transfer_syntax_ndr64.uuid)) {
113 return "NDR64";
116 uuidstr = GUID_string(mem_ctx, &syntax.uuid);
118 return talloc_asprintf(mem_ctx, " uuid %s/0x%02x", uuidstr, syntax.if_version);
119 } else { /* IPX */
120 return talloc_asprintf(mem_ctx, "IPX:%s",
121 data_blob_hex_string_upper(mem_ctx, &epm_floor->rhs.uuid.unknown));
124 case EPM_PROTOCOL_NCACN:
125 return "RPC-C";
127 case EPM_PROTOCOL_NCADG:
128 return "RPC";
130 case EPM_PROTOCOL_NCALRPC:
131 return "NCALRPC";
133 case EPM_PROTOCOL_DNET_NSP:
134 return "DNET/NSP";
136 case EPM_PROTOCOL_IP:
137 return talloc_asprintf(mem_ctx, "IP:%s", epm_floor->rhs.ip.ipaddr);
139 case EPM_PROTOCOL_NAMED_PIPE:
140 return talloc_asprintf(mem_ctx, "NAMED-PIPE:%s", epm_floor->rhs.named_pipe.path);
142 case EPM_PROTOCOL_SMB:
143 return talloc_asprintf(mem_ctx, "SMB:%s", epm_floor->rhs.smb.unc);
145 case EPM_PROTOCOL_UNIX_DS:
146 return talloc_asprintf(mem_ctx, "Unix:%s", epm_floor->rhs.unix_ds.path);
148 case EPM_PROTOCOL_NETBIOS:
149 return talloc_asprintf(mem_ctx, "NetBIOS:%s", epm_floor->rhs.netbios.name);
151 case EPM_PROTOCOL_NETBEUI:
152 return "NETBeui";
154 case EPM_PROTOCOL_SPX:
155 return "SPX";
157 case EPM_PROTOCOL_NB_IPX:
158 return "NB_IPX";
160 case EPM_PROTOCOL_HTTP:
161 return talloc_asprintf(mem_ctx, "HTTP:%d", epm_floor->rhs.http.port);
163 case EPM_PROTOCOL_TCP:
164 return talloc_asprintf(mem_ctx, "TCP:%d", epm_floor->rhs.tcp.port);
166 case EPM_PROTOCOL_UDP:
167 return talloc_asprintf(mem_ctx, "UDP:%d", epm_floor->rhs.udp.port);
169 default:
170 return talloc_asprintf(mem_ctx, "UNK(%02x):", epm_floor->lhs.protocol);
176 form a binding string from a binding structure
178 _PUBLIC_ char *dcerpc_binding_string(TALLOC_CTX *mem_ctx, const struct dcerpc_binding *b)
180 char *s = talloc_strdup(mem_ctx, "");
181 int i;
182 const char *t_name = NULL;
184 if (b->transport != NCA_UNKNOWN) {
185 t_name = derpc_transport_string_by_transport(b->transport);
186 if (!t_name) {
187 return NULL;
191 if (!GUID_all_zero(&b->object.uuid)) {
192 s = talloc_asprintf(s, "%s@",
193 GUID_string(mem_ctx, &b->object.uuid));
196 if (t_name != NULL) {
197 s = talloc_asprintf_append_buffer(s, "%s:", t_name);
198 if (s == NULL) {
199 return NULL;
203 if (b->host) {
204 s = talloc_asprintf_append_buffer(s, "%s", b->host);
207 if (!b->endpoint && !b->options && !b->flags) {
208 return s;
211 s = talloc_asprintf_append_buffer(s, "[");
213 if (b->endpoint) {
214 s = talloc_asprintf_append_buffer(s, "%s", b->endpoint);
217 /* this is a *really* inefficent way of dealing with strings,
218 but this is rarely called and the strings are always short,
219 so I don't care */
220 for (i=0;b->options && b->options[i];i++) {
221 s = talloc_asprintf_append_buffer(s, ",%s", b->options[i]);
222 if (!s) return NULL;
225 for (i=0;i<ARRAY_SIZE(ncacn_options);i++) {
226 if (b->flags & ncacn_options[i].flag) {
227 if (ncacn_options[i].flag == DCERPC_LOCALADDRESS && b->localaddress) {
228 s = talloc_asprintf_append_buffer(s, ",%s=%s", ncacn_options[i].name,
229 b->localaddress);
230 } else {
231 s = talloc_asprintf_append_buffer(s, ",%s", ncacn_options[i].name);
233 if (!s) return NULL;
237 s = talloc_asprintf_append_buffer(s, "]");
239 return s;
243 parse a binding string into a dcerpc_binding structure
245 _PUBLIC_ NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *s, struct dcerpc_binding **b_out)
247 struct dcerpc_binding *b;
248 char *options;
249 char *p;
250 int i, j, comma_count;
252 b = talloc_zero(mem_ctx, struct dcerpc_binding);
253 if (!b) {
254 return NT_STATUS_NO_MEMORY;
257 p = strchr(s, '@');
259 if (p && PTR_DIFF(p, s) == 36) { /* 36 is the length of a UUID */
260 NTSTATUS status;
261 DATA_BLOB blob = data_blob(s, 36);
262 status = GUID_from_data_blob(&blob, &b->object.uuid);
264 if (NT_STATUS_IS_ERR(status)) {
265 DEBUG(0, ("Failed parsing UUID\n"));
266 return status;
269 s = p + 1;
270 } else {
271 ZERO_STRUCT(b->object);
274 b->object.if_version = 0;
276 p = strchr(s, ':');
278 if (p == NULL) {
279 b->transport = NCA_UNKNOWN;
280 } else {
281 char *type = talloc_strndup(mem_ctx, s, PTR_DIFF(p, s));
282 if (!type) {
283 return NT_STATUS_NO_MEMORY;
286 for (i=0;i<ARRAY_SIZE(transports);i++) {
287 if (strcasecmp(type, transports[i].name) == 0) {
288 b->transport = transports[i].transport;
289 break;
293 if (i==ARRAY_SIZE(transports)) {
294 DEBUG(0,("Unknown dcerpc transport '%s'\n", type));
295 return NT_STATUS_INVALID_PARAMETER;
298 talloc_free(type);
300 s = p+1;
303 p = strchr(s, '[');
304 if (p) {
305 b->host = talloc_strndup(b, s, PTR_DIFF(p, s));
306 options = talloc_strdup(mem_ctx, p+1);
307 if (options[strlen(options)-1] != ']') {
308 return NT_STATUS_INVALID_PARAMETER;
310 options[strlen(options)-1] = 0;
311 } else {
312 b->host = talloc_strdup(b, s);
313 options = NULL;
315 if (!b->host) {
316 return NT_STATUS_NO_MEMORY;
319 b->target_hostname = b->host;
321 b->options = NULL;
322 b->flags = 0;
323 b->assoc_group_id = 0;
324 b->endpoint = NULL;
325 b->localaddress = NULL;
327 if (!options) {
328 *b_out = b;
329 return NT_STATUS_OK;
332 comma_count = count_chars(options, ',');
334 b->options = talloc_array(b, const char *, comma_count+2);
335 if (!b->options) {
336 return NT_STATUS_NO_MEMORY;
339 for (i=0; (p = strchr(options, ',')); i++) {
340 b->options[i] = talloc_strndup(b, options, PTR_DIFF(p, options));
341 if (!b->options[i]) {
342 return NT_STATUS_NO_MEMORY;
344 options = p+1;
346 b->options[i] = options;
347 b->options[i+1] = NULL;
349 /* some options are pre-parsed for convenience */
350 for (i=0;b->options[i];i++) {
351 for (j=0;j<ARRAY_SIZE(ncacn_options);j++) {
352 size_t opt_len = strlen(ncacn_options[j].name);
353 if (strncasecmp(ncacn_options[j].name, b->options[i], opt_len) == 0) {
354 int k;
355 char c = b->options[i][opt_len];
357 if (ncacn_options[j].flag == DCERPC_LOCALADDRESS && c == '=') {
358 b->localaddress = talloc_strdup(b, &b->options[i][opt_len+1]);
359 } else if (c != 0) {
360 continue;
363 b->flags |= ncacn_options[j].flag;
364 for (k=i;b->options[k];k++) {
365 b->options[k] = b->options[k+1];
367 i--;
368 break;
373 if (b->options[0]) {
374 /* Endpoint is first option */
375 b->endpoint = b->options[0];
376 if (strlen(b->endpoint) == 0) b->endpoint = NULL;
378 for (i=0;b->options[i];i++) {
379 b->options[i] = b->options[i+1];
383 if (b->options[0] == NULL)
384 b->options = NULL;
386 *b_out = b;
387 return NT_STATUS_OK;
390 _PUBLIC_ NTSTATUS dcerpc_floor_get_lhs_data(const struct epm_floor *epm_floor,
391 struct ndr_syntax_id *syntax)
393 TALLOC_CTX *mem_ctx = talloc_init("floor_get_lhs_data");
394 struct ndr_pull *ndr;
395 enum ndr_err_code ndr_err;
396 uint16_t if_version=0;
398 ndr = ndr_pull_init_blob(&epm_floor->lhs.lhs_data, mem_ctx);
399 if (ndr == NULL) {
400 talloc_free(mem_ctx);
401 return NT_STATUS_NO_MEMORY;
403 ndr->flags |= LIBNDR_FLAG_NOALIGN;
405 ndr_err = ndr_pull_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
406 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
407 talloc_free(mem_ctx);
408 return ndr_map_error2ntstatus(ndr_err);
411 ndr_err = ndr_pull_uint16(ndr, NDR_SCALARS, &if_version);
412 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
413 talloc_free(mem_ctx);
414 return ndr_map_error2ntstatus(ndr_err);
417 syntax->if_version = if_version;
419 talloc_free(mem_ctx);
421 return NT_STATUS_OK;
424 static DATA_BLOB dcerpc_floor_pack_lhs_data(TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax)
426 DATA_BLOB blob;
427 enum ndr_err_code ndr_err;
428 struct ndr_push *ndr;
430 ndr = ndr_push_init_ctx(mem_ctx);
431 if (ndr == NULL) {
432 return data_blob_null;
435 ndr->flags |= LIBNDR_FLAG_NOALIGN;
437 ndr_err = ndr_push_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
438 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
439 return data_blob_null;
441 ndr_err = ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version);
442 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
443 return data_blob_null;
446 blob = ndr_push_blob(ndr);
447 talloc_steal(mem_ctx, blob.data);
448 talloc_free(ndr);
449 return blob;
452 static bool dcerpc_floor_pack_rhs_if_version_data(
453 TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax,
454 DATA_BLOB *pblob)
456 DATA_BLOB blob;
457 struct ndr_push *ndr = ndr_push_init_ctx(mem_ctx);
458 enum ndr_err_code ndr_err;
460 if (ndr == NULL) {
461 return false;
464 ndr->flags |= LIBNDR_FLAG_NOALIGN;
466 ndr_err = ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version >> 16);
467 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
468 return false;
471 blob = ndr_push_blob(ndr);
472 talloc_steal(mem_ctx, blob.data);
473 talloc_free(ndr);
474 *pblob = blob;
475 return true;
478 char *dcerpc_floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
480 switch (epm_floor->lhs.protocol) {
481 case EPM_PROTOCOL_TCP:
482 if (epm_floor->rhs.tcp.port == 0) return NULL;
483 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.tcp.port);
485 case EPM_PROTOCOL_UDP:
486 if (epm_floor->rhs.udp.port == 0) return NULL;
487 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.udp.port);
489 case EPM_PROTOCOL_HTTP:
490 if (epm_floor->rhs.http.port == 0) return NULL;
491 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.http.port);
493 case EPM_PROTOCOL_IP:
494 return talloc_strdup(mem_ctx, epm_floor->rhs.ip.ipaddr);
496 case EPM_PROTOCOL_NCACN:
497 return NULL;
499 case EPM_PROTOCOL_NCADG:
500 return NULL;
502 case EPM_PROTOCOL_SMB:
503 if (strlen(epm_floor->rhs.smb.unc) == 0) return NULL;
504 return talloc_strdup(mem_ctx, epm_floor->rhs.smb.unc);
506 case EPM_PROTOCOL_NAMED_PIPE:
507 if (strlen(epm_floor->rhs.named_pipe.path) == 0) return NULL;
508 return talloc_strdup(mem_ctx, epm_floor->rhs.named_pipe.path);
510 case EPM_PROTOCOL_NETBIOS:
511 if (strlen(epm_floor->rhs.netbios.name) == 0) return NULL;
512 return talloc_strdup(mem_ctx, epm_floor->rhs.netbios.name);
514 case EPM_PROTOCOL_NCALRPC:
515 return NULL;
517 case EPM_PROTOCOL_VINES_SPP:
518 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_spp.port);
520 case EPM_PROTOCOL_VINES_IPC:
521 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_ipc.port);
523 case EPM_PROTOCOL_STREETTALK:
524 return talloc_strdup(mem_ctx, epm_floor->rhs.streettalk.streettalk);
526 case EPM_PROTOCOL_UNIX_DS:
527 if (strlen(epm_floor->rhs.unix_ds.path) == 0) return NULL;
528 return talloc_strdup(mem_ctx, epm_floor->rhs.unix_ds.path);
530 case EPM_PROTOCOL_NULL:
531 return NULL;
533 default:
534 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
535 break;
538 return NULL;
541 static NTSTATUS dcerpc_floor_set_rhs_data(TALLOC_CTX *mem_ctx,
542 struct epm_floor *epm_floor,
543 const char *data)
545 if (data == NULL) {
546 data = "";
549 switch (epm_floor->lhs.protocol) {
550 case EPM_PROTOCOL_TCP:
551 epm_floor->rhs.tcp.port = atoi(data);
552 return NT_STATUS_OK;
554 case EPM_PROTOCOL_UDP:
555 epm_floor->rhs.udp.port = atoi(data);
556 return NT_STATUS_OK;
558 case EPM_PROTOCOL_HTTP:
559 epm_floor->rhs.http.port = atoi(data);
560 return NT_STATUS_OK;
562 case EPM_PROTOCOL_IP:
563 if (!is_ipaddress_v4(data)) {
564 data = "0.0.0.0";
566 epm_floor->rhs.ip.ipaddr = talloc_strdup(mem_ctx, data);
567 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.ip.ipaddr);
568 return NT_STATUS_OK;
570 case EPM_PROTOCOL_NCACN:
571 epm_floor->rhs.ncacn.minor_version = 0;
572 return NT_STATUS_OK;
574 case EPM_PROTOCOL_NCADG:
575 epm_floor->rhs.ncadg.minor_version = 0;
576 return NT_STATUS_OK;
578 case EPM_PROTOCOL_SMB:
579 epm_floor->rhs.smb.unc = talloc_strdup(mem_ctx, data);
580 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.smb.unc);
581 return NT_STATUS_OK;
583 case EPM_PROTOCOL_NAMED_PIPE:
584 epm_floor->rhs.named_pipe.path = talloc_strdup(mem_ctx, data);
585 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.named_pipe.path);
586 return NT_STATUS_OK;
588 case EPM_PROTOCOL_NETBIOS:
589 epm_floor->rhs.netbios.name = talloc_strdup(mem_ctx, data);
590 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.netbios.name);
591 return NT_STATUS_OK;
593 case EPM_PROTOCOL_NCALRPC:
594 return NT_STATUS_OK;
596 case EPM_PROTOCOL_VINES_SPP:
597 epm_floor->rhs.vines_spp.port = atoi(data);
598 return NT_STATUS_OK;
600 case EPM_PROTOCOL_VINES_IPC:
601 epm_floor->rhs.vines_ipc.port = atoi(data);
602 return NT_STATUS_OK;
604 case EPM_PROTOCOL_STREETTALK:
605 epm_floor->rhs.streettalk.streettalk = talloc_strdup(mem_ctx, data);
606 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.streettalk.streettalk);
607 return NT_STATUS_OK;
609 case EPM_PROTOCOL_UNIX_DS:
610 epm_floor->rhs.unix_ds.path = talloc_strdup(mem_ctx, data);
611 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.unix_ds.path);
612 return NT_STATUS_OK;
614 case EPM_PROTOCOL_NULL:
615 return NT_STATUS_OK;
617 default:
618 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
619 break;
622 return NT_STATUS_NOT_SUPPORTED;
625 enum dcerpc_transport_t dcerpc_transport_by_endpoint_protocol(int prot)
627 int i;
629 /* Find a transport that has 'prot' as 4th protocol */
630 for (i=0;i<ARRAY_SIZE(transports);i++) {
631 if (transports[i].num_protocols >= 2 &&
632 transports[i].protseq[1] == prot) {
633 return transports[i].transport;
637 /* Unknown transport */
638 return (unsigned int)-1;
641 _PUBLIC_ enum dcerpc_transport_t dcerpc_transport_by_tower(const struct epm_tower *tower)
643 int i;
645 /* Find a transport that matches this tower */
646 for (i=0;i<ARRAY_SIZE(transports);i++) {
647 int j;
648 if (transports[i].num_protocols != tower->num_floors - 2) {
649 continue;
652 for (j = 0; j < transports[i].num_protocols; j++) {
653 if (transports[i].protseq[j] != tower->floors[j+2].lhs.protocol) {
654 break;
658 if (j == transports[i].num_protocols) {
659 return transports[i].transport;
663 /* Unknown transport */
664 return (unsigned int)-1;
667 _PUBLIC_ const char *derpc_transport_string_by_transport(enum dcerpc_transport_t t)
669 int i;
671 for (i=0; i<ARRAY_SIZE(transports); i++) {
672 if (t == transports[i].transport) {
673 return transports[i].name;
676 return NULL;
679 _PUBLIC_ enum dcerpc_transport_t dcerpc_transport_by_name(const char *name)
681 size_t i;
683 if (name == NULL) {
684 return NCA_UNKNOWN;
687 for (i=0; i<ARRAY_SIZE(transports);i++) {
688 if (strcasecmp(name, transports[i].name) == 0) {
689 return transports[i].transport;
693 return NCA_UNKNOWN;
696 _PUBLIC_ NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx,
697 struct epm_tower *tower,
698 struct dcerpc_binding **b_out)
700 NTSTATUS status;
701 struct dcerpc_binding *binding;
704 * A tower needs to have at least 4 floors to carry useful
705 * information. Floor 3 is the transport identifier which defines
706 * how many floors are required at least.
708 if (tower->num_floors < 4) {
709 return NT_STATUS_INVALID_PARAMETER;
712 binding = talloc_zero(mem_ctx, struct dcerpc_binding);
713 NT_STATUS_HAVE_NO_MEMORY(binding);
715 ZERO_STRUCT(binding->object);
716 binding->options = NULL;
717 binding->host = NULL;
718 binding->target_hostname = NULL;
719 binding->flags = 0;
720 binding->assoc_group_id = 0;
722 binding->transport = dcerpc_transport_by_tower(tower);
724 if (binding->transport == (unsigned int)-1) {
725 talloc_free(binding);
726 return NT_STATUS_NOT_SUPPORTED;
729 /* Set object uuid */
730 status = dcerpc_floor_get_lhs_data(&tower->floors[0], &binding->object);
732 if (!NT_STATUS_IS_OK(status)) {
733 DEBUG(1, ("Error pulling object uuid and version: %s", nt_errstr(status)));
734 talloc_free(binding);
735 return status;
738 /* Ignore floor 1, it contains the NDR version info */
740 binding->options = NULL;
742 /* Set endpoint */
743 errno = 0;
744 if (tower->num_floors >= 4) {
745 binding->endpoint = dcerpc_floor_get_rhs_data(binding, &tower->floors[3]);
747 if (errno != 0) {
748 int saved_errno = errno;
749 talloc_free(binding);
750 return map_nt_error_from_unix_common(saved_errno);
753 /* Set network address */
754 errno = 0;
755 if (tower->num_floors >= 5) {
756 binding->host = dcerpc_floor_get_rhs_data(binding, &tower->floors[4]);
758 if (errno != 0) {
759 int saved_errno = errno;
760 talloc_free(binding);
761 return map_nt_error_from_unix_common(saved_errno);
763 binding->target_hostname = binding->host;
765 *b_out = binding;
766 return NT_STATUS_OK;
769 _PUBLIC_ struct dcerpc_binding *dcerpc_binding_dup(TALLOC_CTX *mem_ctx,
770 const struct dcerpc_binding *b)
772 struct dcerpc_binding *n;
773 uint32_t count;
775 n = talloc_zero(mem_ctx, struct dcerpc_binding);
776 if (n == NULL) {
777 return NULL;
780 n->transport = b->transport;
781 n->object = b->object;
782 n->flags = b->flags;
783 n->assoc_group_id = b->assoc_group_id;
785 if (b->host != NULL) {
786 n->host = talloc_strdup(n, b->host);
787 if (n->host == NULL) {
788 talloc_free(n);
789 return NULL;
793 if (b->target_hostname != NULL) {
794 n->target_hostname = talloc_strdup(n, b->target_hostname);
795 if (n->target_hostname == NULL) {
796 talloc_free(n);
797 return NULL;
801 if (b->target_principal != NULL) {
802 n->target_principal = talloc_strdup(n, b->target_principal);
803 if (n->target_principal == NULL) {
804 talloc_free(n);
805 return NULL;
809 if (b->localaddress != NULL) {
810 n->localaddress = talloc_strdup(n, b->localaddress);
811 if (n->localaddress == NULL) {
812 talloc_free(n);
813 return NULL;
817 if (b->endpoint != NULL) {
818 n->endpoint = talloc_strdup(n, b->endpoint);
819 if (n->endpoint == NULL) {
820 talloc_free(n);
821 return NULL;
825 for (count = 0; b->options && b->options[count]; count++);
827 if (count > 0) {
828 uint32_t i;
830 n->options = talloc_array(n, const char *, count + 1);
831 if (n->options == NULL) {
832 talloc_free(n);
833 return NULL;
836 for (i = 0; i < count; i++) {
837 n->options[i] = talloc_strdup(n->options, b->options[i]);
838 if (n->options[i] == NULL) {
839 talloc_free(n);
840 return NULL;
843 n->options[count] = NULL;
846 return n;
849 _PUBLIC_ NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx,
850 const struct dcerpc_binding *binding,
851 struct epm_tower *tower)
853 const enum epm_protocol *protseq = NULL;
854 int num_protocols = -1, i;
855 NTSTATUS status;
857 /* Find transport */
858 for (i=0;i<ARRAY_SIZE(transports);i++) {
859 if (transports[i].transport == binding->transport) {
860 protseq = transports[i].protseq;
861 num_protocols = transports[i].num_protocols;
862 break;
866 if (num_protocols == -1) {
867 DEBUG(0, ("Unable to find transport with id '%d'\n", binding->transport));
868 return NT_STATUS_UNSUCCESSFUL;
871 tower->num_floors = 2 + num_protocols;
872 tower->floors = talloc_array(mem_ctx, struct epm_floor, tower->num_floors);
874 /* Floor 0 */
875 tower->floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
877 tower->floors[0].lhs.lhs_data = dcerpc_floor_pack_lhs_data(tower->floors, &binding->object);
879 if (!dcerpc_floor_pack_rhs_if_version_data(
880 tower->floors, &binding->object,
881 &tower->floors[0].rhs.uuid.unknown)) {
882 return NT_STATUS_NO_MEMORY;
885 /* Floor 1 */
886 tower->floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
888 tower->floors[1].lhs.lhs_data = dcerpc_floor_pack_lhs_data(tower->floors,
889 &ndr_transfer_syntax_ndr);
891 tower->floors[1].rhs.uuid.unknown = data_blob_talloc_zero(tower->floors, 2);
893 /* Floor 2 to num_protocols */
894 for (i = 0; i < num_protocols; i++) {
895 tower->floors[2 + i].lhs.protocol = protseq[i];
896 tower->floors[2 + i].lhs.lhs_data = data_blob_null;
897 ZERO_STRUCT(tower->floors[2 + i].rhs);
898 status = dcerpc_floor_set_rhs_data(tower->floors,
899 &tower->floors[2 + i],
900 NULL);
901 if (!NT_STATUS_IS_OK(status)) {
902 return status;
906 /* The 4th floor contains the endpoint */
907 if (num_protocols >= 2 && binding->endpoint) {
908 status = dcerpc_floor_set_rhs_data(tower->floors,
909 &tower->floors[3],
910 binding->endpoint);
911 if (!NT_STATUS_IS_OK(status)) {
912 return status;
916 /* The 5th contains the network address */
917 if (num_protocols >= 3 && binding->host) {
918 status = dcerpc_floor_set_rhs_data(tower->floors,
919 &tower->floors[4],
920 binding->host);
921 if (!NT_STATUS_IS_OK(status)) {
922 return status;
926 return NT_STATUS_OK;