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