2 Unix SMB/Netbios implementation.
3 NBT netbios routines and daemon - version 2
4 Copyright (C) Andrew Tridgell 1994-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 14 jan 96: lkcl@pires.co.uk
23 added multiple workgroup domain master support
31 int global_nmb_port = -1;
33 extern pstring global_myname;
34 extern fstring global_myworkgroup;
35 extern char **my_netbios_names;
37 extern BOOL global_in_nmbd;
39 /* are we running as a daemon ? */
40 static BOOL is_daemon = False;
42 /* have we found LanMan clients yet? */
43 BOOL found_lm_clients = False;
45 /* what server type are we currently */
47 time_t StartupTime = 0;
49 /**************************************************************************** **
51 **************************************************************************** */
52 static void sig_term(int sig)
54 BlockSignals(True,SIGTERM);
56 DEBUG(0,("Got SIGTERM: going down...\n"));
58 /* Write out wins.dat file if samba is a WINS server */
59 wins_write_database(False);
61 /* Remove all SELF registered names. */
64 /* Announce all server entries as 0 time-to-live, 0 type. */
65 announce_my_servers_removed();
67 /* If there was an async dns child - kill it. */
68 kill_async_dns_child();
74 /**************************************************************************** **
76 **************************************************************************** */
77 static VOLATILE sig_atomic_t reload_after_sighup = False;
79 static void sig_hup(int sig)
81 BlockSignals( True, SIGHUP );
83 DEBUG( 0, ( "Got SIGHUP dumping debug info.\n" ) );
85 write_browse_list( 0, True );
89 reload_after_sighup = True;
91 BlockSignals(False,SIGHUP);
97 /**************************************************************************** **
98 prepare to dump a core file - carefully!
99 **************************************************************************** */
100 static BOOL dump_core(void)
104 pstrcpy( dname, lp_logfile() );
105 if ((p=strrchr_m(dname,'/')))
107 pstrcat( dname, "/corefiles" );
108 mkdir( dname, 0700 );
109 sys_chown( dname, getuid(), getgid() );
110 chmod( dname, 0700 );
115 #ifdef HAVE_GETRLIMIT
119 getrlimit( RLIMIT_CORE, &rlp );
120 rlp.rlim_cur = MAX( 4*1024*1024, rlp.rlim_cur );
121 setrlimit( RLIMIT_CORE, &rlp );
122 getrlimit( RLIMIT_CORE, &rlp );
123 DEBUG( 3, ( "Core limits now %d %d\n", (int)rlp.rlim_cur, (int)rlp.rlim_max ) );
129 DEBUG(0,("Dumping core in %s\n",dname));
136 /**************************************************************************** **
137 possibly continue after a fault
138 **************************************************************************** */
139 static void fault_continue(void)
144 } /* fault_continue */
146 /**************************************************************************** **
147 expire old names from the namelist and server list
148 **************************************************************************** */
149 static void expire_names_and_servers(time_t t)
151 static time_t lastrun = 0;
155 if ( t < (lastrun + 5) )
160 * Expire any timed out names on all the broadcast
161 * subnets and those registered with the WINS server.
162 * (nmbd_namelistdb.c)
167 * Go through all the broadcast subnets and for each
168 * workgroup known on that subnet remove any expired
169 * server names. If a workgroup has an empty serverlist
170 * and has itself timed out then remove the workgroup.
171 * (nmbd_workgroupdb.c)
173 expire_workgroups_and_servers(t);
174 } /* expire_names_and_servers */
177 /************************************************************************** **
178 reload the list of network interfaces
179 ************************************************************************** */
180 static BOOL reload_interfaces(time_t t)
184 struct subnet_record *subrec;
185 extern BOOL rescan_listen_set;
186 extern struct in_addr loopback_ip;
188 if (t && ((t - lastt) < NMBD_INTERFACES_RELOAD)) return False;
191 if (!interfaces_changed()) return False;
193 /* the list of probed interfaces has changed, we may need to add/remove
197 /* find any interfaces that need adding */
198 for (n=iface_count() - 1; n >= 0; n--) {
199 struct interface *iface = get_interface(n);
202 * We don't want to add a loopback interface, in case
203 * someone has added 127.0.0.1 for smbd, nmbd needs to
204 * ignore it here. JRA.
207 if (ip_equal(iface->ip, loopback_ip)) {
208 DEBUG(2,("reload_interfaces: Ignoring loopback interface %s\n", inet_ntoa(iface->ip)));
212 for (subrec=subnetlist; subrec; subrec=subrec->next) {
213 if (ip_equal(iface->ip, subrec->myip) &&
214 ip_equal(iface->nmask, subrec->mask_ip)) break;
218 /* it wasn't found! add it */
219 DEBUG(2,("Found new interface %s\n",
220 inet_ntoa(iface->ip)));
221 subrec = make_normal_subnet(iface);
222 if (subrec) register_my_workgroup_one_subnet(subrec);
226 /* find any interfaces that need deleting */
227 for (subrec=subnetlist; subrec; subrec=subrec->next) {
228 for (n=iface_count() - 1; n >= 0; n--) {
229 struct interface *iface = get_interface(n);
230 if (ip_equal(iface->ip, subrec->myip) &&
231 ip_equal(iface->nmask, subrec->mask_ip)) break;
234 /* oops, an interface has disapeared. This is
235 tricky, we don't dare actually free the
236 interface as it could be being used, so
237 instead we just wear the memory leak and
238 remove it from the list of interfaces without
240 DEBUG(2,("Deleting dead interface %s\n",
241 inet_ntoa(subrec->myip)));
242 close_subnet(subrec);
246 rescan_listen_set = True;
248 /* We need to shutdown if there are no subnets... */
249 if (FIRST_SUBNET == NULL) {
250 DEBUG(0,("reload_interfaces: No subnets to listen to. Shutting down...\n"));
258 /**************************************************************************** **
259 reload the services file
260 **************************************************************************** */
261 static BOOL reload_nmbd_services(BOOL test)
264 extern fstring remote_machine;
266 fstrcpy( remote_machine, "nmbd" );
271 pstrcpy( fname,lp_configfile());
272 if (file_exist(fname,NULL) && !strcsequal(fname,dyn_CONFIGFILE))
274 pstrcpy(dyn_CONFIGFILE,fname);
279 if ( test && !lp_file_list_changed() )
282 ret = lp_load( dyn_CONFIGFILE, True , False, False);
284 /* perhaps the config filename is now set */
287 DEBUG( 3, ( "services not loaded\n" ) );
288 reload_nmbd_services( True );
291 /* Do a sanity check for a misconfigured nmbd */
292 if( lp_wins_support() && wins_srv_count() )
296 dbgtext( "ERROR: 'wins support = true' and 'wins server = <server>'\n" );
297 dbgtext( "are conflicting settings. nmbd aborting.\n" );
303 } /* reload_nmbd_services */
305 /**************************************************************************** **
306 The main select loop.
307 **************************************************************************** */
308 static void process(void)
314 time_t t = time(NULL);
316 /* check for internal messages */
320 * Check all broadcast subnets to see if
321 * we need to run an election on any of them.
324 run_election = check_elections();
327 * Read incoming UDP packets.
330 if(listen_for_packets(run_election))
334 * Process all incoming packets
335 * read above. This calls the success and
336 * failure functions registered when response
337 * packets arrrive, and also deals with request
338 * packets from other sources.
344 * Run any elections - initiate becoming
345 * a local master browser if we have won.
351 * Send out any broadcast announcements
352 * of our server names. This also announces
353 * the workgroup name if we are a local
355 * (nmbd_sendannounce.c)
357 announce_my_server_names(t);
360 * Send out any LanMan broadcast announcements
361 * of our server names.
362 * (nmbd_sendannounce.c)
364 announce_my_lm_server_names(t);
367 * If we are a local master browser, periodically
368 * announce ourselves to the domain master browser.
369 * This also deals with syncronising the domain master
370 * browser server lists with ourselves as a local
372 * (nmbd_sendannounce.c)
374 announce_myself_to_domain_master_browser(t);
377 * Fullfill any remote announce requests.
378 * (nmbd_sendannounce.c)
383 * Fullfill any remote browse sync announce requests.
384 * (nmbd_sendannounce.c)
386 browse_sync_remote(t);
389 * Scan the broadcast subnets, and WINS client
390 * namelists and refresh any that need refreshing.
396 * Scan the subnet namelists and server lists and
397 * expire thos that have timed out.
400 expire_names_and_servers(t);
403 * Write out a snapshot of our current browse list into
404 * the browse.dat file. This is used by smbd to service
405 * incoming NetServerEnum calls - used to synchronise
406 * browse lists over subnets.
407 * (nmbd_serverlistdb.c)
409 write_browse_list(t, False);
412 * If we are a domain master browser, we have a list of
413 * local master browsers we should synchronise browse
414 * lists with (these are added by an incoming local
415 * master browser announcement packet). Expire any of
416 * these that are no longer current, and pull the server
417 * lists from each of these known local master browsers.
418 * (nmbd_browsesync.c)
420 dmb_expire_and_sync_browser_lists(t);
423 * Check that there is a local master browser for our
424 * workgroup for all our broadcast subnets. If one
425 * is not found, start an election (which we ourselves
426 * may or may not participate in, depending on the
427 * setting of the 'local master' parameter.
430 check_master_browser_exists(t);
433 * If we are configured as a logon server, attempt to
434 * register the special NetBIOS names to become such
435 * (WORKGROUP<1c> name) on all broadcast subnets and
436 * with the WINS server (if used). If we are configured
437 * to become a domain master browser, attempt to register
438 * the special NetBIOS name (WORKGROUP<1b> name) to
440 * (nmbd_become_dmb.c)
445 * If we are a WINS server, do any timer dependent
446 * processing required.
447 * (nmbd_winsserver.c)
449 initiate_wins_processing(t);
452 * If we are a domain master browser, attempt to contact the
453 * WINS server to get a list of all known WORKGROUPS/DOMAINS.
454 * This will only work to a Samba WINS server.
455 * (nmbd_browsesync.c)
457 if (lp_enhanced_browsing()) {
458 collect_all_workgroup_names_from_wins_server(t);
462 * Go through the response record queue and time out or re-transmit
463 * and expired entries.
466 retransmit_or_expire_response_records(t);
469 * check to see if any remote browse sync child processes have completed
471 sync_check_completion();
474 * regularly sync with any other DMBs we know about
476 if (lp_enhanced_browsing()) {
481 * clear the unexpected packet queue
486 * Reload the services file if we got a sighup.
489 if(reload_after_sighup) {
490 reload_nmbd_services( True );
492 if(reload_interfaces(0))
494 reload_after_sighup = False;
497 /* check for new network interfaces */
498 if(reload_interfaces(t))
501 /* free up temp memory */
507 /**************************************************************************** **
508 open the socket communication
509 **************************************************************************** */
510 static BOOL open_sockets(BOOL isdaemon, int port)
512 /* The sockets opened here will be used to receive broadcast
513 packets *only*. Interface specific sockets are opened in
514 make_subnet() in namedbsubnet.c. Thus we bind to the
515 address "0.0.0.0". The parameter 'socket address' is
520 ClientNMB = open_socket_in(SOCK_DGRAM, port,0,0,True);
524 ClientDGRAM = open_socket_in(SOCK_DGRAM,DGRAM_PORT,3,0,True);
526 if ( ClientNMB == -1 )
529 /* we are never interested in SIGPIPE */
530 BlockSignals(True,SIGPIPE);
532 set_socket_options( ClientNMB, "SO_BROADCAST" );
533 set_socket_options( ClientDGRAM, "SO_BROADCAST" );
535 DEBUG( 3, ( "open_sockets: Broadcast sockets opened.\n" ) );
540 /**************************************************************************** **
541 initialise connect, service and file structs
542 **************************************************************************** */
543 static BOOL init_structs(void)
545 extern fstring local_machine;
552 if (! *global_myname)
554 fstrcpy( global_myname, myhostname() );
555 p = strchr_m( global_myname, '.' );
559 strupper( global_myname );
561 /* Add any NETBIOS name aliases. Ensure that the first entry
562 is equal to global_myname.
564 /* Work out the max number of netbios aliases that we have */
565 ptr = lp_netbios_aliases();
568 for( ; *ptr; namecount++,ptr++ )
570 if ( *global_myname )
573 /* Allocate space for the netbios aliases */
574 my_netbios_names = (char **)malloc( sizeof(char *) * (namecount+1) );
575 if( NULL == my_netbios_names )
577 DEBUG( 0, ( "init_structs: malloc fail.\n" ) );
581 /* Use the global_myname string first */
583 if ( *global_myname )
584 my_netbios_names[namecount++] = global_myname;
586 ptr = lp_netbios_aliases();
591 nbname = strdup(*ptr);
594 DEBUG(0,("init_structs: malloc fail when allocating names.\n"));
598 /* Look for duplicates */
600 for( n=0; n<namecount; n++ )
602 if( 0 == strcmp( nbname, my_netbios_names[n] ) )
606 my_netbios_names[namecount++] = nbname;
614 /* Terminate name list */
615 my_netbios_names[namecount++] = NULL;
617 fstrcpy( local_machine, global_myname );
618 trim_string( local_machine, " ", " " );
619 p = strchr_m( local_machine, ' ' );
622 strlower( local_machine );
624 DEBUG( 5, ("Netbios name list:-\n") );
625 for( n=0; my_netbios_names[n]; n++ )
626 DEBUGADD( 5, ( "my_netbios_names[%d]=\"%s\"\n", n, my_netbios_names[n] ) );
631 /**************************************************************************** **
633 **************************************************************************** */
634 static void usage(char *pname)
637 printf( "Usage: %s [-DaiohV] [-H lmhosts file] [-d debuglevel] [-l log basename]\n", pname );
638 printf( " [-n name] [-p port] [-s configuration file]\n" );
639 printf( "\t-D Become a daemon (default)\n" );
640 printf( "\t-a Append to log file (default)\n" );
641 printf( "\t-i Run interactive (not a daemon)\n" );
642 printf( "\t-o Overwrite log file, don't append\n" );
643 printf( "\t-h Print usage\n" );
644 printf( "\t-V Print version\n" );
645 printf( "\t-H hosts file Load a netbios hosts file\n" );
646 printf( "\t-d debuglevel Set the debuglevel\n" );
647 printf( "\t-l log basename. Basename for log/debug files\n" );
648 printf( "\t-n netbiosname. Primary netbios name\n" );
649 printf( "\t-p port Listen on the specified port\n" );
650 printf( "\t-s configuration file Configuration file name\n" );
655 /**************************************************************************** **
657 **************************************************************************** */
658 int main(int argc,char *argv[])
662 extern BOOL append_log;
663 BOOL opt_interactive = False;
666 append_log = True; /* Default, override with '-o' option. */
668 global_nmb_port = NMB_PORT;
669 global_in_nmbd = True;
671 StartupTime = time(NULL);
673 sys_srandom(time(NULL) ^ sys_getpid());
675 slprintf(logfile, sizeof(logfile)-1, "%s/log.nmbd", dyn_LOGFILEBASE);
676 lp_set_logfile(logfile);
678 /* this is for people who can't start the program correctly */
679 while (argc > 1 && (*argv[1] != '-'))
685 fault_setup((void (*)(void *))fault_continue );
687 /* POSIX demands that signals are inherited. If the invoking process has
688 * these signals masked, we will have problems, as we won't recieve them. */
689 BlockSignals(False, SIGHUP);
690 BlockSignals(False, SIGUSR1);
691 BlockSignals(False, SIGTERM);
693 CatchSignal( SIGHUP, SIGNAL_CAST sig_hup );
694 CatchSignal( SIGTERM, SIGNAL_CAST sig_term );
697 /* we are never interested in SIGFPE */
698 BlockSignals(True,SIGFPE);
701 /* We no longer use USR2... */
703 BlockSignals(True, SIGUSR2);
707 (opt = getopt( argc, argv, "Vaos:T:I:C:bAB:N:Rn:l:d:Dp:hSH:G:f:i" )) )
712 pstrcpy(dyn_CONFIGFILE, optarg);
719 DEBUG(0,("Obsolete option '%c' used\n",opt));
722 opt_interactive = True;
725 pstrcpy(dyn_LMHOSTSFILE, optarg);
728 pstrcpy(global_myname,optarg);
729 strupper(global_myname);
732 slprintf(logfile, sizeof(logfile)-1, "%s/log.nmbd", optarg);
733 lp_set_logfile(logfile);
745 DEBUGLEVEL = atoi(optarg);
748 global_nmb_port = atoi(optarg);
755 printf( "Version %s\n", VERSION );
759 if( !is_a_socket(0) )
761 DEBUG(0,("Incorrect program usage - is the command line correct?\n"));
769 setup_logging( argv[0], opt_interactive );
773 DEBUG( 1, ( "Netbios nameserver version %s started.\n", VERSION ) );
774 DEBUGADD( 1, ( "Copyright Andrew Tridgell 1994-1998\n" ) );
776 if ( !reload_nmbd_services(False) )
782 reload_nmbd_services( True );
784 fstrcpy( global_myworkgroup, lp_workgroup() );
786 if (strequal(global_myworkgroup,"*"))
788 DEBUG(0,("ERROR: a workgroup name of * is no longer supported\n"));
794 if (!is_daemon && !is_a_socket(0))
796 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
800 if (is_daemon && !opt_interactive)
802 DEBUG( 2, ( "Becoming a daemon.\n" ) );
807 /* Setup the async dns. We do it here so it doesn't have all the other
808 stuff initialised and thus chewing memory and sockets */
809 if(lp_we_are_a_wins_server()) {
814 if (!directory_exist(lp_lockdir(), NULL)) {
815 mkdir(lp_lockdir(), 0755);
818 pidfile_create("nmbd");
820 message_register(MSG_FORCE_ELECTION, nmbd_message_election);
822 DEBUG( 3, ( "Opening sockets %d\n", global_nmb_port ) );
824 if ( !open_sockets( is_daemon, global_nmb_port ) )
827 /* Determine all the IP addresses we have. */
830 /* Create an nmbd subnet record for each of the above. */
831 if( False == create_subnets() )
833 DEBUG(0,("ERROR: Failed when creating subnet lists. Exiting.\n"));
837 /* Load in any static local names. */
838 load_lmhosts_file(dyn_LMHOSTSFILE);
839 DEBUG(3,("Loaded hosts file %s\n", dyn_LMHOSTSFILE));
841 /* If we are acting as a WINS server, initialise data structures. */
842 if( !initialise_wins() )
844 DEBUG( 0, ( "nmbd: Failed when initialising WINS server.\n" ) );
849 * Register nmbd primary workgroup and nmbd names on all
850 * the broadcast subnets, and on the WINS server (if specified).
851 * Also initiate the startup of our primary workgroup (start
852 * elections if we are setup as being able to be a local
856 if( False == register_my_workgroup_and_names() )
858 DEBUG(0,("ERROR: Failed when creating my my workgroup. Exiting.\n"));
862 /* We can only take signals in the select. */
863 BlockSignals( True, SIGTERM );