- added interface.c and removed all the references to myip, bcast_ip
[samba.git] / source3 / namedb.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 #include "smb.h"
30 #include "loadparm.h"
31
32 extern int ClientNMB;
33 extern int ClientDGRAM;
34
35 extern int DEBUGLEVEL;
36
37 extern time_t StartupTime;
38 extern pstring myname;
39 extern pstring scope;
40
41 /* this is our browse master/backup cache database */
42 struct browse_cache_record *browserlist = NULL;
43
44 /* this is our domain/workgroup/server database */
45 struct domain_record *domainlist = NULL;
46
47 static BOOL updatedlists = True;
48 int  updatecount=0;
49
50 int workgroup_count = 0; /* unique index key: one for each workgroup */
51
52 /* what server type are we currently */
53
54 #define DFLT_SERVER_TYPE (SV_TYPE_WORKSTATION | SV_TYPE_SERVER | \
55                         SV_TYPE_TIME_SOURCE | SV_TYPE_SERVER_UNIX | \
56                         SV_TYPE_PRINTQ_SERVER | SV_TYPE_POTENTIAL_BROWSER)
57
58 /* here are my election parameters */
59 #define MSBROWSE "\001\002__MSBROWSE__\002"
60
61
62 /****************************************************************************
63   add a workgroup into the domain list
64   **************************************************************************/
65 static void add_workgroup(struct work_record *work, struct domain_record *d)
66 {
67   struct work_record *w2;
68
69   if (!work || !d) return;
70
71   if (!d->workgrouplist)
72     {
73       d->workgrouplist = work;
74       work->prev = NULL;
75       work->next = NULL;
76       return;
77     }
78   
79   for (w2 = d->workgrouplist; w2->next; w2 = w2->next);
80   
81   w2->next = work;
82   work->next = NULL;
83   work->prev = w2;
84 }
85
86
87 /****************************************************************************
88   create a blank workgroup 
89   **************************************************************************/
90 static struct work_record *make_workgroup(char *name)
91 {
92   struct work_record *work;
93   struct domain_record *d;
94   int t = -1;
95   
96   if (!name || !name[0]) return NULL;
97   
98   work = (struct work_record *)malloc(sizeof(*work));
99   if (!work) return(NULL);
100   
101   StrnCpy(work->work_group,name,sizeof(work->work_group)-1);
102   work->serverlist = NULL;
103   
104   work->ServerType = DFLT_SERVER_TYPE;
105   work->RunningElection = False;
106   work->ElectionCount = 0;
107   work->needelection = False;
108   work->needannounce = True;
109   
110   /* make sure all token representations of workgroups are unique */
111   
112   for (d = domainlist; d && t == -1; d = d->next)
113     {
114       struct work_record *w;
115       for (w = d->workgrouplist; w && t == -1; w = w->next)
116         {
117           if (strequal(w->work_group, work->work_group)) t = w->token;
118         }
119     }
120   
121   if (t == -1)
122     {
123       work->token = ++workgroup_count;
124     }
125   else
126     {
127       work->token = t;
128     }
129   
130   
131   /* WfWg  uses 01040b01 */
132   /* Win95 uses 01041501 */
133   /* NTAS  uses ???????? */
134   work->ElectionCriterion  = (MAINTAIN_LIST<<1)|(ELECTION_VERSION<<8); 
135   work->ElectionCriterion |= (lp_os_level() << 24);
136   if (lp_domain_master()) {
137     work->ElectionCriterion |= 0x80;
138   }
139   
140   return work;
141 }
142
143
144 /*******************************************************************
145   expire old servers in the serverlist
146   time of -1 indicates everybody dies
147   ******************************************************************/
148 static void remove_old_servers(struct work_record *work, time_t t)
149 {
150   struct server_record *s;
151   struct server_record *nexts;
152   
153   /* expire old entries in the serverlist */
154   for (s = work->serverlist; s; s = nexts)
155     {
156       if (t == -1 || (s->death_time && s->death_time < t))
157         {
158           DEBUG(3,("Removing dead server %s\n",s->serv.name));
159           updatedlists = True;
160           nexts = s->next;
161           
162           if (s->prev) s->prev->next = s->next;
163           if (s->next) s->next->prev = s->prev;
164           
165           if (work->serverlist == s) 
166             work->serverlist = s->next; 
167
168           free(s);
169         }
170       else
171         {
172           nexts = s->next;
173         }
174     }
175 }
176
177
178 /*******************************************************************
179   remove workgroups
180   ******************************************************************/
181 struct work_record *remove_workgroup(struct domain_record *d, 
182                                      struct work_record *work)
183 {
184   struct work_record *ret_work = NULL;
185   
186   if (!d || !work) return NULL;
187   
188   DEBUG(3,("Removing old workgroup %s\n", work->work_group));
189   
190   remove_old_servers(work, -1);
191   
192   ret_work = work->next;
193   
194   if (work->prev) work->prev->next = work->next;
195   if (work->next) work->next->prev = work->prev;
196   
197   if (d->workgrouplist == work) d->workgrouplist = work->next; 
198   
199   free(work);
200   
201   return ret_work;
202 }
203
204
205 /****************************************************************************
206   add a domain into the list
207   **************************************************************************/
208 static void add_domain(struct domain_record *d)
209 {
210   struct domain_record *d2;
211
212   if (!domainlist)
213   {
214     domainlist = d;
215     d->prev = NULL;
216     d->next = NULL;
217     return;
218   }
219
220   for (d2 = domainlist; d2->next; d2 = d2->next);
221
222   d2->next = d;
223   d->next = NULL;
224   d->prev = d2;
225 }
226
227 /***************************************************************************
228   add a browser into the list
229   **************************************************************************/
230 static void add_browse_cache(struct browse_cache_record *b)
231 {
232   struct browse_cache_record *b2;
233
234   if (!browserlist)
235     {
236       browserlist = b;
237       b->prev = NULL;
238       b->next = NULL;
239       return;
240     }
241   
242   for (b2 = browserlist; b2->next; b2 = b2->next) ;
243   
244   b2->next = b;
245   b->next = NULL;
246   b->prev = b2;
247 }
248
249
250 /***************************************************************************
251   add a server into the list
252   **************************************************************************/
253 static void add_server(struct work_record *work,struct server_record *s)
254 {
255   struct server_record *s2;
256
257   if (!work->serverlist) {
258     work->serverlist = s;
259     s->prev = NULL;
260     s->next = NULL;
261     return;
262   }
263
264   for (s2 = work->serverlist; s2->next; s2 = s2->next) ;
265
266   s2->next = s;
267   s->next = NULL;
268   s->prev = s2;
269 }
270
271
272 /*******************************************************************
273   remove old browse entries
274   ******************************************************************/
275 void expire_browse_cache(time_t t)
276 {
277   struct browse_cache_record *b;
278   struct browse_cache_record *nextb;
279   
280   /* expire old entries in the serverlist */
281   for (b = browserlist; b; b = nextb)
282     {
283       if (b->synced && b->sync_time < t)
284         {
285           DEBUG(3,("Removing dead cached browser %s\n",b->name));
286           nextb = b->next;
287           
288           if (b->prev) b->prev->next = b->next;
289           if (b->next) b->next->prev = b->prev;
290           
291           if (browserlist == b) browserlist = b->next; 
292           
293           free(b);
294         }
295       else
296         {
297           nextb = b->next;
298         }
299     }
300 }
301
302
303 /****************************************************************************
304   find a workgroup in the workgrouplist 
305   only create it if the domain allows it, or the parameter 'add' insists
306   that it get created/added anyway. this allows us to force entries in
307   lmhosts file to be added.
308   **************************************************************************/
309 struct work_record *find_workgroupstruct(struct domain_record *d, 
310                                          fstring name, BOOL add)
311 {
312   struct work_record *ret, *work;
313   
314   if (!d) return NULL;
315   
316   DEBUG(4, ("workgroup search for %s: ", name));
317   
318   if (strequal(name, "*"))
319     {
320       DEBUG(2,("add any workgroups: initiating browser search on %s\n",
321                inet_ntoa(d->bcast_ip)));
322       queue_netbios_pkt_wins(ClientNMB,NMB_QUERY, FIND_MASTER,
323                              MSBROWSE,0x1,0,
324                              True,False, d->bcast_ip);
325       return NULL;
326     }
327   
328   for (ret = d->workgrouplist; ret; ret = ret->next) {
329     if (!strcmp(ret->work_group,name)) {
330       DEBUG(4, ("found\n"));
331       return(ret);
332     }
333   }
334
335   if (!add) {
336     DEBUG(4, ("not found\n"));
337     return NULL;
338   }
339
340   DEBUG(4,("not found: creating\n"));
341   
342   if ((work = make_workgroup(name)))
343     {
344       if (lp_preferred_master() &&
345           strequal(lp_workgroup(), name) &&
346           ismybcast(d->bcast_ip))
347         {
348           DEBUG(3, ("preferred master startup for %s\n", work->work_group));
349           work->needelection = True;
350           work->ElectionCriterion |= (1<<3);
351         }
352       if (!ismybcast(d->bcast_ip))
353         {
354           work->needelection = False;
355         }
356       add_workgroup(work, d);
357       return(work);
358     }
359   return NULL;
360 }
361
362 /****************************************************************************
363   find a domain in the domainlist 
364   **************************************************************************/
365 struct domain_record *find_domain(struct in_addr source_ip)
366 {   
367   struct domain_record *d;
368   
369   /* search through domain list for broadcast/netmask that matches
370      the source ip address */
371   
372   for (d = domainlist; d; d = d->next)
373     {
374       if (same_net(source_ip, d->bcast_ip, d->mask_ip))
375         {
376           return(d);
377         }
378     }
379   
380   return (NULL);
381 }
382
383
384 /****************************************************************************
385   dump a copy of the workgroup/domain database
386   **************************************************************************/
387 void dump_workgroups(void)
388 {
389   struct domain_record *d;
390   
391   for (d = domainlist; d; d = d->next)
392     {
393       if (d->workgrouplist)
394         {
395           struct work_record *work;
396           
397           DEBUG(4,("dump domain bcast=%15s: ", inet_ntoa(d->bcast_ip)));
398           DEBUG(4,(" netmask=%15s:\n", inet_ntoa(d->mask_ip)));
399           
400           for (work = d->workgrouplist; work; work = work->next)
401             {
402               DEBUG(4,("\t%s(%d)\n", work->work_group, work->token));
403               if (work->serverlist)
404                 {
405                   struct server_record *s;                
406                   for (s = work->serverlist; s; s = s->next)
407                     {
408                       DEBUG(4,("\t\t%s %8x (%s)\n",
409                                s->serv.name, s->serv.type, s->serv.comment));
410                     }
411                 }
412             }
413         }
414     }
415 }
416
417 /****************************************************************************
418   create a domain entry
419   ****************************************************************************/
420 static struct domain_record *make_domain(struct in_addr ip, struct in_addr mask)
421 {
422   struct domain_record *d;
423   d = (struct domain_record *)malloc(sizeof(*d));
424   
425   if (!d) return(NULL);
426   
427   bzero((char *)d,sizeof(*d));
428   
429   DEBUG(4, ("making domain %s ", inet_ntoa(ip)));
430   DEBUG(4, ("%s\n", inet_ntoa(mask)));
431   
432   d->bcast_ip = ip;
433   d->mask_ip  = mask;
434   d->workgrouplist = NULL;
435   
436   add_domain(d);
437   
438   return d;
439 }
440
441 /****************************************************************************
442   add a domain entry. creates a workgroup, if necessary, and adds the domain
443   to the named a workgroup.
444   ****************************************************************************/
445 struct domain_record *add_domain_entry(struct in_addr source_ip, 
446                                        struct in_addr source_mask,
447                                        char *name, BOOL add)
448 {
449   struct domain_record *d;
450   struct in_addr ip;
451   
452   ip = *interpret_addr2("255.255.255.255");
453   
454   if (zero_ip(source_ip)) 
455     source_ip = *iface_bcast(source_ip);
456   
457   /* add the domain into our domain database */
458   if ((d = find_domain(source_ip)) ||
459       (d = make_domain(source_ip, source_mask)))
460     {
461       struct work_record *w = find_workgroupstruct(d, name, add);
462       
463       /* add WORKGROUP(1e) and WORKGROUP(00) entries into name database
464          or register with WINS server, if it's our workgroup */
465       if (strequal(lp_workgroup(), name))
466         {
467           extern pstring ServerComment;
468           add_name_entry(name,0x1e,NB_ACTIVE|NB_GROUP);
469           add_name_entry(name,0x0 ,NB_ACTIVE|NB_GROUP);
470           add_server_entry(d,w,myname,w->ServerType,0,ServerComment,True);
471         }
472       
473       DEBUG(3,("Added domain name entry %s at %s\n", name,inet_ntoa(ip)));
474       return d;
475     }
476   return NULL;
477 }
478
479 /****************************************************************************
480   add a browser entry
481   ****************************************************************************/
482 struct browse_cache_record *add_browser_entry(char *name, int type, char *wg,
483                                               time_t ttl, struct in_addr ip)
484 {
485   BOOL newentry=False;
486   
487   struct browse_cache_record *b;
488
489   /* search for the entry: if it's already in the cache, update that entry */
490   for (b = browserlist; b; b = b->next)
491     {
492       if (ip_equal(ip,b->ip) && strequal(b->group, wg)) break;
493     }
494   
495   if (b && b->synced)
496     {
497       /* entries get left in the cache for a while. this stops sync'ing too
498          often if the network is large */
499       DEBUG(4, ("browser %s %s %s already sync'd at time %d\n",
500                 b->name, b->group, inet_ntoa(b->ip), b->sync_time));
501       return NULL;
502     }
503   
504   if (!b)
505     {
506       newentry = True;
507       b = (struct browse_cache_record *)malloc(sizeof(*b));
508       
509       if (!b) return(NULL);
510       
511       bzero((char *)b,sizeof(*b));
512     }
513   
514   /* update the entry */
515   ttl = time(NULL)+ttl;
516   
517   StrnCpy(b->name ,name,sizeof(b->name )-1);
518   StrnCpy(b->group,wg  ,sizeof(b->group)-1);
519   strupper(b->name);
520   strupper(b->group);
521   
522   b->ip     = ip;
523   b->type   = type;
524   
525   if (newentry || ttl < b->sync_time) 
526     b->sync_time = ttl;
527   
528   if (newentry)
529     {
530       b->synced = False;
531       add_browse_cache(b);
532       
533       DEBUG(3,("Added cache entry %s %s(%2x) %s ttl %d\n",
534                wg, name, type, inet_ntoa(ip),ttl));
535     }
536   else
537     {
538       DEBUG(3,("Updated cache entry %s %s(%2x) %s ttl %d\n",
539                wg, name, type, inet_ntoa(ip),ttl));
540     }
541   
542   return(b);
543 }
544
545
546 /****************************************************************************
547   add a server entry
548   ****************************************************************************/
549 struct server_record *add_server_entry(struct domain_record *d, 
550                                        struct work_record *work,
551                                        char *name,int servertype, 
552                                        int ttl,char *comment,
553                                        BOOL replace)
554 {
555   BOOL newentry=False;
556   struct server_record *s;
557   
558   if (name[0] == '*')
559     {
560       return (NULL);
561     }
562   
563   for (s = work->serverlist; s; s = s->next)
564     {
565       if (strequal(name,s->serv.name)) break;
566     }
567   
568   if (s && !replace)
569     {
570       DEBUG(4,("Not replacing %s\n",name));
571       return(s);
572     }
573   
574   updatedlists=True;
575   
576   if (!s)
577     {
578       newentry = True;
579       s = (struct server_record *)malloc(sizeof(*s));
580       
581       if (!s) return(NULL);
582       
583       bzero((char *)s,sizeof(*s));
584     }
585   
586   if (ismybcast(d->bcast_ip) &&
587       strequal(lp_workgroup(),work->work_group))
588     {
589       servertype |= SV_TYPE_LOCAL_LIST_ONLY;
590     }
591   else
592     {
593       servertype &= ~SV_TYPE_LOCAL_LIST_ONLY;
594     }
595   
596   /* update the entry */
597   StrnCpy(s->serv.name,name,sizeof(s->serv.name)-1);
598   StrnCpy(s->serv.comment,comment,sizeof(s->serv.comment)-1);
599   strupper(s->serv.name);
600   s->serv.type  = servertype;
601   s->death_time = ttl?time(NULL)+ttl*3:0;
602   
603   /* for a domain entry, the comment field refers to the server name */
604   
605   if (s->serv.type & SV_TYPE_DOMAIN_ENUM) strupper(s->serv.comment);
606   
607   if (newentry)
608     {
609       add_server(work, s);
610       
611       DEBUG(3,("Added "));
612     }
613   else
614     {
615       DEBUG(3,("Updated "));
616     }
617   
618   DEBUG(3,("server entry %s of type %x (%s) to %s %s\n",
619            name,servertype,comment,
620            work->work_group,inet_ntoa(d->bcast_ip)));
621   
622   return(s);
623 }
624
625
626 /*******************************************************************
627   write out browse.dat
628   ******************************************************************/
629 void write_browse_list(void)
630 {
631   struct domain_record *d;
632   
633   pstring fname,fnamenew;
634   FILE *f;
635   
636   if (!updatedlists) return;
637   
638   dump_names();
639   dump_workgroups();
640   
641   updatedlists = False;
642   updatecount++;
643   
644   strcpy(fname,lp_lockdir());
645   trim_string(fname,NULL,"/");
646   strcat(fname,"/");
647   strcat(fname,SERVER_LIST);
648   strcpy(fnamenew,fname);
649   strcat(fnamenew,".");
650   
651   f = fopen(fnamenew,"w");
652   
653   if (!f)
654     {
655       DEBUG(4,("Can't open %s - %s\n",fnamenew,strerror(errno)));
656       return;
657     }
658   
659   for (d = domainlist; d ; d = d->next)
660     {
661       struct work_record *work;
662       for (work = d->workgrouplist; work ; work = work->next)
663         {
664           struct server_record *s;
665           for (s = work->serverlist; s ; s = s->next)
666             {
667               fstring tmp;
668               
669               /* don't list domains I don't have a master for */
670               if ((s->serv.type & SV_TYPE_DOMAIN_ENUM) &&
671                   !s->serv.comment[0])
672                 {
673                   continue;
674                 }
675               
676               /* output server details, plus what workgroup/domain
677                  they're in. without the domain information, the
678                  combined list of all servers in all workgroups gets
679                  sent to anyone asking about any workgroup! */
680               
681               sprintf(tmp, "\"%s\"", s->serv.name);
682               fprintf(f, "%-25s ", tmp);
683               fprintf(f, "%08x ", s->serv.type);
684               sprintf(tmp, "\"%s\" ", s->serv.comment);
685               fprintf(f, "%-30s", tmp);
686               fprintf(f, "\"%s\"\n", work->work_group);
687             }
688         }
689     }
690   
691   fclose(f);
692   unlink(fname);
693   chmod(fnamenew,0644);
694   rename(fnamenew,fname);   
695   DEBUG(3,("Wrote browse list %s\n",fname));
696 }
697
698
699 /*******************************************************************
700   expire old servers in the serverlist
701   ******************************************************************/
702 void expire_servers(time_t t)
703 {
704   struct domain_record *d;
705   
706   for (d = domainlist ; d ; d = d->next)
707     {
708       struct work_record *work;
709       
710       for (work = d->workgrouplist; work; work = work->next)
711         {
712           remove_old_servers(work, t);
713         }
714     }
715 }
716