2 Unix SMB/CIFS implementation.
4 dcerpc over standard sockets transport
6 Copyright (C) Andrew Tridgell 2003
7 Copyright (C) Jelmer Vernooij 2004
8 Copyright (C) Rafal Szczesniak 2006
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 3 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, see <http://www.gnu.org/licenses/>.
25 #include "lib/events/events.h"
26 #include "lib/socket/socket.h"
27 #include "lib/stream/packet.h"
28 #include "libcli/composite/composite.h"
29 #include "librpc/rpc/dcerpc.h"
30 #include "librpc/rpc/dcerpc_proto.h"
31 #include "libcli/resolve/resolve.h"
33 /* transport private information used by general socket pipe transports */
35 struct tevent_fd
*fde
;
36 struct socket_context
*sock
;
39 struct packet_context
*packet
;
40 uint32_t pending_reads
;
42 const char *path
; /* For ncacn_unix_sock and ncalrpc */
49 static void sock_dead(struct dcerpc_connection
*p
, NTSTATUS status
)
51 struct sock_private
*sock
= (struct sock_private
*)p
->transport
.private_data
;
56 packet_recv_disable(sock
->packet
);
57 packet_set_fde(sock
->packet
, NULL
);
58 packet_set_socket(sock
->packet
, NULL
);
62 talloc_free(sock
->fde
);
67 talloc_free(sock
->sock
);
71 if (NT_STATUS_EQUAL(NT_STATUS_UNSUCCESSFUL
, status
)) {
72 status
= NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
75 if (NT_STATUS_EQUAL(NT_STATUS_OK
, status
)) {
76 status
= NT_STATUS_END_OF_FILE
;
79 if (p
->transport
.recv_data
) {
80 p
->transport
.recv_data(p
, NULL
, status
);
86 handle socket recv errors
88 static void sock_error_handler(void *private_data
, NTSTATUS status
)
90 struct dcerpc_connection
*p
= talloc_get_type(private_data
,
91 struct dcerpc_connection
);
96 check if a blob is a complete packet
98 static NTSTATUS
sock_complete_packet(void *private_data
, DATA_BLOB blob
, size_t *size
)
100 if (blob
.length
< DCERPC_FRAG_LEN_OFFSET
+2) {
101 return STATUS_MORE_ENTRIES
;
103 *size
= dcerpc_get_frag_length(&blob
);
104 if (*size
> blob
.length
) {
105 return STATUS_MORE_ENTRIES
;
111 process recv requests
113 static NTSTATUS
sock_process_recv(void *private_data
, DATA_BLOB blob
)
115 struct dcerpc_connection
*p
= talloc_get_type(private_data
,
116 struct dcerpc_connection
);
117 struct sock_private
*sock
= (struct sock_private
*)p
->transport
.private_data
;
118 sock
->pending_reads
--;
119 if (sock
->pending_reads
== 0) {
120 packet_recv_disable(sock
->packet
);
122 p
->transport
.recv_data(p
, &blob
, NT_STATUS_OK
);
127 called when a IO is triggered by the events system
129 static void sock_io_handler(struct tevent_context
*ev
, struct tevent_fd
*fde
,
130 uint16_t flags
, void *private_data
)
132 struct dcerpc_connection
*p
= talloc_get_type(private_data
,
133 struct dcerpc_connection
);
134 struct sock_private
*sock
= (struct sock_private
*)p
->transport
.private_data
;
136 if (flags
& EVENT_FD_WRITE
) {
137 packet_queue_run(sock
->packet
);
141 if (sock
->sock
== NULL
) {
145 if (flags
& EVENT_FD_READ
) {
146 packet_recv(sock
->packet
);
151 initiate a read request - not needed for dcerpc sockets
153 static NTSTATUS
sock_send_read(struct dcerpc_connection
*p
)
155 struct sock_private
*sock
= (struct sock_private
*)p
->transport
.private_data
;
156 sock
->pending_reads
++;
157 if (sock
->pending_reads
== 1) {
158 packet_recv_enable(sock
->packet
);
164 send an initial pdu in a multi-pdu sequence
166 static NTSTATUS
sock_send_request(struct dcerpc_connection
*p
, DATA_BLOB
*data
,
169 struct sock_private
*sock
= (struct sock_private
*)p
->transport
.private_data
;
173 if (sock
->sock
== NULL
) {
174 return NT_STATUS_CONNECTION_DISCONNECTED
;
177 blob
= data_blob_talloc(sock
->packet
, data
->data
, data
->length
);
178 if (blob
.data
== NULL
) {
179 return NT_STATUS_NO_MEMORY
;
182 status
= packet_send(sock
->packet
, blob
);
183 if (!NT_STATUS_IS_OK(status
)) {
195 shutdown sock pipe connection
197 static NTSTATUS
sock_shutdown_pipe(struct dcerpc_connection
*p
, NTSTATUS status
)
199 struct sock_private
*sock
= (struct sock_private
*)p
->transport
.private_data
;
201 if (sock
&& sock
->sock
) {
202 sock_dead(p
, status
);
209 return sock server name
211 static const char *sock_peer_name(struct dcerpc_connection
*p
)
213 struct sock_private
*sock
= talloc_get_type(p
->transport
.private_data
, struct sock_private
);
214 return sock
->server_name
;
218 return remote name we make the actual connection (good for kerberos)
220 static const char *sock_target_hostname(struct dcerpc_connection
*p
)
222 struct sock_private
*sock
= talloc_get_type(p
->transport
.private_data
, struct sock_private
);
223 return sock
->server_name
;
227 struct pipe_open_socket_state
{
228 struct dcerpc_connection
*conn
;
229 struct socket_context
*socket_ctx
;
230 struct sock_private
*sock
;
231 struct socket_address
*server
;
232 const char *target_hostname
;
233 enum dcerpc_transport_t transport
;
237 static void continue_socket_connect(struct composite_context
*ctx
)
239 struct dcerpc_connection
*conn
;
240 struct sock_private
*sock
;
241 struct composite_context
*c
= talloc_get_type(ctx
->async
.private_data
,
242 struct composite_context
);
243 struct pipe_open_socket_state
*s
= talloc_get_type(c
->private_data
,
244 struct pipe_open_socket_state
);
246 /* make it easier to write a function calls */
250 c
->status
= socket_connect_recv(ctx
);
251 if (!NT_STATUS_IS_OK(c
->status
)) {
252 DEBUG(0, ("Failed to connect host %s on port %d - %s\n",
253 s
->server
->addr
, s
->server
->port
,
254 nt_errstr(c
->status
)));
255 composite_error(c
, c
->status
);
260 fill in the transport methods
262 conn
->transport
.transport
= s
->transport
;
263 conn
->transport
.private_data
= NULL
;
265 conn
->transport
.send_request
= sock_send_request
;
266 conn
->transport
.send_read
= sock_send_read
;
267 conn
->transport
.recv_data
= NULL
;
269 conn
->transport
.shutdown_pipe
= sock_shutdown_pipe
;
270 conn
->transport
.peer_name
= sock_peer_name
;
271 conn
->transport
.target_hostname
= sock_target_hostname
;
273 sock
->sock
= s
->socket_ctx
;
274 sock
->pending_reads
= 0;
275 sock
->server_name
= strupper_talloc(sock
, s
->target_hostname
);
277 sock
->fde
= event_add_fd(conn
->event_ctx
, sock
->sock
, socket_get_fd(sock
->sock
),
278 EVENT_FD_READ
, sock_io_handler
, conn
);
280 conn
->transport
.private_data
= sock
;
282 sock
->packet
= packet_init(sock
);
283 if (sock
->packet
== NULL
) {
284 composite_error(c
, NT_STATUS_NO_MEMORY
);
289 packet_set_private(sock
->packet
, conn
);
290 packet_set_socket(sock
->packet
, sock
->sock
);
291 packet_set_callback(sock
->packet
, sock_process_recv
);
292 packet_set_full_request(sock
->packet
, sock_complete_packet
);
293 packet_set_error_handler(sock
->packet
, sock_error_handler
);
294 packet_set_event_context(sock
->packet
, conn
->event_ctx
);
295 packet_set_fde(sock
->packet
, sock
->fde
);
296 packet_set_serialise(sock
->packet
);
297 packet_set_initial_read(sock
->packet
, 16);
299 /* ensure we don't get SIGPIPE */
300 BlockSignals(true, SIGPIPE
);
306 static struct composite_context
*dcerpc_pipe_open_socket_send(TALLOC_CTX
*mem_ctx
,
307 struct dcerpc_connection
*cn
,
308 struct socket_address
*server
,
309 const char *target_hostname
,
310 const char *full_path
,
311 enum dcerpc_transport_t transport
)
313 struct composite_context
*c
;
314 struct pipe_open_socket_state
*s
;
315 struct composite_context
*conn_req
;
317 c
= composite_create(mem_ctx
, cn
->event_ctx
);
318 if (c
== NULL
) return NULL
;
320 s
= talloc_zero(c
, struct pipe_open_socket_state
);
321 if (composite_nomem(s
, c
)) return c
;
325 s
->transport
= transport
;
326 s
->server
= talloc_reference(c
, server
);
327 if (composite_nomem(s
->server
, c
)) return c
;
328 s
->target_hostname
= talloc_reference(s
, target_hostname
);
330 s
->sock
= talloc(cn
, struct sock_private
);
331 if (composite_nomem(s
->sock
, c
)) return c
;
333 c
->status
= socket_create(server
->family
, SOCKET_TYPE_STREAM
, &s
->socket_ctx
, 0);
334 if (!composite_is_ok(c
)) return c
;
336 talloc_steal(s
->sock
, s
->socket_ctx
);
338 s
->sock
->path
= talloc_reference(s
->sock
, full_path
);
340 conn_req
= socket_connect_send(s
->socket_ctx
, NULL
, s
->server
, 0,
342 composite_continue(c
, conn_req
, continue_socket_connect
, c
);
347 static NTSTATUS
dcerpc_pipe_open_socket_recv(struct composite_context
*c
)
349 NTSTATUS status
= composite_wait(c
);
355 struct pipe_tcp_state
{
357 const char *target_hostname
;
360 struct socket_address
*srvaddr
;
361 struct resolve_context
*resolve_ctx
;
362 struct dcerpc_connection
*conn
;
366 #if 0 /* disabled till we can resolve names to ipv6 addresses */
367 static void continue_ipv6_open_socket(struct composite_context
*ctx
);
369 static void continue_ipv4_open_socket(struct composite_context
*ctx
);
370 static void continue_ip_resolve_name(struct composite_context
*ctx
);
372 static void continue_ip_resolve_name(struct composite_context
*ctx
)
374 struct composite_context
*c
= talloc_get_type(ctx
->async
.private_data
,
375 struct composite_context
);
376 struct pipe_tcp_state
*s
= talloc_get_type(c
->private_data
,
377 struct pipe_tcp_state
);
378 struct composite_context
*sock_ipv4_req
;
380 c
->status
= resolve_name_recv(ctx
, s
, &s
->address
);
381 if (!composite_is_ok(c
)) return;
383 /* prepare server address using host ip:port and transport name */
384 s
->srvaddr
= socket_address_from_strings(s
->conn
, "ipv4", s
->address
, s
->port
);
385 if (composite_nomem(s
->srvaddr
, c
)) return;
387 /* resolve_nbt_name gives only ipv4 ... - send socket open request */
388 sock_ipv4_req
= dcerpc_pipe_open_socket_send(c
, s
->conn
,
389 s
->srvaddr
, s
->target_hostname
,
392 composite_continue(c
, sock_ipv4_req
, continue_ipv4_open_socket
, c
);
396 Stage 2 of dcerpc_pipe_open_tcp_send: receive result of pipe open request
397 on IPv6 and send the request on IPv4 unless IPv6 transport succeeded.
399 #if 0 /* disabled till we can resolve names to ipv6 addresses */
400 static void continue_ipv6_open_socket(struct composite_context
*ctx
)
402 struct composite_context
*c
= talloc_get_type(ctx
->async
.private_data
,
403 struct composite_context
);
404 struct pipe_tcp_state
*s
= talloc_get_type(c
->private_data
,
405 struct pipe_tcp_state
);
406 struct composite_context
*sock_ipv4_req
;
408 /* receive result of socket open request */
409 c
->status
= dcerpc_pipe_open_socket_recv(ctx
);
410 if (NT_STATUS_IS_OK(c
->status
)) {
415 talloc_free(s
->srvaddr
);
417 /* prepare server address using host:ip and transport name */
418 s
->srvaddr
= socket_address_from_strings(s
->conn
, "ipv4", s
->address
, s
->port
);
419 if (composite_nomem(s
->srvaddr
, c
)) return;
421 /* try IPv4 if IPv6 fails */
422 sock_ipv4_req
= dcerpc_pipe_open_socket_send(c
, s
->conn
,
423 s
->srvaddr
, s
->target_hostname
,
425 composite_continue(c
, sock_ipv4_req
, continue_ipv4_open_socket
, c
);
430 Stage 2 of dcerpc_pipe_open_tcp_send: receive result of pipe open request
433 static void continue_ipv4_open_socket(struct composite_context
*ctx
)
435 struct composite_context
*c
= talloc_get_type(ctx
->async
.private_data
,
436 struct composite_context
);
437 struct pipe_tcp_state
*s
= talloc_get_type(c
->private_data
,
438 struct pipe_tcp_state
);
440 /* receive result socket open request */
441 c
->status
= dcerpc_pipe_open_socket_recv(ctx
);
442 if (!NT_STATUS_IS_OK(c
->status
)) {
443 /* something went wrong... */
444 DEBUG(0, ("Failed to connect host %s (%s) on port %d - %s.\n",
445 s
->address
, s
->target_hostname
,
446 s
->port
, nt_errstr(c
->status
)));
448 composite_error(c
, c
->status
);
457 Send rpc pipe open request to given host:port using
460 struct composite_context
* dcerpc_pipe_open_tcp_send(struct dcerpc_connection
*conn
,
462 const char *target_hostname
,
464 struct resolve_context
*resolve_ctx
)
466 struct composite_context
*c
;
467 struct pipe_tcp_state
*s
;
468 struct composite_context
*resolve_req
;
469 struct nbt_name name
;
471 /* composite context allocation and setup */
472 c
= composite_create(conn
, conn
->event_ctx
);
473 if (c
== NULL
) return NULL
;
475 s
= talloc_zero(c
, struct pipe_tcp_state
);
476 if (composite_nomem(s
, c
)) return c
;
479 /* store input parameters in state structure */
480 s
->server
= talloc_strdup(c
, server
);
481 if (composite_nomem(s
->server
, c
)) return c
;
482 if (target_hostname
) {
483 s
->target_hostname
= talloc_strdup(c
, target_hostname
);
484 if (composite_nomem(s
->target_hostname
, c
)) return c
;
488 s
->resolve_ctx
= resolve_ctx
;
490 make_nbt_name_server(&name
, server
);
491 resolve_req
= resolve_name_send(resolve_ctx
, s
, &name
, c
->event_ctx
);
492 composite_continue(c
, resolve_req
, continue_ip_resolve_name
, c
);
497 Receive result of pipe open request on tcp/ip
499 NTSTATUS
dcerpc_pipe_open_tcp_recv(struct composite_context
*c
)
502 status
= composite_wait(c
);
509 struct pipe_unix_state
{
511 struct socket_address
*srvaddr
;
512 struct dcerpc_connection
*conn
;
517 Stage 2 of dcerpc_pipe_open_unix_stream_send: receive result of pipe open
518 request on unix socket.
520 static void continue_unix_open_socket(struct composite_context
*ctx
)
522 struct composite_context
*c
= talloc_get_type(ctx
->async
.private_data
,
523 struct composite_context
);
525 c
->status
= dcerpc_pipe_open_socket_recv(ctx
);
526 if (NT_STATUS_IS_OK(c
->status
)) {
531 composite_error(c
, c
->status
);
536 Send pipe open request on unix socket
538 struct composite_context
*dcerpc_pipe_open_unix_stream_send(struct dcerpc_connection
*conn
,
541 struct composite_context
*c
;
542 struct composite_context
*sock_unix_req
;
543 struct pipe_unix_state
*s
;
545 /* composite context allocation and setup */
546 c
= composite_create(conn
, conn
->event_ctx
);
547 if (c
== NULL
) return NULL
;
549 s
= talloc_zero(c
, struct pipe_unix_state
);
550 if (composite_nomem(s
, c
)) return c
;
553 /* store parameters in state structure */
554 s
->path
= talloc_strdup(c
, path
);
555 if (composite_nomem(s
->path
, c
)) return c
;
558 /* prepare server address using socket path and transport name */
559 s
->srvaddr
= socket_address_from_strings(conn
, "unix", s
->path
, 0);
560 if (composite_nomem(s
->srvaddr
, c
)) return c
;
562 /* send socket open request */
563 sock_unix_req
= dcerpc_pipe_open_socket_send(c
, s
->conn
,
567 composite_continue(c
, sock_unix_req
, continue_unix_open_socket
, c
);
573 Receive result of pipe open request on unix socket
575 NTSTATUS
dcerpc_pipe_open_unix_stream_recv(struct composite_context
*c
)
577 NTSTATUS status
= composite_wait(c
);
585 Stage 2 of dcerpc_pipe_open_pipe_send: receive socket open request
587 static void continue_np_open_socket(struct composite_context
*ctx
)
589 struct composite_context
*c
= talloc_get_type(ctx
->async
.private_data
,
590 struct composite_context
);
592 c
->status
= dcerpc_pipe_open_socket_recv(ctx
);
593 if (!composite_is_ok(c
)) return;
600 Send pipe open request on ncalrpc
602 struct composite_context
* dcerpc_pipe_open_pipe_send(struct dcerpc_connection
*conn
,
603 const char *ncalrpc_dir
,
604 const char *identifier
)
608 struct composite_context
*c
;
609 struct composite_context
*sock_np_req
;
610 struct pipe_unix_state
*s
;
612 /* composite context allocation and setup */
613 c
= composite_create(conn
, conn
->event_ctx
);
614 if (c
== NULL
) return NULL
;
616 s
= talloc_zero(c
, struct pipe_unix_state
);
617 if (composite_nomem(s
, c
)) return c
;
620 /* store parameters in state structure */
621 canon
= talloc_strdup(s
, identifier
);
622 if (composite_nomem(canon
, c
)) return c
;
625 string_replace(canon
, '/', '\\');
626 s
->path
= talloc_asprintf(canon
, "%s/%s", ncalrpc_dir
, canon
);
627 if (composite_nomem(s
->path
, c
)) return c
;
629 /* prepare server address using path and transport name */
630 s
->srvaddr
= socket_address_from_strings(conn
, "unix", s
->path
, 0);
631 if (composite_nomem(s
->srvaddr
, c
)) return c
;
633 /* send socket open request */
634 sock_np_req
= dcerpc_pipe_open_socket_send(c
, s
->conn
, s
->srvaddr
, NULL
, s
->path
, NCALRPC
);
635 composite_continue(c
, sock_np_req
, continue_np_open_socket
, c
);
641 Receive result of pipe open request on ncalrpc
643 NTSTATUS
dcerpc_pipe_open_pipe_recv(struct composite_context
*c
)
645 NTSTATUS status
= composite_wait(c
);
653 Open a rpc pipe on a named pipe - sync version
655 NTSTATUS
dcerpc_pipe_open_pipe(struct dcerpc_connection
*conn
, const char *ncalrpc_dir
, const char *identifier
)
657 struct composite_context
*c
= dcerpc_pipe_open_pipe_send(conn
, ncalrpc_dir
, identifier
);
658 return dcerpc_pipe_open_pipe_recv(c
);
661 const char *dcerpc_unix_socket_path(struct dcerpc_connection
*p
)
663 struct sock_private
*sock
= (struct sock_private
*)p
->transport
.private_data
;
667 struct socket_address
*dcerpc_socket_peer_addr(struct dcerpc_connection
*p
, TALLOC_CTX
*mem_ctx
)
669 struct sock_private
*sock
= (struct sock_private
*)p
->transport
.private_data
;
670 return socket_get_peer_addr(sock
->sock
, mem_ctx
);