s3-rpc_server: Added initial generic RPC server infrastructure.
[mat/samba.git] / source3 / rpc_server / rpc_server.c
1 /*
2    Unix SMB/Netbios implementation.
3    Generic infrstructure for RPC Daemons
4    Copyright (C) Simo Sorce 2010
5
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.
10
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.
15
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/>.
18 */
19
20 #include "includes.h"
21 #include "rpc_server/rpc_server.h"
22
23 struct named_pipe_listen_state {
24         int fd;
25 };
26
27 static void named_pipe_listener(struct tevent_context *ev,
28                                 struct tevent_fd *fde,
29                                 uint16_t flags,
30                                 void *private_data)
31 {
32         return;
33 }
34
35 bool setup_named_pipe_socket(const char *pipe_name,
36                              struct tevent_context *ev_ctx)
37 {
38         struct named_pipe_listen_state *state;
39         struct tevent_fd *fde;
40         char *np_dir;
41
42         state = talloc(ev_ctx, struct named_pipe_listen_state);
43         if (!state) {
44                 DEBUG(0, ("Out of memory\n"));
45                 return false;
46         }
47         state->fd = -1;
48
49         np_dir = talloc_asprintf(state, "%s/np", lp_ncalrpc_dir());
50         if (!np_dir) {
51                 DEBUG(0, ("Out of memory\n"));
52                 goto out;
53         }
54
55         if (!directory_create_or_exist(np_dir, geteuid(), 0700)) {
56                 DEBUG(0, ("Failed to create pipe directory %s - %s\n",
57                           np_dir, strerror(errno)));
58                 goto out;
59         }
60
61         state->fd = create_pipe_sock(np_dir, pipe_name, 0700);
62         if (state->fd == -1) {
63                 DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
64                           np_dir, pipe_name));
65                 goto out;
66         }
67
68         DEBUG(10, ("Openened pipe socket fd %d for %s\n",
69                    state->fd, pipe_name));
70
71         fde = tevent_add_fd(ev_ctx,
72                             state, state->fd, TEVENT_FD_READ,
73                             named_pipe_listener, state);
74         if (!fde) {
75                 DEBUG(0, ("Failed to add event handler!\n"));
76                 goto out;
77         }
78
79         tevent_fd_set_auto_close(fde);
80         return true;
81
82 out:
83         if (state->fd != -1) {
84                 close(state->fd);
85         }
86         TALLOC_FREE(state);
87         return false;
88 }