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
, msg_ctx
, pipe_name
,
70 transport
, RPC_LITTLE_ENDIAN
,
72 remote_address
, local_address
, &p
);
78 if (session_info
->unix_token
&& session_info
->unix_info
&& session_info
->security_token
) {
79 /* Don't call create_local_token(), we already have the full details here */
80 p
->session_info
= talloc_steal(p
, session_info
);
83 struct auth_user_info_dc
*auth_user_info_dc
;
84 struct auth_serversupplied_info
*server_info
;
85 struct netr_SamInfo3
*info3
;
87 /* Fake up an auth_user_info_dc for now, to make an info3, to make the session_info structure */
88 auth_user_info_dc
= talloc_zero(p
, struct auth_user_info_dc
);
89 if (!auth_user_info_dc
) {
95 auth_user_info_dc
->num_sids
= session_info
->security_token
->num_sids
;
96 auth_user_info_dc
->sids
= session_info
->security_token
->sids
;
97 auth_user_info_dc
->info
= session_info
->info
;
98 auth_user_info_dc
->user_session_key
= session_info
->session_key
;
100 /* This creates the input structure that make_server_info_info3 is looking for */
101 status
= auth_convert_user_info_dc_saminfo3(p
, auth_user_info_dc
,
104 if (!NT_STATUS_IS_OK(status
)) {
105 DEBUG(1, ("Failed to convert auth_user_info_dc into netr_SamInfo3\n"));
111 status
= make_server_info_info3(p
,
112 info3
->base
.account_name
.string
,
113 info3
->base
.domain
.string
,
114 &server_info
, info3
);
115 if (!NT_STATUS_IS_OK(status
)) {
116 DEBUG(1, ("Failed to init server info\n"));
123 * Some internal functions need a local token to determine access to
126 status
= create_local_token(p
, server_info
, &session_info
->session_key
, info3
->base
.account_name
.string
,
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(ev_ctx
, 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 talloc_free_children(npc
->p
->mem_ctx
);
594 /* Wait for the next packet */
595 subreq
= dcerpc_read_ncacn_packet_send(npc
, npc
->ev
, npc
->tstream
);
597 DEBUG(2, ("Failed to start receving packets\n"));
601 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
605 DEBUG(2, ("Fatal error(%s). "
606 "Terminating client(%s) connection!\n",
607 strerror(sys_errno
), npc
->client_name
));
608 /* terminate client connection */
613 static void dcerpc_ncacn_accept(struct tevent_context
*ev_ctx
,
614 struct messaging_context
*msg_ctx
,
615 enum dcerpc_transport_t transport
,
617 struct tsocket_address
*cli_addr
,
618 struct tsocket_address
*srv_addr
,
620 dcerpc_ncacn_disconnect_fn fn
);
622 /********************************************************************
623 * Start listening on the tcp/ip socket
624 ********************************************************************/
626 static void dcerpc_ncacn_tcpip_listener(struct tevent_context
*ev
,
627 struct tevent_fd
*fde
,
631 int create_tcpip_socket(const struct sockaddr_storage
*ifss
, uint16_t *port
)
638 for (i
= SERVER_TCP_LOW_PORT
; i
<= SERVER_TCP_HIGH_PORT
; i
++) {
639 fd
= open_socket_in(SOCK_STREAM
,
650 fd
= open_socket_in(SOCK_STREAM
,
657 DEBUG(0, ("Failed to create socket on port %u!\n", *port
));
661 DEBUG(10, ("Opened tcpip socket fd %d for port %u\n", fd
, *port
));
666 uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context
*ev_ctx
,
667 struct messaging_context
*msg_ctx
,
668 const struct sockaddr_storage
*ifss
,
671 struct dcerpc_ncacn_listen_state
*state
;
672 struct tevent_fd
*fde
;
675 state
= talloc(ev_ctx
, struct dcerpc_ncacn_listen_state
);
677 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Out of memory\n"));
682 state
->ep
.port
= port
;
683 state
->disconnect_fn
= NULL
;
685 state
->fd
= create_tcpip_socket(ifss
, &state
->ep
.port
);
686 if (state
->fd
== -1) {
690 state
->ev_ctx
= ev_ctx
;
691 state
->msg_ctx
= msg_ctx
;
693 /* ready to listen */
694 set_socket_options(state
->fd
, "SO_KEEPALIVE");
695 set_socket_options(state
->fd
, lp_socket_options());
697 /* Set server socket to non-blocking for the accept. */
698 set_blocking(state
->fd
, false);
700 rc
= listen(state
->fd
, SMBD_LISTEN_BACKLOG
);
702 DEBUG(0,("setup_tcpip_socket: listen - %s\n", strerror(errno
)));
706 DEBUG(10, ("setup_tcpip_socket: openened socket fd %d for port %u\n",
707 state
->fd
, state
->ep
.port
));
709 fde
= tevent_add_fd(state
->ev_ctx
,
713 dcerpc_ncacn_tcpip_listener
,
716 DEBUG(0, ("setup_tcpip_socket: Failed to add event handler!\n"));
720 tevent_fd_set_auto_close(fde
);
722 return state
->ep
.port
;
724 if (state
->fd
!= -1) {
732 static void dcerpc_ncacn_tcpip_listener(struct tevent_context
*ev
,
733 struct tevent_fd
*fde
,
737 struct dcerpc_ncacn_listen_state
*state
=
738 talloc_get_type_abort(private_data
,
739 struct dcerpc_ncacn_listen_state
);
740 struct tsocket_address
*cli_addr
= NULL
;
741 struct tsocket_address
*srv_addr
= NULL
;
742 struct sockaddr_storage addr
;
743 socklen_t in_addrlen
= sizeof(addr
);
747 s
= accept(state
->fd
, (struct sockaddr
*)(void *) &addr
, &in_addrlen
);
749 if (errno
!= EINTR
) {
750 DEBUG(0,("tcpip_listener accept: %s\n",
756 rc
= tsocket_address_bsd_from_sockaddr(state
,
757 (struct sockaddr
*)(void *) &addr
,
765 rc
= getsockname(s
, (struct sockaddr
*)(void *) &addr
, &in_addrlen
);
771 rc
= tsocket_address_bsd_from_sockaddr(state
,
772 (struct sockaddr
*)(void *) &addr
,
780 DEBUG(6, ("tcpip_listener: Accepted socket %d\n", s
));
782 dcerpc_ncacn_accept(state
->ev_ctx
,
792 /********************************************************************
793 * Start listening on the ncalrpc socket
794 ********************************************************************/
796 static void dcerpc_ncalrpc_listener(struct tevent_context
*ev
,
797 struct tevent_fd
*fde
,
801 bool setup_dcerpc_ncalrpc_socket(struct tevent_context
*ev_ctx
,
802 struct messaging_context
*msg_ctx
,
804 dcerpc_ncacn_disconnect_fn fn
)
806 struct dcerpc_ncacn_listen_state
*state
;
807 struct tevent_fd
*fde
;
809 state
= talloc(ev_ctx
, struct dcerpc_ncacn_listen_state
);
811 DEBUG(0, ("Out of memory\n"));
816 state
->disconnect_fn
= fn
;
821 state
->ep
.name
= talloc_strdup(state
, name
);
823 if (state
->ep
.name
== NULL
) {
824 DEBUG(0, ("Out of memory\n"));
829 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) {
830 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
831 lp_ncalrpc_dir(), strerror(errno
)));
835 state
->fd
= create_pipe_sock(lp_ncalrpc_dir(), name
, 0755);
836 if (state
->fd
== -1) {
837 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
838 lp_ncalrpc_dir(), name
));
842 DEBUG(10, ("Openened pipe socket fd %d for %s\n", state
->fd
, name
));
844 state
->ev_ctx
= ev_ctx
;
845 state
->msg_ctx
= msg_ctx
;
847 /* Set server socket to non-blocking for the accept. */
848 set_blocking(state
->fd
, false);
850 fde
= tevent_add_fd(state
->ev_ctx
,
854 dcerpc_ncalrpc_listener
,
857 DEBUG(0, ("Failed to add event handler for ncalrpc!\n"));
861 tevent_fd_set_auto_close(fde
);
865 if (state
->fd
!= -1) {
873 static void dcerpc_ncalrpc_listener(struct tevent_context
*ev
,
874 struct tevent_fd
*fde
,
878 struct dcerpc_ncacn_listen_state
*state
=
879 talloc_get_type_abort(private_data
,
880 struct dcerpc_ncacn_listen_state
);
881 struct tsocket_address
*cli_addr
= NULL
;
882 struct sockaddr_un sunaddr
;
883 struct sockaddr
*addr
= (struct sockaddr
*)(void *)&sunaddr
;
884 socklen_t len
= sizeof(sunaddr
);
888 ZERO_STRUCT(sunaddr
);
890 sd
= accept(state
->fd
, addr
, &len
);
892 if (errno
!= EINTR
) {
893 DEBUG(0, ("ncalrpc accept() failed: %s\n", strerror(errno
)));
898 rc
= tsocket_address_bsd_from_sockaddr(state
,
906 DEBUG(10, ("Accepted ncalrpc socket %d\n", sd
));
908 dcerpc_ncacn_accept(state
->ev_ctx
,
913 state
->disconnect_fn
);
916 struct dcerpc_ncacn_conn
{
917 enum dcerpc_transport_t transport
;
921 struct pipes_struct
*p
;
922 dcerpc_ncacn_disconnect_fn disconnect_fn
;
924 struct tevent_context
*ev_ctx
;
925 struct messaging_context
*msg_ctx
;
927 struct tstream_context
*tstream
;
928 struct tevent_queue
*send_queue
;
930 struct tsocket_address
*client
;
932 struct tsocket_address
*server
;
934 struct auth_session_info
*session_info
;
940 static void dcerpc_ncacn_packet_process(struct tevent_req
*subreq
);
941 static void dcerpc_ncacn_packet_done(struct tevent_req
*subreq
);
943 static void dcerpc_ncacn_accept(struct tevent_context
*ev_ctx
,
944 struct messaging_context
*msg_ctx
,
945 enum dcerpc_transport_t transport
,
947 struct tsocket_address
*cli_addr
,
948 struct tsocket_address
*srv_addr
,
950 dcerpc_ncacn_disconnect_fn fn
) {
951 struct dcerpc_ncacn_conn
*ncacn_conn
;
952 struct tevent_req
*subreq
;
953 bool system_user
= false;
960 DEBUG(10, ("dcerpc_ncacn_accept\n"));
962 ncacn_conn
= talloc_zero(ev_ctx
, struct dcerpc_ncacn_conn
);
963 if (ncacn_conn
== NULL
) {
964 DEBUG(0, ("Out of memory!\n"));
969 ncacn_conn
->transport
= transport
;
970 ncacn_conn
->ev_ctx
= ev_ctx
;
971 ncacn_conn
->msg_ctx
= msg_ctx
;
972 ncacn_conn
->sock
= s
;
973 ncacn_conn
->disconnect_fn
= fn
;
975 ncacn_conn
->client
= talloc_move(ncacn_conn
, &cli_addr
);
976 if (tsocket_address_is_inet(ncacn_conn
->client
, "ip")) {
977 ncacn_conn
->client_name
=
978 tsocket_address_inet_addr_string(ncacn_conn
->client
,
981 ncacn_conn
->client_name
=
982 tsocket_address_unix_path(ncacn_conn
->client
,
985 if (ncacn_conn
->client_name
== NULL
) {
986 DEBUG(0, ("Out of memory!\n"));
987 talloc_free(ncacn_conn
);
992 if (srv_addr
!= NULL
) {
993 ncacn_conn
->server
= talloc_move(ncacn_conn
, &srv_addr
);
995 ncacn_conn
->server_name
=
996 tsocket_address_inet_addr_string(ncacn_conn
->server
,
998 if (ncacn_conn
->server_name
== NULL
) {
999 DEBUG(0, ("Out of memory!\n"));
1000 talloc_free(ncacn_conn
);
1006 switch (transport
) {
1008 pipe_name
= tsocket_address_string(ncacn_conn
->client
,
1010 if (pipe_name
== NULL
) {
1012 talloc_free(ncacn_conn
);
1018 rc
= sys_getpeereid(s
, &uid
);
1020 DEBUG(2, ("Failed to get ncalrpc connecting uid!"));
1022 if (uid
== sec_initial_uid()) {
1027 pipe_name
= talloc_strdup(ncacn_conn
,
1029 if (pipe_name
== NULL
) {
1031 talloc_free(ncacn_conn
);
1036 DEBUG(0, ("unknown dcerpc transport: %u!\n",
1038 talloc_free(ncacn_conn
);
1043 rc
= set_blocking(s
, false);
1045 DEBUG(2, ("Failed to set dcerpc socket to non-blocking\n"));
1046 talloc_free(ncacn_conn
);
1052 * As soon as we have tstream_bsd_existing_socket set up it will
1053 * take care of closing the socket.
1055 rc
= tstream_bsd_existing_socket(ncacn_conn
, s
, &ncacn_conn
->tstream
);
1057 DEBUG(2, ("Failed to create tstream socket for dcerpc\n"));
1058 talloc_free(ncacn_conn
);
1063 if (ncacn_conn
->session_info
== NULL
) {
1064 status
= auth_anonymous_session_info(ncacn_conn
,
1065 &ncacn_conn
->session_info
);
1066 if (!NT_STATUS_IS_OK(status
)) {
1067 DEBUG(2, ("Failed to create "
1068 "auth_anonymous_session_info - %s\n",
1069 nt_errstr(status
)));
1070 talloc_free(ncacn_conn
);
1075 rc
= make_server_pipes_struct(ncacn_conn
,
1076 ncacn_conn
->msg_ctx
,
1078 ncacn_conn
->transport
,
1082 ncacn_conn
->session_info
,
1086 DEBUG(2, ("Failed to create pipe struct - %s",
1087 strerror(sys_errno
)));
1088 talloc_free(ncacn_conn
);
1092 ncacn_conn
->send_queue
= tevent_queue_create(ncacn_conn
,
1093 "dcerpc send queue");
1094 if (ncacn_conn
->send_queue
== NULL
) {
1095 DEBUG(0, ("Out of memory!\n"));
1096 talloc_free(ncacn_conn
);
1100 subreq
= dcerpc_read_ncacn_packet_send(ncacn_conn
,
1102 ncacn_conn
->tstream
);
1103 if (subreq
== NULL
) {
1104 DEBUG(2, ("Failed to send ncacn packet\n"));
1105 talloc_free(ncacn_conn
);
1109 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1111 DEBUG(10, ("dcerpc_ncacn_accept done\n"));
1116 static void dcerpc_ncacn_packet_process(struct tevent_req
*subreq
)
1118 struct dcerpc_ncacn_conn
*ncacn_conn
=
1119 tevent_req_callback_data(subreq
, struct dcerpc_ncacn_conn
);
1121 struct _output_data
*out
= &ncacn_conn
->p
->out_data
;
1122 DATA_BLOB recv_buffer
= data_blob_null
;
1123 struct ncacn_packet
*pkt
;
1131 status
= dcerpc_read_ncacn_packet_recv(subreq
, ncacn_conn
, &pkt
, &recv_buffer
);
1132 TALLOC_FREE(subreq
);
1133 if (!NT_STATUS_IS_OK(status
)) {
1134 if (ncacn_conn
->disconnect_fn
!= NULL
) {
1135 ok
= ncacn_conn
->disconnect_fn(ncacn_conn
->p
);
1137 DEBUG(3, ("Failed to call disconnect function\n"));
1143 data_left
= recv_buffer
.length
;
1144 data
= (char *) recv_buffer
.data
;
1147 data_used
= process_incoming_data(ncacn_conn
->p
, data
, data_left
);
1148 if (data_used
< 0) {
1149 DEBUG(3, ("Failed to process dcerpc request!\n"));
1150 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
1154 data_left
-= data_used
;
1158 /* Do not leak this buffer */
1159 talloc_free(recv_buffer
.data
);
1163 * This is needed because of the way DCERPC binds work in the RPC
1166 to_send
= out
->frag
.length
- out
->current_pdu_sent
;
1169 DEBUG(10, ("Current_pdu_len = %u, "
1170 "current_pdu_sent = %u "
1171 "Returning %u bytes\n",
1172 (unsigned int)out
->frag
.length
,
1173 (unsigned int)out
->current_pdu_sent
,
1174 (unsigned int)to_send
));
1176 ncacn_conn
->iov
= talloc_zero(ncacn_conn
, struct iovec
);
1177 if (ncacn_conn
->iov
== NULL
) {
1178 status
= NT_STATUS_NO_MEMORY
;
1179 DEBUG(3, ("Out of memory!\n"));
1182 ncacn_conn
->count
= 1;
1184 ncacn_conn
->iov
[0].iov_base
= out
->frag
.data
1185 + out
->current_pdu_sent
;
1186 ncacn_conn
->iov
[0].iov_len
= to_send
;
1188 out
->current_pdu_sent
+= to_send
;
1192 * This condition is false for bind packets, or when we haven't yet got
1193 * a full request, and need to wait for more data from the client
1195 while (out
->data_sent_length
< out
->rdata
.length
) {
1196 ok
= create_next_pdu(ncacn_conn
->p
);
1198 DEBUG(3, ("Failed to create next PDU!\n"));
1199 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
1203 ncacn_conn
->iov
= talloc_realloc(ncacn_conn
,
1206 ncacn_conn
->count
+ 1);
1207 if (ncacn_conn
->iov
== NULL
) {
1208 DEBUG(3, ("Out of memory!\n"));
1209 status
= NT_STATUS_NO_MEMORY
;
1213 ncacn_conn
->iov
[ncacn_conn
->count
].iov_base
= out
->frag
.data
;
1214 ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
= out
->frag
.length
;
1216 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
1217 (unsigned int) ncacn_conn
->count
,
1218 (unsigned int) ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
));
1219 dump_data(11, (const uint8_t *) ncacn_conn
->iov
[ncacn_conn
->count
].iov_base
,
1220 ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
);
1221 ncacn_conn
->count
++;
1225 * We still don't have a complete request, go back and wait for more
1228 if (ncacn_conn
->count
== 0) {
1229 /* Wait for the next packet */
1230 subreq
= dcerpc_read_ncacn_packet_send(ncacn_conn
,
1232 ncacn_conn
->tstream
);
1233 if (subreq
== NULL
) {
1234 DEBUG(2, ("Failed to start receving packets\n"));
1235 status
= NT_STATUS_NO_MEMORY
;
1238 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1242 DEBUG(10, ("Sending a total of %u bytes\n",
1243 (unsigned int)ncacn_conn
->p
->out_data
.data_sent_length
));
1245 subreq
= tstream_writev_queue_send(ncacn_conn
,
1247 ncacn_conn
->tstream
,
1248 ncacn_conn
->send_queue
,
1251 if (subreq
== NULL
) {
1252 DEBUG(2, ("Failed to send packet\n"));
1253 status
= NT_STATUS_NO_MEMORY
;
1257 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_done
, ncacn_conn
);
1261 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1262 ncacn_conn
->client_name
, nt_errstr(status
)));
1264 /* Terminate client connection */
1265 talloc_free(ncacn_conn
);
1269 static void dcerpc_ncacn_packet_done(struct tevent_req
*subreq
)
1271 struct dcerpc_ncacn_conn
*ncacn_conn
=
1272 tevent_req_callback_data(subreq
, struct dcerpc_ncacn_conn
);
1273 NTSTATUS status
= NT_STATUS_OK
;
1277 rc
= tstream_writev_queue_recv(subreq
, &sys_errno
);
1278 TALLOC_FREE(subreq
);
1280 DEBUG(2, ("Writev failed!\n"));
1281 status
= map_nt_error_from_unix(sys_errno
);
1285 /* clear out any data that may have been left around */
1286 ncacn_conn
->count
= 0;
1287 TALLOC_FREE(ncacn_conn
->iov
);
1288 data_blob_free(&ncacn_conn
->p
->in_data
.data
);
1289 data_blob_free(&ncacn_conn
->p
->out_data
.frag
);
1290 data_blob_free(&ncacn_conn
->p
->out_data
.rdata
);
1292 talloc_free_children(ncacn_conn
->p
->mem_ctx
);
1294 /* Wait for the next packet */
1295 subreq
= dcerpc_read_ncacn_packet_send(ncacn_conn
,
1297 ncacn_conn
->tstream
);
1298 if (subreq
== NULL
) {
1299 DEBUG(2, ("Failed to start receving packets\n"));
1300 status
= NT_STATUS_NO_MEMORY
;
1304 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1308 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1309 ncacn_conn
->client_name
, nt_errstr(status
)));
1311 /* Terminate client connection */
1312 talloc_free(ncacn_conn
);
1316 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */