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
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #define MAX_PROTSEQ 10
31 enum dcerpc_transport_t transport;
33 enum epm_protocol protseq[MAX_PROTSEQ];
35 { "ncacn_np", NCACN_NP, 3,
36 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SMB, EPM_PROTOCOL_NETBIOS }},
37 { "ncacn_ip_tcp", NCACN_IP_TCP, 3,
38 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_TCP, EPM_PROTOCOL_IP } },
39 { "ncacn_http", NCACN_HTTP, 3,
40 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_HTTP, EPM_PROTOCOL_IP } },
41 { "ncadg_ip_udp", NCACN_IP_UDP, 3,
42 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UDP, EPM_PROTOCOL_IP } },
43 { "ncalrpc", NCALRPC, 2,
44 { EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_PIPE } },
45 { "ncacn_unix_stream", NCACN_UNIX_STREAM, 2,
46 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_UNIX_DS } },
47 { "ncadg_unix_dgram", NCADG_UNIX_DGRAM, 2,
48 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UNIX_DS } },
49 { "ncacn_at_dsp", NCACN_AT_DSP, 3,
50 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DSP } },
51 { "ncadg_at_ddp", NCADG_AT_DDP, 3,
52 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DDP } },
53 { "ncacn_vns_ssp", NCACN_VNS_SPP, 3,
54 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_SPP } },
55 { "ncacn_vns_ipc", NCACN_VNS_IPC, 3,
56 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_IPC }, },
57 { "ncadg_ipx", NCADG_IPX, 2,
58 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_IPX },
60 { "ncacn_spx", NCACN_SPX, 3,
61 /* I guess some MS programmer confused the identifier for
62 * EPM_PROTOCOL_UUID (0x0D or 13) with the one for
63 * EPM_PROTOCOL_SPX (0x13) here. -- jelmer*/
64 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_UUID },
75 const char *epm_floor_string(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
77 struct ndr_syntax_id syntax;
80 switch(epm_floor->lhs.protocol) {
81 case EPM_PROTOCOL_UUID:
82 status = dcerpc_floor_get_lhs_data(epm_floor, &syntax);
83 if (NT_STATUS_IS_OK(status)) {
84 /* lhs is used: UUID */
87 if (GUID_equal(&syntax.uuid, &ndr_transfer_syntax.uuid)) {
91 if (GUID_equal(&syntax.uuid, &ndr64_transfer_syntax.uuid)) {
95 uuidstr = GUID_string(mem_ctx, &syntax.uuid);
97 return talloc_asprintf(mem_ctx, " uuid %s/0x%02x", uuidstr, syntax.if_version);
102 case EPM_PROTOCOL_NCACN:
105 case EPM_PROTOCOL_NCADG:
108 case EPM_PROTOCOL_NCALRPC:
111 case EPM_PROTOCOL_DNET_NSP:
114 case EPM_PROTOCOL_IP:
115 return talloc_asprintf(mem_ctx, "IP:%s", epm_floor->rhs.ip.ipaddr);
117 case EPM_PROTOCOL_PIPE:
118 return talloc_asprintf(mem_ctx, "PIPE:%s", epm_floor->rhs.pipe.path);
120 case EPM_PROTOCOL_SMB:
121 return talloc_asprintf(mem_ctx, "SMB:%s", epm_floor->rhs.smb.unc);
123 case EPM_PROTOCOL_UNIX_DS:
124 return talloc_asprintf(mem_ctx, "Unix:%s", epm_floor->rhs.unix_ds.path);
126 case EPM_PROTOCOL_NETBIOS:
127 return talloc_asprintf(mem_ctx, "NetBIOS:%s", epm_floor->rhs.netbios.name);
129 case EPM_PROTOCOL_NETBEUI:
132 case EPM_PROTOCOL_SPX:
135 case EPM_PROTOCOL_NB_IPX:
138 case EPM_PROTOCOL_HTTP:
139 return talloc_asprintf(mem_ctx, "HTTP:%d", epm_floor->rhs.http.port);
141 case EPM_PROTOCOL_TCP:
142 return talloc_asprintf(mem_ctx, "TCP:%d", epm_floor->rhs.tcp.port);
144 case EPM_PROTOCOL_UDP:
145 return talloc_asprintf(mem_ctx, "UDP:%d", epm_floor->rhs.udp.port);
148 return talloc_asprintf(mem_ctx, "UNK(%02x):", epm_floor->lhs.protocol);
154 form a binding string from a binding structure
156 _PUBLIC_ char *dcerpc_binding_string(TALLOC_CTX *mem_ctx, const struct dcerpc_binding *b)
158 char *s = talloc_strdup(mem_ctx, "");
160 const char *t_name = NULL;
162 if (b->transport != NCA_UNKNOWN) {
163 for (i=0;i<ARRAY_SIZE(transports);i++) {
164 if (transports[i].transport == b->transport) {
165 t_name = transports[i].name;
173 if (!GUID_all_zero(&b->object.uuid)) {
174 s = talloc_asprintf(s, "%s@",
175 GUID_string(mem_ctx, &b->object.uuid));
178 if (t_name != NULL) {
179 s = talloc_asprintf_append_buffer(s, "%s:", t_name);
186 s = talloc_asprintf_append_buffer(s, "%s", b->host);
189 if (!b->endpoint && !b->options && !b->flags) {
193 s = talloc_asprintf_append_buffer(s, "[");
196 s = talloc_asprintf_append_buffer(s, "%s", b->endpoint);
199 /* this is a *really* inefficent way of dealing with strings,
200 but this is rarely called and the strings are always short,
202 for (i=0;b->options && b->options[i];i++) {
203 s = talloc_asprintf_append_buffer(s, ",%s", b->options[i]);
207 for (i=0;i<ARRAY_SIZE(ncacn_options);i++) {
208 if (b->flags & ncacn_options[i].flag) {
209 s = talloc_asprintf_append_buffer(s, ",%s", ncacn_options[i].name);
214 s = talloc_asprintf_append_buffer(s, "]");
220 parse a binding string into a dcerpc_binding structure
222 _PUBLIC_ NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *s, struct dcerpc_binding **b_out)
224 struct dcerpc_binding *b;
227 int i, j, comma_count;
229 b = talloc(mem_ctx, struct dcerpc_binding);
231 return NT_STATUS_NO_MEMORY;
236 if (p && PTR_DIFF(p, s) == 36) { /* 36 is the length of a UUID */
239 status = GUID_from_string(s, &b->object.uuid);
241 if (NT_STATUS_IS_ERR(status)) {
242 DEBUG(0, ("Failed parsing UUID\n"));
248 ZERO_STRUCT(b->object);
251 b->object.if_version = 0;
256 b->transport = NCA_UNKNOWN;
258 char *type = talloc_strndup(mem_ctx, s, PTR_DIFF(p, s));
260 return NT_STATUS_NO_MEMORY;
263 for (i=0;i<ARRAY_SIZE(transports);i++) {
264 if (strcmp(type, transports[i].name) == 0) {
265 b->transport = transports[i].transport;
270 if (i==ARRAY_SIZE(transports)) {
271 DEBUG(0,("Unknown dcerpc transport '%s'\n", type));
272 return NT_STATUS_INVALID_PARAMETER;
282 b->host = talloc_strndup(b, s, PTR_DIFF(p, s));
283 options = talloc_strdup(mem_ctx, p+1);
284 if (options[strlen(options)-1] != ']') {
285 return NT_STATUS_INVALID_PARAMETER;
287 options[strlen(options)-1] = 0;
289 b->host = talloc_strdup(b, s);
293 return NT_STATUS_NO_MEMORY;
296 b->target_hostname = b->host;
300 b->assoc_group_id = 0;
308 comma_count = count_chars(options, ',');
310 b->options = talloc_array(b, const char *, comma_count+2);
312 return NT_STATUS_NO_MEMORY;
315 for (i=0; (p = strchr(options, ',')); i++) {
316 b->options[i] = talloc_strndup(b, options, PTR_DIFF(p, options));
317 if (!b->options[i]) {
318 return NT_STATUS_NO_MEMORY;
322 b->options[i] = options;
323 b->options[i+1] = NULL;
325 /* some options are pre-parsed for convenience */
326 for (i=0;b->options[i];i++) {
327 for (j=0;j<ARRAY_SIZE(ncacn_options);j++) {
328 if (strcmp(ncacn_options[j].name, b->options[i]) == 0) {
330 b->flags |= ncacn_options[j].flag;
331 for (k=i;b->options[k];k++) {
332 b->options[k] = b->options[k+1];
341 /* Endpoint is first option */
342 b->endpoint = b->options[0];
343 if (strlen(b->endpoint) == 0) b->endpoint = NULL;
345 for (i=0;b->options[i];i++) {
346 b->options[i] = b->options[i+1];
350 if (b->options[0] == NULL)
357 _PUBLIC_ NTSTATUS dcerpc_floor_get_lhs_data(struct epm_floor *epm_floor, struct ndr_syntax_id *syntax)
359 TALLOC_CTX *mem_ctx = talloc_init("floor_get_lhs_data");
360 struct ndr_pull *ndr = ndr_pull_init_blob(&epm_floor->lhs.lhs_data, mem_ctx, NULL);
361 enum ndr_err_code ndr_err;
362 uint16_t if_version=0;
364 ndr->flags |= LIBNDR_FLAG_NOALIGN;
366 ndr_err = ndr_pull_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
367 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
368 talloc_free(mem_ctx);
369 return ndr_map_error2ntstatus(ndr_err);
372 ndr_err = ndr_pull_uint16(ndr, NDR_SCALARS, &if_version);
373 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
374 talloc_free(mem_ctx);
375 return ndr_map_error2ntstatus(ndr_err);
378 syntax->if_version = if_version;
380 talloc_free(mem_ctx);
385 static DATA_BLOB dcerpc_floor_pack_lhs_data(TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax)
387 struct ndr_push *ndr = ndr_push_init_ctx(mem_ctx);
389 ndr->flags |= LIBNDR_FLAG_NOALIGN;
391 ndr_push_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
392 ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version);
394 return ndr_push_blob(ndr);
397 const char *dcerpc_floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
399 switch (epm_floor->lhs.protocol) {
400 case EPM_PROTOCOL_TCP:
401 if (epm_floor->rhs.tcp.port == 0) return NULL;
402 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.tcp.port);
404 case EPM_PROTOCOL_UDP:
405 if (epm_floor->rhs.udp.port == 0) return NULL;
406 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.udp.port);
408 case EPM_PROTOCOL_HTTP:
409 if (epm_floor->rhs.http.port == 0) return NULL;
410 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.http.port);
412 case EPM_PROTOCOL_IP:
413 return talloc_strdup(mem_ctx, epm_floor->rhs.ip.ipaddr);
415 case EPM_PROTOCOL_NCACN:
418 case EPM_PROTOCOL_NCADG:
421 case EPM_PROTOCOL_SMB:
422 if (strlen(epm_floor->rhs.smb.unc) == 0) return NULL;
423 return talloc_strdup(mem_ctx, epm_floor->rhs.smb.unc);
425 case EPM_PROTOCOL_PIPE:
426 if (strlen(epm_floor->rhs.pipe.path) == 0) return NULL;
427 return talloc_strdup(mem_ctx, epm_floor->rhs.pipe.path);
429 case EPM_PROTOCOL_NETBIOS:
430 if (strlen(epm_floor->rhs.netbios.name) == 0) return NULL;
431 return talloc_strdup(mem_ctx, epm_floor->rhs.netbios.name);
433 case EPM_PROTOCOL_NCALRPC:
436 case EPM_PROTOCOL_VINES_SPP:
437 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_spp.port);
439 case EPM_PROTOCOL_VINES_IPC:
440 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_ipc.port);
442 case EPM_PROTOCOL_STREETTALK:
443 return talloc_strdup(mem_ctx, epm_floor->rhs.streettalk.streettalk);
445 case EPM_PROTOCOL_UNIX_DS:
446 if (strlen(epm_floor->rhs.unix_ds.path) == 0) return NULL;
447 return talloc_strdup(mem_ctx, epm_floor->rhs.unix_ds.path);
449 case EPM_PROTOCOL_NULL:
453 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
460 static NTSTATUS dcerpc_floor_set_rhs_data(TALLOC_CTX *mem_ctx,
461 struct epm_floor *epm_floor,
464 switch (epm_floor->lhs.protocol) {
465 case EPM_PROTOCOL_TCP:
466 epm_floor->rhs.tcp.port = atoi(data);
469 case EPM_PROTOCOL_UDP:
470 epm_floor->rhs.udp.port = atoi(data);
473 case EPM_PROTOCOL_HTTP:
474 epm_floor->rhs.http.port = atoi(data);
477 case EPM_PROTOCOL_IP:
478 epm_floor->rhs.ip.ipaddr = talloc_strdup(mem_ctx, data);
479 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.ip.ipaddr);
482 case EPM_PROTOCOL_NCACN:
483 epm_floor->rhs.ncacn.minor_version = 0;
486 case EPM_PROTOCOL_NCADG:
487 epm_floor->rhs.ncadg.minor_version = 0;
490 case EPM_PROTOCOL_SMB:
491 epm_floor->rhs.smb.unc = talloc_strdup(mem_ctx, data);
492 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.smb.unc);
495 case EPM_PROTOCOL_PIPE:
496 epm_floor->rhs.pipe.path = talloc_strdup(mem_ctx, data);
497 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.pipe.path);
500 case EPM_PROTOCOL_NETBIOS:
501 epm_floor->rhs.netbios.name = talloc_strdup(mem_ctx, data);
502 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.netbios.name);
505 case EPM_PROTOCOL_NCALRPC:
508 case EPM_PROTOCOL_VINES_SPP:
509 epm_floor->rhs.vines_spp.port = atoi(data);
512 case EPM_PROTOCOL_VINES_IPC:
513 epm_floor->rhs.vines_ipc.port = atoi(data);
516 case EPM_PROTOCOL_STREETTALK:
517 epm_floor->rhs.streettalk.streettalk = talloc_strdup(mem_ctx, data);
518 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.streettalk.streettalk);
521 case EPM_PROTOCOL_UNIX_DS:
522 epm_floor->rhs.unix_ds.path = talloc_strdup(mem_ctx, data);
523 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.unix_ds.path);
526 case EPM_PROTOCOL_NULL:
530 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
534 return NT_STATUS_NOT_SUPPORTED;
537 enum dcerpc_transport_t dcerpc_transport_by_endpoint_protocol(int prot)
541 /* Find a transport that has 'prot' as 4th protocol */
542 for (i=0;i<ARRAY_SIZE(transports);i++) {
543 if (transports[i].num_protocols >= 2 &&
544 transports[i].protseq[1] == prot) {
545 return transports[i].transport;
549 /* Unknown transport */
550 return (unsigned int)-1;
553 _PUBLIC_ enum dcerpc_transport_t dcerpc_transport_by_tower(struct epm_tower *tower)
557 /* Find a transport that matches this tower */
558 for (i=0;i<ARRAY_SIZE(transports);i++) {
560 if (transports[i].num_protocols != tower->num_floors - 2) {
564 for (j = 0; j < transports[i].num_protocols; j++) {
565 if (transports[i].protseq[j] != tower->floors[j+2].lhs.protocol) {
570 if (j == transports[i].num_protocols) {
571 return transports[i].transport;
575 /* Unknown transport */
576 return (unsigned int)-1;
579 _PUBLIC_ NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx,
580 struct epm_tower *tower,
581 struct dcerpc_binding **b_out)
584 struct dcerpc_binding *binding;
586 binding = talloc(mem_ctx, struct dcerpc_binding);
587 NT_STATUS_HAVE_NO_MEMORY(binding);
589 ZERO_STRUCT(binding->object);
590 binding->options = NULL;
591 binding->host = NULL;
592 binding->target_hostname = NULL;
594 binding->assoc_group_id = 0;
596 binding->transport = dcerpc_transport_by_tower(tower);
598 if (binding->transport == (unsigned int)-1) {
599 return NT_STATUS_NOT_SUPPORTED;
602 if (tower->num_floors < 1) {
606 /* Set object uuid */
607 status = dcerpc_floor_get_lhs_data(&tower->floors[0], &binding->object);
609 if (!NT_STATUS_IS_OK(status)) {
610 DEBUG(1, ("Error pulling object uuid and version: %s", nt_errstr(status)));
614 /* Ignore floor 1, it contains the NDR version info */
616 binding->options = NULL;
619 if (tower->num_floors >= 4) {
620 binding->endpoint = dcerpc_floor_get_rhs_data(mem_ctx, &tower->floors[3]);
622 binding->endpoint = NULL;
625 /* Set network address */
626 if (tower->num_floors >= 5) {
627 binding->host = dcerpc_floor_get_rhs_data(mem_ctx, &tower->floors[4]);
628 NT_STATUS_HAVE_NO_MEMORY(binding->host);
629 binding->target_hostname = binding->host;
635 _PUBLIC_ NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx, struct dcerpc_binding *binding, struct epm_tower *tower)
637 const enum epm_protocol *protseq = NULL;
638 int num_protocols = -1, i;
642 for (i=0;i<ARRAY_SIZE(transports);i++) {
643 if (transports[i].transport == binding->transport) {
644 protseq = transports[i].protseq;
645 num_protocols = transports[i].num_protocols;
650 if (num_protocols == -1) {
651 DEBUG(0, ("Unable to find transport with id '%d'\n", binding->transport));
652 return NT_STATUS_UNSUCCESSFUL;
655 tower->num_floors = 2 + num_protocols;
656 tower->floors = talloc_array(mem_ctx, struct epm_floor, tower->num_floors);
659 tower->floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
661 tower->floors[0].lhs.lhs_data = dcerpc_floor_pack_lhs_data(mem_ctx, &binding->object);
663 tower->floors[0].rhs.uuid.unknown = data_blob_talloc_zero(mem_ctx, 2);
666 tower->floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
668 tower->floors[1].lhs.lhs_data = dcerpc_floor_pack_lhs_data(mem_ctx,
669 &ndr_transfer_syntax);
671 tower->floors[1].rhs.uuid.unknown = data_blob_talloc_zero(mem_ctx, 2);
673 /* Floor 2 to num_protocols */
674 for (i = 0; i < num_protocols; i++) {
675 tower->floors[2 + i].lhs.protocol = protseq[i];
676 tower->floors[2 + i].lhs.lhs_data = data_blob_talloc(mem_ctx, NULL, 0);
677 ZERO_STRUCT(tower->floors[2 + i].rhs);
678 dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[2 + i], "");
681 /* The 4th floor contains the endpoint */
682 if (num_protocols >= 2 && binding->endpoint) {
683 status = dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[3], binding->endpoint);
684 if (NT_STATUS_IS_ERR(status)) {
689 /* The 5th contains the network address */
690 if (num_protocols >= 3 && binding->host) {
691 if (is_ipaddress(binding->host)) {
692 status = dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[4],
695 /* note that we don't attempt to resolve the
696 name here - when we get a hostname here we
697 are in the client code, and want to put in
698 a wildcard all-zeros IP for the server to
700 status = dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[4],
703 if (NT_STATUS_IS_ERR(status)) {