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

Last change on this file since 952 was 952, checked in by dmik, 9 years ago

samba server: Make nmbd obey max log size setting.

This is sorta backport from Samba 4 by @diver (to make sure
the manually set log file name is not changed when smb.conf
is loaded) with some additions that cause max log size
to be taken into account.

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