s3-messages: make ndr_messaging.h part of messages.h.
[mdw/samba.git] / source3 / rpc_server / epmd.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *
4  *  SMBD RPC service callbacks
5  *
6  *  Copyright (c) 2011      Andreas Schneider <asn@samba.org>
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 3 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include "includes.h"
23
24 #include "serverid.h"
25 #include "../librpc/gen_ndr/srv_epmapper.h"
26 #include "rpc_server/rpc_server.h"
27 #include "rpc_server/epmapper/srv_epmapper.h"
28 #include "messages.h"
29
30 #define DAEMON_NAME "epmd"
31
32 void start_epmd(struct tevent_context *ev_ctx,
33                 struct messaging_context *msg_ctx);
34
35 static bool epmd_open_sockets(struct tevent_context *ev_ctx,
36                               struct messaging_context *msg_ctx)
37 {
38         uint32_t num_ifs = iface_count();
39         uint16_t port;
40         uint32_t i;
41
42         if (lp_interfaces() && lp_bind_interfaces_only()) {
43                 /*
44                  * We have been given an interfaces line, and been told to only
45                  * bind to those interfaces. Create a socket per interface and
46                  * bind to only these.
47                  */
48
49                 /* Now open a listen socket for each of the interfaces. */
50                 for(i = 0; i < num_ifs; i++) {
51                         const struct sockaddr_storage *ifss =
52                                         iface_n_sockaddr_storage(i);
53
54                         port = setup_dcerpc_ncacn_tcpip_socket(ev_ctx,
55                                                                msg_ctx,
56                                                                ndr_table_epmapper.syntax_id,
57                                                                ifss,
58                                                                135);
59                         if (port == 0) {
60                                 return false;
61                         }
62                 }
63         } else {
64                 const char *sock_addr = lp_socket_address();
65                 const char *sock_ptr;
66                 char *sock_tok;
67
68                 if (strequal(sock_addr, "0.0.0.0") ||
69                     strequal(sock_addr, "::")) {
70 #if HAVE_IPV6
71                         sock_addr = "::";
72 #else
73                         sock_addr = "0.0.0.0";
74 #endif
75                 }
76
77                 for (sock_ptr = sock_addr;
78                      next_token_talloc(talloc_tos(), &sock_ptr, &sock_tok, " \t,");
79                     ) {
80                         struct sockaddr_storage ss;
81
82                         /* open an incoming socket */
83                         if (!interpret_string_addr(&ss,
84                                                    sock_tok,
85                                                    AI_NUMERICHOST|AI_PASSIVE)) {
86                                 continue;
87                         }
88
89                         port = setup_dcerpc_ncacn_tcpip_socket(ev_ctx,
90                                                                msg_ctx,
91                                                                ndr_table_epmapper.syntax_id,
92                                                                &ss,
93                                                                135);
94                         if (port == 0) {
95                                 return false;
96                         }
97                 }
98         }
99
100         return true;
101 }
102
103 static void epmd_reopen_logs(void)
104 {
105         char *lfile = lp_logfile();
106         int rc;
107
108         if (lfile == NULL || lfile[0] == '\0') {
109                 rc = asprintf(&lfile, "%s/log.%s", get_dyn_LOGFILEBASE(), DAEMON_NAME);
110                 if (rc > 0) {
111                         lp_set_logfile(lfile);
112                         SAFE_FREE(lfile);
113                 }
114         } else {
115                 if (strstr(lfile, DAEMON_NAME) == NULL) {
116                         rc = asprintf(&lfile, "%s.%s", lp_logfile(), DAEMON_NAME);
117                         if (rc > 0) {
118                                 lp_set_logfile(lfile);
119                                 SAFE_FREE(lfile);
120                         }
121                 }
122         }
123
124         reopen_logs();
125 }
126
127 static void epmd_smb_conf_updated(struct messaging_context *msg,
128                                   void *private_data,
129                                   uint32_t msg_type,
130                                   struct server_id server_id,
131                                   DATA_BLOB *data)
132 {
133         DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
134         change_to_root_user();
135         epmd_reopen_logs();
136 }
137
138 static void epmd_sig_term_handler(struct tevent_context *ev,
139                                   struct tevent_signal *se,
140                                   int signum,
141                                   int count,
142                                   void *siginfo,
143                                   void *private_data)
144 {
145         rpc_epmapper_shutdown();
146
147         exit_server_cleanly("termination signal");
148 }
149
150 static void epmd_setup_sig_term_handler(struct tevent_context *ev_ctx)
151 {
152         struct tevent_signal *se;
153
154         se = tevent_add_signal(ev_ctx,
155                                ev_ctx,
156                                SIGTERM, 0,
157                                epmd_sig_term_handler,
158                                NULL);
159         if (se == NULL) {
160                 exit_server("failed to setup SIGTERM handler");
161         }
162 }
163
164 static void epmd_sig_hup_handler(struct tevent_context *ev,
165                                     struct tevent_signal *se,
166                                     int signum,
167                                     int count,
168                                     void *siginfo,
169                                     void *private_data)
170 {
171         change_to_root_user();
172
173         DEBUG(1,("Reloading printers after SIGHUP\n"));
174         epmd_reopen_logs();
175 }
176
177 static void epmd_setup_sig_hup_handler(struct tevent_context *ev_ctx,
178                                        struct messaging_context *msg_ctx)
179 {
180         struct tevent_signal *se;
181
182         se = tevent_add_signal(ev_ctx,
183                                ev_ctx,
184                                SIGHUP, 0,
185                                epmd_sig_hup_handler,
186                                msg_ctx);
187         if (se == NULL) {
188                 exit_server("failed to setup SIGHUP handler");
189         }
190 }
191
192 static bool epmapper_shutdown_cb(void *ptr) {
193         srv_epmapper_cleanup();
194
195         return true;
196 }
197
198 void start_epmd(struct tevent_context *ev_ctx,
199                 struct messaging_context *msg_ctx)
200 {
201         struct rpc_srv_callbacks epmapper_cb;
202         NTSTATUS status;
203         pid_t pid;
204         bool ok;
205         int rc;
206
207         epmapper_cb.init = NULL;
208         epmapper_cb.shutdown = epmapper_shutdown_cb;
209         epmapper_cb.private_data = NULL;
210
211         DEBUG(1, ("Forking Endpoint Mapper Daemon\n"));
212
213         pid = sys_fork();
214
215         if (pid == -1) {
216                 DEBUG(0, ("Failed to fork Endpoint Mapper [%s], aborting ...\n",
217                           strerror(errno)));
218                 exit(1);
219         }
220
221         if (pid) {
222                 /* parent */
223                 return;
224         }
225
226         /* child */
227         close_low_fds(false);
228
229         status = reinit_after_fork(msg_ctx,
230                                    ev_ctx,
231                                    procid_self(),
232                                    true);
233         if (!NT_STATUS_IS_OK(status)) {
234                 DEBUG(0,("reinit_after_fork() failed\n"));
235                 smb_panic("reinit_after_fork() failed");
236         }
237
238         epmd_reopen_logs();
239
240         epmd_setup_sig_term_handler(ev_ctx);
241         epmd_setup_sig_hup_handler(ev_ctx, msg_ctx);
242
243         ok = serverid_register(procid_self(),
244                                FLAG_MSG_GENERAL|FLAG_MSG_SMBD
245                                |FLAG_MSG_PRINT_GENERAL);
246         if (!ok) {
247                 DEBUG(0, ("Failed to register serverid in epmd!\n"));
248                 exit(1);
249         }
250
251         messaging_register(msg_ctx,
252                            ev_ctx,
253                            MSG_SMB_CONF_UPDATED,
254                            epmd_smb_conf_updated);
255
256         status = rpc_epmapper_init(&epmapper_cb);
257         if (!NT_STATUS_IS_OK(status)) {
258                 DEBUG(0, ("Failed to register epmd rpc inteface! (%s)\n",
259                           nt_errstr(status)));
260                 exit(1);
261         }
262
263         ok = setup_dcerpc_ncalrpc_socket(ev_ctx,
264                                          msg_ctx,
265                                          ndr_table_epmapper.syntax_id,
266                                          "EPMAPPER",
267                                          srv_epmapper_delete_endpoints);
268         if (!ok) {
269                 DEBUG(0, ("Failed to open epmd ncalrpc pipe!\n"));
270                 exit(1);
271         }
272
273         ok = epmd_open_sockets(ev_ctx, msg_ctx);
274         if (!ok) {
275                 DEBUG(0, ("Failed to open epmd tcpip sockets!\n"));
276                 exit(1);
277         }
278
279         ok = setup_named_pipe_socket("epmapper", ev_ctx);
280         if (!ok) {
281                 DEBUG(0, ("Failed to open epmd named pipe!\n"));
282                 exit(1);
283         }
284
285         DEBUG(1, ("Endpoint Mapper Daemon Started (%d)\n", getpid()));
286
287         /* loop forever */
288         rc = tevent_loop_wait(ev_ctx);
289
290         /* should not be reached */
291         DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
292                  rc, (rc == 0) ? "out of events" : strerror(errno)));
293
294         exit(1);
295 }
296
297 /* vim: set ts=8 sw=8 noet cindent ft=c.doxygen: */