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 const char *client_address
,
80 struct auth_session_info_transport
*session_info
,
81 struct pipes_struct
**_p
,
84 struct netr_SamInfo3
*info3
;
85 struct auth_user_info_dc
*auth_user_info_dc
;
86 struct pipes_struct
*p
;
90 p
= talloc_zero(mem_ctx
, struct pipes_struct
);
97 p
->mem_ctx
= talloc_named(p
, 0, "pipe %s %p", pipe_name
, p
);
104 ok
= init_pipe_handles(p
, &id
);
106 DEBUG(1, ("Failed to init handles\n"));
113 data_blob_free(&p
->in_data
.data
);
114 data_blob_free(&p
->in_data
.pdu
);
116 p
->endian
= RPC_LITTLE_ENDIAN
;
118 /* Fake up an auth_user_info_dc for now, to make an info3, to make the server_info structure */
119 auth_user_info_dc
= talloc_zero(p
, struct auth_user_info_dc
);
120 if (!auth_user_info_dc
) {
126 auth_user_info_dc
->num_sids
= session_info
->security_token
->num_sids
;
127 auth_user_info_dc
->sids
= session_info
->security_token
->sids
;
128 auth_user_info_dc
->info
= session_info
->info
;
129 auth_user_info_dc
->user_session_key
= session_info
->session_key
;
131 /* This creates the input structure that make_server_info_info3 is looking for */
132 status
= auth_convert_user_info_dc_saminfo3(p
, auth_user_info_dc
,
135 if (!NT_STATUS_IS_OK(status
)) {
136 DEBUG(1, ("Failed to convert auth_user_info_dc into netr_SamInfo3\n"));
142 status
= make_server_info_info3(p
,
143 info3
->base
.account_name
.string
,
144 info3
->base
.domain
.string
,
145 &p
->server_info
, info3
);
146 if (!NT_STATUS_IS_OK(status
)) {
147 DEBUG(1, ("Failed to init server info\n"));
154 * Some internal functions need a local token to determine access to
157 status
= create_local_token(p
->server_info
);
158 if (!NT_STATUS_IS_OK(status
)) {
159 DEBUG(1, ("Failed to init local auth token\n"));
165 /* Now override the server_info->security_token with the exact
166 * security_token we were given from the other side,
167 * regardless of what we just calculated */
168 p
->server_info
->security_token
= talloc_move(p
->server_info
, &session_info
->security_token
);
170 /* Also set the session key to the correct value */
171 p
->server_info
->user_session_key
= session_info
->session_key
;
172 p
->server_info
->user_session_key
.data
= talloc_move(p
->server_info
, &session_info
->session_key
.data
);
174 p
->client_id
= talloc_zero(p
, struct client_address
);
180 strlcpy(p
->client_id
->addr
,
181 client_address
, sizeof(p
->client_id
->addr
));
182 p
->client_id
->name
= talloc_strdup(p
->client_id
, client_address
);
183 if (p
->client_id
->name
== NULL
) {
189 talloc_set_destructor(p
, close_internal_rpc_pipe_hnd
);
195 /* Add some helper functions to wrap the common ncacn packet reading functions
196 * until we can share more dcerpc code */
197 struct dcerpc_ncacn_read_packet_state
{
198 struct ncacn_packet
*pkt
;
202 static void dcerpc_ncacn_read_packet_done(struct tevent_req
*subreq
);
204 static struct tevent_req
*dcerpc_ncacn_read_packet_send(TALLOC_CTX
*mem_ctx
,
205 struct tevent_context
*ev
,
206 struct tstream_context
*tstream
)
208 struct dcerpc_ncacn_read_packet_state
*state
;
209 struct tevent_req
*req
, *subreq
;
211 req
= tevent_req_create(mem_ctx
, &state
,
212 struct dcerpc_ncacn_read_packet_state
);
218 subreq
= dcerpc_read_ncacn_packet_send(state
, ev
, tstream
);
220 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
221 tevent_req_post(req
, ev
);
224 tevent_req_set_callback(subreq
, dcerpc_ncacn_read_packet_done
, req
);
229 static void dcerpc_ncacn_read_packet_done(struct tevent_req
*subreq
)
231 struct tevent_req
*req
=
232 tevent_req_callback_data(subreq
, struct tevent_req
);
233 struct dcerpc_ncacn_read_packet_state
*state
=
234 tevent_req_data(req
, struct dcerpc_ncacn_read_packet_state
);
237 status
= dcerpc_read_ncacn_packet_recv(subreq
, state
,
241 if (!NT_STATUS_IS_OK(status
)) {
242 DEBUG(3, ("Failed to receive dceprc packet!\n"));
243 tevent_req_nterror(req
, status
);
247 tevent_req_done(req
);
250 static NTSTATUS
dcerpc_ncacn_read_packet_recv(struct tevent_req
*req
,
254 struct dcerpc_ncacn_read_packet_state
*state
=
255 tevent_req_data(req
, struct dcerpc_ncacn_read_packet_state
);
258 if (tevent_req_is_nterror(req
, &status
)) {
259 tevent_req_received(req
);
263 buffer
->data
= talloc_move(mem_ctx
, &state
->buffer
.data
);
264 buffer
->length
= state
->buffer
.length
;
266 tevent_req_received(req
);
272 /* Start listening on the appropriate unix socket and setup all is needed to
273 * dispatch requests to the pipes rpc implementation */
275 struct dcerpc_ncacn_listen_state
{
276 struct ndr_syntax_id syntax_id
;
284 struct tevent_context
*ev_ctx
;
285 struct messaging_context
*msg_ctx
;
288 static void named_pipe_listener(struct tevent_context
*ev
,
289 struct tevent_fd
*fde
,
293 bool setup_named_pipe_socket(const char *pipe_name
,
294 struct tevent_context
*ev_ctx
)
296 struct dcerpc_ncacn_listen_state
*state
;
297 struct tevent_fd
*fde
;
300 state
= talloc(ev_ctx
, struct dcerpc_ncacn_listen_state
);
302 DEBUG(0, ("Out of memory\n"));
305 state
->ep
.name
= talloc_strdup(state
, pipe_name
);
306 if (state
->ep
.name
== NULL
) {
307 DEBUG(0, ("Out of memory\n"));
312 np_dir
= talloc_asprintf(state
, "%s/np", lp_ncalrpc_dir());
314 DEBUG(0, ("Out of memory\n"));
318 if (!directory_create_or_exist(np_dir
, geteuid(), 0700)) {
319 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
320 np_dir
, strerror(errno
)));
324 state
->fd
= create_pipe_sock(np_dir
, pipe_name
, 0700);
325 if (state
->fd
== -1) {
326 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
331 DEBUG(10, ("Openened pipe socket fd %d for %s\n",
332 state
->fd
, pipe_name
));
334 fde
= tevent_add_fd(ev_ctx
,
335 state
, state
->fd
, TEVENT_FD_READ
,
336 named_pipe_listener
, state
);
338 DEBUG(0, ("Failed to add event handler!\n"));
342 tevent_fd_set_auto_close(fde
);
346 if (state
->fd
!= -1) {
353 static void named_pipe_accept_function(const char *pipe_name
, int fd
);
355 static void named_pipe_listener(struct tevent_context
*ev
,
356 struct tevent_fd
*fde
,
360 struct dcerpc_ncacn_listen_state
*state
=
361 talloc_get_type_abort(private_data
,
362 struct dcerpc_ncacn_listen_state
);
363 struct sockaddr_un sunaddr
;
367 /* TODO: should we have a limit to the number of clients ? */
369 len
= sizeof(sunaddr
);
372 sd
= accept(state
->fd
,
373 (struct sockaddr
*)(void *)&sunaddr
, &len
);
374 if (errno
!= EINTR
) break;
378 DEBUG(6, ("Failed to get a valid socket [%s]\n",
383 DEBUG(6, ("Accepted socket %d\n", sd
));
385 named_pipe_accept_function(state
->ep
.name
, sd
);
389 /* This is the core of the rpc server.
390 * Accepts connections from clients and process requests using the appropriate
391 * dispatcher table. */
393 struct named_pipe_client
{
394 const char *pipe_name
;
395 struct ndr_syntax_id pipe_id
;
397 struct tevent_context
*ev
;
398 struct messaging_context
*msg_ctx
;
401 uint16_t device_state
;
402 uint64_t allocation_size
;
404 struct tstream_context
*tstream
;
406 struct tsocket_address
*client
;
408 struct tsocket_address
*server
;
410 struct auth_session_info_transport
*session_info
;
412 struct pipes_struct
*p
;
414 struct tevent_queue
*write_queue
;
420 static void named_pipe_accept_done(struct tevent_req
*subreq
);
422 static void named_pipe_accept_function(const char *pipe_name
, int fd
)
424 struct ndr_syntax_id syntax
;
425 struct named_pipe_client
*npc
;
426 struct tstream_context
*plain
;
427 struct tevent_req
*subreq
;
431 ok
= is_known_pipename(pipe_name
, &syntax
);
433 DEBUG(1, ("Unknown pipe [%s]\n", pipe_name
));
438 npc
= talloc_zero(NULL
, struct named_pipe_client
);
440 DEBUG(0, ("Out of memory!\n"));
444 npc
->pipe_name
= pipe_name
;
445 npc
->pipe_id
= syntax
;
446 npc
->ev
= server_event_context();
447 npc
->msg_ctx
= server_messaging_context();
449 /* make sure socket is in NON blocking state */
450 ret
= set_blocking(fd
, false);
452 DEBUG(2, ("Failed to make socket non-blocking\n"));
458 ret
= tstream_bsd_existing_socket(npc
, fd
, &plain
);
460 DEBUG(2, ("Failed to create tstream socket\n"));
466 npc
->file_type
= FILE_TYPE_MESSAGE_MODE_PIPE
;
467 npc
->device_state
= 0xff | 0x0400 | 0x0100;
468 npc
->allocation_size
= 4096;
470 subreq
= tstream_npa_accept_existing_send(npc
, npc
->ev
, plain
,
473 npc
->allocation_size
);
475 DEBUG(2, ("Failed to start async accept procedure\n"));
480 tevent_req_set_callback(subreq
, named_pipe_accept_done
, npc
);
483 static void named_pipe_packet_process(struct tevent_req
*subreq
);
484 static void named_pipe_packet_done(struct tevent_req
*subreq
);
486 static void named_pipe_accept_done(struct tevent_req
*subreq
)
488 struct named_pipe_client
*npc
=
489 tevent_req_callback_data(subreq
, struct named_pipe_client
);
490 const char *cli_addr
;
494 ret
= tstream_npa_accept_existing_recv(subreq
, &error
, npc
,
503 DEBUG(2, ("Failed to accept named pipe connection! (%s)\n",
509 if (tsocket_address_is_inet(npc
->client
, "ip")) {
510 cli_addr
= tsocket_address_inet_addr_string(npc
->client
,
512 if (cli_addr
== NULL
) {
520 ret
= make_server_pipes_struct(npc
,
521 npc
->pipe_name
, npc
->pipe_id
,
522 cli_addr
, npc
->session_info
,
525 DEBUG(2, ("Failed to create pipes_struct! (%s)\n",
529 npc
->p
->msg_ctx
= npc
->msg_ctx
;
531 npc
->write_queue
= tevent_queue_create(npc
, "np_server_write_queue");
532 if (!npc
->write_queue
) {
533 DEBUG(2, ("Failed to set up write queue!\n"));
537 /* And now start receaving and processing packets */
538 subreq
= dcerpc_ncacn_read_packet_send(npc
, npc
->ev
, npc
->tstream
);
540 DEBUG(2, ("Failed to start receving packets\n"));
543 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
547 DEBUG(2, ("Fatal error. Terminating client(%s) connection!\n",
549 /* terminate client connection */
554 static void named_pipe_packet_process(struct tevent_req
*subreq
)
556 struct named_pipe_client
*npc
=
557 tevent_req_callback_data(subreq
, struct named_pipe_client
);
558 struct _output_data
*out
= &npc
->p
->out_data
;
559 DATA_BLOB recv_buffer
= data_blob_null
;
567 status
= dcerpc_ncacn_read_packet_recv(subreq
, npc
, &recv_buffer
);
569 if (!NT_STATUS_IS_OK(status
)) {
573 data_left
= recv_buffer
.length
;
574 data
= (char *)recv_buffer
.data
;
578 data_used
= process_incoming_data(npc
->p
, data
, data_left
);
580 DEBUG(3, ("Failed to process dceprc request!\n"));
581 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
585 data_left
-= data_used
;
589 /* Do not leak this buffer, npc is a long lived context */
590 talloc_free(recv_buffer
.data
);
592 /* this is needed because of the way DCERPC Binds work in
593 * the RPC marshalling code */
594 to_send
= out
->frag
.length
- out
->current_pdu_sent
;
597 DEBUG(10, ("Current_pdu_len = %u, "
598 "current_pdu_sent = %u "
599 "Returning %u bytes\n",
600 (unsigned int)out
->frag
.length
,
601 (unsigned int)out
->current_pdu_sent
,
602 (unsigned int)to_send
));
604 npc
->iov
= talloc_zero(npc
, struct iovec
);
606 status
= NT_STATUS_NO_MEMORY
;
611 npc
->iov
[0].iov_base
= out
->frag
.data
612 + out
->current_pdu_sent
;
613 npc
->iov
[0].iov_len
= to_send
;
615 out
->current_pdu_sent
+= to_send
;
618 /* this condition is false for bind packets, or when we haven't
619 * yet got a full request, and need to wait for more data from
621 while (out
->data_sent_length
< out
->rdata
.length
) {
623 ok
= create_next_pdu(npc
->p
);
625 DEBUG(3, ("Failed to create next PDU!\n"));
626 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
630 npc
->iov
= talloc_realloc(npc
, npc
->iov
,
631 struct iovec
, npc
->count
+ 1);
633 status
= NT_STATUS_NO_MEMORY
;
637 npc
->iov
[npc
->count
].iov_base
= out
->frag
.data
;
638 npc
->iov
[npc
->count
].iov_len
= out
->frag
.length
;
640 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
641 (unsigned int)npc
->count
,
642 (unsigned int)npc
->iov
[npc
->count
].iov_len
));
643 dump_data(11, (const uint8_t *)npc
->iov
[npc
->count
].iov_base
,
644 npc
->iov
[npc
->count
].iov_len
);
648 /* we still don't have a complete request, go back and wait for more
650 if (npc
->count
== 0) {
651 /* Wait for the next packet */
652 subreq
= dcerpc_ncacn_read_packet_send(npc
, npc
->ev
, npc
->tstream
);
654 DEBUG(2, ("Failed to start receving packets\n"));
655 status
= NT_STATUS_NO_MEMORY
;
658 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
662 DEBUG(10, ("Sending a total of %u bytes\n",
663 (unsigned int)npc
->p
->out_data
.data_sent_length
));
665 subreq
= tstream_writev_queue_send(npc
, npc
->ev
,
668 npc
->iov
, npc
->count
);
670 DEBUG(2, ("Failed to send packet\n"));
671 status
= NT_STATUS_NO_MEMORY
;
674 tevent_req_set_callback(subreq
, named_pipe_packet_done
, npc
);
678 DEBUG(2, ("Fatal error(%s). "
679 "Terminating client(%s) connection!\n",
680 nt_errstr(status
), npc
->client_name
));
681 /* terminate client connection */
686 static void named_pipe_packet_done(struct tevent_req
*subreq
)
688 struct named_pipe_client
*npc
=
689 tevent_req_callback_data(subreq
, struct named_pipe_client
);
693 ret
= tstream_writev_queue_recv(subreq
, &sys_errno
);
696 DEBUG(2, ("Writev failed!\n"));
700 /* clear out any data that may have been left around */
702 TALLOC_FREE(npc
->iov
);
703 data_blob_free(&npc
->p
->in_data
.data
);
704 data_blob_free(&npc
->p
->out_data
.frag
);
705 data_blob_free(&npc
->p
->out_data
.rdata
);
707 /* Wait for the next packet */
708 subreq
= dcerpc_ncacn_read_packet_send(npc
, npc
->ev
, npc
->tstream
);
710 DEBUG(2, ("Failed to start receving packets\n"));
714 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
718 DEBUG(2, ("Fatal error(%s). "
719 "Terminating client(%s) connection!\n",
720 strerror(sys_errno
), npc
->client_name
));
721 /* terminate client connection */
726 /********************************************************************
727 * Start listening on the tcp/ip socket
728 ********************************************************************/
730 static void dcerpc_ncacn_tcpip_listener(struct tevent_context
*ev
,
731 struct tevent_fd
*fde
,
735 uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context
*ev_ctx
,
736 struct messaging_context
*msg_ctx
,
737 struct ndr_syntax_id syntax_id
,
738 const struct sockaddr_storage
*ifss
,
741 struct dcerpc_ncacn_listen_state
*state
;
742 struct tevent_fd
*fde
;
745 state
= talloc(ev_ctx
, struct dcerpc_ncacn_listen_state
);
747 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Out of memory\n"));
751 state
->syntax_id
= syntax_id
;
753 state
->ep
.port
= port
;
755 if (state
->ep
.port
== 0) {
758 for (i
= SERVER_TCP_LOW_PORT
; i
<= SERVER_TCP_HIGH_PORT
; i
++) {
759 state
->fd
= open_socket_in(SOCK_STREAM
,
770 state
->fd
= open_socket_in(SOCK_STREAM
,
776 if (state
->fd
== -1) {
777 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Failed to create "
778 "socket on port %u!\n", state
->ep
.port
));
782 state
->ev_ctx
= ev_ctx
;
783 state
->msg_ctx
= msg_ctx
;
785 /* ready to listen */
786 set_socket_options(state
->fd
, "SO_KEEPALIVE");
787 set_socket_options(state
->fd
, lp_socket_options());
789 /* Set server socket to non-blocking for the accept. */
790 set_blocking(state
->fd
, false);
792 rc
= listen(state
->fd
, SMBD_LISTEN_BACKLOG
);
794 DEBUG(0,("setup_tcpip_socket: listen - %s\n", strerror(errno
)));
798 DEBUG(10, ("setup_tcpip_socket: openened socket fd %d for port %u\n",
799 state
->fd
, state
->ep
.port
));
801 fde
= tevent_add_fd(state
->ev_ctx
,
805 dcerpc_ncacn_tcpip_listener
,
808 DEBUG(0, ("setup_tcpip_socket: Failed to add event handler!\n"));
812 tevent_fd_set_auto_close(fde
);
814 return state
->ep
.port
;
816 if (state
->fd
!= -1) {
824 static void dcerpc_ncacn_accept(struct tevent_context
*ev_ctx
,
825 struct messaging_context
*msg_ctx
,
826 struct ndr_syntax_id syntax_id
,
827 enum dcerpc_transport_t transport
,
830 struct tsocket_address
*cli_addr
,
833 static void dcerpc_ncacn_tcpip_listener(struct tevent_context
*ev
,
834 struct tevent_fd
*fde
,
838 struct dcerpc_ncacn_listen_state
*state
=
839 talloc_get_type_abort(private_data
,
840 struct dcerpc_ncacn_listen_state
);
841 struct tsocket_address
*cli_addr
;
842 struct sockaddr_storage addr
;
843 socklen_t in_addrlen
= sizeof(addr
);
848 s
= accept(state
->fd
, (struct sockaddr
*)(void *) &addr
, &in_addrlen
);
849 if (s
== -1 && errno
!= EINTR
) {
855 DEBUG(0,("tcpip_listener accept: %s\n",
860 rc
= tsocket_address_bsd_from_sockaddr(state
,
861 (struct sockaddr
*)(void *) &addr
,
869 DEBUG(6, ("tcpip_listener: Accepted socket %d\n", s
));
871 dcerpc_ncacn_accept(state
->ev_ctx
,
881 struct dcerpc_ncacn_conn
{
882 struct ndr_syntax_id syntax_id
;
884 enum dcerpc_transport_t transport
;
893 struct pipes_struct
*p
;
895 struct tevent_context
*ev_ctx
;
896 struct messaging_context
*msg_ctx
;
898 struct tstream_context
*tstream
;
899 struct tevent_queue
*send_queue
;
901 struct tsocket_address
*client
;
903 struct tsocket_address
*server
;
905 struct auth_session_info_transport
*session_info
;
911 static void dcerpc_ncacn_packet_process(struct tevent_req
*subreq
);
912 static void dcerpc_ncacn_packet_done(struct tevent_req
*subreq
);
914 static void dcerpc_ncacn_accept(struct tevent_context
*ev_ctx
,
915 struct messaging_context
*msg_ctx
,
916 struct ndr_syntax_id syntax_id
,
917 enum dcerpc_transport_t transport
,
920 struct tsocket_address
*cli_addr
,
922 struct dcerpc_ncacn_conn
*ncacn_conn
;
923 struct tevent_req
*subreq
;
930 DEBUG(5, ("dcerpc_ncacn_accept\n"));
932 ncacn_conn
= talloc_zero(ev_ctx
, struct dcerpc_ncacn_conn
);
933 if (ncacn_conn
== NULL
) {
934 DEBUG(0, ("dcerpc_ncacn_accept: Out of memory!\n"));
941 ncacn_conn
->ep
.port
= port
;
945 ncacn_conn
->ep
.name
= talloc_strdup(ncacn_conn
, name
);
948 DEBUG(0, ("dcerpc_ncacn_accept_function: "
949 "unknown transport: %u!\n", transport
));
950 talloc_free(ncacn_conn
);
954 ncacn_conn
->transport
= transport
;
955 ncacn_conn
->syntax_id
= syntax_id
;
956 ncacn_conn
->ev_ctx
= ev_ctx
;
957 ncacn_conn
->msg_ctx
= msg_ctx
;
958 ncacn_conn
->sock
= s
;
960 ncacn_conn
->client
= talloc_move(ncacn_conn
, &cli_addr
);
962 rc
= set_blocking(s
, false);
964 DEBUG(2, ("dcerpc_ncacn_accept: Failed to set socket to "
966 talloc_free(ncacn_conn
);
972 * As soon as we have tstream_bsd_existing_socket set up it will take
973 * care closing the socket.
975 rc
= tstream_bsd_existing_socket(ncacn_conn
, s
, &ncacn_conn
->tstream
);
977 DEBUG(2, ("dcerpc_ncacn_accept: Failed to create tstream "
979 talloc_free(ncacn_conn
);
984 switch(ncacn_conn
->transport
) {
986 pipe_name
= tsocket_address_string(ncacn_conn
->client
,
990 pipe_name
= talloc_strdup(ncacn_conn
,
991 ncacn_conn
->ep
.name
);
994 talloc_free(ncacn_conn
);
998 if (tsocket_address_is_inet(ncacn_conn
->client
, "ip")) {
999 cli_str
= tsocket_address_inet_addr_string(ncacn_conn
->client
,
1001 if (cli_str
== NULL
) {
1002 talloc_free(ncacn_conn
);
1009 if (ncacn_conn
->session_info
== NULL
) {
1010 status
= auth_anonymous_session_info(ncacn_conn
,
1011 &ncacn_conn
->session_info
);
1012 if (!NT_STATUS_IS_OK(status
)) {
1013 DEBUG(2, ("dcerpc_ncacn_accept: Failed to create "
1014 "auth_anonymous_session_info - %s\n",
1015 nt_errstr(status
)));
1016 talloc_free(ncacn_conn
);
1021 rc
= make_server_pipes_struct(ncacn_conn
,
1023 ncacn_conn
->syntax_id
,
1025 ncacn_conn
->session_info
,
1029 DEBUG(2, ("dcerpc_ncacn_accept: Failed to create pipe "
1030 "struct - %s", strerror(sys_errno
)));
1031 talloc_free(ncacn_conn
);
1035 ncacn_conn
->send_queue
= tevent_queue_create(ncacn_conn
,
1036 "dcerpc_tcpip_accept_function");
1037 if (ncacn_conn
->send_queue
== NULL
) {
1038 DEBUG(0, ("dcerpc_ncacn_accept_function: Out of memory!\n"));
1039 talloc_free(ncacn_conn
);
1043 subreq
= dcerpc_ncacn_read_packet_send(ncacn_conn
,
1045 ncacn_conn
->tstream
);
1046 if (subreq
== NULL
) {
1047 DEBUG(2, ("dcerpc_ncacn_accept: Failed to send ncacn "
1049 talloc_free(ncacn_conn
);
1053 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1055 DEBUG(5, ("dcerpc_ncacn_accept done\n"));
1060 static void dcerpc_ncacn_packet_process(struct tevent_req
*subreq
)
1062 struct dcerpc_ncacn_conn
*ncacn_conn
=
1063 tevent_req_callback_data(subreq
, struct dcerpc_ncacn_conn
);
1065 struct _output_data
*out
= &ncacn_conn
->p
->out_data
;
1066 DATA_BLOB recv_buffer
= data_blob_null
;
1074 status
= dcerpc_ncacn_read_packet_recv(subreq
, ncacn_conn
, &recv_buffer
);
1075 TALLOC_FREE(subreq
);
1076 if (!NT_STATUS_IS_OK(status
)) {
1080 data_left
= recv_buffer
.length
;
1081 data
= (char *) recv_buffer
.data
;
1084 data_used
= process_incoming_data(ncacn_conn
->p
, data
, data_left
);
1085 if (data_used
< 0) {
1086 DEBUG(3, ("Failed to process dcerpc request!\n"));
1087 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
1091 data_left
-= data_used
;
1095 /* Do not leak this buffer */
1096 talloc_free(recv_buffer
.data
);
1099 * This is needed because of the way DCERPC binds work in the RPC
1102 to_send
= out
->frag
.length
- out
->current_pdu_sent
;
1105 DEBUG(10, ("Current_pdu_len = %u, "
1106 "current_pdu_sent = %u "
1107 "Returning %u bytes\n",
1108 (unsigned int)out
->frag
.length
,
1109 (unsigned int)out
->current_pdu_sent
,
1110 (unsigned int)to_send
));
1112 ncacn_conn
->iov
= talloc_zero(ncacn_conn
, struct iovec
);
1113 if (ncacn_conn
->iov
== NULL
) {
1114 status
= NT_STATUS_NO_MEMORY
;
1117 ncacn_conn
->count
= 1;
1119 ncacn_conn
->iov
[0].iov_base
= out
->frag
.data
1120 + out
->current_pdu_sent
;
1121 ncacn_conn
->iov
[0].iov_len
= to_send
;
1123 out
->current_pdu_sent
+= to_send
;
1127 * This condition is false for bind packets, or when we haven't yet got
1128 * a full request, and need to wait for more data from the client
1130 while (out
->data_sent_length
< out
->rdata
.length
) {
1131 ok
= create_next_pdu(ncacn_conn
->p
);
1133 DEBUG(3, ("Failed to create next PDU!\n"));
1134 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
1138 ncacn_conn
->iov
= talloc_realloc(ncacn_conn
,
1141 ncacn_conn
->count
+ 1);
1142 if (ncacn_conn
->iov
== NULL
) {
1143 status
= NT_STATUS_NO_MEMORY
;
1147 ncacn_conn
->iov
[ncacn_conn
->count
].iov_base
= out
->frag
.data
;
1148 ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
= out
->frag
.length
;
1150 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
1151 (unsigned int) ncacn_conn
->count
,
1152 (unsigned int) ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
));
1153 dump_data(11, (const uint8_t *) ncacn_conn
->iov
[ncacn_conn
->count
].iov_base
,
1154 ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
);
1155 ncacn_conn
->count
++;
1159 * We still don't have a complete request, go back and wait for more
1162 if (ncacn_conn
->count
== 0) {
1163 /* Wait for the next packet */
1164 subreq
= dcerpc_ncacn_read_packet_send(ncacn_conn
,
1166 ncacn_conn
->tstream
);
1167 if (subreq
== NULL
) {
1168 DEBUG(2, ("Failed to start receving packets\n"));
1169 status
= NT_STATUS_NO_MEMORY
;
1172 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1176 DEBUG(10, ("Sending a total of %u bytes\n",
1177 (unsigned int)ncacn_conn
->p
->out_data
.data_sent_length
));
1179 subreq
= tstream_writev_queue_send(ncacn_conn
,
1181 ncacn_conn
->tstream
,
1182 ncacn_conn
->send_queue
,
1185 if (subreq
== NULL
) {
1186 DEBUG(2, ("Failed to send packet\n"));
1187 status
= NT_STATUS_NO_MEMORY
;
1191 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_done
, ncacn_conn
);
1195 DEBUG(2, ("Fatal error(%s). "
1196 "Terminating client(%s) connection!\n",
1197 nt_errstr(status
), ncacn_conn
->client_name
));
1199 /* Terminate client connection */
1200 talloc_free(ncacn_conn
);
1204 static void dcerpc_ncacn_packet_done(struct tevent_req
*subreq
)
1206 struct dcerpc_ncacn_conn
*ncacn_conn
=
1207 tevent_req_callback_data(subreq
, struct dcerpc_ncacn_conn
);
1211 rc
= tstream_writev_queue_recv(subreq
, &sys_errno
);
1212 TALLOC_FREE(subreq
);
1214 DEBUG(2, ("Writev failed!\n"));
1218 /* clear out any data that may have been left around */
1219 ncacn_conn
->count
= 0;
1220 TALLOC_FREE(ncacn_conn
->iov
);
1221 data_blob_free(&ncacn_conn
->p
->in_data
.data
);
1222 data_blob_free(&ncacn_conn
->p
->out_data
.frag
);
1223 data_blob_free(&ncacn_conn
->p
->out_data
.rdata
);
1225 /* Wait for the next packet */
1226 subreq
= dcerpc_ncacn_read_packet_send(ncacn_conn
,
1228 ncacn_conn
->tstream
);
1229 if (subreq
== NULL
) {
1230 DEBUG(2, ("Failed to start receving packets\n"));
1235 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1239 DEBUG(2, ("Fatal error(%s). Terminating client(%s) connection!\n",
1240 strerror(sys_errno
), ncacn_conn
->client_name
));
1242 /* Terminate client connection */
1243 talloc_free(ncacn_conn
);
1247 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */