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 "lib/tsocket/tsocket.h"
26 #include "libcli/named_pipe_auth/npa_tstream.h"
27 #include "../auth/auth_sam_reply.h"
31 #define SERVER_TCP_LOW_PORT 1024
32 #define SERVER_TCP_HIGH_PORT 1300
34 static NTSTATUS
auth_anonymous_session_info(TALLOC_CTX
*mem_ctx
,
35 struct auth_session_info_transport
**session_info
)
37 struct auth_session_info_transport
*i
;
38 struct auth_serversupplied_info
*s
;
39 struct auth_user_info_dc
*u
;
40 union netr_Validation val
;
43 i
= talloc_zero(mem_ctx
, struct auth_session_info_transport
);
45 return NT_STATUS_NO_MEMORY
;
48 status
= make_server_info_guest(i
, &s
);
49 if (!NT_STATUS_IS_OK(status
)) {
53 i
->security_token
= s
->security_token
;
54 i
->session_key
= s
->user_session_key
;
58 status
= make_user_info_dc_netlogon_validation(mem_ctx
,
63 if (!NT_STATUS_IS_OK(status
)) {
64 DEBUG(0, ("conversion of info3 into user_info_dc failed!\n"));
67 i
->info
= talloc_move(i
, &u
->info
);
75 /* Creates a pipes_struct and initializes it with the information
76 * sent from the client */
77 static int make_server_pipes_struct(TALLOC_CTX
*mem_ctx
,
78 const char *pipe_name
,
79 const struct ndr_syntax_id id
,
80 enum dcerpc_transport_t transport
,
81 bool ncalrpc_as_system
,
82 const char *client_address
,
83 const char *server_address
,
84 struct auth_session_info_transport
*session_info
,
85 struct pipes_struct
**_p
,
88 struct netr_SamInfo3
*info3
;
89 struct auth_user_info_dc
*auth_user_info_dc
;
90 struct pipes_struct
*p
;
94 p
= talloc_zero(mem_ctx
, struct pipes_struct
);
100 p
->transport
= transport
;
101 p
->ncalrpc_as_system
= ncalrpc_as_system
;
103 p
->mem_ctx
= talloc_named(p
, 0, "pipe %s %p", pipe_name
, p
);
110 ok
= init_pipe_handles(p
, &id
);
112 DEBUG(1, ("Failed to init handles\n"));
119 data_blob_free(&p
->in_data
.data
);
120 data_blob_free(&p
->in_data
.pdu
);
122 p
->endian
= RPC_LITTLE_ENDIAN
;
124 /* Fake up an auth_user_info_dc for now, to make an info3, to make the session_info structure */
125 auth_user_info_dc
= talloc_zero(p
, struct auth_user_info_dc
);
126 if (!auth_user_info_dc
) {
132 auth_user_info_dc
->num_sids
= session_info
->security_token
->num_sids
;
133 auth_user_info_dc
->sids
= session_info
->security_token
->sids
;
134 auth_user_info_dc
->info
= session_info
->info
;
135 auth_user_info_dc
->user_session_key
= session_info
->session_key
;
137 /* This creates the input structure that make_server_info_info3 is looking for */
138 status
= auth_convert_user_info_dc_saminfo3(p
, auth_user_info_dc
,
141 if (!NT_STATUS_IS_OK(status
)) {
142 DEBUG(1, ("Failed to convert auth_user_info_dc into netr_SamInfo3\n"));
148 status
= make_server_info_info3(p
,
149 info3
->base
.account_name
.string
,
150 info3
->base
.domain
.string
,
151 &p
->session_info
, info3
);
152 if (!NT_STATUS_IS_OK(status
)) {
153 DEBUG(1, ("Failed to init server info\n"));
160 * Some internal functions need a local token to determine access to
163 status
= create_local_token(p
->session_info
);
164 if (!NT_STATUS_IS_OK(status
)) {
165 DEBUG(1, ("Failed to init local auth token\n"));
171 /* Now override the session_info->security_token with the exact
172 * security_token we were given from the other side,
173 * regardless of what we just calculated */
174 p
->session_info
->security_token
= talloc_move(p
->session_info
, &session_info
->security_token
);
176 /* Also set the session key to the correct value */
177 p
->session_info
->user_session_key
= session_info
->session_key
;
178 p
->session_info
->user_session_key
.data
= talloc_move(p
->session_info
, &session_info
->session_key
.data
);
180 p
->client_id
= talloc_zero(p
, struct client_address
);
186 strlcpy(p
->client_id
->addr
,
187 client_address
, sizeof(p
->client_id
->addr
));
188 p
->client_id
->name
= talloc_strdup(p
->client_id
, client_address
);
189 if (p
->client_id
->name
== NULL
) {
195 if (server_address
!= NULL
) {
196 p
->server_id
= talloc_zero(p
, struct client_address
);
197 if (p
->client_id
== NULL
) {
203 strlcpy(p
->server_id
->addr
,
205 sizeof(p
->server_id
->addr
));
207 p
->server_id
->name
= talloc_strdup(p
->server_id
,
209 if (p
->server_id
->name
== NULL
) {
216 talloc_set_destructor(p
, close_internal_rpc_pipe_hnd
);
222 /* Start listening on the appropriate unix socket and setup all is needed to
223 * dispatch requests to the pipes rpc implementation */
225 struct dcerpc_ncacn_listen_state
{
226 struct ndr_syntax_id syntax_id
;
234 struct tevent_context
*ev_ctx
;
235 struct messaging_context
*msg_ctx
;
236 dcerpc_ncacn_disconnect_fn disconnect_fn
;
239 static void named_pipe_listener(struct tevent_context
*ev
,
240 struct tevent_fd
*fde
,
244 bool setup_named_pipe_socket(const char *pipe_name
,
245 struct tevent_context
*ev_ctx
)
247 struct dcerpc_ncacn_listen_state
*state
;
248 struct tevent_fd
*fde
;
251 state
= talloc(ev_ctx
, struct dcerpc_ncacn_listen_state
);
253 DEBUG(0, ("Out of memory\n"));
256 state
->ep
.name
= talloc_strdup(state
, pipe_name
);
257 if (state
->ep
.name
== NULL
) {
258 DEBUG(0, ("Out of memory\n"));
263 np_dir
= talloc_asprintf(state
, "%s/np", lp_ncalrpc_dir());
265 DEBUG(0, ("Out of memory\n"));
269 if (!directory_create_or_exist(np_dir
, geteuid(), 0700)) {
270 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
271 np_dir
, strerror(errno
)));
275 state
->fd
= create_pipe_sock(np_dir
, pipe_name
, 0700);
276 if (state
->fd
== -1) {
277 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
282 DEBUG(10, ("Openened pipe socket fd %d for %s\n",
283 state
->fd
, pipe_name
));
285 fde
= tevent_add_fd(ev_ctx
,
286 state
, state
->fd
, TEVENT_FD_READ
,
287 named_pipe_listener
, state
);
289 DEBUG(0, ("Failed to add event handler!\n"));
293 tevent_fd_set_auto_close(fde
);
297 if (state
->fd
!= -1) {
304 static void named_pipe_accept_function(const char *pipe_name
, int fd
);
306 static void named_pipe_listener(struct tevent_context
*ev
,
307 struct tevent_fd
*fde
,
311 struct dcerpc_ncacn_listen_state
*state
=
312 talloc_get_type_abort(private_data
,
313 struct dcerpc_ncacn_listen_state
);
314 struct sockaddr_un sunaddr
;
318 /* TODO: should we have a limit to the number of clients ? */
320 len
= sizeof(sunaddr
);
322 sd
= accept(state
->fd
,
323 (struct sockaddr
*)(void *)&sunaddr
, &len
);
326 if (errno
!= EINTR
) {
327 DEBUG(6, ("Failed to get a valid socket [%s]\n",
333 DEBUG(6, ("Accepted socket %d\n", sd
));
335 named_pipe_accept_function(state
->ep
.name
, sd
);
339 /* This is the core of the rpc server.
340 * Accepts connections from clients and process requests using the appropriate
341 * dispatcher table. */
343 struct named_pipe_client
{
344 const char *pipe_name
;
345 struct ndr_syntax_id pipe_id
;
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_transport
*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 ndr_syntax_id syntax
;
375 struct named_pipe_client
*npc
;
376 struct tstream_context
*plain
;
377 struct tevent_req
*subreq
;
381 ok
= is_known_pipename(pipe_name
, &syntax
);
383 DEBUG(1, ("Unknown pipe [%s]\n", pipe_name
));
388 npc
= talloc_zero(NULL
, struct named_pipe_client
);
390 DEBUG(0, ("Out of memory!\n"));
394 npc
->pipe_name
= pipe_name
;
395 npc
->pipe_id
= syntax
;
396 npc
->ev
= server_event_context();
397 npc
->msg_ctx
= server_messaging_context();
399 /* make sure socket is in NON blocking state */
400 ret
= set_blocking(fd
, false);
402 DEBUG(2, ("Failed to make socket non-blocking\n"));
408 ret
= tstream_bsd_existing_socket(npc
, fd
, &plain
);
410 DEBUG(2, ("Failed to create tstream socket\n"));
416 npc
->file_type
= FILE_TYPE_MESSAGE_MODE_PIPE
;
417 npc
->device_state
= 0xff | 0x0400 | 0x0100;
418 npc
->allocation_size
= 4096;
420 subreq
= tstream_npa_accept_existing_send(npc
, npc
->ev
, plain
,
423 npc
->allocation_size
);
425 DEBUG(2, ("Failed to start async accept procedure\n"));
430 tevent_req_set_callback(subreq
, named_pipe_accept_done
, npc
);
433 static void named_pipe_packet_process(struct tevent_req
*subreq
);
434 static void named_pipe_packet_done(struct tevent_req
*subreq
);
436 static void named_pipe_accept_done(struct tevent_req
*subreq
)
438 struct named_pipe_client
*npc
=
439 tevent_req_callback_data(subreq
, struct named_pipe_client
);
440 const char *cli_addr
;
444 ret
= tstream_npa_accept_existing_recv(subreq
, &error
, npc
,
453 DEBUG(2, ("Failed to accept named pipe connection! (%s)\n",
459 if (tsocket_address_is_inet(npc
->client
, "ip")) {
460 cli_addr
= tsocket_address_inet_addr_string(npc
->client
,
462 if (cli_addr
== NULL
) {
470 ret
= make_server_pipes_struct(npc
,
471 npc
->pipe_name
, npc
->pipe_id
, NCACN_NP
,
472 false, cli_addr
, NULL
, npc
->session_info
,
475 DEBUG(2, ("Failed to create pipes_struct! (%s)\n",
479 npc
->p
->msg_ctx
= npc
->msg_ctx
;
481 npc
->write_queue
= tevent_queue_create(npc
, "np_server_write_queue");
482 if (!npc
->write_queue
) {
483 DEBUG(2, ("Failed to set up write queue!\n"));
487 /* And now start receaving and processing packets */
488 subreq
= dcerpc_read_ncacn_packet_send(npc
, npc
->ev
, npc
->tstream
);
490 DEBUG(2, ("Failed to start receving packets\n"));
493 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
497 DEBUG(2, ("Fatal error. Terminating client(%s) connection!\n",
499 /* terminate client connection */
504 static void named_pipe_packet_process(struct tevent_req
*subreq
)
506 struct named_pipe_client
*npc
=
507 tevent_req_callback_data(subreq
, struct named_pipe_client
);
508 struct _output_data
*out
= &npc
->p
->out_data
;
509 DATA_BLOB recv_buffer
= data_blob_null
;
510 struct ncacn_packet
*pkt
;
518 status
= dcerpc_read_ncacn_packet_recv(subreq
, npc
, &pkt
, &recv_buffer
);
520 if (!NT_STATUS_IS_OK(status
)) {
524 data_left
= recv_buffer
.length
;
525 data
= (char *)recv_buffer
.data
;
529 data_used
= process_incoming_data(npc
->p
, data
, data_left
);
531 DEBUG(3, ("Failed to process dceprc request!\n"));
532 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
536 data_left
-= data_used
;
540 /* Do not leak this buffer, npc is a long lived context */
541 talloc_free(recv_buffer
.data
);
544 /* this is needed because of the way DCERPC Binds work in
545 * the RPC marshalling code */
546 to_send
= out
->frag
.length
- out
->current_pdu_sent
;
549 DEBUG(10, ("Current_pdu_len = %u, "
550 "current_pdu_sent = %u "
551 "Returning %u bytes\n",
552 (unsigned int)out
->frag
.length
,
553 (unsigned int)out
->current_pdu_sent
,
554 (unsigned int)to_send
));
556 npc
->iov
= talloc_zero(npc
, struct iovec
);
558 status
= NT_STATUS_NO_MEMORY
;
563 npc
->iov
[0].iov_base
= out
->frag
.data
564 + out
->current_pdu_sent
;
565 npc
->iov
[0].iov_len
= to_send
;
567 out
->current_pdu_sent
+= to_send
;
570 /* this condition is false for bind packets, or when we haven't
571 * yet got a full request, and need to wait for more data from
573 while (out
->data_sent_length
< out
->rdata
.length
) {
575 ok
= create_next_pdu(npc
->p
);
577 DEBUG(3, ("Failed to create next PDU!\n"));
578 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
582 npc
->iov
= talloc_realloc(npc
, npc
->iov
,
583 struct iovec
, npc
->count
+ 1);
585 status
= NT_STATUS_NO_MEMORY
;
589 npc
->iov
[npc
->count
].iov_base
= out
->frag
.data
;
590 npc
->iov
[npc
->count
].iov_len
= out
->frag
.length
;
592 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
593 (unsigned int)npc
->count
,
594 (unsigned int)npc
->iov
[npc
->count
].iov_len
));
595 dump_data(11, (const uint8_t *)npc
->iov
[npc
->count
].iov_base
,
596 npc
->iov
[npc
->count
].iov_len
);
600 /* we still don't have a complete request, go back and wait for more
602 if (npc
->count
== 0) {
603 /* Wait for the next packet */
604 subreq
= dcerpc_read_ncacn_packet_send(npc
, npc
->ev
, npc
->tstream
);
606 DEBUG(2, ("Failed to start receving packets\n"));
607 status
= NT_STATUS_NO_MEMORY
;
610 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
614 DEBUG(10, ("Sending a total of %u bytes\n",
615 (unsigned int)npc
->p
->out_data
.data_sent_length
));
617 subreq
= tstream_writev_queue_send(npc
, npc
->ev
,
620 npc
->iov
, npc
->count
);
622 DEBUG(2, ("Failed to send packet\n"));
623 status
= NT_STATUS_NO_MEMORY
;
626 tevent_req_set_callback(subreq
, named_pipe_packet_done
, npc
);
630 DEBUG(2, ("Fatal error(%s). "
631 "Terminating client(%s) connection!\n",
632 nt_errstr(status
), npc
->client_name
));
633 /* terminate client connection */
638 static void named_pipe_packet_done(struct tevent_req
*subreq
)
640 struct named_pipe_client
*npc
=
641 tevent_req_callback_data(subreq
, struct named_pipe_client
);
645 ret
= tstream_writev_queue_recv(subreq
, &sys_errno
);
648 DEBUG(2, ("Writev failed!\n"));
652 /* clear out any data that may have been left around */
654 TALLOC_FREE(npc
->iov
);
655 data_blob_free(&npc
->p
->in_data
.data
);
656 data_blob_free(&npc
->p
->out_data
.frag
);
657 data_blob_free(&npc
->p
->out_data
.rdata
);
659 /* Wait for the next packet */
660 subreq
= dcerpc_read_ncacn_packet_send(npc
, npc
->ev
, npc
->tstream
);
662 DEBUG(2, ("Failed to start receving packets\n"));
666 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
670 DEBUG(2, ("Fatal error(%s). "
671 "Terminating client(%s) connection!\n",
672 strerror(sys_errno
), npc
->client_name
));
673 /* terminate client connection */
678 static void dcerpc_ncacn_accept(struct tevent_context
*ev_ctx
,
679 struct messaging_context
*msg_ctx
,
680 struct ndr_syntax_id syntax_id
,
681 enum dcerpc_transport_t transport
,
684 struct tsocket_address
*cli_addr
,
685 struct tsocket_address
*srv_addr
,
687 dcerpc_ncacn_disconnect_fn fn
);
689 /********************************************************************
690 * Start listening on the tcp/ip socket
691 ********************************************************************/
693 static void dcerpc_ncacn_tcpip_listener(struct tevent_context
*ev
,
694 struct tevent_fd
*fde
,
698 uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context
*ev_ctx
,
699 struct messaging_context
*msg_ctx
,
700 struct ndr_syntax_id syntax_id
,
701 const struct sockaddr_storage
*ifss
,
704 struct dcerpc_ncacn_listen_state
*state
;
705 struct tevent_fd
*fde
;
708 state
= talloc(ev_ctx
, struct dcerpc_ncacn_listen_state
);
710 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Out of memory\n"));
714 state
->syntax_id
= syntax_id
;
716 state
->ep
.port
= port
;
717 state
->disconnect_fn
= NULL
;
719 if (state
->ep
.port
== 0) {
722 for (i
= SERVER_TCP_LOW_PORT
; i
<= SERVER_TCP_HIGH_PORT
; i
++) {
723 state
->fd
= open_socket_in(SOCK_STREAM
,
734 state
->fd
= open_socket_in(SOCK_STREAM
,
740 if (state
->fd
== -1) {
741 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Failed to create "
742 "socket on port %u!\n", state
->ep
.port
));
746 state
->ev_ctx
= ev_ctx
;
747 state
->msg_ctx
= msg_ctx
;
749 /* ready to listen */
750 set_socket_options(state
->fd
, "SO_KEEPALIVE");
751 set_socket_options(state
->fd
, lp_socket_options());
753 /* Set server socket to non-blocking for the accept. */
754 set_blocking(state
->fd
, false);
756 rc
= listen(state
->fd
, SMBD_LISTEN_BACKLOG
);
758 DEBUG(0,("setup_tcpip_socket: listen - %s\n", strerror(errno
)));
762 DEBUG(10, ("setup_tcpip_socket: openened socket fd %d for port %u\n",
763 state
->fd
, state
->ep
.port
));
765 fde
= tevent_add_fd(state
->ev_ctx
,
769 dcerpc_ncacn_tcpip_listener
,
772 DEBUG(0, ("setup_tcpip_socket: Failed to add event handler!\n"));
776 tevent_fd_set_auto_close(fde
);
778 return state
->ep
.port
;
780 if (state
->fd
!= -1) {
788 static void dcerpc_ncacn_tcpip_listener(struct tevent_context
*ev
,
789 struct tevent_fd
*fde
,
793 struct dcerpc_ncacn_listen_state
*state
=
794 talloc_get_type_abort(private_data
,
795 struct dcerpc_ncacn_listen_state
);
796 struct tsocket_address
*cli_addr
= NULL
;
797 struct tsocket_address
*srv_addr
= NULL
;
798 struct sockaddr_storage addr
;
799 socklen_t in_addrlen
= sizeof(addr
);
803 s
= accept(state
->fd
, (struct sockaddr
*)(void *) &addr
, &in_addrlen
);
805 if (errno
!= EINTR
) {
806 DEBUG(0,("tcpip_listener accept: %s\n",
812 rc
= tsocket_address_bsd_from_sockaddr(state
,
813 (struct sockaddr
*)(void *) &addr
,
821 rc
= getsockname(s
, (struct sockaddr
*)(void *) &addr
, &in_addrlen
);
827 rc
= tsocket_address_bsd_from_sockaddr(state
,
828 (struct sockaddr
*)(void *) &addr
,
836 DEBUG(6, ("tcpip_listener: Accepted socket %d\n", s
));
838 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
,
863 dcerpc_ncacn_disconnect_fn fn
)
865 struct dcerpc_ncacn_listen_state
*state
;
866 struct tevent_fd
*fde
;
868 state
= talloc(ev_ctx
, struct dcerpc_ncacn_listen_state
);
870 DEBUG(0, ("Out of memory\n"));
874 state
->syntax_id
= syntax_id
;
876 state
->disconnect_fn
= fn
;
881 state
->ep
.name
= talloc_strdup(state
, name
);
883 if (state
->ep
.name
== NULL
) {
884 DEBUG(0, ("Out of memory\n"));
889 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0700)) {
890 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
891 lp_ncalrpc_dir(), strerror(errno
)));
895 state
->fd
= create_pipe_sock(lp_ncalrpc_dir(), name
, 0700);
896 if (state
->fd
== -1) {
897 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
898 lp_ncalrpc_dir(), name
));
902 DEBUG(10, ("Openened pipe socket fd %d for %s\n", state
->fd
, name
));
904 state
->ev_ctx
= ev_ctx
;
905 state
->msg_ctx
= msg_ctx
;
907 /* Set server socket to non-blocking for the accept. */
908 set_blocking(state
->fd
, false);
910 fde
= tevent_add_fd(state
->ev_ctx
,
914 dcerpc_ncalrpc_listener
,
917 DEBUG(0, ("Failed to add event handler for ncalrpc!\n"));
921 tevent_fd_set_auto_close(fde
);
925 if (state
->fd
!= -1) {
933 static void dcerpc_ncalrpc_listener(struct tevent_context
*ev
,
934 struct tevent_fd
*fde
,
938 struct dcerpc_ncacn_listen_state
*state
=
939 talloc_get_type_abort(private_data
,
940 struct dcerpc_ncacn_listen_state
);
941 struct tsocket_address
*cli_addr
= NULL
;
942 struct sockaddr_un sunaddr
;
943 struct sockaddr
*addr
= (struct sockaddr
*)(void *)&sunaddr
;
944 socklen_t len
= sizeof(sunaddr
);
948 ZERO_STRUCT(sunaddr
);
950 sd
= accept(state
->fd
, addr
, &len
);
952 if (errno
!= EINTR
) {
953 DEBUG(0, ("ncalrpc accept() failed: %s\n", strerror(errno
)));
958 rc
= tsocket_address_bsd_from_sockaddr(state
,
966 DEBUG(10, ("Accepted ncalrpc socket %d\n", sd
));
968 dcerpc_ncacn_accept(state
->ev_ctx
,
970 state
->syntax_id
, NCALRPC
,
973 state
->disconnect_fn
);
976 struct dcerpc_ncacn_conn
{
977 struct ndr_syntax_id syntax_id
;
979 enum dcerpc_transport_t transport
;
988 struct pipes_struct
*p
;
989 dcerpc_ncacn_disconnect_fn disconnect_fn
;
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 dcerpc_ncacn_disconnect_fn fn
) {
1020 struct dcerpc_ncacn_conn
*ncacn_conn
;
1021 struct tevent_req
*subreq
;
1022 const char *cli_str
;
1023 const char *srv_str
= NULL
;
1024 bool system_user
= false;
1031 DEBUG(10, ("dcerpc_ncacn_accept\n"));
1033 ncacn_conn
= talloc_zero(ev_ctx
, struct dcerpc_ncacn_conn
);
1034 if (ncacn_conn
== NULL
) {
1035 DEBUG(0, ("Out of memory!\n"));
1040 ncacn_conn
->transport
= transport
;
1041 ncacn_conn
->syntax_id
= syntax_id
;
1042 ncacn_conn
->ev_ctx
= ev_ctx
;
1043 ncacn_conn
->msg_ctx
= msg_ctx
;
1044 ncacn_conn
->sock
= s
;
1045 ncacn_conn
->disconnect_fn
= fn
;
1047 ncacn_conn
->client
= talloc_move(ncacn_conn
, &cli_addr
);
1048 if (tsocket_address_is_inet(ncacn_conn
->client
, "ip")) {
1049 ncacn_conn
->client_name
=
1050 tsocket_address_inet_addr_string(ncacn_conn
->client
,
1053 ncacn_conn
->client_name
=
1054 tsocket_address_unix_path(ncacn_conn
->client
,
1057 if (ncacn_conn
->client_name
== NULL
) {
1058 DEBUG(0, ("Out of memory!\n"));
1059 talloc_free(ncacn_conn
);
1064 if (srv_addr
!= NULL
) {
1065 ncacn_conn
->server
= talloc_move(ncacn_conn
, &srv_addr
);
1067 ncacn_conn
->server_name
=
1068 tsocket_address_inet_addr_string(ncacn_conn
->server
,
1070 if (ncacn_conn
->server_name
== NULL
) {
1071 DEBUG(0, ("Out of memory!\n"));
1072 talloc_free(ncacn_conn
);
1078 switch (transport
) {
1080 ncacn_conn
->ep
.port
= port
;
1082 pipe_name
= tsocket_address_string(ncacn_conn
->client
,
1084 if (pipe_name
== NULL
) {
1086 talloc_free(ncacn_conn
);
1092 rc
= sys_getpeereid(s
, &uid
);
1094 DEBUG(2, ("Failed to get ncalrpc connecting uid!"));
1096 if (uid
== sec_initial_uid()) {
1101 ncacn_conn
->ep
.name
= talloc_strdup(ncacn_conn
, name
);
1102 if (ncacn_conn
->ep
.name
== NULL
) {
1104 talloc_free(ncacn_conn
);
1108 pipe_name
= talloc_strdup(ncacn_conn
,
1110 if (pipe_name
== NULL
) {
1112 talloc_free(ncacn_conn
);
1117 DEBUG(0, ("unknown dcerpc transport: %u!\n",
1119 talloc_free(ncacn_conn
);
1124 rc
= set_blocking(s
, false);
1126 DEBUG(2, ("Failed to set dcerpc socket to non-blocking\n"));
1127 talloc_free(ncacn_conn
);
1133 * As soon as we have tstream_bsd_existing_socket set up it will
1134 * take care of closing the socket.
1136 rc
= tstream_bsd_existing_socket(ncacn_conn
, s
, &ncacn_conn
->tstream
);
1138 DEBUG(2, ("Failed to create tstream socket for dcerpc\n"));
1139 talloc_free(ncacn_conn
);
1144 if (tsocket_address_is_inet(ncacn_conn
->client
, "ip")) {
1145 cli_str
= ncacn_conn
->client_name
;
1150 if (ncacn_conn
->server
!= NULL
) {
1151 if (tsocket_address_is_inet(ncacn_conn
->server
, "ip")) {
1152 srv_str
= ncacn_conn
->server_name
;
1158 if (ncacn_conn
->session_info
== NULL
) {
1159 status
= auth_anonymous_session_info(ncacn_conn
,
1160 &ncacn_conn
->session_info
);
1161 if (!NT_STATUS_IS_OK(status
)) {
1162 DEBUG(2, ("Failed to create "
1163 "auth_anonymous_session_info - %s\n",
1164 nt_errstr(status
)));
1165 talloc_free(ncacn_conn
);
1170 rc
= make_server_pipes_struct(ncacn_conn
,
1172 ncacn_conn
->syntax_id
,
1173 ncacn_conn
->transport
,
1177 ncacn_conn
->session_info
,
1181 DEBUG(2, ("Failed to create pipe struct - %s",
1182 strerror(sys_errno
)));
1183 talloc_free(ncacn_conn
);
1187 ncacn_conn
->send_queue
= tevent_queue_create(ncacn_conn
,
1188 "dcerpc send queue");
1189 if (ncacn_conn
->send_queue
== NULL
) {
1190 DEBUG(0, ("Out of memory!\n"));
1191 talloc_free(ncacn_conn
);
1195 subreq
= dcerpc_read_ncacn_packet_send(ncacn_conn
,
1197 ncacn_conn
->tstream
);
1198 if (subreq
== NULL
) {
1199 DEBUG(2, ("Failed to send ncacn packet\n"));
1200 talloc_free(ncacn_conn
);
1204 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1206 DEBUG(10, ("dcerpc_ncacn_accept done\n"));
1211 static void dcerpc_ncacn_packet_process(struct tevent_req
*subreq
)
1213 struct dcerpc_ncacn_conn
*ncacn_conn
=
1214 tevent_req_callback_data(subreq
, struct dcerpc_ncacn_conn
);
1216 struct _output_data
*out
= &ncacn_conn
->p
->out_data
;
1217 DATA_BLOB recv_buffer
= data_blob_null
;
1218 struct ncacn_packet
*pkt
;
1226 status
= dcerpc_read_ncacn_packet_recv(subreq
, ncacn_conn
, &pkt
, &recv_buffer
);
1227 TALLOC_FREE(subreq
);
1228 if (!NT_STATUS_IS_OK(status
)) {
1229 if (ncacn_conn
->disconnect_fn
!= NULL
) {
1230 ok
= ncacn_conn
->disconnect_fn(ncacn_conn
->p
);
1232 DEBUG(3, ("Failed to call disconnect function\n"));
1238 data_left
= recv_buffer
.length
;
1239 data
= (char *) recv_buffer
.data
;
1242 data_used
= process_incoming_data(ncacn_conn
->p
, data
, data_left
);
1243 if (data_used
< 0) {
1244 DEBUG(3, ("Failed to process dcerpc request!\n"));
1245 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
1249 data_left
-= data_used
;
1253 /* Do not leak this buffer */
1254 talloc_free(recv_buffer
.data
);
1258 * This is needed because of the way DCERPC binds work in the RPC
1261 to_send
= out
->frag
.length
- out
->current_pdu_sent
;
1264 DEBUG(10, ("Current_pdu_len = %u, "
1265 "current_pdu_sent = %u "
1266 "Returning %u bytes\n",
1267 (unsigned int)out
->frag
.length
,
1268 (unsigned int)out
->current_pdu_sent
,
1269 (unsigned int)to_send
));
1271 ncacn_conn
->iov
= talloc_zero(ncacn_conn
, struct iovec
);
1272 if (ncacn_conn
->iov
== NULL
) {
1273 status
= NT_STATUS_NO_MEMORY
;
1274 DEBUG(3, ("Out of memory!\n"));
1277 ncacn_conn
->count
= 1;
1279 ncacn_conn
->iov
[0].iov_base
= out
->frag
.data
1280 + out
->current_pdu_sent
;
1281 ncacn_conn
->iov
[0].iov_len
= to_send
;
1283 out
->current_pdu_sent
+= to_send
;
1287 * This condition is false for bind packets, or when we haven't yet got
1288 * a full request, and need to wait for more data from the client
1290 while (out
->data_sent_length
< out
->rdata
.length
) {
1291 ok
= create_next_pdu(ncacn_conn
->p
);
1293 DEBUG(3, ("Failed to create next PDU!\n"));
1294 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
1298 ncacn_conn
->iov
= talloc_realloc(ncacn_conn
,
1301 ncacn_conn
->count
+ 1);
1302 if (ncacn_conn
->iov
== NULL
) {
1303 DEBUG(3, ("Out of memory!\n"));
1304 status
= NT_STATUS_NO_MEMORY
;
1308 ncacn_conn
->iov
[ncacn_conn
->count
].iov_base
= out
->frag
.data
;
1309 ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
= out
->frag
.length
;
1311 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
1312 (unsigned int) ncacn_conn
->count
,
1313 (unsigned int) ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
));
1314 dump_data(11, (const uint8_t *) ncacn_conn
->iov
[ncacn_conn
->count
].iov_base
,
1315 ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
);
1316 ncacn_conn
->count
++;
1320 * We still don't have a complete request, go back and wait for more
1323 if (ncacn_conn
->count
== 0) {
1324 /* Wait for the next packet */
1325 subreq
= dcerpc_read_ncacn_packet_send(ncacn_conn
,
1327 ncacn_conn
->tstream
);
1328 if (subreq
== NULL
) {
1329 DEBUG(2, ("Failed to start receving packets\n"));
1330 status
= NT_STATUS_NO_MEMORY
;
1333 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1337 DEBUG(10, ("Sending a total of %u bytes\n",
1338 (unsigned int)ncacn_conn
->p
->out_data
.data_sent_length
));
1340 subreq
= tstream_writev_queue_send(ncacn_conn
,
1342 ncacn_conn
->tstream
,
1343 ncacn_conn
->send_queue
,
1346 if (subreq
== NULL
) {
1347 DEBUG(2, ("Failed to send packet\n"));
1348 status
= NT_STATUS_NO_MEMORY
;
1352 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_done
, ncacn_conn
);
1356 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1357 ncacn_conn
->client_name
, nt_errstr(status
)));
1359 /* Terminate client connection */
1360 talloc_free(ncacn_conn
);
1364 static void dcerpc_ncacn_packet_done(struct tevent_req
*subreq
)
1366 struct dcerpc_ncacn_conn
*ncacn_conn
=
1367 tevent_req_callback_data(subreq
, struct dcerpc_ncacn_conn
);
1368 NTSTATUS status
= NT_STATUS_OK
;
1372 rc
= tstream_writev_queue_recv(subreq
, &sys_errno
);
1373 TALLOC_FREE(subreq
);
1375 DEBUG(2, ("Writev failed!\n"));
1376 status
= map_nt_error_from_unix(sys_errno
);
1380 /* clear out any data that may have been left around */
1381 ncacn_conn
->count
= 0;
1382 TALLOC_FREE(ncacn_conn
->iov
);
1383 data_blob_free(&ncacn_conn
->p
->in_data
.data
);
1384 data_blob_free(&ncacn_conn
->p
->out_data
.frag
);
1385 data_blob_free(&ncacn_conn
->p
->out_data
.rdata
);
1387 /* Wait for the next packet */
1388 subreq
= dcerpc_read_ncacn_packet_send(ncacn_conn
,
1390 ncacn_conn
->tstream
);
1391 if (subreq
== NULL
) {
1392 DEBUG(2, ("Failed to start receving packets\n"));
1393 status
= NT_STATUS_NO_MEMORY
;
1397 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1401 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1402 ncacn_conn
->client_name
, nt_errstr(status
)));
1404 /* Terminate client connection */
1405 talloc_free(ncacn_conn
);
1409 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */