2 Unix SMB/CIFS implementation.
4 dcerpc utility functions
6 Copyright (C) Andrew Tridgell 2003
7 Copyright (C) Jelmer Vernooij 2004
8 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "system/network.h"
27 #include "librpc/gen_ndr/ndr_epmapper.h"
30 find the pipe name for a local IDL interface
32 const char *idl_pipe_name(const char *uuid
, uint32_t if_version
)
34 const struct dcerpc_interface_list
*l
;
35 for (l
=librpc_dcerpc_pipes();l
;l
=l
->next
) {
36 if (strcasecmp(l
->table
->uuid
, uuid
) == 0 &&
37 l
->table
->if_version
== if_version
) {
38 return l
->table
->name
;
45 find the number of calls defined by local IDL
47 int idl_num_calls(const char *uuid
, uint32_t if_version
)
49 const struct dcerpc_interface_list
*l
;
50 for (l
=librpc_dcerpc_pipes();l
;l
=l
->next
){
51 if (strcasecmp(l
->table
->uuid
, uuid
) == 0 &&
52 l
->table
->if_version
== if_version
) {
53 return l
->table
->num_calls
;
61 find a dcerpc interface by name
63 const struct dcerpc_interface_table
*idl_iface_by_name(const char *name
)
65 const struct dcerpc_interface_list
*l
;
66 for (l
=librpc_dcerpc_pipes();l
;l
=l
->next
) {
67 if (strcasecmp(l
->table
->name
, name
) == 0) {
75 find a dcerpc interface by uuid
77 const struct dcerpc_interface_table
*idl_iface_by_uuid(const char *uuid
)
79 const struct dcerpc_interface_list
*l
;
80 for (l
=librpc_dcerpc_pipes();l
;l
=l
->next
) {
81 if (strcasecmp(l
->table
->uuid
, uuid
) == 0) {
90 push a dcerpc_packet into a blob, potentially with auth info
92 NTSTATUS
dcerpc_push_auth(DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
,
93 struct dcerpc_packet
*pkt
,
94 struct dcerpc_auth
*auth_info
)
99 ndr
= ndr_push_init_ctx(mem_ctx
);
101 return NT_STATUS_NO_MEMORY
;
104 if (!(pkt
->drep
[0] & DCERPC_DREP_LE
)) {
105 ndr
->flags
|= LIBNDR_FLAG_BIGENDIAN
;
108 if (pkt
->pfc_flags
& DCERPC_PFC_FLAG_ORPC
) {
109 ndr
->flags
|= LIBNDR_FLAG_OBJECT_PRESENT
;
113 pkt
->auth_length
= auth_info
->credentials
.length
;
115 pkt
->auth_length
= 0;
118 status
= ndr_push_dcerpc_packet(ndr
, NDR_SCALARS
|NDR_BUFFERS
, pkt
);
119 if (!NT_STATUS_IS_OK(status
)) {
124 status
= ndr_push_dcerpc_auth(ndr
, NDR_SCALARS
|NDR_BUFFERS
, auth_info
);
127 *blob
= ndr_push_blob(ndr
);
129 /* fill in the frag length */
130 dcerpc_set_frag_length(blob
, blob
->length
);
135 #define MAX_PROTSEQ 10
137 static const struct {
139 enum dcerpc_transport_t transport
;
141 enum epm_protocol protseq
[MAX_PROTSEQ
];
143 { "ncacn_np", NCACN_NP
, 3,
144 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_SMB
, EPM_PROTOCOL_NETBIOS
}},
145 { "ncacn_ip_tcp", NCACN_IP_TCP
, 3,
146 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_TCP
, EPM_PROTOCOL_IP
} },
147 { "ncacn_http", NCACN_HTTP
, 3,
148 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_HTTP
, EPM_PROTOCOL_IP
} },
149 { "ncadg_ip_udp", NCACN_IP_UDP
, 3,
150 { EPM_PROTOCOL_NCADG
, EPM_PROTOCOL_UDP
, EPM_PROTOCOL_IP
} },
151 { "ncalrpc", NCALRPC
, 2,
152 { EPM_PROTOCOL_NCALRPC
, EPM_PROTOCOL_PIPE
} },
153 { "ncacn_unix_stream", NCACN_UNIX_STREAM
, 2,
154 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_UNIX_DS
} },
155 { "ncadg_unix_dgram", NCADG_UNIX_DGRAM
, 2,
156 { EPM_PROTOCOL_NCADG
, EPM_PROTOCOL_UNIX_DS
} },
157 { "ncacn_at_dsp", NCACN_AT_DSP
, 3,
158 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_APPLETALK
, EPM_PROTOCOL_DSP
} },
159 { "ncadg_at_ddp", NCADG_AT_DDP
, 3,
160 { EPM_PROTOCOL_NCADG
, EPM_PROTOCOL_APPLETALK
, EPM_PROTOCOL_DDP
} },
161 { "ncacn_vns_ssp", NCACN_VNS_SPP
, 3,
162 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_STREETTALK
, EPM_PROTOCOL_VINES_SPP
} },
163 { "ncacn_vns_ipc", NCACN_VNS_IPC
, 3,
164 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_STREETTALK
, EPM_PROTOCOL_VINES_IPC
}, },
165 { "ncadg_ipx", NCADG_IPX
, 2,
166 { EPM_PROTOCOL_NCADG
, EPM_PROTOCOL_IPX
},
168 { "ncacn_spx", NCACN_SPX
, 3,
169 /* I guess some MS programmer confused the identifier for
170 * EPM_PROTOCOL_UUID (0x0D or 13) with the one for
171 * EPM_PROTOCOL_SPX (0x13) here. -- jelmer*/
172 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_NCALRPC
, EPM_PROTOCOL_UUID
},
176 static const struct {
179 } ncacn_options
[] = {
180 {"sign", DCERPC_SIGN
},
181 {"seal", DCERPC_SEAL
},
182 {"connect", DCERPC_CONNECT
},
183 {"spnego", DCERPC_AUTH_SPNEGO
},
184 {"krb5", DCERPC_AUTH_KRB5
},
185 {"validate", DCERPC_DEBUG_VALIDATE_BOTH
},
186 {"print", DCERPC_DEBUG_PRINT_BOTH
},
187 {"padcheck", DCERPC_DEBUG_PAD_CHECK
},
188 {"bigendian", DCERPC_PUSH_BIGENDIAN
}
191 const char *epm_floor_string(TALLOC_CTX
*mem_ctx
, struct epm_floor
*fl
)
197 switch(fl
->lhs
.protocol
) {
198 case EPM_PROTOCOL_UUID
:
199 status
= dcerpc_floor_get_lhs_data(fl
, &uuid
, &if_version
);
200 if (NT_STATUS_IS_OK(status
)) {
201 /* lhs is used: UUID */
204 uuidstr
= GUID_string(mem_ctx
, &uuid
);
206 if (strcasecmp(uuidstr
, NDR_GUID
) == 0) {
210 return talloc_asprintf(mem_ctx
, " uuid %s/0x%02x", uuidstr
, if_version
);
212 return talloc_asprintf(mem_ctx
, "IPX:%s",
213 data_blob_hex_string(mem_ctx
, &fl
->rhs
.uuid
.unknown
));
216 case EPM_PROTOCOL_NCACN
:
219 case EPM_PROTOCOL_NCADG
:
222 case EPM_PROTOCOL_NCALRPC
:
225 case EPM_PROTOCOL_DNET_NSP
:
228 case EPM_PROTOCOL_IP
:
229 return talloc_asprintf(mem_ctx
, "IP:%s", fl
->rhs
.ip
.ipaddr
);
231 case EPM_PROTOCOL_PIPE
:
232 return talloc_asprintf(mem_ctx
, "PIPE:%s", fl
->rhs
.pipe
.path
);
234 case EPM_PROTOCOL_SMB
:
235 return talloc_asprintf(mem_ctx
, "SMB:%s", fl
->rhs
.smb
.unc
);
237 case EPM_PROTOCOL_UNIX_DS
:
238 return talloc_asprintf(mem_ctx
, "Unix:%s", fl
->rhs
.unix_ds
.path
);
240 case EPM_PROTOCOL_NETBIOS
:
241 return talloc_asprintf(mem_ctx
, "NetBIOS:%s", fl
->rhs
.netbios
.name
);
243 case EPM_PROTOCOL_NETBEUI
:
246 case EPM_PROTOCOL_SPX
:
249 case EPM_PROTOCOL_NB_IPX
:
252 case EPM_PROTOCOL_HTTP
:
253 return talloc_asprintf(mem_ctx
, "HTTP:%d", fl
->rhs
.http
.port
);
255 case EPM_PROTOCOL_TCP
:
256 return talloc_asprintf(mem_ctx
, "TCP:%d", fl
->rhs
.tcp
.port
);
258 case EPM_PROTOCOL_UDP
:
259 return talloc_asprintf(mem_ctx
, "UDP:%d", fl
->rhs
.udp
.port
);
262 return talloc_asprintf(mem_ctx
, "UNK(%02x):", fl
->lhs
.protocol
);
268 form a binding string from a binding structure
270 const char *dcerpc_binding_string(TALLOC_CTX
*mem_ctx
, const struct dcerpc_binding
*b
)
272 char *s
= talloc_strdup(mem_ctx
, "");
274 const char *t_name
=NULL
;
276 for (i
=0;i
<ARRAY_SIZE(transports
);i
++) {
277 if (transports
[i
].transport
== b
->transport
) {
278 t_name
= transports
[i
].name
;
285 if (!GUID_all_zero(&b
->object
)) {
286 s
= talloc_asprintf(s
, "%s@",
287 GUID_string(mem_ctx
, &b
->object
));
290 s
= talloc_asprintf_append(s
, "%s:", t_name
);
294 s
= talloc_asprintf_append(s
, "%s", b
->host
);
297 if (!b
->endpoint
&& !b
->options
&& !b
->flags
) {
301 s
= talloc_asprintf_append(s
, "[");
304 s
= talloc_asprintf_append(s
, "%s", b
->endpoint
);
307 /* this is a *really* inefficent way of dealing with strings,
308 but this is rarely called and the strings are always short,
310 for (i
=0;b
->options
&& b
->options
[i
];i
++) {
311 s
= talloc_asprintf_append(s
, ",%s", b
->options
[i
]);
315 for (i
=0;i
<ARRAY_SIZE(ncacn_options
);i
++) {
316 if (b
->flags
& ncacn_options
[i
].flag
) {
317 s
= talloc_asprintf_append(s
, ",%s", ncacn_options
[i
].name
);
322 s
= talloc_asprintf_append(s
, "]");
328 parse a binding string into a dcerpc_binding structure
330 NTSTATUS
dcerpc_parse_binding(TALLOC_CTX
*mem_ctx
, const char *s
, struct dcerpc_binding
**b_out
)
332 struct dcerpc_binding
*b
;
333 char *options
, *type
;
335 int i
, j
, comma_count
;
337 b
= talloc(mem_ctx
, struct dcerpc_binding
);
339 return NT_STATUS_NO_MEMORY
;
342 b
->authservice
= NULL
;
346 if (p
&& PTR_DIFF(p
, s
) == 36) { /* 36 is the length of a UUID */
349 status
= GUID_from_string(s
, &b
->object
);
351 if (NT_STATUS_IS_ERR(status
)) {
352 DEBUG(0, ("Failed parsing UUID\n"));
358 ZERO_STRUCT(b
->object
);
361 b
->object_version
= 0;
365 return NT_STATUS_INVALID_PARAMETER
;
368 type
= talloc_strndup(mem_ctx
, s
, PTR_DIFF(p
, s
));
370 return NT_STATUS_NO_MEMORY
;
373 for (i
=0;i
<ARRAY_SIZE(transports
);i
++) {
374 if (strcasecmp(type
, transports
[i
].name
) == 0) {
375 b
->transport
= transports
[i
].transport
;
379 if (i
==ARRAY_SIZE(transports
)) {
380 DEBUG(0,("Unknown dcerpc transport '%s'\n", type
));
381 return NT_STATUS_INVALID_PARAMETER
;
388 b
->host
= talloc_strndup(b
, s
, PTR_DIFF(p
, s
));
389 options
= talloc_strdup(mem_ctx
, p
+1);
390 if (options
[strlen(options
)-1] != ']') {
391 return NT_STATUS_INVALID_PARAMETER
;
393 options
[strlen(options
)-1] = 0;
395 b
->host
= talloc_strdup(b
, s
);
400 return NT_STATUS_NO_MEMORY
;
412 comma_count
= count_chars(options
, ',');
414 b
->options
= talloc_array(b
, const char *, comma_count
+2);
416 return NT_STATUS_NO_MEMORY
;
419 for (i
=0; (p
= strchr(options
, ',')); i
++) {
420 b
->options
[i
] = talloc_strndup(b
, options
, PTR_DIFF(p
, options
));
421 if (!b
->options
[i
]) {
422 return NT_STATUS_NO_MEMORY
;
426 b
->options
[i
] = options
;
427 b
->options
[i
+1] = NULL
;
429 /* some options are pre-parsed for convenience */
430 for (i
=0;b
->options
[i
];i
++) {
431 for (j
=0;j
<ARRAY_SIZE(ncacn_options
);j
++) {
432 if (strcasecmp(ncacn_options
[j
].name
, b
->options
[i
]) == 0) {
434 b
->flags
|= ncacn_options
[j
].flag
;
435 for (k
=i
;b
->options
[k
];k
++) {
436 b
->options
[k
] = b
->options
[k
+1];
445 /* Endpoint is first option */
446 b
->endpoint
= b
->options
[0];
447 if (strlen(b
->endpoint
) == 0) b
->endpoint
= NULL
;
449 for (i
=0;b
->options
[i
];i
++) {
450 b
->options
[i
] = b
->options
[i
+1];
454 if (b
->options
[0] == NULL
)
461 NTSTATUS
dcerpc_floor_get_lhs_data(struct epm_floor
*floor
, struct GUID
*uuid
, uint16_t *if_version
)
463 TALLOC_CTX
*mem_ctx
= talloc_init("floor_get_lhs_data");
464 struct ndr_pull
*ndr
= ndr_pull_init_blob(&floor
->lhs
.lhs_data
, mem_ctx
);
467 ndr
->flags
|= LIBNDR_FLAG_NOALIGN
;
469 status
= ndr_pull_GUID(ndr
, NDR_SCALARS
| NDR_BUFFERS
, uuid
);
470 if (NT_STATUS_IS_ERR(status
)) {
471 talloc_free(mem_ctx
);
475 status
= ndr_pull_uint16(ndr
, NDR_SCALARS
, if_version
);
477 talloc_free(mem_ctx
);
482 DATA_BLOB
dcerpc_floor_pack_lhs_data(TALLOC_CTX
*mem_ctx
, struct GUID
*uuid
, uint32_t if_version
)
484 struct ndr_push
*ndr
= ndr_push_init_ctx(mem_ctx
);
486 ndr
->flags
|= LIBNDR_FLAG_NOALIGN
;
488 ndr_push_GUID(ndr
, NDR_SCALARS
| NDR_BUFFERS
, uuid
);
489 ndr_push_uint16(ndr
, NDR_SCALARS
, if_version
);
491 return ndr_push_blob(ndr
);
494 const char *dcerpc_floor_get_rhs_data(TALLOC_CTX
*mem_ctx
, struct epm_floor
*floor
)
496 switch (floor
->lhs
.protocol
) {
497 case EPM_PROTOCOL_TCP
:
498 if (floor
->rhs
.tcp
.port
== 0) return NULL
;
499 return talloc_asprintf(mem_ctx
, "%d", floor
->rhs
.tcp
.port
);
501 case EPM_PROTOCOL_UDP
:
502 if (floor
->rhs
.udp
.port
== 0) return NULL
;
503 return talloc_asprintf(mem_ctx
, "%d", floor
->rhs
.udp
.port
);
505 case EPM_PROTOCOL_HTTP
:
506 if (floor
->rhs
.http
.port
== 0) return NULL
;
507 return talloc_asprintf(mem_ctx
, "%d", floor
->rhs
.http
.port
);
509 case EPM_PROTOCOL_IP
:
510 return talloc_strdup(mem_ctx
, floor
->rhs
.ip
.ipaddr
);
512 case EPM_PROTOCOL_NCACN
:
515 case EPM_PROTOCOL_NCADG
:
518 case EPM_PROTOCOL_SMB
:
519 if (strlen(floor
->rhs
.smb
.unc
) == 0) return NULL
;
520 return talloc_strdup(mem_ctx
, floor
->rhs
.smb
.unc
);
522 case EPM_PROTOCOL_PIPE
:
523 if (strlen(floor
->rhs
.pipe
.path
) == 0) return NULL
;
524 return talloc_strdup(mem_ctx
, floor
->rhs
.pipe
.path
);
526 case EPM_PROTOCOL_NETBIOS
:
527 if (strlen(floor
->rhs
.netbios
.name
) == 0) return NULL
;
528 return talloc_strdup(mem_ctx
, floor
->rhs
.netbios
.name
);
530 case EPM_PROTOCOL_NCALRPC
:
533 case EPM_PROTOCOL_VINES_SPP
:
534 return talloc_asprintf(mem_ctx
, "%d", floor
->rhs
.vines_spp
.port
);
536 case EPM_PROTOCOL_VINES_IPC
:
537 return talloc_asprintf(mem_ctx
, "%d", floor
->rhs
.vines_ipc
.port
);
539 case EPM_PROTOCOL_STREETTALK
:
540 return talloc_strdup(mem_ctx
, floor
->rhs
.streettalk
.streettalk
);
542 case EPM_PROTOCOL_UNIX_DS
:
543 if (strlen(floor
->rhs
.unix_ds
.path
) == 0) return NULL
;
544 return talloc_strdup(mem_ctx
, floor
->rhs
.unix_ds
.path
);
546 case EPM_PROTOCOL_NULL
:
550 DEBUG(0,("Unsupported lhs protocol %d\n", floor
->lhs
.protocol
));
557 static NTSTATUS
dcerpc_floor_set_rhs_data(TALLOC_CTX
*mem_ctx
, struct epm_floor
*floor
, const char *data
)
559 switch (floor
->lhs
.protocol
) {
560 case EPM_PROTOCOL_TCP
:
561 floor
->rhs
.tcp
.port
= atoi(data
);
564 case EPM_PROTOCOL_UDP
:
565 floor
->rhs
.udp
.port
= atoi(data
);
568 case EPM_PROTOCOL_HTTP
:
569 floor
->rhs
.http
.port
= atoi(data
);
572 case EPM_PROTOCOL_IP
:
573 floor
->rhs
.ip
.ipaddr
= talloc_strdup(mem_ctx
, data
);
574 NT_STATUS_HAVE_NO_MEMORY(floor
->rhs
.ip
.ipaddr
);
577 case EPM_PROTOCOL_NCACN
:
578 floor
->rhs
.ncacn
.minor_version
= 0;
581 case EPM_PROTOCOL_NCADG
:
582 floor
->rhs
.ncadg
.minor_version
= 0;
585 case EPM_PROTOCOL_SMB
:
586 floor
->rhs
.smb
.unc
= talloc_strdup(mem_ctx
, data
);
587 NT_STATUS_HAVE_NO_MEMORY(floor
->rhs
.smb
.unc
);
590 case EPM_PROTOCOL_PIPE
:
591 floor
->rhs
.pipe
.path
= talloc_strdup(mem_ctx
, data
);
592 NT_STATUS_HAVE_NO_MEMORY(floor
->rhs
.pipe
.path
);
595 case EPM_PROTOCOL_NETBIOS
:
596 floor
->rhs
.netbios
.name
= talloc_strdup(mem_ctx
, data
);
597 NT_STATUS_HAVE_NO_MEMORY(floor
->rhs
.netbios
.name
);
600 case EPM_PROTOCOL_NCALRPC
:
603 case EPM_PROTOCOL_VINES_SPP
:
604 floor
->rhs
.vines_spp
.port
= atoi(data
);
607 case EPM_PROTOCOL_VINES_IPC
:
608 floor
->rhs
.vines_ipc
.port
= atoi(data
);
611 case EPM_PROTOCOL_STREETTALK
:
612 floor
->rhs
.streettalk
.streettalk
= talloc_strdup(mem_ctx
, data
);
613 NT_STATUS_HAVE_NO_MEMORY(floor
->rhs
.streettalk
.streettalk
);
616 case EPM_PROTOCOL_UNIX_DS
:
617 floor
->rhs
.unix_ds
.path
= talloc_strdup(mem_ctx
, data
);
618 NT_STATUS_HAVE_NO_MEMORY(floor
->rhs
.unix_ds
.path
);
621 case EPM_PROTOCOL_NULL
:
625 DEBUG(0,("Unsupported lhs protocol %d\n", floor
->lhs
.protocol
));
629 return NT_STATUS_NOT_SUPPORTED
;
632 enum dcerpc_transport_t
dcerpc_transport_by_endpoint_protocol(int prot
)
636 /* Find a transport that has 'prot' as 4th protocol */
637 for (i
=0;i
<ARRAY_SIZE(transports
);i
++) {
638 if (transports
[i
].num_protocols
>= 2 &&
639 transports
[i
].protseq
[1] == prot
) {
640 return transports
[i
].transport
;
644 /* Unknown transport */
648 enum dcerpc_transport_t
dcerpc_transport_by_tower(struct epm_tower
*tower
)
652 /* Find a transport that matches this tower */
653 for (i
=0;i
<ARRAY_SIZE(transports
);i
++) {
655 if (transports
[i
].num_protocols
!= tower
->num_floors
- 2) {
659 for (j
= 0; j
< transports
[i
].num_protocols
; j
++) {
660 if (transports
[i
].protseq
[j
] != tower
->floors
[j
+2].lhs
.protocol
) {
665 if (j
== transports
[i
].num_protocols
) {
666 return transports
[i
].transport
;
670 /* Unknown transport */
674 NTSTATUS
dcerpc_binding_from_tower(TALLOC_CTX
*mem_ctx
, struct epm_tower
*tower
, struct dcerpc_binding
**b_out
)
677 struct dcerpc_binding
*binding
;
679 binding
= talloc(mem_ctx
, struct dcerpc_binding
);
680 NT_STATUS_HAVE_NO_MEMORY(binding
);
682 ZERO_STRUCT(binding
->object
);
683 binding
->options
= NULL
;
684 binding
->host
= NULL
;
687 binding
->transport
= dcerpc_transport_by_tower(tower
);
689 if (binding
->transport
== -1) {
690 return NT_STATUS_NOT_SUPPORTED
;
693 if (tower
->num_floors
< 1) {
697 /* Set object uuid */
698 status
= dcerpc_floor_get_lhs_data(&tower
->floors
[0], &binding
->object
, &binding
->object_version
);
700 if (!NT_STATUS_IS_OK(status
)) {
701 DEBUG(1, ("Error pulling object uuid and version: %s", nt_errstr(status
)));
705 /* Ignore floor 1, it contains the NDR version info */
707 binding
->options
= NULL
;
710 if (tower
->num_floors
>= 4) {
711 binding
->endpoint
= dcerpc_floor_get_rhs_data(mem_ctx
, &tower
->floors
[3]);
713 binding
->endpoint
= NULL
;
716 /* Set network address */
717 if (tower
->num_floors
>= 5) {
718 binding
->host
= dcerpc_floor_get_rhs_data(mem_ctx
, &tower
->floors
[4]);
724 NTSTATUS
dcerpc_binding_build_tower(TALLOC_CTX
*mem_ctx
, struct dcerpc_binding
*binding
, struct epm_tower
*tower
)
726 const enum epm_protocol
*protseq
= NULL
;
727 int num_protocols
= -1, i
;
728 struct GUID ndr_guid
;
732 for (i
=0;i
<ARRAY_SIZE(transports
);i
++) {
733 if (transports
[i
].transport
== binding
->transport
) {
734 protseq
= transports
[i
].protseq
;
735 num_protocols
= transports
[i
].num_protocols
;
740 if (num_protocols
== -1) {
741 DEBUG(0, ("Unable to find transport with id '%d'\n", binding
->transport
));
742 return NT_STATUS_UNSUCCESSFUL
;
745 tower
->num_floors
= 2 + num_protocols
;
746 tower
->floors
= talloc_array(mem_ctx
, struct epm_floor
, tower
->num_floors
);
749 tower
->floors
[0].lhs
.protocol
= EPM_PROTOCOL_UUID
;
751 tower
->floors
[0].lhs
.lhs_data
= dcerpc_floor_pack_lhs_data(mem_ctx
, &binding
->object
, binding
->object_version
);
753 tower
->floors
[0].rhs
.uuid
.unknown
= data_blob_talloc_zero(mem_ctx
, 2);
756 tower
->floors
[1].lhs
.protocol
= EPM_PROTOCOL_UUID
;
758 status
= GUID_from_string(NDR_GUID
, &ndr_guid
);
759 if (NT_STATUS_IS_ERR(status
)) {
763 tower
->floors
[1].lhs
.lhs_data
= dcerpc_floor_pack_lhs_data(mem_ctx
, &ndr_guid
, NDR_GUID_VERSION
);
765 tower
->floors
[1].rhs
.uuid
.unknown
= data_blob_talloc_zero(mem_ctx
, 2);
767 /* Floor 2 to num_protocols */
768 for (i
= 0; i
< num_protocols
; i
++) {
769 tower
->floors
[2 + i
].lhs
.protocol
= protseq
[i
];
770 tower
->floors
[2 + i
].lhs
.lhs_data
= data_blob_talloc(mem_ctx
, NULL
, 0);
771 ZERO_STRUCT(tower
->floors
[2 + i
].rhs
);
772 dcerpc_floor_set_rhs_data(mem_ctx
, &tower
->floors
[2 + i
], "");
775 /* The 4th floor contains the endpoint */
776 if (num_protocols
>= 2 && binding
->endpoint
) {
777 status
= dcerpc_floor_set_rhs_data(mem_ctx
, &tower
->floors
[3], binding
->endpoint
);
778 if (NT_STATUS_IS_ERR(status
)) {
783 /* The 5th contains the network address */
784 if (num_protocols
>= 3 && binding
->host
) {
785 status
= dcerpc_floor_set_rhs_data(mem_ctx
, &tower
->floors
[4], binding
->host
);
786 if (NT_STATUS_IS_ERR(status
)) {
794 NTSTATUS
dcerpc_epm_map_binding(TALLOC_CTX
*mem_ctx
, struct dcerpc_binding
*binding
,
795 const char *uuid
, uint_t version
)
797 struct dcerpc_pipe
*p
;
800 struct policy_handle handle
;
802 struct epm_twr_t twr
, *twr_r
;
803 struct dcerpc_binding
*epmapper_binding
;
804 const struct dcerpc_interface_table
*table
= idl_iface_by_uuid(uuid
);
807 struct cli_credentials
*anon_creds
808 = cli_credentials_init(mem_ctx
);
809 cli_credentials_set_anonymous(anon_creds
);
810 cli_credentials_guess(anon_creds
);
812 /* First, check if there is a default endpoint specified in the IDL */
815 struct dcerpc_binding
*default_binding
;
817 binding
->authservice
= talloc_strdup(binding
, table
->authservices
->names
[0]);
819 /* Find one of the default pipes for this interface */
820 for (i
= 0; i
< table
->endpoints
->count
; i
++) {
821 status
= dcerpc_parse_binding(mem_ctx
, table
->endpoints
->names
[i
], &default_binding
);
823 if (NT_STATUS_IS_OK(status
)) {
824 if (default_binding
->transport
== binding
->transport
&& default_binding
->endpoint
) {
825 binding
->endpoint
= talloc_reference(binding
, default_binding
->endpoint
);
826 talloc_free(default_binding
);
829 talloc_free(default_binding
);
835 epmapper_binding
= talloc_zero(mem_ctx
, struct dcerpc_binding
);
836 if (!epmapper_binding
) {
837 return NT_STATUS_NO_MEMORY
;
840 epmapper_binding
->transport
= binding
->transport
;
841 epmapper_binding
->host
= talloc_reference(epmapper_binding
,
843 epmapper_binding
->options
= NULL
;
844 epmapper_binding
->flags
= 0;
845 epmapper_binding
->endpoint
= NULL
;
846 epmapper_binding
->authservice
= NULL
;
848 status
= dcerpc_pipe_connect_b(mem_ctx
,
851 DCERPC_EPMAPPER_UUID
,
852 DCERPC_EPMAPPER_VERSION
,
855 if (!NT_STATUS_IS_OK(status
)) {
862 status
= GUID_from_string(uuid
, &binding
->object
);
863 if (NT_STATUS_IS_ERR(status
)) {
867 binding
->object_version
= version
;
869 status
= dcerpc_binding_build_tower(p
, binding
, &twr
.tower
);
870 if (NT_STATUS_IS_ERR(status
)) {
874 /* with some nice pretty paper around it of course */
876 r
.in
.map_tower
= &twr
;
877 r
.in
.entry_handle
= &handle
;
879 r
.out
.entry_handle
= &handle
;
881 status
= dcerpc_epm_Map(p
, p
, &r
);
882 if (!NT_STATUS_IS_OK(status
)) {
886 if (r
.out
.result
!= 0 || r
.out
.num_towers
!= 1) {
888 return NT_STATUS_PORT_UNREACHABLE
;
891 twr_r
= r
.out
.towers
[0].twr
;
894 return NT_STATUS_PORT_UNREACHABLE
;
897 if (twr_r
->tower
.num_floors
!= twr
.tower
.num_floors
||
898 twr_r
->tower
.floors
[3].lhs
.protocol
!= twr
.tower
.floors
[3].lhs
.protocol
) {
900 return NT_STATUS_PORT_UNREACHABLE
;
903 binding
->endpoint
= talloc_reference(binding
, dcerpc_floor_get_rhs_data(mem_ctx
, &twr_r
->tower
.floors
[3]));
912 perform an authenticated bind if needed
914 NTSTATUS
dcerpc_pipe_auth(struct dcerpc_pipe
*p
,
915 struct dcerpc_binding
*binding
,
916 const char *pipe_uuid
,
917 uint32_t pipe_version
,
918 struct cli_credentials
*credentials
)
922 tmp_ctx
= talloc_new(p
);
924 p
->conn
->flags
= binding
->flags
;
926 /* remember the binding string for possible secondary connections */
927 p
->conn
->binding_string
= dcerpc_binding_string(p
, binding
);
929 if (!cli_credentials_is_anonymous(credentials
) &&
930 (binding
->flags
& DCERPC_SCHANNEL_ANY
) &&
931 !cli_credentials_get_netlogon_creds(credentials
)) {
933 /* If we don't already have netlogon credentials for
934 * the schannel bind, then we have to get these
936 status
= dcerpc_bind_auth_schannel(tmp_ctx
,
937 p
, pipe_uuid
, pipe_version
,
939 } else if (!cli_credentials_is_anonymous(credentials
) &&
940 !(binding
->transport
== NCACN_NP
&&
941 !(binding
->flags
& DCERPC_SIGN
) &&
942 !(binding
->flags
& DCERPC_SEAL
))) {
944 if (binding
->flags
& DCERPC_AUTH_SPNEGO
) {
945 auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
946 } else if (binding
->flags
& DCERPC_AUTH_KRB5
) {
947 auth_type
= DCERPC_AUTH_TYPE_KRB5
;
948 } else if (binding
->flags
& DCERPC_SCHANNEL_ANY
) {
949 auth_type
= DCERPC_AUTH_TYPE_SCHANNEL
;
951 auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
954 status
= dcerpc_bind_auth_password(p
, pipe_uuid
, pipe_version
,
955 credentials
, auth_type
,
956 binding
->authservice
);
958 status
= dcerpc_bind_auth_none(p
, pipe_uuid
, pipe_version
);
961 if (!NT_STATUS_IS_OK(status
)) {
962 DEBUG(0,("Failed to bind to uuid %s - %s\n", pipe_uuid
, nt_errstr(status
)));
964 talloc_free(tmp_ctx
);
969 /* open a rpc connection to a rpc pipe on SMB using the binding
970 structure to determine the endpoint and options */
971 static NTSTATUS
dcerpc_pipe_connect_ncacn_np(TALLOC_CTX
*tmp_ctx
,
972 struct dcerpc_pipe
*p
,
973 struct dcerpc_binding
*binding
,
974 const char *pipe_uuid
,
975 uint32_t pipe_version
,
976 struct cli_credentials
*credentials
)
979 struct smbcli_state
*cli
;
980 const char *pipe_name
= NULL
;
982 if (binding
->flags
& DCERPC_SCHANNEL_ANY
) {
983 struct cli_credentials
*anon_creds
984 = cli_credentials_init(tmp_ctx
);
985 cli_credentials_set_anonymous(anon_creds
);
986 cli_credentials_guess(anon_creds
);
987 status
= smbcli_full_connection(p
->conn
, &cli
,
992 status
= smbcli_full_connection(p
->conn
, &cli
,
997 if (!NT_STATUS_IS_OK(status
)) {
998 DEBUG(0,("Failed to connect to %s - %s\n", binding
->host
, nt_errstr(status
)));
1002 /* Look up identifier using the epmapper */
1003 if (!binding
->endpoint
) {
1004 status
= dcerpc_epm_map_binding(tmp_ctx
, binding
, pipe_uuid
, pipe_version
);
1005 if (!NT_STATUS_IS_OK(status
)) {
1006 DEBUG(0,("Failed to map DCERPC/TCP NCACN_NP pipe for '%s' - %s\n",
1007 pipe_uuid
, nt_errstr(status
)));
1010 DEBUG(1,("Mapped to DCERPC/NP pipe %s\n", binding
->endpoint
));
1013 pipe_name
= binding
->endpoint
;
1015 status
= dcerpc_pipe_open_smb(p
->conn
, cli
->tree
, pipe_name
);
1016 if (!NT_STATUS_IS_OK(status
)) {
1017 DEBUG(0,("Failed to open pipe %s - %s\n", pipe_name
, nt_errstr(status
)));
1021 return NT_STATUS_OK
;
1024 /* open a rpc connection to a rpc pipe on SMP using the binding
1025 structure to determine the endpoint and options */
1026 static NTSTATUS
dcerpc_pipe_connect_ncalrpc(TALLOC_CTX
*tmp_ctx
,
1027 struct dcerpc_pipe
*p
,
1028 struct dcerpc_binding
*binding
,
1029 const char *pipe_uuid
,
1030 uint32_t pipe_version
)
1034 /* Look up identifier using the epmapper */
1035 if (!binding
->endpoint
) {
1036 status
= dcerpc_epm_map_binding(tmp_ctx
, binding
, pipe_uuid
, pipe_version
);
1037 if (!NT_STATUS_IS_OK(status
)) {
1038 DEBUG(0,("Failed to map DCERPC/TCP NCALRPC identifier for '%s' - %s\n",
1039 pipe_uuid
, nt_errstr(status
)));
1042 DEBUG(1,("Mapped to DCERPC/LRPC identifier %s\n", binding
->endpoint
));
1045 status
= dcerpc_pipe_open_pipe(p
->conn
, binding
->endpoint
);
1046 if (!NT_STATUS_IS_OK(status
)) {
1047 DEBUG(0,("Failed to open ncalrpc pipe '%s' - %s\n",
1048 binding
->endpoint
, nt_errstr(status
)));
1057 /* open a rpc connection to a rpc pipe on SMP using the binding
1058 structure to determine the endpoint and options */
1059 static NTSTATUS
dcerpc_pipe_connect_ncacn_unix_stream(TALLOC_CTX
*tmp_ctx
,
1060 struct dcerpc_pipe
*p
,
1061 struct dcerpc_binding
*binding
,
1062 const char *pipe_uuid
,
1063 uint32_t pipe_version
)
1067 if (!binding
->endpoint
) {
1068 DEBUG(0, ("Path to unix socket not specified\n"));
1069 return NT_STATUS_INVALID_PARAMETER
;
1072 status
= dcerpc_pipe_open_unix_stream(p
->conn
, binding
->endpoint
);
1073 if (!NT_STATUS_IS_OK(status
)) {
1074 DEBUG(0,("Failed to open unix socket %s - %s\n",
1075 binding
->endpoint
, nt_errstr(status
)));
1083 /* open a rpc connection to a rpc pipe on TCP/IP sockets using the binding
1084 structure to determine the endpoint and options */
1085 static NTSTATUS
dcerpc_pipe_connect_ncacn_ip_tcp(TALLOC_CTX
*tmp_ctx
,
1086 struct dcerpc_pipe
*p
,
1087 struct dcerpc_binding
*binding
,
1088 const char *pipe_uuid
,
1089 uint32_t pipe_version
)
1094 if (!binding
->endpoint
) {
1095 status
= dcerpc_epm_map_binding(tmp_ctx
, binding
,
1096 pipe_uuid
, pipe_version
);
1097 if (!NT_STATUS_IS_OK(status
)) {
1098 DEBUG(0,("Failed to map DCERPC/TCP port for '%s' - %s\n",
1099 pipe_uuid
, nt_errstr(status
)));
1102 DEBUG(1,("Mapped to DCERPC/TCP port %s\n", binding
->endpoint
));
1105 port
= atoi(binding
->endpoint
);
1107 status
= dcerpc_pipe_open_tcp(p
->conn
, binding
->host
, port
);
1108 if (!NT_STATUS_IS_OK(status
)) {
1109 DEBUG(0,("Failed to connect to %s:%d - %s\n",
1110 binding
->host
, port
, nt_errstr(status
)));
1118 /* open a rpc connection to a rpc pipe, using the specified
1119 binding structure to determine the endpoint and options */
1120 NTSTATUS
dcerpc_pipe_connect_b(TALLOC_CTX
*parent_ctx
,
1121 struct dcerpc_pipe
**pp
,
1122 struct dcerpc_binding
*binding
,
1123 const char *pipe_uuid
,
1124 uint32_t pipe_version
,
1125 struct cli_credentials
*credentials
)
1127 NTSTATUS status
= NT_STATUS_INVALID_PARAMETER
;
1128 struct dcerpc_pipe
*p
;
1130 TALLOC_CTX
*tmp_ctx
;
1134 p
= dcerpc_pipe_init(parent_ctx
);
1136 return NT_STATUS_NO_MEMORY
;
1138 tmp_ctx
= talloc_named(p
, 0, "dcerpc_pipe_connect_b tmp_ctx");
1140 switch (binding
->transport
) {
1142 status
= dcerpc_pipe_connect_ncacn_np(tmp_ctx
,
1143 p
, binding
, pipe_uuid
, pipe_version
, credentials
);
1146 status
= dcerpc_pipe_connect_ncacn_ip_tcp(tmp_ctx
,
1147 p
, binding
, pipe_uuid
, pipe_version
);
1149 case NCACN_UNIX_STREAM
:
1150 status
= dcerpc_pipe_connect_ncacn_unix_stream(tmp_ctx
,
1151 p
, binding
, pipe_uuid
, pipe_version
);
1154 status
= dcerpc_pipe_connect_ncalrpc(tmp_ctx
,
1155 p
, binding
, pipe_uuid
, pipe_version
);
1158 return NT_STATUS_NOT_SUPPORTED
;
1161 if (!NT_STATUS_IS_OK(status
)) {
1166 status
= dcerpc_pipe_auth(p
, binding
, pipe_uuid
, pipe_version
, credentials
);
1167 if (!NT_STATUS_IS_OK(status
)) {
1172 talloc_free(tmp_ctx
);
1178 /* open a rpc connection to a rpc pipe, using the specified string
1179 binding to determine the endpoint and options */
1180 NTSTATUS
dcerpc_pipe_connect(TALLOC_CTX
*parent_ctx
,
1181 struct dcerpc_pipe
**pp
,
1182 const char *binding
,
1183 const char *pipe_uuid
,
1184 uint32_t pipe_version
,
1185 struct cli_credentials
*credentials
)
1187 struct dcerpc_binding
*b
;
1189 TALLOC_CTX
*tmp_ctx
;
1191 tmp_ctx
= talloc_named(parent_ctx
, 0, "dcerpc_pipe_connect tmp_ctx");
1193 return NT_STATUS_NO_MEMORY
;
1196 status
= dcerpc_parse_binding(tmp_ctx
, binding
, &b
);
1197 if (!NT_STATUS_IS_OK(status
)) {
1198 DEBUG(0,("Failed to parse dcerpc binding '%s'\n", binding
));
1199 talloc_free(tmp_ctx
);
1203 DEBUG(3,("Using binding %s\n", dcerpc_binding_string(tmp_ctx
, b
)));
1205 status
= dcerpc_pipe_connect_b(tmp_ctx
,
1206 pp
, b
, pipe_uuid
, pipe_version
, credentials
);
1208 if (NT_STATUS_IS_OK(status
)) {
1209 *pp
= talloc_reference(parent_ctx
, *pp
);
1211 talloc_free(tmp_ctx
);
1218 create a secondary dcerpc connection from a primary connection
1220 if the primary is a SMB connection then the secondary connection
1221 will be on the same SMB connection, but use a new fnum
1223 NTSTATUS
dcerpc_secondary_connection(struct dcerpc_pipe
*p
, struct dcerpc_pipe
**p2
,
1224 struct dcerpc_binding
*b
)
1228 struct smbcli_tree
*tree
;
1229 NTSTATUS status
= NT_STATUS_INVALID_PARAMETER
;
1231 (*p2
) = dcerpc_pipe_init(p
);
1233 return NT_STATUS_NO_MEMORY
;
1236 switch (p
->conn
->transport
.transport
) {
1238 tree
= dcerpc_smb_tree(p
->conn
);
1240 return NT_STATUS_INVALID_PARAMETER
;
1242 status
= dcerpc_pipe_open_smb((*p2
)->conn
, tree
, b
->endpoint
);
1246 status
= dcerpc_pipe_open_tcp((*p2
)->conn
, b
->host
, atoi(b
->endpoint
));
1250 status
= dcerpc_pipe_open_pipe((*p2
)->conn
, b
->endpoint
);
1254 return NT_STATUS_NOT_SUPPORTED
;
1257 if (!NT_STATUS_IS_OK(status
)) {
1262 (*p2
)->conn
->flags
= p
->conn
->flags
;
1264 return NT_STATUS_OK
;
1267 NTSTATUS
dcerpc_generic_session_key(struct dcerpc_connection
*c
,
1268 DATA_BLOB
*session_key
)
1270 /* this took quite a few CPU cycles to find ... */
1271 session_key
->data
= discard_const_p(unsigned char, "SystemLibraryDTC");
1272 session_key
->length
= 16;
1273 return NT_STATUS_OK
;
1277 fetch the user session key - may be default (above) or the SMB session key
1279 NTSTATUS
dcerpc_fetch_session_key(struct dcerpc_pipe
*p
,
1280 DATA_BLOB
*session_key
)
1282 return p
->conn
->security_state
.session_key(p
->conn
, session_key
);
1287 log a rpc packet in a format suitable for ndrdump. This is especially useful
1288 for sealed packets, where ethereal cannot easily see the contents
1290 this triggers on a debug level of >= 10
1292 void dcerpc_log_packet(const struct dcerpc_interface_table
*ndr
,
1293 uint32_t opnum
, uint32_t flags
, DATA_BLOB
*pkt
)
1295 const int num_examples
= 20;
1298 if (DEBUGLEVEL
< 10) return;
1300 for (i
=0;i
<num_examples
;i
++) {
1302 asprintf(&name
, "%s/rpclog/%s-%u.%d.%s",
1303 lp_lockdir(), ndr
->name
, opnum
, i
,
1304 (flags
&NDR_IN
)?"in":"out");
1308 if (!file_exist(name
)) {
1309 if (file_save(name
, pkt
->data
, pkt
->length
)) {
1310 DEBUG(10,("Logged rpc packet to %s\n", name
));
1322 create a secondary context from a primary connection
1324 this uses dcerpc_alter_context() to create a new dcerpc context_id
1326 NTSTATUS
dcerpc_secondary_context(struct dcerpc_pipe
*p
,
1327 struct dcerpc_pipe
**pp2
,
1328 const char *pipe_uuid
,
1329 uint32_t pipe_version
)
1332 struct dcerpc_pipe
*p2
;
1334 p2
= talloc_zero(p
, struct dcerpc_pipe
);
1336 return NT_STATUS_NO_MEMORY
;
1338 p2
->conn
= talloc_reference(p2
, p
->conn
);
1340 p2
->context_id
= ++p
->conn
->next_context_id
;
1342 status
= GUID_from_string(pipe_uuid
, &p2
->syntax
.uuid
);
1343 if (!NT_STATUS_IS_OK(status
)) {
1347 p2
->syntax
.if_version
= pipe_version
;
1349 status
= GUID_from_string(NDR_GUID
, &p2
->transfer_syntax
.uuid
);
1350 if (!NT_STATUS_IS_OK(status
)) {
1354 p2
->transfer_syntax
.if_version
= NDR_GUID_VERSION
;
1356 status
= dcerpc_alter_context(p2
, p2
, &p2
->syntax
, &p2
->transfer_syntax
);
1357 if (!NT_STATUS_IS_OK(status
)) {