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/>.
21 #include "rpc_server/rpc_server.h"
23 #include "librpc/gen_ndr/netlogon.h"
24 #include "registry/reg_parse_prs.h"
25 #include "lib/tsocket/tsocket.h"
26 #include "libcli/named_pipe_auth/npa_tstream.h"
28 /* Creates a pipes_struct and initializes it with the information
29 * sent from the client */
30 static int make_server_pipes_struct(TALLOC_CTX *mem_ctx,
31 const char *pipe_name,
32 const struct ndr_syntax_id id,
33 const char *client_address,
34 struct netr_SamInfo3 *info3,
35 struct pipes_struct **_p,
38 struct pipes_struct *p;
42 p = talloc_zero(mem_ctx, struct pipes_struct);
49 p->mem_ctx = talloc_named(p, 0, "pipe %s %p", pipe_name, p);
56 ok = init_pipe_handles(p, &id);
58 DEBUG(1, ("Failed to init handles\n"));
65 data_blob_free(&p->in_data.data);
66 data_blob_free(&p->in_data.pdu);
68 p->endian = RPC_LITTLE_ENDIAN;
70 status = make_server_info_info3(p,
71 info3->base.account_name.string,
72 info3->base.domain.string,
73 &p->server_info, info3);
74 if (!NT_STATUS_IS_OK(status)) {
75 DEBUG(1, ("Failed to init server info\n"));
82 * Some internal functions need a local token to determine access to
85 status = create_local_token(p->server_info);
86 if (!NT_STATUS_IS_OK(status)) {
87 DEBUG(1, ("Failed to init local auth token\n"));
93 p->client_id = talloc_zero(p, struct client_address);
99 strlcpy(p->client_id->addr,
100 client_address, sizeof(p->client_id->addr));
102 talloc_set_destructor(p, close_internal_rpc_pipe_hnd);
108 /* Add some helper functions to wrap the common ncacn packet reading functions
109 * until we can share more dcerpc code */
110 struct named_pipe_read_packet_state {
111 struct ncacn_packet *pkt;
115 static void named_pipe_read_packet_done(struct tevent_req *subreq);
117 static struct tevent_req *named_pipe_read_packet_send(TALLOC_CTX *mem_ctx,
118 struct tevent_context *ev,
119 struct tstream_context *tstream)
121 struct named_pipe_read_packet_state *state;
122 struct tevent_req *req, *subreq;
124 req = tevent_req_create(mem_ctx, &state,
125 struct named_pipe_read_packet_state);
131 subreq = dcerpc_read_ncacn_packet_send(state, ev, tstream);
133 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
134 tevent_req_post(req, ev);
137 tevent_req_set_callback(subreq, named_pipe_read_packet_done, req);
142 static void named_pipe_read_packet_done(struct tevent_req *subreq)
144 struct tevent_req *req =
145 tevent_req_callback_data(subreq, struct tevent_req);
146 struct named_pipe_read_packet_state *state =
147 tevent_req_data(req, struct named_pipe_read_packet_state);
150 status = dcerpc_read_ncacn_packet_recv(subreq, state,
154 if (!NT_STATUS_IS_OK(status)) {
155 DEBUG(3, ("Failed to receive dceprc packet!\n"));
156 tevent_req_nterror(req, status);
160 tevent_req_done(req);
163 static NTSTATUS named_pipe_read_packet_recv(struct tevent_req *req,
167 struct named_pipe_read_packet_state *state =
168 tevent_req_data(req, struct named_pipe_read_packet_state);
171 if (tevent_req_is_nterror(req, &status)) {
172 tevent_req_received(req);
176 buffer->data = talloc_move(mem_ctx, &state->buffer.data);
177 buffer->length = state->buffer.length;
179 tevent_req_received(req);
185 /* Start listening on the appropriate unix socket and setup all is needed to
186 * dispatch requests to the pipes rpc implementation */
188 struct named_pipe_listen_state {
193 static void named_pipe_listener(struct tevent_context *ev,
194 struct tevent_fd *fde,
198 bool setup_named_pipe_socket(const char *pipe_name,
199 struct tevent_context *ev_ctx)
201 struct named_pipe_listen_state *state;
202 struct tevent_fd *fde;
205 state = talloc(ev_ctx, struct named_pipe_listen_state);
207 DEBUG(0, ("Out of memory\n"));
210 state->name = talloc_strdup(state, pipe_name);
212 DEBUG(0, ("Out of memory\n"));
217 np_dir = talloc_asprintf(state, "%s/np", lp_ncalrpc_dir());
219 DEBUG(0, ("Out of memory\n"));
223 if (!directory_create_or_exist(np_dir, geteuid(), 0700)) {
224 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
225 np_dir, strerror(errno)));
229 state->fd = create_pipe_sock(np_dir, pipe_name, 0700);
230 if (state->fd == -1) {
231 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
236 DEBUG(10, ("Openened pipe socket fd %d for %s\n",
237 state->fd, pipe_name));
239 fde = tevent_add_fd(ev_ctx,
240 state, state->fd, TEVENT_FD_READ,
241 named_pipe_listener, state);
243 DEBUG(0, ("Failed to add event handler!\n"));
247 tevent_fd_set_auto_close(fde);
251 if (state->fd != -1) {
258 static void named_pipe_accept_function(const char *pipe_name, int fd);
260 static void named_pipe_listener(struct tevent_context *ev,
261 struct tevent_fd *fde,
265 struct named_pipe_listen_state *state =
266 talloc_get_type_abort(private_data,
267 struct named_pipe_listen_state);
268 struct sockaddr_un sunaddr;
272 /* TODO: should we have a limit to the number of clients ? */
274 len = sizeof(sunaddr);
277 sd = accept(state->fd,
278 (struct sockaddr *)(void *)&sunaddr, &len);
279 if (errno != EINTR) break;
283 DEBUG(6, ("Failed to get a valid socket [%s]\n",
288 DEBUG(6, ("Accepted socket %d\n", sd));
290 named_pipe_accept_function(state->name, sd);
294 /* This is the core of the rpc server.
295 * Accepts connections from clients and process requests using the appropriate
296 * dispatcher table. */
298 struct named_pipe_client {
299 const char *pipe_name;
300 struct ndr_syntax_id pipe_id;
302 struct tevent_context *ev;
305 uint16_t device_state;
306 uint64_t allocation_size;
308 struct tstream_context *tstream;
310 struct tsocket_address *client;
312 struct tsocket_address *server;
314 struct netr_SamInfo3 *info3;
315 DATA_BLOB session_key;
316 DATA_BLOB delegated_creds;
318 struct pipes_struct *p;
320 struct tevent_queue *write_queue;
326 static void named_pipe_accept_done(struct tevent_req *subreq);
328 static void named_pipe_accept_function(const char *pipe_name, int fd)
330 struct ndr_syntax_id syntax;
331 struct named_pipe_client *npc;
332 struct tstream_context *plain;
333 struct tevent_req *subreq;
337 ok = is_known_pipename(pipe_name, &syntax);
339 DEBUG(1, ("Unknown pipe [%s]\n", pipe_name));
344 npc = talloc_zero(NULL, struct named_pipe_client);
346 DEBUG(0, ("Out of memory!\n"));
350 npc->pipe_name = pipe_name;
351 npc->pipe_id = syntax;
352 npc->ev = server_event_context();
354 /* make sure socket is in NON blocking state */
355 ret = set_blocking(fd, false);
357 DEBUG(2, ("Failed to make socket non-blocking\n"));
363 ret = tstream_bsd_existing_socket(npc, fd, &plain);
365 DEBUG(2, ("Failed to create tstream socket\n"));
371 npc->file_type = FILE_TYPE_MESSAGE_MODE_PIPE;
372 npc->device_state = 0xff | 0x0400 | 0x0100;
373 npc->allocation_size = 4096;
375 subreq = tstream_npa_accept_existing_send(npc, npc->ev, plain,
378 npc->allocation_size);
380 DEBUG(2, ("Failed to start async accept procedure\n"));
385 tevent_req_set_callback(subreq, named_pipe_accept_done, npc);
388 static void named_pipe_packet_process(struct tevent_req *subreq);
389 static void named_pipe_packet_done(struct tevent_req *subreq);
391 static void named_pipe_accept_done(struct tevent_req *subreq)
393 struct named_pipe_client *npc =
394 tevent_req_callback_data(subreq, struct named_pipe_client);
395 const char *cli_addr;
399 ret = tstream_npa_accept_existing_recv(subreq, &error, npc,
407 &npc->delegated_creds);
410 DEBUG(2, ("Failed to accept named pipe connection! (%s)\n",
416 if (tsocket_address_is_inet(npc->client, "ip")) {
417 cli_addr = tsocket_address_inet_addr_string(npc->client,
419 if (cli_addr == NULL) {
427 ret = make_server_pipes_struct(npc,
428 npc->pipe_name, npc->pipe_id,
429 cli_addr, npc->info3,
432 DEBUG(2, ("Failed to create pipes_struct! (%s)\n",
437 npc->write_queue = tevent_queue_create(npc, "np_server_write_queue");
438 if (!npc->write_queue) {
439 DEBUG(2, ("Failed to set up write queue!\n"));
443 /* And now start receaving and processing packets */
444 subreq = named_pipe_read_packet_send(npc, npc->ev, npc->tstream);
446 DEBUG(2, ("Failed to start receving packets\n"));
449 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
453 DEBUG(2, ("Fatal error. Terminating client(%s) connection!\n",
455 /* terminate client connection */
460 static void named_pipe_packet_process(struct tevent_req *subreq)
462 struct named_pipe_client *npc =
463 tevent_req_callback_data(subreq, struct named_pipe_client);
464 struct _output_data *out = &npc->p->out_data;
465 DATA_BLOB recv_buffer;
473 status = named_pipe_read_packet_recv(subreq, npc, &recv_buffer);
475 if (!NT_STATUS_IS_OK(status)) {
479 data_left = recv_buffer.length;
480 data = (char *)recv_buffer.data;
484 data_used = process_incoming_data(npc->p, data, data_left);
486 DEBUG(3, ("Failed to process dceprc request!\n"));
487 status = NT_STATUS_UNEXPECTED_IO_ERROR;
491 data_left -= data_used;
495 /* Do not leak this buffer, npc is a long lived context */
496 talloc_free(recv_buffer.data);
498 /* this is needed because of the way DCERPC Binds work in
499 * the RPC marshalling code */
500 to_send = out->frag.length - out->current_pdu_sent;
503 DEBUG(10, ("Current_pdu_len = %u, "
504 "current_pdu_sent = %u "
505 "Returning %u bytes\n",
506 (unsigned int)out->frag.length,
507 (unsigned int)out->current_pdu_sent,
508 (unsigned int)to_send));
510 npc->iov = talloc_zero(npc, struct iovec);
512 status = NT_STATUS_NO_MEMORY;
517 npc->iov[0].iov_base = out->frag.data
518 + out->current_pdu_sent;
519 npc->iov[0].iov_len = to_send;
521 out->current_pdu_sent += to_send;
524 /* this condition is false for bind packets, or when we haven't
525 * yet got a full request, and need to wait for more data from
527 while (out->data_sent_length < out->rdata.length) {
529 ok = create_next_pdu(npc->p);
531 DEBUG(3, ("Failed to create next PDU!\n"));
532 status = NT_STATUS_UNEXPECTED_IO_ERROR;
536 npc->iov = talloc_realloc(npc, npc->iov,
537 struct iovec, npc->count + 1);
539 status = NT_STATUS_NO_MEMORY;
543 npc->iov[npc->count].iov_base = out->frag.data;
544 npc->iov[npc->count].iov_len = out->frag.length;
546 DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
547 (unsigned int)npc->count,
548 (unsigned int)npc->iov[npc->count].iov_len));
549 dump_data(11, npc->iov[npc->count].iov_base,
550 npc->iov[npc->count].iov_len);
554 /* we still don't have a complete request, go back and wait for more
556 if (npc->count == 0) {
557 /* Wait for the next packet */
558 subreq = named_pipe_read_packet_send(npc, npc->ev, npc->tstream);
560 DEBUG(2, ("Failed to start receving packets\n"));
561 status = NT_STATUS_NO_MEMORY;
564 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
568 DEBUG(10, ("Sending a total of %u bytes\n",
569 (unsigned int)npc->p->out_data.data_sent_length));
571 subreq = tstream_writev_queue_send(npc, npc->ev,
574 npc->iov, npc->count);
576 DEBUG(2, ("Failed to send packet\n"));
577 status = NT_STATUS_NO_MEMORY;
580 tevent_req_set_callback(subreq, named_pipe_packet_done, npc);
584 DEBUG(2, ("Fatal error(%s). "
585 "Terminating client(%s) connection!\n",
586 nt_errstr(status), npc->client_name));
587 /* terminate client connection */
592 static void named_pipe_packet_done(struct tevent_req *subreq)
594 struct named_pipe_client *npc =
595 tevent_req_callback_data(subreq, struct named_pipe_client);
599 ret = tstream_writev_queue_recv(subreq, &sys_errno);
602 DEBUG(2, ("Writev failed!\n"));
606 /* clear out any data that may have been left around */
608 TALLOC_FREE(npc->iov);
609 data_blob_free(&npc->p->in_data.data);
610 data_blob_free(&npc->p->out_data.frag);
611 data_blob_free(&npc->p->out_data.rdata);
613 /* Wait for the next packet */
614 subreq = named_pipe_read_packet_send(npc, npc->ev, npc->tstream);
616 DEBUG(2, ("Failed to start receving packets\n"));
620 tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
624 DEBUG(2, ("Fatal error(%s). "
625 "Terminating client(%s) connection!\n",
626 strerror(sys_errno), npc->client_name));
627 /* terminate client connection */