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