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 enum dcerpc_transport_t transport,
80 const char *client_address,
81 const char *server_address,
82 struct auth_session_info_transport *session_info,
83 struct pipes_struct **_p,
86 struct netr_SamInfo3 *info3;
87 struct auth_user_info_dc *auth_user_info_dc;
88 struct pipes_struct *p;
92 p = talloc_zero(mem_ctx, struct pipes_struct);
98 p->transport = transport;
100 p->mem_ctx = talloc_named(p, 0, "pipe %s %p", pipe_name, p);
107 ok = init_pipe_handles(p, &id);
109 DEBUG(1, ("Failed to init handles\n"));
116 data_blob_free(&p->in_data.data);
117 data_blob_free(&p->in_data.pdu);
119 p->endian = RPC_LITTLE_ENDIAN;
121 /* Fake up an auth_user_info_dc for now, to make an info3, to make the session_info structure */
122 auth_user_info_dc = talloc_zero(p, struct auth_user_info_dc);
123 if (!auth_user_info_dc) {
129 auth_user_info_dc->num_sids = session_info->security_token->num_sids;
130 auth_user_info_dc->sids = session_info->security_token->sids;
131 auth_user_info_dc->info = session_info->info;
132 auth_user_info_dc->user_session_key = session_info->session_key;
134 /* This creates the input structure that make_server_info_info3 is looking for */
135 status = auth_convert_user_info_dc_saminfo3(p, auth_user_info_dc,
138 if (!NT_STATUS_IS_OK(status)) {
139 DEBUG(1, ("Failed to convert auth_user_info_dc into netr_SamInfo3\n"));
145 status = make_server_info_info3(p,
146 info3->base.account_name.string,
147 info3->base.domain.string,
148 &p->session_info, info3);
149 if (!NT_STATUS_IS_OK(status)) {
150 DEBUG(1, ("Failed to init server info\n"));
157 * Some internal functions need a local token to determine access to
160 status = create_local_token(p->session_info);
161 if (!NT_STATUS_IS_OK(status)) {
162 DEBUG(1, ("Failed to init local auth token\n"));
168 /* Now override the session_info->security_token with the exact
169 * security_token we were given from the other side,
170 * regardless of what we just calculated */
171 p->session_info->security_token = talloc_move(p->session_info, &session_info->security_token);
173 /* Also set the session key to the correct value */
174 p->session_info->user_session_key = session_info->session_key;
175 p->session_info->user_session_key.data = talloc_move(p->session_info, &session_info->session_key.data);
177 p->client_id = talloc_zero(p, struct client_address);
183 strlcpy(p->client_id->addr,
184 client_address, sizeof(p->client_id->addr));
185 p->client_id->name = talloc_strdup(p->client_id, client_address);
186 if (p->client_id->name == NULL) {
192 if (server_address != NULL) {
193 p->server_id = talloc_zero(p, struct client_address);
194 if (p->client_id == NULL) {
200 strlcpy(p->server_id->addr,
202 sizeof(p->server_id->addr));
204 p->server_id->name = talloc_strdup(p->server_id,
206 if (p->server_id->name == NULL) {
213 talloc_set_destructor(p, close_internal_rpc_pipe_hnd);
219 /* Add some helper functions to wrap the common ncacn packet reading functions
220 * until we can share more dcerpc code */
221 struct dcerpc_ncacn_read_packet_state {
222 struct ncacn_packet *pkt;
226 static void dcerpc_ncacn_read_packet_done(struct tevent_req *subreq);
228 static struct tevent_req *dcerpc_ncacn_read_packet_send(TALLOC_CTX *mem_ctx,
229 struct tevent_context *ev,
230 struct tstream_context *tstream)
232 struct dcerpc_ncacn_read_packet_state *state;
233 struct tevent_req *req, *subreq;
235 req = tevent_req_create(mem_ctx, &state,
236 struct dcerpc_ncacn_read_packet_state);
242 subreq = dcerpc_read_ncacn_packet_send(state, ev, tstream);
244 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
245 tevent_req_post(req, ev);
248 tevent_req_set_callback(subreq, dcerpc_ncacn_read_packet_done, req);
253 static void dcerpc_ncacn_read_packet_done(struct tevent_req *subreq)
255 struct tevent_req *req =
256 tevent_req_callback_data(subreq, struct tevent_req);
257 struct dcerpc_ncacn_read_packet_state *state =
258 tevent_req_data(req, struct dcerpc_ncacn_read_packet_state);
261 status = dcerpc_read_ncacn_packet_recv(subreq, state,
265 if (!NT_STATUS_IS_OK(status)) {
266 DEBUG(3, ("Failed to receive dceprc packet!\n"));
267 tevent_req_nterror(req, status);
271 tevent_req_done(req);
274 static NTSTATUS dcerpc_ncacn_read_packet_recv(struct tevent_req *req,
278 struct dcerpc_ncacn_read_packet_state *state =
279 tevent_req_data(req, struct dcerpc_ncacn_read_packet_state);
282 if (tevent_req_is_nterror(req, &status)) {
283 tevent_req_received(req);
287 buffer->data = talloc_move(mem_ctx, &state->buffer.data);
288 buffer->length = state->buffer.length;
290 tevent_req_received(req);
296 /* Start listening on the appropriate unix socket and setup all is needed to
297 * dispatch requests to the pipes rpc implementation */
299 struct dcerpc_ncacn_listen_state {
300 struct ndr_syntax_id syntax_id;
308 struct tevent_context *ev_ctx;
309 struct messaging_context *msg_ctx;
312 static void named_pipe_listener(struct tevent_context *ev,
313 struct tevent_fd *fde,
317 bool setup_named_pipe_socket(const char *pipe_name,
318 struct tevent_context *ev_ctx)
320 struct dcerpc_ncacn_listen_state *state;
321 struct tevent_fd *fde;
324 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
326 DEBUG(0, ("Out of memory\n"));
329 state->ep.name = talloc_strdup(state, pipe_name);
330 if (state->ep.name == NULL) {
331 DEBUG(0, ("Out of memory\n"));
336 np_dir = talloc_asprintf(state, "%s/np", lp_ncalrpc_dir());
338 DEBUG(0, ("Out of memory\n"));
342 if (!directory_create_or_exist(np_dir, geteuid(), 0700)) {
343 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
344 np_dir, strerror(errno)));
348 state->fd = create_pipe_sock(np_dir, pipe_name, 0700);
349 if (state->fd == -1) {
350 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
355 DEBUG(10, ("Openened pipe socket fd %d for %s\n",
356 state->fd, pipe_name));
358 fde = tevent_add_fd(ev_ctx,
359 state, state->fd, TEVENT_FD_READ,
360 named_pipe_listener, state);
362 DEBUG(0, ("Failed to add event handler!\n"));
366 tevent_fd_set_auto_close(fde);
370 if (state->fd != -1) {
377 static void named_pipe_accept_function(const char *pipe_name, int fd);
379 static void named_pipe_listener(struct tevent_context *ev,
380 struct tevent_fd *fde,
384 struct dcerpc_ncacn_listen_state *state =
385 talloc_get_type_abort(private_data,
386 struct dcerpc_ncacn_listen_state);
387 struct sockaddr_un sunaddr;
391 /* TODO: should we have a limit to the number of clients ? */
393 len = sizeof(sunaddr);
396 sd = accept(state->fd,
397 (struct sockaddr *)(void *)&sunaddr, &len);
398 if (errno != EINTR) break;
402 DEBUG(6, ("Failed to get a valid socket [%s]\n",
407 DEBUG(6, ("Accepted socket %d\n", sd));
409 named_pipe_accept_function(state->ep.name, sd);
413 /* This is the core of the rpc server.
414 * Accepts connections from clients and process requests using the appropriate
415 * dispatcher table. */
417 struct named_pipe_client {
418 const char *pipe_name;
419 struct ndr_syntax_id pipe_id;
421 struct tevent_context *ev;
422 struct messaging_context *msg_ctx;
425 uint16_t device_state;
426 uint64_t allocation_size;
428 struct tstream_context *tstream;
430 struct tsocket_address *client;
432 struct tsocket_address *server;
434 struct auth_session_info_transport *session_info;
436 struct pipes_struct *p;
438 struct tevent_queue *write_queue;
444 static void named_pipe_accept_done(struct tevent_req *subreq);
446 static void named_pipe_accept_function(const char *pipe_name, int fd)
448 struct ndr_syntax_id syntax;
449 struct named_pipe_client *npc;
450 struct tstream_context *plain;
451 struct tevent_req *subreq;
455 ok = is_known_pipename(pipe_name, &syntax);
457 DEBUG(1, ("Unknown pipe [%s]\n", pipe_name));
462 npc = talloc_zero(NULL, struct named_pipe_client);
464 DEBUG(0, ("Out of memory!\n"));
468 npc->pipe_name = pipe_name;
469 npc->pipe_id = syntax;
470 npc->ev = server_event_context();
471 npc->msg_ctx = server_messaging_context();
473 /* make sure socket is in NON blocking state */
474 ret = set_blocking(fd, false);
476 DEBUG(2, ("Failed to make socket non-blocking\n"));
482 ret = tstream_bsd_existing_socket(npc, fd, &plain);
484 DEBUG(2, ("Failed to create tstream socket\n"));
490 npc->file_type = FILE_TYPE_MESSAGE_MODE_PIPE;
491 npc->device_state = 0xff | 0x0400 | 0x0100;
492 npc->allocation_size = 4096;
494 subreq = tstream_npa_accept_existing_send(npc, npc->ev, plain,
497 npc->allocation_size);
499 DEBUG(2, ("Failed to start async accept procedure\n"));
504 tevent_req_set_callback(subreq, named_pipe_accept_done, npc);
507 static void named_pipe_packet_process(struct tevent_req *subreq);
508 static void named_pipe_packet_done(struct tevent_req *subreq);
510 static void named_pipe_accept_done(struct tevent_req *subreq)
512 struct named_pipe_client *npc =
513 tevent_req_callback_data(subreq, struct named_pipe_client);
514 const char *cli_addr;
518 ret = tstream_npa_accept_existing_recv(subreq, &error, npc,
527 DEBUG(2, ("Failed to accept named pipe connection! (%s)\n",
533 if (tsocket_address_is_inet(npc->client, "ip")) {
534 cli_addr = tsocket_address_inet_addr_string(npc->client,
536 if (cli_addr == NULL) {
544 ret = make_server_pipes_struct(npc,
545 npc->pipe_name, npc->pipe_id, NCACN_NP,
546 cli_addr, NULL, npc->session_info,
549 DEBUG(2, ("Failed to create pipes_struct! (%s)\n",
553 npc->p->msg_ctx = npc->msg_ctx;
555 npc->write_queue = tevent_queue_create(npc, "np_server_write_queue");
556 if (!npc->write_queue) {
557 DEBUG(2, ("Failed to set up write queue!\n"));
561 /* And now start receaving and processing packets */
562 subreq = dcerpc_ncacn_read_packet_send(npc, npc->ev, npc->tstream);
564 DEBUG(2, ("Failed to start receving packets\n"));
567 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
571 DEBUG(2, ("Fatal error. Terminating client(%s) connection!\n",
573 /* terminate client connection */
578 static void named_pipe_packet_process(struct tevent_req *subreq)
580 struct named_pipe_client *npc =
581 tevent_req_callback_data(subreq, struct named_pipe_client);
582 struct _output_data *out = &npc->p->out_data;
583 DATA_BLOB recv_buffer = data_blob_null;
591 status = dcerpc_ncacn_read_packet_recv(subreq, npc, &recv_buffer);
593 if (!NT_STATUS_IS_OK(status)) {
597 data_left = recv_buffer.length;
598 data = (char *)recv_buffer.data;
602 data_used = process_incoming_data(npc->p, data, data_left);
604 DEBUG(3, ("Failed to process dceprc request!\n"));
605 status = NT_STATUS_UNEXPECTED_IO_ERROR;
609 data_left -= data_used;
613 /* Do not leak this buffer, npc is a long lived context */
614 talloc_free(recv_buffer.data);
616 /* this is needed because of the way DCERPC Binds work in
617 * the RPC marshalling code */
618 to_send = out->frag.length - out->current_pdu_sent;
621 DEBUG(10, ("Current_pdu_len = %u, "
622 "current_pdu_sent = %u "
623 "Returning %u bytes\n",
624 (unsigned int)out->frag.length,
625 (unsigned int)out->current_pdu_sent,
626 (unsigned int)to_send));
628 npc->iov = talloc_zero(npc, struct iovec);
630 status = NT_STATUS_NO_MEMORY;
635 npc->iov[0].iov_base = out->frag.data
636 + out->current_pdu_sent;
637 npc->iov[0].iov_len = to_send;
639 out->current_pdu_sent += to_send;
642 /* this condition is false for bind packets, or when we haven't
643 * yet got a full request, and need to wait for more data from
645 while (out->data_sent_length < out->rdata.length) {
647 ok = create_next_pdu(npc->p);
649 DEBUG(3, ("Failed to create next PDU!\n"));
650 status = NT_STATUS_UNEXPECTED_IO_ERROR;
654 npc->iov = talloc_realloc(npc, npc->iov,
655 struct iovec, npc->count + 1);
657 status = NT_STATUS_NO_MEMORY;
661 npc->iov[npc->count].iov_base = out->frag.data;
662 npc->iov[npc->count].iov_len = out->frag.length;
664 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
665 (unsigned int)npc->count,
666 (unsigned int)npc->iov[npc->count].iov_len));
667 dump_data(11, (const uint8_t *)npc->iov[npc->count].iov_base,
668 npc->iov[npc->count].iov_len);
672 /* we still don't have a complete request, go back and wait for more
674 if (npc->count == 0) {
675 /* Wait for the next packet */
676 subreq = dcerpc_ncacn_read_packet_send(npc, npc->ev, npc->tstream);
678 DEBUG(2, ("Failed to start receving packets\n"));
679 status = NT_STATUS_NO_MEMORY;
682 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
686 DEBUG(10, ("Sending a total of %u bytes\n",
687 (unsigned int)npc->p->out_data.data_sent_length));
689 subreq = tstream_writev_queue_send(npc, npc->ev,
692 npc->iov, npc->count);
694 DEBUG(2, ("Failed to send packet\n"));
695 status = NT_STATUS_NO_MEMORY;
698 tevent_req_set_callback(subreq, named_pipe_packet_done, npc);
702 DEBUG(2, ("Fatal error(%s). "
703 "Terminating client(%s) connection!\n",
704 nt_errstr(status), npc->client_name));
705 /* terminate client connection */
710 static void named_pipe_packet_done(struct tevent_req *subreq)
712 struct named_pipe_client *npc =
713 tevent_req_callback_data(subreq, struct named_pipe_client);
717 ret = tstream_writev_queue_recv(subreq, &sys_errno);
720 DEBUG(2, ("Writev failed!\n"));
724 /* clear out any data that may have been left around */
726 TALLOC_FREE(npc->iov);
727 data_blob_free(&npc->p->in_data.data);
728 data_blob_free(&npc->p->out_data.frag);
729 data_blob_free(&npc->p->out_data.rdata);
731 /* Wait for the next packet */
732 subreq = dcerpc_ncacn_read_packet_send(npc, npc->ev, npc->tstream);
734 DEBUG(2, ("Failed to start receving packets\n"));
738 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
742 DEBUG(2, ("Fatal error(%s). "
743 "Terminating client(%s) connection!\n",
744 strerror(sys_errno), npc->client_name));
745 /* terminate client connection */
750 static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
751 struct messaging_context *msg_ctx,
752 struct ndr_syntax_id syntax_id,
753 enum dcerpc_transport_t transport,
756 struct tsocket_address *cli_addr,
757 struct tsocket_address *srv_addr,
760 /********************************************************************
761 * Start listening on the tcp/ip socket
762 ********************************************************************/
764 static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
765 struct tevent_fd *fde,
769 uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context *ev_ctx,
770 struct messaging_context *msg_ctx,
771 struct ndr_syntax_id syntax_id,
772 const struct sockaddr_storage *ifss,
775 struct dcerpc_ncacn_listen_state *state;
776 struct tevent_fd *fde;
779 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
781 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Out of memory\n"));
785 state->syntax_id = syntax_id;
787 state->ep.port = port;
789 if (state->ep.port == 0) {
792 for (i = SERVER_TCP_LOW_PORT; i <= SERVER_TCP_HIGH_PORT; i++) {
793 state->fd = open_socket_in(SOCK_STREAM,
804 state->fd = open_socket_in(SOCK_STREAM,
810 if (state->fd == -1) {
811 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Failed to create "
812 "socket on port %u!\n", state->ep.port));
816 state->ev_ctx = ev_ctx;
817 state->msg_ctx = msg_ctx;
819 /* ready to listen */
820 set_socket_options(state->fd, "SO_KEEPALIVE");
821 set_socket_options(state->fd, lp_socket_options());
823 /* Set server socket to non-blocking for the accept. */
824 set_blocking(state->fd, false);
826 rc = listen(state->fd, SMBD_LISTEN_BACKLOG);
828 DEBUG(0,("setup_tcpip_socket: listen - %s\n", strerror(errno)));
832 DEBUG(10, ("setup_tcpip_socket: openened socket fd %d for port %u\n",
833 state->fd, state->ep.port));
835 fde = tevent_add_fd(state->ev_ctx,
839 dcerpc_ncacn_tcpip_listener,
842 DEBUG(0, ("setup_tcpip_socket: Failed to add event handler!\n"));
846 tevent_fd_set_auto_close(fde);
848 return state->ep.port;
850 if (state->fd != -1) {
858 static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
859 struct tevent_fd *fde,
863 struct dcerpc_ncacn_listen_state *state =
864 talloc_get_type_abort(private_data,
865 struct dcerpc_ncacn_listen_state);
866 struct tsocket_address *cli_addr = NULL;
867 struct tsocket_address *srv_addr = NULL;
868 struct sockaddr_storage addr;
869 socklen_t in_addrlen = sizeof(addr);
874 s = accept(state->fd, (struct sockaddr *)(void *) &addr, &in_addrlen);
875 if (s == -1 && errno != EINTR) {
881 DEBUG(0,("tcpip_listener accept: %s\n",
886 rc = tsocket_address_bsd_from_sockaddr(state,
887 (struct sockaddr *)(void *) &addr,
895 rc = getsockname(s, (struct sockaddr *)(void *) &addr, &in_addrlen);
901 rc = tsocket_address_bsd_from_sockaddr(state,
902 (struct sockaddr *)(void *) &addr,
910 DEBUG(6, ("tcpip_listener: Accepted socket %d\n", s));
912 dcerpc_ncacn_accept(state->ev_ctx,
923 /********************************************************************
924 * Start listening on the ncalrpc socket
925 ********************************************************************/
927 static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
928 struct tevent_fd *fde,
932 bool setup_dcerpc_ncalrpc_socket(struct tevent_context *ev_ctx,
933 struct messaging_context *msg_ctx,
934 struct ndr_syntax_id syntax_id,
937 struct dcerpc_ncacn_listen_state *state;
938 struct tevent_fd *fde;
940 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
942 DEBUG(0, ("Out of memory\n"));
946 state->syntax_id = syntax_id;
952 state->ep.name = talloc_strdup(state, name);
954 if (state->ep.name == NULL) {
955 DEBUG(0, ("Out of memory\n"));
960 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0700)) {
961 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
962 lp_ncalrpc_dir(), strerror(errno)));
966 state->fd = create_pipe_sock(lp_ncalrpc_dir(), name, 0700);
967 if (state->fd == -1) {
968 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
969 lp_ncalrpc_dir(), name));
973 DEBUG(10, ("Openened pipe socket fd %d for %s\n", state->fd, name));
975 state->ev_ctx = ev_ctx;
976 state->msg_ctx = msg_ctx;
978 /* Set server socket to non-blocking for the accept. */
979 set_blocking(state->fd, false);
981 fde = tevent_add_fd(state->ev_ctx,
985 dcerpc_ncalrpc_listener,
988 DEBUG(0, ("Failed to add event handler for ncalrpc!\n"));
992 tevent_fd_set_auto_close(fde);
996 if (state->fd != -1) {
1004 static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
1005 struct tevent_fd *fde,
1009 struct dcerpc_ncacn_listen_state *state =
1010 talloc_get_type_abort(private_data,
1011 struct dcerpc_ncacn_listen_state);
1012 struct tsocket_address *cli_addr = NULL;
1013 struct sockaddr_un sunaddr;
1014 struct sockaddr *addr = (struct sockaddr *)(void *)&sunaddr;
1020 sd = accept(state->fd, addr, &len);
1021 if (sd == -1 && errno != EINTR) {
1027 DEBUG(0, ("ncalrpc accept() failed: %s\n", strerror(errno)));
1031 rc = tsocket_address_bsd_from_sockaddr(state,
1039 DEBUG(10, ("Accepted ncalrpc socket %d\n", sd));
1041 dcerpc_ncacn_accept(state->ev_ctx,
1043 state->syntax_id, NCALRPC,
1045 cli_addr, NULL, sd);
1048 struct dcerpc_ncacn_conn {
1049 struct ndr_syntax_id syntax_id;
1051 enum dcerpc_transport_t transport;
1060 struct pipes_struct *p;
1062 struct tevent_context *ev_ctx;
1063 struct messaging_context *msg_ctx;
1065 struct tstream_context *tstream;
1066 struct tevent_queue *send_queue;
1068 struct tsocket_address *client;
1070 struct tsocket_address *server;
1072 struct auth_session_info_transport *session_info;
1078 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq);
1079 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq);
1081 static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
1082 struct messaging_context *msg_ctx,
1083 struct ndr_syntax_id syntax_id,
1084 enum dcerpc_transport_t transport,
1087 struct tsocket_address *cli_addr,
1088 struct tsocket_address *srv_addr,
1090 struct dcerpc_ncacn_conn *ncacn_conn;
1091 struct tevent_req *subreq;
1092 const char *cli_str;
1093 const char *srv_str = NULL;
1099 DEBUG(10, ("dcerpc_ncacn_accept\n"));
1101 ncacn_conn = talloc_zero(ev_ctx, struct dcerpc_ncacn_conn);
1102 if (ncacn_conn == NULL) {
1103 DEBUG(0, ("Out of memory!\n"));
1108 switch (transport) {
1110 ncacn_conn->ep.port = port;
1114 ncacn_conn->ep.name = talloc_strdup(ncacn_conn, name);
1117 DEBUG(0, ("unknown dcerpc transport: %u!\n",
1119 talloc_free(ncacn_conn);
1123 ncacn_conn->transport = transport;
1124 ncacn_conn->syntax_id = syntax_id;
1125 ncacn_conn->ev_ctx = ev_ctx;
1126 ncacn_conn->msg_ctx = msg_ctx;
1127 ncacn_conn->sock = s;
1129 ncacn_conn->client = talloc_move(ncacn_conn, &cli_addr);
1130 if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
1131 ncacn_conn->client_name =
1132 tsocket_address_inet_addr_string(ncacn_conn->client,
1135 ncacn_conn->client_name =
1136 tsocket_address_unix_path(ncacn_conn->client,
1139 if (ncacn_conn->client_name == NULL) {
1140 DEBUG(0, ("Out of memory!\n"));
1141 talloc_free(ncacn_conn);
1146 if (srv_addr != NULL) {
1147 ncacn_conn->server = talloc_move(ncacn_conn, &srv_addr);
1149 ncacn_conn->server_name =
1150 tsocket_address_inet_addr_string(ncacn_conn->server,
1152 if (ncacn_conn->server_name == NULL) {
1153 DEBUG(0, ("Out of memory!\n"));
1154 talloc_free(ncacn_conn);
1160 rc = set_blocking(s, false);
1162 DEBUG(2, ("Failed to set dcerpc socket to non-blocking\n"));
1163 talloc_free(ncacn_conn);
1169 * As soon as we have tstream_bsd_existing_socket set up it will
1170 * take care of closing the socket.
1172 rc = tstream_bsd_existing_socket(ncacn_conn, s, &ncacn_conn->tstream);
1174 DEBUG(2, ("Failed to create tstream socket for dcerpc\n"));
1175 talloc_free(ncacn_conn);
1180 switch (ncacn_conn->transport) {
1182 pipe_name = tsocket_address_string(ncacn_conn->client,
1186 pipe_name = talloc_strdup(ncacn_conn,
1187 ncacn_conn->ep.name);
1190 talloc_free(ncacn_conn);
1194 if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
1195 cli_str = ncacn_conn->client_name;
1200 if (ncacn_conn->server != NULL) {
1201 if (tsocket_address_is_inet(ncacn_conn->server, "ip")) {
1202 srv_str = ncacn_conn->server_name;
1208 if (ncacn_conn->session_info == NULL) {
1209 status = auth_anonymous_session_info(ncacn_conn,
1210 &ncacn_conn->session_info);
1211 if (!NT_STATUS_IS_OK(status)) {
1212 DEBUG(2, ("Failed to create "
1213 "auth_anonymous_session_info - %s\n",
1214 nt_errstr(status)));
1215 talloc_free(ncacn_conn);
1220 rc = make_server_pipes_struct(ncacn_conn,
1222 ncacn_conn->syntax_id,
1223 ncacn_conn->transport,
1226 ncacn_conn->session_info,
1230 DEBUG(2, ("Failed to create pipe struct - %s",
1231 strerror(sys_errno)));
1232 talloc_free(ncacn_conn);
1236 ncacn_conn->send_queue = tevent_queue_create(ncacn_conn,
1237 "dcerpc send queue");
1238 if (ncacn_conn->send_queue == NULL) {
1239 DEBUG(0, ("Out of memory!\n"));
1240 talloc_free(ncacn_conn);
1244 subreq = dcerpc_ncacn_read_packet_send(ncacn_conn,
1246 ncacn_conn->tstream);
1247 if (subreq == NULL) {
1248 DEBUG(2, ("Failed to send ncacn packet\n"));
1249 talloc_free(ncacn_conn);
1253 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1255 DEBUG(10, ("dcerpc_ncacn_accept done\n"));
1260 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq)
1262 struct dcerpc_ncacn_conn *ncacn_conn =
1263 tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
1265 struct _output_data *out = &ncacn_conn->p->out_data;
1266 DATA_BLOB recv_buffer = data_blob_null;
1274 status = dcerpc_ncacn_read_packet_recv(subreq, ncacn_conn, &recv_buffer);
1275 TALLOC_FREE(subreq);
1276 if (!NT_STATUS_IS_OK(status)) {
1280 data_left = recv_buffer.length;
1281 data = (char *) recv_buffer.data;
1284 data_used = process_incoming_data(ncacn_conn->p, data, data_left);
1285 if (data_used < 0) {
1286 DEBUG(3, ("Failed to process dcerpc request!\n"));
1287 status = NT_STATUS_UNEXPECTED_IO_ERROR;
1291 data_left -= data_used;
1295 /* Do not leak this buffer */
1296 talloc_free(recv_buffer.data);
1299 * This is needed because of the way DCERPC binds work in the RPC
1302 to_send = out->frag.length - out->current_pdu_sent;
1305 DEBUG(10, ("Current_pdu_len = %u, "
1306 "current_pdu_sent = %u "
1307 "Returning %u bytes\n",
1308 (unsigned int)out->frag.length,
1309 (unsigned int)out->current_pdu_sent,
1310 (unsigned int)to_send));
1312 ncacn_conn->iov = talloc_zero(ncacn_conn, struct iovec);
1313 if (ncacn_conn->iov == NULL) {
1314 status = NT_STATUS_NO_MEMORY;
1317 ncacn_conn->count = 1;
1319 ncacn_conn->iov[0].iov_base = out->frag.data
1320 + out->current_pdu_sent;
1321 ncacn_conn->iov[0].iov_len = to_send;
1323 out->current_pdu_sent += to_send;
1327 * This condition is false for bind packets, or when we haven't yet got
1328 * a full request, and need to wait for more data from the client
1330 while (out->data_sent_length < out->rdata.length) {
1331 ok = create_next_pdu(ncacn_conn->p);
1333 DEBUG(3, ("Failed to create next PDU!\n"));
1334 status = NT_STATUS_UNEXPECTED_IO_ERROR;
1338 ncacn_conn->iov = talloc_realloc(ncacn_conn,
1341 ncacn_conn->count + 1);
1342 if (ncacn_conn->iov == NULL) {
1343 status = NT_STATUS_NO_MEMORY;
1347 ncacn_conn->iov[ncacn_conn->count].iov_base = out->frag.data;
1348 ncacn_conn->iov[ncacn_conn->count].iov_len = out->frag.length;
1350 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
1351 (unsigned int) ncacn_conn->count,
1352 (unsigned int) ncacn_conn->iov[ncacn_conn->count].iov_len));
1353 dump_data(11, (const uint8_t *) ncacn_conn->iov[ncacn_conn->count].iov_base,
1354 ncacn_conn->iov[ncacn_conn->count].iov_len);
1355 ncacn_conn->count++;
1359 * We still don't have a complete request, go back and wait for more
1362 if (ncacn_conn->count == 0) {
1363 /* Wait for the next packet */
1364 subreq = dcerpc_ncacn_read_packet_send(ncacn_conn,
1366 ncacn_conn->tstream);
1367 if (subreq == NULL) {
1368 DEBUG(2, ("Failed to start receving packets\n"));
1369 status = NT_STATUS_NO_MEMORY;
1372 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1376 DEBUG(10, ("Sending a total of %u bytes\n",
1377 (unsigned int)ncacn_conn->p->out_data.data_sent_length));
1379 subreq = tstream_writev_queue_send(ncacn_conn,
1381 ncacn_conn->tstream,
1382 ncacn_conn->send_queue,
1385 if (subreq == NULL) {
1386 DEBUG(2, ("Failed to send packet\n"));
1387 status = NT_STATUS_NO_MEMORY;
1391 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_done, ncacn_conn);
1395 DEBUG(2, ("Fatal error(%s). "
1396 "Terminating client(%s) connection!\n",
1397 nt_errstr(status), ncacn_conn->client_name));
1399 /* Terminate client connection */
1400 talloc_free(ncacn_conn);
1404 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq)
1406 struct dcerpc_ncacn_conn *ncacn_conn =
1407 tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
1411 rc = tstream_writev_queue_recv(subreq, &sys_errno);
1412 TALLOC_FREE(subreq);
1414 DEBUG(2, ("Writev failed!\n"));
1418 /* clear out any data that may have been left around */
1419 ncacn_conn->count = 0;
1420 TALLOC_FREE(ncacn_conn->iov);
1421 data_blob_free(&ncacn_conn->p->in_data.data);
1422 data_blob_free(&ncacn_conn->p->out_data.frag);
1423 data_blob_free(&ncacn_conn->p->out_data.rdata);
1425 /* Wait for the next packet */
1426 subreq = dcerpc_ncacn_read_packet_send(ncacn_conn,
1428 ncacn_conn->tstream);
1429 if (subreq == NULL) {
1430 DEBUG(2, ("Failed to start receving packets\n"));
1435 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1439 DEBUG(2, ("Fatal error(%s). Terminating client(%s) connection!\n",
1440 strerror(sys_errno), ncacn_conn->client_name));
1442 /* Terminate client connection */
1443 talloc_free(ncacn_conn);
1447 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */