2 Unix SMB/Netbios implementation.
3 Generic infrstructure for RPC Daemons
4 Copyright (C) Simo Sorce 2010
5 Copyright (C) Andrew Bartlett 2011
6 Copyright (C) Andreas Schneider 2011
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "rpc_server/rpc_pipes.h"
24 #include "rpc_server/rpc_server.h"
25 #include "rpc_server/rpc_config.h"
27 #include "librpc/gen_ndr/netlogon.h"
28 #include "librpc/gen_ndr/auth.h"
29 #include "lib/tsocket/tsocket.h"
30 #include "libcli/named_pipe_auth/npa_tstream.h"
31 #include "../auth/auth_sam_reply.h"
33 #include "rpc_server/rpc_ncacn_np.h"
34 #include "rpc_server/srv_pipe_hnd.h"
35 #include "rpc_server/srv_pipe.h"
37 #define SERVER_TCP_LOW_PORT 1024
38 #define SERVER_TCP_HIGH_PORT 1300
40 /* Creates a pipes_struct and initializes it with the information
41 * sent from the client */
42 int make_server_pipes_struct(TALLOC_CTX
*mem_ctx
,
43 struct messaging_context
*msg_ctx
,
44 const char *pipe_name
,
45 enum dcerpc_transport_t transport
,
46 const struct tsocket_address
*local_address
,
47 const struct tsocket_address
*remote_address
,
48 struct auth_session_info
*session_info
,
49 struct pipes_struct
**_p
,
52 struct pipes_struct
*p
;
55 ret
= make_base_pipes_struct(mem_ctx
, msg_ctx
, pipe_name
,
56 transport
, RPC_LITTLE_ENDIAN
,
57 remote_address
, local_address
, &p
);
63 if (session_info
->unix_token
&& session_info
->unix_info
&& session_info
->security_token
) {
64 /* Don't call create_local_token(), we already have the full details here */
65 p
->session_info
= talloc_steal(p
, session_info
);
68 DEBUG(0, ("Supplied session_info in make_server_pipes_struct was incomplete!"));
77 /* Start listening on the appropriate unix socket and setup all is needed to
78 * dispatch requests to the pipes rpc implementation */
80 struct dcerpc_ncacn_listen_state
{
81 struct ndr_syntax_id syntax_id
;
89 struct tevent_context
*ev_ctx
;
90 struct messaging_context
*msg_ctx
;
91 dcerpc_ncacn_disconnect_fn disconnect_fn
;
94 static void named_pipe_listener(struct tevent_context
*ev
,
95 struct tevent_fd
*fde
,
99 int create_named_pipe_socket(const char *pipe_name
)
105 * As lp_ncalrpc_dir() should have 0755, but
106 * lp_ncalrpc_dir()/np should have 0700, we need to
107 * create lp_ncalrpc_dir() first.
109 if (!directory_create_or_exist(lp_ncalrpc_dir(), 0755)) {
110 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
111 lp_ncalrpc_dir(), strerror(errno
)));
115 np_dir
= talloc_asprintf(talloc_tos(), "%s/np", lp_ncalrpc_dir());
117 DEBUG(0, ("Out of memory\n"));
121 if (!directory_create_or_exist_strict(np_dir
, geteuid(), 0700)) {
122 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
123 np_dir
, strerror(errno
)));
127 fd
= create_pipe_sock(np_dir
, pipe_name
, 0700);
129 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
134 DEBUG(10, ("Openened pipe socket fd %d for %s\n", fd
, pipe_name
));
141 bool setup_named_pipe_socket(const char *pipe_name
,
142 struct tevent_context
*ev_ctx
,
143 struct messaging_context
*msg_ctx
)
145 struct dcerpc_ncacn_listen_state
*state
;
146 struct tevent_fd
*fde
;
149 state
= talloc(ev_ctx
, struct dcerpc_ncacn_listen_state
);
151 DEBUG(0, ("Out of memory\n"));
154 state
->ep
.name
= talloc_strdup(state
, pipe_name
);
155 if (state
->ep
.name
== NULL
) {
156 DEBUG(0, ("Out of memory\n"));
159 state
->fd
= create_named_pipe_socket(pipe_name
);
160 if (state
->fd
== -1) {
164 rc
= listen(state
->fd
, 5);
166 DEBUG(0, ("Failed to listen on pipe socket %s: %s\n",
167 pipe_name
, strerror(errno
)));
171 state
->ev_ctx
= ev_ctx
;
172 state
->msg_ctx
= msg_ctx
;
174 DEBUG(10, ("Openened pipe socket fd %d for %s\n",
175 state
->fd
, pipe_name
));
177 fde
= tevent_add_fd(ev_ctx
,
178 state
, state
->fd
, TEVENT_FD_READ
,
179 named_pipe_listener
, state
);
181 DEBUG(0, ("Failed to add event handler!\n"));
185 tevent_fd_set_auto_close(fde
);
189 if (state
->fd
!= -1) {
196 static void named_pipe_listener(struct tevent_context
*ev
,
197 struct tevent_fd
*fde
,
201 struct dcerpc_ncacn_listen_state
*state
=
202 talloc_get_type_abort(private_data
,
203 struct dcerpc_ncacn_listen_state
);
204 struct sockaddr_un sunaddr
;
208 /* TODO: should we have a limit to the number of clients ? */
210 len
= sizeof(sunaddr
);
212 sd
= accept(state
->fd
,
213 (struct sockaddr
*)(void *)&sunaddr
, &len
);
216 if (errno
!= EINTR
) {
217 DEBUG(6, ("Failed to get a valid socket [%s]\n",
223 DEBUG(6, ("Accepted socket %d\n", sd
));
225 named_pipe_accept_function(state
->ev_ctx
,
232 /* This is the core of the rpc server.
233 * Accepts connections from clients and process requests using the appropriate
234 * dispatcher table. */
236 static int named_pipe_destructor(struct named_pipe_client
*npc
)
239 npc
->term_fn(npc
->private_data
);
244 struct named_pipe_client
*named_pipe_client_init(TALLOC_CTX
*mem_ctx
,
245 struct tevent_context
*ev_ctx
,
246 struct messaging_context
*msg_ctx
,
247 const char *pipe_name
,
248 named_pipe_termination_fn
*term_fn
,
250 uint16_t device_state
,
251 uint64_t allocation_size
,
254 struct named_pipe_client
*npc
;
256 npc
= talloc_zero(mem_ctx
, struct named_pipe_client
);
258 DEBUG(0, ("Out of memory!\n"));
261 talloc_set_destructor(npc
, named_pipe_destructor
);
263 npc
->pipe_name
= talloc_strdup(npc
, pipe_name
);
264 if (npc
->pipe_name
== NULL
) {
265 DEBUG(0, ("Out of memory!\n"));
271 npc
->msg_ctx
= msg_ctx
;
272 npc
->term_fn
= term_fn
;
273 npc
->private_data
= private_data
;
275 npc
->file_type
= file_type
;
276 npc
->device_state
= device_state
;
277 npc
->allocation_size
= allocation_size
;
282 static void named_pipe_accept_done(struct tevent_req
*subreq
);
284 void named_pipe_accept_function(struct tevent_context
*ev_ctx
,
285 struct messaging_context
*msg_ctx
,
286 const char *pipe_name
, int fd
,
287 named_pipe_termination_fn
*term_fn
,
290 struct named_pipe_client
*npc
;
291 struct tstream_context
*plain
;
292 struct tevent_req
*subreq
;
295 npc
= talloc_zero(ev_ctx
, struct named_pipe_client
);
297 DEBUG(0, ("Out of memory!\n"));
302 npc
->pipe_name
= talloc_strdup(npc
, pipe_name
);
303 if (npc
->pipe_name
== NULL
) {
304 DEBUG(0, ("Out of memory!\n"));
310 npc
->msg_ctx
= msg_ctx
;
311 npc
->term_fn
= term_fn
;
312 npc
->private_data
= private_data
;
314 talloc_set_destructor(npc
, named_pipe_destructor
);
316 /* make sure socket is in NON blocking state */
317 ret
= set_blocking(fd
, false);
319 DEBUG(2, ("Failed to make socket non-blocking\n"));
325 ret
= tstream_bsd_existing_socket(npc
, fd
, &plain
);
327 DEBUG(2, ("Failed to create tstream socket\n"));
333 npc
->file_type
= FILE_TYPE_MESSAGE_MODE_PIPE
;
334 npc
->device_state
= 0xff | 0x0400 | 0x0100;
335 npc
->allocation_size
= 4096;
337 subreq
= tstream_npa_accept_existing_send(npc
, npc
->ev
, plain
,
340 npc
->allocation_size
);
342 DEBUG(2, ("Failed to start async accept procedure\n"));
347 tevent_req_set_callback(subreq
, named_pipe_accept_done
, npc
);
350 static void named_pipe_packet_done(struct tevent_req
*subreq
);
352 static void named_pipe_accept_done(struct tevent_req
*subreq
)
354 struct auth_session_info_transport
*session_info_transport
;
355 struct named_pipe_client
*npc
=
356 tevent_req_callback_data(subreq
, struct named_pipe_client
);
360 ret
= tstream_npa_accept_existing_recv(subreq
, &error
, npc
,
366 &session_info_transport
);
368 npc
->session_info
= talloc_move(npc
, &session_info_transport
->session_info
);
372 DEBUG(2, ("Failed to accept named pipe connection! (%s)\n",
378 ret
= make_server_pipes_struct(npc
,
380 npc
->pipe_name
, NCACN_NP
,
386 DEBUG(2, ("Failed to create pipes_struct! (%s)\n",
391 npc
->write_queue
= tevent_queue_create(npc
, "np_server_write_queue");
392 if (!npc
->write_queue
) {
393 DEBUG(2, ("Failed to set up write queue!\n"));
397 /* And now start receiving and processing packets */
398 subreq
= dcerpc_read_ncacn_packet_send(npc
, npc
->ev
, npc
->tstream
);
400 DEBUG(2, ("Failed to start receving packets\n"));
403 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
407 DEBUG(2, ("Fatal error. Terminating client(%s) connection!\n",
409 /* terminate client connection */
414 void named_pipe_packet_process(struct tevent_req
*subreq
)
416 struct named_pipe_client
*npc
=
417 tevent_req_callback_data(subreq
, struct named_pipe_client
);
418 struct _output_data
*out
= &npc
->p
->out_data
;
419 DATA_BLOB recv_buffer
= data_blob_null
;
420 struct ncacn_packet
*pkt
;
426 status
= dcerpc_read_ncacn_packet_recv(subreq
, npc
, &pkt
, &recv_buffer
);
428 if (!NT_STATUS_IS_OK(status
)) {
432 /* dcerpc_read_ncacn_packet_recv() returns a full PDU */
433 npc
->p
->in_data
.pdu_needed_len
= 0;
434 npc
->p
->in_data
.pdu
= recv_buffer
;
435 if (dcerpc_get_endian_flag(&recv_buffer
) & DCERPC_DREP_LE
) {
436 npc
->p
->endian
= RPC_LITTLE_ENDIAN
;
438 npc
->p
->endian
= RPC_BIG_ENDIAN
;
440 DEBUG(10, ("PDU is in %s Endian format!\n",
441 npc
->p
->endian
? "Big" : "Little"));
442 process_complete_pdu(npc
->p
, pkt
);
444 /* reset pipe state and free PDU */
445 npc
->p
->in_data
.pdu
.length
= 0;
446 talloc_free(recv_buffer
.data
);
449 /* this is needed because of the way DCERPC Binds work in
450 * the RPC marshalling code */
451 to_send
= out
->frag
.length
- out
->current_pdu_sent
;
454 npc
->iov
= talloc_zero(npc
, struct iovec
);
456 status
= NT_STATUS_NO_MEMORY
;
461 npc
->iov
[0].iov_base
= out
->frag
.data
462 + out
->current_pdu_sent
;
463 npc
->iov
[0].iov_len
= to_send
;
465 out
->current_pdu_sent
+= to_send
;
468 /* this condition is false for bind packets, or when we haven't
469 * yet got a full request, and need to wait for more data from
471 while (out
->data_sent_length
< out
->rdata
.length
) {
473 ok
= create_next_pdu(npc
->p
);
475 DEBUG(3, ("Failed to create next PDU!\n"));
476 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
480 npc
->iov
= talloc_realloc(npc
, npc
->iov
,
481 struct iovec
, npc
->count
+ 1);
483 status
= NT_STATUS_NO_MEMORY
;
487 npc
->iov
[npc
->count
].iov_base
= out
->frag
.data
;
488 npc
->iov
[npc
->count
].iov_len
= out
->frag
.length
;
493 /* we still don't have a complete request, go back and wait for more
495 if (npc
->count
== 0) {
496 /* Wait for the next packet */
497 subreq
= dcerpc_read_ncacn_packet_send(npc
, npc
->ev
, npc
->tstream
);
499 DEBUG(2, ("Failed to start receving packets\n"));
500 status
= NT_STATUS_NO_MEMORY
;
503 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
507 DEBUG(10, ("Sending %u fragments in a total of %u bytes\n",
508 (unsigned int)npc
->count
,
509 (unsigned int)npc
->p
->out_data
.data_sent_length
));
511 for (i
= 0; i
< npc
->count
; i
++) {
512 DEBUG(10, ("Sending PDU number: %d, PDU Length: %u\n",
514 (unsigned int)npc
->iov
[i
].iov_len
));
515 dump_data(11, (const uint8_t *)npc
->iov
[i
].iov_base
,
516 npc
->iov
[i
].iov_len
);
518 subreq
= tstream_writev_queue_send(npc
,
525 DEBUG(2, ("Failed to send packet\n"));
526 status
= NT_STATUS_NO_MEMORY
;
529 tevent_req_set_callback(subreq
, named_pipe_packet_done
, npc
);
535 DEBUG(2, ("Fatal error(%s). "
536 "Terminating client(%s) connection!\n",
537 nt_errstr(status
), npc
->client_name
));
538 /* terminate client connection */
543 static void named_pipe_packet_done(struct tevent_req
*subreq
)
545 struct named_pipe_client
*npc
=
546 tevent_req_callback_data(subreq
, struct named_pipe_client
);
550 ret
= tstream_writev_queue_recv(subreq
, &sys_errno
);
553 DEBUG(2, ("Writev failed!\n"));
557 if (tevent_queue_length(npc
->write_queue
) > 0) {
561 /* clear out any data that may have been left around */
563 TALLOC_FREE(npc
->iov
);
564 data_blob_free(&npc
->p
->in_data
.data
);
565 data_blob_free(&npc
->p
->out_data
.frag
);
566 data_blob_free(&npc
->p
->out_data
.rdata
);
568 talloc_free_children(npc
->p
->mem_ctx
);
570 /* Wait for the next packet */
571 subreq
= dcerpc_read_ncacn_packet_send(npc
, npc
->ev
, npc
->tstream
);
573 DEBUG(2, ("Failed to start receving packets\n"));
577 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
581 DEBUG(2, ("Fatal error(%s). "
582 "Terminating client(%s) connection!\n",
583 strerror(sys_errno
), npc
->client_name
));
584 /* terminate client connection */
589 /********************************************************************
590 * Start listening on the tcp/ip socket
591 ********************************************************************/
593 static void dcerpc_ncacn_tcpip_listener(struct tevent_context
*ev
,
594 struct tevent_fd
*fde
,
598 int create_tcpip_socket(const struct sockaddr_storage
*ifss
, uint16_t *port
)
605 for (i
= SERVER_TCP_LOW_PORT
; i
<= SERVER_TCP_HIGH_PORT
; i
++) {
606 fd
= open_socket_in(SOCK_STREAM
,
617 fd
= open_socket_in(SOCK_STREAM
,
624 DEBUG(0, ("Failed to create socket on port %u!\n", *port
));
628 DEBUG(10, ("Opened tcpip socket fd %d for port %u\n", fd
, *port
));
633 uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context
*ev_ctx
,
634 struct messaging_context
*msg_ctx
,
635 const struct sockaddr_storage
*ifss
,
638 struct dcerpc_ncacn_listen_state
*state
;
639 struct tevent_fd
*fde
;
642 state
= talloc(ev_ctx
, struct dcerpc_ncacn_listen_state
);
644 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Out of memory\n"));
649 state
->ep
.port
= port
;
650 state
->disconnect_fn
= NULL
;
652 state
->fd
= create_tcpip_socket(ifss
, &state
->ep
.port
);
653 if (state
->fd
== -1) {
657 state
->ev_ctx
= ev_ctx
;
658 state
->msg_ctx
= msg_ctx
;
660 /* ready to listen */
661 set_socket_options(state
->fd
, "SO_KEEPALIVE");
662 set_socket_options(state
->fd
, lp_socket_options());
664 /* Set server socket to non-blocking for the accept. */
665 set_blocking(state
->fd
, false);
667 rc
= listen(state
->fd
, SMBD_LISTEN_BACKLOG
);
669 DEBUG(0,("setup_tcpip_socket: listen - %s\n", strerror(errno
)));
673 DEBUG(10, ("setup_tcpip_socket: openened socket fd %d for port %u\n",
674 state
->fd
, state
->ep
.port
));
676 fde
= tevent_add_fd(state
->ev_ctx
,
680 dcerpc_ncacn_tcpip_listener
,
683 DEBUG(0, ("setup_tcpip_socket: Failed to add event handler!\n"));
687 tevent_fd_set_auto_close(fde
);
689 return state
->ep
.port
;
691 if (state
->fd
!= -1) {
699 static void dcerpc_ncacn_tcpip_listener(struct tevent_context
*ev
,
700 struct tevent_fd
*fde
,
704 struct dcerpc_ncacn_listen_state
*state
=
705 talloc_get_type_abort(private_data
,
706 struct dcerpc_ncacn_listen_state
);
707 struct tsocket_address
*cli_addr
= NULL
;
708 struct tsocket_address
*srv_addr
= NULL
;
709 struct sockaddr_storage addr
;
710 socklen_t in_addrlen
= sizeof(addr
);
714 s
= accept(state
->fd
, (struct sockaddr
*)(void *) &addr
, &in_addrlen
);
716 if (errno
!= EINTR
) {
717 DEBUG(0,("tcpip_listener accept: %s\n",
723 rc
= tsocket_address_bsd_from_sockaddr(state
,
724 (struct sockaddr
*)(void *) &addr
,
732 rc
= getsockname(s
, (struct sockaddr
*)(void *) &addr
, &in_addrlen
);
738 rc
= tsocket_address_bsd_from_sockaddr(state
,
739 (struct sockaddr
*)(void *) &addr
,
747 DEBUG(6, ("tcpip_listener: Accepted socket %d\n", s
));
749 dcerpc_ncacn_accept(state
->ev_ctx
,
759 /********************************************************************
760 * Start listening on the ncalrpc socket
761 ********************************************************************/
763 static void dcerpc_ncalrpc_listener(struct tevent_context
*ev
,
764 struct tevent_fd
*fde
,
768 int create_dcerpc_ncalrpc_socket(const char *name
)
776 if (!directory_create_or_exist(lp_ncalrpc_dir(), 0755)) {
777 DEBUG(0, ("Failed to create ncalrpc directory %s - %s\n",
778 lp_ncalrpc_dir(), strerror(errno
)));
782 fd
= create_pipe_sock(lp_ncalrpc_dir(), name
, 0755);
784 DEBUG(0, ("Failed to create ncalrpc socket! [%s/%s]\n",
785 lp_ncalrpc_dir(), name
));
789 DEBUG(10, ("Openened ncalrpc socket fd %d for %s\n", fd
, name
));
794 bool setup_dcerpc_ncalrpc_socket(struct tevent_context
*ev_ctx
,
795 struct messaging_context
*msg_ctx
,
797 dcerpc_ncacn_disconnect_fn fn
)
799 struct dcerpc_ncacn_listen_state
*state
;
800 struct tevent_fd
*fde
;
803 state
= talloc(ev_ctx
, struct dcerpc_ncacn_listen_state
);
805 DEBUG(0, ("Out of memory\n"));
810 state
->disconnect_fn
= fn
;
816 state
->ep
.name
= talloc_strdup(state
, name
);
817 if (state
->ep
.name
== NULL
) {
818 DEBUG(0, ("Out of memory\n"));
823 state
->fd
= create_dcerpc_ncalrpc_socket(name
);
824 if (state
->fd
== -1) {
828 rc
= listen(state
->fd
, 5);
830 DEBUG(0, ("Failed to listen on ncalrpc socket %s: %s\n",
831 name
, strerror(errno
)));
835 state
->ev_ctx
= ev_ctx
;
836 state
->msg_ctx
= msg_ctx
;
838 /* Set server socket to non-blocking for the accept. */
839 set_blocking(state
->fd
, false);
841 fde
= tevent_add_fd(state
->ev_ctx
,
845 dcerpc_ncalrpc_listener
,
848 DEBUG(0, ("Failed to add event handler for ncalrpc!\n"));
852 tevent_fd_set_auto_close(fde
);
856 if (state
->fd
!= -1) {
864 static void dcerpc_ncalrpc_listener(struct tevent_context
*ev
,
865 struct tevent_fd
*fde
,
869 struct dcerpc_ncacn_listen_state
*state
=
870 talloc_get_type_abort(private_data
,
871 struct dcerpc_ncacn_listen_state
);
872 struct tsocket_address
*cli_addr
= NULL
;
873 struct sockaddr_un sunaddr
;
874 struct sockaddr
*addr
= (struct sockaddr
*)(void *)&sunaddr
;
875 socklen_t len
= sizeof(sunaddr
);
879 ZERO_STRUCT(sunaddr
);
881 sd
= accept(state
->fd
, addr
, &len
);
883 if (errno
!= EINTR
) {
884 DEBUG(0, ("ncalrpc accept() failed: %s\n", strerror(errno
)));
889 rc
= tsocket_address_bsd_from_sockaddr(state
,
897 DEBUG(10, ("Accepted ncalrpc socket %d\n", sd
));
899 dcerpc_ncacn_accept(state
->ev_ctx
,
904 state
->disconnect_fn
);
907 struct dcerpc_ncacn_conn
{
908 enum dcerpc_transport_t transport
;
912 struct pipes_struct
*p
;
913 dcerpc_ncacn_disconnect_fn disconnect_fn
;
915 struct tevent_context
*ev_ctx
;
916 struct messaging_context
*msg_ctx
;
918 struct tstream_context
*tstream
;
919 struct tevent_queue
*send_queue
;
921 struct tsocket_address
*client
;
923 struct tsocket_address
*server
;
925 struct auth_session_info
*session_info
;
931 static void dcerpc_ncacn_packet_process(struct tevent_req
*subreq
);
932 static void dcerpc_ncacn_packet_done(struct tevent_req
*subreq
);
934 void dcerpc_ncacn_accept(struct tevent_context
*ev_ctx
,
935 struct messaging_context
*msg_ctx
,
936 enum dcerpc_transport_t transport
,
938 struct tsocket_address
*cli_addr
,
939 struct tsocket_address
*srv_addr
,
941 dcerpc_ncacn_disconnect_fn fn
) {
942 struct dcerpc_ncacn_conn
*ncacn_conn
;
943 struct tevent_req
*subreq
;
951 DEBUG(10, ("dcerpc_ncacn_accept\n"));
953 ncacn_conn
= talloc_zero(ev_ctx
, struct dcerpc_ncacn_conn
);
954 if (ncacn_conn
== NULL
) {
955 DEBUG(0, ("Out of memory!\n"));
960 ncacn_conn
->transport
= transport
;
961 ncacn_conn
->ev_ctx
= ev_ctx
;
962 ncacn_conn
->msg_ctx
= msg_ctx
;
963 ncacn_conn
->sock
= s
;
964 ncacn_conn
->disconnect_fn
= fn
;
966 ncacn_conn
->client
= talloc_move(ncacn_conn
, &cli_addr
);
967 if (tsocket_address_is_inet(ncacn_conn
->client
, "ip")) {
968 ncacn_conn
->client_name
=
969 tsocket_address_inet_addr_string(ncacn_conn
->client
,
972 ncacn_conn
->client_name
=
973 tsocket_address_unix_path(ncacn_conn
->client
,
976 if (ncacn_conn
->client_name
== NULL
) {
977 DEBUG(0, ("Out of memory!\n"));
978 talloc_free(ncacn_conn
);
983 if (srv_addr
!= NULL
) {
984 ncacn_conn
->server
= talloc_move(ncacn_conn
, &srv_addr
);
986 ncacn_conn
->server_name
=
987 tsocket_address_inet_addr_string(ncacn_conn
->server
,
989 if (ncacn_conn
->server_name
== NULL
) {
990 DEBUG(0, ("Out of memory!\n"));
991 talloc_free(ncacn_conn
);
999 pipe_name
= tsocket_address_string(ncacn_conn
->client
,
1001 if (pipe_name
== NULL
) {
1003 talloc_free(ncacn_conn
);
1009 rc
= getpeereid(s
, &uid
, &gid
);
1011 DEBUG(2, ("Failed to get ncalrpc connecting "
1012 "uid - %s!\n", strerror(errno
)));
1014 if (uid
== sec_initial_uid()) {
1015 TALLOC_FREE(ncacn_conn
->client
);
1017 rc
= tsocket_address_unix_from_path(ncacn_conn
,
1018 "/root/ncalrpc_as_system",
1019 &ncacn_conn
->client
);
1021 DEBUG(0, ("Out of memory!\n"));
1022 talloc_free(ncacn_conn
);
1027 TALLOC_FREE(ncacn_conn
->client_name
);
1028 ncacn_conn
->client_name
= tsocket_address_unix_path(ncacn_conn
->client
,
1030 if (ncacn_conn
->client
== NULL
) {
1031 DEBUG(0, ("Out of memory!\n"));
1032 talloc_free(ncacn_conn
);
1040 pipe_name
= talloc_strdup(ncacn_conn
,
1042 if (pipe_name
== NULL
) {
1044 talloc_free(ncacn_conn
);
1049 DEBUG(0, ("unknown dcerpc transport: %u!\n",
1051 talloc_free(ncacn_conn
);
1056 rc
= set_blocking(s
, false);
1058 DEBUG(2, ("Failed to set dcerpc socket to non-blocking\n"));
1059 talloc_free(ncacn_conn
);
1065 * As soon as we have tstream_bsd_existing_socket set up it will
1066 * take care of closing the socket.
1068 rc
= tstream_bsd_existing_socket(ncacn_conn
, s
, &ncacn_conn
->tstream
);
1070 DEBUG(2, ("Failed to create tstream socket for dcerpc\n"));
1071 talloc_free(ncacn_conn
);
1076 if (ncacn_conn
->session_info
== NULL
) {
1078 * TODO: use auth_anonymous_session_info() here?
1080 status
= make_session_info_guest(ncacn_conn
,
1081 &ncacn_conn
->session_info
);
1082 if (!NT_STATUS_IS_OK(status
)) {
1083 DEBUG(2, ("Failed to create "
1084 "make_session_info_guest - %s\n",
1085 nt_errstr(status
)));
1086 talloc_free(ncacn_conn
);
1091 rc
= make_server_pipes_struct(ncacn_conn
,
1092 ncacn_conn
->msg_ctx
,
1094 ncacn_conn
->transport
,
1097 ncacn_conn
->session_info
,
1101 DEBUG(2, ("Failed to create pipe struct - %s",
1102 strerror(sys_errno
)));
1103 talloc_free(ncacn_conn
);
1107 ncacn_conn
->send_queue
= tevent_queue_create(ncacn_conn
,
1108 "dcerpc send queue");
1109 if (ncacn_conn
->send_queue
== NULL
) {
1110 DEBUG(0, ("Out of memory!\n"));
1111 talloc_free(ncacn_conn
);
1115 subreq
= dcerpc_read_ncacn_packet_send(ncacn_conn
,
1117 ncacn_conn
->tstream
);
1118 if (subreq
== NULL
) {
1119 DEBUG(2, ("Failed to send ncacn packet\n"));
1120 talloc_free(ncacn_conn
);
1124 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1126 DEBUG(10, ("dcerpc_ncacn_accept done\n"));
1131 static void dcerpc_ncacn_packet_process(struct tevent_req
*subreq
)
1133 struct dcerpc_ncacn_conn
*ncacn_conn
=
1134 tevent_req_callback_data(subreq
, struct dcerpc_ncacn_conn
);
1136 struct _output_data
*out
= &ncacn_conn
->p
->out_data
;
1137 DATA_BLOB recv_buffer
= data_blob_null
;
1138 struct ncacn_packet
*pkt
;
1143 status
= dcerpc_read_ncacn_packet_recv(subreq
, ncacn_conn
, &pkt
, &recv_buffer
);
1144 TALLOC_FREE(subreq
);
1145 if (!NT_STATUS_IS_OK(status
)) {
1146 if (ncacn_conn
->disconnect_fn
!= NULL
) {
1147 ok
= ncacn_conn
->disconnect_fn(ncacn_conn
->p
);
1149 DEBUG(3, ("Failed to call disconnect function\n"));
1155 /* dcerpc_read_ncacn_packet_recv() returns a full PDU */
1156 ncacn_conn
->p
->in_data
.pdu_needed_len
= 0;
1157 ncacn_conn
->p
->in_data
.pdu
= recv_buffer
;
1158 if (dcerpc_get_endian_flag(&recv_buffer
) & DCERPC_DREP_LE
) {
1159 ncacn_conn
->p
->endian
= RPC_LITTLE_ENDIAN
;
1161 ncacn_conn
->p
->endian
= RPC_BIG_ENDIAN
;
1163 DEBUG(10, ("PDU is in %s Endian format!\n",
1164 ncacn_conn
->p
->endian
? "Big" : "Little"));
1165 process_complete_pdu(ncacn_conn
->p
, pkt
);
1167 /* reset pipe state and free PDU */
1168 ncacn_conn
->p
->in_data
.pdu
.length
= 0;
1169 talloc_free(recv_buffer
.data
);
1173 * This is needed because of the way DCERPC binds work in the RPC
1176 to_send
= out
->frag
.length
- out
->current_pdu_sent
;
1179 DEBUG(10, ("Current_pdu_len = %u, "
1180 "current_pdu_sent = %u "
1181 "Returning %u bytes\n",
1182 (unsigned int)out
->frag
.length
,
1183 (unsigned int)out
->current_pdu_sent
,
1184 (unsigned int)to_send
));
1186 ncacn_conn
->iov
= talloc_zero(ncacn_conn
, struct iovec
);
1187 if (ncacn_conn
->iov
== NULL
) {
1188 status
= NT_STATUS_NO_MEMORY
;
1189 DEBUG(3, ("Out of memory!\n"));
1192 ncacn_conn
->count
= 1;
1194 ncacn_conn
->iov
[0].iov_base
= out
->frag
.data
1195 + out
->current_pdu_sent
;
1196 ncacn_conn
->iov
[0].iov_len
= to_send
;
1198 out
->current_pdu_sent
+= to_send
;
1202 * This condition is false for bind packets, or when we haven't yet got
1203 * a full request, and need to wait for more data from the client
1205 while (out
->data_sent_length
< out
->rdata
.length
) {
1206 ok
= create_next_pdu(ncacn_conn
->p
);
1208 DEBUG(3, ("Failed to create next PDU!\n"));
1209 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
1213 ncacn_conn
->iov
= talloc_realloc(ncacn_conn
,
1216 ncacn_conn
->count
+ 1);
1217 if (ncacn_conn
->iov
== NULL
) {
1218 DEBUG(3, ("Out of memory!\n"));
1219 status
= NT_STATUS_NO_MEMORY
;
1223 ncacn_conn
->iov
[ncacn_conn
->count
].iov_base
= out
->frag
.data
;
1224 ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
= out
->frag
.length
;
1226 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
1227 (unsigned int) ncacn_conn
->count
,
1228 (unsigned int) ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
));
1229 dump_data(11, (const uint8_t *) ncacn_conn
->iov
[ncacn_conn
->count
].iov_base
,
1230 ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
);
1231 ncacn_conn
->count
++;
1235 * We still don't have a complete request, go back and wait for more
1238 if (ncacn_conn
->count
== 0) {
1239 /* Wait for the next packet */
1240 subreq
= dcerpc_read_ncacn_packet_send(ncacn_conn
,
1242 ncacn_conn
->tstream
);
1243 if (subreq
== NULL
) {
1244 DEBUG(2, ("Failed to start receving packets\n"));
1245 status
= NT_STATUS_NO_MEMORY
;
1248 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1252 DEBUG(10, ("Sending a total of %u bytes\n",
1253 (unsigned int)ncacn_conn
->p
->out_data
.data_sent_length
));
1255 subreq
= tstream_writev_queue_send(ncacn_conn
,
1257 ncacn_conn
->tstream
,
1258 ncacn_conn
->send_queue
,
1261 if (subreq
== NULL
) {
1262 DEBUG(2, ("Failed to send packet\n"));
1263 status
= NT_STATUS_NO_MEMORY
;
1267 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_done
, ncacn_conn
);
1271 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1272 ncacn_conn
->client_name
, nt_errstr(status
)));
1274 /* Terminate client connection */
1275 talloc_free(ncacn_conn
);
1279 static void dcerpc_ncacn_packet_done(struct tevent_req
*subreq
)
1281 struct dcerpc_ncacn_conn
*ncacn_conn
=
1282 tevent_req_callback_data(subreq
, struct dcerpc_ncacn_conn
);
1283 NTSTATUS status
= NT_STATUS_OK
;
1287 rc
= tstream_writev_queue_recv(subreq
, &sys_errno
);
1288 TALLOC_FREE(subreq
);
1290 DEBUG(2, ("Writev failed!\n"));
1291 status
= map_nt_error_from_unix(sys_errno
);
1295 /* clear out any data that may have been left around */
1296 ncacn_conn
->count
= 0;
1297 TALLOC_FREE(ncacn_conn
->iov
);
1298 data_blob_free(&ncacn_conn
->p
->in_data
.data
);
1299 data_blob_free(&ncacn_conn
->p
->out_data
.frag
);
1300 data_blob_free(&ncacn_conn
->p
->out_data
.rdata
);
1302 talloc_free_children(ncacn_conn
->p
->mem_ctx
);
1304 /* Wait for the next packet */
1305 subreq
= dcerpc_read_ncacn_packet_send(ncacn_conn
,
1307 ncacn_conn
->tstream
);
1308 if (subreq
== NULL
) {
1309 DEBUG(2, ("Failed to start receving packets\n"));
1310 status
= NT_STATUS_NO_MEMORY
;
1314 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1318 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1319 ncacn_conn
->client_name
, nt_errstr(status
)));
1321 /* Terminate client connection */
1322 talloc_free(ncacn_conn
);
1326 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */