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/>.
21 #include "rpc_server/rpc_server.h"
23 #include "librpc/gen_ndr/netlogon.h"
24 #include "librpc/gen_ndr/auth.h"
25 #include "registry/reg_parse_prs.h"
26 #include "lib/tsocket/tsocket.h"
27 #include "libcli/named_pipe_auth/npa_tstream.h"
28 #include "../auth/auth_sam_reply.h"
30 #define SERVER_TCP_LOW_PORT 1024
31 #define SERVER_TCP_HIGH_PORT 1300
33 static NTSTATUS
auth_anonymous_session_info(TALLOC_CTX
*mem_ctx
,
34 struct auth_session_info_transport
**session_info
)
36 struct auth_session_info_transport
*i
;
37 struct auth_serversupplied_info
*s
;
38 struct auth_user_info_dc
*u
;
39 union netr_Validation val
;
42 i
= talloc_zero(mem_ctx
, struct auth_session_info_transport
);
44 return NT_STATUS_NO_MEMORY
;
47 status
= make_server_info_guest(i
, &s
);
48 if (!NT_STATUS_IS_OK(status
)) {
52 i
->security_token
= s
->security_token
;
53 i
->session_key
= s
->user_session_key
;
57 status
= make_user_info_dc_netlogon_validation(mem_ctx
,
62 if (!NT_STATUS_IS_OK(status
)) {
63 DEBUG(0, ("conversion of info3 into user_info_dc failed!\n"));
66 i
->info
= talloc_move(i
, &u
->info
);
74 /* Creates a pipes_struct and initializes it with the information
75 * sent from the client */
76 static int make_server_pipes_struct(TALLOC_CTX
*mem_ctx
,
77 const char *pipe_name
,
78 const struct ndr_syntax_id id
,
79 enum dcerpc_transport_t transport
,
81 const char *client_address
,
82 const char *server_address
,
83 struct auth_session_info_transport
*session_info
,
84 struct pipes_struct
**_p
,
87 struct netr_SamInfo3
*info3
;
88 struct auth_user_info_dc
*auth_user_info_dc
;
89 struct pipes_struct
*p
;
93 p
= talloc_zero(mem_ctx
, struct pipes_struct
);
99 p
->transport
= transport
;
100 p
->system_user
= system_user
;
102 p
->mem_ctx
= talloc_named(p
, 0, "pipe %s %p", pipe_name
, p
);
109 ok
= init_pipe_handles(p
, &id
);
111 DEBUG(1, ("Failed to init handles\n"));
118 data_blob_free(&p
->in_data
.data
);
119 data_blob_free(&p
->in_data
.pdu
);
121 p
->endian
= RPC_LITTLE_ENDIAN
;
123 /* Fake up an auth_user_info_dc for now, to make an info3, to make the session_info structure */
124 auth_user_info_dc
= talloc_zero(p
, struct auth_user_info_dc
);
125 if (!auth_user_info_dc
) {
131 auth_user_info_dc
->num_sids
= session_info
->security_token
->num_sids
;
132 auth_user_info_dc
->sids
= session_info
->security_token
->sids
;
133 auth_user_info_dc
->info
= session_info
->info
;
134 auth_user_info_dc
->user_session_key
= session_info
->session_key
;
136 /* This creates the input structure that make_server_info_info3 is looking for */
137 status
= auth_convert_user_info_dc_saminfo3(p
, auth_user_info_dc
,
140 if (!NT_STATUS_IS_OK(status
)) {
141 DEBUG(1, ("Failed to convert auth_user_info_dc into netr_SamInfo3\n"));
147 status
= make_server_info_info3(p
,
148 info3
->base
.account_name
.string
,
149 info3
->base
.domain
.string
,
150 &p
->session_info
, info3
);
151 if (!NT_STATUS_IS_OK(status
)) {
152 DEBUG(1, ("Failed to init server info\n"));
159 * Some internal functions need a local token to determine access to
162 status
= create_local_token(p
->session_info
);
163 if (!NT_STATUS_IS_OK(status
)) {
164 DEBUG(1, ("Failed to init local auth token\n"));
170 /* Now override the session_info->security_token with the exact
171 * security_token we were given from the other side,
172 * regardless of what we just calculated */
173 p
->session_info
->security_token
= talloc_move(p
->session_info
, &session_info
->security_token
);
175 /* Also set the session key to the correct value */
176 p
->session_info
->user_session_key
= session_info
->session_key
;
177 p
->session_info
->user_session_key
.data
= talloc_move(p
->session_info
, &session_info
->session_key
.data
);
179 p
->client_id
= talloc_zero(p
, struct client_address
);
185 strlcpy(p
->client_id
->addr
,
186 client_address
, sizeof(p
->client_id
->addr
));
187 p
->client_id
->name
= talloc_strdup(p
->client_id
, client_address
);
188 if (p
->client_id
->name
== NULL
) {
194 if (server_address
!= NULL
) {
195 p
->server_id
= talloc_zero(p
, struct client_address
);
196 if (p
->client_id
== NULL
) {
202 strlcpy(p
->server_id
->addr
,
204 sizeof(p
->server_id
->addr
));
206 p
->server_id
->name
= talloc_strdup(p
->server_id
,
208 if (p
->server_id
->name
== NULL
) {
215 talloc_set_destructor(p
, close_internal_rpc_pipe_hnd
);
221 /* Start listening on the appropriate unix socket and setup all is needed to
222 * dispatch requests to the pipes rpc implementation */
224 struct dcerpc_ncacn_listen_state
{
225 struct ndr_syntax_id syntax_id
;
233 struct tevent_context
*ev_ctx
;
234 struct messaging_context
*msg_ctx
;
237 static void named_pipe_listener(struct tevent_context
*ev
,
238 struct tevent_fd
*fde
,
242 bool setup_named_pipe_socket(const char *pipe_name
,
243 struct tevent_context
*ev_ctx
)
245 struct dcerpc_ncacn_listen_state
*state
;
246 struct tevent_fd
*fde
;
249 state
= talloc(ev_ctx
, struct dcerpc_ncacn_listen_state
);
251 DEBUG(0, ("Out of memory\n"));
254 state
->ep
.name
= talloc_strdup(state
, pipe_name
);
255 if (state
->ep
.name
== NULL
) {
256 DEBUG(0, ("Out of memory\n"));
261 np_dir
= talloc_asprintf(state
, "%s/np", lp_ncalrpc_dir());
263 DEBUG(0, ("Out of memory\n"));
267 if (!directory_create_or_exist(np_dir
, geteuid(), 0700)) {
268 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
269 np_dir
, strerror(errno
)));
273 state
->fd
= create_pipe_sock(np_dir
, pipe_name
, 0700);
274 if (state
->fd
== -1) {
275 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
280 DEBUG(10, ("Openened pipe socket fd %d for %s\n",
281 state
->fd
, pipe_name
));
283 fde
= tevent_add_fd(ev_ctx
,
284 state
, state
->fd
, TEVENT_FD_READ
,
285 named_pipe_listener
, state
);
287 DEBUG(0, ("Failed to add event handler!\n"));
291 tevent_fd_set_auto_close(fde
);
295 if (state
->fd
!= -1) {
302 static void named_pipe_accept_function(const char *pipe_name
, int fd
);
304 static void named_pipe_listener(struct tevent_context
*ev
,
305 struct tevent_fd
*fde
,
309 struct dcerpc_ncacn_listen_state
*state
=
310 talloc_get_type_abort(private_data
,
311 struct dcerpc_ncacn_listen_state
);
312 struct sockaddr_un sunaddr
;
316 /* TODO: should we have a limit to the number of clients ? */
318 len
= sizeof(sunaddr
);
321 sd
= accept(state
->fd
,
322 (struct sockaddr
*)(void *)&sunaddr
, &len
);
323 if (errno
!= EINTR
) break;
327 DEBUG(6, ("Failed to get a valid socket [%s]\n",
332 DEBUG(6, ("Accepted socket %d\n", sd
));
334 named_pipe_accept_function(state
->ep
.name
, sd
);
338 /* This is the core of the rpc server.
339 * Accepts connections from clients and process requests using the appropriate
340 * dispatcher table. */
342 struct named_pipe_client
{
343 const char *pipe_name
;
344 struct ndr_syntax_id pipe_id
;
346 struct tevent_context
*ev
;
347 struct messaging_context
*msg_ctx
;
350 uint16_t device_state
;
351 uint64_t allocation_size
;
353 struct tstream_context
*tstream
;
355 struct tsocket_address
*client
;
357 struct tsocket_address
*server
;
359 struct auth_session_info_transport
*session_info
;
361 struct pipes_struct
*p
;
363 struct tevent_queue
*write_queue
;
369 static void named_pipe_accept_done(struct tevent_req
*subreq
);
371 static void named_pipe_accept_function(const char *pipe_name
, int fd
)
373 struct ndr_syntax_id syntax
;
374 struct named_pipe_client
*npc
;
375 struct tstream_context
*plain
;
376 struct tevent_req
*subreq
;
380 ok
= is_known_pipename(pipe_name
, &syntax
);
382 DEBUG(1, ("Unknown pipe [%s]\n", pipe_name
));
387 npc
= talloc_zero(NULL
, struct named_pipe_client
);
389 DEBUG(0, ("Out of memory!\n"));
393 npc
->pipe_name
= pipe_name
;
394 npc
->pipe_id
= syntax
;
395 npc
->ev
= server_event_context();
396 npc
->msg_ctx
= server_messaging_context();
398 /* make sure socket is in NON blocking state */
399 ret
= set_blocking(fd
, false);
401 DEBUG(2, ("Failed to make socket non-blocking\n"));
407 ret
= tstream_bsd_existing_socket(npc
, fd
, &plain
);
409 DEBUG(2, ("Failed to create tstream socket\n"));
415 npc
->file_type
= FILE_TYPE_MESSAGE_MODE_PIPE
;
416 npc
->device_state
= 0xff | 0x0400 | 0x0100;
417 npc
->allocation_size
= 4096;
419 subreq
= tstream_npa_accept_existing_send(npc
, npc
->ev
, plain
,
422 npc
->allocation_size
);
424 DEBUG(2, ("Failed to start async accept procedure\n"));
429 tevent_req_set_callback(subreq
, named_pipe_accept_done
, npc
);
432 static void named_pipe_packet_process(struct tevent_req
*subreq
);
433 static void named_pipe_packet_done(struct tevent_req
*subreq
);
435 static void named_pipe_accept_done(struct tevent_req
*subreq
)
437 struct named_pipe_client
*npc
=
438 tevent_req_callback_data(subreq
, struct named_pipe_client
);
439 const char *cli_addr
;
443 ret
= tstream_npa_accept_existing_recv(subreq
, &error
, npc
,
452 DEBUG(2, ("Failed to accept named pipe connection! (%s)\n",
458 if (tsocket_address_is_inet(npc
->client
, "ip")) {
459 cli_addr
= tsocket_address_inet_addr_string(npc
->client
,
461 if (cli_addr
== NULL
) {
469 ret
= make_server_pipes_struct(npc
,
470 npc
->pipe_name
, npc
->pipe_id
, NCACN_NP
,
471 false, cli_addr
, NULL
, npc
->session_info
,
474 DEBUG(2, ("Failed to create pipes_struct! (%s)\n",
478 npc
->p
->msg_ctx
= npc
->msg_ctx
;
480 npc
->write_queue
= tevent_queue_create(npc
, "np_server_write_queue");
481 if (!npc
->write_queue
) {
482 DEBUG(2, ("Failed to set up write queue!\n"));
486 /* And now start receaving and processing packets */
487 subreq
= dcerpc_read_ncacn_packet_send(npc
, npc
->ev
, npc
->tstream
);
489 DEBUG(2, ("Failed to start receving packets\n"));
492 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
496 DEBUG(2, ("Fatal error. Terminating client(%s) connection!\n",
498 /* terminate client connection */
503 static void named_pipe_packet_process(struct tevent_req
*subreq
)
505 struct named_pipe_client
*npc
=
506 tevent_req_callback_data(subreq
, struct named_pipe_client
);
507 struct _output_data
*out
= &npc
->p
->out_data
;
508 DATA_BLOB recv_buffer
= data_blob_null
;
509 struct ncacn_packet
*pkt
;
517 status
= dcerpc_read_ncacn_packet_recv(subreq
, npc
, &pkt
, &recv_buffer
);
519 if (!NT_STATUS_IS_OK(status
)) {
523 data_left
= recv_buffer
.length
;
524 data
= (char *)recv_buffer
.data
;
528 data_used
= process_incoming_data(npc
->p
, data
, data_left
);
530 DEBUG(3, ("Failed to process dceprc request!\n"));
531 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
535 data_left
-= data_used
;
539 /* Do not leak this buffer, npc is a long lived context */
540 talloc_free(recv_buffer
.data
);
543 /* this is needed because of the way DCERPC Binds work in
544 * the RPC marshalling code */
545 to_send
= out
->frag
.length
- out
->current_pdu_sent
;
548 DEBUG(10, ("Current_pdu_len = %u, "
549 "current_pdu_sent = %u "
550 "Returning %u bytes\n",
551 (unsigned int)out
->frag
.length
,
552 (unsigned int)out
->current_pdu_sent
,
553 (unsigned int)to_send
));
555 npc
->iov
= talloc_zero(npc
, struct iovec
);
557 status
= NT_STATUS_NO_MEMORY
;
562 npc
->iov
[0].iov_base
= out
->frag
.data
563 + out
->current_pdu_sent
;
564 npc
->iov
[0].iov_len
= to_send
;
566 out
->current_pdu_sent
+= to_send
;
569 /* this condition is false for bind packets, or when we haven't
570 * yet got a full request, and need to wait for more data from
572 while (out
->data_sent_length
< out
->rdata
.length
) {
574 ok
= create_next_pdu(npc
->p
);
576 DEBUG(3, ("Failed to create next PDU!\n"));
577 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
581 npc
->iov
= talloc_realloc(npc
, npc
->iov
,
582 struct iovec
, npc
->count
+ 1);
584 status
= NT_STATUS_NO_MEMORY
;
588 npc
->iov
[npc
->count
].iov_base
= out
->frag
.data
;
589 npc
->iov
[npc
->count
].iov_len
= out
->frag
.length
;
591 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
592 (unsigned int)npc
->count
,
593 (unsigned int)npc
->iov
[npc
->count
].iov_len
));
594 dump_data(11, (const uint8_t *)npc
->iov
[npc
->count
].iov_base
,
595 npc
->iov
[npc
->count
].iov_len
);
599 /* we still don't have a complete request, go back and wait for more
601 if (npc
->count
== 0) {
602 /* Wait for the next packet */
603 subreq
= dcerpc_read_ncacn_packet_send(npc
, npc
->ev
, npc
->tstream
);
605 DEBUG(2, ("Failed to start receving packets\n"));
606 status
= NT_STATUS_NO_MEMORY
;
609 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
613 DEBUG(10, ("Sending a total of %u bytes\n",
614 (unsigned int)npc
->p
->out_data
.data_sent_length
));
616 subreq
= tstream_writev_queue_send(npc
, npc
->ev
,
619 npc
->iov
, npc
->count
);
621 DEBUG(2, ("Failed to send packet\n"));
622 status
= NT_STATUS_NO_MEMORY
;
625 tevent_req_set_callback(subreq
, named_pipe_packet_done
, npc
);
629 DEBUG(2, ("Fatal error(%s). "
630 "Terminating client(%s) connection!\n",
631 nt_errstr(status
), npc
->client_name
));
632 /* terminate client connection */
637 static void named_pipe_packet_done(struct tevent_req
*subreq
)
639 struct named_pipe_client
*npc
=
640 tevent_req_callback_data(subreq
, struct named_pipe_client
);
644 ret
= tstream_writev_queue_recv(subreq
, &sys_errno
);
647 DEBUG(2, ("Writev failed!\n"));
651 /* clear out any data that may have been left around */
653 TALLOC_FREE(npc
->iov
);
654 data_blob_free(&npc
->p
->in_data
.data
);
655 data_blob_free(&npc
->p
->out_data
.frag
);
656 data_blob_free(&npc
->p
->out_data
.rdata
);
658 /* Wait for the next packet */
659 subreq
= dcerpc_read_ncacn_packet_send(npc
, npc
->ev
, npc
->tstream
);
661 DEBUG(2, ("Failed to start receving packets\n"));
665 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
669 DEBUG(2, ("Fatal error(%s). "
670 "Terminating client(%s) connection!\n",
671 strerror(sys_errno
), npc
->client_name
));
672 /* terminate client connection */
677 static void dcerpc_ncacn_accept(struct tevent_context
*ev_ctx
,
678 struct messaging_context
*msg_ctx
,
679 struct ndr_syntax_id syntax_id
,
680 enum dcerpc_transport_t transport
,
683 struct tsocket_address
*cli_addr
,
684 struct tsocket_address
*srv_addr
,
687 /********************************************************************
688 * Start listening on the tcp/ip socket
689 ********************************************************************/
691 static void dcerpc_ncacn_tcpip_listener(struct tevent_context
*ev
,
692 struct tevent_fd
*fde
,
696 uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context
*ev_ctx
,
697 struct messaging_context
*msg_ctx
,
698 struct ndr_syntax_id syntax_id
,
699 const struct sockaddr_storage
*ifss
,
702 struct dcerpc_ncacn_listen_state
*state
;
703 struct tevent_fd
*fde
;
706 state
= talloc(ev_ctx
, struct dcerpc_ncacn_listen_state
);
708 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Out of memory\n"));
712 state
->syntax_id
= syntax_id
;
714 state
->ep
.port
= port
;
716 if (state
->ep
.port
== 0) {
719 for (i
= SERVER_TCP_LOW_PORT
; i
<= SERVER_TCP_HIGH_PORT
; i
++) {
720 state
->fd
= open_socket_in(SOCK_STREAM
,
731 state
->fd
= open_socket_in(SOCK_STREAM
,
737 if (state
->fd
== -1) {
738 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Failed to create "
739 "socket on port %u!\n", state
->ep
.port
));
743 state
->ev_ctx
= ev_ctx
;
744 state
->msg_ctx
= msg_ctx
;
746 /* ready to listen */
747 set_socket_options(state
->fd
, "SO_KEEPALIVE");
748 set_socket_options(state
->fd
, lp_socket_options());
750 /* Set server socket to non-blocking for the accept. */
751 set_blocking(state
->fd
, false);
753 rc
= listen(state
->fd
, SMBD_LISTEN_BACKLOG
);
755 DEBUG(0,("setup_tcpip_socket: listen - %s\n", strerror(errno
)));
759 DEBUG(10, ("setup_tcpip_socket: openened socket fd %d for port %u\n",
760 state
->fd
, state
->ep
.port
));
762 fde
= tevent_add_fd(state
->ev_ctx
,
766 dcerpc_ncacn_tcpip_listener
,
769 DEBUG(0, ("setup_tcpip_socket: Failed to add event handler!\n"));
773 tevent_fd_set_auto_close(fde
);
775 return state
->ep
.port
;
777 if (state
->fd
!= -1) {
785 static void dcerpc_ncacn_tcpip_listener(struct tevent_context
*ev
,
786 struct tevent_fd
*fde
,
790 struct dcerpc_ncacn_listen_state
*state
=
791 talloc_get_type_abort(private_data
,
792 struct dcerpc_ncacn_listen_state
);
793 struct tsocket_address
*cli_addr
= NULL
;
794 struct tsocket_address
*srv_addr
= NULL
;
795 struct sockaddr_storage addr
;
796 socklen_t in_addrlen
= sizeof(addr
);
801 s
= accept(state
->fd
, (struct sockaddr
*)(void *) &addr
, &in_addrlen
);
802 if (s
== -1 && errno
!= EINTR
) {
808 DEBUG(0,("tcpip_listener accept: %s\n",
813 rc
= tsocket_address_bsd_from_sockaddr(state
,
814 (struct sockaddr
*)(void *) &addr
,
822 rc
= getsockname(s
, (struct sockaddr
*)(void *) &addr
, &in_addrlen
);
828 rc
= tsocket_address_bsd_from_sockaddr(state
,
829 (struct sockaddr
*)(void *) &addr
,
837 DEBUG(6, ("tcpip_listener: Accepted socket %d\n", s
));
839 dcerpc_ncacn_accept(state
->ev_ctx
,
850 /********************************************************************
851 * Start listening on the ncalrpc socket
852 ********************************************************************/
854 static void dcerpc_ncalrpc_listener(struct tevent_context
*ev
,
855 struct tevent_fd
*fde
,
859 bool setup_dcerpc_ncalrpc_socket(struct tevent_context
*ev_ctx
,
860 struct messaging_context
*msg_ctx
,
861 struct ndr_syntax_id syntax_id
,
864 struct dcerpc_ncacn_listen_state
*state
;
865 struct tevent_fd
*fde
;
867 state
= talloc(ev_ctx
, struct dcerpc_ncacn_listen_state
);
869 DEBUG(0, ("Out of memory\n"));
873 state
->syntax_id
= syntax_id
;
879 state
->ep
.name
= talloc_strdup(state
, name
);
881 if (state
->ep
.name
== NULL
) {
882 DEBUG(0, ("Out of memory\n"));
887 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0700)) {
888 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
889 lp_ncalrpc_dir(), strerror(errno
)));
893 state
->fd
= create_pipe_sock(lp_ncalrpc_dir(), name
, 0700);
894 if (state
->fd
== -1) {
895 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
896 lp_ncalrpc_dir(), name
));
900 DEBUG(10, ("Openened pipe socket fd %d for %s\n", state
->fd
, name
));
902 state
->ev_ctx
= ev_ctx
;
903 state
->msg_ctx
= msg_ctx
;
905 /* Set server socket to non-blocking for the accept. */
906 set_blocking(state
->fd
, false);
908 fde
= tevent_add_fd(state
->ev_ctx
,
912 dcerpc_ncalrpc_listener
,
915 DEBUG(0, ("Failed to add event handler for ncalrpc!\n"));
919 tevent_fd_set_auto_close(fde
);
923 if (state
->fd
!= -1) {
931 static void dcerpc_ncalrpc_listener(struct tevent_context
*ev
,
932 struct tevent_fd
*fde
,
936 struct dcerpc_ncacn_listen_state
*state
=
937 talloc_get_type_abort(private_data
,
938 struct dcerpc_ncacn_listen_state
);
939 struct tsocket_address
*cli_addr
= NULL
;
940 struct sockaddr_un sunaddr
;
941 struct sockaddr
*addr
= (struct sockaddr
*)(void *)&sunaddr
;
942 socklen_t len
= sizeof(sunaddr
);
946 ZERO_STRUCT(sunaddr
);
949 sd
= accept(state
->fd
, addr
, &len
);
950 if (sd
== -1 && errno
!= EINTR
) {
956 DEBUG(0, ("ncalrpc accept() failed: %s\n", strerror(errno
)));
960 rc
= tsocket_address_bsd_from_sockaddr(state
,
968 DEBUG(10, ("Accepted ncalrpc socket %d\n", sd
));
970 dcerpc_ncacn_accept(state
->ev_ctx
,
972 state
->syntax_id
, NCALRPC
,
977 struct dcerpc_ncacn_conn
{
978 struct ndr_syntax_id syntax_id
;
980 enum dcerpc_transport_t transport
;
989 struct pipes_struct
*p
;
991 struct tevent_context
*ev_ctx
;
992 struct messaging_context
*msg_ctx
;
994 struct tstream_context
*tstream
;
995 struct tevent_queue
*send_queue
;
997 struct tsocket_address
*client
;
999 struct tsocket_address
*server
;
1001 struct auth_session_info_transport
*session_info
;
1007 static void dcerpc_ncacn_packet_process(struct tevent_req
*subreq
);
1008 static void dcerpc_ncacn_packet_done(struct tevent_req
*subreq
);
1010 static void dcerpc_ncacn_accept(struct tevent_context
*ev_ctx
,
1011 struct messaging_context
*msg_ctx
,
1012 struct ndr_syntax_id syntax_id
,
1013 enum dcerpc_transport_t transport
,
1016 struct tsocket_address
*cli_addr
,
1017 struct tsocket_address
*srv_addr
,
1019 struct dcerpc_ncacn_conn
*ncacn_conn
;
1020 struct tevent_req
*subreq
;
1021 const char *cli_str
;
1022 const char *srv_str
= NULL
;
1023 bool system_user
= false;
1030 DEBUG(10, ("dcerpc_ncacn_accept\n"));
1032 ncacn_conn
= talloc_zero(ev_ctx
, struct dcerpc_ncacn_conn
);
1033 if (ncacn_conn
== NULL
) {
1034 DEBUG(0, ("Out of memory!\n"));
1039 ncacn_conn
->transport
= transport
;
1040 ncacn_conn
->syntax_id
= syntax_id
;
1041 ncacn_conn
->ev_ctx
= ev_ctx
;
1042 ncacn_conn
->msg_ctx
= msg_ctx
;
1043 ncacn_conn
->sock
= s
;
1045 ncacn_conn
->client
= talloc_move(ncacn_conn
, &cli_addr
);
1046 if (tsocket_address_is_inet(ncacn_conn
->client
, "ip")) {
1047 ncacn_conn
->client_name
=
1048 tsocket_address_inet_addr_string(ncacn_conn
->client
,
1051 ncacn_conn
->client_name
=
1052 tsocket_address_unix_path(ncacn_conn
->client
,
1055 if (ncacn_conn
->client_name
== NULL
) {
1056 DEBUG(0, ("Out of memory!\n"));
1057 talloc_free(ncacn_conn
);
1062 if (srv_addr
!= NULL
) {
1063 ncacn_conn
->server
= talloc_move(ncacn_conn
, &srv_addr
);
1065 ncacn_conn
->server_name
=
1066 tsocket_address_inet_addr_string(ncacn_conn
->server
,
1068 if (ncacn_conn
->server_name
== NULL
) {
1069 DEBUG(0, ("Out of memory!\n"));
1070 talloc_free(ncacn_conn
);
1076 switch (transport
) {
1078 ncacn_conn
->ep
.port
= port
;
1080 pipe_name
= tsocket_address_string(ncacn_conn
->client
,
1082 if (pipe_name
== NULL
) {
1084 talloc_free(ncacn_conn
);
1090 rc
= sys_getpeereid(s
, &uid
);
1092 DEBUG(2, ("Failed to get ncalrpc connecting uid!"));
1094 if (uid
== sec_initial_uid()) {
1099 ncacn_conn
->ep
.name
= talloc_strdup(ncacn_conn
, name
);
1100 if (ncacn_conn
->ep
.name
== NULL
) {
1102 talloc_free(ncacn_conn
);
1106 pipe_name
= talloc_strdup(ncacn_conn
,
1108 if (pipe_name
== NULL
) {
1110 talloc_free(ncacn_conn
);
1115 DEBUG(0, ("unknown dcerpc transport: %u!\n",
1117 talloc_free(ncacn_conn
);
1122 rc
= set_blocking(s
, false);
1124 DEBUG(2, ("Failed to set dcerpc socket to non-blocking\n"));
1125 talloc_free(ncacn_conn
);
1131 * As soon as we have tstream_bsd_existing_socket set up it will
1132 * take care of closing the socket.
1134 rc
= tstream_bsd_existing_socket(ncacn_conn
, s
, &ncacn_conn
->tstream
);
1136 DEBUG(2, ("Failed to create tstream socket for dcerpc\n"));
1137 talloc_free(ncacn_conn
);
1142 if (tsocket_address_is_inet(ncacn_conn
->client
, "ip")) {
1143 cli_str
= ncacn_conn
->client_name
;
1148 if (ncacn_conn
->server
!= NULL
) {
1149 if (tsocket_address_is_inet(ncacn_conn
->server
, "ip")) {
1150 srv_str
= ncacn_conn
->server_name
;
1156 if (ncacn_conn
->session_info
== NULL
) {
1157 status
= auth_anonymous_session_info(ncacn_conn
,
1158 &ncacn_conn
->session_info
);
1159 if (!NT_STATUS_IS_OK(status
)) {
1160 DEBUG(2, ("Failed to create "
1161 "auth_anonymous_session_info - %s\n",
1162 nt_errstr(status
)));
1163 talloc_free(ncacn_conn
);
1168 rc
= make_server_pipes_struct(ncacn_conn
,
1170 ncacn_conn
->syntax_id
,
1171 ncacn_conn
->transport
,
1175 ncacn_conn
->session_info
,
1179 DEBUG(2, ("Failed to create pipe struct - %s",
1180 strerror(sys_errno
)));
1181 talloc_free(ncacn_conn
);
1185 ncacn_conn
->send_queue
= tevent_queue_create(ncacn_conn
,
1186 "dcerpc send queue");
1187 if (ncacn_conn
->send_queue
== NULL
) {
1188 DEBUG(0, ("Out of memory!\n"));
1189 talloc_free(ncacn_conn
);
1193 subreq
= dcerpc_read_ncacn_packet_send(ncacn_conn
,
1195 ncacn_conn
->tstream
);
1196 if (subreq
== NULL
) {
1197 DEBUG(2, ("Failed to send ncacn packet\n"));
1198 talloc_free(ncacn_conn
);
1202 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1204 DEBUG(10, ("dcerpc_ncacn_accept done\n"));
1209 static void dcerpc_ncacn_packet_process(struct tevent_req
*subreq
)
1211 struct dcerpc_ncacn_conn
*ncacn_conn
=
1212 tevent_req_callback_data(subreq
, struct dcerpc_ncacn_conn
);
1214 struct _output_data
*out
= &ncacn_conn
->p
->out_data
;
1215 DATA_BLOB recv_buffer
= data_blob_null
;
1216 struct ncacn_packet
*pkt
;
1224 status
= dcerpc_read_ncacn_packet_recv(subreq
, ncacn_conn
, &pkt
, &recv_buffer
);
1225 TALLOC_FREE(subreq
);
1226 if (!NT_STATUS_IS_OK(status
)) {
1227 DEBUG(3, ("Failed to receive ncacn packet!\n"));
1231 data_left
= recv_buffer
.length
;
1232 data
= (char *) recv_buffer
.data
;
1235 data_used
= process_incoming_data(ncacn_conn
->p
, data
, data_left
);
1236 if (data_used
< 0) {
1237 DEBUG(3, ("Failed to process dcerpc request!\n"));
1238 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
1242 data_left
-= data_used
;
1246 /* Do not leak this buffer */
1247 talloc_free(recv_buffer
.data
);
1251 * This is needed because of the way DCERPC binds work in the RPC
1254 to_send
= out
->frag
.length
- out
->current_pdu_sent
;
1257 DEBUG(10, ("Current_pdu_len = %u, "
1258 "current_pdu_sent = %u "
1259 "Returning %u bytes\n",
1260 (unsigned int)out
->frag
.length
,
1261 (unsigned int)out
->current_pdu_sent
,
1262 (unsigned int)to_send
));
1264 ncacn_conn
->iov
= talloc_zero(ncacn_conn
, struct iovec
);
1265 if (ncacn_conn
->iov
== NULL
) {
1266 status
= NT_STATUS_NO_MEMORY
;
1267 DEBUG(3, ("Out of memory!\n"));
1270 ncacn_conn
->count
= 1;
1272 ncacn_conn
->iov
[0].iov_base
= out
->frag
.data
1273 + out
->current_pdu_sent
;
1274 ncacn_conn
->iov
[0].iov_len
= to_send
;
1276 out
->current_pdu_sent
+= to_send
;
1280 * This condition is false for bind packets, or when we haven't yet got
1281 * a full request, and need to wait for more data from the client
1283 while (out
->data_sent_length
< out
->rdata
.length
) {
1284 ok
= create_next_pdu(ncacn_conn
->p
);
1286 DEBUG(3, ("Failed to create next PDU!\n"));
1287 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
1291 ncacn_conn
->iov
= talloc_realloc(ncacn_conn
,
1294 ncacn_conn
->count
+ 1);
1295 if (ncacn_conn
->iov
== NULL
) {
1296 DEBUG(3, ("Out of memory!\n"));
1297 status
= NT_STATUS_NO_MEMORY
;
1301 ncacn_conn
->iov
[ncacn_conn
->count
].iov_base
= out
->frag
.data
;
1302 ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
= out
->frag
.length
;
1304 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
1305 (unsigned int) ncacn_conn
->count
,
1306 (unsigned int) ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
));
1307 dump_data(11, (const uint8_t *) ncacn_conn
->iov
[ncacn_conn
->count
].iov_base
,
1308 ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
);
1309 ncacn_conn
->count
++;
1313 * We still don't have a complete request, go back and wait for more
1316 if (ncacn_conn
->count
== 0) {
1317 /* Wait for the next packet */
1318 subreq
= dcerpc_read_ncacn_packet_send(ncacn_conn
,
1320 ncacn_conn
->tstream
);
1321 if (subreq
== NULL
) {
1322 DEBUG(2, ("Failed to start receving packets\n"));
1323 status
= NT_STATUS_NO_MEMORY
;
1326 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1330 DEBUG(10, ("Sending a total of %u bytes\n",
1331 (unsigned int)ncacn_conn
->p
->out_data
.data_sent_length
));
1333 subreq
= tstream_writev_queue_send(ncacn_conn
,
1335 ncacn_conn
->tstream
,
1336 ncacn_conn
->send_queue
,
1339 if (subreq
== NULL
) {
1340 DEBUG(2, ("Failed to send packet\n"));
1341 status
= NT_STATUS_NO_MEMORY
;
1345 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_done
, ncacn_conn
);
1349 DEBUG(2, ("Fatal error(%s). "
1350 "Terminating client(%s) connection!\n",
1351 nt_errstr(status
), ncacn_conn
->client_name
));
1353 /* Terminate client connection */
1354 talloc_free(ncacn_conn
);
1358 static void dcerpc_ncacn_packet_done(struct tevent_req
*subreq
)
1360 struct dcerpc_ncacn_conn
*ncacn_conn
=
1361 tevent_req_callback_data(subreq
, struct dcerpc_ncacn_conn
);
1365 rc
= tstream_writev_queue_recv(subreq
, &sys_errno
);
1366 TALLOC_FREE(subreq
);
1368 DEBUG(2, ("Writev failed!\n"));
1372 /* clear out any data that may have been left around */
1373 ncacn_conn
->count
= 0;
1374 TALLOC_FREE(ncacn_conn
->iov
);
1375 data_blob_free(&ncacn_conn
->p
->in_data
.data
);
1376 data_blob_free(&ncacn_conn
->p
->out_data
.frag
);
1377 data_blob_free(&ncacn_conn
->p
->out_data
.rdata
);
1379 /* Wait for the next packet */
1380 subreq
= dcerpc_read_ncacn_packet_send(ncacn_conn
,
1382 ncacn_conn
->tstream
);
1383 if (subreq
== NULL
) {
1384 DEBUG(2, ("Failed to start receving packets\n"));
1389 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1393 DEBUG(2, ("Fatal error(%s). Terminating client(%s) connection!\n",
1394 strerror(sys_errno
), ncacn_conn
->client_name
));
1396 /* Terminate client connection */
1397 talloc_free(ncacn_conn
);
1401 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */