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