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.
25 #include "system/network.h"
26 #include "librpc/gen_ndr/ndr_epmapper.h"
29 find the pipe name for a local IDL interface
31 const char *idl_pipe_name(const char *uuid, uint32_t if_version)
33 const struct dcerpc_interface_list *l;
34 for (l=librpc_dcerpc_pipes();l;l=l->next) {
35 if (strcasecmp(l->table->uuid, uuid) == 0 &&
36 l->table->if_version == if_version) {
37 return l->table->name;
44 find the number of calls defined by local IDL
46 int idl_num_calls(const char *uuid, uint32_t if_version)
48 const struct dcerpc_interface_list *l;
49 for (l=librpc_dcerpc_pipes();l;l=l->next){
50 if (strcasecmp(l->table->uuid, uuid) == 0 &&
51 l->table->if_version == if_version) {
52 return l->table->num_calls;
60 find a dcerpc interface by name
62 const struct dcerpc_interface_table *idl_iface_by_name(const char *name)
64 const struct dcerpc_interface_list *l;
65 for (l=librpc_dcerpc_pipes();l;l=l->next) {
66 if (strcasecmp(l->table->name, name) == 0) {
74 find a dcerpc interface by uuid
76 const struct dcerpc_interface_table *idl_iface_by_uuid(const char *uuid)
78 const struct dcerpc_interface_list *l;
79 for (l=librpc_dcerpc_pipes();l;l=l->next) {
80 if (strcasecmp(l->table->uuid, uuid) == 0) {
89 push a dcerpc_packet into a blob, potentially with auth info
91 NTSTATUS dcerpc_push_auth(DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
92 struct dcerpc_packet *pkt,
93 struct dcerpc_auth *auth_info)
98 ndr = ndr_push_init_ctx(mem_ctx);
100 return NT_STATUS_NO_MEMORY;
103 if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
104 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
107 if (pkt->pfc_flags & DCERPC_PFC_FLAG_ORPC) {
108 ndr->flags |= LIBNDR_FLAG_OBJECT_PRESENT;
112 pkt->auth_length = auth_info->credentials.length;
114 pkt->auth_length = 0;
117 status = ndr_push_dcerpc_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
118 if (!NT_STATUS_IS_OK(status)) {
123 status = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, auth_info);
126 *blob = ndr_push_blob(ndr);
128 /* fill in the frag length */
129 dcerpc_set_frag_length(blob, blob->length);
134 #define MAX_PROTSEQ 10
136 static const struct {
138 enum dcerpc_transport_t transport;
140 enum epm_protocol protseq[MAX_PROTSEQ];
142 { "ncacn_np", NCACN_NP, 3,
143 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SMB, EPM_PROTOCOL_NETBIOS }},
144 { "ncacn_ip_tcp", NCACN_IP_TCP, 3,
145 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_TCP, EPM_PROTOCOL_IP } },
146 { "ncacn_http", NCACN_HTTP, 3,
147 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_HTTP, EPM_PROTOCOL_IP } },
148 { "ncadg_ip_udp", NCACN_IP_UDP, 3,
149 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UDP, EPM_PROTOCOL_IP } },
150 { "ncalrpc", NCALRPC, 2,
151 { EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_PIPE } },
152 { "ncacn_unix_stream", NCACN_UNIX_STREAM, 2,
153 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_UNIX_DS } },
154 { "ncadg_unix_dgram", NCADG_UNIX_DGRAM, 2,
155 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_UNIX_DS } },
156 { "ncacn_at_dsp", NCACN_AT_DSP, 3,
157 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DSP } },
158 { "ncadg_at_ddp", NCADG_AT_DDP, 3,
159 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_APPLETALK, EPM_PROTOCOL_DDP } },
160 { "ncacn_vns_ssp", NCACN_VNS_SPP, 3,
161 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_SPP } },
162 { "ncacn_vns_ipc", NCACN_VNS_IPC, 3,
163 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_STREETTALK, EPM_PROTOCOL_VINES_IPC }, },
164 { "ncadg_ipx", NCADG_IPX, 2,
165 { EPM_PROTOCOL_NCADG, EPM_PROTOCOL_IPX },
167 { "ncacn_spx", NCACN_SPX, 2,
168 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SPX },
172 static const struct {
175 } ncacn_options[] = {
176 {"sign", DCERPC_SIGN},
177 {"seal", DCERPC_SEAL},
178 {"connect", DCERPC_CONNECT},
179 {"validate", DCERPC_DEBUG_VALIDATE_BOTH},
180 {"print", DCERPC_DEBUG_PRINT_BOTH},
181 {"padcheck", DCERPC_DEBUG_PAD_CHECK},
182 {"bigendian", DCERPC_PUSH_BIGENDIAN}
188 form a binding string from a binding structure
190 const char *dcerpc_binding_string(TALLOC_CTX *mem_ctx, const struct dcerpc_binding *b)
192 char *s = talloc_strdup(mem_ctx, "");
194 const char *t_name=NULL;
196 for (i=0;i<ARRAY_SIZE(transports);i++) {
197 if (transports[i].transport == b->transport) {
198 t_name = transports[i].name;
205 if (!GUID_all_zero(&b->object)) {
206 s = talloc_asprintf(s, "%s@",
207 GUID_string(mem_ctx, &b->object));
210 s = talloc_asprintf_append(s, "%s:", t_name);
214 s = talloc_asprintf_append(s, "%s", b->host);
217 if (!b->endpoint && !b->options && !b->flags) {
221 s = talloc_asprintf_append(s, "[");
224 s = talloc_asprintf_append(s, "%s", b->endpoint);
227 /* this is a *really* inefficent way of dealing with strings,
228 but this is rarely called and the strings are always short,
230 for (i=0;b->options && b->options[i];i++) {
231 s = talloc_asprintf_append(s, ",%s", b->options[i]);
235 for (i=0;i<ARRAY_SIZE(ncacn_options);i++) {
236 if (b->flags & ncacn_options[i].flag) {
237 s = talloc_asprintf_append(s, ",%s", ncacn_options[i].name);
242 s = talloc_asprintf_append(s, "]");
248 parse a binding string into a dcerpc_binding structure
250 NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *s, struct dcerpc_binding *b)
252 char *options, *type;
254 int i, j, comma_count;
258 if (p && PTR_DIFF(p, s) == 36) { /* 36 is the length of a UUID */
261 status = GUID_from_string(s, &b->object);
263 if (NT_STATUS_IS_ERR(status)) {
264 DEBUG(0, ("Failed parsing UUID\n"));
270 ZERO_STRUCT(b->object);
273 b->object_version = 0;
277 return NT_STATUS_INVALID_PARAMETER;
280 type = talloc_strndup(mem_ctx, s, PTR_DIFF(p, s));
282 return NT_STATUS_NO_MEMORY;
285 for (i=0;i<ARRAY_SIZE(transports);i++) {
286 if (strcasecmp(type, transports[i].name) == 0) {
287 b->transport = transports[i].transport;
291 if (i==ARRAY_SIZE(transports)) {
292 DEBUG(0,("Unknown dcerpc transport '%s'\n", type));
293 return NT_STATUS_INVALID_PARAMETER;
300 b->host = talloc_strndup(mem_ctx, s, PTR_DIFF(p, s));
301 options = talloc_strdup(mem_ctx, p+1);
302 if (options[strlen(options)-1] != ']') {
303 return NT_STATUS_INVALID_PARAMETER;
305 options[strlen(options)-1] = 0;
307 b->host = talloc_strdup(mem_ctx, s);
312 return NT_STATUS_NO_MEMORY;
323 comma_count = count_chars(options, ',');
325 b->options = talloc_array_p(mem_ctx, const char *, comma_count+2);
327 return NT_STATUS_NO_MEMORY;
330 for (i=0; (p = strchr(options, ',')); i++) {
331 b->options[i] = talloc_strndup(mem_ctx, options, PTR_DIFF(p, options));
332 if (!b->options[i]) {
333 return NT_STATUS_NO_MEMORY;
337 b->options[i] = options;
338 b->options[i+1] = NULL;
340 /* some options are pre-parsed for convenience */
341 for (i=0;b->options[i];i++) {
342 for (j=0;j<ARRAY_SIZE(ncacn_options);j++) {
343 if (strcasecmp(ncacn_options[j].name, b->options[i]) == 0) {
345 b->flags |= ncacn_options[j].flag;
346 for (k=i;b->options[k];k++) {
347 b->options[k] = b->options[k+1];
356 /* Endpoint is first option */
357 b->endpoint = b->options[0];
358 if (strlen(b->endpoint) == 0) b->endpoint = NULL;
360 for (i=0;b->options[i];i++) {
361 b->options[i] = b->options[i+1];
365 if (b->options[0] == NULL)
371 const char *dcerpc_floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *floor)
373 switch (floor->lhs.protocol) {
374 case EPM_PROTOCOL_TCP:
375 if (floor->rhs.tcp.port == 0) return NULL;
376 return talloc_asprintf(mem_ctx, "%d", floor->rhs.tcp.port);
378 case EPM_PROTOCOL_UDP:
379 if (floor->rhs.udp.port == 0) return NULL;
380 return talloc_asprintf(mem_ctx, "%d", floor->rhs.udp.port);
382 case EPM_PROTOCOL_HTTP:
383 if (floor->rhs.http.port == 0) return NULL;
384 return talloc_asprintf(mem_ctx, "%d", floor->rhs.http.port);
386 case EPM_PROTOCOL_IP:
387 if (floor->rhs.ip.address == 0) {
393 in.addr = htonl(floor->rhs.ip.address);
394 return talloc_strdup(mem_ctx, sys_inet_ntoa(in));
397 case EPM_PROTOCOL_NCACN:
400 case EPM_PROTOCOL_NCADG:
403 case EPM_PROTOCOL_SMB:
404 if (strlen(floor->rhs.smb.unc) == 0) return NULL;
405 return talloc_strdup(mem_ctx, floor->rhs.smb.unc);
407 case EPM_PROTOCOL_PIPE:
408 if (strlen(floor->rhs.pipe.path) == 0) return NULL;
409 return talloc_strdup(mem_ctx, floor->rhs.pipe.path);
411 case EPM_PROTOCOL_NETBIOS:
412 if (strlen(floor->rhs.netbios.name) == 0) return NULL;
413 return talloc_strdup(mem_ctx, floor->rhs.netbios.name);
415 case EPM_PROTOCOL_NCALRPC:
418 case EPM_PROTOCOL_VINES_SPP:
419 return talloc_asprintf(mem_ctx, "%d", floor->rhs.vines_spp.port);
421 case EPM_PROTOCOL_VINES_IPC:
422 return talloc_asprintf(mem_ctx, "%d", floor->rhs.vines_ipc.port);
424 case EPM_PROTOCOL_STREETTALK:
425 return talloc_strdup(mem_ctx, floor->rhs.streettalk.streettalk);
427 case EPM_PROTOCOL_UNIX_DS:
428 if (strlen(floor->rhs.unix_ds.path) == 0) return NULL;
429 return talloc_strdup(mem_ctx, floor->rhs.unix_ds.path);
431 case EPM_PROTOCOL_NULL:
435 DEBUG(0,("Unsupported lhs protocol %d\n", floor->lhs.protocol));
442 static NTSTATUS dcerpc_floor_set_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *floor, const char *data)
444 switch (floor->lhs.protocol) {
445 case EPM_PROTOCOL_TCP:
446 floor->rhs.tcp.port = atoi(data);
449 case EPM_PROTOCOL_UDP:
450 floor->rhs.udp.port = atoi(data);
453 case EPM_PROTOCOL_HTTP:
454 floor->rhs.http.port = atoi(data);
457 case EPM_PROTOCOL_IP:
458 if (strlen(data) > 0) {
459 floor->rhs.ip.address = ntohl(interpret_addr(data));
461 floor->rhs.ip.address = 0;
465 case EPM_PROTOCOL_NCACN:
466 floor->rhs.ncacn.minor_version = 0;
469 case EPM_PROTOCOL_NCADG:
470 floor->rhs.ncadg.minor_version = 0;
473 case EPM_PROTOCOL_SMB:
474 floor->rhs.smb.unc = talloc_strdup(mem_ctx, data);
475 if (!floor->rhs.smb.unc) {
476 return NT_STATUS_NO_MEMORY;
480 case EPM_PROTOCOL_PIPE:
481 floor->rhs.pipe.path = talloc_strdup(mem_ctx, data);
482 if (!floor->rhs.pipe.path) {
483 return NT_STATUS_NO_MEMORY;
487 case EPM_PROTOCOL_NETBIOS:
488 floor->rhs.netbios.name = talloc_strdup(mem_ctx, data);
489 if (!floor->rhs.netbios.name) {
490 return NT_STATUS_NO_MEMORY;
494 case EPM_PROTOCOL_NCALRPC:
497 case EPM_PROTOCOL_VINES_SPP:
498 floor->rhs.vines_spp.port = atoi(data);
501 case EPM_PROTOCOL_VINES_IPC:
502 floor->rhs.vines_ipc.port = atoi(data);
505 case EPM_PROTOCOL_STREETTALK:
506 floor->rhs.streettalk.streettalk = talloc_strdup(mem_ctx, data);
507 if (!floor->rhs.streettalk.streettalk) {
508 return NT_STATUS_NO_MEMORY;
512 case EPM_PROTOCOL_UNIX_DS:
513 floor->rhs.unix_ds.path = talloc_strdup(mem_ctx, data);
514 if (!floor->rhs.unix_ds.path) {
515 return NT_STATUS_NO_MEMORY;
519 case EPM_PROTOCOL_NULL:
523 DEBUG(0,("Unsupported lhs protocol %d\n", floor->lhs.protocol));
527 return NT_STATUS_NOT_SUPPORTED;
530 enum dcerpc_transport_t dcerpc_transport_by_endpoint_protocol(int prot)
534 /* Find a transport that has 'prot' as 4th protocol */
535 for (i=0;i<ARRAY_SIZE(transports);i++) {
536 if (transports[i].num_protocols >= 2 &&
537 transports[i].protseq[1] == prot) {
538 return transports[i].transport;
542 /* Unknown transport */
546 enum dcerpc_transport_t dcerpc_transport_by_tower(struct epm_tower *tower)
550 /* Find a transport that matches this tower */
551 for (i=0;i<ARRAY_SIZE(transports);i++) {
553 if (transports[i].num_protocols != tower->num_floors - 2) {
557 for (j = 0; j < transports[i].num_protocols; j++) {
558 if (transports[i].protseq[j] != tower->floors[j+2].lhs.protocol) {
563 if (j == transports[i].num_protocols) {
564 return transports[i].transport;
568 /* Unknown transport */
572 NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx, struct epm_tower *tower, struct dcerpc_binding *binding)
574 ZERO_STRUCT(binding->object);
575 binding->options = NULL;
576 binding->host = NULL;
579 binding->transport = dcerpc_transport_by_tower(tower);
581 if (binding->transport == -1) {
582 return NT_STATUS_NOT_SUPPORTED;
585 if (tower->num_floors < 1) {
589 /* Set object uuid */
590 binding->object = tower->floors[0].lhs.info.uuid.uuid;
591 binding->object_version = tower->floors[0].lhs.info.uuid.version;
593 /* Ignore floor 1, it contains the NDR version info */
595 binding->options = NULL;
598 if (tower->num_floors >= 4) {
599 binding->endpoint = dcerpc_floor_get_rhs_data(mem_ctx, &tower->floors[3]);
601 binding->endpoint = NULL;
604 /* Set network address */
605 if (tower->num_floors >= 5) {
606 binding->host = dcerpc_floor_get_rhs_data(mem_ctx, &tower->floors[4]);
611 NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx, struct dcerpc_binding *binding, struct epm_tower *tower)
613 const enum epm_protocol *protseq = NULL;
614 int num_protocols = -1, i;
618 for (i=0;i<ARRAY_SIZE(transports);i++) {
619 if (transports[i].transport == binding->transport) {
620 protseq = transports[i].protseq;
621 num_protocols = transports[i].num_protocols;
626 if (num_protocols == -1) {
627 DEBUG(0, ("Unable to find transport with id '%d'\n", binding->transport));
628 return NT_STATUS_UNSUCCESSFUL;
631 tower->num_floors = 2 + num_protocols;
632 tower->floors = talloc_array_p(mem_ctx, struct epm_floor, tower->num_floors);
635 tower->floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
636 tower->floors[0].lhs.info.uuid.uuid = binding->object;
637 tower->floors[0].lhs.info.uuid.version = binding->object_version;
638 tower->floors[0].rhs.uuid.unknown = 0;
641 tower->floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
642 tower->floors[1].lhs.info.uuid.version = NDR_GUID_VERSION;
643 tower->floors[1].rhs.uuid.unknown = 0;
644 status = GUID_from_string(NDR_GUID, &tower->floors[1].lhs.info.uuid.uuid);
645 if (NT_STATUS_IS_ERR(status)) {
649 /* Floor 2 to num_protocols */
650 for (i = 0; i < num_protocols; i++) {
651 tower->floors[2 + i].lhs.protocol = protseq[i];
652 tower->floors[2 + i].lhs.info.lhs_data = data_blob_talloc(mem_ctx, NULL, 0);
653 ZERO_STRUCT(tower->floors[2 + i].rhs);
654 dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[2 + i], "");
657 /* The 4th floor contains the endpoint */
658 if (num_protocols >= 2 && binding->endpoint) {
659 status = dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[3], binding->endpoint);
660 if (NT_STATUS_IS_ERR(status)) {
665 /* The 5th contains the network address */
666 if (num_protocols >= 3 && binding->host) {
667 status = dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[4], binding->host);
668 if (NT_STATUS_IS_ERR(status)) {
676 NTSTATUS dcerpc_epm_map_binding(TALLOC_CTX *mem_ctx, struct dcerpc_binding *binding,
677 const char *uuid, uint_t version)
679 struct dcerpc_pipe *p;
682 struct policy_handle handle;
684 struct epm_twr_t twr, *twr_r;
685 struct dcerpc_binding epmapper_binding;
686 const struct dcerpc_interface_table *table = idl_iface_by_uuid(uuid);
689 /* First, check if there is a default endpoint specified in the IDL */
692 struct dcerpc_binding default_binding;
694 /* Find one of the default pipes for this interface */
695 for (i = 0; i < table->endpoints->count; i++) {
696 status = dcerpc_parse_binding(mem_ctx, table->endpoints->names[i], &default_binding);
698 if (NT_STATUS_IS_OK(status) && default_binding.transport == binding->transport && default_binding.endpoint) {
699 binding->endpoint = talloc_strdup(mem_ctx, default_binding.endpoint);
706 ZERO_STRUCT(epmapper_binding);
707 epmapper_binding.transport = binding->transport;
708 epmapper_binding.host = binding->host;
709 epmapper_binding.options = NULL;
710 epmapper_binding.flags = 0;
711 epmapper_binding.endpoint = NULL;
713 status = dcerpc_pipe_connect_b(&p,
715 DCERPC_EPMAPPER_UUID,
716 DCERPC_EPMAPPER_VERSION,
719 if (!NT_STATUS_IS_OK(status)) {
726 status = GUID_from_string(uuid, &binding->object);
727 if (NT_STATUS_IS_ERR(status)) {
731 binding->object_version = version;
733 status = dcerpc_binding_build_tower(p, binding, &twr.tower);
734 if (NT_STATUS_IS_ERR(status)) {
738 /* with some nice pretty paper around it of course */
740 r.in.map_tower = &twr;
741 r.in.entry_handle = &handle;
743 r.out.entry_handle = &handle;
745 status = dcerpc_epm_Map(p, p, &r);
746 if (!NT_STATUS_IS_OK(status)) {
747 dcerpc_pipe_close(p);
750 if (r.out.result != 0 || r.out.num_towers != 1) {
751 dcerpc_pipe_close(p);
752 return NT_STATUS_PORT_UNREACHABLE;
755 twr_r = r.out.towers[0].twr;
757 dcerpc_pipe_close(p);
758 return NT_STATUS_PORT_UNREACHABLE;
761 if (twr_r->tower.num_floors != twr.tower.num_floors ||
762 twr_r->tower.floors[3].lhs.protocol != twr.tower.floors[3].lhs.protocol) {
763 dcerpc_pipe_close(p);
764 return NT_STATUS_PORT_UNREACHABLE;
767 binding->endpoint = dcerpc_floor_get_rhs_data(mem_ctx, &twr_r->tower.floors[3]);
769 dcerpc_pipe_close(p);
775 /* open a rpc connection to a rpc pipe on SMB using the binding
776 structure to determine the endpoint and options */
777 static NTSTATUS dcerpc_pipe_connect_ncacn_np(struct dcerpc_pipe **pp,
778 struct dcerpc_binding *binding,
779 const char *pipe_uuid,
780 uint32_t pipe_version,
782 const char *username,
783 const char *password)
785 struct dcerpc_pipe *p;
788 struct smbcli_state *cli;
789 const char *pipe_name = NULL;
794 p = dcerpc_pipe_init(NULL);
796 return NT_STATUS_NO_MEMORY;
798 tmp_ctx = talloc_new(p);
800 /* Look up identifier using the epmapper */
801 if (!binding->endpoint) {
802 status = dcerpc_epm_map_binding(tmp_ctx, binding, pipe_uuid, pipe_version);
803 if (!NT_STATUS_IS_OK(status)) {
804 DEBUG(0,("Failed to map DCERPC/TCP NCACN_NP pipe for '%s' - %s\n",
805 pipe_uuid, nt_errstr(status)));
809 DEBUG(1,("Mapped to DCERPC/NP pipe %s\n", binding->endpoint));
812 pipe_name = binding->endpoint;
814 if (!strncasecmp(pipe_name, "/pipe/", 6) ||
815 !strncasecmp(pipe_name, "\\pipe\\", 6)) {
819 if (pipe_name[0] != '\\') {
820 pipe_name = talloc_asprintf(tmp_ctx, "\\%s", pipe_name);
823 if (!username || !username[0] ||
824 (binding->flags & DCERPC_SCHANNEL_ANY)) {
825 status = smbcli_full_connection(p->conn, &cli, lp_netbios_name(),
828 "", "", NULL, 0, &retry);
830 status = smbcli_full_connection(p->conn, &cli, lp_netbios_name(),
834 password, 0, &retry);
836 if (!NT_STATUS_IS_OK(status)) {
837 DEBUG(0,("Failed to connect to %s - %s\n", binding->host, nt_errstr(status)));
842 status = dcerpc_pipe_open_smb(p->conn, cli->tree, pipe_name);
843 if (!NT_STATUS_IS_OK(status)) {
844 DEBUG(0,("Failed to open pipe %s - %s\n", pipe_name, nt_errstr(status)));
849 p->conn->flags = binding->flags;
851 /* remember the binding string for possible secondary connections */
852 p->conn->binding_string = dcerpc_binding_string(p, binding);
854 if (username && username[0] && (binding->flags & DCERPC_SCHANNEL_ANY)) {
855 status = dcerpc_bind_auth_schannel(p, pipe_uuid, pipe_version,
856 domain, username, password);
857 } else if (username && username[0] &&
858 (binding->flags & (DCERPC_CONNECT|DCERPC_SIGN|DCERPC_SEAL))) {
859 status = dcerpc_bind_auth_ntlm(p, pipe_uuid, pipe_version, domain, username, password);
861 status = dcerpc_bind_auth_none(p, pipe_uuid, pipe_version);
865 if (!NT_STATUS_IS_OK(status)) {
866 DEBUG(0,("Failed to bind to uuid %s - %s\n", pipe_uuid, nt_errstr(status)));
872 talloc_free(tmp_ctx);
877 /* open a rpc connection to a rpc pipe on SMP using the binding
878 structure to determine the endpoint and options */
879 static NTSTATUS dcerpc_pipe_connect_ncalrpc(struct dcerpc_pipe **pp,
880 struct dcerpc_binding *binding,
881 const char *pipe_uuid,
882 uint32_t pipe_version,
884 const char *username,
885 const char *password)
888 struct dcerpc_pipe *p;
893 p = dcerpc_pipe_init(NULL);
895 return NT_STATUS_NO_MEMORY;
897 tmp_ctx = talloc_new(p);
899 /* Look up identifier using the epmapper */
900 if (!binding->endpoint) {
901 status = dcerpc_epm_map_binding(tmp_ctx, binding, pipe_uuid, pipe_version);
902 if (!NT_STATUS_IS_OK(status)) {
903 DEBUG(0,("Failed to map DCERPC/TCP NCALRPC identifier for '%s' - %s\n",
904 pipe_uuid, nt_errstr(status)));
908 DEBUG(1,("Mapped to DCERPC/LRPC identifier %s\n", binding->endpoint));
911 status = dcerpc_pipe_open_pipe(p->conn, binding->endpoint);
912 if (!NT_STATUS_IS_OK(status)) {
913 DEBUG(0,("Failed to open ncalrpc pipe '%s' - %s\n",
914 binding->endpoint, nt_errstr(status)));
919 p->conn->flags = binding->flags;
921 /* remember the binding string for possible secondary connections */
922 p->conn->binding_string = dcerpc_binding_string(p, binding);
924 if (username && username[0] && (binding->flags & DCERPC_SCHANNEL_ANY)) {
925 status = dcerpc_bind_auth_schannel(p, pipe_uuid, pipe_version,
926 domain, username, password);
927 } else if (username && username[0]) {
928 status = dcerpc_bind_auth_ntlm(p, pipe_uuid, pipe_version, domain, username, password);
930 status = dcerpc_bind_auth_none(p, pipe_uuid, pipe_version);
933 if (!NT_STATUS_IS_OK(status)) {
934 DEBUG(0,("Failed to bind to uuid %s - %s\n", pipe_uuid, nt_errstr(status)));
940 talloc_free(tmp_ctx);
947 /* open a rpc connection to a rpc pipe on SMP using the binding
948 structure to determine the endpoint and options */
949 static NTSTATUS dcerpc_pipe_connect_ncacn_unix_stream(struct dcerpc_pipe **pp,
950 struct dcerpc_binding *binding,
951 const char *pipe_uuid,
952 uint32_t pipe_version,
954 const char *username,
955 const char *password)
958 struct dcerpc_pipe *p;
962 if (!binding->endpoint) {
963 DEBUG(0, ("Path to unix socket not specified\n"));
964 return NT_STATUS_INVALID_PARAMETER;
967 p = dcerpc_pipe_init(NULL);
969 return NT_STATUS_NO_MEMORY;
972 status = dcerpc_pipe_open_unix_stream(p->conn, binding->endpoint);
973 if (!NT_STATUS_IS_OK(status)) {
974 DEBUG(0,("Failed to open unix socket %s - %s\n",
975 binding->endpoint, nt_errstr(status)));
980 p->conn->flags = binding->flags;
982 /* remember the binding string for possible secondary connections */
983 p->conn->binding_string = dcerpc_binding_string(p, binding);
985 if (username && username[0] && (binding->flags & DCERPC_SCHANNEL_ANY)) {
986 status = dcerpc_bind_auth_schannel(p, pipe_uuid, pipe_version,
987 domain, username, password);
988 } else if (username && username[0]) {
989 status = dcerpc_bind_auth_ntlm(p, pipe_uuid, pipe_version, domain, username, password);
991 status = dcerpc_bind_auth_none(p, pipe_uuid, pipe_version);
994 if (!NT_STATUS_IS_OK(status)) {
995 DEBUG(0,("Failed to bind to uuid %s - %s\n", pipe_uuid, nt_errstr(status)));
1005 /* open a rpc connection to a rpc pipe on SMP using the binding
1006 structure to determine the endpoint and options */
1007 static NTSTATUS dcerpc_pipe_connect_ncacn_ip_tcp(struct dcerpc_pipe **pp,
1008 struct dcerpc_binding *binding,
1009 const char *pipe_uuid,
1010 uint32_t pipe_version,
1012 const char *username,
1013 const char *password)
1017 struct dcerpc_pipe *p;
1018 TALLOC_CTX *tmp_ctx;
1022 p = dcerpc_pipe_init(NULL);
1024 return NT_STATUS_NO_MEMORY;
1026 tmp_ctx = talloc_new(p);
1028 if (!binding->endpoint) {
1029 status = dcerpc_epm_map_binding(tmp_ctx, binding,
1030 pipe_uuid, pipe_version);
1031 if (!NT_STATUS_IS_OK(status)) {
1032 DEBUG(0,("Failed to map DCERPC/TCP port for '%s' - %s\n",
1033 pipe_uuid, nt_errstr(status)));
1037 DEBUG(1,("Mapped to DCERPC/TCP port %s\n", binding->endpoint));
1040 port = atoi(binding->endpoint);
1042 status = dcerpc_pipe_open_tcp(p->conn, binding->host, port);
1043 if (!NT_STATUS_IS_OK(status)) {
1044 DEBUG(0,("Failed to connect to %s:%d - %s\n",
1045 binding->host, port, nt_errstr(status)));
1050 p->conn->flags = binding->flags;
1052 /* remember the binding string for possible secondary connections */
1053 p->conn->binding_string = dcerpc_binding_string(p, binding);
1055 if (username && username[0] && (binding->flags & DCERPC_SCHANNEL_ANY)) {
1056 status = dcerpc_bind_auth_schannel(p, pipe_uuid, pipe_version,
1057 domain, username, password);
1058 } else if (username && username[0]) {
1059 status = dcerpc_bind_auth_ntlm(p, pipe_uuid, pipe_version, domain, username, password);
1061 status = dcerpc_bind_auth_none(p, pipe_uuid, pipe_version);
1064 if (!NT_STATUS_IS_OK(status)) {
1065 DEBUG(0,("Failed to bind to uuid %s - %s\n",
1066 pipe_uuid, nt_errstr(status)));
1072 talloc_free(tmp_ctx);
1078 /* open a rpc connection to a rpc pipe, using the specified
1079 binding structure to determine the endpoint and options */
1080 NTSTATUS dcerpc_pipe_connect_b(struct dcerpc_pipe **pp,
1081 struct dcerpc_binding *binding,
1082 const char *pipe_uuid,
1083 uint32_t pipe_version,
1085 const char *username,
1086 const char *password)
1088 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
1090 switch (binding->transport) {
1092 status = dcerpc_pipe_connect_ncacn_np(pp, binding, pipe_uuid, pipe_version,
1093 domain, username, password);
1096 status = dcerpc_pipe_connect_ncacn_ip_tcp(pp, binding, pipe_uuid, pipe_version,
1097 domain, username, password);
1099 case NCACN_UNIX_STREAM:
1100 status = dcerpc_pipe_connect_ncacn_unix_stream(pp, binding, pipe_uuid, pipe_version,
1101 domain, username, password);
1104 status = dcerpc_pipe_connect_ncalrpc(pp, binding, pipe_uuid, pipe_version,
1105 domain, username, password);
1108 return NT_STATUS_NOT_SUPPORTED;
1115 /* open a rpc connection to a rpc pipe, using the specified string
1116 binding to determine the endpoint and options */
1117 NTSTATUS dcerpc_pipe_connect(struct dcerpc_pipe **pp,
1118 const char *binding,
1119 const char *pipe_uuid,
1120 uint32_t pipe_version,
1122 const char *username,
1123 const char *password)
1125 struct dcerpc_binding b;
1127 TALLOC_CTX *tmp_ctx;
1129 tmp_ctx = talloc_new(NULL);
1131 status = dcerpc_parse_binding(tmp_ctx, binding, &b);
1132 if (!NT_STATUS_IS_OK(status)) {
1133 DEBUG(0,("Failed to parse dcerpc binding '%s'\n", binding));
1134 talloc_free(tmp_ctx);
1138 DEBUG(3,("Using binding %s\n", dcerpc_binding_string(tmp_ctx, &b)));
1140 status = dcerpc_pipe_connect_b(pp, &b, pipe_uuid, pipe_version, domain, username, password);
1142 talloc_free(tmp_ctx);
1149 create a secondary dcerpc connection from a primary connection
1151 if the primary is a SMB connection then the secondary connection
1152 will be on the same SMB connection, but use a new fnum
1154 NTSTATUS dcerpc_secondary_connection(struct dcerpc_pipe *p, struct dcerpc_pipe **p2,
1155 const char *pipe_name,
1156 const char *pipe_uuid,
1157 uint32_t pipe_version)
1159 struct smbcli_tree *tree;
1160 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
1161 struct dcerpc_binding b;
1163 (*p2) = dcerpc_pipe_init(p);
1165 return NT_STATUS_NO_MEMORY;
1168 switch (p->conn->transport.transport) {
1170 tree = dcerpc_smb_tree(p->conn);
1172 return NT_STATUS_INVALID_PARAMETER;
1175 status = dcerpc_pipe_open_smb((*p2)->conn, tree, pipe_name);
1179 status = dcerpc_parse_binding(p, p->conn->binding_string, &b);
1180 if (!NT_STATUS_IS_OK(status)) {
1183 b.flags &= ~DCERPC_AUTH_OPTIONS;
1184 status = dcerpc_pipe_open_tcp((*p2)->conn, b.host, atoi(b.endpoint));
1188 status = dcerpc_parse_binding(p, p->conn->binding_string, &b);
1189 if (!NT_STATUS_IS_OK(status)) {
1192 b.flags &= ~DCERPC_AUTH_OPTIONS;
1193 status = dcerpc_pipe_open_pipe((*p2)->conn, b.endpoint);
1197 return NT_STATUS_NOT_SUPPORTED;
1200 if (!NT_STATUS_IS_OK(status)) {
1205 (*p2)->conn->flags = p->conn->flags;
1207 status = dcerpc_bind_auth_none(*p2, pipe_uuid, pipe_version);
1208 if (!NT_STATUS_IS_OK(status)) {
1213 return NT_STATUS_OK;
1216 NTSTATUS dcerpc_generic_session_key(struct dcerpc_connection *c,
1217 DATA_BLOB *session_key)
1219 /* this took quite a few CPU cycles to find ... */
1220 session_key->data = discard_const_p(unsigned char, "SystemLibraryDTC");
1221 session_key->length = 16;
1222 return NT_STATUS_OK;
1226 fetch the user session key - may be default (above) or the SMB session key
1228 NTSTATUS dcerpc_fetch_session_key(struct dcerpc_pipe *p,
1229 DATA_BLOB *session_key)
1231 return p->conn->security_state.session_key(p->conn, session_key);
1236 log a rpc packet in a format suitable for ndrdump. This is especially useful
1237 for sealed packets, where ethereal cannot easily see the contents
1239 this triggers on a debug level of >= 10
1241 void dcerpc_log_packet(const struct dcerpc_interface_table *ndr,
1242 uint32_t opnum, uint32_t flags, DATA_BLOB *pkt)
1244 const int num_examples = 20;
1247 if (DEBUGLEVEL < 10) return;
1249 for (i=0;i<num_examples;i++) {
1251 asprintf(&name, "%s/rpclog/%s-%u.%d.%s",
1252 lp_lockdir(), ndr->name, opnum, i,
1253 (flags&NDR_IN)?"in":"out");
1257 if (!file_exist(name, NULL)) {
1258 if (file_save(name, pkt->data, pkt->length)) {
1259 DEBUG(10,("Logged rpc packet to %s\n", name));