2 Unix SMB/Netbios implementation.
3 Generic infrstructure for RPC Daemons
4 Copyright (C) Simo Sorce 2010
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "rpc_server/rpc_server.h"
23 #include "librpc/gen_ndr/netlogon.h"
24 #include "librpc/gen_ndr/auth.h"
25 #include "registry/reg_parse_prs.h"
26 #include "lib/tsocket/tsocket.h"
27 #include "libcli/named_pipe_auth/npa_tstream.h"
28 #include "../auth/auth_sam_reply.h"
30 #define SERVER_TCP_LOW_PORT 1024
31 #define SERVER_TCP_HIGH_PORT 1300
33 static NTSTATUS auth_anonymous_session_info(TALLOC_CTX *mem_ctx,
34 struct auth_session_info_transport **session_info)
36 struct auth_session_info_transport *i;
37 struct auth_serversupplied_info *s;
38 struct auth_user_info_dc *u;
39 union netr_Validation val;
42 i = talloc_zero(mem_ctx, struct auth_session_info_transport);
44 return NT_STATUS_NO_MEMORY;
47 status = make_server_info_guest(i, &s);
48 if (!NT_STATUS_IS_OK(status)) {
52 i->security_token = s->security_token;
53 i->session_key = s->user_session_key;
57 status = make_user_info_dc_netlogon_validation(mem_ctx,
62 if (!NT_STATUS_IS_OK(status)) {
63 DEBUG(0, ("conversion of info3 into user_info_dc failed!\n"));
66 i->info = talloc_move(i, &u->info);
74 /* Creates a pipes_struct and initializes it with the information
75 * sent from the client */
76 static int make_server_pipes_struct(TALLOC_CTX *mem_ctx,
77 const char *pipe_name,
78 const struct ndr_syntax_id id,
79 const char *client_address,
80 const char *server_address,
81 struct auth_session_info_transport *session_info,
82 struct pipes_struct **_p,
85 struct netr_SamInfo3 *info3;
86 struct auth_user_info_dc *auth_user_info_dc;
87 struct pipes_struct *p;
91 p = talloc_zero(mem_ctx, struct pipes_struct);
98 p->mem_ctx = talloc_named(p, 0, "pipe %s %p", pipe_name, p);
105 ok = init_pipe_handles(p, &id);
107 DEBUG(1, ("Failed to init handles\n"));
114 data_blob_free(&p->in_data.data);
115 data_blob_free(&p->in_data.pdu);
117 p->endian = RPC_LITTLE_ENDIAN;
119 /* Fake up an auth_user_info_dc for now, to make an info3, to make the session_info structure */
120 auth_user_info_dc = talloc_zero(p, struct auth_user_info_dc);
121 if (!auth_user_info_dc) {
127 auth_user_info_dc->num_sids = session_info->security_token->num_sids;
128 auth_user_info_dc->sids = session_info->security_token->sids;
129 auth_user_info_dc->info = session_info->info;
130 auth_user_info_dc->user_session_key = session_info->session_key;
132 /* This creates the input structure that make_server_info_info3 is looking for */
133 status = auth_convert_user_info_dc_saminfo3(p, auth_user_info_dc,
136 if (!NT_STATUS_IS_OK(status)) {
137 DEBUG(1, ("Failed to convert auth_user_info_dc into netr_SamInfo3\n"));
143 status = make_server_info_info3(p,
144 info3->base.account_name.string,
145 info3->base.domain.string,
146 &p->session_info, info3);
147 if (!NT_STATUS_IS_OK(status)) {
148 DEBUG(1, ("Failed to init server info\n"));
155 * Some internal functions need a local token to determine access to
158 status = create_local_token(p->session_info);
159 if (!NT_STATUS_IS_OK(status)) {
160 DEBUG(1, ("Failed to init local auth token\n"));
166 /* Now override the session_info->security_token with the exact
167 * security_token we were given from the other side,
168 * regardless of what we just calculated */
169 p->session_info->security_token = talloc_move(p->session_info, &session_info->security_token);
171 /* Also set the session key to the correct value */
172 p->session_info->user_session_key = session_info->session_key;
173 p->session_info->user_session_key.data = talloc_move(p->session_info, &session_info->session_key.data);
175 p->client_id = talloc_zero(p, struct client_address);
181 strlcpy(p->client_id->addr,
182 client_address, sizeof(p->client_id->addr));
183 p->client_id->name = talloc_strdup(p->client_id, client_address);
184 if (p->client_id->name == NULL) {
190 if (server_address != NULL) {
191 p->server_id = talloc_zero(p, struct client_address);
192 if (p->client_id == NULL) {
198 strlcpy(p->server_id->addr,
200 sizeof(p->server_id->addr));
202 p->server_id->name = talloc_strdup(p->server_id,
204 if (p->server_id->name == NULL) {
211 talloc_set_destructor(p, close_internal_rpc_pipe_hnd);
217 /* Add some helper functions to wrap the common ncacn packet reading functions
218 * until we can share more dcerpc code */
219 struct dcerpc_ncacn_read_packet_state {
220 struct ncacn_packet *pkt;
224 static void dcerpc_ncacn_read_packet_done(struct tevent_req *subreq);
226 static struct tevent_req *dcerpc_ncacn_read_packet_send(TALLOC_CTX *mem_ctx,
227 struct tevent_context *ev,
228 struct tstream_context *tstream)
230 struct dcerpc_ncacn_read_packet_state *state;
231 struct tevent_req *req, *subreq;
233 req = tevent_req_create(mem_ctx, &state,
234 struct dcerpc_ncacn_read_packet_state);
240 subreq = dcerpc_read_ncacn_packet_send(state, ev, tstream);
242 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
243 tevent_req_post(req, ev);
246 tevent_req_set_callback(subreq, dcerpc_ncacn_read_packet_done, req);
251 static void dcerpc_ncacn_read_packet_done(struct tevent_req *subreq)
253 struct tevent_req *req =
254 tevent_req_callback_data(subreq, struct tevent_req);
255 struct dcerpc_ncacn_read_packet_state *state =
256 tevent_req_data(req, struct dcerpc_ncacn_read_packet_state);
259 status = dcerpc_read_ncacn_packet_recv(subreq, state,
263 if (!NT_STATUS_IS_OK(status)) {
264 DEBUG(3, ("Failed to receive dceprc packet!\n"));
265 tevent_req_nterror(req, status);
269 tevent_req_done(req);
272 static NTSTATUS dcerpc_ncacn_read_packet_recv(struct tevent_req *req,
276 struct dcerpc_ncacn_read_packet_state *state =
277 tevent_req_data(req, struct dcerpc_ncacn_read_packet_state);
280 if (tevent_req_is_nterror(req, &status)) {
281 tevent_req_received(req);
285 buffer->data = talloc_move(mem_ctx, &state->buffer.data);
286 buffer->length = state->buffer.length;
288 tevent_req_received(req);
294 /* Start listening on the appropriate unix socket and setup all is needed to
295 * dispatch requests to the pipes rpc implementation */
297 struct dcerpc_ncacn_listen_state {
298 struct ndr_syntax_id syntax_id;
306 struct tevent_context *ev_ctx;
307 struct messaging_context *msg_ctx;
310 static void named_pipe_listener(struct tevent_context *ev,
311 struct tevent_fd *fde,
315 bool setup_named_pipe_socket(const char *pipe_name,
316 struct tevent_context *ev_ctx)
318 struct dcerpc_ncacn_listen_state *state;
319 struct tevent_fd *fde;
322 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
324 DEBUG(0, ("Out of memory\n"));
327 state->ep.name = talloc_strdup(state, pipe_name);
328 if (state->ep.name == NULL) {
329 DEBUG(0, ("Out of memory\n"));
334 np_dir = talloc_asprintf(state, "%s/np", lp_ncalrpc_dir());
336 DEBUG(0, ("Out of memory\n"));
340 if (!directory_create_or_exist(np_dir, geteuid(), 0700)) {
341 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
342 np_dir, strerror(errno)));
346 state->fd = create_pipe_sock(np_dir, pipe_name, 0700);
347 if (state->fd == -1) {
348 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
353 DEBUG(10, ("Openened pipe socket fd %d for %s\n",
354 state->fd, pipe_name));
356 fde = tevent_add_fd(ev_ctx,
357 state, state->fd, TEVENT_FD_READ,
358 named_pipe_listener, state);
360 DEBUG(0, ("Failed to add event handler!\n"));
364 tevent_fd_set_auto_close(fde);
368 if (state->fd != -1) {
375 static void named_pipe_accept_function(const char *pipe_name, int fd);
377 static void named_pipe_listener(struct tevent_context *ev,
378 struct tevent_fd *fde,
382 struct dcerpc_ncacn_listen_state *state =
383 talloc_get_type_abort(private_data,
384 struct dcerpc_ncacn_listen_state);
385 struct sockaddr_un sunaddr;
389 /* TODO: should we have a limit to the number of clients ? */
391 len = sizeof(sunaddr);
394 sd = accept(state->fd,
395 (struct sockaddr *)(void *)&sunaddr, &len);
396 if (errno != EINTR) break;
400 DEBUG(6, ("Failed to get a valid socket [%s]\n",
405 DEBUG(6, ("Accepted socket %d\n", sd));
407 named_pipe_accept_function(state->ep.name, sd);
411 /* This is the core of the rpc server.
412 * Accepts connections from clients and process requests using the appropriate
413 * dispatcher table. */
415 struct named_pipe_client {
416 const char *pipe_name;
417 struct ndr_syntax_id pipe_id;
419 struct tevent_context *ev;
420 struct messaging_context *msg_ctx;
423 uint16_t device_state;
424 uint64_t allocation_size;
426 struct tstream_context *tstream;
428 struct tsocket_address *client;
430 struct tsocket_address *server;
432 struct auth_session_info_transport *session_info;
434 struct pipes_struct *p;
436 struct tevent_queue *write_queue;
442 static void named_pipe_accept_done(struct tevent_req *subreq);
444 static void named_pipe_accept_function(const char *pipe_name, int fd)
446 struct ndr_syntax_id syntax;
447 struct named_pipe_client *npc;
448 struct tstream_context *plain;
449 struct tevent_req *subreq;
453 ok = is_known_pipename(pipe_name, &syntax);
455 DEBUG(1, ("Unknown pipe [%s]\n", pipe_name));
460 npc = talloc_zero(NULL, struct named_pipe_client);
462 DEBUG(0, ("Out of memory!\n"));
466 npc->pipe_name = pipe_name;
467 npc->pipe_id = syntax;
468 npc->ev = server_event_context();
469 npc->msg_ctx = server_messaging_context();
471 /* make sure socket is in NON blocking state */
472 ret = set_blocking(fd, false);
474 DEBUG(2, ("Failed to make socket non-blocking\n"));
480 ret = tstream_bsd_existing_socket(npc, fd, &plain);
482 DEBUG(2, ("Failed to create tstream socket\n"));
488 npc->file_type = FILE_TYPE_MESSAGE_MODE_PIPE;
489 npc->device_state = 0xff | 0x0400 | 0x0100;
490 npc->allocation_size = 4096;
492 subreq = tstream_npa_accept_existing_send(npc, npc->ev, plain,
495 npc->allocation_size);
497 DEBUG(2, ("Failed to start async accept procedure\n"));
502 tevent_req_set_callback(subreq, named_pipe_accept_done, npc);
505 static void named_pipe_packet_process(struct tevent_req *subreq);
506 static void named_pipe_packet_done(struct tevent_req *subreq);
508 static void named_pipe_accept_done(struct tevent_req *subreq)
510 struct named_pipe_client *npc =
511 tevent_req_callback_data(subreq, struct named_pipe_client);
512 const char *cli_addr;
516 ret = tstream_npa_accept_existing_recv(subreq, &error, npc,
525 DEBUG(2, ("Failed to accept named pipe connection! (%s)\n",
531 if (tsocket_address_is_inet(npc->client, "ip")) {
532 cli_addr = tsocket_address_inet_addr_string(npc->client,
534 if (cli_addr == NULL) {
542 ret = make_server_pipes_struct(npc,
543 npc->pipe_name, npc->pipe_id,
544 cli_addr, NULL, npc->session_info,
547 DEBUG(2, ("Failed to create pipes_struct! (%s)\n",
551 npc->p->msg_ctx = npc->msg_ctx;
553 npc->write_queue = tevent_queue_create(npc, "np_server_write_queue");
554 if (!npc->write_queue) {
555 DEBUG(2, ("Failed to set up write queue!\n"));
559 /* And now start receaving and processing packets */
560 subreq = dcerpc_ncacn_read_packet_send(npc, npc->ev, npc->tstream);
562 DEBUG(2, ("Failed to start receving packets\n"));
565 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
569 DEBUG(2, ("Fatal error. Terminating client(%s) connection!\n",
571 /* terminate client connection */
576 static void named_pipe_packet_process(struct tevent_req *subreq)
578 struct named_pipe_client *npc =
579 tevent_req_callback_data(subreq, struct named_pipe_client);
580 struct _output_data *out = &npc->p->out_data;
581 DATA_BLOB recv_buffer = data_blob_null;
589 status = dcerpc_ncacn_read_packet_recv(subreq, npc, &recv_buffer);
591 if (!NT_STATUS_IS_OK(status)) {
595 data_left = recv_buffer.length;
596 data = (char *)recv_buffer.data;
600 data_used = process_incoming_data(npc->p, data, data_left);
602 DEBUG(3, ("Failed to process dceprc request!\n"));
603 status = NT_STATUS_UNEXPECTED_IO_ERROR;
607 data_left -= data_used;
611 /* Do not leak this buffer, npc is a long lived context */
612 talloc_free(recv_buffer.data);
614 /* this is needed because of the way DCERPC Binds work in
615 * the RPC marshalling code */
616 to_send = out->frag.length - out->current_pdu_sent;
619 DEBUG(10, ("Current_pdu_len = %u, "
620 "current_pdu_sent = %u "
621 "Returning %u bytes\n",
622 (unsigned int)out->frag.length,
623 (unsigned int)out->current_pdu_sent,
624 (unsigned int)to_send));
626 npc->iov = talloc_zero(npc, struct iovec);
628 status = NT_STATUS_NO_MEMORY;
633 npc->iov[0].iov_base = out->frag.data
634 + out->current_pdu_sent;
635 npc->iov[0].iov_len = to_send;
637 out->current_pdu_sent += to_send;
640 /* this condition is false for bind packets, or when we haven't
641 * yet got a full request, and need to wait for more data from
643 while (out->data_sent_length < out->rdata.length) {
645 ok = create_next_pdu(npc->p);
647 DEBUG(3, ("Failed to create next PDU!\n"));
648 status = NT_STATUS_UNEXPECTED_IO_ERROR;
652 npc->iov = talloc_realloc(npc, npc->iov,
653 struct iovec, npc->count + 1);
655 status = NT_STATUS_NO_MEMORY;
659 npc->iov[npc->count].iov_base = out->frag.data;
660 npc->iov[npc->count].iov_len = out->frag.length;
662 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
663 (unsigned int)npc->count,
664 (unsigned int)npc->iov[npc->count].iov_len));
665 dump_data(11, (const uint8_t *)npc->iov[npc->count].iov_base,
666 npc->iov[npc->count].iov_len);
670 /* we still don't have a complete request, go back and wait for more
672 if (npc->count == 0) {
673 /* Wait for the next packet */
674 subreq = dcerpc_ncacn_read_packet_send(npc, npc->ev, npc->tstream);
676 DEBUG(2, ("Failed to start receving packets\n"));
677 status = NT_STATUS_NO_MEMORY;
680 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
684 DEBUG(10, ("Sending a total of %u bytes\n",
685 (unsigned int)npc->p->out_data.data_sent_length));
687 subreq = tstream_writev_queue_send(npc, npc->ev,
690 npc->iov, npc->count);
692 DEBUG(2, ("Failed to send packet\n"));
693 status = NT_STATUS_NO_MEMORY;
696 tevent_req_set_callback(subreq, named_pipe_packet_done, npc);
700 DEBUG(2, ("Fatal error(%s). "
701 "Terminating client(%s) connection!\n",
702 nt_errstr(status), npc->client_name));
703 /* terminate client connection */
708 static void named_pipe_packet_done(struct tevent_req *subreq)
710 struct named_pipe_client *npc =
711 tevent_req_callback_data(subreq, struct named_pipe_client);
715 ret = tstream_writev_queue_recv(subreq, &sys_errno);
718 DEBUG(2, ("Writev failed!\n"));
722 /* clear out any data that may have been left around */
724 TALLOC_FREE(npc->iov);
725 data_blob_free(&npc->p->in_data.data);
726 data_blob_free(&npc->p->out_data.frag);
727 data_blob_free(&npc->p->out_data.rdata);
729 /* Wait for the next packet */
730 subreq = dcerpc_ncacn_read_packet_send(npc, npc->ev, npc->tstream);
732 DEBUG(2, ("Failed to start receving packets\n"));
736 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
740 DEBUG(2, ("Fatal error(%s). "
741 "Terminating client(%s) connection!\n",
742 strerror(sys_errno), npc->client_name));
743 /* terminate client connection */
748 /********************************************************************
749 * Start listening on the tcp/ip socket
750 ********************************************************************/
752 static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
753 struct tevent_fd *fde,
757 uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context *ev_ctx,
758 struct messaging_context *msg_ctx,
759 struct ndr_syntax_id syntax_id,
760 const struct sockaddr_storage *ifss,
763 struct dcerpc_ncacn_listen_state *state;
764 struct tevent_fd *fde;
767 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
769 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Out of memory\n"));
773 state->syntax_id = syntax_id;
775 state->ep.port = port;
777 if (state->ep.port == 0) {
780 for (i = SERVER_TCP_LOW_PORT; i <= SERVER_TCP_HIGH_PORT; i++) {
781 state->fd = open_socket_in(SOCK_STREAM,
792 state->fd = open_socket_in(SOCK_STREAM,
798 if (state->fd == -1) {
799 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Failed to create "
800 "socket on port %u!\n", state->ep.port));
804 state->ev_ctx = ev_ctx;
805 state->msg_ctx = msg_ctx;
807 /* ready to listen */
808 set_socket_options(state->fd, "SO_KEEPALIVE");
809 set_socket_options(state->fd, lp_socket_options());
811 /* Set server socket to non-blocking for the accept. */
812 set_blocking(state->fd, false);
814 rc = listen(state->fd, SMBD_LISTEN_BACKLOG);
816 DEBUG(0,("setup_tcpip_socket: listen - %s\n", strerror(errno)));
820 DEBUG(10, ("setup_tcpip_socket: openened socket fd %d for port %u\n",
821 state->fd, state->ep.port));
823 fde = tevent_add_fd(state->ev_ctx,
827 dcerpc_ncacn_tcpip_listener,
830 DEBUG(0, ("setup_tcpip_socket: Failed to add event handler!\n"));
834 tevent_fd_set_auto_close(fde);
836 return state->ep.port;
838 if (state->fd != -1) {
846 static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
847 struct messaging_context *msg_ctx,
848 struct ndr_syntax_id syntax_id,
849 enum dcerpc_transport_t transport,
852 struct tsocket_address *cli_addr,
853 struct tsocket_address *srv_addr,
856 static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
857 struct tevent_fd *fde,
861 struct dcerpc_ncacn_listen_state *state =
862 talloc_get_type_abort(private_data,
863 struct dcerpc_ncacn_listen_state);
864 struct tsocket_address *cli_addr = NULL;
865 struct tsocket_address *srv_addr = NULL;
866 struct sockaddr_storage addr;
867 socklen_t in_addrlen = sizeof(addr);
872 s = accept(state->fd, (struct sockaddr *)(void *) &addr, &in_addrlen);
873 if (s == -1 && errno != EINTR) {
879 DEBUG(0,("tcpip_listener accept: %s\n",
884 rc = tsocket_address_bsd_from_sockaddr(state,
885 (struct sockaddr *)(void *) &addr,
893 rc = getsockname(s, (struct sockaddr *)(void *) &addr, &in_addrlen);
899 rc = tsocket_address_bsd_from_sockaddr(state,
900 (struct sockaddr *)(void *) &addr,
908 DEBUG(6, ("tcpip_listener: Accepted socket %d\n", s));
910 dcerpc_ncacn_accept(state->ev_ctx,
921 struct dcerpc_ncacn_conn {
922 struct ndr_syntax_id syntax_id;
924 enum dcerpc_transport_t transport;
933 struct pipes_struct *p;
935 struct tevent_context *ev_ctx;
936 struct messaging_context *msg_ctx;
938 struct tstream_context *tstream;
939 struct tevent_queue *send_queue;
941 struct tsocket_address *client;
943 struct tsocket_address *server;
945 struct auth_session_info_transport *session_info;
951 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq);
952 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq);
954 static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
955 struct messaging_context *msg_ctx,
956 struct ndr_syntax_id syntax_id,
957 enum dcerpc_transport_t transport,
960 struct tsocket_address *cli_addr,
961 struct tsocket_address *srv_addr,
963 struct dcerpc_ncacn_conn *ncacn_conn;
964 struct tevent_req *subreq;
966 const char *srv_str = NULL;
972 DEBUG(5, ("dcerpc_ncacn_accept\n"));
974 ncacn_conn = talloc_zero(ev_ctx, struct dcerpc_ncacn_conn);
975 if (ncacn_conn == NULL) {
976 DEBUG(0, ("dcerpc_ncacn_accept: Out of memory!\n"));
983 ncacn_conn->ep.port = port;
987 ncacn_conn->ep.name = talloc_strdup(ncacn_conn, name);
990 DEBUG(0, ("dcerpc_ncacn_accept_function: "
991 "unknown transport: %u!\n", transport));
992 talloc_free(ncacn_conn);
996 ncacn_conn->transport = transport;
997 ncacn_conn->syntax_id = syntax_id;
998 ncacn_conn->ev_ctx = ev_ctx;
999 ncacn_conn->msg_ctx = msg_ctx;
1000 ncacn_conn->sock = s;
1002 ncacn_conn->client = talloc_move(ncacn_conn, &cli_addr);
1003 if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
1004 ncacn_conn->client_name =
1005 tsocket_address_inet_addr_string(ncacn_conn->client,
1008 ncacn_conn->client_name =
1009 tsocket_address_unix_path(ncacn_conn->client,
1012 if (ncacn_conn->client_name == NULL) {
1013 DEBUG(0, ("dcerpc_ncacn_accept: Out of memory!\n"));
1014 talloc_free(ncacn_conn);
1019 if (srv_addr != NULL) {
1020 ncacn_conn->server = talloc_move(ncacn_conn, &srv_addr);
1022 ncacn_conn->server_name =
1023 tsocket_address_inet_addr_string(ncacn_conn->server,
1025 if (ncacn_conn->server_name == NULL) {
1026 DEBUG(0, ("dcerpc_ncacn_accept: Out of memory!\n"));
1027 talloc_free(ncacn_conn);
1033 rc = set_blocking(s, false);
1035 DEBUG(2, ("dcerpc_ncacn_accept: Failed to set socket to "
1037 talloc_free(ncacn_conn);
1043 * As soon as we have tstream_bsd_existing_socket set up it will take
1044 * care closing the socket.
1046 rc = tstream_bsd_existing_socket(ncacn_conn, s, &ncacn_conn->tstream);
1048 DEBUG(2, ("dcerpc_ncacn_accept: Failed to create tstream "
1050 talloc_free(ncacn_conn);
1055 switch(ncacn_conn->transport) {
1057 pipe_name = tsocket_address_string(ncacn_conn->client,
1061 pipe_name = talloc_strdup(ncacn_conn,
1062 ncacn_conn->ep.name);
1065 talloc_free(ncacn_conn);
1069 if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
1070 cli_str = ncacn_conn->client_name;
1075 if (ncacn_conn->server != NULL) {
1076 if (tsocket_address_is_inet(ncacn_conn->server, "ip")) {
1077 srv_str = ncacn_conn->server_name;
1083 if (ncacn_conn->session_info == NULL) {
1084 status = auth_anonymous_session_info(ncacn_conn,
1085 &ncacn_conn->session_info);
1086 if (!NT_STATUS_IS_OK(status)) {
1087 DEBUG(2, ("dcerpc_ncacn_accept: Failed to create "
1088 "auth_anonymous_session_info - %s\n",
1089 nt_errstr(status)));
1090 talloc_free(ncacn_conn);
1095 rc = make_server_pipes_struct(ncacn_conn,
1097 ncacn_conn->syntax_id,
1100 ncacn_conn->session_info,
1104 DEBUG(2, ("dcerpc_ncacn_accept: Failed to create pipe "
1105 "struct - %s", strerror(sys_errno)));
1106 talloc_free(ncacn_conn);
1110 ncacn_conn->send_queue = tevent_queue_create(ncacn_conn,
1111 "dcerpc_tcpip_accept_function");
1112 if (ncacn_conn->send_queue == NULL) {
1113 DEBUG(0, ("dcerpc_ncacn_accept_function: Out of memory!\n"));
1114 talloc_free(ncacn_conn);
1118 subreq = dcerpc_ncacn_read_packet_send(ncacn_conn,
1120 ncacn_conn->tstream);
1121 if (subreq == NULL) {
1122 DEBUG(2, ("dcerpc_ncacn_accept: Failed to send ncacn "
1124 talloc_free(ncacn_conn);
1128 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1130 DEBUG(5, ("dcerpc_ncacn_accept done\n"));
1135 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq)
1137 struct dcerpc_ncacn_conn *ncacn_conn =
1138 tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
1140 struct _output_data *out = &ncacn_conn->p->out_data;
1141 DATA_BLOB recv_buffer = data_blob_null;
1149 status = dcerpc_ncacn_read_packet_recv(subreq, ncacn_conn, &recv_buffer);
1150 TALLOC_FREE(subreq);
1151 if (!NT_STATUS_IS_OK(status)) {
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);
1174 * This is needed because of the way DCERPC binds work in the RPC
1177 to_send = out->frag.length - out->current_pdu_sent;
1180 DEBUG(10, ("Current_pdu_len = %u, "
1181 "current_pdu_sent = %u "
1182 "Returning %u bytes\n",
1183 (unsigned int)out->frag.length,
1184 (unsigned int)out->current_pdu_sent,
1185 (unsigned int)to_send));
1187 ncacn_conn->iov = talloc_zero(ncacn_conn, struct iovec);
1188 if (ncacn_conn->iov == NULL) {
1189 status = NT_STATUS_NO_MEMORY;
1192 ncacn_conn->count = 1;
1194 ncacn_conn->iov[0].iov_base = out->frag.data
1195 + out->current_pdu_sent;
1196 ncacn_conn->iov[0].iov_len = to_send;
1198 out->current_pdu_sent += to_send;
1202 * This condition is false for bind packets, or when we haven't yet got
1203 * a full request, and need to wait for more data from the client
1205 while (out->data_sent_length < out->rdata.length) {
1206 ok = create_next_pdu(ncacn_conn->p);
1208 DEBUG(3, ("Failed to create next PDU!\n"));
1209 status = NT_STATUS_UNEXPECTED_IO_ERROR;
1213 ncacn_conn->iov = talloc_realloc(ncacn_conn,
1216 ncacn_conn->count + 1);
1217 if (ncacn_conn->iov == NULL) {
1218 status = NT_STATUS_NO_MEMORY;
1222 ncacn_conn->iov[ncacn_conn->count].iov_base = out->frag.data;
1223 ncacn_conn->iov[ncacn_conn->count].iov_len = out->frag.length;
1225 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
1226 (unsigned int) ncacn_conn->count,
1227 (unsigned int) ncacn_conn->iov[ncacn_conn->count].iov_len));
1228 dump_data(11, (const uint8_t *) ncacn_conn->iov[ncacn_conn->count].iov_base,
1229 ncacn_conn->iov[ncacn_conn->count].iov_len);
1230 ncacn_conn->count++;
1234 * We still don't have a complete request, go back and wait for more
1237 if (ncacn_conn->count == 0) {
1238 /* Wait for the next packet */
1239 subreq = dcerpc_ncacn_read_packet_send(ncacn_conn,
1241 ncacn_conn->tstream);
1242 if (subreq == NULL) {
1243 DEBUG(2, ("Failed to start receving packets\n"));
1244 status = NT_STATUS_NO_MEMORY;
1247 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1251 DEBUG(10, ("Sending a total of %u bytes\n",
1252 (unsigned int)ncacn_conn->p->out_data.data_sent_length));
1254 subreq = tstream_writev_queue_send(ncacn_conn,
1256 ncacn_conn->tstream,
1257 ncacn_conn->send_queue,
1260 if (subreq == NULL) {
1261 DEBUG(2, ("Failed to send packet\n"));
1262 status = NT_STATUS_NO_MEMORY;
1266 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_done, ncacn_conn);
1270 DEBUG(2, ("Fatal error(%s). "
1271 "Terminating client(%s) connection!\n",
1272 nt_errstr(status), ncacn_conn->client_name));
1274 /* Terminate client connection */
1275 talloc_free(ncacn_conn);
1279 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq)
1281 struct dcerpc_ncacn_conn *ncacn_conn =
1282 tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
1286 rc = tstream_writev_queue_recv(subreq, &sys_errno);
1287 TALLOC_FREE(subreq);
1289 DEBUG(2, ("Writev failed!\n"));
1293 /* clear out any data that may have been left around */
1294 ncacn_conn->count = 0;
1295 TALLOC_FREE(ncacn_conn->iov);
1296 data_blob_free(&ncacn_conn->p->in_data.data);
1297 data_blob_free(&ncacn_conn->p->out_data.frag);
1298 data_blob_free(&ncacn_conn->p->out_data.rdata);
1300 /* Wait for the next packet */
1301 subreq = dcerpc_ncacn_read_packet_send(ncacn_conn,
1303 ncacn_conn->tstream);
1304 if (subreq == NULL) {
1305 DEBUG(2, ("Failed to start receving packets\n"));
1310 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1314 DEBUG(2, ("Fatal error(%s). Terminating client(%s) connection!\n",
1315 strerror(sys_errno), ncacn_conn->client_name));
1317 /* Terminate client connection */
1318 talloc_free(ncacn_conn);
1322 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */