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/>.
26 #include "librpc/gen_ndr/ndr_epmapper.h"
27 #include "librpc/gen_ndr/ndr_misc.h"
28 #include "librpc/rpc/dcerpc.h"
31 #define MAX_PROTSEQ 10
35 enum dcerpc_transport_t transport;
37 enum epm_protocol protseq[MAX_PROTSEQ];
39 { "ncacn_np", NCACN_NP, 3,
40 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SMB, EPM_PROTOCOL_NETBIOS }},
41 { "ncacn_ip_tcp", NCACN_IP_TCP, 3,
42 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_TCP, EPM_PROTOCOL_IP } },
43 { "ncacn_http", NCACN_HTTP, 3,
44 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_HTTP, EPM_PROTOCOL_IP } },
45 { "ncadg_ip_udp", NCACN_IP_UDP, 3,
46 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UDP, EPM_PROTOCOL_IP } },
47 { "ncalrpc", NCALRPC, 2,
48 { EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_PIPE } },
49 { "ncacn_unix_stream", NCACN_UNIX_STREAM, 2,
50 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_UNIX_DS } },
51 { "ncadg_unix_dgram", NCADG_UNIX_DGRAM, 2,
52 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UNIX_DS } },
53 { "ncacn_at_dsp", NCACN_AT_DSP, 3,
54 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DSP } },
55 { "ncadg_at_ddp", NCADG_AT_DDP, 3,
56 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DDP } },
57 { "ncacn_vns_ssp", NCACN_VNS_SPP, 3,
58 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_SPP } },
59 { "ncacn_vns_ipc", NCACN_VNS_IPC, 3,
60 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_IPC }, },
61 { "ncadg_ipx", NCADG_IPX, 2,
62 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_IPX },
64 { "ncacn_spx", NCACN_SPX, 3,
65 /* I guess some MS programmer confused the identifier for
66 * EPM_PROTOCOL_UUID (0x0D or 13) with the one for
67 * EPM_PROTOCOL_SPX (0x13) here. -- jelmer*/
68 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_UUID },
76 {"sign", DCERPC_SIGN},
77 {"seal", DCERPC_SEAL},
78 {"connect", DCERPC_CONNECT},
79 {"spnego", DCERPC_AUTH_SPNEGO},
80 {"ntlm", DCERPC_AUTH_NTLM},
81 {"krb5", DCERPC_AUTH_KRB5},
82 {"validate", DCERPC_DEBUG_VALIDATE_BOTH},
83 {"print", DCERPC_DEBUG_PRINT_BOTH},
84 {"padcheck", DCERPC_DEBUG_PAD_CHECK},
85 {"bigendian", DCERPC_PUSH_BIGENDIAN},
86 {"smb2", DCERPC_SMB2},
87 {"hdrsign", DCERPC_HEADER_SIGNING}
90 const char *epm_floor_string(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
92 struct ndr_syntax_id syntax;
95 switch(epm_floor->lhs.protocol) {
96 case EPM_PROTOCOL_UUID:
97 status = dcerpc_floor_get_lhs_data(epm_floor, &syntax);
98 if (NT_STATUS_IS_OK(status)) {
99 /* lhs is used: UUID */
102 if (GUID_equal(&syntax.uuid, &ndr_transfer_syntax.uuid)) {
106 if (GUID_equal(&syntax.uuid, &ndr64_transfer_syntax.uuid)) {
110 uuidstr = GUID_string(mem_ctx, &syntax.uuid);
112 return talloc_asprintf(mem_ctx, " uuid %s/0x%02x", uuidstr, syntax.if_version);
114 return talloc_asprintf(mem_ctx, "IPX:%s",
115 data_blob_hex_string(mem_ctx, &epm_floor->rhs.uuid.unknown));
118 case EPM_PROTOCOL_NCACN:
121 case EPM_PROTOCOL_NCADG:
124 case EPM_PROTOCOL_NCALRPC:
127 case EPM_PROTOCOL_DNET_NSP:
130 case EPM_PROTOCOL_IP:
131 return talloc_asprintf(mem_ctx, "IP:%s", epm_floor->rhs.ip.ipaddr);
133 case EPM_PROTOCOL_PIPE:
134 return talloc_asprintf(mem_ctx, "PIPE:%s", epm_floor->rhs.pipe.path);
136 case EPM_PROTOCOL_SMB:
137 return talloc_asprintf(mem_ctx, "SMB:%s", epm_floor->rhs.smb.unc);
139 case EPM_PROTOCOL_UNIX_DS:
140 return talloc_asprintf(mem_ctx, "Unix:%s", epm_floor->rhs.unix_ds.path);
142 case EPM_PROTOCOL_NETBIOS:
143 return talloc_asprintf(mem_ctx, "NetBIOS:%s", epm_floor->rhs.netbios.name);
145 case EPM_PROTOCOL_NETBEUI:
148 case EPM_PROTOCOL_SPX:
151 case EPM_PROTOCOL_NB_IPX:
154 case EPM_PROTOCOL_HTTP:
155 return talloc_asprintf(mem_ctx, "HTTP:%d", epm_floor->rhs.http.port);
157 case EPM_PROTOCOL_TCP:
158 return talloc_asprintf(mem_ctx, "TCP:%d", epm_floor->rhs.tcp.port);
160 case EPM_PROTOCOL_UDP:
161 return talloc_asprintf(mem_ctx, "UDP:%d", epm_floor->rhs.udp.port);
164 return talloc_asprintf(mem_ctx, "UNK(%02x):", epm_floor->lhs.protocol);
170 form a binding string from a binding structure
172 _PUBLIC_ char *dcerpc_binding_string(TALLOC_CTX *mem_ctx, const struct dcerpc_binding *b)
174 char *s = talloc_strdup(mem_ctx, "");
176 const char *t_name = NULL;
178 if (b->transport != NCA_UNKNOWN) {
179 for (i=0;i<ARRAY_SIZE(transports);i++) {
180 if (transports[i].transport == b->transport) {
181 t_name = transports[i].name;
189 if (!GUID_all_zero(&b->object.uuid)) {
190 s = talloc_asprintf(s, "%s@",
191 GUID_string(mem_ctx, &b->object.uuid));
194 if (t_name != NULL) {
195 s = talloc_asprintf_append_buffer(s, "%s:", t_name);
202 s = talloc_asprintf_append_buffer(s, "%s", b->host);
205 if (!b->endpoint && !b->options && !b->flags) {
209 s = talloc_asprintf_append_buffer(s, "[");
212 s = talloc_asprintf_append_buffer(s, "%s", b->endpoint);
215 /* this is a *really* inefficent way of dealing with strings,
216 but this is rarely called and the strings are always short,
218 for (i=0;b->options && b->options[i];i++) {
219 s = talloc_asprintf_append_buffer(s, ",%s", b->options[i]);
223 for (i=0;i<ARRAY_SIZE(ncacn_options);i++) {
224 if (b->flags & ncacn_options[i].flag) {
225 s = talloc_asprintf_append_buffer(s, ",%s", ncacn_options[i].name);
230 s = talloc_asprintf_append_buffer(s, "]");
236 parse a binding string into a dcerpc_binding structure
238 _PUBLIC_ NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *s, struct dcerpc_binding **b_out)
240 struct dcerpc_binding *b;
243 int i, j, comma_count;
245 b = talloc(mem_ctx, struct dcerpc_binding);
247 return NT_STATUS_NO_MEMORY;
252 if (p && PTR_DIFF(p, s) == 36) { /* 36 is the length of a UUID */
255 status = GUID_from_string(s, &b->object.uuid);
257 if (NT_STATUS_IS_ERR(status)) {
258 DEBUG(0, ("Failed parsing UUID\n"));
264 ZERO_STRUCT(b->object);
267 b->object.if_version = 0;
272 b->transport = NCA_UNKNOWN;
274 char *type = talloc_strndup(mem_ctx, s, PTR_DIFF(p, s));
276 return NT_STATUS_NO_MEMORY;
279 for (i=0;i<ARRAY_SIZE(transports);i++) {
280 if (strcasecmp(type, transports[i].name) == 0) {
281 b->transport = transports[i].transport;
286 if (i==ARRAY_SIZE(transports)) {
287 DEBUG(0,("Unknown dcerpc transport '%s'\n", type));
288 return NT_STATUS_INVALID_PARAMETER;
298 b->host = talloc_strndup(b, s, PTR_DIFF(p, s));
299 options = talloc_strdup(mem_ctx, p+1);
300 if (options[strlen(options)-1] != ']') {
301 return NT_STATUS_INVALID_PARAMETER;
303 options[strlen(options)-1] = 0;
305 b->host = talloc_strdup(b, s);
309 return NT_STATUS_NO_MEMORY;
312 b->target_hostname = b->host;
316 b->assoc_group_id = 0;
324 comma_count = count_chars(options, ',');
326 b->options = talloc_array(b, const char *, comma_count+2);
328 return NT_STATUS_NO_MEMORY;
331 for (i=0; (p = strchr(options, ',')); i++) {
332 b->options[i] = talloc_strndup(b, options, PTR_DIFF(p, options));
333 if (!b->options[i]) {
334 return NT_STATUS_NO_MEMORY;
338 b->options[i] = options;
339 b->options[i+1] = NULL;
341 /* some options are pre-parsed for convenience */
342 for (i=0;b->options[i];i++) {
343 for (j=0;j<ARRAY_SIZE(ncacn_options);j++) {
344 if (strcasecmp(ncacn_options[j].name, b->options[i]) == 0) {
346 b->flags |= ncacn_options[j].flag;
347 for (k=i;b->options[k];k++) {
348 b->options[k] = b->options[k+1];
357 /* Endpoint is first option */
358 b->endpoint = b->options[0];
359 if (strlen(b->endpoint) == 0) b->endpoint = NULL;
361 for (i=0;b->options[i];i++) {
362 b->options[i] = b->options[i+1];
366 if (b->options[0] == NULL)
373 _PUBLIC_ NTSTATUS dcerpc_floor_get_lhs_data(struct epm_floor *epm_floor, struct ndr_syntax_id *syntax)
375 TALLOC_CTX *mem_ctx = talloc_init("floor_get_lhs_data");
376 struct ndr_pull *ndr = ndr_pull_init_blob(&epm_floor->lhs.lhs_data, mem_ctx, NULL);
377 enum ndr_err_code ndr_err;
378 uint16_t if_version=0;
380 ndr->flags |= LIBNDR_FLAG_NOALIGN;
382 ndr_err = ndr_pull_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
383 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
384 talloc_free(mem_ctx);
385 return ndr_map_error2ntstatus(ndr_err);
388 ndr_err = ndr_pull_uint16(ndr, NDR_SCALARS, &if_version);
389 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
390 talloc_free(mem_ctx);
391 return ndr_map_error2ntstatus(ndr_err);
394 syntax->if_version = if_version;
396 talloc_free(mem_ctx);
401 static DATA_BLOB dcerpc_floor_pack_lhs_data(TALLOC_CTX *mem_ctx, const struct ndr_syntax_id *syntax)
403 struct ndr_push *ndr = ndr_push_init_ctx(mem_ctx, NULL);
405 ndr->flags |= LIBNDR_FLAG_NOALIGN;
407 ndr_push_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, &syntax->uuid);
408 ndr_push_uint16(ndr, NDR_SCALARS, syntax->if_version);
410 return ndr_push_blob(ndr);
413 const char *dcerpc_floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
415 switch (epm_floor->lhs.protocol) {
416 case EPM_PROTOCOL_TCP:
417 if (epm_floor->rhs.tcp.port == 0) return NULL;
418 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.tcp.port);
420 case EPM_PROTOCOL_UDP:
421 if (epm_floor->rhs.udp.port == 0) return NULL;
422 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.udp.port);
424 case EPM_PROTOCOL_HTTP:
425 if (epm_floor->rhs.http.port == 0) return NULL;
426 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.http.port);
428 case EPM_PROTOCOL_IP:
429 return talloc_strdup(mem_ctx, epm_floor->rhs.ip.ipaddr);
431 case EPM_PROTOCOL_NCACN:
434 case EPM_PROTOCOL_NCADG:
437 case EPM_PROTOCOL_SMB:
438 if (strlen(epm_floor->rhs.smb.unc) == 0) return NULL;
439 return talloc_strdup(mem_ctx, epm_floor->rhs.smb.unc);
441 case EPM_PROTOCOL_PIPE:
442 if (strlen(epm_floor->rhs.pipe.path) == 0) return NULL;
443 return talloc_strdup(mem_ctx, epm_floor->rhs.pipe.path);
445 case EPM_PROTOCOL_NETBIOS:
446 if (strlen(epm_floor->rhs.netbios.name) == 0) return NULL;
447 return talloc_strdup(mem_ctx, epm_floor->rhs.netbios.name);
449 case EPM_PROTOCOL_NCALRPC:
452 case EPM_PROTOCOL_VINES_SPP:
453 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_spp.port);
455 case EPM_PROTOCOL_VINES_IPC:
456 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_ipc.port);
458 case EPM_PROTOCOL_STREETTALK:
459 return talloc_strdup(mem_ctx, epm_floor->rhs.streettalk.streettalk);
461 case EPM_PROTOCOL_UNIX_DS:
462 if (strlen(epm_floor->rhs.unix_ds.path) == 0) return NULL;
463 return talloc_strdup(mem_ctx, epm_floor->rhs.unix_ds.path);
465 case EPM_PROTOCOL_NULL:
469 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
476 static NTSTATUS dcerpc_floor_set_rhs_data(TALLOC_CTX *mem_ctx,
477 struct epm_floor *epm_floor,
480 switch (epm_floor->lhs.protocol) {
481 case EPM_PROTOCOL_TCP:
482 epm_floor->rhs.tcp.port = atoi(data);
485 case EPM_PROTOCOL_UDP:
486 epm_floor->rhs.udp.port = atoi(data);
489 case EPM_PROTOCOL_HTTP:
490 epm_floor->rhs.http.port = atoi(data);
493 case EPM_PROTOCOL_IP:
494 epm_floor->rhs.ip.ipaddr = talloc_strdup(mem_ctx, data);
495 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.ip.ipaddr);
498 case EPM_PROTOCOL_NCACN:
499 epm_floor->rhs.ncacn.minor_version = 0;
502 case EPM_PROTOCOL_NCADG:
503 epm_floor->rhs.ncadg.minor_version = 0;
506 case EPM_PROTOCOL_SMB:
507 epm_floor->rhs.smb.unc = talloc_strdup(mem_ctx, data);
508 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.smb.unc);
511 case EPM_PROTOCOL_PIPE:
512 epm_floor->rhs.pipe.path = talloc_strdup(mem_ctx, data);
513 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.pipe.path);
516 case EPM_PROTOCOL_NETBIOS:
517 epm_floor->rhs.netbios.name = talloc_strdup(mem_ctx, data);
518 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.netbios.name);
521 case EPM_PROTOCOL_NCALRPC:
524 case EPM_PROTOCOL_VINES_SPP:
525 epm_floor->rhs.vines_spp.port = atoi(data);
528 case EPM_PROTOCOL_VINES_IPC:
529 epm_floor->rhs.vines_ipc.port = atoi(data);
532 case EPM_PROTOCOL_STREETTALK:
533 epm_floor->rhs.streettalk.streettalk = talloc_strdup(mem_ctx, data);
534 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.streettalk.streettalk);
537 case EPM_PROTOCOL_UNIX_DS:
538 epm_floor->rhs.unix_ds.path = talloc_strdup(mem_ctx, data);
539 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.unix_ds.path);
542 case EPM_PROTOCOL_NULL:
546 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
550 return NT_STATUS_NOT_SUPPORTED;
553 enum dcerpc_transport_t dcerpc_transport_by_endpoint_protocol(int prot)
557 /* Find a transport that has 'prot' as 4th protocol */
558 for (i=0;i<ARRAY_SIZE(transports);i++) {
559 if (transports[i].num_protocols >= 2 &&
560 transports[i].protseq[1] == prot) {
561 return transports[i].transport;
565 /* Unknown transport */
566 return (unsigned int)-1;
569 _PUBLIC_ enum dcerpc_transport_t dcerpc_transport_by_tower(struct epm_tower *tower)
573 /* Find a transport that matches this tower */
574 for (i=0;i<ARRAY_SIZE(transports);i++) {
576 if (transports[i].num_protocols != tower->num_floors - 2) {
580 for (j = 0; j < transports[i].num_protocols; j++) {
581 if (transports[i].protseq[j] != tower->floors[j+2].lhs.protocol) {
586 if (j == transports[i].num_protocols) {
587 return transports[i].transport;
591 /* Unknown transport */
592 return (unsigned int)-1;
595 _PUBLIC_ NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx,
596 struct epm_tower *tower,
597 struct dcerpc_binding **b_out)
600 struct dcerpc_binding *binding;
602 binding = talloc(mem_ctx, struct dcerpc_binding);
603 NT_STATUS_HAVE_NO_MEMORY(binding);
605 ZERO_STRUCT(binding->object);
606 binding->options = NULL;
607 binding->host = NULL;
608 binding->target_hostname = NULL;
610 binding->assoc_group_id = 0;
612 binding->transport = dcerpc_transport_by_tower(tower);
614 if (binding->transport == (unsigned int)-1) {
615 return NT_STATUS_NOT_SUPPORTED;
618 if (tower->num_floors < 1) {
622 /* Set object uuid */
623 status = dcerpc_floor_get_lhs_data(&tower->floors[0], &binding->object);
625 if (!NT_STATUS_IS_OK(status)) {
626 DEBUG(1, ("Error pulling object uuid and version: %s", nt_errstr(status)));
630 /* Ignore floor 1, it contains the NDR version info */
632 binding->options = NULL;
635 if (tower->num_floors >= 4) {
636 binding->endpoint = dcerpc_floor_get_rhs_data(mem_ctx, &tower->floors[3]);
638 binding->endpoint = NULL;
641 /* Set network address */
642 if (tower->num_floors >= 5) {
643 binding->host = dcerpc_floor_get_rhs_data(mem_ctx, &tower->floors[4]);
644 NT_STATUS_HAVE_NO_MEMORY(binding->host);
645 binding->target_hostname = binding->host;
651 _PUBLIC_ NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx, struct dcerpc_binding *binding, struct epm_tower *tower)
653 const enum epm_protocol *protseq = NULL;
654 int num_protocols = -1, i;
658 for (i=0;i<ARRAY_SIZE(transports);i++) {
659 if (transports[i].transport == binding->transport) {
660 protseq = transports[i].protseq;
661 num_protocols = transports[i].num_protocols;
666 if (num_protocols == -1) {
667 DEBUG(0, ("Unable to find transport with id '%d'\n", binding->transport));
668 return NT_STATUS_UNSUCCESSFUL;
671 tower->num_floors = 2 + num_protocols;
672 tower->floors = talloc_array(mem_ctx, struct epm_floor, tower->num_floors);
675 tower->floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
677 tower->floors[0].lhs.lhs_data = dcerpc_floor_pack_lhs_data(mem_ctx, &binding->object);
679 tower->floors[0].rhs.uuid.unknown = data_blob_talloc_zero(mem_ctx, 2);
682 tower->floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
684 tower->floors[1].lhs.lhs_data = dcerpc_floor_pack_lhs_data(mem_ctx,
685 &ndr_transfer_syntax);
687 tower->floors[1].rhs.uuid.unknown = data_blob_talloc_zero(mem_ctx, 2);
689 /* Floor 2 to num_protocols */
690 for (i = 0; i < num_protocols; i++) {
691 tower->floors[2 + i].lhs.protocol = protseq[i];
692 tower->floors[2 + i].lhs.lhs_data = data_blob_talloc(mem_ctx, NULL, 0);
693 ZERO_STRUCT(tower->floors[2 + i].rhs);
694 dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[2 + i], "");
697 /* The 4th floor contains the endpoint */
698 if (num_protocols >= 2 && binding->endpoint) {
699 status = dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[3], binding->endpoint);
700 if (NT_STATUS_IS_ERR(status)) {
705 /* The 5th contains the network address */
706 if (num_protocols >= 3 && binding->host) {
707 if (is_ipaddress(binding->host)) {
708 status = dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[4],
711 /* note that we don't attempt to resolve the
712 name here - when we get a hostname here we
713 are in the client code, and want to put in
714 a wildcard all-zeros IP for the server to
716 status = dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[4],
719 if (NT_STATUS_IS_ERR(status)) {