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
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "librpc/gen_ndr/ndr_epmapper.h"
27 #include "librpc/gen_ndr/ndr_dcerpc.h"
28 #include "librpc/gen_ndr/ndr_misc.h"
31 find a dcerpc call on an interface by name
33 const struct dcerpc_interface_call *dcerpc_iface_find_call(const struct dcerpc_interface_table *iface,
37 for (i=0;i<iface->num_calls;i++) {
38 if (strcmp(iface->calls[i].name, name) == 0) {
39 return &iface->calls[i];
46 push a ncacn_packet into a blob, potentially with auth info
48 NTSTATUS ncacn_push_auth(DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
49 struct ncacn_packet *pkt,
50 struct dcerpc_auth *auth_info)
55 ndr = ndr_push_init_ctx(mem_ctx);
57 return NT_STATUS_NO_MEMORY;
60 if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
61 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
64 if (pkt->pfc_flags & DCERPC_PFC_FLAG_ORPC) {
65 ndr->flags |= LIBNDR_FLAG_OBJECT_PRESENT;
69 pkt->auth_length = auth_info->credentials.length;
74 status = ndr_push_ncacn_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
75 if (!NT_STATUS_IS_OK(status)) {
80 status = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, auth_info);
83 *blob = ndr_push_blob(ndr);
85 /* fill in the frag length */
86 dcerpc_set_frag_length(blob, blob->length);
91 #define MAX_PROTSEQ 10
95 enum dcerpc_transport_t transport;
97 enum epm_protocol protseq[MAX_PROTSEQ];
99 { "ncacn_np", NCACN_NP, 3,
100 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SMB, EPM_PROTOCOL_NETBIOS }},
101 { "ncacn_ip_tcp", NCACN_IP_TCP, 3,
102 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_TCP, EPM_PROTOCOL_IP } },
103 { "ncacn_http", NCACN_HTTP, 3,
104 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_HTTP, EPM_PROTOCOL_IP } },
105 { "ncadg_ip_udp", NCACN_IP_UDP, 3,
106 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UDP, EPM_PROTOCOL_IP } },
107 { "ncalrpc", NCALRPC, 2,
108 { EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_PIPE } },
109 { "ncacn_unix_stream", NCACN_UNIX_STREAM, 2,
110 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_UNIX_DS } },
111 { "ncadg_unix_dgram", NCADG_UNIX_DGRAM, 2,
112 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UNIX_DS } },
113 { "ncacn_at_dsp", NCACN_AT_DSP, 3,
114 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DSP } },
115 { "ncadg_at_ddp", NCADG_AT_DDP, 3,
116 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DDP } },
117 { "ncacn_vns_ssp", NCACN_VNS_SPP, 3,
118 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_SPP } },
119 { "ncacn_vns_ipc", NCACN_VNS_IPC, 3,
120 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_IPC }, },
121 { "ncadg_ipx", NCADG_IPX, 2,
122 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_IPX },
124 { "ncacn_spx", NCACN_SPX, 3,
125 /* I guess some MS programmer confused the identifier for
126 * EPM_PROTOCOL_UUID (0x0D or 13) with the one for
127 * EPM_PROTOCOL_SPX (0x13) here. -- jelmer*/
128 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_UUID },
132 static const struct {
135 } ncacn_options[] = {
136 {"sign", DCERPC_SIGN},
137 {"seal", DCERPC_SEAL},
138 {"connect", DCERPC_CONNECT},
139 {"spnego", DCERPC_AUTH_SPNEGO},
140 {"krb5", DCERPC_AUTH_KRB5},
141 {"validate", DCERPC_DEBUG_VALIDATE_BOTH},
142 {"print", DCERPC_DEBUG_PRINT_BOTH},
143 {"padcheck", DCERPC_DEBUG_PAD_CHECK},
144 {"bigendian", DCERPC_PUSH_BIGENDIAN},
145 {"smb2", DCERPC_SMB2}
148 const char *epm_floor_string(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
154 switch(epm_floor->lhs.protocol) {
155 case EPM_PROTOCOL_UUID:
156 status = dcerpc_floor_get_lhs_data(epm_floor, &uuid, &if_version);
157 if (NT_STATUS_IS_OK(status)) {
158 /* lhs is used: UUID */
161 if (GUID_equal(&uuid, &ndr_transfer_syntax.uuid)) {
165 if (GUID_equal(&uuid, &ndr64_transfer_syntax.uuid)) {
169 uuidstr = GUID_string(mem_ctx, &uuid);
171 return talloc_asprintf(mem_ctx, " uuid %s/0x%02x", uuidstr, if_version);
173 return talloc_asprintf(mem_ctx, "IPX:%s",
174 data_blob_hex_string(mem_ctx, &epm_floor->rhs.uuid.unknown));
177 case EPM_PROTOCOL_NCACN:
180 case EPM_PROTOCOL_NCADG:
183 case EPM_PROTOCOL_NCALRPC:
186 case EPM_PROTOCOL_DNET_NSP:
189 case EPM_PROTOCOL_IP:
190 return talloc_asprintf(mem_ctx, "IP:%s", epm_floor->rhs.ip.ipaddr);
192 case EPM_PROTOCOL_PIPE:
193 return talloc_asprintf(mem_ctx, "PIPE:%s", epm_floor->rhs.pipe.path);
195 case EPM_PROTOCOL_SMB:
196 return talloc_asprintf(mem_ctx, "SMB:%s", epm_floor->rhs.smb.unc);
198 case EPM_PROTOCOL_UNIX_DS:
199 return talloc_asprintf(mem_ctx, "Unix:%s", epm_floor->rhs.unix_ds.path);
201 case EPM_PROTOCOL_NETBIOS:
202 return talloc_asprintf(mem_ctx, "NetBIOS:%s", epm_floor->rhs.netbios.name);
204 case EPM_PROTOCOL_NETBEUI:
207 case EPM_PROTOCOL_SPX:
210 case EPM_PROTOCOL_NB_IPX:
213 case EPM_PROTOCOL_HTTP:
214 return talloc_asprintf(mem_ctx, "HTTP:%d", epm_floor->rhs.http.port);
216 case EPM_PROTOCOL_TCP:
217 return talloc_asprintf(mem_ctx, "TCP:%d", epm_floor->rhs.tcp.port);
219 case EPM_PROTOCOL_UDP:
220 return talloc_asprintf(mem_ctx, "UDP:%d", epm_floor->rhs.udp.port);
223 return talloc_asprintf(mem_ctx, "UNK(%02x):", epm_floor->lhs.protocol);
229 form a binding string from a binding structure
231 const char *dcerpc_binding_string(TALLOC_CTX *mem_ctx, const struct dcerpc_binding *b)
233 char *s = talloc_strdup(mem_ctx, "");
235 const char *t_name=NULL;
237 for (i=0;i<ARRAY_SIZE(transports);i++) {
238 if (transports[i].transport == b->transport) {
239 t_name = transports[i].name;
246 if (!GUID_all_zero(&b->object)) {
247 s = talloc_asprintf(s, "%s@",
248 GUID_string(mem_ctx, &b->object));
251 s = talloc_asprintf_append(s, "%s:", t_name);
255 s = talloc_asprintf_append(s, "%s", b->host);
258 if (!b->endpoint && !b->options && !b->flags) {
262 s = talloc_asprintf_append(s, "[");
265 s = talloc_asprintf_append(s, "%s", b->endpoint);
268 /* this is a *really* inefficent way of dealing with strings,
269 but this is rarely called and the strings are always short,
271 for (i=0;b->options && b->options[i];i++) {
272 s = talloc_asprintf_append(s, ",%s", b->options[i]);
276 for (i=0;i<ARRAY_SIZE(ncacn_options);i++) {
277 if (b->flags & ncacn_options[i].flag) {
278 s = talloc_asprintf_append(s, ",%s", ncacn_options[i].name);
283 s = talloc_asprintf_append(s, "]");
289 parse a binding string into a dcerpc_binding structure
291 NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *s, struct dcerpc_binding **b_out)
293 struct dcerpc_binding *b;
294 char *options, *type;
296 int i, j, comma_count;
298 b = talloc(mem_ctx, struct dcerpc_binding);
300 return NT_STATUS_NO_MEMORY;
305 if (p && PTR_DIFF(p, s) == 36) { /* 36 is the length of a UUID */
308 status = GUID_from_string(s, &b->object);
310 if (NT_STATUS_IS_ERR(status)) {
311 DEBUG(0, ("Failed parsing UUID\n"));
317 ZERO_STRUCT(b->object);
320 b->object_version = 0;
324 return NT_STATUS_INVALID_PARAMETER;
327 type = talloc_strndup(mem_ctx, s, PTR_DIFF(p, s));
329 return NT_STATUS_NO_MEMORY;
332 for (i=0;i<ARRAY_SIZE(transports);i++) {
333 if (strcasecmp(type, transports[i].name) == 0) {
334 b->transport = transports[i].transport;
338 if (i==ARRAY_SIZE(transports)) {
339 DEBUG(0,("Unknown dcerpc transport '%s'\n", type));
340 return NT_STATUS_INVALID_PARAMETER;
347 b->host = talloc_strndup(b, s, PTR_DIFF(p, s));
348 options = talloc_strdup(mem_ctx, p+1);
349 if (options[strlen(options)-1] != ']') {
350 return NT_STATUS_INVALID_PARAMETER;
352 options[strlen(options)-1] = 0;
354 b->host = talloc_strdup(b, s);
359 return NT_STATUS_NO_MEMORY;
371 comma_count = count_chars(options, ',');
373 b->options = talloc_array(b, const char *, comma_count+2);
375 return NT_STATUS_NO_MEMORY;
378 for (i=0; (p = strchr(options, ',')); i++) {
379 b->options[i] = talloc_strndup(b, options, PTR_DIFF(p, options));
380 if (!b->options[i]) {
381 return NT_STATUS_NO_MEMORY;
385 b->options[i] = options;
386 b->options[i+1] = NULL;
388 /* some options are pre-parsed for convenience */
389 for (i=0;b->options[i];i++) {
390 for (j=0;j<ARRAY_SIZE(ncacn_options);j++) {
391 if (strcasecmp(ncacn_options[j].name, b->options[i]) == 0) {
393 b->flags |= ncacn_options[j].flag;
394 for (k=i;b->options[k];k++) {
395 b->options[k] = b->options[k+1];
404 /* Endpoint is first option */
405 b->endpoint = b->options[0];
406 if (strlen(b->endpoint) == 0) b->endpoint = NULL;
408 for (i=0;b->options[i];i++) {
409 b->options[i] = b->options[i+1];
413 if (b->options[0] == NULL)
420 NTSTATUS dcerpc_floor_get_lhs_data(struct epm_floor *epm_floor, struct GUID *uuid, uint16_t *if_version)
422 TALLOC_CTX *mem_ctx = talloc_init("floor_get_lhs_data");
423 struct ndr_pull *ndr = ndr_pull_init_blob(&epm_floor->lhs.lhs_data, mem_ctx);
426 ndr->flags |= LIBNDR_FLAG_NOALIGN;
428 status = ndr_pull_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, uuid);
429 if (NT_STATUS_IS_ERR(status)) {
430 talloc_free(mem_ctx);
434 status = ndr_pull_uint16(ndr, NDR_SCALARS, if_version);
436 talloc_free(mem_ctx);
441 static DATA_BLOB dcerpc_floor_pack_lhs_data(TALLOC_CTX *mem_ctx, const struct GUID *uuid, uint32_t if_version)
443 struct ndr_push *ndr = ndr_push_init_ctx(mem_ctx);
445 ndr->flags |= LIBNDR_FLAG_NOALIGN;
447 ndr_push_GUID(ndr, NDR_SCALARS | NDR_BUFFERS, uuid);
448 ndr_push_uint16(ndr, NDR_SCALARS, if_version);
450 return ndr_push_blob(ndr);
453 const char *dcerpc_floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
455 switch (epm_floor->lhs.protocol) {
456 case EPM_PROTOCOL_TCP:
457 if (epm_floor->rhs.tcp.port == 0) return NULL;
458 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.tcp.port);
460 case EPM_PROTOCOL_UDP:
461 if (epm_floor->rhs.udp.port == 0) return NULL;
462 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.udp.port);
464 case EPM_PROTOCOL_HTTP:
465 if (epm_floor->rhs.http.port == 0) return NULL;
466 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.http.port);
468 case EPM_PROTOCOL_IP:
469 return talloc_strdup(mem_ctx, epm_floor->rhs.ip.ipaddr);
471 case EPM_PROTOCOL_NCACN:
474 case EPM_PROTOCOL_NCADG:
477 case EPM_PROTOCOL_SMB:
478 if (strlen(epm_floor->rhs.smb.unc) == 0) return NULL;
479 return talloc_strdup(mem_ctx, epm_floor->rhs.smb.unc);
481 case EPM_PROTOCOL_PIPE:
482 if (strlen(epm_floor->rhs.pipe.path) == 0) return NULL;
483 return talloc_strdup(mem_ctx, epm_floor->rhs.pipe.path);
485 case EPM_PROTOCOL_NETBIOS:
486 if (strlen(epm_floor->rhs.netbios.name) == 0) return NULL;
487 return talloc_strdup(mem_ctx, epm_floor->rhs.netbios.name);
489 case EPM_PROTOCOL_NCALRPC:
492 case EPM_PROTOCOL_VINES_SPP:
493 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_spp.port);
495 case EPM_PROTOCOL_VINES_IPC:
496 return talloc_asprintf(mem_ctx, "%d", epm_floor->rhs.vines_ipc.port);
498 case EPM_PROTOCOL_STREETTALK:
499 return talloc_strdup(mem_ctx, epm_floor->rhs.streettalk.streettalk);
501 case EPM_PROTOCOL_UNIX_DS:
502 if (strlen(epm_floor->rhs.unix_ds.path) == 0) return NULL;
503 return talloc_strdup(mem_ctx, epm_floor->rhs.unix_ds.path);
505 case EPM_PROTOCOL_NULL:
509 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
516 static NTSTATUS dcerpc_floor_set_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor, const char *data)
518 switch (epm_floor->lhs.protocol) {
519 case EPM_PROTOCOL_TCP:
520 epm_floor->rhs.tcp.port = atoi(data);
523 case EPM_PROTOCOL_UDP:
524 epm_floor->rhs.udp.port = atoi(data);
527 case EPM_PROTOCOL_HTTP:
528 epm_floor->rhs.http.port = atoi(data);
531 case EPM_PROTOCOL_IP:
532 epm_floor->rhs.ip.ipaddr = talloc_strdup(mem_ctx, data);
533 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.ip.ipaddr);
536 case EPM_PROTOCOL_NCACN:
537 epm_floor->rhs.ncacn.minor_version = 0;
540 case EPM_PROTOCOL_NCADG:
541 epm_floor->rhs.ncadg.minor_version = 0;
544 case EPM_PROTOCOL_SMB:
545 epm_floor->rhs.smb.unc = talloc_strdup(mem_ctx, data);
546 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.smb.unc);
549 case EPM_PROTOCOL_PIPE:
550 epm_floor->rhs.pipe.path = talloc_strdup(mem_ctx, data);
551 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.pipe.path);
554 case EPM_PROTOCOL_NETBIOS:
555 epm_floor->rhs.netbios.name = talloc_strdup(mem_ctx, data);
556 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.netbios.name);
559 case EPM_PROTOCOL_NCALRPC:
562 case EPM_PROTOCOL_VINES_SPP:
563 epm_floor->rhs.vines_spp.port = atoi(data);
566 case EPM_PROTOCOL_VINES_IPC:
567 epm_floor->rhs.vines_ipc.port = atoi(data);
570 case EPM_PROTOCOL_STREETTALK:
571 epm_floor->rhs.streettalk.streettalk = talloc_strdup(mem_ctx, data);
572 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.streettalk.streettalk);
575 case EPM_PROTOCOL_UNIX_DS:
576 epm_floor->rhs.unix_ds.path = talloc_strdup(mem_ctx, data);
577 NT_STATUS_HAVE_NO_MEMORY(epm_floor->rhs.unix_ds.path);
580 case EPM_PROTOCOL_NULL:
584 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor->lhs.protocol));
588 return NT_STATUS_NOT_SUPPORTED;
591 enum dcerpc_transport_t dcerpc_transport_by_endpoint_protocol(int prot)
595 /* Find a transport that has 'prot' as 4th protocol */
596 for (i=0;i<ARRAY_SIZE(transports);i++) {
597 if (transports[i].num_protocols >= 2 &&
598 transports[i].protseq[1] == prot) {
599 return transports[i].transport;
603 /* Unknown transport */
607 enum dcerpc_transport_t dcerpc_transport_by_tower(struct epm_tower *tower)
611 /* Find a transport that matches this tower */
612 for (i=0;i<ARRAY_SIZE(transports);i++) {
614 if (transports[i].num_protocols != tower->num_floors - 2) {
618 for (j = 0; j < transports[i].num_protocols; j++) {
619 if (transports[i].protseq[j] != tower->floors[j+2].lhs.protocol) {
624 if (j == transports[i].num_protocols) {
625 return transports[i].transport;
629 /* Unknown transport */
633 NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx, struct epm_tower *tower, struct dcerpc_binding **b_out)
636 struct dcerpc_binding *binding;
638 binding = talloc(mem_ctx, struct dcerpc_binding);
639 NT_STATUS_HAVE_NO_MEMORY(binding);
641 ZERO_STRUCT(binding->object);
642 binding->options = NULL;
643 binding->host = NULL;
646 binding->transport = dcerpc_transport_by_tower(tower);
648 if (binding->transport == -1) {
649 return NT_STATUS_NOT_SUPPORTED;
652 if (tower->num_floors < 1) {
656 /* Set object uuid */
657 status = dcerpc_floor_get_lhs_data(&tower->floors[0], &binding->object, &binding->object_version);
659 if (!NT_STATUS_IS_OK(status)) {
660 DEBUG(1, ("Error pulling object uuid and version: %s", nt_errstr(status)));
664 /* Ignore floor 1, it contains the NDR version info */
666 binding->options = NULL;
669 if (tower->num_floors >= 4) {
670 binding->endpoint = dcerpc_floor_get_rhs_data(mem_ctx, &tower->floors[3]);
672 binding->endpoint = NULL;
675 /* Set network address */
676 if (tower->num_floors >= 5) {
677 binding->host = dcerpc_floor_get_rhs_data(mem_ctx, &tower->floors[4]);
683 NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx, struct dcerpc_binding *binding, struct epm_tower *tower)
685 const enum epm_protocol *protseq = NULL;
686 int num_protocols = -1, i;
690 for (i=0;i<ARRAY_SIZE(transports);i++) {
691 if (transports[i].transport == binding->transport) {
692 protseq = transports[i].protseq;
693 num_protocols = transports[i].num_protocols;
698 if (num_protocols == -1) {
699 DEBUG(0, ("Unable to find transport with id '%d'\n", binding->transport));
700 return NT_STATUS_UNSUCCESSFUL;
703 tower->num_floors = 2 + num_protocols;
704 tower->floors = talloc_array(mem_ctx, struct epm_floor, tower->num_floors);
707 tower->floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
709 tower->floors[0].lhs.lhs_data = dcerpc_floor_pack_lhs_data(mem_ctx, &binding->object, binding->object_version);
711 tower->floors[0].rhs.uuid.unknown = data_blob_talloc_zero(mem_ctx, 2);
714 tower->floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
716 tower->floors[1].lhs.lhs_data = dcerpc_floor_pack_lhs_data(mem_ctx,
717 &ndr_transfer_syntax.uuid,
718 ndr_transfer_syntax.if_version);
720 tower->floors[1].rhs.uuid.unknown = data_blob_talloc_zero(mem_ctx, 2);
722 /* Floor 2 to num_protocols */
723 for (i = 0; i < num_protocols; i++) {
724 tower->floors[2 + i].lhs.protocol = protseq[i];
725 tower->floors[2 + i].lhs.lhs_data = data_blob_talloc(mem_ctx, NULL, 0);
726 ZERO_STRUCT(tower->floors[2 + i].rhs);
727 dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[2 + i], "");
730 /* The 4th floor contains the endpoint */
731 if (num_protocols >= 2 && binding->endpoint) {
732 status = dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[3], binding->endpoint);
733 if (NT_STATUS_IS_ERR(status)) {
738 /* The 5th contains the network address */
739 if (num_protocols >= 3 && binding->host) {
740 if (is_ipaddress(binding->host)) {
741 status = dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[4],
744 /* note that we don't attempt to resolve the
745 name here - when we get a hostname here we
746 are in the client code, and want to put in
747 a wildcard all-zeros IP for the server to
749 status = dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[4],
752 if (NT_STATUS_IS_ERR(status)) {
760 NTSTATUS dcerpc_epm_map_binding(TALLOC_CTX *mem_ctx, struct dcerpc_binding *binding,
761 const struct dcerpc_interface_table *table, struct event_context *ev)
763 struct dcerpc_pipe *p;
766 struct policy_handle handle;
768 struct epm_twr_t twr, *twr_r;
769 struct dcerpc_binding *epmapper_binding;
772 struct cli_credentials *anon_creds
773 = cli_credentials_init(mem_ctx);
774 cli_credentials_set_conf(anon_creds);
775 cli_credentials_set_anonymous(anon_creds);
777 /* First, check if there is a default endpoint specified in the IDL */
780 struct dcerpc_binding *default_binding;
782 /* Find one of the default pipes for this interface */
783 for (i = 0; i < table->endpoints->count; i++) {
784 status = dcerpc_parse_binding(mem_ctx, table->endpoints->names[i], &default_binding);
786 if (NT_STATUS_IS_OK(status)) {
787 if (default_binding->transport == binding->transport && default_binding->endpoint) {
788 binding->endpoint = talloc_reference(binding, default_binding->endpoint);
789 talloc_free(default_binding);
792 talloc_free(default_binding);
798 epmapper_binding = talloc_zero(mem_ctx, struct dcerpc_binding);
799 if (!epmapper_binding) {
800 return NT_STATUS_NO_MEMORY;
803 epmapper_binding->transport = binding->transport;
804 epmapper_binding->host = talloc_reference(epmapper_binding,
806 epmapper_binding->options = NULL;
807 epmapper_binding->flags = 0;
808 epmapper_binding->endpoint = NULL;
810 status = dcerpc_pipe_connect_b(mem_ctx,
813 &dcerpc_table_epmapper,
816 if (!NT_STATUS_IS_OK(status)) {
823 binding->object = table->uuid;
824 binding->object_version = table->if_version;
826 status = dcerpc_binding_build_tower(p, binding, &twr.tower);
827 if (NT_STATUS_IS_ERR(status)) {
831 /* with some nice pretty paper around it of course */
833 r.in.map_tower = &twr;
834 r.in.entry_handle = &handle;
836 r.out.entry_handle = &handle;
838 status = dcerpc_epm_Map(p, p, &r);
839 if (!NT_STATUS_IS_OK(status)) {
843 if (r.out.result != 0 || r.out.num_towers != 1) {
845 return NT_STATUS_PORT_UNREACHABLE;
848 twr_r = r.out.towers[0].twr;
851 return NT_STATUS_PORT_UNREACHABLE;
854 if (twr_r->tower.num_floors != twr.tower.num_floors ||
855 twr_r->tower.floors[3].lhs.protocol != twr.tower.floors[3].lhs.protocol) {
857 return NT_STATUS_PORT_UNREACHABLE;
860 binding->endpoint = talloc_reference(binding, dcerpc_floor_get_rhs_data(mem_ctx, &twr_r->tower.floors[3]));
869 perform an authenticated bind if needed
871 NTSTATUS dcerpc_pipe_auth(struct dcerpc_pipe *p,
872 struct dcerpc_binding *binding,
873 const struct dcerpc_interface_table *table,
874 struct cli_credentials *credentials)
878 tmp_ctx = talloc_new(p);
880 p->conn->flags = binding->flags;
882 /* remember the binding string for possible secondary connections */
883 p->conn->binding_string = dcerpc_binding_string(p, binding);
885 if (!cli_credentials_is_anonymous(credentials) &&
886 (binding->flags & DCERPC_SCHANNEL) &&
887 !cli_credentials_get_netlogon_creds(credentials)) {
889 /* If we don't already have netlogon credentials for
890 * the schannel bind, then we have to get these
892 status = dcerpc_bind_auth_schannel(tmp_ctx, p, table, credentials);
893 } else if (!cli_credentials_is_anonymous(credentials) &&
894 !(binding->transport == NCACN_NP &&
895 !(binding->flags & DCERPC_SIGN) &&
896 !(binding->flags & DCERPC_SEAL))) {
898 /* Perform an authenticated DCE-RPC bind, except where
899 * we ask for a connection on NCACN_NP, and that
900 * connection is not signed or sealed. For that case
901 * we rely on the already authenicated CIFS connection
906 if ((p->conn->flags & (DCERPC_SIGN|DCERPC_SEAL)) == 0) {
908 we are doing an authenticated connection,
909 but not using sign or seal. We must force
910 the CONNECT dcerpc auth type as a NONE auth
911 type doesn't allow authentication
912 information to be passed.
914 p->conn->flags |= DCERPC_CONNECT;
917 if (binding->flags & DCERPC_AUTH_SPNEGO) {
918 auth_type = DCERPC_AUTH_TYPE_SPNEGO;
919 } else if (binding->flags & DCERPC_AUTH_KRB5) {
920 auth_type = DCERPC_AUTH_TYPE_KRB5;
921 } else if (binding->flags & DCERPC_SCHANNEL) {
922 auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
924 auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
927 status = dcerpc_bind_auth(p, table,
928 credentials, auth_type, table->authservices->names[0]);
930 status = dcerpc_bind_auth_none(p, table);
933 if (!NT_STATUS_IS_OK(status)) {
934 char *uuid_str = GUID_string(p, &table->uuid);
935 DEBUG(0,("Failed to bind to uuid %s - %s\n", uuid_str, nt_errstr(status)));
936 talloc_free(uuid_str);
938 talloc_free(tmp_ctx);
943 /* open a rpc connection to a rpc pipe on SMB using the binding
944 structure to determine the endpoint and options */
945 static NTSTATUS dcerpc_pipe_connect_ncacn_np(TALLOC_CTX *tmp_ctx,
946 struct dcerpc_pipe_connect *io)
948 if (io->binding->flags & DCERPC_SMB2) {
949 return dcerpc_pipe_connect_ncacn_np_smb2(tmp_ctx, io);
952 return dcerpc_pipe_connect_ncacn_np_smb(tmp_ctx, io);
956 /* open a rpc connection to a rpc pipe on SMP using the binding
957 structure to determine the endpoint and options */
958 static NTSTATUS dcerpc_pipe_connect_ncalrpc(TALLOC_CTX *tmp_ctx,
959 struct dcerpc_pipe *p,
960 struct dcerpc_binding *binding,
961 const struct dcerpc_interface_table *table)
965 status = dcerpc_pipe_open_pipe(p->conn, binding->endpoint);
966 if (!NT_STATUS_IS_OK(status)) {
967 DEBUG(0,("Failed to open ncalrpc pipe '%s' - %s\n",
968 binding->endpoint, nt_errstr(status)));
977 /* open a rpc connection to a rpc pipe on SMP using the binding
978 structure to determine the endpoint and options */
979 static NTSTATUS dcerpc_pipe_connect_ncacn_unix_stream(TALLOC_CTX *tmp_ctx,
980 struct dcerpc_pipe *p,
981 struct dcerpc_binding *binding,
982 const struct dcerpc_interface_table *table)
986 if (!binding->endpoint) {
987 DEBUG(0, ("Path to unix socket not specified\n"));
988 return NT_STATUS_INVALID_PARAMETER;
991 status = dcerpc_pipe_open_unix_stream(p->conn, binding->endpoint);
992 if (!NT_STATUS_IS_OK(status)) {
993 DEBUG(0,("Failed to open unix socket %s - %s\n",
994 binding->endpoint, nt_errstr(status)));
1002 /* open a rpc connection to a rpc pipe on TCP/IP sockets using the binding
1003 structure to determine the endpoint and options */
1004 static NTSTATUS dcerpc_pipe_connect_ncacn_ip_tcp(TALLOC_CTX *tmp_ctx,
1005 struct dcerpc_pipe *p,
1006 struct dcerpc_binding *binding,
1007 const struct dcerpc_interface_table *table)
1012 port = atoi(binding->endpoint);
1014 status = dcerpc_pipe_open_tcp(p->conn, binding->host, port);
1015 if (!NT_STATUS_IS_OK(status)) {
1016 DEBUG(0,("Failed to connect to %s:%d - %s\n",
1017 binding->host, port, nt_errstr(status)));
1025 /* open a rpc connection to a rpc pipe, using the specified
1026 binding structure to determine the endpoint and options */
1027 NTSTATUS dcerpc_pipe_connect_b(TALLOC_CTX *parent_ctx,
1028 struct dcerpc_pipe **pp,
1029 struct dcerpc_binding *binding,
1030 const struct dcerpc_interface_table *table,
1031 struct cli_credentials *credentials,
1032 struct event_context *ev)
1034 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
1035 struct dcerpc_pipe *p;
1036 struct dcerpc_pipe_connect pc;
1038 TALLOC_CTX *tmp_ctx;
1042 p = dcerpc_pipe_init(parent_ctx, ev);
1044 return NT_STATUS_NO_MEMORY;
1046 tmp_ctx = talloc_named(p, 0, "dcerpc_pipe_connect_b tmp_ctx");
1048 switch (binding->transport) {
1052 if (!binding->endpoint) {
1053 status = dcerpc_epm_map_binding(tmp_ctx, binding, table,
1054 p->conn->event_ctx);
1055 if (!NT_STATUS_IS_OK(status)) {
1056 DEBUG(0,("Failed to map DCERPC endpoint for '%s' - %s\n",
1057 GUID_string(tmp_ctx, &table->uuid), nt_errstr(status)));
1058 talloc_free(tmp_ctx);
1061 DEBUG(2,("Mapped to DCERPC endpoint %s\n", binding->endpoint));
1065 /* Fall through to next switch statement */
1072 pc.binding = binding;
1073 pc.interface = table;
1074 pc.creds = credentials;
1076 switch (binding->transport) {
1078 status = dcerpc_pipe_connect_ncacn_np(tmp_ctx, &pc);
1082 status = dcerpc_pipe_connect_ncacn_ip_tcp(tmp_ctx,
1085 case NCACN_UNIX_STREAM:
1086 status = dcerpc_pipe_connect_ncacn_unix_stream(tmp_ctx,
1090 status = dcerpc_pipe_connect_ncalrpc(tmp_ctx,
1094 return NT_STATUS_NOT_SUPPORTED;
1097 if (!NT_STATUS_IS_OK(status)) {
1102 status = dcerpc_pipe_auth(p, binding, table, credentials);
1103 if (!NT_STATUS_IS_OK(status)) {
1108 talloc_free(tmp_ctx);
1114 /* open a rpc connection to a rpc pipe, using the specified string
1115 binding to determine the endpoint and options */
1116 NTSTATUS dcerpc_pipe_connect(TALLOC_CTX *parent_ctx,
1117 struct dcerpc_pipe **pp,
1118 const char *binding,
1119 const struct dcerpc_interface_table *table,
1120 struct cli_credentials *credentials,
1121 struct event_context *ev)
1123 struct dcerpc_binding *b;
1125 TALLOC_CTX *tmp_ctx;
1127 tmp_ctx = talloc_named(parent_ctx, 0, "dcerpc_pipe_connect tmp_ctx");
1129 return NT_STATUS_NO_MEMORY;
1132 status = dcerpc_parse_binding(tmp_ctx, binding, &b);
1133 if (!NT_STATUS_IS_OK(status)) {
1134 DEBUG(0,("Failed to parse dcerpc binding '%s'\n", binding));
1135 talloc_free(tmp_ctx);
1139 DEBUG(3,("Using binding %s\n", dcerpc_binding_string(tmp_ctx, b)));
1141 status = dcerpc_pipe_connect_b(tmp_ctx, pp, b, table, credentials, ev);
1143 if (NT_STATUS_IS_OK(status)) {
1144 *pp = talloc_steal(parent_ctx, *pp);
1146 talloc_free(tmp_ctx);
1153 create a secondary dcerpc connection from a primary connection
1155 if the primary is a SMB connection then the secondary connection
1156 will be on the same SMB connection, but use a new fnum
1158 NTSTATUS dcerpc_secondary_connection(struct dcerpc_pipe *p, struct dcerpc_pipe **p2,
1159 struct dcerpc_binding *b)
1163 struct smbcli_tree *tree;
1164 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
1166 (*p2) = dcerpc_pipe_init(p, p->conn->event_ctx);
1168 return NT_STATUS_NO_MEMORY;
1171 switch (p->conn->transport.transport) {
1173 tree = dcerpc_smb_tree(p->conn);
1175 return NT_STATUS_INVALID_PARAMETER;
1177 status = dcerpc_pipe_open_smb((*p2)->conn, tree, b->endpoint);
1181 status = dcerpc_pipe_open_tcp((*p2)->conn, b->host, atoi(b->endpoint));
1185 status = dcerpc_pipe_open_pipe((*p2)->conn, b->endpoint);
1189 return NT_STATUS_NOT_SUPPORTED;
1192 if (!NT_STATUS_IS_OK(status)) {
1197 (*p2)->conn->flags = p->conn->flags;
1199 return NT_STATUS_OK;
1202 NTSTATUS dcerpc_generic_session_key(struct dcerpc_connection *c,
1203 DATA_BLOB *session_key)
1205 /* this took quite a few CPU cycles to find ... */
1206 session_key->data = discard_const_p(unsigned char, "SystemLibraryDTC");
1207 session_key->length = 16;
1208 return NT_STATUS_OK;
1212 fetch the user session key - may be default (above) or the SMB session key
1214 NTSTATUS dcerpc_fetch_session_key(struct dcerpc_pipe *p,
1215 DATA_BLOB *session_key)
1217 return p->conn->security_state.session_key(p->conn, session_key);
1222 log a rpc packet in a format suitable for ndrdump. This is especially useful
1223 for sealed packets, where ethereal cannot easily see the contents
1225 this triggers on a debug level of >= 10
1227 void dcerpc_log_packet(const struct dcerpc_interface_table *ndr,
1228 uint32_t opnum, uint32_t flags, DATA_BLOB *pkt)
1230 const int num_examples = 20;
1233 if (DEBUGLEVEL < 10) return;
1235 for (i=0;i<num_examples;i++) {
1237 asprintf(&name, "%s/rpclog/%s-%u.%d.%s",
1238 lp_lockdir(), ndr->name, opnum, i,
1239 (flags&NDR_IN)?"in":"out");
1243 if (!file_exist(name)) {
1244 if (file_save(name, pkt->data, pkt->length)) {
1245 DEBUG(10,("Logged rpc packet to %s\n", name));
1257 create a secondary context from a primary connection
1259 this uses dcerpc_alter_context() to create a new dcerpc context_id
1261 NTSTATUS dcerpc_secondary_context(struct dcerpc_pipe *p,
1262 struct dcerpc_pipe **pp2,
1263 const struct dcerpc_interface_table *table)
1266 struct dcerpc_pipe *p2;
1268 p2 = talloc_zero(p, struct dcerpc_pipe);
1270 return NT_STATUS_NO_MEMORY;
1272 p2->conn = talloc_reference(p2, p->conn);
1273 p2->request_timeout = p->request_timeout;
1275 p2->context_id = ++p->conn->next_context_id;
1277 p2->syntax.uuid = table->uuid;
1278 p2->syntax.if_version = table->if_version;
1280 p2->transfer_syntax = ndr_transfer_syntax;
1282 status = dcerpc_alter_context(p2, p2, &p2->syntax, &p2->transfer_syntax);
1283 if (!NT_STATUS_IS_OK(status)) {