2 * Unix SMB/CIFS implementation.
4 * SMBD RPC service callbacks
6 * Copyright (c) 2011 Andreas Schneider <asn@samba.org>
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.
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.
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/>.
25 #include "../librpc/gen_ndr/messaging.h"
26 #include "../librpc/gen_ndr/srv_epmapper.h"
27 #include "rpc_server/rpc_server.h"
29 #define DAEMON_NAME "epmd"
31 void start_epmd(struct tevent_context
*ev_ctx
,
32 struct messaging_context
*msg_ctx
);
34 static bool epmd_open_sockets(struct tevent_context
*ev_ctx
,
35 struct messaging_context
*msg_ctx
)
37 uint32_t num_ifs
= iface_count();
41 if (lp_interfaces() && lp_bind_interfaces_only()) {
43 * We have been given an interfaces line, and been told to only
44 * bind to those interfaces. Create a socket per interface and
48 /* Now open a listen socket for each of the interfaces. */
49 for(i
= 0; i
< num_ifs
; i
++) {
50 const struct sockaddr_storage
*ifss
=
51 iface_n_sockaddr_storage(i
);
53 port
= setup_dcerpc_ncacn_tcpip_socket(ev_ctx
,
55 ndr_table_epmapper
.syntax_id
,
63 const char *sock_addr
= lp_socket_address();
67 if (strequal(sock_addr
, "0.0.0.0") ||
68 strequal(sock_addr
, "::")) {
72 sock_addr
= "0.0.0.0";
76 for (sock_ptr
= sock_addr
;
77 next_token_talloc(talloc_tos(), &sock_ptr
, &sock_tok
, " \t,");
79 struct sockaddr_storage ss
;
81 /* open an incoming socket */
82 if (!interpret_string_addr(&ss
,
84 AI_NUMERICHOST
|AI_PASSIVE
)) {
88 port
= setup_dcerpc_ncacn_tcpip_socket(ev_ctx
,
90 ndr_table_epmapper
.syntax_id
,
102 static void epmd_reopen_logs(void)
104 char *lfile
= lp_logfile();
107 if (lfile
== NULL
|| lfile
[0] == '\0') {
108 rc
= asprintf(&lfile
, "%s/log.%s", get_dyn_LOGFILEBASE(), DAEMON_NAME
);
110 lp_set_logfile(lfile
);
114 if (strstr(lfile
, DAEMON_NAME
) == NULL
) {
115 rc
= asprintf(&lfile
, "%s.%s", lp_logfile(), DAEMON_NAME
);
117 lp_set_logfile(lfile
);
126 static void epmd_smb_conf_updated(struct messaging_context
*msg
,
129 struct server_id server_id
,
132 struct tevent_context
*ev_ctx
=
133 talloc_get_type_abort(private_data
, struct tevent_context
);
135 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
136 change_to_root_user();
140 static void epmd_sig_term_handler(struct tevent_context
*ev
,
141 struct tevent_signal
*se
,
147 exit_server_cleanly("termination signal");
150 static void epmd_setup_sig_term_handler(struct tevent_context
*ev_ctx
)
152 struct tevent_signal
*se
;
154 se
= tevent_add_signal(ev_ctx
,
157 epmd_sig_term_handler
,
160 exit_server("failed to setup SIGTERM handler");
164 static void epmd_sig_hup_handler(struct tevent_context
*ev
,
165 struct tevent_signal
*se
,
171 struct messaging_context
*msg_ctx
= talloc_get_type_abort(private_data
,
172 struct messaging_context
);
174 change_to_root_user();
176 DEBUG(1,("Reloading printers after SIGHUP\n"));
180 static void epmd_setup_sig_hup_handler(struct tevent_context
*ev_ctx
,
181 struct messaging_context
*msg_ctx
)
183 struct tevent_signal
*se
;
185 se
= tevent_add_signal(ev_ctx
,
188 epmd_sig_hup_handler
,
191 exit_server("failed to setup SIGHUP handler");
195 void start_epmd(struct tevent_context
*ev_ctx
,
196 struct messaging_context
*msg_ctx
)
203 DEBUG(1, ("Forking Endpoint Mapper Daemon\n"));
208 DEBUG(0, ("Failed to fork Endpoint Mapper [%s], aborting ...\n",
219 close_low_fds(false);
221 status
= reinit_after_fork(msg_ctx
,
225 if (!NT_STATUS_IS_OK(status
)) {
226 DEBUG(0,("reinit_after_fork() failed\n"));
227 smb_panic("reinit_after_fork() failed");
232 epmd_setup_sig_term_handler(ev_ctx
);
233 epmd_setup_sig_hup_handler(ev_ctx
, msg_ctx
);
235 ok
= serverid_register(procid_self(),
236 FLAG_MSG_GENERAL
|FLAG_MSG_SMBD
237 |FLAG_MSG_PRINT_GENERAL
);
239 DEBUG(0, ("Failed to register serverid in epmd!\n"));
243 messaging_register(msg_ctx
,
245 MSG_SMB_CONF_UPDATED
,
246 epmd_smb_conf_updated
);
248 status
= rpc_epmapper_init(NULL
);
249 if (!NT_STATUS_IS_OK(status
)) {
250 DEBUG(0, ("Failed to register epmd rpc inteface! (%s)\n",
255 ok
= setup_dcerpc_ncalrpc_socket(ev_ctx
,
257 ndr_table_epmapper
.syntax_id
,
260 DEBUG(0, ("Failed to open epmd ncalrpc pipe!\n"));
264 ok
= epmd_open_sockets(ev_ctx
, msg_ctx
);
266 DEBUG(0, ("Failed to open epmd tcpip sockets!\n"));
270 ok
= setup_named_pipe_socket("epmapper", ev_ctx
);
272 DEBUG(0, ("Failed to open epmd named pipe!\n"));
276 DEBUG(1, ("Endpoint Mapper Daemon Started (%d)\n", getpid()));
279 rc
= tevent_loop_wait(ev_ctx
);
281 /* should not be reached */
282 DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
283 rc
, (rc
== 0) ? "out of events" : strerror(errno
)));
288 /* vim: set ts=8 sw=8 noet cindent ft=c.doxygen: */