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/>.
22 #include "rpc_server/rpc_server.h"
24 #include "librpc/gen_ndr/netlogon.h"
25 #include "librpc/gen_ndr/auth.h"
26 #include "lib/tsocket/tsocket.h"
27 #include "libcli/named_pipe_auth/npa_tstream.h"
28 #include "../auth/auth_sam_reply.h"
30 #include "rpc_server/rpc_ncacn_np.h"
31 #include "rpc_server/srv_pipe_hnd.h"
32 #include "rpc_server/srv_pipe.h"
34 #define SERVER_TCP_LOW_PORT 1024
35 #define SERVER_TCP_HIGH_PORT 1300
37 static NTSTATUS auth_anonymous_session_info(TALLOC_CTX *mem_ctx,
38 struct auth_session_info **session_info)
40 struct auth_session_info *i;
41 struct auth_serversupplied_info *s;
42 struct auth_user_info_dc *u;
43 union netr_Validation val;
46 i = talloc_zero(mem_ctx, struct auth_session_info);
48 return NT_STATUS_NO_MEMORY;
51 status = make_server_info_guest(i, &s);
52 if (!NT_STATUS_IS_OK(status)) {
56 i->security_token = s->security_token;
57 i->session_key = s->session_key;
61 status = make_user_info_dc_netlogon_validation(mem_ctx,
66 if (!NT_STATUS_IS_OK(status)) {
67 DEBUG(0, ("conversion of info3 into user_info_dc failed!\n"));
70 i->info = talloc_move(i, &u->info);
78 /* Creates a pipes_struct and initializes it with the information
79 * sent from the client */
80 static int make_server_pipes_struct(TALLOC_CTX *mem_ctx,
81 const char *pipe_name,
82 enum dcerpc_transport_t transport,
83 bool ncalrpc_as_system,
84 const struct tsocket_address *local_address,
85 const struct tsocket_address *remote_address,
86 struct auth_session_info *session_info,
87 struct pipes_struct **_p,
90 struct netr_SamInfo3 *info3;
91 struct auth_user_info_dc *auth_user_info_dc;
92 struct pipes_struct *p;
93 struct auth_serversupplied_info *server_info;
96 p = talloc_zero(mem_ctx, struct pipes_struct);
102 p->transport = transport;
103 p->ncalrpc_as_system = ncalrpc_as_system;
105 p->mem_ctx = talloc_named(p, 0, "pipe %s %p", pipe_name, p);
112 data_blob_free(&p->in_data.data);
113 data_blob_free(&p->in_data.pdu);
115 p->endian = RPC_LITTLE_ENDIAN;
117 /* Fake up an auth_user_info_dc for now, to make an info3, to make the session_info structure */
118 auth_user_info_dc = talloc_zero(p, struct auth_user_info_dc);
119 if (!auth_user_info_dc) {
125 auth_user_info_dc->num_sids = session_info->security_token->num_sids;
126 auth_user_info_dc->sids = session_info->security_token->sids;
127 auth_user_info_dc->info = session_info->info;
128 auth_user_info_dc->user_session_key = session_info->session_key;
130 /* This creates the input structure that make_server_info_info3 is looking for */
131 status = auth_convert_user_info_dc_saminfo3(p, auth_user_info_dc,
134 if (!NT_STATUS_IS_OK(status)) {
135 DEBUG(1, ("Failed to convert auth_user_info_dc into netr_SamInfo3\n"));
141 status = make_server_info_info3(p,
142 info3->base.account_name.string,
143 info3->base.domain.string,
144 &server_info, info3);
145 if (!NT_STATUS_IS_OK(status)) {
146 DEBUG(1, ("Failed to init server info\n"));
153 * Some internal functions need a local token to determine access to
156 status = create_local_token(p, server_info, &session_info->session_key, &p->session_info);
157 talloc_free(server_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 p->remote_address = tsocket_address_copy(remote_address, p);
171 if (p->remote_address == NULL) {
177 if (local_address != NULL) {
178 p->local_address = tsocket_address_copy(local_address, p);
179 if (p->local_address == NULL) {
186 talloc_set_destructor(p, close_internal_rpc_pipe_hnd);
192 /* Start listening on the appropriate unix socket and setup all is needed to
193 * dispatch requests to the pipes rpc implementation */
195 struct dcerpc_ncacn_listen_state {
196 struct ndr_syntax_id syntax_id;
204 struct tevent_context *ev_ctx;
205 struct messaging_context *msg_ctx;
206 dcerpc_ncacn_disconnect_fn disconnect_fn;
209 static void named_pipe_listener(struct tevent_context *ev,
210 struct tevent_fd *fde,
214 bool setup_named_pipe_socket(const char *pipe_name,
215 struct tevent_context *ev_ctx,
216 struct messaging_context *msg_ctx)
218 struct dcerpc_ncacn_listen_state *state;
219 struct tevent_fd *fde;
222 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
224 DEBUG(0, ("Out of memory\n"));
227 state->ep.name = talloc_strdup(state, pipe_name);
228 if (state->ep.name == NULL) {
229 DEBUG(0, ("Out of memory\n"));
234 state->ev_ctx = ev_ctx;
235 state->msg_ctx = msg_ctx;
238 * As lp_ncalrpc_dir() should have 0755, but
239 * lp_ncalrpc_dir()/np should have 0700, we need to
240 * create lp_ncalrpc_dir() first.
242 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) {
243 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
244 lp_ncalrpc_dir(), strerror(errno)));
248 np_dir = talloc_asprintf(state, "%s/np", lp_ncalrpc_dir());
250 DEBUG(0, ("Out of memory\n"));
254 if (!directory_create_or_exist(np_dir, geteuid(), 0700)) {
255 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
256 np_dir, strerror(errno)));
260 state->fd = create_pipe_sock(np_dir, pipe_name, 0700);
261 if (state->fd == -1) {
262 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
267 DEBUG(10, ("Openened pipe socket fd %d for %s\n",
268 state->fd, pipe_name));
270 fde = tevent_add_fd(ev_ctx,
271 state, state->fd, TEVENT_FD_READ,
272 named_pipe_listener, state);
274 DEBUG(0, ("Failed to add event handler!\n"));
278 tevent_fd_set_auto_close(fde);
282 if (state->fd != -1) {
289 static void named_pipe_accept_function(struct tevent_context *ev_ctx,
290 struct messaging_context *msg_ctx,
291 const char *pipe_name,
294 static void named_pipe_listener(struct tevent_context *ev,
295 struct tevent_fd *fde,
299 struct dcerpc_ncacn_listen_state *state =
300 talloc_get_type_abort(private_data,
301 struct dcerpc_ncacn_listen_state);
302 struct sockaddr_un sunaddr;
306 /* TODO: should we have a limit to the number of clients ? */
308 len = sizeof(sunaddr);
310 sd = accept(state->fd,
311 (struct sockaddr *)(void *)&sunaddr, &len);
314 if (errno != EINTR) {
315 DEBUG(6, ("Failed to get a valid socket [%s]\n",
321 DEBUG(6, ("Accepted socket %d\n", sd));
323 named_pipe_accept_function(state->ev_ctx,
330 /* This is the core of the rpc server.
331 * Accepts connections from clients and process requests using the appropriate
332 * dispatcher table. */
334 struct named_pipe_client {
335 const char *pipe_name;
337 struct tevent_context *ev;
338 struct messaging_context *msg_ctx;
341 uint16_t device_state;
342 uint64_t allocation_size;
344 struct tstream_context *tstream;
346 struct tsocket_address *client;
348 struct tsocket_address *server;
351 struct auth_session_info *session_info;
353 struct pipes_struct *p;
355 struct tevent_queue *write_queue;
361 static void named_pipe_accept_done(struct tevent_req *subreq);
363 static void named_pipe_accept_function(struct tevent_context *ev_ctx,
364 struct messaging_context *msg_ctx,
365 const char *pipe_name,
368 struct named_pipe_client *npc;
369 struct tstream_context *plain;
370 struct tevent_req *subreq;
373 npc = talloc_zero(NULL, struct named_pipe_client);
375 DEBUG(0, ("Out of memory!\n"));
379 npc->pipe_name = pipe_name;
381 npc->msg_ctx = msg_ctx;
383 /* make sure socket is in NON blocking state */
384 ret = set_blocking(fd, false);
386 DEBUG(2, ("Failed to make socket non-blocking\n"));
392 ret = tstream_bsd_existing_socket(npc, fd, &plain);
394 DEBUG(2, ("Failed to create tstream socket\n"));
400 npc->file_type = FILE_TYPE_MESSAGE_MODE_PIPE;
401 npc->device_state = 0xff | 0x0400 | 0x0100;
402 npc->allocation_size = 4096;
404 subreq = tstream_npa_accept_existing_send(npc, npc->ev, plain,
407 npc->allocation_size);
409 DEBUG(2, ("Failed to start async accept procedure\n"));
414 tevent_req_set_callback(subreq, named_pipe_accept_done, npc);
417 static void named_pipe_packet_process(struct tevent_req *subreq);
418 static void named_pipe_packet_done(struct tevent_req *subreq);
420 static void named_pipe_accept_done(struct tevent_req *subreq)
422 struct auth_session_info_transport *session_info_transport;
423 struct named_pipe_client *npc =
424 tevent_req_callback_data(subreq, struct named_pipe_client);
428 ret = tstream_npa_accept_existing_recv(subreq, &error, npc,
434 &session_info_transport);
436 npc->session_info = talloc_move(npc, &session_info_transport->session_info);
440 DEBUG(2, ("Failed to accept named pipe connection! (%s)\n",
446 ret = make_server_pipes_struct(npc,
447 npc->pipe_name, NCACN_NP,
448 false, npc->server, npc->client, npc->session_info,
451 DEBUG(2, ("Failed to create pipes_struct! (%s)\n",
455 npc->p->msg_ctx = npc->msg_ctx;
457 npc->write_queue = tevent_queue_create(npc, "np_server_write_queue");
458 if (!npc->write_queue) {
459 DEBUG(2, ("Failed to set up write queue!\n"));
463 /* And now start receiving and processing packets */
464 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
466 DEBUG(2, ("Failed to start receving packets\n"));
469 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
473 DEBUG(2, ("Fatal error. Terminating client(%s) connection!\n",
475 /* terminate client connection */
480 static void named_pipe_packet_process(struct tevent_req *subreq)
482 struct named_pipe_client *npc =
483 tevent_req_callback_data(subreq, struct named_pipe_client);
484 struct _output_data *out = &npc->p->out_data;
485 DATA_BLOB recv_buffer = data_blob_null;
486 struct ncacn_packet *pkt;
494 status = dcerpc_read_ncacn_packet_recv(subreq, npc, &pkt, &recv_buffer);
496 if (!NT_STATUS_IS_OK(status)) {
500 data_left = recv_buffer.length;
501 data = (char *)recv_buffer.data;
505 data_used = process_incoming_data(npc->p, data, data_left);
507 DEBUG(3, ("Failed to process dceprc request!\n"));
508 status = NT_STATUS_UNEXPECTED_IO_ERROR;
512 data_left -= data_used;
516 /* Do not leak this buffer, npc is a long lived context */
517 talloc_free(recv_buffer.data);
520 /* this is needed because of the way DCERPC Binds work in
521 * the RPC marshalling code */
522 to_send = out->frag.length - out->current_pdu_sent;
525 DEBUG(10, ("Current_pdu_len = %u, "
526 "current_pdu_sent = %u "
527 "Returning %u bytes\n",
528 (unsigned int)out->frag.length,
529 (unsigned int)out->current_pdu_sent,
530 (unsigned int)to_send));
532 npc->iov = talloc_zero(npc, struct iovec);
534 status = NT_STATUS_NO_MEMORY;
539 npc->iov[0].iov_base = out->frag.data
540 + out->current_pdu_sent;
541 npc->iov[0].iov_len = to_send;
543 out->current_pdu_sent += to_send;
546 /* this condition is false for bind packets, or when we haven't
547 * yet got a full request, and need to wait for more data from
549 while (out->data_sent_length < out->rdata.length) {
551 ok = create_next_pdu(npc->p);
553 DEBUG(3, ("Failed to create next PDU!\n"));
554 status = NT_STATUS_UNEXPECTED_IO_ERROR;
558 npc->iov = talloc_realloc(npc, npc->iov,
559 struct iovec, npc->count + 1);
561 status = NT_STATUS_NO_MEMORY;
565 npc->iov[npc->count].iov_base = out->frag.data;
566 npc->iov[npc->count].iov_len = out->frag.length;
568 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
569 (unsigned int)npc->count,
570 (unsigned int)npc->iov[npc->count].iov_len));
571 dump_data(11, (const uint8_t *)npc->iov[npc->count].iov_base,
572 npc->iov[npc->count].iov_len);
576 /* we still don't have a complete request, go back and wait for more
578 if (npc->count == 0) {
579 /* Wait for the next packet */
580 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
582 DEBUG(2, ("Failed to start receving packets\n"));
583 status = NT_STATUS_NO_MEMORY;
586 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
590 DEBUG(10, ("Sending a total of %u bytes\n",
591 (unsigned int)npc->p->out_data.data_sent_length));
593 subreq = tstream_writev_queue_send(npc, npc->ev,
596 npc->iov, npc->count);
598 DEBUG(2, ("Failed to send packet\n"));
599 status = NT_STATUS_NO_MEMORY;
602 tevent_req_set_callback(subreq, named_pipe_packet_done, npc);
606 DEBUG(2, ("Fatal error(%s). "
607 "Terminating client(%s) connection!\n",
608 nt_errstr(status), npc->client_name));
609 /* terminate client connection */
614 static void named_pipe_packet_done(struct tevent_req *subreq)
616 struct named_pipe_client *npc =
617 tevent_req_callback_data(subreq, struct named_pipe_client);
621 ret = tstream_writev_queue_recv(subreq, &sys_errno);
624 DEBUG(2, ("Writev failed!\n"));
628 /* clear out any data that may have been left around */
630 TALLOC_FREE(npc->iov);
631 data_blob_free(&npc->p->in_data.data);
632 data_blob_free(&npc->p->out_data.frag);
633 data_blob_free(&npc->p->out_data.rdata);
635 /* Wait for the next packet */
636 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
638 DEBUG(2, ("Failed to start receving packets\n"));
642 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
646 DEBUG(2, ("Fatal error(%s). "
647 "Terminating client(%s) connection!\n",
648 strerror(sys_errno), npc->client_name));
649 /* terminate client connection */
654 static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
655 struct messaging_context *msg_ctx,
656 enum dcerpc_transport_t transport,
659 struct tsocket_address *cli_addr,
660 struct tsocket_address *srv_addr,
662 dcerpc_ncacn_disconnect_fn fn);
664 /********************************************************************
665 * Start listening on the tcp/ip socket
666 ********************************************************************/
668 static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
669 struct tevent_fd *fde,
673 uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context *ev_ctx,
674 struct messaging_context *msg_ctx,
675 const struct sockaddr_storage *ifss,
678 struct dcerpc_ncacn_listen_state *state;
679 struct tevent_fd *fde;
682 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
684 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Out of memory\n"));
689 state->ep.port = port;
690 state->disconnect_fn = NULL;
692 if (state->ep.port == 0) {
695 for (i = SERVER_TCP_LOW_PORT; i <= SERVER_TCP_HIGH_PORT; i++) {
696 state->fd = open_socket_in(SOCK_STREAM,
707 state->fd = open_socket_in(SOCK_STREAM,
713 if (state->fd == -1) {
714 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Failed to create "
715 "socket on port %u!\n", state->ep.port));
719 state->ev_ctx = ev_ctx;
720 state->msg_ctx = msg_ctx;
722 /* ready to listen */
723 set_socket_options(state->fd, "SO_KEEPALIVE");
724 set_socket_options(state->fd, lp_socket_options());
726 /* Set server socket to non-blocking for the accept. */
727 set_blocking(state->fd, false);
729 rc = listen(state->fd, SMBD_LISTEN_BACKLOG);
731 DEBUG(0,("setup_tcpip_socket: listen - %s\n", strerror(errno)));
735 DEBUG(10, ("setup_tcpip_socket: openened socket fd %d for port %u\n",
736 state->fd, state->ep.port));
738 fde = tevent_add_fd(state->ev_ctx,
742 dcerpc_ncacn_tcpip_listener,
745 DEBUG(0, ("setup_tcpip_socket: Failed to add event handler!\n"));
749 tevent_fd_set_auto_close(fde);
751 return state->ep.port;
753 if (state->fd != -1) {
761 static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
762 struct tevent_fd *fde,
766 struct dcerpc_ncacn_listen_state *state =
767 talloc_get_type_abort(private_data,
768 struct dcerpc_ncacn_listen_state);
769 struct tsocket_address *cli_addr = NULL;
770 struct tsocket_address *srv_addr = NULL;
771 struct sockaddr_storage addr;
772 socklen_t in_addrlen = sizeof(addr);
776 s = accept(state->fd, (struct sockaddr *)(void *) &addr, &in_addrlen);
778 if (errno != EINTR) {
779 DEBUG(0,("tcpip_listener accept: %s\n",
785 rc = tsocket_address_bsd_from_sockaddr(state,
786 (struct sockaddr *)(void *) &addr,
794 rc = getsockname(s, (struct sockaddr *)(void *) &addr, &in_addrlen);
800 rc = tsocket_address_bsd_from_sockaddr(state,
801 (struct sockaddr *)(void *) &addr,
809 DEBUG(6, ("tcpip_listener: Accepted socket %d\n", s));
811 dcerpc_ncacn_accept(state->ev_ctx,
822 /********************************************************************
823 * Start listening on the ncalrpc socket
824 ********************************************************************/
826 static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
827 struct tevent_fd *fde,
831 bool setup_dcerpc_ncalrpc_socket(struct tevent_context *ev_ctx,
832 struct messaging_context *msg_ctx,
834 dcerpc_ncacn_disconnect_fn fn)
836 struct dcerpc_ncacn_listen_state *state;
837 struct tevent_fd *fde;
839 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
841 DEBUG(0, ("Out of memory\n"));
846 state->disconnect_fn = fn;
851 state->ep.name = talloc_strdup(state, name);
853 if (state->ep.name == NULL) {
854 DEBUG(0, ("Out of memory\n"));
859 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) {
860 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
861 lp_ncalrpc_dir(), strerror(errno)));
865 state->fd = create_pipe_sock(lp_ncalrpc_dir(), name, 0755);
866 if (state->fd == -1) {
867 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
868 lp_ncalrpc_dir(), name));
872 DEBUG(10, ("Openened pipe socket fd %d for %s\n", state->fd, name));
874 state->ev_ctx = ev_ctx;
875 state->msg_ctx = msg_ctx;
877 /* Set server socket to non-blocking for the accept. */
878 set_blocking(state->fd, false);
880 fde = tevent_add_fd(state->ev_ctx,
884 dcerpc_ncalrpc_listener,
887 DEBUG(0, ("Failed to add event handler for ncalrpc!\n"));
891 tevent_fd_set_auto_close(fde);
895 if (state->fd != -1) {
903 static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
904 struct tevent_fd *fde,
908 struct dcerpc_ncacn_listen_state *state =
909 talloc_get_type_abort(private_data,
910 struct dcerpc_ncacn_listen_state);
911 struct tsocket_address *cli_addr = NULL;
912 struct sockaddr_un sunaddr;
913 struct sockaddr *addr = (struct sockaddr *)(void *)&sunaddr;
914 socklen_t len = sizeof(sunaddr);
918 ZERO_STRUCT(sunaddr);
920 sd = accept(state->fd, addr, &len);
922 if (errno != EINTR) {
923 DEBUG(0, ("ncalrpc accept() failed: %s\n", strerror(errno)));
928 rc = tsocket_address_bsd_from_sockaddr(state,
936 DEBUG(10, ("Accepted ncalrpc socket %d\n", sd));
938 dcerpc_ncacn_accept(state->ev_ctx,
943 state->disconnect_fn);
946 struct dcerpc_ncacn_conn {
947 enum dcerpc_transport_t transport;
956 struct pipes_struct *p;
957 dcerpc_ncacn_disconnect_fn disconnect_fn;
959 struct tevent_context *ev_ctx;
960 struct messaging_context *msg_ctx;
962 struct tstream_context *tstream;
963 struct tevent_queue *send_queue;
965 struct tsocket_address *client;
967 struct tsocket_address *server;
969 struct auth_session_info *session_info;
975 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq);
976 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq);
978 static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
979 struct messaging_context *msg_ctx,
980 enum dcerpc_transport_t transport,
983 struct tsocket_address *cli_addr,
984 struct tsocket_address *srv_addr,
986 dcerpc_ncacn_disconnect_fn fn) {
987 struct dcerpc_ncacn_conn *ncacn_conn;
988 struct tevent_req *subreq;
990 const char *srv_str = NULL;
991 bool system_user = false;
998 DEBUG(10, ("dcerpc_ncacn_accept\n"));
1000 ncacn_conn = talloc_zero(ev_ctx, struct dcerpc_ncacn_conn);
1001 if (ncacn_conn == NULL) {
1002 DEBUG(0, ("Out of memory!\n"));
1007 ncacn_conn->transport = transport;
1008 ncacn_conn->ev_ctx = ev_ctx;
1009 ncacn_conn->msg_ctx = msg_ctx;
1010 ncacn_conn->sock = s;
1011 ncacn_conn->disconnect_fn = fn;
1013 ncacn_conn->client = talloc_move(ncacn_conn, &cli_addr);
1014 if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
1015 ncacn_conn->client_name =
1016 tsocket_address_inet_addr_string(ncacn_conn->client,
1019 ncacn_conn->client_name =
1020 tsocket_address_unix_path(ncacn_conn->client,
1023 if (ncacn_conn->client_name == NULL) {
1024 DEBUG(0, ("Out of memory!\n"));
1025 talloc_free(ncacn_conn);
1030 if (srv_addr != NULL) {
1031 ncacn_conn->server = talloc_move(ncacn_conn, &srv_addr);
1033 ncacn_conn->server_name =
1034 tsocket_address_inet_addr_string(ncacn_conn->server,
1036 if (ncacn_conn->server_name == NULL) {
1037 DEBUG(0, ("Out of memory!\n"));
1038 talloc_free(ncacn_conn);
1044 switch (transport) {
1046 ncacn_conn->ep.port = port;
1048 pipe_name = tsocket_address_string(ncacn_conn->client,
1050 if (pipe_name == NULL) {
1052 talloc_free(ncacn_conn);
1058 rc = sys_getpeereid(s, &uid);
1060 DEBUG(2, ("Failed to get ncalrpc connecting uid!"));
1062 if (uid == sec_initial_uid()) {
1067 ncacn_conn->ep.name = talloc_strdup(ncacn_conn, name);
1068 if (ncacn_conn->ep.name == NULL) {
1070 talloc_free(ncacn_conn);
1074 pipe_name = talloc_strdup(ncacn_conn,
1076 if (pipe_name == NULL) {
1078 talloc_free(ncacn_conn);
1083 DEBUG(0, ("unknown dcerpc transport: %u!\n",
1085 talloc_free(ncacn_conn);
1090 rc = set_blocking(s, false);
1092 DEBUG(2, ("Failed to set dcerpc socket to non-blocking\n"));
1093 talloc_free(ncacn_conn);
1099 * As soon as we have tstream_bsd_existing_socket set up it will
1100 * take care of closing the socket.
1102 rc = tstream_bsd_existing_socket(ncacn_conn, s, &ncacn_conn->tstream);
1104 DEBUG(2, ("Failed to create tstream socket for dcerpc\n"));
1105 talloc_free(ncacn_conn);
1110 if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
1111 cli_str = ncacn_conn->client_name;
1116 if (ncacn_conn->server != NULL) {
1117 if (tsocket_address_is_inet(ncacn_conn->server, "ip")) {
1118 srv_str = ncacn_conn->server_name;
1124 if (ncacn_conn->session_info == NULL) {
1125 status = auth_anonymous_session_info(ncacn_conn,
1126 &ncacn_conn->session_info);
1127 if (!NT_STATUS_IS_OK(status)) {
1128 DEBUG(2, ("Failed to create "
1129 "auth_anonymous_session_info - %s\n",
1130 nt_errstr(status)));
1131 talloc_free(ncacn_conn);
1136 rc = make_server_pipes_struct(ncacn_conn,
1138 ncacn_conn->transport,
1142 ncacn_conn->session_info,
1146 DEBUG(2, ("Failed to create pipe struct - %s",
1147 strerror(sys_errno)));
1148 talloc_free(ncacn_conn);
1152 ncacn_conn->send_queue = tevent_queue_create(ncacn_conn,
1153 "dcerpc send queue");
1154 if (ncacn_conn->send_queue == NULL) {
1155 DEBUG(0, ("Out of memory!\n"));
1156 talloc_free(ncacn_conn);
1160 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1162 ncacn_conn->tstream);
1163 if (subreq == NULL) {
1164 DEBUG(2, ("Failed to send ncacn packet\n"));
1165 talloc_free(ncacn_conn);
1169 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1171 DEBUG(10, ("dcerpc_ncacn_accept done\n"));
1176 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq)
1178 struct dcerpc_ncacn_conn *ncacn_conn =
1179 tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
1181 struct _output_data *out = &ncacn_conn->p->out_data;
1182 DATA_BLOB recv_buffer = data_blob_null;
1183 struct ncacn_packet *pkt;
1191 status = dcerpc_read_ncacn_packet_recv(subreq, ncacn_conn, &pkt, &recv_buffer);
1192 TALLOC_FREE(subreq);
1193 if (!NT_STATUS_IS_OK(status)) {
1194 if (ncacn_conn->disconnect_fn != NULL) {
1195 ok = ncacn_conn->disconnect_fn(ncacn_conn->p);
1197 DEBUG(3, ("Failed to call disconnect function\n"));
1203 data_left = recv_buffer.length;
1204 data = (char *) recv_buffer.data;
1207 data_used = process_incoming_data(ncacn_conn->p, data, data_left);
1208 if (data_used < 0) {
1209 DEBUG(3, ("Failed to process dcerpc request!\n"));
1210 status = NT_STATUS_UNEXPECTED_IO_ERROR;
1214 data_left -= data_used;
1218 /* Do not leak this buffer */
1219 talloc_free(recv_buffer.data);
1223 * This is needed because of the way DCERPC binds work in the RPC
1226 to_send = out->frag.length - out->current_pdu_sent;
1229 DEBUG(10, ("Current_pdu_len = %u, "
1230 "current_pdu_sent = %u "
1231 "Returning %u bytes\n",
1232 (unsigned int)out->frag.length,
1233 (unsigned int)out->current_pdu_sent,
1234 (unsigned int)to_send));
1236 ncacn_conn->iov = talloc_zero(ncacn_conn, struct iovec);
1237 if (ncacn_conn->iov == NULL) {
1238 status = NT_STATUS_NO_MEMORY;
1239 DEBUG(3, ("Out of memory!\n"));
1242 ncacn_conn->count = 1;
1244 ncacn_conn->iov[0].iov_base = out->frag.data
1245 + out->current_pdu_sent;
1246 ncacn_conn->iov[0].iov_len = to_send;
1248 out->current_pdu_sent += to_send;
1252 * This condition is false for bind packets, or when we haven't yet got
1253 * a full request, and need to wait for more data from the client
1255 while (out->data_sent_length < out->rdata.length) {
1256 ok = create_next_pdu(ncacn_conn->p);
1258 DEBUG(3, ("Failed to create next PDU!\n"));
1259 status = NT_STATUS_UNEXPECTED_IO_ERROR;
1263 ncacn_conn->iov = talloc_realloc(ncacn_conn,
1266 ncacn_conn->count + 1);
1267 if (ncacn_conn->iov == NULL) {
1268 DEBUG(3, ("Out of memory!\n"));
1269 status = NT_STATUS_NO_MEMORY;
1273 ncacn_conn->iov[ncacn_conn->count].iov_base = out->frag.data;
1274 ncacn_conn->iov[ncacn_conn->count].iov_len = out->frag.length;
1276 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
1277 (unsigned int) ncacn_conn->count,
1278 (unsigned int) ncacn_conn->iov[ncacn_conn->count].iov_len));
1279 dump_data(11, (const uint8_t *) ncacn_conn->iov[ncacn_conn->count].iov_base,
1280 ncacn_conn->iov[ncacn_conn->count].iov_len);
1281 ncacn_conn->count++;
1285 * We still don't have a complete request, go back and wait for more
1288 if (ncacn_conn->count == 0) {
1289 /* Wait for the next packet */
1290 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1292 ncacn_conn->tstream);
1293 if (subreq == NULL) {
1294 DEBUG(2, ("Failed to start receving packets\n"));
1295 status = NT_STATUS_NO_MEMORY;
1298 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1302 DEBUG(10, ("Sending a total of %u bytes\n",
1303 (unsigned int)ncacn_conn->p->out_data.data_sent_length));
1305 subreq = tstream_writev_queue_send(ncacn_conn,
1307 ncacn_conn->tstream,
1308 ncacn_conn->send_queue,
1311 if (subreq == NULL) {
1312 DEBUG(2, ("Failed to send packet\n"));
1313 status = NT_STATUS_NO_MEMORY;
1317 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_done, ncacn_conn);
1321 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1322 ncacn_conn->client_name, nt_errstr(status)));
1324 /* Terminate client connection */
1325 talloc_free(ncacn_conn);
1329 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq)
1331 struct dcerpc_ncacn_conn *ncacn_conn =
1332 tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
1333 NTSTATUS status = NT_STATUS_OK;
1337 rc = tstream_writev_queue_recv(subreq, &sys_errno);
1338 TALLOC_FREE(subreq);
1340 DEBUG(2, ("Writev failed!\n"));
1341 status = map_nt_error_from_unix(sys_errno);
1345 /* clear out any data that may have been left around */
1346 ncacn_conn->count = 0;
1347 TALLOC_FREE(ncacn_conn->iov);
1348 data_blob_free(&ncacn_conn->p->in_data.data);
1349 data_blob_free(&ncacn_conn->p->out_data.frag);
1350 data_blob_free(&ncacn_conn->p->out_data.rdata);
1352 /* Wait for the next packet */
1353 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1355 ncacn_conn->tstream);
1356 if (subreq == NULL) {
1357 DEBUG(2, ("Failed to start receving packets\n"));
1358 status = NT_STATUS_NO_MEMORY;
1362 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1366 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1367 ncacn_conn->client_name, nt_errstr(status)));
1369 /* Terminate client connection */
1370 talloc_free(ncacn_conn);
1374 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */