2 Unix SMB/CIFS implementation.
4 dcerpc utility functions
6 Copyright (C) Andrew Tridgell 2003
7 Copyright (C) Jelmer Vernooij 2004
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 work out what TCP port to use for a given interface on a given host
29 NTSTATUS dcerpc_epm_map_tcp_port(const char *server,
30 const char *uuid, uint_t version,
33 struct dcerpc_pipe *p;
36 struct policy_handle handle;
38 struct epm_twr_t twr, *twr_r;
40 if (strcasecmp(uuid, DCERPC_EPMAPPER_UUID) == 0 ||
41 strcasecmp(uuid, DCERPC_MGMT_UUID) == 0) {
42 /* don't lookup epmapper via epmapper! */
43 *port = EPMAPPER_PORT;
47 status = dcerpc_pipe_open_tcp(&p, server, EPMAPPER_PORT, AF_UNSPEC );
48 if (!NT_STATUS_IS_OK(status)) {
52 /* we can use the pipes memory context here as we will have a short
54 status = dcerpc_bind_byuuid(p, p,
56 DCERPC_EPMAPPER_VERSION);
57 if (!NT_STATUS_IS_OK(status)) {
65 twr.tower.num_floors = 5;
66 twr.tower.floors = talloc(p, sizeof(twr.tower.floors[0]) * 5);
68 /* what I'd like for christmas ... */
70 /* an RPC interface ... */
71 twr.tower.floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
72 GUID_from_string(uuid, &twr.tower.floors[0].lhs.info.uuid.uuid);
73 twr.tower.floors[0].lhs.info.uuid.version = version;
74 twr.tower.floors[0].rhs.uuid.unknown = 0;
76 /* encoded with NDR ... */
77 twr.tower.floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
78 GUID_from_string(NDR_GUID, &twr.tower.floors[1].lhs.info.uuid.uuid);
79 twr.tower.floors[1].lhs.info.uuid.version = NDR_GUID_VERSION;
80 twr.tower.floors[1].rhs.uuid.unknown = 0;
82 /* on an RPC connection ... */
83 twr.tower.floors[2].lhs.protocol = EPM_PROTOCOL_NCACN;
84 twr.tower.floors[2].lhs.info.lhs_data = data_blob(NULL, 0);
85 twr.tower.floors[2].rhs.ncacn.minor_version = 0;
87 /* on a TCP port ... */
88 twr.tower.floors[3].lhs.protocol = EPM_PROTOCOL_TCP;
89 twr.tower.floors[3].lhs.info.lhs_data = data_blob(NULL, 0);
90 twr.tower.floors[3].rhs.tcp.port = 0;
92 /* on an IP link ... */
93 twr.tower.floors[4].lhs.protocol = EPM_PROTOCOL_IP;
94 twr.tower.floors[4].lhs.info.lhs_data = data_blob(NULL, 0);
95 twr.tower.floors[4].rhs.ip.address = 0;
97 /* with some nice pretty paper around it of course */
99 r.in.map_tower = &twr;
100 r.in.entry_handle = &handle;
102 r.out.entry_handle = &handle;
104 status = dcerpc_epm_Map(p, p, &r);
105 if (!NT_STATUS_IS_OK(status)) {
106 dcerpc_pipe_close(p);
109 if (r.out.result != 0 || r.out.num_towers != 1) {
110 dcerpc_pipe_close(p);
111 return NT_STATUS_PORT_UNREACHABLE;
114 twr_r = r.out.towers[0].twr;
116 dcerpc_pipe_close(p);
117 return NT_STATUS_PORT_UNREACHABLE;
120 if (twr_r->tower.num_floors != 5 ||
121 twr_r->tower.floors[3].lhs.protocol != twr.tower.floors[3].lhs.protocol) {
122 dcerpc_pipe_close(p);
123 return NT_STATUS_PORT_UNREACHABLE;
126 *port = twr_r->tower.floors[3].rhs.tcp.port;
128 dcerpc_pipe_close(p);
134 find the pipe name for a local IDL interface
136 const char *idl_pipe_name(const char *uuid, uint32_t if_version)
139 for (i=0;dcerpc_pipes[i];i++) {
140 if (strcasecmp(dcerpc_pipes[i]->uuid, uuid) == 0 &&
141 dcerpc_pipes[i]->if_version == if_version) {
142 return dcerpc_pipes[i]->name;
149 find the number of calls defined by local IDL
151 int idl_num_calls(const char *uuid, uint32_t if_version)
154 for (i=0;dcerpc_pipes[i];i++) {
155 if (strcasecmp(dcerpc_pipes[i]->uuid, uuid) == 0 &&
156 dcerpc_pipes[i]->if_version == if_version) {
157 return dcerpc_pipes[i]->num_calls;
165 find a dcerpc interface by name
167 const struct dcerpc_interface_table *idl_iface_by_name(const char *name)
170 for (i=0;dcerpc_pipes[i];i++) {
171 if (strcasecmp(dcerpc_pipes[i]->name, name) == 0) {
172 return dcerpc_pipes[i];
179 find a dcerpc interface by uuid
181 const struct dcerpc_interface_table *idl_iface_by_uuid(const char *uuid)
184 for (i=0;dcerpc_pipes[i];i++) {
185 if (strcasecmp(dcerpc_pipes[i]->uuid, uuid) == 0) {
186 return dcerpc_pipes[i];
195 push a dcerpc_packet into a blob, potentially with auth info
197 NTSTATUS dcerpc_push_auth(DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
198 struct dcerpc_packet *pkt,
199 struct dcerpc_auth *auth_info)
202 struct ndr_push *ndr;
204 ndr = ndr_push_init_ctx(mem_ctx);
206 return NT_STATUS_NO_MEMORY;
209 if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
210 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
214 pkt->auth_length = auth_info->credentials.length;
216 pkt->auth_length = 0;
219 status = ndr_push_dcerpc_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
220 if (!NT_STATUS_IS_OK(status)) {
225 status = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, auth_info);
228 *blob = ndr_push_blob(ndr);
230 /* fill in the frag length */
231 dcerpc_set_frag_length(blob, blob->length);
236 #define MAX_PROTSEQ 10
238 static const struct {
240 enum dcerpc_transport_t transport;
242 enum epm_protocols protseq[MAX_PROTSEQ];
244 { "ncacn_np", NCACN_NP, 3,
245 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SMB, EPM_PROTOCOL_PIPE }},
246 { "ncacn_ip_tcp", NCACN_IP_TCP, 3,
247 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_IP, EPM_PROTOCOL_TCP } },
248 { "ncadg_ip_udp", NCACN_IP_UDP, 3,
249 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_IP, EPM_PROTOCOL_UDP } },
250 { "ncalrpc", NCALRPC, 2,
251 { EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_PIPE } },
252 { "ncacn_unix_stream", NCACN_UNIX_STREAM, 2,
253 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_UNIX_DS } },
254 { "ncadg_unix_dgram", NCADG_UNIX_DGRAM, 2,
255 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UNIX_DS } },
256 { "ncacn_at_dsp", NCACN_AT_DSP, 3,
257 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DSP } },
258 { "ncadg_at_ddp", NCADG_AT_DDP, 3,
259 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DDP } },
260 { "ncacn_vns_ssp", NCACN_VNS_SPP, 3,
261 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_SPP } },
262 { "ncacn_vns_ipc", NCACN_VNS_IPC, 3,
263 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_IPC }, },
266 static const struct {
269 } ncacn_options[] = {
270 {"sign", DCERPC_SIGN},
271 {"seal", DCERPC_SEAL},
272 {"connect", DCERPC_CONNECT},
273 {"validate", DCERPC_DEBUG_VALIDATE_BOTH},
274 {"print", DCERPC_DEBUG_PRINT_BOTH},
275 {"padcheck", DCERPC_DEBUG_PAD_CHECK},
276 {"bigendian", DCERPC_PUSH_BIGENDIAN}
282 form a binding string from a binding structure
284 const char *dcerpc_binding_string(TALLOC_CTX *mem_ctx, const struct dcerpc_binding *b)
288 const char *t_name=NULL;
290 for (i=0;i<ARRAY_SIZE(transports);i++) {
291 if (transports[i].transport == b->transport) {
292 t_name = transports[i].name;
300 s = talloc_asprintf(mem_ctx, "%s@", GUID_string(mem_ctx, b->object));
303 s = talloc_asprintf_append(s, "%s:%s", t_name, b->host);
306 if ((!b->options || !b->options[0]) && !b->flags) {
310 s = talloc_asprintf_append(s, "[");
312 /* this is a *really* inefficent way of dealing with strings,
313 but this is rarely called and the strings are always short,
315 for (i=0;b->options && b->options[i];i++) {
316 s = talloc_asprintf_append(s, "%s,", b->options[i]);
319 for (i=0;i<ARRAY_SIZE(ncacn_options);i++) {
320 if (b->flags & ncacn_options[i].flag) {
321 s = talloc_asprintf_append(s, "%s,", ncacn_options[i].name);
327 s = talloc_asprintf_append(s, "]");
333 parse a binding string into a dcerpc_binding structure
335 NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *s, struct dcerpc_binding *b)
337 char *options, *type;
339 int i, j, comma_count;
343 if (p && PTR_DIFF(p, s) == 36) { /* 36 is the length of a UUID */
346 b->object = talloc_p(mem_ctx, struct GUID);
348 status = GUID_from_string(s, b->object);
350 if (NT_STATUS_IS_ERR(status)) {
351 DEBUG(0, ("Failed parsing UUID\n"));
362 return NT_STATUS_INVALID_PARAMETER;
365 type = talloc_strndup(mem_ctx, s, PTR_DIFF(p, s));
367 return NT_STATUS_NO_MEMORY;
370 for (i=0;i<ARRAY_SIZE(transports);i++) {
371 if (strcasecmp(type, transports[i].name) == 0) {
372 b->transport = transports[i].transport;
376 if (i==ARRAY_SIZE(transports)) {
377 DEBUG(0,("Unknown dcerpc transport '%s'\n", type));
378 return NT_STATUS_INVALID_PARAMETER;
385 b->host = talloc_strndup(mem_ctx, s, PTR_DIFF(p, s));
386 options = talloc_strdup(mem_ctx, p+1);
387 if (options[strlen(options)-1] != ']') {
388 return NT_STATUS_INVALID_PARAMETER;
390 options[strlen(options)-1] = 0;
392 b->host = talloc_strdup(mem_ctx, s);
397 return NT_STATUS_NO_MEMORY;
407 comma_count = count_chars(options, ',');
408 b->options = talloc_array_p(mem_ctx, const char *, comma_count+2);
410 return NT_STATUS_NO_MEMORY;
413 for (i=0; (p = strchr(options, ',')); i++) {
414 b->options[i] = talloc_strndup(mem_ctx, options, PTR_DIFF(p, options));
415 if (!b->options[i]) {
416 return NT_STATUS_NO_MEMORY;
420 b->options[i] = options;
421 b->options[i+1] = NULL;
423 /* some options are pre-parsed for convenience */
424 for (i=0;b->options[i];i++) {
425 for (j=0;j<ARRAY_SIZE(ncacn_options);j++) {
426 if (strcasecmp(ncacn_options[j].name, b->options[i]) == 0) {
428 b->flags |= ncacn_options[j].flag;
429 for (k=i;b->options[k];k++) {
430 b->options[k] = b->options[k+1];
441 static NTSTATUS floor_set_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *floor, const char *data)
443 switch (floor->lhs.protocol) {
444 case EPM_PROTOCOL_TCP:
445 floor->rhs.tcp.port = atoi(data);
448 case EPM_PROTOCOL_UDP:
449 floor->rhs.udp.port = atoi(data);
452 case EPM_PROTOCOL_HTTP:
453 floor->rhs.http.port = atoi(data);
456 case EPM_PROTOCOL_IP:
457 floor->rhs.ip.address = 0;
459 /* Only try to put in a IPv4 address. Windows 2003 just returns
460 * 0.0.0.0 for IPv6 addresses */
461 if (strlen(data) > 0) {
462 struct addrinfo hints, *res;
464 memset(&hints, 0, sizeof(struct addrinfo));
466 hints.ai_family = AF_INET;
468 if (getaddrinfo(data, NULL, &hints, &res) < 0) {
469 return NT_STATUS_BAD_NETWORK_NAME;
472 floor->rhs.ip.address = ntohl(((struct in_addr *)res->ai_addr)->s_addr);
478 case EPM_PROTOCOL_NCACN:
479 floor->rhs.ncacn.minor_version = 0;
482 case EPM_PROTOCOL_NCADG:
483 floor->rhs.ncadg.minor_version = 0;
486 case EPM_PROTOCOL_SMB:
487 floor->rhs.smb.unc = talloc_strdup(mem_ctx, data);
488 if (!floor->rhs.smb.unc) {
489 return NT_STATUS_NO_MEMORY;
493 case EPM_PROTOCOL_PIPE:
494 floor->rhs.pipe.path = talloc_strdup(mem_ctx, data);
495 if (!floor->rhs.pipe.path) {
496 return NT_STATUS_NO_MEMORY;
500 case EPM_PROTOCOL_NETBIOS:
501 floor->rhs.netbios.name = talloc_strdup(mem_ctx, data);
502 if (!floor->rhs.netbios.name) {
503 return NT_STATUS_NO_MEMORY;
507 case EPM_PROTOCOL_NCALRPC:
510 case EPM_PROTOCOL_VINES_SPP:
511 floor->rhs.vines_spp.port = atoi(data);
514 case EPM_PROTOCOL_VINES_IPC:
515 floor->rhs.vines_ipc.port = atoi(data);
518 case EPM_PROTOCOL_STREETTALK:
519 floor->rhs.streettalk.streettalk = talloc_strdup(mem_ctx, data);
520 if (!floor->rhs.streettalk.streettalk) {
521 return NT_STATUS_NO_MEMORY;
525 case EPM_PROTOCOL_UNIX_DS:
526 floor->rhs.unix_ds.path = talloc_strdup(mem_ctx, data);
527 if (!floor->rhs.unix_ds.path) {
528 return NT_STATUS_NO_MEMORY;
532 case EPM_PROTOCOL_NULL:
536 return NT_STATUS_NOT_SUPPORTED;
540 NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx, struct dcerpc_binding *binding, struct epm_tower **tower)
542 const enum epm_protocols *protseq;
543 int num_protocols = -1, i;
546 *tower = talloc_p(mem_ctx, struct epm_tower);
549 return NT_STATUS_NO_MEMORY;
553 for (i=0;i<ARRAY_SIZE(transports);i++) {
554 if (transports[i].transport == binding->transport) {
555 protseq = transports[i].protseq;
556 num_protocols = transports[i].num_protocols;
561 if (num_protocols == -1) {
562 DEBUG(0, ("Unable to find transport with id '%d'\n", binding->transport));
563 return NT_STATUS_UNSUCCESSFUL;
566 (*tower)->num_floors = 2 + num_protocols;
567 (*tower)->floors = talloc_array_p(mem_ctx, struct epm_floor, (*tower)->num_floors);
570 (*tower)->floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
571 if (binding->object) {
572 (*tower)->floors[0].lhs.info.uuid.uuid = *binding->object;
574 (*tower)->floors[0].lhs.info.uuid.version = 0;
577 (*tower)->floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
578 (*tower)->floors[1].lhs.info.uuid.version = NDR_GUID_VERSION;
579 status = GUID_from_string(NDR_GUID, &(*tower)->floors[1].lhs.info.uuid.uuid);
580 if (NT_STATUS_IS_ERR(status)) {
584 /* Floor 2 to num_protocols */
585 for (i = 0; i < num_protocols; i++) {
586 (*tower)->floors[2 + i].lhs.protocol = protseq[i];
589 /* The top floor contains the endpoint */
590 if (num_protocols >= 1 && binding->options && binding->options[0]) {
591 status = floor_set_rhs_data(mem_ctx, &(*tower)->floors[2 + num_protocols - 1], binding->options[0]);
592 if (NT_STATUS_IS_ERR(status)) {
597 /* The second-to-top floor contains the network address */
598 if (num_protocols >= 2 && binding->host) {
599 status = floor_set_rhs_data(mem_ctx, &(*tower)->floors[2 + num_protocols - 2], binding->host);
600 if (NT_STATUS_IS_ERR(status)) {
609 /* open a rpc connection to a rpc pipe on SMB using the binding
610 structure to determine the endpoint and options */
611 static NTSTATUS dcerpc_pipe_connect_ncacn_np(struct dcerpc_pipe **p,
612 struct dcerpc_binding *binding,
613 const char *pipe_uuid,
614 uint32_t pipe_version,
616 const char *username,
617 const char *password)
621 struct smbcli_state *cli;
622 const char *pipe_name;
623 TALLOC_CTX *mem_ctx = talloc_init("dcerpc_pipe_connect_ncacn_np");
625 if (!binding->options || !binding->options[0] || !strlen(binding->options[0])) {
626 const struct dcerpc_interface_table *table = idl_iface_by_uuid(pipe_uuid);
627 struct dcerpc_binding default_binding;
631 DEBUG(0,("Unknown interface endpoint '%s'\n", pipe_uuid));
632 talloc_destroy(mem_ctx);
633 return NT_STATUS_INVALID_PARAMETER;
636 /* Find one of the default pipes for this interface */
637 for (i = 0; i < table->endpoints->count; i++) {
638 status = dcerpc_parse_binding(mem_ctx, table->endpoints->names[i], &default_binding);
640 if (NT_STATUS_IS_OK(status) && default_binding.transport == NCACN_NP) {
641 pipe_name = default_binding.options[0];
647 pipe_name = binding->options[0];
650 if (!strncasecmp(pipe_name, "/pipe/", 6) ||
651 !strncasecmp(pipe_name, "\\pipe\\", 6)) {
655 if (pipe_name[0] != '\\') {
656 pipe_name = talloc_asprintf(mem_ctx, "\\%s", pipe_name);
659 if (!username || !username[0]) {
660 status = smbcli_full_connection(NULL, &cli, lp_netbios_name(),
663 "", "", NULL, 0, &retry);
665 status = smbcli_full_connection(NULL, &cli, lp_netbios_name(),
669 password, 0, &retry);
671 if (!NT_STATUS_IS_OK(status)) {
672 DEBUG(0,("Failed to connect to %s - %s\n", binding->host, nt_errstr(status)));
673 talloc_destroy(mem_ctx);
677 status = dcerpc_pipe_open_smb(p, cli->tree, pipe_name);
678 if (!NT_STATUS_IS_OK(status)) {
679 DEBUG(0,("Failed to open pipe %s - %s\n", pipe_name, nt_errstr(status)));
681 smbcli_shutdown(cli);
682 talloc_destroy(mem_ctx);
686 talloc_destroy(mem_ctx);
688 /* this ensures that the reference count is decremented so
689 a pipe close will really close the link */
690 talloc_steal(*p, cli);
692 (*p)->flags = binding->flags;
694 /* remember the binding string for possible secondary connections */
695 (*p)->binding_string = dcerpc_binding_string((*p), binding);
697 if (username && username[0] && (binding->flags & DCERPC_SCHANNEL_ANY)) {
698 status = dcerpc_bind_auth_schannel(*p, pipe_uuid, pipe_version,
699 domain, username, password);
700 } else if (username && username[0] &&
701 (binding->flags & (DCERPC_CONNECT|DCERPC_SIGN|DCERPC_SEAL))) {
702 status = dcerpc_bind_auth_ntlm(*p, pipe_uuid, pipe_version, domain, username, password);
704 status = dcerpc_bind_auth_none(*p, pipe_uuid, pipe_version);
708 if (!NT_STATUS_IS_OK(status)) {
709 DEBUG(0,("Failed to bind to uuid %s - %s\n", pipe_uuid, nt_errstr(status)));
710 dcerpc_pipe_close(*p);
719 /* open a rpc connection to a rpc pipe on SMP using the binding
720 structure to determine the endpoint and options */
721 static NTSTATUS dcerpc_pipe_connect_ncacn_ip_tcp(struct dcerpc_pipe **p,
722 struct dcerpc_binding *binding,
723 const char *pipe_uuid,
724 uint32_t pipe_version,
726 const char *username,
727 const char *password)
732 if (binding->options && binding->options[0] && strlen(binding->options[0])) {
733 port = atoi(binding->options[0]);
737 status = dcerpc_epm_map_tcp_port(binding->host,
738 pipe_uuid, pipe_version,
740 if (!NT_STATUS_IS_OK(status)) {
741 DEBUG(0,("Failed to map DCERPC/TCP port for '%s' - %s\n",
742 pipe_uuid, nt_errstr(status)));
745 DEBUG(1,("Mapped to DCERPC/TCP port %u\n", port));
748 status = dcerpc_pipe_open_tcp(p, binding->host, port, AF_UNSPEC);
749 if (!NT_STATUS_IS_OK(status)) {
750 DEBUG(0,("Failed to connect to %s:%d\n", binding->host, port));
754 (*p)->flags = binding->flags;
756 /* remember the binding string for possible secondary connections */
757 (*p)->binding_string = dcerpc_binding_string((*p), binding);
759 if (username && username[0] && (binding->flags & DCERPC_SCHANNEL_ANY)) {
760 status = dcerpc_bind_auth_schannel(*p, pipe_uuid, pipe_version,
761 domain, username, password);
762 } else if (username && username[0]) {
763 status = dcerpc_bind_auth_ntlm(*p, pipe_uuid, pipe_version, domain, username, password);
765 status = dcerpc_bind_auth_none(*p, pipe_uuid, pipe_version);
768 if (!NT_STATUS_IS_OK(status)) {
769 DEBUG(0,("Failed to bind to uuid %s - %s\n",
770 pipe_uuid, nt_errstr(status)));
771 dcerpc_pipe_close(*p);
780 /* open a rpc connection to a rpc pipe, using the specified
781 binding structure to determine the endpoint and options */
782 NTSTATUS dcerpc_pipe_connect_b(struct dcerpc_pipe **p,
783 struct dcerpc_binding *binding,
784 const char *pipe_uuid,
785 uint32_t pipe_version,
787 const char *username,
788 const char *password)
790 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
792 switch (binding->transport) {
794 status = dcerpc_pipe_connect_ncacn_np(p, binding, pipe_uuid, pipe_version,
795 domain, username, password);
798 status = dcerpc_pipe_connect_ncacn_ip_tcp(p, binding, pipe_uuid, pipe_version,
799 domain, username, password);
802 return NT_STATUS_NOT_SUPPORTED;
809 /* open a rpc connection to a rpc pipe, using the specified string
810 binding to determine the endpoint and options */
811 NTSTATUS dcerpc_pipe_connect(struct dcerpc_pipe **p,
813 const char *pipe_uuid,
814 uint32_t pipe_version,
816 const char *username,
817 const char *password)
819 struct dcerpc_binding b;
823 mem_ctx = talloc_init("dcerpc_pipe_connect");
824 if (!mem_ctx) return NT_STATUS_NO_MEMORY;
826 status = dcerpc_parse_binding(mem_ctx, binding, &b);
827 if (!NT_STATUS_IS_OK(status)) {
828 DEBUG(0,("Failed to parse dcerpc binding '%s'\n", binding));
829 talloc_destroy(mem_ctx);
833 DEBUG(3,("Using binding %s\n", dcerpc_binding_string(mem_ctx, &b)));
835 status = dcerpc_pipe_connect_b(p, &b, pipe_uuid, pipe_version, domain, username, password);
837 talloc_destroy(mem_ctx);
843 create a secondary dcerpc connection from a primary connection
845 if the primary is a SMB connection then the secondary connection
846 will be on the same SMB connection, but use a new fnum
848 NTSTATUS dcerpc_secondary_connection(struct dcerpc_pipe *p, struct dcerpc_pipe **p2,
849 const char *pipe_name,
850 const char *pipe_uuid,
851 uint32_t pipe_version)
853 struct smbcli_tree *tree;
854 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
855 struct dcerpc_binding b;
857 switch (p->transport.transport) {
859 tree = dcerpc_smb_tree(p);
861 return NT_STATUS_INVALID_PARAMETER;
864 status = dcerpc_pipe_open_smb(p2, tree, pipe_name);
868 status = dcerpc_parse_binding(p, p->binding_string, &b);
869 if (!NT_STATUS_IS_OK(status)) {
872 b.flags &= ~DCERPC_AUTH_OPTIONS;
873 status = dcerpc_pipe_connect_ncacn_ip_tcp(p2, &b, pipe_uuid,
878 return NT_STATUS_NOT_SUPPORTED;
881 if (!NT_STATUS_IS_OK(status)) {
885 (*p2)->flags = p->flags;
887 status = dcerpc_bind_auth_none(*p2, pipe_uuid, pipe_version);
888 if (!NT_STATUS_IS_OK(status)) {
895 NTSTATUS dcerpc_generic_session_key(struct dcerpc_pipe *p,
896 DATA_BLOB *session_key)
898 /* this took quite a few CPU cycles to find ... */
899 session_key->data = discard_const_p(unsigned char, "SystemLibraryDTC");
900 session_key->length = 16;
905 fetch the user session key - may be default (above) or the SMB session key
907 NTSTATUS dcerpc_fetch_session_key(struct dcerpc_pipe *p,
908 DATA_BLOB *session_key)
910 return p->security_state.session_key(p, session_key);
915 log a rpc packet in a format suitable for ndrdump. This is especially useful
916 for sealed packets, where ethereal cannot easily see the contents
918 this triggers on a debug level of >= 10
920 void dcerpc_log_packet(const struct dcerpc_interface_table *ndr,
921 uint32_t opnum, uint32_t flags, DATA_BLOB *pkt)
923 const int num_examples = 20;
926 if (DEBUGLEVEL < 10) return;
928 for (i=0;i<num_examples;i++) {
930 asprintf(&name, "%s/rpclog/%s-%u.%d.%s",
931 lp_lockdir(), ndr->name, opnum, i,
932 (flags&NDR_IN)?"in":"out");
936 if (!file_exist(name, NULL)) {
937 if (file_save(name, pkt->data, pkt->length)) {
938 DEBUG(10,("Logged rpc packet to %s\n", name));