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 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_strict(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_done(struct tevent_req *subreq);
359 static void named_pipe_accept_done(struct tevent_req *subreq)
361 struct auth_session_info_transport *session_info_transport;
362 struct named_pipe_client *npc =
363 tevent_req_callback_data(subreq, struct named_pipe_client);
367 ret = tstream_npa_accept_existing_recv(subreq, &error, npc,
373 &session_info_transport);
375 npc->session_info = talloc_move(npc, &session_info_transport->session_info);
379 DEBUG(2, ("Failed to accept named pipe connection! (%s)\n",
385 ret = make_server_pipes_struct(npc,
387 npc->pipe_name, NCACN_NP,
388 false, npc->server, npc->client, npc->session_info,
391 DEBUG(2, ("Failed to create pipes_struct! (%s)\n",
396 npc->write_queue = tevent_queue_create(npc, "np_server_write_queue");
397 if (!npc->write_queue) {
398 DEBUG(2, ("Failed to set up write queue!\n"));
402 /* And now start receiving and processing packets */
403 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
405 DEBUG(2, ("Failed to start receving packets\n"));
408 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
412 DEBUG(2, ("Fatal error. Terminating client(%s) connection!\n",
414 /* terminate client connection */
419 void named_pipe_packet_process(struct tevent_req *subreq)
421 struct named_pipe_client *npc =
422 tevent_req_callback_data(subreq, struct named_pipe_client);
423 struct _output_data *out = &npc->p->out_data;
424 DATA_BLOB recv_buffer = data_blob_null;
425 struct ncacn_packet *pkt;
434 status = dcerpc_read_ncacn_packet_recv(subreq, npc, &pkt, &recv_buffer);
436 if (!NT_STATUS_IS_OK(status)) {
440 data_left = recv_buffer.length;
441 data = (char *)recv_buffer.data;
445 data_used = process_incoming_data(npc->p, data, data_left);
447 DEBUG(3, ("Failed to process dceprc request!\n"));
448 status = NT_STATUS_UNEXPECTED_IO_ERROR;
452 data_left -= data_used;
456 /* Do not leak this buffer, npc is a long lived context */
457 talloc_free(recv_buffer.data);
460 /* this is needed because of the way DCERPC Binds work in
461 * the RPC marshalling code */
462 to_send = out->frag.length - out->current_pdu_sent;
465 npc->iov = talloc_zero(npc, struct iovec);
467 status = NT_STATUS_NO_MEMORY;
472 npc->iov[0].iov_base = out->frag.data
473 + out->current_pdu_sent;
474 npc->iov[0].iov_len = to_send;
476 out->current_pdu_sent += to_send;
479 /* this condition is false for bind packets, or when we haven't
480 * yet got a full request, and need to wait for more data from
482 while (out->data_sent_length < out->rdata.length) {
484 ok = create_next_pdu(npc->p);
486 DEBUG(3, ("Failed to create next PDU!\n"));
487 status = NT_STATUS_UNEXPECTED_IO_ERROR;
491 npc->iov = talloc_realloc(npc, npc->iov,
492 struct iovec, npc->count + 1);
494 status = NT_STATUS_NO_MEMORY;
498 npc->iov[npc->count].iov_base = out->frag.data;
499 npc->iov[npc->count].iov_len = out->frag.length;
504 /* we still don't have a complete request, go back and wait for more
506 if (npc->count == 0) {
507 /* Wait for the next packet */
508 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
510 DEBUG(2, ("Failed to start receving packets\n"));
511 status = NT_STATUS_NO_MEMORY;
514 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
518 DEBUG(10, ("Sending %u fragments in a total of %u bytes\n",
519 (unsigned int)npc->count,
520 (unsigned int)npc->p->out_data.data_sent_length));
522 for (i = 0; i < npc->count; i++) {
523 DEBUG(10, ("Sending PDU number: %d, PDU Length: %u\n",
525 (unsigned int)npc->iov[i].iov_len));
526 dump_data(11, (const uint8_t *)npc->iov[i].iov_base,
527 npc->iov[i].iov_len);
529 subreq = tstream_writev_queue_send(npc,
536 DEBUG(2, ("Failed to send packet\n"));
537 status = NT_STATUS_NO_MEMORY;
540 tevent_req_set_callback(subreq, named_pipe_packet_done, npc);
546 DEBUG(2, ("Fatal error(%s). "
547 "Terminating client(%s) connection!\n",
548 nt_errstr(status), npc->client_name));
549 /* terminate client connection */
554 static void named_pipe_packet_done(struct tevent_req *subreq)
556 struct named_pipe_client *npc =
557 tevent_req_callback_data(subreq, struct named_pipe_client);
561 ret = tstream_writev_queue_recv(subreq, &sys_errno);
564 DEBUG(2, ("Writev failed!\n"));
568 if (tevent_queue_length(npc->write_queue) > 0) {
572 /* clear out any data that may have been left around */
574 TALLOC_FREE(npc->iov);
575 data_blob_free(&npc->p->in_data.data);
576 data_blob_free(&npc->p->out_data.frag);
577 data_blob_free(&npc->p->out_data.rdata);
579 talloc_free_children(npc->p->mem_ctx);
581 /* Wait for the next packet */
582 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
584 DEBUG(2, ("Failed to start receving packets\n"));
588 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
592 DEBUG(2, ("Fatal error(%s). "
593 "Terminating client(%s) connection!\n",
594 strerror(sys_errno), npc->client_name));
595 /* terminate client connection */
600 /********************************************************************
601 * Start listening on the tcp/ip socket
602 ********************************************************************/
604 static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
605 struct tevent_fd *fde,
609 int create_tcpip_socket(const struct sockaddr_storage *ifss, uint16_t *port)
616 for (i = SERVER_TCP_LOW_PORT; i <= SERVER_TCP_HIGH_PORT; i++) {
617 fd = open_socket_in(SOCK_STREAM,
628 fd = open_socket_in(SOCK_STREAM,
635 DEBUG(0, ("Failed to create socket on port %u!\n", *port));
639 DEBUG(10, ("Opened tcpip socket fd %d for port %u\n", fd, *port));
644 uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context *ev_ctx,
645 struct messaging_context *msg_ctx,
646 const struct sockaddr_storage *ifss,
649 struct dcerpc_ncacn_listen_state *state;
650 struct tevent_fd *fde;
653 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
655 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Out of memory\n"));
660 state->ep.port = port;
661 state->disconnect_fn = NULL;
663 state->fd = create_tcpip_socket(ifss, &state->ep.port);
664 if (state->fd == -1) {
668 state->ev_ctx = ev_ctx;
669 state->msg_ctx = msg_ctx;
671 /* ready to listen */
672 set_socket_options(state->fd, "SO_KEEPALIVE");
673 set_socket_options(state->fd, lp_socket_options());
675 /* Set server socket to non-blocking for the accept. */
676 set_blocking(state->fd, false);
678 rc = listen(state->fd, SMBD_LISTEN_BACKLOG);
680 DEBUG(0,("setup_tcpip_socket: listen - %s\n", strerror(errno)));
684 DEBUG(10, ("setup_tcpip_socket: openened socket fd %d for port %u\n",
685 state->fd, state->ep.port));
687 fde = tevent_add_fd(state->ev_ctx,
691 dcerpc_ncacn_tcpip_listener,
694 DEBUG(0, ("setup_tcpip_socket: Failed to add event handler!\n"));
698 tevent_fd_set_auto_close(fde);
700 return state->ep.port;
702 if (state->fd != -1) {
710 static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
711 struct tevent_fd *fde,
715 struct dcerpc_ncacn_listen_state *state =
716 talloc_get_type_abort(private_data,
717 struct dcerpc_ncacn_listen_state);
718 struct tsocket_address *cli_addr = NULL;
719 struct tsocket_address *srv_addr = NULL;
720 struct sockaddr_storage addr;
721 socklen_t in_addrlen = sizeof(addr);
725 s = accept(state->fd, (struct sockaddr *)(void *) &addr, &in_addrlen);
727 if (errno != EINTR) {
728 DEBUG(0,("tcpip_listener accept: %s\n",
734 rc = tsocket_address_bsd_from_sockaddr(state,
735 (struct sockaddr *)(void *) &addr,
743 rc = getsockname(s, (struct sockaddr *)(void *) &addr, &in_addrlen);
749 rc = tsocket_address_bsd_from_sockaddr(state,
750 (struct sockaddr *)(void *) &addr,
758 DEBUG(6, ("tcpip_listener: Accepted socket %d\n", s));
760 dcerpc_ncacn_accept(state->ev_ctx,
770 /********************************************************************
771 * Start listening on the ncalrpc socket
772 ********************************************************************/
774 static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
775 struct tevent_fd *fde,
779 int create_dcerpc_ncalrpc_socket(const char *name)
787 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) {
788 DEBUG(0, ("Failed to create ncalrpc directory %s - %s\n",
789 lp_ncalrpc_dir(), strerror(errno)));
793 fd = create_pipe_sock(lp_ncalrpc_dir(), name, 0755);
795 DEBUG(0, ("Failed to create ncalrpc socket! [%s/%s]\n",
796 lp_ncalrpc_dir(), name));
800 DEBUG(10, ("Openened ncalrpc socket fd %d for %s\n", fd, name));
805 bool setup_dcerpc_ncalrpc_socket(struct tevent_context *ev_ctx,
806 struct messaging_context *msg_ctx,
808 dcerpc_ncacn_disconnect_fn fn)
810 struct dcerpc_ncacn_listen_state *state;
811 struct tevent_fd *fde;
814 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
816 DEBUG(0, ("Out of memory\n"));
821 state->disconnect_fn = fn;
827 state->ep.name = talloc_strdup(state, name);
828 if (state->ep.name == NULL) {
829 DEBUG(0, ("Out of memory\n"));
834 state->fd = create_dcerpc_ncalrpc_socket(name);
835 if (state->fd == -1) {
839 rc = listen(state->fd, 5);
841 DEBUG(0, ("Failed to listen on ncalrpc socket %s: %s\n",
842 name, strerror(errno)));
846 state->ev_ctx = ev_ctx;
847 state->msg_ctx = msg_ctx;
849 /* Set server socket to non-blocking for the accept. */
850 set_blocking(state->fd, false);
852 fde = tevent_add_fd(state->ev_ctx,
856 dcerpc_ncalrpc_listener,
859 DEBUG(0, ("Failed to add event handler for ncalrpc!\n"));
863 tevent_fd_set_auto_close(fde);
867 if (state->fd != -1) {
875 static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
876 struct tevent_fd *fde,
880 struct dcerpc_ncacn_listen_state *state =
881 talloc_get_type_abort(private_data,
882 struct dcerpc_ncacn_listen_state);
883 struct tsocket_address *cli_addr = NULL;
884 struct sockaddr_un sunaddr;
885 struct sockaddr *addr = (struct sockaddr *)(void *)&sunaddr;
886 socklen_t len = sizeof(sunaddr);
890 ZERO_STRUCT(sunaddr);
892 sd = accept(state->fd, addr, &len);
894 if (errno != EINTR) {
895 DEBUG(0, ("ncalrpc accept() failed: %s\n", strerror(errno)));
900 rc = tsocket_address_bsd_from_sockaddr(state,
908 DEBUG(10, ("Accepted ncalrpc socket %d\n", sd));
910 dcerpc_ncacn_accept(state->ev_ctx,
915 state->disconnect_fn);
918 struct dcerpc_ncacn_conn {
919 enum dcerpc_transport_t transport;
923 struct pipes_struct *p;
924 dcerpc_ncacn_disconnect_fn disconnect_fn;
926 struct tevent_context *ev_ctx;
927 struct messaging_context *msg_ctx;
929 struct tstream_context *tstream;
930 struct tevent_queue *send_queue;
932 struct tsocket_address *client;
934 struct tsocket_address *server;
936 struct auth_session_info *session_info;
942 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq);
943 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq);
945 void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
946 struct messaging_context *msg_ctx,
947 enum dcerpc_transport_t transport,
949 struct tsocket_address *cli_addr,
950 struct tsocket_address *srv_addr,
952 dcerpc_ncacn_disconnect_fn fn) {
953 struct dcerpc_ncacn_conn *ncacn_conn;
954 struct tevent_req *subreq;
955 bool system_user = false;
963 DEBUG(10, ("dcerpc_ncacn_accept\n"));
965 ncacn_conn = talloc_zero(ev_ctx, struct dcerpc_ncacn_conn);
966 if (ncacn_conn == NULL) {
967 DEBUG(0, ("Out of memory!\n"));
972 ncacn_conn->transport = transport;
973 ncacn_conn->ev_ctx = ev_ctx;
974 ncacn_conn->msg_ctx = msg_ctx;
975 ncacn_conn->sock = s;
976 ncacn_conn->disconnect_fn = fn;
978 ncacn_conn->client = talloc_move(ncacn_conn, &cli_addr);
979 if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
980 ncacn_conn->client_name =
981 tsocket_address_inet_addr_string(ncacn_conn->client,
984 ncacn_conn->client_name =
985 tsocket_address_unix_path(ncacn_conn->client,
988 if (ncacn_conn->client_name == NULL) {
989 DEBUG(0, ("Out of memory!\n"));
990 talloc_free(ncacn_conn);
995 if (srv_addr != NULL) {
996 ncacn_conn->server = talloc_move(ncacn_conn, &srv_addr);
998 ncacn_conn->server_name =
999 tsocket_address_inet_addr_string(ncacn_conn->server,
1001 if (ncacn_conn->server_name == NULL) {
1002 DEBUG(0, ("Out of memory!\n"));
1003 talloc_free(ncacn_conn);
1009 switch (transport) {
1011 pipe_name = tsocket_address_string(ncacn_conn->client,
1013 if (pipe_name == NULL) {
1015 talloc_free(ncacn_conn);
1021 rc = getpeereid(s, &uid, &gid);
1023 DEBUG(2, ("Failed to get ncalrpc connecting "
1024 "uid - %s!\n", strerror(errno)));
1026 if (uid == sec_initial_uid()) {
1032 pipe_name = talloc_strdup(ncacn_conn,
1034 if (pipe_name == NULL) {
1036 talloc_free(ncacn_conn);
1041 DEBUG(0, ("unknown dcerpc transport: %u!\n",
1043 talloc_free(ncacn_conn);
1048 rc = set_blocking(s, false);
1050 DEBUG(2, ("Failed to set dcerpc socket to non-blocking\n"));
1051 talloc_free(ncacn_conn);
1057 * As soon as we have tstream_bsd_existing_socket set up it will
1058 * take care of closing the socket.
1060 rc = tstream_bsd_existing_socket(ncacn_conn, s, &ncacn_conn->tstream);
1062 DEBUG(2, ("Failed to create tstream socket for dcerpc\n"));
1063 talloc_free(ncacn_conn);
1068 if (ncacn_conn->session_info == NULL) {
1069 status = auth_anonymous_session_info(ncacn_conn,
1070 &ncacn_conn->session_info);
1071 if (!NT_STATUS_IS_OK(status)) {
1072 DEBUG(2, ("Failed to create "
1073 "auth_anonymous_session_info - %s\n",
1074 nt_errstr(status)));
1075 talloc_free(ncacn_conn);
1080 rc = make_server_pipes_struct(ncacn_conn,
1081 ncacn_conn->msg_ctx,
1083 ncacn_conn->transport,
1087 ncacn_conn->session_info,
1091 DEBUG(2, ("Failed to create pipe struct - %s",
1092 strerror(sys_errno)));
1093 talloc_free(ncacn_conn);
1097 ncacn_conn->send_queue = tevent_queue_create(ncacn_conn,
1098 "dcerpc send queue");
1099 if (ncacn_conn->send_queue == NULL) {
1100 DEBUG(0, ("Out of memory!\n"));
1101 talloc_free(ncacn_conn);
1105 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1107 ncacn_conn->tstream);
1108 if (subreq == NULL) {
1109 DEBUG(2, ("Failed to send ncacn packet\n"));
1110 talloc_free(ncacn_conn);
1114 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1116 DEBUG(10, ("dcerpc_ncacn_accept done\n"));
1121 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq)
1123 struct dcerpc_ncacn_conn *ncacn_conn =
1124 tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
1126 struct _output_data *out = &ncacn_conn->p->out_data;
1127 DATA_BLOB recv_buffer = data_blob_null;
1128 struct ncacn_packet *pkt;
1136 status = dcerpc_read_ncacn_packet_recv(subreq, ncacn_conn, &pkt, &recv_buffer);
1137 TALLOC_FREE(subreq);
1138 if (!NT_STATUS_IS_OK(status)) {
1139 if (ncacn_conn->disconnect_fn != NULL) {
1140 ok = ncacn_conn->disconnect_fn(ncacn_conn->p);
1142 DEBUG(3, ("Failed to call disconnect function\n"));
1148 data_left = recv_buffer.length;
1149 data = (char *) recv_buffer.data;
1152 data_used = process_incoming_data(ncacn_conn->p, data, data_left);
1153 if (data_used < 0) {
1154 DEBUG(3, ("Failed to process dcerpc request!\n"));
1155 status = NT_STATUS_UNEXPECTED_IO_ERROR;
1159 data_left -= data_used;
1163 /* Do not leak this buffer */
1164 talloc_free(recv_buffer.data);
1168 * This is needed because of the way DCERPC binds work in the RPC
1171 to_send = out->frag.length - out->current_pdu_sent;
1174 DEBUG(10, ("Current_pdu_len = %u, "
1175 "current_pdu_sent = %u "
1176 "Returning %u bytes\n",
1177 (unsigned int)out->frag.length,
1178 (unsigned int)out->current_pdu_sent,
1179 (unsigned int)to_send));
1181 ncacn_conn->iov = talloc_zero(ncacn_conn, struct iovec);
1182 if (ncacn_conn->iov == NULL) {
1183 status = NT_STATUS_NO_MEMORY;
1184 DEBUG(3, ("Out of memory!\n"));
1187 ncacn_conn->count = 1;
1189 ncacn_conn->iov[0].iov_base = out->frag.data
1190 + out->current_pdu_sent;
1191 ncacn_conn->iov[0].iov_len = to_send;
1193 out->current_pdu_sent += to_send;
1197 * This condition is false for bind packets, or when we haven't yet got
1198 * a full request, and need to wait for more data from the client
1200 while (out->data_sent_length < out->rdata.length) {
1201 ok = create_next_pdu(ncacn_conn->p);
1203 DEBUG(3, ("Failed to create next PDU!\n"));
1204 status = NT_STATUS_UNEXPECTED_IO_ERROR;
1208 ncacn_conn->iov = talloc_realloc(ncacn_conn,
1211 ncacn_conn->count + 1);
1212 if (ncacn_conn->iov == NULL) {
1213 DEBUG(3, ("Out of memory!\n"));
1214 status = NT_STATUS_NO_MEMORY;
1218 ncacn_conn->iov[ncacn_conn->count].iov_base = out->frag.data;
1219 ncacn_conn->iov[ncacn_conn->count].iov_len = out->frag.length;
1221 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
1222 (unsigned int) ncacn_conn->count,
1223 (unsigned int) ncacn_conn->iov[ncacn_conn->count].iov_len));
1224 dump_data(11, (const uint8_t *) ncacn_conn->iov[ncacn_conn->count].iov_base,
1225 ncacn_conn->iov[ncacn_conn->count].iov_len);
1226 ncacn_conn->count++;
1230 * We still don't have a complete request, go back and wait for more
1233 if (ncacn_conn->count == 0) {
1234 /* Wait for the next packet */
1235 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1237 ncacn_conn->tstream);
1238 if (subreq == NULL) {
1239 DEBUG(2, ("Failed to start receving packets\n"));
1240 status = NT_STATUS_NO_MEMORY;
1243 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1247 DEBUG(10, ("Sending a total of %u bytes\n",
1248 (unsigned int)ncacn_conn->p->out_data.data_sent_length));
1250 subreq = tstream_writev_queue_send(ncacn_conn,
1252 ncacn_conn->tstream,
1253 ncacn_conn->send_queue,
1256 if (subreq == NULL) {
1257 DEBUG(2, ("Failed to send packet\n"));
1258 status = NT_STATUS_NO_MEMORY;
1262 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_done, ncacn_conn);
1266 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1267 ncacn_conn->client_name, nt_errstr(status)));
1269 /* Terminate client connection */
1270 talloc_free(ncacn_conn);
1274 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq)
1276 struct dcerpc_ncacn_conn *ncacn_conn =
1277 tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
1278 NTSTATUS status = NT_STATUS_OK;
1282 rc = tstream_writev_queue_recv(subreq, &sys_errno);
1283 TALLOC_FREE(subreq);
1285 DEBUG(2, ("Writev failed!\n"));
1286 status = map_nt_error_from_unix(sys_errno);
1290 /* clear out any data that may have been left around */
1291 ncacn_conn->count = 0;
1292 TALLOC_FREE(ncacn_conn->iov);
1293 data_blob_free(&ncacn_conn->p->in_data.data);
1294 data_blob_free(&ncacn_conn->p->out_data.frag);
1295 data_blob_free(&ncacn_conn->p->out_data.rdata);
1297 talloc_free_children(ncacn_conn->p->mem_ctx);
1299 /* Wait for the next packet */
1300 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1302 ncacn_conn->tstream);
1303 if (subreq == NULL) {
1304 DEBUG(2, ("Failed to start receving packets\n"));
1305 status = NT_STATUS_NO_MEMORY;
1309 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1313 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1314 ncacn_conn->client_name, nt_errstr(status)));
1316 /* Terminate client connection */
1317 talloc_free(ncacn_conn);
1321 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */