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 /* Creates a pipes_struct and initializes it with the information
38 * sent from the client */
39 int make_server_pipes_struct(TALLOC_CTX
*mem_ctx
,
40 struct messaging_context
*msg_ctx
,
41 const char *pipe_name
,
42 enum dcerpc_transport_t transport
,
43 const struct tsocket_address
*remote_address
,
44 const struct tsocket_address
*local_address
,
45 struct auth_session_info
*session_info
,
46 struct pipes_struct
**_p
,
49 struct pipes_struct
*p
;
52 ret
= make_base_pipes_struct(mem_ctx
, msg_ctx
, pipe_name
,
53 transport
, RPC_LITTLE_ENDIAN
,
54 remote_address
, local_address
, &p
);
60 if (session_info
->unix_token
&& session_info
->unix_info
&& session_info
->security_token
) {
61 /* Don't call create_local_token(), we already have the full details here */
62 p
->session_info
= talloc_steal(p
, session_info
);
65 DEBUG(0, ("Supplied session_info in make_server_pipes_struct was incomplete!"));
74 /* Start listening on the appropriate unix socket and setup all is needed to
75 * dispatch requests to the pipes rpc implementation */
77 struct dcerpc_ncacn_listen_state
{
78 struct ndr_syntax_id syntax_id
;
86 struct tevent_context
*ev_ctx
;
87 struct messaging_context
*msg_ctx
;
88 dcerpc_ncacn_disconnect_fn disconnect_fn
;
91 static void named_pipe_listener(struct tevent_context
*ev
,
92 struct tevent_fd
*fde
,
96 int create_named_pipe_socket(const char *pipe_name
)
102 * As lp_ncalrpc_dir() should have 0755, but
103 * lp_ncalrpc_dir()/np should have 0700, we need to
104 * create lp_ncalrpc_dir() first.
106 if (!directory_create_or_exist(lp_ncalrpc_dir(), 0755)) {
107 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
108 lp_ncalrpc_dir(), strerror(errno
)));
112 np_dir
= talloc_asprintf(talloc_tos(), "%s/np", lp_ncalrpc_dir());
114 DEBUG(0, ("Out of memory\n"));
118 if (!directory_create_or_exist_strict(np_dir
, geteuid(), 0700)) {
119 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
120 np_dir
, strerror(errno
)));
124 fd
= create_pipe_sock(np_dir
, pipe_name
, 0700);
126 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
131 DEBUG(10, ("Opened pipe socket fd %d for %s\n", fd
, pipe_name
));
138 bool setup_named_pipe_socket(const char *pipe_name
,
139 struct tevent_context
*ev_ctx
,
140 struct messaging_context
*msg_ctx
)
142 struct dcerpc_ncacn_listen_state
*state
;
143 struct tevent_fd
*fde
;
146 state
= talloc(ev_ctx
, struct dcerpc_ncacn_listen_state
);
148 DEBUG(0, ("Out of memory\n"));
151 state
->ep
.name
= talloc_strdup(state
, pipe_name
);
152 if (state
->ep
.name
== NULL
) {
153 DEBUG(0, ("Out of memory\n"));
156 state
->fd
= create_named_pipe_socket(pipe_name
);
157 if (state
->fd
== -1) {
161 rc
= listen(state
->fd
, 5);
163 DEBUG(0, ("Failed to listen on pipe socket %s: %s\n",
164 pipe_name
, strerror(errno
)));
168 state
->ev_ctx
= ev_ctx
;
169 state
->msg_ctx
= msg_ctx
;
171 DEBUG(10, ("Opened pipe socket fd %d for %s\n",
172 state
->fd
, pipe_name
));
174 fde
= tevent_add_fd(ev_ctx
,
175 state
, state
->fd
, TEVENT_FD_READ
,
176 named_pipe_listener
, state
);
178 DEBUG(0, ("Failed to add event handler!\n"));
182 tevent_fd_set_auto_close(fde
);
186 if (state
->fd
!= -1) {
193 static void named_pipe_listener(struct tevent_context
*ev
,
194 struct tevent_fd
*fde
,
198 struct dcerpc_ncacn_listen_state
*state
=
199 talloc_get_type_abort(private_data
,
200 struct dcerpc_ncacn_listen_state
);
201 struct sockaddr_un sunaddr
;
205 /* TODO: should we have a limit to the number of clients ? */
207 len
= sizeof(sunaddr
);
209 sd
= accept(state
->fd
,
210 (struct sockaddr
*)(void *)&sunaddr
, &len
);
213 if (errno
!= EINTR
) {
214 DEBUG(6, ("Failed to get a valid socket [%s]\n",
220 DEBUG(6, ("Accepted socket %d\n", sd
));
222 named_pipe_accept_function(state
->ev_ctx
,
229 /* This is the core of the rpc server.
230 * Accepts connections from clients and process requests using the appropriate
231 * dispatcher table. */
233 static int named_pipe_destructor(struct named_pipe_client
*npc
)
236 npc
->term_fn(npc
->private_data
);
241 struct named_pipe_client
*named_pipe_client_init(TALLOC_CTX
*mem_ctx
,
242 struct tevent_context
*ev_ctx
,
243 struct messaging_context
*msg_ctx
,
244 const char *pipe_name
,
245 named_pipe_termination_fn
*term_fn
,
247 uint16_t device_state
,
248 uint64_t allocation_size
,
251 struct named_pipe_client
*npc
;
253 npc
= talloc_zero(mem_ctx
, struct named_pipe_client
);
255 DEBUG(0, ("Out of memory!\n"));
258 talloc_set_destructor(npc
, named_pipe_destructor
);
260 npc
->pipe_name
= talloc_strdup(npc
, pipe_name
);
261 if (npc
->pipe_name
== NULL
) {
262 DEBUG(0, ("Out of memory!\n"));
268 npc
->msg_ctx
= msg_ctx
;
269 npc
->term_fn
= term_fn
;
270 npc
->private_data
= private_data
;
272 npc
->file_type
= file_type
;
273 npc
->device_state
= device_state
;
274 npc
->allocation_size
= allocation_size
;
279 static void named_pipe_accept_done(struct tevent_req
*subreq
);
281 void named_pipe_accept_function(struct tevent_context
*ev_ctx
,
282 struct messaging_context
*msg_ctx
,
283 const char *pipe_name
, int fd
,
284 named_pipe_termination_fn
*term_fn
,
287 struct named_pipe_client
*npc
;
288 struct tstream_context
*plain
;
289 struct tevent_req
*subreq
;
292 npc
= talloc_zero(ev_ctx
, struct named_pipe_client
);
294 DEBUG(0, ("Out of memory!\n"));
299 npc
->pipe_name
= talloc_strdup(npc
, pipe_name
);
300 if (npc
->pipe_name
== NULL
) {
301 DEBUG(0, ("Out of memory!\n"));
307 npc
->msg_ctx
= msg_ctx
;
308 npc
->term_fn
= term_fn
;
309 npc
->private_data
= private_data
;
311 talloc_set_destructor(npc
, named_pipe_destructor
);
313 /* make sure socket is in NON blocking state */
314 ret
= set_blocking(fd
, false);
316 DEBUG(2, ("Failed to make socket non-blocking\n"));
322 ret
= tstream_bsd_existing_socket(npc
, fd
, &plain
);
324 DEBUG(2, ("Failed to create tstream socket\n"));
330 npc
->file_type
= FILE_TYPE_MESSAGE_MODE_PIPE
;
331 npc
->device_state
= 0xff | 0x0400 | 0x0100;
332 npc
->allocation_size
= 4096;
334 subreq
= tstream_npa_accept_existing_send(npc
, npc
->ev
, plain
,
337 npc
->allocation_size
);
339 DEBUG(2, ("Failed to start async accept procedure\n"));
344 tevent_req_set_callback(subreq
, named_pipe_accept_done
, npc
);
347 static void named_pipe_packet_done(struct tevent_req
*subreq
);
349 static void named_pipe_accept_done(struct tevent_req
*subreq
)
351 struct auth_session_info_transport
*session_info_transport
;
352 struct named_pipe_client
*npc
=
353 tevent_req_callback_data(subreq
, struct named_pipe_client
);
357 ret
= tstream_npa_accept_existing_recv(subreq
, &error
, npc
,
359 &npc
->remote_client_addr
,
360 &npc
->remote_client_name
,
361 &npc
->local_server_addr
,
362 &npc
->local_server_name
,
363 &session_info_transport
);
365 npc
->session_info
= talloc_move(npc
, &session_info_transport
->session_info
);
369 DEBUG(2, ("Failed to accept named pipe connection! (%s)\n",
375 ret
= make_server_pipes_struct(npc
,
377 npc
->pipe_name
, NCACN_NP
,
378 npc
->remote_client_addr
,
379 npc
->local_server_addr
,
383 DEBUG(2, ("Failed to create pipes_struct! (%s)\n",
388 npc
->write_queue
= tevent_queue_create(npc
, "np_server_write_queue");
389 if (!npc
->write_queue
) {
390 DEBUG(2, ("Failed to set up write queue!\n"));
394 /* And now start receiving and processing packets */
395 subreq
= dcerpc_read_ncacn_packet_send(npc
, npc
->ev
, npc
->tstream
);
397 DEBUG(2, ("Failed to start receving packets\n"));
400 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
404 DEBUG(2, ("Fatal error. Terminating client(%s) connection!\n",
405 npc
->remote_client_name
));
406 /* terminate client connection */
411 void named_pipe_packet_process(struct tevent_req
*subreq
)
413 struct named_pipe_client
*npc
=
414 tevent_req_callback_data(subreq
, struct named_pipe_client
);
415 struct _output_data
*out
= &npc
->p
->out_data
;
416 DATA_BLOB recv_buffer
= data_blob_null
;
417 struct ncacn_packet
*pkt
;
423 status
= dcerpc_read_ncacn_packet_recv(subreq
, npc
, &pkt
, &recv_buffer
);
425 if (!NT_STATUS_IS_OK(status
)) {
429 /* dcerpc_read_ncacn_packet_recv() returns a full PDU */
430 npc
->p
->in_data
.pdu_needed_len
= 0;
431 npc
->p
->in_data
.pdu
= recv_buffer
;
432 if (dcerpc_get_endian_flag(&recv_buffer
) & DCERPC_DREP_LE
) {
433 npc
->p
->endian
= RPC_LITTLE_ENDIAN
;
435 npc
->p
->endian
= RPC_BIG_ENDIAN
;
437 DEBUG(10, ("PDU is in %s Endian format!\n",
438 npc
->p
->endian
? "Big" : "Little"));
439 process_complete_pdu(npc
->p
, pkt
);
441 /* reset pipe state and free PDU */
442 npc
->p
->in_data
.pdu
.length
= 0;
443 talloc_free(recv_buffer
.data
);
446 /* this is needed because of the way DCERPC Binds work in
447 * the RPC marshalling code */
448 to_send
= out
->frag
.length
- out
->current_pdu_sent
;
451 npc
->iov
= talloc_zero(npc
, struct iovec
);
453 status
= NT_STATUS_NO_MEMORY
;
458 npc
->iov
[0].iov_base
= out
->frag
.data
459 + out
->current_pdu_sent
;
460 npc
->iov
[0].iov_len
= to_send
;
462 out
->current_pdu_sent
+= to_send
;
465 /* this condition is false for bind packets, or when we haven't
466 * yet got a full request, and need to wait for more data from
468 while (out
->data_sent_length
< out
->rdata
.length
) {
470 ok
= create_next_pdu(npc
->p
);
472 DEBUG(3, ("Failed to create next PDU!\n"));
473 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
477 npc
->iov
= talloc_realloc(npc
, npc
->iov
,
478 struct iovec
, npc
->count
+ 1);
480 status
= NT_STATUS_NO_MEMORY
;
484 npc
->iov
[npc
->count
].iov_base
= out
->frag
.data
;
485 npc
->iov
[npc
->count
].iov_len
= out
->frag
.length
;
490 /* we still don't have a complete request, go back and wait for more
492 if (npc
->count
== 0) {
493 /* Wait for the next packet */
494 subreq
= dcerpc_read_ncacn_packet_send(npc
, npc
->ev
, npc
->tstream
);
496 DEBUG(2, ("Failed to start receving packets\n"));
497 status
= NT_STATUS_NO_MEMORY
;
500 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
504 DEBUG(10, ("Sending %u fragments in a total of %u bytes\n",
505 (unsigned int)npc
->count
,
506 (unsigned int)npc
->p
->out_data
.data_sent_length
));
508 for (i
= 0; i
< npc
->count
; i
++) {
509 DEBUG(10, ("Sending PDU number: %d, PDU Length: %u\n",
511 (unsigned int)npc
->iov
[i
].iov_len
));
512 dump_data(11, (const uint8_t *)npc
->iov
[i
].iov_base
,
513 npc
->iov
[i
].iov_len
);
515 subreq
= tstream_writev_queue_send(npc
,
522 DEBUG(2, ("Failed to send packet\n"));
523 status
= NT_STATUS_NO_MEMORY
;
526 tevent_req_set_callback(subreq
, named_pipe_packet_done
, npc
);
532 DEBUG(2, ("Fatal error(%s). "
533 "Terminating client(%s) connection!\n",
534 nt_errstr(status
), npc
->remote_client_name
));
535 /* terminate client connection */
540 static void named_pipe_packet_done(struct tevent_req
*subreq
)
542 struct named_pipe_client
*npc
=
543 tevent_req_callback_data(subreq
, struct named_pipe_client
);
547 ret
= tstream_writev_queue_recv(subreq
, &sys_errno
);
550 DEBUG(2, ("Writev failed!\n"));
554 if (tevent_queue_length(npc
->write_queue
) > 0) {
558 if (npc
->p
->fault_state
!= 0) {
559 DEBUG(2, ("Disconnect after fault\n"));
564 /* clear out any data that may have been left around */
566 TALLOC_FREE(npc
->iov
);
567 data_blob_free(&npc
->p
->in_data
.data
);
568 data_blob_free(&npc
->p
->out_data
.frag
);
569 data_blob_free(&npc
->p
->out_data
.rdata
);
571 talloc_free_children(npc
->p
->mem_ctx
);
573 /* Wait for the next packet */
574 subreq
= dcerpc_read_ncacn_packet_send(npc
, npc
->ev
, npc
->tstream
);
576 DEBUG(2, ("Failed to start receving packets\n"));
580 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
584 DEBUG(2, ("Fatal error(%s). "
585 "Terminating client(%s) connection!\n",
586 strerror(sys_errno
), npc
->remote_client_name
));
587 /* terminate client connection */
592 /********************************************************************
593 * Start listening on the tcp/ip socket
594 ********************************************************************/
596 static void dcerpc_ncacn_tcpip_listener(struct tevent_context
*ev
,
597 struct tevent_fd
*fde
,
601 int create_tcpip_socket(const struct sockaddr_storage
*ifss
, uint16_t *port
)
608 for (i
= lp_rpc_low_port(); i
<= lp_rpc_high_port(); i
++) {
609 fd
= open_socket_in(SOCK_STREAM
,
620 fd
= open_socket_in(SOCK_STREAM
,
627 DEBUG(0, ("Failed to create socket on port %u!\n", *port
));
631 DEBUG(10, ("Opened tcpip socket fd %d for port %u\n", fd
, *port
));
636 uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context
*ev_ctx
,
637 struct messaging_context
*msg_ctx
,
638 const struct sockaddr_storage
*ifss
,
641 struct dcerpc_ncacn_listen_state
*state
;
642 struct tevent_fd
*fde
;
645 state
= talloc(ev_ctx
, struct dcerpc_ncacn_listen_state
);
647 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Out of memory\n"));
652 state
->ep
.port
= port
;
653 state
->disconnect_fn
= NULL
;
655 state
->fd
= create_tcpip_socket(ifss
, &state
->ep
.port
);
656 if (state
->fd
== -1) {
660 state
->ev_ctx
= ev_ctx
;
661 state
->msg_ctx
= msg_ctx
;
663 /* ready to listen */
664 set_socket_options(state
->fd
, "SO_KEEPALIVE");
665 set_socket_options(state
->fd
, lp_socket_options());
667 /* Set server socket to non-blocking for the accept. */
668 set_blocking(state
->fd
, false);
670 rc
= listen(state
->fd
, SMBD_LISTEN_BACKLOG
);
672 DEBUG(0,("setup_tcpip_socket: listen - %s\n", strerror(errno
)));
676 DEBUG(10, ("setup_tcpip_socket: openened socket fd %d for port %u\n",
677 state
->fd
, state
->ep
.port
));
679 fde
= tevent_add_fd(state
->ev_ctx
,
683 dcerpc_ncacn_tcpip_listener
,
686 DEBUG(0, ("setup_tcpip_socket: Failed to add event handler!\n"));
690 tevent_fd_set_auto_close(fde
);
692 return state
->ep
.port
;
694 if (state
->fd
!= -1) {
702 static void dcerpc_ncacn_tcpip_listener(struct tevent_context
*ev
,
703 struct tevent_fd
*fde
,
707 struct dcerpc_ncacn_listen_state
*state
=
708 talloc_get_type_abort(private_data
,
709 struct dcerpc_ncacn_listen_state
);
710 struct tsocket_address
*cli_addr
= NULL
;
711 struct tsocket_address
*srv_addr
= NULL
;
712 struct sockaddr_storage addr
;
713 socklen_t in_addrlen
= sizeof(addr
);
717 s
= accept(state
->fd
, (struct sockaddr
*)(void *) &addr
, &in_addrlen
);
719 if (errno
!= EINTR
) {
720 DEBUG(0,("tcpip_listener accept: %s\n",
726 rc
= tsocket_address_bsd_from_sockaddr(state
,
727 (struct sockaddr
*)(void *) &addr
,
735 rc
= getsockname(s
, (struct sockaddr
*)(void *) &addr
, &in_addrlen
);
741 rc
= tsocket_address_bsd_from_sockaddr(state
,
742 (struct sockaddr
*)(void *) &addr
,
750 DEBUG(6, ("tcpip_listener: Accepted socket %d\n", s
));
752 dcerpc_ncacn_accept(state
->ev_ctx
,
762 /********************************************************************
763 * Start listening on the ncalrpc socket
764 ********************************************************************/
766 static void dcerpc_ncalrpc_listener(struct tevent_context
*ev
,
767 struct tevent_fd
*fde
,
771 int create_dcerpc_ncalrpc_socket(const char *name
)
779 if (!directory_create_or_exist(lp_ncalrpc_dir(), 0755)) {
780 DEBUG(0, ("Failed to create ncalrpc directory %s - %s\n",
781 lp_ncalrpc_dir(), strerror(errno
)));
785 fd
= create_pipe_sock(lp_ncalrpc_dir(), name
, 0755);
787 DEBUG(0, ("Failed to create ncalrpc socket! [%s/%s]\n",
788 lp_ncalrpc_dir(), name
));
792 DEBUG(10, ("Opened ncalrpc socket fd %d for %s\n", fd
, name
));
797 bool setup_dcerpc_ncalrpc_socket(struct tevent_context
*ev_ctx
,
798 struct messaging_context
*msg_ctx
,
800 dcerpc_ncacn_disconnect_fn fn
)
802 struct dcerpc_ncacn_listen_state
*state
;
803 struct tevent_fd
*fde
;
806 state
= talloc(ev_ctx
, struct dcerpc_ncacn_listen_state
);
808 DEBUG(0, ("Out of memory\n"));
813 state
->disconnect_fn
= fn
;
819 state
->ep
.name
= talloc_strdup(state
, name
);
820 if (state
->ep
.name
== NULL
) {
821 DEBUG(0, ("Out of memory\n"));
826 state
->fd
= create_dcerpc_ncalrpc_socket(name
);
827 if (state
->fd
== -1) {
831 rc
= listen(state
->fd
, 5);
833 DEBUG(0, ("Failed to listen on ncalrpc socket %s: %s\n",
834 name
, strerror(errno
)));
838 state
->ev_ctx
= ev_ctx
;
839 state
->msg_ctx
= msg_ctx
;
841 /* Set server socket to non-blocking for the accept. */
842 set_blocking(state
->fd
, false);
844 fde
= tevent_add_fd(state
->ev_ctx
,
848 dcerpc_ncalrpc_listener
,
851 DEBUG(0, ("Failed to add event handler for ncalrpc!\n"));
855 tevent_fd_set_auto_close(fde
);
859 if (state
->fd
!= -1) {
867 static void dcerpc_ncalrpc_listener(struct tevent_context
*ev
,
868 struct tevent_fd
*fde
,
872 struct dcerpc_ncacn_listen_state
*state
=
873 talloc_get_type_abort(private_data
,
874 struct dcerpc_ncacn_listen_state
);
875 struct tsocket_address
*cli_addr
= NULL
, *srv_addr
= NULL
;
876 struct sockaddr_un sunaddr
;
877 struct sockaddr
*addr
= (struct sockaddr
*)(void *)&sunaddr
;
878 socklen_t len
= sizeof(sunaddr
);
879 struct sockaddr_un sunaddr_server
;
880 struct sockaddr
*addr_server
= (struct sockaddr
*)(void *)&sunaddr_server
;
881 socklen_t len_server
= sizeof(sunaddr_server
);
885 ZERO_STRUCT(sunaddr
);
886 ZERO_STRUCT(sunaddr_server
);
888 sd
= accept(state
->fd
, addr
, &len
);
890 if (errno
!= EINTR
) {
891 DEBUG(0, ("ncalrpc accept() failed: %s\n", strerror(errno
)));
896 rc
= tsocket_address_bsd_from_sockaddr(state
,
904 rc
= getsockname(sd
, addr_server
, &len_server
);
910 rc
= tsocket_address_bsd_from_sockaddr(state
,
919 DEBUG(10, ("Accepted ncalrpc socket %s (fd: %d)\n",
920 sunaddr
.sun_path
, sd
));
922 dcerpc_ncacn_accept(state
->ev_ctx
,
926 cli_addr
, srv_addr
, sd
,
927 state
->disconnect_fn
);
930 struct dcerpc_ncacn_conn
{
931 enum dcerpc_transport_t transport
;
935 struct pipes_struct
*p
;
936 dcerpc_ncacn_disconnect_fn disconnect_fn
;
938 struct tevent_context
*ev_ctx
;
939 struct messaging_context
*msg_ctx
;
941 struct tstream_context
*tstream
;
942 struct tevent_queue
*send_queue
;
944 struct tsocket_address
*remote_client_addr
;
945 char *remote_client_name
;
946 struct tsocket_address
*local_server_addr
;
947 char *local_server_name
;
948 struct auth_session_info
*session_info
;
954 static void dcerpc_ncacn_packet_process(struct tevent_req
*subreq
);
955 static void dcerpc_ncacn_packet_done(struct tevent_req
*subreq
);
957 void dcerpc_ncacn_accept(struct tevent_context
*ev_ctx
,
958 struct messaging_context
*msg_ctx
,
959 enum dcerpc_transport_t transport
,
961 struct tsocket_address
*cli_addr
,
962 struct tsocket_address
*srv_addr
,
964 dcerpc_ncacn_disconnect_fn fn
) {
965 struct dcerpc_ncacn_conn
*ncacn_conn
;
966 struct tevent_req
*subreq
;
974 DEBUG(10, ("dcerpc_ncacn_accept\n"));
976 ncacn_conn
= talloc_zero(ev_ctx
, struct dcerpc_ncacn_conn
);
977 if (ncacn_conn
== NULL
) {
978 DEBUG(0, ("Out of memory!\n"));
983 ncacn_conn
->transport
= transport
;
984 ncacn_conn
->ev_ctx
= ev_ctx
;
985 ncacn_conn
->msg_ctx
= msg_ctx
;
986 ncacn_conn
->sock
= s
;
987 ncacn_conn
->disconnect_fn
= fn
;
989 ncacn_conn
->remote_client_addr
= talloc_move(ncacn_conn
, &cli_addr
);
990 if (tsocket_address_is_inet(ncacn_conn
->remote_client_addr
, "ip")) {
991 ncacn_conn
->remote_client_name
=
992 tsocket_address_inet_addr_string(ncacn_conn
->remote_client_addr
,
995 ncacn_conn
->remote_client_name
=
996 tsocket_address_unix_path(ncacn_conn
->remote_client_addr
,
999 if (ncacn_conn
->remote_client_name
== NULL
) {
1000 DEBUG(0, ("Out of memory obtaining remote socket address as a string!\n"));
1001 talloc_free(ncacn_conn
);
1006 if (srv_addr
!= NULL
) {
1007 ncacn_conn
->local_server_addr
= talloc_move(ncacn_conn
, &srv_addr
);
1009 if (tsocket_address_is_inet(ncacn_conn
->local_server_addr
, "ip")) {
1010 ncacn_conn
->local_server_name
=
1011 tsocket_address_inet_addr_string(ncacn_conn
->local_server_addr
,
1014 ncacn_conn
->local_server_name
=
1015 tsocket_address_unix_path(ncacn_conn
->local_server_addr
,
1018 if (ncacn_conn
->local_server_name
== NULL
) {
1019 DEBUG(0, ("Out of memory obtaining local socket address as a string!\n"));
1020 talloc_free(ncacn_conn
);
1026 switch (transport
) {
1028 pipe_name
= tsocket_address_string(ncacn_conn
->remote_client_addr
,
1030 if (pipe_name
== NULL
) {
1032 talloc_free(ncacn_conn
);
1038 rc
= getpeereid(s
, &uid
, &gid
);
1040 DEBUG(2, ("Failed to get ncalrpc connecting "
1041 "uid - %s!\n", strerror(errno
)));
1043 if (uid
== sec_initial_uid()) {
1044 TALLOC_FREE(ncacn_conn
->remote_client_addr
);
1046 rc
= tsocket_address_unix_from_path(ncacn_conn
,
1047 "/root/ncalrpc_as_system",
1048 &ncacn_conn
->remote_client_addr
);
1050 DEBUG(0, ("Out of memory building magic ncalrpc_as_system path!\n"));
1051 talloc_free(ncacn_conn
);
1056 TALLOC_FREE(ncacn_conn
->remote_client_name
);
1057 ncacn_conn
->remote_client_name
1058 = tsocket_address_unix_path(ncacn_conn
->remote_client_addr
,
1060 if (ncacn_conn
->remote_client_name
== NULL
) {
1061 DEBUG(0, ("Out of memory getting magic ncalrpc_as_system string!\n"));
1062 talloc_free(ncacn_conn
);
1070 pipe_name
= talloc_strdup(ncacn_conn
,
1072 if (pipe_name
== NULL
) {
1074 talloc_free(ncacn_conn
);
1079 DEBUG(0, ("unknown dcerpc transport: %u!\n",
1081 talloc_free(ncacn_conn
);
1086 rc
= set_blocking(s
, false);
1088 DEBUG(2, ("Failed to set dcerpc socket to non-blocking\n"));
1089 talloc_free(ncacn_conn
);
1095 * As soon as we have tstream_bsd_existing_socket set up it will
1096 * take care of closing the socket.
1098 rc
= tstream_bsd_existing_socket(ncacn_conn
, s
, &ncacn_conn
->tstream
);
1100 DEBUG(2, ("Failed to create tstream socket for dcerpc\n"));
1101 talloc_free(ncacn_conn
);
1106 if (ncacn_conn
->session_info
== NULL
) {
1108 * TODO: use auth_anonymous_session_info() here?
1110 status
= make_session_info_guest(ncacn_conn
,
1111 &ncacn_conn
->session_info
);
1112 if (!NT_STATUS_IS_OK(status
)) {
1113 DEBUG(2, ("Failed to create "
1114 "make_session_info_guest - %s\n",
1115 nt_errstr(status
)));
1116 talloc_free(ncacn_conn
);
1121 rc
= make_server_pipes_struct(ncacn_conn
,
1122 ncacn_conn
->msg_ctx
,
1124 ncacn_conn
->transport
,
1125 ncacn_conn
->remote_client_addr
,
1126 ncacn_conn
->local_server_addr
,
1127 ncacn_conn
->session_info
,
1131 DEBUG(2, ("Failed to create pipe struct - %s",
1132 strerror(sys_errno
)));
1133 talloc_free(ncacn_conn
);
1137 ncacn_conn
->send_queue
= tevent_queue_create(ncacn_conn
,
1138 "dcerpc send queue");
1139 if (ncacn_conn
->send_queue
== NULL
) {
1140 DEBUG(0, ("Out of memory building dcerpc send queue!\n"));
1141 talloc_free(ncacn_conn
);
1145 subreq
= dcerpc_read_ncacn_packet_send(ncacn_conn
,
1147 ncacn_conn
->tstream
);
1148 if (subreq
== NULL
) {
1149 DEBUG(2, ("Failed to send ncacn packet\n"));
1150 talloc_free(ncacn_conn
);
1154 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1156 DEBUG(10, ("dcerpc_ncacn_accept done\n"));
1161 static void dcerpc_ncacn_packet_process(struct tevent_req
*subreq
)
1163 struct dcerpc_ncacn_conn
*ncacn_conn
=
1164 tevent_req_callback_data(subreq
, struct dcerpc_ncacn_conn
);
1166 struct _output_data
*out
= &ncacn_conn
->p
->out_data
;
1167 DATA_BLOB recv_buffer
= data_blob_null
;
1168 struct ncacn_packet
*pkt
;
1173 status
= dcerpc_read_ncacn_packet_recv(subreq
, ncacn_conn
, &pkt
, &recv_buffer
);
1174 TALLOC_FREE(subreq
);
1175 if (!NT_STATUS_IS_OK(status
)) {
1176 if (ncacn_conn
->disconnect_fn
!= NULL
) {
1177 ok
= ncacn_conn
->disconnect_fn(ncacn_conn
->p
);
1179 DEBUG(3, ("Failed to call disconnect function\n"));
1185 /* dcerpc_read_ncacn_packet_recv() returns a full PDU */
1186 ncacn_conn
->p
->in_data
.pdu_needed_len
= 0;
1187 ncacn_conn
->p
->in_data
.pdu
= recv_buffer
;
1188 if (dcerpc_get_endian_flag(&recv_buffer
) & DCERPC_DREP_LE
) {
1189 ncacn_conn
->p
->endian
= RPC_LITTLE_ENDIAN
;
1191 ncacn_conn
->p
->endian
= RPC_BIG_ENDIAN
;
1193 DEBUG(10, ("PDU is in %s Endian format!\n",
1194 ncacn_conn
->p
->endian
? "Big" : "Little"));
1195 process_complete_pdu(ncacn_conn
->p
, pkt
);
1197 /* reset pipe state and free PDU */
1198 ncacn_conn
->p
->in_data
.pdu
.length
= 0;
1199 talloc_free(recv_buffer
.data
);
1203 * This is needed because of the way DCERPC binds work in the RPC
1206 to_send
= out
->frag
.length
- out
->current_pdu_sent
;
1209 DEBUG(10, ("Current_pdu_len = %u, "
1210 "current_pdu_sent = %u "
1211 "Returning %u bytes\n",
1212 (unsigned int)out
->frag
.length
,
1213 (unsigned int)out
->current_pdu_sent
,
1214 (unsigned int)to_send
));
1216 ncacn_conn
->iov
= talloc_zero(ncacn_conn
, struct iovec
);
1217 if (ncacn_conn
->iov
== NULL
) {
1218 status
= NT_STATUS_NO_MEMORY
;
1219 DEBUG(3, ("Out of memory!\n"));
1222 ncacn_conn
->count
= 1;
1224 ncacn_conn
->iov
[0].iov_base
= out
->frag
.data
1225 + out
->current_pdu_sent
;
1226 ncacn_conn
->iov
[0].iov_len
= to_send
;
1228 out
->current_pdu_sent
+= to_send
;
1232 * This condition is false for bind packets, or when we haven't yet got
1233 * a full request, and need to wait for more data from the client
1235 while (out
->data_sent_length
< out
->rdata
.length
) {
1236 ok
= create_next_pdu(ncacn_conn
->p
);
1238 DEBUG(3, ("Failed to create next PDU!\n"));
1239 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
1243 ncacn_conn
->iov
= talloc_realloc(ncacn_conn
,
1246 ncacn_conn
->count
+ 1);
1247 if (ncacn_conn
->iov
== NULL
) {
1248 DEBUG(3, ("Out of memory!\n"));
1249 status
= NT_STATUS_NO_MEMORY
;
1253 ncacn_conn
->iov
[ncacn_conn
->count
].iov_base
= out
->frag
.data
;
1254 ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
= out
->frag
.length
;
1256 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
1257 (unsigned int) ncacn_conn
->count
,
1258 (unsigned int) ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
));
1259 dump_data(11, (const uint8_t *) ncacn_conn
->iov
[ncacn_conn
->count
].iov_base
,
1260 ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
);
1261 ncacn_conn
->count
++;
1265 * We still don't have a complete request, go back and wait for more
1268 if (ncacn_conn
->count
== 0) {
1269 /* Wait for the next packet */
1270 subreq
= dcerpc_read_ncacn_packet_send(ncacn_conn
,
1272 ncacn_conn
->tstream
);
1273 if (subreq
== NULL
) {
1274 DEBUG(2, ("Failed to start receving packets\n"));
1275 status
= NT_STATUS_NO_MEMORY
;
1278 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1282 DEBUG(10, ("Sending a total of %u bytes\n",
1283 (unsigned int)ncacn_conn
->p
->out_data
.data_sent_length
));
1285 subreq
= tstream_writev_queue_send(ncacn_conn
,
1287 ncacn_conn
->tstream
,
1288 ncacn_conn
->send_queue
,
1291 if (subreq
== NULL
) {
1292 DEBUG(2, ("Failed to send packet\n"));
1293 status
= NT_STATUS_NO_MEMORY
;
1297 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_done
, ncacn_conn
);
1301 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1302 ncacn_conn
->remote_client_name
, nt_errstr(status
)));
1304 /* Terminate client connection */
1305 talloc_free(ncacn_conn
);
1309 static void dcerpc_ncacn_packet_done(struct tevent_req
*subreq
)
1311 struct dcerpc_ncacn_conn
*ncacn_conn
=
1312 tevent_req_callback_data(subreq
, struct dcerpc_ncacn_conn
);
1313 NTSTATUS status
= NT_STATUS_OK
;
1317 rc
= tstream_writev_queue_recv(subreq
, &sys_errno
);
1318 TALLOC_FREE(subreq
);
1320 DEBUG(2, ("Writev failed!\n"));
1321 status
= map_nt_error_from_unix(sys_errno
);
1325 if (ncacn_conn
->p
->fault_state
!= 0) {
1326 DEBUG(2, ("Disconnect after fault\n"));
1331 /* clear out any data that may have been left around */
1332 ncacn_conn
->count
= 0;
1333 TALLOC_FREE(ncacn_conn
->iov
);
1334 data_blob_free(&ncacn_conn
->p
->in_data
.data
);
1335 data_blob_free(&ncacn_conn
->p
->out_data
.frag
);
1336 data_blob_free(&ncacn_conn
->p
->out_data
.rdata
);
1338 talloc_free_children(ncacn_conn
->p
->mem_ctx
);
1340 /* Wait for the next packet */
1341 subreq
= dcerpc_read_ncacn_packet_send(ncacn_conn
,
1343 ncacn_conn
->tstream
);
1344 if (subreq
== NULL
) {
1345 DEBUG(2, ("Failed to start receving packets\n"));
1346 status
= NT_STATUS_NO_MEMORY
;
1350 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1354 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1355 ncacn_conn
->remote_client_name
, nt_errstr(status
)));
1357 /* Terminate client connection */
1358 talloc_free(ncacn_conn
);
1362 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */