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"
26 #include "librpc/gen_ndr/netlogon.h"
27 #include "librpc/gen_ndr/auth.h"
28 #include "lib/tsocket/tsocket.h"
29 #include "libcli/named_pipe_auth/npa_tstream.h"
30 #include "../auth/auth_sam_reply.h"
32 #include "rpc_server/rpc_ncacn_np.h"
33 #include "rpc_server/srv_pipe_hnd.h"
34 #include "rpc_server/srv_pipe.h"
36 #define SERVER_TCP_LOW_PORT 1024
37 #define SERVER_TCP_HIGH_PORT 1300
39 static NTSTATUS
auth_anonymous_session_info(TALLOC_CTX
*mem_ctx
,
40 struct auth_session_info
**session_info
)
44 status
= make_session_info_guest(mem_ctx
, session_info
);
45 if (!NT_STATUS_IS_OK(status
)) {
52 /* Creates a pipes_struct and initializes it with the information
53 * sent from the client */
54 static int make_server_pipes_struct(TALLOC_CTX
*mem_ctx
,
55 struct messaging_context
*msg_ctx
,
56 const char *pipe_name
,
57 enum dcerpc_transport_t transport
,
58 bool ncalrpc_as_system
,
59 const struct tsocket_address
*local_address
,
60 const struct tsocket_address
*remote_address
,
61 struct auth_session_info
*session_info
,
62 struct pipes_struct
**_p
,
65 struct pipes_struct
*p
;
69 ret
= make_base_pipes_struct(mem_ctx
, NULL
, pipe_name
,
70 transport
, RPC_LITTLE_ENDIAN
,
72 remote_address
, local_address
, &p
);
79 if (session_info
->unix_token
&& session_info
->unix_info
&& session_info
->security_token
) {
80 /* Don't call create_local_token(), we already have the full details here */
81 p
->session_info
= talloc_steal(p
, session_info
);
84 struct auth_user_info_dc
*auth_user_info_dc
;
85 struct auth_serversupplied_info
*server_info
;
86 struct netr_SamInfo3
*info3
;
88 /* Fake up an auth_user_info_dc for now, to make an info3, to make the session_info structure */
89 auth_user_info_dc
= talloc_zero(p
, struct auth_user_info_dc
);
90 if (!auth_user_info_dc
) {
96 auth_user_info_dc
->num_sids
= session_info
->security_token
->num_sids
;
97 auth_user_info_dc
->sids
= session_info
->security_token
->sids
;
98 auth_user_info_dc
->info
= session_info
->info
;
99 auth_user_info_dc
->user_session_key
= session_info
->session_key
;
101 /* This creates the input structure that make_server_info_info3 is looking for */
102 status
= auth_convert_user_info_dc_saminfo3(p
, auth_user_info_dc
,
105 if (!NT_STATUS_IS_OK(status
)) {
106 DEBUG(1, ("Failed to convert auth_user_info_dc into netr_SamInfo3\n"));
112 status
= make_server_info_info3(p
,
113 info3
->base
.account_name
.string
,
114 info3
->base
.domain
.string
,
115 &server_info
, info3
);
116 if (!NT_STATUS_IS_OK(status
)) {
117 DEBUG(1, ("Failed to init server info\n"));
124 * Some internal functions need a local token to determine access to
127 status
= create_local_token(p
, server_info
, &session_info
->session_key
, &p
->session_info
);
128 talloc_free(server_info
);
129 if (!NT_STATUS_IS_OK(status
)) {
130 DEBUG(1, ("Failed to init local auth token\n"));
141 /* Start listening on the appropriate unix socket and setup all is needed to
142 * dispatch requests to the pipes rpc implementation */
144 struct dcerpc_ncacn_listen_state
{
145 struct ndr_syntax_id syntax_id
;
153 struct tevent_context
*ev_ctx
;
154 struct messaging_context
*msg_ctx
;
155 dcerpc_ncacn_disconnect_fn disconnect_fn
;
158 static void named_pipe_listener(struct tevent_context
*ev
,
159 struct tevent_fd
*fde
,
163 bool setup_named_pipe_socket(const char *pipe_name
,
164 struct tevent_context
*ev_ctx
,
165 struct messaging_context
*msg_ctx
)
167 struct dcerpc_ncacn_listen_state
*state
;
168 struct tevent_fd
*fde
;
171 state
= talloc(ev_ctx
, struct dcerpc_ncacn_listen_state
);
173 DEBUG(0, ("Out of memory\n"));
176 state
->ep
.name
= talloc_strdup(state
, pipe_name
);
177 if (state
->ep
.name
== NULL
) {
178 DEBUG(0, ("Out of memory\n"));
183 state
->ev_ctx
= ev_ctx
;
184 state
->msg_ctx
= msg_ctx
;
187 * As lp_ncalrpc_dir() should have 0755, but
188 * lp_ncalrpc_dir()/np should have 0700, we need to
189 * create lp_ncalrpc_dir() first.
191 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) {
192 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
193 lp_ncalrpc_dir(), strerror(errno
)));
197 np_dir
= talloc_asprintf(state
, "%s/np", lp_ncalrpc_dir());
199 DEBUG(0, ("Out of memory\n"));
203 if (!directory_create_or_exist(np_dir
, geteuid(), 0700)) {
204 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
205 np_dir
, strerror(errno
)));
209 state
->fd
= create_pipe_sock(np_dir
, pipe_name
, 0700);
210 if (state
->fd
== -1) {
211 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
217 DEBUG(10, ("Openened pipe socket fd %d for %s\n",
218 state
->fd
, pipe_name
));
220 fde
= tevent_add_fd(ev_ctx
,
221 state
, state
->fd
, TEVENT_FD_READ
,
222 named_pipe_listener
, state
);
224 DEBUG(0, ("Failed to add event handler!\n"));
228 tevent_fd_set_auto_close(fde
);
232 if (state
->fd
!= -1) {
239 static void named_pipe_accept_function(struct tevent_context
*ev_ctx
,
240 struct messaging_context
*msg_ctx
,
241 const char *pipe_name
,
244 static void named_pipe_listener(struct tevent_context
*ev
,
245 struct tevent_fd
*fde
,
249 struct dcerpc_ncacn_listen_state
*state
=
250 talloc_get_type_abort(private_data
,
251 struct dcerpc_ncacn_listen_state
);
252 struct sockaddr_un sunaddr
;
256 /* TODO: should we have a limit to the number of clients ? */
258 len
= sizeof(sunaddr
);
260 sd
= accept(state
->fd
,
261 (struct sockaddr
*)(void *)&sunaddr
, &len
);
264 if (errno
!= EINTR
) {
265 DEBUG(6, ("Failed to get a valid socket [%s]\n",
271 DEBUG(6, ("Accepted socket %d\n", sd
));
273 named_pipe_accept_function(state
->ev_ctx
,
280 /* This is the core of the rpc server.
281 * Accepts connections from clients and process requests using the appropriate
282 * dispatcher table. */
284 struct named_pipe_client
{
285 const char *pipe_name
;
287 struct tevent_context
*ev
;
288 struct messaging_context
*msg_ctx
;
291 uint16_t device_state
;
292 uint64_t allocation_size
;
294 struct tstream_context
*tstream
;
296 struct tsocket_address
*client
;
298 struct tsocket_address
*server
;
301 struct auth_session_info
*session_info
;
303 struct pipes_struct
*p
;
305 struct tevent_queue
*write_queue
;
311 static void named_pipe_accept_done(struct tevent_req
*subreq
);
313 static void named_pipe_accept_function(struct tevent_context
*ev_ctx
,
314 struct messaging_context
*msg_ctx
,
315 const char *pipe_name
,
318 struct named_pipe_client
*npc
;
319 struct tstream_context
*plain
;
320 struct tevent_req
*subreq
;
323 npc
= talloc_zero(NULL
, struct named_pipe_client
);
325 DEBUG(0, ("Out of memory!\n"));
330 npc
->pipe_name
= talloc_strdup(npc
, pipe_name
);
331 if (npc
->pipe_name
== NULL
) {
332 DEBUG(0, ("Out of memory!\n"));
338 npc
->msg_ctx
= msg_ctx
;
340 /* make sure socket is in NON blocking state */
341 ret
= set_blocking(fd
, false);
343 DEBUG(2, ("Failed to make socket non-blocking\n"));
349 ret
= tstream_bsd_existing_socket(npc
, fd
, &plain
);
351 DEBUG(2, ("Failed to create tstream socket\n"));
357 npc
->file_type
= FILE_TYPE_MESSAGE_MODE_PIPE
;
358 npc
->device_state
= 0xff | 0x0400 | 0x0100;
359 npc
->allocation_size
= 4096;
361 subreq
= tstream_npa_accept_existing_send(npc
, npc
->ev
, plain
,
364 npc
->allocation_size
);
366 DEBUG(2, ("Failed to start async accept procedure\n"));
371 tevent_req_set_callback(subreq
, named_pipe_accept_done
, npc
);
374 static void named_pipe_packet_process(struct tevent_req
*subreq
);
375 static void named_pipe_packet_done(struct tevent_req
*subreq
);
377 static void named_pipe_accept_done(struct tevent_req
*subreq
)
379 struct auth_session_info_transport
*session_info_transport
;
380 struct named_pipe_client
*npc
=
381 tevent_req_callback_data(subreq
, struct named_pipe_client
);
385 ret
= tstream_npa_accept_existing_recv(subreq
, &error
, npc
,
391 &session_info_transport
);
393 npc
->session_info
= talloc_move(npc
, &session_info_transport
->session_info
);
397 DEBUG(2, ("Failed to accept named pipe connection! (%s)\n",
403 ret
= make_server_pipes_struct(npc
,
405 npc
->pipe_name
, NCACN_NP
,
406 false, npc
->server
, npc
->client
, npc
->session_info
,
409 DEBUG(2, ("Failed to create pipes_struct! (%s)\n",
414 npc
->write_queue
= tevent_queue_create(npc
, "np_server_write_queue");
415 if (!npc
->write_queue
) {
416 DEBUG(2, ("Failed to set up write queue!\n"));
420 /* And now start receiving and processing packets */
421 subreq
= dcerpc_read_ncacn_packet_send(npc
, npc
->ev
, npc
->tstream
);
423 DEBUG(2, ("Failed to start receving packets\n"));
426 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
430 DEBUG(2, ("Fatal error. Terminating client(%s) connection!\n",
432 /* terminate client connection */
437 static void named_pipe_packet_process(struct tevent_req
*subreq
)
439 struct named_pipe_client
*npc
=
440 tevent_req_callback_data(subreq
, struct named_pipe_client
);
441 struct _output_data
*out
= &npc
->p
->out_data
;
442 DATA_BLOB recv_buffer
= data_blob_null
;
443 struct ncacn_packet
*pkt
;
451 status
= dcerpc_read_ncacn_packet_recv(subreq
, npc
, &pkt
, &recv_buffer
);
453 if (!NT_STATUS_IS_OK(status
)) {
457 data_left
= recv_buffer
.length
;
458 data
= (char *)recv_buffer
.data
;
462 data_used
= process_incoming_data(npc
->p
, data
, data_left
);
464 DEBUG(3, ("Failed to process dceprc request!\n"));
465 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
469 data_left
-= data_used
;
473 /* Do not leak this buffer, npc is a long lived context */
474 talloc_free(recv_buffer
.data
);
477 /* this is needed because of the way DCERPC Binds work in
478 * the RPC marshalling code */
479 to_send
= out
->frag
.length
- out
->current_pdu_sent
;
482 DEBUG(10, ("Current_pdu_len = %u, "
483 "current_pdu_sent = %u "
484 "Returning %u bytes\n",
485 (unsigned int)out
->frag
.length
,
486 (unsigned int)out
->current_pdu_sent
,
487 (unsigned int)to_send
));
489 npc
->iov
= talloc_zero(npc
, struct iovec
);
491 status
= NT_STATUS_NO_MEMORY
;
496 npc
->iov
[0].iov_base
= out
->frag
.data
497 + out
->current_pdu_sent
;
498 npc
->iov
[0].iov_len
= to_send
;
500 out
->current_pdu_sent
+= to_send
;
503 /* this condition is false for bind packets, or when we haven't
504 * yet got a full request, and need to wait for more data from
506 while (out
->data_sent_length
< out
->rdata
.length
) {
508 ok
= create_next_pdu(npc
->p
);
510 DEBUG(3, ("Failed to create next PDU!\n"));
511 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
515 npc
->iov
= talloc_realloc(npc
, npc
->iov
,
516 struct iovec
, npc
->count
+ 1);
518 status
= NT_STATUS_NO_MEMORY
;
522 npc
->iov
[npc
->count
].iov_base
= out
->frag
.data
;
523 npc
->iov
[npc
->count
].iov_len
= out
->frag
.length
;
525 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
526 (unsigned int)npc
->count
,
527 (unsigned int)npc
->iov
[npc
->count
].iov_len
));
528 dump_data(11, (const uint8_t *)npc
->iov
[npc
->count
].iov_base
,
529 npc
->iov
[npc
->count
].iov_len
);
533 /* we still don't have a complete request, go back and wait for more
535 if (npc
->count
== 0) {
536 /* Wait for the next packet */
537 subreq
= dcerpc_read_ncacn_packet_send(npc
, npc
->ev
, npc
->tstream
);
539 DEBUG(2, ("Failed to start receving packets\n"));
540 status
= NT_STATUS_NO_MEMORY
;
543 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
547 DEBUG(10, ("Sending a total of %u bytes\n",
548 (unsigned int)npc
->p
->out_data
.data_sent_length
));
550 subreq
= tstream_writev_queue_send(npc
, npc
->ev
,
553 npc
->iov
, npc
->count
);
555 DEBUG(2, ("Failed to send packet\n"));
556 status
= NT_STATUS_NO_MEMORY
;
559 tevent_req_set_callback(subreq
, named_pipe_packet_done
, npc
);
563 DEBUG(2, ("Fatal error(%s). "
564 "Terminating client(%s) connection!\n",
565 nt_errstr(status
), npc
->client_name
));
566 /* terminate client connection */
571 static void named_pipe_packet_done(struct tevent_req
*subreq
)
573 struct named_pipe_client
*npc
=
574 tevent_req_callback_data(subreq
, struct named_pipe_client
);
578 ret
= tstream_writev_queue_recv(subreq
, &sys_errno
);
581 DEBUG(2, ("Writev failed!\n"));
585 /* clear out any data that may have been left around */
587 TALLOC_FREE(npc
->iov
);
588 data_blob_free(&npc
->p
->in_data
.data
);
589 data_blob_free(&npc
->p
->out_data
.frag
);
590 data_blob_free(&npc
->p
->out_data
.rdata
);
592 /* Wait for the next packet */
593 subreq
= dcerpc_read_ncacn_packet_send(npc
, npc
->ev
, npc
->tstream
);
595 DEBUG(2, ("Failed to start receving packets\n"));
599 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
603 DEBUG(2, ("Fatal error(%s). "
604 "Terminating client(%s) connection!\n",
605 strerror(sys_errno
), npc
->client_name
));
606 /* terminate client connection */
611 static void dcerpc_ncacn_accept(struct tevent_context
*ev_ctx
,
612 struct messaging_context
*msg_ctx
,
613 enum dcerpc_transport_t transport
,
615 struct tsocket_address
*cli_addr
,
616 struct tsocket_address
*srv_addr
,
618 dcerpc_ncacn_disconnect_fn fn
);
620 /********************************************************************
621 * Start listening on the tcp/ip socket
622 ********************************************************************/
624 static void dcerpc_ncacn_tcpip_listener(struct tevent_context
*ev
,
625 struct tevent_fd
*fde
,
629 uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context
*ev_ctx
,
630 struct messaging_context
*msg_ctx
,
631 const struct sockaddr_storage
*ifss
,
634 struct dcerpc_ncacn_listen_state
*state
;
635 struct tevent_fd
*fde
;
638 state
= talloc(ev_ctx
, struct dcerpc_ncacn_listen_state
);
640 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Out of memory\n"));
645 state
->ep
.port
= port
;
646 state
->disconnect_fn
= NULL
;
648 if (state
->ep
.port
== 0) {
651 for (i
= SERVER_TCP_LOW_PORT
; i
<= SERVER_TCP_HIGH_PORT
; i
++) {
652 state
->fd
= open_socket_in(SOCK_STREAM
,
663 state
->fd
= open_socket_in(SOCK_STREAM
,
669 if (state
->fd
== -1) {
670 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Failed to create "
671 "socket on port %u!\n", state
->ep
.port
));
675 state
->ev_ctx
= ev_ctx
;
676 state
->msg_ctx
= msg_ctx
;
678 /* ready to listen */
679 set_socket_options(state
->fd
, "SO_KEEPALIVE");
680 set_socket_options(state
->fd
, lp_socket_options());
682 /* Set server socket to non-blocking for the accept. */
683 set_blocking(state
->fd
, false);
685 rc
= listen(state
->fd
, SMBD_LISTEN_BACKLOG
);
687 DEBUG(0,("setup_tcpip_socket: listen - %s\n", strerror(errno
)));
691 DEBUG(10, ("setup_tcpip_socket: openened socket fd %d for port %u\n",
692 state
->fd
, state
->ep
.port
));
694 fde
= tevent_add_fd(state
->ev_ctx
,
698 dcerpc_ncacn_tcpip_listener
,
701 DEBUG(0, ("setup_tcpip_socket: Failed to add event handler!\n"));
705 tevent_fd_set_auto_close(fde
);
707 return state
->ep
.port
;
709 if (state
->fd
!= -1) {
717 static void dcerpc_ncacn_tcpip_listener(struct tevent_context
*ev
,
718 struct tevent_fd
*fde
,
722 struct dcerpc_ncacn_listen_state
*state
=
723 talloc_get_type_abort(private_data
,
724 struct dcerpc_ncacn_listen_state
);
725 struct tsocket_address
*cli_addr
= NULL
;
726 struct tsocket_address
*srv_addr
= NULL
;
727 struct sockaddr_storage addr
;
728 socklen_t in_addrlen
= sizeof(addr
);
732 s
= accept(state
->fd
, (struct sockaddr
*)(void *) &addr
, &in_addrlen
);
734 if (errno
!= EINTR
) {
735 DEBUG(0,("tcpip_listener accept: %s\n",
741 rc
= tsocket_address_bsd_from_sockaddr(state
,
742 (struct sockaddr
*)(void *) &addr
,
750 rc
= getsockname(s
, (struct sockaddr
*)(void *) &addr
, &in_addrlen
);
756 rc
= tsocket_address_bsd_from_sockaddr(state
,
757 (struct sockaddr
*)(void *) &addr
,
765 DEBUG(6, ("tcpip_listener: Accepted socket %d\n", s
));
767 dcerpc_ncacn_accept(state
->ev_ctx
,
777 /********************************************************************
778 * Start listening on the ncalrpc socket
779 ********************************************************************/
781 static void dcerpc_ncalrpc_listener(struct tevent_context
*ev
,
782 struct tevent_fd
*fde
,
786 bool setup_dcerpc_ncalrpc_socket(struct tevent_context
*ev_ctx
,
787 struct messaging_context
*msg_ctx
,
789 dcerpc_ncacn_disconnect_fn fn
)
791 struct dcerpc_ncacn_listen_state
*state
;
792 struct tevent_fd
*fde
;
794 state
= talloc(ev_ctx
, struct dcerpc_ncacn_listen_state
);
796 DEBUG(0, ("Out of memory\n"));
801 state
->disconnect_fn
= fn
;
806 state
->ep
.name
= talloc_strdup(state
, name
);
808 if (state
->ep
.name
== NULL
) {
809 DEBUG(0, ("Out of memory\n"));
814 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) {
815 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
816 lp_ncalrpc_dir(), strerror(errno
)));
820 state
->fd
= create_pipe_sock(lp_ncalrpc_dir(), name
, 0755);
821 if (state
->fd
== -1) {
822 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
823 lp_ncalrpc_dir(), name
));
827 DEBUG(10, ("Openened pipe socket fd %d for %s\n", state
->fd
, name
));
829 state
->ev_ctx
= ev_ctx
;
830 state
->msg_ctx
= msg_ctx
;
832 /* Set server socket to non-blocking for the accept. */
833 set_blocking(state
->fd
, false);
835 fde
= tevent_add_fd(state
->ev_ctx
,
839 dcerpc_ncalrpc_listener
,
842 DEBUG(0, ("Failed to add event handler for ncalrpc!\n"));
846 tevent_fd_set_auto_close(fde
);
850 if (state
->fd
!= -1) {
858 static void dcerpc_ncalrpc_listener(struct tevent_context
*ev
,
859 struct tevent_fd
*fde
,
863 struct dcerpc_ncacn_listen_state
*state
=
864 talloc_get_type_abort(private_data
,
865 struct dcerpc_ncacn_listen_state
);
866 struct tsocket_address
*cli_addr
= NULL
;
867 struct sockaddr_un sunaddr
;
868 struct sockaddr
*addr
= (struct sockaddr
*)(void *)&sunaddr
;
869 socklen_t len
= sizeof(sunaddr
);
873 ZERO_STRUCT(sunaddr
);
875 sd
= accept(state
->fd
, addr
, &len
);
877 if (errno
!= EINTR
) {
878 DEBUG(0, ("ncalrpc accept() failed: %s\n", strerror(errno
)));
883 rc
= tsocket_address_bsd_from_sockaddr(state
,
891 DEBUG(10, ("Accepted ncalrpc socket %d\n", sd
));
893 dcerpc_ncacn_accept(state
->ev_ctx
,
898 state
->disconnect_fn
);
901 struct dcerpc_ncacn_conn
{
902 enum dcerpc_transport_t transport
;
906 struct pipes_struct
*p
;
907 dcerpc_ncacn_disconnect_fn disconnect_fn
;
909 struct tevent_context
*ev_ctx
;
910 struct messaging_context
*msg_ctx
;
912 struct tstream_context
*tstream
;
913 struct tevent_queue
*send_queue
;
915 struct tsocket_address
*client
;
917 struct tsocket_address
*server
;
919 struct auth_session_info
*session_info
;
925 static void dcerpc_ncacn_packet_process(struct tevent_req
*subreq
);
926 static void dcerpc_ncacn_packet_done(struct tevent_req
*subreq
);
928 static void dcerpc_ncacn_accept(struct tevent_context
*ev_ctx
,
929 struct messaging_context
*msg_ctx
,
930 enum dcerpc_transport_t transport
,
932 struct tsocket_address
*cli_addr
,
933 struct tsocket_address
*srv_addr
,
935 dcerpc_ncacn_disconnect_fn fn
) {
936 struct dcerpc_ncacn_conn
*ncacn_conn
;
937 struct tevent_req
*subreq
;
939 const char *srv_str
= NULL
;
940 bool system_user
= false;
947 DEBUG(10, ("dcerpc_ncacn_accept\n"));
949 ncacn_conn
= talloc_zero(ev_ctx
, struct dcerpc_ncacn_conn
);
950 if (ncacn_conn
== NULL
) {
951 DEBUG(0, ("Out of memory!\n"));
956 ncacn_conn
->transport
= transport
;
957 ncacn_conn
->ev_ctx
= ev_ctx
;
958 ncacn_conn
->msg_ctx
= msg_ctx
;
959 ncacn_conn
->sock
= s
;
960 ncacn_conn
->disconnect_fn
= fn
;
962 ncacn_conn
->client
= talloc_move(ncacn_conn
, &cli_addr
);
963 if (tsocket_address_is_inet(ncacn_conn
->client
, "ip")) {
964 ncacn_conn
->client_name
=
965 tsocket_address_inet_addr_string(ncacn_conn
->client
,
968 ncacn_conn
->client_name
=
969 tsocket_address_unix_path(ncacn_conn
->client
,
972 if (ncacn_conn
->client_name
== NULL
) {
973 DEBUG(0, ("Out of memory!\n"));
974 talloc_free(ncacn_conn
);
979 if (srv_addr
!= NULL
) {
980 ncacn_conn
->server
= talloc_move(ncacn_conn
, &srv_addr
);
982 ncacn_conn
->server_name
=
983 tsocket_address_inet_addr_string(ncacn_conn
->server
,
985 if (ncacn_conn
->server_name
== NULL
) {
986 DEBUG(0, ("Out of memory!\n"));
987 talloc_free(ncacn_conn
);
995 pipe_name
= tsocket_address_string(ncacn_conn
->client
,
997 if (pipe_name
== NULL
) {
999 talloc_free(ncacn_conn
);
1005 rc
= sys_getpeereid(s
, &uid
);
1007 DEBUG(2, ("Failed to get ncalrpc connecting uid!"));
1009 if (uid
== sec_initial_uid()) {
1014 pipe_name
= talloc_strdup(ncacn_conn
,
1016 if (pipe_name
== NULL
) {
1018 talloc_free(ncacn_conn
);
1023 DEBUG(0, ("unknown dcerpc transport: %u!\n",
1025 talloc_free(ncacn_conn
);
1030 rc
= set_blocking(s
, false);
1032 DEBUG(2, ("Failed to set dcerpc socket to non-blocking\n"));
1033 talloc_free(ncacn_conn
);
1039 * As soon as we have tstream_bsd_existing_socket set up it will
1040 * take care of closing the socket.
1042 rc
= tstream_bsd_existing_socket(ncacn_conn
, s
, &ncacn_conn
->tstream
);
1044 DEBUG(2, ("Failed to create tstream socket for dcerpc\n"));
1045 talloc_free(ncacn_conn
);
1050 if (tsocket_address_is_inet(ncacn_conn
->client
, "ip")) {
1051 cli_str
= ncacn_conn
->client_name
;
1056 if (ncacn_conn
->server
!= NULL
) {
1057 if (tsocket_address_is_inet(ncacn_conn
->server
, "ip")) {
1058 srv_str
= ncacn_conn
->server_name
;
1064 if (ncacn_conn
->session_info
== NULL
) {
1065 status
= auth_anonymous_session_info(ncacn_conn
,
1066 &ncacn_conn
->session_info
);
1067 if (!NT_STATUS_IS_OK(status
)) {
1068 DEBUG(2, ("Failed to create "
1069 "auth_anonymous_session_info - %s\n",
1070 nt_errstr(status
)));
1071 talloc_free(ncacn_conn
);
1076 rc
= make_server_pipes_struct(ncacn_conn
,
1077 ncacn_conn
->msg_ctx
,
1079 ncacn_conn
->transport
,
1083 ncacn_conn
->session_info
,
1087 DEBUG(2, ("Failed to create pipe struct - %s",
1088 strerror(sys_errno
)));
1089 talloc_free(ncacn_conn
);
1093 ncacn_conn
->send_queue
= tevent_queue_create(ncacn_conn
,
1094 "dcerpc send queue");
1095 if (ncacn_conn
->send_queue
== NULL
) {
1096 DEBUG(0, ("Out of memory!\n"));
1097 talloc_free(ncacn_conn
);
1101 subreq
= dcerpc_read_ncacn_packet_send(ncacn_conn
,
1103 ncacn_conn
->tstream
);
1104 if (subreq
== NULL
) {
1105 DEBUG(2, ("Failed to send ncacn packet\n"));
1106 talloc_free(ncacn_conn
);
1110 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1112 DEBUG(10, ("dcerpc_ncacn_accept done\n"));
1117 static void dcerpc_ncacn_packet_process(struct tevent_req
*subreq
)
1119 struct dcerpc_ncacn_conn
*ncacn_conn
=
1120 tevent_req_callback_data(subreq
, struct dcerpc_ncacn_conn
);
1122 struct _output_data
*out
= &ncacn_conn
->p
->out_data
;
1123 DATA_BLOB recv_buffer
= data_blob_null
;
1124 struct ncacn_packet
*pkt
;
1132 status
= dcerpc_read_ncacn_packet_recv(subreq
, ncacn_conn
, &pkt
, &recv_buffer
);
1133 TALLOC_FREE(subreq
);
1134 if (!NT_STATUS_IS_OK(status
)) {
1135 if (ncacn_conn
->disconnect_fn
!= NULL
) {
1136 ok
= ncacn_conn
->disconnect_fn(ncacn_conn
->p
);
1138 DEBUG(3, ("Failed to call disconnect function\n"));
1144 data_left
= recv_buffer
.length
;
1145 data
= (char *) recv_buffer
.data
;
1148 data_used
= process_incoming_data(ncacn_conn
->p
, data
, data_left
);
1149 if (data_used
< 0) {
1150 DEBUG(3, ("Failed to process dcerpc request!\n"));
1151 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
1155 data_left
-= data_used
;
1159 /* Do not leak this buffer */
1160 talloc_free(recv_buffer
.data
);
1164 * This is needed because of the way DCERPC binds work in the RPC
1167 to_send
= out
->frag
.length
- out
->current_pdu_sent
;
1170 DEBUG(10, ("Current_pdu_len = %u, "
1171 "current_pdu_sent = %u "
1172 "Returning %u bytes\n",
1173 (unsigned int)out
->frag
.length
,
1174 (unsigned int)out
->current_pdu_sent
,
1175 (unsigned int)to_send
));
1177 ncacn_conn
->iov
= talloc_zero(ncacn_conn
, struct iovec
);
1178 if (ncacn_conn
->iov
== NULL
) {
1179 status
= NT_STATUS_NO_MEMORY
;
1180 DEBUG(3, ("Out of memory!\n"));
1183 ncacn_conn
->count
= 1;
1185 ncacn_conn
->iov
[0].iov_base
= out
->frag
.data
1186 + out
->current_pdu_sent
;
1187 ncacn_conn
->iov
[0].iov_len
= to_send
;
1189 out
->current_pdu_sent
+= to_send
;
1193 * This condition is false for bind packets, or when we haven't yet got
1194 * a full request, and need to wait for more data from the client
1196 while (out
->data_sent_length
< out
->rdata
.length
) {
1197 ok
= create_next_pdu(ncacn_conn
->p
);
1199 DEBUG(3, ("Failed to create next PDU!\n"));
1200 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
1204 ncacn_conn
->iov
= talloc_realloc(ncacn_conn
,
1207 ncacn_conn
->count
+ 1);
1208 if (ncacn_conn
->iov
== NULL
) {
1209 DEBUG(3, ("Out of memory!\n"));
1210 status
= NT_STATUS_NO_MEMORY
;
1214 ncacn_conn
->iov
[ncacn_conn
->count
].iov_base
= out
->frag
.data
;
1215 ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
= out
->frag
.length
;
1217 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
1218 (unsigned int) ncacn_conn
->count
,
1219 (unsigned int) ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
));
1220 dump_data(11, (const uint8_t *) ncacn_conn
->iov
[ncacn_conn
->count
].iov_base
,
1221 ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
);
1222 ncacn_conn
->count
++;
1226 * We still don't have a complete request, go back and wait for more
1229 if (ncacn_conn
->count
== 0) {
1230 /* Wait for the next packet */
1231 subreq
= dcerpc_read_ncacn_packet_send(ncacn_conn
,
1233 ncacn_conn
->tstream
);
1234 if (subreq
== NULL
) {
1235 DEBUG(2, ("Failed to start receving packets\n"));
1236 status
= NT_STATUS_NO_MEMORY
;
1239 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1243 DEBUG(10, ("Sending a total of %u bytes\n",
1244 (unsigned int)ncacn_conn
->p
->out_data
.data_sent_length
));
1246 subreq
= tstream_writev_queue_send(ncacn_conn
,
1248 ncacn_conn
->tstream
,
1249 ncacn_conn
->send_queue
,
1252 if (subreq
== NULL
) {
1253 DEBUG(2, ("Failed to send packet\n"));
1254 status
= NT_STATUS_NO_MEMORY
;
1258 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_done
, ncacn_conn
);
1262 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1263 ncacn_conn
->client_name
, nt_errstr(status
)));
1265 /* Terminate client connection */
1266 talloc_free(ncacn_conn
);
1270 static void dcerpc_ncacn_packet_done(struct tevent_req
*subreq
)
1272 struct dcerpc_ncacn_conn
*ncacn_conn
=
1273 tevent_req_callback_data(subreq
, struct dcerpc_ncacn_conn
);
1274 NTSTATUS status
= NT_STATUS_OK
;
1278 rc
= tstream_writev_queue_recv(subreq
, &sys_errno
);
1279 TALLOC_FREE(subreq
);
1281 DEBUG(2, ("Writev failed!\n"));
1282 status
= map_nt_error_from_unix(sys_errno
);
1286 /* clear out any data that may have been left around */
1287 ncacn_conn
->count
= 0;
1288 TALLOC_FREE(ncacn_conn
->iov
);
1289 data_blob_free(&ncacn_conn
->p
->in_data
.data
);
1290 data_blob_free(&ncacn_conn
->p
->out_data
.frag
);
1291 data_blob_free(&ncacn_conn
->p
->out_data
.rdata
);
1293 /* Wait for the next packet */
1294 subreq
= dcerpc_read_ncacn_packet_send(ncacn_conn
,
1296 ncacn_conn
->tstream
);
1297 if (subreq
== NULL
) {
1298 DEBUG(2, ("Failed to start receving packets\n"));
1299 status
= NT_STATUS_NO_MEMORY
;
1303 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1307 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1308 ncacn_conn
->client_name
, nt_errstr(status
)));
1310 /* Terminate client connection */
1311 talloc_free(ncacn_conn
);
1315 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */