s3: Fix Coverity ID 986, BUFFER_SIZE_WARNING
[Samba.git] / source3 / rpc_server / rpc_server.c
blobe3b6e6aa34e3b1cb558913b25679db07fad29a3f
1 /*
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/>.
20 #include "includes.h"
21 #include "rpc_server/rpc_server.h"
22 #include "rpc_dce.h"
23 #include "librpc/gen_ndr/netlogon.h"
24 #include "librpc/gen_ndr/auth.h"
25 #include "lib/tsocket/tsocket.h"
26 #include "libcli/named_pipe_auth/npa_tstream.h"
27 #include "../auth/auth_sam_reply.h"
28 #include "auth.h"
29 #include "ntdomain.h"
31 #define SERVER_TCP_LOW_PORT 1024
32 #define SERVER_TCP_HIGH_PORT 1300
34 static NTSTATUS auth_anonymous_session_info(TALLOC_CTX *mem_ctx,
35 struct auth_session_info_transport **session_info)
37 struct auth_session_info_transport *i;
38 struct auth_serversupplied_info *s;
39 struct auth_user_info_dc *u;
40 union netr_Validation val;
41 NTSTATUS status;
43 i = talloc_zero(mem_ctx, struct auth_session_info_transport);
44 if (i == NULL) {
45 return NT_STATUS_NO_MEMORY;
48 status = make_server_info_guest(i, &s);
49 if (!NT_STATUS_IS_OK(status)) {
50 return status;
53 i->security_token = s->security_token;
54 i->session_key = s->user_session_key;
56 val.sam3 = s->info3;
58 status = make_user_info_dc_netlogon_validation(mem_ctx,
59 "",
61 &val,
62 &u);
63 if (!NT_STATUS_IS_OK(status)) {
64 DEBUG(0, ("conversion of info3 into user_info_dc failed!\n"));
65 return status;
67 i->info = talloc_move(i, &u->info);
68 talloc_free(u);
70 *session_info = i;
72 return NT_STATUS_OK;
75 /* Creates a pipes_struct and initializes it with the information
76 * sent from the client */
77 static int make_server_pipes_struct(TALLOC_CTX *mem_ctx,
78 const char *pipe_name,
79 const struct ndr_syntax_id id,
80 enum dcerpc_transport_t transport,
81 bool ncalrpc_as_system,
82 const char *client_address,
83 const char *server_address,
84 struct auth_session_info_transport *session_info,
85 struct pipes_struct **_p,
86 int *perrno)
88 struct netr_SamInfo3 *info3;
89 struct auth_user_info_dc *auth_user_info_dc;
90 struct pipes_struct *p;
91 NTSTATUS status;
92 bool ok;
94 p = talloc_zero(mem_ctx, struct pipes_struct);
95 if (!p) {
96 *perrno = ENOMEM;
97 return -1;
99 p->syntax = id;
100 p->transport = transport;
101 p->ncalrpc_as_system = ncalrpc_as_system;
103 p->mem_ctx = talloc_named(p, 0, "pipe %s %p", pipe_name, p);
104 if (!p->mem_ctx) {
105 TALLOC_FREE(p);
106 *perrno = ENOMEM;
107 return -1;
110 ok = init_pipe_handles(p, &id);
111 if (!ok) {
112 DEBUG(1, ("Failed to init handles\n"));
113 TALLOC_FREE(p);
114 *perrno = EINVAL;
115 return -1;
119 data_blob_free(&p->in_data.data);
120 data_blob_free(&p->in_data.pdu);
122 p->endian = RPC_LITTLE_ENDIAN;
124 /* Fake up an auth_user_info_dc for now, to make an info3, to make the session_info structure */
125 auth_user_info_dc = talloc_zero(p, struct auth_user_info_dc);
126 if (!auth_user_info_dc) {
127 TALLOC_FREE(p);
128 *perrno = ENOMEM;
129 return -1;
132 auth_user_info_dc->num_sids = session_info->security_token->num_sids;
133 auth_user_info_dc->sids = session_info->security_token->sids;
134 auth_user_info_dc->info = session_info->info;
135 auth_user_info_dc->user_session_key = session_info->session_key;
137 /* This creates the input structure that make_server_info_info3 is looking for */
138 status = auth_convert_user_info_dc_saminfo3(p, auth_user_info_dc,
139 &info3);
141 if (!NT_STATUS_IS_OK(status)) {
142 DEBUG(1, ("Failed to convert auth_user_info_dc into netr_SamInfo3\n"));
143 TALLOC_FREE(p);
144 *perrno = EINVAL;
145 return -1;
148 status = make_server_info_info3(p,
149 info3->base.account_name.string,
150 info3->base.domain.string,
151 &p->session_info, info3);
152 if (!NT_STATUS_IS_OK(status)) {
153 DEBUG(1, ("Failed to init server info\n"));
154 TALLOC_FREE(p);
155 *perrno = EINVAL;
156 return -1;
160 * Some internal functions need a local token to determine access to
161 * resoutrces.
163 status = create_local_token(p->session_info);
164 if (!NT_STATUS_IS_OK(status)) {
165 DEBUG(1, ("Failed to init local auth token\n"));
166 TALLOC_FREE(p);
167 *perrno = EINVAL;
168 return -1;
171 /* Now override the session_info->security_token with the exact
172 * security_token we were given from the other side,
173 * regardless of what we just calculated */
174 p->session_info->security_token = talloc_move(p->session_info, &session_info->security_token);
176 /* Also set the session key to the correct value */
177 p->session_info->user_session_key = session_info->session_key;
178 p->session_info->user_session_key.data = talloc_move(p->session_info, &session_info->session_key.data);
180 p->client_id = talloc_zero(p, struct client_address);
181 if (!p->client_id) {
182 TALLOC_FREE(p);
183 *perrno = ENOMEM;
184 return -1;
186 strlcpy(p->client_id->addr,
187 client_address, sizeof(p->client_id->addr));
188 p->client_id->name = talloc_strdup(p->client_id, client_address);
189 if (p->client_id->name == NULL) {
190 TALLOC_FREE(p);
191 *perrno = ENOMEM;
192 return -1;
195 if (server_address != NULL) {
196 p->server_id = talloc_zero(p, struct client_address);
197 if (p->client_id == NULL) {
198 TALLOC_FREE(p);
199 *perrno = ENOMEM;
200 return -1;
203 strlcpy(p->server_id->addr,
204 server_address,
205 sizeof(p->server_id->addr));
207 p->server_id->name = talloc_strdup(p->server_id,
208 server_address);
209 if (p->server_id->name == NULL) {
210 TALLOC_FREE(p);
211 *perrno = ENOMEM;
212 return -1;
216 talloc_set_destructor(p, close_internal_rpc_pipe_hnd);
218 *_p = p;
219 return 0;
222 /* Start listening on the appropriate unix socket and setup all is needed to
223 * dispatch requests to the pipes rpc implementation */
225 struct dcerpc_ncacn_listen_state {
226 struct ndr_syntax_id syntax_id;
228 int fd;
229 union {
230 char *name;
231 uint16_t port;
232 } ep;
234 struct tevent_context *ev_ctx;
235 struct messaging_context *msg_ctx;
236 dcerpc_ncacn_disconnect_fn disconnect_fn;
239 static void named_pipe_listener(struct tevent_context *ev,
240 struct tevent_fd *fde,
241 uint16_t flags,
242 void *private_data);
244 bool setup_named_pipe_socket(const char *pipe_name,
245 struct tevent_context *ev_ctx)
247 struct dcerpc_ncacn_listen_state *state;
248 struct tevent_fd *fde;
249 char *np_dir;
251 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
252 if (!state) {
253 DEBUG(0, ("Out of memory\n"));
254 return false;
256 state->ep.name = talloc_strdup(state, pipe_name);
257 if (state->ep.name == NULL) {
258 DEBUG(0, ("Out of memory\n"));
259 goto out;
261 state->fd = -1;
263 np_dir = talloc_asprintf(state, "%s/np", lp_ncalrpc_dir());
264 if (!np_dir) {
265 DEBUG(0, ("Out of memory\n"));
266 goto out;
269 if (!directory_create_or_exist(np_dir, geteuid(), 0700)) {
270 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
271 np_dir, strerror(errno)));
272 goto out;
275 state->fd = create_pipe_sock(np_dir, pipe_name, 0700);
276 if (state->fd == -1) {
277 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
278 np_dir, pipe_name));
279 goto out;
282 DEBUG(10, ("Openened pipe socket fd %d for %s\n",
283 state->fd, pipe_name));
285 fde = tevent_add_fd(ev_ctx,
286 state, state->fd, TEVENT_FD_READ,
287 named_pipe_listener, state);
288 if (!fde) {
289 DEBUG(0, ("Failed to add event handler!\n"));
290 goto out;
293 tevent_fd_set_auto_close(fde);
294 return true;
296 out:
297 if (state->fd != -1) {
298 close(state->fd);
300 TALLOC_FREE(state);
301 return false;
304 static void named_pipe_accept_function(const char *pipe_name, int fd);
306 static void named_pipe_listener(struct tevent_context *ev,
307 struct tevent_fd *fde,
308 uint16_t flags,
309 void *private_data)
311 struct dcerpc_ncacn_listen_state *state =
312 talloc_get_type_abort(private_data,
313 struct dcerpc_ncacn_listen_state);
314 struct sockaddr_un sunaddr;
315 socklen_t len;
316 int sd = -1;
318 /* TODO: should we have a limit to the number of clients ? */
320 len = sizeof(sunaddr);
322 sd = accept(state->fd,
323 (struct sockaddr *)(void *)&sunaddr, &len);
325 if (sd == -1) {
326 if (errno != EINTR) {
327 DEBUG(6, ("Failed to get a valid socket [%s]\n",
328 strerror(errno)));
330 return;
333 DEBUG(6, ("Accepted socket %d\n", sd));
335 named_pipe_accept_function(state->ep.name, sd);
339 /* This is the core of the rpc server.
340 * Accepts connections from clients and process requests using the appropriate
341 * dispatcher table. */
343 struct named_pipe_client {
344 const char *pipe_name;
345 struct ndr_syntax_id pipe_id;
347 struct tevent_context *ev;
348 struct messaging_context *msg_ctx;
350 uint16_t file_type;
351 uint16_t device_state;
352 uint64_t allocation_size;
354 struct tstream_context *tstream;
356 struct tsocket_address *client;
357 char *client_name;
358 struct tsocket_address *server;
359 char *server_name;
360 struct auth_session_info_transport *session_info;
362 struct pipes_struct *p;
364 struct tevent_queue *write_queue;
366 struct iovec *iov;
367 size_t count;
370 static void named_pipe_accept_done(struct tevent_req *subreq);
372 static void named_pipe_accept_function(const char *pipe_name, int fd)
374 struct ndr_syntax_id syntax;
375 struct named_pipe_client *npc;
376 struct tstream_context *plain;
377 struct tevent_req *subreq;
378 bool ok;
379 int ret;
381 ok = is_known_pipename(pipe_name, &syntax);
382 if (!ok) {
383 DEBUG(1, ("Unknown pipe [%s]\n", pipe_name));
384 close(fd);
385 return;
388 npc = talloc_zero(NULL, struct named_pipe_client);
389 if (!npc) {
390 DEBUG(0, ("Out of memory!\n"));
391 close(fd);
392 return;
394 npc->pipe_name = pipe_name;
395 npc->pipe_id = syntax;
396 npc->ev = server_event_context();
397 npc->msg_ctx = server_messaging_context();
399 /* make sure socket is in NON blocking state */
400 ret = set_blocking(fd, false);
401 if (ret != 0) {
402 DEBUG(2, ("Failed to make socket non-blocking\n"));
403 TALLOC_FREE(npc);
404 close(fd);
405 return;
408 ret = tstream_bsd_existing_socket(npc, fd, &plain);
409 if (ret != 0) {
410 DEBUG(2, ("Failed to create tstream socket\n"));
411 TALLOC_FREE(npc);
412 close(fd);
413 return;
416 npc->file_type = FILE_TYPE_MESSAGE_MODE_PIPE;
417 npc->device_state = 0xff | 0x0400 | 0x0100;
418 npc->allocation_size = 4096;
420 subreq = tstream_npa_accept_existing_send(npc, npc->ev, plain,
421 npc->file_type,
422 npc->device_state,
423 npc->allocation_size);
424 if (!subreq) {
425 DEBUG(2, ("Failed to start async accept procedure\n"));
426 TALLOC_FREE(npc);
427 close(fd);
428 return;
430 tevent_req_set_callback(subreq, named_pipe_accept_done, npc);
433 static void named_pipe_packet_process(struct tevent_req *subreq);
434 static void named_pipe_packet_done(struct tevent_req *subreq);
436 static void named_pipe_accept_done(struct tevent_req *subreq)
438 struct named_pipe_client *npc =
439 tevent_req_callback_data(subreq, struct named_pipe_client);
440 const char *cli_addr;
441 int error;
442 int ret;
444 ret = tstream_npa_accept_existing_recv(subreq, &error, npc,
445 &npc->tstream,
446 &npc->client,
447 &npc->client_name,
448 &npc->server,
449 &npc->server_name,
450 &npc->session_info);
451 TALLOC_FREE(subreq);
452 if (ret != 0) {
453 DEBUG(2, ("Failed to accept named pipe connection! (%s)\n",
454 strerror(error)));
455 TALLOC_FREE(npc);
456 return;
459 if (tsocket_address_is_inet(npc->client, "ip")) {
460 cli_addr = tsocket_address_inet_addr_string(npc->client,
461 subreq);
462 if (cli_addr == NULL) {
463 TALLOC_FREE(npc);
464 return;
466 } else {
467 cli_addr = "";
470 ret = make_server_pipes_struct(npc,
471 npc->pipe_name, npc->pipe_id, NCACN_NP,
472 false, cli_addr, NULL, npc->session_info,
473 &npc->p, &error);
474 if (ret != 0) {
475 DEBUG(2, ("Failed to create pipes_struct! (%s)\n",
476 strerror(error)));
477 goto fail;
479 npc->p->msg_ctx = npc->msg_ctx;
481 npc->write_queue = tevent_queue_create(npc, "np_server_write_queue");
482 if (!npc->write_queue) {
483 DEBUG(2, ("Failed to set up write queue!\n"));
484 goto fail;
487 /* And now start receaving and processing packets */
488 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
489 if (!subreq) {
490 DEBUG(2, ("Failed to start receving packets\n"));
491 goto fail;
493 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
494 return;
496 fail:
497 DEBUG(2, ("Fatal error. Terminating client(%s) connection!\n",
498 npc->client_name));
499 /* terminate client connection */
500 talloc_free(npc);
501 return;
504 static void named_pipe_packet_process(struct tevent_req *subreq)
506 struct named_pipe_client *npc =
507 tevent_req_callback_data(subreq, struct named_pipe_client);
508 struct _output_data *out = &npc->p->out_data;
509 DATA_BLOB recv_buffer = data_blob_null;
510 struct ncacn_packet *pkt;
511 NTSTATUS status;
512 ssize_t data_left;
513 ssize_t data_used;
514 char *data;
515 uint32_t to_send;
516 bool ok;
518 status = dcerpc_read_ncacn_packet_recv(subreq, npc, &pkt, &recv_buffer);
519 TALLOC_FREE(subreq);
520 if (!NT_STATUS_IS_OK(status)) {
521 goto fail;
524 data_left = recv_buffer.length;
525 data = (char *)recv_buffer.data;
527 while (data_left) {
529 data_used = process_incoming_data(npc->p, data, data_left);
530 if (data_used < 0) {
531 DEBUG(3, ("Failed to process dceprc request!\n"));
532 status = NT_STATUS_UNEXPECTED_IO_ERROR;
533 goto fail;
536 data_left -= data_used;
537 data += data_used;
540 /* Do not leak this buffer, npc is a long lived context */
541 talloc_free(recv_buffer.data);
542 talloc_free(pkt);
544 /* this is needed because of the way DCERPC Binds work in
545 * the RPC marshalling code */
546 to_send = out->frag.length - out->current_pdu_sent;
547 if (to_send > 0) {
549 DEBUG(10, ("Current_pdu_len = %u, "
550 "current_pdu_sent = %u "
551 "Returning %u bytes\n",
552 (unsigned int)out->frag.length,
553 (unsigned int)out->current_pdu_sent,
554 (unsigned int)to_send));
556 npc->iov = talloc_zero(npc, struct iovec);
557 if (!npc->iov) {
558 status = NT_STATUS_NO_MEMORY;
559 goto fail;
561 npc->count = 1;
563 npc->iov[0].iov_base = out->frag.data
564 + out->current_pdu_sent;
565 npc->iov[0].iov_len = to_send;
567 out->current_pdu_sent += to_send;
570 /* this condition is false for bind packets, or when we haven't
571 * yet got a full request, and need to wait for more data from
572 * the client */
573 while (out->data_sent_length < out->rdata.length) {
575 ok = create_next_pdu(npc->p);
576 if (!ok) {
577 DEBUG(3, ("Failed to create next PDU!\n"));
578 status = NT_STATUS_UNEXPECTED_IO_ERROR;
579 goto fail;
582 npc->iov = talloc_realloc(npc, npc->iov,
583 struct iovec, npc->count + 1);
584 if (!npc->iov) {
585 status = NT_STATUS_NO_MEMORY;
586 goto fail;
589 npc->iov[npc->count].iov_base = out->frag.data;
590 npc->iov[npc->count].iov_len = out->frag.length;
592 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
593 (unsigned int)npc->count,
594 (unsigned int)npc->iov[npc->count].iov_len));
595 dump_data(11, (const uint8_t *)npc->iov[npc->count].iov_base,
596 npc->iov[npc->count].iov_len);
597 npc->count++;
600 /* we still don't have a complete request, go back and wait for more
601 * data */
602 if (npc->count == 0) {
603 /* Wait for the next packet */
604 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
605 if (!subreq) {
606 DEBUG(2, ("Failed to start receving packets\n"));
607 status = NT_STATUS_NO_MEMORY;
608 goto fail;
610 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
611 return;
614 DEBUG(10, ("Sending a total of %u bytes\n",
615 (unsigned int)npc->p->out_data.data_sent_length));
617 subreq = tstream_writev_queue_send(npc, npc->ev,
618 npc->tstream,
619 npc->write_queue,
620 npc->iov, npc->count);
621 if (!subreq) {
622 DEBUG(2, ("Failed to send packet\n"));
623 status = NT_STATUS_NO_MEMORY;
624 goto fail;
626 tevent_req_set_callback(subreq, named_pipe_packet_done, npc);
627 return;
629 fail:
630 DEBUG(2, ("Fatal error(%s). "
631 "Terminating client(%s) connection!\n",
632 nt_errstr(status), npc->client_name));
633 /* terminate client connection */
634 talloc_free(npc);
635 return;
638 static void named_pipe_packet_done(struct tevent_req *subreq)
640 struct named_pipe_client *npc =
641 tevent_req_callback_data(subreq, struct named_pipe_client);
642 int sys_errno;
643 int ret;
645 ret = tstream_writev_queue_recv(subreq, &sys_errno);
646 TALLOC_FREE(subreq);
647 if (ret == -1) {
648 DEBUG(2, ("Writev failed!\n"));
649 goto fail;
652 /* clear out any data that may have been left around */
653 npc->count = 0;
654 TALLOC_FREE(npc->iov);
655 data_blob_free(&npc->p->in_data.data);
656 data_blob_free(&npc->p->out_data.frag);
657 data_blob_free(&npc->p->out_data.rdata);
659 /* Wait for the next packet */
660 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
661 if (!subreq) {
662 DEBUG(2, ("Failed to start receving packets\n"));
663 sys_errno = ENOMEM;
664 goto fail;
666 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
667 return;
669 fail:
670 DEBUG(2, ("Fatal error(%s). "
671 "Terminating client(%s) connection!\n",
672 strerror(sys_errno), npc->client_name));
673 /* terminate client connection */
674 talloc_free(npc);
675 return;
678 static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
679 struct messaging_context *msg_ctx,
680 struct ndr_syntax_id syntax_id,
681 enum dcerpc_transport_t transport,
682 const char *name,
683 uint16_t port,
684 struct tsocket_address *cli_addr,
685 struct tsocket_address *srv_addr,
686 int s,
687 dcerpc_ncacn_disconnect_fn fn);
689 /********************************************************************
690 * Start listening on the tcp/ip socket
691 ********************************************************************/
693 static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
694 struct tevent_fd *fde,
695 uint16_t flags,
696 void *private_data);
698 uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context *ev_ctx,
699 struct messaging_context *msg_ctx,
700 struct ndr_syntax_id syntax_id,
701 const struct sockaddr_storage *ifss,
702 uint16_t port)
704 struct dcerpc_ncacn_listen_state *state;
705 struct tevent_fd *fde;
706 int rc;
708 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
709 if (state == NULL) {
710 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Out of memory\n"));
711 return 0;
714 state->syntax_id = syntax_id;
715 state->fd = -1;
716 state->ep.port = port;
717 state->disconnect_fn = NULL;
719 if (state->ep.port == 0) {
720 uint16_t i;
722 for (i = SERVER_TCP_LOW_PORT; i <= SERVER_TCP_HIGH_PORT; i++) {
723 state->fd = open_socket_in(SOCK_STREAM,
726 ifss,
727 false);
728 if (state->fd > 0) {
729 state->ep.port = i;
730 break;
733 } else {
734 state->fd = open_socket_in(SOCK_STREAM,
735 state->ep.port,
737 ifss,
738 true);
740 if (state->fd == -1) {
741 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Failed to create "
742 "socket on port %u!\n", state->ep.port));
743 goto out;
746 state->ev_ctx = ev_ctx;
747 state->msg_ctx = msg_ctx;
749 /* ready to listen */
750 set_socket_options(state->fd, "SO_KEEPALIVE");
751 set_socket_options(state->fd, lp_socket_options());
753 /* Set server socket to non-blocking for the accept. */
754 set_blocking(state->fd, false);
756 rc = listen(state->fd, SMBD_LISTEN_BACKLOG);
757 if (rc == -1) {
758 DEBUG(0,("setup_tcpip_socket: listen - %s\n", strerror(errno)));
759 goto out;
762 DEBUG(10, ("setup_tcpip_socket: openened socket fd %d for port %u\n",
763 state->fd, state->ep.port));
765 fde = tevent_add_fd(state->ev_ctx,
766 state,
767 state->fd,
768 TEVENT_FD_READ,
769 dcerpc_ncacn_tcpip_listener,
770 state);
771 if (fde == NULL) {
772 DEBUG(0, ("setup_tcpip_socket: Failed to add event handler!\n"));
773 goto out;
776 tevent_fd_set_auto_close(fde);
778 return state->ep.port;
779 out:
780 if (state->fd != -1) {
781 close(state->fd);
783 TALLOC_FREE(state);
785 return 0;
788 static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
789 struct tevent_fd *fde,
790 uint16_t flags,
791 void *private_data)
793 struct dcerpc_ncacn_listen_state *state =
794 talloc_get_type_abort(private_data,
795 struct dcerpc_ncacn_listen_state);
796 struct tsocket_address *cli_addr = NULL;
797 struct tsocket_address *srv_addr = NULL;
798 struct sockaddr_storage addr;
799 socklen_t in_addrlen = sizeof(addr);
800 int s = -1;
801 int rc;
803 s = accept(state->fd, (struct sockaddr *)(void *) &addr, &in_addrlen);
804 if (s == -1) {
805 if (errno != EINTR) {
806 DEBUG(0,("tcpip_listener accept: %s\n",
807 strerror(errno)));
809 return;
812 rc = tsocket_address_bsd_from_sockaddr(state,
813 (struct sockaddr *)(void *) &addr,
814 in_addrlen,
815 &cli_addr);
816 if (rc < 0) {
817 close(s);
818 return;
821 rc = getsockname(s, (struct sockaddr *)(void *) &addr, &in_addrlen);
822 if (rc < 0) {
823 close(s);
824 return;
827 rc = tsocket_address_bsd_from_sockaddr(state,
828 (struct sockaddr *)(void *) &addr,
829 in_addrlen,
830 &srv_addr);
831 if (rc < 0) {
832 close(s);
833 return;
836 DEBUG(6, ("tcpip_listener: Accepted socket %d\n", s));
838 dcerpc_ncacn_accept(state->ev_ctx,
839 state->msg_ctx,
840 state->syntax_id,
841 NCACN_IP_TCP,
842 NULL,
843 state->ep.port,
844 cli_addr,
845 srv_addr,
847 NULL);
850 /********************************************************************
851 * Start listening on the ncalrpc socket
852 ********************************************************************/
854 static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
855 struct tevent_fd *fde,
856 uint16_t flags,
857 void *private_data);
859 bool setup_dcerpc_ncalrpc_socket(struct tevent_context *ev_ctx,
860 struct messaging_context *msg_ctx,
861 struct ndr_syntax_id syntax_id,
862 const char *name,
863 dcerpc_ncacn_disconnect_fn fn)
865 struct dcerpc_ncacn_listen_state *state;
866 struct tevent_fd *fde;
868 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
869 if (state == NULL) {
870 DEBUG(0, ("Out of memory\n"));
871 return false;
874 state->syntax_id = syntax_id;
875 state->fd = -1;
876 state->disconnect_fn = fn;
878 if (name == NULL) {
879 name = "DEFAULT";
881 state->ep.name = talloc_strdup(state, name);
883 if (state->ep.name == NULL) {
884 DEBUG(0, ("Out of memory\n"));
885 talloc_free(state);
886 return false;
889 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0700)) {
890 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
891 lp_ncalrpc_dir(), strerror(errno)));
892 goto out;
895 state->fd = create_pipe_sock(lp_ncalrpc_dir(), name, 0700);
896 if (state->fd == -1) {
897 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
898 lp_ncalrpc_dir(), name));
899 goto out;
902 DEBUG(10, ("Openened pipe socket fd %d for %s\n", state->fd, name));
904 state->ev_ctx = ev_ctx;
905 state->msg_ctx = msg_ctx;
907 /* Set server socket to non-blocking for the accept. */
908 set_blocking(state->fd, false);
910 fde = tevent_add_fd(state->ev_ctx,
911 state,
912 state->fd,
913 TEVENT_FD_READ,
914 dcerpc_ncalrpc_listener,
915 state);
916 if (fde == NULL) {
917 DEBUG(0, ("Failed to add event handler for ncalrpc!\n"));
918 goto out;
921 tevent_fd_set_auto_close(fde);
923 return true;
924 out:
925 if (state->fd != -1) {
926 close(state->fd);
928 TALLOC_FREE(state);
930 return 0;
933 static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
934 struct tevent_fd *fde,
935 uint16_t flags,
936 void *private_data)
938 struct dcerpc_ncacn_listen_state *state =
939 talloc_get_type_abort(private_data,
940 struct dcerpc_ncacn_listen_state);
941 struct tsocket_address *cli_addr = NULL;
942 struct sockaddr_un sunaddr;
943 struct sockaddr *addr = (struct sockaddr *)(void *)&sunaddr;
944 socklen_t len = sizeof(sunaddr);
945 int sd = -1;
946 int rc;
948 ZERO_STRUCT(sunaddr);
950 sd = accept(state->fd, addr, &len);
951 if (sd == -1) {
952 if (errno != EINTR) {
953 DEBUG(0, ("ncalrpc accept() failed: %s\n", strerror(errno)));
955 return;
958 rc = tsocket_address_bsd_from_sockaddr(state,
959 addr, len,
960 &cli_addr);
961 if (rc < 0) {
962 close(sd);
963 return;
966 DEBUG(10, ("Accepted ncalrpc socket %d\n", sd));
968 dcerpc_ncacn_accept(state->ev_ctx,
969 state->msg_ctx,
970 state->syntax_id, NCALRPC,
971 state->ep.name, 0,
972 cli_addr, NULL, sd,
973 state->disconnect_fn);
976 struct dcerpc_ncacn_conn {
977 struct ndr_syntax_id syntax_id;
979 enum dcerpc_transport_t transport;
981 union {
982 const char *name;
983 uint16_t port;
984 } ep;
986 int sock;
988 struct pipes_struct *p;
989 dcerpc_ncacn_disconnect_fn disconnect_fn;
991 struct tevent_context *ev_ctx;
992 struct messaging_context *msg_ctx;
994 struct tstream_context *tstream;
995 struct tevent_queue *send_queue;
997 struct tsocket_address *client;
998 char *client_name;
999 struct tsocket_address *server;
1000 char *server_name;
1001 struct auth_session_info_transport *session_info;
1003 struct iovec *iov;
1004 size_t count;
1007 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq);
1008 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq);
1010 static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
1011 struct messaging_context *msg_ctx,
1012 struct ndr_syntax_id syntax_id,
1013 enum dcerpc_transport_t transport,
1014 const char *name,
1015 uint16_t port,
1016 struct tsocket_address *cli_addr,
1017 struct tsocket_address *srv_addr,
1018 int s,
1019 dcerpc_ncacn_disconnect_fn fn) {
1020 struct dcerpc_ncacn_conn *ncacn_conn;
1021 struct tevent_req *subreq;
1022 const char *cli_str;
1023 const char *srv_str = NULL;
1024 bool system_user = false;
1025 char *pipe_name;
1026 NTSTATUS status;
1027 int sys_errno;
1028 uid_t uid;
1029 int rc;
1031 DEBUG(10, ("dcerpc_ncacn_accept\n"));
1033 ncacn_conn = talloc_zero(ev_ctx, struct dcerpc_ncacn_conn);
1034 if (ncacn_conn == NULL) {
1035 DEBUG(0, ("Out of memory!\n"));
1036 close(s);
1037 return;
1040 ncacn_conn->transport = transport;
1041 ncacn_conn->syntax_id = syntax_id;
1042 ncacn_conn->ev_ctx = ev_ctx;
1043 ncacn_conn->msg_ctx = msg_ctx;
1044 ncacn_conn->sock = s;
1045 ncacn_conn->disconnect_fn = fn;
1047 ncacn_conn->client = talloc_move(ncacn_conn, &cli_addr);
1048 if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
1049 ncacn_conn->client_name =
1050 tsocket_address_inet_addr_string(ncacn_conn->client,
1051 ncacn_conn);
1052 } else {
1053 ncacn_conn->client_name =
1054 tsocket_address_unix_path(ncacn_conn->client,
1055 ncacn_conn);
1057 if (ncacn_conn->client_name == NULL) {
1058 DEBUG(0, ("Out of memory!\n"));
1059 talloc_free(ncacn_conn);
1060 close(s);
1061 return;
1064 if (srv_addr != NULL) {
1065 ncacn_conn->server = talloc_move(ncacn_conn, &srv_addr);
1067 ncacn_conn->server_name =
1068 tsocket_address_inet_addr_string(ncacn_conn->server,
1069 ncacn_conn);
1070 if (ncacn_conn->server_name == NULL) {
1071 DEBUG(0, ("Out of memory!\n"));
1072 talloc_free(ncacn_conn);
1073 close(s);
1074 return;
1078 switch (transport) {
1079 case NCACN_IP_TCP:
1080 ncacn_conn->ep.port = port;
1082 pipe_name = tsocket_address_string(ncacn_conn->client,
1083 ncacn_conn);
1084 if (pipe_name == NULL) {
1085 close(s);
1086 talloc_free(ncacn_conn);
1087 return;
1090 break;
1091 case NCALRPC:
1092 rc = sys_getpeereid(s, &uid);
1093 if (rc < 0) {
1094 DEBUG(2, ("Failed to get ncalrpc connecting uid!"));
1095 } else {
1096 if (uid == sec_initial_uid()) {
1097 system_user = true;
1100 case NCACN_NP:
1101 ncacn_conn->ep.name = talloc_strdup(ncacn_conn, name);
1102 if (ncacn_conn->ep.name == NULL) {
1103 close(s);
1104 talloc_free(ncacn_conn);
1105 return;
1108 pipe_name = talloc_strdup(ncacn_conn,
1109 name);
1110 if (pipe_name == NULL) {
1111 close(s);
1112 talloc_free(ncacn_conn);
1113 return;
1115 break;
1116 default:
1117 DEBUG(0, ("unknown dcerpc transport: %u!\n",
1118 transport));
1119 talloc_free(ncacn_conn);
1120 close(s);
1121 return;
1124 rc = set_blocking(s, false);
1125 if (rc < 0) {
1126 DEBUG(2, ("Failed to set dcerpc socket to non-blocking\n"));
1127 talloc_free(ncacn_conn);
1128 close(s);
1129 return;
1133 * As soon as we have tstream_bsd_existing_socket set up it will
1134 * take care of closing the socket.
1136 rc = tstream_bsd_existing_socket(ncacn_conn, s, &ncacn_conn->tstream);
1137 if (rc < 0) {
1138 DEBUG(2, ("Failed to create tstream socket for dcerpc\n"));
1139 talloc_free(ncacn_conn);
1140 close(s);
1141 return;
1144 if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
1145 cli_str = ncacn_conn->client_name;
1146 } else {
1147 cli_str = "";
1150 if (ncacn_conn->server != NULL) {
1151 if (tsocket_address_is_inet(ncacn_conn->server, "ip")) {
1152 srv_str = ncacn_conn->server_name;
1153 } else {
1154 srv_str = NULL;
1158 if (ncacn_conn->session_info == NULL) {
1159 status = auth_anonymous_session_info(ncacn_conn,
1160 &ncacn_conn->session_info);
1161 if (!NT_STATUS_IS_OK(status)) {
1162 DEBUG(2, ("Failed to create "
1163 "auth_anonymous_session_info - %s\n",
1164 nt_errstr(status)));
1165 talloc_free(ncacn_conn);
1166 return;
1170 rc = make_server_pipes_struct(ncacn_conn,
1171 pipe_name,
1172 ncacn_conn->syntax_id,
1173 ncacn_conn->transport,
1174 system_user,
1175 cli_str,
1176 srv_str,
1177 ncacn_conn->session_info,
1178 &ncacn_conn->p,
1179 &sys_errno);
1180 if (rc < 0) {
1181 DEBUG(2, ("Failed to create pipe struct - %s",
1182 strerror(sys_errno)));
1183 talloc_free(ncacn_conn);
1184 return;
1187 ncacn_conn->send_queue = tevent_queue_create(ncacn_conn,
1188 "dcerpc send queue");
1189 if (ncacn_conn->send_queue == NULL) {
1190 DEBUG(0, ("Out of memory!\n"));
1191 talloc_free(ncacn_conn);
1192 return;
1195 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1196 ncacn_conn->ev_ctx,
1197 ncacn_conn->tstream);
1198 if (subreq == NULL) {
1199 DEBUG(2, ("Failed to send ncacn packet\n"));
1200 talloc_free(ncacn_conn);
1201 return;
1204 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1206 DEBUG(10, ("dcerpc_ncacn_accept done\n"));
1208 return;
1211 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq)
1213 struct dcerpc_ncacn_conn *ncacn_conn =
1214 tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
1216 struct _output_data *out = &ncacn_conn->p->out_data;
1217 DATA_BLOB recv_buffer = data_blob_null;
1218 struct ncacn_packet *pkt;
1219 ssize_t data_left;
1220 ssize_t data_used;
1221 uint32_t to_send;
1222 char *data;
1223 NTSTATUS status;
1224 bool ok;
1226 status = dcerpc_read_ncacn_packet_recv(subreq, ncacn_conn, &pkt, &recv_buffer);
1227 TALLOC_FREE(subreq);
1228 if (!NT_STATUS_IS_OK(status)) {
1229 if (ncacn_conn->disconnect_fn != NULL) {
1230 ok = ncacn_conn->disconnect_fn(ncacn_conn->p);
1231 if (!ok) {
1232 DEBUG(3, ("Failed to call disconnect function\n"));
1235 goto fail;
1238 data_left = recv_buffer.length;
1239 data = (char *) recv_buffer.data;
1241 while (data_left) {
1242 data_used = process_incoming_data(ncacn_conn->p, data, data_left);
1243 if (data_used < 0) {
1244 DEBUG(3, ("Failed to process dcerpc request!\n"));
1245 status = NT_STATUS_UNEXPECTED_IO_ERROR;
1246 goto fail;
1249 data_left -= data_used;
1250 data += data_used;
1253 /* Do not leak this buffer */
1254 talloc_free(recv_buffer.data);
1255 talloc_free(pkt);
1258 * This is needed because of the way DCERPC binds work in the RPC
1259 * marshalling code
1261 to_send = out->frag.length - out->current_pdu_sent;
1262 if (to_send > 0) {
1264 DEBUG(10, ("Current_pdu_len = %u, "
1265 "current_pdu_sent = %u "
1266 "Returning %u bytes\n",
1267 (unsigned int)out->frag.length,
1268 (unsigned int)out->current_pdu_sent,
1269 (unsigned int)to_send));
1271 ncacn_conn->iov = talloc_zero(ncacn_conn, struct iovec);
1272 if (ncacn_conn->iov == NULL) {
1273 status = NT_STATUS_NO_MEMORY;
1274 DEBUG(3, ("Out of memory!\n"));
1275 goto fail;
1277 ncacn_conn->count = 1;
1279 ncacn_conn->iov[0].iov_base = out->frag.data
1280 + out->current_pdu_sent;
1281 ncacn_conn->iov[0].iov_len = to_send;
1283 out->current_pdu_sent += to_send;
1287 * This condition is false for bind packets, or when we haven't yet got
1288 * a full request, and need to wait for more data from the client
1290 while (out->data_sent_length < out->rdata.length) {
1291 ok = create_next_pdu(ncacn_conn->p);
1292 if (!ok) {
1293 DEBUG(3, ("Failed to create next PDU!\n"));
1294 status = NT_STATUS_UNEXPECTED_IO_ERROR;
1295 goto fail;
1298 ncacn_conn->iov = talloc_realloc(ncacn_conn,
1299 ncacn_conn->iov,
1300 struct iovec,
1301 ncacn_conn->count + 1);
1302 if (ncacn_conn->iov == NULL) {
1303 DEBUG(3, ("Out of memory!\n"));
1304 status = NT_STATUS_NO_MEMORY;
1305 goto fail;
1308 ncacn_conn->iov[ncacn_conn->count].iov_base = out->frag.data;
1309 ncacn_conn->iov[ncacn_conn->count].iov_len = out->frag.length;
1311 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
1312 (unsigned int) ncacn_conn->count,
1313 (unsigned int) ncacn_conn->iov[ncacn_conn->count].iov_len));
1314 dump_data(11, (const uint8_t *) ncacn_conn->iov[ncacn_conn->count].iov_base,
1315 ncacn_conn->iov[ncacn_conn->count].iov_len);
1316 ncacn_conn->count++;
1320 * We still don't have a complete request, go back and wait for more
1321 * data.
1323 if (ncacn_conn->count == 0) {
1324 /* Wait for the next packet */
1325 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1326 ncacn_conn->ev_ctx,
1327 ncacn_conn->tstream);
1328 if (subreq == NULL) {
1329 DEBUG(2, ("Failed to start receving packets\n"));
1330 status = NT_STATUS_NO_MEMORY;
1331 goto fail;
1333 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1334 return;
1337 DEBUG(10, ("Sending a total of %u bytes\n",
1338 (unsigned int)ncacn_conn->p->out_data.data_sent_length));
1340 subreq = tstream_writev_queue_send(ncacn_conn,
1341 ncacn_conn->ev_ctx,
1342 ncacn_conn->tstream,
1343 ncacn_conn->send_queue,
1344 ncacn_conn->iov,
1345 ncacn_conn->count);
1346 if (subreq == NULL) {
1347 DEBUG(2, ("Failed to send packet\n"));
1348 status = NT_STATUS_NO_MEMORY;
1349 goto fail;
1352 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_done, ncacn_conn);
1353 return;
1355 fail:
1356 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1357 ncacn_conn->client_name, nt_errstr(status)));
1359 /* Terminate client connection */
1360 talloc_free(ncacn_conn);
1361 return;
1364 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq)
1366 struct dcerpc_ncacn_conn *ncacn_conn =
1367 tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
1368 NTSTATUS status = NT_STATUS_OK;
1369 int sys_errno;
1370 int rc;
1372 rc = tstream_writev_queue_recv(subreq, &sys_errno);
1373 TALLOC_FREE(subreq);
1374 if (rc < 0) {
1375 DEBUG(2, ("Writev failed!\n"));
1376 status = map_nt_error_from_unix(sys_errno);
1377 goto fail;
1380 /* clear out any data that may have been left around */
1381 ncacn_conn->count = 0;
1382 TALLOC_FREE(ncacn_conn->iov);
1383 data_blob_free(&ncacn_conn->p->in_data.data);
1384 data_blob_free(&ncacn_conn->p->out_data.frag);
1385 data_blob_free(&ncacn_conn->p->out_data.rdata);
1387 /* Wait for the next packet */
1388 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1389 ncacn_conn->ev_ctx,
1390 ncacn_conn->tstream);
1391 if (subreq == NULL) {
1392 DEBUG(2, ("Failed to start receving packets\n"));
1393 status = NT_STATUS_NO_MEMORY;
1394 goto fail;
1397 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1398 return;
1400 fail:
1401 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1402 ncacn_conn->client_name, nt_errstr(status)));
1404 /* Terminate client connection */
1405 talloc_free(ncacn_conn);
1406 return;
1409 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */