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/>.
22 #include "rpc_server/rpc_server.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"
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 **session_info)
40 struct auth_session_info *i;
41 struct auth_serversupplied_info *s;
42 struct auth_user_info_dc *u;
43 union netr_Validation val;
46 i = talloc_zero(mem_ctx, struct auth_session_info);
48 return NT_STATUS_NO_MEMORY;
51 status = make_server_info_guest(i, &s);
52 if (!NT_STATUS_IS_OK(status)) {
56 i->security_token = s->security_token;
57 i->session_key = s->session_key;
61 status = make_user_info_dc_netlogon_validation(mem_ctx,
66 if (!NT_STATUS_IS_OK(status)) {
67 DEBUG(0, ("conversion of info3 into user_info_dc failed!\n"));
70 i->info = talloc_move(i, &u->info);
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 enum dcerpc_transport_t transport,
83 bool ncalrpc_as_system,
84 const char *client_address,
85 const char *server_address,
86 struct auth_session_info *session_info,
87 struct pipes_struct **_p,
90 struct netr_SamInfo3 *info3;
91 struct auth_user_info_dc *auth_user_info_dc;
92 struct pipes_struct *p;
93 struct auth_serversupplied_info *server_info;
96 p = talloc_zero(mem_ctx, struct pipes_struct);
102 p->syntax = null_ndr_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);
113 data_blob_free(&p->in_data.data);
114 data_blob_free(&p->in_data.pdu);
116 p->endian = RPC_LITTLE_ENDIAN;
118 /* Fake up an auth_user_info_dc for now, to make an info3, to make the session_info structure */
119 auth_user_info_dc = talloc_zero(p, struct auth_user_info_dc);
120 if (!auth_user_info_dc) {
126 auth_user_info_dc->num_sids = session_info->security_token->num_sids;
127 auth_user_info_dc->sids = session_info->security_token->sids;
128 auth_user_info_dc->info = session_info->info;
129 auth_user_info_dc->user_session_key = session_info->session_key;
131 /* This creates the input structure that make_server_info_info3 is looking for */
132 status = auth_convert_user_info_dc_saminfo3(p, auth_user_info_dc,
135 if (!NT_STATUS_IS_OK(status)) {
136 DEBUG(1, ("Failed to convert auth_user_info_dc into netr_SamInfo3\n"));
142 status = make_server_info_info3(p,
143 info3->base.account_name.string,
144 info3->base.domain.string,
145 &server_info, info3);
146 if (!NT_STATUS_IS_OK(status)) {
147 DEBUG(1, ("Failed to init server info\n"));
154 * Some internal functions need a local token to determine access to
157 status = create_local_token(p, server_info, &session_info->session_key, &p->session_info);
158 talloc_free(server_info);
159 if (!NT_STATUS_IS_OK(status)) {
160 DEBUG(1, ("Failed to init local auth token\n"));
166 /* Now override the session_info->security_token with the exact
167 * security_token we were given from the other side,
168 * regardless of what we just calculated */
169 p->session_info->security_token = talloc_move(p->session_info, &session_info->security_token);
171 p->client_id = talloc_zero(p, struct client_address);
177 strlcpy(p->client_id->addr,
178 client_address, sizeof(p->client_id->addr));
179 p->client_id->name = talloc_strdup(p->client_id, client_address);
180 if (p->client_id->name == NULL) {
186 if (server_address != NULL) {
187 p->server_id = talloc_zero(p, struct client_address);
188 if (p->client_id == NULL) {
194 strlcpy(p->server_id->addr,
196 sizeof(p->server_id->addr));
198 p->server_id->name = talloc_strdup(p->server_id,
200 if (p->server_id->name == NULL) {
207 talloc_set_destructor(p, close_internal_rpc_pipe_hnd);
213 /* Start listening on the appropriate unix socket and setup all is needed to
214 * dispatch requests to the pipes rpc implementation */
216 struct dcerpc_ncacn_listen_state {
217 struct ndr_syntax_id syntax_id;
225 struct tevent_context *ev_ctx;
226 struct messaging_context *msg_ctx;
227 dcerpc_ncacn_disconnect_fn disconnect_fn;
230 static void named_pipe_listener(struct tevent_context *ev,
231 struct tevent_fd *fde,
235 bool setup_named_pipe_socket(const char *pipe_name,
236 struct tevent_context *ev_ctx)
238 struct dcerpc_ncacn_listen_state *state;
239 struct tevent_fd *fde;
242 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
244 DEBUG(0, ("Out of memory\n"));
247 state->ep.name = talloc_strdup(state, pipe_name);
248 if (state->ep.name == NULL) {
249 DEBUG(0, ("Out of memory\n"));
255 * As lp_ncalrpc_dir() should have 0755, but
256 * lp_ncalrpc_dir()/np should have 0700, we need to
257 * create lp_ncalrpc_dir() first.
259 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) {
260 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
261 lp_ncalrpc_dir(), strerror(errno)));
265 np_dir = talloc_asprintf(state, "%s/np", lp_ncalrpc_dir());
267 DEBUG(0, ("Out of memory\n"));
271 if (!directory_create_or_exist(np_dir, geteuid(), 0700)) {
272 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
273 np_dir, strerror(errno)));
277 state->fd = create_pipe_sock(np_dir, pipe_name, 0700);
278 if (state->fd == -1) {
279 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
284 DEBUG(10, ("Openened pipe socket fd %d for %s\n",
285 state->fd, pipe_name));
287 fde = tevent_add_fd(ev_ctx,
288 state, state->fd, TEVENT_FD_READ,
289 named_pipe_listener, state);
291 DEBUG(0, ("Failed to add event handler!\n"));
295 tevent_fd_set_auto_close(fde);
299 if (state->fd != -1) {
306 static void named_pipe_accept_function(const char *pipe_name, int fd);
308 static void named_pipe_listener(struct tevent_context *ev,
309 struct tevent_fd *fde,
313 struct dcerpc_ncacn_listen_state *state =
314 talloc_get_type_abort(private_data,
315 struct dcerpc_ncacn_listen_state);
316 struct sockaddr_un sunaddr;
320 /* TODO: should we have a limit to the number of clients ? */
322 len = sizeof(sunaddr);
324 sd = accept(state->fd,
325 (struct sockaddr *)(void *)&sunaddr, &len);
328 if (errno != EINTR) {
329 DEBUG(6, ("Failed to get a valid socket [%s]\n",
335 DEBUG(6, ("Accepted socket %d\n", sd));
337 named_pipe_accept_function(state->ep.name, sd);
341 /* This is the core of the rpc server.
342 * Accepts connections from clients and process requests using the appropriate
343 * dispatcher table. */
345 struct named_pipe_client {
346 const char *pipe_name;
348 struct tevent_context *ev;
349 struct messaging_context *msg_ctx;
352 uint16_t device_state;
353 uint64_t allocation_size;
355 struct tstream_context *tstream;
357 struct tsocket_address *client;
359 struct tsocket_address *server;
361 struct auth_session_info *session_info;
363 struct pipes_struct *p;
365 struct tevent_queue *write_queue;
371 static void named_pipe_accept_done(struct tevent_req *subreq);
373 static void named_pipe_accept_function(const char *pipe_name, int fd)
375 struct named_pipe_client *npc;
376 struct tstream_context *plain;
377 struct tevent_req *subreq;
380 npc = talloc_zero(NULL, struct named_pipe_client);
382 DEBUG(0, ("Out of memory!\n"));
386 npc->pipe_name = pipe_name;
387 npc->ev = server_event_context();
388 npc->msg_ctx = server_messaging_context();
390 /* make sure socket is in NON blocking state */
391 ret = set_blocking(fd, false);
393 DEBUG(2, ("Failed to make socket non-blocking\n"));
399 ret = tstream_bsd_existing_socket(npc, fd, &plain);
401 DEBUG(2, ("Failed to create tstream socket\n"));
407 npc->file_type = FILE_TYPE_MESSAGE_MODE_PIPE;
408 npc->device_state = 0xff | 0x0400 | 0x0100;
409 npc->allocation_size = 4096;
411 subreq = tstream_npa_accept_existing_send(npc, npc->ev, plain,
414 npc->allocation_size);
416 DEBUG(2, ("Failed to start async accept procedure\n"));
421 tevent_req_set_callback(subreq, named_pipe_accept_done, npc);
424 static void named_pipe_packet_process(struct tevent_req *subreq);
425 static void named_pipe_packet_done(struct tevent_req *subreq);
427 static void named_pipe_accept_done(struct tevent_req *subreq)
429 struct auth_session_info_transport *session_info_transport;
430 struct named_pipe_client *npc =
431 tevent_req_callback_data(subreq, struct named_pipe_client);
432 const char *cli_addr;
436 ret = tstream_npa_accept_existing_recv(subreq, &error, npc,
442 &session_info_transport);
444 npc->session_info = talloc_move(npc, &session_info_transport->session_info);
448 DEBUG(2, ("Failed to accept named pipe connection! (%s)\n",
454 if (tsocket_address_is_inet(npc->client, "ip")) {
455 cli_addr = tsocket_address_inet_addr_string(npc->client,
457 if (cli_addr == NULL) {
465 ret = make_server_pipes_struct(npc,
466 npc->pipe_name, NCACN_NP,
467 false, cli_addr, NULL, npc->session_info,
470 DEBUG(2, ("Failed to create pipes_struct! (%s)\n",
474 npc->p->msg_ctx = npc->msg_ctx;
476 npc->write_queue = tevent_queue_create(npc, "np_server_write_queue");
477 if (!npc->write_queue) {
478 DEBUG(2, ("Failed to set up write queue!\n"));
482 /* And now start receiving and processing packets */
483 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
485 DEBUG(2, ("Failed to start receving packets\n"));
488 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
492 DEBUG(2, ("Fatal error. Terminating client(%s) connection!\n",
494 /* terminate client connection */
499 static void named_pipe_packet_process(struct tevent_req *subreq)
501 struct named_pipe_client *npc =
502 tevent_req_callback_data(subreq, struct named_pipe_client);
503 struct _output_data *out = &npc->p->out_data;
504 DATA_BLOB recv_buffer = data_blob_null;
505 struct ncacn_packet *pkt;
513 status = dcerpc_read_ncacn_packet_recv(subreq, npc, &pkt, &recv_buffer);
515 if (!NT_STATUS_IS_OK(status)) {
519 data_left = recv_buffer.length;
520 data = (char *)recv_buffer.data;
524 data_used = process_incoming_data(npc->p, data, data_left);
526 DEBUG(3, ("Failed to process dceprc request!\n"));
527 status = NT_STATUS_UNEXPECTED_IO_ERROR;
531 data_left -= data_used;
535 /* Do not leak this buffer, npc is a long lived context */
536 talloc_free(recv_buffer.data);
539 /* this is needed because of the way DCERPC Binds work in
540 * the RPC marshalling code */
541 to_send = out->frag.length - out->current_pdu_sent;
544 DEBUG(10, ("Current_pdu_len = %u, "
545 "current_pdu_sent = %u "
546 "Returning %u bytes\n",
547 (unsigned int)out->frag.length,
548 (unsigned int)out->current_pdu_sent,
549 (unsigned int)to_send));
551 npc->iov = talloc_zero(npc, struct iovec);
553 status = NT_STATUS_NO_MEMORY;
558 npc->iov[0].iov_base = out->frag.data
559 + out->current_pdu_sent;
560 npc->iov[0].iov_len = to_send;
562 out->current_pdu_sent += to_send;
565 /* this condition is false for bind packets, or when we haven't
566 * yet got a full request, and need to wait for more data from
568 while (out->data_sent_length < out->rdata.length) {
570 ok = create_next_pdu(npc->p);
572 DEBUG(3, ("Failed to create next PDU!\n"));
573 status = NT_STATUS_UNEXPECTED_IO_ERROR;
577 npc->iov = talloc_realloc(npc, npc->iov,
578 struct iovec, npc->count + 1);
580 status = NT_STATUS_NO_MEMORY;
584 npc->iov[npc->count].iov_base = out->frag.data;
585 npc->iov[npc->count].iov_len = out->frag.length;
587 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
588 (unsigned int)npc->count,
589 (unsigned int)npc->iov[npc->count].iov_len));
590 dump_data(11, (const uint8_t *)npc->iov[npc->count].iov_base,
591 npc->iov[npc->count].iov_len);
595 /* we still don't have a complete request, go back and wait for more
597 if (npc->count == 0) {
598 /* Wait for the next packet */
599 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
601 DEBUG(2, ("Failed to start receving packets\n"));
602 status = NT_STATUS_NO_MEMORY;
605 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
609 DEBUG(10, ("Sending a total of %u bytes\n",
610 (unsigned int)npc->p->out_data.data_sent_length));
612 subreq = tstream_writev_queue_send(npc, npc->ev,
615 npc->iov, npc->count);
617 DEBUG(2, ("Failed to send packet\n"));
618 status = NT_STATUS_NO_MEMORY;
621 tevent_req_set_callback(subreq, named_pipe_packet_done, npc);
625 DEBUG(2, ("Fatal error(%s). "
626 "Terminating client(%s) connection!\n",
627 nt_errstr(status), npc->client_name));
628 /* terminate client connection */
633 static void named_pipe_packet_done(struct tevent_req *subreq)
635 struct named_pipe_client *npc =
636 tevent_req_callback_data(subreq, struct named_pipe_client);
640 ret = tstream_writev_queue_recv(subreq, &sys_errno);
643 DEBUG(2, ("Writev failed!\n"));
647 /* clear out any data that may have been left around */
649 TALLOC_FREE(npc->iov);
650 data_blob_free(&npc->p->in_data.data);
651 data_blob_free(&npc->p->out_data.frag);
652 data_blob_free(&npc->p->out_data.rdata);
654 /* Wait for the next packet */
655 subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
657 DEBUG(2, ("Failed to start receving packets\n"));
661 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
665 DEBUG(2, ("Fatal error(%s). "
666 "Terminating client(%s) connection!\n",
667 strerror(sys_errno), npc->client_name));
668 /* terminate client connection */
673 static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
674 struct messaging_context *msg_ctx,
675 enum dcerpc_transport_t transport,
678 struct tsocket_address *cli_addr,
679 struct tsocket_address *srv_addr,
681 dcerpc_ncacn_disconnect_fn fn);
683 /********************************************************************
684 * Start listening on the tcp/ip socket
685 ********************************************************************/
687 static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
688 struct tevent_fd *fde,
692 uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context *ev_ctx,
693 struct messaging_context *msg_ctx,
694 const struct sockaddr_storage *ifss,
697 struct dcerpc_ncacn_listen_state *state;
698 struct tevent_fd *fde;
701 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
703 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Out of memory\n"));
708 state->ep.port = port;
709 state->disconnect_fn = NULL;
711 if (state->ep.port == 0) {
714 for (i = SERVER_TCP_LOW_PORT; i <= SERVER_TCP_HIGH_PORT; i++) {
715 state->fd = open_socket_in(SOCK_STREAM,
726 state->fd = open_socket_in(SOCK_STREAM,
732 if (state->fd == -1) {
733 DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Failed to create "
734 "socket on port %u!\n", state->ep.port));
738 state->ev_ctx = ev_ctx;
739 state->msg_ctx = msg_ctx;
741 /* ready to listen */
742 set_socket_options(state->fd, "SO_KEEPALIVE");
743 set_socket_options(state->fd, lp_socket_options());
745 /* Set server socket to non-blocking for the accept. */
746 set_blocking(state->fd, false);
748 rc = listen(state->fd, SMBD_LISTEN_BACKLOG);
750 DEBUG(0,("setup_tcpip_socket: listen - %s\n", strerror(errno)));
754 DEBUG(10, ("setup_tcpip_socket: openened socket fd %d for port %u\n",
755 state->fd, state->ep.port));
757 fde = tevent_add_fd(state->ev_ctx,
761 dcerpc_ncacn_tcpip_listener,
764 DEBUG(0, ("setup_tcpip_socket: Failed to add event handler!\n"));
768 tevent_fd_set_auto_close(fde);
770 return state->ep.port;
772 if (state->fd != -1) {
780 static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
781 struct tevent_fd *fde,
785 struct dcerpc_ncacn_listen_state *state =
786 talloc_get_type_abort(private_data,
787 struct dcerpc_ncacn_listen_state);
788 struct tsocket_address *cli_addr = NULL;
789 struct tsocket_address *srv_addr = NULL;
790 struct sockaddr_storage addr;
791 socklen_t in_addrlen = sizeof(addr);
795 s = accept(state->fd, (struct sockaddr *)(void *) &addr, &in_addrlen);
797 if (errno != EINTR) {
798 DEBUG(0,("tcpip_listener accept: %s\n",
804 rc = tsocket_address_bsd_from_sockaddr(state,
805 (struct sockaddr *)(void *) &addr,
813 rc = getsockname(s, (struct sockaddr *)(void *) &addr, &in_addrlen);
819 rc = tsocket_address_bsd_from_sockaddr(state,
820 (struct sockaddr *)(void *) &addr,
828 DEBUG(6, ("tcpip_listener: Accepted socket %d\n", s));
830 dcerpc_ncacn_accept(state->ev_ctx,
841 /********************************************************************
842 * Start listening on the ncalrpc socket
843 ********************************************************************/
845 static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
846 struct tevent_fd *fde,
850 bool setup_dcerpc_ncalrpc_socket(struct tevent_context *ev_ctx,
851 struct messaging_context *msg_ctx,
853 dcerpc_ncacn_disconnect_fn fn)
855 struct dcerpc_ncacn_listen_state *state;
856 struct tevent_fd *fde;
858 state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
860 DEBUG(0, ("Out of memory\n"));
865 state->disconnect_fn = fn;
870 state->ep.name = talloc_strdup(state, name);
872 if (state->ep.name == NULL) {
873 DEBUG(0, ("Out of memory\n"));
878 if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) {
879 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
880 lp_ncalrpc_dir(), strerror(errno)));
884 state->fd = create_pipe_sock(lp_ncalrpc_dir(), name, 0755);
885 if (state->fd == -1) {
886 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
887 lp_ncalrpc_dir(), name));
891 DEBUG(10, ("Openened pipe socket fd %d for %s\n", state->fd, name));
893 state->ev_ctx = ev_ctx;
894 state->msg_ctx = msg_ctx;
896 /* Set server socket to non-blocking for the accept. */
897 set_blocking(state->fd, false);
899 fde = tevent_add_fd(state->ev_ctx,
903 dcerpc_ncalrpc_listener,
906 DEBUG(0, ("Failed to add event handler for ncalrpc!\n"));
910 tevent_fd_set_auto_close(fde);
914 if (state->fd != -1) {
922 static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
923 struct tevent_fd *fde,
927 struct dcerpc_ncacn_listen_state *state =
928 talloc_get_type_abort(private_data,
929 struct dcerpc_ncacn_listen_state);
930 struct tsocket_address *cli_addr = NULL;
931 struct sockaddr_un sunaddr;
932 struct sockaddr *addr = (struct sockaddr *)(void *)&sunaddr;
933 socklen_t len = sizeof(sunaddr);
937 ZERO_STRUCT(sunaddr);
939 sd = accept(state->fd, addr, &len);
941 if (errno != EINTR) {
942 DEBUG(0, ("ncalrpc accept() failed: %s\n", strerror(errno)));
947 rc = tsocket_address_bsd_from_sockaddr(state,
955 DEBUG(10, ("Accepted ncalrpc socket %d\n", sd));
957 dcerpc_ncacn_accept(state->ev_ctx,
962 state->disconnect_fn);
965 struct dcerpc_ncacn_conn {
966 enum dcerpc_transport_t transport;
975 struct pipes_struct *p;
976 dcerpc_ncacn_disconnect_fn disconnect_fn;
978 struct tevent_context *ev_ctx;
979 struct messaging_context *msg_ctx;
981 struct tstream_context *tstream;
982 struct tevent_queue *send_queue;
984 struct tsocket_address *client;
986 struct tsocket_address *server;
988 struct auth_session_info *session_info;
994 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq);
995 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq);
997 static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
998 struct messaging_context *msg_ctx,
999 enum dcerpc_transport_t transport,
1002 struct tsocket_address *cli_addr,
1003 struct tsocket_address *srv_addr,
1005 dcerpc_ncacn_disconnect_fn fn) {
1006 struct dcerpc_ncacn_conn *ncacn_conn;
1007 struct tevent_req *subreq;
1008 const char *cli_str;
1009 const char *srv_str = NULL;
1010 bool system_user = false;
1017 DEBUG(10, ("dcerpc_ncacn_accept\n"));
1019 ncacn_conn = talloc_zero(ev_ctx, struct dcerpc_ncacn_conn);
1020 if (ncacn_conn == NULL) {
1021 DEBUG(0, ("Out of memory!\n"));
1026 ncacn_conn->transport = transport;
1027 ncacn_conn->ev_ctx = ev_ctx;
1028 ncacn_conn->msg_ctx = msg_ctx;
1029 ncacn_conn->sock = s;
1030 ncacn_conn->disconnect_fn = fn;
1032 ncacn_conn->client = talloc_move(ncacn_conn, &cli_addr);
1033 if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
1034 ncacn_conn->client_name =
1035 tsocket_address_inet_addr_string(ncacn_conn->client,
1038 ncacn_conn->client_name =
1039 tsocket_address_unix_path(ncacn_conn->client,
1042 if (ncacn_conn->client_name == NULL) {
1043 DEBUG(0, ("Out of memory!\n"));
1044 talloc_free(ncacn_conn);
1049 if (srv_addr != NULL) {
1050 ncacn_conn->server = talloc_move(ncacn_conn, &srv_addr);
1052 ncacn_conn->server_name =
1053 tsocket_address_inet_addr_string(ncacn_conn->server,
1055 if (ncacn_conn->server_name == NULL) {
1056 DEBUG(0, ("Out of memory!\n"));
1057 talloc_free(ncacn_conn);
1063 switch (transport) {
1065 ncacn_conn->ep.port = port;
1067 pipe_name = tsocket_address_string(ncacn_conn->client,
1069 if (pipe_name == NULL) {
1071 talloc_free(ncacn_conn);
1077 rc = sys_getpeereid(s, &uid);
1079 DEBUG(2, ("Failed to get ncalrpc connecting uid!"));
1081 if (uid == sec_initial_uid()) {
1086 ncacn_conn->ep.name = talloc_strdup(ncacn_conn, name);
1087 if (ncacn_conn->ep.name == NULL) {
1089 talloc_free(ncacn_conn);
1093 pipe_name = talloc_strdup(ncacn_conn,
1095 if (pipe_name == NULL) {
1097 talloc_free(ncacn_conn);
1102 DEBUG(0, ("unknown dcerpc transport: %u!\n",
1104 talloc_free(ncacn_conn);
1109 rc = set_blocking(s, false);
1111 DEBUG(2, ("Failed to set dcerpc socket to non-blocking\n"));
1112 talloc_free(ncacn_conn);
1118 * As soon as we have tstream_bsd_existing_socket set up it will
1119 * take care of closing the socket.
1121 rc = tstream_bsd_existing_socket(ncacn_conn, s, &ncacn_conn->tstream);
1123 DEBUG(2, ("Failed to create tstream socket for dcerpc\n"));
1124 talloc_free(ncacn_conn);
1129 if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
1130 cli_str = ncacn_conn->client_name;
1135 if (ncacn_conn->server != NULL) {
1136 if (tsocket_address_is_inet(ncacn_conn->server, "ip")) {
1137 srv_str = ncacn_conn->server_name;
1143 if (ncacn_conn->session_info == NULL) {
1144 status = auth_anonymous_session_info(ncacn_conn,
1145 &ncacn_conn->session_info);
1146 if (!NT_STATUS_IS_OK(status)) {
1147 DEBUG(2, ("Failed to create "
1148 "auth_anonymous_session_info - %s\n",
1149 nt_errstr(status)));
1150 talloc_free(ncacn_conn);
1155 rc = make_server_pipes_struct(ncacn_conn,
1157 ncacn_conn->transport,
1161 ncacn_conn->session_info,
1165 DEBUG(2, ("Failed to create pipe struct - %s",
1166 strerror(sys_errno)));
1167 talloc_free(ncacn_conn);
1171 ncacn_conn->send_queue = tevent_queue_create(ncacn_conn,
1172 "dcerpc send queue");
1173 if (ncacn_conn->send_queue == NULL) {
1174 DEBUG(0, ("Out of memory!\n"));
1175 talloc_free(ncacn_conn);
1179 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1181 ncacn_conn->tstream);
1182 if (subreq == NULL) {
1183 DEBUG(2, ("Failed to send ncacn packet\n"));
1184 talloc_free(ncacn_conn);
1188 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1190 DEBUG(10, ("dcerpc_ncacn_accept done\n"));
1195 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq)
1197 struct dcerpc_ncacn_conn *ncacn_conn =
1198 tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
1200 struct _output_data *out = &ncacn_conn->p->out_data;
1201 DATA_BLOB recv_buffer = data_blob_null;
1202 struct ncacn_packet *pkt;
1210 status = dcerpc_read_ncacn_packet_recv(subreq, ncacn_conn, &pkt, &recv_buffer);
1211 TALLOC_FREE(subreq);
1212 if (!NT_STATUS_IS_OK(status)) {
1213 if (ncacn_conn->disconnect_fn != NULL) {
1214 ok = ncacn_conn->disconnect_fn(ncacn_conn->p);
1216 DEBUG(3, ("Failed to call disconnect function\n"));
1222 data_left = recv_buffer.length;
1223 data = (char *) recv_buffer.data;
1226 data_used = process_incoming_data(ncacn_conn->p, data, data_left);
1227 if (data_used < 0) {
1228 DEBUG(3, ("Failed to process dcerpc request!\n"));
1229 status = NT_STATUS_UNEXPECTED_IO_ERROR;
1233 data_left -= data_used;
1237 /* Do not leak this buffer */
1238 talloc_free(recv_buffer.data);
1242 * This is needed because of the way DCERPC binds work in the RPC
1245 to_send = out->frag.length - out->current_pdu_sent;
1248 DEBUG(10, ("Current_pdu_len = %u, "
1249 "current_pdu_sent = %u "
1250 "Returning %u bytes\n",
1251 (unsigned int)out->frag.length,
1252 (unsigned int)out->current_pdu_sent,
1253 (unsigned int)to_send));
1255 ncacn_conn->iov = talloc_zero(ncacn_conn, struct iovec);
1256 if (ncacn_conn->iov == NULL) {
1257 status = NT_STATUS_NO_MEMORY;
1258 DEBUG(3, ("Out of memory!\n"));
1261 ncacn_conn->count = 1;
1263 ncacn_conn->iov[0].iov_base = out->frag.data
1264 + out->current_pdu_sent;
1265 ncacn_conn->iov[0].iov_len = to_send;
1267 out->current_pdu_sent += to_send;
1271 * This condition is false for bind packets, or when we haven't yet got
1272 * a full request, and need to wait for more data from the client
1274 while (out->data_sent_length < out->rdata.length) {
1275 ok = create_next_pdu(ncacn_conn->p);
1277 DEBUG(3, ("Failed to create next PDU!\n"));
1278 status = NT_STATUS_UNEXPECTED_IO_ERROR;
1282 ncacn_conn->iov = talloc_realloc(ncacn_conn,
1285 ncacn_conn->count + 1);
1286 if (ncacn_conn->iov == NULL) {
1287 DEBUG(3, ("Out of memory!\n"));
1288 status = NT_STATUS_NO_MEMORY;
1292 ncacn_conn->iov[ncacn_conn->count].iov_base = out->frag.data;
1293 ncacn_conn->iov[ncacn_conn->count].iov_len = out->frag.length;
1295 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
1296 (unsigned int) ncacn_conn->count,
1297 (unsigned int) ncacn_conn->iov[ncacn_conn->count].iov_len));
1298 dump_data(11, (const uint8_t *) ncacn_conn->iov[ncacn_conn->count].iov_base,
1299 ncacn_conn->iov[ncacn_conn->count].iov_len);
1300 ncacn_conn->count++;
1304 * We still don't have a complete request, go back and wait for more
1307 if (ncacn_conn->count == 0) {
1308 /* Wait for the next packet */
1309 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1311 ncacn_conn->tstream);
1312 if (subreq == NULL) {
1313 DEBUG(2, ("Failed to start receving packets\n"));
1314 status = NT_STATUS_NO_MEMORY;
1317 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1321 DEBUG(10, ("Sending a total of %u bytes\n",
1322 (unsigned int)ncacn_conn->p->out_data.data_sent_length));
1324 subreq = tstream_writev_queue_send(ncacn_conn,
1326 ncacn_conn->tstream,
1327 ncacn_conn->send_queue,
1330 if (subreq == NULL) {
1331 DEBUG(2, ("Failed to send packet\n"));
1332 status = NT_STATUS_NO_MEMORY;
1336 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_done, ncacn_conn);
1340 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1341 ncacn_conn->client_name, nt_errstr(status)));
1343 /* Terminate client connection */
1344 talloc_free(ncacn_conn);
1348 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq)
1350 struct dcerpc_ncacn_conn *ncacn_conn =
1351 tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
1352 NTSTATUS status = NT_STATUS_OK;
1356 rc = tstream_writev_queue_recv(subreq, &sys_errno);
1357 TALLOC_FREE(subreq);
1359 DEBUG(2, ("Writev failed!\n"));
1360 status = map_nt_error_from_unix(sys_errno);
1364 /* clear out any data that may have been left around */
1365 ncacn_conn->count = 0;
1366 TALLOC_FREE(ncacn_conn->iov);
1367 data_blob_free(&ncacn_conn->p->in_data.data);
1368 data_blob_free(&ncacn_conn->p->out_data.frag);
1369 data_blob_free(&ncacn_conn->p->out_data.rdata);
1371 /* Wait for the next packet */
1372 subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
1374 ncacn_conn->tstream);
1375 if (subreq == NULL) {
1376 DEBUG(2, ("Failed to start receving packets\n"));
1377 status = NT_STATUS_NO_MEMORY;
1381 tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
1385 DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
1386 ncacn_conn->client_name, nt_errstr(status)));
1388 /* Terminate client connection */
1389 talloc_free(ncacn_conn);
1393 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */