s3:winbindd: Remove unused set_routing_domain()
[samba.git] / source3 / nmbd / nmbd_workgroupdb.c
blobcd97efd8273aa1f45059b4afd6082b878f0f91d3
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 3 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, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "../librpc/gen_ndr/svcctl.h"
25 #include "nmbd/nmbd.h"
26 #include "lib/util/string_wrappers.h"
28 extern uint16_t 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 Copy name to unstring. Used by create_workgroup() and find_workgroup_on_subnet().
45 **************************************************************************/
47 static void name_to_unstring(unstring unname, const char *name)
49 nstring nname;
51 errno = 0;
52 push_ascii_nstring(nname, name);
53 if (errno == E2BIG) {
54 unstring tname;
55 pull_ascii_nstring(tname, sizeof(tname), nname);
56 strlcpy(unname, tname, sizeof(nname));
57 DEBUG(0,("name_to_nstring: workgroup name %s is too long. Truncating to %s\n",
58 name, tname));
59 } else {
60 unstrcpy(unname, name);
64 /****************************************************************************
65 Create an empty workgroup.
66 **************************************************************************/
68 static struct work_record *create_workgroup(const char *name, int ttl)
70 struct work_record *work;
71 struct subnet_record *subrec;
72 int t = -1;
74 if((work = SMB_MALLOC_P(struct work_record)) == NULL) {
75 DEBUG(0,("create_workgroup: malloc fail !\n"));
76 return NULL;
78 memset((char *)work, '\0', sizeof(*work));
80 name_to_unstring(work->work_group, name);
82 work->serverlist = NULL;
84 work->RunningElection = False;
85 work->ElectionCount = 0;
86 work->announce_interval = 0;
87 work->needelection = False;
88 work->needannounce = True;
89 work->lastannounce_time = time(NULL);
90 work->mst_state = lp_local_master() ? MST_POTENTIAL : MST_NONE;
91 work->dom_state = DOMAIN_NONE;
92 work->log_state = LOGON_NONE;
94 work->death_time = (ttl != PERMANENT_TTL) ? time(NULL)+(ttl*3) : PERMANENT_TTL;
96 /* Make sure all token representations of workgroups are unique. */
98 for (subrec = FIRST_SUBNET; subrec && (t == -1); subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
99 struct work_record *w;
100 for (w = subrec->workgrouplist; w && t == -1; w = w->next) {
101 if (strequal(w->work_group, work->work_group))
102 t = w->token;
106 if (t == -1)
107 work->token = ++workgroup_count;
108 else
109 work->token = t;
111 /* No known local master browser as yet. */
112 *work->local_master_browser_name = '\0';
114 /* No known domain master browser as yet. */
115 *work->dmb_name.name = '\0';
116 zero_ip_v4(&work->dmb_addr);
118 /* WfWg uses 01040b01 */
119 /* Win95 uses 01041501 */
120 /* NTAS uses ???????? */
121 work->ElectionCriterion = (MAINTAIN_LIST)|(BROWSER_ELECTION_VERSION<<8);
122 work->ElectionCriterion |= (lp_os_level() << 24);
123 if (lp_domain_master())
124 work->ElectionCriterion |= 0x80;
126 return work;
129 /*******************************************************************
130 Remove a workgroup.
131 ******************************************************************/
133 static struct work_record *remove_workgroup_from_subnet(struct subnet_record *subrec,
134 struct work_record *work)
136 struct work_record *ret_work = NULL;
138 DEBUG(3,("remove_workgroup: Removing workgroup %s\n", work->work_group));
140 ret_work = work->next;
142 remove_all_servers(work);
144 if (!work->serverlist) {
145 DLIST_REMOVE(subrec->workgrouplist, work);
146 ZERO_STRUCTP(work);
147 SAFE_FREE(work);
150 subrec->work_changed = True;
152 return ret_work;
155 /****************************************************************************
156 Find a workgroup in the workgroup list of a subnet.
157 **************************************************************************/
159 struct work_record *find_workgroup_on_subnet(struct subnet_record *subrec,
160 const char *name)
162 struct work_record *ret;
163 unstring un_name;
165 DEBUG(4, ("find_workgroup_on_subnet: workgroup search for %s on subnet %s: ",
166 name, subrec->subnet_name));
168 name_to_unstring(un_name, name);
170 for (ret = subrec->workgrouplist; ret; ret = ret->next) {
171 if (strequal(ret->work_group,un_name)) {
172 DEBUGADD(4, ("found.\n"));
173 return(ret);
176 DEBUGADD(4, ("not found.\n"));
177 return NULL;
180 /****************************************************************************
181 Create a workgroup in the workgroup list of the subnet.
182 **************************************************************************/
184 struct work_record *create_workgroup_on_subnet(struct subnet_record *subrec,
185 const char *name, int ttl)
187 struct work_record *work = NULL;
189 DEBUG(4,("create_workgroup_on_subnet: creating group %s on subnet %s\n",
190 name, subrec->subnet_name));
192 if ((work = create_workgroup(name, ttl))) {
193 add_workgroup(subrec, work);
194 subrec->work_changed = True;
195 return(work);
198 return NULL;
201 /****************************************************************************
202 Update a workgroup ttl.
203 **************************************************************************/
205 void update_workgroup_ttl(struct work_record *work, int ttl)
207 if(work->death_time != PERMANENT_TTL)
208 work->death_time = time(NULL)+(ttl*3);
209 work->subnet->work_changed = True;
212 /****************************************************************************
213 Fail function called if we cannot register the WORKGROUP<0> and
214 WORKGROUP<1e> names on the net.
215 **************************************************************************/
217 static void fail_register(struct subnet_record *subrec, struct response_record *rrec,
218 struct nmb_name *nmbname)
220 DEBUG(0,("fail_register: Failed to register name %s on subnet %s.\n",
221 nmb_namestr(nmbname), subrec->subnet_name));
224 /****************************************************************************
225 If the workgroup is our primary workgroup, add the required names to it.
226 **************************************************************************/
228 void initiate_myworkgroup_startup(struct subnet_record *subrec, struct work_record *work)
230 const struct loadparm_substitution *lp_sub =
231 loadparm_s3_global_substitution();
232 int i;
234 if(!strequal(lp_workgroup(), work->work_group))
235 return;
237 /* If this is a broadcast subnet then start elections on it if we are so configured. */
239 if ((subrec != unicast_subnet) && (subrec != remote_broadcast_subnet) &&
240 (subrec != wins_server_subnet) && lp_preferred_master() && lp_local_master()) {
241 DEBUG(3, ("initiate_myworkgroup_startup: preferred master startup for \
242 workgroup %s on subnet %s\n", work->work_group, subrec->subnet_name));
243 work->needelection = True;
244 work->ElectionCriterion |= (1<<3);
247 /* Register the WORKGROUP<0> and WORKGROUP<1e> names on the network. */
249 register_name(subrec,lp_workgroup(),0x0,samba_nb_type|NB_GROUP, NULL, fail_register,NULL);
250 register_name(subrec,lp_workgroup(),0x1e,samba_nb_type|NB_GROUP, NULL, fail_register,NULL);
252 for( i = 0; my_netbios_names(i); i++) {
253 const char *name = my_netbios_names(i);
254 int stype = lp_default_server_announce() | (lp_local_master() ? SV_TYPE_POTENTIAL_BROWSER : 0 );
256 if(!strequal(lp_netbios_name(), name))
257 stype &= ~(SV_TYPE_MASTER_BROWSER|SV_TYPE_POTENTIAL_BROWSER|SV_TYPE_DOMAIN_MASTER|SV_TYPE_DOMAIN_MEMBER);
259 create_server_on_workgroup(work,name,stype|SV_TYPE_LOCAL_LIST_ONLY, PERMANENT_TTL,
260 string_truncate(lp_server_string(talloc_tos(), lp_sub), MAX_SERVER_STRING_LENGTH));
261 DEBUG(3,("initiate_myworkgroup_startup: Added server name entry %s \
262 on subnet %s\n", name, subrec->subnet_name));
266 /****************************************************************************
267 Dump a copy of the workgroup database into the log file.
268 **************************************************************************/
270 void dump_workgroups(bool force_write)
272 struct subnet_record *subrec;
273 int debuglevel = force_write ? 0 : 4;
275 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
276 if (subrec->workgrouplist) {
277 struct work_record *work;
279 if( DEBUGLVL( debuglevel ) ) {
280 dbgtext( "dump_workgroups()\n " );
281 dbgtext( "dump workgroup on subnet %15s: ", subrec->subnet_name );
282 dbgtext( "netmask=%15s:\n", inet_ntoa(subrec->mask_ip) );
285 for (work = subrec->workgrouplist; work; work = work->next) {
286 DEBUGADD( debuglevel, ( "\t%s(%d) current master browser = %s\n", work->work_group,
287 work->token, *work->local_master_browser_name ? work->local_master_browser_name : "UNKNOWN" ) );
288 if (work->serverlist) {
289 struct server_record *servrec;
290 for (servrec = work->serverlist; servrec; servrec = servrec->next) {
291 DEBUGADD( debuglevel, ( "\t\t%s %8x (%s)\n",
292 servrec->serv.name,
293 servrec->serv.type,
294 servrec->serv.comment ) );
302 /****************************************************************************
303 Expire any dead servers on all workgroups. If the workgroup has expired
304 remove it.
305 **************************************************************************/
307 void expire_workgroups_and_servers(time_t t)
309 struct subnet_record *subrec;
311 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
312 struct work_record *work;
313 struct work_record *nextwork;
315 for (work = subrec->workgrouplist; work; work = nextwork) {
316 nextwork = work->next;
317 expire_servers(work, t);
319 if ((work->serverlist == NULL) && (work->death_time != PERMANENT_TTL) &&
320 ((t == (time_t)-1) || (work->death_time < t))) {
321 DEBUG(3,("expire_workgroups_and_servers: Removing timed out workgroup %s\n",
322 work->work_group));
323 remove_workgroup_from_subnet(subrec, work);