s3-rpc_server: Create common function to allocate pipes_struct
[Samba/bjacke.git] / source3 / rpc_server / rpc_server.c
blob43e549b5ae3c4772ccf9c0ff68fd2ab5bc5bbbe8
1 /*
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/>.
22 #include "includes.h"
23 #include "rpc_server/rpc_pipes.h"
24 #include "rpc_server/rpc_server.h"
25 #include "rpc_dce.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"
31 #include "auth.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)
42 NTSTATUS status;
44 status = make_session_info_guest(mem_ctx, session_info);
45 if (!NT_STATUS_IS_OK(status)) {
46 return status;
49 return NT_STATUS_OK;
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,
63 int *perrno)
65 struct pipes_struct *p;
66 NTSTATUS status;
67 int ret;
69 ret = make_base_pipes_struct(mem_ctx, NULL, pipe_name,
70 transport, RPC_LITTLE_ENDIAN,
71 ncalrpc_as_system,
72 remote_address, local_address, &p);
73 if (ret) {
74 *perrno = ret;
75 return -1;
77 p->msg_ctx = msg_ctx;
79 if (session_info->unix_token && session_info->unix_info && session_info->security_token) {
80 /* Don't call create_local_token(), we already have the full details here */
81 p->session_info = talloc_steal(p, session_info);
83 } else {
84 struct auth_user_info_dc *auth_user_info_dc;
85 struct auth_serversupplied_info *server_info;
86 struct netr_SamInfo3 *info3;
88 /* Fake up an auth_user_info_dc for now, to make an info3, to make the session_info structure */
89 auth_user_info_dc = talloc_zero(p, struct auth_user_info_dc);
90 if (!auth_user_info_dc) {
91 TALLOC_FREE(p);
92 *perrno = ENOMEM;
93 return -1;
96 auth_user_info_dc->num_sids = session_info->security_token->num_sids;
97 auth_user_info_dc->sids = session_info->security_token->sids;
98 auth_user_info_dc->info = session_info->info;
99 auth_user_info_dc->user_session_key = session_info->session_key;
101 /* This creates the input structure that make_server_info_info3 is looking for */
102 status = auth_convert_user_info_dc_saminfo3(p, auth_user_info_dc,
103 &info3);
105 if (!NT_STATUS_IS_OK(status)) {
106 DEBUG(1, ("Failed to convert auth_user_info_dc into netr_SamInfo3\n"));
107 TALLOC_FREE(p);
108 *perrno = EINVAL;
109 return -1;
112 status = make_server_info_info3(p,
113 info3->base.account_name.string,
114 info3->base.domain.string,
115 &server_info, info3);
116 if (!NT_STATUS_IS_OK(status)) {
117 DEBUG(1, ("Failed to init server info\n"));
118 TALLOC_FREE(p);
119 *perrno = EINVAL;
120 return -1;
124 * Some internal functions need a local token to determine access to
125 * resources.
127 status = create_local_token(p, server_info, &session_info->session_key, &p->session_info);
128 talloc_free(server_info);
129 if (!NT_STATUS_IS_OK(status)) {
130 DEBUG(1, ("Failed to init local auth token\n"));
131 TALLOC_FREE(p);
132 *perrno = EINVAL;
133 return -1;
137 *_p = p;
138 return 0;
141 /* Start listening on the appropriate unix socket and setup all is needed to
142 * dispatch requests to the pipes rpc implementation */
144 struct dcerpc_ncacn_listen_state {
145 struct ndr_syntax_id syntax_id;
147 int fd;
148 union {
149 char *name;
150 uint16_t port;
151 } ep;
153 struct tevent_context *ev_ctx;
154 struct messaging_context *msg_ctx;
155 dcerpc_ncacn_disconnect_fn disconnect_fn;
158 static void named_pipe_listener(struct tevent_context *ev,
159 struct tevent_fd *fde,
160 uint16_t flags,
161 void *private_data);
163 bool setup_named_pipe_socket(const char *pipe_name,
164 struct tevent_context *ev_ctx,
165 struct messaging_context *msg_ctx)
167 struct dcerpc_ncacn_listen_state *state;
168 struct tevent_fd *fde;
169 char *np_dir;
171 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
172 if (!state) {
173 DEBUG(0, ("Out of memory\n"));
174 return false;
176 state->ep.name = talloc_strdup(state, pipe_name);
177 if (state->ep.name == NULL) {
178 DEBUG(0, ("Out of memory\n"));
179 goto out;
181 state->fd = -1;
183 state->ev_ctx = ev_ctx;
184 state->msg_ctx = msg_ctx;
187 * As lp_ncalrpc_dir() should have 0755, but
188 * lp_ncalrpc_dir()/np should have 0700, we need to
189 * create lp_ncalrpc_dir() first.
191 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) {
192 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
193 lp_ncalrpc_dir(), strerror(errno)));
194 goto out;
197 np_dir = talloc_asprintf(state, "%s/np", lp_ncalrpc_dir());
198 if (!np_dir) {
199 DEBUG(0, ("Out of memory\n"));
200 goto out;
203 if (!directory_create_or_exist(np_dir, geteuid(), 0700)) {
204 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
205 np_dir, strerror(errno)));
206 goto out;
209 state->fd = create_pipe_sock(np_dir, pipe_name, 0700);
210 if (state->fd == -1) {
211 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
212 np_dir, pipe_name));
213 goto out;
215 talloc_free(np_dir);
217 DEBUG(10, ("Openened pipe socket fd %d for %s\n",
218 state->fd, pipe_name));
220 fde = tevent_add_fd(ev_ctx,
221 state, state->fd, TEVENT_FD_READ,
222 named_pipe_listener, state);
223 if (!fde) {
224 DEBUG(0, ("Failed to add event handler!\n"));
225 goto out;
228 tevent_fd_set_auto_close(fde);
229 return true;
231 out:
232 if (state->fd != -1) {
233 close(state->fd);
235 TALLOC_FREE(state);
236 return false;
239 static void named_pipe_accept_function(struct tevent_context *ev_ctx,
240 struct messaging_context *msg_ctx,
241 const char *pipe_name,
242 int fd);
244 static void named_pipe_listener(struct tevent_context *ev,
245 struct tevent_fd *fde,
246 uint16_t flags,
247 void *private_data)
249 struct dcerpc_ncacn_listen_state *state =
250 talloc_get_type_abort(private_data,
251 struct dcerpc_ncacn_listen_state);
252 struct sockaddr_un sunaddr;
253 socklen_t len;
254 int sd = -1;
256 /* TODO: should we have a limit to the number of clients ? */
258 len = sizeof(sunaddr);
260 sd = accept(state->fd,
261 (struct sockaddr *)(void *)&sunaddr, &len);
263 if (sd == -1) {
264 if (errno != EINTR) {
265 DEBUG(6, ("Failed to get a valid socket [%s]\n",
266 strerror(errno)));
268 return;
271 DEBUG(6, ("Accepted socket %d\n", sd));
273 named_pipe_accept_function(state->ev_ctx,
274 state->msg_ctx,
275 state->ep.name,
276 sd);
280 /* This is the core of the rpc server.
281 * Accepts connections from clients and process requests using the appropriate
282 * dispatcher table. */
284 struct named_pipe_client {
285 const char *pipe_name;
287 struct tevent_context *ev;
288 struct messaging_context *msg_ctx;
290 uint16_t file_type;
291 uint16_t device_state;
292 uint64_t allocation_size;
294 struct tstream_context *tstream;
296 struct tsocket_address *client;
297 char *client_name;
298 struct tsocket_address *server;
299 char *server_name;
301 struct auth_session_info *session_info;
303 struct pipes_struct *p;
305 struct tevent_queue *write_queue;
307 struct iovec *iov;
308 size_t count;
311 static void named_pipe_accept_done(struct tevent_req *subreq);
313 static void named_pipe_accept_function(struct tevent_context *ev_ctx,
314 struct messaging_context *msg_ctx,
315 const char *pipe_name,
316 int fd)
318 struct named_pipe_client *npc;
319 struct tstream_context *plain;
320 struct tevent_req *subreq;
321 int ret;
323 npc = talloc_zero(NULL, struct named_pipe_client);
324 if (!npc) {
325 DEBUG(0, ("Out of memory!\n"));
326 close(fd);
327 return;
330 npc->pipe_name = talloc_strdup(npc, pipe_name);
331 if (npc->pipe_name == NULL) {
332 DEBUG(0, ("Out of memory!\n"));
333 TALLOC_FREE(npc);
334 close(fd);
335 return;
337 npc->ev = ev_ctx;
338 npc->msg_ctx = msg_ctx;
340 /* make sure socket is in NON blocking state */
341 ret = set_blocking(fd, false);
342 if (ret != 0) {
343 DEBUG(2, ("Failed to make socket non-blocking\n"));
344 TALLOC_FREE(npc);
345 close(fd);
346 return;
349 ret = tstream_bsd_existing_socket(npc, fd, &plain);
350 if (ret != 0) {
351 DEBUG(2, ("Failed to create tstream socket\n"));
352 TALLOC_FREE(npc);
353 close(fd);
354 return;
357 npc->file_type = FILE_TYPE_MESSAGE_MODE_PIPE;
358 npc->device_state = 0xff | 0x0400 | 0x0100;
359 npc->allocation_size = 4096;
361 subreq = tstream_npa_accept_existing_send(npc, npc->ev, plain,
362 npc->file_type,
363 npc->device_state,
364 npc->allocation_size);
365 if (!subreq) {
366 DEBUG(2, ("Failed to start async accept procedure\n"));
367 TALLOC_FREE(npc);
368 close(fd);
369 return;
371 tevent_req_set_callback(subreq, named_pipe_accept_done, npc);
374 static void named_pipe_packet_process(struct tevent_req *subreq);
375 static void named_pipe_packet_done(struct tevent_req *subreq);
377 static void named_pipe_accept_done(struct tevent_req *subreq)
379 struct auth_session_info_transport *session_info_transport;
380 struct named_pipe_client *npc =
381 tevent_req_callback_data(subreq, struct named_pipe_client);
382 int error;
383 int ret;
385 ret = tstream_npa_accept_existing_recv(subreq, &error, npc,
386 &npc->tstream,
387 &npc->client,
388 &npc->client_name,
389 &npc->server,
390 &npc->server_name,
391 &session_info_transport);
393 npc->session_info = talloc_move(npc, &session_info_transport->session_info);
395 TALLOC_FREE(subreq);
396 if (ret != 0) {
397 DEBUG(2, ("Failed to accept named pipe connection! (%s)\n",
398 strerror(error)));
399 TALLOC_FREE(npc);
400 return;
403 ret = make_server_pipes_struct(npc,
404 npc->msg_ctx,
405 npc->pipe_name, NCACN_NP,
406 false, npc->server, npc->client, npc->session_info,
407 &npc->p, &error);
408 if (ret != 0) {
409 DEBUG(2, ("Failed to create pipes_struct! (%s)\n",
410 strerror(error)));
411 goto fail;
414 npc->write_queue = tevent_queue_create(npc, "np_server_write_queue");
415 if (!npc->write_queue) {
416 DEBUG(2, ("Failed to set up write queue!\n"));
417 goto fail;
420 /* And now start receiving and processing packets */
421 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
422 if (!subreq) {
423 DEBUG(2, ("Failed to start receving packets\n"));
424 goto fail;
426 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
427 return;
429 fail:
430 DEBUG(2, ("Fatal error. Terminating client(%s) connection!\n",
431 npc->client_name));
432 /* terminate client connection */
433 talloc_free(npc);
434 return;
437 static void named_pipe_packet_process(struct tevent_req *subreq)
439 struct named_pipe_client *npc =
440 tevent_req_callback_data(subreq, struct named_pipe_client);
441 struct _output_data *out = &npc->p->out_data;
442 DATA_BLOB recv_buffer = data_blob_null;
443 struct ncacn_packet *pkt;
444 NTSTATUS status;
445 ssize_t data_left;
446 ssize_t data_used;
447 char *data;
448 uint32_t to_send;
449 bool ok;
451 status = dcerpc_read_ncacn_packet_recv(subreq, npc, &pkt, &recv_buffer);
452 TALLOC_FREE(subreq);
453 if (!NT_STATUS_IS_OK(status)) {
454 goto fail;
457 data_left = recv_buffer.length;
458 data = (char *)recv_buffer.data;
460 while (data_left) {
462 data_used = process_incoming_data(npc->p, data, data_left);
463 if (data_used < 0) {
464 DEBUG(3, ("Failed to process dceprc request!\n"));
465 status = NT_STATUS_UNEXPECTED_IO_ERROR;
466 goto fail;
469 data_left -= data_used;
470 data += data_used;
473 /* Do not leak this buffer, npc is a long lived context */
474 talloc_free(recv_buffer.data);
475 talloc_free(pkt);
477 /* this is needed because of the way DCERPC Binds work in
478 * the RPC marshalling code */
479 to_send = out->frag.length - out->current_pdu_sent;
480 if (to_send > 0) {
482 DEBUG(10, ("Current_pdu_len = %u, "
483 "current_pdu_sent = %u "
484 "Returning %u bytes\n",
485 (unsigned int)out->frag.length,
486 (unsigned int)out->current_pdu_sent,
487 (unsigned int)to_send));
489 npc->iov = talloc_zero(npc, struct iovec);
490 if (!npc->iov) {
491 status = NT_STATUS_NO_MEMORY;
492 goto fail;
494 npc->count = 1;
496 npc->iov[0].iov_base = out->frag.data
497 + out->current_pdu_sent;
498 npc->iov[0].iov_len = to_send;
500 out->current_pdu_sent += to_send;
503 /* this condition is false for bind packets, or when we haven't
504 * yet got a full request, and need to wait for more data from
505 * the client */
506 while (out->data_sent_length < out->rdata.length) {
508 ok = create_next_pdu(npc->p);
509 if (!ok) {
510 DEBUG(3, ("Failed to create next PDU!\n"));
511 status = NT_STATUS_UNEXPECTED_IO_ERROR;
512 goto fail;
515 npc->iov = talloc_realloc(npc, npc->iov,
516 struct iovec, npc->count + 1);
517 if (!npc->iov) {
518 status = NT_STATUS_NO_MEMORY;
519 goto fail;
522 npc->iov[npc->count].iov_base = out->frag.data;
523 npc->iov[npc->count].iov_len = out->frag.length;
525 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
526 (unsigned int)npc->count,
527 (unsigned int)npc->iov[npc->count].iov_len));
528 dump_data(11, (const uint8_t *)npc->iov[npc->count].iov_base,
529 npc->iov[npc->count].iov_len);
530 npc->count++;
533 /* we still don't have a complete request, go back and wait for more
534 * data */
535 if (npc->count == 0) {
536 /* Wait for the next packet */
537 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
538 if (!subreq) {
539 DEBUG(2, ("Failed to start receving packets\n"));
540 status = NT_STATUS_NO_MEMORY;
541 goto fail;
543 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
544 return;
547 DEBUG(10, ("Sending a total of %u bytes\n",
548 (unsigned int)npc->p->out_data.data_sent_length));
550 subreq = tstream_writev_queue_send(npc, npc->ev,
551 npc->tstream,
552 npc->write_queue,
553 npc->iov, npc->count);
554 if (!subreq) {
555 DEBUG(2, ("Failed to send packet\n"));
556 status = NT_STATUS_NO_MEMORY;
557 goto fail;
559 tevent_req_set_callback(subreq, named_pipe_packet_done, npc);
560 return;
562 fail:
563 DEBUG(2, ("Fatal error(%s). "
564 "Terminating client(%s) connection!\n",
565 nt_errstr(status), npc->client_name));
566 /* terminate client connection */
567 talloc_free(npc);
568 return;
571 static void named_pipe_packet_done(struct tevent_req *subreq)
573 struct named_pipe_client *npc =
574 tevent_req_callback_data(subreq, struct named_pipe_client);
575 int sys_errno;
576 int ret;
578 ret = tstream_writev_queue_recv(subreq, &sys_errno);
579 TALLOC_FREE(subreq);
580 if (ret == -1) {
581 DEBUG(2, ("Writev failed!\n"));
582 goto fail;
585 /* clear out any data that may have been left around */
586 npc->count = 0;
587 TALLOC_FREE(npc->iov);
588 data_blob_free(&npc->p->in_data.data);
589 data_blob_free(&npc->p->out_data.frag);
590 data_blob_free(&npc->p->out_data.rdata);
592 /* Wait for the next packet */
593 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
594 if (!subreq) {
595 DEBUG(2, ("Failed to start receving packets\n"));
596 sys_errno = ENOMEM;
597 goto fail;
599 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
600 return;
602 fail:
603 DEBUG(2, ("Fatal error(%s). "
604 "Terminating client(%s) connection!\n",
605 strerror(sys_errno), npc->client_name));
606 /* terminate client connection */
607 talloc_free(npc);
608 return;
611 static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
612 struct messaging_context *msg_ctx,
613 enum dcerpc_transport_t transport,
614 const char *name,
615 struct tsocket_address *cli_addr,
616 struct tsocket_address *srv_addr,
617 int s,
618 dcerpc_ncacn_disconnect_fn fn);
620 /********************************************************************
621 * Start listening on the tcp/ip socket
622 ********************************************************************/
624 static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
625 struct tevent_fd *fde,
626 uint16_t flags,
627 void *private_data);
629 uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context *ev_ctx,
630 struct messaging_context *msg_ctx,
631 const struct sockaddr_storage *ifss,
632 uint16_t port)
634 struct dcerpc_ncacn_listen_state *state;
635 struct tevent_fd *fde;
636 int rc;
638 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
639 if (state == NULL) {
640 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Out of memory\n"));
641 return 0;
644 state->fd = -1;
645 state->ep.port = port;
646 state->disconnect_fn = NULL;
648 if (state->ep.port == 0) {
649 uint16_t i;
651 for (i = SERVER_TCP_LOW_PORT; i <= SERVER_TCP_HIGH_PORT; i++) {
652 state->fd = open_socket_in(SOCK_STREAM,
655 ifss,
656 false);
657 if (state->fd > 0) {
658 state->ep.port = i;
659 break;
662 } else {
663 state->fd = open_socket_in(SOCK_STREAM,
664 state->ep.port,
666 ifss,
667 true);
669 if (state->fd == -1) {
670 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Failed to create "
671 "socket on port %u!\n", state->ep.port));
672 goto out;
675 state->ev_ctx = ev_ctx;
676 state->msg_ctx = msg_ctx;
678 /* ready to listen */
679 set_socket_options(state->fd, "SO_KEEPALIVE");
680 set_socket_options(state->fd, lp_socket_options());
682 /* Set server socket to non-blocking for the accept. */
683 set_blocking(state->fd, false);
685 rc = listen(state->fd, SMBD_LISTEN_BACKLOG);
686 if (rc == -1) {
687 DEBUG(0,("setup_tcpip_socket: listen - %s\n", strerror(errno)));
688 goto out;
691 DEBUG(10, ("setup_tcpip_socket: openened socket fd %d for port %u\n",
692 state->fd, state->ep.port));
694 fde = tevent_add_fd(state->ev_ctx,
695 state,
696 state->fd,
697 TEVENT_FD_READ,
698 dcerpc_ncacn_tcpip_listener,
699 state);
700 if (fde == NULL) {
701 DEBUG(0, ("setup_tcpip_socket: Failed to add event handler!\n"));
702 goto out;
705 tevent_fd_set_auto_close(fde);
707 return state->ep.port;
708 out:
709 if (state->fd != -1) {
710 close(state->fd);
712 TALLOC_FREE(state);
714 return 0;
717 static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
718 struct tevent_fd *fde,
719 uint16_t flags,
720 void *private_data)
722 struct dcerpc_ncacn_listen_state *state =
723 talloc_get_type_abort(private_data,
724 struct dcerpc_ncacn_listen_state);
725 struct tsocket_address *cli_addr = NULL;
726 struct tsocket_address *srv_addr = NULL;
727 struct sockaddr_storage addr;
728 socklen_t in_addrlen = sizeof(addr);
729 int s = -1;
730 int rc;
732 s = accept(state->fd, (struct sockaddr *)(void *) &addr, &in_addrlen);
733 if (s == -1) {
734 if (errno != EINTR) {
735 DEBUG(0,("tcpip_listener accept: %s\n",
736 strerror(errno)));
738 return;
741 rc = tsocket_address_bsd_from_sockaddr(state,
742 (struct sockaddr *)(void *) &addr,
743 in_addrlen,
744 &cli_addr);
745 if (rc < 0) {
746 close(s);
747 return;
750 rc = getsockname(s, (struct sockaddr *)(void *) &addr, &in_addrlen);
751 if (rc < 0) {
752 close(s);
753 return;
756 rc = tsocket_address_bsd_from_sockaddr(state,
757 (struct sockaddr *)(void *) &addr,
758 in_addrlen,
759 &srv_addr);
760 if (rc < 0) {
761 close(s);
762 return;
765 DEBUG(6, ("tcpip_listener: Accepted socket %d\n", s));
767 dcerpc_ncacn_accept(state->ev_ctx,
768 state->msg_ctx,
769 NCACN_IP_TCP,
770 NULL,
771 cli_addr,
772 srv_addr,
774 NULL);
777 /********************************************************************
778 * Start listening on the ncalrpc socket
779 ********************************************************************/
781 static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
782 struct tevent_fd *fde,
783 uint16_t flags,
784 void *private_data);
786 bool setup_dcerpc_ncalrpc_socket(struct tevent_context *ev_ctx,
787 struct messaging_context *msg_ctx,
788 const char *name,
789 dcerpc_ncacn_disconnect_fn fn)
791 struct dcerpc_ncacn_listen_state *state;
792 struct tevent_fd *fde;
794 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
795 if (state == NULL) {
796 DEBUG(0, ("Out of memory\n"));
797 return false;
800 state->fd = -1;
801 state->disconnect_fn = fn;
803 if (name == NULL) {
804 name = "DEFAULT";
806 state->ep.name = talloc_strdup(state, name);
808 if (state->ep.name == NULL) {
809 DEBUG(0, ("Out of memory\n"));
810 talloc_free(state);
811 return false;
814 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) {
815 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
816 lp_ncalrpc_dir(), strerror(errno)));
817 goto out;
820 state->fd = create_pipe_sock(lp_ncalrpc_dir(), name, 0755);
821 if (state->fd == -1) {
822 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
823 lp_ncalrpc_dir(), name));
824 goto out;
827 DEBUG(10, ("Openened pipe socket fd %d for %s\n", state->fd, name));
829 state->ev_ctx = ev_ctx;
830 state->msg_ctx = msg_ctx;
832 /* Set server socket to non-blocking for the accept. */
833 set_blocking(state->fd, false);
835 fde = tevent_add_fd(state->ev_ctx,
836 state,
837 state->fd,
838 TEVENT_FD_READ,
839 dcerpc_ncalrpc_listener,
840 state);
841 if (fde == NULL) {
842 DEBUG(0, ("Failed to add event handler for ncalrpc!\n"));
843 goto out;
846 tevent_fd_set_auto_close(fde);
848 return true;
849 out:
850 if (state->fd != -1) {
851 close(state->fd);
853 TALLOC_FREE(state);
855 return 0;
858 static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
859 struct tevent_fd *fde,
860 uint16_t flags,
861 void *private_data)
863 struct dcerpc_ncacn_listen_state *state =
864 talloc_get_type_abort(private_data,
865 struct dcerpc_ncacn_listen_state);
866 struct tsocket_address *cli_addr = NULL;
867 struct sockaddr_un sunaddr;
868 struct sockaddr *addr = (struct sockaddr *)(void *)&sunaddr;
869 socklen_t len = sizeof(sunaddr);
870 int sd = -1;
871 int rc;
873 ZERO_STRUCT(sunaddr);
875 sd = accept(state->fd, addr, &len);
876 if (sd == -1) {
877 if (errno != EINTR) {
878 DEBUG(0, ("ncalrpc accept() failed: %s\n", strerror(errno)));
880 return;
883 rc = tsocket_address_bsd_from_sockaddr(state,
884 addr, len,
885 &cli_addr);
886 if (rc < 0) {
887 close(sd);
888 return;
891 DEBUG(10, ("Accepted ncalrpc socket %d\n", sd));
893 dcerpc_ncacn_accept(state->ev_ctx,
894 state->msg_ctx,
895 NCALRPC,
896 state->ep.name,
897 cli_addr, NULL, sd,
898 state->disconnect_fn);
901 struct dcerpc_ncacn_conn {
902 enum dcerpc_transport_t transport;
904 int sock;
906 struct pipes_struct *p;
907 dcerpc_ncacn_disconnect_fn disconnect_fn;
909 struct tevent_context *ev_ctx;
910 struct messaging_context *msg_ctx;
912 struct tstream_context *tstream;
913 struct tevent_queue *send_queue;
915 struct tsocket_address *client;
916 char *client_name;
917 struct tsocket_address *server;
918 char *server_name;
919 struct auth_session_info *session_info;
921 struct iovec *iov;
922 size_t count;
925 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq);
926 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq);
928 static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
929 struct messaging_context *msg_ctx,
930 enum dcerpc_transport_t transport,
931 const char *name,
932 struct tsocket_address *cli_addr,
933 struct tsocket_address *srv_addr,
934 int s,
935 dcerpc_ncacn_disconnect_fn fn) {
936 struct dcerpc_ncacn_conn *ncacn_conn;
937 struct tevent_req *subreq;
938 const char *cli_str;
939 const char *srv_str = NULL;
940 bool system_user = false;
941 char *pipe_name;
942 NTSTATUS status;
943 int sys_errno;
944 uid_t uid;
945 int rc;
947 DEBUG(10, ("dcerpc_ncacn_accept\n"));
949 ncacn_conn = talloc_zero(ev_ctx, struct dcerpc_ncacn_conn);
950 if (ncacn_conn == NULL) {
951 DEBUG(0, ("Out of memory!\n"));
952 close(s);
953 return;
956 ncacn_conn->transport = transport;
957 ncacn_conn->ev_ctx = ev_ctx;
958 ncacn_conn->msg_ctx = msg_ctx;
959 ncacn_conn->sock = s;
960 ncacn_conn->disconnect_fn = fn;
962 ncacn_conn->client = talloc_move(ncacn_conn, &cli_addr);
963 if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
964 ncacn_conn->client_name =
965 tsocket_address_inet_addr_string(ncacn_conn->client,
966 ncacn_conn);
967 } else {
968 ncacn_conn->client_name =
969 tsocket_address_unix_path(ncacn_conn->client,
970 ncacn_conn);
972 if (ncacn_conn->client_name == NULL) {
973 DEBUG(0, ("Out of memory!\n"));
974 talloc_free(ncacn_conn);
975 close(s);
976 return;
979 if (srv_addr != NULL) {
980 ncacn_conn->server = talloc_move(ncacn_conn, &srv_addr);
982 ncacn_conn->server_name =
983 tsocket_address_inet_addr_string(ncacn_conn->server,
984 ncacn_conn);
985 if (ncacn_conn->server_name == NULL) {
986 DEBUG(0, ("Out of memory!\n"));
987 talloc_free(ncacn_conn);
988 close(s);
989 return;
993 switch (transport) {
994 case NCACN_IP_TCP:
995 pipe_name = tsocket_address_string(ncacn_conn->client,
996 ncacn_conn);
997 if (pipe_name == NULL) {
998 close(s);
999 talloc_free(ncacn_conn);
1000 return;
1003 break;
1004 case NCALRPC:
1005 rc = sys_getpeereid(s, &uid);
1006 if (rc < 0) {
1007 DEBUG(2, ("Failed to get ncalrpc connecting uid!"));
1008 } else {
1009 if (uid == sec_initial_uid()) {
1010 system_user = true;
1013 case NCACN_NP:
1014 pipe_name = talloc_strdup(ncacn_conn,
1015 name);
1016 if (pipe_name == NULL) {
1017 close(s);
1018 talloc_free(ncacn_conn);
1019 return;
1021 break;
1022 default:
1023 DEBUG(0, ("unknown dcerpc transport: %u!\n",
1024 transport));
1025 talloc_free(ncacn_conn);
1026 close(s);
1027 return;
1030 rc = set_blocking(s, false);
1031 if (rc < 0) {
1032 DEBUG(2, ("Failed to set dcerpc socket to non-blocking\n"));
1033 talloc_free(ncacn_conn);
1034 close(s);
1035 return;
1039 * As soon as we have tstream_bsd_existing_socket set up it will
1040 * take care of closing the socket.
1042 rc = tstream_bsd_existing_socket(ncacn_conn, s, &ncacn_conn->tstream);
1043 if (rc < 0) {
1044 DEBUG(2, ("Failed to create tstream socket for dcerpc\n"));
1045 talloc_free(ncacn_conn);
1046 close(s);
1047 return;
1050 if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
1051 cli_str = ncacn_conn->client_name;
1052 } else {
1053 cli_str = "";
1056 if (ncacn_conn->server != NULL) {
1057 if (tsocket_address_is_inet(ncacn_conn->server, "ip")) {
1058 srv_str = ncacn_conn->server_name;
1059 } else {
1060 srv_str = NULL;
1064 if (ncacn_conn->session_info == NULL) {
1065 status = auth_anonymous_session_info(ncacn_conn,
1066 &ncacn_conn->session_info);
1067 if (!NT_STATUS_IS_OK(status)) {
1068 DEBUG(2, ("Failed to create "
1069 "auth_anonymous_session_info - %s\n",
1070 nt_errstr(status)));
1071 talloc_free(ncacn_conn);
1072 return;
1076 rc = make_server_pipes_struct(ncacn_conn,
1077 ncacn_conn->msg_ctx,
1078 pipe_name,
1079 ncacn_conn->transport,
1080 system_user,
1081 ncacn_conn->server,
1082 ncacn_conn->client,
1083 ncacn_conn->session_info,
1084 &ncacn_conn->p,
1085 &sys_errno);
1086 if (rc < 0) {
1087 DEBUG(2, ("Failed to create pipe struct - %s",
1088 strerror(sys_errno)));
1089 talloc_free(ncacn_conn);
1090 return;
1093 ncacn_conn->send_queue = tevent_queue_create(ncacn_conn,
1094 "dcerpc send queue");
1095 if (ncacn_conn->send_queue == NULL) {
1096 DEBUG(0, ("Out of memory!\n"));
1097 talloc_free(ncacn_conn);
1098 return;
1101 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1102 ncacn_conn->ev_ctx,
1103 ncacn_conn->tstream);
1104 if (subreq == NULL) {
1105 DEBUG(2, ("Failed to send ncacn packet\n"));
1106 talloc_free(ncacn_conn);
1107 return;
1110 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1112 DEBUG(10, ("dcerpc_ncacn_accept done\n"));
1114 return;
1117 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq)
1119 struct dcerpc_ncacn_conn *ncacn_conn =
1120 tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
1122 struct _output_data *out = &ncacn_conn->p->out_data;
1123 DATA_BLOB recv_buffer = data_blob_null;
1124 struct ncacn_packet *pkt;
1125 ssize_t data_left;
1126 ssize_t data_used;
1127 uint32_t to_send;
1128 char *data;
1129 NTSTATUS status;
1130 bool ok;
1132 status = dcerpc_read_ncacn_packet_recv(subreq, ncacn_conn, &pkt, &recv_buffer);
1133 TALLOC_FREE(subreq);
1134 if (!NT_STATUS_IS_OK(status)) {
1135 if (ncacn_conn->disconnect_fn != NULL) {
1136 ok = ncacn_conn->disconnect_fn(ncacn_conn->p);
1137 if (!ok) {
1138 DEBUG(3, ("Failed to call disconnect function\n"));
1141 goto fail;
1144 data_left = recv_buffer.length;
1145 data = (char *) recv_buffer.data;
1147 while (data_left) {
1148 data_used = process_incoming_data(ncacn_conn->p, data, data_left);
1149 if (data_used < 0) {
1150 DEBUG(3, ("Failed to process dcerpc request!\n"));
1151 status = NT_STATUS_UNEXPECTED_IO_ERROR;
1152 goto fail;
1155 data_left -= data_used;
1156 data += data_used;
1159 /* Do not leak this buffer */
1160 talloc_free(recv_buffer.data);
1161 talloc_free(pkt);
1164 * This is needed because of the way DCERPC binds work in the RPC
1165 * marshalling code
1167 to_send = out->frag.length - out->current_pdu_sent;
1168 if (to_send > 0) {
1170 DEBUG(10, ("Current_pdu_len = %u, "
1171 "current_pdu_sent = %u "
1172 "Returning %u bytes\n",
1173 (unsigned int)out->frag.length,
1174 (unsigned int)out->current_pdu_sent,
1175 (unsigned int)to_send));
1177 ncacn_conn->iov = talloc_zero(ncacn_conn, struct iovec);
1178 if (ncacn_conn->iov == NULL) {
1179 status = NT_STATUS_NO_MEMORY;
1180 DEBUG(3, ("Out of memory!\n"));
1181 goto fail;
1183 ncacn_conn->count = 1;
1185 ncacn_conn->iov[0].iov_base = out->frag.data
1186 + out->current_pdu_sent;
1187 ncacn_conn->iov[0].iov_len = to_send;
1189 out->current_pdu_sent += to_send;
1193 * This condition is false for bind packets, or when we haven't yet got
1194 * a full request, and need to wait for more data from the client
1196 while (out->data_sent_length < out->rdata.length) {
1197 ok = create_next_pdu(ncacn_conn->p);
1198 if (!ok) {
1199 DEBUG(3, ("Failed to create next PDU!\n"));
1200 status = NT_STATUS_UNEXPECTED_IO_ERROR;
1201 goto fail;
1204 ncacn_conn->iov = talloc_realloc(ncacn_conn,
1205 ncacn_conn->iov,
1206 struct iovec,
1207 ncacn_conn->count + 1);
1208 if (ncacn_conn->iov == NULL) {
1209 DEBUG(3, ("Out of memory!\n"));
1210 status = NT_STATUS_NO_MEMORY;
1211 goto fail;
1214 ncacn_conn->iov[ncacn_conn->count].iov_base = out->frag.data;
1215 ncacn_conn->iov[ncacn_conn->count].iov_len = out->frag.length;
1217 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
1218 (unsigned int) ncacn_conn->count,
1219 (unsigned int) ncacn_conn->iov[ncacn_conn->count].iov_len));
1220 dump_data(11, (const uint8_t *) ncacn_conn->iov[ncacn_conn->count].iov_base,
1221 ncacn_conn->iov[ncacn_conn->count].iov_len);
1222 ncacn_conn->count++;
1226 * We still don't have a complete request, go back and wait for more
1227 * data.
1229 if (ncacn_conn->count == 0) {
1230 /* Wait for the next packet */
1231 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1232 ncacn_conn->ev_ctx,
1233 ncacn_conn->tstream);
1234 if (subreq == NULL) {
1235 DEBUG(2, ("Failed to start receving packets\n"));
1236 status = NT_STATUS_NO_MEMORY;
1237 goto fail;
1239 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1240 return;
1243 DEBUG(10, ("Sending a total of %u bytes\n",
1244 (unsigned int)ncacn_conn->p->out_data.data_sent_length));
1246 subreq = tstream_writev_queue_send(ncacn_conn,
1247 ncacn_conn->ev_ctx,
1248 ncacn_conn->tstream,
1249 ncacn_conn->send_queue,
1250 ncacn_conn->iov,
1251 ncacn_conn->count);
1252 if (subreq == NULL) {
1253 DEBUG(2, ("Failed to send packet\n"));
1254 status = NT_STATUS_NO_MEMORY;
1255 goto fail;
1258 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_done, ncacn_conn);
1259 return;
1261 fail:
1262 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1263 ncacn_conn->client_name, nt_errstr(status)));
1265 /* Terminate client connection */
1266 talloc_free(ncacn_conn);
1267 return;
1270 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq)
1272 struct dcerpc_ncacn_conn *ncacn_conn =
1273 tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
1274 NTSTATUS status = NT_STATUS_OK;
1275 int sys_errno;
1276 int rc;
1278 rc = tstream_writev_queue_recv(subreq, &sys_errno);
1279 TALLOC_FREE(subreq);
1280 if (rc < 0) {
1281 DEBUG(2, ("Writev failed!\n"));
1282 status = map_nt_error_from_unix(sys_errno);
1283 goto fail;
1286 /* clear out any data that may have been left around */
1287 ncacn_conn->count = 0;
1288 TALLOC_FREE(ncacn_conn->iov);
1289 data_blob_free(&ncacn_conn->p->in_data.data);
1290 data_blob_free(&ncacn_conn->p->out_data.frag);
1291 data_blob_free(&ncacn_conn->p->out_data.rdata);
1293 /* Wait for the next packet */
1294 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1295 ncacn_conn->ev_ctx,
1296 ncacn_conn->tstream);
1297 if (subreq == NULL) {
1298 DEBUG(2, ("Failed to start receving packets\n"));
1299 status = NT_STATUS_NO_MEMORY;
1300 goto fail;
1303 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1304 return;
1306 fail:
1307 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1308 ncacn_conn->client_name, nt_errstr(status)));
1310 /* Terminate client connection */
1311 talloc_free(ncacn_conn);
1312 return;
1315 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */