r11830: patch from Rashid N. Achilov <shelton@granch.ru> to add descriptions for...
[Samba.git] / source / nmbd / nmbd.c
blob01fdb8e74cf41d6462ee652aad60a01f7cc795c5
1 /*
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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "includes.h"
26 int ClientNMB = -1;
27 int ClientDGRAM = -1;
28 int global_nmb_port = -1;
30 extern BOOL rescan_listen_set;
31 extern struct in_addr loopback_ip;
32 extern BOOL global_in_nmbd;
34 extern BOOL override_logfile;
36 /* are we running as a daemon ? */
37 static BOOL is_daemon;
39 /* fork or run in foreground ? */
40 static BOOL Fork = True;
42 /* log to standard output ? */
43 static BOOL log_stdout;
45 /* have we found LanMan clients yet? */
46 BOOL found_lm_clients = False;
48 /* what server type are we currently */
50 time_t StartupTime = 0;
52 /**************************************************************************** **
53 Handle a SIGTERM in band.
54 **************************************************************************** */
56 static void terminate(void)
58 DEBUG(0,("Got SIGTERM: going down...\n"));
60 /* Write out wins.dat file if samba is a WINS server */
61 wins_write_database(False);
63 /* Remove all SELF registered names from WINS */
64 release_wins_names();
66 /* Announce all server entries as 0 time-to-live, 0 type. */
67 announce_my_servers_removed();
69 /* If there was an async dns child - kill it. */
70 kill_async_dns_child();
72 exit(0);
75 /**************************************************************************** **
76 Handle a SHUTDOWN message from smbcontrol.
77 **************************************************************************** */
79 static void nmbd_terminate(int msg_type, struct process_id src,
80 void *buf, size_t len)
82 terminate();
85 /**************************************************************************** **
86 Catch a SIGTERM signal.
87 **************************************************************************** */
89 static SIG_ATOMIC_T got_sig_term;
91 static void sig_term(int sig)
93 got_sig_term = 1;
94 sys_select_signal(SIGTERM);
97 /**************************************************************************** **
98 Catch a SIGHUP signal.
99 **************************************************************************** */
101 static SIG_ATOMIC_T reload_after_sighup;
103 static void sig_hup(int sig)
105 reload_after_sighup = 1;
106 sys_select_signal(SIGHUP);
109 #if DUMP_CORE
110 /**************************************************************************** **
111 Prepare to dump a core file - carefully!
112 **************************************************************************** */
114 static BOOL dump_core(void)
116 char *p;
117 pstring dname;
118 pstrcpy( dname, lp_logfile() );
119 if ((p=strrchr_m(dname,'/')))
120 *p=0;
121 pstrcat( dname, "/corefiles" );
122 mkdir( dname, 0700 );
123 sys_chown( dname, getuid(), getgid() );
124 chmod( dname, 0700 );
125 if ( chdir(dname) )
126 return( False );
127 umask( ~(0700) );
129 #ifdef HAVE_GETRLIMIT
130 #ifdef RLIMIT_CORE
132 struct rlimit rlp;
133 getrlimit( RLIMIT_CORE, &rlp );
134 rlp.rlim_cur = MAX( 4*1024*1024, rlp.rlim_cur );
135 setrlimit( RLIMIT_CORE, &rlp );
136 getrlimit( RLIMIT_CORE, &rlp );
137 DEBUG( 3, ( "Core limits now %d %d\n", (int)rlp.rlim_cur, (int)rlp.rlim_max ) );
139 #endif
140 #endif
143 DEBUG(0,("Dumping core in %s\n",dname));
144 abort();
145 return( True );
147 #endif
149 /**************************************************************************** **
150 Possibly continue after a fault.
151 **************************************************************************** */
153 static void fault_continue(void)
155 #if DUMP_CORE
156 dump_core();
157 #endif
160 /**************************************************************************** **
161 Expire old names from the namelist and server list.
162 **************************************************************************** */
164 static void expire_names_and_servers(time_t t)
166 static time_t lastrun = 0;
168 if ( !lastrun )
169 lastrun = t;
170 if ( t < (lastrun + 5) )
171 return;
172 lastrun = t;
175 * Expire any timed out names on all the broadcast
176 * subnets and those registered with the WINS server.
177 * (nmbd_namelistdb.c)
180 expire_names(t);
183 * Go through all the broadcast subnets and for each
184 * workgroup known on that subnet remove any expired
185 * server names. If a workgroup has an empty serverlist
186 * and has itself timed out then remove the workgroup.
187 * (nmbd_workgroupdb.c)
190 expire_workgroups_and_servers(t);
193 /************************************************************************** **
194 Reload the list of network interfaces.
195 ************************************************************************** */
197 static BOOL reload_interfaces(time_t t)
199 static time_t lastt;
200 int n;
201 struct subnet_record *subrec;
203 if (t && ((t - lastt) < NMBD_INTERFACES_RELOAD)) return False;
204 lastt = t;
206 if (!interfaces_changed()) return False;
208 /* the list of probed interfaces has changed, we may need to add/remove
209 some subnets */
210 load_interfaces();
212 /* find any interfaces that need adding */
213 for (n=iface_count() - 1; n >= 0; n--) {
214 struct interface *iface = get_interface(n);
217 * We don't want to add a loopback interface, in case
218 * someone has added 127.0.0.1 for smbd, nmbd needs to
219 * ignore it here. JRA.
222 if (ip_equal(iface->ip, loopback_ip)) {
223 DEBUG(2,("reload_interfaces: Ignoring loopback interface %s\n", inet_ntoa(iface->ip)));
224 continue;
227 for (subrec=subnetlist; subrec; subrec=subrec->next) {
228 if (ip_equal(iface->ip, subrec->myip) &&
229 ip_equal(iface->nmask, subrec->mask_ip)) break;
232 if (!subrec) {
233 /* it wasn't found! add it */
234 DEBUG(2,("Found new interface %s\n",
235 inet_ntoa(iface->ip)));
236 subrec = make_normal_subnet(iface);
237 if (subrec)
238 register_my_workgroup_one_subnet(subrec);
242 /* find any interfaces that need deleting */
243 for (subrec=subnetlist; subrec; subrec=subrec->next) {
244 for (n=iface_count() - 1; n >= 0; n--) {
245 struct interface *iface = get_interface(n);
246 if (ip_equal(iface->ip, subrec->myip) &&
247 ip_equal(iface->nmask, subrec->mask_ip)) break;
249 if (n == -1) {
250 /* oops, an interface has disapeared. This is
251 tricky, we don't dare actually free the
252 interface as it could be being used, so
253 instead we just wear the memory leak and
254 remove it from the list of interfaces without
255 freeing it */
256 DEBUG(2,("Deleting dead interface %s\n",
257 inet_ntoa(subrec->myip)));
258 close_subnet(subrec);
262 rescan_listen_set = True;
264 /* We need to shutdown if there are no subnets... */
265 if (FIRST_SUBNET == NULL) {
266 DEBUG(0,("reload_interfaces: No subnets to listen to. Shutting down...\n"));
267 return True;
269 return False;
272 /**************************************************************************** **
273 Reload the services file.
274 **************************************************************************** */
276 static BOOL reload_nmbd_services(BOOL test)
278 BOOL ret;
280 set_remote_machine_name("nmbd", False);
282 if ( lp_loaded() ) {
283 pstring fname;
284 pstrcpy( fname,lp_configfile());
285 if (file_exist(fname,NULL) && !strcsequal(fname,dyn_CONFIGFILE)) {
286 pstrcpy(dyn_CONFIGFILE,fname);
287 test = False;
291 if ( test && !lp_file_list_changed() )
292 return(True);
294 ret = lp_load( dyn_CONFIGFILE, True , False, False);
296 /* perhaps the config filename is now set */
297 if ( !test ) {
298 DEBUG( 3, ( "services not loaded\n" ) );
299 reload_nmbd_services( True );
302 return(ret);
305 /**************************************************************************** **
306 * React on 'smbcontrol nmbd reload-config' in the same way as to SIGHUP
307 * We use buf here to return BOOL result to process() when reload_interfaces()
308 * detects that there are no subnets.
309 **************************************************************************** */
311 static void msg_reload_nmbd_services(int msg_type, struct process_id src,
312 void *buf, size_t len)
314 write_browse_list( 0, True );
315 dump_all_namelists();
316 reload_nmbd_services( True );
317 reopen_logs();
319 if(buf) {
320 /* We were called from process() */
321 /* If reload_interfaces() returned True */
322 /* we need to shutdown if there are no subnets... */
323 /* pass this info back to process() */
324 *((BOOL*)buf) = reload_interfaces(0);
328 static void msg_nmbd_send_packet(int msg_type, struct process_id src,
329 void *buf, size_t len)
331 struct packet_struct *p = (struct packet_struct *)buf;
332 struct subnet_record *subrec;
333 struct in_addr *local_ip;
335 DEBUG(10, ("Received send_packet from %d\n", procid_to_pid(&src)));
337 if (len != sizeof(struct packet_struct)) {
338 DEBUG(2, ("Discarding invalid packet length from %d\n",
339 procid_to_pid(&src)));
340 return;
343 if ((p->packet_type != NMB_PACKET) &&
344 (p->packet_type != DGRAM_PACKET)) {
345 DEBUG(2, ("Discarding invalid packet type from %d: %d\n",
346 procid_to_pid(&src), p->packet_type));
347 return;
350 local_ip = iface_ip(p->ip);
352 if (local_ip == NULL) {
353 DEBUG(2, ("Could not find ip for packet from %d\n",
354 procid_to_pid(&src)));
355 return;
358 subrec = FIRST_SUBNET;
360 p->fd = (p->packet_type == NMB_PACKET) ?
361 subrec->nmb_sock : subrec->dgram_sock;
363 for (subrec = FIRST_SUBNET; subrec != NULL;
364 subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
365 if (ip_equal(*local_ip, subrec->myip)) {
366 p->fd = (p->packet_type == NMB_PACKET) ?
367 subrec->nmb_sock : subrec->dgram_sock;
368 break;
372 if (p->packet_type == DGRAM_PACKET) {
373 p->port = 138;
374 p->packet.dgram.header.source_ip.s_addr = local_ip->s_addr;
375 p->packet.dgram.header.source_port = 138;
378 send_packet(p);
381 /**************************************************************************** **
382 The main select loop.
383 **************************************************************************** */
385 static void process(void)
387 BOOL run_election;
388 BOOL no_subnets;
390 while( True ) {
391 time_t t = time(NULL);
393 /* Check for internal messages */
395 message_dispatch();
398 * Check all broadcast subnets to see if
399 * we need to run an election on any of them.
400 * (nmbd_elections.c)
403 run_election = check_elections();
406 * Read incoming UDP packets.
407 * (nmbd_packets.c)
410 if(listen_for_packets(run_election))
411 return;
414 * Handle termination inband.
417 if (got_sig_term) {
418 got_sig_term = 0;
419 terminate();
423 * Process all incoming packets
424 * read above. This calls the success and
425 * failure functions registered when response
426 * packets arrrive, and also deals with request
427 * packets from other sources.
428 * (nmbd_packets.c)
431 run_packet_queue();
434 * Run any elections - initiate becoming
435 * a local master browser if we have won.
436 * (nmbd_elections.c)
439 run_elections(t);
442 * Send out any broadcast announcements
443 * of our server names. This also announces
444 * the workgroup name if we are a local
445 * master browser.
446 * (nmbd_sendannounce.c)
449 announce_my_server_names(t);
452 * Send out any LanMan broadcast announcements
453 * of our server names.
454 * (nmbd_sendannounce.c)
457 announce_my_lm_server_names(t);
460 * If we are a local master browser, periodically
461 * announce ourselves to the domain master browser.
462 * This also deals with syncronising the domain master
463 * browser server lists with ourselves as a local
464 * master browser.
465 * (nmbd_sendannounce.c)
468 announce_myself_to_domain_master_browser(t);
471 * Fullfill any remote announce requests.
472 * (nmbd_sendannounce.c)
475 announce_remote(t);
478 * Fullfill any remote browse sync announce requests.
479 * (nmbd_sendannounce.c)
482 browse_sync_remote(t);
485 * Scan the broadcast subnets, and WINS client
486 * namelists and refresh any that need refreshing.
487 * (nmbd_mynames.c)
490 refresh_my_names(t);
493 * Scan the subnet namelists and server lists and
494 * expire thos that have timed out.
495 * (nmbd.c)
498 expire_names_and_servers(t);
501 * Write out a snapshot of our current browse list into
502 * the browse.dat file. This is used by smbd to service
503 * incoming NetServerEnum calls - used to synchronise
504 * browse lists over subnets.
505 * (nmbd_serverlistdb.c)
508 write_browse_list(t, False);
511 * If we are a domain master browser, we have a list of
512 * local master browsers we should synchronise browse
513 * lists with (these are added by an incoming local
514 * master browser announcement packet). Expire any of
515 * these that are no longer current, and pull the server
516 * lists from each of these known local master browsers.
517 * (nmbd_browsesync.c)
520 dmb_expire_and_sync_browser_lists(t);
523 * Check that there is a local master browser for our
524 * workgroup for all our broadcast subnets. If one
525 * is not found, start an election (which we ourselves
526 * may or may not participate in, depending on the
527 * setting of the 'local master' parameter.
528 * (nmbd_elections.c)
531 check_master_browser_exists(t);
534 * If we are configured as a logon server, attempt to
535 * register the special NetBIOS names to become such
536 * (WORKGROUP<1c> name) on all broadcast subnets and
537 * with the WINS server (if used). If we are configured
538 * to become a domain master browser, attempt to register
539 * the special NetBIOS name (WORKGROUP<1b> name) to
540 * become such.
541 * (nmbd_become_dmb.c)
544 add_domain_names(t);
547 * If we are a WINS server, do any timer dependent
548 * processing required.
549 * (nmbd_winsserver.c)
552 initiate_wins_processing(t);
555 * If we are a domain master browser, attempt to contact the
556 * WINS server to get a list of all known WORKGROUPS/DOMAINS.
557 * This will only work to a Samba WINS server.
558 * (nmbd_browsesync.c)
561 if (lp_enhanced_browsing())
562 collect_all_workgroup_names_from_wins_server(t);
565 * Go through the response record queue and time out or re-transmit
566 * and expired entries.
567 * (nmbd_packets.c)
570 retransmit_or_expire_response_records(t);
573 * check to see if any remote browse sync child processes have completed
576 sync_check_completion();
579 * regularly sync with any other DMBs we know about
582 if (lp_enhanced_browsing())
583 sync_all_dmbs(t);
586 * clear the unexpected packet queue
589 clear_unexpected(t);
592 * Reload the services file if we got a sighup.
595 if(reload_after_sighup) {
596 DEBUG( 0, ( "Got SIGHUP dumping debug info.\n" ) );
597 msg_reload_nmbd_services(MSG_SMB_CONF_UPDATED,
598 pid_to_procid(0), (void*) &no_subnets, 0);
599 if(no_subnets)
600 return;
601 reload_after_sighup = 0;
604 /* check for new network interfaces */
606 if(reload_interfaces(t))
607 return;
609 /* free up temp memory */
610 lp_talloc_free();
614 /**************************************************************************** **
615 Open the socket communication.
616 **************************************************************************** */
618 static BOOL open_sockets(BOOL isdaemon, int port)
621 * The sockets opened here will be used to receive broadcast
622 * packets *only*. Interface specific sockets are opened in
623 * make_subnet() in namedbsubnet.c. Thus we bind to the
624 * address "0.0.0.0". The parameter 'socket address' is
625 * now deprecated.
628 if ( isdaemon )
629 ClientNMB = open_socket_in(SOCK_DGRAM, port,
630 0, interpret_addr(lp_socket_address()),
631 True);
632 else
633 ClientNMB = 0;
635 ClientDGRAM = open_socket_in(SOCK_DGRAM, DGRAM_PORT,
636 3, interpret_addr(lp_socket_address()),
637 True);
639 if ( ClientNMB == -1 )
640 return( False );
642 /* we are never interested in SIGPIPE */
643 BlockSignals(True,SIGPIPE);
645 set_socket_options( ClientNMB, "SO_BROADCAST" );
646 set_socket_options( ClientDGRAM, "SO_BROADCAST" );
648 DEBUG( 3, ( "open_sockets: Broadcast sockets opened.\n" ) );
649 return( True );
652 /**************************************************************************** **
653 main program
654 **************************************************************************** */
655 int main(int argc, const char *argv[])
657 pstring logfile;
658 static BOOL opt_interactive;
659 poptContext pc;
660 struct poptOption long_options[] = {
661 POPT_AUTOHELP
662 {"daemon", 'D', POPT_ARG_VAL, &is_daemon, True, "Become a daemon(default)" },
663 {"interactive", 'i', POPT_ARG_VAL, &opt_interactive, True, "Run interactive (not a daemon)" },
664 {"foreground", 'F', POPT_ARG_VAL, &Fork, False, "Run daemon in foreground (for daemontools & etc)" },
665 {"log-stdout", 'S', POPT_ARG_VAL, &log_stdout, True, "Log to stdout" },
666 {"hosts", 'H', POPT_ARG_STRING, dyn_LMHOSTSFILE, 'H', "Load a netbios hosts file"},
667 {"port", 'p', POPT_ARG_INT, &global_nmb_port, NMB_PORT, "Listen on the specified port" },
668 POPT_COMMON_SAMBA
669 { NULL }
672 global_nmb_port = NMB_PORT;
674 pc = poptGetContext("nmbd", argc, argv, long_options, 0);
675 while (poptGetNextOpt(pc) != -1) {};
676 poptFreeContext(pc);
678 global_in_nmbd = True;
680 StartupTime = time(NULL);
682 sys_srandom(time(NULL) ^ sys_getpid());
684 if (!override_logfile) {
685 slprintf(logfile, sizeof(logfile)-1, "%s/log.nmbd", dyn_LOGFILEBASE);
686 lp_set_logfile(logfile);
689 fault_setup((void (*)(void *))fault_continue );
691 /* POSIX demands that signals are inherited. If the invoking process has
692 * these signals masked, we will have problems, as we won't receive them. */
693 BlockSignals(False, SIGHUP);
694 BlockSignals(False, SIGUSR1);
695 BlockSignals(False, SIGTERM);
697 CatchSignal( SIGHUP, SIGNAL_CAST sig_hup );
698 CatchSignal( SIGTERM, SIGNAL_CAST sig_term );
700 #if defined(SIGFPE)
701 /* we are never interested in SIGFPE */
702 BlockSignals(True,SIGFPE);
703 #endif
705 /* We no longer use USR2... */
706 #if defined(SIGUSR2)
707 BlockSignals(True, SIGUSR2);
708 #endif
710 if ( opt_interactive ) {
711 Fork = False;
712 log_stdout = True;
715 if ( log_stdout && Fork ) {
716 DEBUG(0,("ERROR: Can't log to stdout (-S) unless daemon is in foreground (-F) or interactive (-i)\n"));
717 exit(1);
720 setup_logging( argv[0], log_stdout );
722 reopen_logs();
724 DEBUG( 0, ( "Netbios nameserver version %s started.\n", SAMBA_VERSION_STRING) );
725 DEBUGADD( 0, ( "%s\n", COPYRIGHT_STARTUP_MESSAGE ) );
727 if ( !reload_nmbd_services(False) )
728 return(-1);
730 if(!init_names())
731 return -1;
733 reload_nmbd_services( True );
735 if (strequal(lp_workgroup(),"*")) {
736 DEBUG(0,("ERROR: a workgroup name of * is no longer supported\n"));
737 exit(1);
740 set_samba_nb_type();
742 if (!is_daemon && !is_a_socket(0)) {
743 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
744 is_daemon = True;
747 if (is_daemon && !opt_interactive) {
748 DEBUG( 2, ( "Becoming a daemon.\n" ) );
749 become_daemon(Fork);
752 #if HAVE_SETPGID
754 * If we're interactive we want to set our own process group for
755 * signal management.
757 if (opt_interactive)
758 setpgid( (pid_t)0, (pid_t)0 );
759 #endif
761 #ifndef SYNC_DNS
762 /* Setup the async dns. We do it here so it doesn't have all the other
763 stuff initialised and thus chewing memory and sockets */
764 if(lp_we_are_a_wins_server() && lp_dns_proxy()) {
765 start_async_dns();
767 #endif
769 if (!directory_exist(lp_lockdir(), NULL)) {
770 mkdir(lp_lockdir(), 0755);
773 pidfile_create("nmbd");
774 message_init();
775 message_register(MSG_FORCE_ELECTION, nmbd_message_election);
776 message_register(MSG_WINS_NEW_ENTRY, nmbd_wins_new_entry);
777 message_register(MSG_SHUTDOWN, nmbd_terminate);
778 message_register(MSG_SMB_CONF_UPDATED, msg_reload_nmbd_services);
779 message_register(MSG_SEND_PACKET, msg_nmbd_send_packet);
781 TimeInit();
783 DEBUG( 3, ( "Opening sockets %d\n", global_nmb_port ) );
785 if ( !open_sockets( is_daemon, global_nmb_port ) ) {
786 kill_async_dns_child();
787 return 1;
790 /* Determine all the IP addresses we have. */
791 load_interfaces();
793 /* Create an nmbd subnet record for each of the above. */
794 if( False == create_subnets() ) {
795 DEBUG(0,("ERROR: Failed when creating subnet lists. Exiting.\n"));
796 kill_async_dns_child();
797 exit(1);
800 /* Load in any static local names. */
801 load_lmhosts_file(dyn_LMHOSTSFILE);
802 DEBUG(3,("Loaded hosts file %s\n", dyn_LMHOSTSFILE));
804 /* If we are acting as a WINS server, initialise data structures. */
805 if( !initialise_wins() ) {
806 DEBUG( 0, ( "nmbd: Failed when initialising WINS server.\n" ) );
807 kill_async_dns_child();
808 exit(1);
812 * Register nmbd primary workgroup and nmbd names on all
813 * the broadcast subnets, and on the WINS server (if specified).
814 * Also initiate the startup of our primary workgroup (start
815 * elections if we are setup as being able to be a local
816 * master browser.
819 if( False == register_my_workgroup_and_names() ) {
820 DEBUG(0,("ERROR: Failed when creating my my workgroup. Exiting.\n"));
821 kill_async_dns_child();
822 exit(1);
825 /* We can only take signals in the select. */
826 BlockSignals( True, SIGTERM );
828 process();
830 if (dbf)
831 x_fclose(dbf);
832 kill_async_dns_child();
833 return(0);