2 Unix SMB/Netbios implementation.
3 Generic infrstructure for RPC Daemons
4 Copyright (C) Simo Sorce 2010
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "rpc_server/rpc_server.h"
24 #include "librpc/gen_ndr/netlogon.h"
25 #include "librpc/gen_ndr/auth.h"
26 #include "lib/tsocket/tsocket.h"
27 #include "libcli/named_pipe_auth/npa_tstream.h"
28 #include "../auth/auth_sam_reply.h"
30 #include "rpc_server/rpc_ncacn_np.h"
31 #include "rpc_server/srv_pipe_hnd.h"
32 #include "rpc_server/srv_pipe.h"
34 #define SERVER_TCP_LOW_PORT 1024
35 #define SERVER_TCP_HIGH_PORT 1300
37 static NTSTATUS
auth_anonymous_session_info(TALLOC_CTX
*mem_ctx
,
38 struct auth_session_info
**session_info
)
40 struct auth_session_info
*i
;
41 struct auth_serversupplied_info
*s
;
42 struct auth_user_info_dc
*u
;
43 union netr_Validation val
;
46 i
= talloc_zero(mem_ctx
, struct auth_session_info
);
48 return NT_STATUS_NO_MEMORY
;
51 status
= make_server_info_guest(i
, &s
);
52 if (!NT_STATUS_IS_OK(status
)) {
56 i
->security_token
= s
->security_token
;
57 i
->session_key
= s
->session_key
;
61 status
= make_user_info_dc_netlogon_validation(mem_ctx
,
66 if (!NT_STATUS_IS_OK(status
)) {
67 DEBUG(0, ("conversion of info3 into user_info_dc failed!\n"));
70 i
->info
= talloc_move(i
, &u
->info
);
78 /* Creates a pipes_struct and initializes it with the information
79 * sent from the client */
80 static int make_server_pipes_struct(TALLOC_CTX
*mem_ctx
,
81 const char *pipe_name
,
82 enum dcerpc_transport_t transport
,
83 bool ncalrpc_as_system
,
84 const char *client_address
,
85 const char *server_address
,
86 struct auth_session_info
*session_info
,
87 struct pipes_struct
**_p
,
90 struct netr_SamInfo3
*info3
;
91 struct auth_user_info_dc
*auth_user_info_dc
;
92 struct pipes_struct
*p
;
93 struct auth_serversupplied_info
*server_info
;
96 p
= talloc_zero(mem_ctx
, struct pipes_struct
);
102 p
->transport
= transport
;
103 p
->ncalrpc_as_system
= ncalrpc_as_system
;
105 p
->mem_ctx
= talloc_named(p
, 0, "pipe %s %p", pipe_name
, p
);
112 data_blob_free(&p
->in_data
.data
);
113 data_blob_free(&p
->in_data
.pdu
);
115 p
->endian
= RPC_LITTLE_ENDIAN
;
117 /* Fake up an auth_user_info_dc for now, to make an info3, to make the session_info structure */
118 auth_user_info_dc
= talloc_zero(p
, struct auth_user_info_dc
);
119 if (!auth_user_info_dc
) {
125 auth_user_info_dc
->num_sids
= session_info
->security_token
->num_sids
;
126 auth_user_info_dc
->sids
= session_info
->security_token
->sids
;
127 auth_user_info_dc
->info
= session_info
->info
;
128 auth_user_info_dc
->user_session_key
= session_info
->session_key
;
130 /* This creates the input structure that make_server_info_info3 is looking for */
131 status
= auth_convert_user_info_dc_saminfo3(p
, auth_user_info_dc
,
134 if (!NT_STATUS_IS_OK(status
)) {
135 DEBUG(1, ("Failed to convert auth_user_info_dc into netr_SamInfo3\n"));
141 status
= make_server_info_info3(p
,
142 info3
->base
.account_name
.string
,
143 info3
->base
.domain
.string
,
144 &server_info
, info3
);
145 if (!NT_STATUS_IS_OK(status
)) {
146 DEBUG(1, ("Failed to init server info\n"));
153 * Some internal functions need a local token to determine access to
156 status
= create_local_token(p
, server_info
, &session_info
->session_key
, &p
->session_info
);
157 talloc_free(server_info
);
158 if (!NT_STATUS_IS_OK(status
)) {
159 DEBUG(1, ("Failed to init local auth token\n"));
165 /* Now override the session_info->security_token with the exact
166 * security_token we were given from the other side,
167 * regardless of what we just calculated */
168 p
->session_info
->security_token
= talloc_move(p
->session_info
, &session_info
->security_token
);
170 p
->client_id
= talloc_zero(p
, struct client_address
);
176 strlcpy(p
->client_id
->addr
,
177 client_address
, sizeof(p
->client_id
->addr
));
178 p
->client_id
->name
= talloc_strdup(p
->client_id
, client_address
);
179 if (p
->client_id
->name
== NULL
) {
185 if (server_address
!= NULL
) {
186 p
->server_id
= talloc_zero(p
, struct client_address
);
187 if (p
->client_id
== NULL
) {
193 strlcpy(p
->server_id
->addr
,
195 sizeof(p
->server_id
->addr
));
197 p
->server_id
->name
= talloc_strdup(p
->server_id
,
199 if (p
->server_id
->name
== NULL
) {
206 talloc_set_destructor(p
, close_internal_rpc_pipe_hnd
);
212 /* Start listening on the appropriate unix socket and setup all is needed to
213 * dispatch requests to the pipes rpc implementation */
215 struct dcerpc_ncacn_listen_state
{
216 struct ndr_syntax_id syntax_id
;
224 struct tevent_context
*ev_ctx
;
225 struct messaging_context
*msg_ctx
;
226 dcerpc_ncacn_disconnect_fn disconnect_fn
;
229 static void named_pipe_listener(struct tevent_context
*ev
,
230 struct tevent_fd
*fde
,
234 bool setup_named_pipe_socket(const char *pipe_name
,
235 struct tevent_context
*ev_ctx
)
237 struct dcerpc_ncacn_listen_state
*state
;
238 struct tevent_fd
*fde
;
241 state
= talloc(ev_ctx
, struct dcerpc_ncacn_listen_state
);
243 DEBUG(0, ("Out of memory\n"));
246 state
->ep
.name
= talloc_strdup(state
, pipe_name
);
247 if (state
->ep
.name
== NULL
) {
248 DEBUG(0, ("Out of memory\n"));
254 * As lp_ncalrpc_dir() should have 0755, but
255 * lp_ncalrpc_dir()/np should have 0700, we need to
256 * create lp_ncalrpc_dir() first.
258 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) {
259 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
260 lp_ncalrpc_dir(), strerror(errno
)));
264 np_dir
= talloc_asprintf(state
, "%s/np", lp_ncalrpc_dir());
266 DEBUG(0, ("Out of memory\n"));
270 if (!directory_create_or_exist(np_dir
, geteuid(), 0700)) {
271 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
272 np_dir
, strerror(errno
)));
276 state
->fd
= create_pipe_sock(np_dir
, pipe_name
, 0700);
277 if (state
->fd
== -1) {
278 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
283 DEBUG(10, ("Openened pipe socket fd %d for %s\n",
284 state
->fd
, pipe_name
));
286 fde
= tevent_add_fd(ev_ctx
,
287 state
, state
->fd
, TEVENT_FD_READ
,
288 named_pipe_listener
, state
);
290 DEBUG(0, ("Failed to add event handler!\n"));
294 tevent_fd_set_auto_close(fde
);
298 if (state
->fd
!= -1) {
305 static void named_pipe_accept_function(const char *pipe_name
, int fd
);
307 static void named_pipe_listener(struct tevent_context
*ev
,
308 struct tevent_fd
*fde
,
312 struct dcerpc_ncacn_listen_state
*state
=
313 talloc_get_type_abort(private_data
,
314 struct dcerpc_ncacn_listen_state
);
315 struct sockaddr_un sunaddr
;
319 /* TODO: should we have a limit to the number of clients ? */
321 len
= sizeof(sunaddr
);
323 sd
= accept(state
->fd
,
324 (struct sockaddr
*)(void *)&sunaddr
, &len
);
327 if (errno
!= EINTR
) {
328 DEBUG(6, ("Failed to get a valid socket [%s]\n",
334 DEBUG(6, ("Accepted socket %d\n", sd
));
336 named_pipe_accept_function(state
->ep
.name
, sd
);
340 /* This is the core of the rpc server.
341 * Accepts connections from clients and process requests using the appropriate
342 * dispatcher table. */
344 struct named_pipe_client
{
345 const char *pipe_name
;
347 struct tevent_context
*ev
;
348 struct messaging_context
*msg_ctx
;
351 uint16_t device_state
;
352 uint64_t allocation_size
;
354 struct tstream_context
*tstream
;
356 struct tsocket_address
*client
;
358 struct tsocket_address
*server
;
360 struct auth_session_info
*session_info
;
362 struct pipes_struct
*p
;
364 struct tevent_queue
*write_queue
;
370 static void named_pipe_accept_done(struct tevent_req
*subreq
);
372 static void named_pipe_accept_function(const char *pipe_name
, int fd
)
374 struct named_pipe_client
*npc
;
375 struct tstream_context
*plain
;
376 struct tevent_req
*subreq
;
379 npc
= talloc_zero(NULL
, struct named_pipe_client
);
381 DEBUG(0, ("Out of memory!\n"));
385 npc
->pipe_name
= pipe_name
;
386 npc
->ev
= server_event_context();
387 npc
->msg_ctx
= server_messaging_context();
389 /* make sure socket is in NON blocking state */
390 ret
= set_blocking(fd
, false);
392 DEBUG(2, ("Failed to make socket non-blocking\n"));
398 ret
= tstream_bsd_existing_socket(npc
, fd
, &plain
);
400 DEBUG(2, ("Failed to create tstream socket\n"));
406 npc
->file_type
= FILE_TYPE_MESSAGE_MODE_PIPE
;
407 npc
->device_state
= 0xff | 0x0400 | 0x0100;
408 npc
->allocation_size
= 4096;
410 subreq
= tstream_npa_accept_existing_send(npc
, npc
->ev
, plain
,
413 npc
->allocation_size
);
415 DEBUG(2, ("Failed to start async accept procedure\n"));
420 tevent_req_set_callback(subreq
, named_pipe_accept_done
, npc
);
423 static void named_pipe_packet_process(struct tevent_req
*subreq
);
424 static void named_pipe_packet_done(struct tevent_req
*subreq
);
426 static void named_pipe_accept_done(struct tevent_req
*subreq
)
428 struct auth_session_info_transport
*session_info_transport
;
429 struct named_pipe_client
*npc
=
430 tevent_req_callback_data(subreq
, struct named_pipe_client
);
431 const char *cli_addr
;
435 ret
= tstream_npa_accept_existing_recv(subreq
, &error
, npc
,
441 &session_info_transport
);
443 npc
->session_info
= talloc_move(npc
, &session_info_transport
->session_info
);
447 DEBUG(2, ("Failed to accept named pipe connection! (%s)\n",
453 if (tsocket_address_is_inet(npc
->client
, "ip")) {
454 cli_addr
= tsocket_address_inet_addr_string(npc
->client
,
456 if (cli_addr
== NULL
) {
464 ret
= make_server_pipes_struct(npc
,
465 npc
->pipe_name
, NCACN_NP
,
466 false, cli_addr
, NULL
, npc
->session_info
,
469 DEBUG(2, ("Failed to create pipes_struct! (%s)\n",
473 npc
->p
->msg_ctx
= npc
->msg_ctx
;
475 npc
->write_queue
= tevent_queue_create(npc
, "np_server_write_queue");
476 if (!npc
->write_queue
) {
477 DEBUG(2, ("Failed to set up write queue!\n"));
481 /* And now start receiving and processing packets */
482 subreq
= dcerpc_read_ncacn_packet_send(npc
, npc
->ev
, npc
->tstream
);
484 DEBUG(2, ("Failed to start receving packets\n"));
487 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
491 DEBUG(2, ("Fatal error. Terminating client(%s) connection!\n",
493 /* terminate client connection */
498 static void named_pipe_packet_process(struct tevent_req
*subreq
)
500 struct named_pipe_client
*npc
=
501 tevent_req_callback_data(subreq
, struct named_pipe_client
);
502 struct _output_data
*out
= &npc
->p
->out_data
;
503 DATA_BLOB recv_buffer
= data_blob_null
;
504 struct ncacn_packet
*pkt
;
512 status
= dcerpc_read_ncacn_packet_recv(subreq
, npc
, &pkt
, &recv_buffer
);
514 if (!NT_STATUS_IS_OK(status
)) {
518 data_left
= recv_buffer
.length
;
519 data
= (char *)recv_buffer
.data
;
523 data_used
= process_incoming_data(npc
->p
, data
, data_left
);
525 DEBUG(3, ("Failed to process dceprc request!\n"));
526 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
530 data_left
-= data_used
;
534 /* Do not leak this buffer, npc is a long lived context */
535 talloc_free(recv_buffer
.data
);
538 /* this is needed because of the way DCERPC Binds work in
539 * the RPC marshalling code */
540 to_send
= out
->frag
.length
- out
->current_pdu_sent
;
543 DEBUG(10, ("Current_pdu_len = %u, "
544 "current_pdu_sent = %u "
545 "Returning %u bytes\n",
546 (unsigned int)out
->frag
.length
,
547 (unsigned int)out
->current_pdu_sent
,
548 (unsigned int)to_send
));
550 npc
->iov
= talloc_zero(npc
, struct iovec
);
552 status
= NT_STATUS_NO_MEMORY
;
557 npc
->iov
[0].iov_base
= out
->frag
.data
558 + out
->current_pdu_sent
;
559 npc
->iov
[0].iov_len
= to_send
;
561 out
->current_pdu_sent
+= to_send
;
564 /* this condition is false for bind packets, or when we haven't
565 * yet got a full request, and need to wait for more data from
567 while (out
->data_sent_length
< out
->rdata
.length
) {
569 ok
= create_next_pdu(npc
->p
);
571 DEBUG(3, ("Failed to create next PDU!\n"));
572 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
576 npc
->iov
= talloc_realloc(npc
, npc
->iov
,
577 struct iovec
, npc
->count
+ 1);
579 status
= NT_STATUS_NO_MEMORY
;
583 npc
->iov
[npc
->count
].iov_base
= out
->frag
.data
;
584 npc
->iov
[npc
->count
].iov_len
= out
->frag
.length
;
586 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
587 (unsigned int)npc
->count
,
588 (unsigned int)npc
->iov
[npc
->count
].iov_len
));
589 dump_data(11, (const uint8_t *)npc
->iov
[npc
->count
].iov_base
,
590 npc
->iov
[npc
->count
].iov_len
);
594 /* we still don't have a complete request, go back and wait for more
596 if (npc
->count
== 0) {
597 /* Wait for the next packet */
598 subreq
= dcerpc_read_ncacn_packet_send(npc
, npc
->ev
, npc
->tstream
);
600 DEBUG(2, ("Failed to start receving packets\n"));
601 status
= NT_STATUS_NO_MEMORY
;
604 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
608 DEBUG(10, ("Sending a total of %u bytes\n",
609 (unsigned int)npc
->p
->out_data
.data_sent_length
));
611 subreq
= tstream_writev_queue_send(npc
, npc
->ev
,
614 npc
->iov
, npc
->count
);
616 DEBUG(2, ("Failed to send packet\n"));
617 status
= NT_STATUS_NO_MEMORY
;
620 tevent_req_set_callback(subreq
, named_pipe_packet_done
, npc
);
624 DEBUG(2, ("Fatal error(%s). "
625 "Terminating client(%s) connection!\n",
626 nt_errstr(status
), npc
->client_name
));
627 /* terminate client connection */
632 static void named_pipe_packet_done(struct tevent_req
*subreq
)
634 struct named_pipe_client
*npc
=
635 tevent_req_callback_data(subreq
, struct named_pipe_client
);
639 ret
= tstream_writev_queue_recv(subreq
, &sys_errno
);
642 DEBUG(2, ("Writev failed!\n"));
646 /* clear out any data that may have been left around */
648 TALLOC_FREE(npc
->iov
);
649 data_blob_free(&npc
->p
->in_data
.data
);
650 data_blob_free(&npc
->p
->out_data
.frag
);
651 data_blob_free(&npc
->p
->out_data
.rdata
);
653 /* Wait for the next packet */
654 subreq
= dcerpc_read_ncacn_packet_send(npc
, npc
->ev
, npc
->tstream
);
656 DEBUG(2, ("Failed to start receving packets\n"));
660 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
664 DEBUG(2, ("Fatal error(%s). "
665 "Terminating client(%s) connection!\n",
666 strerror(sys_errno
), npc
->client_name
));
667 /* terminate client connection */
672 static void dcerpc_ncacn_accept(struct tevent_context
*ev_ctx
,
673 struct messaging_context
*msg_ctx
,
674 enum dcerpc_transport_t transport
,
677 struct tsocket_address
*cli_addr
,
678 struct tsocket_address
*srv_addr
,
680 dcerpc_ncacn_disconnect_fn fn
);
682 /********************************************************************
683 * Start listening on the tcp/ip socket
684 ********************************************************************/
686 static void dcerpc_ncacn_tcpip_listener(struct tevent_context
*ev
,
687 struct tevent_fd
*fde
,
691 uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context
*ev_ctx
,
692 struct messaging_context
*msg_ctx
,
693 const struct sockaddr_storage
*ifss
,
696 struct dcerpc_ncacn_listen_state
*state
;
697 struct tevent_fd
*fde
;
700 state
= talloc(ev_ctx
, struct dcerpc_ncacn_listen_state
);
702 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Out of memory\n"));
707 state
->ep
.port
= port
;
708 state
->disconnect_fn
= NULL
;
710 if (state
->ep
.port
== 0) {
713 for (i
= SERVER_TCP_LOW_PORT
; i
<= SERVER_TCP_HIGH_PORT
; i
++) {
714 state
->fd
= open_socket_in(SOCK_STREAM
,
725 state
->fd
= open_socket_in(SOCK_STREAM
,
731 if (state
->fd
== -1) {
732 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Failed to create "
733 "socket on port %u!\n", state
->ep
.port
));
737 state
->ev_ctx
= ev_ctx
;
738 state
->msg_ctx
= msg_ctx
;
740 /* ready to listen */
741 set_socket_options(state
->fd
, "SO_KEEPALIVE");
742 set_socket_options(state
->fd
, lp_socket_options());
744 /* Set server socket to non-blocking for the accept. */
745 set_blocking(state
->fd
, false);
747 rc
= listen(state
->fd
, SMBD_LISTEN_BACKLOG
);
749 DEBUG(0,("setup_tcpip_socket: listen - %s\n", strerror(errno
)));
753 DEBUG(10, ("setup_tcpip_socket: openened socket fd %d for port %u\n",
754 state
->fd
, state
->ep
.port
));
756 fde
= tevent_add_fd(state
->ev_ctx
,
760 dcerpc_ncacn_tcpip_listener
,
763 DEBUG(0, ("setup_tcpip_socket: Failed to add event handler!\n"));
767 tevent_fd_set_auto_close(fde
);
769 return state
->ep
.port
;
771 if (state
->fd
!= -1) {
779 static void dcerpc_ncacn_tcpip_listener(struct tevent_context
*ev
,
780 struct tevent_fd
*fde
,
784 struct dcerpc_ncacn_listen_state
*state
=
785 talloc_get_type_abort(private_data
,
786 struct dcerpc_ncacn_listen_state
);
787 struct tsocket_address
*cli_addr
= NULL
;
788 struct tsocket_address
*srv_addr
= NULL
;
789 struct sockaddr_storage addr
;
790 socklen_t in_addrlen
= sizeof(addr
);
794 s
= accept(state
->fd
, (struct sockaddr
*)(void *) &addr
, &in_addrlen
);
796 if (errno
!= EINTR
) {
797 DEBUG(0,("tcpip_listener accept: %s\n",
803 rc
= tsocket_address_bsd_from_sockaddr(state
,
804 (struct sockaddr
*)(void *) &addr
,
812 rc
= getsockname(s
, (struct sockaddr
*)(void *) &addr
, &in_addrlen
);
818 rc
= tsocket_address_bsd_from_sockaddr(state
,
819 (struct sockaddr
*)(void *) &addr
,
827 DEBUG(6, ("tcpip_listener: Accepted socket %d\n", s
));
829 dcerpc_ncacn_accept(state
->ev_ctx
,
840 /********************************************************************
841 * Start listening on the ncalrpc socket
842 ********************************************************************/
844 static void dcerpc_ncalrpc_listener(struct tevent_context
*ev
,
845 struct tevent_fd
*fde
,
849 bool setup_dcerpc_ncalrpc_socket(struct tevent_context
*ev_ctx
,
850 struct messaging_context
*msg_ctx
,
852 dcerpc_ncacn_disconnect_fn fn
)
854 struct dcerpc_ncacn_listen_state
*state
;
855 struct tevent_fd
*fde
;
857 state
= talloc(ev_ctx
, struct dcerpc_ncacn_listen_state
);
859 DEBUG(0, ("Out of memory\n"));
864 state
->disconnect_fn
= fn
;
869 state
->ep
.name
= talloc_strdup(state
, name
);
871 if (state
->ep
.name
== NULL
) {
872 DEBUG(0, ("Out of memory\n"));
877 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) {
878 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
879 lp_ncalrpc_dir(), strerror(errno
)));
883 state
->fd
= create_pipe_sock(lp_ncalrpc_dir(), name
, 0755);
884 if (state
->fd
== -1) {
885 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
886 lp_ncalrpc_dir(), name
));
890 DEBUG(10, ("Openened pipe socket fd %d for %s\n", state
->fd
, name
));
892 state
->ev_ctx
= ev_ctx
;
893 state
->msg_ctx
= msg_ctx
;
895 /* Set server socket to non-blocking for the accept. */
896 set_blocking(state
->fd
, false);
898 fde
= tevent_add_fd(state
->ev_ctx
,
902 dcerpc_ncalrpc_listener
,
905 DEBUG(0, ("Failed to add event handler for ncalrpc!\n"));
909 tevent_fd_set_auto_close(fde
);
913 if (state
->fd
!= -1) {
921 static void dcerpc_ncalrpc_listener(struct tevent_context
*ev
,
922 struct tevent_fd
*fde
,
926 struct dcerpc_ncacn_listen_state
*state
=
927 talloc_get_type_abort(private_data
,
928 struct dcerpc_ncacn_listen_state
);
929 struct tsocket_address
*cli_addr
= NULL
;
930 struct sockaddr_un sunaddr
;
931 struct sockaddr
*addr
= (struct sockaddr
*)(void *)&sunaddr
;
932 socklen_t len
= sizeof(sunaddr
);
936 ZERO_STRUCT(sunaddr
);
938 sd
= accept(state
->fd
, addr
, &len
);
940 if (errno
!= EINTR
) {
941 DEBUG(0, ("ncalrpc accept() failed: %s\n", strerror(errno
)));
946 rc
= tsocket_address_bsd_from_sockaddr(state
,
954 DEBUG(10, ("Accepted ncalrpc socket %d\n", sd
));
956 dcerpc_ncacn_accept(state
->ev_ctx
,
961 state
->disconnect_fn
);
964 struct dcerpc_ncacn_conn
{
965 enum dcerpc_transport_t transport
;
974 struct pipes_struct
*p
;
975 dcerpc_ncacn_disconnect_fn disconnect_fn
;
977 struct tevent_context
*ev_ctx
;
978 struct messaging_context
*msg_ctx
;
980 struct tstream_context
*tstream
;
981 struct tevent_queue
*send_queue
;
983 struct tsocket_address
*client
;
985 struct tsocket_address
*server
;
987 struct auth_session_info
*session_info
;
993 static void dcerpc_ncacn_packet_process(struct tevent_req
*subreq
);
994 static void dcerpc_ncacn_packet_done(struct tevent_req
*subreq
);
996 static void dcerpc_ncacn_accept(struct tevent_context
*ev_ctx
,
997 struct messaging_context
*msg_ctx
,
998 enum dcerpc_transport_t transport
,
1001 struct tsocket_address
*cli_addr
,
1002 struct tsocket_address
*srv_addr
,
1004 dcerpc_ncacn_disconnect_fn fn
) {
1005 struct dcerpc_ncacn_conn
*ncacn_conn
;
1006 struct tevent_req
*subreq
;
1007 const char *cli_str
;
1008 const char *srv_str
= NULL
;
1009 bool system_user
= false;
1016 DEBUG(10, ("dcerpc_ncacn_accept\n"));
1018 ncacn_conn
= talloc_zero(ev_ctx
, struct dcerpc_ncacn_conn
);
1019 if (ncacn_conn
== NULL
) {
1020 DEBUG(0, ("Out of memory!\n"));
1025 ncacn_conn
->transport
= transport
;
1026 ncacn_conn
->ev_ctx
= ev_ctx
;
1027 ncacn_conn
->msg_ctx
= msg_ctx
;
1028 ncacn_conn
->sock
= s
;
1029 ncacn_conn
->disconnect_fn
= fn
;
1031 ncacn_conn
->client
= talloc_move(ncacn_conn
, &cli_addr
);
1032 if (tsocket_address_is_inet(ncacn_conn
->client
, "ip")) {
1033 ncacn_conn
->client_name
=
1034 tsocket_address_inet_addr_string(ncacn_conn
->client
,
1037 ncacn_conn
->client_name
=
1038 tsocket_address_unix_path(ncacn_conn
->client
,
1041 if (ncacn_conn
->client_name
== NULL
) {
1042 DEBUG(0, ("Out of memory!\n"));
1043 talloc_free(ncacn_conn
);
1048 if (srv_addr
!= NULL
) {
1049 ncacn_conn
->server
= talloc_move(ncacn_conn
, &srv_addr
);
1051 ncacn_conn
->server_name
=
1052 tsocket_address_inet_addr_string(ncacn_conn
->server
,
1054 if (ncacn_conn
->server_name
== NULL
) {
1055 DEBUG(0, ("Out of memory!\n"));
1056 talloc_free(ncacn_conn
);
1062 switch (transport
) {
1064 ncacn_conn
->ep
.port
= port
;
1066 pipe_name
= tsocket_address_string(ncacn_conn
->client
,
1068 if (pipe_name
== NULL
) {
1070 talloc_free(ncacn_conn
);
1076 rc
= sys_getpeereid(s
, &uid
);
1078 DEBUG(2, ("Failed to get ncalrpc connecting uid!"));
1080 if (uid
== sec_initial_uid()) {
1085 ncacn_conn
->ep
.name
= talloc_strdup(ncacn_conn
, name
);
1086 if (ncacn_conn
->ep
.name
== NULL
) {
1088 talloc_free(ncacn_conn
);
1092 pipe_name
= talloc_strdup(ncacn_conn
,
1094 if (pipe_name
== NULL
) {
1096 talloc_free(ncacn_conn
);
1101 DEBUG(0, ("unknown dcerpc transport: %u!\n",
1103 talloc_free(ncacn_conn
);
1108 rc
= set_blocking(s
, false);
1110 DEBUG(2, ("Failed to set dcerpc socket to non-blocking\n"));
1111 talloc_free(ncacn_conn
);
1117 * As soon as we have tstream_bsd_existing_socket set up it will
1118 * take care of closing the socket.
1120 rc
= tstream_bsd_existing_socket(ncacn_conn
, s
, &ncacn_conn
->tstream
);
1122 DEBUG(2, ("Failed to create tstream socket for dcerpc\n"));
1123 talloc_free(ncacn_conn
);
1128 if (tsocket_address_is_inet(ncacn_conn
->client
, "ip")) {
1129 cli_str
= ncacn_conn
->client_name
;
1134 if (ncacn_conn
->server
!= NULL
) {
1135 if (tsocket_address_is_inet(ncacn_conn
->server
, "ip")) {
1136 srv_str
= ncacn_conn
->server_name
;
1142 if (ncacn_conn
->session_info
== NULL
) {
1143 status
= auth_anonymous_session_info(ncacn_conn
,
1144 &ncacn_conn
->session_info
);
1145 if (!NT_STATUS_IS_OK(status
)) {
1146 DEBUG(2, ("Failed to create "
1147 "auth_anonymous_session_info - %s\n",
1148 nt_errstr(status
)));
1149 talloc_free(ncacn_conn
);
1154 rc
= make_server_pipes_struct(ncacn_conn
,
1156 ncacn_conn
->transport
,
1160 ncacn_conn
->session_info
,
1164 DEBUG(2, ("Failed to create pipe struct - %s",
1165 strerror(sys_errno
)));
1166 talloc_free(ncacn_conn
);
1170 ncacn_conn
->send_queue
= tevent_queue_create(ncacn_conn
,
1171 "dcerpc send queue");
1172 if (ncacn_conn
->send_queue
== NULL
) {
1173 DEBUG(0, ("Out of memory!\n"));
1174 talloc_free(ncacn_conn
);
1178 subreq
= dcerpc_read_ncacn_packet_send(ncacn_conn
,
1180 ncacn_conn
->tstream
);
1181 if (subreq
== NULL
) {
1182 DEBUG(2, ("Failed to send ncacn packet\n"));
1183 talloc_free(ncacn_conn
);
1187 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1189 DEBUG(10, ("dcerpc_ncacn_accept done\n"));
1194 static void dcerpc_ncacn_packet_process(struct tevent_req
*subreq
)
1196 struct dcerpc_ncacn_conn
*ncacn_conn
=
1197 tevent_req_callback_data(subreq
, struct dcerpc_ncacn_conn
);
1199 struct _output_data
*out
= &ncacn_conn
->p
->out_data
;
1200 DATA_BLOB recv_buffer
= data_blob_null
;
1201 struct ncacn_packet
*pkt
;
1209 status
= dcerpc_read_ncacn_packet_recv(subreq
, ncacn_conn
, &pkt
, &recv_buffer
);
1210 TALLOC_FREE(subreq
);
1211 if (!NT_STATUS_IS_OK(status
)) {
1212 if (ncacn_conn
->disconnect_fn
!= NULL
) {
1213 ok
= ncacn_conn
->disconnect_fn(ncacn_conn
->p
);
1215 DEBUG(3, ("Failed to call disconnect function\n"));
1221 data_left
= recv_buffer
.length
;
1222 data
= (char *) recv_buffer
.data
;
1225 data_used
= process_incoming_data(ncacn_conn
->p
, data
, data_left
);
1226 if (data_used
< 0) {
1227 DEBUG(3, ("Failed to process dcerpc request!\n"));
1228 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
1232 data_left
-= data_used
;
1236 /* Do not leak this buffer */
1237 talloc_free(recv_buffer
.data
);
1241 * This is needed because of the way DCERPC binds work in the RPC
1244 to_send
= out
->frag
.length
- out
->current_pdu_sent
;
1247 DEBUG(10, ("Current_pdu_len = %u, "
1248 "current_pdu_sent = %u "
1249 "Returning %u bytes\n",
1250 (unsigned int)out
->frag
.length
,
1251 (unsigned int)out
->current_pdu_sent
,
1252 (unsigned int)to_send
));
1254 ncacn_conn
->iov
= talloc_zero(ncacn_conn
, struct iovec
);
1255 if (ncacn_conn
->iov
== NULL
) {
1256 status
= NT_STATUS_NO_MEMORY
;
1257 DEBUG(3, ("Out of memory!\n"));
1260 ncacn_conn
->count
= 1;
1262 ncacn_conn
->iov
[0].iov_base
= out
->frag
.data
1263 + out
->current_pdu_sent
;
1264 ncacn_conn
->iov
[0].iov_len
= to_send
;
1266 out
->current_pdu_sent
+= to_send
;
1270 * This condition is false for bind packets, or when we haven't yet got
1271 * a full request, and need to wait for more data from the client
1273 while (out
->data_sent_length
< out
->rdata
.length
) {
1274 ok
= create_next_pdu(ncacn_conn
->p
);
1276 DEBUG(3, ("Failed to create next PDU!\n"));
1277 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
1281 ncacn_conn
->iov
= talloc_realloc(ncacn_conn
,
1284 ncacn_conn
->count
+ 1);
1285 if (ncacn_conn
->iov
== NULL
) {
1286 DEBUG(3, ("Out of memory!\n"));
1287 status
= NT_STATUS_NO_MEMORY
;
1291 ncacn_conn
->iov
[ncacn_conn
->count
].iov_base
= out
->frag
.data
;
1292 ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
= out
->frag
.length
;
1294 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
1295 (unsigned int) ncacn_conn
->count
,
1296 (unsigned int) ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
));
1297 dump_data(11, (const uint8_t *) ncacn_conn
->iov
[ncacn_conn
->count
].iov_base
,
1298 ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
);
1299 ncacn_conn
->count
++;
1303 * We still don't have a complete request, go back and wait for more
1306 if (ncacn_conn
->count
== 0) {
1307 /* Wait for the next packet */
1308 subreq
= dcerpc_read_ncacn_packet_send(ncacn_conn
,
1310 ncacn_conn
->tstream
);
1311 if (subreq
== NULL
) {
1312 DEBUG(2, ("Failed to start receving packets\n"));
1313 status
= NT_STATUS_NO_MEMORY
;
1316 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1320 DEBUG(10, ("Sending a total of %u bytes\n",
1321 (unsigned int)ncacn_conn
->p
->out_data
.data_sent_length
));
1323 subreq
= tstream_writev_queue_send(ncacn_conn
,
1325 ncacn_conn
->tstream
,
1326 ncacn_conn
->send_queue
,
1329 if (subreq
== NULL
) {
1330 DEBUG(2, ("Failed to send packet\n"));
1331 status
= NT_STATUS_NO_MEMORY
;
1335 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_done
, ncacn_conn
);
1339 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1340 ncacn_conn
->client_name
, nt_errstr(status
)));
1342 /* Terminate client connection */
1343 talloc_free(ncacn_conn
);
1347 static void dcerpc_ncacn_packet_done(struct tevent_req
*subreq
)
1349 struct dcerpc_ncacn_conn
*ncacn_conn
=
1350 tevent_req_callback_data(subreq
, struct dcerpc_ncacn_conn
);
1351 NTSTATUS status
= NT_STATUS_OK
;
1355 rc
= tstream_writev_queue_recv(subreq
, &sys_errno
);
1356 TALLOC_FREE(subreq
);
1358 DEBUG(2, ("Writev failed!\n"));
1359 status
= map_nt_error_from_unix(sys_errno
);
1363 /* clear out any data that may have been left around */
1364 ncacn_conn
->count
= 0;
1365 TALLOC_FREE(ncacn_conn
->iov
);
1366 data_blob_free(&ncacn_conn
->p
->in_data
.data
);
1367 data_blob_free(&ncacn_conn
->p
->out_data
.frag
);
1368 data_blob_free(&ncacn_conn
->p
->out_data
.rdata
);
1370 /* Wait for the next packet */
1371 subreq
= dcerpc_read_ncacn_packet_send(ncacn_conn
,
1373 ncacn_conn
->tstream
);
1374 if (subreq
== NULL
) {
1375 DEBUG(2, ("Failed to start receving packets\n"));
1376 status
= NT_STATUS_NO_MEMORY
;
1380 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1384 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1385 ncacn_conn
->client_name
, nt_errstr(status
)));
1387 /* Terminate client connection */
1388 talloc_free(ncacn_conn
);
1392 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */