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