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