2 Unix SMB/Netbios implementation.
4 NBT netbios routines and daemon - version 2
5 Copyright (C) Andrew Tridgell 1994-1995
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 14 jan 96: lkcl@pires.co.uk
24 added multiple workgroup domain master support
30 extern int DEBUGLEVEL;
32 extern pstring debugf;
33 pstring servicesf = CONFIGFILE;
40 extern pstring myhostname;
41 static pstring host_file;
42 extern pstring myname;
44 /* are we running as a daemon ? */
45 static BOOL is_daemon = False;
47 /* what server type are we currently */
49 time_t StartupTime =0;
51 extern struct in_addr ipzero;
53 /****************************************************************************
55 ****************************************************************************/
58 BlockSignals(True,SIGTERM);
60 DEBUG(0,("Got SIGTERM: going down...\n"));
62 /* write out wins.dat file if samba is a WINS server */
65 /* remove all samba names, with wins server if necessary. */
68 /* announce all server entries as 0 time-to-live, 0 type */
69 /* XXXX don't care if we never receive a response back... yet */
72 /* XXXX other things: if we are a master browser, force an election? */
78 /****************************************************************************
80 ****************************************************************************/
81 static int sig_hup(void)
83 BlockSignals(True,SIGHUP);
85 DEBUG(0,("Got SIGHUP (reload not implemented)\n"));
87 reload_services(True);
91 BlockSignals(False,SIGHUP);
92 #ifndef DONT_REINSTALL_SIG
93 signal(SIGHUP,SIGNAL_CAST sig_hup);
98 /****************************************************************************
100 ****************************************************************************/
101 static int sig_pipe(void)
103 BlockSignals(True,SIGPIPE);
105 DEBUG(0,("Got SIGPIPE\n"));
108 BlockSignals(False,SIGPIPE);
113 /*******************************************************************
114 prepare to dump a core file - carefully!
115 ********************************************************************/
116 static BOOL dump_core(void)
120 strcpy(dname,debugf);
121 if ((p=strrchr(dname,'/'))) *p=0;
122 strcat(dname,"/corefiles");
124 sys_chown(dname,getuid(),getgid());
126 if (chdir(dname)) return(False);
133 getrlimit(RLIMIT_CORE, &rlp);
134 rlp.rlim_cur = MAX(4*1024*1024,rlp.rlim_cur);
135 setrlimit(RLIMIT_CORE, &rlp);
136 getrlimit(RLIMIT_CORE, &rlp);
137 DEBUG(3,("Core limits now %d %d\n",rlp.rlim_cur,rlp.rlim_max));
143 DEBUG(0,("Dumping core in %s\n",dname));
149 /****************************************************************************
150 possibly continue after a fault
151 ****************************************************************************/
152 static void fault_continue(void)
159 /*******************************************************************
160 expire old names from the namelist and server list
161 ******************************************************************/
162 static void expire_names_and_servers(void)
164 static time_t lastrun = 0;
165 time_t t = time(NULL);
167 if (!lastrun) lastrun = t;
168 if (t < lastrun + 5) return;
175 /*****************************************************************************
176 reload the services file
177 **************************************************************************/
178 BOOL reload_services(BOOL test)
181 extern fstring remote_machine;
183 strcpy(remote_machine,"nmbd");
188 strcpy(fname,lp_configfile());
189 if (file_exist(fname,NULL) && !strcsequal(fname,servicesf))
191 strcpy(servicesf,fname);
196 if (test && !lp_file_list_changed())
199 ret = lp_load(servicesf,True);
201 /* perhaps the config filename is now set */
203 DEBUG(3,("services not loaded\n"));
204 reload_services(True);
208 add_subnet_interfaces();
215 /****************************************************************************
216 load a netbios hosts file
217 ****************************************************************************/
218 static void load_hosts_file(char *fname)
220 FILE *f = fopen(fname,"r");
223 DEBUG(2,("Can't open lmhosts file %s\n",fname));
229 pstring ip,name,flags,extra;
230 struct subnet_record *d;
233 struct in_addr ipaddr;
234 enum name_source source = LMHOSTS;
236 if (!fgets_slash(line,sizeof(pstring),f)) continue;
238 if (*line == '#') continue;
246 if (next_token(&ptr,ip ,NULL)) ++count;
247 if (next_token(&ptr,name ,NULL)) ++count;
248 if (next_token(&ptr,flags,NULL)) ++count;
249 if (next_token(&ptr,extra,NULL)) ++count;
251 if (count <= 0) continue;
253 if (count > 0 && count < 2) {
254 DEBUG(0,("Ill formed hosts line [%s]\n",line));
259 DEBUG(0,("too many columns in %s (obsolete syntax)\n",fname));
263 DEBUG(4, ("lmhost entry: %s %s %s\n", ip, name, flags));
265 if (strchr(flags,'G') || strchr(flags,'S')) {
266 DEBUG(0,("group flag in %s ignored (obsolete)\n",fname));
270 if (strchr(flags,'M')) {
275 ipaddr = *interpret_addr2(ip);
276 d = find_subnet(ipaddr);
278 add_netbios_entry(d,name,0x00,NB_ACTIVE,0,source,ipaddr,True,True);
279 add_netbios_entry(d,name,0x20,NB_ACTIVE,0,source,ipaddr,True,True);
287 /****************************************************************************
288 The main select loop.
289 ***************************************************************************/
290 static void process(void)
296 time_t t = time(NULL);
297 run_election = check_elections();
298 listen_for_packets(run_election);
309 query_refresh_names();
311 expire_names_and_servers();
312 expire_netbios_response_entries();
317 check_master_browser();
322 /****************************************************************************
323 open the socket communication
324 ****************************************************************************/
325 static BOOL open_sockets(BOOL isdaemon, int port)
330 if ((hp = Get_Hostbyname(myhostname)) == 0) {
331 DEBUG(0,( "Get_Hostbyname: Unknown host. %s\n",myhostname));
336 ClientNMB = open_socket_in(SOCK_DGRAM, port,0,interpret_addr(lp_socket_address()));
340 ClientDGRAM = open_socket_in(SOCK_DGRAM,DGRAM_PORT,3,interpret_addr(lp_socket_address()));
345 signal(SIGPIPE, SIGNAL_CAST sig_pipe);
347 set_socket_options(ClientNMB,"SO_BROADCAST");
348 set_socket_options(ClientDGRAM,"SO_BROADCAST");
350 DEBUG(3,("Sockets opened.\n"));
355 /****************************************************************************
356 initialise connect, service and file structs
357 ****************************************************************************/
358 static BOOL init_structs()
360 extern fstring local_machine;
364 strcpy(myname,myhostname);
365 p = strchr(myname,'.');
370 strcpy(local_machine,myname);
371 trim_string(local_machine," "," ");
372 p = strchr(local_machine,' ');
374 strlower(local_machine);
379 /****************************************************************************
381 ****************************************************************************/
382 static void usage(char *pname)
384 DEBUG(0,("Incorrect program usage - is the command line correct?\n"));
386 printf("Usage: %s [-n name] [-D] [-p port] [-d debuglevel] [-l log basename]\n",pname);
387 printf("Version %s\n",VERSION);
388 printf("\t-D become a daemon\n");
389 printf("\t-p port listen on the specified port\n");
390 printf("\t-d debuglevel set the debuglevel\n");
391 printf("\t-l log basename. Basename for log/debug files\n");
392 printf("\t-n netbiosname. the netbios name to advertise for this host\n");
393 printf("\t-H hosts file load a netbios hosts file\n");
398 /****************************************************************************
400 **************************************************************************/
401 int main(int argc,char *argv[])
410 StartupTime = time(NULL);
414 strcpy(debugf,NMBLOGFILE);
416 setup_logging(argv[0],False);
418 charset_initialise();
421 strcpy(host_file,LMHOSTSFILE);
424 /* this is for people who can't start the program correctly */
425 while (argc > 1 && (*argv[1] != '-')) {
430 fault_setup(fault_continue);
432 signal(SIGHUP ,SIGNAL_CAST sig_hup);
433 signal(SIGTERM,SIGNAL_CAST sig_term);
435 while ((opt = getopt(argc, argv, "s:T:I:C:bAi:B:N:Rn:l:d:Dp:hSH:G:")) != EOF)
440 strcpy(servicesf,optarg);
447 DEBUG(0,("Obsolete option '%c' used\n",opt));
450 strcpy(host_file,optarg);
453 strcpy(myname,optarg);
457 sprintf(debugf,"%s.nmb",optarg);
460 strcpy(scope,optarg);
467 DEBUGLEVEL = atoi(optarg);
477 if (!is_a_socket(0)) {
484 DEBUG(1,("%s netbios nameserver version %s started\n",timestring(),VERSION));
485 DEBUG(1,("Copyright Andrew Tridgell 1994\n"));
487 get_myname(myhostname,NULL);
489 if (!reload_services(False))
494 reload_services(True);
498 if (!is_daemon && !is_a_socket(0)) {
499 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
504 DEBUG(2,("%s becoming a daemon\n",timestring()));
508 DEBUG(3,("Opening sockets %d\n", port));
510 if (!open_sockets(is_daemon,port)) return 1;
513 load_hosts_file(host_file);
514 DEBUG(3,("Loaded hosts file\n"));
519 if (strequal(lp_workgroup(),"*")) {
520 DEBUG(0,("ERROR: a workgroup name of * is no longer supported\n"));
523 add_my_subnets(lp_workgroup());
525 DEBUG(3,("Checked names\n"));
527 load_netbios_names();
529 DEBUG(3,("Loaded names\n"));
533 DEBUG(3,("Dumped names\n"));