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
31 extern int DEBUGLEVEL;
33 extern pstring debugf;
34 pstring servicesf = CONFIGFILE;
41 extern pstring myhostname;
42 static pstring host_file;
43 extern pstring myname;
45 /* are we running as a daemon ? */
46 static BOOL is_daemon = False;
48 /* machine comment for host announcements */
49 pstring ServerComment="";
51 /* what server type are we currently */
53 time_t StartupTime =0;
55 extern struct in_addr ipzero;
58 /****************************************************************************
60 ****************************************************************************/
65 DEBUG(0,("Got SIGTERM: going down...\n"));
68 reload_services(True);
70 /* remove all samba names, with wins server if necessary. */
73 /* XXXX don't care if we never receive a response back... yet */
74 /* XXXX other things: if we are a master browser, force an election? */
80 /****************************************************************************
82 ****************************************************************************/
83 static int sig_hup(void)
87 DEBUG(0,("Got SIGHUP (reload not implemented)\n"));
89 reload_services(True);
92 #ifndef DONT_REINSTALL_SIG
93 signal(SIGHUP,SIGNAL_CAST sig_hup);
98 /****************************************************************************
100 ****************************************************************************/
101 static int sig_pipe(void)
105 DEBUG(0,("Got SIGPIPE\n"));
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);
214 /****************************************************************************
215 load a netbios hosts file
216 ****************************************************************************/
217 static void load_hosts_file(char *fname)
219 FILE *f = fopen(fname,"r");
222 DEBUG(2,("Can't open lmhosts file %s\n",fname));
228 if (!fgets_slash(line,sizeof(pstring),f)) continue;
230 if (*line == '#') continue;
235 pstring ip,name,mask,flags,extra;
239 struct in_addr ipaddr;
240 struct in_addr ipmask;
241 enum name_source source = LMHOSTS;
251 if (next_token(&ptr,ip ,NULL)) ++count;
252 if (next_token(&ptr,name ,NULL)) ++count;
253 if (next_token(&ptr,mask ,NULL)) ++count;
254 if (next_token(&ptr,flags,NULL)) ++count;
255 if (next_token(&ptr,extra,NULL)) ++count;
257 if (count <= 0) continue;
259 if (count > 0 && count < 2) {
260 DEBUG(0,("Ill formed hosts line [%s]\n",line));
264 /* work out if we need to shuffle the tokens along due to the
265 optional subnet mask argument */
267 if (strchr(mask, 'G') || strchr(mask, 'S') || strchr(mask, 'M')) {
268 strcpy(flags, mask );
269 /* default action for no subnet mask */
273 DEBUG(4, ("lmhost entry: %s %s %s %s\n", ip, name, mask, flags));
275 if (strchr(flags,'G') || strchr(flags,'S'))
278 if (strchr(flags,'M') && !group) {
283 ipaddr = *interpret_addr2(ip);
285 ipmask = *interpret_addr2(mask);
287 ipmask = *iface_nmask(ipaddr);
290 add_domain_entry(ipaddr, ipmask, name, True);
292 add_netbios_entry(name,0x20,NB_ACTIVE,0,source,ipaddr,True);
301 /****************************************************************************
302 The main select loop.
303 ***************************************************************************/
304 static void process(void)
310 time_t t = time(NULL);
311 run_election = check_elections();
312 listen_for_packets(run_election);
320 /* what was this stuff supposed to do? It sent
321 ANN_GetBackupListReq packets which I think should only be
322 sent when trying to find out who to browse with */
328 expire_names_and_servers();
329 expire_netbios_response_entries(t-10);
334 check_master_browser();
339 /****************************************************************************
340 open the socket communication
341 ****************************************************************************/
342 static BOOL open_sockets(BOOL isdaemon, int port)
347 if ((hp = Get_Hostbyname(myhostname)) == 0) {
348 DEBUG(0,( "Get_Hostbyname: Unknown host. %s\n",myhostname));
353 ClientNMB = open_socket_in(SOCK_DGRAM, port,0);
357 ClientDGRAM = open_socket_in(SOCK_DGRAM,DGRAM_PORT,3);
362 signal(SIGPIPE, SIGNAL_CAST sig_pipe);
364 set_socket_options(ClientNMB,"SO_BROADCAST");
365 set_socket_options(ClientDGRAM,"SO_BROADCAST");
367 DEBUG(3,("Sockets opened.\n"));
372 /****************************************************************************
373 initialise connect, service and file structs
374 ****************************************************************************/
375 static BOOL init_structs()
377 if (!get_myname(myhostname,NULL))
382 strcpy(myname,myhostname);
383 p = strchr(myname,'.');
390 /****************************************************************************
392 ****************************************************************************/
393 static void usage(char *pname)
395 DEBUG(0,("Incorrect program usage - is the command line correct?\n"));
397 printf("Usage: %s [-n name] [-B bcast address] [-D] [-p port] [-d debuglevel] [-l log basename]\n",pname);
398 printf("Version %s\n",VERSION);
399 printf("\t-D become a daemon\n");
400 printf("\t-p port listen on the specified port\n");
401 printf("\t-d debuglevel set the debuglevel\n");
402 printf("\t-l log basename. Basename for log/debug files\n");
403 printf("\t-n netbiosname. the netbios name to advertise for this host\n");
404 printf("\t-B broadcast address the address to use for broadcasts\n");
405 printf("\t-N netmask the netmask to use for subnet determination\n");
406 printf("\t-H hosts file load a netbios hosts file\n");
407 printf("\t-G group name add a group name to be part of\n");
408 printf("\t-I ip-address override the IP address\n");
409 printf("\t-C comment sets the machine comment that appears in browse lists\n");
414 /****************************************************************************
416 **************************************************************************/
417 int main(int argc,char *argv[])
428 StartupTime = time(NULL);
432 strcpy(debugf,NMBLOGFILE);
434 setup_logging(argv[0],False);
436 charset_initialise();
439 strcpy(host_file,LMHOSTSFILE);
442 /* this is for people who can't start the program correctly */
443 while (argc > 1 && (*argv[1] != '-')) {
448 fault_setup(fault_continue);
450 signal(SIGHUP,SIGNAL_CAST sig_hup);
451 signal(SIGTERM,SIGNAL_CAST sig_term);
453 while ((opt = getopt(argc, argv, "s:T:I:C:bAi:B:N:Rn:l:d:Dp:hSH:G:")) != EOF)
458 strcpy(servicesf,optarg);
461 strcpy(ServerComment,optarg);
464 strcpy(group,optarg);
467 strcpy(host_file,optarg);
470 iface_set_default(optarg,NULL,NULL);
473 iface_set_default(NULL,optarg,NULL);
476 iface_set_default(NULL,NULL,optarg);
479 strcpy(myname,optarg);
482 sprintf(debugf,"%s.nmb",optarg);
485 strcpy(scope,optarg);
492 DEBUGLEVEL = atoi(optarg);
502 if (!is_a_socket(0)) {
509 DEBUG(1,("%s netbios nameserver version %s started\n",timestring(),VERSION));
510 DEBUG(1,("Copyright Andrew Tridgell 1994\n"));
514 if (!reload_services(False))
518 add_my_domains(group);
520 if (!is_daemon && !is_a_socket(0)) {
521 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
526 DEBUG(2,("%s becoming a daemon\n",timestring()));
530 DEBUG(3,("Opening sockets %d\n", port));
532 if (!open_sockets(is_daemon,port)) return 1;
535 load_hosts_file(host_file);
536 DEBUG(3,("Loaded hosts file\n"));
540 strcpy(ServerComment,"Samba %v");
541 string_sub(ServerComment,"%v",VERSION);
542 string_sub(ServerComment,"%h",myhostname);
545 add_my_domains(lp_workgroup());
547 DEBUG(3,("Checked names\n"));
551 DEBUG(3,("Dumped names\n"));