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 "lib/tsocket/tsocket.h"
26 #include "libcli/named_pipe_auth/npa_tstream.h"
27 #include "../auth/auth_sam_reply.h"
31 #define SERVER_TCP_LOW_PORT 1024
32 #define SERVER_TCP_HIGH_PORT 1300
34 static NTSTATUS auth_anonymous_session_info(TALLOC_CTX *mem_ctx,
35 struct auth_session_info **session_info)
37 struct auth_session_info *i;
38 struct auth_serversupplied_info *s;
39 struct auth_user_info_dc *u;
40 union netr_Validation val;
43 i = talloc_zero(mem_ctx, struct auth_session_info);
45 return NT_STATUS_NO_MEMORY;
48 status = make_server_info_guest(i, &s);
49 if (!NT_STATUS_IS_OK(status)) {
53 i->security_token = s->security_token;
54 i->session_key = s->session_key;
58 status = make_user_info_dc_netlogon_validation(mem_ctx,
63 if (!NT_STATUS_IS_OK(status)) {
64 DEBUG(0, ("conversion of info3 into user_info_dc failed!\n"));
67 i->info = talloc_move(i, &u->info);
75 /* Creates a pipes_struct and initializes it with the information
76 * sent from the client */
77 static int make_server_pipes_struct(TALLOC_CTX *mem_ctx,
78 const char *pipe_name,
79 const struct ndr_syntax_id id,
80 enum dcerpc_transport_t transport,
81 bool ncalrpc_as_system,
82 const char *client_address,
83 const char *server_address,
84 struct auth_session_info *session_info,
85 struct pipes_struct **_p,
88 struct netr_SamInfo3 *info3;
89 struct auth_user_info_dc *auth_user_info_dc;
90 struct pipes_struct *p;
91 struct auth_serversupplied_info *server_info;
95 p = talloc_zero(mem_ctx, struct pipes_struct);
101 p->transport = transport;
102 p->ncalrpc_as_system = ncalrpc_as_system;
104 p->mem_ctx = talloc_named(p, 0, "pipe %s %p", pipe_name, p);
111 ok = init_pipe_handles(p, &id);
113 DEBUG(1, ("Failed to init handles\n"));
120 data_blob_free(&p->in_data.data);
121 data_blob_free(&p->in_data.pdu);
123 p->endian = RPC_LITTLE_ENDIAN;
125 /* Fake up an auth_user_info_dc for now, to make an info3, to make the session_info structure */
126 auth_user_info_dc = talloc_zero(p, struct auth_user_info_dc);
127 if (!auth_user_info_dc) {
133 auth_user_info_dc->num_sids = session_info->security_token->num_sids;
134 auth_user_info_dc->sids = session_info->security_token->sids;
135 auth_user_info_dc->info = session_info->info;
136 auth_user_info_dc->user_session_key = session_info->session_key;
138 /* This creates the input structure that make_server_info_info3 is looking for */
139 status = auth_convert_user_info_dc_saminfo3(p, auth_user_info_dc,
142 if (!NT_STATUS_IS_OK(status)) {
143 DEBUG(1, ("Failed to convert auth_user_info_dc into netr_SamInfo3\n"));
149 status = make_server_info_info3(p,
150 info3->base.account_name.string,
151 info3->base.domain.string,
152 &server_info, info3);
153 if (!NT_STATUS_IS_OK(status)) {
154 DEBUG(1, ("Failed to init server info\n"));
161 * Some internal functions need a local token to determine access to
164 status = create_local_token(p, server_info, &session_info->session_key, &p->session_info);
165 talloc_free(server_info);
166 if (!NT_STATUS_IS_OK(status)) {
167 DEBUG(1, ("Failed to init local auth token\n"));
173 /* Now override the session_info->security_token with the exact
174 * security_token we were given from the other side,
175 * regardless of what we just calculated */
176 p->session_info->security_token = talloc_move(p->session_info, &session_info->security_token);
178 p->client_id = talloc_zero(p, struct client_address);
184 strlcpy(p->client_id->addr,
185 client_address, sizeof(p->client_id->addr));
186 p->client_id->name = talloc_strdup(p->client_id, client_address);
187 if (p->client_id->name == NULL) {
193 if (server_address != NULL) {
194 p->server_id = talloc_zero(p, struct client_address);
195 if (p->client_id == NULL) {
201 strlcpy(p->server_id->addr,
203 sizeof(p->server_id->addr));
205 p->server_id->name = talloc_strdup(p->server_id,
207 if (p->server_id->name == NULL) {
214 talloc_set_destructor(p, close_internal_rpc_pipe_hnd);
220 /* Start listening on the appropriate unix socket and setup all is needed to
221 * dispatch requests to the pipes rpc implementation */
223 struct dcerpc_ncacn_listen_state {
224 struct ndr_syntax_id syntax_id;
232 struct tevent_context *ev_ctx;
233 struct messaging_context *msg_ctx;
234 dcerpc_ncacn_disconnect_fn disconnect_fn;
237 static void named_pipe_listener(struct tevent_context *ev,
238 struct tevent_fd *fde,
242 bool setup_named_pipe_socket(const char *pipe_name,
243 struct tevent_context *ev_ctx)
245 struct dcerpc_ncacn_listen_state *state;
246 struct tevent_fd *fde;
249 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
251 DEBUG(0, ("Out of memory\n"));
254 state->ep.name = talloc_strdup(state, pipe_name);
255 if (state->ep.name == NULL) {
256 DEBUG(0, ("Out of memory\n"));
261 np_dir = talloc_asprintf(state, "%s/np", lp_ncalrpc_dir());
263 DEBUG(0, ("Out of memory\n"));
267 if (!directory_create_or_exist(np_dir, geteuid(), 0700)) {
268 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
269 np_dir, strerror(errno)));
273 state->fd = create_pipe_sock(np_dir, pipe_name, 0700);
274 if (state->fd == -1) {
275 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
280 DEBUG(10, ("Openened pipe socket fd %d for %s\n",
281 state->fd, pipe_name));
283 fde = tevent_add_fd(ev_ctx,
284 state, state->fd, TEVENT_FD_READ,
285 named_pipe_listener, state);
287 DEBUG(0, ("Failed to add event handler!\n"));
291 tevent_fd_set_auto_close(fde);
295 if (state->fd != -1) {
302 static void named_pipe_accept_function(const char *pipe_name, int fd);
304 static void named_pipe_listener(struct tevent_context *ev,
305 struct tevent_fd *fde,
309 struct dcerpc_ncacn_listen_state *state =
310 talloc_get_type_abort(private_data,
311 struct dcerpc_ncacn_listen_state);
312 struct sockaddr_un sunaddr;
316 /* TODO: should we have a limit to the number of clients ? */
318 len = sizeof(sunaddr);
320 sd = accept(state->fd,
321 (struct sockaddr *)(void *)&sunaddr, &len);
324 if (errno != EINTR) {
325 DEBUG(6, ("Failed to get a valid socket [%s]\n",
331 DEBUG(6, ("Accepted socket %d\n", sd));
333 named_pipe_accept_function(state->ep.name, sd);
337 /* This is the core of the rpc server.
338 * Accepts connections from clients and process requests using the appropriate
339 * dispatcher table. */
341 struct named_pipe_client {
342 const char *pipe_name;
343 struct ndr_syntax_id pipe_id;
345 struct tevent_context *ev;
346 struct messaging_context *msg_ctx;
349 uint16_t device_state;
350 uint64_t allocation_size;
352 struct tstream_context *tstream;
354 struct tsocket_address *client;
356 struct tsocket_address *server;
358 struct auth_session_info *session_info;
360 struct pipes_struct *p;
362 struct tevent_queue *write_queue;
368 static void named_pipe_accept_done(struct tevent_req *subreq);
370 static void named_pipe_accept_function(const char *pipe_name, int fd)
372 struct ndr_syntax_id syntax;
373 struct named_pipe_client *npc;
374 struct tstream_context *plain;
375 struct tevent_req *subreq;
379 ok = is_known_pipename(pipe_name, &syntax);
381 DEBUG(1, ("Unknown pipe [%s]\n", pipe_name));
386 npc = talloc_zero(NULL, struct named_pipe_client);
388 DEBUG(0, ("Out of memory!\n"));
392 npc->pipe_name = pipe_name;
393 npc->pipe_id = syntax;
394 npc->ev = server_event_context();
395 npc->msg_ctx = server_messaging_context();
397 /* make sure socket is in NON blocking state */
398 ret = set_blocking(fd, false);
400 DEBUG(2, ("Failed to make socket non-blocking\n"));
406 ret = tstream_bsd_existing_socket(npc, fd, &plain);
408 DEBUG(2, ("Failed to create tstream socket\n"));
414 npc->file_type = FILE_TYPE_MESSAGE_MODE_PIPE;
415 npc->device_state = 0xff | 0x0400 | 0x0100;
416 npc->allocation_size = 4096;
418 subreq = tstream_npa_accept_existing_send(npc, npc->ev, plain,
421 npc->allocation_size);
423 DEBUG(2, ("Failed to start async accept procedure\n"));
428 tevent_req_set_callback(subreq, named_pipe_accept_done, npc);
431 static void named_pipe_packet_process(struct tevent_req *subreq);
432 static void named_pipe_packet_done(struct tevent_req *subreq);
434 static void named_pipe_accept_done(struct tevent_req *subreq)
436 struct auth_session_info_transport *session_info_transport;
437 struct named_pipe_client *npc =
438 tevent_req_callback_data(subreq, struct named_pipe_client);
439 const char *cli_addr;
443 ret = tstream_npa_accept_existing_recv(subreq, &error, npc,
449 &session_info_transport);
451 npc->session_info = talloc_move(npc, &session_info_transport->session_info);
455 DEBUG(2, ("Failed to accept named pipe connection! (%s)\n",
461 if (tsocket_address_is_inet(npc->client, "ip")) {
462 cli_addr = tsocket_address_inet_addr_string(npc->client,
464 if (cli_addr == NULL) {
472 ret = make_server_pipes_struct(npc,
473 npc->pipe_name, npc->pipe_id, NCACN_NP,
474 false, cli_addr, NULL, npc->session_info,
477 DEBUG(2, ("Failed to create pipes_struct! (%s)\n",
481 npc->p->msg_ctx = npc->msg_ctx;
483 npc->write_queue = tevent_queue_create(npc, "np_server_write_queue");
484 if (!npc->write_queue) {
485 DEBUG(2, ("Failed to set up write queue!\n"));
489 /* And now start receiving and processing packets */
490 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
492 DEBUG(2, ("Failed to start receving packets\n"));
495 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
499 DEBUG(2, ("Fatal error. Terminating client(%s) connection!\n",
501 /* terminate client connection */
506 static void named_pipe_packet_process(struct tevent_req *subreq)
508 struct named_pipe_client *npc =
509 tevent_req_callback_data(subreq, struct named_pipe_client);
510 struct _output_data *out = &npc->p->out_data;
511 DATA_BLOB recv_buffer = data_blob_null;
512 struct ncacn_packet *pkt;
520 status = dcerpc_read_ncacn_packet_recv(subreq, npc, &pkt, &recv_buffer);
522 if (!NT_STATUS_IS_OK(status)) {
526 data_left = recv_buffer.length;
527 data = (char *)recv_buffer.data;
531 data_used = process_incoming_data(npc->p, data, data_left);
533 DEBUG(3, ("Failed to process dceprc request!\n"));
534 status = NT_STATUS_UNEXPECTED_IO_ERROR;
538 data_left -= data_used;
542 /* Do not leak this buffer, npc is a long lived context */
543 talloc_free(recv_buffer.data);
546 /* this is needed because of the way DCERPC Binds work in
547 * the RPC marshalling code */
548 to_send = out->frag.length - out->current_pdu_sent;
551 DEBUG(10, ("Current_pdu_len = %u, "
552 "current_pdu_sent = %u "
553 "Returning %u bytes\n",
554 (unsigned int)out->frag.length,
555 (unsigned int)out->current_pdu_sent,
556 (unsigned int)to_send));
558 npc->iov = talloc_zero(npc, struct iovec);
560 status = NT_STATUS_NO_MEMORY;
565 npc->iov[0].iov_base = out->frag.data
566 + out->current_pdu_sent;
567 npc->iov[0].iov_len = to_send;
569 out->current_pdu_sent += to_send;
572 /* this condition is false for bind packets, or when we haven't
573 * yet got a full request, and need to wait for more data from
575 while (out->data_sent_length < out->rdata.length) {
577 ok = create_next_pdu(npc->p);
579 DEBUG(3, ("Failed to create next PDU!\n"));
580 status = NT_STATUS_UNEXPECTED_IO_ERROR;
584 npc->iov = talloc_realloc(npc, npc->iov,
585 struct iovec, npc->count + 1);
587 status = NT_STATUS_NO_MEMORY;
591 npc->iov[npc->count].iov_base = out->frag.data;
592 npc->iov[npc->count].iov_len = out->frag.length;
594 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
595 (unsigned int)npc->count,
596 (unsigned int)npc->iov[npc->count].iov_len));
597 dump_data(11, (const uint8_t *)npc->iov[npc->count].iov_base,
598 npc->iov[npc->count].iov_len);
602 /* we still don't have a complete request, go back and wait for more
604 if (npc->count == 0) {
605 /* Wait for the next packet */
606 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
608 DEBUG(2, ("Failed to start receving packets\n"));
609 status = NT_STATUS_NO_MEMORY;
612 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
616 DEBUG(10, ("Sending a total of %u bytes\n",
617 (unsigned int)npc->p->out_data.data_sent_length));
619 subreq = tstream_writev_queue_send(npc, npc->ev,
622 npc->iov, npc->count);
624 DEBUG(2, ("Failed to send packet\n"));
625 status = NT_STATUS_NO_MEMORY;
628 tevent_req_set_callback(subreq, named_pipe_packet_done, npc);
632 DEBUG(2, ("Fatal error(%s). "
633 "Terminating client(%s) connection!\n",
634 nt_errstr(status), npc->client_name));
635 /* terminate client connection */
640 static void named_pipe_packet_done(struct tevent_req *subreq)
642 struct named_pipe_client *npc =
643 tevent_req_callback_data(subreq, struct named_pipe_client);
647 ret = tstream_writev_queue_recv(subreq, &sys_errno);
650 DEBUG(2, ("Writev failed!\n"));
654 /* clear out any data that may have been left around */
656 TALLOC_FREE(npc->iov);
657 data_blob_free(&npc->p->in_data.data);
658 data_blob_free(&npc->p->out_data.frag);
659 data_blob_free(&npc->p->out_data.rdata);
661 /* Wait for the next packet */
662 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
664 DEBUG(2, ("Failed to start receving packets\n"));
668 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
672 DEBUG(2, ("Fatal error(%s). "
673 "Terminating client(%s) connection!\n",
674 strerror(sys_errno), npc->client_name));
675 /* terminate client connection */
680 static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
681 struct messaging_context *msg_ctx,
682 struct ndr_syntax_id syntax_id,
683 enum dcerpc_transport_t transport,
686 struct tsocket_address *cli_addr,
687 struct tsocket_address *srv_addr,
689 dcerpc_ncacn_disconnect_fn fn);
691 /********************************************************************
692 * Start listening on the tcp/ip socket
693 ********************************************************************/
695 static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
696 struct tevent_fd *fde,
700 uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context *ev_ctx,
701 struct messaging_context *msg_ctx,
702 struct ndr_syntax_id syntax_id,
703 const struct sockaddr_storage *ifss,
706 struct dcerpc_ncacn_listen_state *state;
707 struct tevent_fd *fde;
710 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
712 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Out of memory\n"));
716 state->syntax_id = syntax_id;
718 state->ep.port = port;
719 state->disconnect_fn = NULL;
721 if (state->ep.port == 0) {
724 for (i = SERVER_TCP_LOW_PORT; i <= SERVER_TCP_HIGH_PORT; i++) {
725 state->fd = open_socket_in(SOCK_STREAM,
736 state->fd = open_socket_in(SOCK_STREAM,
742 if (state->fd == -1) {
743 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Failed to create "
744 "socket on port %u!\n", state->ep.port));
748 state->ev_ctx = ev_ctx;
749 state->msg_ctx = msg_ctx;
751 /* ready to listen */
752 set_socket_options(state->fd, "SO_KEEPALIVE");
753 set_socket_options(state->fd, lp_socket_options());
755 /* Set server socket to non-blocking for the accept. */
756 set_blocking(state->fd, false);
758 rc = listen(state->fd, SMBD_LISTEN_BACKLOG);
760 DEBUG(0,("setup_tcpip_socket: listen - %s\n", strerror(errno)));
764 DEBUG(10, ("setup_tcpip_socket: openened socket fd %d for port %u\n",
765 state->fd, state->ep.port));
767 fde = tevent_add_fd(state->ev_ctx,
771 dcerpc_ncacn_tcpip_listener,
774 DEBUG(0, ("setup_tcpip_socket: Failed to add event handler!\n"));
778 tevent_fd_set_auto_close(fde);
780 return state->ep.port;
782 if (state->fd != -1) {
790 static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
791 struct tevent_fd *fde,
795 struct dcerpc_ncacn_listen_state *state =
796 talloc_get_type_abort(private_data,
797 struct dcerpc_ncacn_listen_state);
798 struct tsocket_address *cli_addr = NULL;
799 struct tsocket_address *srv_addr = NULL;
800 struct sockaddr_storage addr;
801 socklen_t in_addrlen = sizeof(addr);
805 s = accept(state->fd, (struct sockaddr *)(void *) &addr, &in_addrlen);
807 if (errno != EINTR) {
808 DEBUG(0,("tcpip_listener accept: %s\n",
814 rc = tsocket_address_bsd_from_sockaddr(state,
815 (struct sockaddr *)(void *) &addr,
823 rc = getsockname(s, (struct sockaddr *)(void *) &addr, &in_addrlen);
829 rc = tsocket_address_bsd_from_sockaddr(state,
830 (struct sockaddr *)(void *) &addr,
838 DEBUG(6, ("tcpip_listener: Accepted socket %d\n", s));
840 dcerpc_ncacn_accept(state->ev_ctx,
852 /********************************************************************
853 * Start listening on the ncalrpc socket
854 ********************************************************************/
856 static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
857 struct tevent_fd *fde,
861 bool setup_dcerpc_ncalrpc_socket(struct tevent_context *ev_ctx,
862 struct messaging_context *msg_ctx,
863 struct ndr_syntax_id syntax_id,
865 dcerpc_ncacn_disconnect_fn fn)
867 struct dcerpc_ncacn_listen_state *state;
868 struct tevent_fd *fde;
870 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
872 DEBUG(0, ("Out of memory\n"));
876 state->syntax_id = syntax_id;
878 state->disconnect_fn = fn;
883 state->ep.name = talloc_strdup(state, name);
885 if (state->ep.name == NULL) {
886 DEBUG(0, ("Out of memory\n"));
891 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0700)) {
892 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
893 lp_ncalrpc_dir(), strerror(errno)));
897 state->fd = create_pipe_sock(lp_ncalrpc_dir(), name, 0700);
898 if (state->fd == -1) {
899 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
900 lp_ncalrpc_dir(), name));
904 DEBUG(10, ("Openened pipe socket fd %d for %s\n", state->fd, name));
906 state->ev_ctx = ev_ctx;
907 state->msg_ctx = msg_ctx;
909 /* Set server socket to non-blocking for the accept. */
910 set_blocking(state->fd, false);
912 fde = tevent_add_fd(state->ev_ctx,
916 dcerpc_ncalrpc_listener,
919 DEBUG(0, ("Failed to add event handler for ncalrpc!\n"));
923 tevent_fd_set_auto_close(fde);
927 if (state->fd != -1) {
935 static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
936 struct tevent_fd *fde,
940 struct dcerpc_ncacn_listen_state *state =
941 talloc_get_type_abort(private_data,
942 struct dcerpc_ncacn_listen_state);
943 struct tsocket_address *cli_addr = NULL;
944 struct sockaddr_un sunaddr;
945 struct sockaddr *addr = (struct sockaddr *)(void *)&sunaddr;
946 socklen_t len = sizeof(sunaddr);
950 ZERO_STRUCT(sunaddr);
952 sd = accept(state->fd, addr, &len);
954 if (errno != EINTR) {
955 DEBUG(0, ("ncalrpc accept() failed: %s\n", strerror(errno)));
960 rc = tsocket_address_bsd_from_sockaddr(state,
968 DEBUG(10, ("Accepted ncalrpc socket %d\n", sd));
970 dcerpc_ncacn_accept(state->ev_ctx,
972 state->syntax_id, NCALRPC,
975 state->disconnect_fn);
978 struct dcerpc_ncacn_conn {
979 struct ndr_syntax_id syntax_id;
981 enum dcerpc_transport_t transport;
990 struct pipes_struct *p;
991 dcerpc_ncacn_disconnect_fn disconnect_fn;
993 struct tevent_context *ev_ctx;
994 struct messaging_context *msg_ctx;
996 struct tstream_context *tstream;
997 struct tevent_queue *send_queue;
999 struct tsocket_address *client;
1001 struct tsocket_address *server;
1003 struct auth_session_info *session_info;
1009 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq);
1010 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq);
1012 static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
1013 struct messaging_context *msg_ctx,
1014 struct ndr_syntax_id syntax_id,
1015 enum dcerpc_transport_t transport,
1018 struct tsocket_address *cli_addr,
1019 struct tsocket_address *srv_addr,
1021 dcerpc_ncacn_disconnect_fn fn) {
1022 struct dcerpc_ncacn_conn *ncacn_conn;
1023 struct tevent_req *subreq;
1024 const char *cli_str;
1025 const char *srv_str = NULL;
1026 bool system_user = false;
1033 DEBUG(10, ("dcerpc_ncacn_accept\n"));
1035 ncacn_conn = talloc_zero(ev_ctx, struct dcerpc_ncacn_conn);
1036 if (ncacn_conn == NULL) {
1037 DEBUG(0, ("Out of memory!\n"));
1042 ncacn_conn->transport = transport;
1043 ncacn_conn->syntax_id = syntax_id;
1044 ncacn_conn->ev_ctx = ev_ctx;
1045 ncacn_conn->msg_ctx = msg_ctx;
1046 ncacn_conn->sock = s;
1047 ncacn_conn->disconnect_fn = fn;
1049 ncacn_conn->client = talloc_move(ncacn_conn, &cli_addr);
1050 if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
1051 ncacn_conn->client_name =
1052 tsocket_address_inet_addr_string(ncacn_conn->client,
1055 ncacn_conn->client_name =
1056 tsocket_address_unix_path(ncacn_conn->client,
1059 if (ncacn_conn->client_name == NULL) {
1060 DEBUG(0, ("Out of memory!\n"));
1061 talloc_free(ncacn_conn);
1066 if (srv_addr != NULL) {
1067 ncacn_conn->server = talloc_move(ncacn_conn, &srv_addr);
1069 ncacn_conn->server_name =
1070 tsocket_address_inet_addr_string(ncacn_conn->server,
1072 if (ncacn_conn->server_name == NULL) {
1073 DEBUG(0, ("Out of memory!\n"));
1074 talloc_free(ncacn_conn);
1080 switch (transport) {
1082 ncacn_conn->ep.port = port;
1084 pipe_name = tsocket_address_string(ncacn_conn->client,
1086 if (pipe_name == NULL) {
1088 talloc_free(ncacn_conn);
1094 rc = sys_getpeereid(s, &uid);
1096 DEBUG(2, ("Failed to get ncalrpc connecting uid!"));
1098 if (uid == sec_initial_uid()) {
1103 ncacn_conn->ep.name = talloc_strdup(ncacn_conn, name);
1104 if (ncacn_conn->ep.name == NULL) {
1106 talloc_free(ncacn_conn);
1110 pipe_name = talloc_strdup(ncacn_conn,
1112 if (pipe_name == NULL) {
1114 talloc_free(ncacn_conn);
1119 DEBUG(0, ("unknown dcerpc transport: %u!\n",
1121 talloc_free(ncacn_conn);
1126 rc = set_blocking(s, false);
1128 DEBUG(2, ("Failed to set dcerpc socket to non-blocking\n"));
1129 talloc_free(ncacn_conn);
1135 * As soon as we have tstream_bsd_existing_socket set up it will
1136 * take care of closing the socket.
1138 rc = tstream_bsd_existing_socket(ncacn_conn, s, &ncacn_conn->tstream);
1140 DEBUG(2, ("Failed to create tstream socket for dcerpc\n"));
1141 talloc_free(ncacn_conn);
1146 if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
1147 cli_str = ncacn_conn->client_name;
1152 if (ncacn_conn->server != NULL) {
1153 if (tsocket_address_is_inet(ncacn_conn->server, "ip")) {
1154 srv_str = ncacn_conn->server_name;
1160 if (ncacn_conn->session_info == NULL) {
1161 status = auth_anonymous_session_info(ncacn_conn,
1162 &ncacn_conn->session_info);
1163 if (!NT_STATUS_IS_OK(status)) {
1164 DEBUG(2, ("Failed to create "
1165 "auth_anonymous_session_info - %s\n",
1166 nt_errstr(status)));
1167 talloc_free(ncacn_conn);
1172 rc = make_server_pipes_struct(ncacn_conn,
1174 ncacn_conn->syntax_id,
1175 ncacn_conn->transport,
1179 ncacn_conn->session_info,
1183 DEBUG(2, ("Failed to create pipe struct - %s",
1184 strerror(sys_errno)));
1185 talloc_free(ncacn_conn);
1189 ncacn_conn->send_queue = tevent_queue_create(ncacn_conn,
1190 "dcerpc send queue");
1191 if (ncacn_conn->send_queue == NULL) {
1192 DEBUG(0, ("Out of memory!\n"));
1193 talloc_free(ncacn_conn);
1197 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1199 ncacn_conn->tstream);
1200 if (subreq == NULL) {
1201 DEBUG(2, ("Failed to send ncacn packet\n"));
1202 talloc_free(ncacn_conn);
1206 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1208 DEBUG(10, ("dcerpc_ncacn_accept done\n"));
1213 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq)
1215 struct dcerpc_ncacn_conn *ncacn_conn =
1216 tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
1218 struct _output_data *out = &ncacn_conn->p->out_data;
1219 DATA_BLOB recv_buffer = data_blob_null;
1220 struct ncacn_packet *pkt;
1228 status = dcerpc_read_ncacn_packet_recv(subreq, ncacn_conn, &pkt, &recv_buffer);
1229 TALLOC_FREE(subreq);
1230 if (!NT_STATUS_IS_OK(status)) {
1231 if (ncacn_conn->disconnect_fn != NULL) {
1232 ok = ncacn_conn->disconnect_fn(ncacn_conn->p);
1234 DEBUG(3, ("Failed to call disconnect function\n"));
1240 data_left = recv_buffer.length;
1241 data = (char *) recv_buffer.data;
1244 data_used = process_incoming_data(ncacn_conn->p, data, data_left);
1245 if (data_used < 0) {
1246 DEBUG(3, ("Failed to process dcerpc request!\n"));
1247 status = NT_STATUS_UNEXPECTED_IO_ERROR;
1251 data_left -= data_used;
1255 /* Do not leak this buffer */
1256 talloc_free(recv_buffer.data);
1260 * This is needed because of the way DCERPC binds work in the RPC
1263 to_send = out->frag.length - out->current_pdu_sent;
1266 DEBUG(10, ("Current_pdu_len = %u, "
1267 "current_pdu_sent = %u "
1268 "Returning %u bytes\n",
1269 (unsigned int)out->frag.length,
1270 (unsigned int)out->current_pdu_sent,
1271 (unsigned int)to_send));
1273 ncacn_conn->iov = talloc_zero(ncacn_conn, struct iovec);
1274 if (ncacn_conn->iov == NULL) {
1275 status = NT_STATUS_NO_MEMORY;
1276 DEBUG(3, ("Out of memory!\n"));
1279 ncacn_conn->count = 1;
1281 ncacn_conn->iov[0].iov_base = out->frag.data
1282 + out->current_pdu_sent;
1283 ncacn_conn->iov[0].iov_len = to_send;
1285 out->current_pdu_sent += to_send;
1289 * This condition is false for bind packets, or when we haven't yet got
1290 * a full request, and need to wait for more data from the client
1292 while (out->data_sent_length < out->rdata.length) {
1293 ok = create_next_pdu(ncacn_conn->p);
1295 DEBUG(3, ("Failed to create next PDU!\n"));
1296 status = NT_STATUS_UNEXPECTED_IO_ERROR;
1300 ncacn_conn->iov = talloc_realloc(ncacn_conn,
1303 ncacn_conn->count + 1);
1304 if (ncacn_conn->iov == NULL) {
1305 DEBUG(3, ("Out of memory!\n"));
1306 status = NT_STATUS_NO_MEMORY;
1310 ncacn_conn->iov[ncacn_conn->count].iov_base = out->frag.data;
1311 ncacn_conn->iov[ncacn_conn->count].iov_len = out->frag.length;
1313 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
1314 (unsigned int) ncacn_conn->count,
1315 (unsigned int) ncacn_conn->iov[ncacn_conn->count].iov_len));
1316 dump_data(11, (const uint8_t *) ncacn_conn->iov[ncacn_conn->count].iov_base,
1317 ncacn_conn->iov[ncacn_conn->count].iov_len);
1318 ncacn_conn->count++;
1322 * We still don't have a complete request, go back and wait for more
1325 if (ncacn_conn->count == 0) {
1326 /* Wait for the next packet */
1327 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1329 ncacn_conn->tstream);
1330 if (subreq == NULL) {
1331 DEBUG(2, ("Failed to start receving packets\n"));
1332 status = NT_STATUS_NO_MEMORY;
1335 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1339 DEBUG(10, ("Sending a total of %u bytes\n",
1340 (unsigned int)ncacn_conn->p->out_data.data_sent_length));
1342 subreq = tstream_writev_queue_send(ncacn_conn,
1344 ncacn_conn->tstream,
1345 ncacn_conn->send_queue,
1348 if (subreq == NULL) {
1349 DEBUG(2, ("Failed to send packet\n"));
1350 status = NT_STATUS_NO_MEMORY;
1354 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_done, ncacn_conn);
1358 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1359 ncacn_conn->client_name, nt_errstr(status)));
1361 /* Terminate client connection */
1362 talloc_free(ncacn_conn);
1366 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq)
1368 struct dcerpc_ncacn_conn *ncacn_conn =
1369 tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
1370 NTSTATUS status = NT_STATUS_OK;
1374 rc = tstream_writev_queue_recv(subreq, &sys_errno);
1375 TALLOC_FREE(subreq);
1377 DEBUG(2, ("Writev failed!\n"));
1378 status = map_nt_error_from_unix(sys_errno);
1382 /* clear out any data that may have been left around */
1383 ncacn_conn->count = 0;
1384 TALLOC_FREE(ncacn_conn->iov);
1385 data_blob_free(&ncacn_conn->p->in_data.data);
1386 data_blob_free(&ncacn_conn->p->out_data.frag);
1387 data_blob_free(&ncacn_conn->p->out_data.rdata);
1389 /* Wait for the next packet */
1390 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1392 ncacn_conn->tstream);
1393 if (subreq == NULL) {
1394 DEBUG(2, ("Failed to start receving packets\n"));
1395 status = NT_STATUS_NO_MEMORY;
1399 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1403 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1404 ncacn_conn->client_name, nt_errstr(status)));
1406 /* Terminate client connection */
1407 talloc_free(ncacn_conn);
1411 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */