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