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