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 {"spnego", DCERPC_AUTH_SPNEGO},
180 {"krb5", DCERPC_AUTH_KRB5},
181 {"validate", DCERPC_DEBUG_VALIDATE_BOTH},
182 {"print", DCERPC_DEBUG_PRINT_BOTH},
183 {"padcheck", DCERPC_DEBUG_PAD_CHECK},
184 {"bigendian", DCERPC_PUSH_BIGENDIAN}
190 form a binding string from a binding structure
192 const char *dcerpc_binding_string(TALLOC_CTX *mem_ctx, const struct dcerpc_binding *b)
194 char *s = talloc_strdup(mem_ctx, "");
196 const char *t_name=NULL;
198 for (i=0;i<ARRAY_SIZE(transports);i++) {
199 if (transports[i].transport == b->transport) {
200 t_name = transports[i].name;
207 if (!GUID_all_zero(&b->object)) {
208 s = talloc_asprintf(s, "%s@",
209 GUID_string(mem_ctx, &b->object));
212 s = talloc_asprintf_append(s, "%s:", t_name);
216 s = talloc_asprintf_append(s, "%s", b->host);
219 if (!b->endpoint && !b->options && !b->flags) {
223 s = talloc_asprintf_append(s, "[");
226 s = talloc_asprintf_append(s, "%s", b->endpoint);
229 /* this is a *really* inefficent way of dealing with strings,
230 but this is rarely called and the strings are always short,
232 for (i=0;b->options && b->options[i];i++) {
233 s = talloc_asprintf_append(s, ",%s", b->options[i]);
237 for (i=0;i<ARRAY_SIZE(ncacn_options);i++) {
238 if (b->flags & ncacn_options[i].flag) {
239 s = talloc_asprintf_append(s, ",%s", ncacn_options[i].name);
244 s = talloc_asprintf_append(s, "]");
250 parse a binding string into a dcerpc_binding structure
252 NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *s, struct dcerpc_binding *b)
254 char *options, *type;
256 int i, j, comma_count;
260 if (p && PTR_DIFF(p, s) == 36) { /* 36 is the length of a UUID */
263 status = GUID_from_string(s, &b->object);
265 if (NT_STATUS_IS_ERR(status)) {
266 DEBUG(0, ("Failed parsing UUID\n"));
272 ZERO_STRUCT(b->object);
275 b->object_version = 0;
279 return NT_STATUS_INVALID_PARAMETER;
282 type = talloc_strndup(mem_ctx, s, PTR_DIFF(p, s));
284 return NT_STATUS_NO_MEMORY;
287 for (i=0;i<ARRAY_SIZE(transports);i++) {
288 if (strcasecmp(type, transports[i].name) == 0) {
289 b->transport = transports[i].transport;
293 if (i==ARRAY_SIZE(transports)) {
294 DEBUG(0,("Unknown dcerpc transport '%s'\n", type));
295 return NT_STATUS_INVALID_PARAMETER;
302 b->host = talloc_strndup(mem_ctx, s, PTR_DIFF(p, s));
303 options = talloc_strdup(mem_ctx, p+1);
304 if (options[strlen(options)-1] != ']') {
305 return NT_STATUS_INVALID_PARAMETER;
307 options[strlen(options)-1] = 0;
309 b->host = talloc_strdup(mem_ctx, s);
314 return NT_STATUS_NO_MEMORY;
325 comma_count = count_chars(options, ',');
327 b->options = talloc_array(mem_ctx, const char *, comma_count+2);
329 return NT_STATUS_NO_MEMORY;
332 for (i=0; (p = strchr(options, ',')); i++) {
333 b->options[i] = talloc_strndup(mem_ctx, options, PTR_DIFF(p, options));
334 if (!b->options[i]) {
335 return NT_STATUS_NO_MEMORY;
339 b->options[i] = options;
340 b->options[i+1] = NULL;
342 /* some options are pre-parsed for convenience */
343 for (i=0;b->options[i];i++) {
344 for (j=0;j<ARRAY_SIZE(ncacn_options);j++) {
345 if (strcasecmp(ncacn_options[j].name, b->options[i]) == 0) {
347 b->flags |= ncacn_options[j].flag;
348 for (k=i;b->options[k];k++) {
349 b->options[k] = b->options[k+1];
358 /* Endpoint is first option */
359 b->endpoint = b->options[0];
360 if (strlen(b->endpoint) == 0) b->endpoint = NULL;
362 for (i=0;b->options[i];i++) {
363 b->options[i] = b->options[i+1];
367 if (b->options[0] == NULL)
373 const char *dcerpc_floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *floor)
375 switch (floor->lhs.protocol) {
376 case EPM_PROTOCOL_TCP:
377 if (floor->rhs.tcp.port == 0) return NULL;
378 return talloc_asprintf(mem_ctx, "%d", floor->rhs.tcp.port);
380 case EPM_PROTOCOL_UDP:
381 if (floor->rhs.udp.port == 0) return NULL;
382 return talloc_asprintf(mem_ctx, "%d", floor->rhs.udp.port);
384 case EPM_PROTOCOL_HTTP:
385 if (floor->rhs.http.port == 0) return NULL;
386 return talloc_asprintf(mem_ctx, "%d", floor->rhs.http.port);
388 case EPM_PROTOCOL_IP:
389 return talloc_strdup(mem_ctx, floor->rhs.ip.ipaddr);
391 case EPM_PROTOCOL_NCACN:
394 case EPM_PROTOCOL_NCADG:
397 case EPM_PROTOCOL_SMB:
398 if (strlen(floor->rhs.smb.unc) == 0) return NULL;
399 return talloc_strdup(mem_ctx, floor->rhs.smb.unc);
401 case EPM_PROTOCOL_PIPE:
402 if (strlen(floor->rhs.pipe.path) == 0) return NULL;
403 return talloc_strdup(mem_ctx, floor->rhs.pipe.path);
405 case EPM_PROTOCOL_NETBIOS:
406 if (strlen(floor->rhs.netbios.name) == 0) return NULL;
407 return talloc_strdup(mem_ctx, floor->rhs.netbios.name);
409 case EPM_PROTOCOL_NCALRPC:
412 case EPM_PROTOCOL_VINES_SPP:
413 return talloc_asprintf(mem_ctx, "%d", floor->rhs.vines_spp.port);
415 case EPM_PROTOCOL_VINES_IPC:
416 return talloc_asprintf(mem_ctx, "%d", floor->rhs.vines_ipc.port);
418 case EPM_PROTOCOL_STREETTALK:
419 return talloc_strdup(mem_ctx, floor->rhs.streettalk.streettalk);
421 case EPM_PROTOCOL_UNIX_DS:
422 if (strlen(floor->rhs.unix_ds.path) == 0) return NULL;
423 return talloc_strdup(mem_ctx, floor->rhs.unix_ds.path);
425 case EPM_PROTOCOL_NULL:
429 DEBUG(0,("Unsupported lhs protocol %d\n", floor->lhs.protocol));
436 static NTSTATUS dcerpc_floor_set_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *floor, const char *data)
438 switch (floor->lhs.protocol) {
439 case EPM_PROTOCOL_TCP:
440 floor->rhs.tcp.port = atoi(data);
443 case EPM_PROTOCOL_UDP:
444 floor->rhs.udp.port = atoi(data);
447 case EPM_PROTOCOL_HTTP:
448 floor->rhs.http.port = atoi(data);
451 case EPM_PROTOCOL_IP:
452 floor->rhs.ip.ipaddr = talloc_strdup(mem_ctx, data);
453 NT_STATUS_HAVE_NO_MEMORY(floor->rhs.ip.ipaddr);
456 case EPM_PROTOCOL_NCACN:
457 floor->rhs.ncacn.minor_version = 0;
460 case EPM_PROTOCOL_NCADG:
461 floor->rhs.ncadg.minor_version = 0;
464 case EPM_PROTOCOL_SMB:
465 floor->rhs.smb.unc = talloc_strdup(mem_ctx, data);
466 NT_STATUS_HAVE_NO_MEMORY(floor->rhs.smb.unc);
469 case EPM_PROTOCOL_PIPE:
470 floor->rhs.pipe.path = talloc_strdup(mem_ctx, data);
471 NT_STATUS_HAVE_NO_MEMORY(floor->rhs.pipe.path);
474 case EPM_PROTOCOL_NETBIOS:
475 floor->rhs.netbios.name = talloc_strdup(mem_ctx, data);
476 NT_STATUS_HAVE_NO_MEMORY(floor->rhs.netbios.name);
479 case EPM_PROTOCOL_NCALRPC:
482 case EPM_PROTOCOL_VINES_SPP:
483 floor->rhs.vines_spp.port = atoi(data);
486 case EPM_PROTOCOL_VINES_IPC:
487 floor->rhs.vines_ipc.port = atoi(data);
490 case EPM_PROTOCOL_STREETTALK:
491 floor->rhs.streettalk.streettalk = talloc_strdup(mem_ctx, data);
492 NT_STATUS_HAVE_NO_MEMORY(floor->rhs.streettalk.streettalk);
495 case EPM_PROTOCOL_UNIX_DS:
496 floor->rhs.unix_ds.path = talloc_strdup(mem_ctx, data);
497 NT_STATUS_HAVE_NO_MEMORY(floor->rhs.unix_ds.path);
500 case EPM_PROTOCOL_NULL:
504 DEBUG(0,("Unsupported lhs protocol %d\n", floor->lhs.protocol));
508 return NT_STATUS_NOT_SUPPORTED;
511 enum dcerpc_transport_t dcerpc_transport_by_endpoint_protocol(int prot)
515 /* Find a transport that has 'prot' as 4th protocol */
516 for (i=0;i<ARRAY_SIZE(transports);i++) {
517 if (transports[i].num_protocols >= 2 &&
518 transports[i].protseq[1] == prot) {
519 return transports[i].transport;
523 /* Unknown transport */
527 enum dcerpc_transport_t dcerpc_transport_by_tower(struct epm_tower *tower)
531 /* Find a transport that matches this tower */
532 for (i=0;i<ARRAY_SIZE(transports);i++) {
534 if (transports[i].num_protocols != tower->num_floors - 2) {
538 for (j = 0; j < transports[i].num_protocols; j++) {
539 if (transports[i].protseq[j] != tower->floors[j+2].lhs.protocol) {
544 if (j == transports[i].num_protocols) {
545 return transports[i].transport;
549 /* Unknown transport */
553 NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx, struct epm_tower *tower, struct dcerpc_binding *binding)
555 ZERO_STRUCT(binding->object);
556 binding->options = NULL;
557 binding->host = NULL;
560 binding->transport = dcerpc_transport_by_tower(tower);
562 if (binding->transport == -1) {
563 return NT_STATUS_NOT_SUPPORTED;
566 if (tower->num_floors < 1) {
570 /* Set object uuid */
571 binding->object = tower->floors[0].lhs.info.uuid.uuid;
572 binding->object_version = tower->floors[0].lhs.info.uuid.version;
574 /* Ignore floor 1, it contains the NDR version info */
576 binding->options = NULL;
579 if (tower->num_floors >= 4) {
580 binding->endpoint = dcerpc_floor_get_rhs_data(mem_ctx, &tower->floors[3]);
582 binding->endpoint = NULL;
585 /* Set network address */
586 if (tower->num_floors >= 5) {
587 binding->host = dcerpc_floor_get_rhs_data(mem_ctx, &tower->floors[4]);
592 NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx, struct dcerpc_binding *binding, struct epm_tower *tower)
594 const enum epm_protocol *protseq = NULL;
595 int num_protocols = -1, i;
599 for (i=0;i<ARRAY_SIZE(transports);i++) {
600 if (transports[i].transport == binding->transport) {
601 protseq = transports[i].protseq;
602 num_protocols = transports[i].num_protocols;
607 if (num_protocols == -1) {
608 DEBUG(0, ("Unable to find transport with id '%d'\n", binding->transport));
609 return NT_STATUS_UNSUCCESSFUL;
612 tower->num_floors = 2 + num_protocols;
613 tower->floors = talloc_array(mem_ctx, struct epm_floor, tower->num_floors);
616 tower->floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
617 tower->floors[0].lhs.info.uuid.uuid = binding->object;
618 tower->floors[0].lhs.info.uuid.version = binding->object_version;
619 tower->floors[0].rhs.uuid.unknown = 0;
622 tower->floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
623 tower->floors[1].lhs.info.uuid.version = NDR_GUID_VERSION;
624 tower->floors[1].rhs.uuid.unknown = 0;
625 status = GUID_from_string(NDR_GUID, &tower->floors[1].lhs.info.uuid.uuid);
626 if (NT_STATUS_IS_ERR(status)) {
630 /* Floor 2 to num_protocols */
631 for (i = 0; i < num_protocols; i++) {
632 tower->floors[2 + i].lhs.protocol = protseq[i];
633 tower->floors[2 + i].lhs.info.lhs_data = data_blob_talloc(mem_ctx, NULL, 0);
634 ZERO_STRUCT(tower->floors[2 + i].rhs);
635 dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[2 + i], "");
638 /* The 4th floor contains the endpoint */
639 if (num_protocols >= 2 && binding->endpoint) {
640 status = dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[3], binding->endpoint);
641 if (NT_STATUS_IS_ERR(status)) {
646 /* The 5th contains the network address */
647 if (num_protocols >= 3 && binding->host) {
648 status = dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[4], binding->host);
649 if (NT_STATUS_IS_ERR(status)) {
657 NTSTATUS dcerpc_epm_map_binding(TALLOC_CTX *mem_ctx, struct dcerpc_binding *binding,
658 const char *uuid, uint_t version)
660 struct dcerpc_pipe *p;
663 struct policy_handle handle;
665 struct epm_twr_t twr, *twr_r;
666 struct dcerpc_binding epmapper_binding;
667 const struct dcerpc_interface_table *table = idl_iface_by_uuid(uuid);
670 /* First, check if there is a default endpoint specified in the IDL */
673 struct dcerpc_binding default_binding;
675 /* Find one of the default pipes for this interface */
676 for (i = 0; i < table->endpoints->count; i++) {
677 status = dcerpc_parse_binding(mem_ctx, table->endpoints->names[i], &default_binding);
679 if (NT_STATUS_IS_OK(status) && default_binding.transport == binding->transport && default_binding.endpoint) {
680 binding->endpoint = talloc_strdup(mem_ctx, default_binding.endpoint);
687 ZERO_STRUCT(epmapper_binding);
688 epmapper_binding.transport = binding->transport;
689 epmapper_binding.host = binding->host;
690 epmapper_binding.options = NULL;
691 epmapper_binding.flags = 0;
692 epmapper_binding.endpoint = NULL;
694 status = dcerpc_pipe_connect_b(&p,
696 DCERPC_EPMAPPER_UUID,
697 DCERPC_EPMAPPER_VERSION,
700 if (!NT_STATUS_IS_OK(status)) {
707 status = GUID_from_string(uuid, &binding->object);
708 if (NT_STATUS_IS_ERR(status)) {
712 binding->object_version = version;
714 status = dcerpc_binding_build_tower(p, binding, &twr.tower);
715 if (NT_STATUS_IS_ERR(status)) {
719 /* with some nice pretty paper around it of course */
721 r.in.map_tower = &twr;
722 r.in.entry_handle = &handle;
724 r.out.entry_handle = &handle;
726 status = dcerpc_epm_Map(p, p, &r);
727 if (!NT_STATUS_IS_OK(status)) {
728 dcerpc_pipe_close(p);
731 if (r.out.result != 0 || r.out.num_towers != 1) {
732 dcerpc_pipe_close(p);
733 return NT_STATUS_PORT_UNREACHABLE;
736 twr_r = r.out.towers[0].twr;
738 dcerpc_pipe_close(p);
739 return NT_STATUS_PORT_UNREACHABLE;
742 if (twr_r->tower.num_floors != twr.tower.num_floors ||
743 twr_r->tower.floors[3].lhs.protocol != twr.tower.floors[3].lhs.protocol) {
744 dcerpc_pipe_close(p);
745 return NT_STATUS_PORT_UNREACHABLE;
748 binding->endpoint = dcerpc_floor_get_rhs_data(mem_ctx, &twr_r->tower.floors[3]);
750 dcerpc_pipe_close(p);
757 perform an authenticated bind if needed
759 static NTSTATUS dcerpc_pipe_auth(struct dcerpc_pipe *p,
760 struct dcerpc_binding *binding,
761 const char *pipe_uuid,
762 uint32_t pipe_version,
764 const char *username,
765 const char *password)
768 p->conn->flags = binding->flags;
770 /* remember the binding string for possible secondary connections */
771 p->conn->binding_string = dcerpc_binding_string(p, binding);
773 if (username && username[0] && (binding->flags & DCERPC_SCHANNEL_ANY)) {
774 status = dcerpc_bind_auth_schannel(p, pipe_uuid, pipe_version,
775 domain, username, password);
776 } else if (username && username[0]) {
778 if (binding->flags & DCERPC_AUTH_SPNEGO) {
779 auth_type = DCERPC_AUTH_TYPE_SPNEGO;
780 } else if (binding->flags & DCERPC_AUTH_KRB5) {
781 auth_type = DCERPC_AUTH_TYPE_KRB5;
783 auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
786 status = dcerpc_bind_auth_password(p, pipe_uuid, pipe_version,
787 domain, username, password,
790 status = dcerpc_bind_auth_none(p, pipe_uuid, pipe_version);
793 if (!NT_STATUS_IS_OK(status)) {
794 DEBUG(0,("Failed to bind to uuid %s - %s\n", pipe_uuid, nt_errstr(status)));
800 /* open a rpc connection to a rpc pipe on SMB using the binding
801 structure to determine the endpoint and options */
802 static NTSTATUS dcerpc_pipe_connect_ncacn_np(struct dcerpc_pipe **pp,
803 struct dcerpc_binding *binding,
804 const char *pipe_uuid,
805 uint32_t pipe_version,
807 const char *username,
808 const char *password)
810 struct dcerpc_pipe *p;
812 struct smbcli_state *cli;
813 const char *pipe_name = NULL;
818 p = dcerpc_pipe_init(NULL);
820 return NT_STATUS_NO_MEMORY;
822 tmp_ctx = talloc_new(p);
824 /* Look up identifier using the epmapper */
825 if (!binding->endpoint) {
826 status = dcerpc_epm_map_binding(tmp_ctx, binding, pipe_uuid, pipe_version);
827 if (!NT_STATUS_IS_OK(status)) {
828 DEBUG(0,("Failed to map DCERPC/TCP NCACN_NP pipe for '%s' - %s\n",
829 pipe_uuid, nt_errstr(status)));
833 DEBUG(1,("Mapped to DCERPC/NP pipe %s\n", binding->endpoint));
836 pipe_name = binding->endpoint;
838 if (!strncasecmp(pipe_name, "/pipe/", 6) ||
839 !strncasecmp(pipe_name, "\\pipe\\", 6)) {
843 if (pipe_name[0] != '\\') {
844 pipe_name = talloc_asprintf(tmp_ctx, "\\%s", pipe_name);
847 if (!username || !username[0] ||
848 (binding->flags & DCERPC_SCHANNEL_ANY)) {
849 status = smbcli_full_connection(p->conn, &cli, lp_netbios_name(),
854 status = smbcli_full_connection(p->conn, &cli, lp_netbios_name(),
860 if (!NT_STATUS_IS_OK(status)) {
861 DEBUG(0,("Failed to connect to %s - %s\n", binding->host, nt_errstr(status)));
866 status = dcerpc_pipe_open_smb(p->conn, cli->tree, pipe_name);
867 if (!NT_STATUS_IS_OK(status)) {
868 DEBUG(0,("Failed to open pipe %s - %s\n", pipe_name, nt_errstr(status)));
873 if (!(binding->flags & DCERPC_AUTH_OPTIONS)) {
877 status = dcerpc_pipe_auth(p, binding, pipe_uuid, pipe_version, domain, username, password);
878 if (!NT_STATUS_IS_OK(status)) {
884 talloc_free(tmp_ctx);
889 /* open a rpc connection to a rpc pipe on SMP using the binding
890 structure to determine the endpoint and options */
891 static NTSTATUS dcerpc_pipe_connect_ncalrpc(struct dcerpc_pipe **pp,
892 struct dcerpc_binding *binding,
893 const char *pipe_uuid,
894 uint32_t pipe_version,
896 const char *username,
897 const char *password)
900 struct dcerpc_pipe *p;
905 p = dcerpc_pipe_init(NULL);
907 return NT_STATUS_NO_MEMORY;
909 tmp_ctx = talloc_new(p);
911 /* Look up identifier using the epmapper */
912 if (!binding->endpoint) {
913 status = dcerpc_epm_map_binding(tmp_ctx, binding, pipe_uuid, pipe_version);
914 if (!NT_STATUS_IS_OK(status)) {
915 DEBUG(0,("Failed to map DCERPC/TCP NCALRPC identifier for '%s' - %s\n",
916 pipe_uuid, nt_errstr(status)));
920 DEBUG(1,("Mapped to DCERPC/LRPC identifier %s\n", binding->endpoint));
923 status = dcerpc_pipe_open_pipe(p->conn, binding->endpoint);
924 if (!NT_STATUS_IS_OK(status)) {
925 DEBUG(0,("Failed to open ncalrpc pipe '%s' - %s\n",
926 binding->endpoint, nt_errstr(status)));
931 status = dcerpc_pipe_auth(p, binding, pipe_uuid, pipe_version, domain, username, password);
932 if (!NT_STATUS_IS_OK(status)) {
938 talloc_free(tmp_ctx);
945 /* open a rpc connection to a rpc pipe on SMP using the binding
946 structure to determine the endpoint and options */
947 static NTSTATUS dcerpc_pipe_connect_ncacn_unix_stream(struct dcerpc_pipe **pp,
948 struct dcerpc_binding *binding,
949 const char *pipe_uuid,
950 uint32_t pipe_version,
952 const char *username,
953 const char *password)
956 struct dcerpc_pipe *p;
960 if (!binding->endpoint) {
961 DEBUG(0, ("Path to unix socket not specified\n"));
962 return NT_STATUS_INVALID_PARAMETER;
965 p = dcerpc_pipe_init(NULL);
967 return NT_STATUS_NO_MEMORY;
970 status = dcerpc_pipe_open_unix_stream(p->conn, binding->endpoint);
971 if (!NT_STATUS_IS_OK(status)) {
972 DEBUG(0,("Failed to open unix socket %s - %s\n",
973 binding->endpoint, nt_errstr(status)));
978 status = dcerpc_pipe_auth(p, binding, pipe_uuid, pipe_version, domain, username, password);
979 if (!NT_STATUS_IS_OK(status)) {
989 /* open a rpc connection to a rpc pipe on SMP using the binding
990 structure to determine the endpoint and options */
991 static NTSTATUS dcerpc_pipe_connect_ncacn_ip_tcp(struct dcerpc_pipe **pp,
992 struct dcerpc_binding *binding,
993 const char *pipe_uuid,
994 uint32_t pipe_version,
996 const char *username,
997 const char *password)
1001 struct dcerpc_pipe *p;
1002 TALLOC_CTX *tmp_ctx;
1006 p = dcerpc_pipe_init(NULL);
1008 return NT_STATUS_NO_MEMORY;
1010 tmp_ctx = talloc_new(p);
1012 if (!binding->endpoint) {
1013 status = dcerpc_epm_map_binding(tmp_ctx, binding,
1014 pipe_uuid, pipe_version);
1015 if (!NT_STATUS_IS_OK(status)) {
1016 DEBUG(0,("Failed to map DCERPC/TCP port for '%s' - %s\n",
1017 pipe_uuid, nt_errstr(status)));
1021 DEBUG(1,("Mapped to DCERPC/TCP port %s\n", binding->endpoint));
1024 port = atoi(binding->endpoint);
1026 status = dcerpc_pipe_open_tcp(p->conn, binding->host, port);
1027 if (!NT_STATUS_IS_OK(status)) {
1028 DEBUG(0,("Failed to connect to %s:%d - %s\n",
1029 binding->host, port, nt_errstr(status)));
1034 status = dcerpc_pipe_auth(p, binding, pipe_uuid, pipe_version, domain, username, password);
1035 if (!NT_STATUS_IS_OK(status)) {
1041 talloc_free(tmp_ctx);
1047 /* open a rpc connection to a rpc pipe, using the specified
1048 binding structure to determine the endpoint and options */
1049 NTSTATUS dcerpc_pipe_connect_b(struct dcerpc_pipe **pp,
1050 struct dcerpc_binding *binding,
1051 const char *pipe_uuid,
1052 uint32_t pipe_version,
1054 const char *username,
1055 const char *password)
1057 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
1059 switch (binding->transport) {
1061 status = dcerpc_pipe_connect_ncacn_np(pp, binding, pipe_uuid, pipe_version,
1062 domain, username, password);
1065 status = dcerpc_pipe_connect_ncacn_ip_tcp(pp, binding, pipe_uuid, pipe_version,
1066 domain, username, password);
1068 case NCACN_UNIX_STREAM:
1069 status = dcerpc_pipe_connect_ncacn_unix_stream(pp, binding, pipe_uuid, pipe_version,
1070 domain, username, password);
1073 status = dcerpc_pipe_connect_ncalrpc(pp, binding, pipe_uuid, pipe_version,
1074 domain, username, password);
1077 return NT_STATUS_NOT_SUPPORTED;
1084 /* open a rpc connection to a rpc pipe, using the specified string
1085 binding to determine the endpoint and options */
1086 NTSTATUS dcerpc_pipe_connect(struct dcerpc_pipe **pp,
1087 const char *binding,
1088 const char *pipe_uuid,
1089 uint32_t pipe_version,
1091 const char *username,
1092 const char *password)
1094 struct dcerpc_binding b;
1096 TALLOC_CTX *tmp_ctx;
1098 tmp_ctx = talloc_new(NULL);
1100 status = dcerpc_parse_binding(tmp_ctx, binding, &b);
1101 if (!NT_STATUS_IS_OK(status)) {
1102 DEBUG(0,("Failed to parse dcerpc binding '%s'\n", binding));
1103 talloc_free(tmp_ctx);
1107 DEBUG(3,("Using binding %s\n", dcerpc_binding_string(tmp_ctx, &b)));
1109 status = dcerpc_pipe_connect_b(pp, &b, pipe_uuid, pipe_version, domain, username, password);
1111 talloc_free(tmp_ctx);
1118 create a secondary dcerpc connection from a primary connection
1120 if the primary is a SMB connection then the secondary connection
1121 will be on the same SMB connection, but use a new fnum
1123 NTSTATUS dcerpc_secondary_connection(struct dcerpc_pipe *p, struct dcerpc_pipe **p2,
1124 const char *pipe_name,
1125 const char *pipe_uuid,
1126 uint32_t pipe_version)
1128 struct smbcli_tree *tree;
1129 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
1130 struct dcerpc_binding b;
1132 (*p2) = dcerpc_pipe_init(p);
1134 return NT_STATUS_NO_MEMORY;
1137 switch (p->conn->transport.transport) {
1139 tree = dcerpc_smb_tree(p->conn);
1141 return NT_STATUS_INVALID_PARAMETER;
1143 status = dcerpc_pipe_open_smb((*p2)->conn, tree, pipe_name);
1147 status = dcerpc_parse_binding(p, p->conn->binding_string, &b);
1148 if (!NT_STATUS_IS_OK(status)) {
1151 status = dcerpc_pipe_open_tcp((*p2)->conn, b.host, atoi(b.endpoint));
1155 status = dcerpc_parse_binding(p, p->conn->binding_string, &b);
1156 if (!NT_STATUS_IS_OK(status)) {
1159 status = dcerpc_pipe_open_pipe((*p2)->conn, b.endpoint);
1163 return NT_STATUS_NOT_SUPPORTED;
1166 if (!NT_STATUS_IS_OK(status)) {
1171 (*p2)->conn->flags = p->conn->flags;
1173 status = dcerpc_bind_auth_none(*p2, pipe_uuid, pipe_version);
1174 if (!NT_STATUS_IS_OK(status)) {
1179 return NT_STATUS_OK;
1182 NTSTATUS dcerpc_generic_session_key(struct dcerpc_connection *c,
1183 DATA_BLOB *session_key)
1185 /* this took quite a few CPU cycles to find ... */
1186 session_key->data = discard_const_p(unsigned char, "SystemLibraryDTC");
1187 session_key->length = 16;
1188 return NT_STATUS_OK;
1192 fetch the user session key - may be default (above) or the SMB session key
1194 NTSTATUS dcerpc_fetch_session_key(struct dcerpc_pipe *p,
1195 DATA_BLOB *session_key)
1197 return p->conn->security_state.session_key(p->conn, session_key);
1202 log a rpc packet in a format suitable for ndrdump. This is especially useful
1203 for sealed packets, where ethereal cannot easily see the contents
1205 this triggers on a debug level of >= 10
1207 void dcerpc_log_packet(const struct dcerpc_interface_table *ndr,
1208 uint32_t opnum, uint32_t flags, DATA_BLOB *pkt)
1210 const int num_examples = 20;
1213 if (DEBUGLEVEL < 10) return;
1215 for (i=0;i<num_examples;i++) {
1217 asprintf(&name, "%s/rpclog/%s-%u.%d.%s",
1218 lp_lockdir(), ndr->name, opnum, i,
1219 (flags&NDR_IN)?"in":"out");
1223 if (!file_exist(name, NULL)) {
1224 if (file_save(name, pkt->data, pkt->length)) {
1225 DEBUG(10,("Logged rpc packet to %s\n", name));
1237 create a secondary context from a primary connection
1239 this uses dcerpc_alter_context() to create a new dcerpc context_id
1241 NTSTATUS dcerpc_secondary_context(struct dcerpc_pipe *p,
1242 struct dcerpc_pipe **pp2,
1243 const char *pipe_uuid,
1244 uint32_t pipe_version)
1247 struct dcerpc_pipe *p2;
1249 p2 = talloc_zero(p, struct dcerpc_pipe);
1251 return NT_STATUS_NO_MEMORY;
1253 p2->conn = talloc_reference(p2, p->conn);
1255 p2->context_id = ++p->conn->next_context_id;
1257 status = GUID_from_string(pipe_uuid, &p2->syntax.uuid);
1258 if (!NT_STATUS_IS_OK(status)) {
1262 p2->syntax.if_version = pipe_version;
1264 status = GUID_from_string(NDR_GUID, &p2->transfer_syntax.uuid);
1265 if (!NT_STATUS_IS_OK(status)) {
1269 p2->transfer_syntax.if_version = NDR_GUID_VERSION;
1271 status = dcerpc_alter_context(p2, p2, &p2->syntax, &p2->transfer_syntax);
1272 if (!NT_STATUS_IS_OK(status)) {