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;
39 int global_nmb_port = -1;
41 extern pstring myhostname;
42 static pstring host_file;
43 extern pstring myname;
44 extern fstring myworkgroup;
45 extern char **my_netbios_names;
47 extern BOOL global_in_nmbd;
49 /* are we running as a daemon ? */
50 static BOOL is_daemon = False;
52 /* have we found LanMan clients yet? */
53 BOOL found_lm_clients = False;
55 /* what server type are we currently */
57 time_t StartupTime = 0;
59 extern struct in_addr ipzero;
61 /**************************************************************************** **
63 **************************************************************************** */
64 static int sig_term(void)
66 BlockSignals(True,SIGTERM);
68 DEBUG(0,("Got SIGTERM: going down...\n"));
70 /* Write out wins.dat file if samba is a WINS server */
71 wins_write_database();
73 /* Remove all SELF registered names. */
76 /* Announce all server entries as 0 time-to-live, 0 type. */
77 announce_my_servers_removed();
79 /* If there was an async dns child - kill it. */
80 kill_async_dns_child();
84 /* Keep compiler happy.. */
88 /**************************************************************************** **
90 **************************************************************************** */
91 static int sig_hup(void)
93 BlockSignals( True, SIGHUP );
95 DEBUG( 0, ( "Got SIGHUP dumping debug info.\n" ) );
97 write_browse_list( 0, True );
100 reload_services( True );
104 BlockSignals(False,SIGHUP);
105 #ifndef DONT_REINSTALL_SIG
106 signal(SIGHUP,SIGNAL_CAST sig_hup);
111 /**************************************************************************** **
113 **************************************************************************** */
114 static int sig_pipe(void)
116 BlockSignals( True, SIGPIPE );
118 DEBUG( 0, ("Got SIGPIPE\n") );
121 BlockSignals( False, SIGPIPE );
126 /**************************************************************************** **
127 prepare to dump a core file - carefully!
128 **************************************************************************** */
129 static BOOL dump_core(void)
133 pstrcpy( dname, debugf );
134 if ((p=strrchr(dname,'/')))
136 strcat( dname, "/corefiles" );
137 mkdir( dname, 0700 );
138 sys_chown( dname, getuid(), getgid() );
139 chmod( dname, 0700 );
148 getrlimit( RLIMIT_CORE, &rlp );
149 rlp.rlim_cur = MAX( 4*1024*1024, rlp.rlim_cur );
150 setrlimit( RLIMIT_CORE, &rlp );
151 getrlimit( RLIMIT_CORE, &rlp );
152 DEBUG( 3, ( "Core limits now %d %d\n", rlp.rlim_cur, rlp.rlim_max ) );
158 DEBUG( 0, ( "Dumping core in %s\n",dname ) );
164 /**************************************************************************** **
165 possibly continue after a fault
166 **************************************************************************** */
167 static void fault_continue(void)
172 } /* fault_continue */
174 /**************************************************************************** **
175 expire old names from the namelist and server list
176 **************************************************************************** */
177 static void expire_names_and_servers(time_t t)
179 static time_t lastrun = 0;
183 if ( t < (lastrun + 5) )
188 * Expire any timed out names on all the broadcast
189 * subnets and those registered with the WINS server.
190 * (nmbd_namelistdb.c)
195 * Go through all the broadcast subnets and for each
196 * workgroup known on that subnet remove any expired
197 * server names. If a workgroup has an empty serverlist
198 * and has itself timed out then remove the workgroup.
199 * (nmbd_workgroupdb.c)
201 expire_workgroups_and_servers(t);
202 } /* expire_names_and_servers */
204 /**************************************************************************** **
205 reload the services file
206 **************************************************************************** */
207 BOOL reload_services(BOOL test)
210 extern fstring remote_machine;
212 strcpy( remote_machine, "nmb" );
217 pstrcpy( fname,lp_configfile());
218 if (file_exist(fname,NULL) && !strcsequal(fname,servicesf))
220 pstrcpy(servicesf,fname);
225 if ( test && !lp_file_list_changed() )
228 ret = lp_load( servicesf, True , False, False);
230 /* perhaps the config filename is now set */
233 DEBUG( 3, ( "services not loaded\n" ) );
234 reload_services( True );
237 /* Do a sanity check for a misconfigured nmbd */
238 if( lp_wins_support() && *lp_wins_server() )
240 DEBUG(0,("ERROR: both 'wins support = true' and 'wins server = <server>' \
241 cannot be set in the smb.conf file. nmbd aborting.\n"));
246 } /* reload_services */
248 /**************************************************************************** **
249 The main select loop.
250 **************************************************************************** */
251 static void process(void)
257 time_t t = time(NULL);
260 * Check all broadcast subnets to see if
261 * we need to run an election on any of them.
264 run_election = check_elections();
267 * Read incoming UDP packets.
270 if(listen_for_packets(run_election))
274 * Process all incoming packets
275 * read above. This calls the success and
276 * failure functions registered when response
277 * packets arrrive, and also deals with request
278 * packets from other sources.
284 * Run any elections - initiate becoming
285 * a local master browser if we have won.
291 * Send out any broadcast announcements
292 * of our server names. This also announces
293 * the workgroup name if we are a local
295 * (nmbd_sendannounce.c)
297 announce_my_server_names(t);
300 * Send out any LanMan broadcast announcements
301 * of our server names.
302 * (nmbd_sendannounce.c)
304 announce_my_lm_server_names(t);
307 * If we are a local master browser, periodically
308 * announce ourselves to the domain master browser.
309 * This also deals with syncronising the domain master
310 * browser server lists with ourselves as a local
312 * (nmbd_sendannounce.c)
314 announce_myself_to_domain_master_browser(t);
317 * Fullfill any remote announce requests.
318 * (nmbd_sendannounce.c)
323 * Fullfill any remote browse sync announce requests.
324 * (nmbd_sendannounce.c)
326 browse_sync_remote(t);
329 * Scan the broadcast subnets, and WINS client
330 * namelists and refresh any that need refreshing.
336 * Scan the subnet namelists and server lists and
337 * expire thos that have timed out.
340 expire_names_and_servers(t);
343 * Write out a snapshot of our current browse list into
344 * the browse.dat file. This is used by smbd to service
345 * incoming NetServerEnum calls - used to synchronise
346 * browse lists over subnets.
347 * (nmbd_serverlistdb.c)
349 write_browse_list(t, False);
352 * If we are a domain master browser, we have a list of
353 * local master browsers we should synchronise browse
354 * lists with (these are added by an incoming local
355 * master browser announcement packet). Expire any of
356 * these that are no longer current, and pull the server
357 * lists from each of these known local master browsers.
358 * (nmbd_browsesync.c)
360 dmb_expire_and_sync_browser_lists(t);
363 * Check that there is a local master browser for our
364 * workgroup for all our broadcast subnets. If one
365 * is not found, start an election (which we ourselves
366 * may or may not participate in, depending on the
367 * setting of the 'local master' parameter.
370 check_master_browser_exists(t);
373 * If we are configured as a logon server, attempt to
374 * register the special NetBIOS names to become such
375 * (WORKGROUP<1c> name) on all broadcast subnets and
376 * with the WINS server (if used). If we are configured
377 * to become a domain master browser, attempt to register
378 * the special NetBIOS name (WORKGROUP<1b> name) to
380 * (nmbd_become_dmb.c)
385 * If we are a WINS server, do any timer dependent
386 * processing required.
387 * (nmbd_winsserver.c)
389 initiate_wins_processing(t);
392 * If we are a domain master browser, attempt to contact the
393 * WINS server to get a list of all known WORKGROUPS/DOMAINS.
394 * This will only work to a Samba WINS server.
395 * (nmbd_browsesync.c)
397 collect_all_workgroup_names_from_wins_server(t);
400 * Go through the response record queue and time out or re-transmit
401 * and expired entries.
404 retransmit_or_expire_response_records(t);
409 /**************************************************************************** **
410 open the socket communication
411 **************************************************************************** */
412 static BOOL open_sockets(BOOL isdaemon, int port)
414 /* The sockets opened here will be used to receive broadcast
415 packets *only*. Interface specific sockets are opened in
416 make_subnet() in namedbsubnet.c. Thus we bind to the
417 address "0.0.0.0". The parameter 'socket address' is
422 ClientNMB = open_socket_in(SOCK_DGRAM, port,0,0);
426 ClientDGRAM = open_socket_in(SOCK_DGRAM,DGRAM_PORT,3,0);
428 if ( ClientNMB == -1 )
431 signal( SIGPIPE, SIGNAL_CAST sig_pipe );
433 set_socket_options( ClientNMB, "SO_BROADCAST" );
434 set_socket_options( ClientDGRAM, "SO_BROADCAST" );
436 DEBUG( 3, ( "open_sockets: Broadcast sockets opened.\n" ) );
441 /**************************************************************************** **
442 initialise connect, service and file structs
443 **************************************************************************** */
444 static BOOL init_structs(void)
446 extern fstring local_machine;
455 fstrcpy( myname, myhostname );
456 p = strchr( myname, '.' );
462 /* Add any NETBIOS name aliases. Ensure that the first entry
465 /* Work out the max number of netbios aliases that we have */
466 ptr = lp_netbios_aliases();
467 for( namecount=0; next_token(&ptr,nbname,NULL); namecount++ )
472 /* Allocate space for the netbios aliases */
473 my_netbios_names = (char **)malloc( sizeof(char *) * (namecount+1) );
474 if( NULL == my_netbios_names )
476 DEBUG( 0, ( "init_structs: malloc fail.\n" ) );
480 /* Use the myname string first */
483 my_netbios_names[namecount++] = myname;
485 ptr = lp_netbios_aliases();
486 while ( next_token( &ptr, nbname, NULL ) )
489 /* Look for duplicates */
491 for( n=0; n<namecount; n++ )
493 if( 0 == strcmp( nbname, my_netbios_names[n] ) )
497 my_netbios_names[namecount++] = strdup( nbname );
500 /* Check the strdups succeeded. */
501 for( n = 0; n < namecount; n++ )
502 if( NULL == my_netbios_names[n] )
504 DEBUG(0,("init_structs: malloc fail when allocating names.\n"));
508 /* Terminate name list */
509 my_netbios_names[namecount++] = NULL;
511 fstrcpy( local_machine, myname );
512 trim_string( local_machine, " ", " " );
513 p = strchr( local_machine, ' ' );
516 strlower( local_machine );
518 DEBUG( 5, ("Netbios name list:-\n") );
519 for( n=0; my_netbios_names[n]; n++ )
520 DEBUG( 5, ( "my_netbios_names[%d]=\"%s\"\n", n, my_netbios_names[n] ) );
525 /**************************************************************************** **
527 **************************************************************************** */
528 static void usage(char *pname)
530 DEBUG(0,("Incorrect program usage - is the command line correct?\n"));
532 printf( "Usage: %s [-n name] [-D] [-p port] [-d debuglevel] ", pname );
533 printf( "[-l log basename]\n" );
534 printf( "Version %s\n", VERSION );
535 printf( "\t-D become a daemon\n" );
536 printf( "\t-p port listen on the specified port\n" );
537 printf( "\t-d debuglevel set the debuglevel\n" );
538 printf( "\t-l log basename. Basename for log/debug files\n" );
539 printf( "\t-n netbiosname. " );
540 printf( "the netbios name to advertise for this host\n");
541 printf( "\t-H hosts file load a netbios hosts file\n" );
546 /**************************************************************************** **
548 **************************************************************************** */
549 int main(int argc,char *argv[])
555 global_nmb_port = NMB_PORT;
557 global_in_nmbd = True;
559 StartupTime = time(NULL);
563 strcpy( debugf, NMBLOGFILE );
565 setup_logging( argv[0], False );
567 charset_initialise();
570 strcpy( host_file, LMHOSTSFILE );
573 /* this is for people who can't start the program correctly */
574 while (argc > 1 && (*argv[1] != '-'))
580 fault_setup((void (*)(void *))fault_continue );
582 signal( SIGHUP, SIGNAL_CAST sig_hup );
583 signal( SIGTERM, SIGNAL_CAST sig_term );
585 /* Setup the signals that allow the debug log level
586 to by dynamically changed. */
588 /* If we are using the malloc debug code we can't use
589 SIGUSR1 and SIGUSR2 to do debug level changes. */
592 signal( SIGUSR1, SIGNAL_CAST sig_usr1 );
596 signal( SIGUSR2, SIGNAL_CAST sig_usr2 );
600 while((opt = getopt(argc, argv, "as:T:I:C:bAi:B:N:Rn:l:d:Dp:hSH:G:f:")) != EOF)
605 pstrcpy(servicesf,optarg);
612 DEBUG(0,("Obsolete option '%c' used\n",opt));
615 pstrcpy(host_file,optarg);
618 pstrcpy(myname,optarg);
622 sprintf(debugf,"%s.nmb",optarg);
625 pstrcpy(scope,optarg);
630 extern BOOL append_log;
631 append_log = !append_log;
638 DEBUGLEVEL = atoi(optarg);
641 global_nmb_port = atoi(optarg);
656 DEBUG(1,("%s netbios nameserver version %s started\n",timestring(),VERSION));
657 DEBUG(1,("Copyright Andrew Tridgell 1994-1997\n"));
659 if( !get_myname( myhostname, NULL) )
661 DEBUG(0,("Unable to get my hostname - exiting.\n"));
665 if ( !reload_services(False) )
668 codepage_initialise(lp_client_code_page());
673 reload_services( True );
675 fstrcpy( myworkgroup, lp_workgroup() );
677 if (strequal(myworkgroup,"*"))
679 DEBUG(0,("ERROR: a workgroup name of * is no longer supported\n"));
685 if (!is_daemon && !is_a_socket(0))
687 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
693 DEBUG(2,("%s becoming a daemon\n",timestring()));
697 if (!directory_exist(lp_lockdir(), NULL)) {
698 mkdir(lp_lockdir(), 0755);
701 pidfile_create("nmbd");
703 DEBUG( 3, ( "Opening sockets %d\n", global_nmb_port ) );
705 if ( !open_sockets( is_daemon, global_nmb_port ) )
708 /* Determine all the IP addresses we have. */
711 /* Create an nmbd subnet record for each of the above. */
712 if( False == create_subnets() )
714 DEBUG(0,("ERROR: Failed when creating subnet lists. Exiting.\n"));
718 /* Load in any static local names. */
721 load_lmhosts_file(host_file);
722 DEBUG(3,("Loaded hosts file\n"));
725 /* If we are acting as a WINS server, initialise data structures. */
726 if( !initialise_wins() )
728 DEBUG( 0, ( "nmbd: Failed when initialising WINS server.\n" ) );
733 * Register nmbd primary workgroup and nmbd names on all
734 * the broadcast subnets, and on the WINS server (if specified).
735 * Also initiate the startup of our primary workgroup (start
736 * elections if we are setup as being able to be a local
740 if( False == register_my_workgroup_and_names() )
742 DEBUG(0,("ERROR: Failed when creating my my workgroup. Exiting.\n"));
746 /* We can only take signals in the select. */
747 BlockSignals( True, SIGTERM );
749 BlockSignals( True, SIGUSR1);
752 BlockSignals( True, SIGUSR2);
763 /* ========================================================================== */