updated doc files to include a GPL header, with revision history in it.
[kai/samba.git] / source3 / namedbwork.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-1996
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    04 jul 96: lkcl@pires.co.uk
27    created module namedbwork containing workgroup database functions
28
29 */
30
31 #include "includes.h"
32 #include "smb.h"
33
34 extern int ClientNMB;
35
36 extern int DEBUGLEVEL;
37
38 /* this is our domain/workgroup/server database */
39 extern struct subnet_record *subnetlist;
40
41 int workgroup_count = 0; /* unique index key: one for each workgroup */
42
43 /* what server type are we currently */
44
45 #define DFLT_SERVER_TYPE (SV_TYPE_WORKSTATION | SV_TYPE_SERVER | \
46                         SV_TYPE_TIME_SOURCE | SV_TYPE_SERVER_UNIX | \
47                         SV_TYPE_PRINTQ_SERVER | SV_TYPE_POTENTIAL_BROWSER)
48
49
50 /****************************************************************************
51   add a workgroup into the domain list
52   **************************************************************************/
53 static void add_workgroup(struct work_record *work, struct subnet_record *d)
54 {
55   struct work_record *w2;
56
57   if (!work || !d) return;
58
59   if (!d->workgrouplist)
60     {
61       d->workgrouplist = work;
62       work->prev = NULL;
63       work->next = NULL;
64       return;
65     }
66   
67   for (w2 = d->workgrouplist; w2->next; w2 = w2->next);
68   
69   w2->next = work;
70   work->next = NULL;
71   work->prev = w2;
72 }
73
74
75 /****************************************************************************
76   create a blank workgroup 
77   **************************************************************************/
78 static struct work_record *make_workgroup(char *name)
79 {
80   struct work_record *work;
81   struct subnet_record *d;
82   int t = -1;
83   
84   if (!name || !name[0]) return NULL;
85   
86   work = (struct work_record *)malloc(sizeof(*work));
87   if (!work) return(NULL);
88   
89   StrnCpy(work->work_group,name,sizeof(work->work_group)-1);
90   work->serverlist = NULL;
91   
92   work->ServerType = DFLT_SERVER_TYPE;
93   work->RunningElection = False;
94   work->ElectionCount = 0;
95   work->needelection = False;
96   work->needannounce = True;
97   work->state = MST_NONE;
98   
99   /* make sure all token representations of workgroups are unique */
100   
101   for (d = subnetlist; d && t == -1; d = d->next)
102     {
103       struct work_record *w;
104       for (w = d->workgrouplist; w && t == -1; w = w->next)
105         {
106           if (strequal(w->work_group, work->work_group)) t = w->token;
107         }
108     }
109   
110   if (t == -1)
111     {
112       work->token = ++workgroup_count;
113     }
114   else
115     {
116       work->token = t;
117     }
118   
119   
120   /* WfWg  uses 01040b01 */
121   /* Win95 uses 01041501 */
122   /* NTAS  uses ???????? */
123   work->ElectionCriterion  = (MAINTAIN_LIST<<1)|(ELECTION_VERSION<<8); 
124   work->ElectionCriterion |= (lp_os_level() << 24);
125   if (lp_domain_master()) {
126     work->ElectionCriterion |= 0x80;
127   }
128   
129   return work;
130 }
131
132
133 /*******************************************************************
134   remove workgroups
135   ******************************************************************/
136 struct work_record *remove_workgroup(struct subnet_record *d, 
137                                      struct work_record *work,
138                                          BOOL remove_all_servers)
139 {
140   struct work_record *ret_work = NULL;
141   
142   if (!d || !work) return NULL;
143   
144   DEBUG(3,("Removing old workgroup %s\n", work->work_group));
145   
146   ret_work = work->next;
147
148   remove_old_servers(work, -1, remove_all_servers);
149   
150   if (!work->serverlist)
151   {
152     if (work->prev) work->prev->next = work->next;
153     if (work->next) work->next->prev = work->prev;
154   
155     if (d->workgrouplist == work) d->workgrouplist = work->next; 
156   
157     free(work);
158   }
159   
160   return ret_work;
161 }
162
163
164 /****************************************************************************
165   find a workgroup in the workgrouplist 
166   only create it if the domain allows it, or the parameter 'add' insists
167   that it get created/added anyway. this allows us to force entries in
168   lmhosts file to be added.
169   **************************************************************************/
170 struct work_record *find_workgroupstruct(struct subnet_record *d, 
171                                          fstring name, BOOL add)
172 {
173   struct work_record *ret, *work;
174   
175   if (!d) return NULL;
176   
177   DEBUG(4, ("workgroup search for %s: ", name));
178   
179   if (strequal(name, "*"))
180     {
181       DEBUG(2,("add any workgroups: initiating browser search on %s\n",
182                inet_ntoa(d->bcast_ip)));
183       queue_netbios_pkt_wins(d,ClientNMB,NMB_QUERY, NAME_QUERY_FIND_MST,
184                              MSBROWSE,0x1,0,0,0,NULL,NULL,
185                              True,False, d->bcast_ip, d->bcast_ip);
186       return NULL;
187     }
188   
189   for (ret = d->workgrouplist; ret; ret = ret->next) {
190     if (!strcmp(ret->work_group,name)) {
191       DEBUG(4, ("found\n"));
192       return(ret);
193     }
194   }
195
196   if (!add) {
197     DEBUG(4, ("not found\n"));
198     return NULL;
199   }
200
201   DEBUG(4,("not found: creating\n"));
202   
203   if ((work = make_workgroup(name)))
204     {
205       if (lp_preferred_master() &&
206           strequal(lp_workgroup(), name))
207         {
208           DEBUG(3, ("preferred master startup for %s\n", work->work_group));
209           work->needelection = True;
210           work->ElectionCriterion |= (1<<3);
211         }
212       add_workgroup(work, d);
213       return(work);
214     }
215   return NULL;
216 }
217
218
219 /****************************************************************************
220   dump a copy of the workgroup/domain database
221   **************************************************************************/
222 void dump_workgroups(void)
223 {
224   struct subnet_record *d;
225   
226   for (d = subnetlist; d; d = d->next)
227     {
228       if (d->workgrouplist)
229         {
230           struct work_record *work;
231           
232           DEBUG(4,("dump domain bcast=%15s: ", inet_ntoa(d->bcast_ip)));
233           DEBUG(4,(" netmask=%15s:\n", inet_ntoa(d->mask_ip)));
234           
235           for (work = d->workgrouplist; work; work = work->next)
236             {
237               DEBUG(4,("\t%s(%d)\n", work->work_group, work->token));
238               if (work->serverlist)
239                 {
240                   struct server_record *s;                
241                   for (s = work->serverlist; s; s = s->next)
242                     {
243                       DEBUG(4,("\t\t%s %8x (%s)\n",
244                                s->serv.name, s->serv.type, s->serv.comment));
245                     }
246                 }
247             }
248         }
249     }
250 }