2 Unix SMB/CIFS implementation.
3 NBT netbios routines and daemon - version 2
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 1997-2002
6 Copyright (C) Jelmer Vernooij 2002,2003 (Conversion to popt)
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/>.
27 int global_nmb_port = -1;
29 extern BOOL rescan_listen_set;
30 extern struct in_addr loopback_ip;
31 extern BOOL global_in_nmbd;
33 extern BOOL override_logfile;
35 /* have we found LanMan clients yet? */
36 BOOL found_lm_clients = False;
38 /* what server type are we currently */
40 time_t StartupTime = 0;
42 struct event_context *nmbd_event_context(void)
44 static struct event_context *ctx;
46 if (!ctx && !(ctx = event_context_init(NULL))) {
47 smb_panic("Could not init nmbd event context");
52 struct messaging_context *nmbd_messaging_context(void)
54 static struct messaging_context *ctx;
56 if (!ctx && !(ctx = messaging_init(NULL, server_id_self(),
57 nmbd_event_context()))) {
58 smb_panic("Could not init nmbd messaging context");
63 /**************************************************************************** **
64 Handle a SIGTERM in band.
65 **************************************************************************** */
67 static void terminate(void)
69 DEBUG(0,("Got SIGTERM: going down...\n"));
71 /* Write out wins.dat file if samba is a WINS server */
72 wins_write_database(0,False);
74 /* Remove all SELF registered names from WINS */
77 /* Announce all server entries as 0 time-to-live, 0 type. */
78 announce_my_servers_removed();
80 /* If there was an async dns child - kill it. */
81 kill_async_dns_child();
86 /**************************************************************************** **
87 Handle a SHUTDOWN message from smbcontrol.
88 **************************************************************************** */
90 static void nmbd_terminate(struct messaging_context *msg,
93 struct server_id server_id,
99 /**************************************************************************** **
100 Catch a SIGTERM signal.
101 **************************************************************************** */
103 static SIG_ATOMIC_T got_sig_term;
105 static void sig_term(int sig)
108 sys_select_signal(SIGTERM);
111 /**************************************************************************** **
112 Catch a SIGHUP signal.
113 **************************************************************************** */
115 static SIG_ATOMIC_T reload_after_sighup;
117 static void sig_hup(int sig)
119 reload_after_sighup = 1;
120 sys_select_signal(SIGHUP);
123 /**************************************************************************** **
124 Possibly continue after a fault.
125 **************************************************************************** */
127 static void fault_continue(void)
132 /**************************************************************************** **
133 Expire old names from the namelist and server list.
134 **************************************************************************** */
136 static void expire_names_and_servers(time_t t)
138 static time_t lastrun = 0;
142 if ( t < (lastrun + 5) )
147 * Expire any timed out names on all the broadcast
148 * subnets and those registered with the WINS server.
149 * (nmbd_namelistdb.c)
155 * Go through all the broadcast subnets and for each
156 * workgroup known on that subnet remove any expired
157 * server names. If a workgroup has an empty serverlist
158 * and has itself timed out then remove the workgroup.
159 * (nmbd_workgroupdb.c)
162 expire_workgroups_and_servers(t);
165 /************************************************************************** **
166 Reload the list of network interfaces.
167 ************************************************************************** */
169 static BOOL reload_interfaces(time_t t)
173 struct subnet_record *subrec;
175 if (t && ((t - lastt) < NMBD_INTERFACES_RELOAD)) return False;
178 if (!interfaces_changed()) return False;
180 /* the list of probed interfaces has changed, we may need to add/remove
184 /* find any interfaces that need adding */
185 for (n=iface_count() - 1; n >= 0; n--) {
186 struct interface *iface = get_interface(n);
189 DEBUG(2,("reload_interfaces: failed to get interface %d\n", n));
194 * We don't want to add a loopback interface, in case
195 * someone has added 127.0.0.1 for smbd, nmbd needs to
196 * ignore it here. JRA.
199 if (ip_equal(iface->ip, loopback_ip)) {
200 DEBUG(2,("reload_interfaces: Ignoring loopback interface %s\n", inet_ntoa(iface->ip)));
204 for (subrec=subnetlist; subrec; subrec=subrec->next) {
205 if (ip_equal(iface->ip, subrec->myip) &&
206 ip_equal(iface->nmask, subrec->mask_ip)) break;
210 /* it wasn't found! add it */
211 DEBUG(2,("Found new interface %s\n",
212 inet_ntoa(iface->ip)));
213 subrec = make_normal_subnet(iface);
215 register_my_workgroup_one_subnet(subrec);
219 /* find any interfaces that need deleting */
220 for (subrec=subnetlist; subrec; subrec=subrec->next) {
221 for (n=iface_count() - 1; n >= 0; n--) {
222 struct interface *iface = get_interface(n);
223 if (ip_equal(iface->ip, subrec->myip) &&
224 ip_equal(iface->nmask, subrec->mask_ip)) break;
227 /* oops, an interface has disapeared. This is
228 tricky, we don't dare actually free the
229 interface as it could be being used, so
230 instead we just wear the memory leak and
231 remove it from the list of interfaces without
233 DEBUG(2,("Deleting dead interface %s\n",
234 inet_ntoa(subrec->myip)));
235 close_subnet(subrec);
239 rescan_listen_set = True;
241 /* We need to shutdown if there are no subnets... */
242 if (FIRST_SUBNET == NULL) {
243 DEBUG(0,("reload_interfaces: No subnets to listen to. Shutting down...\n"));
249 /**************************************************************************** **
250 Reload the services file.
251 **************************************************************************** */
253 static BOOL reload_nmbd_services(BOOL test)
257 set_remote_machine_name("nmbd", False);
261 pstrcpy( fname,lp_configfile());
262 if (file_exist(fname,NULL) && !strcsequal(fname,dyn_CONFIGFILE)) {
263 pstrcpy(dyn_CONFIGFILE,fname);
268 if ( test && !lp_file_list_changed() )
271 ret = lp_load( dyn_CONFIGFILE, True , False, False, True);
273 /* perhaps the config filename is now set */
275 DEBUG( 3, ( "services not loaded\n" ) );
276 reload_nmbd_services( True );
282 /**************************************************************************** **
283 * React on 'smbcontrol nmbd reload-config' in the same way as to SIGHUP
284 * We use buf here to return BOOL result to process() when reload_interfaces()
285 * detects that there are no subnets.
286 **************************************************************************** */
288 static void msg_reload_nmbd_services(struct messaging_context *msg,
291 struct server_id server_id,
294 write_browse_list( 0, True );
295 dump_all_namelists();
296 reload_nmbd_services( True );
300 /* We were called from process() */
301 /* If reload_interfaces() returned True */
302 /* we need to shutdown if there are no subnets... */
303 /* pass this info back to process() */
304 *((BOOL*)data->data) = reload_interfaces(0);
308 static void msg_nmbd_send_packet(struct messaging_context *msg,
311 struct server_id src,
314 struct packet_struct *p = (struct packet_struct *)data->data;
315 struct subnet_record *subrec;
316 struct in_addr *local_ip;
318 DEBUG(10, ("Received send_packet from %d\n", procid_to_pid(&src)));
320 if (data->length != sizeof(struct packet_struct)) {
321 DEBUG(2, ("Discarding invalid packet length from %d\n",
322 procid_to_pid(&src)));
326 if ((p->packet_type != NMB_PACKET) &&
327 (p->packet_type != DGRAM_PACKET)) {
328 DEBUG(2, ("Discarding invalid packet type from %d: %d\n",
329 procid_to_pid(&src), p->packet_type));
333 local_ip = iface_ip(p->ip);
335 if (local_ip == NULL) {
336 DEBUG(2, ("Could not find ip for packet from %d\n",
337 procid_to_pid(&src)));
341 subrec = FIRST_SUBNET;
343 p->fd = (p->packet_type == NMB_PACKET) ?
344 subrec->nmb_sock : subrec->dgram_sock;
346 for (subrec = FIRST_SUBNET; subrec != NULL;
347 subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
348 if (ip_equal(*local_ip, subrec->myip)) {
349 p->fd = (p->packet_type == NMB_PACKET) ?
350 subrec->nmb_sock : subrec->dgram_sock;
355 if (p->packet_type == DGRAM_PACKET) {
357 p->packet.dgram.header.source_ip.s_addr = local_ip->s_addr;
358 p->packet.dgram.header.source_port = 138;
364 /**************************************************************************** **
365 The main select loop.
366 **************************************************************************** */
368 static void process(void)
374 time_t t = time(NULL);
376 /* Check for internal messages */
378 message_dispatch(nmbd_messaging_context());
381 * Check all broadcast subnets to see if
382 * we need to run an election on any of them.
386 run_election = check_elections();
389 * Read incoming UDP packets.
393 if(listen_for_packets(run_election))
397 * Handle termination inband.
406 * Process all incoming packets
407 * read above. This calls the success and
408 * failure functions registered when response
409 * packets arrrive, and also deals with request
410 * packets from other sources.
417 * Run any elections - initiate becoming
418 * a local master browser if we have won.
425 * Send out any broadcast announcements
426 * of our server names. This also announces
427 * the workgroup name if we are a local
429 * (nmbd_sendannounce.c)
432 announce_my_server_names(t);
435 * Send out any LanMan broadcast announcements
436 * of our server names.
437 * (nmbd_sendannounce.c)
440 announce_my_lm_server_names(t);
443 * If we are a local master browser, periodically
444 * announce ourselves to the domain master browser.
445 * This also deals with syncronising the domain master
446 * browser server lists with ourselves as a local
448 * (nmbd_sendannounce.c)
451 announce_myself_to_domain_master_browser(t);
454 * Fullfill any remote announce requests.
455 * (nmbd_sendannounce.c)
461 * Fullfill any remote browse sync announce requests.
462 * (nmbd_sendannounce.c)
465 browse_sync_remote(t);
468 * Scan the broadcast subnets, and WINS client
469 * namelists and refresh any that need refreshing.
476 * Scan the subnet namelists and server lists and
477 * expire thos that have timed out.
481 expire_names_and_servers(t);
484 * Write out a snapshot of our current browse list into
485 * the browse.dat file. This is used by smbd to service
486 * incoming NetServerEnum calls - used to synchronise
487 * browse lists over subnets.
488 * (nmbd_serverlistdb.c)
491 write_browse_list(t, False);
494 * If we are a domain master browser, we have a list of
495 * local master browsers we should synchronise browse
496 * lists with (these are added by an incoming local
497 * master browser announcement packet). Expire any of
498 * these that are no longer current, and pull the server
499 * lists from each of these known local master browsers.
500 * (nmbd_browsesync.c)
503 dmb_expire_and_sync_browser_lists(t);
506 * Check that there is a local master browser for our
507 * workgroup for all our broadcast subnets. If one
508 * is not found, start an election (which we ourselves
509 * may or may not participate in, depending on the
510 * setting of the 'local master' parameter.
514 check_master_browser_exists(t);
517 * If we are configured as a logon server, attempt to
518 * register the special NetBIOS names to become such
519 * (WORKGROUP<1c> name) on all broadcast subnets and
520 * with the WINS server (if used). If we are configured
521 * to become a domain master browser, attempt to register
522 * the special NetBIOS name (WORKGROUP<1b> name) to
524 * (nmbd_become_dmb.c)
530 * If we are a WINS server, do any timer dependent
531 * processing required.
532 * (nmbd_winsserver.c)
535 initiate_wins_processing(t);
538 * If we are a domain master browser, attempt to contact the
539 * WINS server to get a list of all known WORKGROUPS/DOMAINS.
540 * This will only work to a Samba WINS server.
541 * (nmbd_browsesync.c)
544 if (lp_enhanced_browsing())
545 collect_all_workgroup_names_from_wins_server(t);
548 * Go through the response record queue and time out or re-transmit
549 * and expired entries.
553 retransmit_or_expire_response_records(t);
556 * check to see if any remote browse sync child processes have completed
559 sync_check_completion();
562 * regularly sync with any other DMBs we know about
565 if (lp_enhanced_browsing())
569 * clear the unexpected packet queue
575 * Reload the services file if we got a sighup.
578 if(reload_after_sighup) {
579 DATA_BLOB blob = data_blob_const(&no_subnets,
581 DEBUG( 0, ( "Got SIGHUP dumping debug info.\n" ) );
582 msg_reload_nmbd_services(nmbd_messaging_context(),
583 NULL, MSG_SMB_CONF_UPDATED,
584 procid_self(), &blob);
588 reload_after_sighup = 0;
591 /* check for new network interfaces */
593 if(reload_interfaces(t))
596 /* free up temp memory */
601 /**************************************************************************** **
602 Open the socket communication.
603 **************************************************************************** */
605 static BOOL open_sockets(enum smb_server_mode server_mode, int port)
608 * The sockets opened here will be used to receive broadcast
609 * packets *only*. Interface specific sockets are opened in
610 * make_subnet() in namedbsubnet.c. Thus we bind to the
611 * address "0.0.0.0". The parameter 'socket address' is
615 if ( server_mode == SERVER_MODE_INETD ) {
618 ClientNMB = open_socket_in(SOCK_DGRAM, port,
619 0, interpret_addr(lp_socket_address()),
623 ClientDGRAM = open_socket_in(SOCK_DGRAM, DGRAM_PORT,
624 3, interpret_addr(lp_socket_address()),
627 if ( ClientNMB == -1 )
630 /* we are never interested in SIGPIPE */
631 BlockSignals(True,SIGPIPE);
633 set_socket_options( ClientNMB, "SO_BROADCAST" );
634 set_socket_options( ClientDGRAM, "SO_BROADCAST" );
636 /* Ensure we're non-blocking. */
637 set_blocking( ClientNMB, False);
638 set_blocking( ClientDGRAM, False);
640 DEBUG( 3, ( "open_sockets: Broadcast sockets opened.\n" ) );
644 /**************************************************************************** **
646 **************************************************************************** */
647 int main(int argc, const char *argv[])
651 const char *p_lmhosts = dyn_LMHOSTSFILE;
652 BOOL no_process_group = False;
653 BOOL log_stdout = False;
654 enum smb_server_mode server_mode = SERVER_MODE_DAEMON;
657 struct poptOption long_options[] = {
659 {"daemon", 'D', POPT_ARG_VAL, &server_mode, SERVER_MODE_DAEMON,
660 "Become a daemon(default)" },
661 {"interactive", 'i', POPT_ARG_VAL, &server_mode,
662 SERVER_MODE_INTERACTIVE, "Run interactive (not a daemon)" },
663 {"foreground", 'F', POPT_ARG_VAL, &server_mode,
664 SERVER_MODE_FOREGROUND, "Run daemon in foreground (for daemontools & etc)" },
665 {"no-process-group", 0, POPT_ARG_VAL, &no_process_group, True, "Don't create a new process group" },
666 {"log-stdout", 'S', POPT_ARG_VAL, &log_stdout, True, "Log to stdout" },
667 {"hosts", 'H', POPT_ARG_STRING, &p_lmhosts, 'H', "Load a netbios hosts file"},
668 {"port", 'p', POPT_ARG_INT, &global_nmb_port, NMB_PORT, "Listen on the specified port" },
675 global_nmb_port = NMB_PORT;
677 pc = poptGetContext("nmbd", argc, argv, long_options, 0);
678 while ((opt = poptGetNextOpt(pc)) != -1) {
681 d_fprintf(stderr, "\nInvalid option %s: %s\n",
682 poptBadOption(pc, 0), poptStrerror(opt));
688 global_in_nmbd = True;
690 StartupTime = time(NULL);
692 sys_srandom(time(NULL) ^ sys_getpid());
694 if (!override_logfile) {
695 slprintf(logfile, sizeof(logfile)-1, "%s/log.nmbd", dyn_LOGFILEBASE);
696 lp_set_logfile(logfile);
699 fault_setup((void (*)(void *))fault_continue );
700 dump_core_setup("nmbd");
702 /* POSIX demands that signals are inherited. If the invoking process has
703 * these signals masked, we will have problems, as we won't receive them. */
704 BlockSignals(False, SIGHUP);
705 BlockSignals(False, SIGUSR1);
706 BlockSignals(False, SIGTERM);
708 CatchSignal( SIGHUP, SIGNAL_CAST sig_hup );
709 CatchSignal( SIGTERM, SIGNAL_CAST sig_term );
712 /* we are never interested in SIGFPE */
713 BlockSignals(True,SIGFPE);
716 /* We no longer use USR2... */
718 BlockSignals(True, SIGUSR2);
721 if (server_mode == SERVER_MODE_INTERACTIVE) {
725 if (log_stdout && server_mode == SERVER_MODE_DAEMON) {
726 DEBUG(0,("ERROR: Can't log to stdout (-S) unless daemon is in foreground (-F) or interactive (-i)\n"));
730 setup_logging( argv[0], log_stdout );
734 DEBUG( 0, ( "Netbios nameserver version %s started.\n", SAMBA_VERSION_STRING) );
735 DEBUGADD( 0, ( "%s\n", COPYRIGHT_STARTUP_MESSAGE ) );
737 if ( !reload_nmbd_services(False) )
743 reload_nmbd_services( True );
745 if (strequal(lp_workgroup(),"*")) {
746 DEBUG(0,("ERROR: a workgroup name of * is no longer supported\n"));
752 if (is_a_socket(0)) {
753 if (server_mode == SERVER_MODE_DAEMON) {
754 DEBUG(0,("standard input is a socket, "
755 "assuming -F option\n"));
757 server_mode = SERVER_MODE_INETD;
760 if (server_mode == SERVER_MODE_DAEMON) {
761 DEBUG( 2, ( "Becoming a daemon.\n" ) );
762 become_daemon(True, no_process_group);
763 } else if (server_mode == SERVER_MODE_FOREGROUND) {
764 become_daemon(False, no_process_group);
769 * If we're interactive we want to set our own process group for
772 if (server_mode == SERVER_MODE_INTERACTIVE && !no_process_group)
773 setpgid( (pid_t)0, (pid_t)0 );
776 if (nmbd_messaging_context() == NULL) {
781 /* Setup the async dns. We do it here so it doesn't have all the other
782 stuff initialised and thus chewing memory and sockets */
783 if(lp_we_are_a_wins_server() && lp_dns_proxy()) {
788 if (!directory_exist(lp_lockdir(), NULL)) {
789 mkdir(lp_lockdir(), 0755);
792 pidfile_create("nmbd");
793 messaging_register(nmbd_messaging_context(), NULL,
794 MSG_FORCE_ELECTION, nmbd_message_election);
796 /* Until winsrepl is done. */
797 messaging_register(nmbd_messaging_context(), NULL,
798 MSG_WINS_NEW_ENTRY, nmbd_wins_new_entry);
800 messaging_register(nmbd_messaging_context(), NULL,
801 MSG_SHUTDOWN, nmbd_terminate);
802 messaging_register(nmbd_messaging_context(), NULL,
803 MSG_SMB_CONF_UPDATED, msg_reload_nmbd_services);
804 messaging_register(nmbd_messaging_context(), NULL,
805 MSG_SEND_PACKET, msg_nmbd_send_packet);
809 DEBUG( 3, ( "Opening sockets %d\n", global_nmb_port ) );
811 if ( !open_sockets( server_mode, global_nmb_port ) ) {
812 kill_async_dns_child();
816 /* Determine all the IP addresses we have. */
819 /* Create an nmbd subnet record for each of the above. */
820 if( False == create_subnets() ) {
821 DEBUG(0,("ERROR: Failed when creating subnet lists. Exiting.\n"));
822 kill_async_dns_child();
826 /* Load in any static local names. */
827 load_lmhosts_file(p_lmhosts);
828 DEBUG(3,("Loaded hosts file %s\n", p_lmhosts));
830 /* If we are acting as a WINS server, initialise data structures. */
831 if( !initialise_wins() ) {
832 DEBUG( 0, ( "nmbd: Failed when initialising WINS server.\n" ) );
833 kill_async_dns_child();
838 * Register nmbd primary workgroup and nmbd names on all
839 * the broadcast subnets, and on the WINS server (if specified).
840 * Also initiate the startup of our primary workgroup (start
841 * elections if we are setup as being able to be a local
845 if( False == register_my_workgroup_and_names() ) {
846 DEBUG(0,("ERROR: Failed when creating my my workgroup. Exiting.\n"));
847 kill_async_dns_child();
851 /* We can only take signals in the select. */
852 BlockSignals( True, SIGTERM );
858 kill_async_dns_child();