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