2 Unix SMB/Netbios implementation.
3 Generic infrstructure for RPC Daemons
4 Copyright (C) Simo Sorce 2010
5 Copyright (C) Andrew Bartlett 2011
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "rpc_server/rpc_server.h"
25 #include "librpc/gen_ndr/netlogon.h"
26 #include "librpc/gen_ndr/auth.h"
27 #include "lib/tsocket/tsocket.h"
28 #include "libcli/named_pipe_auth/npa_tstream.h"
29 #include "../auth/auth_sam_reply.h"
31 #include "rpc_server/rpc_ncacn_np.h"
32 #include "rpc_server/srv_pipe_hnd.h"
33 #include "rpc_server/srv_pipe.h"
35 #define SERVER_TCP_LOW_PORT 1024
36 #define SERVER_TCP_HIGH_PORT 1300
38 static NTSTATUS auth_anonymous_session_info(TALLOC_CTX *mem_ctx,
39 struct auth_session_info **session_info)
41 struct auth_session_info *i;
42 struct auth_serversupplied_info *s;
43 struct auth_user_info_dc *u;
44 union netr_Validation val;
47 i = talloc_zero(mem_ctx, struct auth_session_info);
49 return NT_STATUS_NO_MEMORY;
52 status = make_server_info_guest(i, &s);
53 if (!NT_STATUS_IS_OK(status)) {
57 i->security_token = s->security_token;
58 i->session_key = s->session_key;
62 status = make_user_info_dc_netlogon_validation(mem_ctx,
67 if (!NT_STATUS_IS_OK(status)) {
68 DEBUG(0, ("conversion of info3 into user_info_dc failed!\n"));
71 i->info = talloc_move(i, &u->info);
79 /* Creates a pipes_struct and initializes it with the information
80 * sent from the client */
81 static int make_server_pipes_struct(TALLOC_CTX *mem_ctx,
82 const char *pipe_name,
83 enum dcerpc_transport_t transport,
84 bool ncalrpc_as_system,
85 const struct tsocket_address *local_address,
86 const struct tsocket_address *remote_address,
87 struct auth_session_info *session_info,
88 struct pipes_struct **_p,
91 struct netr_SamInfo3 *info3;
92 struct auth_user_info_dc *auth_user_info_dc;
93 struct pipes_struct *p;
94 struct auth_serversupplied_info *server_info;
97 p = talloc_zero(mem_ctx, struct pipes_struct);
103 p->transport = transport;
104 p->ncalrpc_as_system = ncalrpc_as_system;
106 p->mem_ctx = talloc_named(p, 0, "pipe %s %p", pipe_name, p);
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 &server_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, server_info, &session_info->session_key, &p->session_info);
158 talloc_free(server_info);
159 if (!NT_STATUS_IS_OK(status)) {
160 DEBUG(1, ("Failed to init local auth token\n"));
166 /* Now override the session_info->security_token with the exact
167 * security_token we were given from the other side,
168 * regardless of what we just calculated */
169 p->session_info->security_token = talloc_move(p->session_info, &session_info->security_token);
171 p->remote_address = tsocket_address_copy(remote_address, p);
172 if (p->remote_address == NULL) {
178 if (local_address != NULL) {
179 p->local_address = tsocket_address_copy(local_address, p);
180 if (p->local_address == NULL) {
187 talloc_set_destructor(p, close_internal_rpc_pipe_hnd);
193 /* Start listening on the appropriate unix socket and setup all is needed to
194 * dispatch requests to the pipes rpc implementation */
196 struct dcerpc_ncacn_listen_state {
197 struct ndr_syntax_id syntax_id;
205 struct tevent_context *ev_ctx;
206 struct messaging_context *msg_ctx;
207 dcerpc_ncacn_disconnect_fn disconnect_fn;
210 static void named_pipe_listener(struct tevent_context *ev,
211 struct tevent_fd *fde,
215 bool setup_named_pipe_socket(const char *pipe_name,
216 struct tevent_context *ev_ctx,
217 struct messaging_context *msg_ctx)
219 struct dcerpc_ncacn_listen_state *state;
220 struct tevent_fd *fde;
223 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
225 DEBUG(0, ("Out of memory\n"));
228 state->ep.name = talloc_strdup(state, pipe_name);
229 if (state->ep.name == NULL) {
230 DEBUG(0, ("Out of memory\n"));
235 state->ev_ctx = ev_ctx;
236 state->msg_ctx = msg_ctx;
239 * As lp_ncalrpc_dir() should have 0755, but
240 * lp_ncalrpc_dir()/np should have 0700, we need to
241 * create lp_ncalrpc_dir() first.
243 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) {
244 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
245 lp_ncalrpc_dir(), strerror(errno)));
249 np_dir = talloc_asprintf(state, "%s/np", lp_ncalrpc_dir());
251 DEBUG(0, ("Out of memory\n"));
255 if (!directory_create_or_exist(np_dir, geteuid(), 0700)) {
256 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
257 np_dir, strerror(errno)));
261 state->fd = create_pipe_sock(np_dir, pipe_name, 0700);
262 if (state->fd == -1) {
263 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
268 DEBUG(10, ("Openened pipe socket fd %d for %s\n",
269 state->fd, pipe_name));
271 fde = tevent_add_fd(ev_ctx,
272 state, state->fd, TEVENT_FD_READ,
273 named_pipe_listener, state);
275 DEBUG(0, ("Failed to add event handler!\n"));
279 tevent_fd_set_auto_close(fde);
283 if (state->fd != -1) {
290 static void named_pipe_accept_function(struct tevent_context *ev_ctx,
291 struct messaging_context *msg_ctx,
292 const char *pipe_name,
295 static void named_pipe_listener(struct tevent_context *ev,
296 struct tevent_fd *fde,
300 struct dcerpc_ncacn_listen_state *state =
301 talloc_get_type_abort(private_data,
302 struct dcerpc_ncacn_listen_state);
303 struct sockaddr_un sunaddr;
307 /* TODO: should we have a limit to the number of clients ? */
309 len = sizeof(sunaddr);
311 sd = accept(state->fd,
312 (struct sockaddr *)(void *)&sunaddr, &len);
315 if (errno != EINTR) {
316 DEBUG(6, ("Failed to get a valid socket [%s]\n",
322 DEBUG(6, ("Accepted socket %d\n", sd));
324 named_pipe_accept_function(state->ev_ctx,
331 /* This is the core of the rpc server.
332 * Accepts connections from clients and process requests using the appropriate
333 * dispatcher table. */
335 struct named_pipe_client {
336 const char *pipe_name;
338 struct tevent_context *ev;
339 struct messaging_context *msg_ctx;
342 uint16_t device_state;
343 uint64_t allocation_size;
345 struct tstream_context *tstream;
347 struct tsocket_address *client;
349 struct tsocket_address *server;
352 struct auth_session_info *session_info;
354 struct pipes_struct *p;
356 struct tevent_queue *write_queue;
362 static void named_pipe_accept_done(struct tevent_req *subreq);
364 static void named_pipe_accept_function(struct tevent_context *ev_ctx,
365 struct messaging_context *msg_ctx,
366 const char *pipe_name,
369 struct named_pipe_client *npc;
370 struct tstream_context *plain;
371 struct tevent_req *subreq;
374 npc = talloc_zero(NULL, struct named_pipe_client);
376 DEBUG(0, ("Out of memory!\n"));
380 npc->pipe_name = pipe_name;
382 npc->msg_ctx = msg_ctx;
384 /* make sure socket is in NON blocking state */
385 ret = set_blocking(fd, false);
387 DEBUG(2, ("Failed to make socket non-blocking\n"));
393 ret = tstream_bsd_existing_socket(npc, fd, &plain);
395 DEBUG(2, ("Failed to create tstream socket\n"));
401 npc->file_type = FILE_TYPE_MESSAGE_MODE_PIPE;
402 npc->device_state = 0xff | 0x0400 | 0x0100;
403 npc->allocation_size = 4096;
405 subreq = tstream_npa_accept_existing_send(npc, npc->ev, plain,
408 npc->allocation_size);
410 DEBUG(2, ("Failed to start async accept procedure\n"));
415 tevent_req_set_callback(subreq, named_pipe_accept_done, npc);
418 static void named_pipe_packet_process(struct tevent_req *subreq);
419 static void named_pipe_packet_done(struct tevent_req *subreq);
421 static void named_pipe_accept_done(struct tevent_req *subreq)
423 struct auth_session_info_transport *session_info_transport;
424 struct named_pipe_client *npc =
425 tevent_req_callback_data(subreq, struct named_pipe_client);
429 ret = tstream_npa_accept_existing_recv(subreq, &error, npc,
435 &session_info_transport);
437 npc->session_info = talloc_move(npc, &session_info_transport->session_info);
441 DEBUG(2, ("Failed to accept named pipe connection! (%s)\n",
447 ret = make_server_pipes_struct(npc,
448 npc->pipe_name, NCACN_NP,
449 false, npc->server, npc->client, npc->session_info,
452 DEBUG(2, ("Failed to create pipes_struct! (%s)\n",
456 npc->p->msg_ctx = npc->msg_ctx;
458 npc->write_queue = tevent_queue_create(npc, "np_server_write_queue");
459 if (!npc->write_queue) {
460 DEBUG(2, ("Failed to set up write queue!\n"));
464 /* And now start receiving and processing packets */
465 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
467 DEBUG(2, ("Failed to start receving packets\n"));
470 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
474 DEBUG(2, ("Fatal error. Terminating client(%s) connection!\n",
476 /* terminate client connection */
481 static void named_pipe_packet_process(struct tevent_req *subreq)
483 struct named_pipe_client *npc =
484 tevent_req_callback_data(subreq, struct named_pipe_client);
485 struct _output_data *out = &npc->p->out_data;
486 DATA_BLOB recv_buffer = data_blob_null;
487 struct ncacn_packet *pkt;
495 status = dcerpc_read_ncacn_packet_recv(subreq, npc, &pkt, &recv_buffer);
497 if (!NT_STATUS_IS_OK(status)) {
501 data_left = recv_buffer.length;
502 data = (char *)recv_buffer.data;
506 data_used = process_incoming_data(npc->p, data, data_left);
508 DEBUG(3, ("Failed to process dceprc request!\n"));
509 status = NT_STATUS_UNEXPECTED_IO_ERROR;
513 data_left -= data_used;
517 /* Do not leak this buffer, npc is a long lived context */
518 talloc_free(recv_buffer.data);
521 /* this is needed because of the way DCERPC Binds work in
522 * the RPC marshalling code */
523 to_send = out->frag.length - out->current_pdu_sent;
526 DEBUG(10, ("Current_pdu_len = %u, "
527 "current_pdu_sent = %u "
528 "Returning %u bytes\n",
529 (unsigned int)out->frag.length,
530 (unsigned int)out->current_pdu_sent,
531 (unsigned int)to_send));
533 npc->iov = talloc_zero(npc, struct iovec);
535 status = NT_STATUS_NO_MEMORY;
540 npc->iov[0].iov_base = out->frag.data
541 + out->current_pdu_sent;
542 npc->iov[0].iov_len = to_send;
544 out->current_pdu_sent += to_send;
547 /* this condition is false for bind packets, or when we haven't
548 * yet got a full request, and need to wait for more data from
550 while (out->data_sent_length < out->rdata.length) {
552 ok = create_next_pdu(npc->p);
554 DEBUG(3, ("Failed to create next PDU!\n"));
555 status = NT_STATUS_UNEXPECTED_IO_ERROR;
559 npc->iov = talloc_realloc(npc, npc->iov,
560 struct iovec, npc->count + 1);
562 status = NT_STATUS_NO_MEMORY;
566 npc->iov[npc->count].iov_base = out->frag.data;
567 npc->iov[npc->count].iov_len = out->frag.length;
569 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
570 (unsigned int)npc->count,
571 (unsigned int)npc->iov[npc->count].iov_len));
572 dump_data(11, (const uint8_t *)npc->iov[npc->count].iov_base,
573 npc->iov[npc->count].iov_len);
577 /* we still don't have a complete request, go back and wait for more
579 if (npc->count == 0) {
580 /* Wait for the next packet */
581 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
583 DEBUG(2, ("Failed to start receving packets\n"));
584 status = NT_STATUS_NO_MEMORY;
587 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
591 DEBUG(10, ("Sending a total of %u bytes\n",
592 (unsigned int)npc->p->out_data.data_sent_length));
594 subreq = tstream_writev_queue_send(npc, npc->ev,
597 npc->iov, npc->count);
599 DEBUG(2, ("Failed to send packet\n"));
600 status = NT_STATUS_NO_MEMORY;
603 tevent_req_set_callback(subreq, named_pipe_packet_done, npc);
607 DEBUG(2, ("Fatal error(%s). "
608 "Terminating client(%s) connection!\n",
609 nt_errstr(status), npc->client_name));
610 /* terminate client connection */
615 static void named_pipe_packet_done(struct tevent_req *subreq)
617 struct named_pipe_client *npc =
618 tevent_req_callback_data(subreq, struct named_pipe_client);
622 ret = tstream_writev_queue_recv(subreq, &sys_errno);
625 DEBUG(2, ("Writev failed!\n"));
629 /* clear out any data that may have been left around */
631 TALLOC_FREE(npc->iov);
632 data_blob_free(&npc->p->in_data.data);
633 data_blob_free(&npc->p->out_data.frag);
634 data_blob_free(&npc->p->out_data.rdata);
636 /* Wait for the next packet */
637 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
639 DEBUG(2, ("Failed to start receving packets\n"));
643 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
647 DEBUG(2, ("Fatal error(%s). "
648 "Terminating client(%s) connection!\n",
649 strerror(sys_errno), npc->client_name));
650 /* terminate client connection */
655 static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
656 struct messaging_context *msg_ctx,
657 enum dcerpc_transport_t transport,
660 struct tsocket_address *cli_addr,
661 struct tsocket_address *srv_addr,
663 dcerpc_ncacn_disconnect_fn fn);
665 /********************************************************************
666 * Start listening on the tcp/ip socket
667 ********************************************************************/
669 static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
670 struct tevent_fd *fde,
674 uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context *ev_ctx,
675 struct messaging_context *msg_ctx,
676 const struct sockaddr_storage *ifss,
679 struct dcerpc_ncacn_listen_state *state;
680 struct tevent_fd *fde;
683 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
685 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Out of memory\n"));
690 state->ep.port = port;
691 state->disconnect_fn = NULL;
693 if (state->ep.port == 0) {
696 for (i = SERVER_TCP_LOW_PORT; i <= SERVER_TCP_HIGH_PORT; i++) {
697 state->fd = open_socket_in(SOCK_STREAM,
708 state->fd = open_socket_in(SOCK_STREAM,
714 if (state->fd == -1) {
715 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Failed to create "
716 "socket on port %u!\n", state->ep.port));
720 state->ev_ctx = ev_ctx;
721 state->msg_ctx = msg_ctx;
723 /* ready to listen */
724 set_socket_options(state->fd, "SO_KEEPALIVE");
725 set_socket_options(state->fd, lp_socket_options());
727 /* Set server socket to non-blocking for the accept. */
728 set_blocking(state->fd, false);
730 rc = listen(state->fd, SMBD_LISTEN_BACKLOG);
732 DEBUG(0,("setup_tcpip_socket: listen - %s\n", strerror(errno)));
736 DEBUG(10, ("setup_tcpip_socket: openened socket fd %d for port %u\n",
737 state->fd, state->ep.port));
739 fde = tevent_add_fd(state->ev_ctx,
743 dcerpc_ncacn_tcpip_listener,
746 DEBUG(0, ("setup_tcpip_socket: Failed to add event handler!\n"));
750 tevent_fd_set_auto_close(fde);
752 return state->ep.port;
754 if (state->fd != -1) {
762 static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
763 struct tevent_fd *fde,
767 struct dcerpc_ncacn_listen_state *state =
768 talloc_get_type_abort(private_data,
769 struct dcerpc_ncacn_listen_state);
770 struct tsocket_address *cli_addr = NULL;
771 struct tsocket_address *srv_addr = NULL;
772 struct sockaddr_storage addr;
773 socklen_t in_addrlen = sizeof(addr);
777 s = accept(state->fd, (struct sockaddr *)(void *) &addr, &in_addrlen);
779 if (errno != EINTR) {
780 DEBUG(0,("tcpip_listener accept: %s\n",
786 rc = tsocket_address_bsd_from_sockaddr(state,
787 (struct sockaddr *)(void *) &addr,
795 rc = getsockname(s, (struct sockaddr *)(void *) &addr, &in_addrlen);
801 rc = tsocket_address_bsd_from_sockaddr(state,
802 (struct sockaddr *)(void *) &addr,
810 DEBUG(6, ("tcpip_listener: Accepted socket %d\n", s));
812 dcerpc_ncacn_accept(state->ev_ctx,
823 /********************************************************************
824 * Start listening on the ncalrpc socket
825 ********************************************************************/
827 static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
828 struct tevent_fd *fde,
832 bool setup_dcerpc_ncalrpc_socket(struct tevent_context *ev_ctx,
833 struct messaging_context *msg_ctx,
835 dcerpc_ncacn_disconnect_fn fn)
837 struct dcerpc_ncacn_listen_state *state;
838 struct tevent_fd *fde;
840 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
842 DEBUG(0, ("Out of memory\n"));
847 state->disconnect_fn = fn;
852 state->ep.name = talloc_strdup(state, name);
854 if (state->ep.name == NULL) {
855 DEBUG(0, ("Out of memory\n"));
860 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) {
861 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
862 lp_ncalrpc_dir(), strerror(errno)));
866 state->fd = create_pipe_sock(lp_ncalrpc_dir(), name, 0755);
867 if (state->fd == -1) {
868 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
869 lp_ncalrpc_dir(), name));
873 DEBUG(10, ("Openened pipe socket fd %d for %s\n", state->fd, name));
875 state->ev_ctx = ev_ctx;
876 state->msg_ctx = msg_ctx;
878 /* Set server socket to non-blocking for the accept. */
879 set_blocking(state->fd, false);
881 fde = tevent_add_fd(state->ev_ctx,
885 dcerpc_ncalrpc_listener,
888 DEBUG(0, ("Failed to add event handler for ncalrpc!\n"));
892 tevent_fd_set_auto_close(fde);
896 if (state->fd != -1) {
904 static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
905 struct tevent_fd *fde,
909 struct dcerpc_ncacn_listen_state *state =
910 talloc_get_type_abort(private_data,
911 struct dcerpc_ncacn_listen_state);
912 struct tsocket_address *cli_addr = NULL;
913 struct sockaddr_un sunaddr;
914 struct sockaddr *addr = (struct sockaddr *)(void *)&sunaddr;
915 socklen_t len = sizeof(sunaddr);
919 ZERO_STRUCT(sunaddr);
921 sd = accept(state->fd, addr, &len);
923 if (errno != EINTR) {
924 DEBUG(0, ("ncalrpc accept() failed: %s\n", strerror(errno)));
929 rc = tsocket_address_bsd_from_sockaddr(state,
937 DEBUG(10, ("Accepted ncalrpc socket %d\n", sd));
939 dcerpc_ncacn_accept(state->ev_ctx,
944 state->disconnect_fn);
947 struct dcerpc_ncacn_conn {
948 enum dcerpc_transport_t transport;
957 struct pipes_struct *p;
958 dcerpc_ncacn_disconnect_fn disconnect_fn;
960 struct tevent_context *ev_ctx;
961 struct messaging_context *msg_ctx;
963 struct tstream_context *tstream;
964 struct tevent_queue *send_queue;
966 struct tsocket_address *client;
968 struct tsocket_address *server;
970 struct auth_session_info *session_info;
976 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq);
977 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq);
979 static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
980 struct messaging_context *msg_ctx,
981 enum dcerpc_transport_t transport,
984 struct tsocket_address *cli_addr,
985 struct tsocket_address *srv_addr,
987 dcerpc_ncacn_disconnect_fn fn) {
988 struct dcerpc_ncacn_conn *ncacn_conn;
989 struct tevent_req *subreq;
991 const char *srv_str = NULL;
992 bool system_user = false;
999 DEBUG(10, ("dcerpc_ncacn_accept\n"));
1001 ncacn_conn = talloc_zero(ev_ctx, struct dcerpc_ncacn_conn);
1002 if (ncacn_conn == NULL) {
1003 DEBUG(0, ("Out of memory!\n"));
1008 ncacn_conn->transport = transport;
1009 ncacn_conn->ev_ctx = ev_ctx;
1010 ncacn_conn->msg_ctx = msg_ctx;
1011 ncacn_conn->sock = s;
1012 ncacn_conn->disconnect_fn = fn;
1014 ncacn_conn->client = talloc_move(ncacn_conn, &cli_addr);
1015 if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
1016 ncacn_conn->client_name =
1017 tsocket_address_inet_addr_string(ncacn_conn->client,
1020 ncacn_conn->client_name =
1021 tsocket_address_unix_path(ncacn_conn->client,
1024 if (ncacn_conn->client_name == NULL) {
1025 DEBUG(0, ("Out of memory!\n"));
1026 talloc_free(ncacn_conn);
1031 if (srv_addr != NULL) {
1032 ncacn_conn->server = talloc_move(ncacn_conn, &srv_addr);
1034 ncacn_conn->server_name =
1035 tsocket_address_inet_addr_string(ncacn_conn->server,
1037 if (ncacn_conn->server_name == NULL) {
1038 DEBUG(0, ("Out of memory!\n"));
1039 talloc_free(ncacn_conn);
1045 switch (transport) {
1047 ncacn_conn->ep.port = port;
1049 pipe_name = tsocket_address_string(ncacn_conn->client,
1051 if (pipe_name == NULL) {
1053 talloc_free(ncacn_conn);
1059 rc = sys_getpeereid(s, &uid);
1061 DEBUG(2, ("Failed to get ncalrpc connecting uid!"));
1063 if (uid == sec_initial_uid()) {
1068 ncacn_conn->ep.name = talloc_strdup(ncacn_conn, name);
1069 if (ncacn_conn->ep.name == NULL) {
1071 talloc_free(ncacn_conn);
1075 pipe_name = talloc_strdup(ncacn_conn,
1077 if (pipe_name == NULL) {
1079 talloc_free(ncacn_conn);
1084 DEBUG(0, ("unknown dcerpc transport: %u!\n",
1086 talloc_free(ncacn_conn);
1091 rc = set_blocking(s, false);
1093 DEBUG(2, ("Failed to set dcerpc socket to non-blocking\n"));
1094 talloc_free(ncacn_conn);
1100 * As soon as we have tstream_bsd_existing_socket set up it will
1101 * take care of closing the socket.
1103 rc = tstream_bsd_existing_socket(ncacn_conn, s, &ncacn_conn->tstream);
1105 DEBUG(2, ("Failed to create tstream socket for dcerpc\n"));
1106 talloc_free(ncacn_conn);
1111 if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
1112 cli_str = ncacn_conn->client_name;
1117 if (ncacn_conn->server != NULL) {
1118 if (tsocket_address_is_inet(ncacn_conn->server, "ip")) {
1119 srv_str = ncacn_conn->server_name;
1125 if (ncacn_conn->session_info == NULL) {
1126 status = auth_anonymous_session_info(ncacn_conn,
1127 &ncacn_conn->session_info);
1128 if (!NT_STATUS_IS_OK(status)) {
1129 DEBUG(2, ("Failed to create "
1130 "auth_anonymous_session_info - %s\n",
1131 nt_errstr(status)));
1132 talloc_free(ncacn_conn);
1137 rc = make_server_pipes_struct(ncacn_conn,
1139 ncacn_conn->transport,
1143 ncacn_conn->session_info,
1147 DEBUG(2, ("Failed to create pipe struct - %s",
1148 strerror(sys_errno)));
1149 talloc_free(ncacn_conn);
1153 ncacn_conn->send_queue = tevent_queue_create(ncacn_conn,
1154 "dcerpc send queue");
1155 if (ncacn_conn->send_queue == NULL) {
1156 DEBUG(0, ("Out of memory!\n"));
1157 talloc_free(ncacn_conn);
1161 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1163 ncacn_conn->tstream);
1164 if (subreq == NULL) {
1165 DEBUG(2, ("Failed to send ncacn packet\n"));
1166 talloc_free(ncacn_conn);
1170 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1172 DEBUG(10, ("dcerpc_ncacn_accept done\n"));
1177 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq)
1179 struct dcerpc_ncacn_conn *ncacn_conn =
1180 tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
1182 struct _output_data *out = &ncacn_conn->p->out_data;
1183 DATA_BLOB recv_buffer = data_blob_null;
1184 struct ncacn_packet *pkt;
1192 status = dcerpc_read_ncacn_packet_recv(subreq, ncacn_conn, &pkt, &recv_buffer);
1193 TALLOC_FREE(subreq);
1194 if (!NT_STATUS_IS_OK(status)) {
1195 if (ncacn_conn->disconnect_fn != NULL) {
1196 ok = ncacn_conn->disconnect_fn(ncacn_conn->p);
1198 DEBUG(3, ("Failed to call disconnect function\n"));
1204 data_left = recv_buffer.length;
1205 data = (char *) recv_buffer.data;
1208 data_used = process_incoming_data(ncacn_conn->p, data, data_left);
1209 if (data_used < 0) {
1210 DEBUG(3, ("Failed to process dcerpc request!\n"));
1211 status = NT_STATUS_UNEXPECTED_IO_ERROR;
1215 data_left -= data_used;
1219 /* Do not leak this buffer */
1220 talloc_free(recv_buffer.data);
1224 * This is needed because of the way DCERPC binds work in the RPC
1227 to_send = out->frag.length - out->current_pdu_sent;
1230 DEBUG(10, ("Current_pdu_len = %u, "
1231 "current_pdu_sent = %u "
1232 "Returning %u bytes\n",
1233 (unsigned int)out->frag.length,
1234 (unsigned int)out->current_pdu_sent,
1235 (unsigned int)to_send));
1237 ncacn_conn->iov = talloc_zero(ncacn_conn, struct iovec);
1238 if (ncacn_conn->iov == NULL) {
1239 status = NT_STATUS_NO_MEMORY;
1240 DEBUG(3, ("Out of memory!\n"));
1243 ncacn_conn->count = 1;
1245 ncacn_conn->iov[0].iov_base = out->frag.data
1246 + out->current_pdu_sent;
1247 ncacn_conn->iov[0].iov_len = to_send;
1249 out->current_pdu_sent += to_send;
1253 * This condition is false for bind packets, or when we haven't yet got
1254 * a full request, and need to wait for more data from the client
1256 while (out->data_sent_length < out->rdata.length) {
1257 ok = create_next_pdu(ncacn_conn->p);
1259 DEBUG(3, ("Failed to create next PDU!\n"));
1260 status = NT_STATUS_UNEXPECTED_IO_ERROR;
1264 ncacn_conn->iov = talloc_realloc(ncacn_conn,
1267 ncacn_conn->count + 1);
1268 if (ncacn_conn->iov == NULL) {
1269 DEBUG(3, ("Out of memory!\n"));
1270 status = NT_STATUS_NO_MEMORY;
1274 ncacn_conn->iov[ncacn_conn->count].iov_base = out->frag.data;
1275 ncacn_conn->iov[ncacn_conn->count].iov_len = out->frag.length;
1277 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
1278 (unsigned int) ncacn_conn->count,
1279 (unsigned int) ncacn_conn->iov[ncacn_conn->count].iov_len));
1280 dump_data(11, (const uint8_t *) ncacn_conn->iov[ncacn_conn->count].iov_base,
1281 ncacn_conn->iov[ncacn_conn->count].iov_len);
1282 ncacn_conn->count++;
1286 * We still don't have a complete request, go back and wait for more
1289 if (ncacn_conn->count == 0) {
1290 /* Wait for the next packet */
1291 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1293 ncacn_conn->tstream);
1294 if (subreq == NULL) {
1295 DEBUG(2, ("Failed to start receving packets\n"));
1296 status = NT_STATUS_NO_MEMORY;
1299 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1303 DEBUG(10, ("Sending a total of %u bytes\n",
1304 (unsigned int)ncacn_conn->p->out_data.data_sent_length));
1306 subreq = tstream_writev_queue_send(ncacn_conn,
1308 ncacn_conn->tstream,
1309 ncacn_conn->send_queue,
1312 if (subreq == NULL) {
1313 DEBUG(2, ("Failed to send packet\n"));
1314 status = NT_STATUS_NO_MEMORY;
1318 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_done, ncacn_conn);
1322 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1323 ncacn_conn->client_name, nt_errstr(status)));
1325 /* Terminate client connection */
1326 talloc_free(ncacn_conn);
1330 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq)
1332 struct dcerpc_ncacn_conn *ncacn_conn =
1333 tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
1334 NTSTATUS status = NT_STATUS_OK;
1338 rc = tstream_writev_queue_recv(subreq, &sys_errno);
1339 TALLOC_FREE(subreq);
1341 DEBUG(2, ("Writev failed!\n"));
1342 status = map_nt_error_from_unix(sys_errno);
1346 /* clear out any data that may have been left around */
1347 ncacn_conn->count = 0;
1348 TALLOC_FREE(ncacn_conn->iov);
1349 data_blob_free(&ncacn_conn->p->in_data.data);
1350 data_blob_free(&ncacn_conn->p->out_data.frag);
1351 data_blob_free(&ncacn_conn->p->out_data.rdata);
1353 /* Wait for the next packet */
1354 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1356 ncacn_conn->tstream);
1357 if (subreq == NULL) {
1358 DEBUG(2, ("Failed to start receving packets\n"));
1359 status = NT_STATUS_NO_MEMORY;
1363 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1367 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1368 ncacn_conn->client_name, nt_errstr(status)));
1370 /* Terminate client connection */
1371 talloc_free(ncacn_conn);
1375 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */