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 /* machine comment for host announcements */
48 pstring ServerComment="";
50 /* what server type are we currently */
52 time_t StartupTime =0;
54 extern struct in_addr ipzero;
56 /****************************************************************************
58 ****************************************************************************/
61 BlockSignals(True,SIGTERM);
63 DEBUG(0,("Got SIGTERM: going down...\n"));
65 /* write out wins.dat file if samba is a WINS server */
68 /* remove all samba names, with wins server if necessary. */
71 /* announce all server entries as 0 time-to-live, 0 type */
72 /* XXXX don't care if we never receive a response back... yet */
75 /* XXXX other things: if we are a master browser, force an election? */
81 /****************************************************************************
83 ****************************************************************************/
84 static int sig_hup(void)
86 BlockSignals(True,SIGHUP);
88 DEBUG(0,("Got SIGHUP (reload not implemented)\n"));
90 reload_services(True);
94 BlockSignals(False,SIGHUP);
95 #ifndef DONT_REINSTALL_SIG
96 signal(SIGHUP,SIGNAL_CAST sig_hup);
101 /****************************************************************************
103 ****************************************************************************/
104 static int sig_pipe(void)
106 BlockSignals(True,SIGPIPE);
108 DEBUG(0,("Got SIGPIPE\n"));
111 BlockSignals(False,SIGPIPE);
116 /*******************************************************************
117 prepare to dump a core file - carefully!
118 ********************************************************************/
119 static BOOL dump_core(void)
123 strcpy(dname,debugf);
124 if ((p=strrchr(dname,'/'))) *p=0;
125 strcat(dname,"/corefiles");
127 sys_chown(dname,getuid(),getgid());
129 if (chdir(dname)) return(False);
136 getrlimit(RLIMIT_CORE, &rlp);
137 rlp.rlim_cur = MAX(4*1024*1024,rlp.rlim_cur);
138 setrlimit(RLIMIT_CORE, &rlp);
139 getrlimit(RLIMIT_CORE, &rlp);
140 DEBUG(3,("Core limits now %d %d\n",rlp.rlim_cur,rlp.rlim_max));
146 DEBUG(0,("Dumping core in %s\n",dname));
152 /****************************************************************************
153 possibly continue after a fault
154 ****************************************************************************/
155 static void fault_continue(void)
162 /*******************************************************************
163 expire old names from the namelist and server list
164 ******************************************************************/
165 static void expire_names_and_servers(void)
167 static time_t lastrun = 0;
168 time_t t = time(NULL);
170 if (!lastrun) lastrun = t;
171 if (t < lastrun + 5) return;
178 /*****************************************************************************
179 reload the services file
180 **************************************************************************/
181 BOOL reload_services(BOOL test)
184 extern fstring remote_machine;
186 strcpy(remote_machine,"nmbd");
191 strcpy(fname,lp_configfile());
192 if (file_exist(fname,NULL) && !strcsequal(fname,servicesf))
194 strcpy(servicesf,fname);
199 if (test && !lp_file_list_changed())
202 ret = lp_load(servicesf,True);
204 /* perhaps the config filename is now set */
206 DEBUG(3,("services not loaded\n"));
207 reload_services(True);
211 add_subnet_interfaces();
218 /****************************************************************************
219 load a netbios hosts file
220 ****************************************************************************/
221 static void load_hosts_file(char *fname)
223 FILE *f = fopen(fname,"r");
226 DEBUG(2,("Can't open lmhosts file %s\n",fname));
232 pstring ip,name,flags,extra;
233 struct subnet_record *d;
236 struct in_addr ipaddr;
237 enum name_source source = LMHOSTS;
239 if (!fgets_slash(line,sizeof(pstring),f)) continue;
241 if (*line == '#') continue;
249 if (next_token(&ptr,ip ,NULL)) ++count;
250 if (next_token(&ptr,name ,NULL)) ++count;
251 if (next_token(&ptr,flags,NULL)) ++count;
252 if (next_token(&ptr,extra,NULL)) ++count;
254 if (count <= 0) continue;
256 if (count > 0 && count < 2) {
257 DEBUG(0,("Ill formed hosts line [%s]\n",line));
262 DEBUG(0,("too many columns in %s (obsolete syntax)\n",fname));
266 DEBUG(4, ("lmhost entry: %s %s %s\n", ip, name, flags));
268 if (strchr(flags,'G') || strchr(flags,'S')) {
269 DEBUG(0,("group flag in %s ignored (obsolete)\n",fname));
273 if (strchr(flags,'M')) {
278 ipaddr = *interpret_addr2(ip);
279 d = find_subnet(ipaddr);
281 add_netbios_entry(d,name,0x00,NB_ACTIVE,0,source,ipaddr,True,True);
282 add_netbios_entry(d,name,0x20,NB_ACTIVE,0,source,ipaddr,True,True);
290 /****************************************************************************
291 The main select loop.
292 ***************************************************************************/
293 static void process(void)
299 time_t t = time(NULL);
300 run_election = check_elections();
301 listen_for_packets(run_election);
309 /* XXXX what was this stuff supposed to do? It sent
310 ANN_GetBackupListReq packets which I think should only be
311 sent when trying to find out who to browse with */
320 query_refresh_names();
322 expire_names_and_servers();
323 expire_netbios_response_entries();
328 check_master_browser();
333 /****************************************************************************
334 open the socket communication
335 ****************************************************************************/
336 static BOOL open_sockets(BOOL isdaemon, int port)
341 if ((hp = Get_Hostbyname(myhostname)) == 0) {
342 DEBUG(0,( "Get_Hostbyname: Unknown host. %s\n",myhostname));
347 ClientNMB = open_socket_in(SOCK_DGRAM, port,0);
351 ClientDGRAM = open_socket_in(SOCK_DGRAM,DGRAM_PORT,3);
356 signal(SIGPIPE, SIGNAL_CAST sig_pipe);
358 set_socket_options(ClientNMB,"SO_BROADCAST");
359 set_socket_options(ClientDGRAM,"SO_BROADCAST");
361 DEBUG(3,("Sockets opened.\n"));
366 /****************************************************************************
367 initialise connect, service and file structs
368 ****************************************************************************/
369 static BOOL init_structs()
371 if (!get_myname(myhostname,NULL))
376 strcpy(myname,myhostname);
377 p = strchr(myname,'.');
384 /****************************************************************************
386 ****************************************************************************/
387 static void usage(char *pname)
389 DEBUG(0,("Incorrect program usage - is the command line correct?\n"));
391 printf("Usage: %s [-n name] [-B bcast address] [-D] [-p port] [-d debuglevel] [-l log basename]\n",pname);
392 printf("Version %s\n",VERSION);
393 printf("\t-D become a daemon\n");
394 printf("\t-p port listen on the specified port\n");
395 printf("\t-d debuglevel set the debuglevel\n");
396 printf("\t-l log basename. Basename for log/debug files\n");
397 printf("\t-n netbiosname. the netbios name to advertise for this host\n");
398 printf("\t-B broadcast address the address to use for broadcasts\n");
399 printf("\t-N netmask the netmask to use for subnet determination\n");
400 printf("\t-H hosts file load a netbios hosts file\n");
401 printf("\t-G group name add a group name to be part of\n");
402 printf("\t-I ip-address override the IP address\n");
403 printf("\t-C comment sets the machine comment that appears in browse lists\n");
408 /****************************************************************************
410 **************************************************************************/
411 int main(int argc,char *argv[])
422 StartupTime = time(NULL);
426 strcpy(debugf,NMBLOGFILE);
428 setup_logging(argv[0],False);
430 charset_initialise();
433 strcpy(host_file,LMHOSTSFILE);
436 /* this is for people who can't start the program correctly */
437 while (argc > 1 && (*argv[1] != '-')) {
442 fault_setup(fault_continue);
444 signal(SIGHUP ,SIGNAL_CAST sig_hup);
445 signal(SIGTERM,SIGNAL_CAST sig_term);
447 while ((opt = getopt(argc, argv, "s:T:I:C:bAi:B:N:Rn:l:d:Dp:hSH:G:")) != EOF)
452 strcpy(servicesf,optarg);
455 strcpy(ServerComment,optarg);
458 strcpy(group,optarg);
461 strcpy(host_file,optarg);
464 iface_set_default(optarg,NULL,NULL);
467 iface_set_default(NULL,optarg,NULL);
470 iface_set_default(NULL,NULL,optarg);
473 strcpy(myname,optarg);
476 sprintf(debugf,"%s.nmb",optarg);
479 strcpy(scope,optarg);
486 DEBUGLEVEL = atoi(optarg);
496 if (!is_a_socket(0)) {
503 DEBUG(1,("%s netbios nameserver version %s started\n",timestring(),VERSION));
504 DEBUG(1,("Copyright Andrew Tridgell 1994\n"));
508 if (!reload_services(False))
514 add_my_subnets(group);
516 if (!is_daemon && !is_a_socket(0)) {
517 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
522 DEBUG(2,("%s becoming a daemon\n",timestring()));
526 DEBUG(3,("Opening sockets %d\n", port));
528 if (!open_sockets(is_daemon,port)) return 1;
531 load_hosts_file(host_file);
532 DEBUG(3,("Loaded hosts file\n"));
538 strcpy(ServerComment,"Samba %v");
539 string_sub(ServerComment,"%v",VERSION);
540 string_sub(ServerComment,"%h",myhostname);
543 add_my_subnets(lp_workgroup());
545 DEBUG(3,("Checked names\n"));
547 load_netbios_names();
549 DEBUG(3,("Loaded names\n"));
553 DEBUG(3,("Dumped names\n"));