Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / samba / source / nmbd / nmbd.c
blob3ecaac5a3d32327fde6721ef0f718ef6725f0d7c
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
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.
21 Revision History:
23 14 jan 96: lkcl@pires.co.uk
24 added multiple workgroup domain master support
28 #include "includes.h"
30 extern int DEBUGLEVEL;
32 extern pstring debugf;
33 pstring servicesf = CONFIGFILE;
35 int ClientNMB = -1;
36 int ClientDGRAM = -1;
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 /**************************************************************************** **
59 catch a sigterm
60 **************************************************************************** */
61 static void sig_term(int sig)
63 BlockSignals(True,SIGTERM);
65 DEBUG(1,("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. */
71 release_my_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();
79 exit(0);
81 } /* sig_term */
83 /**************************************************************************** **
84 catch a sighup
85 **************************************************************************** */
86 static VOLATILE SIG_ATOMIC_T reload_after_sighup = False;
88 static void sig_hup(int sig)
90 BlockSignals( True, SIGHUP );
92 DEBUG( 1, ( "Got SIGHUP dumping debug info.\n" ) );
94 write_browse_list( 0, True );
96 dump_all_namelists();
98 reload_after_sighup = True;
100 BlockSignals(False,SIGHUP);
102 } /* sig_hup */
105 #if DUMP_CORE
106 /**************************************************************************** **
107 prepare to dump a core file - carefully!
108 **************************************************************************** */
109 static BOOL dump_core(void)
111 char *p;
112 pstring dname;
113 pstrcpy( dname, debugf );
114 if ((p=strrchr(dname,'/')))
115 *p=0;
116 pstrcat( dname, "/corefiles" );
117 mkdir( dname, 0700 );
118 sys_chown( dname, getuid(), getgid() );
119 chmod( dname, 0700 );
120 if ( chdir(dname) )
121 return( False );
122 umask( ~(0700) );
124 #ifdef HAVE_GETRLIMIT
125 #ifdef RLIMIT_CORE
127 struct rlimit rlp;
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 ) );
134 #endif
135 #endif
138 DEBUG(0,("Dumping core in %s\n",dname));
139 abort();
140 return( True );
141 } /* dump_core */
142 #endif
145 /**************************************************************************** **
146 possibly continue after a fault
147 **************************************************************************** */
148 static void fault_continue(void)
150 #if DUMP_CORE
151 dump_core();
152 #endif
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;
162 if ( !lastrun )
163 lastrun = t;
164 if ( t < (lastrun + 5) )
165 return;
166 lastrun = t;
169 * Expire any timed out names on all the broadcast
170 * subnets and those registered with the WINS server.
171 * (nmbd_namelistdb.c)
173 expire_names(t);
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 void reload_interfaces(time_t t)
191 static time_t lastt;
192 int n;
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;
198 lastt = t;
200 if (!interfaces_changed()) return;
202 /* the list of probed interfaces has changed, we may need to add/remove
203 some subnets */
204 load_interfaces();
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)));
218 continue;
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;
226 if (!subrec) {
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;
242 if (n == -1) {
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
248 freeing it */
249 DEBUG(2,("Deleting dead interface %s\n",
250 inet_ntoa(subrec->myip)));
251 close_subnet(subrec);
255 rescan_listen_set = True;
260 /**************************************************************************** **
261 reload the services file
262 **************************************************************************** */
263 BOOL reload_services(BOOL test)
265 BOOL ret;
266 extern fstring remote_machine;
268 fstrcpy( remote_machine, "nmb" );
270 if ( lp_loaded() )
272 pstring fname;
273 pstrcpy( fname,lp_configfile());
274 if (file_exist(fname,NULL) && !strcsequal(fname,servicesf))
276 pstrcpy(servicesf,fname);
277 test = False;
281 if ( test && !lp_file_list_changed() )
282 return(True);
284 ret = lp_load( servicesf, True , False, False);
286 /* perhaps the config filename is now set */
287 if ( !test )
289 DEBUG( 3, ( "services not loaded\n" ) );
290 reload_services( True );
293 /* Do a sanity check for a misconfigured nmbd */
294 if( lp_wins_support() && *lp_wins_server() )
296 DEBUG(0,("ERROR: both 'wins support = true' and 'wins server = <server>' \
297 cannot be set in the smb.conf file. nmbd aborting.\n"));
298 exit(10);
301 return(ret);
302 } /* reload_services */
304 /**************************************************************************** **
305 The main select loop.
306 **************************************************************************** */
307 static void process(void)
309 BOOL run_election;
311 while( True )
313 time_t t = time(NULL);
316 * Check all broadcast subnets to see if
317 * we need to run an election on any of them.
318 * (nmbd_elections.c)
320 run_election = check_elections();
323 * Read incoming UDP packets.
324 * (nmbd_packets.c)
326 if(listen_for_packets(run_election))
327 return;
330 * Process all incoming packets
331 * read above. This calls the success and
332 * failure functions registered when response
333 * packets arrrive, and also deals with request
334 * packets from other sources.
335 * (nmbd_packets.c)
337 run_packet_queue();
340 * Run any elections - initiate becoming
341 * a local master browser if we have won.
342 * (nmbd_elections.c)
344 run_elections(t);
347 * Send out any broadcast announcements
348 * of our server names. This also announces
349 * the workgroup name if we are a local
350 * master browser.
351 * (nmbd_sendannounce.c)
353 announce_my_server_names(t);
356 * Send out any LanMan broadcast announcements
357 * of our server names.
358 * (nmbd_sendannounce.c)
360 announce_my_lm_server_names(t);
363 * If we are a local master browser, periodically
364 * announce ourselves to the domain master browser.
365 * This also deals with syncronising the domain master
366 * browser server lists with ourselves as a local
367 * master browser.
368 * (nmbd_sendannounce.c)
370 announce_myself_to_domain_master_browser(t);
373 * Fullfill any remote announce requests.
374 * (nmbd_sendannounce.c)
376 announce_remote(t);
379 * Fullfill any remote browse sync announce requests.
380 * (nmbd_sendannounce.c)
382 browse_sync_remote(t);
385 * Scan the broadcast subnets, and WINS client
386 * namelists and refresh any that need refreshing.
387 * (nmbd_mynames.c)
389 refresh_my_names(t);
392 * Scan the subnet namelists and server lists and
393 * expire thos that have timed out.
394 * (nmbd.c)
396 expire_names_and_servers(t);
399 * Write out a snapshot of our current browse list into
400 * the browse.dat file. This is used by smbd to service
401 * incoming NetServerEnum calls - used to synchronise
402 * browse lists over subnets.
403 * (nmbd_serverlistdb.c)
405 write_browse_list(t, False);
408 * If we are a domain master browser, we have a list of
409 * local master browsers we should synchronise browse
410 * lists with (these are added by an incoming local
411 * master browser announcement packet). Expire any of
412 * these that are no longer current, and pull the server
413 * lists from each of these known local master browsers.
414 * (nmbd_browsesync.c)
416 dmb_expire_and_sync_browser_lists(t);
419 * Check that there is a local master browser for our
420 * workgroup for all our broadcast subnets. If one
421 * is not found, start an election (which we ourselves
422 * may or may not participate in, depending on the
423 * setting of the 'local master' parameter.
424 * (nmbd_elections.c)
426 check_master_browser_exists(t);
429 * If we are configured as a logon server, attempt to
430 * register the special NetBIOS names to become such
431 * (WORKGROUP<1c> name) on all broadcast subnets and
432 * with the WINS server (if used). If we are configured
433 * to become a domain master browser, attempt to register
434 * the special NetBIOS name (WORKGROUP<1b> name) to
435 * become such.
436 * (nmbd_become_dmb.c)
438 add_domain_names(t);
441 * If we are a WINS server, do any timer dependent
442 * processing required.
443 * (nmbd_winsserver.c)
445 initiate_wins_processing(t);
448 * If we are a domain master browser, attempt to contact the
449 * WINS server to get a list of all known WORKGROUPS/DOMAINS.
450 * This will only work to a Samba WINS server.
451 * (nmbd_browsesync.c)
453 collect_all_workgroup_names_from_wins_server(t);
456 * Go through the response record queue and time out or re-transmit
457 * and expired entries.
458 * (nmbd_packets.c)
460 retransmit_or_expire_response_records(t);
463 * check to see if any remote browse sync child processes have completed
465 sync_check_completion();
468 * regularly sync with any other DMBs we know about
470 sync_all_dmbs(t);
473 * Reload the services file if we got a sighup.
476 if(reload_after_sighup) {
477 reload_services( True );
478 reopen_logs();
479 reload_interfaces(0);
480 reload_after_sighup = False;
483 /* check for new network interfaces */
484 reload_interfaces(t);
486 /* free up temp memory */
487 lp_talloc_free();
489 } /* process */
492 /**************************************************************************** **
493 open the socket communication
494 **************************************************************************** */
495 static BOOL open_sockets(BOOL isdaemon, int port)
497 /* The sockets opened here will be used to receive broadcast
498 packets *only*. Interface specific sockets are opened in
499 make_subnet() in namedbsubnet.c. Thus we bind to the
500 address "0.0.0.0". The parameter 'socket address' is
501 now deprecated.
504 if ( isdaemon )
505 ClientNMB = open_socket_in(SOCK_DGRAM, port,0,0,True);
506 else
507 ClientNMB = 0;
509 ClientDGRAM = open_socket_in(SOCK_DGRAM,DGRAM_PORT,3,0,True);
511 if ( ClientNMB == -1 )
512 return( False );
514 /* we are never interested in SIGPIPE */
515 BlockSignals(True,SIGPIPE);
517 set_socket_options( ClientNMB, "SO_BROADCAST" );
518 set_socket_options( ClientDGRAM, "SO_BROADCAST" );
520 DEBUG( 3, ( "open_sockets: Broadcast sockets opened.\n" ) );
521 return( True );
522 } /* open_sockets */
525 /**************************************************************************** **
526 initialise connect, service and file structs
527 **************************************************************************** */
528 static BOOL init_structs(void)
530 extern fstring local_machine;
531 char *p, *ptr;
532 int namecount;
533 int n;
534 int nodup;
535 pstring nbname;
537 if (! *global_myname)
539 fstrcpy( global_myname, myhostname() );
540 p = strchr( global_myname, '.' );
541 if (p)
542 *p = 0;
544 strupper( global_myname );
546 /* Add any NETBIOS name aliases. Ensure that the first entry
547 is equal to global_myname.
549 /* Work out the max number of netbios aliases that we have */
550 ptr = lp_netbios_aliases();
551 for( namecount=0; next_token(&ptr,nbname,NULL, sizeof(nbname)); namecount++ )
553 if ( *global_myname )
554 namecount++;
556 /* Allocate space for the netbios aliases */
557 my_netbios_names = (char **)malloc( sizeof(char *) * (namecount+1) );
558 if( NULL == my_netbios_names )
560 DEBUG( 0, ( "init_structs: malloc fail.\n" ) );
561 return( False );
564 /* Use the global_myname string first */
565 namecount=0;
566 if ( *global_myname )
567 my_netbios_names[namecount++] = global_myname;
569 ptr = lp_netbios_aliases();
570 while ( next_token( &ptr, nbname, NULL, sizeof(nbname) ) )
572 strupper( nbname );
573 /* Look for duplicates */
574 nodup=1;
575 for( n=0; n<namecount; n++ )
577 if( 0 == strcmp( nbname, my_netbios_names[n] ) )
578 nodup=0;
580 if (nodup)
581 my_netbios_names[namecount++] = strdup( nbname );
584 /* Check the strdups succeeded. */
585 for( n = 0; n < namecount; n++ )
586 if( NULL == my_netbios_names[n] )
588 DEBUG(0,("init_structs: malloc fail when allocating names.\n"));
589 return False;
592 /* Terminate name list */
593 my_netbios_names[namecount++] = NULL;
595 fstrcpy( local_machine, global_myname );
596 trim_string( local_machine, " ", " " );
597 p = strchr( local_machine, ' ' );
598 if (p)
599 *p = 0;
600 strlower( local_machine );
602 DEBUG( 5, ("Netbios name list:-\n") );
603 for( n=0; my_netbios_names[n]; n++ )
604 DEBUGADD( 5, ( "my_netbios_names[%d]=\"%s\"\n", n, my_netbios_names[n] ) );
606 return( True );
607 } /* init_structs */
609 /**************************************************************************** **
610 usage on the program
611 **************************************************************************** */
612 static void usage(char *pname)
615 printf( "Usage: %s [-DaohV] [-H lmhosts file] [-d debuglevel] [-l log basename]\n", pname );
616 printf( " [-n name] [-p port] [-s configuration file]\n" );
617 printf( "\t-D Become a daemon\n" );
618 printf( "\t-a Append to log file (default)\n" );
619 printf( "\t-o Overwrite log file, don't append\n" );
620 printf( "\t-h Print usage\n" );
621 printf( "\t-V Print version\n" );
622 printf( "\t-H hosts file Load a netbios hosts file\n" );
623 printf( "\t-d debuglevel Set the debuglevel\n" );
624 printf( "\t-l log basename. Basename for log/debug files\n" );
625 printf( "\t-n netbiosname. Primary netbios name\n" );
626 printf( "\t-p port Listen on the specified port\n" );
627 printf( "\t-s configuration file Configuration file name\n" );
628 printf( "\n");
629 } /* usage */
632 /**************************************************************************** **
633 main program
634 **************************************************************************** */
635 int main(int argc,char *argv[])
637 int opt;
638 extern FILE *dbf;
639 extern char *optarg;
640 extern BOOL append_log;
642 append_log = True; /* Default, override with '-o' option. */
644 global_nmb_port = NMB_PORT;
645 *host_file = 0;
646 global_in_nmbd = True;
648 StartupTime = time(NULL);
650 sys_srandom(time(NULL) ^ getpid());
652 TimeInit();
654 pstrcpy( debugf, NMBLOGFILE );
656 setup_logging( argv[0], False );
658 charset_initialise();
660 #ifdef LMHOSTSFILE
661 pstrcpy( host_file, LMHOSTSFILE );
662 #endif
664 /* this is for people who can't start the program correctly */
665 while (argc > 1 && (*argv[1] != '-'))
667 argv++;
668 argc--;
671 fault_setup((void (*)(void *))fault_continue );
673 CatchSignal( SIGHUP, SIGNAL_CAST sig_hup );
674 CatchSignal( SIGTERM, SIGNAL_CAST sig_term );
676 #if defined(SIGFPE)
677 /* we are never interested in SIGFPE */
678 BlockSignals(True,SIGFPE);
679 #endif
681 /* Setup the signals that allow the debug log level
682 to by dynamically changed. */
684 /* If we are using the malloc debug code we can't use
685 SIGUSR1 and SIGUSR2 to do debug level changes. */
686 #ifndef MEM_MAN
687 #if defined(SIGUSR1)
688 CatchSignal( SIGUSR1, SIGNAL_CAST sig_usr1 );
689 #endif /* SIGUSR1 */
691 #if defined(SIGUSR2)
692 CatchSignal( SIGUSR2, SIGNAL_CAST sig_usr2 );
693 #endif /* SIGUSR2 */
694 #endif /* MEM_MAN */
696 while( EOF !=
697 (opt = getopt( argc, argv, "Vaos:T:I:C:bAi:B:N:Rn:l:d:Dp:hSH:G:f:" )) )
699 switch (opt)
701 case 's':
702 pstrcpy(servicesf,optarg);
703 break;
704 case 'N':
705 case 'B':
706 case 'I':
707 case 'C':
708 case 'G':
709 DEBUG(0,("Obsolete option '%c' used\n",opt));
710 break;
711 case 'H':
712 pstrcpy(host_file,optarg);
713 break;
714 case 'n':
715 pstrcpy(global_myname,optarg);
716 strupper(global_myname);
717 break;
718 case 'l':
719 slprintf(debugf,sizeof(debugf)-1, "%s.nmb",optarg);
720 break;
721 case 'a':
722 append_log = True;
723 break;
724 case 'o':
725 append_log = False;
726 break;
727 case 'D':
728 is_daemon = True;
729 break;
730 case 'd':
731 DEBUGLEVEL = atoi(optarg);
732 break;
733 case 'p':
734 global_nmb_port = atoi(optarg);
735 break;
736 case 'h':
737 usage(argv[0]);
738 exit(0);
739 break;
740 case 'V':
741 printf( "Version %s\n", VERSION );
742 exit(0);
743 break;
744 default:
745 if( !is_a_socket(0) )
747 DEBUG(0,("Incorrect program usage - is the command line correct?\n"));
748 usage(argv[0]);
749 exit(0);
751 break;
755 reopen_logs();
757 DEBUG( 1, ( "Netbios nameserver version %s started.\n", VERSION ) );
758 DEBUGADD( 1, ( "Copyright Andrew Tridgell 1994-1998\n" ) );
760 if ( !reload_services(False) )
761 return(-1);
763 codepage_initialise(lp_client_code_page());
765 if(!init_structs())
766 return -1;
768 reload_services( True );
770 fstrcpy( global_myworkgroup, lp_workgroup() );
772 if (strequal(global_myworkgroup,"*"))
774 DEBUG(0,("ERROR: a workgroup name of * is no longer supported\n"));
775 exit(1);
778 set_samba_nb_type();
780 if (!is_daemon && !is_a_socket(0))
782 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
783 is_daemon = True;
786 if (is_daemon)
788 DEBUG( 2, ( "Becoming a daemon.\n" ) );
789 become_daemon();
792 #ifndef SYNC_DNS
793 /* Setup the async dns. We do it here so it doesn't have all the other
794 stuff initialised and thus chewing memory and sockets */
795 if(lp_we_are_a_wins_server()) {
796 start_async_dns();
798 #endif
800 if (!directory_exist(lp_lockdir(), NULL)) {
801 mkdir(lp_lockdir(), 0755);
804 pidfile_create("nmbd");
806 DEBUG( 3, ( "Opening sockets %d\n", global_nmb_port ) );
808 if ( !open_sockets( is_daemon, global_nmb_port ) )
809 return 1;
811 /* Determine all the IP addresses we have. */
812 load_interfaces();
814 /* Create an nmbd subnet record for each of the above. */
815 if( False == create_subnets() )
817 DEBUG(0,("ERROR: Failed when creating subnet lists. Exiting.\n"));
818 exit(1);
821 /* Load in any static local names. */
822 if ( *host_file )
824 load_lmhosts_file(host_file);
825 DEBUG(3,("Loaded hosts file\n"));
828 /* If we are acting as a WINS server, initialise data structures. */
829 if( !initialise_wins() )
831 DEBUG( 0, ( "nmbd: Failed when initialising WINS server.\n" ) );
832 exit(1);
836 * Register nmbd primary workgroup and nmbd names on all
837 * the broadcast subnets, and on the WINS server (if specified).
838 * Also initiate the startup of our primary workgroup (start
839 * elections if we are setup as being able to be a local
840 * master browser.
843 if( False == register_my_workgroup_and_names() )
845 DEBUG(0,("ERROR: Failed when creating my my workgroup. Exiting.\n"));
846 exit(1);
849 /* We can only take signals in the select. */
850 BlockSignals( True, SIGTERM );
851 #if defined(SIGUSR1)
852 BlockSignals( True, SIGUSR1);
853 #endif /* SIGUSR1 */
854 #if defined(SIGUSR2)
855 BlockSignals( True, SIGUSR2);
856 #endif /* SIGUSR2 */
858 process();
859 close_sockets();
861 if (dbf)
862 fclose(dbf);
863 return(0);
864 } /* main */