summaryrefslogtreecommitdiff
path: root/source3/nameannounce.c
blob: 14c16cadc1c8d565743cc2c8ed2bf0db51bbd4d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/* 
   Unix SMB/Netbios implementation.
   Version 1.9.
   NBT netbios routines and daemon - version 2
   Copyright (C) Andrew Tridgell 1994-1995
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   
   Revision History:

   14 jan 96: lkcl@pires.co.uk
   added multiple workgroup domain master support

*/

#include "includes.h"

#define TEST_CODE

extern int DEBUGLEVEL;
extern BOOL CanRecurse;

extern struct in_addr ipzero;

extern pstring myname;

extern int ClientDGRAM;
extern int ClientNMB;

/* this is our domain/workgroup/server database */
extern struct subnet_record *subnetlist;

/* machine comment for host announcements */
extern  pstring ServerComment;

extern int  updatecount;
extern int  workgroup_count;

/* what server type are we currently */

/****************************************************************************
  send a announce request to the local net
  **************************************************************************/
void announce_request(struct work_record *work, struct in_addr ip)
{
  pstring outbuf;
  char *p;

  if (!work) return;

  work->needannounce = True;

  DEBUG(2,("sending announce request to %s for workgroup %s\n",
	   inet_ntoa(ip),work->work_group));

  bzero(outbuf,sizeof(outbuf));
  p = outbuf;
  CVAL(p,0) = ANN_AnnouncementRequest;
  p++;

  CVAL(p,0) = work->token; /* flags?? XXXX probably a token*/
  p++;
  StrnCpy(p,myname,16);
  strupper(p);
  p = skip_string(p,1);
  
  send_mailslot_reply(BROWSE_MAILSLOT,ClientDGRAM,outbuf,PTR_DIFF(p,outbuf),
		      myname,work->work_group,0x20,0x1e,ip,*iface_ip(ip));
}


/****************************************************************************
  request an announcement
  **************************************************************************/
void do_announce_request(char *info, char *to_name, int announce_type, 
			 int from,
			 int to, struct in_addr dest_ip)
{
  pstring outbuf;
  char *p;
  
  bzero(outbuf,sizeof(outbuf));
  p = outbuf;
  CVAL(p,0) = announce_type; 
  p++;
  
  DEBUG(2,("sending announce type %d: info %s to %s - server %s(%x)\n",
	   announce_type, info, inet_ntoa(dest_ip),to_name,to));
  
  StrnCpy(p,info,16);
  strupper(p);
  p = skip_string(p,1);
  
  send_mailslot_reply(BROWSE_MAILSLOT,ClientDGRAM,outbuf,PTR_DIFF(p,outbuf),
		      myname,to_name,from,to,dest_ip,*iface_ip(dest_ip));
}


/****************************************************************************
  find a server responsible for a workgroup, and sync browse lists
  control ends up back here via response_name_query.
  **************************************************************************/
void sync_server(enum state_type state, char *serv_name, char *work_name, 
		 int name_type,
		 struct in_addr ip)
{                     
  add_browser_entry(serv_name, name_type, work_name, 0, ip);

  if (state == NAME_QUERY_MST_SRV_CHK)
    {
      /* announce ourselves as a master browser to serv_name */
      do_announce_request(myname, serv_name, ANN_MasterAnnouncement,
			  0x20, 0, ip);
    }
}


/****************************************************************************
  construct a host announcement unicast
  **************************************************************************/
void announce_backup(void)
{
  static time_t lastrun = 0;
  time_t t = time(NULL);
  pstring outbuf;
  char *p;
  struct subnet_record *d1;
  int tok;
  
  if (!lastrun) lastrun = t;
  if (t < lastrun + CHECK_TIME_ANNOUNCE_BACKUP * 60)
	return;
  lastrun = t;
  
  for (tok = 0; tok <= workgroup_count; tok++)
    {
      for (d1 = subnetlist; d1; d1 = d1->next)
	{
	  struct work_record *work;
	  struct subnet_record *d;
	  
	  /* search for unique workgroup: only the name matters */
	  for (work = d1->workgrouplist;
	       work && (tok != work->token);
	       work = work->next);
	  
	  if (!work) continue;

	  /* found one: announce it across all domains */
	  for (d = subnetlist; d; d = d->next)
	    {
	      int type=0;

	      if (AM_DOMCTL(work)) {
		type = 0x1b;
	      } else if (AM_MASTER(work)) {
		type = 0x1d;
	      } else {
		continue;
	      }
	      
	      DEBUG(2,("sending announce backup %s workgroup %s(%d)\n",
		       inet_ntoa(d->bcast_ip),work->work_group,
		       work->token));
	      
	      bzero(outbuf,sizeof(outbuf));
	      p = outbuf;
	      CVAL(p,0) = ANN_GetBackupListReq;
	      p++;
	      
	      CVAL(p,0) = 1; /* count? */
	      SIVAL(p,1,work->token); /* workgroup unique key index */
	      p += 5;
	      p++;
	      
	      send_mailslot_reply(BROWSE_MAILSLOT,
				  ClientDGRAM,outbuf,
				  PTR_DIFF(p,outbuf),
				  myname, work->work_group,
				  0x0,type,d->bcast_ip,
				  *iface_ip(d->bcast_ip));
	    }
	}
    }
}


