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 const struct ndr_syntax_id id
,
83 enum dcerpc_transport_t transport
,
84 bool ncalrpc_as_system
,
85 const char *client_address
,
86 const char *server_address
,
87 struct auth_session_info
*session_info
,
88 struct pipes_struct
**_p
,
91 struct netr_SamInfo3
*info3
;
92 struct auth_user_info_dc
*auth_user_info_dc
;
93 struct pipes_struct
*p
;
94 struct auth_serversupplied_info
*server_info
;
98 p
= talloc_zero(mem_ctx
, struct pipes_struct
);
104 p
->transport
= transport
;
105 p
->ncalrpc_as_system
= ncalrpc_as_system
;
107 p
->mem_ctx
= talloc_named(p
, 0, "pipe %s %p", pipe_name
, p
);
114 ok
= init_pipe_handles(p
, &id
);
116 DEBUG(1, ("Failed to init handles\n"));
123 data_blob_free(&p
->in_data
.data
);
124 data_blob_free(&p
->in_data
.pdu
);
126 p
->endian
= RPC_LITTLE_ENDIAN
;
128 /* Fake up an auth_user_info_dc for now, to make an info3, to make the session_info structure */
129 auth_user_info_dc
= talloc_zero(p
, struct auth_user_info_dc
);
130 if (!auth_user_info_dc
) {
136 auth_user_info_dc
->num_sids
= session_info
->security_token
->num_sids
;
137 auth_user_info_dc
->sids
= session_info
->security_token
->sids
;
138 auth_user_info_dc
->info
= session_info
->info
;
139 auth_user_info_dc
->user_session_key
= session_info
->session_key
;
141 /* This creates the input structure that make_server_info_info3 is looking for */
142 status
= auth_convert_user_info_dc_saminfo3(p
, auth_user_info_dc
,
145 if (!NT_STATUS_IS_OK(status
)) {
146 DEBUG(1, ("Failed to convert auth_user_info_dc into netr_SamInfo3\n"));
152 status
= make_server_info_info3(p
,
153 info3
->base
.account_name
.string
,
154 info3
->base
.domain
.string
,
155 &server_info
, info3
);
156 if (!NT_STATUS_IS_OK(status
)) {
157 DEBUG(1, ("Failed to init server info\n"));
164 * Some internal functions need a local token to determine access to
167 status
= create_local_token(p
, server_info
, &session_info
->session_key
, &p
->session_info
);
168 talloc_free(server_info
);
169 if (!NT_STATUS_IS_OK(status
)) {
170 DEBUG(1, ("Failed to init local auth token\n"));
176 /* Now override the session_info->security_token with the exact
177 * security_token we were given from the other side,
178 * regardless of what we just calculated */
179 p
->session_info
->security_token
= talloc_move(p
->session_info
, &session_info
->security_token
);
181 p
->client_id
= talloc_zero(p
, struct client_address
);
187 strlcpy(p
->client_id
->addr
,
188 client_address
, sizeof(p
->client_id
->addr
));
189 p
->client_id
->name
= talloc_strdup(p
->client_id
, client_address
);
190 if (p
->client_id
->name
== NULL
) {
196 if (server_address
!= NULL
) {
197 p
->server_id
= talloc_zero(p
, struct client_address
);
198 if (p
->client_id
== NULL
) {
204 strlcpy(p
->server_id
->addr
,
206 sizeof(p
->server_id
->addr
));
208 p
->server_id
->name
= talloc_strdup(p
->server_id
,
210 if (p
->server_id
->name
== NULL
) {
217 talloc_set_destructor(p
, close_internal_rpc_pipe_hnd
);
223 /* Start listening on the appropriate unix socket and setup all is needed to
224 * dispatch requests to the pipes rpc implementation */
226 struct dcerpc_ncacn_listen_state
{
227 struct ndr_syntax_id syntax_id
;
235 struct tevent_context
*ev_ctx
;
236 struct messaging_context
*msg_ctx
;
237 dcerpc_ncacn_disconnect_fn disconnect_fn
;
240 static void named_pipe_listener(struct tevent_context
*ev
,
241 struct tevent_fd
*fde
,
245 bool setup_named_pipe_socket(const char *pipe_name
,
246 struct tevent_context
*ev_ctx
)
248 struct dcerpc_ncacn_listen_state
*state
;
249 struct tevent_fd
*fde
;
252 state
= talloc(ev_ctx
, struct dcerpc_ncacn_listen_state
);
254 DEBUG(0, ("Out of memory\n"));
257 state
->ep
.name
= talloc_strdup(state
, pipe_name
);
258 if (state
->ep
.name
== NULL
) {
259 DEBUG(0, ("Out of memory\n"));
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
;
346 struct ndr_syntax_id pipe_id
;
348 struct tevent_context
*ev
;
349 struct messaging_context
*msg_ctx
;
352 uint16_t device_state
;
353 uint64_t allocation_size
;
355 struct tstream_context
*tstream
;
357 struct tsocket_address
*client
;
359 struct tsocket_address
*server
;
361 struct auth_session_info
*session_info
;
363 struct pipes_struct
*p
;
365 struct tevent_queue
*write_queue
;
371 static void named_pipe_accept_done(struct tevent_req
*subreq
);
373 static void named_pipe_accept_function(const char *pipe_name
, int fd
)
375 struct ndr_syntax_id syntax
;
376 struct named_pipe_client
*npc
;
377 struct tstream_context
*plain
;
378 struct tevent_req
*subreq
;
382 ok
= is_known_pipename(pipe_name
, &syntax
);
384 DEBUG(1, ("Unknown pipe [%s]\n", pipe_name
));
389 npc
= talloc_zero(NULL
, struct named_pipe_client
);
391 DEBUG(0, ("Out of memory!\n"));
395 npc
->pipe_name
= pipe_name
;
396 npc
->pipe_id
= syntax
;
397 npc
->ev
= server_event_context();
398 npc
->msg_ctx
= server_messaging_context();
400 /* make sure socket is in NON blocking state */
401 ret
= set_blocking(fd
, false);
403 DEBUG(2, ("Failed to make socket non-blocking\n"));
409 ret
= tstream_bsd_existing_socket(npc
, fd
, &plain
);
411 DEBUG(2, ("Failed to create tstream socket\n"));
417 npc
->file_type
= FILE_TYPE_MESSAGE_MODE_PIPE
;
418 npc
->device_state
= 0xff | 0x0400 | 0x0100;
419 npc
->allocation_size
= 4096;
421 subreq
= tstream_npa_accept_existing_send(npc
, npc
->ev
, plain
,
424 npc
->allocation_size
);
426 DEBUG(2, ("Failed to start async accept procedure\n"));
431 tevent_req_set_callback(subreq
, named_pipe_accept_done
, npc
);
434 static void named_pipe_packet_process(struct tevent_req
*subreq
);
435 static void named_pipe_packet_done(struct tevent_req
*subreq
);
437 static void named_pipe_accept_done(struct tevent_req
*subreq
)
439 struct auth_session_info_transport
*session_info_transport
;
440 struct named_pipe_client
*npc
=
441 tevent_req_callback_data(subreq
, struct named_pipe_client
);
442 const char *cli_addr
;
446 ret
= tstream_npa_accept_existing_recv(subreq
, &error
, npc
,
452 &session_info_transport
);
454 npc
->session_info
= talloc_move(npc
, &session_info_transport
->session_info
);
458 DEBUG(2, ("Failed to accept named pipe connection! (%s)\n",
464 if (tsocket_address_is_inet(npc
->client
, "ip")) {
465 cli_addr
= tsocket_address_inet_addr_string(npc
->client
,
467 if (cli_addr
== NULL
) {
475 ret
= make_server_pipes_struct(npc
,
476 npc
->pipe_name
, npc
->pipe_id
, NCACN_NP
,
477 false, cli_addr
, NULL
, npc
->session_info
,
480 DEBUG(2, ("Failed to create pipes_struct! (%s)\n",
484 npc
->p
->msg_ctx
= npc
->msg_ctx
;
486 npc
->write_queue
= tevent_queue_create(npc
, "np_server_write_queue");
487 if (!npc
->write_queue
) {
488 DEBUG(2, ("Failed to set up write queue!\n"));
492 /* And now start receiving and processing packets */
493 subreq
= dcerpc_read_ncacn_packet_send(npc
, npc
->ev
, npc
->tstream
);
495 DEBUG(2, ("Failed to start receving packets\n"));
498 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
502 DEBUG(2, ("Fatal error. Terminating client(%s) connection!\n",
504 /* terminate client connection */
509 static void named_pipe_packet_process(struct tevent_req
*subreq
)
511 struct named_pipe_client
*npc
=
512 tevent_req_callback_data(subreq
, struct named_pipe_client
);
513 struct _output_data
*out
= &npc
->p
->out_data
;
514 DATA_BLOB recv_buffer
= data_blob_null
;
515 struct ncacn_packet
*pkt
;
523 status
= dcerpc_read_ncacn_packet_recv(subreq
, npc
, &pkt
, &recv_buffer
);
525 if (!NT_STATUS_IS_OK(status
)) {
529 data_left
= recv_buffer
.length
;
530 data
= (char *)recv_buffer
.data
;
534 data_used
= process_incoming_data(npc
->p
, data
, data_left
);
536 DEBUG(3, ("Failed to process dceprc request!\n"));
537 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
541 data_left
-= data_used
;
545 /* Do not leak this buffer, npc is a long lived context */
546 talloc_free(recv_buffer
.data
);
549 /* this is needed because of the way DCERPC Binds work in
550 * the RPC marshalling code */
551 to_send
= out
->frag
.length
- out
->current_pdu_sent
;
554 DEBUG(10, ("Current_pdu_len = %u, "
555 "current_pdu_sent = %u "
556 "Returning %u bytes\n",
557 (unsigned int)out
->frag
.length
,
558 (unsigned int)out
->current_pdu_sent
,
559 (unsigned int)to_send
));
561 npc
->iov
= talloc_zero(npc
, struct iovec
);
563 status
= NT_STATUS_NO_MEMORY
;
568 npc
->iov
[0].iov_base
= out
->frag
.data
569 + out
->current_pdu_sent
;
570 npc
->iov
[0].iov_len
= to_send
;
572 out
->current_pdu_sent
+= to_send
;
575 /* this condition is false for bind packets, or when we haven't
576 * yet got a full request, and need to wait for more data from
578 while (out
->data_sent_length
< out
->rdata
.length
) {
580 ok
= create_next_pdu(npc
->p
);
582 DEBUG(3, ("Failed to create next PDU!\n"));
583 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
587 npc
->iov
= talloc_realloc(npc
, npc
->iov
,
588 struct iovec
, npc
->count
+ 1);
590 status
= NT_STATUS_NO_MEMORY
;
594 npc
->iov
[npc
->count
].iov_base
= out
->frag
.data
;
595 npc
->iov
[npc
->count
].iov_len
= out
->frag
.length
;
597 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
598 (unsigned int)npc
->count
,
599 (unsigned int)npc
->iov
[npc
->count
].iov_len
));
600 dump_data(11, (const uint8_t *)npc
->iov
[npc
->count
].iov_base
,
601 npc
->iov
[npc
->count
].iov_len
);
605 /* we still don't have a complete request, go back and wait for more
607 if (npc
->count
== 0) {
608 /* Wait for the next packet */
609 subreq
= dcerpc_read_ncacn_packet_send(npc
, npc
->ev
, npc
->tstream
);
611 DEBUG(2, ("Failed to start receving packets\n"));
612 status
= NT_STATUS_NO_MEMORY
;
615 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
619 DEBUG(10, ("Sending a total of %u bytes\n",
620 (unsigned int)npc
->p
->out_data
.data_sent_length
));
622 subreq
= tstream_writev_queue_send(npc
, npc
->ev
,
625 npc
->iov
, npc
->count
);
627 DEBUG(2, ("Failed to send packet\n"));
628 status
= NT_STATUS_NO_MEMORY
;
631 tevent_req_set_callback(subreq
, named_pipe_packet_done
, npc
);
635 DEBUG(2, ("Fatal error(%s). "
636 "Terminating client(%s) connection!\n",
637 nt_errstr(status
), npc
->client_name
));
638 /* terminate client connection */
643 static void named_pipe_packet_done(struct tevent_req
*subreq
)
645 struct named_pipe_client
*npc
=
646 tevent_req_callback_data(subreq
, struct named_pipe_client
);
650 ret
= tstream_writev_queue_recv(subreq
, &sys_errno
);
653 DEBUG(2, ("Writev failed!\n"));
657 /* clear out any data that may have been left around */
659 TALLOC_FREE(npc
->iov
);
660 data_blob_free(&npc
->p
->in_data
.data
);
661 data_blob_free(&npc
->p
->out_data
.frag
);
662 data_blob_free(&npc
->p
->out_data
.rdata
);
664 /* Wait for the next packet */
665 subreq
= dcerpc_read_ncacn_packet_send(npc
, npc
->ev
, npc
->tstream
);
667 DEBUG(2, ("Failed to start receving packets\n"));
671 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
675 DEBUG(2, ("Fatal error(%s). "
676 "Terminating client(%s) connection!\n",
677 strerror(sys_errno
), npc
->client_name
));
678 /* terminate client connection */
683 static void dcerpc_ncacn_accept(struct tevent_context
*ev_ctx
,
684 struct messaging_context
*msg_ctx
,
685 struct ndr_syntax_id syntax_id
,
686 enum dcerpc_transport_t transport
,
689 struct tsocket_address
*cli_addr
,
690 struct tsocket_address
*srv_addr
,
692 dcerpc_ncacn_disconnect_fn fn
);
694 /********************************************************************
695 * Start listening on the tcp/ip socket
696 ********************************************************************/
698 static void dcerpc_ncacn_tcpip_listener(struct tevent_context
*ev
,
699 struct tevent_fd
*fde
,
703 uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context
*ev_ctx
,
704 struct messaging_context
*msg_ctx
,
705 struct ndr_syntax_id syntax_id
,
706 const struct sockaddr_storage
*ifss
,
709 struct dcerpc_ncacn_listen_state
*state
;
710 struct tevent_fd
*fde
;
713 state
= talloc(ev_ctx
, struct dcerpc_ncacn_listen_state
);
715 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Out of memory\n"));
719 state
->syntax_id
= syntax_id
;
721 state
->ep
.port
= port
;
722 state
->disconnect_fn
= NULL
;
724 if (state
->ep
.port
== 0) {
727 for (i
= SERVER_TCP_LOW_PORT
; i
<= SERVER_TCP_HIGH_PORT
; i
++) {
728 state
->fd
= open_socket_in(SOCK_STREAM
,
739 state
->fd
= open_socket_in(SOCK_STREAM
,
745 if (state
->fd
== -1) {
746 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Failed to create "
747 "socket on port %u!\n", state
->ep
.port
));
751 state
->ev_ctx
= ev_ctx
;
752 state
->msg_ctx
= msg_ctx
;
754 /* ready to listen */
755 set_socket_options(state
->fd
, "SO_KEEPALIVE");
756 set_socket_options(state
->fd
, lp_socket_options());
758 /* Set server socket to non-blocking for the accept. */
759 set_blocking(state
->fd
, false);
761 rc
= listen(state
->fd
, SMBD_LISTEN_BACKLOG
);
763 DEBUG(0,("setup_tcpip_socket: listen - %s\n", strerror(errno
)));
767 DEBUG(10, ("setup_tcpip_socket: openened socket fd %d for port %u\n",
768 state
->fd
, state
->ep
.port
));
770 fde
= tevent_add_fd(state
->ev_ctx
,
774 dcerpc_ncacn_tcpip_listener
,
777 DEBUG(0, ("setup_tcpip_socket: Failed to add event handler!\n"));
781 tevent_fd_set_auto_close(fde
);
783 return state
->ep
.port
;
785 if (state
->fd
!= -1) {
793 static void dcerpc_ncacn_tcpip_listener(struct tevent_context
*ev
,
794 struct tevent_fd
*fde
,
798 struct dcerpc_ncacn_listen_state
*state
=
799 talloc_get_type_abort(private_data
,
800 struct dcerpc_ncacn_listen_state
);
801 struct tsocket_address
*cli_addr
= NULL
;
802 struct tsocket_address
*srv_addr
= NULL
;
803 struct sockaddr_storage addr
;
804 socklen_t in_addrlen
= sizeof(addr
);
808 s
= accept(state
->fd
, (struct sockaddr
*)(void *) &addr
, &in_addrlen
);
810 if (errno
!= EINTR
) {
811 DEBUG(0,("tcpip_listener accept: %s\n",
817 rc
= tsocket_address_bsd_from_sockaddr(state
,
818 (struct sockaddr
*)(void *) &addr
,
826 rc
= getsockname(s
, (struct sockaddr
*)(void *) &addr
, &in_addrlen
);
832 rc
= tsocket_address_bsd_from_sockaddr(state
,
833 (struct sockaddr
*)(void *) &addr
,
841 DEBUG(6, ("tcpip_listener: Accepted socket %d\n", s
));
843 dcerpc_ncacn_accept(state
->ev_ctx
,
855 /********************************************************************
856 * Start listening on the ncalrpc socket
857 ********************************************************************/
859 static void dcerpc_ncalrpc_listener(struct tevent_context
*ev
,
860 struct tevent_fd
*fde
,
864 bool setup_dcerpc_ncalrpc_socket(struct tevent_context
*ev_ctx
,
865 struct messaging_context
*msg_ctx
,
866 struct ndr_syntax_id syntax_id
,
868 dcerpc_ncacn_disconnect_fn fn
)
870 struct dcerpc_ncacn_listen_state
*state
;
871 struct tevent_fd
*fde
;
873 state
= talloc(ev_ctx
, struct dcerpc_ncacn_listen_state
);
875 DEBUG(0, ("Out of memory\n"));
879 state
->syntax_id
= syntax_id
;
881 state
->disconnect_fn
= fn
;
886 state
->ep
.name
= talloc_strdup(state
, name
);
888 if (state
->ep
.name
== NULL
) {
889 DEBUG(0, ("Out of memory\n"));
894 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0700)) {
895 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
896 lp_ncalrpc_dir(), strerror(errno
)));
900 state
->fd
= create_pipe_sock(lp_ncalrpc_dir(), name
, 0700);
901 if (state
->fd
== -1) {
902 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
903 lp_ncalrpc_dir(), name
));
907 DEBUG(10, ("Openened pipe socket fd %d for %s\n", state
->fd
, name
));
909 state
->ev_ctx
= ev_ctx
;
910 state
->msg_ctx
= msg_ctx
;
912 /* Set server socket to non-blocking for the accept. */
913 set_blocking(state
->fd
, false);
915 fde
= tevent_add_fd(state
->ev_ctx
,
919 dcerpc_ncalrpc_listener
,
922 DEBUG(0, ("Failed to add event handler for ncalrpc!\n"));
926 tevent_fd_set_auto_close(fde
);
930 if (state
->fd
!= -1) {
938 static void dcerpc_ncalrpc_listener(struct tevent_context
*ev
,
939 struct tevent_fd
*fde
,
943 struct dcerpc_ncacn_listen_state
*state
=
944 talloc_get_type_abort(private_data
,
945 struct dcerpc_ncacn_listen_state
);
946 struct tsocket_address
*cli_addr
= NULL
;
947 struct sockaddr_un sunaddr
;
948 struct sockaddr
*addr
= (struct sockaddr
*)(void *)&sunaddr
;
949 socklen_t len
= sizeof(sunaddr
);
953 ZERO_STRUCT(sunaddr
);
955 sd
= accept(state
->fd
, addr
, &len
);
957 if (errno
!= EINTR
) {
958 DEBUG(0, ("ncalrpc accept() failed: %s\n", strerror(errno
)));
963 rc
= tsocket_address_bsd_from_sockaddr(state
,
971 DEBUG(10, ("Accepted ncalrpc socket %d\n", sd
));
973 dcerpc_ncacn_accept(state
->ev_ctx
,
975 state
->syntax_id
, NCALRPC
,
978 state
->disconnect_fn
);
981 struct dcerpc_ncacn_conn
{
982 struct ndr_syntax_id syntax_id
;
984 enum dcerpc_transport_t transport
;
993 struct pipes_struct
*p
;
994 dcerpc_ncacn_disconnect_fn disconnect_fn
;
996 struct tevent_context
*ev_ctx
;
997 struct messaging_context
*msg_ctx
;
999 struct tstream_context
*tstream
;
1000 struct tevent_queue
*send_queue
;
1002 struct tsocket_address
*client
;
1004 struct tsocket_address
*server
;
1006 struct auth_session_info
*session_info
;
1012 static void dcerpc_ncacn_packet_process(struct tevent_req
*subreq
);
1013 static void dcerpc_ncacn_packet_done(struct tevent_req
*subreq
);
1015 static void dcerpc_ncacn_accept(struct tevent_context
*ev_ctx
,
1016 struct messaging_context
*msg_ctx
,
1017 struct ndr_syntax_id syntax_id
,
1018 enum dcerpc_transport_t transport
,
1021 struct tsocket_address
*cli_addr
,
1022 struct tsocket_address
*srv_addr
,
1024 dcerpc_ncacn_disconnect_fn fn
) {
1025 struct dcerpc_ncacn_conn
*ncacn_conn
;
1026 struct tevent_req
*subreq
;
1027 const char *cli_str
;
1028 const char *srv_str
= NULL
;
1029 bool system_user
= false;
1036 DEBUG(10, ("dcerpc_ncacn_accept\n"));
1038 ncacn_conn
= talloc_zero(ev_ctx
, struct dcerpc_ncacn_conn
);
1039 if (ncacn_conn
== NULL
) {
1040 DEBUG(0, ("Out of memory!\n"));
1045 ncacn_conn
->transport
= transport
;
1046 ncacn_conn
->syntax_id
= syntax_id
;
1047 ncacn_conn
->ev_ctx
= ev_ctx
;
1048 ncacn_conn
->msg_ctx
= msg_ctx
;
1049 ncacn_conn
->sock
= s
;
1050 ncacn_conn
->disconnect_fn
= fn
;
1052 ncacn_conn
->client
= talloc_move(ncacn_conn
, &cli_addr
);
1053 if (tsocket_address_is_inet(ncacn_conn
->client
, "ip")) {
1054 ncacn_conn
->client_name
=
1055 tsocket_address_inet_addr_string(ncacn_conn
->client
,
1058 ncacn_conn
->client_name
=
1059 tsocket_address_unix_path(ncacn_conn
->client
,
1062 if (ncacn_conn
->client_name
== NULL
) {
1063 DEBUG(0, ("Out of memory!\n"));
1064 talloc_free(ncacn_conn
);
1069 if (srv_addr
!= NULL
) {
1070 ncacn_conn
->server
= talloc_move(ncacn_conn
, &srv_addr
);
1072 ncacn_conn
->server_name
=
1073 tsocket_address_inet_addr_string(ncacn_conn
->server
,
1075 if (ncacn_conn
->server_name
== NULL
) {
1076 DEBUG(0, ("Out of memory!\n"));
1077 talloc_free(ncacn_conn
);
1083 switch (transport
) {
1085 ncacn_conn
->ep
.port
= port
;
1087 pipe_name
= tsocket_address_string(ncacn_conn
->client
,
1089 if (pipe_name
== NULL
) {
1091 talloc_free(ncacn_conn
);
1097 rc
= sys_getpeereid(s
, &uid
);
1099 DEBUG(2, ("Failed to get ncalrpc connecting uid!"));
1101 if (uid
== sec_initial_uid()) {
1106 ncacn_conn
->ep
.name
= talloc_strdup(ncacn_conn
, name
);
1107 if (ncacn_conn
->ep
.name
== NULL
) {
1109 talloc_free(ncacn_conn
);
1113 pipe_name
= talloc_strdup(ncacn_conn
,
1115 if (pipe_name
== NULL
) {
1117 talloc_free(ncacn_conn
);
1122 DEBUG(0, ("unknown dcerpc transport: %u!\n",
1124 talloc_free(ncacn_conn
);
1129 rc
= set_blocking(s
, false);
1131 DEBUG(2, ("Failed to set dcerpc socket to non-blocking\n"));
1132 talloc_free(ncacn_conn
);
1138 * As soon as we have tstream_bsd_existing_socket set up it will
1139 * take care of closing the socket.
1141 rc
= tstream_bsd_existing_socket(ncacn_conn
, s
, &ncacn_conn
->tstream
);
1143 DEBUG(2, ("Failed to create tstream socket for dcerpc\n"));
1144 talloc_free(ncacn_conn
);
1149 if (tsocket_address_is_inet(ncacn_conn
->client
, "ip")) {
1150 cli_str
= ncacn_conn
->client_name
;
1155 if (ncacn_conn
->server
!= NULL
) {
1156 if (tsocket_address_is_inet(ncacn_conn
->server
, "ip")) {
1157 srv_str
= ncacn_conn
->server_name
;
1163 if (ncacn_conn
->session_info
== NULL
) {
1164 status
= auth_anonymous_session_info(ncacn_conn
,
1165 &ncacn_conn
->session_info
);
1166 if (!NT_STATUS_IS_OK(status
)) {
1167 DEBUG(2, ("Failed to create "
1168 "auth_anonymous_session_info - %s\n",
1169 nt_errstr(status
)));
1170 talloc_free(ncacn_conn
);
1175 rc
= make_server_pipes_struct(ncacn_conn
,
1177 ncacn_conn
->syntax_id
,
1178 ncacn_conn
->transport
,
1182 ncacn_conn
->session_info
,
1186 DEBUG(2, ("Failed to create pipe struct - %s",
1187 strerror(sys_errno
)));
1188 talloc_free(ncacn_conn
);
1192 ncacn_conn
->send_queue
= tevent_queue_create(ncacn_conn
,
1193 "dcerpc send queue");
1194 if (ncacn_conn
->send_queue
== NULL
) {
1195 DEBUG(0, ("Out of memory!\n"));
1196 talloc_free(ncacn_conn
);
1200 subreq
= dcerpc_read_ncacn_packet_send(ncacn_conn
,
1202 ncacn_conn
->tstream
);
1203 if (subreq
== NULL
) {
1204 DEBUG(2, ("Failed to send ncacn packet\n"));
1205 talloc_free(ncacn_conn
);
1209 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1211 DEBUG(10, ("dcerpc_ncacn_accept done\n"));
1216 static void dcerpc_ncacn_packet_process(struct tevent_req
*subreq
)
1218 struct dcerpc_ncacn_conn
*ncacn_conn
=
1219 tevent_req_callback_data(subreq
, struct dcerpc_ncacn_conn
);
1221 struct _output_data
*out
= &ncacn_conn
->p
->out_data
;
1222 DATA_BLOB recv_buffer
= data_blob_null
;
1223 struct ncacn_packet
*pkt
;
1231 status
= dcerpc_read_ncacn_packet_recv(subreq
, ncacn_conn
, &pkt
, &recv_buffer
);
1232 TALLOC_FREE(subreq
);
1233 if (!NT_STATUS_IS_OK(status
)) {
1234 if (ncacn_conn
->disconnect_fn
!= NULL
) {
1235 ok
= ncacn_conn
->disconnect_fn(ncacn_conn
->p
);
1237 DEBUG(3, ("Failed to call disconnect function\n"));
1243 data_left
= recv_buffer
.length
;
1244 data
= (char *) recv_buffer
.data
;
1247 data_used
= process_incoming_data(ncacn_conn
->p
, data
, data_left
);
1248 if (data_used
< 0) {
1249 DEBUG(3, ("Failed to process dcerpc request!\n"));
1250 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
1254 data_left
-= data_used
;
1258 /* Do not leak this buffer */
1259 talloc_free(recv_buffer
.data
);
1263 * This is needed because of the way DCERPC binds work in the RPC
1266 to_send
= out
->frag
.length
- out
->current_pdu_sent
;
1269 DEBUG(10, ("Current_pdu_len = %u, "
1270 "current_pdu_sent = %u "
1271 "Returning %u bytes\n",
1272 (unsigned int)out
->frag
.length
,
1273 (unsigned int)out
->current_pdu_sent
,
1274 (unsigned int)to_send
));
1276 ncacn_conn
->iov
= talloc_zero(ncacn_conn
, struct iovec
);
1277 if (ncacn_conn
->iov
== NULL
) {
1278 status
= NT_STATUS_NO_MEMORY
;
1279 DEBUG(3, ("Out of memory!\n"));
1282 ncacn_conn
->count
= 1;
1284 ncacn_conn
->iov
[0].iov_base
= out
->frag
.data
1285 + out
->current_pdu_sent
;
1286 ncacn_conn
->iov
[0].iov_len
= to_send
;
1288 out
->current_pdu_sent
+= to_send
;
1292 * This condition is false for bind packets, or when we haven't yet got
1293 * a full request, and need to wait for more data from the client
1295 while (out
->data_sent_length
< out
->rdata
.length
) {
1296 ok
= create_next_pdu(ncacn_conn
->p
);
1298 DEBUG(3, ("Failed to create next PDU!\n"));
1299 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
1303 ncacn_conn
->iov
= talloc_realloc(ncacn_conn
,
1306 ncacn_conn
->count
+ 1);
1307 if (ncacn_conn
->iov
== NULL
) {
1308 DEBUG(3, ("Out of memory!\n"));
1309 status
= NT_STATUS_NO_MEMORY
;
1313 ncacn_conn
->iov
[ncacn_conn
->count
].iov_base
= out
->frag
.data
;
1314 ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
= out
->frag
.length
;
1316 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
1317 (unsigned int) ncacn_conn
->count
,
1318 (unsigned int) ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
));
1319 dump_data(11, (const uint8_t *) ncacn_conn
->iov
[ncacn_conn
->count
].iov_base
,
1320 ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
);
1321 ncacn_conn
->count
++;
1325 * We still don't have a complete request, go back and wait for more
1328 if (ncacn_conn
->count
== 0) {
1329 /* Wait for the next packet */
1330 subreq
= dcerpc_read_ncacn_packet_send(ncacn_conn
,
1332 ncacn_conn
->tstream
);
1333 if (subreq
== NULL
) {
1334 DEBUG(2, ("Failed to start receving packets\n"));
1335 status
= NT_STATUS_NO_MEMORY
;
1338 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1342 DEBUG(10, ("Sending a total of %u bytes\n",
1343 (unsigned int)ncacn_conn
->p
->out_data
.data_sent_length
));
1345 subreq
= tstream_writev_queue_send(ncacn_conn
,
1347 ncacn_conn
->tstream
,
1348 ncacn_conn
->send_queue
,
1351 if (subreq
== NULL
) {
1352 DEBUG(2, ("Failed to send packet\n"));
1353 status
= NT_STATUS_NO_MEMORY
;
1357 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_done
, ncacn_conn
);
1361 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1362 ncacn_conn
->client_name
, nt_errstr(status
)));
1364 /* Terminate client connection */
1365 talloc_free(ncacn_conn
);
1369 static void dcerpc_ncacn_packet_done(struct tevent_req
*subreq
)
1371 struct dcerpc_ncacn_conn
*ncacn_conn
=
1372 tevent_req_callback_data(subreq
, struct dcerpc_ncacn_conn
);
1373 NTSTATUS status
= NT_STATUS_OK
;
1377 rc
= tstream_writev_queue_recv(subreq
, &sys_errno
);
1378 TALLOC_FREE(subreq
);
1380 DEBUG(2, ("Writev failed!\n"));
1381 status
= map_nt_error_from_unix(sys_errno
);
1385 /* clear out any data that may have been left around */
1386 ncacn_conn
->count
= 0;
1387 TALLOC_FREE(ncacn_conn
->iov
);
1388 data_blob_free(&ncacn_conn
->p
->in_data
.data
);
1389 data_blob_free(&ncacn_conn
->p
->out_data
.frag
);
1390 data_blob_free(&ncacn_conn
->p
->out_data
.rdata
);
1392 /* Wait for the next packet */
1393 subreq
= dcerpc_read_ncacn_packet_send(ncacn_conn
,
1395 ncacn_conn
->tstream
);
1396 if (subreq
== NULL
) {
1397 DEBUG(2, ("Failed to start receving packets\n"));
1398 status
= NT_STATUS_NO_MEMORY
;
1402 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1406 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1407 ncacn_conn
->client_name
, nt_errstr(status
)));
1409 /* Terminate client connection */
1410 talloc_free(ncacn_conn
);
1414 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */