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"
26 #include "librpc/gen_ndr/netlogon.h"
27 #include "librpc/gen_ndr/auth.h"
28 #include "lib/tsocket/tsocket.h"
29 #include "libcli/named_pipe_auth/npa_tstream.h"
30 #include "../auth/auth_sam_reply.h"
32 #include "rpc_server/rpc_ncacn_np.h"
33 #include "rpc_server/srv_pipe_hnd.h"
34 #include "rpc_server/srv_pipe.h"
36 #define SERVER_TCP_LOW_PORT 1024
37 #define SERVER_TCP_HIGH_PORT 1300
39 static NTSTATUS auth_anonymous_session_info(TALLOC_CTX *mem_ctx,
40 struct auth_session_info **session_info)
44 status = make_session_info_guest(mem_ctx, session_info);
45 if (!NT_STATUS_IS_OK(status)) {
52 /* Creates a pipes_struct and initializes it with the information
53 * sent from the client */
54 static int make_server_pipes_struct(TALLOC_CTX *mem_ctx,
55 struct messaging_context *msg_ctx,
56 const char *pipe_name,
57 enum dcerpc_transport_t transport,
58 bool ncalrpc_as_system,
59 const struct tsocket_address *local_address,
60 const struct tsocket_address *remote_address,
61 struct auth_session_info *session_info,
62 struct pipes_struct **_p,
65 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 struct auth_user_info_dc *auth_user_info_dc;
84 struct auth_serversupplied_info *server_info;
85 struct netr_SamInfo3 *info3;
87 /* Fake up an auth_user_info_dc for now, to make an info3, to make the session_info structure */
88 auth_user_info_dc = talloc_zero(p, struct auth_user_info_dc);
89 if (!auth_user_info_dc) {
95 auth_user_info_dc->num_sids = session_info->security_token->num_sids;
96 auth_user_info_dc->sids = session_info->security_token->sids;
97 auth_user_info_dc->info = session_info->info;
98 auth_user_info_dc->user_session_key = session_info->session_key;
100 /* This creates the input structure that make_server_info_info3 is looking for */
101 status = auth_convert_user_info_dc_saminfo3(p, auth_user_info_dc,
104 if (!NT_STATUS_IS_OK(status)) {
105 DEBUG(1, ("Failed to convert auth_user_info_dc into netr_SamInfo3\n"));
111 status = make_server_info_info3(p,
112 info3->base.account_name.string,
113 info3->base.domain.string,
114 &server_info, info3);
115 if (!NT_STATUS_IS_OK(status)) {
116 DEBUG(1, ("Failed to init server info\n"));
123 * Some internal functions need a local token to determine access to
126 status = create_local_token(p, server_info, &session_info->session_key, info3->base.account_name.string,
128 talloc_free(server_info);
129 if (!NT_STATUS_IS_OK(status)) {
130 DEBUG(1, ("Failed to init local auth token\n"));
141 /* Start listening on the appropriate unix socket and setup all is needed to
142 * dispatch requests to the pipes rpc implementation */
144 struct dcerpc_ncacn_listen_state {
145 struct ndr_syntax_id syntax_id;
153 struct tevent_context *ev_ctx;
154 struct messaging_context *msg_ctx;
155 dcerpc_ncacn_disconnect_fn disconnect_fn;
158 static void named_pipe_listener(struct tevent_context *ev,
159 struct tevent_fd *fde,
163 int create_named_pipe_socket(const char *pipe_name)
169 * As lp_ncalrpc_dir() should have 0755, but
170 * lp_ncalrpc_dir()/np should have 0700, we need to
171 * create lp_ncalrpc_dir() first.
173 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) {
174 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
175 lp_ncalrpc_dir(), strerror(errno)));
179 np_dir = talloc_asprintf(talloc_tos(), "%s/np", lp_ncalrpc_dir());
181 DEBUG(0, ("Out of memory\n"));
185 if (!directory_create_or_exist(np_dir, geteuid(), 0700)) {
186 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
187 np_dir, strerror(errno)));
191 fd = create_pipe_sock(np_dir, pipe_name, 0700);
193 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
198 DEBUG(10, ("Openened pipe socket fd %d for %s\n", fd, pipe_name));
205 bool setup_named_pipe_socket(const char *pipe_name,
206 struct tevent_context *ev_ctx,
207 struct messaging_context *msg_ctx)
209 struct dcerpc_ncacn_listen_state *state;
210 struct tevent_fd *fde;
212 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
214 DEBUG(0, ("Out of memory\n"));
217 state->ep.name = talloc_strdup(state, pipe_name);
218 if (state->ep.name == NULL) {
219 DEBUG(0, ("Out of memory\n"));
222 state->fd = create_named_pipe_socket(pipe_name);
223 if (state->fd == -1) {
227 state->ev_ctx = ev_ctx;
228 state->msg_ctx = msg_ctx;
230 DEBUG(10, ("Openened pipe socket fd %d for %s\n",
231 state->fd, pipe_name));
233 fde = tevent_add_fd(ev_ctx,
234 state, state->fd, TEVENT_FD_READ,
235 named_pipe_listener, state);
237 DEBUG(0, ("Failed to add event handler!\n"));
241 tevent_fd_set_auto_close(fde);
245 if (state->fd != -1) {
252 static void named_pipe_listener(struct tevent_context *ev,
253 struct tevent_fd *fde,
257 struct dcerpc_ncacn_listen_state *state =
258 talloc_get_type_abort(private_data,
259 struct dcerpc_ncacn_listen_state);
260 struct sockaddr_un sunaddr;
264 /* TODO: should we have a limit to the number of clients ? */
266 len = sizeof(sunaddr);
268 sd = accept(state->fd,
269 (struct sockaddr *)(void *)&sunaddr, &len);
272 if (errno != EINTR) {
273 DEBUG(6, ("Failed to get a valid socket [%s]\n",
279 DEBUG(6, ("Accepted socket %d\n", sd));
281 named_pipe_accept_function(state->ev_ctx,
288 /* This is the core of the rpc server.
289 * Accepts connections from clients and process requests using the appropriate
290 * dispatcher table. */
292 struct named_pipe_client {
293 const char *pipe_name;
295 struct tevent_context *ev;
296 struct messaging_context *msg_ctx;
299 uint16_t device_state;
300 uint64_t allocation_size;
302 struct tstream_context *tstream;
304 struct tsocket_address *client;
306 struct tsocket_address *server;
309 struct auth_session_info *session_info;
311 struct pipes_struct *p;
313 struct tevent_queue *write_queue;
319 static void named_pipe_accept_done(struct tevent_req *subreq);
321 void named_pipe_accept_function(struct tevent_context *ev_ctx,
322 struct messaging_context *msg_ctx,
323 const char *pipe_name, int fd)
325 struct named_pipe_client *npc;
326 struct tstream_context *plain;
327 struct tevent_req *subreq;
330 npc = talloc_zero(ev_ctx, struct named_pipe_client);
332 DEBUG(0, ("Out of memory!\n"));
337 npc->pipe_name = talloc_strdup(npc, pipe_name);
338 if (npc->pipe_name == NULL) {
339 DEBUG(0, ("Out of memory!\n"));
345 npc->msg_ctx = msg_ctx;
347 /* make sure socket is in NON blocking state */
348 ret = set_blocking(fd, false);
350 DEBUG(2, ("Failed to make socket non-blocking\n"));
356 ret = tstream_bsd_existing_socket(npc, fd, &plain);
358 DEBUG(2, ("Failed to create tstream socket\n"));
364 npc->file_type = FILE_TYPE_MESSAGE_MODE_PIPE;
365 npc->device_state = 0xff | 0x0400 | 0x0100;
366 npc->allocation_size = 4096;
368 subreq = tstream_npa_accept_existing_send(npc, npc->ev, plain,
371 npc->allocation_size);
373 DEBUG(2, ("Failed to start async accept procedure\n"));
378 tevent_req_set_callback(subreq, named_pipe_accept_done, npc);
381 static void named_pipe_packet_process(struct tevent_req *subreq);
382 static void named_pipe_packet_done(struct tevent_req *subreq);
384 static void named_pipe_accept_done(struct tevent_req *subreq)
386 struct auth_session_info_transport *session_info_transport;
387 struct named_pipe_client *npc =
388 tevent_req_callback_data(subreq, struct named_pipe_client);
392 ret = tstream_npa_accept_existing_recv(subreq, &error, npc,
398 &session_info_transport);
400 npc->session_info = talloc_move(npc, &session_info_transport->session_info);
404 DEBUG(2, ("Failed to accept named pipe connection! (%s)\n",
410 ret = make_server_pipes_struct(npc,
412 npc->pipe_name, NCACN_NP,
413 false, npc->server, npc->client, npc->session_info,
416 DEBUG(2, ("Failed to create pipes_struct! (%s)\n",
421 npc->write_queue = tevent_queue_create(npc, "np_server_write_queue");
422 if (!npc->write_queue) {
423 DEBUG(2, ("Failed to set up write queue!\n"));
427 /* And now start receiving and processing packets */
428 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
430 DEBUG(2, ("Failed to start receving packets\n"));
433 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
437 DEBUG(2, ("Fatal error. Terminating client(%s) connection!\n",
439 /* terminate client connection */
444 static void named_pipe_packet_process(struct tevent_req *subreq)
446 struct named_pipe_client *npc =
447 tevent_req_callback_data(subreq, struct named_pipe_client);
448 struct _output_data *out = &npc->p->out_data;
449 DATA_BLOB recv_buffer = data_blob_null;
450 struct ncacn_packet *pkt;
459 status = dcerpc_read_ncacn_packet_recv(subreq, npc, &pkt, &recv_buffer);
461 if (!NT_STATUS_IS_OK(status)) {
465 data_left = recv_buffer.length;
466 data = (char *)recv_buffer.data;
470 data_used = process_incoming_data(npc->p, data, data_left);
472 DEBUG(3, ("Failed to process dceprc request!\n"));
473 status = NT_STATUS_UNEXPECTED_IO_ERROR;
477 data_left -= data_used;
481 /* Do not leak this buffer, npc is a long lived context */
482 talloc_free(recv_buffer.data);
485 /* this is needed because of the way DCERPC Binds work in
486 * the RPC marshalling code */
487 to_send = out->frag.length - out->current_pdu_sent;
490 npc->iov = talloc_zero(npc, struct iovec);
492 status = NT_STATUS_NO_MEMORY;
497 npc->iov[0].iov_base = out->frag.data
498 + out->current_pdu_sent;
499 npc->iov[0].iov_len = to_send;
501 out->current_pdu_sent += to_send;
504 /* this condition is false for bind packets, or when we haven't
505 * yet got a full request, and need to wait for more data from
507 while (out->data_sent_length < out->rdata.length) {
509 ok = create_next_pdu(npc->p);
511 DEBUG(3, ("Failed to create next PDU!\n"));
512 status = NT_STATUS_UNEXPECTED_IO_ERROR;
516 npc->iov = talloc_realloc(npc, npc->iov,
517 struct iovec, npc->count + 1);
519 status = NT_STATUS_NO_MEMORY;
523 npc->iov[npc->count].iov_base = out->frag.data;
524 npc->iov[npc->count].iov_len = out->frag.length;
529 /* we still don't have a complete request, go back and wait for more
531 if (npc->count == 0) {
532 /* Wait for the next packet */
533 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
535 DEBUG(2, ("Failed to start receving packets\n"));
536 status = NT_STATUS_NO_MEMORY;
539 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
543 DEBUG(10, ("Sending %u fragments in a total of %u bytes\n",
544 (unsigned int)npc->count,
545 (unsigned int)npc->p->out_data.data_sent_length));
547 for (i = 0; i < npc->count; i++) {
548 DEBUG(10, ("Sending PDU number: %d, PDU Length: %u\n",
550 (unsigned int)npc->iov[i].iov_len));
551 dump_data(11, (const uint8_t *)npc->iov[i].iov_base,
552 npc->iov[i].iov_len);
554 subreq = tstream_writev_queue_send(npc,
561 DEBUG(2, ("Failed to send packet\n"));
562 status = NT_STATUS_NO_MEMORY;
565 tevent_req_set_callback(subreq, named_pipe_packet_done, npc);
571 DEBUG(2, ("Fatal error(%s). "
572 "Terminating client(%s) connection!\n",
573 nt_errstr(status), npc->client_name));
574 /* terminate client connection */
579 static void named_pipe_packet_done(struct tevent_req *subreq)
581 struct named_pipe_client *npc =
582 tevent_req_callback_data(subreq, struct named_pipe_client);
586 ret = tstream_writev_queue_recv(subreq, &sys_errno);
589 DEBUG(2, ("Writev failed!\n"));
593 if (tevent_queue_length(npc->write_queue) > 0) {
597 /* clear out any data that may have been left around */
599 TALLOC_FREE(npc->iov);
600 data_blob_free(&npc->p->in_data.data);
601 data_blob_free(&npc->p->out_data.frag);
602 data_blob_free(&npc->p->out_data.rdata);
604 talloc_free_children(npc->p->mem_ctx);
606 /* Wait for the next packet */
607 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
609 DEBUG(2, ("Failed to start receving packets\n"));
613 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
617 DEBUG(2, ("Fatal error(%s). "
618 "Terminating client(%s) connection!\n",
619 strerror(sys_errno), npc->client_name));
620 /* terminate client connection */
625 static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
626 struct messaging_context *msg_ctx,
627 enum dcerpc_transport_t transport,
629 struct tsocket_address *cli_addr,
630 struct tsocket_address *srv_addr,
632 dcerpc_ncacn_disconnect_fn fn);
634 /********************************************************************
635 * Start listening on the tcp/ip socket
636 ********************************************************************/
638 static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
639 struct tevent_fd *fde,
643 int create_tcpip_socket(const struct sockaddr_storage *ifss, uint16_t *port)
650 for (i = SERVER_TCP_LOW_PORT; i <= SERVER_TCP_HIGH_PORT; i++) {
651 fd = open_socket_in(SOCK_STREAM,
662 fd = open_socket_in(SOCK_STREAM,
669 DEBUG(0, ("Failed to create socket on port %u!\n", *port));
673 DEBUG(10, ("Opened tcpip socket fd %d for port %u\n", fd, *port));
678 uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context *ev_ctx,
679 struct messaging_context *msg_ctx,
680 const struct sockaddr_storage *ifss,
683 struct dcerpc_ncacn_listen_state *state;
684 struct tevent_fd *fde;
687 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
689 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Out of memory\n"));
694 state->ep.port = port;
695 state->disconnect_fn = NULL;
697 state->fd = create_tcpip_socket(ifss, &state->ep.port);
698 if (state->fd == -1) {
702 state->ev_ctx = ev_ctx;
703 state->msg_ctx = msg_ctx;
705 /* ready to listen */
706 set_socket_options(state->fd, "SO_KEEPALIVE");
707 set_socket_options(state->fd, lp_socket_options());
709 /* Set server socket to non-blocking for the accept. */
710 set_blocking(state->fd, false);
712 rc = listen(state->fd, SMBD_LISTEN_BACKLOG);
714 DEBUG(0,("setup_tcpip_socket: listen - %s\n", strerror(errno)));
718 DEBUG(10, ("setup_tcpip_socket: openened socket fd %d for port %u\n",
719 state->fd, state->ep.port));
721 fde = tevent_add_fd(state->ev_ctx,
725 dcerpc_ncacn_tcpip_listener,
728 DEBUG(0, ("setup_tcpip_socket: Failed to add event handler!\n"));
732 tevent_fd_set_auto_close(fde);
734 return state->ep.port;
736 if (state->fd != -1) {
744 static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
745 struct tevent_fd *fde,
749 struct dcerpc_ncacn_listen_state *state =
750 talloc_get_type_abort(private_data,
751 struct dcerpc_ncacn_listen_state);
752 struct tsocket_address *cli_addr = NULL;
753 struct tsocket_address *srv_addr = NULL;
754 struct sockaddr_storage addr;
755 socklen_t in_addrlen = sizeof(addr);
759 s = accept(state->fd, (struct sockaddr *)(void *) &addr, &in_addrlen);
761 if (errno != EINTR) {
762 DEBUG(0,("tcpip_listener accept: %s\n",
768 rc = tsocket_address_bsd_from_sockaddr(state,
769 (struct sockaddr *)(void *) &addr,
777 rc = getsockname(s, (struct sockaddr *)(void *) &addr, &in_addrlen);
783 rc = tsocket_address_bsd_from_sockaddr(state,
784 (struct sockaddr *)(void *) &addr,
792 DEBUG(6, ("tcpip_listener: Accepted socket %d\n", s));
794 dcerpc_ncacn_accept(state->ev_ctx,
804 /********************************************************************
805 * Start listening on the ncalrpc socket
806 ********************************************************************/
808 static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
809 struct tevent_fd *fde,
813 bool setup_dcerpc_ncalrpc_socket(struct tevent_context *ev_ctx,
814 struct messaging_context *msg_ctx,
816 dcerpc_ncacn_disconnect_fn fn)
818 struct dcerpc_ncacn_listen_state *state;
819 struct tevent_fd *fde;
821 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
823 DEBUG(0, ("Out of memory\n"));
828 state->disconnect_fn = fn;
833 state->ep.name = talloc_strdup(state, name);
835 if (state->ep.name == NULL) {
836 DEBUG(0, ("Out of memory\n"));
841 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) {
842 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
843 lp_ncalrpc_dir(), strerror(errno)));
847 state->fd = create_pipe_sock(lp_ncalrpc_dir(), name, 0755);
848 if (state->fd == -1) {
849 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
850 lp_ncalrpc_dir(), name));
854 DEBUG(10, ("Openened pipe socket fd %d for %s\n", state->fd, name));
856 state->ev_ctx = ev_ctx;
857 state->msg_ctx = msg_ctx;
859 /* Set server socket to non-blocking for the accept. */
860 set_blocking(state->fd, false);
862 fde = tevent_add_fd(state->ev_ctx,
866 dcerpc_ncalrpc_listener,
869 DEBUG(0, ("Failed to add event handler for ncalrpc!\n"));
873 tevent_fd_set_auto_close(fde);
877 if (state->fd != -1) {
885 static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
886 struct tevent_fd *fde,
890 struct dcerpc_ncacn_listen_state *state =
891 talloc_get_type_abort(private_data,
892 struct dcerpc_ncacn_listen_state);
893 struct tsocket_address *cli_addr = NULL;
894 struct sockaddr_un sunaddr;
895 struct sockaddr *addr = (struct sockaddr *)(void *)&sunaddr;
896 socklen_t len = sizeof(sunaddr);
900 ZERO_STRUCT(sunaddr);
902 sd = accept(state->fd, addr, &len);
904 if (errno != EINTR) {
905 DEBUG(0, ("ncalrpc accept() failed: %s\n", strerror(errno)));
910 rc = tsocket_address_bsd_from_sockaddr(state,
918 DEBUG(10, ("Accepted ncalrpc socket %d\n", sd));
920 dcerpc_ncacn_accept(state->ev_ctx,
925 state->disconnect_fn);
928 struct dcerpc_ncacn_conn {
929 enum dcerpc_transport_t transport;
933 struct pipes_struct *p;
934 dcerpc_ncacn_disconnect_fn disconnect_fn;
936 struct tevent_context *ev_ctx;
937 struct messaging_context *msg_ctx;
939 struct tstream_context *tstream;
940 struct tevent_queue *send_queue;
942 struct tsocket_address *client;
944 struct tsocket_address *server;
946 struct auth_session_info *session_info;
952 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq);
953 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq);
955 static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
956 struct messaging_context *msg_ctx,
957 enum dcerpc_transport_t transport,
959 struct tsocket_address *cli_addr,
960 struct tsocket_address *srv_addr,
962 dcerpc_ncacn_disconnect_fn fn) {
963 struct dcerpc_ncacn_conn *ncacn_conn;
964 struct tevent_req *subreq;
965 bool system_user = false;
972 DEBUG(10, ("dcerpc_ncacn_accept\n"));
974 ncacn_conn = talloc_zero(ev_ctx, struct dcerpc_ncacn_conn);
975 if (ncacn_conn == NULL) {
976 DEBUG(0, ("Out of memory!\n"));
981 ncacn_conn->transport = transport;
982 ncacn_conn->ev_ctx = ev_ctx;
983 ncacn_conn->msg_ctx = msg_ctx;
984 ncacn_conn->sock = s;
985 ncacn_conn->disconnect_fn = fn;
987 ncacn_conn->client = talloc_move(ncacn_conn, &cli_addr);
988 if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
989 ncacn_conn->client_name =
990 tsocket_address_inet_addr_string(ncacn_conn->client,
993 ncacn_conn->client_name =
994 tsocket_address_unix_path(ncacn_conn->client,
997 if (ncacn_conn->client_name == NULL) {
998 DEBUG(0, ("Out of memory!\n"));
999 talloc_free(ncacn_conn);
1004 if (srv_addr != NULL) {
1005 ncacn_conn->server = talloc_move(ncacn_conn, &srv_addr);
1007 ncacn_conn->server_name =
1008 tsocket_address_inet_addr_string(ncacn_conn->server,
1010 if (ncacn_conn->server_name == NULL) {
1011 DEBUG(0, ("Out of memory!\n"));
1012 talloc_free(ncacn_conn);
1018 switch (transport) {
1020 pipe_name = tsocket_address_string(ncacn_conn->client,
1022 if (pipe_name == NULL) {
1024 talloc_free(ncacn_conn);
1030 rc = sys_getpeereid(s, &uid);
1032 DEBUG(2, ("Failed to get ncalrpc connecting uid!"));
1034 if (uid == sec_initial_uid()) {
1039 pipe_name = talloc_strdup(ncacn_conn,
1041 if (pipe_name == NULL) {
1043 talloc_free(ncacn_conn);
1048 DEBUG(0, ("unknown dcerpc transport: %u!\n",
1050 talloc_free(ncacn_conn);
1055 rc = set_blocking(s, false);
1057 DEBUG(2, ("Failed to set dcerpc socket to non-blocking\n"));
1058 talloc_free(ncacn_conn);
1064 * As soon as we have tstream_bsd_existing_socket set up it will
1065 * take care of closing the socket.
1067 rc = tstream_bsd_existing_socket(ncacn_conn, s, &ncacn_conn->tstream);
1069 DEBUG(2, ("Failed to create tstream socket for dcerpc\n"));
1070 talloc_free(ncacn_conn);
1075 if (ncacn_conn->session_info == NULL) {
1076 status = auth_anonymous_session_info(ncacn_conn,
1077 &ncacn_conn->session_info);
1078 if (!NT_STATUS_IS_OK(status)) {
1079 DEBUG(2, ("Failed to create "
1080 "auth_anonymous_session_info - %s\n",
1081 nt_errstr(status)));
1082 talloc_free(ncacn_conn);
1087 rc = make_server_pipes_struct(ncacn_conn,
1088 ncacn_conn->msg_ctx,
1090 ncacn_conn->transport,
1094 ncacn_conn->session_info,
1098 DEBUG(2, ("Failed to create pipe struct - %s",
1099 strerror(sys_errno)));
1100 talloc_free(ncacn_conn);
1104 ncacn_conn->send_queue = tevent_queue_create(ncacn_conn,
1105 "dcerpc send queue");
1106 if (ncacn_conn->send_queue == NULL) {
1107 DEBUG(0, ("Out of memory!\n"));
1108 talloc_free(ncacn_conn);
1112 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1114 ncacn_conn->tstream);
1115 if (subreq == NULL) {
1116 DEBUG(2, ("Failed to send ncacn packet\n"));
1117 talloc_free(ncacn_conn);
1121 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1123 DEBUG(10, ("dcerpc_ncacn_accept done\n"));
1128 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq)
1130 struct dcerpc_ncacn_conn *ncacn_conn =
1131 tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
1133 struct _output_data *out = &ncacn_conn->p->out_data;
1134 DATA_BLOB recv_buffer = data_blob_null;
1135 struct ncacn_packet *pkt;
1143 status = dcerpc_read_ncacn_packet_recv(subreq, ncacn_conn, &pkt, &recv_buffer);
1144 TALLOC_FREE(subreq);
1145 if (!NT_STATUS_IS_OK(status)) {
1146 if (ncacn_conn->disconnect_fn != NULL) {
1147 ok = ncacn_conn->disconnect_fn(ncacn_conn->p);
1149 DEBUG(3, ("Failed to call disconnect function\n"));
1155 data_left = recv_buffer.length;
1156 data = (char *) recv_buffer.data;
1159 data_used = process_incoming_data(ncacn_conn->p, data, data_left);
1160 if (data_used < 0) {
1161 DEBUG(3, ("Failed to process dcerpc request!\n"));
1162 status = NT_STATUS_UNEXPECTED_IO_ERROR;
1166 data_left -= data_used;
1170 /* Do not leak this buffer */
1171 talloc_free(recv_buffer.data);
1175 * This is needed because of the way DCERPC binds work in the RPC
1178 to_send = out->frag.length - out->current_pdu_sent;
1181 DEBUG(10, ("Current_pdu_len = %u, "
1182 "current_pdu_sent = %u "
1183 "Returning %u bytes\n",
1184 (unsigned int)out->frag.length,
1185 (unsigned int)out->current_pdu_sent,
1186 (unsigned int)to_send));
1188 ncacn_conn->iov = talloc_zero(ncacn_conn, struct iovec);
1189 if (ncacn_conn->iov == NULL) {
1190 status = NT_STATUS_NO_MEMORY;
1191 DEBUG(3, ("Out of memory!\n"));
1194 ncacn_conn->count = 1;
1196 ncacn_conn->iov[0].iov_base = out->frag.data
1197 + out->current_pdu_sent;
1198 ncacn_conn->iov[0].iov_len = to_send;
1200 out->current_pdu_sent += to_send;
1204 * This condition is false for bind packets, or when we haven't yet got
1205 * a full request, and need to wait for more data from the client
1207 while (out->data_sent_length < out->rdata.length) {
1208 ok = create_next_pdu(ncacn_conn->p);
1210 DEBUG(3, ("Failed to create next PDU!\n"));
1211 status = NT_STATUS_UNEXPECTED_IO_ERROR;
1215 ncacn_conn->iov = talloc_realloc(ncacn_conn,
1218 ncacn_conn->count + 1);
1219 if (ncacn_conn->iov == NULL) {
1220 DEBUG(3, ("Out of memory!\n"));
1221 status = NT_STATUS_NO_MEMORY;
1225 ncacn_conn->iov[ncacn_conn->count].iov_base = out->frag.data;
1226 ncacn_conn->iov[ncacn_conn->count].iov_len = out->frag.length;
1228 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
1229 (unsigned int) ncacn_conn->count,
1230 (unsigned int) ncacn_conn->iov[ncacn_conn->count].iov_len));
1231 dump_data(11, (const uint8_t *) ncacn_conn->iov[ncacn_conn->count].iov_base,
1232 ncacn_conn->iov[ncacn_conn->count].iov_len);
1233 ncacn_conn->count++;
1237 * We still don't have a complete request, go back and wait for more
1240 if (ncacn_conn->count == 0) {
1241 /* Wait for the next packet */
1242 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1244 ncacn_conn->tstream);
1245 if (subreq == NULL) {
1246 DEBUG(2, ("Failed to start receving packets\n"));
1247 status = NT_STATUS_NO_MEMORY;
1250 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1254 DEBUG(10, ("Sending a total of %u bytes\n",
1255 (unsigned int)ncacn_conn->p->out_data.data_sent_length));
1257 subreq = tstream_writev_queue_send(ncacn_conn,
1259 ncacn_conn->tstream,
1260 ncacn_conn->send_queue,
1263 if (subreq == NULL) {
1264 DEBUG(2, ("Failed to send packet\n"));
1265 status = NT_STATUS_NO_MEMORY;
1269 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_done, ncacn_conn);
1273 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1274 ncacn_conn->client_name, nt_errstr(status)));
1276 /* Terminate client connection */
1277 talloc_free(ncacn_conn);
1281 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq)
1283 struct dcerpc_ncacn_conn *ncacn_conn =
1284 tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
1285 NTSTATUS status = NT_STATUS_OK;
1289 rc = tstream_writev_queue_recv(subreq, &sys_errno);
1290 TALLOC_FREE(subreq);
1292 DEBUG(2, ("Writev failed!\n"));
1293 status = map_nt_error_from_unix(sys_errno);
1297 /* clear out any data that may have been left around */
1298 ncacn_conn->count = 0;
1299 TALLOC_FREE(ncacn_conn->iov);
1300 data_blob_free(&ncacn_conn->p->in_data.data);
1301 data_blob_free(&ncacn_conn->p->out_data.frag);
1302 data_blob_free(&ncacn_conn->p->out_data.rdata);
1304 talloc_free_children(ncacn_conn->p->mem_ctx);
1306 /* Wait for the next packet */
1307 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1309 ncacn_conn->tstream);
1310 if (subreq == NULL) {
1311 DEBUG(2, ("Failed to start receving packets\n"));
1312 status = NT_STATUS_NO_MEMORY;
1316 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1320 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1321 ncacn_conn->client_name, nt_errstr(status)));
1323 /* Terminate client connection */
1324 talloc_free(ncacn_conn);
1328 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */