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 /* Creates a pipes_struct and initializes it with the information
41 * sent from the client */
42 int make_server_pipes_struct(TALLOC_CTX *mem_ctx,
43 struct messaging_context *msg_ctx,
44 const char *pipe_name,
45 enum dcerpc_transport_t transport,
46 bool ncalrpc_as_system,
47 const struct tsocket_address *local_address,
48 const struct tsocket_address *remote_address,
49 struct auth_session_info *session_info,
50 struct pipes_struct **_p,
53 struct pipes_struct *p;
56 ret = make_base_pipes_struct(mem_ctx, msg_ctx, pipe_name,
57 transport, RPC_LITTLE_ENDIAN,
59 remote_address, local_address, &p);
65 if (session_info->unix_token && session_info->unix_info && session_info->security_token) {
66 /* Don't call create_local_token(), we already have the full details here */
67 p->session_info = talloc_steal(p, session_info);
70 DEBUG(0, ("Supplied session_info in make_server_pipes_struct was incomplete!"));
79 /* Start listening on the appropriate unix socket and setup all is needed to
80 * dispatch requests to the pipes rpc implementation */
82 struct dcerpc_ncacn_listen_state {
83 struct ndr_syntax_id syntax_id;
91 struct tevent_context *ev_ctx;
92 struct messaging_context *msg_ctx;
93 dcerpc_ncacn_disconnect_fn disconnect_fn;
96 static void named_pipe_listener(struct tevent_context *ev,
97 struct tevent_fd *fde,
101 int create_named_pipe_socket(const char *pipe_name)
107 * As lp_ncalrpc_dir() should have 0755, but
108 * lp_ncalrpc_dir()/np should have 0700, we need to
109 * create lp_ncalrpc_dir() first.
111 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) {
112 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
113 lp_ncalrpc_dir(), strerror(errno)));
117 np_dir = talloc_asprintf(talloc_tos(), "%s/np", lp_ncalrpc_dir());
119 DEBUG(0, ("Out of memory\n"));
123 if (!directory_create_or_exist_strict(np_dir, geteuid(), 0700)) {
124 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
125 np_dir, strerror(errno)));
129 fd = create_pipe_sock(np_dir, pipe_name, 0700);
131 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
136 DEBUG(10, ("Openened pipe socket fd %d for %s\n", fd, pipe_name));
143 bool setup_named_pipe_socket(const char *pipe_name,
144 struct tevent_context *ev_ctx,
145 struct messaging_context *msg_ctx)
147 struct dcerpc_ncacn_listen_state *state;
148 struct tevent_fd *fde;
151 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
153 DEBUG(0, ("Out of memory\n"));
156 state->ep.name = talloc_strdup(state, pipe_name);
157 if (state->ep.name == NULL) {
158 DEBUG(0, ("Out of memory\n"));
161 state->fd = create_named_pipe_socket(pipe_name);
162 if (state->fd == -1) {
166 rc = listen(state->fd, 5);
168 DEBUG(0, ("Failed to listen on pipe socket %s: %s\n",
169 pipe_name, strerror(errno)));
173 state->ev_ctx = ev_ctx;
174 state->msg_ctx = msg_ctx;
176 DEBUG(10, ("Openened pipe socket fd %d for %s\n",
177 state->fd, pipe_name));
179 fde = tevent_add_fd(ev_ctx,
180 state, state->fd, TEVENT_FD_READ,
181 named_pipe_listener, state);
183 DEBUG(0, ("Failed to add event handler!\n"));
187 tevent_fd_set_auto_close(fde);
191 if (state->fd != -1) {
198 static void named_pipe_listener(struct tevent_context *ev,
199 struct tevent_fd *fde,
203 struct dcerpc_ncacn_listen_state *state =
204 talloc_get_type_abort(private_data,
205 struct dcerpc_ncacn_listen_state);
206 struct sockaddr_un sunaddr;
210 /* TODO: should we have a limit to the number of clients ? */
212 len = sizeof(sunaddr);
214 sd = accept(state->fd,
215 (struct sockaddr *)(void *)&sunaddr, &len);
218 if (errno != EINTR) {
219 DEBUG(6, ("Failed to get a valid socket [%s]\n",
225 DEBUG(6, ("Accepted socket %d\n", sd));
227 named_pipe_accept_function(state->ev_ctx,
234 /* This is the core of the rpc server.
235 * Accepts connections from clients and process requests using the appropriate
236 * dispatcher table. */
238 static int named_pipe_destructor(struct named_pipe_client *npc)
241 npc->term_fn(npc->private_data);
246 struct named_pipe_client *named_pipe_client_init(TALLOC_CTX *mem_ctx,
247 struct tevent_context *ev_ctx,
248 struct messaging_context *msg_ctx,
249 const char *pipe_name,
250 named_pipe_termination_fn *term_fn,
252 uint16_t device_state,
253 uint64_t allocation_size,
256 struct named_pipe_client *npc;
258 npc = talloc_zero(mem_ctx, struct named_pipe_client);
260 DEBUG(0, ("Out of memory!\n"));
263 talloc_set_destructor(npc, named_pipe_destructor);
265 npc->pipe_name = talloc_strdup(npc, pipe_name);
266 if (npc->pipe_name == NULL) {
267 DEBUG(0, ("Out of memory!\n"));
273 npc->msg_ctx = msg_ctx;
274 npc->term_fn = term_fn;
275 npc->private_data = private_data;
277 npc->file_type = file_type;
278 npc->device_state = device_state;
279 npc->allocation_size = allocation_size;
284 static void named_pipe_accept_done(struct tevent_req *subreq);
286 void named_pipe_accept_function(struct tevent_context *ev_ctx,
287 struct messaging_context *msg_ctx,
288 const char *pipe_name, int fd,
289 named_pipe_termination_fn *term_fn,
292 struct named_pipe_client *npc;
293 struct tstream_context *plain;
294 struct tevent_req *subreq;
297 npc = talloc_zero(ev_ctx, struct named_pipe_client);
299 DEBUG(0, ("Out of memory!\n"));
304 npc->pipe_name = talloc_strdup(npc, pipe_name);
305 if (npc->pipe_name == NULL) {
306 DEBUG(0, ("Out of memory!\n"));
312 npc->msg_ctx = msg_ctx;
313 npc->term_fn = term_fn;
314 npc->private_data = private_data;
316 talloc_set_destructor(npc, named_pipe_destructor);
318 /* make sure socket is in NON blocking state */
319 ret = set_blocking(fd, false);
321 DEBUG(2, ("Failed to make socket non-blocking\n"));
327 ret = tstream_bsd_existing_socket(npc, fd, &plain);
329 DEBUG(2, ("Failed to create tstream socket\n"));
335 npc->file_type = FILE_TYPE_MESSAGE_MODE_PIPE;
336 npc->device_state = 0xff | 0x0400 | 0x0100;
337 npc->allocation_size = 4096;
339 subreq = tstream_npa_accept_existing_send(npc, npc->ev, plain,
342 npc->allocation_size);
344 DEBUG(2, ("Failed to start async accept procedure\n"));
349 tevent_req_set_callback(subreq, named_pipe_accept_done, npc);
352 static void named_pipe_packet_done(struct tevent_req *subreq);
354 static void named_pipe_accept_done(struct tevent_req *subreq)
356 struct auth_session_info_transport *session_info_transport;
357 struct named_pipe_client *npc =
358 tevent_req_callback_data(subreq, struct named_pipe_client);
362 ret = tstream_npa_accept_existing_recv(subreq, &error, npc,
368 &session_info_transport);
370 npc->session_info = talloc_move(npc, &session_info_transport->session_info);
374 DEBUG(2, ("Failed to accept named pipe connection! (%s)\n",
380 ret = make_server_pipes_struct(npc,
382 npc->pipe_name, NCACN_NP,
383 false, npc->server, npc->client, npc->session_info,
386 DEBUG(2, ("Failed to create pipes_struct! (%s)\n",
391 npc->write_queue = tevent_queue_create(npc, "np_server_write_queue");
392 if (!npc->write_queue) {
393 DEBUG(2, ("Failed to set up write queue!\n"));
397 /* And now start receiving and processing packets */
398 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
400 DEBUG(2, ("Failed to start receving packets\n"));
403 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
407 DEBUG(2, ("Fatal error. Terminating client(%s) connection!\n",
409 /* terminate client connection */
414 void named_pipe_packet_process(struct tevent_req *subreq)
416 struct named_pipe_client *npc =
417 tevent_req_callback_data(subreq, struct named_pipe_client);
418 struct _output_data *out = &npc->p->out_data;
419 DATA_BLOB recv_buffer = data_blob_null;
420 struct ncacn_packet *pkt;
426 status = dcerpc_read_ncacn_packet_recv(subreq, npc, &pkt, &recv_buffer);
428 if (!NT_STATUS_IS_OK(status)) {
432 /* dcerpc_read_ncacn_packet_recv() returns a full PDU */
433 npc->p->in_data.pdu_needed_len = 0;
434 npc->p->in_data.pdu = recv_buffer;
435 if (dcerpc_get_endian_flag(&recv_buffer) & DCERPC_DREP_LE) {
436 npc->p->endian = RPC_LITTLE_ENDIAN;
438 npc->p->endian = RPC_BIG_ENDIAN;
440 DEBUG(10, ("PDU is in %s Endian format!\n",
441 npc->p->endian ? "Big" : "Little"));
442 process_complete_pdu(npc->p, pkt);
444 /* reset pipe state and free PDU */
445 npc->p->in_data.pdu.length = 0;
446 talloc_free(recv_buffer.data);
449 /* this is needed because of the way DCERPC Binds work in
450 * the RPC marshalling code */
451 to_send = out->frag.length - out->current_pdu_sent;
454 npc->iov = talloc_zero(npc, struct iovec);
456 status = NT_STATUS_NO_MEMORY;
461 npc->iov[0].iov_base = out->frag.data
462 + out->current_pdu_sent;
463 npc->iov[0].iov_len = to_send;
465 out->current_pdu_sent += to_send;
468 /* this condition is false for bind packets, or when we haven't
469 * yet got a full request, and need to wait for more data from
471 while (out->data_sent_length < out->rdata.length) {
473 ok = create_next_pdu(npc->p);
475 DEBUG(3, ("Failed to create next PDU!\n"));
476 status = NT_STATUS_UNEXPECTED_IO_ERROR;
480 npc->iov = talloc_realloc(npc, npc->iov,
481 struct iovec, npc->count + 1);
483 status = NT_STATUS_NO_MEMORY;
487 npc->iov[npc->count].iov_base = out->frag.data;
488 npc->iov[npc->count].iov_len = out->frag.length;
493 /* we still don't have a complete request, go back and wait for more
495 if (npc->count == 0) {
496 /* Wait for the next packet */
497 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
499 DEBUG(2, ("Failed to start receving packets\n"));
500 status = NT_STATUS_NO_MEMORY;
503 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
507 DEBUG(10, ("Sending %u fragments in a total of %u bytes\n",
508 (unsigned int)npc->count,
509 (unsigned int)npc->p->out_data.data_sent_length));
511 for (i = 0; i < npc->count; i++) {
512 DEBUG(10, ("Sending PDU number: %d, PDU Length: %u\n",
514 (unsigned int)npc->iov[i].iov_len));
515 dump_data(11, (const uint8_t *)npc->iov[i].iov_base,
516 npc->iov[i].iov_len);
518 subreq = tstream_writev_queue_send(npc,
525 DEBUG(2, ("Failed to send packet\n"));
526 status = NT_STATUS_NO_MEMORY;
529 tevent_req_set_callback(subreq, named_pipe_packet_done, npc);
535 DEBUG(2, ("Fatal error(%s). "
536 "Terminating client(%s) connection!\n",
537 nt_errstr(status), npc->client_name));
538 /* terminate client connection */
543 static void named_pipe_packet_done(struct tevent_req *subreq)
545 struct named_pipe_client *npc =
546 tevent_req_callback_data(subreq, struct named_pipe_client);
550 ret = tstream_writev_queue_recv(subreq, &sys_errno);
553 DEBUG(2, ("Writev failed!\n"));
557 if (tevent_queue_length(npc->write_queue) > 0) {
561 /* clear out any data that may have been left around */
563 TALLOC_FREE(npc->iov);
564 data_blob_free(&npc->p->in_data.data);
565 data_blob_free(&npc->p->out_data.frag);
566 data_blob_free(&npc->p->out_data.rdata);
568 talloc_free_children(npc->p->mem_ctx);
570 /* Wait for the next packet */
571 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
573 DEBUG(2, ("Failed to start receving packets\n"));
577 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
581 DEBUG(2, ("Fatal error(%s). "
582 "Terminating client(%s) connection!\n",
583 strerror(sys_errno), npc->client_name));
584 /* terminate client connection */
589 /********************************************************************
590 * Start listening on the tcp/ip socket
591 ********************************************************************/
593 static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
594 struct tevent_fd *fde,
598 int create_tcpip_socket(const struct sockaddr_storage *ifss, uint16_t *port)
605 for (i = SERVER_TCP_LOW_PORT; i <= SERVER_TCP_HIGH_PORT; i++) {
606 fd = open_socket_in(SOCK_STREAM,
617 fd = open_socket_in(SOCK_STREAM,
624 DEBUG(0, ("Failed to create socket on port %u!\n", *port));
628 DEBUG(10, ("Opened tcpip socket fd %d for port %u\n", fd, *port));
633 uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context *ev_ctx,
634 struct messaging_context *msg_ctx,
635 const struct sockaddr_storage *ifss,
638 struct dcerpc_ncacn_listen_state *state;
639 struct tevent_fd *fde;
642 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
644 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Out of memory\n"));
649 state->ep.port = port;
650 state->disconnect_fn = NULL;
652 state->fd = create_tcpip_socket(ifss, &state->ep.port);
653 if (state->fd == -1) {
657 state->ev_ctx = ev_ctx;
658 state->msg_ctx = msg_ctx;
660 /* ready to listen */
661 set_socket_options(state->fd, "SO_KEEPALIVE");
662 set_socket_options(state->fd, lp_socket_options());
664 /* Set server socket to non-blocking for the accept. */
665 set_blocking(state->fd, false);
667 rc = listen(state->fd, SMBD_LISTEN_BACKLOG);
669 DEBUG(0,("setup_tcpip_socket: listen - %s\n", strerror(errno)));
673 DEBUG(10, ("setup_tcpip_socket: openened socket fd %d for port %u\n",
674 state->fd, state->ep.port));
676 fde = tevent_add_fd(state->ev_ctx,
680 dcerpc_ncacn_tcpip_listener,
683 DEBUG(0, ("setup_tcpip_socket: Failed to add event handler!\n"));
687 tevent_fd_set_auto_close(fde);
689 return state->ep.port;
691 if (state->fd != -1) {
699 static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
700 struct tevent_fd *fde,
704 struct dcerpc_ncacn_listen_state *state =
705 talloc_get_type_abort(private_data,
706 struct dcerpc_ncacn_listen_state);
707 struct tsocket_address *cli_addr = NULL;
708 struct tsocket_address *srv_addr = NULL;
709 struct sockaddr_storage addr;
710 socklen_t in_addrlen = sizeof(addr);
714 s = accept(state->fd, (struct sockaddr *)(void *) &addr, &in_addrlen);
716 if (errno != EINTR) {
717 DEBUG(0,("tcpip_listener accept: %s\n",
723 rc = tsocket_address_bsd_from_sockaddr(state,
724 (struct sockaddr *)(void *) &addr,
732 rc = getsockname(s, (struct sockaddr *)(void *) &addr, &in_addrlen);
738 rc = tsocket_address_bsd_from_sockaddr(state,
739 (struct sockaddr *)(void *) &addr,
747 DEBUG(6, ("tcpip_listener: Accepted socket %d\n", s));
749 dcerpc_ncacn_accept(state->ev_ctx,
759 /********************************************************************
760 * Start listening on the ncalrpc socket
761 ********************************************************************/
763 static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
764 struct tevent_fd *fde,
768 int create_dcerpc_ncalrpc_socket(const char *name)
776 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) {
777 DEBUG(0, ("Failed to create ncalrpc directory %s - %s\n",
778 lp_ncalrpc_dir(), strerror(errno)));
782 fd = create_pipe_sock(lp_ncalrpc_dir(), name, 0755);
784 DEBUG(0, ("Failed to create ncalrpc socket! [%s/%s]\n",
785 lp_ncalrpc_dir(), name));
789 DEBUG(10, ("Openened ncalrpc socket fd %d for %s\n", fd, name));
794 bool setup_dcerpc_ncalrpc_socket(struct tevent_context *ev_ctx,
795 struct messaging_context *msg_ctx,
797 dcerpc_ncacn_disconnect_fn fn)
799 struct dcerpc_ncacn_listen_state *state;
800 struct tevent_fd *fde;
803 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
805 DEBUG(0, ("Out of memory\n"));
810 state->disconnect_fn = fn;
816 state->ep.name = talloc_strdup(state, name);
817 if (state->ep.name == NULL) {
818 DEBUG(0, ("Out of memory\n"));
823 state->fd = create_dcerpc_ncalrpc_socket(name);
824 if (state->fd == -1) {
828 rc = listen(state->fd, 5);
830 DEBUG(0, ("Failed to listen on ncalrpc socket %s: %s\n",
831 name, strerror(errno)));
835 state->ev_ctx = ev_ctx;
836 state->msg_ctx = msg_ctx;
838 /* Set server socket to non-blocking for the accept. */
839 set_blocking(state->fd, false);
841 fde = tevent_add_fd(state->ev_ctx,
845 dcerpc_ncalrpc_listener,
848 DEBUG(0, ("Failed to add event handler for ncalrpc!\n"));
852 tevent_fd_set_auto_close(fde);
856 if (state->fd != -1) {
864 static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
865 struct tevent_fd *fde,
869 struct dcerpc_ncacn_listen_state *state =
870 talloc_get_type_abort(private_data,
871 struct dcerpc_ncacn_listen_state);
872 struct tsocket_address *cli_addr = NULL;
873 struct sockaddr_un sunaddr;
874 struct sockaddr *addr = (struct sockaddr *)(void *)&sunaddr;
875 socklen_t len = sizeof(sunaddr);
879 ZERO_STRUCT(sunaddr);
881 sd = accept(state->fd, addr, &len);
883 if (errno != EINTR) {
884 DEBUG(0, ("ncalrpc accept() failed: %s\n", strerror(errno)));
889 rc = tsocket_address_bsd_from_sockaddr(state,
897 DEBUG(10, ("Accepted ncalrpc socket %d\n", sd));
899 dcerpc_ncacn_accept(state->ev_ctx,
904 state->disconnect_fn);
907 struct dcerpc_ncacn_conn {
908 enum dcerpc_transport_t transport;
912 struct pipes_struct *p;
913 dcerpc_ncacn_disconnect_fn disconnect_fn;
915 struct tevent_context *ev_ctx;
916 struct messaging_context *msg_ctx;
918 struct tstream_context *tstream;
919 struct tevent_queue *send_queue;
921 struct tsocket_address *client;
923 struct tsocket_address *server;
925 struct auth_session_info *session_info;
931 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq);
932 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq);
934 void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
935 struct messaging_context *msg_ctx,
936 enum dcerpc_transport_t transport,
938 struct tsocket_address *cli_addr,
939 struct tsocket_address *srv_addr,
941 dcerpc_ncacn_disconnect_fn fn) {
942 struct dcerpc_ncacn_conn *ncacn_conn;
943 struct tevent_req *subreq;
944 bool system_user = false;
952 DEBUG(10, ("dcerpc_ncacn_accept\n"));
954 ncacn_conn = talloc_zero(ev_ctx, struct dcerpc_ncacn_conn);
955 if (ncacn_conn == NULL) {
956 DEBUG(0, ("Out of memory!\n"));
961 ncacn_conn->transport = transport;
962 ncacn_conn->ev_ctx = ev_ctx;
963 ncacn_conn->msg_ctx = msg_ctx;
964 ncacn_conn->sock = s;
965 ncacn_conn->disconnect_fn = fn;
967 ncacn_conn->client = talloc_move(ncacn_conn, &cli_addr);
968 if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
969 ncacn_conn->client_name =
970 tsocket_address_inet_addr_string(ncacn_conn->client,
973 ncacn_conn->client_name =
974 tsocket_address_unix_path(ncacn_conn->client,
977 if (ncacn_conn->client_name == NULL) {
978 DEBUG(0, ("Out of memory!\n"));
979 talloc_free(ncacn_conn);
984 if (srv_addr != NULL) {
985 ncacn_conn->server = talloc_move(ncacn_conn, &srv_addr);
987 ncacn_conn->server_name =
988 tsocket_address_inet_addr_string(ncacn_conn->server,
990 if (ncacn_conn->server_name == NULL) {
991 DEBUG(0, ("Out of memory!\n"));
992 talloc_free(ncacn_conn);
1000 pipe_name = tsocket_address_string(ncacn_conn->client,
1002 if (pipe_name == NULL) {
1004 talloc_free(ncacn_conn);
1010 rc = getpeereid(s, &uid, &gid);
1012 DEBUG(2, ("Failed to get ncalrpc connecting "
1013 "uid - %s!\n", strerror(errno)));
1015 if (uid == sec_initial_uid()) {
1021 pipe_name = talloc_strdup(ncacn_conn,
1023 if (pipe_name == NULL) {
1025 talloc_free(ncacn_conn);
1030 DEBUG(0, ("unknown dcerpc transport: %u!\n",
1032 talloc_free(ncacn_conn);
1037 rc = set_blocking(s, false);
1039 DEBUG(2, ("Failed to set dcerpc socket to non-blocking\n"));
1040 talloc_free(ncacn_conn);
1046 * As soon as we have tstream_bsd_existing_socket set up it will
1047 * take care of closing the socket.
1049 rc = tstream_bsd_existing_socket(ncacn_conn, s, &ncacn_conn->tstream);
1051 DEBUG(2, ("Failed to create tstream socket for dcerpc\n"));
1052 talloc_free(ncacn_conn);
1057 if (ncacn_conn->session_info == NULL) {
1059 * TODO: use auth_anonymous_session_info() here?
1061 status = make_session_info_guest(ncacn_conn,
1062 &ncacn_conn->session_info);
1063 if (!NT_STATUS_IS_OK(status)) {
1064 DEBUG(2, ("Failed to create "
1065 "make_session_info_guest - %s\n",
1066 nt_errstr(status)));
1067 talloc_free(ncacn_conn);
1072 rc = make_server_pipes_struct(ncacn_conn,
1073 ncacn_conn->msg_ctx,
1075 ncacn_conn->transport,
1079 ncacn_conn->session_info,
1083 DEBUG(2, ("Failed to create pipe struct - %s",
1084 strerror(sys_errno)));
1085 talloc_free(ncacn_conn);
1089 ncacn_conn->send_queue = tevent_queue_create(ncacn_conn,
1090 "dcerpc send queue");
1091 if (ncacn_conn->send_queue == NULL) {
1092 DEBUG(0, ("Out of memory!\n"));
1093 talloc_free(ncacn_conn);
1097 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1099 ncacn_conn->tstream);
1100 if (subreq == NULL) {
1101 DEBUG(2, ("Failed to send ncacn packet\n"));
1102 talloc_free(ncacn_conn);
1106 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1108 DEBUG(10, ("dcerpc_ncacn_accept done\n"));
1113 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq)
1115 struct dcerpc_ncacn_conn *ncacn_conn =
1116 tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
1118 struct _output_data *out = &ncacn_conn->p->out_data;
1119 DATA_BLOB recv_buffer = data_blob_null;
1120 struct ncacn_packet *pkt;
1125 status = dcerpc_read_ncacn_packet_recv(subreq, ncacn_conn, &pkt, &recv_buffer);
1126 TALLOC_FREE(subreq);
1127 if (!NT_STATUS_IS_OK(status)) {
1128 if (ncacn_conn->disconnect_fn != NULL) {
1129 ok = ncacn_conn->disconnect_fn(ncacn_conn->p);
1131 DEBUG(3, ("Failed to call disconnect function\n"));
1137 /* dcerpc_read_ncacn_packet_recv() returns a full PDU */
1138 ncacn_conn->p->in_data.pdu_needed_len = 0;
1139 ncacn_conn->p->in_data.pdu = recv_buffer;
1140 if (dcerpc_get_endian_flag(&recv_buffer) & DCERPC_DREP_LE) {
1141 ncacn_conn->p->endian = RPC_LITTLE_ENDIAN;
1143 ncacn_conn->p->endian = RPC_BIG_ENDIAN;
1145 DEBUG(10, ("PDU is in %s Endian format!\n",
1146 ncacn_conn->p->endian ? "Big" : "Little"));
1147 process_complete_pdu(ncacn_conn->p, pkt);
1149 /* reset pipe state and free PDU */
1150 ncacn_conn->p->in_data.pdu.length = 0;
1151 talloc_free(recv_buffer.data);
1155 * This is needed because of the way DCERPC binds work in the RPC
1158 to_send = out->frag.length - out->current_pdu_sent;
1161 DEBUG(10, ("Current_pdu_len = %u, "
1162 "current_pdu_sent = %u "
1163 "Returning %u bytes\n",
1164 (unsigned int)out->frag.length,
1165 (unsigned int)out->current_pdu_sent,
1166 (unsigned int)to_send));
1168 ncacn_conn->iov = talloc_zero(ncacn_conn, struct iovec);
1169 if (ncacn_conn->iov == NULL) {
1170 status = NT_STATUS_NO_MEMORY;
1171 DEBUG(3, ("Out of memory!\n"));
1174 ncacn_conn->count = 1;
1176 ncacn_conn->iov[0].iov_base = out->frag.data
1177 + out->current_pdu_sent;
1178 ncacn_conn->iov[0].iov_len = to_send;
1180 out->current_pdu_sent += to_send;
1184 * This condition is false for bind packets, or when we haven't yet got
1185 * a full request, and need to wait for more data from the client
1187 while (out->data_sent_length < out->rdata.length) {
1188 ok = create_next_pdu(ncacn_conn->p);
1190 DEBUG(3, ("Failed to create next PDU!\n"));
1191 status = NT_STATUS_UNEXPECTED_IO_ERROR;
1195 ncacn_conn->iov = talloc_realloc(ncacn_conn,
1198 ncacn_conn->count + 1);
1199 if (ncacn_conn->iov == NULL) {
1200 DEBUG(3, ("Out of memory!\n"));
1201 status = NT_STATUS_NO_MEMORY;
1205 ncacn_conn->iov[ncacn_conn->count].iov_base = out->frag.data;
1206 ncacn_conn->iov[ncacn_conn->count].iov_len = out->frag.length;
1208 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
1209 (unsigned int) ncacn_conn->count,
1210 (unsigned int) ncacn_conn->iov[ncacn_conn->count].iov_len));
1211 dump_data(11, (const uint8_t *) ncacn_conn->iov[ncacn_conn->count].iov_base,
1212 ncacn_conn->iov[ncacn_conn->count].iov_len);
1213 ncacn_conn->count++;
1217 * We still don't have a complete request, go back and wait for more
1220 if (ncacn_conn->count == 0) {
1221 /* Wait for the next packet */
1222 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1224 ncacn_conn->tstream);
1225 if (subreq == NULL) {
1226 DEBUG(2, ("Failed to start receving packets\n"));
1227 status = NT_STATUS_NO_MEMORY;
1230 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1234 DEBUG(10, ("Sending a total of %u bytes\n",
1235 (unsigned int)ncacn_conn->p->out_data.data_sent_length));
1237 subreq = tstream_writev_queue_send(ncacn_conn,
1239 ncacn_conn->tstream,
1240 ncacn_conn->send_queue,
1243 if (subreq == NULL) {
1244 DEBUG(2, ("Failed to send packet\n"));
1245 status = NT_STATUS_NO_MEMORY;
1249 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_done, ncacn_conn);
1253 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1254 ncacn_conn->client_name, nt_errstr(status)));
1256 /* Terminate client connection */
1257 talloc_free(ncacn_conn);
1261 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq)
1263 struct dcerpc_ncacn_conn *ncacn_conn =
1264 tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
1265 NTSTATUS status = NT_STATUS_OK;
1269 rc = tstream_writev_queue_recv(subreq, &sys_errno);
1270 TALLOC_FREE(subreq);
1272 DEBUG(2, ("Writev failed!\n"));
1273 status = map_nt_error_from_unix(sys_errno);
1277 /* clear out any data that may have been left around */
1278 ncacn_conn->count = 0;
1279 TALLOC_FREE(ncacn_conn->iov);
1280 data_blob_free(&ncacn_conn->p->in_data.data);
1281 data_blob_free(&ncacn_conn->p->out_data.frag);
1282 data_blob_free(&ncacn_conn->p->out_data.rdata);
1284 talloc_free_children(ncacn_conn->p->mem_ctx);
1286 /* Wait for the next packet */
1287 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1289 ncacn_conn->tstream);
1290 if (subreq == NULL) {
1291 DEBUG(2, ("Failed to start receving packets\n"));
1292 status = NT_STATUS_NO_MEMORY;
1296 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1300 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1301 ncacn_conn->client_name, nt_errstr(status)));
1303 /* Terminate client connection */
1304 talloc_free(ncacn_conn);
1308 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */