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 3 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, see <http://www.gnu.org/licenses/>.
26 #include "lib/events/events.h"
27 #include "libcli/composite/composite.h"
28 #include "librpc/gen_ndr/ndr_epmapper_c.h"
29 #include "librpc/gen_ndr/ndr_dcerpc.h"
30 #include "librpc/gen_ndr/ndr_misc.h"
31 #include "librpc/rpc/dcerpc_proto.h"
32 #include "auth/credentials/credentials.h"
33 #include "param/param.h"
36 find a dcerpc call on an interface by name
38 const struct ndr_interface_call *dcerpc_iface_find_call(const struct ndr_interface_table *iface,
42 for (i=0;i<iface->num_calls;i++) {
43 if (strcmp(iface->calls[i].name, name) == 0) {
44 return &iface->calls[i];
51 push a ncacn_packet into a blob, potentially with auth info
53 NTSTATUS ncacn_push_auth(DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
54 struct smb_iconv_convenience *iconv_convenience,
55 struct ncacn_packet *pkt,
56 struct dcerpc_auth *auth_info)
59 enum ndr_err_code ndr_err;
61 ndr = ndr_push_init_ctx(mem_ctx, iconv_convenience);
63 return NT_STATUS_NO_MEMORY;
66 if (!(pkt->drep[0] & DCERPC_DREP_LE)) {
67 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
70 if (pkt->pfc_flags & DCERPC_PFC_FLAG_OBJECT_UUID) {
71 ndr->flags |= LIBNDR_FLAG_OBJECT_PRESENT;
75 pkt->auth_length = auth_info->credentials.length;
80 ndr_err = ndr_push_ncacn_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
81 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
82 return ndr_map_error2ntstatus(ndr_err);
86 ndr_err = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, auth_info);
87 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
88 return ndr_map_error2ntstatus(ndr_err);
92 *blob = ndr_push_blob(ndr);
94 /* fill in the frag length */
95 dcerpc_set_frag_length(blob, blob->length);
101 struct epm_map_binding_state {
102 struct dcerpc_binding *binding;
103 const struct ndr_interface_table *table;
104 struct dcerpc_pipe *pipe;
105 struct policy_handle handle;
107 struct epm_twr_t twr;
108 struct epm_twr_t *twr_r;
113 static void continue_epm_recv_binding(struct composite_context *ctx);
114 static void continue_epm_map(struct rpc_request *req);
118 Stage 2 of epm_map_binding: Receive connected rpc pipe and send endpoint
121 static void continue_epm_recv_binding(struct composite_context *ctx)
123 struct rpc_request *map_req;
125 struct composite_context *c = talloc_get_type(ctx->async.private_data,
126 struct composite_context);
127 struct epm_map_binding_state *s = talloc_get_type(c->private_data,
128 struct epm_map_binding_state);
130 /* receive result of rpc pipe connect request */
131 c->status = dcerpc_pipe_connect_b_recv(ctx, c, &s->pipe);
132 if (!composite_is_ok(c)) return;
134 s->pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;
136 /* prepare requested binding parameters */
137 s->binding->object = s->table->syntax_id;
139 c->status = dcerpc_binding_build_tower(s->pipe, s->binding, &s->twr.tower);
140 if (!composite_is_ok(c)) return;
142 /* with some nice pretty paper around it of course */
143 s->r.in.object = &s->guid;
144 s->r.in.map_tower = &s->twr;
145 s->r.in.entry_handle = &s->handle;
146 s->r.in.max_towers = 1;
147 s->r.out.entry_handle = &s->handle;
149 /* send request for an endpoint mapping - a rpc request on connected pipe */
150 map_req = dcerpc_epm_Map_send(s->pipe, c, &s->r);
151 if (composite_nomem(map_req, c)) return;
153 composite_continue_rpc(c, map_req, continue_epm_map, c);
158 Stage 3 of epm_map_binding: Receive endpoint mapping and provide binding details
160 static void continue_epm_map(struct rpc_request *req)
162 struct composite_context *c = talloc_get_type(req->async.private_data,
163 struct composite_context);
164 struct epm_map_binding_state *s = talloc_get_type(c->private_data,
165 struct epm_map_binding_state);
167 /* receive result of a rpc request */
168 c->status = dcerpc_ndr_request_recv(req);
169 if (!composite_is_ok(c)) return;
171 /* check the details */
172 if (s->r.out.result != 0 || *s->r.out.num_towers != 1) {
173 composite_error(c, NT_STATUS_PORT_UNREACHABLE);
177 s->twr_r = s->r.out.towers[0].twr;
178 if (s->twr_r == NULL) {
179 composite_error(c, NT_STATUS_PORT_UNREACHABLE);
183 if (s->twr_r->tower.num_floors != s->twr.tower.num_floors ||
184 s->twr_r->tower.floors[3].lhs.protocol != s->twr.tower.floors[3].lhs.protocol) {
185 composite_error(c, NT_STATUS_PORT_UNREACHABLE);
189 /* get received endpoint */
190 s->binding->endpoint = talloc_reference(s->binding,
191 dcerpc_floor_get_rhs_data(c, &s->twr_r->tower.floors[3]));
192 if (composite_nomem(s->binding->endpoint, c)) return;
199 Request for endpoint mapping of dcerpc binding - try to request for endpoint
200 unless there is default one.
202 struct composite_context *dcerpc_epm_map_binding_send(TALLOC_CTX *mem_ctx,
203 struct dcerpc_binding *binding,
204 const struct ndr_interface_table *table,
205 struct event_context *ev,
206 struct loadparm_context *lp_ctx)
208 struct composite_context *c;
209 struct epm_map_binding_state *s;
210 struct composite_context *pipe_connect_req;
211 struct cli_credentials *anon_creds;
212 struct event_context *new_ev = NULL;
215 struct dcerpc_binding *epmapper_binding;
218 /* Try to find event context in memory context in case passed
219 * event_context (argument) was NULL. If there's none, just
223 ev = event_context_find(mem_ctx);
225 new_ev = event_context_init(mem_ctx);
226 if (new_ev == NULL) return NULL;
231 /* composite context allocation and setup */
232 c = composite_create(mem_ctx, ev);
237 talloc_steal(c, new_ev);
239 s = talloc_zero(c, struct epm_map_binding_state);
240 if (composite_nomem(s, c)) return c;
243 s->binding = binding;
246 /* anonymous credentials for rpc connection used to get endpoint mapping */
247 anon_creds = cli_credentials_init(mem_ctx);
248 cli_credentials_set_anonymous(anon_creds);
251 First, check if there is a default endpoint specified in the IDL
254 struct dcerpc_binding *default_binding;
256 /* Find one of the default pipes for this interface */
257 for (i = 0; i < table->endpoints->count; i++) {
258 status = dcerpc_parse_binding(mem_ctx, table->endpoints->names[i], &default_binding);
260 if (NT_STATUS_IS_OK(status)) {
261 if (binding->transport == NCA_UNKNOWN)
262 binding->transport = default_binding->transport;
263 if (default_binding->transport == binding->transport &&
264 default_binding->endpoint) {
265 binding->endpoint = talloc_reference(binding, default_binding->endpoint);
266 talloc_free(default_binding);
272 talloc_free(default_binding);
278 epmapper_binding = talloc_zero(c, struct dcerpc_binding);
279 if (composite_nomem(epmapper_binding, c)) return c;
281 /* basic endpoint mapping data */
282 epmapper_binding->transport = binding->transport;
283 epmapper_binding->host = talloc_reference(epmapper_binding, binding->host);
284 epmapper_binding->target_hostname = epmapper_binding->host;
285 epmapper_binding->options = NULL;
286 epmapper_binding->flags = 0;
287 epmapper_binding->assoc_group_id = 0;
288 epmapper_binding->endpoint = NULL;
290 /* initiate rpc pipe connection */
291 pipe_connect_req = dcerpc_pipe_connect_b_send(c, epmapper_binding,
293 anon_creds, c->event_ctx,
295 if (composite_nomem(pipe_connect_req, c)) return c;
297 composite_continue(c, pipe_connect_req, continue_epm_recv_binding, c);
303 Receive result of endpoint mapping request
305 NTSTATUS dcerpc_epm_map_binding_recv(struct composite_context *c)
307 NTSTATUS status = composite_wait(c);
315 Get endpoint mapping for rpc connection
317 _PUBLIC_ NTSTATUS dcerpc_epm_map_binding(TALLOC_CTX *mem_ctx, struct dcerpc_binding *binding,
318 const struct ndr_interface_table *table, struct event_context *ev,
319 struct loadparm_context *lp_ctx)
321 struct composite_context *c;
323 c = dcerpc_epm_map_binding_send(mem_ctx, binding, table, ev, lp_ctx);
324 return dcerpc_epm_map_binding_recv(c);
328 struct pipe_auth_state {
329 struct dcerpc_pipe *pipe;
330 struct dcerpc_binding *binding;
331 const struct ndr_interface_table *table;
332 struct loadparm_context *lp_ctx;
333 struct cli_credentials *credentials;
337 static void continue_auth_schannel(struct composite_context *ctx);
338 static void continue_auth(struct composite_context *ctx);
339 static void continue_auth_none(struct composite_context *ctx);
340 static void continue_ntlmssp_connection(struct composite_context *ctx);
341 static void continue_spnego_after_wrong_pass(struct composite_context *ctx);
345 Stage 2 of pipe_auth: Receive result of schannel bind request
347 static void continue_auth_schannel(struct composite_context *ctx)
349 struct composite_context *c = talloc_get_type(ctx->async.private_data,
350 struct composite_context);
352 c->status = dcerpc_bind_auth_schannel_recv(ctx);
353 if (!composite_is_ok(c)) return;
360 Stage 2 of pipe_auth: Receive result of authenticated bind request
362 static void continue_auth(struct composite_context *ctx)
364 struct composite_context *c = talloc_get_type(ctx->async.private_data,
365 struct composite_context);
367 c->status = dcerpc_bind_auth_recv(ctx);
368 if (!composite_is_ok(c)) return;
373 Stage 2 of pipe_auth: Receive result of authenticated bind request, but handle fallbacks:
376 static void continue_auth_auto(struct composite_context *ctx)
378 struct composite_context *c = talloc_get_type(ctx->async.private_data,
379 struct composite_context);
380 struct pipe_auth_state *s = talloc_get_type(c->private_data, struct pipe_auth_state);
381 struct composite_context *sec_conn_req;
383 c->status = dcerpc_bind_auth_recv(ctx);
384 if (NT_STATUS_EQUAL(c->status, NT_STATUS_INVALID_PARAMETER)) {
386 * Retry with NTLMSSP auth as fallback
387 * send a request for secondary rpc connection
389 sec_conn_req = dcerpc_secondary_connection_send(s->pipe,
391 composite_continue(c, sec_conn_req, continue_ntlmssp_connection, c);
393 } else if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
394 if (cli_credentials_wrong_password(s->credentials)) {
396 * Retry SPNEGO with a better password
397 * send a request for secondary rpc connection
399 sec_conn_req = dcerpc_secondary_connection_send(s->pipe,
401 composite_continue(c, sec_conn_req, continue_spnego_after_wrong_pass, c);
406 if (!composite_is_ok(c)) return;
412 Stage 3 of pipe_auth (fallback to NTLMSSP case): Receive secondary
413 rpc connection (the first one can't be used any more, due to the
414 bind nak) and perform authenticated bind request
416 static void continue_ntlmssp_connection(struct composite_context *ctx)
418 struct composite_context *c;
419 struct pipe_auth_state *s;
420 struct composite_context *auth_req;
421 struct dcerpc_pipe *p2;
423 c = talloc_get_type(ctx->async.private_data, struct composite_context);
424 s = talloc_get_type(c->private_data, struct pipe_auth_state);
426 /* receive secondary rpc connection */
427 c->status = dcerpc_secondary_connection_recv(ctx, &p2);
428 if (!composite_is_ok(c)) return;
431 talloc_steal(p2, s->pipe);
434 /* initiate a authenticated bind */
435 auth_req = dcerpc_bind_auth_send(c, s->pipe, s->table,
436 s->credentials, s->lp_ctx,
437 DCERPC_AUTH_TYPE_NTLMSSP,
438 dcerpc_auth_level(s->pipe->conn),
439 s->table->authservices->names[0]);
440 composite_continue(c, auth_req, continue_auth, c);
444 Stage 3 of pipe_auth (retry on wrong password): Receive secondary
445 rpc connection (the first one can't be used any more, due to the
446 bind nak) and perform authenticated bind request
448 static void continue_spnego_after_wrong_pass(struct composite_context *ctx)
450 struct composite_context *c;
451 struct pipe_auth_state *s;
452 struct composite_context *auth_req;
453 struct dcerpc_pipe *p2;
455 c = talloc_get_type(ctx->async.private_data, struct composite_context);
456 s = talloc_get_type(c->private_data, struct pipe_auth_state);
458 /* receive secondary rpc connection */
459 c->status = dcerpc_secondary_connection_recv(ctx, &p2);
460 if (!composite_is_ok(c)) return;
463 talloc_steal(p2, s->pipe);
466 /* initiate a authenticated bind */
467 auth_req = dcerpc_bind_auth_send(c, s->pipe, s->table,
468 s->credentials, s->lp_ctx, DCERPC_AUTH_TYPE_SPNEGO,
469 dcerpc_auth_level(s->pipe->conn),
470 s->table->authservices->names[0]);
471 composite_continue(c, auth_req, continue_auth, c);
476 Stage 2 of pipe_auth: Receive result of non-authenticated bind request
478 static void continue_auth_none(struct composite_context *ctx)
480 struct composite_context *c = talloc_get_type(ctx->async.private_data,
481 struct composite_context);
483 c->status = dcerpc_bind_auth_none_recv(ctx);
484 if (!composite_is_ok(c)) return;
491 Request to perform an authenticated bind if required. Authentication
492 is determined using credentials passed and binding flags.
494 struct composite_context *dcerpc_pipe_auth_send(struct dcerpc_pipe *p,
495 struct dcerpc_binding *binding,
496 const struct ndr_interface_table *table,
497 struct cli_credentials *credentials,
498 struct loadparm_context *lp_ctx)
500 struct composite_context *c;
501 struct pipe_auth_state *s;
502 struct composite_context *auth_schannel_req;
503 struct composite_context *auth_req;
504 struct composite_context *auth_none_req;
505 struct dcerpc_connection *conn;
508 /* composite context allocation and setup */
509 c = composite_create(p, p->conn->event_ctx);
510 if (c == NULL) return NULL;
512 s = talloc_zero(c, struct pipe_auth_state);
513 if (composite_nomem(s, c)) return c;
516 /* store parameters in state structure */
517 s->binding = binding;
519 s->credentials = credentials;
523 conn = s->pipe->conn;
524 conn->flags = binding->flags;
526 /* remember the binding string for possible secondary connections */
527 conn->binding_string = dcerpc_binding_string(p, binding);
529 if (cli_credentials_is_anonymous(s->credentials)) {
530 auth_none_req = dcerpc_bind_auth_none_send(c, s->pipe, s->table);
531 composite_continue(c, auth_none_req, continue_auth_none, c);
535 if ((binding->flags & DCERPC_SCHANNEL) &&
536 !cli_credentials_get_netlogon_creds(s->credentials)) {
537 /* If we don't already have netlogon credentials for
538 * the schannel bind, then we have to get these
540 auth_schannel_req = dcerpc_bind_auth_schannel_send(c, s->pipe, s->table,
541 s->credentials, s->lp_ctx,
542 dcerpc_auth_level(conn));
543 composite_continue(c, auth_schannel_req, continue_auth_schannel, c);
548 * we rely on the already authenticated CIFS connection
549 * if not doing sign or seal
551 if (conn->transport.transport == NCACN_NP &&
552 !(s->binding->flags & (DCERPC_SIGN|DCERPC_SEAL))) {
553 auth_none_req = dcerpc_bind_auth_none_send(c, s->pipe, s->table);
554 composite_continue(c, auth_none_req, continue_auth_none, c);
559 /* Perform an authenticated DCE-RPC bind
561 if (!(conn->flags & (DCERPC_SIGN|DCERPC_SEAL))) {
563 we are doing an authenticated connection,
564 but not using sign or seal. We must force
565 the CONNECT dcerpc auth type as a NONE auth
566 type doesn't allow authentication
567 information to be passed.
569 conn->flags |= DCERPC_CONNECT;
572 if (s->binding->flags & DCERPC_AUTH_SPNEGO) {
573 auth_type = DCERPC_AUTH_TYPE_SPNEGO;
575 } else if (s->binding->flags & DCERPC_AUTH_KRB5) {
576 auth_type = DCERPC_AUTH_TYPE_KRB5;
578 } else if (s->binding->flags & DCERPC_SCHANNEL) {
579 auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
581 } else if (s->binding->flags & DCERPC_AUTH_NTLM) {
582 auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
585 /* try SPNEGO with fallback to NTLMSSP */
586 auth_req = dcerpc_bind_auth_send(c, s->pipe, s->table,
587 s->credentials, s->lp_ctx, DCERPC_AUTH_TYPE_SPNEGO,
588 dcerpc_auth_level(conn),
589 s->table->authservices->names[0]);
590 composite_continue(c, auth_req, continue_auth_auto, c);
594 auth_req = dcerpc_bind_auth_send(c, s->pipe, s->table,
595 s->credentials, s->lp_ctx, auth_type,
596 dcerpc_auth_level(conn),
597 s->table->authservices->names[0]);
598 composite_continue(c, auth_req, continue_auth, c);
604 Receive result of authenticated bind request on dcerpc pipe
606 This returns *p, which may be different to the one originally
607 supllied, as it rebinds to a new pipe due to authentication fallback
610 NTSTATUS dcerpc_pipe_auth_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
611 struct dcerpc_pipe **p)
615 struct pipe_auth_state *s = talloc_get_type(c->private_data,
616 struct pipe_auth_state);
617 status = composite_wait(c);
618 if (!NT_STATUS_IS_OK(status)) {
619 char *uuid_str = GUID_string(s->pipe, &s->table->syntax_id.uuid);
620 DEBUG(0, ("Failed to bind to uuid %s - %s\n", uuid_str, nt_errstr(status)));
621 talloc_free(uuid_str);
623 talloc_steal(mem_ctx, s->pipe);
633 Perform an authenticated bind if needed - sync version
635 This may change *p, as it rebinds to a new pipe due to authentication fallback
637 _PUBLIC_ NTSTATUS dcerpc_pipe_auth(TALLOC_CTX *mem_ctx,
638 struct dcerpc_pipe **p,
639 struct dcerpc_binding *binding,
640 const struct ndr_interface_table *table,
641 struct cli_credentials *credentials,
642 struct loadparm_context *lp_ctx)
644 struct composite_context *c;
646 c = dcerpc_pipe_auth_send(*p, binding, table, credentials, lp_ctx);
647 return dcerpc_pipe_auth_recv(c, mem_ctx, p);
651 NTSTATUS dcerpc_generic_session_key(struct dcerpc_connection *c,
652 DATA_BLOB *session_key)
654 /* this took quite a few CPU cycles to find ... */
655 session_key->data = discard_const_p(unsigned char, "SystemLibraryDTC");
656 session_key->length = 16;
661 fetch the user session key - may be default (above) or the SMB session key
663 _PUBLIC_ NTSTATUS dcerpc_fetch_session_key(struct dcerpc_pipe *p,
664 DATA_BLOB *session_key)
666 return p->conn->security_state.session_key(p->conn, session_key);
671 log a rpc packet in a format suitable for ndrdump. This is especially useful
672 for sealed packets, where ethereal cannot easily see the contents
674 this triggers on a debug level of >= 10
676 _PUBLIC_ void dcerpc_log_packet(const struct ndr_interface_table *ndr,
677 uint32_t opnum, uint32_t flags,
680 const int num_examples = 20;
683 if (DEBUGLEVEL < 10) return;
685 for (i=0;i<num_examples;i++) {
687 asprintf(&name, "%s/rpclog/%s-%u.%d.%s",
688 lp_lockdir(global_loadparm), ndr->name, opnum, i,
689 (flags&NDR_IN)?"in":"out");
693 if (!file_exist(name)) {
694 if (file_save(name, pkt->data, pkt->length)) {
695 DEBUG(10,("Logged rpc packet to %s\n", name));
707 create a secondary context from a primary connection
709 this uses dcerpc_alter_context() to create a new dcerpc context_id
711 _PUBLIC_ NTSTATUS dcerpc_secondary_context(struct dcerpc_pipe *p,
712 struct dcerpc_pipe **pp2,
713 const struct ndr_interface_table *table)
716 struct dcerpc_pipe *p2;
718 p2 = talloc_zero(p, struct dcerpc_pipe);
720 return NT_STATUS_NO_MEMORY;
722 p2->conn = talloc_reference(p2, p->conn);
723 p2->request_timeout = p->request_timeout;
725 p2->context_id = ++p->conn->next_context_id;
727 p2->syntax = table->syntax_id;
729 p2->transfer_syntax = ndr_transfer_syntax;
731 p2->binding = talloc_reference(p2, p->binding);
733 status = dcerpc_alter_context(p2, p2, &p2->syntax, &p2->transfer_syntax);
734 if (!NT_STATUS_IS_OK(status)) {