skel_ -> cap_
[Samba/gebeck_regimport.git] / source3 / nmbd / nmbd_workgroupdb.c
blobc957b517cf7dec9606a5e8fb7667418dd8c1c979
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
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.
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.
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.
24 #include "includes.h"
26 extern int ClientNMB;
28 extern uint16 samba_nb_type;
30 int workgroup_count = 0; /* unique index key: one for each workgroup */
32 /****************************************************************************
33 Add a workgroup into the list.
34 **************************************************************************/
36 static void add_workgroup(struct subnet_record *subrec, struct work_record *work)
38 work->subnet = subrec;
39 DLIST_ADD(subrec->workgrouplist, work);
40 subrec->work_changed = True;
43 /****************************************************************************
44 Create an empty workgroup.
45 **************************************************************************/
47 static struct work_record *create_workgroup(const char *name, int ttl)
49 struct work_record *work;
50 struct subnet_record *subrec;
51 int t = -1;
53 if((work = (struct work_record *)malloc(sizeof(*work))) == NULL) {
54 DEBUG(0,("create_workgroup: malloc fail !\n"));
55 return NULL;
57 memset((char *)work, '\0', sizeof(*work));
59 nstrcpy(work->work_group,name);
60 work->serverlist = NULL;
62 work->RunningElection = False;
63 work->ElectionCount = 0;
64 work->announce_interval = 0;
65 work->needelection = False;
66 work->needannounce = True;
67 work->lastannounce_time = time(NULL);
68 work->mst_state = lp_local_master() ? MST_POTENTIAL : MST_NONE;
69 work->dom_state = DOMAIN_NONE;
70 work->log_state = LOGON_NONE;
72 work->death_time = (ttl != PERMANENT_TTL) ? time(NULL)+(ttl*3) : PERMANENT_TTL;
74 /* Make sure all token representations of workgroups are unique. */
76 for (subrec = FIRST_SUBNET; subrec && (t == -1); subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
77 struct work_record *w;
78 for (w = subrec->workgrouplist; w && t == -1; w = w->next) {
79 if (strnequal(w->work_group, work->work_group, sizeof(nstring)))
80 t = w->token;
84 if (t == -1)
85 work->token = ++workgroup_count;
86 else
87 work->token = t;
89 /* No known local master browser as yet. */
90 *work->local_master_browser_name = '\0';
92 /* No known domain master browser as yet. */
93 *work->dmb_name.name = '\0';
94 zero_ip(&work->dmb_addr);
96 /* WfWg uses 01040b01 */
97 /* Win95 uses 01041501 */
98 /* NTAS uses ???????? */
99 work->ElectionCriterion = (MAINTAIN_LIST)|(BROWSER_ELECTION_VERSION<<8);
100 work->ElectionCriterion |= (lp_os_level() << 24);
101 if (lp_domain_master())
102 work->ElectionCriterion |= 0x80;
104 return work;
107 /*******************************************************************
108 Remove a workgroup.
109 ******************************************************************/
111 static struct work_record *remove_workgroup_from_subnet(struct subnet_record *subrec,
112 struct work_record *work)
114 struct work_record *ret_work = NULL;
116 DEBUG(3,("remove_workgroup: Removing workgroup %s\n", work->work_group));
118 ret_work = work->next;
120 remove_all_servers(work);
122 if (!work->serverlist) {
123 if (work->prev)
124 work->prev->next = work->next;
125 if (work->next)
126 work->next->prev = work->prev;
128 if (subrec->workgrouplist == work)
129 subrec->workgrouplist = work->next;
131 ZERO_STRUCTP(work);
132 SAFE_FREE(work);
135 subrec->work_changed = True;
137 return ret_work;
140 /****************************************************************************
141 Find a workgroup in the workgroup list of a subnet.
142 **************************************************************************/
144 struct work_record *find_workgroup_on_subnet(struct subnet_record *subrec,
145 const char *name)
147 struct work_record *ret;
149 DEBUG(4, ("find_workgroup_on_subnet: workgroup search for %s on subnet %s: ",
150 name, subrec->subnet_name));
152 for (ret = subrec->workgrouplist; ret; ret = ret->next) {
153 if (strnequal(ret->work_group,name,sizeof(nstring))) {
154 DEBUGADD(4, ("found.\n"));
155 return(ret);
158 DEBUGADD(4, ("not found.\n"));
159 return NULL;
162 /****************************************************************************
163 Create a workgroup in the workgroup list of the subnet.
164 **************************************************************************/
166 struct work_record *create_workgroup_on_subnet(struct subnet_record *subrec,
167 const char *name, int ttl)
169 struct work_record *work = NULL;
171 DEBUG(4,("create_workgroup_on_subnet: creating group %s on subnet %s\n",
172 name, subrec->subnet_name));
174 if ((work = create_workgroup(name, ttl))) {
175 add_workgroup(subrec, work);
176 subrec->work_changed = True;
177 return(work);
180 return NULL;
183 /****************************************************************************
184 Update a workgroup ttl.
185 **************************************************************************/
187 void update_workgroup_ttl(struct work_record *work, int ttl)
189 if(work->death_time != PERMANENT_TTL)
190 work->death_time = time(NULL)+(ttl*3);
191 work->subnet->work_changed = True;
194 /****************************************************************************
195 Fail function called if we cannot register the WORKGROUP<0> and
196 WORKGROUP<1e> names on the net.
197 **************************************************************************/
199 static void fail_register(struct subnet_record *subrec, struct response_record *rrec,
200 struct nmb_name *nmbname)
202 DEBUG(0,("fail_register: Failed to register name %s on subnet %s.\n",
203 nmb_namestr(nmbname), subrec->subnet_name));
206 /****************************************************************************
207 If the workgroup is our primary workgroup, add the required names to it.
208 **************************************************************************/
210 void initiate_myworkgroup_startup(struct subnet_record *subrec, struct work_record *work)
212 int i;
214 if(!strnequal(lp_workgroup(), work->work_group,sizeof(nstring)))
215 return;
217 /* If this is a broadcast subnet then start elections on it if we are so configured. */
219 if ((subrec != unicast_subnet) && (subrec != remote_broadcast_subnet) &&
220 (subrec != wins_server_subnet) && lp_preferred_master() && lp_local_master()) {
221 DEBUG(3, ("initiate_myworkgroup_startup: preferred master startup for \
222 workgroup %s on subnet %s\n", work->work_group, subrec->subnet_name));
223 work->needelection = True;
224 work->ElectionCriterion |= (1<<3);
227 /* Register the WORKGROUP<0> and WORKGROUP<1e> names on the network. */
229 register_name(subrec,lp_workgroup(),0x0,samba_nb_type|NB_GROUP, NULL, fail_register,NULL);
230 register_name(subrec,lp_workgroup(),0x1e,samba_nb_type|NB_GROUP, NULL, fail_register,NULL);
232 for( i = 0; my_netbios_names(i); i++) {
233 const char *name = my_netbios_names(i);
234 int stype = lp_default_server_announce() | (lp_local_master() ? SV_TYPE_POTENTIAL_BROWSER : 0 );
236 if(!strequal(global_myname(), name))
237 stype &= ~(SV_TYPE_MASTER_BROWSER|SV_TYPE_POTENTIAL_BROWSER|SV_TYPE_DOMAIN_MASTER|SV_TYPE_DOMAIN_MEMBER);
239 create_server_on_workgroup(work,name,stype|SV_TYPE_LOCAL_LIST_ONLY, PERMANENT_TTL,
240 string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH));
241 DEBUG(3,("initiate_myworkgroup_startup: Added server name entry %s \
242 on subnet %s\n", name, subrec->subnet_name));
246 /****************************************************************************
247 Dump a copy of the workgroup database into the log file.
248 **************************************************************************/
250 void dump_workgroups(BOOL force_write)
252 struct subnet_record *subrec;
253 int debuglevel = force_write ? 0 : 4;
255 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
256 if (subrec->workgrouplist) {
257 struct work_record *work;
259 if( DEBUGLVL( debuglevel ) ) {
260 dbgtext( "dump_workgroups()\n " );
261 dbgtext( "dump workgroup on subnet %15s: ", subrec->subnet_name );
262 dbgtext( "netmask=%15s:\n", inet_ntoa(subrec->mask_ip) );
265 for (work = subrec->workgrouplist; work; work = work->next) {
266 DEBUGADD( debuglevel, ( "\t%s(%d) current master browser = %s\n", work->work_group,
267 work->token, *work->local_master_browser_name ? work->local_master_browser_name : "UNKNOWN" ) );
268 if (work->serverlist) {
269 struct server_record *servrec;
270 for (servrec = work->serverlist; servrec; servrec = servrec->next) {
271 DEBUGADD( debuglevel, ( "\t\t%s %8x (%s)\n",
272 servrec->serv.name,
273 servrec->serv.type,
274 servrec->serv.comment ) );
282 /****************************************************************************
283 Expire any dead servers on all workgroups. If the workgroup has expired
284 remove it.
285 **************************************************************************/
287 void expire_workgroups_and_servers(time_t t)
289 struct subnet_record *subrec;
291 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
292 struct work_record *work;
293 struct work_record *nextwork;
295 for (work = subrec->workgrouplist; work; work = nextwork) {
296 nextwork = work->next;
297 expire_servers(work, t);
299 if ((work->serverlist == NULL) && (work->death_time != PERMANENT_TTL) &&
300 ((t == -1) || (work->death_time < t))) {
301 DEBUG(3,("expire_workgroups_and_servers: Removing timed out workgroup %s\n",
302 work->work_group));
303 remove_workgroup_from_subnet(subrec, work);