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_transport
**session_info
)
40 struct auth_session_info_transport
*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_transport
);
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
->user_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_transport
*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
;
97 p
= talloc_zero(mem_ctx
, struct pipes_struct
);
103 p
->transport
= transport
;
104 p
->ncalrpc_as_system
= ncalrpc_as_system
;
106 p
->mem_ctx
= talloc_named(p
, 0, "pipe %s %p", pipe_name
, p
);
113 ok
= init_pipe_handles(p
, &id
);
115 DEBUG(1, ("Failed to init handles\n"));
122 data_blob_free(&p
->in_data
.data
);
123 data_blob_free(&p
->in_data
.pdu
);
125 p
->endian
= RPC_LITTLE_ENDIAN
;
127 /* Fake up an auth_user_info_dc for now, to make an info3, to make the session_info structure */
128 auth_user_info_dc
= talloc_zero(p
, struct auth_user_info_dc
);
129 if (!auth_user_info_dc
) {
135 auth_user_info_dc
->num_sids
= session_info
->security_token
->num_sids
;
136 auth_user_info_dc
->sids
= session_info
->security_token
->sids
;
137 auth_user_info_dc
->info
= session_info
->info
;
138 auth_user_info_dc
->user_session_key
= session_info
->session_key
;
140 /* This creates the input structure that make_server_info_info3 is looking for */
141 status
= auth_convert_user_info_dc_saminfo3(p
, auth_user_info_dc
,
144 if (!NT_STATUS_IS_OK(status
)) {
145 DEBUG(1, ("Failed to convert auth_user_info_dc into netr_SamInfo3\n"));
151 status
= make_server_info_info3(p
,
152 info3
->base
.account_name
.string
,
153 info3
->base
.domain
.string
,
154 &p
->session_info
, info3
);
155 if (!NT_STATUS_IS_OK(status
)) {
156 DEBUG(1, ("Failed to init server info\n"));
163 * Some internal functions need a local token to determine access to
166 status
= create_local_token(p
->session_info
);
167 if (!NT_STATUS_IS_OK(status
)) {
168 DEBUG(1, ("Failed to init local auth token\n"));
174 /* Now override the session_info->security_token with the exact
175 * security_token we were given from the other side,
176 * regardless of what we just calculated */
177 p
->session_info
->security_token
= talloc_move(p
->session_info
, &session_info
->security_token
);
179 /* Also set the session key to the correct value */
180 p
->session_info
->user_session_key
= session_info
->session_key
;
181 p
->session_info
->user_session_key
.data
= talloc_move(p
->session_info
, &session_info
->session_key
.data
);
183 p
->client_id
= talloc_zero(p
, struct client_address
);
189 strlcpy(p
->client_id
->addr
,
190 client_address
, sizeof(p
->client_id
->addr
));
191 p
->client_id
->name
= talloc_strdup(p
->client_id
, client_address
);
192 if (p
->client_id
->name
== NULL
) {
198 if (server_address
!= NULL
) {
199 p
->server_id
= talloc_zero(p
, struct client_address
);
200 if (p
->client_id
== NULL
) {
206 strlcpy(p
->server_id
->addr
,
208 sizeof(p
->server_id
->addr
));
210 p
->server_id
->name
= talloc_strdup(p
->server_id
,
212 if (p
->server_id
->name
== NULL
) {
219 talloc_set_destructor(p
, close_internal_rpc_pipe_hnd
);
225 /* Start listening on the appropriate unix socket and setup all is needed to
226 * dispatch requests to the pipes rpc implementation */
228 struct dcerpc_ncacn_listen_state
{
229 struct ndr_syntax_id syntax_id
;
237 struct tevent_context
*ev_ctx
;
238 struct messaging_context
*msg_ctx
;
239 dcerpc_ncacn_disconnect_fn disconnect_fn
;
242 static void named_pipe_listener(struct tevent_context
*ev
,
243 struct tevent_fd
*fde
,
247 bool setup_named_pipe_socket(const char *pipe_name
,
248 struct tevent_context
*ev_ctx
)
250 struct dcerpc_ncacn_listen_state
*state
;
251 struct tevent_fd
*fde
;
254 state
= talloc(ev_ctx
, struct dcerpc_ncacn_listen_state
);
256 DEBUG(0, ("Out of memory\n"));
259 state
->ep
.name
= talloc_strdup(state
, pipe_name
);
260 if (state
->ep
.name
== NULL
) {
261 DEBUG(0, ("Out of memory\n"));
267 * As lp_ncalrpc_dir() should have 0755, but
268 * lp_ncalrpc_dir()/np should have 0700, we need to
269 * create lp_ncalrpc_dir() first.
271 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) {
272 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
273 lp_ncalrpc_dir(), strerror(errno
)));
277 np_dir
= talloc_asprintf(state
, "%s/np", lp_ncalrpc_dir());
279 DEBUG(0, ("Out of memory\n"));
283 if (!directory_create_or_exist(np_dir
, geteuid(), 0700)) {
284 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
285 np_dir
, strerror(errno
)));
289 state
->fd
= create_pipe_sock(np_dir
, pipe_name
, 0700);
290 if (state
->fd
== -1) {
291 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
296 DEBUG(10, ("Openened pipe socket fd %d for %s\n",
297 state
->fd
, pipe_name
));
299 fde
= tevent_add_fd(ev_ctx
,
300 state
, state
->fd
, TEVENT_FD_READ
,
301 named_pipe_listener
, state
);
303 DEBUG(0, ("Failed to add event handler!\n"));
307 tevent_fd_set_auto_close(fde
);
311 if (state
->fd
!= -1) {
318 static void named_pipe_accept_function(const char *pipe_name
, int fd
);
320 static void named_pipe_listener(struct tevent_context
*ev
,
321 struct tevent_fd
*fde
,
325 struct dcerpc_ncacn_listen_state
*state
=
326 talloc_get_type_abort(private_data
,
327 struct dcerpc_ncacn_listen_state
);
328 struct sockaddr_un sunaddr
;
332 /* TODO: should we have a limit to the number of clients ? */
334 len
= sizeof(sunaddr
);
336 sd
= accept(state
->fd
,
337 (struct sockaddr
*)(void *)&sunaddr
, &len
);
340 if (errno
!= EINTR
) {
341 DEBUG(6, ("Failed to get a valid socket [%s]\n",
347 DEBUG(6, ("Accepted socket %d\n", sd
));
349 named_pipe_accept_function(state
->ep
.name
, sd
);
353 /* This is the core of the rpc server.
354 * Accepts connections from clients and process requests using the appropriate
355 * dispatcher table. */
357 struct named_pipe_client
{
358 const char *pipe_name
;
359 struct ndr_syntax_id pipe_id
;
361 struct tevent_context
*ev
;
362 struct messaging_context
*msg_ctx
;
365 uint16_t device_state
;
366 uint64_t allocation_size
;
368 struct tstream_context
*tstream
;
370 struct tsocket_address
*client
;
372 struct tsocket_address
*server
;
374 struct auth_session_info_transport
*session_info
;
376 struct pipes_struct
*p
;
378 struct tevent_queue
*write_queue
;
384 static void named_pipe_accept_done(struct tevent_req
*subreq
);
386 static void named_pipe_accept_function(const char *pipe_name
, int fd
)
388 struct ndr_syntax_id syntax
;
389 struct named_pipe_client
*npc
;
390 struct tstream_context
*plain
;
391 struct tevent_req
*subreq
;
395 ok
= is_known_pipename(pipe_name
, &syntax
);
397 DEBUG(1, ("Unknown pipe [%s]\n", pipe_name
));
402 npc
= talloc_zero(NULL
, struct named_pipe_client
);
404 DEBUG(0, ("Out of memory!\n"));
408 npc
->pipe_name
= pipe_name
;
409 npc
->pipe_id
= syntax
;
410 npc
->ev
= server_event_context();
411 npc
->msg_ctx
= server_messaging_context();
413 /* make sure socket is in NON blocking state */
414 ret
= set_blocking(fd
, false);
416 DEBUG(2, ("Failed to make socket non-blocking\n"));
422 ret
= tstream_bsd_existing_socket(npc
, fd
, &plain
);
424 DEBUG(2, ("Failed to create tstream socket\n"));
430 npc
->file_type
= FILE_TYPE_MESSAGE_MODE_PIPE
;
431 npc
->device_state
= 0xff | 0x0400 | 0x0100;
432 npc
->allocation_size
= 4096;
434 subreq
= tstream_npa_accept_existing_send(npc
, npc
->ev
, plain
,
437 npc
->allocation_size
);
439 DEBUG(2, ("Failed to start async accept procedure\n"));
444 tevent_req_set_callback(subreq
, named_pipe_accept_done
, npc
);
447 static void named_pipe_packet_process(struct tevent_req
*subreq
);
448 static void named_pipe_packet_done(struct tevent_req
*subreq
);
450 static void named_pipe_accept_done(struct tevent_req
*subreq
)
452 struct named_pipe_client
*npc
=
453 tevent_req_callback_data(subreq
, struct named_pipe_client
);
454 const char *cli_addr
;
458 ret
= tstream_npa_accept_existing_recv(subreq
, &error
, npc
,
467 DEBUG(2, ("Failed to accept named pipe connection! (%s)\n",
473 if (tsocket_address_is_inet(npc
->client
, "ip")) {
474 cli_addr
= tsocket_address_inet_addr_string(npc
->client
,
476 if (cli_addr
== NULL
) {
484 ret
= make_server_pipes_struct(npc
,
485 npc
->pipe_name
, npc
->pipe_id
, NCACN_NP
,
486 false, cli_addr
, NULL
, npc
->session_info
,
489 DEBUG(2, ("Failed to create pipes_struct! (%s)\n",
493 npc
->p
->msg_ctx
= npc
->msg_ctx
;
495 npc
->write_queue
= tevent_queue_create(npc
, "np_server_write_queue");
496 if (!npc
->write_queue
) {
497 DEBUG(2, ("Failed to set up write queue!\n"));
501 /* And now start receaving and processing packets */
502 subreq
= dcerpc_read_ncacn_packet_send(npc
, npc
->ev
, npc
->tstream
);
504 DEBUG(2, ("Failed to start receving packets\n"));
507 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
511 DEBUG(2, ("Fatal error. Terminating client(%s) connection!\n",
513 /* terminate client connection */
518 static void named_pipe_packet_process(struct tevent_req
*subreq
)
520 struct named_pipe_client
*npc
=
521 tevent_req_callback_data(subreq
, struct named_pipe_client
);
522 struct _output_data
*out
= &npc
->p
->out_data
;
523 DATA_BLOB recv_buffer
= data_blob_null
;
524 struct ncacn_packet
*pkt
;
532 status
= dcerpc_read_ncacn_packet_recv(subreq
, npc
, &pkt
, &recv_buffer
);
534 if (!NT_STATUS_IS_OK(status
)) {
538 data_left
= recv_buffer
.length
;
539 data
= (char *)recv_buffer
.data
;
543 data_used
= process_incoming_data(npc
->p
, data
, data_left
);
545 DEBUG(3, ("Failed to process dceprc request!\n"));
546 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
550 data_left
-= data_used
;
554 /* Do not leak this buffer, npc is a long lived context */
555 talloc_free(recv_buffer
.data
);
558 /* this is needed because of the way DCERPC Binds work in
559 * the RPC marshalling code */
560 to_send
= out
->frag
.length
- out
->current_pdu_sent
;
563 DEBUG(10, ("Current_pdu_len = %u, "
564 "current_pdu_sent = %u "
565 "Returning %u bytes\n",
566 (unsigned int)out
->frag
.length
,
567 (unsigned int)out
->current_pdu_sent
,
568 (unsigned int)to_send
));
570 npc
->iov
= talloc_zero(npc
, struct iovec
);
572 status
= NT_STATUS_NO_MEMORY
;
577 npc
->iov
[0].iov_base
= out
->frag
.data
578 + out
->current_pdu_sent
;
579 npc
->iov
[0].iov_len
= to_send
;
581 out
->current_pdu_sent
+= to_send
;
584 /* this condition is false for bind packets, or when we haven't
585 * yet got a full request, and need to wait for more data from
587 while (out
->data_sent_length
< out
->rdata
.length
) {
589 ok
= create_next_pdu(npc
->p
);
591 DEBUG(3, ("Failed to create next PDU!\n"));
592 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
596 npc
->iov
= talloc_realloc(npc
, npc
->iov
,
597 struct iovec
, npc
->count
+ 1);
599 status
= NT_STATUS_NO_MEMORY
;
603 npc
->iov
[npc
->count
].iov_base
= out
->frag
.data
;
604 npc
->iov
[npc
->count
].iov_len
= out
->frag
.length
;
606 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
607 (unsigned int)npc
->count
,
608 (unsigned int)npc
->iov
[npc
->count
].iov_len
));
609 dump_data(11, (const uint8_t *)npc
->iov
[npc
->count
].iov_base
,
610 npc
->iov
[npc
->count
].iov_len
);
614 /* we still don't have a complete request, go back and wait for more
616 if (npc
->count
== 0) {
617 /* Wait for the next packet */
618 subreq
= dcerpc_read_ncacn_packet_send(npc
, npc
->ev
, npc
->tstream
);
620 DEBUG(2, ("Failed to start receving packets\n"));
621 status
= NT_STATUS_NO_MEMORY
;
624 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
628 DEBUG(10, ("Sending a total of %u bytes\n",
629 (unsigned int)npc
->p
->out_data
.data_sent_length
));
631 subreq
= tstream_writev_queue_send(npc
, npc
->ev
,
634 npc
->iov
, npc
->count
);
636 DEBUG(2, ("Failed to send packet\n"));
637 status
= NT_STATUS_NO_MEMORY
;
640 tevent_req_set_callback(subreq
, named_pipe_packet_done
, npc
);
644 DEBUG(2, ("Fatal error(%s). "
645 "Terminating client(%s) connection!\n",
646 nt_errstr(status
), npc
->client_name
));
647 /* terminate client connection */
652 static void named_pipe_packet_done(struct tevent_req
*subreq
)
654 struct named_pipe_client
*npc
=
655 tevent_req_callback_data(subreq
, struct named_pipe_client
);
659 ret
= tstream_writev_queue_recv(subreq
, &sys_errno
);
662 DEBUG(2, ("Writev failed!\n"));
666 /* clear out any data that may have been left around */
668 TALLOC_FREE(npc
->iov
);
669 data_blob_free(&npc
->p
->in_data
.data
);
670 data_blob_free(&npc
->p
->out_data
.frag
);
671 data_blob_free(&npc
->p
->out_data
.rdata
);
673 /* Wait for the next packet */
674 subreq
= dcerpc_read_ncacn_packet_send(npc
, npc
->ev
, npc
->tstream
);
676 DEBUG(2, ("Failed to start receving packets\n"));
680 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
684 DEBUG(2, ("Fatal error(%s). "
685 "Terminating client(%s) connection!\n",
686 strerror(sys_errno
), npc
->client_name
));
687 /* terminate client connection */
692 static void dcerpc_ncacn_accept(struct tevent_context
*ev_ctx
,
693 struct messaging_context
*msg_ctx
,
694 struct ndr_syntax_id syntax_id
,
695 enum dcerpc_transport_t transport
,
698 struct tsocket_address
*cli_addr
,
699 struct tsocket_address
*srv_addr
,
701 dcerpc_ncacn_disconnect_fn fn
);
703 /********************************************************************
704 * Start listening on the tcp/ip socket
705 ********************************************************************/
707 static void dcerpc_ncacn_tcpip_listener(struct tevent_context
*ev
,
708 struct tevent_fd
*fde
,
712 uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context
*ev_ctx
,
713 struct messaging_context
*msg_ctx
,
714 struct ndr_syntax_id syntax_id
,
715 const struct sockaddr_storage
*ifss
,
718 struct dcerpc_ncacn_listen_state
*state
;
719 struct tevent_fd
*fde
;
722 state
= talloc(ev_ctx
, struct dcerpc_ncacn_listen_state
);
724 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Out of memory\n"));
728 state
->syntax_id
= syntax_id
;
730 state
->ep
.port
= port
;
731 state
->disconnect_fn
= NULL
;
733 if (state
->ep
.port
== 0) {
736 for (i
= SERVER_TCP_LOW_PORT
; i
<= SERVER_TCP_HIGH_PORT
; i
++) {
737 state
->fd
= open_socket_in(SOCK_STREAM
,
748 state
->fd
= open_socket_in(SOCK_STREAM
,
754 if (state
->fd
== -1) {
755 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Failed to create "
756 "socket on port %u!\n", state
->ep
.port
));
760 state
->ev_ctx
= ev_ctx
;
761 state
->msg_ctx
= msg_ctx
;
763 /* ready to listen */
764 set_socket_options(state
->fd
, "SO_KEEPALIVE");
765 set_socket_options(state
->fd
, lp_socket_options());
767 /* Set server socket to non-blocking for the accept. */
768 set_blocking(state
->fd
, false);
770 rc
= listen(state
->fd
, SMBD_LISTEN_BACKLOG
);
772 DEBUG(0,("setup_tcpip_socket: listen - %s\n", strerror(errno
)));
776 DEBUG(10, ("setup_tcpip_socket: openened socket fd %d for port %u\n",
777 state
->fd
, state
->ep
.port
));
779 fde
= tevent_add_fd(state
->ev_ctx
,
783 dcerpc_ncacn_tcpip_listener
,
786 DEBUG(0, ("setup_tcpip_socket: Failed to add event handler!\n"));
790 tevent_fd_set_auto_close(fde
);
792 return state
->ep
.port
;
794 if (state
->fd
!= -1) {
802 static void dcerpc_ncacn_tcpip_listener(struct tevent_context
*ev
,
803 struct tevent_fd
*fde
,
807 struct dcerpc_ncacn_listen_state
*state
=
808 talloc_get_type_abort(private_data
,
809 struct dcerpc_ncacn_listen_state
);
810 struct tsocket_address
*cli_addr
= NULL
;
811 struct tsocket_address
*srv_addr
= NULL
;
812 struct sockaddr_storage addr
;
813 socklen_t in_addrlen
= sizeof(addr
);
817 s
= accept(state
->fd
, (struct sockaddr
*)(void *) &addr
, &in_addrlen
);
819 if (errno
!= EINTR
) {
820 DEBUG(0,("tcpip_listener accept: %s\n",
826 rc
= tsocket_address_bsd_from_sockaddr(state
,
827 (struct sockaddr
*)(void *) &addr
,
835 rc
= getsockname(s
, (struct sockaddr
*)(void *) &addr
, &in_addrlen
);
841 rc
= tsocket_address_bsd_from_sockaddr(state
,
842 (struct sockaddr
*)(void *) &addr
,
850 DEBUG(6, ("tcpip_listener: Accepted socket %d\n", s
));
852 dcerpc_ncacn_accept(state
->ev_ctx
,
864 /********************************************************************
865 * Start listening on the ncalrpc socket
866 ********************************************************************/
868 static void dcerpc_ncalrpc_listener(struct tevent_context
*ev
,
869 struct tevent_fd
*fde
,
873 bool setup_dcerpc_ncalrpc_socket(struct tevent_context
*ev_ctx
,
874 struct messaging_context
*msg_ctx
,
875 struct ndr_syntax_id syntax_id
,
877 dcerpc_ncacn_disconnect_fn fn
)
879 struct dcerpc_ncacn_listen_state
*state
;
880 struct tevent_fd
*fde
;
882 state
= talloc(ev_ctx
, struct dcerpc_ncacn_listen_state
);
884 DEBUG(0, ("Out of memory\n"));
888 state
->syntax_id
= syntax_id
;
890 state
->disconnect_fn
= fn
;
895 state
->ep
.name
= talloc_strdup(state
, name
);
897 if (state
->ep
.name
== NULL
) {
898 DEBUG(0, ("Out of memory\n"));
903 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) {
904 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
905 lp_ncalrpc_dir(), strerror(errno
)));
909 state
->fd
= create_pipe_sock(lp_ncalrpc_dir(), name
, 0755);
910 if (state
->fd
== -1) {
911 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
912 lp_ncalrpc_dir(), name
));
916 DEBUG(10, ("Openened pipe socket fd %d for %s\n", state
->fd
, name
));
918 state
->ev_ctx
= ev_ctx
;
919 state
->msg_ctx
= msg_ctx
;
921 /* Set server socket to non-blocking for the accept. */
922 set_blocking(state
->fd
, false);
924 fde
= tevent_add_fd(state
->ev_ctx
,
928 dcerpc_ncalrpc_listener
,
931 DEBUG(0, ("Failed to add event handler for ncalrpc!\n"));
935 tevent_fd_set_auto_close(fde
);
939 if (state
->fd
!= -1) {
947 static void dcerpc_ncalrpc_listener(struct tevent_context
*ev
,
948 struct tevent_fd
*fde
,
952 struct dcerpc_ncacn_listen_state
*state
=
953 talloc_get_type_abort(private_data
,
954 struct dcerpc_ncacn_listen_state
);
955 struct tsocket_address
*cli_addr
= NULL
;
956 struct sockaddr_un sunaddr
;
957 struct sockaddr
*addr
= (struct sockaddr
*)(void *)&sunaddr
;
958 socklen_t len
= sizeof(sunaddr
);
962 ZERO_STRUCT(sunaddr
);
964 sd
= accept(state
->fd
, addr
, &len
);
966 if (errno
!= EINTR
) {
967 DEBUG(0, ("ncalrpc accept() failed: %s\n", strerror(errno
)));
972 rc
= tsocket_address_bsd_from_sockaddr(state
,
980 DEBUG(10, ("Accepted ncalrpc socket %d\n", sd
));
982 dcerpc_ncacn_accept(state
->ev_ctx
,
984 state
->syntax_id
, NCALRPC
,
987 state
->disconnect_fn
);
990 struct dcerpc_ncacn_conn
{
991 struct ndr_syntax_id syntax_id
;
993 enum dcerpc_transport_t transport
;
1002 struct pipes_struct
*p
;
1003 dcerpc_ncacn_disconnect_fn disconnect_fn
;
1005 struct tevent_context
*ev_ctx
;
1006 struct messaging_context
*msg_ctx
;
1008 struct tstream_context
*tstream
;
1009 struct tevent_queue
*send_queue
;
1011 struct tsocket_address
*client
;
1013 struct tsocket_address
*server
;
1015 struct auth_session_info_transport
*session_info
;
1021 static void dcerpc_ncacn_packet_process(struct tevent_req
*subreq
);
1022 static void dcerpc_ncacn_packet_done(struct tevent_req
*subreq
);
1024 static void dcerpc_ncacn_accept(struct tevent_context
*ev_ctx
,
1025 struct messaging_context
*msg_ctx
,
1026 struct ndr_syntax_id syntax_id
,
1027 enum dcerpc_transport_t transport
,
1030 struct tsocket_address
*cli_addr
,
1031 struct tsocket_address
*srv_addr
,
1033 dcerpc_ncacn_disconnect_fn fn
) {
1034 struct dcerpc_ncacn_conn
*ncacn_conn
;
1035 struct tevent_req
*subreq
;
1036 const char *cli_str
;
1037 const char *srv_str
= NULL
;
1038 bool system_user
= false;
1045 DEBUG(10, ("dcerpc_ncacn_accept\n"));
1047 ncacn_conn
= talloc_zero(ev_ctx
, struct dcerpc_ncacn_conn
);
1048 if (ncacn_conn
== NULL
) {
1049 DEBUG(0, ("Out of memory!\n"));
1054 ncacn_conn
->transport
= transport
;
1055 ncacn_conn
->syntax_id
= syntax_id
;
1056 ncacn_conn
->ev_ctx
= ev_ctx
;
1057 ncacn_conn
->msg_ctx
= msg_ctx
;
1058 ncacn_conn
->sock
= s
;
1059 ncacn_conn
->disconnect_fn
= fn
;
1061 ncacn_conn
->client
= talloc_move(ncacn_conn
, &cli_addr
);
1062 if (tsocket_address_is_inet(ncacn_conn
->client
, "ip")) {
1063 ncacn_conn
->client_name
=
1064 tsocket_address_inet_addr_string(ncacn_conn
->client
,
1067 ncacn_conn
->client_name
=
1068 tsocket_address_unix_path(ncacn_conn
->client
,
1071 if (ncacn_conn
->client_name
== NULL
) {
1072 DEBUG(0, ("Out of memory!\n"));
1073 talloc_free(ncacn_conn
);
1078 if (srv_addr
!= NULL
) {
1079 ncacn_conn
->server
= talloc_move(ncacn_conn
, &srv_addr
);
1081 ncacn_conn
->server_name
=
1082 tsocket_address_inet_addr_string(ncacn_conn
->server
,
1084 if (ncacn_conn
->server_name
== NULL
) {
1085 DEBUG(0, ("Out of memory!\n"));
1086 talloc_free(ncacn_conn
);
1092 switch (transport
) {
1094 ncacn_conn
->ep
.port
= port
;
1096 pipe_name
= tsocket_address_string(ncacn_conn
->client
,
1098 if (pipe_name
== NULL
) {
1100 talloc_free(ncacn_conn
);
1106 rc
= sys_getpeereid(s
, &uid
);
1108 DEBUG(2, ("Failed to get ncalrpc connecting uid!"));
1110 if (uid
== sec_initial_uid()) {
1115 ncacn_conn
->ep
.name
= talloc_strdup(ncacn_conn
, name
);
1116 if (ncacn_conn
->ep
.name
== NULL
) {
1118 talloc_free(ncacn_conn
);
1122 pipe_name
= talloc_strdup(ncacn_conn
,
1124 if (pipe_name
== NULL
) {
1126 talloc_free(ncacn_conn
);
1131 DEBUG(0, ("unknown dcerpc transport: %u!\n",
1133 talloc_free(ncacn_conn
);
1138 rc
= set_blocking(s
, false);
1140 DEBUG(2, ("Failed to set dcerpc socket to non-blocking\n"));
1141 talloc_free(ncacn_conn
);
1147 * As soon as we have tstream_bsd_existing_socket set up it will
1148 * take care of closing the socket.
1150 rc
= tstream_bsd_existing_socket(ncacn_conn
, s
, &ncacn_conn
->tstream
);
1152 DEBUG(2, ("Failed to create tstream socket for dcerpc\n"));
1153 talloc_free(ncacn_conn
);
1158 if (tsocket_address_is_inet(ncacn_conn
->client
, "ip")) {
1159 cli_str
= ncacn_conn
->client_name
;
1164 if (ncacn_conn
->server
!= NULL
) {
1165 if (tsocket_address_is_inet(ncacn_conn
->server
, "ip")) {
1166 srv_str
= ncacn_conn
->server_name
;
1172 if (ncacn_conn
->session_info
== NULL
) {
1173 status
= auth_anonymous_session_info(ncacn_conn
,
1174 &ncacn_conn
->session_info
);
1175 if (!NT_STATUS_IS_OK(status
)) {
1176 DEBUG(2, ("Failed to create "
1177 "auth_anonymous_session_info - %s\n",
1178 nt_errstr(status
)));
1179 talloc_free(ncacn_conn
);
1184 rc
= make_server_pipes_struct(ncacn_conn
,
1186 ncacn_conn
->syntax_id
,
1187 ncacn_conn
->transport
,
1191 ncacn_conn
->session_info
,
1195 DEBUG(2, ("Failed to create pipe struct - %s",
1196 strerror(sys_errno
)));
1197 talloc_free(ncacn_conn
);
1201 ncacn_conn
->send_queue
= tevent_queue_create(ncacn_conn
,
1202 "dcerpc send queue");
1203 if (ncacn_conn
->send_queue
== NULL
) {
1204 DEBUG(0, ("Out of memory!\n"));
1205 talloc_free(ncacn_conn
);
1209 subreq
= dcerpc_read_ncacn_packet_send(ncacn_conn
,
1211 ncacn_conn
->tstream
);
1212 if (subreq
== NULL
) {
1213 DEBUG(2, ("Failed to send ncacn packet\n"));
1214 talloc_free(ncacn_conn
);
1218 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1220 DEBUG(10, ("dcerpc_ncacn_accept done\n"));
1225 static void dcerpc_ncacn_packet_process(struct tevent_req
*subreq
)
1227 struct dcerpc_ncacn_conn
*ncacn_conn
=
1228 tevent_req_callback_data(subreq
, struct dcerpc_ncacn_conn
);
1230 struct _output_data
*out
= &ncacn_conn
->p
->out_data
;
1231 DATA_BLOB recv_buffer
= data_blob_null
;
1232 struct ncacn_packet
*pkt
;
1240 status
= dcerpc_read_ncacn_packet_recv(subreq
, ncacn_conn
, &pkt
, &recv_buffer
);
1241 TALLOC_FREE(subreq
);
1242 if (!NT_STATUS_IS_OK(status
)) {
1243 if (ncacn_conn
->disconnect_fn
!= NULL
) {
1244 ok
= ncacn_conn
->disconnect_fn(ncacn_conn
->p
);
1246 DEBUG(3, ("Failed to call disconnect function\n"));
1252 data_left
= recv_buffer
.length
;
1253 data
= (char *) recv_buffer
.data
;
1256 data_used
= process_incoming_data(ncacn_conn
->p
, data
, data_left
);
1257 if (data_used
< 0) {
1258 DEBUG(3, ("Failed to process dcerpc request!\n"));
1259 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
1263 data_left
-= data_used
;
1267 /* Do not leak this buffer */
1268 talloc_free(recv_buffer
.data
);
1272 * This is needed because of the way DCERPC binds work in the RPC
1275 to_send
= out
->frag
.length
- out
->current_pdu_sent
;
1278 DEBUG(10, ("Current_pdu_len = %u, "
1279 "current_pdu_sent = %u "
1280 "Returning %u bytes\n",
1281 (unsigned int)out
->frag
.length
,
1282 (unsigned int)out
->current_pdu_sent
,
1283 (unsigned int)to_send
));
1285 ncacn_conn
->iov
= talloc_zero(ncacn_conn
, struct iovec
);
1286 if (ncacn_conn
->iov
== NULL
) {
1287 status
= NT_STATUS_NO_MEMORY
;
1288 DEBUG(3, ("Out of memory!\n"));
1291 ncacn_conn
->count
= 1;
1293 ncacn_conn
->iov
[0].iov_base
= out
->frag
.data
1294 + out
->current_pdu_sent
;
1295 ncacn_conn
->iov
[0].iov_len
= to_send
;
1297 out
->current_pdu_sent
+= to_send
;
1301 * This condition is false for bind packets, or when we haven't yet got
1302 * a full request, and need to wait for more data from the client
1304 while (out
->data_sent_length
< out
->rdata
.length
) {
1305 ok
= create_next_pdu(ncacn_conn
->p
);
1307 DEBUG(3, ("Failed to create next PDU!\n"));
1308 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
1312 ncacn_conn
->iov
= talloc_realloc(ncacn_conn
,
1315 ncacn_conn
->count
+ 1);
1316 if (ncacn_conn
->iov
== NULL
) {
1317 DEBUG(3, ("Out of memory!\n"));
1318 status
= NT_STATUS_NO_MEMORY
;
1322 ncacn_conn
->iov
[ncacn_conn
->count
].iov_base
= out
->frag
.data
;
1323 ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
= out
->frag
.length
;
1325 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
1326 (unsigned int) ncacn_conn
->count
,
1327 (unsigned int) ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
));
1328 dump_data(11, (const uint8_t *) ncacn_conn
->iov
[ncacn_conn
->count
].iov_base
,
1329 ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
);
1330 ncacn_conn
->count
++;
1334 * We still don't have a complete request, go back and wait for more
1337 if (ncacn_conn
->count
== 0) {
1338 /* Wait for the next packet */
1339 subreq
= dcerpc_read_ncacn_packet_send(ncacn_conn
,
1341 ncacn_conn
->tstream
);
1342 if (subreq
== NULL
) {
1343 DEBUG(2, ("Failed to start receving packets\n"));
1344 status
= NT_STATUS_NO_MEMORY
;
1347 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1351 DEBUG(10, ("Sending a total of %u bytes\n",
1352 (unsigned int)ncacn_conn
->p
->out_data
.data_sent_length
));
1354 subreq
= tstream_writev_queue_send(ncacn_conn
,
1356 ncacn_conn
->tstream
,
1357 ncacn_conn
->send_queue
,
1360 if (subreq
== NULL
) {
1361 DEBUG(2, ("Failed to send packet\n"));
1362 status
= NT_STATUS_NO_MEMORY
;
1366 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_done
, ncacn_conn
);
1370 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1371 ncacn_conn
->client_name
, nt_errstr(status
)));
1373 /* Terminate client connection */
1374 talloc_free(ncacn_conn
);
1378 static void dcerpc_ncacn_packet_done(struct tevent_req
*subreq
)
1380 struct dcerpc_ncacn_conn
*ncacn_conn
=
1381 tevent_req_callback_data(subreq
, struct dcerpc_ncacn_conn
);
1382 NTSTATUS status
= NT_STATUS_OK
;
1386 rc
= tstream_writev_queue_recv(subreq
, &sys_errno
);
1387 TALLOC_FREE(subreq
);
1389 DEBUG(2, ("Writev failed!\n"));
1390 status
= map_nt_error_from_unix(sys_errno
);
1394 /* clear out any data that may have been left around */
1395 ncacn_conn
->count
= 0;
1396 TALLOC_FREE(ncacn_conn
->iov
);
1397 data_blob_free(&ncacn_conn
->p
->in_data
.data
);
1398 data_blob_free(&ncacn_conn
->p
->out_data
.frag
);
1399 data_blob_free(&ncacn_conn
->p
->out_data
.rdata
);
1401 /* Wait for the next packet */
1402 subreq
= dcerpc_read_ncacn_packet_send(ncacn_conn
,
1404 ncacn_conn
->tstream
);
1405 if (subreq
== NULL
) {
1406 DEBUG(2, ("Failed to start receving packets\n"));
1407 status
= NT_STATUS_NO_MEMORY
;
1411 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1415 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1416 ncacn_conn
->client_name
, nt_errstr(status
)));
1418 /* Terminate client connection */
1419 talloc_free(ncacn_conn
);
1423 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */