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 struct auth_session_info_transport *session_info,
81 struct pipes_struct **_p,
84 struct netr_SamInfo3 *info3;
85 struct auth_user_info_dc *auth_user_info_dc;
86 struct pipes_struct *p;
90 p = talloc_zero(mem_ctx, struct pipes_struct);
97 p->mem_ctx = talloc_named(p, 0, "pipe %s %p", pipe_name, p);
104 ok = init_pipe_handles(p, &id);
106 DEBUG(1, ("Failed to init handles\n"));
113 data_blob_free(&p->in_data.data);
114 data_blob_free(&p->in_data.pdu);
116 p->endian = RPC_LITTLE_ENDIAN;
118 /* Fake up an auth_user_info_dc for now, to make an info3, to make the session_info structure */
119 auth_user_info_dc = talloc_zero(p, struct auth_user_info_dc);
120 if (!auth_user_info_dc) {
126 auth_user_info_dc->num_sids = session_info->security_token->num_sids;
127 auth_user_info_dc->sids = session_info->security_token->sids;
128 auth_user_info_dc->info = session_info->info;
129 auth_user_info_dc->user_session_key = session_info->session_key;
131 /* This creates the input structure that make_server_info_info3 is looking for */
132 status = auth_convert_user_info_dc_saminfo3(p, auth_user_info_dc,
135 if (!NT_STATUS_IS_OK(status)) {
136 DEBUG(1, ("Failed to convert auth_user_info_dc into netr_SamInfo3\n"));
142 status = make_server_info_info3(p,
143 info3->base.account_name.string,
144 info3->base.domain.string,
145 &p->session_info, info3);
146 if (!NT_STATUS_IS_OK(status)) {
147 DEBUG(1, ("Failed to init server info\n"));
154 * Some internal functions need a local token to determine access to
157 status = create_local_token(p->session_info);
158 if (!NT_STATUS_IS_OK(status)) {
159 DEBUG(1, ("Failed to init local auth token\n"));
165 /* Now override the session_info->security_token with the exact
166 * security_token we were given from the other side,
167 * regardless of what we just calculated */
168 p->session_info->security_token = talloc_move(p->session_info, &session_info->security_token);
170 /* Also set the session key to the correct value */
171 p->session_info->user_session_key = session_info->session_key;
172 p->session_info->user_session_key.data = talloc_move(p->session_info, &session_info->session_key.data);
174 p->client_id = talloc_zero(p, struct client_address);
180 strlcpy(p->client_id->addr,
181 client_address, sizeof(p->client_id->addr));
182 p->client_id->name = talloc_strdup(p->client_id, client_address);
183 if (p->client_id->name == NULL) {
189 talloc_set_destructor(p, close_internal_rpc_pipe_hnd);
195 /* Add some helper functions to wrap the common ncacn packet reading functions
196 * until we can share more dcerpc code */
197 struct dcerpc_ncacn_read_packet_state {
198 struct ncacn_packet *pkt;
202 static void dcerpc_ncacn_read_packet_done(struct tevent_req *subreq);
204 static struct tevent_req *dcerpc_ncacn_read_packet_send(TALLOC_CTX *mem_ctx,
205 struct tevent_context *ev,
206 struct tstream_context *tstream)
208 struct dcerpc_ncacn_read_packet_state *state;
209 struct tevent_req *req, *subreq;
211 req = tevent_req_create(mem_ctx, &state,
212 struct dcerpc_ncacn_read_packet_state);
218 subreq = dcerpc_read_ncacn_packet_send(state, ev, tstream);
220 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
221 tevent_req_post(req, ev);
224 tevent_req_set_callback(subreq, dcerpc_ncacn_read_packet_done, req);
229 static void dcerpc_ncacn_read_packet_done(struct tevent_req *subreq)
231 struct tevent_req *req =
232 tevent_req_callback_data(subreq, struct tevent_req);
233 struct dcerpc_ncacn_read_packet_state *state =
234 tevent_req_data(req, struct dcerpc_ncacn_read_packet_state);
237 status = dcerpc_read_ncacn_packet_recv(subreq, state,
241 if (!NT_STATUS_IS_OK(status)) {
242 DEBUG(3, ("Failed to receive dceprc packet!\n"));
243 tevent_req_nterror(req, status);
247 tevent_req_done(req);
250 static NTSTATUS dcerpc_ncacn_read_packet_recv(struct tevent_req *req,
254 struct dcerpc_ncacn_read_packet_state *state =
255 tevent_req_data(req, struct dcerpc_ncacn_read_packet_state);
258 if (tevent_req_is_nterror(req, &status)) {
259 tevent_req_received(req);
263 buffer->data = talloc_move(mem_ctx, &state->buffer.data);
264 buffer->length = state->buffer.length;
266 tevent_req_received(req);
272 /* Start listening on the appropriate unix socket and setup all is needed to
273 * dispatch requests to the pipes rpc implementation */
275 struct dcerpc_ncacn_listen_state {
276 struct ndr_syntax_id syntax_id;
284 struct tevent_context *ev_ctx;
285 struct messaging_context *msg_ctx;
288 static void named_pipe_listener(struct tevent_context *ev,
289 struct tevent_fd *fde,
293 bool setup_named_pipe_socket(const char *pipe_name,
294 struct tevent_context *ev_ctx)
296 struct dcerpc_ncacn_listen_state *state;
297 struct tevent_fd *fde;
300 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
302 DEBUG(0, ("Out of memory\n"));
305 state->ep.name = talloc_strdup(state, pipe_name);
306 if (state->ep.name == NULL) {
307 DEBUG(0, ("Out of memory\n"));
312 np_dir = talloc_asprintf(state, "%s/np", lp_ncalrpc_dir());
314 DEBUG(0, ("Out of memory\n"));
318 if (!directory_create_or_exist(np_dir, geteuid(), 0700)) {
319 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
320 np_dir, strerror(errno)));
324 state->fd = create_pipe_sock(np_dir, pipe_name, 0700);
325 if (state->fd == -1) {
326 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
331 DEBUG(10, ("Openened pipe socket fd %d for %s\n",
332 state->fd, pipe_name));
334 fde = tevent_add_fd(ev_ctx,
335 state, state->fd, TEVENT_FD_READ,
336 named_pipe_listener, state);
338 DEBUG(0, ("Failed to add event handler!\n"));
342 tevent_fd_set_auto_close(fde);
346 if (state->fd != -1) {
353 static void named_pipe_accept_function(const char *pipe_name, int fd);
355 static void named_pipe_listener(struct tevent_context *ev,
356 struct tevent_fd *fde,
360 struct dcerpc_ncacn_listen_state *state =
361 talloc_get_type_abort(private_data,
362 struct dcerpc_ncacn_listen_state);
363 struct sockaddr_un sunaddr;
367 /* TODO: should we have a limit to the number of clients ? */
369 len = sizeof(sunaddr);
372 sd = accept(state->fd,
373 (struct sockaddr *)(void *)&sunaddr, &len);
374 if (errno != EINTR) break;
378 DEBUG(6, ("Failed to get a valid socket [%s]\n",
383 DEBUG(6, ("Accepted socket %d\n", sd));
385 named_pipe_accept_function(state->ep.name, sd);
389 /* This is the core of the rpc server.
390 * Accepts connections from clients and process requests using the appropriate
391 * dispatcher table. */
393 struct named_pipe_client {
394 const char *pipe_name;
395 struct ndr_syntax_id pipe_id;
397 struct tevent_context *ev;
398 struct messaging_context *msg_ctx;
401 uint16_t device_state;
402 uint64_t allocation_size;
404 struct tstream_context *tstream;
406 struct tsocket_address *client;
408 struct tsocket_address *server;
410 struct auth_session_info_transport *session_info;
412 struct pipes_struct *p;
414 struct tevent_queue *write_queue;
420 static void named_pipe_accept_done(struct tevent_req *subreq);
422 static void named_pipe_accept_function(const char *pipe_name, int fd)
424 struct ndr_syntax_id syntax;
425 struct named_pipe_client *npc;
426 struct tstream_context *plain;
427 struct tevent_req *subreq;
431 ok = is_known_pipename(pipe_name, &syntax);
433 DEBUG(1, ("Unknown pipe [%s]\n", pipe_name));
438 npc = talloc_zero(NULL, struct named_pipe_client);
440 DEBUG(0, ("Out of memory!\n"));
444 npc->pipe_name = pipe_name;
445 npc->pipe_id = syntax;
446 npc->ev = server_event_context();
447 npc->msg_ctx = server_messaging_context();
449 /* make sure socket is in NON blocking state */
450 ret = set_blocking(fd, false);
452 DEBUG(2, ("Failed to make socket non-blocking\n"));
458 ret = tstream_bsd_existing_socket(npc, fd, &plain);
460 DEBUG(2, ("Failed to create tstream socket\n"));
466 npc->file_type = FILE_TYPE_MESSAGE_MODE_PIPE;
467 npc->device_state = 0xff | 0x0400 | 0x0100;
468 npc->allocation_size = 4096;
470 subreq = tstream_npa_accept_existing_send(npc, npc->ev, plain,
473 npc->allocation_size);
475 DEBUG(2, ("Failed to start async accept procedure\n"));
480 tevent_req_set_callback(subreq, named_pipe_accept_done, npc);
483 static void named_pipe_packet_process(struct tevent_req *subreq);
484 static void named_pipe_packet_done(struct tevent_req *subreq);
486 static void named_pipe_accept_done(struct tevent_req *subreq)
488 struct named_pipe_client *npc =
489 tevent_req_callback_data(subreq, struct named_pipe_client);
490 const char *cli_addr;
494 ret = tstream_npa_accept_existing_recv(subreq, &error, npc,
503 DEBUG(2, ("Failed to accept named pipe connection! (%s)\n",
509 if (tsocket_address_is_inet(npc->client, "ip")) {
510 cli_addr = tsocket_address_inet_addr_string(npc->client,
512 if (cli_addr == NULL) {
520 ret = make_server_pipes_struct(npc,
521 npc->pipe_name, npc->pipe_id,
522 cli_addr, npc->session_info,
525 DEBUG(2, ("Failed to create pipes_struct! (%s)\n",
529 npc->p->msg_ctx = npc->msg_ctx;
531 npc->write_queue = tevent_queue_create(npc, "np_server_write_queue");
532 if (!npc->write_queue) {
533 DEBUG(2, ("Failed to set up write queue!\n"));
537 /* And now start receaving and processing packets */
538 subreq = dcerpc_ncacn_read_packet_send(npc, npc->ev, npc->tstream);
540 DEBUG(2, ("Failed to start receving packets\n"));
543 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
547 DEBUG(2, ("Fatal error. Terminating client(%s) connection!\n",
549 /* terminate client connection */
554 static void named_pipe_packet_process(struct tevent_req *subreq)
556 struct named_pipe_client *npc =
557 tevent_req_callback_data(subreq, struct named_pipe_client);
558 struct _output_data *out = &npc->p->out_data;
559 DATA_BLOB recv_buffer = data_blob_null;
567 status = dcerpc_ncacn_read_packet_recv(subreq, npc, &recv_buffer);
569 if (!NT_STATUS_IS_OK(status)) {
573 data_left = recv_buffer.length;
574 data = (char *)recv_buffer.data;
578 data_used = process_incoming_data(npc->p, data, data_left);
580 DEBUG(3, ("Failed to process dceprc request!\n"));
581 status = NT_STATUS_UNEXPECTED_IO_ERROR;
585 data_left -= data_used;
589 /* Do not leak this buffer, npc is a long lived context */
590 talloc_free(recv_buffer.data);
592 /* this is needed because of the way DCERPC Binds work in
593 * the RPC marshalling code */
594 to_send = out->frag.length - out->current_pdu_sent;
597 DEBUG(10, ("Current_pdu_len = %u, "
598 "current_pdu_sent = %u "
599 "Returning %u bytes\n",
600 (unsigned int)out->frag.length,
601 (unsigned int)out->current_pdu_sent,
602 (unsigned int)to_send));
604 npc->iov = talloc_zero(npc, struct iovec);
606 status = NT_STATUS_NO_MEMORY;
611 npc->iov[0].iov_base = out->frag.data
612 + out->current_pdu_sent;
613 npc->iov[0].iov_len = to_send;
615 out->current_pdu_sent += to_send;
618 /* this condition is false for bind packets, or when we haven't
619 * yet got a full request, and need to wait for more data from
621 while (out->data_sent_length < out->rdata.length) {
623 ok = create_next_pdu(npc->p);
625 DEBUG(3, ("Failed to create next PDU!\n"));
626 status = NT_STATUS_UNEXPECTED_IO_ERROR;
630 npc->iov = talloc_realloc(npc, npc->iov,
631 struct iovec, npc->count + 1);
633 status = NT_STATUS_NO_MEMORY;
637 npc->iov[npc->count].iov_base = out->frag.data;
638 npc->iov[npc->count].iov_len = out->frag.length;
640 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
641 (unsigned int)npc->count,
642 (unsigned int)npc->iov[npc->count].iov_len));
643 dump_data(11, (const uint8_t *)npc->iov[npc->count].iov_base,
644 npc->iov[npc->count].iov_len);
648 /* we still don't have a complete request, go back and wait for more
650 if (npc->count == 0) {
651 /* Wait for the next packet */
652 subreq = dcerpc_ncacn_read_packet_send(npc, npc->ev, npc->tstream);
654 DEBUG(2, ("Failed to start receving packets\n"));
655 status = NT_STATUS_NO_MEMORY;
658 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
662 DEBUG(10, ("Sending a total of %u bytes\n",
663 (unsigned int)npc->p->out_data.data_sent_length));
665 subreq = tstream_writev_queue_send(npc, npc->ev,
668 npc->iov, npc->count);
670 DEBUG(2, ("Failed to send packet\n"));
671 status = NT_STATUS_NO_MEMORY;
674 tevent_req_set_callback(subreq, named_pipe_packet_done, npc);
678 DEBUG(2, ("Fatal error(%s). "
679 "Terminating client(%s) connection!\n",
680 nt_errstr(status), npc->client_name));
681 /* terminate client connection */
686 static void named_pipe_packet_done(struct tevent_req *subreq)
688 struct named_pipe_client *npc =
689 tevent_req_callback_data(subreq, struct named_pipe_client);
693 ret = tstream_writev_queue_recv(subreq, &sys_errno);
696 DEBUG(2, ("Writev failed!\n"));
700 /* clear out any data that may have been left around */
702 TALLOC_FREE(npc->iov);
703 data_blob_free(&npc->p->in_data.data);
704 data_blob_free(&npc->p->out_data.frag);
705 data_blob_free(&npc->p->out_data.rdata);
707 /* Wait for the next packet */
708 subreq = dcerpc_ncacn_read_packet_send(npc, npc->ev, npc->tstream);
710 DEBUG(2, ("Failed to start receving packets\n"));
714 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
718 DEBUG(2, ("Fatal error(%s). "
719 "Terminating client(%s) connection!\n",
720 strerror(sys_errno), npc->client_name));
721 /* terminate client connection */
726 /********************************************************************
727 * Start listening on the tcp/ip socket
728 ********************************************************************/
730 static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
731 struct tevent_fd *fde,
735 uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context *ev_ctx,
736 struct messaging_context *msg_ctx,
737 struct ndr_syntax_id syntax_id,
738 const struct sockaddr_storage *ifss,
741 struct dcerpc_ncacn_listen_state *state;
742 struct tevent_fd *fde;
745 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
747 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Out of memory\n"));
751 state->syntax_id = syntax_id;
753 state->ep.port = port;
755 if (state->ep.port == 0) {
758 for (i = SERVER_TCP_LOW_PORT; i <= SERVER_TCP_HIGH_PORT; i++) {
759 state->fd = open_socket_in(SOCK_STREAM,
770 state->fd = open_socket_in(SOCK_STREAM,
776 if (state->fd == -1) {
777 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Failed to create "
778 "socket on port %u!\n", state->ep.port));
782 state->ev_ctx = ev_ctx;
783 state->msg_ctx = msg_ctx;
785 /* ready to listen */
786 set_socket_options(state->fd, "SO_KEEPALIVE");
787 set_socket_options(state->fd, lp_socket_options());
789 /* Set server socket to non-blocking for the accept. */
790 set_blocking(state->fd, false);
792 rc = listen(state->fd, SMBD_LISTEN_BACKLOG);
794 DEBUG(0,("setup_tcpip_socket: listen - %s\n", strerror(errno)));
798 DEBUG(10, ("setup_tcpip_socket: openened socket fd %d for port %u\n",
799 state->fd, state->ep.port));
801 fde = tevent_add_fd(state->ev_ctx,
805 dcerpc_ncacn_tcpip_listener,
808 DEBUG(0, ("setup_tcpip_socket: Failed to add event handler!\n"));
812 tevent_fd_set_auto_close(fde);
814 return state->ep.port;
816 if (state->fd != -1) {
824 static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
825 struct messaging_context *msg_ctx,
826 struct ndr_syntax_id syntax_id,
827 enum dcerpc_transport_t transport,
830 struct tsocket_address *cli_addr,
831 struct tsocket_address *srv_addr,
834 static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
835 struct tevent_fd *fde,
839 struct dcerpc_ncacn_listen_state *state =
840 talloc_get_type_abort(private_data,
841 struct dcerpc_ncacn_listen_state);
842 struct tsocket_address *cli_addr = NULL;
843 struct tsocket_address *srv_addr = NULL;
844 struct sockaddr_storage addr;
845 socklen_t in_addrlen = sizeof(addr);
850 s = accept(state->fd, (struct sockaddr *)(void *) &addr, &in_addrlen);
851 if (s == -1 && errno != EINTR) {
857 DEBUG(0,("tcpip_listener accept: %s\n",
862 rc = tsocket_address_bsd_from_sockaddr(state,
863 (struct sockaddr *)(void *) &addr,
871 rc = getsockname(s, (struct sockaddr *)(void *) &addr, &in_addrlen);
877 rc = tsocket_address_bsd_from_sockaddr(state,
878 (struct sockaddr *)(void *) &addr,
886 DEBUG(6, ("tcpip_listener: Accepted socket %d\n", s));
888 dcerpc_ncacn_accept(state->ev_ctx,
899 struct dcerpc_ncacn_conn {
900 struct ndr_syntax_id syntax_id;
902 enum dcerpc_transport_t transport;
911 struct pipes_struct *p;
913 struct tevent_context *ev_ctx;
914 struct messaging_context *msg_ctx;
916 struct tstream_context *tstream;
917 struct tevent_queue *send_queue;
919 struct tsocket_address *client;
921 struct tsocket_address *server;
923 struct auth_session_info_transport *session_info;
929 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq);
930 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq);
932 static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
933 struct messaging_context *msg_ctx,
934 struct ndr_syntax_id syntax_id,
935 enum dcerpc_transport_t transport,
938 struct tsocket_address *cli_addr,
939 struct tsocket_address *srv_addr,
941 struct dcerpc_ncacn_conn *ncacn_conn;
942 struct tevent_req *subreq;
949 DEBUG(5, ("dcerpc_ncacn_accept\n"));
951 ncacn_conn = talloc_zero(ev_ctx, struct dcerpc_ncacn_conn);
952 if (ncacn_conn == NULL) {
953 DEBUG(0, ("dcerpc_ncacn_accept: Out of memory!\n"));
960 ncacn_conn->ep.port = port;
964 ncacn_conn->ep.name = talloc_strdup(ncacn_conn, name);
967 DEBUG(0, ("dcerpc_ncacn_accept_function: "
968 "unknown transport: %u!\n", transport));
969 talloc_free(ncacn_conn);
973 ncacn_conn->transport = transport;
974 ncacn_conn->syntax_id = syntax_id;
975 ncacn_conn->ev_ctx = ev_ctx;
976 ncacn_conn->msg_ctx = msg_ctx;
977 ncacn_conn->sock = s;
979 ncacn_conn->client = talloc_move(ncacn_conn, &cli_addr);
980 if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
981 ncacn_conn->client_name =
982 tsocket_address_inet_addr_string(ncacn_conn->client,
985 ncacn_conn->client_name =
986 tsocket_address_unix_path(ncacn_conn->client,
989 if (ncacn_conn->client_name == NULL) {
990 DEBUG(0, ("dcerpc_ncacn_accept: Out of memory!\n"));
991 talloc_free(ncacn_conn);
996 if (srv_addr != NULL) {
997 ncacn_conn->server = talloc_move(ncacn_conn, &srv_addr);
999 ncacn_conn->server_name =
1000 tsocket_address_inet_addr_string(ncacn_conn->server,
1002 if (ncacn_conn->server_name == NULL) {
1003 DEBUG(0, ("dcerpc_ncacn_accept: Out of memory!\n"));
1004 talloc_free(ncacn_conn);
1010 rc = set_blocking(s, false);
1012 DEBUG(2, ("dcerpc_ncacn_accept: Failed to set socket to "
1014 talloc_free(ncacn_conn);
1020 * As soon as we have tstream_bsd_existing_socket set up it will take
1021 * care closing the socket.
1023 rc = tstream_bsd_existing_socket(ncacn_conn, s, &ncacn_conn->tstream);
1025 DEBUG(2, ("dcerpc_ncacn_accept: Failed to create tstream "
1027 talloc_free(ncacn_conn);
1032 switch(ncacn_conn->transport) {
1034 pipe_name = tsocket_address_string(ncacn_conn->client,
1038 pipe_name = talloc_strdup(ncacn_conn,
1039 ncacn_conn->ep.name);
1042 talloc_free(ncacn_conn);
1046 if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
1047 cli_str = tsocket_address_inet_addr_string(ncacn_conn->client,
1049 if (cli_str == NULL) {
1050 talloc_free(ncacn_conn);
1057 if (ncacn_conn->session_info == NULL) {
1058 status = auth_anonymous_session_info(ncacn_conn,
1059 &ncacn_conn->session_info);
1060 if (!NT_STATUS_IS_OK(status)) {
1061 DEBUG(2, ("dcerpc_ncacn_accept: Failed to create "
1062 "auth_anonymous_session_info - %s\n",
1063 nt_errstr(status)));
1064 talloc_free(ncacn_conn);
1069 rc = make_server_pipes_struct(ncacn_conn,
1071 ncacn_conn->syntax_id,
1073 ncacn_conn->session_info,
1077 DEBUG(2, ("dcerpc_ncacn_accept: Failed to create pipe "
1078 "struct - %s", strerror(sys_errno)));
1079 talloc_free(ncacn_conn);
1083 ncacn_conn->send_queue = tevent_queue_create(ncacn_conn,
1084 "dcerpc_tcpip_accept_function");
1085 if (ncacn_conn->send_queue == NULL) {
1086 DEBUG(0, ("dcerpc_ncacn_accept_function: Out of memory!\n"));
1087 talloc_free(ncacn_conn);
1091 subreq = dcerpc_ncacn_read_packet_send(ncacn_conn,
1093 ncacn_conn->tstream);
1094 if (subreq == NULL) {
1095 DEBUG(2, ("dcerpc_ncacn_accept: Failed to send ncacn "
1097 talloc_free(ncacn_conn);
1101 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1103 DEBUG(5, ("dcerpc_ncacn_accept done\n"));
1108 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq)
1110 struct dcerpc_ncacn_conn *ncacn_conn =
1111 tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
1113 struct _output_data *out = &ncacn_conn->p->out_data;
1114 DATA_BLOB recv_buffer = data_blob_null;
1122 status = dcerpc_ncacn_read_packet_recv(subreq, ncacn_conn, &recv_buffer);
1123 TALLOC_FREE(subreq);
1124 if (!NT_STATUS_IS_OK(status)) {
1128 data_left = recv_buffer.length;
1129 data = (char *) recv_buffer.data;
1132 data_used = process_incoming_data(ncacn_conn->p, data, data_left);
1133 if (data_used < 0) {
1134 DEBUG(3, ("Failed to process dcerpc request!\n"));
1135 status = NT_STATUS_UNEXPECTED_IO_ERROR;
1139 data_left -= data_used;
1143 /* Do not leak this buffer */
1144 talloc_free(recv_buffer.data);
1147 * This is needed because of the way DCERPC binds work in the RPC
1150 to_send = out->frag.length - out->current_pdu_sent;
1153 DEBUG(10, ("Current_pdu_len = %u, "
1154 "current_pdu_sent = %u "
1155 "Returning %u bytes\n",
1156 (unsigned int)out->frag.length,
1157 (unsigned int)out->current_pdu_sent,
1158 (unsigned int)to_send));
1160 ncacn_conn->iov = talloc_zero(ncacn_conn, struct iovec);
1161 if (ncacn_conn->iov == NULL) {
1162 status = NT_STATUS_NO_MEMORY;
1165 ncacn_conn->count = 1;
1167 ncacn_conn->iov[0].iov_base = out->frag.data
1168 + out->current_pdu_sent;
1169 ncacn_conn->iov[0].iov_len = to_send;
1171 out->current_pdu_sent += to_send;
1175 * This condition is false for bind packets, or when we haven't yet got
1176 * a full request, and need to wait for more data from the client
1178 while (out->data_sent_length < out->rdata.length) {
1179 ok = create_next_pdu(ncacn_conn->p);
1181 DEBUG(3, ("Failed to create next PDU!\n"));
1182 status = NT_STATUS_UNEXPECTED_IO_ERROR;
1186 ncacn_conn->iov = talloc_realloc(ncacn_conn,
1189 ncacn_conn->count + 1);
1190 if (ncacn_conn->iov == NULL) {
1191 status = NT_STATUS_NO_MEMORY;
1195 ncacn_conn->iov[ncacn_conn->count].iov_base = out->frag.data;
1196 ncacn_conn->iov[ncacn_conn->count].iov_len = out->frag.length;
1198 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
1199 (unsigned int) ncacn_conn->count,
1200 (unsigned int) ncacn_conn->iov[ncacn_conn->count].iov_len));
1201 dump_data(11, (const uint8_t *) ncacn_conn->iov[ncacn_conn->count].iov_base,
1202 ncacn_conn->iov[ncacn_conn->count].iov_len);
1203 ncacn_conn->count++;
1207 * We still don't have a complete request, go back and wait for more
1210 if (ncacn_conn->count == 0) {
1211 /* Wait for the next packet */
1212 subreq = dcerpc_ncacn_read_packet_send(ncacn_conn,
1214 ncacn_conn->tstream);
1215 if (subreq == NULL) {
1216 DEBUG(2, ("Failed to start receving packets\n"));
1217 status = NT_STATUS_NO_MEMORY;
1220 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1224 DEBUG(10, ("Sending a total of %u bytes\n",
1225 (unsigned int)ncacn_conn->p->out_data.data_sent_length));
1227 subreq = tstream_writev_queue_send(ncacn_conn,
1229 ncacn_conn->tstream,
1230 ncacn_conn->send_queue,
1233 if (subreq == NULL) {
1234 DEBUG(2, ("Failed to send packet\n"));
1235 status = NT_STATUS_NO_MEMORY;
1239 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_done, ncacn_conn);
1243 DEBUG(2, ("Fatal error(%s). "
1244 "Terminating client(%s) connection!\n",
1245 nt_errstr(status), ncacn_conn->client_name));
1247 /* Terminate client connection */
1248 talloc_free(ncacn_conn);
1252 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq)
1254 struct dcerpc_ncacn_conn *ncacn_conn =
1255 tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
1259 rc = tstream_writev_queue_recv(subreq, &sys_errno);
1260 TALLOC_FREE(subreq);
1262 DEBUG(2, ("Writev failed!\n"));
1266 /* clear out any data that may have been left around */
1267 ncacn_conn->count = 0;
1268 TALLOC_FREE(ncacn_conn->iov);
1269 data_blob_free(&ncacn_conn->p->in_data.data);
1270 data_blob_free(&ncacn_conn->p->out_data.frag);
1271 data_blob_free(&ncacn_conn->p->out_data.rdata);
1273 /* Wait for the next packet */
1274 subreq = dcerpc_ncacn_read_packet_send(ncacn_conn,
1276 ncacn_conn->tstream);
1277 if (subreq == NULL) {
1278 DEBUG(2, ("Failed to start receving packets\n"));
1283 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1287 DEBUG(2, ("Fatal error(%s). Terminating client(%s) connection!\n",
1288 strerror(sys_errno), ncacn_conn->client_name));
1290 /* Terminate client connection */
1291 talloc_free(ncacn_conn);
1295 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */