Copyright (C) Andrew Tridgell 1994-1998
Copyright (C) Jeremy Allison 1997-2002
Copyright (C) Jelmer Vernooij 2002,2003 (Conversion to popt)
-
+
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
-
+
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
-
+
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-
*/
#include "includes.h"
+#include "system/filesys.h"
+#include "popt_common.h"
+#include "nmbd/nmbd.h"
+#include "serverid.h"
+#include "messages.h"
int ClientNMB = -1;
int ClientDGRAM = -1;
struct event_context *nmbd_event_context(void)
{
- static struct event_context *ctx;
-
- if (!ctx && !(ctx = event_context_init(NULL))) {
- smb_panic("Could not init nmbd event context");
- }
- return ctx;
+ return server_event_context();
}
-struct messaging_context *nmbd_messaging_context(void)
+static struct messaging_context *nmbd_messaging_context(void)
{
- static struct messaging_context *ctx;
-
- if (!ctx && !(ctx = messaging_init(NULL, server_id_self(),
- nmbd_event_context()))) {
- smb_panic("Could not init nmbd messaging context");
+ struct messaging_context *msg_ctx = server_messaging_context();
+ if (likely(msg_ctx != NULL)) {
+ return msg_ctx;
}
- return ctx;
+ smb_panic("Could not init nmbd's messaging context.\n");
+ return NULL;
}
/**************************************************************************** **
Handle a SIGTERM in band.
**************************************************************************** */
-static void terminate(void)
+static void terminate(struct messaging_context *msg)
{
DEBUG(0,("Got SIGTERM: going down...\n"));
-
+
/* Write out wins.dat file if samba is a WINS server */
wins_write_database(0,False);
-
+
/* Remove all SELF registered names from WINS */
release_wins_names();
-
+
/* Announce all server entries as 0 time-to-live, 0 type. */
announce_my_servers_removed();
/* If there was an async dns child - kill it. */
kill_async_dns_child();
- exit(0);
-}
+ gencache_stabilize();
+ serverid_deregister(messaging_server_id(msg));
-/**************************************************************************** **
- Handle a SHUTDOWN message from smbcontrol.
- **************************************************************************** */
+ pidfile_unlink();
-static void nmbd_terminate(struct messaging_context *msg,
- void *private_data,
- uint32_t msg_type,
- struct server_id server_id,
- DATA_BLOB *data)
-{
- terminate();
+ exit(0);
}
-/**************************************************************************** **
- Catch a SIGTERM signal.
- **************************************************************************** */
+static void nmbd_sig_term_handler(struct tevent_context *ev,
+ struct tevent_signal *se,
+ int signum,
+ int count,
+ void *siginfo,
+ void *private_data)
+{
+ struct messaging_context *msg = talloc_get_type_abort(
+ private_data, struct messaging_context);
-static SIG_ATOMIC_T got_sig_term;
+ terminate(msg);
+}
-static void sig_term(int sig)
+static bool nmbd_setup_sig_term_handler(struct messaging_context *msg)
{
- got_sig_term = 1;
- sys_select_signal(SIGTERM);
+ struct tevent_signal *se;
+
+ se = tevent_add_signal(nmbd_event_context(),
+ nmbd_event_context(),
+ SIGTERM, 0,
+ nmbd_sig_term_handler,
+ msg);
+ if (!se) {
+ DEBUG(0,("failed to setup SIGTERM handler"));
+ return false;
+ }
+
+ return true;
}
-/**************************************************************************** **
- Catch a SIGHUP signal.
- **************************************************************************** */
+static void msg_reload_nmbd_services(struct messaging_context *msg,
+ void *private_data,
+ uint32_t msg_type,
+ struct server_id server_id,
+ DATA_BLOB *data);
+
+static void nmbd_sig_hup_handler(struct tevent_context *ev,
+ struct tevent_signal *se,
+ int signum,
+ int count,
+ void *siginfo,
+ void *private_data)
+{
+ struct messaging_context *msg = talloc_get_type_abort(
+ private_data, struct messaging_context);
-static SIG_ATOMIC_T reload_after_sighup;
+ DEBUG(0,("Got SIGHUP dumping debug info.\n"));
+ msg_reload_nmbd_services(msg, NULL, MSG_SMB_CONF_UPDATED,
+ messaging_server_id(msg), NULL);
+}
-static void sig_hup(int sig)
+static bool nmbd_setup_sig_hup_handler(struct messaging_context *msg)
{
- reload_after_sighup = 1;
- sys_select_signal(SIGHUP);
+ struct tevent_signal *se;
+
+ se = tevent_add_signal(nmbd_event_context(),
+ nmbd_event_context(),
+ SIGHUP, 0,
+ nmbd_sig_hup_handler,
+ msg);
+ if (!se) {
+ DEBUG(0,("failed to setup SIGHUP handler"));
+ return false;
+ }
+
+ return true;
}
/**************************************************************************** **
- Possibly continue after a fault.
+ Handle a SHUTDOWN message from smbcontrol.
**************************************************************************** */
-static void fault_continue(void)
+static void nmbd_terminate(struct messaging_context *msg,
+ void *private_data,
+ uint32_t msg_type,
+ struct server_id server_id,
+ DATA_BLOB *data)
{
- dump_core();
+ terminate(msg);
}
/**************************************************************************** **
static void expire_names_and_servers(time_t t)
{
static time_t lastrun = 0;
-
+
if ( !lastrun )
lastrun = t;
if ( t < (lastrun + 5) )
/************************************************************************** **
Reload the list of network interfaces.
+ Doesn't return until a network interface is up.
************************************************************************** */
-static bool reload_interfaces(time_t t)
+static void reload_interfaces(time_t t)
{
static time_t lastt;
int n;
+ bool print_waiting_msg = true;
struct subnet_record *subrec;
- if (t && ((t - lastt) < NMBD_INTERFACES_RELOAD)) return False;
+ if (t && ((t - lastt) < NMBD_INTERFACES_RELOAD)) {
+ return;
+ }
+
lastt = t;
- if (!interfaces_changed()) return False;
+ if (!interfaces_changed()) {
+ return;
+ }
+
+ try_again:
/* the list of probed interfaces has changed, we may need to add/remove
some subnets */
continue;
}
- ip = ((struct sockaddr_in *)&iface->ip)->sin_addr;
- nmask = ((struct sockaddr_in *)&iface->netmask)->sin_addr;
+ ip = ((const struct sockaddr_in *)(const void *)&iface->ip)->sin_addr;
+ nmask = ((const struct sockaddr_in *)(const void *)
+ &iface->netmask)->sin_addr;
/*
* We don't want to add a loopback interface, in case
* ignore it here. JRA.
*/
- if (is_loopback_addr(&iface->ip)) {
+ if (is_loopback_addr((const struct sockaddr *)(const void *)&iface->ip)) {
DEBUG(2,("reload_interfaces: Ignoring loopback "
"interface %s\n",
print_sockaddr(str, sizeof(str), &iface->ip) ));
"ignoring non IPv4 interface.\n"));
continue;
}
- ip = ((struct sockaddr_in *)&iface->ip)->sin_addr;
- nmask = ((struct sockaddr_in *)&iface->netmask)->sin_addr;
+ ip = ((struct sockaddr_in *)(void *)
+ &iface->ip)->sin_addr;
+ nmask = ((struct sockaddr_in *)(void *)
+ &iface->netmask)->sin_addr;
if (ip_equal_v4(ip, subrec->myip) &&
ip_equal_v4(nmask, subrec->mask_ip)) {
break;
rescan_listen_set = True;
- /* We need to shutdown if there are no subnets... */
+ /* We need to wait if there are no subnets... */
if (FIRST_SUBNET == NULL) {
- DEBUG(0,("reload_interfaces: No subnets to listen to. Shutting down...\n"));
- return True;
+ void (*saved_handler)(int);
+
+ if (print_waiting_msg) {
+ DEBUG(0,("reload_interfaces: "
+ "No subnets to listen to. Waiting..\n"));
+ print_waiting_msg = false;
+ }
+
+ /*
+ * Whilst we're waiting for an interface, allow SIGTERM to
+ * cause us to exit.
+ */
+ saved_handler = CatchSignal(SIGTERM, SIG_DFL);
+
+ /* We only count IPv4, non-loopback interfaces here. */
+ while (iface_count_v4_nl() == 0) {
+ sleep(5);
+ load_interfaces();
+ }
+
+ CatchSignal(SIGTERM, saved_handler);
+
+ /*
+ * We got an interface, go back to blocking term.
+ */
+
+ goto try_again;
}
- return False;
}
/**************************************************************************** **
set_remote_machine_name("nmbd", False);
if ( lp_loaded() ) {
- pstring fname;
- pstrcpy( fname,lp_configfile());
- if (file_exist(fname,NULL) && !strcsequal(fname,dyn_CONFIGFILE)) {
- pstrcpy(dyn_CONFIGFILE,fname);
+ const char *fname = lp_configfile();
+ if (file_exist(fname) && !strcsequal(fname,get_dyn_CONFIGFILE())) {
+ set_dyn_CONFIGFILE(fname);
test = False;
}
}
if ( test && !lp_file_list_changed() )
return(True);
- ret = lp_load( dyn_CONFIGFILE, True , False, False, True);
+ ret = lp_load_global(get_dyn_CONFIGFILE());
/* perhaps the config filename is now set */
if ( !test ) {
/**************************************************************************** **
* React on 'smbcontrol nmbd reload-config' in the same way as to SIGHUP
- * We use buf here to return bool result to process() when reload_interfaces()
- * detects that there are no subnets.
**************************************************************************** */
static void msg_reload_nmbd_services(struct messaging_context *msg,
dump_all_namelists();
reload_nmbd_services( True );
reopen_logs();
-
- if (data->data) {
- /* We were called from process() */
- /* If reload_interfaces() returned True */
- /* we need to shutdown if there are no subnets... */
- /* pass this info back to process() */
- *((bool *)data->data) = reload_interfaces(0);
- }
+ reload_interfaces(0);
}
static void msg_nmbd_send_packet(struct messaging_context *msg,
const struct sockaddr_storage *pss;
const struct in_addr *local_ip;
- DEBUG(10, ("Received send_packet from %d\n", procid_to_pid(&src)));
+ DEBUG(10, ("Received send_packet from %u\n", (unsigned int)procid_to_pid(&src)));
if (data->length != sizeof(struct packet_struct)) {
- DEBUG(2, ("Discarding invalid packet length from %d\n",
- procid_to_pid(&src)));
+ DEBUG(2, ("Discarding invalid packet length from %u\n",
+ (unsigned int)procid_to_pid(&src)));
return;
}
if ((p->packet_type != NMB_PACKET) &&
(p->packet_type != DGRAM_PACKET)) {
- DEBUG(2, ("Discarding invalid packet type from %d: %d\n",
- procid_to_pid(&src), p->packet_type));
+ DEBUG(2, ("Discarding invalid packet type from %u: %d\n",
+ (unsigned int)procid_to_pid(&src), p->packet_type));
return;
}
in_addr_to_sockaddr_storage(&ss, p->ip);
- pss = iface_ip(&ss);
+ pss = iface_ip((struct sockaddr *)(void *)&ss);
if (pss == NULL) {
- DEBUG(2, ("Could not find ip for packet from %d\n",
- procid_to_pid(&src)));
+ DEBUG(2, ("Could not find ip for packet from %u\n",
+ (unsigned int)procid_to_pid(&src)));
return;
}
local_ip = &((const struct sockaddr_in *)pss)->sin_addr;
subrec = FIRST_SUBNET;
- p->fd = (p->packet_type == NMB_PACKET) ?
+ p->recv_fd = -1;
+ p->send_fd = (p->packet_type == NMB_PACKET) ?
subrec->nmb_sock : subrec->dgram_sock;
for (subrec = FIRST_SUBNET; subrec != NULL;
subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
if (ip_equal_v4(*local_ip, subrec->myip)) {
- p->fd = (p->packet_type == NMB_PACKET) ?
+ p->send_fd = (p->packet_type == NMB_PACKET) ?
subrec->nmb_sock : subrec->dgram_sock;
break;
}
The main select loop.
**************************************************************************** */
-static void process(void)
+static void process(struct messaging_context *msg)
{
bool run_election;
- bool no_subnets;
while( True ) {
time_t t = time(NULL);
TALLOC_CTX *frame = talloc_stackframe();
- /* Check for internal messages */
-
- message_dispatch(nmbd_messaging_context());
-
/*
* Check all broadcast subnets to see if
* we need to run an election on any of them.
* (nmbd_packets.c)
*/
- if(listen_for_packets(run_election)) {
+ if (listen_for_packets(msg, run_election)) {
TALLOC_FREE(frame);
return;
}
- /*
- * Handle termination inband.
- */
-
- if (got_sig_term) {
- got_sig_term = 0;
- terminate();
- }
-
/*
* Process all incoming packets
* read above. This calls the success and
if (lp_enhanced_browsing())
sync_all_dmbs(t);
- /*
- * clear the unexpected packet queue
- */
-
- clear_unexpected(t);
-
- /*
- * Reload the services file if we got a sighup.
- */
-
- if(reload_after_sighup) {
- DATA_BLOB blob = data_blob_const(&no_subnets,
- sizeof(no_subnets));
- DEBUG( 0, ( "Got SIGHUP dumping debug info.\n" ) );
- msg_reload_nmbd_services(nmbd_messaging_context(),
- NULL, MSG_SMB_CONF_UPDATED,
- procid_self(), &blob);
-
- if(no_subnets) {
- TALLOC_FREE(frame);
- return;
- }
- reload_after_sighup = 0;
- }
-
/* check for new network interfaces */
- if(reload_interfaces(t)) {
- TALLOC_FREE(frame);
- return;
- }
+ reload_interfaces(t);
/* free up temp memory */
TALLOC_FREE(frame);
ClientNMB = 0;
}
+ if (ClientNMB == -1) {
+ return false;
+ }
+
ClientDGRAM = open_socket_in(SOCK_DGRAM, DGRAM_PORT,
3, &ss,
true);
- if (ClientNMB == -1) {
+ if (ClientDGRAM == -1) {
+ if (ClientNMB != 0) {
+ close(ClientNMB);
+ }
return false;
}
static bool Fork = true;
static bool no_process_group;
static bool log_stdout;
- pstring logfile;
poptContext pc;
- static char *p_lmhosts = dyn_LMHOSTSFILE;
+ char *p_lmhosts = NULL;
int opt;
enum {
OPT_DAEMON = 1000,
{"foreground", 'F', POPT_ARG_NONE, NULL, OPT_FORK, "Run daemon in foreground (for daemontools & etc)" },
{"no-process-group", 0, POPT_ARG_NONE, NULL, OPT_NO_PROCESS_GROUP, "Don't create a new process group" },
{"log-stdout", 'S', POPT_ARG_NONE, NULL, OPT_LOG_STDOUT, "Log to stdout" },
- {"hosts", 'H', POPT_ARG_STRING, &p_lmhosts, 'H', "Load a netbios hosts file"},
- {"port", 'p', POPT_ARG_INT, &global_nmb_port, NMB_PORT, "Listen on the specified port" },
+ {"hosts", 'H', POPT_ARG_STRING, &p_lmhosts, 0, "Load a netbios hosts file"},
+ {"port", 'p', POPT_ARG_INT, &global_nmb_port, 0, "Listen on the specified port" },
POPT_COMMON_SAMBA
{ NULL }
};
- TALLOC_CTX *frame = talloc_stackframe(); /* Setup tos. */
+ TALLOC_CTX *frame;
+ NTSTATUS status;
+
+ /*
+ * Do this before any other talloc operation
+ */
+ talloc_enable_null_tracking();
+ frame = talloc_stackframe();
+
+ setup_logging(argv[0], DEBUG_DEFAULT_STDOUT);
load_case_tables();
poptFreeContext(pc);
global_in_nmbd = true;
-
+
StartupTime = time(NULL);
-
+
sys_srandom(time(NULL) ^ sys_getpid());
-
+
if (!override_logfile) {
- slprintf(logfile, sizeof(logfile)-1, "%s/log.nmbd", dyn_LOGFILEBASE);
- lp_set_logfile(logfile);
+ char *lfile = NULL;
+ if (asprintf(&lfile, "%s/log.nmbd", get_dyn_LOGFILEBASE()) < 0) {
+ exit(1);
+ }
+ lp_set_logfile(lfile);
+ SAFE_FREE(lfile);
}
-
- fault_setup((void (*)(void *))fault_continue );
- dump_core_setup("nmbd");
-
+
+ fault_setup();
+ dump_core_setup("nmbd", lp_logfile());
+
/* POSIX demands that signals are inherited. If the invoking process has
* these signals masked, we will have problems, as we won't receive them. */
BlockSignals(False, SIGHUP);
BlockSignals(False, SIGUSR1);
BlockSignals(False, SIGTERM);
-
- CatchSignal( SIGHUP, SIGNAL_CAST sig_hup );
- CatchSignal( SIGTERM, SIGNAL_CAST sig_term );
-
+
#if defined(SIGFPE)
/* we are never interested in SIGFPE */
BlockSignals(True,SIGFPE);
exit(1);
}
- setup_logging( argv[0], log_stdout );
+ if (log_stdout) {
+ setup_logging(argv[0], DEBUG_STDOUT);
+ } else {
+ setup_logging( argv[0], DEBUG_FILE);
+ }
reopen_logs();
- DEBUG(0,("nmbd version %s started.\n", SAMBA_VERSION_STRING));
+ DEBUG(0,("nmbd version %s started.\n", samba_version_string()));
DEBUGADD(0,("%s\n", COPYRIGHT_STARTUP_MESSAGE));
+ if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
+ DEBUG(0, ("error opening config file '%s'\n", get_dyn_CONFIGFILE()));
+ exit(1);
+ }
+
+ if (nmbd_messaging_context() == NULL) {
+ return 1;
+ }
+
if ( !reload_nmbd_services(False) )
return(-1);
DEBUG(0,("standard input is not a socket, assuming -D option\n"));
is_daemon = True;
}
-
+
if (is_daemon && !opt_interactive) {
DEBUG( 2, ( "Becoming a daemon.\n" ) );
- become_daemon(Fork, no_process_group);
+ become_daemon(Fork, no_process_group, log_stdout);
}
#if HAVE_SETPGID
/* Setup the async dns. We do it here so it doesn't have all the other
stuff initialised and thus chewing memory and sockets */
if(lp_we_are_a_wins_server() && lp_dns_proxy()) {
- start_async_dns();
+ start_async_dns(nmbd_messaging_context());
}
#endif
- if (!directory_exist(lp_lockdir(), NULL)) {
+ if (!directory_exist(lp_lockdir())) {
mkdir(lp_lockdir(), 0755);
}
pidfile_create("nmbd");
+
+ status = reinit_after_fork(nmbd_messaging_context(),
+ nmbd_event_context(),
+ false);
+
+ if (!NT_STATUS_IS_OK(status)) {
+ DEBUG(0,("reinit_after_fork() failed\n"));
+ exit(1);
+ }
+
+ if (!nmbd_setup_sig_term_handler(nmbd_messaging_context()))
+ exit(1);
+ if (!nmbd_setup_sig_hup_handler(nmbd_messaging_context()))
+ exit(1);
+
+ /* get broadcast messages */
+
+ if (!serverid_register(procid_self(),
+ FLAG_MSG_GENERAL |
+ FLAG_MSG_NMBD |
+ FLAG_MSG_DBWRAP)) {
+ DEBUG(1, ("Could not register myself in serverid.tdb\n"));
+ exit(1);
+ }
+
messaging_register(nmbd_messaging_context(), NULL,
MSG_FORCE_ELECTION, nmbd_message_election);
#if 0
}
/* Load in any static local names. */
- load_lmhosts_file(p_lmhosts);
- DEBUG(3,("Loaded hosts file %s\n", p_lmhosts));
+ if (p_lmhosts) {
+ set_dyn_LMHOSTSFILE(p_lmhosts);
+ }
+ load_lmhosts_file(get_dyn_LMHOSTSFILE());
+ DEBUG(3,("Loaded hosts file %s\n", get_dyn_LMHOSTSFILE()));
/* If we are acting as a WINS server, initialise data structures. */
if( !initialise_wins() ) {
exit(1);
}
- /* We can only take signals in the select. */
- BlockSignals( True, SIGTERM );
+ if (!initialize_nmbd_proxy_logon()) {
+ DEBUG(0,("ERROR: Failed setup nmbd_proxy_logon.\n"));
+ kill_async_dns_child();
+ exit(1);
+ }
+
+ if (!nmbd_init_packet_server()) {
+ kill_async_dns_child();
+ exit(1);
+ }
TALLOC_FREE(frame);
- process();
+ process(nmbd_messaging_context());
- if (dbf)
- x_fclose(dbf);
kill_async_dns_child();
return(0);
}