- added "netbios name" option in smb.conf to make controlling the name
[samba.git] / source / nmbd / nmbd.c
1 /*
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    NBT netbios routines and daemon - version 2
5    Copyright (C) Andrew Tridgell 1994-1995
6    
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.
11    
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.
16    
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.
20    
21    Revision History:
22
23    14 jan 96: lkcl@pires.co.uk
24    added multiple workgroup domain master support
25
26 */
27
28 #include "includes.h"
29
30 extern int DEBUGLEVEL;
31
32 extern pstring debugf;
33 pstring servicesf = CONFIGFILE;
34
35 extern pstring scope;
36
37 int ClientNMB   = -1;
38 int ClientDGRAM = -1;
39
40 extern pstring myhostname;
41 static pstring host_file;
42 extern pstring myname;
43
44 /* are we running as a daemon ? */
45 static BOOL is_daemon = False;
46
47 /* machine comment for host announcements */
48 pstring ServerComment="";
49
50 /* what server type are we currently */
51
52 time_t StartupTime =0;
53
54 extern struct in_addr ipzero;
55
56  /****************************************************************************
57   catch a sigterm
58   ****************************************************************************/
59 static int sig_term()
60 {
61   BlockSignals(True,SIGTERM);
62   
63   DEBUG(0,("Got SIGTERM: going down...\n"));
64   
65   /* write out wins.dat file if samba is a WINS server */
66   dump_names();
67   
68   /* remove all samba names, with wins server if necessary. */
69   remove_my_names();
70   
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 */
73   remove_my_servers();
74
75   /* XXXX other things: if we are a master browser, force an election? */
76   
77   exit(0);
78 }
79
80
81 /****************************************************************************
82 catch a sighup
83 ****************************************************************************/
84 static int sig_hup(void)
85 {
86   BlockSignals(True,SIGHUP);
87
88   DEBUG(0,("Got SIGHUP (reload not implemented)\n"));
89   dump_names();
90   reload_services(True);
91
92   set_samba_nb_type();
93
94   BlockSignals(False,SIGHUP);
95 #ifndef DONT_REINSTALL_SIG
96   signal(SIGHUP,SIGNAL_CAST sig_hup);
97 #endif
98   return(0);
99 }
100
101 /****************************************************************************
102 catch a sigpipe
103 ****************************************************************************/
104 static int sig_pipe(void)
105 {
106   BlockSignals(True,SIGPIPE);
107
108   DEBUG(0,("Got SIGPIPE\n"));
109   if (!is_daemon)
110     exit(1);
111   BlockSignals(False,SIGPIPE);
112   return(0);
113 }
114
115 #if DUMP_CORE
116 /*******************************************************************
117 prepare to dump a core file - carefully!
118 ********************************************************************/
119 static BOOL dump_core(void)
120 {
121   char *p;
122   pstring dname;
123   strcpy(dname,debugf);
124   if ((p=strrchr(dname,'/'))) *p=0;
125   strcat(dname,"/corefiles");
126   mkdir(dname,0700);
127   sys_chown(dname,getuid(),getgid());
128   chmod(dname,0700);
129   if (chdir(dname)) return(False);
130   umask(~(0700));
131
132 #ifndef NO_GETRLIMIT
133 #ifdef RLIMIT_CORE
134   {
135     struct rlimit rlp;
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));
141   }
142 #endif
143 #endif
144
145
146   DEBUG(0,("Dumping core in %s\n",dname));
147   return(True);
148 }
149 #endif
150
151
152 /****************************************************************************
153 possibly continue after a fault
154 ****************************************************************************/
155 static void fault_continue(void)
156 {
157 #if DUMP_CORE
158   dump_core();
159 #endif
160 }
161
162 /*******************************************************************
163   expire old names from the namelist and server list
164   ******************************************************************/
165 static void expire_names_and_servers(void)
166 {
167   static time_t lastrun = 0;
168   time_t t = time(NULL);
169   
170   if (!lastrun) lastrun = t;
171   if (t < lastrun + 5) return;
172   lastrun = t;
173   
174   expire_names(t);
175   expire_servers(t);
176 }
177
178 /*****************************************************************************
179   reload the services file
180   **************************************************************************/
181 BOOL reload_services(BOOL test)
182 {
183   BOOL ret;
184   extern fstring remote_machine;
185
186   strcpy(remote_machine,"nmbd");
187
188   if (lp_loaded())
189     {
190       pstring fname;
191       strcpy(fname,lp_configfile());
192       if (file_exist(fname,NULL) && !strcsequal(fname,servicesf))
193         {
194           strcpy(servicesf,fname);
195           test = False;
196         }
197     }
198
199   if (test && !lp_file_list_changed())
200     return(True);
201
202   ret = lp_load(servicesf,True);
203
204   /* perhaps the config filename is now set */
205   if (!test) {
206     DEBUG(3,("services not loaded\n"));
207     reload_services(True);
208   }
209
210   load_interfaces();
211   add_subnet_interfaces();
212
213   return(ret);
214 }
215
216
217
218 /****************************************************************************
219 load a netbios hosts file
220 ****************************************************************************/
221 static void load_hosts_file(char *fname)
222 {
223   FILE *f = fopen(fname,"r");
224   pstring line;
225   if (!f) {
226     DEBUG(2,("Can't open lmhosts file %s\n",fname));
227     return;
228   }
229
230   while (!feof(f))
231     {
232       pstring ip,name,flags,extra;
233       struct subnet_record *d;
234       char *ptr;
235       int count = 0;
236       struct in_addr ipaddr;
237       enum name_source source = LMHOSTS;
238
239       if (!fgets_slash(line,sizeof(pstring),f)) continue;
240
241       if (*line == '#') continue;
242
243       strcpy(ip,"");
244       strcpy(name,"");
245       strcpy(flags,"");
246       
247       ptr = line;
248       
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;
253       
254       if (count <= 0) continue;
255       
256       if (count > 0 && count < 2) {
257         DEBUG(0,("Ill formed hosts line [%s]\n",line));     
258         continue;
259       }
260       
261       if (count >= 4) {
262         DEBUG(0,("too many columns in %s (obsolete syntax)\n",fname));
263         continue;
264       }
265       
266       DEBUG(4, ("lmhost entry: %s %s %s\n", ip, name, flags));
267       
268       if (strchr(flags,'G') || strchr(flags,'S')) {
269         DEBUG(0,("group flag in %s ignored (obsolete)\n",fname));
270         continue;
271       }
272       
273       if (strchr(flags,'M')) {
274         source = SELF;
275         strcpy(myname,name);
276       }
277       
278       ipaddr = *interpret_addr2(ip);
279       d = find_subnet(ipaddr);
280       if (d) {
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);
283       } 
284     }
285   
286   fclose(f);
287 }
288
289
290 /****************************************************************************
291   The main select loop.
292   ***************************************************************************/
293 static void process(void)
294 {
295   BOOL run_election;
296
297   while (True)
298     {
299       time_t t = time(NULL);
300       run_election = check_elections();
301       listen_for_packets(run_election);
302
303       run_packet_queue();
304       run_elections();
305
306       announce_host();
307
308       announce_master();
309
310       announce_remote();
311
312       query_refresh_names();
313
314       expire_names_and_servers();
315       expire_netbios_response_entries();
316       refresh_my_names(t);
317
318       write_browse_list();
319       do_browser_lists();
320       check_master_browser();
321     }
322 }
323
324
325 /****************************************************************************
326   open the socket communication
327 ****************************************************************************/
328 static BOOL open_sockets(BOOL isdaemon, int port)
329 {
330   struct hostent *hp;
331  
332   /* get host info */
333   if ((hp = Get_Hostbyname(myhostname)) == 0) {
334     DEBUG(0,( "Get_Hostbyname: Unknown host. %s\n",myhostname));
335     return False;
336   }   
337
338   if (isdaemon)
339     ClientNMB = open_socket_in(SOCK_DGRAM, port,0,interpret_addr(lp_socket_address()));
340   else
341     ClientNMB = 0;
342   
343   ClientDGRAM = open_socket_in(SOCK_DGRAM,DGRAM_PORT,3,interpret_addr(lp_socket_address()));
344
345   if (ClientNMB == -1)
346     return(False);
347
348   signal(SIGPIPE, SIGNAL_CAST sig_pipe);
349
350   set_socket_options(ClientNMB,"SO_BROADCAST");
351   set_socket_options(ClientDGRAM,"SO_BROADCAST");
352
353   DEBUG(3,("Sockets opened.\n"));
354   return True;
355 }
356
357
358 /****************************************************************************
359   initialise connect, service and file structs
360 ****************************************************************************/
361 static BOOL init_structs()
362 {
363   if (!get_myname(myhostname,NULL))
364     return(False);
365
366   if (! *myname) {
367     char *p;
368     strcpy(myname,myhostname);
369     p = strchr(myname,'.');
370     if (p) *p = 0;
371   }
372   strupper(myname);
373
374   return True;
375 }
376
377 /****************************************************************************
378 usage on the program
379 ****************************************************************************/
380 static void usage(char *pname)
381 {
382   DEBUG(0,("Incorrect program usage - is the command line correct?\n"));
383
384   printf("Usage: %s [-n name] [-D] [-p port] [-d debuglevel] [-l log basename]\n",pname);
385   printf("Version %s\n",VERSION);
386   printf("\t-D                    become a daemon\n");
387   printf("\t-p port               listen on the specified port\n");
388   printf("\t-d debuglevel         set the debuglevel\n");
389   printf("\t-l log basename.      Basename for log/debug files\n");
390   printf("\t-n netbiosname.       the netbios name to advertise for this host\n");
391   printf("\t-H hosts file        load a netbios hosts file\n");
392   printf("\n");
393 }
394
395
396 /****************************************************************************
397   main program
398   **************************************************************************/
399  int main(int argc,char *argv[])
400 {
401   int port = NMB_PORT;
402   int opt;
403   extern FILE *dbf;
404   extern char *optarg;
405
406   *host_file = 0;
407
408   StartupTime = time(NULL);
409
410   TimeInit();
411
412   strcpy(debugf,NMBLOGFILE);
413
414   setup_logging(argv[0],False);
415
416   charset_initialise();
417
418 #ifdef LMHOSTSFILE
419   strcpy(host_file,LMHOSTSFILE);
420 #endif
421
422   /* this is for people who can't start the program correctly */
423   while (argc > 1 && (*argv[1] != '-')) {
424     argv++;
425     argc--;
426   }
427
428   fault_setup(fault_continue);
429
430   signal(SIGHUP ,SIGNAL_CAST sig_hup);
431   signal(SIGTERM,SIGNAL_CAST sig_term);
432
433   while ((opt = getopt(argc, argv, "s:T:I:C:bAi:B:N:Rn:l:d:Dp:hSH:G:")) != EOF)
434     {
435       switch (opt)
436         {
437         case 's':
438           strcpy(servicesf,optarg);
439           break;          
440         case 'N':
441         case 'B':
442         case 'I':
443         case 'C':
444         case 'G':
445           DEBUG(0,("Obsolete option '%c' used\n",opt));
446           break;
447         case 'H':
448           strcpy(host_file,optarg);
449           break;
450         case 'n':
451           strcpy(myname,optarg);
452           strupper(myname);
453           break;
454         case 'l':
455           sprintf(debugf,"%s.nmb",optarg);
456           break;
457         case 'i':
458           strcpy(scope,optarg);
459           strupper(scope);
460           break;
461         case 'D':
462           is_daemon = True;
463           break;
464         case 'd':
465           DEBUGLEVEL = atoi(optarg);
466           break;
467         case 'p':
468           port = atoi(optarg);
469           break;
470         case 'h':
471           usage(argv[0]);
472           exit(0);
473           break;
474         default:
475           if (!is_a_socket(0)) {
476             usage(argv[0]);
477           }
478           break;
479         }
480     }
481
482   DEBUG(1,("%s netbios nameserver version %s started\n",timestring(),VERSION));
483   DEBUG(1,("Copyright Andrew Tridgell 1994\n"));
484
485   if (!reload_services(False))
486     return(-1); 
487
488   init_structs();
489
490   set_samba_nb_type();
491
492   if (!is_daemon && !is_a_socket(0)) {
493     DEBUG(0,("standard input is not a socket, assuming -D option\n"));
494     is_daemon = True;
495   }
496   
497   if (is_daemon) {
498     DEBUG(2,("%s becoming a daemon\n",timestring()));
499     become_daemon();
500   }
501
502   DEBUG(3,("Opening sockets %d\n", port));
503
504   if (!open_sockets(is_daemon,port)) return 1;
505
506   if (*host_file) {
507     load_hosts_file(host_file);
508     DEBUG(3,("Loaded hosts file\n"));
509   }
510
511
512
513   if (!*ServerComment)
514     strcpy(ServerComment,"Samba %v");
515   string_sub(ServerComment,"%v",VERSION);
516   string_sub(ServerComment,"%h",myhostname);
517
518   add_my_names();
519
520   if (strequal(lp_workgroup(),"*")) {
521     DEBUG(0,("ERROR: a workgroup name of * is no longer supported\n"));
522   }
523
524   add_my_subnets(lp_workgroup());
525
526   DEBUG(3,("Checked names\n"));
527   
528   load_netbios_names();
529
530   DEBUG(3,("Loaded names\n"));
531
532   write_browse_list();
533
534   DEBUG(3,("Dumped names\n"));
535
536   process();
537   close_sockets();
538
539   if (dbf)
540     fclose(dbf);
541   return(0);
542 }