2 Unix SMB/Netbios implementation.
3 Generic infrstructure for RPC Daemons
4 Copyright (C) Simo Sorce 2010
5 Copyright (C) Andrew Bartlett 2011
6 Copyright (C) Andreas Schneider 2011
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "rpc_server/rpc_server.h"
26 #include "librpc/gen_ndr/netlogon.h"
27 #include "librpc/gen_ndr/auth.h"
28 #include "lib/tsocket/tsocket.h"
29 #include "libcli/named_pipe_auth/npa_tstream.h"
30 #include "../auth/auth_sam_reply.h"
32 #include "rpc_server/rpc_ncacn_np.h"
33 #include "rpc_server/srv_pipe_hnd.h"
34 #include "rpc_server/srv_pipe.h"
36 #define SERVER_TCP_LOW_PORT 1024
37 #define SERVER_TCP_HIGH_PORT 1300
39 static NTSTATUS auth_anonymous_session_info(TALLOC_CTX *mem_ctx,
40 struct auth_session_info **session_info)
44 status = make_session_info_guest(mem_ctx, session_info);
45 if (!NT_STATUS_IS_OK(status)) {
52 /* Creates a pipes_struct and initializes it with the information
53 * sent from the client */
54 static int make_server_pipes_struct(TALLOC_CTX *mem_ctx,
55 struct messaging_context *msg_ctx,
56 const char *pipe_name,
57 enum dcerpc_transport_t transport,
58 bool ncalrpc_as_system,
59 const struct tsocket_address *local_address,
60 const struct tsocket_address *remote_address,
61 struct auth_session_info *session_info,
62 struct pipes_struct **_p,
65 struct pipes_struct *p;
68 p = talloc_zero(mem_ctx, struct pipes_struct);
74 p->transport = transport;
75 p->ncalrpc_as_system = ncalrpc_as_system;
77 p->mem_ctx = talloc_named(p, 0, "pipe %s %p", pipe_name, p);
85 data_blob_free(&p->in_data.data);
86 data_blob_free(&p->in_data.pdu);
88 p->endian = RPC_LITTLE_ENDIAN;
90 if (session_info->unix_token && session_info->unix_info && session_info->security_token) {
91 /* Don't call create_local_token(), we already have the full details here */
92 p->session_info = talloc_steal(p, session_info);
95 struct auth_user_info_dc *auth_user_info_dc;
96 struct auth_serversupplied_info *server_info;
97 struct netr_SamInfo3 *info3;
99 /* Fake up an auth_user_info_dc for now, to make an info3, to make the session_info structure */
100 auth_user_info_dc = talloc_zero(p, struct auth_user_info_dc);
101 if (!auth_user_info_dc) {
107 auth_user_info_dc->num_sids = session_info->security_token->num_sids;
108 auth_user_info_dc->sids = session_info->security_token->sids;
109 auth_user_info_dc->info = session_info->info;
110 auth_user_info_dc->user_session_key = session_info->session_key;
112 /* This creates the input structure that make_server_info_info3 is looking for */
113 status = auth_convert_user_info_dc_saminfo3(p, auth_user_info_dc,
116 if (!NT_STATUS_IS_OK(status)) {
117 DEBUG(1, ("Failed to convert auth_user_info_dc into netr_SamInfo3\n"));
123 status = make_server_info_info3(p,
124 info3->base.account_name.string,
125 info3->base.domain.string,
126 &server_info, info3);
127 if (!NT_STATUS_IS_OK(status)) {
128 DEBUG(1, ("Failed to init server info\n"));
135 * Some internal functions need a local token to determine access to
138 status = create_local_token(p, server_info, &session_info->session_key, &p->session_info);
139 talloc_free(server_info);
140 if (!NT_STATUS_IS_OK(status)) {
141 DEBUG(1, ("Failed to init local auth token\n"));
148 p->remote_address = tsocket_address_copy(remote_address, p);
149 if (p->remote_address == NULL) {
155 if (local_address != NULL) {
156 p->local_address = tsocket_address_copy(local_address, p);
157 if (p->local_address == NULL) {
164 talloc_set_destructor(p, close_internal_rpc_pipe_hnd);
170 /* Start listening on the appropriate unix socket and setup all is needed to
171 * dispatch requests to the pipes rpc implementation */
173 struct dcerpc_ncacn_listen_state {
174 struct ndr_syntax_id syntax_id;
182 struct tevent_context *ev_ctx;
183 struct messaging_context *msg_ctx;
184 dcerpc_ncacn_disconnect_fn disconnect_fn;
187 static void named_pipe_listener(struct tevent_context *ev,
188 struct tevent_fd *fde,
192 bool setup_named_pipe_socket(const char *pipe_name,
193 struct tevent_context *ev_ctx,
194 struct messaging_context *msg_ctx)
196 struct dcerpc_ncacn_listen_state *state;
197 struct tevent_fd *fde;
200 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
202 DEBUG(0, ("Out of memory\n"));
205 state->ep.name = talloc_strdup(state, pipe_name);
206 if (state->ep.name == NULL) {
207 DEBUG(0, ("Out of memory\n"));
212 state->ev_ctx = ev_ctx;
213 state->msg_ctx = msg_ctx;
216 * As lp_ncalrpc_dir() should have 0755, but
217 * lp_ncalrpc_dir()/np should have 0700, we need to
218 * create lp_ncalrpc_dir() first.
220 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) {
221 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
222 lp_ncalrpc_dir(), strerror(errno)));
226 np_dir = talloc_asprintf(state, "%s/np", lp_ncalrpc_dir());
228 DEBUG(0, ("Out of memory\n"));
232 if (!directory_create_or_exist(np_dir, geteuid(), 0700)) {
233 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
234 np_dir, strerror(errno)));
238 state->fd = create_pipe_sock(np_dir, pipe_name, 0700);
239 if (state->fd == -1) {
240 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
245 DEBUG(10, ("Openened pipe socket fd %d for %s\n",
246 state->fd, pipe_name));
248 fde = tevent_add_fd(ev_ctx,
249 state, state->fd, TEVENT_FD_READ,
250 named_pipe_listener, state);
252 DEBUG(0, ("Failed to add event handler!\n"));
256 tevent_fd_set_auto_close(fde);
260 if (state->fd != -1) {
267 static void named_pipe_accept_function(struct tevent_context *ev_ctx,
268 struct messaging_context *msg_ctx,
269 const char *pipe_name,
272 static void named_pipe_listener(struct tevent_context *ev,
273 struct tevent_fd *fde,
277 struct dcerpc_ncacn_listen_state *state =
278 talloc_get_type_abort(private_data,
279 struct dcerpc_ncacn_listen_state);
280 struct sockaddr_un sunaddr;
284 /* TODO: should we have a limit to the number of clients ? */
286 len = sizeof(sunaddr);
288 sd = accept(state->fd,
289 (struct sockaddr *)(void *)&sunaddr, &len);
292 if (errno != EINTR) {
293 DEBUG(6, ("Failed to get a valid socket [%s]\n",
299 DEBUG(6, ("Accepted socket %d\n", sd));
301 named_pipe_accept_function(state->ev_ctx,
308 /* This is the core of the rpc server.
309 * Accepts connections from clients and process requests using the appropriate
310 * dispatcher table. */
312 struct named_pipe_client {
313 const char *pipe_name;
315 struct tevent_context *ev;
316 struct messaging_context *msg_ctx;
319 uint16_t device_state;
320 uint64_t allocation_size;
322 struct tstream_context *tstream;
324 struct tsocket_address *client;
326 struct tsocket_address *server;
329 struct auth_session_info *session_info;
331 struct pipes_struct *p;
333 struct tevent_queue *write_queue;
339 static void named_pipe_accept_done(struct tevent_req *subreq);
341 static void named_pipe_accept_function(struct tevent_context *ev_ctx,
342 struct messaging_context *msg_ctx,
343 const char *pipe_name,
346 struct named_pipe_client *npc;
347 struct tstream_context *plain;
348 struct tevent_req *subreq;
351 npc = talloc_zero(NULL, struct named_pipe_client);
353 DEBUG(0, ("Out of memory!\n"));
357 npc->pipe_name = pipe_name;
359 npc->msg_ctx = msg_ctx;
361 /* make sure socket is in NON blocking state */
362 ret = set_blocking(fd, false);
364 DEBUG(2, ("Failed to make socket non-blocking\n"));
370 ret = tstream_bsd_existing_socket(npc, fd, &plain);
372 DEBUG(2, ("Failed to create tstream socket\n"));
378 npc->file_type = FILE_TYPE_MESSAGE_MODE_PIPE;
379 npc->device_state = 0xff | 0x0400 | 0x0100;
380 npc->allocation_size = 4096;
382 subreq = tstream_npa_accept_existing_send(npc, npc->ev, plain,
385 npc->allocation_size);
387 DEBUG(2, ("Failed to start async accept procedure\n"));
392 tevent_req_set_callback(subreq, named_pipe_accept_done, npc);
395 static void named_pipe_packet_process(struct tevent_req *subreq);
396 static void named_pipe_packet_done(struct tevent_req *subreq);
398 static void named_pipe_accept_done(struct tevent_req *subreq)
400 struct auth_session_info_transport *session_info_transport;
401 struct named_pipe_client *npc =
402 tevent_req_callback_data(subreq, struct named_pipe_client);
406 ret = tstream_npa_accept_existing_recv(subreq, &error, npc,
412 &session_info_transport);
414 npc->session_info = talloc_move(npc, &session_info_transport->session_info);
418 DEBUG(2, ("Failed to accept named pipe connection! (%s)\n",
424 ret = make_server_pipes_struct(npc,
426 npc->pipe_name, NCACN_NP,
427 false, npc->server, npc->client, npc->session_info,
430 DEBUG(2, ("Failed to create pipes_struct! (%s)\n",
435 npc->write_queue = tevent_queue_create(npc, "np_server_write_queue");
436 if (!npc->write_queue) {
437 DEBUG(2, ("Failed to set up write queue!\n"));
441 /* And now start receiving and processing packets */
442 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
444 DEBUG(2, ("Failed to start receving packets\n"));
447 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
451 DEBUG(2, ("Fatal error. Terminating client(%s) connection!\n",
453 /* terminate client connection */
458 static void named_pipe_packet_process(struct tevent_req *subreq)
460 struct named_pipe_client *npc =
461 tevent_req_callback_data(subreq, struct named_pipe_client);
462 struct _output_data *out = &npc->p->out_data;
463 DATA_BLOB recv_buffer = data_blob_null;
464 struct ncacn_packet *pkt;
472 status = dcerpc_read_ncacn_packet_recv(subreq, npc, &pkt, &recv_buffer);
474 if (!NT_STATUS_IS_OK(status)) {
478 data_left = recv_buffer.length;
479 data = (char *)recv_buffer.data;
483 data_used = process_incoming_data(npc->p, data, data_left);
485 DEBUG(3, ("Failed to process dceprc request!\n"));
486 status = NT_STATUS_UNEXPECTED_IO_ERROR;
490 data_left -= data_used;
494 /* Do not leak this buffer, npc is a long lived context */
495 talloc_free(recv_buffer.data);
498 /* this is needed because of the way DCERPC Binds work in
499 * the RPC marshalling code */
500 to_send = out->frag.length - out->current_pdu_sent;
503 DEBUG(10, ("Current_pdu_len = %u, "
504 "current_pdu_sent = %u "
505 "Returning %u bytes\n",
506 (unsigned int)out->frag.length,
507 (unsigned int)out->current_pdu_sent,
508 (unsigned int)to_send));
510 npc->iov = talloc_zero(npc, struct iovec);
512 status = NT_STATUS_NO_MEMORY;
517 npc->iov[0].iov_base = out->frag.data
518 + out->current_pdu_sent;
519 npc->iov[0].iov_len = to_send;
521 out->current_pdu_sent += to_send;
524 /* this condition is false for bind packets, or when we haven't
525 * yet got a full request, and need to wait for more data from
527 while (out->data_sent_length < out->rdata.length) {
529 ok = create_next_pdu(npc->p);
531 DEBUG(3, ("Failed to create next PDU!\n"));
532 status = NT_STATUS_UNEXPECTED_IO_ERROR;
536 npc->iov = talloc_realloc(npc, npc->iov,
537 struct iovec, npc->count + 1);
539 status = NT_STATUS_NO_MEMORY;
543 npc->iov[npc->count].iov_base = out->frag.data;
544 npc->iov[npc->count].iov_len = out->frag.length;
546 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
547 (unsigned int)npc->count,
548 (unsigned int)npc->iov[npc->count].iov_len));
549 dump_data(11, (const uint8_t *)npc->iov[npc->count].iov_base,
550 npc->iov[npc->count].iov_len);
554 /* we still don't have a complete request, go back and wait for more
556 if (npc->count == 0) {
557 /* Wait for the next packet */
558 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
560 DEBUG(2, ("Failed to start receving packets\n"));
561 status = NT_STATUS_NO_MEMORY;
564 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
568 DEBUG(10, ("Sending a total of %u bytes\n",
569 (unsigned int)npc->p->out_data.data_sent_length));
571 subreq = tstream_writev_queue_send(npc, npc->ev,
574 npc->iov, npc->count);
576 DEBUG(2, ("Failed to send packet\n"));
577 status = NT_STATUS_NO_MEMORY;
580 tevent_req_set_callback(subreq, named_pipe_packet_done, npc);
584 DEBUG(2, ("Fatal error(%s). "
585 "Terminating client(%s) connection!\n",
586 nt_errstr(status), npc->client_name));
587 /* terminate client connection */
592 static void named_pipe_packet_done(struct tevent_req *subreq)
594 struct named_pipe_client *npc =
595 tevent_req_callback_data(subreq, struct named_pipe_client);
599 ret = tstream_writev_queue_recv(subreq, &sys_errno);
602 DEBUG(2, ("Writev failed!\n"));
606 /* clear out any data that may have been left around */
608 TALLOC_FREE(npc->iov);
609 data_blob_free(&npc->p->in_data.data);
610 data_blob_free(&npc->p->out_data.frag);
611 data_blob_free(&npc->p->out_data.rdata);
613 /* Wait for the next packet */
614 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
616 DEBUG(2, ("Failed to start receving packets\n"));
620 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
624 DEBUG(2, ("Fatal error(%s). "
625 "Terminating client(%s) connection!\n",
626 strerror(sys_errno), npc->client_name));
627 /* terminate client connection */
632 static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
633 struct messaging_context *msg_ctx,
634 enum dcerpc_transport_t transport,
637 struct tsocket_address *cli_addr,
638 struct tsocket_address *srv_addr,
640 dcerpc_ncacn_disconnect_fn fn);
642 /********************************************************************
643 * Start listening on the tcp/ip socket
644 ********************************************************************/
646 static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
647 struct tevent_fd *fde,
651 uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context *ev_ctx,
652 struct messaging_context *msg_ctx,
653 const struct sockaddr_storage *ifss,
656 struct dcerpc_ncacn_listen_state *state;
657 struct tevent_fd *fde;
660 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
662 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Out of memory\n"));
667 state->ep.port = port;
668 state->disconnect_fn = NULL;
670 if (state->ep.port == 0) {
673 for (i = SERVER_TCP_LOW_PORT; i <= SERVER_TCP_HIGH_PORT; i++) {
674 state->fd = open_socket_in(SOCK_STREAM,
685 state->fd = open_socket_in(SOCK_STREAM,
691 if (state->fd == -1) {
692 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Failed to create "
693 "socket on port %u!\n", state->ep.port));
697 state->ev_ctx = ev_ctx;
698 state->msg_ctx = msg_ctx;
700 /* ready to listen */
701 set_socket_options(state->fd, "SO_KEEPALIVE");
702 set_socket_options(state->fd, lp_socket_options());
704 /* Set server socket to non-blocking for the accept. */
705 set_blocking(state->fd, false);
707 rc = listen(state->fd, SMBD_LISTEN_BACKLOG);
709 DEBUG(0,("setup_tcpip_socket: listen - %s\n", strerror(errno)));
713 DEBUG(10, ("setup_tcpip_socket: openened socket fd %d for port %u\n",
714 state->fd, state->ep.port));
716 fde = tevent_add_fd(state->ev_ctx,
720 dcerpc_ncacn_tcpip_listener,
723 DEBUG(0, ("setup_tcpip_socket: Failed to add event handler!\n"));
727 tevent_fd_set_auto_close(fde);
729 return state->ep.port;
731 if (state->fd != -1) {
739 static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
740 struct tevent_fd *fde,
744 struct dcerpc_ncacn_listen_state *state =
745 talloc_get_type_abort(private_data,
746 struct dcerpc_ncacn_listen_state);
747 struct tsocket_address *cli_addr = NULL;
748 struct tsocket_address *srv_addr = NULL;
749 struct sockaddr_storage addr;
750 socklen_t in_addrlen = sizeof(addr);
754 s = accept(state->fd, (struct sockaddr *)(void *) &addr, &in_addrlen);
756 if (errno != EINTR) {
757 DEBUG(0,("tcpip_listener accept: %s\n",
763 rc = tsocket_address_bsd_from_sockaddr(state,
764 (struct sockaddr *)(void *) &addr,
772 rc = getsockname(s, (struct sockaddr *)(void *) &addr, &in_addrlen);
778 rc = tsocket_address_bsd_from_sockaddr(state,
779 (struct sockaddr *)(void *) &addr,
787 DEBUG(6, ("tcpip_listener: Accepted socket %d\n", s));
789 dcerpc_ncacn_accept(state->ev_ctx,
800 /********************************************************************
801 * Start listening on the ncalrpc socket
802 ********************************************************************/
804 static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
805 struct tevent_fd *fde,
809 bool setup_dcerpc_ncalrpc_socket(struct tevent_context *ev_ctx,
810 struct messaging_context *msg_ctx,
812 dcerpc_ncacn_disconnect_fn fn)
814 struct dcerpc_ncacn_listen_state *state;
815 struct tevent_fd *fde;
817 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
819 DEBUG(0, ("Out of memory\n"));
824 state->disconnect_fn = fn;
829 state->ep.name = talloc_strdup(state, name);
831 if (state->ep.name == NULL) {
832 DEBUG(0, ("Out of memory\n"));
837 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) {
838 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
839 lp_ncalrpc_dir(), strerror(errno)));
843 state->fd = create_pipe_sock(lp_ncalrpc_dir(), name, 0755);
844 if (state->fd == -1) {
845 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
846 lp_ncalrpc_dir(), name));
850 DEBUG(10, ("Openened pipe socket fd %d for %s\n", state->fd, name));
852 state->ev_ctx = ev_ctx;
853 state->msg_ctx = msg_ctx;
855 /* Set server socket to non-blocking for the accept. */
856 set_blocking(state->fd, false);
858 fde = tevent_add_fd(state->ev_ctx,
862 dcerpc_ncalrpc_listener,
865 DEBUG(0, ("Failed to add event handler for ncalrpc!\n"));
869 tevent_fd_set_auto_close(fde);
873 if (state->fd != -1) {
881 static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
882 struct tevent_fd *fde,
886 struct dcerpc_ncacn_listen_state *state =
887 talloc_get_type_abort(private_data,
888 struct dcerpc_ncacn_listen_state);
889 struct tsocket_address *cli_addr = NULL;
890 struct sockaddr_un sunaddr;
891 struct sockaddr *addr = (struct sockaddr *)(void *)&sunaddr;
892 socklen_t len = sizeof(sunaddr);
896 ZERO_STRUCT(sunaddr);
898 sd = accept(state->fd, addr, &len);
900 if (errno != EINTR) {
901 DEBUG(0, ("ncalrpc accept() failed: %s\n", strerror(errno)));
906 rc = tsocket_address_bsd_from_sockaddr(state,
914 DEBUG(10, ("Accepted ncalrpc socket %d\n", sd));
916 dcerpc_ncacn_accept(state->ev_ctx,
921 state->disconnect_fn);
924 struct dcerpc_ncacn_conn {
925 enum dcerpc_transport_t transport;
934 struct pipes_struct *p;
935 dcerpc_ncacn_disconnect_fn disconnect_fn;
937 struct tevent_context *ev_ctx;
938 struct messaging_context *msg_ctx;
940 struct tstream_context *tstream;
941 struct tevent_queue *send_queue;
943 struct tsocket_address *client;
945 struct tsocket_address *server;
947 struct auth_session_info *session_info;
953 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq);
954 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq);
956 static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
957 struct messaging_context *msg_ctx,
958 enum dcerpc_transport_t transport,
961 struct tsocket_address *cli_addr,
962 struct tsocket_address *srv_addr,
964 dcerpc_ncacn_disconnect_fn fn) {
965 struct dcerpc_ncacn_conn *ncacn_conn;
966 struct tevent_req *subreq;
968 const char *srv_str = NULL;
969 bool system_user = false;
976 DEBUG(10, ("dcerpc_ncacn_accept\n"));
978 ncacn_conn = talloc_zero(ev_ctx, struct dcerpc_ncacn_conn);
979 if (ncacn_conn == NULL) {
980 DEBUG(0, ("Out of memory!\n"));
985 ncacn_conn->transport = transport;
986 ncacn_conn->ev_ctx = ev_ctx;
987 ncacn_conn->msg_ctx = msg_ctx;
988 ncacn_conn->sock = s;
989 ncacn_conn->disconnect_fn = fn;
991 ncacn_conn->client = talloc_move(ncacn_conn, &cli_addr);
992 if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
993 ncacn_conn->client_name =
994 tsocket_address_inet_addr_string(ncacn_conn->client,
997 ncacn_conn->client_name =
998 tsocket_address_unix_path(ncacn_conn->client,
1001 if (ncacn_conn->client_name == NULL) {
1002 DEBUG(0, ("Out of memory!\n"));
1003 talloc_free(ncacn_conn);
1008 if (srv_addr != NULL) {
1009 ncacn_conn->server = talloc_move(ncacn_conn, &srv_addr);
1011 ncacn_conn->server_name =
1012 tsocket_address_inet_addr_string(ncacn_conn->server,
1014 if (ncacn_conn->server_name == NULL) {
1015 DEBUG(0, ("Out of memory!\n"));
1016 talloc_free(ncacn_conn);
1022 switch (transport) {
1024 ncacn_conn->ep.port = port;
1026 pipe_name = tsocket_address_string(ncacn_conn->client,
1028 if (pipe_name == NULL) {
1030 talloc_free(ncacn_conn);
1036 rc = sys_getpeereid(s, &uid);
1038 DEBUG(2, ("Failed to get ncalrpc connecting uid!"));
1040 if (uid == sec_initial_uid()) {
1045 ncacn_conn->ep.name = talloc_strdup(ncacn_conn, name);
1046 if (ncacn_conn->ep.name == NULL) {
1048 talloc_free(ncacn_conn);
1052 pipe_name = talloc_strdup(ncacn_conn,
1054 if (pipe_name == NULL) {
1056 talloc_free(ncacn_conn);
1061 DEBUG(0, ("unknown dcerpc transport: %u!\n",
1063 talloc_free(ncacn_conn);
1068 rc = set_blocking(s, false);
1070 DEBUG(2, ("Failed to set dcerpc socket to non-blocking\n"));
1071 talloc_free(ncacn_conn);
1077 * As soon as we have tstream_bsd_existing_socket set up it will
1078 * take care of closing the socket.
1080 rc = tstream_bsd_existing_socket(ncacn_conn, s, &ncacn_conn->tstream);
1082 DEBUG(2, ("Failed to create tstream socket for dcerpc\n"));
1083 talloc_free(ncacn_conn);
1088 if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
1089 cli_str = ncacn_conn->client_name;
1094 if (ncacn_conn->server != NULL) {
1095 if (tsocket_address_is_inet(ncacn_conn->server, "ip")) {
1096 srv_str = ncacn_conn->server_name;
1102 if (ncacn_conn->session_info == NULL) {
1103 status = auth_anonymous_session_info(ncacn_conn,
1104 &ncacn_conn->session_info);
1105 if (!NT_STATUS_IS_OK(status)) {
1106 DEBUG(2, ("Failed to create "
1107 "auth_anonymous_session_info - %s\n",
1108 nt_errstr(status)));
1109 talloc_free(ncacn_conn);
1114 rc = make_server_pipes_struct(ncacn_conn,
1115 ncacn_conn->msg_ctx,
1117 ncacn_conn->transport,
1121 ncacn_conn->session_info,
1125 DEBUG(2, ("Failed to create pipe struct - %s",
1126 strerror(sys_errno)));
1127 talloc_free(ncacn_conn);
1131 ncacn_conn->send_queue = tevent_queue_create(ncacn_conn,
1132 "dcerpc send queue");
1133 if (ncacn_conn->send_queue == NULL) {
1134 DEBUG(0, ("Out of memory!\n"));
1135 talloc_free(ncacn_conn);
1139 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1141 ncacn_conn->tstream);
1142 if (subreq == NULL) {
1143 DEBUG(2, ("Failed to send ncacn packet\n"));
1144 talloc_free(ncacn_conn);
1148 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1150 DEBUG(10, ("dcerpc_ncacn_accept done\n"));
1155 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq)
1157 struct dcerpc_ncacn_conn *ncacn_conn =
1158 tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
1160 struct _output_data *out = &ncacn_conn->p->out_data;
1161 DATA_BLOB recv_buffer = data_blob_null;
1162 struct ncacn_packet *pkt;
1170 status = dcerpc_read_ncacn_packet_recv(subreq, ncacn_conn, &pkt, &recv_buffer);
1171 TALLOC_FREE(subreq);
1172 if (!NT_STATUS_IS_OK(status)) {
1173 if (ncacn_conn->disconnect_fn != NULL) {
1174 ok = ncacn_conn->disconnect_fn(ncacn_conn->p);
1176 DEBUG(3, ("Failed to call disconnect function\n"));
1182 data_left = recv_buffer.length;
1183 data = (char *) recv_buffer.data;
1186 data_used = process_incoming_data(ncacn_conn->p, data, data_left);
1187 if (data_used < 0) {
1188 DEBUG(3, ("Failed to process dcerpc request!\n"));
1189 status = NT_STATUS_UNEXPECTED_IO_ERROR;
1193 data_left -= data_used;
1197 /* Do not leak this buffer */
1198 talloc_free(recv_buffer.data);
1202 * This is needed because of the way DCERPC binds work in the RPC
1205 to_send = out->frag.length - out->current_pdu_sent;
1208 DEBUG(10, ("Current_pdu_len = %u, "
1209 "current_pdu_sent = %u "
1210 "Returning %u bytes\n",
1211 (unsigned int)out->frag.length,
1212 (unsigned int)out->current_pdu_sent,
1213 (unsigned int)to_send));
1215 ncacn_conn->iov = talloc_zero(ncacn_conn, struct iovec);
1216 if (ncacn_conn->iov == NULL) {
1217 status = NT_STATUS_NO_MEMORY;
1218 DEBUG(3, ("Out of memory!\n"));
1221 ncacn_conn->count = 1;
1223 ncacn_conn->iov[0].iov_base = out->frag.data
1224 + out->current_pdu_sent;
1225 ncacn_conn->iov[0].iov_len = to_send;
1227 out->current_pdu_sent += to_send;
1231 * This condition is false for bind packets, or when we haven't yet got
1232 * a full request, and need to wait for more data from the client
1234 while (out->data_sent_length < out->rdata.length) {
1235 ok = create_next_pdu(ncacn_conn->p);
1237 DEBUG(3, ("Failed to create next PDU!\n"));
1238 status = NT_STATUS_UNEXPECTED_IO_ERROR;
1242 ncacn_conn->iov = talloc_realloc(ncacn_conn,
1245 ncacn_conn->count + 1);
1246 if (ncacn_conn->iov == NULL) {
1247 DEBUG(3, ("Out of memory!\n"));
1248 status = NT_STATUS_NO_MEMORY;
1252 ncacn_conn->iov[ncacn_conn->count].iov_base = out->frag.data;
1253 ncacn_conn->iov[ncacn_conn->count].iov_len = out->frag.length;
1255 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
1256 (unsigned int) ncacn_conn->count,
1257 (unsigned int) ncacn_conn->iov[ncacn_conn->count].iov_len));
1258 dump_data(11, (const uint8_t *) ncacn_conn->iov[ncacn_conn->count].iov_base,
1259 ncacn_conn->iov[ncacn_conn->count].iov_len);
1260 ncacn_conn->count++;
1264 * We still don't have a complete request, go back and wait for more
1267 if (ncacn_conn->count == 0) {
1268 /* Wait for the next packet */
1269 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1271 ncacn_conn->tstream);
1272 if (subreq == NULL) {
1273 DEBUG(2, ("Failed to start receving packets\n"));
1274 status = NT_STATUS_NO_MEMORY;
1277 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1281 DEBUG(10, ("Sending a total of %u bytes\n",
1282 (unsigned int)ncacn_conn->p->out_data.data_sent_length));
1284 subreq = tstream_writev_queue_send(ncacn_conn,
1286 ncacn_conn->tstream,
1287 ncacn_conn->send_queue,
1290 if (subreq == NULL) {
1291 DEBUG(2, ("Failed to send packet\n"));
1292 status = NT_STATUS_NO_MEMORY;
1296 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_done, ncacn_conn);
1300 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1301 ncacn_conn->client_name, nt_errstr(status)));
1303 /* Terminate client connection */
1304 talloc_free(ncacn_conn);
1308 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq)
1310 struct dcerpc_ncacn_conn *ncacn_conn =
1311 tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
1312 NTSTATUS status = NT_STATUS_OK;
1316 rc = tstream_writev_queue_recv(subreq, &sys_errno);
1317 TALLOC_FREE(subreq);
1319 DEBUG(2, ("Writev failed!\n"));
1320 status = map_nt_error_from_unix(sys_errno);
1324 /* clear out any data that may have been left around */
1325 ncacn_conn->count = 0;
1326 TALLOC_FREE(ncacn_conn->iov);
1327 data_blob_free(&ncacn_conn->p->in_data.data);
1328 data_blob_free(&ncacn_conn->p->out_data.frag);
1329 data_blob_free(&ncacn_conn->p->out_data.rdata);
1331 /* Wait for the next packet */
1332 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1334 ncacn_conn->tstream);
1335 if (subreq == NULL) {
1336 DEBUG(2, ("Failed to start receving packets\n"));
1337 status = NT_STATUS_NO_MEMORY;
1341 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1345 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1346 ncacn_conn->client_name, nt_errstr(status)));
1348 /* Terminate client connection */
1349 talloc_free(ncacn_conn);
1353 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */