2 Unix SMB/Netbios implementation.
4 NBT netbios routines and daemon - version 2
5 Copyright (C) Andrew Tridgell 1994-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 14 jan 96: lkcl@pires.co.uk
24 added multiple workgroup domain master support
30 extern int DEBUGLEVEL;
32 extern pstring debugf;
33 pstring servicesf = CONFIGFILE;
37 int global_nmb_port = -1;
39 static pstring host_file;
40 extern pstring global_myname;
41 extern fstring global_myworkgroup;
42 extern char **my_netbios_names;
44 extern BOOL global_in_nmbd;
46 /* are we running as a daemon ? */
47 static BOOL is_daemon = False;
49 /* have we found LanMan clients yet? */
50 BOOL found_lm_clients = False;
52 /* what server type are we currently */
54 time_t StartupTime = 0;
56 extern struct in_addr ipzero;
58 /**************************************************************************** **
60 **************************************************************************** */
61 static void sig_term(int sig)
63 BlockSignals(True,SIGTERM);
65 DEBUG(0,("Got SIGTERM: going down...\n"));
67 /* Write out wins.dat file if samba is a WINS server */
68 wins_write_database(False);
70 /* Remove all SELF registered names. */
73 /* Announce all server entries as 0 time-to-live, 0 type. */
74 announce_my_servers_removed();
76 /* If there was an async dns child - kill it. */
77 kill_async_dns_child();
83 /**************************************************************************** **
85 **************************************************************************** */
86 static VOLATILE SIG_ATOMIC_T reload_after_sighup = False;
88 static void sig_hup(int sig)
90 BlockSignals( True, SIGHUP );
92 DEBUG( 0, ( "Got SIGHUP dumping debug info.\n" ) );
94 write_browse_list( 0, True );
98 reload_after_sighup = True;
100 BlockSignals(False,SIGHUP);
106 /**************************************************************************** **
107 prepare to dump a core file - carefully!
108 **************************************************************************** */
109 static BOOL dump_core(void)
113 pstrcpy( dname, debugf );
114 if ((p=strrchr(dname,'/')))
116 pstrcat( dname, "/corefiles" );
117 mkdir( dname, 0700 );
118 sys_chown( dname, getuid(), getgid() );
119 chmod( dname, 0700 );
124 #ifdef HAVE_GETRLIMIT
128 getrlimit( RLIMIT_CORE, &rlp );
129 rlp.rlim_cur = MAX( 4*1024*1024, rlp.rlim_cur );
130 setrlimit( RLIMIT_CORE, &rlp );
131 getrlimit( RLIMIT_CORE, &rlp );
132 DEBUG( 3, ( "Core limits now %d %d\n", (int)rlp.rlim_cur, (int)rlp.rlim_max ) );
138 DEBUG(0,("Dumping core in %s\n",dname));
145 /**************************************************************************** **
146 possibly continue after a fault
147 **************************************************************************** */
148 static void fault_continue(void)
153 } /* fault_continue */
155 /**************************************************************************** **
156 expire old names from the namelist and server list
157 **************************************************************************** */
158 static void expire_names_and_servers(time_t t)
160 static time_t lastrun = 0;
164 if ( t < (lastrun + 5) )
169 * Expire any timed out names on all the broadcast
170 * subnets and those registered with the WINS server.
171 * (nmbd_namelistdb.c)
176 * Go through all the broadcast subnets and for each
177 * workgroup known on that subnet remove any expired
178 * server names. If a workgroup has an empty serverlist
179 * and has itself timed out then remove the workgroup.
180 * (nmbd_workgroupdb.c)
182 expire_workgroups_and_servers(t);
183 } /* expire_names_and_servers */
186 /************************************************************************** **
187 reload the list of network interfaces
188 ************************************************************************** */
189 static BOOL reload_interfaces(time_t t)
193 struct subnet_record *subrec;
194 extern BOOL rescan_listen_set;
195 extern struct in_addr loopback_ip;
197 if (t && ((t - lastt) < NMBD_INTERFACES_RELOAD)) return False;
200 if (!interfaces_changed()) return False;
202 /* the list of probed interfaces has changed, we may need to add/remove
206 /* find any interfaces that need adding */
207 for (n=iface_count() - 1; n >= 0; n--) {
208 struct interface *iface = get_interface(n);
211 * We don't want to add a loopback interface, in case
212 * someone has added 127.0.0.1 for smbd, nmbd needs to
213 * ignore it here. JRA.
216 if (ip_equal(iface->ip, loopback_ip)) {
217 DEBUG(2,("reload_interfaces: Ignoring loopback interface %s\n", inet_ntoa(iface->ip)));
221 for (subrec=subnetlist; subrec; subrec=subrec->next) {
222 if (ip_equal(iface->ip, subrec->myip) &&
223 ip_equal(iface->nmask, subrec->mask_ip)) break;
227 /* it wasn't found! add it */
228 DEBUG(2,("Found new interface %s\n",
229 inet_ntoa(iface->ip)));
230 subrec = make_normal_subnet(iface);
231 if (subrec) register_my_workgroup_one_subnet(subrec);
235 /* find any interfaces that need deleting */
236 for (subrec=subnetlist; subrec; subrec=subrec->next) {
237 for (n=iface_count() - 1; n >= 0; n--) {
238 struct interface *iface = get_interface(n);
239 if (ip_equal(iface->ip, subrec->myip) &&
240 ip_equal(iface->nmask, subrec->mask_ip)) break;
243 /* oops, an interface has disapeared. This is
244 tricky, we don't dare actually free the
245 interface as it could be being used, so
246 instead we just wear the memory leak and
247 remove it from the list of interfaces without
249 DEBUG(2,("Deleting dead interface %s\n",
250 inet_ntoa(subrec->myip)));
251 close_subnet(subrec);
255 rescan_listen_set = True;
257 /* We need to shutdown if there are no subnets... */
258 if (FIRST_SUBNET == NULL) {
259 DEBUG(0,("reload_interfaces: No subnets to listen to. Shutting down...\n"));
267 /**************************************************************************** **
268 reload the services file
269 **************************************************************************** */
270 static BOOL reload_nmbd_services(BOOL test)
273 extern fstring remote_machine;
275 fstrcpy( remote_machine, "nmb" );
280 pstrcpy( fname,lp_configfile());
281 if (file_exist(fname,NULL) && !strcsequal(fname,servicesf))
283 pstrcpy(servicesf,fname);
288 if ( test && !lp_file_list_changed() )
291 ret = lp_load( servicesf, True , False, False);
293 /* perhaps the config filename is now set */
296 DEBUG( 3, ( "services not loaded\n" ) );
297 reload_nmbd_services( True );
300 /* Do a sanity check for a misconfigured nmbd */
301 if( lp_wins_support() && *lp_wins_server() )
303 DEBUG(0,("ERROR: both 'wins support = true' and 'wins server = <server>' \
304 cannot be set in the smb.conf file. nmbd aborting.\n"));
309 } /* reload_nmbd_services */
311 /**************************************************************************** **
312 The main select loop.
313 **************************************************************************** */
314 static void process(void)
320 time_t t = time(NULL);
322 /* check for internal messages */
326 * Check all broadcast subnets to see if
327 * we need to run an election on any of them.
330 run_election = check_elections();
333 * Read incoming UDP packets.
336 if(listen_for_packets(run_election))
340 * Process all incoming packets
341 * read above. This calls the success and
342 * failure functions registered when response
343 * packets arrrive, and also deals with request
344 * packets from other sources.
350 * Run any elections - initiate becoming
351 * a local master browser if we have won.
357 * Send out any broadcast announcements
358 * of our server names. This also announces
359 * the workgroup name if we are a local
361 * (nmbd_sendannounce.c)
363 announce_my_server_names(t);
366 * Send out any LanMan broadcast announcements
367 * of our server names.
368 * (nmbd_sendannounce.c)
370 announce_my_lm_server_names(t);
373 * If we are a local master browser, periodically
374 * announce ourselves to the domain master browser.
375 * This also deals with syncronising the domain master
376 * browser server lists with ourselves as a local
378 * (nmbd_sendannounce.c)
380 announce_myself_to_domain_master_browser(t);
383 * Fullfill any remote announce requests.
384 * (nmbd_sendannounce.c)
389 * Fullfill any remote browse sync announce requests.
390 * (nmbd_sendannounce.c)
392 browse_sync_remote(t);
395 * Scan the broadcast subnets, and WINS client
396 * namelists and refresh any that need refreshing.
402 * Scan the subnet namelists and server lists and
403 * expire thos that have timed out.
406 expire_names_and_servers(t);
409 * Write out a snapshot of our current browse list into
410 * the browse.dat file. This is used by smbd to service
411 * incoming NetServerEnum calls - used to synchronise
412 * browse lists over subnets.
413 * (nmbd_serverlistdb.c)
415 write_browse_list(t, False);
418 * If we are a domain master browser, we have a list of
419 * local master browsers we should synchronise browse
420 * lists with (these are added by an incoming local
421 * master browser announcement packet). Expire any of
422 * these that are no longer current, and pull the server
423 * lists from each of these known local master browsers.
424 * (nmbd_browsesync.c)
426 dmb_expire_and_sync_browser_lists(t);
429 * Check that there is a local master browser for our
430 * workgroup for all our broadcast subnets. If one
431 * is not found, start an election (which we ourselves
432 * may or may not participate in, depending on the
433 * setting of the 'local master' parameter.
436 check_master_browser_exists(t);
439 * If we are configured as a logon server, attempt to
440 * register the special NetBIOS names to become such
441 * (WORKGROUP<1c> name) on all broadcast subnets and
442 * with the WINS server (if used). If we are configured
443 * to become a domain master browser, attempt to register
444 * the special NetBIOS name (WORKGROUP<1b> name) to
446 * (nmbd_become_dmb.c)
451 * If we are a WINS server, do any timer dependent
452 * processing required.
453 * (nmbd_winsserver.c)
455 initiate_wins_processing(t);
458 * If we are a domain master browser, attempt to contact the
459 * WINS server to get a list of all known WORKGROUPS/DOMAINS.
460 * This will only work to a Samba WINS server.
461 * (nmbd_browsesync.c)
463 collect_all_workgroup_names_from_wins_server(t);
466 * Go through the response record queue and time out or re-transmit
467 * and expired entries.
470 retransmit_or_expire_response_records(t);
473 * check to see if any remote browse sync child processes have completed
475 sync_check_completion();
478 * regularly sync with any other DMBs we know about
483 * clear the unexpected packet queue
488 * Reload the services file if we got a sighup.
491 if(reload_after_sighup) {
492 reload_nmbd_services( True );
494 if(reload_interfaces(0))
496 reload_after_sighup = False;
499 /* check for new network interfaces */
500 if(reload_interfaces(t))
503 /* free up temp memory */
509 /**************************************************************************** **
510 open the socket communication
511 **************************************************************************** */
512 static BOOL open_sockets(BOOL isdaemon, int port)
514 /* The sockets opened here will be used to receive broadcast
515 packets *only*. Interface specific sockets are opened in
516 make_subnet() in namedbsubnet.c. Thus we bind to the
517 address "0.0.0.0". The parameter 'socket address' is
522 ClientNMB = open_socket_in(SOCK_DGRAM, port,0,0,True);
526 ClientDGRAM = open_socket_in(SOCK_DGRAM,DGRAM_PORT,3,0,True);
528 if ( ClientNMB == -1 )
531 /* we are never interested in SIGPIPE */
532 BlockSignals(True,SIGPIPE);
534 set_socket_options( ClientNMB, "SO_BROADCAST" );
535 set_socket_options( ClientDGRAM, "SO_BROADCAST" );
537 DEBUG( 3, ( "open_sockets: Broadcast sockets opened.\n" ) );
542 /**************************************************************************** **
543 initialise connect, service and file structs
544 **************************************************************************** */
545 static BOOL init_structs(void)
547 extern fstring local_machine;
554 if (! *global_myname)
556 fstrcpy( global_myname, myhostname() );
557 p = strchr( global_myname, '.' );
561 strupper( global_myname );
563 /* Add any NETBIOS name aliases. Ensure that the first entry
564 is equal to global_myname.
566 /* Work out the max number of netbios aliases that we have */
567 ptr = lp_netbios_aliases();
568 for( namecount=0; next_token(&ptr,nbname,NULL, sizeof(nbname)); namecount++ )
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();
587 while ( next_token( &ptr, nbname, NULL, sizeof(nbname) ) )
590 /* Look for duplicates */
592 for( n=0; n<namecount; n++ )
594 if( 0 == strcmp( nbname, my_netbios_names[n] ) )
598 my_netbios_names[namecount++] = strdup( nbname );
601 /* Check the strdups succeeded. */
602 for( n = 0; n < namecount; n++ )
603 if( NULL == my_netbios_names[n] )
605 DEBUG(0,("init_structs: malloc fail when allocating names.\n"));
609 /* Terminate name list */
610 my_netbios_names[namecount++] = NULL;
612 fstrcpy( local_machine, global_myname );
613 trim_string( local_machine, " ", " " );
614 p = strchr( local_machine, ' ' );
617 strlower( local_machine );
619 DEBUG( 5, ("Netbios name list:-\n") );
620 for( n=0; my_netbios_names[n]; n++ )
621 DEBUGADD( 5, ( "my_netbios_names[%d]=\"%s\"\n", n, my_netbios_names[n] ) );
626 /**************************************************************************** **
628 **************************************************************************** */
629 static void usage(char *pname)
632 printf( "Usage: %s [-DaohV] [-H lmhosts file] [-d debuglevel] [-l log basename]\n", pname );
633 printf( " [-n name] [-p port] [-s configuration file]\n" );
634 printf( "\t-D Become a daemon\n" );
635 printf( "\t-a Append to log file (default)\n" );
636 printf( "\t-o Overwrite log file, don't append\n" );
637 printf( "\t-h Print usage\n" );
638 printf( "\t-V Print version\n" );
639 printf( "\t-H hosts file Load a netbios hosts file\n" );
640 printf( "\t-d debuglevel Set the debuglevel\n" );
641 printf( "\t-l log basename. Basename for log/debug files\n" );
642 printf( "\t-n netbiosname. Primary netbios name\n" );
643 printf( "\t-p port Listen on the specified port\n" );
644 printf( "\t-s configuration file Configuration file name\n" );
649 /**************************************************************************** **
651 **************************************************************************** */
652 int main(int argc,char *argv[])
657 extern BOOL append_log;
659 append_log = True; /* Default, override with '-o' option. */
661 global_nmb_port = NMB_PORT;
663 global_in_nmbd = True;
665 StartupTime = time(NULL);
667 sys_srandom(time(NULL) ^ sys_getpid());
671 slprintf(debugf, sizeof(debugf), "%s/log.nmbd", LOGFILEBASE);
672 setup_logging( argv[0], False );
674 charset_initialise();
677 pstrcpy( host_file, LMHOSTSFILE );
680 /* this is for people who can't start the program correctly */
681 while (argc > 1 && (*argv[1] != '-'))
687 fault_setup((void (*)(void *))fault_continue );
689 /* POSIX demands that signals are inherited. If the invoking process has
690 * these signals masked, we will have problems, as we won't recieve them. */
691 BlockSignals(False, SIGHUP);
692 BlockSignals(False, SIGUSR1);
693 BlockSignals(False, SIGTERM);
695 CatchSignal( SIGHUP, SIGNAL_CAST sig_hup );
696 CatchSignal( SIGTERM, SIGNAL_CAST sig_term );
699 /* we are never interested in SIGFPE */
700 BlockSignals(True,SIGFPE);
703 /* We no longer use USR2... */
705 BlockSignals(True, SIGUSR2);
709 (opt = getopt( argc, argv, "Vaos:T:I:C:bAB:N:Rn:l:d:Dp:hSH:G:f:" )) )
714 pstrcpy(servicesf,optarg);
721 DEBUG(0,("Obsolete option '%c' used\n",opt));
724 pstrcpy(host_file,optarg);
727 pstrcpy(global_myname,optarg);
728 strupper(global_myname);
731 slprintf(debugf,sizeof(debugf)-1, "%s.nmb",optarg);
743 DEBUGLEVEL = atoi(optarg);
746 global_nmb_port = atoi(optarg);
753 printf( "Version %s\n", VERSION );
757 if( !is_a_socket(0) )
759 DEBUG(0,("Incorrect program usage - is the command line correct?\n"));
769 DEBUG( 1, ( "Netbios nameserver version %s started.\n", VERSION ) );
770 DEBUGADD( 1, ( "Copyright Andrew Tridgell 1994-1998\n" ) );
772 if ( !reload_nmbd_services(False) )
775 codepage_initialise(lp_client_code_page());
780 reload_nmbd_services( True );
782 fstrcpy( global_myworkgroup, lp_workgroup() );
784 if (strequal(global_myworkgroup,"*"))
786 DEBUG(0,("ERROR: a workgroup name of * is no longer supported\n"));
792 if (!is_daemon && !is_a_socket(0))
794 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
800 DEBUG( 2, ( "Becoming a daemon.\n" ) );
805 /* Setup the async dns. We do it here so it doesn't have all the other
806 stuff initialised and thus chewing memory and sockets */
807 if(lp_we_are_a_wins_server()) {
812 if (!directory_exist(lp_lockdir(), NULL)) {
813 mkdir(lp_lockdir(), 0755);
816 pidfile_create("nmbd");
818 message_register(MSG_FORCE_ELECTION, nmbd_message_election);
820 DEBUG( 3, ( "Opening sockets %d\n", global_nmb_port ) );
822 if ( !open_sockets( is_daemon, global_nmb_port ) )
825 /* Determine all the IP addresses we have. */
828 /* Create an nmbd subnet record for each of the above. */
829 if( False == create_subnets() )
831 DEBUG(0,("ERROR: Failed when creating subnet lists. Exiting.\n"));
835 /* Load in any static local names. */
838 load_lmhosts_file(host_file);
839 DEBUG(3,("Loaded hosts file\n"));
842 /* If we are acting as a WINS server, initialise data structures. */
843 if( !initialise_wins() )
845 DEBUG( 0, ( "nmbd: Failed when initialising WINS server.\n" ) );
850 * Register nmbd primary workgroup and nmbd names on all
851 * the broadcast subnets, and on the WINS server (if specified).
852 * Also initiate the startup of our primary workgroup (start
853 * elections if we are setup as being able to be a local
857 if( False == register_my_workgroup_and_names() )
859 DEBUG(0,("ERROR: Failed when creating my my workgroup. Exiting.\n"));
863 /* We can only take signals in the select. */
864 BlockSignals( True, SIGTERM );