/****************************************************************************
  send a host announcement packet
  **************************************************************************/
static void do_announce_host(int command,
		char *from_name, int from_type, struct in_addr from_ip,
		char *to_name  , int to_type  , struct in_addr to_ip,
		time_t announce_interval,
		char *server_name, int server_type, char *server_comment)
{
	pstring outbuf;
	char *p;

	bzero(outbuf,sizeof(outbuf));
	p = outbuf+1;

	/* command type */
	CVAL(outbuf,0) = command;

	/* announcement parameters */
	CVAL(p,0) = updatecount;
	SIVAL(p,1,announce_interval*1000); /* ms - despite the spec */

	StrnCpy(p+5,server_name,16);
	strupper(p+5);

	CVAL(p,21) = 2; /* major version */
	CVAL(p,22) = 2; /* minor version */

	SIVAL(p,23,server_type);
	SSVAL(p,27,0xaa55); /* browse signature */
	SSVAL(p,29,1); /* browse version */

	strcpy(p+31,server_comment);
	p += 31;
	p = skip_string(p,1);

	/* send the announcement */
	send_mailslot_reply(BROWSE_MAILSLOT,ClientDGRAM,outbuf,
					  PTR_DIFF(p,outbuf),
					  from_name, to_name,
					  from_type, to_type,
					  to_ip, from_ip);
}


/****************************************************************************
  announce a server entry
  ****************************************************************************/
void announce_server(struct subnet_record *d, struct work_record *work,
					char *name, char *comment, time_t ttl, int server_type)
{
	if (AM_MASTER(work))
	{
		DEBUG(3,("sending local master announce to %s for %s(1e)\n",
						inet_ntoa(d->bcast_ip),work->work_group));

		do_announce_host(ANN_LocalMasterAnnouncement,
						name            , 0x00, d->myip,
						work->work_group, 0x1e, d->bcast_ip,
						ttl*1000,
						name, server_type, comment);

		DEBUG(3,("sending domain announce to %s for %s\n",
						inet_ntoa(d->bcast_ip),work->work_group));

		/* XXXX should we do a domain-announce-kill? */
		if (server_type != 0)
		{
			do_announce_host(ANN_DomainAnnouncement,
						work->work_group, 0x00, d->myip,
						MSBROWSE        , 0x01, d->bcast_ip,
						ttl*1000,
						name, server_type ? SV_TYPE_DOMAIN_ENUM : 0, comment);
		}
	}
	else
	{
		DEBUG(3,("sending host announce to %s for %s(1d)\n",
						inet_ntoa(d->bcast_ip),work->work_group));

		do_announce_host(ANN_HostAnnouncement,
						name            , 0x00, d->myip,
						work->work_group, 0x1d, d->bcast_ip,
						ttl*1000,
						name, server_type, comment);
	}
}

/****************************************************************************
  construct a host announcement unicast
  **************************************************************************/
void announce_host(void)
{
  time_t t = time(NULL);
  struct subnet_record *d;
  pstring comment;
  char *my_name;

  StrnCpy(comment, *ServerComment ? ServerComment : "NoComment", 43);

  my_name = *myname ? myname : "NoName";

  for (d = subnetlist; d; d = d->next)
    {
      struct work_record *work;
      
      for (work = d->workgrouplist; work; work = work->next)
	{
	  uint32 stype = work->ServerType;
	  struct server_record *s;
	  BOOL announce = False;
	  
	  if (work->needannounce) {
	    /* drop back to a max 3 minute announce - this is to prevent a
	       single lost packet from stuffing things up for too long */
	    work->announce_interval = MIN(work->announce_interval,
						CHECK_TIME_MIN_HOST_ANNCE*60);
	    work->lastannounce_time = t - (work->announce_interval+1);
	  }
	  
	  /* announce every minute at first then progress to every 12 mins */
	  if (work->lastannounce_time && 
	      (t - work->lastannounce_time) < work->announce_interval)
	    continue;
	  
	  if (work->announce_interval < CHECK_TIME_MAX_HOST_ANNCE * 60) 
	    work->announce_interval += 60;
	  
	  work->lastannounce_time = t;

	  if (!d->my_interface) {
	    stype &= ~(SV_TYPE_POTENTIAL_BROWSER | SV_TYPE_MASTER_BROWSER |
		       SV_TYPE_DOMAIN_MASTER | SV_TYPE_BACKUP_BROWSER |
		       SV_TYPE_DOMAIN_CTRL | SV_TYPE_DOMAIN_MEMBER);
	  }

	  for (s = work->serverlist; s; s = s->next) {
	    if (strequal(myname, s->serv.name)) { 
	      announce = True; 
	      break; 
	    }
	  }
	  
	  if (announce)
	  {
		announce_server(d,work,my_name,comment,work->announce_interval,stype);
	  }
	  
	  if (work->needannounce)
	  {
	      work->needannounce = False;
	      break;
	      /* sorry: can't do too many announces. do some more later */
	  }
	}
  }
}


/****************************************************************************
  announce myself as a master to all other primary domain conrollers.

  BIG NOTE: this code will remain untested until some kind soul that has access
  to a couple of windows NT advanced servers runs this version of nmbd for at
  least 15 minutes.
  
  this actually gets done in search_and_sync_workgroups() via the
  NAME_QUERY_MST_SRV_CHK command, if there is a response from the
  name query initiated here.  see response_name_query()
  **************************************************************************/
void announce_master(void)
{
  struct subnet_record *d;
  static time_t last=0;
  time_t t = time(NULL);
  BOOL am_master = False; /* are we a master of some sort? :-) */

  if (last && (t-last < CHECK_TIME_MST_ANNOUNCE * 60))
	return;

  last = t;

  for (d = subnetlist; d; d = d->next)
    {
      struct work_record *work;
      for (work = d->workgrouplist; work; work = work->next)
	{
	  if (AM_MASTER(work))
	    {
	      am_master = True;
	    }
	}
    }
  
  if (!am_master) return; /* only proceed if we are a master browser */
  
  for (d = subnetlist; d; d = d->next)
    {
      struct work_record *work;
      for (work = d->workgrouplist; work; work = work->next)
	{
	  struct server_record *s;
	  for (s = work->serverlist; s; s = s->next)
	    {
	      if (strequal(s->serv.name, myname)) continue;
	      
	      /* all PDCs (which should also be master browsers) */
	      if (s->serv.type & SV_TYPE_DOMAIN_CTRL)
		{
		  /* check the existence of a pdc for this workgroup, and if
		     one exists at the specified ip, sync with it and announce
		     ourselves as a master browser to it */
		  
		  if (!*lp_domain_controller() ||
		      !strequal(lp_domain_controller(), s->serv.name))
		    {
		      if (!lp_wins_support() && *lp_wins_server())
			{
			  struct in_addr ip;
			  ip = ipzero;
			  
			  queue_netbios_pkt_wins(d,ClientNMB,NMB_QUERY,
						 NAME_QUERY_MST_SRV_CHK,
						 work->work_group,0x1b,0,0,
						 False, False, ip);
			}
		      else
			{
			  struct subnet_record *d2;
			  for (d2 = subnetlist; d2; d2 = d2->next)
			    {
			      queue_netbios_packet(d,ClientNMB,NMB_QUERY,
						   NAME_QUERY_MST_SRV_CHK,
						   work->work_group,0x1b,0,0,
						   True, False, d2->bcast_ip);
			    }
			}
		    }
		}
	    }
	  
	  /* now do primary domain controller - the one that's not
	     necessarily in our browse lists, although it ought to be
	     this pdc is the one that we get TOLD about through smb.conf.
	     basically, if it's on a subnet that we know about, it may end
	     up in our browse lists (which is why it's explicitly excluded
	     in the code above) */
	  
	  if (*lp_domain_controller())
	    {
	      struct in_addr ip;
	      BOOL bcast = False;
	      
	      ip = *interpret_addr2(lp_domain_controller());
	      
	      if (zero_ip(ip)) {
		ip = d->bcast_ip;
		bcast = True;
	      }

	      DEBUG(2, ("Searching for PDC %s at %s\n",
			lp_domain_controller(), inet_ntoa(ip)));
	      
	      /* check the existence of a pdc for this workgroup, and if
		 one exists at the specified ip, sync with it and announce
		 ourselves as a master browser to it */
	      queue_netbios_pkt_wins(d,ClientNMB, NMB_QUERY,NAME_QUERY_MST_SRV_CHK,
				     work->work_group,0x1b, 0, 0,
				     bcast, False, ip);
	    }
	}
    }
}