source: trunk/server/source3/nmbd/nmbd.c@ 751

Last change on this file since 751 was 751, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.9

File size: 26.2 KB
Line 
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 3 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, see <http://www.gnu.org/licenses/>.
20*/
21
22#include "includes.h"
23#include "system/filesys.h"
24#include "popt_common.h"
25#include "nmbd/nmbd.h"
26#include "serverid.h"
27#include "messages.h"
28
29int ClientNMB = -1;
30int ClientDGRAM = -1;
31int global_nmb_port = -1;
32
33extern bool rescan_listen_set;
34extern bool global_in_nmbd;
35
36#ifdef __OS2__
37extern int global_Sem32Add;
38#endif
39
40extern bool override_logfile;
41
42/* have we found LanMan clients yet? */
43bool found_lm_clients = False;
44
45/* what server type are we currently */
46
47time_t StartupTime = 0;
48
49struct event_context *nmbd_event_context(void)
50{
51 return server_event_context();
52}
53
54struct messaging_context *nmbd_messaging_context(void)
55{
56 struct messaging_context *msg_ctx = server_messaging_context();
57 if (likely(msg_ctx != NULL)) {
58 return msg_ctx;
59 }
60 smb_panic("Could not init nmbd's messaging context.\n");
61 return NULL;
62}
63
64/**************************************************************************** **
65 Handle a SIGTERM in band.
66 **************************************************************************** */
67
68static void terminate(void)
69{
70 DEBUG(0,("Got SIGTERM: going down...\n"));
71
72 /* Write out wins.dat file if samba is a WINS server */
73 wins_write_database(0,False);
74
75 /* Remove all SELF registered names from WINS */
76 release_wins_names();
77
78 /* Announce all server entries as 0 time-to-live, 0 type. */
79 announce_my_servers_removed();
80
81 /* If there was an async dns child - kill it. */
82 kill_async_dns_child();
83
84 gencache_stabilize();
85 serverid_deregister(procid_self());
86
87 pidfile_unlink();
88
89 exit(0);
90}
91
92static void nmbd_sig_term_handler(struct tevent_context *ev,
93 struct tevent_signal *se,
94 int signum,
95 int count,
96 void *siginfo,
97 void *private_data)
98{
99 terminate();
100}
101
102static bool nmbd_setup_sig_term_handler(void)
103{
104 struct tevent_signal *se;
105
106 se = tevent_add_signal(nmbd_event_context(),
107 nmbd_event_context(),
108 SIGTERM, 0,
109 nmbd_sig_term_handler,
110 NULL);
111 if (!se) {
112 DEBUG(0,("failed to setup SIGTERM handler"));
113 return false;
114 }
115
116 return true;
117}
118
119static void msg_reload_nmbd_services(struct messaging_context *msg,
120 void *private_data,
121 uint32_t msg_type,
122 struct server_id server_id,
123 DATA_BLOB *data);
124
125static void nmbd_sig_hup_handler(struct tevent_context *ev,
126 struct tevent_signal *se,
127 int signum,
128 int count,
129 void *siginfo,
130 void *private_data)
131{
132 DEBUG(0,("Got SIGHUP dumping debug info.\n"));
133 msg_reload_nmbd_services(nmbd_messaging_context(),
134 NULL, MSG_SMB_CONF_UPDATED,
135 procid_self(), NULL);
136}
137
138static bool nmbd_setup_sig_hup_handler(void)
139{
140 struct tevent_signal *se;
141
142 se = tevent_add_signal(nmbd_event_context(),
143 nmbd_event_context(),
144 SIGHUP, 0,
145 nmbd_sig_hup_handler,
146 NULL);
147 if (!se) {
148 DEBUG(0,("failed to setup SIGHUP handler"));
149 return false;
150 }
151
152 return true;
153}
154
155/**************************************************************************** **
156 Handle a SHUTDOWN message from smbcontrol.
157 **************************************************************************** */
158
159static void nmbd_terminate(struct messaging_context *msg,
160 void *private_data,
161 uint32_t msg_type,
162 struct server_id server_id,
163 DATA_BLOB *data)
164{
165 terminate();
166}
167
168/**************************************************************************** **
169 Possibly continue after a fault.
170 **************************************************************************** */
171
172static void fault_continue(void)
173{
174 dump_core();
175}
176
177/**************************************************************************** **
178 Expire old names from the namelist and server list.
179 **************************************************************************** */
180
181static void expire_names_and_servers(time_t t)
182{
183 static time_t lastrun = 0;
184
185 if ( !lastrun )
186 lastrun = t;
187 if ( t < (lastrun + 5) )
188 return;
189 lastrun = t;
190
191 /*
192 * Expire any timed out names on all the broadcast
193 * subnets and those registered with the WINS server.
194 * (nmbd_namelistdb.c)
195 */
196
197 expire_names(t);
198
199 /*
200 * Go through all the broadcast subnets and for each
201 * workgroup known on that subnet remove any expired
202 * server names. If a workgroup has an empty serverlist
203 * and has itself timed out then remove the workgroup.
204 * (nmbd_workgroupdb.c)
205 */
206
207 expire_workgroups_and_servers(t);
208}
209
210/************************************************************************** **
211 Reload the list of network interfaces.
212 Doesn't return until a network interface is up.
213 ************************************************************************** */
214
215static void reload_interfaces(time_t t)
216{
217 static time_t lastt;
218 int n;
219 bool print_waiting_msg = true;
220 struct subnet_record *subrec;
221
222 if (t && ((t - lastt) < NMBD_INTERFACES_RELOAD)) {
223 return;
224 }
225
226 lastt = t;
227
228 if (!interfaces_changed()) {
229 return;
230 }
231
232 try_again:
233
234 /* the list of probed interfaces has changed, we may need to add/remove
235 some subnets */
236 load_interfaces();
237
238 /* find any interfaces that need adding */
239 for (n=iface_count() - 1; n >= 0; n--) {
240 char str[INET6_ADDRSTRLEN];
241 const struct interface *iface = get_interface(n);
242 struct in_addr ip, nmask;
243
244 if (!iface) {
245 DEBUG(2,("reload_interfaces: failed to get interface %d\n", n));
246 continue;
247 }
248
249 /* Ensure we're only dealing with IPv4 here. */
250 if (iface->ip.ss_family != AF_INET) {
251 DEBUG(2,("reload_interfaces: "
252 "ignoring non IPv4 interface.\n"));
253 continue;
254 }
255
256 ip = ((struct sockaddr_in *)(void *)&iface->ip)->sin_addr;
257 nmask = ((struct sockaddr_in *)(void *)
258 &iface->netmask)->sin_addr;
259
260 /*
261 * We don't want to add a loopback interface, in case
262 * someone has added 127.0.0.1 for smbd, nmbd needs to
263 * ignore it here. JRA.
264 */
265
266 if (is_loopback_addr((struct sockaddr *)(void *)&iface->ip)) {
267 DEBUG(2,("reload_interfaces: Ignoring loopback "
268 "interface %s\n",
269 print_sockaddr(str, sizeof(str), &iface->ip) ));
270 continue;
271 }
272
273 for (subrec=subnetlist; subrec; subrec=subrec->next) {
274 if (ip_equal_v4(ip, subrec->myip) &&
275 ip_equal_v4(nmask, subrec->mask_ip)) {
276 break;
277 }
278 }
279
280 if (!subrec) {
281 /* it wasn't found! add it */
282 DEBUG(2,("Found new interface %s\n",
283 print_sockaddr(str,
284 sizeof(str), &iface->ip) ));
285 subrec = make_normal_subnet(iface);
286 if (subrec)
287 register_my_workgroup_one_subnet(subrec);
288 }
289 }
290
291 /* find any interfaces that need deleting */
292 for (subrec=subnetlist; subrec; subrec=subrec->next) {
293 for (n=iface_count() - 1; n >= 0; n--) {
294 struct interface *iface = get_interface(n);
295 struct in_addr ip, nmask;
296 if (!iface) {
297 continue;
298 }
299 /* Ensure we're only dealing with IPv4 here. */
300 if (iface->ip.ss_family != AF_INET) {
301 DEBUG(2,("reload_interfaces: "
302 "ignoring non IPv4 interface.\n"));
303 continue;
304 }
305 ip = ((struct sockaddr_in *)(void *)
306 &iface->ip)->sin_addr;
307 nmask = ((struct sockaddr_in *)(void *)
308 &iface->netmask)->sin_addr;
309 if (ip_equal_v4(ip, subrec->myip) &&
310 ip_equal_v4(nmask, subrec->mask_ip)) {
311 break;
312 }
313 }
314 if (n == -1) {
315 /* oops, an interface has disapeared. This is
316 tricky, we don't dare actually free the
317 interface as it could be being used, so
318 instead we just wear the memory leak and
319 remove it from the list of interfaces without
320 freeing it */
321 DEBUG(2,("Deleting dead interface %s\n",
322 inet_ntoa(subrec->myip)));
323 close_subnet(subrec);
324 }
325 }
326
327 rescan_listen_set = True;
328
329 /* We need to wait if there are no subnets... */
330 if (FIRST_SUBNET == NULL) {
331 void (*saved_handler)(int);
332
333 if (print_waiting_msg) {
334 DEBUG(0,("reload_interfaces: "
335 "No subnets to listen to. Waiting..\n"));
336 print_waiting_msg = false;
337 }
338
339 /*
340 * Whilst we're waiting for an interface, allow SIGTERM to
341 * cause us to exit.
342 */
343 saved_handler = CatchSignal(SIGTERM, SIG_DFL);
344
345 /* We only count IPv4, non-loopback interfaces here. */
346 while (iface_count_v4_nl() == 0) {
347 sleep(5);
348 load_interfaces();
349 }
350
351 CatchSignal(SIGTERM, saved_handler);
352
353 /*
354 * We got an interface, go back to blocking term.
355 */
356
357 goto try_again;
358 }
359}
360
361/**************************************************************************** **
362 Reload the services file.
363 **************************************************************************** */
364
365static bool reload_nmbd_services(bool test)
366{
367 bool ret;
368
369 set_remote_machine_name("nmbd", False);
370
371 if ( lp_loaded() ) {
372 char *fname = lp_configfile();
373 if (file_exist(fname) && !strcsequal(fname,get_dyn_CONFIGFILE())) {
374 set_dyn_CONFIGFILE(fname);
375 test = False;
376 }
377 TALLOC_FREE(fname);
378 }
379
380 if ( test && !lp_file_list_changed() )
381 return(True);
382
383 ret = lp_load(get_dyn_CONFIGFILE(), True , False, False, True);
384
385 /* perhaps the config filename is now set */
386 if ( !test ) {
387 DEBUG( 3, ( "services not loaded\n" ) );
388 reload_nmbd_services( True );
389 }
390
391#ifdef __OS2__
392 /* restore the logfile to log.nmbd always */
393 char *lfile = NULL;
394 if (asprintf(&lfile, "%s/log.nmbd", get_dyn_LOGFILEBASE()) < 0) {
395 exit(1);
396 }
397 lp_set_logfile(lfile);
398 SAFE_FREE(lfile);
399#endif
400
401 return(ret);
402}
403
404/**************************************************************************** **
405 * React on 'smbcontrol nmbd reload-config' in the same way as to SIGHUP
406 **************************************************************************** */
407
408static void msg_reload_nmbd_services(struct messaging_context *msg,
409 void *private_data,
410 uint32_t msg_type,
411 struct server_id server_id,
412 DATA_BLOB *data)
413{
414 write_browse_list( 0, True );
415 dump_all_namelists();
416 reload_nmbd_services( True );
417 reopen_logs();
418 reload_interfaces(0);
419}
420
421static void msg_nmbd_send_packet(struct messaging_context *msg,
422 void *private_data,
423 uint32_t msg_type,
424 struct server_id src,
425 DATA_BLOB *data)
426{
427 struct packet_struct *p = (struct packet_struct *)data->data;
428 struct subnet_record *subrec;
429 struct sockaddr_storage ss;
430 const struct sockaddr_storage *pss;
431 const struct in_addr *local_ip;
432
433 DEBUG(10, ("Received send_packet from %u\n", (unsigned int)procid_to_pid(&src)));
434
435 if (data->length != sizeof(struct packet_struct)) {
436 DEBUG(2, ("Discarding invalid packet length from %u\n",
437 (unsigned int)procid_to_pid(&src)));
438 return;
439 }
440
441 if ((p->packet_type != NMB_PACKET) &&
442 (p->packet_type != DGRAM_PACKET)) {
443 DEBUG(2, ("Discarding invalid packet type from %u: %d\n",
444 (unsigned int)procid_to_pid(&src), p->packet_type));
445 return;
446 }
447
448 in_addr_to_sockaddr_storage(&ss, p->ip);
449 pss = iface_ip((struct sockaddr *)(void *)&ss);
450
451 if (pss == NULL) {
452 DEBUG(2, ("Could not find ip for packet from %u\n",
453 (unsigned int)procid_to_pid(&src)));
454 return;
455 }
456
457 local_ip = &((const struct sockaddr_in *)pss)->sin_addr;
458 subrec = FIRST_SUBNET;
459
460 p->recv_fd = -1;
461 p->send_fd = (p->packet_type == NMB_PACKET) ?
462 subrec->nmb_sock : subrec->dgram_sock;
463
464 for (subrec = FIRST_SUBNET; subrec != NULL;
465 subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
466 if (ip_equal_v4(*local_ip, subrec->myip)) {
467 p->send_fd = (p->packet_type == NMB_PACKET) ?
468 subrec->nmb_sock : subrec->dgram_sock;
469 break;
470 }
471 }
472
473 if (p->packet_type == DGRAM_PACKET) {
474 p->port = 138;
475 p->packet.dgram.header.source_ip.s_addr = local_ip->s_addr;
476 p->packet.dgram.header.source_port = 138;
477 }
478
479 send_packet(p);
480}
481
482/**************************************************************************** **
483 The main select loop.
484 **************************************************************************** */
485
486static void process(void)
487{
488 bool run_election;
489
490 while( True ) {
491 time_t t = time(NULL);
492 TALLOC_CTX *frame = talloc_stackframe();
493
494 /*
495 * Check all broadcast subnets to see if
496 * we need to run an election on any of them.
497 * (nmbd_elections.c)
498 */
499
500 run_election = check_elections();
501
502 /*
503 * Read incoming UDP packets.
504 * (nmbd_packets.c)
505 */
506
507 if(listen_for_packets(run_election)) {
508 TALLOC_FREE(frame);
509 return;
510 }
511
512 /*
513 * Process all incoming packets
514 * read above. This calls the success and
515 * failure functions registered when response
516 * packets arrrive, and also deals with request
517 * packets from other sources.
518 * (nmbd_packets.c)
519 */
520
521 run_packet_queue();
522
523 /*
524 * Run any elections - initiate becoming
525 * a local master browser if we have won.
526 * (nmbd_elections.c)
527 */
528
529 run_elections(t);
530
531 /*
532 * Send out any broadcast announcements
533 * of our server names. This also announces
534 * the workgroup name if we are a local
535 * master browser.
536 * (nmbd_sendannounce.c)
537 */
538
539 announce_my_server_names(t);
540
541 /*
542 * Send out any LanMan broadcast announcements
543 * of our server names.
544 * (nmbd_sendannounce.c)
545 */
546
547 announce_my_lm_server_names(t);
548
549 /*
550 * If we are a local master browser, periodically
551 * announce ourselves to the domain master browser.
552 * This also deals with syncronising the domain master
553 * browser server lists with ourselves as a local
554 * master browser.
555 * (nmbd_sendannounce.c)
556 */
557
558 announce_myself_to_domain_master_browser(t);
559
560 /*
561 * Fullfill any remote announce requests.
562 * (nmbd_sendannounce.c)
563 */
564
565 announce_remote(t);
566
567 /*
568 * Fullfill any remote browse sync announce requests.
569 * (nmbd_sendannounce.c)
570 */
571
572 browse_sync_remote(t);
573
574 /*
575 * Scan the broadcast subnets, and WINS client
576 * namelists and refresh any that need refreshing.
577 * (nmbd_mynames.c)
578 */
579
580 refresh_my_names(t);
581
582 /*
583 * Scan the subnet namelists and server lists and
584 * expire thos that have timed out.
585 * (nmbd.c)
586 */
587
588 expire_names_and_servers(t);
589
590 /*
591 * Write out a snapshot of our current browse list into
592 * the browse.dat file. This is used by smbd to service
593 * incoming NetServerEnum calls - used to synchronise
594 * browse lists over subnets.
595 * (nmbd_serverlistdb.c)
596 */
597
598 write_browse_list(t, False);
599
600 /*
601 * If we are a domain master browser, we have a list of
602 * local master browsers we should synchronise browse
603 * lists with (these are added by an incoming local
604 * master browser announcement packet). Expire any of
605 * these that are no longer current, and pull the server
606 * lists from each of these known local master browsers.
607 * (nmbd_browsesync.c)
608 */
609
610 dmb_expire_and_sync_browser_lists(t);
611
612 /*
613 * Check that there is a local master browser for our
614 * workgroup for all our broadcast subnets. If one
615 * is not found, start an election (which we ourselves
616 * may or may not participate in, depending on the
617 * setting of the 'local master' parameter.
618 * (nmbd_elections.c)
619 */
620
621 check_master_browser_exists(t);
622
623 /*
624 * If we are configured as a logon server, attempt to
625 * register the special NetBIOS names to become such
626 * (WORKGROUP<1c> name) on all broadcast subnets and
627 * with the WINS server (if used). If we are configured
628 * to become a domain master browser, attempt to register
629 * the special NetBIOS name (WORKGROUP<1b> name) to
630 * become such.
631 * (nmbd_become_dmb.c)
632 */
633
634 add_domain_names(t);
635
636 /*
637 * If we are a WINS server, do any timer dependent
638 * processing required.
639 * (nmbd_winsserver.c)
640 */
641
642 initiate_wins_processing(t);
643
644 /*
645 * If we are a domain master browser, attempt to contact the
646 * WINS server to get a list of all known WORKGROUPS/DOMAINS.
647 * This will only work to a Samba WINS server.
648 * (nmbd_browsesync.c)
649 */
650
651 if (lp_enhanced_browsing())
652 collect_all_workgroup_names_from_wins_server(t);
653
654 /*
655 * Go through the response record queue and time out or re-transmit
656 * and expired entries.
657 * (nmbd_packets.c)
658 */
659
660 retransmit_or_expire_response_records(t);
661
662 /*
663 * check to see if any remote browse sync child processes have completed
664 */
665
666 sync_check_completion();
667
668 /*
669 * regularly sync with any other DMBs we know about
670 */
671
672 if (lp_enhanced_browsing())
673 sync_all_dmbs(t);
674
675 /* check for new network interfaces */
676
677 reload_interfaces(t);
678
679 /* free up temp memory */
680 TALLOC_FREE(frame);
681 }
682}
683
684/**************************************************************************** **
685 Open the socket communication.
686 **************************************************************************** */
687
688static bool open_sockets(bool isdaemon, int port)
689{
690 struct sockaddr_storage ss;
691 const char *sock_addr = lp_socket_address();
692
693 /*
694 * The sockets opened here will be used to receive broadcast
695 * packets *only*. Interface specific sockets are opened in
696 * make_subnet() in namedbsubnet.c. Thus we bind to the
697 * address "0.0.0.0". The parameter 'socket address' is
698 * now deprecated.
699 */
700
701 if (!interpret_string_addr(&ss, sock_addr,
702 AI_NUMERICHOST|AI_PASSIVE)) {
703 DEBUG(0,("open_sockets: unable to get socket address "
704 "from string %s", sock_addr));
705 return false;
706 }
707 if (ss.ss_family != AF_INET) {
708 DEBUG(0,("open_sockets: unable to use IPv6 socket"
709 "%s in nmbd\n",
710 sock_addr));
711 return false;
712 }
713
714 if (isdaemon) {
715 ClientNMB = open_socket_in(SOCK_DGRAM, port,
716 0, &ss,
717 true);
718 } else {
719 ClientNMB = 0;
720 }
721
722 if (ClientNMB == -1) {
723 return false;
724 }
725
726 ClientDGRAM = open_socket_in(SOCK_DGRAM, DGRAM_PORT,
727 3, &ss,
728 true);
729
730 if (ClientDGRAM == -1) {
731 if (ClientNMB != 0) {
732 close(ClientNMB);
733 }
734 return false;
735 }
736
737 /* we are never interested in SIGPIPE */
738 BlockSignals(True,SIGPIPE);
739
740 set_socket_options( ClientNMB, "SO_BROADCAST" );
741 set_socket_options( ClientDGRAM, "SO_BROADCAST" );
742
743 /* Ensure we're non-blocking. */
744 set_blocking( ClientNMB, False);
745 set_blocking( ClientDGRAM, False);
746
747 DEBUG( 3, ( "open_sockets: Broadcast sockets opened.\n" ) );
748 return( True );
749}
750
751/**************************************************************************** **
752 main program
753 **************************************************************************** */
754
755 int main(int argc, const char *argv[])
756{
757 static bool is_daemon;
758 static bool opt_interactive;
759 static bool Fork = true;
760 static bool no_process_group;
761 static bool log_stdout;
762 poptContext pc;
763 char *p_lmhosts = NULL;
764 int opt;
765 enum {
766 OPT_DAEMON = 1000,
767 OPT_INTERACTIVE,
768 OPT_FORK,
769 OPT_NO_PROCESS_GROUP,
770 OPT_LOG_STDOUT
771 };
772 struct poptOption long_options[] = {
773 POPT_AUTOHELP
774 {"daemon", 'D', POPT_ARG_NONE, NULL, OPT_DAEMON, "Become a daemon(default)" },
775 {"interactive", 'i', POPT_ARG_NONE, NULL, OPT_INTERACTIVE, "Run interactive (not a daemon)" },
776 {"foreground", 'F', POPT_ARG_NONE, NULL, OPT_FORK, "Run daemon in foreground (for daemontools & etc)" },
777 {"no-process-group", 0, POPT_ARG_NONE, NULL, OPT_NO_PROCESS_GROUP, "Don't create a new process group" },
778 {"log-stdout", 'S', POPT_ARG_NONE, NULL, OPT_LOG_STDOUT, "Log to stdout" },
779 {"hosts", 'H', POPT_ARG_STRING, &p_lmhosts, 0, "Load a netbios hosts file"},
780 {"port", 'p', POPT_ARG_INT, &global_nmb_port, 0, "Listen on the specified port" },
781 POPT_COMMON_SAMBA
782 { NULL }
783 };
784 TALLOC_CTX *frame;
785 NTSTATUS status;
786
787 /*
788 * Do this before any other talloc operation
789 */
790 talloc_enable_null_tracking();
791 frame = talloc_stackframe();
792
793#ifdef __OS2__
794 global_Sem32Add = 1;
795#endif
796
797 load_case_tables();
798
799 global_nmb_port = NMB_PORT;
800
801 pc = poptGetContext("nmbd", argc, argv, long_options, 0);
802 while ((opt = poptGetNextOpt(pc)) != -1) {
803 switch (opt) {
804 case OPT_DAEMON:
805 is_daemon = true;
806 break;
807 case OPT_INTERACTIVE:
808 opt_interactive = true;
809 break;
810 case OPT_FORK:
811 Fork = false;
812 break;
813 case OPT_NO_PROCESS_GROUP:
814 no_process_group = true;
815 break;
816 case OPT_LOG_STDOUT:
817 log_stdout = true;
818 break;
819 default:
820 d_fprintf(stderr, "\nInvalid option %s: %s\n\n",
821 poptBadOption(pc, 0), poptStrerror(opt));
822 poptPrintUsage(pc, stderr, 0);
823 exit(1);
824 }
825 };
826 poptFreeContext(pc);
827
828 global_in_nmbd = true;
829
830 StartupTime = time(NULL);
831
832 sys_srandom(time(NULL) ^ sys_getpid());
833
834 if (!override_logfile) {
835 char *lfile = NULL;
836 if (asprintf(&lfile, "%s/log.nmbd", get_dyn_LOGFILEBASE()) < 0) {
837 exit(1);
838 }
839 lp_set_logfile(lfile);
840 SAFE_FREE(lfile);
841 }
842
843 fault_setup((void (*)(void *))fault_continue );
844 dump_core_setup("nmbd");
845
846 /* POSIX demands that signals are inherited. If the invoking process has
847 * these signals masked, we will have problems, as we won't receive them. */
848 BlockSignals(False, SIGHUP);
849 BlockSignals(False, SIGUSR1);
850 BlockSignals(False, SIGTERM);
851
852#if defined(SIGFPE)
853 /* we are never interested in SIGFPE */
854 BlockSignals(True,SIGFPE);
855#endif
856
857 /* We no longer use USR2... */
858#if defined(SIGUSR2)
859 BlockSignals(True, SIGUSR2);
860#endif
861
862 if ( opt_interactive ) {
863 Fork = False;
864 log_stdout = True;
865 }
866
867 if ( log_stdout && Fork ) {
868 DEBUG(0,("ERROR: Can't log to stdout (-S) unless daemon is in foreground (-F) or interactive (-i)\n"));
869 exit(1);
870 }
871 if (log_stdout) {
872 setup_logging( argv[0], DEBUG_STDOUT);
873 } else {
874 setup_logging( argv[0], DEBUG_FILE);
875 }
876
877 reopen_logs();
878
879 DEBUG(0,("nmbd version %s started.\n", samba_version_string()));
880 DEBUGADD(0,("%s\n", COPYRIGHT_STARTUP_MESSAGE));
881
882 if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
883 DEBUG(0, ("error opening config file\n"));
884 exit(1);
885 }
886
887 if (nmbd_messaging_context() == NULL) {
888 return 1;
889 }
890
891 if ( !reload_nmbd_services(False) )
892 return(-1);
893
894 if(!init_names())
895 return -1;
896
897 reload_nmbd_services( True );
898
899 if (strequal(lp_workgroup(),"*")) {
900 DEBUG(0,("ERROR: a workgroup name of * is no longer supported\n"));
901 exit(1);
902 }
903
904 set_samba_nb_type();
905
906 if (!is_daemon && !is_a_socket(0)) {
907 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
908 is_daemon = True;
909 }
910
911 if (is_daemon && !opt_interactive) {
912 DEBUG( 2, ( "Becoming a daemon.\n" ) );
913 become_daemon(Fork, no_process_group, log_stdout);
914 }
915
916#if HAVE_SETPGID
917 /*
918 * If we're interactive we want to set our own process group for
919 * signal management.
920 */
921 if (opt_interactive && !no_process_group)
922 setpgid( (pid_t)0, (pid_t)0 );
923#endif
924
925 if (nmbd_messaging_context() == NULL) {
926 return 1;
927 }
928
929#ifndef SYNC_DNS
930 /* Setup the async dns. We do it here so it doesn't have all the other
931 stuff initialised and thus chewing memory and sockets */
932 if(lp_we_are_a_wins_server() && lp_dns_proxy()) {
933 start_async_dns();
934 }
935#endif
936
937 if (!directory_exist(lp_lockdir())) {
938 mkdir(lp_lockdir(), 0755);
939 }
940
941 pidfile_create("nmbd");
942
943 status = reinit_after_fork(nmbd_messaging_context(),
944 nmbd_event_context(),
945 procid_self(), false);
946
947 if (!NT_STATUS_IS_OK(status)) {
948 DEBUG(0,("reinit_after_fork() failed\n"));
949 exit(1);
950 }
951
952 if (!nmbd_setup_sig_term_handler())
953 exit(1);
954 if (!nmbd_setup_sig_hup_handler())
955 exit(1);
956
957 /* get broadcast messages */
958
959 if (!serverid_register(procid_self(),
960 FLAG_MSG_GENERAL|FLAG_MSG_DBWRAP)) {
961 DEBUG(1, ("Could not register myself in serverid.tdb\n"));
962 exit(1);
963 }
964
965 messaging_register(nmbd_messaging_context(), NULL,
966 MSG_FORCE_ELECTION, nmbd_message_election);
967#if 0
968 /* Until winsrepl is done. */
969 messaging_register(nmbd_messaging_context(), NULL,
970 MSG_WINS_NEW_ENTRY, nmbd_wins_new_entry);
971#endif
972 messaging_register(nmbd_messaging_context(), NULL,
973 MSG_SHUTDOWN, nmbd_terminate);
974 messaging_register(nmbd_messaging_context(), NULL,
975 MSG_SMB_CONF_UPDATED, msg_reload_nmbd_services);
976 messaging_register(nmbd_messaging_context(), NULL,
977 MSG_SEND_PACKET, msg_nmbd_send_packet);
978
979 TimeInit();
980
981 DEBUG( 3, ( "Opening sockets %d\n", global_nmb_port ) );
982
983 if ( !open_sockets( is_daemon, global_nmb_port ) ) {
984 kill_async_dns_child();
985 return 1;
986 }
987
988 /* Determine all the IP addresses we have. */
989 load_interfaces();
990
991 /* Create an nmbd subnet record for each of the above. */
992 if( False == create_subnets() ) {
993 DEBUG(0,("ERROR: Failed when creating subnet lists. Exiting.\n"));
994 kill_async_dns_child();
995 exit(1);
996 }
997
998 /* Load in any static local names. */
999 if (p_lmhosts) {
1000 set_dyn_LMHOSTSFILE(p_lmhosts);
1001 }
1002 load_lmhosts_file(get_dyn_LMHOSTSFILE());
1003 DEBUG(3,("Loaded hosts file %s\n", get_dyn_LMHOSTSFILE()));
1004
1005 /* If we are acting as a WINS server, initialise data structures. */
1006 if( !initialise_wins() ) {
1007 DEBUG( 0, ( "nmbd: Failed when initialising WINS server.\n" ) );
1008 kill_async_dns_child();
1009 exit(1);
1010 }
1011
1012 /*
1013 * Register nmbd primary workgroup and nmbd names on all
1014 * the broadcast subnets, and on the WINS server (if specified).
1015 * Also initiate the startup of our primary workgroup (start
1016 * elections if we are setup as being able to be a local
1017 * master browser.
1018 */
1019
1020 if( False == register_my_workgroup_and_names() ) {
1021 DEBUG(0,("ERROR: Failed when creating my my workgroup. Exiting.\n"));
1022 kill_async_dns_child();
1023 exit(1);
1024 }
1025
1026 if (!initialize_nmbd_proxy_logon()) {
1027 DEBUG(0,("ERROR: Failed setup nmbd_proxy_logon.\n"));
1028 kill_async_dns_child();
1029 exit(1);
1030 }
1031
1032 if (!nmbd_init_packet_server()) {
1033 kill_async_dns_child();
1034 exit(1);
1035 }
1036
1037 TALLOC_FREE(frame);
1038 process();
1039
1040 kill_async_dns_child();
1041 return(0);
1042}
Note: See TracBrowser for help on using the repository browser.