Optimization. Don't do the retry logic if sitename_fetch() returned NULL, we already...
[Samba.git] / source3 / rpc_server / rpc_server.c
blob8ec55bbfb86fe074fbce1eb7377f195c3290294b
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 "ntdomain.h"
22 #include "rpc_server/rpc_server.h"
23 #include "rpc_dce.h"
24 #include "librpc/gen_ndr/netlogon.h"
25 #include "librpc/gen_ndr/auth.h"
26 #include "lib/tsocket/tsocket.h"
27 #include "libcli/named_pipe_auth/npa_tstream.h"
28 #include "../auth/auth_sam_reply.h"
29 #include "auth.h"
30 #include "rpc_server/rpc_ncacn_np.h"
31 #include "rpc_server/srv_pipe_hnd.h"
32 #include "rpc_server/srv_pipe.h"
34 #define SERVER_TCP_LOW_PORT 1024
35 #define SERVER_TCP_HIGH_PORT 1300
37 static NTSTATUS auth_anonymous_session_info(TALLOC_CTX *mem_ctx,
38 struct auth_session_info_transport **session_info)
40 struct auth_session_info_transport *i;
41 struct auth_serversupplied_info *s;
42 struct auth_user_info_dc *u;
43 union netr_Validation val;
44 NTSTATUS status;
46 i = talloc_zero(mem_ctx, struct auth_session_info_transport);
47 if (i == NULL) {
48 return NT_STATUS_NO_MEMORY;
51 status = make_server_info_guest(i, &s);
52 if (!NT_STATUS_IS_OK(status)) {
53 return status;
56 i->security_token = s->security_token;
57 i->session_key = s->user_session_key;
59 val.sam3 = s->info3;
61 status = make_user_info_dc_netlogon_validation(mem_ctx,
62 "",
64 &val,
65 &u);
66 if (!NT_STATUS_IS_OK(status)) {
67 DEBUG(0, ("conversion of info3 into user_info_dc failed!\n"));
68 return status;
70 i->info = talloc_move(i, &u->info);
71 talloc_free(u);
73 *session_info = i;
75 return NT_STATUS_OK;
78 /* Creates a pipes_struct and initializes it with the information
79 * sent from the client */
80 static int make_server_pipes_struct(TALLOC_CTX *mem_ctx,
81 const char *pipe_name,
82 const struct ndr_syntax_id id,
83 enum dcerpc_transport_t transport,
84 bool ncalrpc_as_system,
85 const char *client_address,
86 const char *server_address,
87 struct auth_session_info_transport *session_info,
88 struct pipes_struct **_p,
89 int *perrno)
91 struct netr_SamInfo3 *info3;
92 struct auth_user_info_dc *auth_user_info_dc;
93 struct pipes_struct *p;
94 NTSTATUS status;
95 bool ok;
97 p = talloc_zero(mem_ctx, struct pipes_struct);
98 if (!p) {
99 *perrno = ENOMEM;
100 return -1;
102 p->syntax = id;
103 p->transport = transport;
104 p->ncalrpc_as_system = ncalrpc_as_system;
106 p->mem_ctx = talloc_named(p, 0, "pipe %s %p", pipe_name, p);
107 if (!p->mem_ctx) {
108 TALLOC_FREE(p);
109 *perrno = ENOMEM;
110 return -1;
113 ok = init_pipe_handles(p, &id);
114 if (!ok) {
115 DEBUG(1, ("Failed to init handles\n"));
116 TALLOC_FREE(p);
117 *perrno = EINVAL;
118 return -1;
122 data_blob_free(&p->in_data.data);
123 data_blob_free(&p->in_data.pdu);
125 p->endian = RPC_LITTLE_ENDIAN;
127 /* Fake up an auth_user_info_dc for now, to make an info3, to make the session_info structure */
128 auth_user_info_dc = talloc_zero(p, struct auth_user_info_dc);
129 if (!auth_user_info_dc) {
130 TALLOC_FREE(p);
131 *perrno = ENOMEM;
132 return -1;
135 auth_user_info_dc->num_sids = session_info->security_token->num_sids;
136 auth_user_info_dc->sids = session_info->security_token->sids;
137 auth_user_info_dc->info = session_info->info;
138 auth_user_info_dc->user_session_key = session_info->session_key;
140 /* This creates the input structure that make_server_info_info3 is looking for */
141 status = auth_convert_user_info_dc_saminfo3(p, auth_user_info_dc,
142 &info3);
144 if (!NT_STATUS_IS_OK(status)) {
145 DEBUG(1, ("Failed to convert auth_user_info_dc into netr_SamInfo3\n"));
146 TALLOC_FREE(p);
147 *perrno = EINVAL;
148 return -1;
151 status = make_server_info_info3(p,
152 info3->base.account_name.string,
153 info3->base.domain.string,
154 &p->session_info, info3);
155 if (!NT_STATUS_IS_OK(status)) {
156 DEBUG(1, ("Failed to init server info\n"));
157 TALLOC_FREE(p);
158 *perrno = EINVAL;
159 return -1;
163 * Some internal functions need a local token to determine access to
164 * resoutrces.
166 status = create_local_token(p->session_info);
167 if (!NT_STATUS_IS_OK(status)) {
168 DEBUG(1, ("Failed to init local auth token\n"));
169 TALLOC_FREE(p);
170 *perrno = EINVAL;
171 return -1;
174 /* Now override the session_info->security_token with the exact
175 * security_token we were given from the other side,
176 * regardless of what we just calculated */
177 p->session_info->security_token = talloc_move(p->session_info, &session_info->security_token);
179 /* Also set the session key to the correct value */
180 p->session_info->user_session_key = session_info->session_key;
181 p->session_info->user_session_key.data = talloc_move(p->session_info, &session_info->session_key.data);
183 p->client_id = talloc_zero(p, struct client_address);
184 if (!p->client_id) {
185 TALLOC_FREE(p);
186 *perrno = ENOMEM;
187 return -1;
189 strlcpy(p->client_id->addr,
190 client_address, sizeof(p->client_id->addr));
191 p->client_id->name = talloc_strdup(p->client_id, client_address);
192 if (p->client_id->name == NULL) {
193 TALLOC_FREE(p);
194 *perrno = ENOMEM;
195 return -1;
198 if (server_address != NULL) {
199 p->server_id = talloc_zero(p, struct client_address);
200 if (p->client_id == NULL) {
201 TALLOC_FREE(p);
202 *perrno = ENOMEM;
203 return -1;
206 strlcpy(p->server_id->addr,
207 server_address,
208 sizeof(p->server_id->addr));
210 p->server_id->name = talloc_strdup(p->server_id,
211 server_address);
212 if (p->server_id->name == NULL) {
213 TALLOC_FREE(p);
214 *perrno = ENOMEM;
215 return -1;
219 talloc_set_destructor(p, close_internal_rpc_pipe_hnd);
221 *_p = p;
222 return 0;
225 /* Start listening on the appropriate unix socket and setup all is needed to
226 * dispatch requests to the pipes rpc implementation */
228 struct dcerpc_ncacn_listen_state {
229 struct ndr_syntax_id syntax_id;
231 int fd;
232 union {
233 char *name;
234 uint16_t port;
235 } ep;
237 struct tevent_context *ev_ctx;
238 struct messaging_context *msg_ctx;
239 dcerpc_ncacn_disconnect_fn disconnect_fn;
242 static void named_pipe_listener(struct tevent_context *ev,
243 struct tevent_fd *fde,
244 uint16_t flags,
245 void *private_data);
247 bool setup_named_pipe_socket(const char *pipe_name,
248 struct tevent_context *ev_ctx)
250 struct dcerpc_ncacn_listen_state *state;
251 struct tevent_fd *fde;
252 char *np_dir;
254 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
255 if (!state) {
256 DEBUG(0, ("Out of memory\n"));
257 return false;
259 state->ep.name = talloc_strdup(state, pipe_name);
260 if (state->ep.name == NULL) {
261 DEBUG(0, ("Out of memory\n"));
262 goto out;
264 state->fd = -1;
267 * As lp_ncalrpc_dir() should have 0755, but
268 * lp_ncalrpc_dir()/np should have 0700, we need to
269 * create lp_ncalrpc_dir() first.
271 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) {
272 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
273 lp_ncalrpc_dir(), strerror(errno)));
274 goto out;
277 np_dir = talloc_asprintf(state, "%s/np", lp_ncalrpc_dir());
278 if (!np_dir) {
279 DEBUG(0, ("Out of memory\n"));
280 goto out;
283 if (!directory_create_or_exist(np_dir, geteuid(), 0700)) {
284 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
285 np_dir, strerror(errno)));
286 goto out;
289 state->fd = create_pipe_sock(np_dir, pipe_name, 0700);
290 if (state->fd == -1) {
291 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
292 np_dir, pipe_name));
293 goto out;
296 DEBUG(10, ("Openened pipe socket fd %d for %s\n",
297 state->fd, pipe_name));
299 fde = tevent_add_fd(ev_ctx,
300 state, state->fd, TEVENT_FD_READ,
301 named_pipe_listener, state);
302 if (!fde) {
303 DEBUG(0, ("Failed to add event handler!\n"));
304 goto out;
307 tevent_fd_set_auto_close(fde);
308 return true;
310 out:
311 if (state->fd != -1) {
312 close(state->fd);
314 TALLOC_FREE(state);
315 return false;
318 static void named_pipe_accept_function(const char *pipe_name, int fd);
320 static void named_pipe_listener(struct tevent_context *ev,
321 struct tevent_fd *fde,
322 uint16_t flags,
323 void *private_data)
325 struct dcerpc_ncacn_listen_state *state =
326 talloc_get_type_abort(private_data,
327 struct dcerpc_ncacn_listen_state);
328 struct sockaddr_un sunaddr;
329 socklen_t len;
330 int sd = -1;
332 /* TODO: should we have a limit to the number of clients ? */
334 len = sizeof(sunaddr);
336 sd = accept(state->fd,
337 (struct sockaddr *)(void *)&sunaddr, &len);
339 if (sd == -1) {
340 if (errno != EINTR) {
341 DEBUG(6, ("Failed to get a valid socket [%s]\n",
342 strerror(errno)));
344 return;
347 DEBUG(6, ("Accepted socket %d\n", sd));
349 named_pipe_accept_function(state->ep.name, sd);
353 /* This is the core of the rpc server.
354 * Accepts connections from clients and process requests using the appropriate
355 * dispatcher table. */
357 struct named_pipe_client {
358 const char *pipe_name;
359 struct ndr_syntax_id pipe_id;
361 struct tevent_context *ev;
362 struct messaging_context *msg_ctx;
364 uint16_t file_type;
365 uint16_t device_state;
366 uint64_t allocation_size;
368 struct tstream_context *tstream;
370 struct tsocket_address *client;
371 char *client_name;
372 struct tsocket_address *server;
373 char *server_name;
374 struct auth_session_info_transport *session_info;
376 struct pipes_struct *p;
378 struct tevent_queue *write_queue;
380 struct iovec *iov;
381 size_t count;
384 static void named_pipe_accept_done(struct tevent_req *subreq);
386 static void named_pipe_accept_function(const char *pipe_name, int fd)
388 struct ndr_syntax_id syntax;
389 struct named_pipe_client *npc;
390 struct tstream_context *plain;
391 struct tevent_req *subreq;
392 bool ok;
393 int ret;
395 ok = is_known_pipename(pipe_name, &syntax);
396 if (!ok) {
397 DEBUG(1, ("Unknown pipe [%s]\n", pipe_name));
398 close(fd);
399 return;
402 npc = talloc_zero(NULL, struct named_pipe_client);
403 if (!npc) {
404 DEBUG(0, ("Out of memory!\n"));
405 close(fd);
406 return;
408 npc->pipe_name = pipe_name;
409 npc->pipe_id = syntax;
410 npc->ev = server_event_context();
411 npc->msg_ctx = server_messaging_context();
413 /* make sure socket is in NON blocking state */
414 ret = set_blocking(fd, false);
415 if (ret != 0) {
416 DEBUG(2, ("Failed to make socket non-blocking\n"));
417 TALLOC_FREE(npc);
418 close(fd);
419 return;
422 ret = tstream_bsd_existing_socket(npc, fd, &plain);
423 if (ret != 0) {
424 DEBUG(2, ("Failed to create tstream socket\n"));
425 TALLOC_FREE(npc);
426 close(fd);
427 return;
430 npc->file_type = FILE_TYPE_MESSAGE_MODE_PIPE;
431 npc->device_state = 0xff | 0x0400 | 0x0100;
432 npc->allocation_size = 4096;
434 subreq = tstream_npa_accept_existing_send(npc, npc->ev, plain,
435 npc->file_type,
436 npc->device_state,
437 npc->allocation_size);
438 if (!subreq) {
439 DEBUG(2, ("Failed to start async accept procedure\n"));
440 TALLOC_FREE(npc);
441 close(fd);
442 return;
444 tevent_req_set_callback(subreq, named_pipe_accept_done, npc);
447 static void named_pipe_packet_process(struct tevent_req *subreq);
448 static void named_pipe_packet_done(struct tevent_req *subreq);
450 static void named_pipe_accept_done(struct tevent_req *subreq)
452 struct named_pipe_client *npc =
453 tevent_req_callback_data(subreq, struct named_pipe_client);
454 const char *cli_addr;
455 int error;
456 int ret;
458 ret = tstream_npa_accept_existing_recv(subreq, &error, npc,
459 &npc->tstream,
460 &npc->client,
461 &npc->client_name,
462 &npc->server,
463 &npc->server_name,
464 &npc->session_info);
465 TALLOC_FREE(subreq);
466 if (ret != 0) {
467 DEBUG(2, ("Failed to accept named pipe connection! (%s)\n",
468 strerror(error)));
469 TALLOC_FREE(npc);
470 return;
473 if (tsocket_address_is_inet(npc->client, "ip")) {
474 cli_addr = tsocket_address_inet_addr_string(npc->client,
475 subreq);
476 if (cli_addr == NULL) {
477 TALLOC_FREE(npc);
478 return;
480 } else {
481 cli_addr = "";
484 ret = make_server_pipes_struct(npc,
485 npc->pipe_name, npc->pipe_id, NCACN_NP,
486 false, cli_addr, NULL, npc->session_info,
487 &npc->p, &error);
488 if (ret != 0) {
489 DEBUG(2, ("Failed to create pipes_struct! (%s)\n",
490 strerror(error)));
491 goto fail;
493 npc->p->msg_ctx = npc->msg_ctx;
495 npc->write_queue = tevent_queue_create(npc, "np_server_write_queue");
496 if (!npc->write_queue) {
497 DEBUG(2, ("Failed to set up write queue!\n"));
498 goto fail;
501 /* And now start receaving and processing packets */
502 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
503 if (!subreq) {
504 DEBUG(2, ("Failed to start receving packets\n"));
505 goto fail;
507 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
508 return;
510 fail:
511 DEBUG(2, ("Fatal error. Terminating client(%s) connection!\n",
512 npc->client_name));
513 /* terminate client connection */
514 talloc_free(npc);
515 return;
518 static void named_pipe_packet_process(struct tevent_req *subreq)
520 struct named_pipe_client *npc =
521 tevent_req_callback_data(subreq, struct named_pipe_client);
522 struct _output_data *out = &npc->p->out_data;
523 DATA_BLOB recv_buffer = data_blob_null;
524 struct ncacn_packet *pkt;
525 NTSTATUS status;
526 ssize_t data_left;
527 ssize_t data_used;
528 char *data;
529 uint32_t to_send;
530 bool ok;
532 status = dcerpc_read_ncacn_packet_recv(subreq, npc, &pkt, &recv_buffer);
533 TALLOC_FREE(subreq);
534 if (!NT_STATUS_IS_OK(status)) {
535 goto fail;
538 data_left = recv_buffer.length;
539 data = (char *)recv_buffer.data;
541 while (data_left) {
543 data_used = process_incoming_data(npc->p, data, data_left);
544 if (data_used < 0) {
545 DEBUG(3, ("Failed to process dceprc request!\n"));
546 status = NT_STATUS_UNEXPECTED_IO_ERROR;
547 goto fail;
550 data_left -= data_used;
551 data += data_used;
554 /* Do not leak this buffer, npc is a long lived context */
555 talloc_free(recv_buffer.data);
556 talloc_free(pkt);
558 /* this is needed because of the way DCERPC Binds work in
559 * the RPC marshalling code */
560 to_send = out->frag.length - out->current_pdu_sent;
561 if (to_send > 0) {
563 DEBUG(10, ("Current_pdu_len = %u, "
564 "current_pdu_sent = %u "
565 "Returning %u bytes\n",
566 (unsigned int)out->frag.length,
567 (unsigned int)out->current_pdu_sent,
568 (unsigned int)to_send));
570 npc->iov = talloc_zero(npc, struct iovec);
571 if (!npc->iov) {
572 status = NT_STATUS_NO_MEMORY;
573 goto fail;
575 npc->count = 1;
577 npc->iov[0].iov_base = out->frag.data
578 + out->current_pdu_sent;
579 npc->iov[0].iov_len = to_send;
581 out->current_pdu_sent += to_send;
584 /* this condition is false for bind packets, or when we haven't
585 * yet got a full request, and need to wait for more data from
586 * the client */
587 while (out->data_sent_length < out->rdata.length) {
589 ok = create_next_pdu(npc->p);
590 if (!ok) {
591 DEBUG(3, ("Failed to create next PDU!\n"));
592 status = NT_STATUS_UNEXPECTED_IO_ERROR;
593 goto fail;
596 npc->iov = talloc_realloc(npc, npc->iov,
597 struct iovec, npc->count + 1);
598 if (!npc->iov) {
599 status = NT_STATUS_NO_MEMORY;
600 goto fail;
603 npc->iov[npc->count].iov_base = out->frag.data;
604 npc->iov[npc->count].iov_len = out->frag.length;
606 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
607 (unsigned int)npc->count,
608 (unsigned int)npc->iov[npc->count].iov_len));
609 dump_data(11, (const uint8_t *)npc->iov[npc->count].iov_base,
610 npc->iov[npc->count].iov_len);
611 npc->count++;
614 /* we still don't have a complete request, go back and wait for more
615 * data */
616 if (npc->count == 0) {
617 /* Wait for the next packet */
618 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
619 if (!subreq) {
620 DEBUG(2, ("Failed to start receving packets\n"));
621 status = NT_STATUS_NO_MEMORY;
622 goto fail;
624 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
625 return;
628 DEBUG(10, ("Sending a total of %u bytes\n",
629 (unsigned int)npc->p->out_data.data_sent_length));
631 subreq = tstream_writev_queue_send(npc, npc->ev,
632 npc->tstream,
633 npc->write_queue,
634 npc->iov, npc->count);
635 if (!subreq) {
636 DEBUG(2, ("Failed to send packet\n"));
637 status = NT_STATUS_NO_MEMORY;
638 goto fail;
640 tevent_req_set_callback(subreq, named_pipe_packet_done, npc);
641 return;
643 fail:
644 DEBUG(2, ("Fatal error(%s). "
645 "Terminating client(%s) connection!\n",
646 nt_errstr(status), npc->client_name));
647 /* terminate client connection */
648 talloc_free(npc);
649 return;
652 static void named_pipe_packet_done(struct tevent_req *subreq)
654 struct named_pipe_client *npc =
655 tevent_req_callback_data(subreq, struct named_pipe_client);
656 int sys_errno;
657 int ret;
659 ret = tstream_writev_queue_recv(subreq, &sys_errno);
660 TALLOC_FREE(subreq);
661 if (ret == -1) {
662 DEBUG(2, ("Writev failed!\n"));
663 goto fail;
666 /* clear out any data that may have been left around */
667 npc->count = 0;
668 TALLOC_FREE(npc->iov);
669 data_blob_free(&npc->p->in_data.data);
670 data_blob_free(&npc->p->out_data.frag);
671 data_blob_free(&npc->p->out_data.rdata);
673 /* Wait for the next packet */
674 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
675 if (!subreq) {
676 DEBUG(2, ("Failed to start receving packets\n"));
677 sys_errno = ENOMEM;
678 goto fail;
680 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
681 return;
683 fail:
684 DEBUG(2, ("Fatal error(%s). "
685 "Terminating client(%s) connection!\n",
686 strerror(sys_errno), npc->client_name));
687 /* terminate client connection */
688 talloc_free(npc);
689 return;
692 static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
693 struct messaging_context *msg_ctx,
694 struct ndr_syntax_id syntax_id,
695 enum dcerpc_transport_t transport,
696 const char *name,
697 uint16_t port,
698 struct tsocket_address *cli_addr,
699 struct tsocket_address *srv_addr,
700 int s,
701 dcerpc_ncacn_disconnect_fn fn);
703 /********************************************************************
704 * Start listening on the tcp/ip socket
705 ********************************************************************/
707 static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
708 struct tevent_fd *fde,
709 uint16_t flags,
710 void *private_data);
712 uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context *ev_ctx,
713 struct messaging_context *msg_ctx,
714 struct ndr_syntax_id syntax_id,
715 const struct sockaddr_storage *ifss,
716 uint16_t port)
718 struct dcerpc_ncacn_listen_state *state;
719 struct tevent_fd *fde;
720 int rc;
722 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
723 if (state == NULL) {
724 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Out of memory\n"));
725 return 0;
728 state->syntax_id = syntax_id;
729 state->fd = -1;
730 state->ep.port = port;
731 state->disconnect_fn = NULL;
733 if (state->ep.port == 0) {
734 uint16_t i;
736 for (i = SERVER_TCP_LOW_PORT; i <= SERVER_TCP_HIGH_PORT; i++) {
737 state->fd = open_socket_in(SOCK_STREAM,
740 ifss,
741 false);
742 if (state->fd > 0) {
743 state->ep.port = i;
744 break;
747 } else {
748 state->fd = open_socket_in(SOCK_STREAM,
749 state->ep.port,
751 ifss,
752 true);
754 if (state->fd == -1) {
755 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Failed to create "
756 "socket on port %u!\n", state->ep.port));
757 goto out;
760 state->ev_ctx = ev_ctx;
761 state->msg_ctx = msg_ctx;
763 /* ready to listen */
764 set_socket_options(state->fd, "SO_KEEPALIVE");
765 set_socket_options(state->fd, lp_socket_options());
767 /* Set server socket to non-blocking for the accept. */
768 set_blocking(state->fd, false);
770 rc = listen(state->fd, SMBD_LISTEN_BACKLOG);
771 if (rc == -1) {
772 DEBUG(0,("setup_tcpip_socket: listen - %s\n", strerror(errno)));
773 goto out;
776 DEBUG(10, ("setup_tcpip_socket: openened socket fd %d for port %u\n",
777 state->fd, state->ep.port));
779 fde = tevent_add_fd(state->ev_ctx,
780 state,
781 state->fd,
782 TEVENT_FD_READ,
783 dcerpc_ncacn_tcpip_listener,
784 state);
785 if (fde == NULL) {
786 DEBUG(0, ("setup_tcpip_socket: Failed to add event handler!\n"));
787 goto out;
790 tevent_fd_set_auto_close(fde);
792 return state->ep.port;
793 out:
794 if (state->fd != -1) {
795 close(state->fd);
797 TALLOC_FREE(state);
799 return 0;
802 static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
803 struct tevent_fd *fde,
804 uint16_t flags,
805 void *private_data)
807 struct dcerpc_ncacn_listen_state *state =
808 talloc_get_type_abort(private_data,
809 struct dcerpc_ncacn_listen_state);
810 struct tsocket_address *cli_addr = NULL;
811 struct tsocket_address *srv_addr = NULL;
812 struct sockaddr_storage addr;
813 socklen_t in_addrlen = sizeof(addr);
814 int s = -1;
815 int rc;
817 s = accept(state->fd, (struct sockaddr *)(void *) &addr, &in_addrlen);
818 if (s == -1) {
819 if (errno != EINTR) {
820 DEBUG(0,("tcpip_listener accept: %s\n",
821 strerror(errno)));
823 return;
826 rc = tsocket_address_bsd_from_sockaddr(state,
827 (struct sockaddr *)(void *) &addr,
828 in_addrlen,
829 &cli_addr);
830 if (rc < 0) {
831 close(s);
832 return;
835 rc = getsockname(s, (struct sockaddr *)(void *) &addr, &in_addrlen);
836 if (rc < 0) {
837 close(s);
838 return;
841 rc = tsocket_address_bsd_from_sockaddr(state,
842 (struct sockaddr *)(void *) &addr,
843 in_addrlen,
844 &srv_addr);
845 if (rc < 0) {
846 close(s);
847 return;
850 DEBUG(6, ("tcpip_listener: Accepted socket %d\n", s));
852 dcerpc_ncacn_accept(state->ev_ctx,
853 state->msg_ctx,
854 state->syntax_id,
855 NCACN_IP_TCP,
856 NULL,
857 state->ep.port,
858 cli_addr,
859 srv_addr,
861 NULL);
864 /********************************************************************
865 * Start listening on the ncalrpc socket
866 ********************************************************************/
868 static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
869 struct tevent_fd *fde,
870 uint16_t flags,
871 void *private_data);
873 bool setup_dcerpc_ncalrpc_socket(struct tevent_context *ev_ctx,
874 struct messaging_context *msg_ctx,
875 struct ndr_syntax_id syntax_id,
876 const char *name,
877 dcerpc_ncacn_disconnect_fn fn)
879 struct dcerpc_ncacn_listen_state *state;
880 struct tevent_fd *fde;
882 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
883 if (state == NULL) {
884 DEBUG(0, ("Out of memory\n"));
885 return false;
888 state->syntax_id = syntax_id;
889 state->fd = -1;
890 state->disconnect_fn = fn;
892 if (name == NULL) {
893 name = "DEFAULT";
895 state->ep.name = talloc_strdup(state, name);
897 if (state->ep.name == NULL) {
898 DEBUG(0, ("Out of memory\n"));
899 talloc_free(state);
900 return false;
903 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) {
904 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
905 lp_ncalrpc_dir(), strerror(errno)));
906 goto out;
909 state->fd = create_pipe_sock(lp_ncalrpc_dir(), name, 0755);
910 if (state->fd == -1) {
911 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
912 lp_ncalrpc_dir(), name));
913 goto out;
916 DEBUG(10, ("Openened pipe socket fd %d for %s\n", state->fd, name));
918 state->ev_ctx = ev_ctx;
919 state->msg_ctx = msg_ctx;
921 /* Set server socket to non-blocking for the accept. */
922 set_blocking(state->fd, false);
924 fde = tevent_add_fd(state->ev_ctx,
925 state,
926 state->fd,
927 TEVENT_FD_READ,
928 dcerpc_ncalrpc_listener,
929 state);
930 if (fde == NULL) {
931 DEBUG(0, ("Failed to add event handler for ncalrpc!\n"));
932 goto out;
935 tevent_fd_set_auto_close(fde);
937 return true;
938 out:
939 if (state->fd != -1) {
940 close(state->fd);
942 TALLOC_FREE(state);
944 return 0;
947 static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
948 struct tevent_fd *fde,
949 uint16_t flags,
950 void *private_data)
952 struct dcerpc_ncacn_listen_state *state =
953 talloc_get_type_abort(private_data,
954 struct dcerpc_ncacn_listen_state);
955 struct tsocket_address *cli_addr = NULL;
956 struct sockaddr_un sunaddr;
957 struct sockaddr *addr = (struct sockaddr *)(void *)&sunaddr;
958 socklen_t len = sizeof(sunaddr);
959 int sd = -1;
960 int rc;
962 ZERO_STRUCT(sunaddr);
964 sd = accept(state->fd, addr, &len);
965 if (sd == -1) {
966 if (errno != EINTR) {
967 DEBUG(0, ("ncalrpc accept() failed: %s\n", strerror(errno)));
969 return;
972 rc = tsocket_address_bsd_from_sockaddr(state,
973 addr, len,
974 &cli_addr);
975 if (rc < 0) {
976 close(sd);
977 return;
980 DEBUG(10, ("Accepted ncalrpc socket %d\n", sd));
982 dcerpc_ncacn_accept(state->ev_ctx,
983 state->msg_ctx,
984 state->syntax_id, NCALRPC,
985 state->ep.name, 0,
986 cli_addr, NULL, sd,
987 state->disconnect_fn);
990 struct dcerpc_ncacn_conn {
991 struct ndr_syntax_id syntax_id;
993 enum dcerpc_transport_t transport;
995 union {
996 const char *name;
997 uint16_t port;
998 } ep;
1000 int sock;
1002 struct pipes_struct *p;
1003 dcerpc_ncacn_disconnect_fn disconnect_fn;
1005 struct tevent_context *ev_ctx;
1006 struct messaging_context *msg_ctx;
1008 struct tstream_context *tstream;
1009 struct tevent_queue *send_queue;
1011 struct tsocket_address *client;
1012 char *client_name;
1013 struct tsocket_address *server;
1014 char *server_name;
1015 struct auth_session_info_transport *session_info;
1017 struct iovec *iov;
1018 size_t count;
1021 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq);
1022 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq);
1024 static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
1025 struct messaging_context *msg_ctx,
1026 struct ndr_syntax_id syntax_id,
1027 enum dcerpc_transport_t transport,
1028 const char *name,
1029 uint16_t port,
1030 struct tsocket_address *cli_addr,
1031 struct tsocket_address *srv_addr,
1032 int s,
1033 dcerpc_ncacn_disconnect_fn fn) {
1034 struct dcerpc_ncacn_conn *ncacn_conn;
1035 struct tevent_req *subreq;
1036 const char *cli_str;
1037 const char *srv_str = NULL;
1038 bool system_user = false;
1039 char *pipe_name;
1040 NTSTATUS status;
1041 int sys_errno;
1042 uid_t uid;
1043 int rc;
1045 DEBUG(10, ("dcerpc_ncacn_accept\n"));
1047 ncacn_conn = talloc_zero(ev_ctx, struct dcerpc_ncacn_conn);
1048 if (ncacn_conn == NULL) {
1049 DEBUG(0, ("Out of memory!\n"));
1050 close(s);
1051 return;
1054 ncacn_conn->transport = transport;
1055 ncacn_conn->syntax_id = syntax_id;
1056 ncacn_conn->ev_ctx = ev_ctx;
1057 ncacn_conn->msg_ctx = msg_ctx;
1058 ncacn_conn->sock = s;
1059 ncacn_conn->disconnect_fn = fn;
1061 ncacn_conn->client = talloc_move(ncacn_conn, &cli_addr);
1062 if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
1063 ncacn_conn->client_name =
1064 tsocket_address_inet_addr_string(ncacn_conn->client,
1065 ncacn_conn);
1066 } else {
1067 ncacn_conn->client_name =
1068 tsocket_address_unix_path(ncacn_conn->client,
1069 ncacn_conn);
1071 if (ncacn_conn->client_name == NULL) {
1072 DEBUG(0, ("Out of memory!\n"));
1073 talloc_free(ncacn_conn);
1074 close(s);
1075 return;
1078 if (srv_addr != NULL) {
1079 ncacn_conn->server = talloc_move(ncacn_conn, &srv_addr);
1081 ncacn_conn->server_name =
1082 tsocket_address_inet_addr_string(ncacn_conn->server,
1083 ncacn_conn);
1084 if (ncacn_conn->server_name == NULL) {
1085 DEBUG(0, ("Out of memory!\n"));
1086 talloc_free(ncacn_conn);
1087 close(s);
1088 return;
1092 switch (transport) {
1093 case NCACN_IP_TCP:
1094 ncacn_conn->ep.port = port;
1096 pipe_name = tsocket_address_string(ncacn_conn->client,
1097 ncacn_conn);
1098 if (pipe_name == NULL) {
1099 close(s);
1100 talloc_free(ncacn_conn);
1101 return;
1104 break;
1105 case NCALRPC:
1106 rc = sys_getpeereid(s, &uid);
1107 if (rc < 0) {
1108 DEBUG(2, ("Failed to get ncalrpc connecting uid!"));
1109 } else {
1110 if (uid == sec_initial_uid()) {
1111 system_user = true;
1114 case NCACN_NP:
1115 ncacn_conn->ep.name = talloc_strdup(ncacn_conn, name);
1116 if (ncacn_conn->ep.name == NULL) {
1117 close(s);
1118 talloc_free(ncacn_conn);
1119 return;
1122 pipe_name = talloc_strdup(ncacn_conn,
1123 name);
1124 if (pipe_name == NULL) {
1125 close(s);
1126 talloc_free(ncacn_conn);
1127 return;
1129 break;
1130 default:
1131 DEBUG(0, ("unknown dcerpc transport: %u!\n",
1132 transport));
1133 talloc_free(ncacn_conn);
1134 close(s);
1135 return;
1138 rc = set_blocking(s, false);
1139 if (rc < 0) {
1140 DEBUG(2, ("Failed to set dcerpc socket to non-blocking\n"));
1141 talloc_free(ncacn_conn);
1142 close(s);
1143 return;
1147 * As soon as we have tstream_bsd_existing_socket set up it will
1148 * take care of closing the socket.
1150 rc = tstream_bsd_existing_socket(ncacn_conn, s, &ncacn_conn->tstream);
1151 if (rc < 0) {
1152 DEBUG(2, ("Failed to create tstream socket for dcerpc\n"));
1153 talloc_free(ncacn_conn);
1154 close(s);
1155 return;
1158 if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
1159 cli_str = ncacn_conn->client_name;
1160 } else {
1161 cli_str = "";
1164 if (ncacn_conn->server != NULL) {
1165 if (tsocket_address_is_inet(ncacn_conn->server, "ip")) {
1166 srv_str = ncacn_conn->server_name;
1167 } else {
1168 srv_str = NULL;
1172 if (ncacn_conn->session_info == NULL) {
1173 status = auth_anonymous_session_info(ncacn_conn,
1174 &ncacn_conn->session_info);
1175 if (!NT_STATUS_IS_OK(status)) {
1176 DEBUG(2, ("Failed to create "
1177 "auth_anonymous_session_info - %s\n",
1178 nt_errstr(status)));
1179 talloc_free(ncacn_conn);
1180 return;
1184 rc = make_server_pipes_struct(ncacn_conn,
1185 pipe_name,
1186 ncacn_conn->syntax_id,
1187 ncacn_conn->transport,
1188 system_user,
1189 cli_str,
1190 srv_str,
1191 ncacn_conn->session_info,
1192 &ncacn_conn->p,
1193 &sys_errno);
1194 if (rc < 0) {
1195 DEBUG(2, ("Failed to create pipe struct - %s",
1196 strerror(sys_errno)));
1197 talloc_free(ncacn_conn);
1198 return;
1201 ncacn_conn->send_queue = tevent_queue_create(ncacn_conn,
1202 "dcerpc send queue");
1203 if (ncacn_conn->send_queue == NULL) {
1204 DEBUG(0, ("Out of memory!\n"));
1205 talloc_free(ncacn_conn);
1206 return;
1209 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1210 ncacn_conn->ev_ctx,
1211 ncacn_conn->tstream);
1212 if (subreq == NULL) {
1213 DEBUG(2, ("Failed to send ncacn packet\n"));
1214 talloc_free(ncacn_conn);
1215 return;
1218 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1220 DEBUG(10, ("dcerpc_ncacn_accept done\n"));
1222 return;
1225 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq)
1227 struct dcerpc_ncacn_conn *ncacn_conn =
1228 tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
1230 struct _output_data *out = &ncacn_conn->p->out_data;
1231 DATA_BLOB recv_buffer = data_blob_null;
1232 struct ncacn_packet *pkt;
1233 ssize_t data_left;
1234 ssize_t data_used;
1235 uint32_t to_send;
1236 char *data;
1237 NTSTATUS status;
1238 bool ok;
1240 status = dcerpc_read_ncacn_packet_recv(subreq, ncacn_conn, &pkt, &recv_buffer);
1241 TALLOC_FREE(subreq);
1242 if (!NT_STATUS_IS_OK(status)) {
1243 if (ncacn_conn->disconnect_fn != NULL) {
1244 ok = ncacn_conn->disconnect_fn(ncacn_conn->p);
1245 if (!ok) {
1246 DEBUG(3, ("Failed to call disconnect function\n"));
1249 goto fail;
1252 data_left = recv_buffer.length;
1253 data = (char *) recv_buffer.data;
1255 while (data_left) {
1256 data_used = process_incoming_data(ncacn_conn->p, data, data_left);
1257 if (data_used < 0) {
1258 DEBUG(3, ("Failed to process dcerpc request!\n"));
1259 status = NT_STATUS_UNEXPECTED_IO_ERROR;
1260 goto fail;
1263 data_left -= data_used;
1264 data += data_used;
1267 /* Do not leak this buffer */
1268 talloc_free(recv_buffer.data);
1269 talloc_free(pkt);
1272 * This is needed because of the way DCERPC binds work in the RPC
1273 * marshalling code
1275 to_send = out->frag.length - out->current_pdu_sent;
1276 if (to_send > 0) {
1278 DEBUG(10, ("Current_pdu_len = %u, "
1279 "current_pdu_sent = %u "
1280 "Returning %u bytes\n",
1281 (unsigned int)out->frag.length,
1282 (unsigned int)out->current_pdu_sent,
1283 (unsigned int)to_send));
1285 ncacn_conn->iov = talloc_zero(ncacn_conn, struct iovec);
1286 if (ncacn_conn->iov == NULL) {
1287 status = NT_STATUS_NO_MEMORY;
1288 DEBUG(3, ("Out of memory!\n"));
1289 goto fail;
1291 ncacn_conn->count = 1;
1293 ncacn_conn->iov[0].iov_base = out->frag.data
1294 + out->current_pdu_sent;
1295 ncacn_conn->iov[0].iov_len = to_send;
1297 out->current_pdu_sent += to_send;
1301 * This condition is false for bind packets, or when we haven't yet got
1302 * a full request, and need to wait for more data from the client
1304 while (out->data_sent_length < out->rdata.length) {
1305 ok = create_next_pdu(ncacn_conn->p);
1306 if (!ok) {
1307 DEBUG(3, ("Failed to create next PDU!\n"));
1308 status = NT_STATUS_UNEXPECTED_IO_ERROR;
1309 goto fail;
1312 ncacn_conn->iov = talloc_realloc(ncacn_conn,
1313 ncacn_conn->iov,
1314 struct iovec,
1315 ncacn_conn->count + 1);
1316 if (ncacn_conn->iov == NULL) {
1317 DEBUG(3, ("Out of memory!\n"));
1318 status = NT_STATUS_NO_MEMORY;
1319 goto fail;
1322 ncacn_conn->iov[ncacn_conn->count].iov_base = out->frag.data;
1323 ncacn_conn->iov[ncacn_conn->count].iov_len = out->frag.length;
1325 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
1326 (unsigned int) ncacn_conn->count,
1327 (unsigned int) ncacn_conn->iov[ncacn_conn->count].iov_len));
1328 dump_data(11, (const uint8_t *) ncacn_conn->iov[ncacn_conn->count].iov_base,
1329 ncacn_conn->iov[ncacn_conn->count].iov_len);
1330 ncacn_conn->count++;
1334 * We still don't have a complete request, go back and wait for more
1335 * data.
1337 if (ncacn_conn->count == 0) {
1338 /* Wait for the next packet */
1339 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1340 ncacn_conn->ev_ctx,
1341 ncacn_conn->tstream);
1342 if (subreq == NULL) {
1343 DEBUG(2, ("Failed to start receving packets\n"));
1344 status = NT_STATUS_NO_MEMORY;
1345 goto fail;
1347 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1348 return;
1351 DEBUG(10, ("Sending a total of %u bytes\n",
1352 (unsigned int)ncacn_conn->p->out_data.data_sent_length));
1354 subreq = tstream_writev_queue_send(ncacn_conn,
1355 ncacn_conn->ev_ctx,
1356 ncacn_conn->tstream,
1357 ncacn_conn->send_queue,
1358 ncacn_conn->iov,
1359 ncacn_conn->count);
1360 if (subreq == NULL) {
1361 DEBUG(2, ("Failed to send packet\n"));
1362 status = NT_STATUS_NO_MEMORY;
1363 goto fail;
1366 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_done, ncacn_conn);
1367 return;
1369 fail:
1370 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1371 ncacn_conn->client_name, nt_errstr(status)));
1373 /* Terminate client connection */
1374 talloc_free(ncacn_conn);
1375 return;
1378 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq)
1380 struct dcerpc_ncacn_conn *ncacn_conn =
1381 tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
1382 NTSTATUS status = NT_STATUS_OK;
1383 int sys_errno;
1384 int rc;
1386 rc = tstream_writev_queue_recv(subreq, &sys_errno);
1387 TALLOC_FREE(subreq);
1388 if (rc < 0) {
1389 DEBUG(2, ("Writev failed!\n"));
1390 status = map_nt_error_from_unix(sys_errno);
1391 goto fail;
1394 /* clear out any data that may have been left around */
1395 ncacn_conn->count = 0;
1396 TALLOC_FREE(ncacn_conn->iov);
1397 data_blob_free(&ncacn_conn->p->in_data.data);
1398 data_blob_free(&ncacn_conn->p->out_data.frag);
1399 data_blob_free(&ncacn_conn->p->out_data.rdata);
1401 /* Wait for the next packet */
1402 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1403 ncacn_conn->ev_ctx,
1404 ncacn_conn->tstream);
1405 if (subreq == NULL) {
1406 DEBUG(2, ("Failed to start receving packets\n"));
1407 status = NT_STATUS_NO_MEMORY;
1408 goto fail;
1411 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1412 return;
1414 fail:
1415 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1416 ncacn_conn->client_name, nt_errstr(status)));
1418 /* Terminate client connection */
1419 talloc_free(ncacn_conn);
1420 return;
1423 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */