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
9 Copyright (C) Rafal Szczesniak 2006
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include "lib/events/events.h"
28 #include "libcli/composite/composite.h"
29 #include "librpc/gen_ndr/ndr_epmapper_c.h"
30 #include "librpc/gen_ndr/ndr_dcerpc.h"
31 #include "librpc/gen_ndr/ndr_misc.h"
32 #include "auth/credentials/credentials.h"
35 find a dcerpc call on an interface by name
37 const struct dcerpc_interface_call
*dcerpc_iface_find_call(const struct dcerpc_interface_table
*iface
,
41 for (i
=0;i
<iface
->num_calls
;i
++) {
42 if (strcmp(iface
->calls
[i
].name
, name
) == 0) {
43 return &iface
->calls
[i
];
50 push a ncacn_packet into a blob, potentially with auth info
52 NTSTATUS
ncacn_push_auth(DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
,
53 struct ncacn_packet
*pkt
,
54 struct dcerpc_auth
*auth_info
)
59 ndr
= ndr_push_init_ctx(mem_ctx
);
61 return NT_STATUS_NO_MEMORY
;
64 if (!(pkt
->drep
[0] & DCERPC_DREP_LE
)) {
65 ndr
->flags
|= LIBNDR_FLAG_BIGENDIAN
;
68 if (pkt
->pfc_flags
& DCERPC_PFC_FLAG_ORPC
) {
69 ndr
->flags
|= LIBNDR_FLAG_OBJECT_PRESENT
;
73 pkt
->auth_length
= auth_info
->credentials
.length
;
78 status
= ndr_push_ncacn_packet(ndr
, NDR_SCALARS
|NDR_BUFFERS
, pkt
);
79 if (!NT_STATUS_IS_OK(status
)) {
84 status
= ndr_push_dcerpc_auth(ndr
, NDR_SCALARS
|NDR_BUFFERS
, auth_info
);
87 *blob
= ndr_push_blob(ndr
);
89 /* fill in the frag length */
90 dcerpc_set_frag_length(blob
, blob
->length
);
95 #define MAX_PROTSEQ 10
99 enum dcerpc_transport_t transport
;
101 enum epm_protocol protseq
[MAX_PROTSEQ
];
103 { "ncacn_np", NCACN_NP
, 3,
104 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_SMB
, EPM_PROTOCOL_NETBIOS
}},
105 { "ncacn_ip_tcp", NCACN_IP_TCP
, 3,
106 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_TCP
, EPM_PROTOCOL_IP
} },
107 { "ncacn_http", NCACN_HTTP
, 3,
108 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_HTTP
, EPM_PROTOCOL_IP
} },
109 { "ncadg_ip_udp", NCACN_IP_UDP
, 3,
110 { EPM_PROTOCOL_NCADG
, EPM_PROTOCOL_UDP
, EPM_PROTOCOL_IP
} },
111 { "ncalrpc", NCALRPC
, 2,
112 { EPM_PROTOCOL_NCALRPC
, EPM_PROTOCOL_PIPE
} },
113 { "ncacn_unix_stream", NCACN_UNIX_STREAM
, 2,
114 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_UNIX_DS
} },
115 { "ncadg_unix_dgram", NCADG_UNIX_DGRAM
, 2,
116 { EPM_PROTOCOL_NCADG
, EPM_PROTOCOL_UNIX_DS
} },
117 { "ncacn_at_dsp", NCACN_AT_DSP
, 3,
118 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_APPLETALK
, EPM_PROTOCOL_DSP
} },
119 { "ncadg_at_ddp", NCADG_AT_DDP
, 3,
120 { EPM_PROTOCOL_NCADG
, EPM_PROTOCOL_APPLETALK
, EPM_PROTOCOL_DDP
} },
121 { "ncacn_vns_ssp", NCACN_VNS_SPP
, 3,
122 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_STREETTALK
, EPM_PROTOCOL_VINES_SPP
} },
123 { "ncacn_vns_ipc", NCACN_VNS_IPC
, 3,
124 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_STREETTALK
, EPM_PROTOCOL_VINES_IPC
}, },
125 { "ncadg_ipx", NCADG_IPX
, 2,
126 { EPM_PROTOCOL_NCADG
, EPM_PROTOCOL_IPX
},
128 { "ncacn_spx", NCACN_SPX
, 3,
129 /* I guess some MS programmer confused the identifier for
130 * EPM_PROTOCOL_UUID (0x0D or 13) with the one for
131 * EPM_PROTOCOL_SPX (0x13) here. -- jelmer*/
132 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_NCALRPC
, EPM_PROTOCOL_UUID
},
136 static const struct {
139 } ncacn_options
[] = {
140 {"sign", DCERPC_SIGN
},
141 {"seal", DCERPC_SEAL
},
142 {"connect", DCERPC_CONNECT
},
143 {"spnego", DCERPC_AUTH_SPNEGO
},
144 {"ntlm", DCERPC_AUTH_NTLM
},
145 {"krb5", DCERPC_AUTH_KRB5
},
146 {"validate", DCERPC_DEBUG_VALIDATE_BOTH
},
147 {"print", DCERPC_DEBUG_PRINT_BOTH
},
148 {"padcheck", DCERPC_DEBUG_PAD_CHECK
},
149 {"bigendian", DCERPC_PUSH_BIGENDIAN
},
150 {"smb2", DCERPC_SMB2
}
153 const char *epm_floor_string(TALLOC_CTX
*mem_ctx
, struct epm_floor
*epm_floor
)
155 struct dcerpc_syntax_id syntax
;
158 switch(epm_floor
->lhs
.protocol
) {
159 case EPM_PROTOCOL_UUID
:
160 status
= dcerpc_floor_get_lhs_data(epm_floor
, &syntax
);
161 if (NT_STATUS_IS_OK(status
)) {
162 /* lhs is used: UUID */
165 if (GUID_equal(&syntax
.uuid
, &ndr_transfer_syntax
.uuid
)) {
169 if (GUID_equal(&syntax
.uuid
, &ndr64_transfer_syntax
.uuid
)) {
173 uuidstr
= GUID_string(mem_ctx
, &syntax
.uuid
);
175 return talloc_asprintf(mem_ctx
, " uuid %s/0x%02x", uuidstr
, syntax
.if_version
);
177 return talloc_asprintf(mem_ctx
, "IPX:%s",
178 data_blob_hex_string(mem_ctx
, &epm_floor
->rhs
.uuid
.unknown
));
181 case EPM_PROTOCOL_NCACN
:
184 case EPM_PROTOCOL_NCADG
:
187 case EPM_PROTOCOL_NCALRPC
:
190 case EPM_PROTOCOL_DNET_NSP
:
193 case EPM_PROTOCOL_IP
:
194 return talloc_asprintf(mem_ctx
, "IP:%s", epm_floor
->rhs
.ip
.ipaddr
);
196 case EPM_PROTOCOL_PIPE
:
197 return talloc_asprintf(mem_ctx
, "PIPE:%s", epm_floor
->rhs
.pipe
.path
);
199 case EPM_PROTOCOL_SMB
:
200 return talloc_asprintf(mem_ctx
, "SMB:%s", epm_floor
->rhs
.smb
.unc
);
202 case EPM_PROTOCOL_UNIX_DS
:
203 return talloc_asprintf(mem_ctx
, "Unix:%s", epm_floor
->rhs
.unix_ds
.path
);
205 case EPM_PROTOCOL_NETBIOS
:
206 return talloc_asprintf(mem_ctx
, "NetBIOS:%s", epm_floor
->rhs
.netbios
.name
);
208 case EPM_PROTOCOL_NETBEUI
:
211 case EPM_PROTOCOL_SPX
:
214 case EPM_PROTOCOL_NB_IPX
:
217 case EPM_PROTOCOL_HTTP
:
218 return talloc_asprintf(mem_ctx
, "HTTP:%d", epm_floor
->rhs
.http
.port
);
220 case EPM_PROTOCOL_TCP
:
221 return talloc_asprintf(mem_ctx
, "TCP:%d", epm_floor
->rhs
.tcp
.port
);
223 case EPM_PROTOCOL_UDP
:
224 return talloc_asprintf(mem_ctx
, "UDP:%d", epm_floor
->rhs
.udp
.port
);
227 return talloc_asprintf(mem_ctx
, "UNK(%02x):", epm_floor
->lhs
.protocol
);
233 form a binding string from a binding structure
235 char *dcerpc_binding_string(TALLOC_CTX
*mem_ctx
, const struct dcerpc_binding
*b
)
237 char *s
= talloc_strdup(mem_ctx
, "");
239 const char *t_name
=NULL
;
241 for (i
=0;i
<ARRAY_SIZE(transports
);i
++) {
242 if (transports
[i
].transport
== b
->transport
) {
243 t_name
= transports
[i
].name
;
250 if (!GUID_all_zero(&b
->object
.uuid
)) {
251 s
= talloc_asprintf(s
, "%s@",
252 GUID_string(mem_ctx
, &b
->object
.uuid
));
255 s
= talloc_asprintf_append(s
, "%s:", t_name
);
259 s
= talloc_asprintf_append(s
, "%s", b
->host
);
262 if (!b
->endpoint
&& !b
->options
&& !b
->flags
) {
266 s
= talloc_asprintf_append(s
, "[");
269 s
= talloc_asprintf_append(s
, "%s", b
->endpoint
);
272 /* this is a *really* inefficent way of dealing with strings,
273 but this is rarely called and the strings are always short,
275 for (i
=0;b
->options
&& b
->options
[i
];i
++) {
276 s
= talloc_asprintf_append(s
, ",%s", b
->options
[i
]);
280 for (i
=0;i
<ARRAY_SIZE(ncacn_options
);i
++) {
281 if (b
->flags
& ncacn_options
[i
].flag
) {
282 s
= talloc_asprintf_append(s
, ",%s", ncacn_options
[i
].name
);
287 s
= talloc_asprintf_append(s
, "]");
293 parse a binding string into a dcerpc_binding structure
295 NTSTATUS
dcerpc_parse_binding(TALLOC_CTX
*mem_ctx
, const char *s
, struct dcerpc_binding
**b_out
)
297 struct dcerpc_binding
*b
;
298 char *options
, *type
;
300 int i
, j
, comma_count
;
302 b
= talloc(mem_ctx
, struct dcerpc_binding
);
304 return NT_STATUS_NO_MEMORY
;
309 if (p
&& PTR_DIFF(p
, s
) == 36) { /* 36 is the length of a UUID */
312 status
= GUID_from_string(s
, &b
->object
.uuid
);
314 if (NT_STATUS_IS_ERR(status
)) {
315 DEBUG(0, ("Failed parsing UUID\n"));
321 ZERO_STRUCT(b
->object
);
324 b
->object
.if_version
= 0;
328 return NT_STATUS_INVALID_PARAMETER
;
331 type
= talloc_strndup(mem_ctx
, s
, PTR_DIFF(p
, s
));
333 return NT_STATUS_NO_MEMORY
;
336 for (i
=0;i
<ARRAY_SIZE(transports
);i
++) {
337 if (strcasecmp(type
, transports
[i
].name
) == 0) {
338 b
->transport
= transports
[i
].transport
;
342 if (i
==ARRAY_SIZE(transports
)) {
343 DEBUG(0,("Unknown dcerpc transport '%s'\n", type
));
344 return NT_STATUS_INVALID_PARAMETER
;
351 b
->host
= talloc_strndup(b
, s
, PTR_DIFF(p
, s
));
352 options
= talloc_strdup(mem_ctx
, p
+1);
353 if (options
[strlen(options
)-1] != ']') {
354 return NT_STATUS_INVALID_PARAMETER
;
356 options
[strlen(options
)-1] = 0;
358 b
->host
= talloc_strdup(b
, s
);
362 return NT_STATUS_NO_MEMORY
;
365 b
->target_hostname
= b
->host
;
376 comma_count
= count_chars(options
, ',');
378 b
->options
= talloc_array(b
, const char *, comma_count
+2);
380 return NT_STATUS_NO_MEMORY
;
383 for (i
=0; (p
= strchr(options
, ',')); i
++) {
384 b
->options
[i
] = talloc_strndup(b
, options
, PTR_DIFF(p
, options
));
385 if (!b
->options
[i
]) {
386 return NT_STATUS_NO_MEMORY
;
390 b
->options
[i
] = options
;
391 b
->options
[i
+1] = NULL
;
393 /* some options are pre-parsed for convenience */
394 for (i
=0;b
->options
[i
];i
++) {
395 for (j
=0;j
<ARRAY_SIZE(ncacn_options
);j
++) {
396 if (strcasecmp(ncacn_options
[j
].name
, b
->options
[i
]) == 0) {
398 b
->flags
|= ncacn_options
[j
].flag
;
399 for (k
=i
;b
->options
[k
];k
++) {
400 b
->options
[k
] = b
->options
[k
+1];
409 /* Endpoint is first option */
410 b
->endpoint
= b
->options
[0];
411 if (strlen(b
->endpoint
) == 0) b
->endpoint
= NULL
;
413 for (i
=0;b
->options
[i
];i
++) {
414 b
->options
[i
] = b
->options
[i
+1];
418 if (b
->options
[0] == NULL
)
425 NTSTATUS
dcerpc_floor_get_lhs_data(struct epm_floor
*epm_floor
, struct dcerpc_syntax_id
*syntax
)
427 TALLOC_CTX
*mem_ctx
= talloc_init("floor_get_lhs_data");
428 struct ndr_pull
*ndr
= ndr_pull_init_blob(&epm_floor
->lhs
.lhs_data
, mem_ctx
);
430 uint16_t if_version
=0;
432 ndr
->flags
|= LIBNDR_FLAG_NOALIGN
;
434 status
= ndr_pull_GUID(ndr
, NDR_SCALARS
| NDR_BUFFERS
, &syntax
->uuid
);
435 if (NT_STATUS_IS_ERR(status
)) {
436 talloc_free(mem_ctx
);
440 status
= ndr_pull_uint16(ndr
, NDR_SCALARS
, &if_version
);
441 syntax
->if_version
= if_version
;
443 talloc_free(mem_ctx
);
448 static DATA_BLOB
dcerpc_floor_pack_lhs_data(TALLOC_CTX
*mem_ctx
, const struct dcerpc_syntax_id
*syntax
)
450 struct ndr_push
*ndr
= ndr_push_init_ctx(mem_ctx
);
452 ndr
->flags
|= LIBNDR_FLAG_NOALIGN
;
454 ndr_push_GUID(ndr
, NDR_SCALARS
| NDR_BUFFERS
, &syntax
->uuid
);
455 ndr_push_uint16(ndr
, NDR_SCALARS
, syntax
->if_version
);
457 return ndr_push_blob(ndr
);
460 const char *dcerpc_floor_get_rhs_data(TALLOC_CTX
*mem_ctx
, struct epm_floor
*epm_floor
)
462 switch (epm_floor
->lhs
.protocol
) {
463 case EPM_PROTOCOL_TCP
:
464 if (epm_floor
->rhs
.tcp
.port
== 0) return NULL
;
465 return talloc_asprintf(mem_ctx
, "%d", epm_floor
->rhs
.tcp
.port
);
467 case EPM_PROTOCOL_UDP
:
468 if (epm_floor
->rhs
.udp
.port
== 0) return NULL
;
469 return talloc_asprintf(mem_ctx
, "%d", epm_floor
->rhs
.udp
.port
);
471 case EPM_PROTOCOL_HTTP
:
472 if (epm_floor
->rhs
.http
.port
== 0) return NULL
;
473 return talloc_asprintf(mem_ctx
, "%d", epm_floor
->rhs
.http
.port
);
475 case EPM_PROTOCOL_IP
:
476 return talloc_strdup(mem_ctx
, epm_floor
->rhs
.ip
.ipaddr
);
478 case EPM_PROTOCOL_NCACN
:
481 case EPM_PROTOCOL_NCADG
:
484 case EPM_PROTOCOL_SMB
:
485 if (strlen(epm_floor
->rhs
.smb
.unc
) == 0) return NULL
;
486 return talloc_strdup(mem_ctx
, epm_floor
->rhs
.smb
.unc
);
488 case EPM_PROTOCOL_PIPE
:
489 if (strlen(epm_floor
->rhs
.pipe
.path
) == 0) return NULL
;
490 return talloc_strdup(mem_ctx
, epm_floor
->rhs
.pipe
.path
);
492 case EPM_PROTOCOL_NETBIOS
:
493 if (strlen(epm_floor
->rhs
.netbios
.name
) == 0) return NULL
;
494 return talloc_strdup(mem_ctx
, epm_floor
->rhs
.netbios
.name
);
496 case EPM_PROTOCOL_NCALRPC
:
499 case EPM_PROTOCOL_VINES_SPP
:
500 return talloc_asprintf(mem_ctx
, "%d", epm_floor
->rhs
.vines_spp
.port
);
502 case EPM_PROTOCOL_VINES_IPC
:
503 return talloc_asprintf(mem_ctx
, "%d", epm_floor
->rhs
.vines_ipc
.port
);
505 case EPM_PROTOCOL_STREETTALK
:
506 return talloc_strdup(mem_ctx
, epm_floor
->rhs
.streettalk
.streettalk
);
508 case EPM_PROTOCOL_UNIX_DS
:
509 if (strlen(epm_floor
->rhs
.unix_ds
.path
) == 0) return NULL
;
510 return talloc_strdup(mem_ctx
, epm_floor
->rhs
.unix_ds
.path
);
512 case EPM_PROTOCOL_NULL
:
516 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor
->lhs
.protocol
));
523 static NTSTATUS
dcerpc_floor_set_rhs_data(TALLOC_CTX
*mem_ctx
, struct epm_floor
*epm_floor
, const char *data
)
525 switch (epm_floor
->lhs
.protocol
) {
526 case EPM_PROTOCOL_TCP
:
527 epm_floor
->rhs
.tcp
.port
= atoi(data
);
530 case EPM_PROTOCOL_UDP
:
531 epm_floor
->rhs
.udp
.port
= atoi(data
);
534 case EPM_PROTOCOL_HTTP
:
535 epm_floor
->rhs
.http
.port
= atoi(data
);
538 case EPM_PROTOCOL_IP
:
539 epm_floor
->rhs
.ip
.ipaddr
= talloc_strdup(mem_ctx
, data
);
540 NT_STATUS_HAVE_NO_MEMORY(epm_floor
->rhs
.ip
.ipaddr
);
543 case EPM_PROTOCOL_NCACN
:
544 epm_floor
->rhs
.ncacn
.minor_version
= 0;
547 case EPM_PROTOCOL_NCADG
:
548 epm_floor
->rhs
.ncadg
.minor_version
= 0;
551 case EPM_PROTOCOL_SMB
:
552 epm_floor
->rhs
.smb
.unc
= talloc_strdup(mem_ctx
, data
);
553 NT_STATUS_HAVE_NO_MEMORY(epm_floor
->rhs
.smb
.unc
);
556 case EPM_PROTOCOL_PIPE
:
557 epm_floor
->rhs
.pipe
.path
= talloc_strdup(mem_ctx
, data
);
558 NT_STATUS_HAVE_NO_MEMORY(epm_floor
->rhs
.pipe
.path
);
561 case EPM_PROTOCOL_NETBIOS
:
562 epm_floor
->rhs
.netbios
.name
= talloc_strdup(mem_ctx
, data
);
563 NT_STATUS_HAVE_NO_MEMORY(epm_floor
->rhs
.netbios
.name
);
566 case EPM_PROTOCOL_NCALRPC
:
569 case EPM_PROTOCOL_VINES_SPP
:
570 epm_floor
->rhs
.vines_spp
.port
= atoi(data
);
573 case EPM_PROTOCOL_VINES_IPC
:
574 epm_floor
->rhs
.vines_ipc
.port
= atoi(data
);
577 case EPM_PROTOCOL_STREETTALK
:
578 epm_floor
->rhs
.streettalk
.streettalk
= talloc_strdup(mem_ctx
, data
);
579 NT_STATUS_HAVE_NO_MEMORY(epm_floor
->rhs
.streettalk
.streettalk
);
582 case EPM_PROTOCOL_UNIX_DS
:
583 epm_floor
->rhs
.unix_ds
.path
= talloc_strdup(mem_ctx
, data
);
584 NT_STATUS_HAVE_NO_MEMORY(epm_floor
->rhs
.unix_ds
.path
);
587 case EPM_PROTOCOL_NULL
:
591 DEBUG(0,("Unsupported lhs protocol %d\n", epm_floor
->lhs
.protocol
));
595 return NT_STATUS_NOT_SUPPORTED
;
598 enum dcerpc_transport_t
dcerpc_transport_by_endpoint_protocol(int prot
)
602 /* Find a transport that has 'prot' as 4th protocol */
603 for (i
=0;i
<ARRAY_SIZE(transports
);i
++) {
604 if (transports
[i
].num_protocols
>= 2 &&
605 transports
[i
].protseq
[1] == prot
) {
606 return transports
[i
].transport
;
610 /* Unknown transport */
611 return (unsigned int)-1;
614 enum dcerpc_transport_t
dcerpc_transport_by_tower(struct epm_tower
*tower
)
618 /* Find a transport that matches this tower */
619 for (i
=0;i
<ARRAY_SIZE(transports
);i
++) {
621 if (transports
[i
].num_protocols
!= tower
->num_floors
- 2) {
625 for (j
= 0; j
< transports
[i
].num_protocols
; j
++) {
626 if (transports
[i
].protseq
[j
] != tower
->floors
[j
+2].lhs
.protocol
) {
631 if (j
== transports
[i
].num_protocols
) {
632 return transports
[i
].transport
;
636 /* Unknown transport */
637 return (unsigned int)-1;
640 NTSTATUS
dcerpc_binding_from_tower(TALLOC_CTX
*mem_ctx
, struct epm_tower
*tower
, struct dcerpc_binding
**b_out
)
643 struct dcerpc_binding
*binding
;
645 binding
= talloc(mem_ctx
, struct dcerpc_binding
);
646 NT_STATUS_HAVE_NO_MEMORY(binding
);
648 ZERO_STRUCT(binding
->object
);
649 binding
->options
= NULL
;
650 binding
->host
= NULL
;
651 binding
->target_hostname
= NULL
;
654 binding
->transport
= dcerpc_transport_by_tower(tower
);
656 if (binding
->transport
== (unsigned int)-1) {
657 return NT_STATUS_NOT_SUPPORTED
;
660 if (tower
->num_floors
< 1) {
664 /* Set object uuid */
665 status
= dcerpc_floor_get_lhs_data(&tower
->floors
[0], &binding
->object
);
667 if (!NT_STATUS_IS_OK(status
)) {
668 DEBUG(1, ("Error pulling object uuid and version: %s", nt_errstr(status
)));
672 /* Ignore floor 1, it contains the NDR version info */
674 binding
->options
= NULL
;
677 if (tower
->num_floors
>= 4) {
678 binding
->endpoint
= dcerpc_floor_get_rhs_data(mem_ctx
, &tower
->floors
[3]);
680 binding
->endpoint
= NULL
;
683 /* Set network address */
684 if (tower
->num_floors
>= 5) {
685 binding
->host
= dcerpc_floor_get_rhs_data(mem_ctx
, &tower
->floors
[4]);
686 NT_STATUS_HAVE_NO_MEMORY(binding
->host
);
687 binding
->target_hostname
= binding
->host
;
693 NTSTATUS
dcerpc_binding_build_tower(TALLOC_CTX
*mem_ctx
, struct dcerpc_binding
*binding
, struct epm_tower
*tower
)
695 const enum epm_protocol
*protseq
= NULL
;
696 int num_protocols
= -1, i
;
700 for (i
=0;i
<ARRAY_SIZE(transports
);i
++) {
701 if (transports
[i
].transport
== binding
->transport
) {
702 protseq
= transports
[i
].protseq
;
703 num_protocols
= transports
[i
].num_protocols
;
708 if (num_protocols
== -1) {
709 DEBUG(0, ("Unable to find transport with id '%d'\n", binding
->transport
));
710 return NT_STATUS_UNSUCCESSFUL
;
713 tower
->num_floors
= 2 + num_protocols
;
714 tower
->floors
= talloc_array(mem_ctx
, struct epm_floor
, tower
->num_floors
);
717 tower
->floors
[0].lhs
.protocol
= EPM_PROTOCOL_UUID
;
719 tower
->floors
[0].lhs
.lhs_data
= dcerpc_floor_pack_lhs_data(mem_ctx
, &binding
->object
);
721 tower
->floors
[0].rhs
.uuid
.unknown
= data_blob_talloc_zero(mem_ctx
, 2);
724 tower
->floors
[1].lhs
.protocol
= EPM_PROTOCOL_UUID
;
726 tower
->floors
[1].lhs
.lhs_data
= dcerpc_floor_pack_lhs_data(mem_ctx
,
727 &ndr_transfer_syntax
);
729 tower
->floors
[1].rhs
.uuid
.unknown
= data_blob_talloc_zero(mem_ctx
, 2);
731 /* Floor 2 to num_protocols */
732 for (i
= 0; i
< num_protocols
; i
++) {
733 tower
->floors
[2 + i
].lhs
.protocol
= protseq
[i
];
734 tower
->floors
[2 + i
].lhs
.lhs_data
= data_blob_talloc(mem_ctx
, NULL
, 0);
735 ZERO_STRUCT(tower
->floors
[2 + i
].rhs
);
736 dcerpc_floor_set_rhs_data(mem_ctx
, &tower
->floors
[2 + i
], "");
739 /* The 4th floor contains the endpoint */
740 if (num_protocols
>= 2 && binding
->endpoint
) {
741 status
= dcerpc_floor_set_rhs_data(mem_ctx
, &tower
->floors
[3], binding
->endpoint
);
742 if (NT_STATUS_IS_ERR(status
)) {
747 /* The 5th contains the network address */
748 if (num_protocols
>= 3 && binding
->host
) {
749 if (is_ipaddress(binding
->host
)) {
750 status
= dcerpc_floor_set_rhs_data(mem_ctx
, &tower
->floors
[4],
753 /* note that we don't attempt to resolve the
754 name here - when we get a hostname here we
755 are in the client code, and want to put in
756 a wildcard all-zeros IP for the server to
758 status
= dcerpc_floor_set_rhs_data(mem_ctx
, &tower
->floors
[4],
761 if (NT_STATUS_IS_ERR(status
)) {
770 struct epm_map_binding_state
{
771 struct dcerpc_binding
*binding
;
772 const struct dcerpc_interface_table
*table
;
773 struct dcerpc_pipe
*pipe
;
774 struct policy_handle handle
;
776 struct epm_twr_t twr
;
777 struct epm_twr_t
*twr_r
;
782 static void continue_epm_recv_binding(struct composite_context
*ctx
);
783 static void continue_epm_map(struct rpc_request
*req
);
787 Stage 2 of epm_map_binding: Receive connected rpc pipe and send endpoint
790 static void continue_epm_recv_binding(struct composite_context
*ctx
)
792 struct rpc_request
*map_req
;
794 struct composite_context
*c
= talloc_get_type(ctx
->async
.private_data
,
795 struct composite_context
);
796 struct epm_map_binding_state
*s
= talloc_get_type(c
->private_data
,
797 struct epm_map_binding_state
);
799 /* receive result of rpc pipe connect request */
800 c
->status
= dcerpc_pipe_connect_b_recv(ctx
, c
, &s
->pipe
);
801 if (!composite_is_ok(c
)) return;
803 s
->pipe
->conn
->flags
|= DCERPC_NDR_REF_ALLOC
;
805 /* prepare requested binding parameters */
806 s
->binding
->object
= s
->table
->syntax_id
;
808 c
->status
= dcerpc_binding_build_tower(s
->pipe
, s
->binding
, &s
->twr
.tower
);
809 if (!composite_is_ok(c
)) return;
811 /* with some nice pretty paper around it of course */
812 s
->r
.in
.object
= &s
->guid
;
813 s
->r
.in
.map_tower
= &s
->twr
;
814 s
->r
.in
.entry_handle
= &s
->handle
;
815 s
->r
.in
.max_towers
= 1;
816 s
->r
.out
.entry_handle
= &s
->handle
;
818 /* send request for an endpoint mapping - a rpc request on connected pipe */
819 map_req
= dcerpc_epm_Map_send(s
->pipe
, c
, &s
->r
);
820 if (composite_nomem(map_req
, c
)) return;
822 composite_continue_rpc(c
, map_req
, continue_epm_map
, c
);
827 Stage 3 of epm_map_binding: Receive endpoint mapping and provide binding details
829 static void continue_epm_map(struct rpc_request
*req
)
831 struct composite_context
*c
= talloc_get_type(req
->async
.private,
832 struct composite_context
);
833 struct epm_map_binding_state
*s
= talloc_get_type(c
->private_data
,
834 struct epm_map_binding_state
);
836 /* receive result of a rpc request */
837 c
->status
= dcerpc_ndr_request_recv(req
);
838 if (!composite_is_ok(c
)) return;
840 /* check the details */
841 if (s
->r
.out
.result
!= 0 || *s
->r
.out
.num_towers
!= 1) {
842 composite_error(c
, NT_STATUS_PORT_UNREACHABLE
);
846 s
->twr_r
= s
->r
.out
.towers
[0].twr
;
847 if (s
->twr_r
== NULL
) {
848 composite_error(c
, NT_STATUS_PORT_UNREACHABLE
);
852 if (s
->twr_r
->tower
.num_floors
!= s
->twr
.tower
.num_floors
||
853 s
->twr_r
->tower
.floors
[3].lhs
.protocol
!= s
->twr
.tower
.floors
[3].lhs
.protocol
) {
854 composite_error(c
, NT_STATUS_PORT_UNREACHABLE
);
858 /* get received endpoint */
859 s
->binding
->endpoint
= talloc_reference(s
->binding
,
860 dcerpc_floor_get_rhs_data(c
, &s
->twr_r
->tower
.floors
[3]));
861 if (composite_nomem(s
->binding
->endpoint
, c
)) return;
868 Request for endpoint mapping of dcerpc binding - try to request for endpoint
869 unless there is default one.
871 struct composite_context
*dcerpc_epm_map_binding_send(TALLOC_CTX
*mem_ctx
,
872 struct dcerpc_binding
*binding
,
873 const struct dcerpc_interface_table
*table
,
874 struct event_context
*ev
)
876 struct composite_context
*c
;
877 struct epm_map_binding_state
*s
;
878 struct composite_context
*pipe_connect_req
;
879 struct cli_credentials
*anon_creds
;
880 struct event_context
*new_ev
= NULL
;
883 struct dcerpc_binding
*epmapper_binding
;
886 /* Try to find event context in memory context in case passed
887 * event_context (argument) was NULL. If there's none, just
891 ev
= event_context_find(mem_ctx
);
893 new_ev
= event_context_init(mem_ctx
);
894 if (new_ev
== NULL
) return NULL
;
899 /* composite context allocation and setup */
900 c
= composite_create(mem_ctx
, ev
);
905 talloc_steal(c
, new_ev
);
907 s
= talloc_zero(c
, struct epm_map_binding_state
);
908 if (composite_nomem(s
, c
)) return c
;
911 s
->binding
= binding
;
914 /* anonymous credentials for rpc connection used to get endpoint mapping */
915 anon_creds
= cli_credentials_init(mem_ctx
);
916 cli_credentials_set_conf(anon_creds
);
917 cli_credentials_set_anonymous(anon_creds
);
920 First, check if there is a default endpoint specified in the IDL
923 struct dcerpc_binding
*default_binding
;
925 /* Find one of the default pipes for this interface */
926 for (i
= 0; i
< table
->endpoints
->count
; i
++) {
927 status
= dcerpc_parse_binding(mem_ctx
, table
->endpoints
->names
[i
], &default_binding
);
929 if (NT_STATUS_IS_OK(status
)) {
930 if (default_binding
->transport
== binding
->transport
&& default_binding
->endpoint
) {
931 binding
->endpoint
= talloc_reference(binding
, default_binding
->endpoint
);
932 talloc_free(default_binding
);
938 talloc_free(default_binding
);
944 epmapper_binding
= talloc_zero(c
, struct dcerpc_binding
);
945 if (composite_nomem(epmapper_binding
, c
)) return c
;
947 /* basic endpoint mapping data */
948 epmapper_binding
->transport
= binding
->transport
;
949 epmapper_binding
->host
= talloc_reference(epmapper_binding
, binding
->host
);
950 epmapper_binding
->target_hostname
= epmapper_binding
->host
;
951 epmapper_binding
->options
= NULL
;
952 epmapper_binding
->flags
= 0;
953 epmapper_binding
->endpoint
= NULL
;
955 /* initiate rpc pipe connection */
956 pipe_connect_req
= dcerpc_pipe_connect_b_send(c
, epmapper_binding
, &dcerpc_table_epmapper
,
957 anon_creds
, c
->event_ctx
);
958 if (composite_nomem(pipe_connect_req
, c
)) return c
;
960 composite_continue(c
, pipe_connect_req
, continue_epm_recv_binding
, c
);
966 Receive result of endpoint mapping request
968 NTSTATUS
dcerpc_epm_map_binding_recv(struct composite_context
*c
)
970 NTSTATUS status
= composite_wait(c
);
978 Get endpoint mapping for rpc connection
980 NTSTATUS
dcerpc_epm_map_binding(TALLOC_CTX
*mem_ctx
, struct dcerpc_binding
*binding
,
981 const struct dcerpc_interface_table
*table
, struct event_context
*ev
)
983 struct composite_context
*c
;
985 c
= dcerpc_epm_map_binding_send(mem_ctx
, binding
, table
, ev
);
986 return dcerpc_epm_map_binding_recv(c
);
990 struct pipe_auth_state
{
991 struct dcerpc_pipe
*pipe
;
992 struct dcerpc_binding
*binding
;
993 const struct dcerpc_interface_table
*table
;
994 struct cli_credentials
*credentials
;
998 static void continue_auth_schannel(struct composite_context
*ctx
);
999 static void continue_auth(struct composite_context
*ctx
);
1000 static void continue_auth_none(struct composite_context
*ctx
);
1001 static void continue_ntlmssp_connection(struct composite_context
*ctx
);
1002 static void continue_spnego_after_wrong_pass(struct composite_context
*ctx
);
1006 Stage 2 of pipe_auth: Receive result of schannel bind request
1008 static void continue_auth_schannel(struct composite_context
*ctx
)
1010 struct composite_context
*c
= talloc_get_type(ctx
->async
.private_data
,
1011 struct composite_context
);
1013 c
->status
= dcerpc_bind_auth_schannel_recv(ctx
);
1014 if (!composite_is_ok(c
)) return;
1021 Stage 2 of pipe_auth: Receive result of authenticated bind request
1023 static void continue_auth(struct composite_context
*ctx
)
1025 struct composite_context
*c
= talloc_get_type(ctx
->async
.private_data
,
1026 struct composite_context
);
1028 c
->status
= dcerpc_bind_auth_recv(ctx
);
1029 if (!composite_is_ok(c
)) return;
1034 Stage 2 of pipe_auth: Receive result of authenticated bind request, but handle fallbacks:
1037 static void continue_auth_auto(struct composite_context
*ctx
)
1039 struct composite_context
*c
= talloc_get_type(ctx
->async
.private_data
,
1040 struct composite_context
);
1041 struct pipe_auth_state
*s
= talloc_get_type(c
->private_data
, struct pipe_auth_state
);
1042 struct composite_context
*sec_conn_req
;
1044 c
->status
= dcerpc_bind_auth_recv(ctx
);
1045 if (NT_STATUS_EQUAL(c
->status
, NT_STATUS_INVALID_PARAMETER
)) {
1047 * Retry with NTLMSSP auth as fallback
1048 * send a request for secondary rpc connection
1050 sec_conn_req
= dcerpc_secondary_connection_send(s
->pipe
,
1052 composite_continue(c
, sec_conn_req
, continue_ntlmssp_connection
, c
);
1054 } else if (NT_STATUS_EQUAL(c
->status
, NT_STATUS_LOGON_FAILURE
)) {
1055 if (cli_credentials_wrong_password(s
->credentials
)) {
1057 * Retry SPNEGO with a better password
1058 * send a request for secondary rpc connection
1060 sec_conn_req
= dcerpc_secondary_connection_send(s
->pipe
,
1062 composite_continue(c
, sec_conn_req
, continue_spnego_after_wrong_pass
, c
);
1067 if (!composite_is_ok(c
)) return;
1073 Stage 3 of pipe_auth (fallback to NTLMSSP case): Receive secondary
1074 rpc connection (the first one can't be used any more, due to the
1075 bind nak) and perform authenticated bind request
1077 static void continue_ntlmssp_connection(struct composite_context
*ctx
)
1079 struct composite_context
*c
;
1080 struct pipe_auth_state
*s
;
1081 struct composite_context
*auth_req
;
1082 struct dcerpc_pipe
*p2
;
1084 c
= talloc_get_type(ctx
->async
.private_data
, struct composite_context
);
1085 s
= talloc_get_type(c
->private_data
, struct pipe_auth_state
);
1087 /* receive secondary rpc connection */
1088 c
->status
= dcerpc_secondary_connection_recv(ctx
, &p2
);
1089 if (!composite_is_ok(c
)) return;
1091 talloc_steal(s
, p2
);
1092 talloc_steal(p2
, s
->pipe
);
1095 /* initiate a authenticated bind */
1096 auth_req
= dcerpc_bind_auth_send(c
, s
->pipe
, s
->table
,
1097 s
->credentials
, DCERPC_AUTH_TYPE_NTLMSSP
,
1098 dcerpc_auth_level(s
->pipe
->conn
),
1099 s
->table
->authservices
->names
[0]);
1100 composite_continue(c
, auth_req
, continue_auth
, c
);
1104 Stage 3 of pipe_auth (retry on wrong password): Receive secondary
1105 rpc connection (the first one can't be used any more, due to the
1106 bind nak) and perform authenticated bind request
1108 static void continue_spnego_after_wrong_pass(struct composite_context
*ctx
)
1110 struct composite_context
*c
;
1111 struct pipe_auth_state
*s
;
1112 struct composite_context
*auth_req
;
1113 struct dcerpc_pipe
*p2
;
1115 c
= talloc_get_type(ctx
->async
.private_data
, struct composite_context
);
1116 s
= talloc_get_type(c
->private_data
, struct pipe_auth_state
);
1118 /* receive secondary rpc connection */
1119 c
->status
= dcerpc_secondary_connection_recv(ctx
, &p2
);
1120 if (!composite_is_ok(c
)) return;
1122 talloc_steal(s
, p2
);
1123 talloc_steal(p2
, s
->pipe
);
1126 /* initiate a authenticated bind */
1127 auth_req
= dcerpc_bind_auth_send(c
, s
->pipe
, s
->table
,
1128 s
->credentials
, DCERPC_AUTH_TYPE_SPNEGO
,
1129 dcerpc_auth_level(s
->pipe
->conn
),
1130 s
->table
->authservices
->names
[0]);
1131 composite_continue(c
, auth_req
, continue_auth
, c
);
1136 Stage 2 of pipe_auth: Receive result of non-authenticated bind request
1138 static void continue_auth_none(struct composite_context
*ctx
)
1140 struct composite_context
*c
= talloc_get_type(ctx
->async
.private_data
,
1141 struct composite_context
);
1143 c
->status
= dcerpc_bind_auth_none_recv(ctx
);
1144 if (!composite_is_ok(c
)) return;
1151 Request to perform an authenticated bind if required. Authentication
1152 is determined using credentials passed and binding flags.
1154 struct composite_context
*dcerpc_pipe_auth_send(struct dcerpc_pipe
*p
,
1155 struct dcerpc_binding
*binding
,
1156 const struct dcerpc_interface_table
*table
,
1157 struct cli_credentials
*credentials
)
1159 struct composite_context
*c
;
1160 struct pipe_auth_state
*s
;
1161 struct composite_context
*auth_schannel_req
;
1162 struct composite_context
*auth_req
;
1163 struct composite_context
*auth_none_req
;
1164 struct dcerpc_connection
*conn
;
1167 /* composite context allocation and setup */
1168 c
= composite_create(p
, p
->conn
->event_ctx
);
1169 if (c
== NULL
) return NULL
;
1171 s
= talloc_zero(c
, struct pipe_auth_state
);
1172 if (composite_nomem(s
, c
)) return c
;
1173 c
->private_data
= s
;
1175 /* store parameters in state structure */
1176 s
->binding
= binding
;
1178 s
->credentials
= credentials
;
1181 conn
= s
->pipe
->conn
;
1182 conn
->flags
= binding
->flags
;
1184 /* remember the binding string for possible secondary connections */
1185 conn
->binding_string
= dcerpc_binding_string(p
, binding
);
1187 if (cli_credentials_is_anonymous(s
->credentials
)) {
1188 auth_none_req
= dcerpc_bind_auth_none_send(c
, s
->pipe
, s
->table
);
1189 composite_continue(c
, auth_none_req
, continue_auth_none
, c
);
1193 if ((binding
->flags
& DCERPC_SCHANNEL
) &&
1194 !cli_credentials_get_netlogon_creds(s
->credentials
)) {
1195 /* If we don't already have netlogon credentials for
1196 * the schannel bind, then we have to get these
1198 auth_schannel_req
= dcerpc_bind_auth_schannel_send(c
, s
->pipe
, s
->table
,
1200 dcerpc_auth_level(conn
));
1201 composite_continue(c
, auth_schannel_req
, continue_auth_schannel
, c
);
1206 * we rely on the already authenticated CIFS connection
1207 * if not doing sign or seal
1209 if (conn
->transport
.transport
== NCACN_NP
&&
1210 !(s
->binding
->flags
& (DCERPC_SIGN
|DCERPC_SEAL
))) {
1211 auth_none_req
= dcerpc_bind_auth_none_send(c
, s
->pipe
, s
->table
);
1212 composite_continue(c
, auth_none_req
, continue_auth_none
, c
);
1217 /* Perform an authenticated DCE-RPC bind
1219 if (!(conn
->flags
& (DCERPC_SIGN
|DCERPC_SEAL
))) {
1221 we are doing an authenticated connection,
1222 but not using sign or seal. We must force
1223 the CONNECT dcerpc auth type as a NONE auth
1224 type doesn't allow authentication
1225 information to be passed.
1227 conn
->flags
|= DCERPC_CONNECT
;
1230 if (s
->binding
->flags
& DCERPC_AUTH_SPNEGO
) {
1231 auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
1233 } else if (s
->binding
->flags
& DCERPC_AUTH_KRB5
) {
1234 auth_type
= DCERPC_AUTH_TYPE_KRB5
;
1236 } else if (s
->binding
->flags
& DCERPC_SCHANNEL
) {
1237 auth_type
= DCERPC_AUTH_TYPE_SCHANNEL
;
1239 } else if (s
->binding
->flags
& DCERPC_AUTH_NTLM
) {
1240 auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
1243 /* try SPNEGO with fallback to NTLMSSP */
1244 auth_req
= dcerpc_bind_auth_send(c
, s
->pipe
, s
->table
,
1245 s
->credentials
, DCERPC_AUTH_TYPE_SPNEGO
,
1246 dcerpc_auth_level(conn
),
1247 s
->table
->authservices
->names
[0]);
1248 composite_continue(c
, auth_req
, continue_auth_auto
, c
);
1252 auth_req
= dcerpc_bind_auth_send(c
, s
->pipe
, s
->table
,
1253 s
->credentials
, auth_type
,
1254 dcerpc_auth_level(conn
),
1255 s
->table
->authservices
->names
[0]);
1256 composite_continue(c
, auth_req
, continue_auth
, c
);
1262 Receive result of authenticated bind request on dcerpc pipe
1264 This returns *p, which may be different to the one originally
1265 supllied, as it rebinds to a new pipe due to authentication fallback
1268 NTSTATUS
dcerpc_pipe_auth_recv(struct composite_context
*c
, TALLOC_CTX
*mem_ctx
,
1269 struct dcerpc_pipe
**p
)
1273 struct pipe_auth_state
*s
= talloc_get_type(c
->private_data
,
1274 struct pipe_auth_state
);
1275 status
= composite_wait(c
);
1276 if (!NT_STATUS_IS_OK(status
)) {
1277 char *uuid_str
= GUID_string(s
->pipe
, &s
->table
->syntax_id
.uuid
);
1278 DEBUG(0, ("Failed to bind to uuid %s - %s\n", uuid_str
, nt_errstr(status
)));
1279 talloc_free(uuid_str
);
1281 talloc_steal(mem_ctx
, s
->pipe
);
1291 Perform an authenticated bind if needed - sync version
1293 This may change *p, as it rebinds to a new pipe due to authentication fallback
1295 NTSTATUS
dcerpc_pipe_auth(TALLOC_CTX
*mem_ctx
,
1296 struct dcerpc_pipe
**p
,
1297 struct dcerpc_binding
*binding
,
1298 const struct dcerpc_interface_table
*table
,
1299 struct cli_credentials
*credentials
)
1301 struct composite_context
*c
;
1303 c
= dcerpc_pipe_auth_send(*p
, binding
, table
, credentials
);
1304 return dcerpc_pipe_auth_recv(c
, mem_ctx
, p
);
1308 NTSTATUS
dcerpc_generic_session_key(struct dcerpc_connection
*c
,
1309 DATA_BLOB
*session_key
)
1311 /* this took quite a few CPU cycles to find ... */
1312 session_key
->data
= discard_const_p(unsigned char, "SystemLibraryDTC");
1313 session_key
->length
= 16;
1314 return NT_STATUS_OK
;
1318 fetch the user session key - may be default (above) or the SMB session key
1320 NTSTATUS
dcerpc_fetch_session_key(struct dcerpc_pipe
*p
,
1321 DATA_BLOB
*session_key
)
1323 return p
->conn
->security_state
.session_key(p
->conn
, session_key
);
1328 log a rpc packet in a format suitable for ndrdump. This is especially useful
1329 for sealed packets, where ethereal cannot easily see the contents
1331 this triggers on a debug level of >= 10
1333 void dcerpc_log_packet(const struct dcerpc_interface_table
*ndr
,
1334 uint32_t opnum
, uint32_t flags
, DATA_BLOB
*pkt
)
1336 const int num_examples
= 20;
1339 if (DEBUGLEVEL
< 10) return;
1341 for (i
=0;i
<num_examples
;i
++) {
1343 asprintf(&name
, "%s/rpclog/%s-%u.%d.%s",
1344 lp_lockdir(), ndr
->name
, opnum
, i
,
1345 (flags
&NDR_IN
)?"in":"out");
1349 if (!file_exist(name
)) {
1350 if (file_save(name
, pkt
->data
, pkt
->length
)) {
1351 DEBUG(10,("Logged rpc packet to %s\n", name
));
1363 create a secondary context from a primary connection
1365 this uses dcerpc_alter_context() to create a new dcerpc context_id
1367 NTSTATUS
dcerpc_secondary_context(struct dcerpc_pipe
*p
,
1368 struct dcerpc_pipe
**pp2
,
1369 const struct dcerpc_interface_table
*table
)
1372 struct dcerpc_pipe
*p2
;
1374 p2
= talloc_zero(p
, struct dcerpc_pipe
);
1376 return NT_STATUS_NO_MEMORY
;
1378 p2
->conn
= talloc_reference(p2
, p
->conn
);
1379 p2
->request_timeout
= p
->request_timeout
;
1381 p2
->context_id
= ++p
->conn
->next_context_id
;
1383 p2
->syntax
= table
->syntax_id
;
1385 p2
->transfer_syntax
= ndr_transfer_syntax
;
1387 status
= dcerpc_alter_context(p2
, p2
, &p2
->syntax
, &p2
->transfer_syntax
);
1388 if (!NT_STATUS_IS_OK(status
)) {