2 Unix SMB/Netbios implementation.
3 Generic infrstructure for RPC Daemons
4 Copyright (C) Simo Sorce 2010
5 Copyright (C) Andrew Bartlett 2011
6 Copyright (C) Andreas Schneider 2011
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "rpc_server/rpc_pipes.h"
24 #include "rpc_server/rpc_server.h"
25 #include "rpc_server/rpc_config.h"
27 #include "librpc/gen_ndr/netlogon.h"
28 #include "librpc/gen_ndr/auth.h"
29 #include "lib/tsocket/tsocket.h"
30 #include "libcli/named_pipe_auth/npa_tstream.h"
31 #include "../auth/auth_sam_reply.h"
33 #include "rpc_server/rpc_ncacn_np.h"
34 #include "rpc_server/srv_pipe_hnd.h"
35 #include "rpc_server/srv_pipe.h"
37 #define SERVER_TCP_LOW_PORT 1024
38 #define SERVER_TCP_HIGH_PORT 1300
40 static NTSTATUS
auth_anonymous_session_info(TALLOC_CTX
*mem_ctx
,
41 struct auth_session_info
**session_info
)
45 status
= make_session_info_guest(mem_ctx
, session_info
);
46 if (!NT_STATUS_IS_OK(status
)) {
53 /* Creates a pipes_struct and initializes it with the information
54 * sent from the client */
55 static int make_server_pipes_struct(TALLOC_CTX
*mem_ctx
,
56 struct messaging_context
*msg_ctx
,
57 const char *pipe_name
,
58 enum dcerpc_transport_t transport
,
59 bool ncalrpc_as_system
,
60 const struct tsocket_address
*local_address
,
61 const struct tsocket_address
*remote_address
,
62 struct auth_session_info
*session_info
,
63 struct pipes_struct
**_p
,
66 struct pipes_struct
*p
;
69 ret
= make_base_pipes_struct(mem_ctx
, msg_ctx
, pipe_name
,
70 transport
, RPC_LITTLE_ENDIAN
,
72 remote_address
, local_address
, &p
);
78 if (session_info
->unix_token
&& session_info
->unix_info
&& session_info
->security_token
) {
79 /* Don't call create_local_token(), we already have the full details here */
80 p
->session_info
= talloc_steal(p
, session_info
);
83 DEBUG(0, ("Supplied session_info in make_server_pipes_struct was incomplete!"));
92 /* Start listening on the appropriate unix socket and setup all is needed to
93 * dispatch requests to the pipes rpc implementation */
95 struct dcerpc_ncacn_listen_state
{
96 struct ndr_syntax_id syntax_id
;
104 struct tevent_context
*ev_ctx
;
105 struct messaging_context
*msg_ctx
;
106 dcerpc_ncacn_disconnect_fn disconnect_fn
;
109 static void named_pipe_listener(struct tevent_context
*ev
,
110 struct tevent_fd
*fde
,
114 int create_named_pipe_socket(const char *pipe_name
)
120 * As lp_ncalrpc_dir() should have 0755, but
121 * lp_ncalrpc_dir()/np should have 0700, we need to
122 * create lp_ncalrpc_dir() first.
124 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) {
125 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
126 lp_ncalrpc_dir(), strerror(errno
)));
130 np_dir
= talloc_asprintf(talloc_tos(), "%s/np", lp_ncalrpc_dir());
132 DEBUG(0, ("Out of memory\n"));
136 if (!directory_create_or_exist(np_dir
, geteuid(), 0700)) {
137 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
138 np_dir
, strerror(errno
)));
142 fd
= create_pipe_sock(np_dir
, pipe_name
, 0700);
144 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
149 DEBUG(10, ("Openened pipe socket fd %d for %s\n", fd
, pipe_name
));
156 bool setup_named_pipe_socket(const char *pipe_name
,
157 struct tevent_context
*ev_ctx
,
158 struct messaging_context
*msg_ctx
)
160 struct dcerpc_ncacn_listen_state
*state
;
161 struct tevent_fd
*fde
;
164 state
= talloc(ev_ctx
, struct dcerpc_ncacn_listen_state
);
166 DEBUG(0, ("Out of memory\n"));
169 state
->ep
.name
= talloc_strdup(state
, pipe_name
);
170 if (state
->ep
.name
== NULL
) {
171 DEBUG(0, ("Out of memory\n"));
174 state
->fd
= create_named_pipe_socket(pipe_name
);
175 if (state
->fd
== -1) {
179 rc
= listen(state
->fd
, 5);
181 DEBUG(0, ("Failed to listen on pipe socket %s: %s\n",
182 pipe_name
, strerror(errno
)));
186 state
->ev_ctx
= ev_ctx
;
187 state
->msg_ctx
= msg_ctx
;
189 DEBUG(10, ("Openened pipe socket fd %d for %s\n",
190 state
->fd
, pipe_name
));
192 fde
= tevent_add_fd(ev_ctx
,
193 state
, state
->fd
, TEVENT_FD_READ
,
194 named_pipe_listener
, state
);
196 DEBUG(0, ("Failed to add event handler!\n"));
200 tevent_fd_set_auto_close(fde
);
204 if (state
->fd
!= -1) {
211 static void named_pipe_listener(struct tevent_context
*ev
,
212 struct tevent_fd
*fde
,
216 struct dcerpc_ncacn_listen_state
*state
=
217 talloc_get_type_abort(private_data
,
218 struct dcerpc_ncacn_listen_state
);
219 struct sockaddr_un sunaddr
;
223 /* TODO: should we have a limit to the number of clients ? */
225 len
= sizeof(sunaddr
);
227 sd
= accept(state
->fd
,
228 (struct sockaddr
*)(void *)&sunaddr
, &len
);
231 if (errno
!= EINTR
) {
232 DEBUG(6, ("Failed to get a valid socket [%s]\n",
238 DEBUG(6, ("Accepted socket %d\n", sd
));
240 named_pipe_accept_function(state
->ev_ctx
,
247 /* This is the core of the rpc server.
248 * Accepts connections from clients and process requests using the appropriate
249 * dispatcher table. */
251 struct named_pipe_client
{
252 const char *pipe_name
;
254 struct tevent_context
*ev
;
255 struct messaging_context
*msg_ctx
;
258 uint16_t device_state
;
259 uint64_t allocation_size
;
261 struct tstream_context
*tstream
;
263 struct tsocket_address
*client
;
265 struct tsocket_address
*server
;
268 struct auth_session_info
*session_info
;
270 struct pipes_struct
*p
;
272 struct tevent_queue
*write_queue
;
277 named_pipe_termination_fn
*term_fn
;
281 static int named_pipe_destructor(struct named_pipe_client
*npc
)
284 npc
->term_fn(npc
->private_data
);
289 static void named_pipe_accept_done(struct tevent_req
*subreq
);
291 void named_pipe_accept_function(struct tevent_context
*ev_ctx
,
292 struct messaging_context
*msg_ctx
,
293 const char *pipe_name
, int fd
,
294 named_pipe_termination_fn
*term_fn
,
297 struct named_pipe_client
*npc
;
298 struct tstream_context
*plain
;
299 struct tevent_req
*subreq
;
302 npc
= talloc_zero(ev_ctx
, struct named_pipe_client
);
304 DEBUG(0, ("Out of memory!\n"));
309 npc
->pipe_name
= talloc_strdup(npc
, pipe_name
);
310 if (npc
->pipe_name
== NULL
) {
311 DEBUG(0, ("Out of memory!\n"));
317 npc
->msg_ctx
= msg_ctx
;
318 npc
->term_fn
= term_fn
;
319 npc
->private_data
= private_data
;
321 talloc_set_destructor(npc
, named_pipe_destructor
);
323 /* make sure socket is in NON blocking state */
324 ret
= set_blocking(fd
, false);
326 DEBUG(2, ("Failed to make socket non-blocking\n"));
332 ret
= tstream_bsd_existing_socket(npc
, fd
, &plain
);
334 DEBUG(2, ("Failed to create tstream socket\n"));
340 npc
->file_type
= FILE_TYPE_MESSAGE_MODE_PIPE
;
341 npc
->device_state
= 0xff | 0x0400 | 0x0100;
342 npc
->allocation_size
= 4096;
344 subreq
= tstream_npa_accept_existing_send(npc
, npc
->ev
, plain
,
347 npc
->allocation_size
);
349 DEBUG(2, ("Failed to start async accept procedure\n"));
354 tevent_req_set_callback(subreq
, named_pipe_accept_done
, npc
);
357 static void named_pipe_packet_process(struct tevent_req
*subreq
);
358 static void named_pipe_packet_done(struct tevent_req
*subreq
);
360 static void named_pipe_accept_done(struct tevent_req
*subreq
)
362 struct auth_session_info_transport
*session_info_transport
;
363 struct named_pipe_client
*npc
=
364 tevent_req_callback_data(subreq
, struct named_pipe_client
);
368 ret
= tstream_npa_accept_existing_recv(subreq
, &error
, npc
,
374 &session_info_transport
);
376 npc
->session_info
= talloc_move(npc
, &session_info_transport
->session_info
);
380 DEBUG(2, ("Failed to accept named pipe connection! (%s)\n",
386 ret
= make_server_pipes_struct(npc
,
388 npc
->pipe_name
, NCACN_NP
,
389 false, npc
->server
, npc
->client
, npc
->session_info
,
392 DEBUG(2, ("Failed to create pipes_struct! (%s)\n",
397 npc
->write_queue
= tevent_queue_create(npc
, "np_server_write_queue");
398 if (!npc
->write_queue
) {
399 DEBUG(2, ("Failed to set up write queue!\n"));
403 /* And now start receiving and processing packets */
404 subreq
= dcerpc_read_ncacn_packet_send(npc
, npc
->ev
, npc
->tstream
);
406 DEBUG(2, ("Failed to start receving packets\n"));
409 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
413 DEBUG(2, ("Fatal error. Terminating client(%s) connection!\n",
415 /* terminate client connection */
420 static void named_pipe_packet_process(struct tevent_req
*subreq
)
422 struct named_pipe_client
*npc
=
423 tevent_req_callback_data(subreq
, struct named_pipe_client
);
424 struct _output_data
*out
= &npc
->p
->out_data
;
425 DATA_BLOB recv_buffer
= data_blob_null
;
426 struct ncacn_packet
*pkt
;
435 status
= dcerpc_read_ncacn_packet_recv(subreq
, npc
, &pkt
, &recv_buffer
);
437 if (!NT_STATUS_IS_OK(status
)) {
441 data_left
= recv_buffer
.length
;
442 data
= (char *)recv_buffer
.data
;
446 data_used
= process_incoming_data(npc
->p
, data
, data_left
);
448 DEBUG(3, ("Failed to process dceprc request!\n"));
449 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
453 data_left
-= data_used
;
457 /* Do not leak this buffer, npc is a long lived context */
458 talloc_free(recv_buffer
.data
);
461 /* this is needed because of the way DCERPC Binds work in
462 * the RPC marshalling code */
463 to_send
= out
->frag
.length
- out
->current_pdu_sent
;
466 npc
->iov
= talloc_zero(npc
, struct iovec
);
468 status
= NT_STATUS_NO_MEMORY
;
473 npc
->iov
[0].iov_base
= out
->frag
.data
474 + out
->current_pdu_sent
;
475 npc
->iov
[0].iov_len
= to_send
;
477 out
->current_pdu_sent
+= to_send
;
480 /* this condition is false for bind packets, or when we haven't
481 * yet got a full request, and need to wait for more data from
483 while (out
->data_sent_length
< out
->rdata
.length
) {
485 ok
= create_next_pdu(npc
->p
);
487 DEBUG(3, ("Failed to create next PDU!\n"));
488 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
492 npc
->iov
= talloc_realloc(npc
, npc
->iov
,
493 struct iovec
, npc
->count
+ 1);
495 status
= NT_STATUS_NO_MEMORY
;
499 npc
->iov
[npc
->count
].iov_base
= out
->frag
.data
;
500 npc
->iov
[npc
->count
].iov_len
= out
->frag
.length
;
505 /* we still don't have a complete request, go back and wait for more
507 if (npc
->count
== 0) {
508 /* Wait for the next packet */
509 subreq
= dcerpc_read_ncacn_packet_send(npc
, npc
->ev
, npc
->tstream
);
511 DEBUG(2, ("Failed to start receving packets\n"));
512 status
= NT_STATUS_NO_MEMORY
;
515 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
519 DEBUG(10, ("Sending %u fragments in a total of %u bytes\n",
520 (unsigned int)npc
->count
,
521 (unsigned int)npc
->p
->out_data
.data_sent_length
));
523 for (i
= 0; i
< npc
->count
; i
++) {
524 DEBUG(10, ("Sending PDU number: %d, PDU Length: %u\n",
526 (unsigned int)npc
->iov
[i
].iov_len
));
527 dump_data(11, (const uint8_t *)npc
->iov
[i
].iov_base
,
528 npc
->iov
[i
].iov_len
);
530 subreq
= tstream_writev_queue_send(npc
,
537 DEBUG(2, ("Failed to send packet\n"));
538 status
= NT_STATUS_NO_MEMORY
;
541 tevent_req_set_callback(subreq
, named_pipe_packet_done
, npc
);
547 DEBUG(2, ("Fatal error(%s). "
548 "Terminating client(%s) connection!\n",
549 nt_errstr(status
), npc
->client_name
));
550 /* terminate client connection */
555 static void named_pipe_packet_done(struct tevent_req
*subreq
)
557 struct named_pipe_client
*npc
=
558 tevent_req_callback_data(subreq
, struct named_pipe_client
);
562 ret
= tstream_writev_queue_recv(subreq
, &sys_errno
);
565 DEBUG(2, ("Writev failed!\n"));
569 if (tevent_queue_length(npc
->write_queue
) > 0) {
573 /* clear out any data that may have been left around */
575 TALLOC_FREE(npc
->iov
);
576 data_blob_free(&npc
->p
->in_data
.data
);
577 data_blob_free(&npc
->p
->out_data
.frag
);
578 data_blob_free(&npc
->p
->out_data
.rdata
);
580 talloc_free_children(npc
->p
->mem_ctx
);
582 /* Wait for the next packet */
583 subreq
= dcerpc_read_ncacn_packet_send(npc
, npc
->ev
, npc
->tstream
);
585 DEBUG(2, ("Failed to start receving packets\n"));
589 tevent_req_set_callback(subreq
, named_pipe_packet_process
, npc
);
593 DEBUG(2, ("Fatal error(%s). "
594 "Terminating client(%s) connection!\n",
595 strerror(sys_errno
), npc
->client_name
));
596 /* terminate client connection */
601 /********************************************************************
602 * Start listening on the tcp/ip socket
603 ********************************************************************/
605 static void dcerpc_ncacn_tcpip_listener(struct tevent_context
*ev
,
606 struct tevent_fd
*fde
,
610 int create_tcpip_socket(const struct sockaddr_storage
*ifss
, uint16_t *port
)
617 for (i
= SERVER_TCP_LOW_PORT
; i
<= SERVER_TCP_HIGH_PORT
; i
++) {
618 fd
= open_socket_in(SOCK_STREAM
,
629 fd
= open_socket_in(SOCK_STREAM
,
636 DEBUG(0, ("Failed to create socket on port %u!\n", *port
));
640 DEBUG(10, ("Opened tcpip socket fd %d for port %u\n", fd
, *port
));
645 uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context
*ev_ctx
,
646 struct messaging_context
*msg_ctx
,
647 const struct sockaddr_storage
*ifss
,
650 struct dcerpc_ncacn_listen_state
*state
;
651 struct tevent_fd
*fde
;
654 state
= talloc(ev_ctx
, struct dcerpc_ncacn_listen_state
);
656 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Out of memory\n"));
661 state
->ep
.port
= port
;
662 state
->disconnect_fn
= NULL
;
664 state
->fd
= create_tcpip_socket(ifss
, &state
->ep
.port
);
665 if (state
->fd
== -1) {
669 state
->ev_ctx
= ev_ctx
;
670 state
->msg_ctx
= msg_ctx
;
672 /* ready to listen */
673 set_socket_options(state
->fd
, "SO_KEEPALIVE");
674 set_socket_options(state
->fd
, lp_socket_options());
676 /* Set server socket to non-blocking for the accept. */
677 set_blocking(state
->fd
, false);
679 rc
= listen(state
->fd
, SMBD_LISTEN_BACKLOG
);
681 DEBUG(0,("setup_tcpip_socket: listen - %s\n", strerror(errno
)));
685 DEBUG(10, ("setup_tcpip_socket: openened socket fd %d for port %u\n",
686 state
->fd
, state
->ep
.port
));
688 fde
= tevent_add_fd(state
->ev_ctx
,
692 dcerpc_ncacn_tcpip_listener
,
695 DEBUG(0, ("setup_tcpip_socket: Failed to add event handler!\n"));
699 tevent_fd_set_auto_close(fde
);
701 return state
->ep
.port
;
703 if (state
->fd
!= -1) {
711 static void dcerpc_ncacn_tcpip_listener(struct tevent_context
*ev
,
712 struct tevent_fd
*fde
,
716 struct dcerpc_ncacn_listen_state
*state
=
717 talloc_get_type_abort(private_data
,
718 struct dcerpc_ncacn_listen_state
);
719 struct tsocket_address
*cli_addr
= NULL
;
720 struct tsocket_address
*srv_addr
= NULL
;
721 struct sockaddr_storage addr
;
722 socklen_t in_addrlen
= sizeof(addr
);
726 s
= accept(state
->fd
, (struct sockaddr
*)(void *) &addr
, &in_addrlen
);
728 if (errno
!= EINTR
) {
729 DEBUG(0,("tcpip_listener accept: %s\n",
735 rc
= tsocket_address_bsd_from_sockaddr(state
,
736 (struct sockaddr
*)(void *) &addr
,
744 rc
= getsockname(s
, (struct sockaddr
*)(void *) &addr
, &in_addrlen
);
750 rc
= tsocket_address_bsd_from_sockaddr(state
,
751 (struct sockaddr
*)(void *) &addr
,
759 DEBUG(6, ("tcpip_listener: Accepted socket %d\n", s
));
761 dcerpc_ncacn_accept(state
->ev_ctx
,
771 /********************************************************************
772 * Start listening on the ncalrpc socket
773 ********************************************************************/
775 static void dcerpc_ncalrpc_listener(struct tevent_context
*ev
,
776 struct tevent_fd
*fde
,
780 int create_dcerpc_ncalrpc_socket(const char *name
)
788 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) {
789 DEBUG(0, ("Failed to create ncalrpc directory %s - %s\n",
790 lp_ncalrpc_dir(), strerror(errno
)));
794 fd
= create_pipe_sock(lp_ncalrpc_dir(), name
, 0755);
796 DEBUG(0, ("Failed to create ncalrpc socket! [%s/%s]\n",
797 lp_ncalrpc_dir(), name
));
801 DEBUG(10, ("Openened ncalrpc socket fd %d for %s\n", fd
, name
));
806 bool setup_dcerpc_ncalrpc_socket(struct tevent_context
*ev_ctx
,
807 struct messaging_context
*msg_ctx
,
809 dcerpc_ncacn_disconnect_fn fn
)
811 struct dcerpc_ncacn_listen_state
*state
;
812 struct tevent_fd
*fde
;
815 state
= talloc(ev_ctx
, struct dcerpc_ncacn_listen_state
);
817 DEBUG(0, ("Out of memory\n"));
822 state
->disconnect_fn
= fn
;
828 state
->ep
.name
= talloc_strdup(state
, name
);
829 if (state
->ep
.name
== NULL
) {
830 DEBUG(0, ("Out of memory\n"));
835 state
->fd
= create_dcerpc_ncalrpc_socket(name
);
836 if (state
->fd
== -1) {
840 rc
= listen(state
->fd
, 5);
842 DEBUG(0, ("Failed to listen on ncalrpc socket %s: %s\n",
843 name
, strerror(errno
)));
847 state
->ev_ctx
= ev_ctx
;
848 state
->msg_ctx
= msg_ctx
;
850 /* Set server socket to non-blocking for the accept. */
851 set_blocking(state
->fd
, false);
853 fde
= tevent_add_fd(state
->ev_ctx
,
857 dcerpc_ncalrpc_listener
,
860 DEBUG(0, ("Failed to add event handler for ncalrpc!\n"));
864 tevent_fd_set_auto_close(fde
);
868 if (state
->fd
!= -1) {
876 static void dcerpc_ncalrpc_listener(struct tevent_context
*ev
,
877 struct tevent_fd
*fde
,
881 struct dcerpc_ncacn_listen_state
*state
=
882 talloc_get_type_abort(private_data
,
883 struct dcerpc_ncacn_listen_state
);
884 struct tsocket_address
*cli_addr
= NULL
;
885 struct sockaddr_un sunaddr
;
886 struct sockaddr
*addr
= (struct sockaddr
*)(void *)&sunaddr
;
887 socklen_t len
= sizeof(sunaddr
);
891 ZERO_STRUCT(sunaddr
);
893 sd
= accept(state
->fd
, addr
, &len
);
895 if (errno
!= EINTR
) {
896 DEBUG(0, ("ncalrpc accept() failed: %s\n", strerror(errno
)));
901 rc
= tsocket_address_bsd_from_sockaddr(state
,
909 DEBUG(10, ("Accepted ncalrpc socket %d\n", sd
));
911 dcerpc_ncacn_accept(state
->ev_ctx
,
916 state
->disconnect_fn
);
919 struct dcerpc_ncacn_conn
{
920 enum dcerpc_transport_t transport
;
924 struct pipes_struct
*p
;
925 dcerpc_ncacn_disconnect_fn disconnect_fn
;
927 struct tevent_context
*ev_ctx
;
928 struct messaging_context
*msg_ctx
;
930 struct tstream_context
*tstream
;
931 struct tevent_queue
*send_queue
;
933 struct tsocket_address
*client
;
935 struct tsocket_address
*server
;
937 struct auth_session_info
*session_info
;
943 static void dcerpc_ncacn_packet_process(struct tevent_req
*subreq
);
944 static void dcerpc_ncacn_packet_done(struct tevent_req
*subreq
);
946 void dcerpc_ncacn_accept(struct tevent_context
*ev_ctx
,
947 struct messaging_context
*msg_ctx
,
948 enum dcerpc_transport_t transport
,
950 struct tsocket_address
*cli_addr
,
951 struct tsocket_address
*srv_addr
,
953 dcerpc_ncacn_disconnect_fn fn
) {
954 struct dcerpc_ncacn_conn
*ncacn_conn
;
955 struct tevent_req
*subreq
;
956 bool system_user
= false;
964 DEBUG(10, ("dcerpc_ncacn_accept\n"));
966 ncacn_conn
= talloc_zero(ev_ctx
, struct dcerpc_ncacn_conn
);
967 if (ncacn_conn
== NULL
) {
968 DEBUG(0, ("Out of memory!\n"));
973 ncacn_conn
->transport
= transport
;
974 ncacn_conn
->ev_ctx
= ev_ctx
;
975 ncacn_conn
->msg_ctx
= msg_ctx
;
976 ncacn_conn
->sock
= s
;
977 ncacn_conn
->disconnect_fn
= fn
;
979 ncacn_conn
->client
= talloc_move(ncacn_conn
, &cli_addr
);
980 if (tsocket_address_is_inet(ncacn_conn
->client
, "ip")) {
981 ncacn_conn
->client_name
=
982 tsocket_address_inet_addr_string(ncacn_conn
->client
,
985 ncacn_conn
->client_name
=
986 tsocket_address_unix_path(ncacn_conn
->client
,
989 if (ncacn_conn
->client_name
== NULL
) {
990 DEBUG(0, ("Out of memory!\n"));
991 talloc_free(ncacn_conn
);
996 if (srv_addr
!= NULL
) {
997 ncacn_conn
->server
= talloc_move(ncacn_conn
, &srv_addr
);
999 ncacn_conn
->server_name
=
1000 tsocket_address_inet_addr_string(ncacn_conn
->server
,
1002 if (ncacn_conn
->server_name
== NULL
) {
1003 DEBUG(0, ("Out of memory!\n"));
1004 talloc_free(ncacn_conn
);
1010 switch (transport
) {
1012 pipe_name
= tsocket_address_string(ncacn_conn
->client
,
1014 if (pipe_name
== NULL
) {
1016 talloc_free(ncacn_conn
);
1022 rc
= getpeereid(s
, &uid
, &gid
);
1024 DEBUG(2, ("Failed to get ncalrpc connecting "
1025 "uid - %s!\n", strerror(errno
)));
1027 if (uid
== sec_initial_uid()) {
1033 pipe_name
= talloc_strdup(ncacn_conn
,
1035 if (pipe_name
== NULL
) {
1037 talloc_free(ncacn_conn
);
1042 DEBUG(0, ("unknown dcerpc transport: %u!\n",
1044 talloc_free(ncacn_conn
);
1049 rc
= set_blocking(s
, false);
1051 DEBUG(2, ("Failed to set dcerpc socket to non-blocking\n"));
1052 talloc_free(ncacn_conn
);
1058 * As soon as we have tstream_bsd_existing_socket set up it will
1059 * take care of closing the socket.
1061 rc
= tstream_bsd_existing_socket(ncacn_conn
, s
, &ncacn_conn
->tstream
);
1063 DEBUG(2, ("Failed to create tstream socket for dcerpc\n"));
1064 talloc_free(ncacn_conn
);
1069 if (ncacn_conn
->session_info
== NULL
) {
1070 status
= auth_anonymous_session_info(ncacn_conn
,
1071 &ncacn_conn
->session_info
);
1072 if (!NT_STATUS_IS_OK(status
)) {
1073 DEBUG(2, ("Failed to create "
1074 "auth_anonymous_session_info - %s\n",
1075 nt_errstr(status
)));
1076 talloc_free(ncacn_conn
);
1081 rc
= make_server_pipes_struct(ncacn_conn
,
1082 ncacn_conn
->msg_ctx
,
1084 ncacn_conn
->transport
,
1088 ncacn_conn
->session_info
,
1092 DEBUG(2, ("Failed to create pipe struct - %s",
1093 strerror(sys_errno
)));
1094 talloc_free(ncacn_conn
);
1098 ncacn_conn
->send_queue
= tevent_queue_create(ncacn_conn
,
1099 "dcerpc send queue");
1100 if (ncacn_conn
->send_queue
== NULL
) {
1101 DEBUG(0, ("Out of memory!\n"));
1102 talloc_free(ncacn_conn
);
1106 subreq
= dcerpc_read_ncacn_packet_send(ncacn_conn
,
1108 ncacn_conn
->tstream
);
1109 if (subreq
== NULL
) {
1110 DEBUG(2, ("Failed to send ncacn packet\n"));
1111 talloc_free(ncacn_conn
);
1115 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1117 DEBUG(10, ("dcerpc_ncacn_accept done\n"));
1122 static void dcerpc_ncacn_packet_process(struct tevent_req
*subreq
)
1124 struct dcerpc_ncacn_conn
*ncacn_conn
=
1125 tevent_req_callback_data(subreq
, struct dcerpc_ncacn_conn
);
1127 struct _output_data
*out
= &ncacn_conn
->p
->out_data
;
1128 DATA_BLOB recv_buffer
= data_blob_null
;
1129 struct ncacn_packet
*pkt
;
1137 status
= dcerpc_read_ncacn_packet_recv(subreq
, ncacn_conn
, &pkt
, &recv_buffer
);
1138 TALLOC_FREE(subreq
);
1139 if (!NT_STATUS_IS_OK(status
)) {
1140 if (ncacn_conn
->disconnect_fn
!= NULL
) {
1141 ok
= ncacn_conn
->disconnect_fn(ncacn_conn
->p
);
1143 DEBUG(3, ("Failed to call disconnect function\n"));
1149 data_left
= recv_buffer
.length
;
1150 data
= (char *) recv_buffer
.data
;
1153 data_used
= process_incoming_data(ncacn_conn
->p
, data
, data_left
);
1154 if (data_used
< 0) {
1155 DEBUG(3, ("Failed to process dcerpc request!\n"));
1156 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
1160 data_left
-= data_used
;
1164 /* Do not leak this buffer */
1165 talloc_free(recv_buffer
.data
);
1169 * This is needed because of the way DCERPC binds work in the RPC
1172 to_send
= out
->frag
.length
- out
->current_pdu_sent
;
1175 DEBUG(10, ("Current_pdu_len = %u, "
1176 "current_pdu_sent = %u "
1177 "Returning %u bytes\n",
1178 (unsigned int)out
->frag
.length
,
1179 (unsigned int)out
->current_pdu_sent
,
1180 (unsigned int)to_send
));
1182 ncacn_conn
->iov
= talloc_zero(ncacn_conn
, struct iovec
);
1183 if (ncacn_conn
->iov
== NULL
) {
1184 status
= NT_STATUS_NO_MEMORY
;
1185 DEBUG(3, ("Out of memory!\n"));
1188 ncacn_conn
->count
= 1;
1190 ncacn_conn
->iov
[0].iov_base
= out
->frag
.data
1191 + out
->current_pdu_sent
;
1192 ncacn_conn
->iov
[0].iov_len
= to_send
;
1194 out
->current_pdu_sent
+= to_send
;
1198 * This condition is false for bind packets, or when we haven't yet got
1199 * a full request, and need to wait for more data from the client
1201 while (out
->data_sent_length
< out
->rdata
.length
) {
1202 ok
= create_next_pdu(ncacn_conn
->p
);
1204 DEBUG(3, ("Failed to create next PDU!\n"));
1205 status
= NT_STATUS_UNEXPECTED_IO_ERROR
;
1209 ncacn_conn
->iov
= talloc_realloc(ncacn_conn
,
1212 ncacn_conn
->count
+ 1);
1213 if (ncacn_conn
->iov
== NULL
) {
1214 DEBUG(3, ("Out of memory!\n"));
1215 status
= NT_STATUS_NO_MEMORY
;
1219 ncacn_conn
->iov
[ncacn_conn
->count
].iov_base
= out
->frag
.data
;
1220 ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
= out
->frag
.length
;
1222 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
1223 (unsigned int) ncacn_conn
->count
,
1224 (unsigned int) ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
));
1225 dump_data(11, (const uint8_t *) ncacn_conn
->iov
[ncacn_conn
->count
].iov_base
,
1226 ncacn_conn
->iov
[ncacn_conn
->count
].iov_len
);
1227 ncacn_conn
->count
++;
1231 * We still don't have a complete request, go back and wait for more
1234 if (ncacn_conn
->count
== 0) {
1235 /* Wait for the next packet */
1236 subreq
= dcerpc_read_ncacn_packet_send(ncacn_conn
,
1238 ncacn_conn
->tstream
);
1239 if (subreq
== NULL
) {
1240 DEBUG(2, ("Failed to start receving packets\n"));
1241 status
= NT_STATUS_NO_MEMORY
;
1244 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1248 DEBUG(10, ("Sending a total of %u bytes\n",
1249 (unsigned int)ncacn_conn
->p
->out_data
.data_sent_length
));
1251 subreq
= tstream_writev_queue_send(ncacn_conn
,
1253 ncacn_conn
->tstream
,
1254 ncacn_conn
->send_queue
,
1257 if (subreq
== NULL
) {
1258 DEBUG(2, ("Failed to send packet\n"));
1259 status
= NT_STATUS_NO_MEMORY
;
1263 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_done
, ncacn_conn
);
1267 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1268 ncacn_conn
->client_name
, nt_errstr(status
)));
1270 /* Terminate client connection */
1271 talloc_free(ncacn_conn
);
1275 static void dcerpc_ncacn_packet_done(struct tevent_req
*subreq
)
1277 struct dcerpc_ncacn_conn
*ncacn_conn
=
1278 tevent_req_callback_data(subreq
, struct dcerpc_ncacn_conn
);
1279 NTSTATUS status
= NT_STATUS_OK
;
1283 rc
= tstream_writev_queue_recv(subreq
, &sys_errno
);
1284 TALLOC_FREE(subreq
);
1286 DEBUG(2, ("Writev failed!\n"));
1287 status
= map_nt_error_from_unix(sys_errno
);
1291 /* clear out any data that may have been left around */
1292 ncacn_conn
->count
= 0;
1293 TALLOC_FREE(ncacn_conn
->iov
);
1294 data_blob_free(&ncacn_conn
->p
->in_data
.data
);
1295 data_blob_free(&ncacn_conn
->p
->out_data
.frag
);
1296 data_blob_free(&ncacn_conn
->p
->out_data
.rdata
);
1298 talloc_free_children(ncacn_conn
->p
->mem_ctx
);
1300 /* Wait for the next packet */
1301 subreq
= dcerpc_read_ncacn_packet_send(ncacn_conn
,
1303 ncacn_conn
->tstream
);
1304 if (subreq
== NULL
) {
1305 DEBUG(2, ("Failed to start receving packets\n"));
1306 status
= NT_STATUS_NO_MEMORY
;
1310 tevent_req_set_callback(subreq
, dcerpc_ncacn_packet_process
, ncacn_conn
);
1314 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1315 ncacn_conn
->client_name
, nt_errstr(status
)));
1317 /* Terminate client connection */
1318 talloc_free(ncacn_conn
);
1322 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */