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 ****************************************************************************/
61 static int sig_hup(void)
65 DEBUG(0,("Got SIGHUP (reload not implemented)\n"));
67 reload_services(True);
70 #ifndef DONT_REINSTALL_SIG
71 signal(SIGHUP,SIGNAL_CAST sig_hup);
76 /****************************************************************************
78 ****************************************************************************/
79 static int sig_pipe(void)
83 DEBUG(0,("Got SIGPIPE\n"));
91 /*******************************************************************
92 prepare to dump a core file - carefully!
93 ********************************************************************/
94 static BOOL dump_core(void)
99 if ((p=strrchr(dname,'/'))) *p=0;
100 strcat(dname,"/corefiles");
102 sys_chown(dname,getuid(),getgid());
104 if (chdir(dname)) return(False);
111 getrlimit(RLIMIT_CORE, &rlp);
112 rlp.rlim_cur = MAX(4*1024*1024,rlp.rlim_cur);
113 setrlimit(RLIMIT_CORE, &rlp);
114 getrlimit(RLIMIT_CORE, &rlp);
115 DEBUG(3,("Core limits now %d %d\n",rlp.rlim_cur,rlp.rlim_max));
121 DEBUG(0,("Dumping core in %s\n",dname));
127 /****************************************************************************
128 possibly continue after a fault
129 ****************************************************************************/
130 static void fault_continue(void)
137 /*******************************************************************
138 expire old names from the namelist and server list
139 ******************************************************************/
140 static void expire_names_and_servers(void)
142 static time_t lastrun = 0;
143 time_t t = time(NULL);
145 if (!lastrun) lastrun = t;
146 if (t < lastrun + 5) return;
153 /*****************************************************************************
154 reload the services file
155 **************************************************************************/
156 BOOL reload_services(BOOL test)
159 extern fstring remote_machine;
161 strcpy(remote_machine,"nmbd");
166 strcpy(fname,lp_configfile());
167 if (file_exist(fname,NULL) && !strcsequal(fname,servicesf))
169 strcpy(servicesf,fname);
174 if (test && !lp_file_list_changed())
177 ret = lp_load(servicesf,True);
179 /* perhaps the config filename is now set */
181 DEBUG(3,("services not loaded\n"));
182 reload_services(True);
192 /****************************************************************************
193 load a netbios hosts file
194 ****************************************************************************/
195 static void load_hosts_file(char *fname)
197 FILE *f = fopen(fname,"r");
200 DEBUG(2,("Can't open lmhosts file %s\n",fname));
206 if (!fgets_slash(line,sizeof(pstring),f)) continue;
208 if (*line == '#') continue;
213 pstring ip,name,mask,flags,extra;
217 struct in_addr ipaddr;
218 struct in_addr ipmask;
219 enum name_source source = LMHOSTS;
229 if (next_token(&ptr,ip ,NULL)) ++count;
230 if (next_token(&ptr,name ,NULL)) ++count;
231 if (next_token(&ptr,mask ,NULL)) ++count;
232 if (next_token(&ptr,flags,NULL)) ++count;
233 if (next_token(&ptr,extra,NULL)) ++count;
235 if (count <= 0) continue;
237 if (count > 0 && count < 2) {
238 DEBUG(0,("Ill formed hosts line [%s]\n",line));
242 /* work out if we need to shuffle the tokens along due to the
243 optional subnet mask argument */
245 if (strchr(mask, 'G') || strchr(mask, 'S') || strchr(mask, 'M')) {
246 strcpy(flags, mask );
247 /* default action for no subnet mask */
251 DEBUG(4, ("lmhost entry: %s %s %s %s\n", ip, name, mask, flags));
253 if (strchr(flags,'G') || strchr(flags,'S'))
256 if (strchr(flags,'M') && !group) {
261 ipaddr = *interpret_addr2(ip);
263 ipmask = *interpret_addr2(mask);
265 ipmask = *iface_nmask(ipaddr);
268 add_domain_entry(ipaddr, ipmask, name, True);
270 add_netbios_entry(name,0x20,NB_ACTIVE,0,source,ipaddr,False);
279 /****************************************************************************
280 The main select loop.
281 ***************************************************************************/
282 static void process(void)
288 time_t t = time(NULL);
289 run_election = check_elections();
290 listen_for_packets(run_election);
298 /* what was this stuff supposed to do? It sent
299 ANN_GetBackupListReq packets which I think should only be
300 sent when trying to find out who to browse with */
306 expire_names_and_servers();
307 expire_netbios_response_entries(t-10);
312 check_master_browser();
317 /****************************************************************************
318 open the socket communication
319 ****************************************************************************/
320 static BOOL open_sockets(BOOL isdaemon, int port)
325 if ((hp = Get_Hostbyname(myhostname)) == 0) {
326 DEBUG(0,( "Get_Hostbyname: Unknown host. %s\n",myhostname));
331 ClientNMB = open_socket_in(SOCK_DGRAM, port,0);
335 ClientDGRAM = open_socket_in(SOCK_DGRAM,DGRAM_PORT,3);
340 signal(SIGPIPE, SIGNAL_CAST sig_pipe);
342 set_socket_options(ClientNMB,"SO_BROADCAST");
343 set_socket_options(ClientDGRAM,"SO_BROADCAST");
345 DEBUG(3,("Sockets opened.\n"));
350 /****************************************************************************
351 initialise connect, service and file structs
352 ****************************************************************************/
353 static BOOL init_structs()
355 if (!get_myname(myhostname,NULL))
360 strcpy(myname,myhostname);
361 p = strchr(myname,'.');
368 /****************************************************************************
370 ****************************************************************************/
371 static void usage(char *pname)
373 DEBUG(0,("Incorrect program usage - is the command line correct?\n"));
375 printf("Usage: %s [-n name] [-B bcast address] [-D] [-p port] [-d debuglevel] [-l log basename]\n",pname);
376 printf("Version %s\n",VERSION);
377 printf("\t-D become a daemon\n");
378 printf("\t-p port listen on the specified port\n");
379 printf("\t-d debuglevel set the debuglevel\n");
380 printf("\t-l log basename. Basename for log/debug files\n");
381 printf("\t-n netbiosname. the netbios name to advertise for this host\n");
382 printf("\t-B broadcast address the address to use for broadcasts\n");
383 printf("\t-N netmask the netmask to use for subnet determination\n");
384 printf("\t-H hosts file load a netbios hosts file\n");
385 printf("\t-G group name add a group name to be part of\n");
386 printf("\t-I ip-address override the IP address\n");
387 printf("\t-C comment sets the machine comment that appears in browse lists\n");
392 /****************************************************************************
394 **************************************************************************/
395 int main(int argc,char *argv[])
406 StartupTime = time(NULL);
410 strcpy(debugf,NMBLOGFILE);
412 setup_logging(argv[0],False);
414 charset_initialise();
417 strcpy(host_file,LMHOSTSFILE);
420 /* this is for people who can't start the program correctly */
421 while (argc > 1 && (*argv[1] != '-')) {
426 fault_setup(fault_continue);
428 signal(SIGHUP,SIGNAL_CAST sig_hup);
430 while ((opt = getopt (argc, argv, "s:T:I:C:bAi:B:N:Rn:l:d:Dp:hSH:G:")) != EOF)
435 strcpy(servicesf,optarg);
438 strcpy(ServerComment,optarg);
441 strcpy(group,optarg);
444 strcpy(host_file,optarg);
447 iface_set_default(optarg,NULL,NULL);
450 iface_set_default(NULL,optarg,NULL);
453 iface_set_default(NULL,NULL,optarg);
456 strcpy(myname,optarg);
459 sprintf(debugf,"%s.nmb",optarg);
462 strcpy(scope,optarg);
469 DEBUGLEVEL = atoi(optarg);
479 if (!is_a_socket(0)) {
486 DEBUG(1,("%s netbios nameserver version %s started\n",timestring(),VERSION));
487 DEBUG(1,("Copyright Andrew Tridgell 1994\n"));
491 if (!reload_services(False))
495 add_domain_entry(*iface_bcast(ipzero),*iface_nmask(ipzero),group, True);
497 if (!is_daemon && !is_a_socket(0)) {
498 DEBUG(0,("standard input is not a socket, assuming -D option\n"));
503 DEBUG(2,("%s becoming a daemon\n",timestring()));
507 DEBUG(3,("Opening sockets %d\n", port));
509 if (!open_sockets(is_daemon,port)) return 1;
512 load_hosts_file(host_file);
513 DEBUG(3,("Loaded hosts file\n"));
517 strcpy(ServerComment,"Samba %v");
518 string_sub(ServerComment,"%v",VERSION);
519 string_sub(ServerComment,"%h",myhostname);
524 DEBUG(3,("Checked names\n"));
528 DEBUG(3,("Dumped names\n"));