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_p(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 if (floor->rhs.ip.address == 0) {
395 in.addr = htonl(floor->rhs.ip.address);
396 return talloc_strdup(mem_ctx, sys_inet_ntoa(in));
399 case EPM_PROTOCOL_NCACN:
402 case EPM_PROTOCOL_NCADG:
405 case EPM_PROTOCOL_SMB:
406 if (strlen(floor->rhs.smb.unc) == 0) return NULL;
407 return talloc_strdup(mem_ctx, floor->rhs.smb.unc);
409 case EPM_PROTOCOL_PIPE:
410 if (strlen(floor->rhs.pipe.path) == 0) return NULL;
411 return talloc_strdup(mem_ctx, floor->rhs.pipe.path);
413 case EPM_PROTOCOL_NETBIOS:
414 if (strlen(floor->rhs.netbios.name) == 0) return NULL;
415 return talloc_strdup(mem_ctx, floor->rhs.netbios.name);
417 case EPM_PROTOCOL_NCALRPC:
420 case EPM_PROTOCOL_VINES_SPP:
421 return talloc_asprintf(mem_ctx, "%d", floor->rhs.vines_spp.port);
423 case EPM_PROTOCOL_VINES_IPC:
424 return talloc_asprintf(mem_ctx, "%d", floor->rhs.vines_ipc.port);
426 case EPM_PROTOCOL_STREETTALK:
427 return talloc_strdup(mem_ctx, floor->rhs.streettalk.streettalk);
429 case EPM_PROTOCOL_UNIX_DS:
430 if (strlen(floor->rhs.unix_ds.path) == 0) return NULL;
431 return talloc_strdup(mem_ctx, floor->rhs.unix_ds.path);
433 case EPM_PROTOCOL_NULL:
437 DEBUG(0,("Unsupported lhs protocol %d\n", floor->lhs.protocol));
444 static NTSTATUS dcerpc_floor_set_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *floor, const char *data)
446 switch (floor->lhs.protocol) {
447 case EPM_PROTOCOL_TCP:
448 floor->rhs.tcp.port = atoi(data);
451 case EPM_PROTOCOL_UDP:
452 floor->rhs.udp.port = atoi(data);
455 case EPM_PROTOCOL_HTTP:
456 floor->rhs.http.port = atoi(data);
459 case EPM_PROTOCOL_IP:
460 if (strlen(data) > 0) {
461 floor->rhs.ip.address = ntohl(interpret_addr(data));
463 floor->rhs.ip.address = 0;
467 case EPM_PROTOCOL_NCACN:
468 floor->rhs.ncacn.minor_version = 0;
471 case EPM_PROTOCOL_NCADG:
472 floor->rhs.ncadg.minor_version = 0;
475 case EPM_PROTOCOL_SMB:
476 floor->rhs.smb.unc = talloc_strdup(mem_ctx, data);
477 if (!floor->rhs.smb.unc) {
478 return NT_STATUS_NO_MEMORY;
482 case EPM_PROTOCOL_PIPE:
483 floor->rhs.pipe.path = talloc_strdup(mem_ctx, data);
484 if (!floor->rhs.pipe.path) {
485 return NT_STATUS_NO_MEMORY;
489 case EPM_PROTOCOL_NETBIOS:
490 floor->rhs.netbios.name = talloc_strdup(mem_ctx, data);
491 if (!floor->rhs.netbios.name) {
492 return NT_STATUS_NO_MEMORY;
496 case EPM_PROTOCOL_NCALRPC:
499 case EPM_PROTOCOL_VINES_SPP:
500 floor->rhs.vines_spp.port = atoi(data);
503 case EPM_PROTOCOL_VINES_IPC:
504 floor->rhs.vines_ipc.port = atoi(data);
507 case EPM_PROTOCOL_STREETTALK:
508 floor->rhs.streettalk.streettalk = talloc_strdup(mem_ctx, data);
509 if (!floor->rhs.streettalk.streettalk) {
510 return NT_STATUS_NO_MEMORY;
514 case EPM_PROTOCOL_UNIX_DS:
515 floor->rhs.unix_ds.path = talloc_strdup(mem_ctx, data);
516 if (!floor->rhs.unix_ds.path) {
517 return NT_STATUS_NO_MEMORY;
521 case EPM_PROTOCOL_NULL:
525 DEBUG(0,("Unsupported lhs protocol %d\n", floor->lhs.protocol));
529 return NT_STATUS_NOT_SUPPORTED;
532 enum dcerpc_transport_t dcerpc_transport_by_endpoint_protocol(int prot)
536 /* Find a transport that has 'prot' as 4th protocol */
537 for (i=0;i<ARRAY_SIZE(transports);i++) {
538 if (transports[i].num_protocols >= 2 &&
539 transports[i].protseq[1] == prot) {
540 return transports[i].transport;
544 /* Unknown transport */
548 enum dcerpc_transport_t dcerpc_transport_by_tower(struct epm_tower *tower)
552 /* Find a transport that matches this tower */
553 for (i=0;i<ARRAY_SIZE(transports);i++) {
555 if (transports[i].num_protocols != tower->num_floors - 2) {
559 for (j = 0; j < transports[i].num_protocols; j++) {
560 if (transports[i].protseq[j] != tower->floors[j+2].lhs.protocol) {
565 if (j == transports[i].num_protocols) {
566 return transports[i].transport;
570 /* Unknown transport */
574 NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx, struct epm_tower *tower, struct dcerpc_binding *binding)
576 ZERO_STRUCT(binding->object);
577 binding->options = NULL;
578 binding->host = NULL;
581 binding->transport = dcerpc_transport_by_tower(tower);
583 if (binding->transport == -1) {
584 return NT_STATUS_NOT_SUPPORTED;
587 if (tower->num_floors < 1) {
591 /* Set object uuid */
592 binding->object = tower->floors[0].lhs.info.uuid.uuid;
593 binding->object_version = tower->floors[0].lhs.info.uuid.version;
595 /* Ignore floor 1, it contains the NDR version info */
597 binding->options = NULL;
600 if (tower->num_floors >= 4) {
601 binding->endpoint = dcerpc_floor_get_rhs_data(mem_ctx, &tower->floors[3]);
603 binding->endpoint = NULL;
606 /* Set network address */
607 if (tower->num_floors >= 5) {
608 binding->host = dcerpc_floor_get_rhs_data(mem_ctx, &tower->floors[4]);
613 NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx, struct dcerpc_binding *binding, struct epm_tower *tower)
615 const enum epm_protocol *protseq = NULL;
616 int num_protocols = -1, i;
620 for (i=0;i<ARRAY_SIZE(transports);i++) {
621 if (transports[i].transport == binding->transport) {
622 protseq = transports[i].protseq;
623 num_protocols = transports[i].num_protocols;
628 if (num_protocols == -1) {
629 DEBUG(0, ("Unable to find transport with id '%d'\n", binding->transport));
630 return NT_STATUS_UNSUCCESSFUL;
633 tower->num_floors = 2 + num_protocols;
634 tower->floors = talloc_array_p(mem_ctx, struct epm_floor, tower->num_floors);
637 tower->floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
638 tower->floors[0].lhs.info.uuid.uuid = binding->object;
639 tower->floors[0].lhs.info.uuid.version = binding->object_version;
640 tower->floors[0].rhs.uuid.unknown = 0;
643 tower->floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
644 tower->floors[1].lhs.info.uuid.version = NDR_GUID_VERSION;
645 tower->floors[1].rhs.uuid.unknown = 0;
646 status = GUID_from_string(NDR_GUID, &tower->floors[1].lhs.info.uuid.uuid);
647 if (NT_STATUS_IS_ERR(status)) {
651 /* Floor 2 to num_protocols */
652 for (i = 0; i < num_protocols; i++) {
653 tower->floors[2 + i].lhs.protocol = protseq[i];
654 tower->floors[2 + i].lhs.info.lhs_data = data_blob_talloc(mem_ctx, NULL, 0);
655 ZERO_STRUCT(tower->floors[2 + i].rhs);
656 dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[2 + i], "");
659 /* The 4th floor contains the endpoint */
660 if (num_protocols >= 2 && binding->endpoint) {
661 status = dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[3], binding->endpoint);
662 if (NT_STATUS_IS_ERR(status)) {
667 /* The 5th contains the network address */
668 if (num_protocols >= 3 && binding->host) {
669 status = dcerpc_floor_set_rhs_data(mem_ctx, &tower->floors[4], binding->host);
670 if (NT_STATUS_IS_ERR(status)) {
678 NTSTATUS dcerpc_epm_map_binding(TALLOC_CTX *mem_ctx, struct dcerpc_binding *binding,
679 const char *uuid, uint_t version)
681 struct dcerpc_pipe *p;
684 struct policy_handle handle;
686 struct epm_twr_t twr, *twr_r;
687 struct dcerpc_binding epmapper_binding;
688 const struct dcerpc_interface_table *table = idl_iface_by_uuid(uuid);
691 /* First, check if there is a default endpoint specified in the IDL */
694 struct dcerpc_binding default_binding;
696 /* Find one of the default pipes for this interface */
697 for (i = 0; i < table->endpoints->count; i++) {
698 status = dcerpc_parse_binding(mem_ctx, table->endpoints->names[i], &default_binding);
700 if (NT_STATUS_IS_OK(status) && default_binding.transport == binding->transport && default_binding.endpoint) {
701 binding->endpoint = talloc_strdup(mem_ctx, default_binding.endpoint);
708 ZERO_STRUCT(epmapper_binding);
709 epmapper_binding.transport = binding->transport;
710 epmapper_binding.host = binding->host;
711 epmapper_binding.options = NULL;
712 epmapper_binding.flags = 0;
713 epmapper_binding.endpoint = NULL;
715 status = dcerpc_pipe_connect_b(&p,
717 DCERPC_EPMAPPER_UUID,
718 DCERPC_EPMAPPER_VERSION,
721 if (!NT_STATUS_IS_OK(status)) {
728 status = GUID_from_string(uuid, &binding->object);
729 if (NT_STATUS_IS_ERR(status)) {
733 binding->object_version = version;
735 status = dcerpc_binding_build_tower(p, binding, &twr.tower);
736 if (NT_STATUS_IS_ERR(status)) {
740 /* with some nice pretty paper around it of course */
742 r.in.map_tower = &twr;
743 r.in.entry_handle = &handle;
745 r.out.entry_handle = &handle;
747 status = dcerpc_epm_Map(p, p, &r);
748 if (!NT_STATUS_IS_OK(status)) {
749 dcerpc_pipe_close(p);
752 if (r.out.result != 0 || r.out.num_towers != 1) {
753 dcerpc_pipe_close(p);
754 return NT_STATUS_PORT_UNREACHABLE;
757 twr_r = r.out.towers[0].twr;
759 dcerpc_pipe_close(p);
760 return NT_STATUS_PORT_UNREACHABLE;
763 if (twr_r->tower.num_floors != twr.tower.num_floors ||
764 twr_r->tower.floors[3].lhs.protocol != twr.tower.floors[3].lhs.protocol) {
765 dcerpc_pipe_close(p);
766 return NT_STATUS_PORT_UNREACHABLE;
769 binding->endpoint = dcerpc_floor_get_rhs_data(mem_ctx, &twr_r->tower.floors[3]);
771 dcerpc_pipe_close(p);
778 perform an authenticated bind if needed
780 static NTSTATUS dcerpc_pipe_auth(struct dcerpc_pipe *p,
781 struct dcerpc_binding *binding,
782 const char *pipe_uuid,
783 uint32_t pipe_version,
785 const char *username,
786 const char *password)
789 p->conn->flags = binding->flags;
791 /* remember the binding string for possible secondary connections */
792 p->conn->binding_string = dcerpc_binding_string(p, binding);
794 if (username && username[0] && (binding->flags & DCERPC_SCHANNEL_ANY)) {
795 status = dcerpc_bind_auth_schannel(p, pipe_uuid, pipe_version,
796 domain, username, password);
797 } else if (username && username[0]) {
799 if (binding->flags & DCERPC_AUTH_SPNEGO) {
800 auth_type = DCERPC_AUTH_TYPE_SPNEGO;
801 } else if (binding->flags & DCERPC_AUTH_KRB5) {
802 auth_type = DCERPC_AUTH_TYPE_KRB5;
804 auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
807 status = dcerpc_bind_auth_password(p, pipe_uuid, pipe_version,
808 domain, username, password,
811 status = dcerpc_bind_auth_none(p, pipe_uuid, pipe_version);
814 if (!NT_STATUS_IS_OK(status)) {
815 DEBUG(0,("Failed to bind to uuid %s - %s\n", pipe_uuid, nt_errstr(status)));
821 /* open a rpc connection to a rpc pipe on SMB using the binding
822 structure to determine the endpoint and options */
823 static NTSTATUS dcerpc_pipe_connect_ncacn_np(struct dcerpc_pipe **pp,
824 struct dcerpc_binding *binding,
825 const char *pipe_uuid,
826 uint32_t pipe_version,
828 const char *username,
829 const char *password)
831 struct dcerpc_pipe *p;
833 struct smbcli_state *cli;
834 const char *pipe_name = NULL;
839 p = dcerpc_pipe_init(NULL);
841 return NT_STATUS_NO_MEMORY;
843 tmp_ctx = talloc_new(p);
845 /* Look up identifier using the epmapper */
846 if (!binding->endpoint) {
847 status = dcerpc_epm_map_binding(tmp_ctx, binding, pipe_uuid, pipe_version);
848 if (!NT_STATUS_IS_OK(status)) {
849 DEBUG(0,("Failed to map DCERPC/TCP NCACN_NP pipe for '%s' - %s\n",
850 pipe_uuid, nt_errstr(status)));
854 DEBUG(1,("Mapped to DCERPC/NP pipe %s\n", binding->endpoint));
857 pipe_name = binding->endpoint;
859 if (!strncasecmp(pipe_name, "/pipe/", 6) ||
860 !strncasecmp(pipe_name, "\\pipe\\", 6)) {
864 if (pipe_name[0] != '\\') {
865 pipe_name = talloc_asprintf(tmp_ctx, "\\%s", pipe_name);
868 if (!username || !username[0] ||
869 (binding->flags & DCERPC_SCHANNEL_ANY)) {
870 status = smbcli_full_connection(p->conn, &cli, lp_netbios_name(),
875 status = smbcli_full_connection(p->conn, &cli, lp_netbios_name(),
881 if (!NT_STATUS_IS_OK(status)) {
882 DEBUG(0,("Failed to connect to %s - %s\n", binding->host, nt_errstr(status)));
887 status = dcerpc_pipe_open_smb(p->conn, cli->tree, pipe_name);
888 if (!NT_STATUS_IS_OK(status)) {
889 DEBUG(0,("Failed to open pipe %s - %s\n", pipe_name, nt_errstr(status)));
894 if (!(binding->flags & DCERPC_AUTH_OPTIONS)) {
898 status = dcerpc_pipe_auth(p, binding, pipe_uuid, pipe_version, domain, username, password);
899 if (!NT_STATUS_IS_OK(status)) {
905 talloc_free(tmp_ctx);
910 /* open a rpc connection to a rpc pipe on SMP using the binding
911 structure to determine the endpoint and options */
912 static NTSTATUS dcerpc_pipe_connect_ncalrpc(struct dcerpc_pipe **pp,
913 struct dcerpc_binding *binding,
914 const char *pipe_uuid,
915 uint32_t pipe_version,
917 const char *username,
918 const char *password)
921 struct dcerpc_pipe *p;
926 p = dcerpc_pipe_init(NULL);
928 return NT_STATUS_NO_MEMORY;
930 tmp_ctx = talloc_new(p);
932 /* Look up identifier using the epmapper */
933 if (!binding->endpoint) {
934 status = dcerpc_epm_map_binding(tmp_ctx, binding, pipe_uuid, pipe_version);
935 if (!NT_STATUS_IS_OK(status)) {
936 DEBUG(0,("Failed to map DCERPC/TCP NCALRPC identifier for '%s' - %s\n",
937 pipe_uuid, nt_errstr(status)));
941 DEBUG(1,("Mapped to DCERPC/LRPC identifier %s\n", binding->endpoint));
944 status = dcerpc_pipe_open_pipe(p->conn, binding->endpoint);
945 if (!NT_STATUS_IS_OK(status)) {
946 DEBUG(0,("Failed to open ncalrpc pipe '%s' - %s\n",
947 binding->endpoint, nt_errstr(status)));
952 status = dcerpc_pipe_auth(p, binding, pipe_uuid, pipe_version, domain, username, password);
953 if (!NT_STATUS_IS_OK(status)) {
959 talloc_free(tmp_ctx);
966 /* open a rpc connection to a rpc pipe on SMP using the binding
967 structure to determine the endpoint and options */
968 static NTSTATUS dcerpc_pipe_connect_ncacn_unix_stream(struct dcerpc_pipe **pp,
969 struct dcerpc_binding *binding,
970 const char *pipe_uuid,
971 uint32_t pipe_version,
973 const char *username,
974 const char *password)
977 struct dcerpc_pipe *p;
981 if (!binding->endpoint) {
982 DEBUG(0, ("Path to unix socket not specified\n"));
983 return NT_STATUS_INVALID_PARAMETER;
986 p = dcerpc_pipe_init(NULL);
988 return NT_STATUS_NO_MEMORY;
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)));
999 status = dcerpc_pipe_auth(p, binding, pipe_uuid, pipe_version, domain, username, password);
1000 if (!NT_STATUS_IS_OK(status)) {
1010 /* open a rpc connection to a rpc pipe on SMP using the binding
1011 structure to determine the endpoint and options */
1012 static NTSTATUS dcerpc_pipe_connect_ncacn_ip_tcp(struct dcerpc_pipe **pp,
1013 struct dcerpc_binding *binding,
1014 const char *pipe_uuid,
1015 uint32_t pipe_version,
1017 const char *username,
1018 const char *password)
1022 struct dcerpc_pipe *p;
1023 TALLOC_CTX *tmp_ctx;
1027 p = dcerpc_pipe_init(NULL);
1029 return NT_STATUS_NO_MEMORY;
1031 tmp_ctx = talloc_new(p);
1033 if (!binding->endpoint) {
1034 status = dcerpc_epm_map_binding(tmp_ctx, binding,
1035 pipe_uuid, pipe_version);
1036 if (!NT_STATUS_IS_OK(status)) {
1037 DEBUG(0,("Failed to map DCERPC/TCP port for '%s' - %s\n",
1038 pipe_uuid, nt_errstr(status)));
1042 DEBUG(1,("Mapped to DCERPC/TCP port %s\n", binding->endpoint));
1045 port = atoi(binding->endpoint);
1047 status = dcerpc_pipe_open_tcp(p->conn, binding->host, port);
1048 if (!NT_STATUS_IS_OK(status)) {
1049 DEBUG(0,("Failed to connect to %s:%d - %s\n",
1050 binding->host, port, nt_errstr(status)));
1055 status = dcerpc_pipe_auth(p, binding, pipe_uuid, pipe_version, domain, username, password);
1056 if (!NT_STATUS_IS_OK(status)) {
1062 talloc_free(tmp_ctx);
1068 /* open a rpc connection to a rpc pipe, using the specified
1069 binding structure to determine the endpoint and options */
1070 NTSTATUS dcerpc_pipe_connect_b(struct dcerpc_pipe **pp,
1071 struct dcerpc_binding *binding,
1072 const char *pipe_uuid,
1073 uint32_t pipe_version,
1075 const char *username,
1076 const char *password)
1078 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
1080 switch (binding->transport) {
1082 status = dcerpc_pipe_connect_ncacn_np(pp, binding, pipe_uuid, pipe_version,
1083 domain, username, password);
1086 status = dcerpc_pipe_connect_ncacn_ip_tcp(pp, binding, pipe_uuid, pipe_version,
1087 domain, username, password);
1089 case NCACN_UNIX_STREAM:
1090 status = dcerpc_pipe_connect_ncacn_unix_stream(pp, binding, pipe_uuid, pipe_version,
1091 domain, username, password);
1094 status = dcerpc_pipe_connect_ncalrpc(pp, binding, pipe_uuid, pipe_version,
1095 domain, username, password);
1098 return NT_STATUS_NOT_SUPPORTED;
1105 /* open a rpc connection to a rpc pipe, using the specified string
1106 binding to determine the endpoint and options */
1107 NTSTATUS dcerpc_pipe_connect(struct dcerpc_pipe **pp,
1108 const char *binding,
1109 const char *pipe_uuid,
1110 uint32_t pipe_version,
1112 const char *username,
1113 const char *password)
1115 struct dcerpc_binding b;
1117 TALLOC_CTX *tmp_ctx;
1119 tmp_ctx = talloc_new(NULL);
1121 status = dcerpc_parse_binding(tmp_ctx, binding, &b);
1122 if (!NT_STATUS_IS_OK(status)) {
1123 DEBUG(0,("Failed to parse dcerpc binding '%s'\n", binding));
1124 talloc_free(tmp_ctx);
1128 DEBUG(3,("Using binding %s\n", dcerpc_binding_string(tmp_ctx, &b)));
1130 status = dcerpc_pipe_connect_b(pp, &b, pipe_uuid, pipe_version, domain, username, password);
1132 talloc_free(tmp_ctx);
1139 create a secondary dcerpc connection from a primary connection
1141 if the primary is a SMB connection then the secondary connection
1142 will be on the same SMB connection, but use a new fnum
1144 NTSTATUS dcerpc_secondary_connection(struct dcerpc_pipe *p, struct dcerpc_pipe **p2,
1145 const char *pipe_name,
1146 const char *pipe_uuid,
1147 uint32_t pipe_version)
1149 struct smbcli_tree *tree;
1150 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
1151 struct dcerpc_binding b;
1153 (*p2) = dcerpc_pipe_init(p);
1155 return NT_STATUS_NO_MEMORY;
1158 switch (p->conn->transport.transport) {
1160 tree = dcerpc_smb_tree(p->conn);
1162 return NT_STATUS_INVALID_PARAMETER;
1164 status = dcerpc_pipe_open_smb((*p2)->conn, tree, pipe_name);
1168 status = dcerpc_parse_binding(p, p->conn->binding_string, &b);
1169 if (!NT_STATUS_IS_OK(status)) {
1172 status = dcerpc_pipe_open_tcp((*p2)->conn, b.host, atoi(b.endpoint));
1176 status = dcerpc_parse_binding(p, p->conn->binding_string, &b);
1177 if (!NT_STATUS_IS_OK(status)) {
1180 status = dcerpc_pipe_open_pipe((*p2)->conn, b.endpoint);
1184 return NT_STATUS_NOT_SUPPORTED;
1187 if (!NT_STATUS_IS_OK(status)) {
1192 (*p2)->conn->flags = p->conn->flags;
1194 status = dcerpc_bind_auth_none(*p2, pipe_uuid, pipe_version);
1195 if (!NT_STATUS_IS_OK(status)) {
1200 return NT_STATUS_OK;
1203 NTSTATUS dcerpc_generic_session_key(struct dcerpc_connection *c,
1204 DATA_BLOB *session_key)
1206 /* this took quite a few CPU cycles to find ... */
1207 session_key->data = discard_const_p(unsigned char, "SystemLibraryDTC");
1208 session_key->length = 16;
1209 return NT_STATUS_OK;
1213 fetch the user session key - may be default (above) or the SMB session key
1215 NTSTATUS dcerpc_fetch_session_key(struct dcerpc_pipe *p,
1216 DATA_BLOB *session_key)
1218 return p->conn->security_state.session_key(p->conn, session_key);
1223 log a rpc packet in a format suitable for ndrdump. This is especially useful
1224 for sealed packets, where ethereal cannot easily see the contents
1226 this triggers on a debug level of >= 10
1228 void dcerpc_log_packet(const struct dcerpc_interface_table *ndr,
1229 uint32_t opnum, uint32_t flags, DATA_BLOB *pkt)
1231 const int num_examples = 20;
1234 if (DEBUGLEVEL < 10) return;
1236 for (i=0;i<num_examples;i++) {
1238 asprintf(&name, "%s/rpclog/%s-%u.%d.%s",
1239 lp_lockdir(), ndr->name, opnum, i,
1240 (flags&NDR_IN)?"in":"out");
1244 if (!file_exist(name, NULL)) {
1245 if (file_save(name, pkt->data, pkt->length)) {
1246 DEBUG(10,("Logged rpc packet to %s\n", name));
1258 create a secondary context from a primary connection
1260 this uses dcerpc_alter_context() to create a new dcerpc context_id
1262 NTSTATUS dcerpc_secondary_context(struct dcerpc_pipe *p,
1263 struct dcerpc_pipe **pp2,
1264 const char *pipe_uuid,
1265 uint32_t pipe_version)
1268 struct dcerpc_pipe *p2;
1270 p2 = talloc_zero(p, struct dcerpc_pipe);
1272 return NT_STATUS_NO_MEMORY;
1274 p2->conn = talloc_reference(p2, p->conn);
1276 p2->context_id = ++p->conn->next_context_id;
1278 status = GUID_from_string(pipe_uuid, &p2->syntax.uuid);
1279 if (!NT_STATUS_IS_OK(status)) {
1283 p2->syntax.if_version = pipe_version;
1285 status = GUID_from_string(NDR_GUID, &p2->transfer_syntax.uuid);
1286 if (!NT_STATUS_IS_OK(status)) {
1290 p2->transfer_syntax.if_version = NDR_GUID_VERSION;
1292 status = dcerpc_alter_context(p2, p2, &p2->syntax, &p2->transfer_syntax);
1293 if (!NT_STATUS_IS_OK(status)) {