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/>.
26 #include "../librpc/gen_ndr/srv_epmapper.h"
27 #include "rpc_server/rpc_server.h"
28 #include "rpc_server/epmapper/srv_epmapper.h"
31 #define DAEMON_NAME "epmd"
33 void start_epmd(struct tevent_context
*ev_ctx
,
34 struct messaging_context
*msg_ctx
);
36 static bool epmd_open_sockets(struct tevent_context
*ev_ctx
,
37 struct messaging_context
*msg_ctx
)
39 uint32_t num_ifs
= iface_count();
43 if (lp_interfaces() && lp_bind_interfaces_only()) {
45 * We have been given an interfaces line, and been told to only
46 * bind to those interfaces. Create a socket per interface and
50 /* Now open a listen socket for each of the interfaces. */
51 for(i
= 0; i
< num_ifs
; i
++) {
52 const struct sockaddr_storage
*ifss
=
53 iface_n_sockaddr_storage(i
);
55 port
= setup_dcerpc_ncacn_tcpip_socket(ev_ctx
,
64 const char *sock_addr
= lp_socket_address();
68 if (strequal(sock_addr
, "0.0.0.0") ||
69 strequal(sock_addr
, "::")) {
71 sock_addr
= "::,0.0.0.0";
73 sock_addr
= "0.0.0.0";
77 for (sock_ptr
= sock_addr
;
78 next_token_talloc(talloc_tos(), &sock_ptr
, &sock_tok
, " \t,");
80 struct sockaddr_storage ss
;
82 /* open an incoming socket */
83 if (!interpret_string_addr(&ss
,
85 AI_NUMERICHOST
|AI_PASSIVE
)) {
89 port
= setup_dcerpc_ncacn_tcpip_socket(ev_ctx
,
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 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
133 change_to_root_user();
137 static void epmd_sig_term_handler(struct tevent_context
*ev
,
138 struct tevent_signal
*se
,
144 rpc_epmapper_shutdown();
146 exit_server_cleanly("termination signal");
149 static void epmd_setup_sig_term_handler(struct tevent_context
*ev_ctx
)
151 struct tevent_signal
*se
;
153 se
= tevent_add_signal(ev_ctx
,
156 epmd_sig_term_handler
,
159 exit_server("failed to setup SIGTERM handler");
163 static void epmd_sig_hup_handler(struct tevent_context
*ev
,
164 struct tevent_signal
*se
,
170 change_to_root_user();
172 DEBUG(1,("Reloading printers after SIGHUP\n"));
176 static void epmd_setup_sig_hup_handler(struct tevent_context
*ev_ctx
,
177 struct messaging_context
*msg_ctx
)
179 struct tevent_signal
*se
;
181 se
= tevent_add_signal(ev_ctx
,
184 epmd_sig_hup_handler
,
187 exit_server("failed to setup SIGHUP handler");
191 static bool epmapper_shutdown_cb(void *ptr
) {
192 srv_epmapper_cleanup();
197 void start_epmd(struct tevent_context
*ev_ctx
,
198 struct messaging_context
*msg_ctx
)
200 struct rpc_srv_callbacks epmapper_cb
;
206 epmapper_cb
.init
= NULL
;
207 epmapper_cb
.shutdown
= epmapper_shutdown_cb
;
208 epmapper_cb
.private_data
= NULL
;
210 DEBUG(1, ("Forking Endpoint Mapper Daemon\n"));
215 DEBUG(0, ("Failed to fork Endpoint Mapper [%s], aborting ...\n",
226 close_low_fds(false);
228 status
= reinit_after_fork(msg_ctx
,
232 if (!NT_STATUS_IS_OK(status
)) {
233 DEBUG(0,("reinit_after_fork() failed\n"));
234 smb_panic("reinit_after_fork() failed");
239 epmd_setup_sig_term_handler(ev_ctx
);
240 epmd_setup_sig_hup_handler(ev_ctx
, msg_ctx
);
242 ok
= serverid_register(procid_self(),
243 FLAG_MSG_GENERAL
|FLAG_MSG_SMBD
244 |FLAG_MSG_PRINT_GENERAL
);
246 DEBUG(0, ("Failed to register serverid in epmd!\n"));
250 messaging_register(msg_ctx
,
252 MSG_SMB_CONF_UPDATED
,
253 epmd_smb_conf_updated
);
255 status
= rpc_epmapper_init(&epmapper_cb
);
256 if (!NT_STATUS_IS_OK(status
)) {
257 DEBUG(0, ("Failed to register epmd rpc inteface! (%s)\n",
262 ok
= setup_dcerpc_ncalrpc_socket(ev_ctx
,
265 srv_epmapper_delete_endpoints
);
267 DEBUG(0, ("Failed to open epmd ncalrpc pipe!\n"));
271 ok
= epmd_open_sockets(ev_ctx
, msg_ctx
);
273 DEBUG(0, ("Failed to open epmd tcpip sockets!\n"));
277 ok
= setup_named_pipe_socket("epmapper", ev_ctx
, msg_ctx
);
279 DEBUG(0, ("Failed to open epmd named pipe!\n"));
283 DEBUG(1, ("Endpoint Mapper Daemon Started (%d)\n", getpid()));
286 rc
= tevent_loop_wait(ev_ctx
);
288 /* should not be reached */
289 DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
290 rc
, (rc
== 0) ? "out of events" : strerror(errno
)));
295 /* vim: set ts=8 sw=8 noet cindent ft=c.doxygen: */