import HEAD into svn+ssh://svn.samba.org/home/svn/samba/trunk
[metze/old/v3-2-winbind-ndr.git] / source / nmbd / nmbd_workgroupdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3    NBT netbios routines and daemon - version 2
4    Copyright (C) Andrew Tridgell 1994-1998
5    Copyright (C) Luke Kenneth Casson Leighton 1994-1998
6    Copyright (C) Jeremy Allison 1994-1998
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21    
22 */
23
24 #include "includes.h"
25
26 extern int ClientNMB;
27
28 extern uint16 samba_nb_type;
29
30 int workgroup_count = 0; /* unique index key: one for each workgroup */
31
32 /****************************************************************************
33   Add a workgroup into the list.
34 **************************************************************************/
35
36 static void add_workgroup(struct subnet_record *subrec, struct work_record *work)
37 {
38         work->subnet = subrec;
39         DLIST_ADD(subrec->workgrouplist, work);
40         subrec->work_changed = True;
41 }
42
43 /****************************************************************************
44   Create an empty workgroup.
45 **************************************************************************/
46
47 static struct work_record *create_workgroup(const char *name, int ttl)
48 {
49         struct work_record *work;
50         struct subnet_record *subrec;
51         nstring nname;
52                                                                                                                          
53         int t = -1;
54   
55         if((work = (struct work_record *)malloc(sizeof(*work))) == NULL) {
56                 DEBUG(0,("create_workgroup: malloc fail !\n"));
57                 return NULL;
58         }
59         memset((char *)work, '\0', sizeof(*work));
60
61         errno = 0;
62         push_ascii_nstring(nname, name);
63         if (errno == E2BIG) {
64                 unstring tname;
65                 pull_ascii_nstring(tname, sizeof(tname), nname);
66                 unstrcpy(work->work_group,tname);
67                 DEBUG(0,("create_workgroup: workgroup name %s is too long. Truncating to %s\n",
68                         name, tname));
69         } else {
70                 unstrcpy(work->work_group,name);
71         }
72         work->serverlist = NULL;
73   
74         work->RunningElection = False;
75         work->ElectionCount = 0;
76         work->announce_interval = 0;
77         work->needelection = False;
78         work->needannounce = True;
79         work->lastannounce_time = time(NULL);
80         work->mst_state = lp_local_master() ? MST_POTENTIAL : MST_NONE;
81         work->dom_state = DOMAIN_NONE;
82         work->log_state = LOGON_NONE;
83   
84         work->death_time = (ttl != PERMANENT_TTL) ? time(NULL)+(ttl*3) : PERMANENT_TTL;
85
86         /* Make sure all token representations of workgroups are unique. */
87   
88         for (subrec = FIRST_SUBNET; subrec && (t == -1); subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
89                 struct work_record *w;
90                 for (w = subrec->workgrouplist; w && t == -1; w = w->next) {
91                         if (strequal(w->work_group, work->work_group))
92                                 t = w->token;
93                 }
94         }
95   
96         if (t == -1)
97                 work->token = ++workgroup_count;
98         else
99                 work->token = t;
100   
101         /* No known local master browser as yet. */
102         *work->local_master_browser_name = '\0';
103
104         /* No known domain master browser as yet. */
105         *work->dmb_name.name = '\0';
106         zero_ip(&work->dmb_addr);
107
108         /* WfWg  uses 01040b01 */
109         /* Win95 uses 01041501 */
110         /* NTAS  uses ???????? */
111         work->ElectionCriterion  = (MAINTAIN_LIST)|(BROWSER_ELECTION_VERSION<<8); 
112         work->ElectionCriterion |= (lp_os_level() << 24);
113         if (lp_domain_master())
114                 work->ElectionCriterion |= 0x80;
115   
116         return work;
117 }
118
119 /*******************************************************************
120   Remove a workgroup.
121 ******************************************************************/
122
123 static struct work_record *remove_workgroup_from_subnet(struct subnet_record *subrec, 
124                                      struct work_record *work)
125 {
126         struct work_record *ret_work = NULL;
127   
128         DEBUG(3,("remove_workgroup: Removing workgroup %s\n", work->work_group));
129   
130         ret_work = work->next;
131
132         remove_all_servers(work);
133   
134         if (!work->serverlist) {
135                 if (work->prev)
136                         work->prev->next = work->next;
137                 if (work->next)
138                         work->next->prev = work->prev;
139   
140                 if (subrec->workgrouplist == work)
141                         subrec->workgrouplist = work->next; 
142   
143                 ZERO_STRUCTP(work);
144                 SAFE_FREE(work);
145         }
146   
147         subrec->work_changed = True;
148
149         return ret_work;
150 }
151
152 /****************************************************************************
153   Find a workgroup in the workgroup list of a subnet.
154 **************************************************************************/
155
156 struct work_record *find_workgroup_on_subnet(struct subnet_record *subrec, 
157                                              const char *name)
158 {
159         struct work_record *ret;
160   
161         DEBUG(4, ("find_workgroup_on_subnet: workgroup search for %s on subnet %s: ",
162                 name, subrec->subnet_name));
163   
164         for (ret = subrec->workgrouplist; ret; ret = ret->next) {
165                 if (strequal(ret->work_group,name)) {
166                         DEBUGADD(4, ("found.\n"));
167                         return(ret);
168                 }
169         }
170         DEBUGADD(4, ("not found.\n"));
171         return NULL;
172 }
173
174 /****************************************************************************
175   Create a workgroup in the workgroup list of the subnet.
176 **************************************************************************/
177
178 struct work_record *create_workgroup_on_subnet(struct subnet_record *subrec,
179                                                const char *name, int ttl)
180 {
181         struct work_record *work = NULL;
182
183         DEBUG(4,("create_workgroup_on_subnet: creating group %s on subnet %s\n",
184                 name, subrec->subnet_name));
185   
186         if ((work = create_workgroup(name, ttl))) {
187                 add_workgroup(subrec, work);
188                 subrec->work_changed = True;
189                 return(work);
190         }
191
192         return NULL;
193 }
194
195 /****************************************************************************
196   Update a workgroup ttl.
197 **************************************************************************/
198
199 void update_workgroup_ttl(struct work_record *work, int ttl)
200 {
201         if(work->death_time != PERMANENT_TTL)
202                 work->death_time = time(NULL)+(ttl*3);
203         work->subnet->work_changed = True;
204 }
205
206 /****************************************************************************
207  Fail function called if we cannot register the WORKGROUP<0> and
208  WORKGROUP<1e> names on the net.
209 **************************************************************************/
210      
211 static void fail_register(struct subnet_record *subrec, struct response_record *rrec,
212                           struct nmb_name *nmbname)
213 {  
214         DEBUG(0,("fail_register: Failed to register name %s on subnet %s.\n",
215                 nmb_namestr(nmbname), subrec->subnet_name));
216 }  
217
218 /****************************************************************************
219  If the workgroup is our primary workgroup, add the required names to it.
220 **************************************************************************/
221
222 void initiate_myworkgroup_startup(struct subnet_record *subrec, struct work_record *work)
223 {
224         int i;
225
226         if(!strequal(lp_workgroup(), work->work_group))
227                 return;
228
229         /* If this is a broadcast subnet then start elections on it if we are so configured. */
230
231         if ((subrec != unicast_subnet) && (subrec != remote_broadcast_subnet) &&
232                         (subrec != wins_server_subnet) && lp_preferred_master() && lp_local_master()) {
233                 DEBUG(3, ("initiate_myworkgroup_startup: preferred master startup for \
234 workgroup %s on subnet %s\n", work->work_group, subrec->subnet_name));
235                 work->needelection = True;
236                 work->ElectionCriterion |= (1<<3);
237         }
238   
239         /* Register the WORKGROUP<0> and WORKGROUP<1e> names on the network. */
240
241         register_name(subrec,lp_workgroup(),0x0,samba_nb_type|NB_GROUP, NULL, fail_register,NULL);
242         register_name(subrec,lp_workgroup(),0x1e,samba_nb_type|NB_GROUP, NULL, fail_register,NULL);
243
244         for( i = 0; my_netbios_names(i); i++) {
245                 const char *name = my_netbios_names(i);
246                 int stype = lp_default_server_announce() | (lp_local_master() ?  SV_TYPE_POTENTIAL_BROWSER : 0 );
247    
248                 if(!strequal(global_myname(), name))
249                         stype &= ~(SV_TYPE_MASTER_BROWSER|SV_TYPE_POTENTIAL_BROWSER|SV_TYPE_DOMAIN_MASTER|SV_TYPE_DOMAIN_MEMBER);
250    
251                 create_server_on_workgroup(work,name,stype|SV_TYPE_LOCAL_LIST_ONLY, PERMANENT_TTL, 
252                                 string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH));
253                 DEBUG(3,("initiate_myworkgroup_startup: Added server name entry %s \
254 on subnet %s\n", name, subrec->subnet_name));
255         }
256
257
258 /****************************************************************************
259   Dump a copy of the workgroup database into the log file.
260   **************************************************************************/
261
262 void dump_workgroups(BOOL force_write)
263 {
264         struct subnet_record *subrec;
265         int debuglevel = force_write ? 0 : 4;
266  
267         for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
268                 if (subrec->workgrouplist) {
269                         struct work_record *work;
270
271                         if( DEBUGLVL( debuglevel ) ) {
272                                 dbgtext( "dump_workgroups()\n " );
273                                 dbgtext( "dump workgroup on subnet %15s: ", subrec->subnet_name );
274                                 dbgtext( "netmask=%15s:\n", inet_ntoa(subrec->mask_ip) );
275                         }
276
277                         for (work = subrec->workgrouplist; work; work = work->next) {
278                                 DEBUGADD( debuglevel, ( "\t%s(%d) current master browser = %s\n", work->work_group,
279                                         work->token, *work->local_master_browser_name ? work->local_master_browser_name : "UNKNOWN" ) );
280                                 if (work->serverlist) {
281                                         struct server_record *servrec;            
282                                         for (servrec = work->serverlist; servrec; servrec = servrec->next) {
283                                                 DEBUGADD( debuglevel, ( "\t\t%s %8x (%s)\n",
284                                                         servrec->serv.name,
285                                                         servrec->serv.type,
286                                                         servrec->serv.comment ) );
287                                         }
288                                 }
289                         }
290                 }
291         }
292 }
293
294 /****************************************************************************
295   Expire any dead servers on all workgroups. If the workgroup has expired
296   remove it.
297   **************************************************************************/
298
299 void expire_workgroups_and_servers(time_t t)
300 {
301         struct subnet_record *subrec;
302    
303         for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
304                 struct work_record *work;
305                 struct work_record *nextwork;
306
307                 for (work = subrec->workgrouplist; work; work = nextwork) {
308                         nextwork = work->next;
309                         expire_servers(work, t);
310
311                         if ((work->serverlist == NULL) && (work->death_time != PERMANENT_TTL) && 
312                                         ((t == -1) || (work->death_time < t))) {
313                                 DEBUG(3,("expire_workgroups_and_servers: Removing timed out workgroup %s\n",
314                                                 work->work_group));
315                                 remove_workgroup_from_subnet(subrec, work);
316                         }
317                 }
318         }
319 }