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