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) 7 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. 12 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. 17 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. 21 22*/ 23 24#include "includes.h" 25 26int ClientNMB = -1; 27int ClientDGRAM = -1; 28int global_nmb_port = -1; 29 30extern BOOL global_in_nmbd; 31 32extern BOOL override_logfile; 33 34/* are we running as a daemon ? */ 35static BOOL is_daemon; 36 37/* fork or run in foreground ? */ 38static BOOL Fork = True; 39 40/* log to standard output ? */ 41static BOOL log_stdout; 42 43/* have we found LanMan clients yet? */ 44BOOL found_lm_clients = False; 45 46/* what server type are we currently */ 47 48time_t StartupTime = 0; 49 50/**************************************************************************** ** 51 Handle a SIGTERM in band. 52 **************************************************************************** */ 53 54static void terminate(void) 55{ 56 DEBUG(0,("Got SIGTERM: going down...\n")); 57 58 /* Write out wins.dat file if samba is a WINS server */ 59 wins_write_database(False); 60 61 /* Remove all SELF registered names from WINS */ 62 release_wins_names(); 63 64 /* Announce all server entries as 0 time-to-live, 0 type. */ 65 announce_my_servers_removed(); 66 67 /* If there was an async dns child - kill it. */ 68 kill_async_dns_child(); 69 70 exit(0); 71} 72 73/**************************************************************************** ** 74 Handle a SHUTDOWN message from smbcontrol. 75 **************************************************************************** */ 76 77static void nmbd_terminate(int msg_type, pid_t src, void *buf, size_t len) 78{ 79 terminate(); 80} 81 82/**************************************************************************** ** 83 Catch a SIGTERM signal. 84 **************************************************************************** */ 85 86static SIG_ATOMIC_T got_sig_term; 87 88static void sig_term(int sig) 89{ 90 got_sig_term = 1; 91 sys_select_signal(); 92} 93 94/**************************************************************************** ** 95 Catch a SIGHUP signal. 96 **************************************************************************** */ 97 98static SIG_ATOMIC_T reload_after_sighup; 99 100static void sig_hup(int sig) 101{ 102 reload_after_sighup = 1; 103 sys_select_signal(); 104} 105 106#if DUMP_CORE 107/**************************************************************************** ** 108 Prepare to dump a core file - carefully! 109 **************************************************************************** */ 110 111static BOOL dump_core(void) 112{ 113 char *p; 114 pstring dname; 115 pstrcpy( dname, lp_logfile() ); 116 if ((p=strrchr_m(dname,'/'))) 117 *p=0; 118 pstrcat( dname, "/corefiles" ); 119 mkdir( dname, 0700 ); 120 sys_chown( dname, getuid(), getgid() ); 121 chmod( dname, 0700 ); 122 if ( chdir(dname) ) 123 return( False ); 124 umask( ~(0700) ); 125 126#ifdef HAVE_GETRLIMIT 127#ifdef RLIMIT_CORE 128 { 129 struct rlimit rlp; 130 getrlimit( RLIMIT_CORE, &rlp ); 131 rlp.rlim_cur = MAX( 4*1024*1024, rlp.rlim_cur ); 132 setrlimit( RLIMIT_CORE, &rlp ); 133 getrlimit( RLIMIT_CORE, &rlp ); 134 DEBUG( 3, ( "Core limits now %d %d\n", (int)rlp.rlim_cur, (int)rlp.rlim_max ) ); 135 } 136#endif 137#endif 138 139 140 DEBUG(0,("Dumping core in %s\n",dname)); 141 abort(); 142 return( True ); 143} 144#endif 145 146/**************************************************************************** ** 147 Possibly continue after a fault. 148 **************************************************************************** */ 149 150static void fault_continue(void) 151{ 152#if DUMP_CORE 153 dump_core(); 154#endif 155} 156 157/**************************************************************************** ** 158 Expire old names from the namelist and server list. 159 **************************************************************************** */ 160 161static void expire_names_and_servers(time_t t) 162{ 163 static time_t lastrun = 0; 164 165 if ( !lastrun ) 166 lastrun = t; 167 if ( t < (lastrun + 5) ) 168 return; 169 lastrun = t; 170 171 /* 172 * Expire any timed out names on all the broadcast 173 * subnets and those registered with the WINS server. 174 * (nmbd_namelistdb.c) 175 */ 176 177 expire_names(t); 178 179 /* 180 * Go through all the broadcast subnets and for each 181 * workgroup known on that subnet remove any expired 182 * server names. If a workgroup has an empty serverlist 183 * and has itself timed out then remove the workgroup. 184 * (nmbd_workgroupdb.c) 185 */ 186 187 expire_workgroups_and_servers(t); 188} 189 190/************************************************************************** ** 191 Reload the list of network interfaces. 192 ************************************************************************** */ 193 194static BOOL reload_interfaces(time_t t) 195{ 196 static time_t lastt; 197 int n; 198 struct subnet_record *subrec; 199 extern BOOL rescan_listen_set; 200 extern struct in_addr loopback_ip; 201 202 if (t && ((t - lastt) < NMBD_INTERFACES_RELOAD)) return False; 203 lastt = t; 204 205 if (!interfaces_changed()) return False; 206 207 /* the list of probed interfaces has changed, we may need to add/remove 208 some subnets */ 209 load_interfaces(); 210 211 /* find any interfaces that need adding */ 212 for (n=iface_count() - 1; n >= 0; n--) { 213 struct interface *iface = get_interface(n); 214 215 /* 216 * We don't want to add a loopback interface, in case 217 * someone has added 127.0.0.1 for smbd, nmbd needs to 218 * ignore it here. JRA. 219 */ 220 221 if (ip_equal(iface->ip, loopback_ip)) { 222 DEBUG(2,("reload_interfaces: Ignoring loopback interface %s\n", inet_ntoa(iface->ip))); 223 continue; 224 } 225 226 for (subrec=subnetlist; subrec; subrec=subrec->next) { 227 if (ip_equal(iface->ip, subrec->myip) && 228 ip_equal(iface->nmask, subrec->mask_ip)) break; 229 } 230 231 if (!subrec) { 232 /* it wasn't found! add it */ 233 DEBUG(2,("Found new interface %s\n", 234 inet_ntoa(iface->ip))); 235 subrec = make_normal_subnet(iface); 236 if (subrec) 237 register_my_workgroup_one_subnet(subrec); 238 } 239 } 240 241 /* find any interfaces that need deleting */ 242 for (subrec=subnetlist; subrec; subrec=subrec->next) { 243 for (n=iface_count() - 1; n >= 0; n--) { 244 struct interface *iface = get_interface(n); 245 if (ip_equal(iface->ip, subrec->myip) && 246 ip_equal(iface->nmask, subrec->mask_ip)) break; 247 } 248 if (n == -1) { 249 /* oops, an interface has disapeared. This is 250 tricky, we don't dare actually free the 251 interface as it could be being used, so 252 instead we just wear the memory leak and 253 remove it from the list of interfaces without 254 freeing it */ 255 DEBUG(2,("Deleting dead interface %s\n", 256 inet_ntoa(subrec->myip))); 257 close_subnet(subrec); 258 } 259 } 260 261 rescan_listen_set = True; 262 263 /* We need to shutdown if there are no subnets... */ 264 if (FIRST_SUBNET == NULL) { 265 DEBUG(0,("reload_interfaces: No subnets to listen to. Shutting down...\n")); 266 return True; 267 } 268 return False; 269} 270 271/**************************************************************************** ** 272 Reload the services file. 273 **************************************************************************** */ 274 275static BOOL reload_nmbd_services(BOOL test) 276{ 277 BOOL ret; 278 279 set_remote_machine_name("nmbd", False); 280 281 if ( lp_loaded() ) { 282 pstring fname; 283 pstrcpy( fname,lp_configfile()); 284 if (file_exist(fname,NULL) && !strcsequal(fname,dyn_CONFIGFILE)) { 285 pstrcpy(dyn_CONFIGFILE,fname); 286 test = False; 287 } 288 } 289 290 if ( test && !lp_file_list_changed() ) 291 return(True); 292 293 ret = lp_load( dyn_CONFIGFILE, True , False, False); 294 295 /* perhaps the config filename is now set */ 296 if ( !test ) { 297 DEBUG( 3, ( "services not loaded\n" ) ); 298 reload_nmbd_services( True ); 299 } 300 301 return(ret); 302} 303 304/**************************************************************************** ** 305 * React on 'smbcontrol nmbd reload-config' in the same way as to SIGHUP 306 * We use buf here to return BOOL result to process() when reload_interfaces() 307 * detects that there are no subnets. 308 **************************************************************************** */ 309 310static void msg_reload_nmbd_services(int msg_type, pid_t src, void *buf, size_t len) 311{ 312 write_browse_list( 0, True ); 313 dump_all_namelists(); 314 reload_nmbd_services( True ); 315 reopen_logs(); 316 317 if(buf) { 318 /* We were called from process() */ 319 /* If reload_interfaces() returned True */ 320 /* we need to shutdown if there are no subnets... */ 321 /* pass this info back to process() */ 322 *((BOOL*)buf) = reload_interfaces(0); 323 } 324} 325 326 327/**************************************************************************** ** 328 The main select loop. 329 **************************************************************************** */ 330 331static void process(void) 332{ 333 BOOL run_election; 334 BOOL no_subnets; 335 336 while( True ) { 337 time_t t = time(NULL); 338 339 /* Check for internal messages */ 340 341 message_dispatch(); 342 343 /* 344 * Check all broadcast subnets to see if 345 * we need to run an election on any of them. 346 * (nmbd_elections.c) 347 */ 348 349 run_election = check_elections(); 350 351 /* 352 * Read incoming UDP packets. 353 * (nmbd_packets.c) 354 */ 355 356 if(listen_for_packets(run_election)) 357 return; 358 359 /* 360 * Handle termination inband. 361 */ 362 363 if (got_sig_term) { 364 got_sig_term = 0; 365 terminate(); 366 } 367 368 /* 369 * Process all incoming packets 370 * read above. This calls the success and 371 * failure functions registered when response 372 * packets arrrive, and also deals with request 373 * packets from other sources. 374 * (nmbd_packets.c) 375 */ 376 377 run_packet_queue(); 378 379 /* 380 * Run any elections - initiate becoming 381 * a local master browser if we have won. 382 * (nmbd_elections.c) 383 */ 384 385 run_elections(t); 386 387 /* 388 * Send out any broadcast announcements 389 * of our server names. This also announces 390 * the workgroup name if we are a local 391 * master browser. 392 * (nmbd_sendannounce.c) 393 */ 394 395 announce_my_server_names(t); 396 397 /* 398 * Send out any LanMan broadcast announcements 399 * of our server names. 400 * (nmbd_sendannounce.c) 401 */ 402 403 announce_my_lm_server_names(t); 404 405 /* 406 * If we are a local master browser, periodically 407 * announce ourselves to the domain master browser. 408 * This also deals with syncronising the domain master 409 * browser server lists with ourselves as a local 410 * master browser. 411 * (nmbd_sendannounce.c) 412 */ 413 414 announce_myself_to_domain_master_browser(t); 415 416 /* 417 * Fullfill any remote announce requests. 418 * (nmbd_sendannounce.c) 419 */ 420 421 announce_remote(t); 422 423 /* 424 * Fullfill any remote browse sync announce requests. 425 * (nmbd_sendannounce.c) 426 */ 427 428 browse_sync_remote(t); 429 430 /* 431 * Scan the broadcast subnets, and WINS client 432 * namelists and refresh any that need refreshing. 433 * (nmbd_mynames.c) 434 */ 435 436 refresh_my_names(t); 437 438 /* 439 * Scan the subnet namelists and server lists and 440 * expire thos that have timed out. 441 * (nmbd.c) 442 */ 443 444 expire_names_and_servers(t); 445 446 /* 447 * Write out a snapshot of our current browse list into 448 * the browse.dat file. This is used by smbd to service 449 * incoming NetServerEnum calls - used to synchronise 450 * browse lists over subnets. 451 * (nmbd_serverlistdb.c) 452 */ 453 454 write_browse_list(t, False); 455 456 /* 457 * If we are a domain master browser, we have a list of 458 * local master browsers we should synchronise browse 459 * lists with (these are added by an incoming local 460 * master browser announcement packet). Expire any of 461 * these that are no longer current, and pull the server 462 * lists from each of these known local master browsers. 463 * (nmbd_browsesync.c) 464 */ 465 466 dmb_expire_and_sync_browser_lists(t); 467 468 /* 469 * Check that there is a local master browser for our 470 * workgroup for all our broadcast subnets. If one 471 * is not found, start an election (which we ourselves 472 * may or may not participate in, depending on the 473 * setting of the 'local master' parameter. 474 * (nmbd_elections.c) 475 */ 476 477 check_master_browser_exists(t); 478 479 /* 480 * If we are configured as a logon server, attempt to 481 * register the special NetBIOS names to become such 482 * (WORKGROUP<1c> name) on all broadcast subnets and 483 * with the WINS server (if used). If we are configured 484 * to become a domain master browser, attempt to register 485 * the special NetBIOS name (WORKGROUP<1b> name) to 486 * become such. 487 * (nmbd_become_dmb.c) 488 */ 489 490 add_domain_names(t); 491 492 /* 493 * If we are a WINS server, do any timer dependent 494 * processing required. 495 * (nmbd_winsserver.c) 496 */ 497 498 initiate_wins_processing(t); 499 500 /* 501 * If we are a domain master browser, attempt to contact the 502 * WINS server to get a list of all known WORKGROUPS/DOMAINS. 503 * This will only work to a Samba WINS server. 504 * (nmbd_browsesync.c) 505 */ 506 507 if (lp_enhanced_browsing()) 508 collect_all_workgroup_names_from_wins_server(t); 509 510 /* 511 * Go through the response record queue and time out or re-transmit 512 * and expired entries. 513 * (nmbd_packets.c) 514 */ 515 516 retransmit_or_expire_response_records(t); 517 518 /* 519 * check to see if any remote browse sync child processes have completed 520 */ 521 522 sync_check_completion(); 523 524 /* 525 * regularly sync with any other DMBs we know about 526 */ 527 528 if (lp_enhanced_browsing()) 529 sync_all_dmbs(t); 530 531 /* 532 * clear the unexpected packet queue 533 */ 534 535 clear_unexpected(t); 536 537 /* 538 * Reload the services file if we got a sighup. 539 */ 540 541 if(reload_after_sighup) { 542 DEBUG( 0, ( "Got SIGHUP dumping debug info.\n" ) ); 543 msg_reload_nmbd_services(MSG_SMB_CONF_UPDATED, (pid_t) 0, (void*) &no_subnets, 0); 544 if(no_subnets) 545 return; 546 reload_after_sighup = 0; 547 } 548 549 /* check for new network interfaces */ 550 551 if(reload_interfaces(t)) 552 return; 553 554 /* free up temp memory */ 555 lp_talloc_free(); 556 } 557} 558 559/**************************************************************************** ** 560 Open the socket communication. 561 **************************************************************************** */ 562 563static BOOL open_sockets(BOOL isdaemon, int port) 564{ 565 /* 566 * The sockets opened here will be used to receive broadcast 567 * packets *only*. Interface specific sockets are opened in 568 * make_subnet() in namedbsubnet.c. Thus we bind to the 569 * address "0.0.0.0". The parameter 'socket address' is 570 * now deprecated. 571 */ 572 573 if ( isdaemon ) 574 ClientNMB = open_socket_in(SOCK_DGRAM, port, 575 0, interpret_addr(lp_socket_address()), 576 True); 577 else 578 ClientNMB = 0; 579 580 ClientDGRAM = open_socket_in(SOCK_DGRAM,DGRAM_PORT,3,0,True); 581 582 if ( ClientNMB == -1 ) 583 return( False ); 584 585 /* we are never interested in SIGPIPE */ 586 BlockSignals(True,SIGPIPE); 587 588 set_socket_options( ClientNMB, "SO_BROADCAST" ); 589 set_socket_options( ClientDGRAM, "SO_BROADCAST" ); 590 591 DEBUG( 3, ( "open_sockets: Broadcast sockets opened.\n" ) ); 592 return( True ); 593} 594 595/**************************************************************************** ** 596 main program 597 **************************************************************************** */ 598 int main(int argc, const char *argv[]) 599{ 600 pstring logfile; 601 static BOOL opt_interactive; 602 poptContext pc; 603 int opt; 604 struct poptOption long_options[] = { 605 POPT_AUTOHELP 606 {"daemon", 'D', POPT_ARG_VAL, &is_daemon, True, "Become a daemon(default)" }, 607 {"interactive", 'i', POPT_ARG_VAL, &opt_interactive, True, "Run interactive (not a daemon)" }, 608 {"foreground", 'F', POPT_ARG_VAL, &Fork, False, "Run daemon in foreground (for daemontools & etc)" }, 609 {"log-stdout", 'S', POPT_ARG_VAL, &log_stdout, True, "Log to stdout" }, 610 {"hosts", 'H', POPT_ARG_STRING, dyn_LMHOSTSFILE, 'H', "Load a netbios hosts file"}, 611 {"port", 'p', POPT_ARG_INT, &global_nmb_port, NMB_PORT, "Listen on the specified port" }, 612 POPT_COMMON_SAMBA 613 { NULL } 614 }; 615 616 global_nmb_port = NMB_PORT; 617 618 pc = poptGetContext("nmbd", argc, argv, long_options, 0); 619 while ((opt = poptGetNextOpt(pc)) != -1) ; 620 poptFreeContext(pc); 621 622 global_in_nmbd = True; 623 624 StartupTime = time(NULL); 625 626 sys_srandom(time(NULL) ^ sys_getpid()); 627 628 if (!override_logfile) { 629 slprintf(logfile, sizeof(logfile)-1, "%s/log.nmbd", dyn_LOGFILEBASE); 630 lp_set_logfile(logfile); 631 } 632 633 fault_setup((void (*)(void *))fault_continue ); 634 635 /* POSIX demands that signals are inherited. If the invoking process has 636 * these signals masked, we will have problems, as we won't receive them. */ 637 BlockSignals(False, SIGHUP); 638 BlockSignals(False, SIGUSR1); 639 BlockSignals(False, SIGTERM); 640 641 CatchSignal( SIGHUP, SIGNAL_CAST sig_hup ); 642 CatchSignal( SIGTERM, SIGNAL_CAST sig_term ); 643 644#if defined(SIGFPE) 645 /* we are never interested in SIGFPE */ 646 BlockSignals(True,SIGFPE); 647#endif 648 649 /* We no longer use USR2... */ 650#if defined(SIGUSR2) 651 BlockSignals(True, SIGUSR2); 652#endif 653 654 if ( opt_interactive ) { 655 Fork = False; 656 log_stdout = True; 657 } 658 659 if ( log_stdout && Fork ) { 660 DEBUG(0,("ERROR: Can't log to stdout (-S) unless daemon is in foreground (-F) or interactive (-i)\n")); 661 exit(1); 662 } 663 664 setup_logging( argv[0], log_stdout ); 665 666 reopen_logs(); 667 668 DEBUG( 0, ( "Netbios nameserver version %s started.\n", SAMBA_VERSION_STRING) ); 669 DEBUGADD( 0, ( "Copyright Andrew Tridgell and the Samba Team 1994-2004\n" ) ); 670 671 if ( !reload_nmbd_services(False) ) 672 return(-1); 673 674 if(!init_names()) 675 return -1; 676 677 reload_nmbd_services( True ); 678 679 if (strequal(lp_workgroup(),"*")) { 680 DEBUG(0,("ERROR: a workgroup name of * is no longer supported\n")); 681 exit(1); 682 } 683 684 set_samba_nb_type(); 685 686 if (!is_daemon && !is_a_socket(0)) { 687 DEBUG(0,("standard input is not a socket, assuming -D option\n")); 688 is_daemon = True; 689 } 690 691 if (is_daemon && !opt_interactive) { 692 DEBUG( 2, ( "Becoming a daemon.\n" ) ); 693 become_daemon(Fork); 694 } 695 696#if HAVE_SETPGID 697 /* 698 * If we're interactive we want to set our own process group for 699 * signal management. 700 */ 701 if (opt_interactive) 702 setpgid( (pid_t)0, (pid_t)0 ); 703#endif 704 705#ifndef SYNC_DNS 706 /* Setup the async dns. We do it here so it doesn't have all the other 707 stuff initialised and thus chewing memory and sockets */ 708 if(lp_we_are_a_wins_server() && lp_dns_proxy()) { 709 start_async_dns(); 710 } 711#endif 712 713 if (!directory_exist(lp_lockdir(), NULL)) { 714 mkdir(lp_lockdir(), 0755); 715 } 716 717 pidfile_create("nmbd"); 718 message_init(); 719 message_register(MSG_FORCE_ELECTION, nmbd_message_election); 720 message_register(MSG_WINS_NEW_ENTRY, nmbd_wins_new_entry); 721 message_register(MSG_SHUTDOWN, nmbd_terminate); 722 message_register(MSG_SMB_CONF_UPDATED, msg_reload_nmbd_services); 723 724 DEBUG( 3, ( "Opening sockets %d\n", global_nmb_port ) ); 725 726 if ( !open_sockets( is_daemon, global_nmb_port ) ) { 727 kill_async_dns_child(); 728 return 1; 729 } 730 731 /* Determine all the IP addresses we have. */ 732 load_interfaces(); 733 734 /* Create an nmbd subnet record for each of the above. */ 735 if( False == create_subnets() ) { 736 DEBUG(0,("ERROR: Failed when creating subnet lists. Exiting.\n")); 737 kill_async_dns_child(); 738 exit(1); 739 } 740 741 /* Load in any static local names. */ 742 load_lmhosts_file(dyn_LMHOSTSFILE); 743 DEBUG(3,("Loaded hosts file %s\n", dyn_LMHOSTSFILE)); 744 745 /* If we are acting as a WINS server, initialise data structures. */ 746 if( !initialise_wins() ) { 747 DEBUG( 0, ( "nmbd: Failed when initialising WINS server.\n" ) ); 748 kill_async_dns_child(); 749 exit(1); 750 } 751 752 /* 753 * Register nmbd primary workgroup and nmbd names on all 754 * the broadcast subnets, and on the WINS server (if specified). 755 * Also initiate the startup of our primary workgroup (start 756 * elections if we are setup as being able to be a local 757 * master browser. 758 */ 759 760 if( False == register_my_workgroup_and_names() ) { 761 DEBUG(0,("ERROR: Failed when creating my my workgroup. Exiting.\n")); 762 kill_async_dns_child(); 763 exit(1); 764 } 765 766 /* We can only take signals in the select. */ 767 BlockSignals( True, SIGTERM ); 768 769 process(); 770 771 if (dbf) 772 x_fclose(dbf); 773 kill_async_dns_child(); 774 return(0); 775} 776