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
9 Copyright (C) John H Terpstra 1995-1998
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "../librpc/gen_ndr/svcctl.h"
28 #include "nmbd/nmbd.h"
30 extern int updatecount
;
31 extern bool found_lm_clients
;
33 /****************************************************************************
34 Send a browser reset packet.
35 **************************************************************************/
37 void send_browser_reset(int reset_type
, const char *to_name
, int to_type
, struct in_addr to_ip
)
42 DEBUG(3,("send_browser_reset: sending reset request type %d to %s<%02x> IP %s.\n",
43 reset_type
, to_name
, to_type
, inet_ntoa(to_ip
) ));
45 memset(outbuf
,'\0',sizeof(outbuf
));
47 SCVAL(p
,0,ANN_ResetBrowserState
);
49 SCVAL(p
,0,reset_type
);
52 send_mailslot(True
, BROWSE_MAILSLOT
, outbuf
,PTR_DIFF(p
,outbuf
),
53 global_myname(), 0x0, to_name
, to_type
, to_ip
,
54 FIRST_SUBNET
->myip
, DGRAM_PORT
);
57 /****************************************************************************
58 Broadcast a packet to the local net requesting that all servers in this
59 workgroup announce themselves to us.
60 **************************************************************************/
62 void broadcast_announce_request(struct subnet_record
*subrec
, struct work_record
*work
)
67 work
->needannounce
= True
;
69 DEBUG(3,("broadcast_announce_request: sending announce request for workgroup %s \
70 to subnet %s\n", work
->work_group
, subrec
->subnet_name
));
72 memset(outbuf
,'\0',sizeof(outbuf
));
74 SCVAL(p
,0,ANN_AnnouncementRequest
);
77 SCVAL(p
,0,work
->token
); /* (local) Unique workgroup token id. */
79 p
+= push_string_check(p
+1, global_myname(), 15, STR_ASCII
|STR_UPPER
|STR_TERMINATE
);
81 send_mailslot(False
, BROWSE_MAILSLOT
, outbuf
,PTR_DIFF(p
,outbuf
),
82 global_myname(), 0x0, work
->work_group
,0x1e, subrec
->bcast_ip
,
83 subrec
->myip
, DGRAM_PORT
);
86 /****************************************************************************
87 Broadcast an announcement.
88 **************************************************************************/
90 static void send_announcement(struct subnet_record
*subrec
, int announce_type
,
91 const char *from_name
, const char *to_name
, int to_type
, struct in_addr to_ip
,
92 time_t announce_interval
,
93 const char *server_name
, int server_type
, const char *server_comment
)
96 unstring upper_server_name
;
99 memset(outbuf
,'\0',sizeof(outbuf
));
102 SCVAL(outbuf
,0,announce_type
);
104 /* Announcement parameters. */
105 SCVAL(p
,0,updatecount
);
106 SIVAL(p
,1,announce_interval
*1000); /* Milliseconds - despite the spec. */
108 safe_strcpy(upper_server_name
, server_name
, sizeof(upper_server_name
)-1);
109 strupper_m(upper_server_name
);
110 push_string_check(p
+5, upper_server_name
, 16, STR_ASCII
|STR_TERMINATE
);
112 SCVAL(p
,21,lp_major_announce_version()); /* Major version. */
113 SCVAL(p
,22,lp_minor_announce_version()); /* Minor version. */
115 SIVAL(p
,23,server_type
& ~SV_TYPE_LOCAL_LIST_ONLY
);
116 /* Browse version: got from NT/AS 4.00 - Value defined in smb.h (JHT). */
117 SSVAL(p
,27,BROWSER_ELECTION_VERSION
);
118 SSVAL(p
,29,BROWSER_CONSTANT
); /* Browse signature. */
120 p
+= 31 + push_string_check(p
+31, server_comment
, sizeof(outbuf
) - (p
+ 31 - outbuf
), STR_ASCII
|STR_TERMINATE
);
122 send_mailslot(False
,BROWSE_MAILSLOT
, outbuf
, PTR_DIFF(p
,outbuf
),
123 from_name
, 0x0, to_name
, to_type
, to_ip
, subrec
->myip
,
127 /****************************************************************************
128 Broadcast a LanMan announcement.
129 **************************************************************************/
131 static void send_lm_announcement(struct subnet_record
*subrec
, int announce_type
,
132 char *from_name
, char *to_name
, int to_type
, struct in_addr to_ip
,
133 time_t announce_interval
,
134 char *server_name
, int server_type
, char *server_comment
)
139 memset(outbuf
,'\0',sizeof(outbuf
));
141 SSVAL(p
,0,announce_type
);
142 SIVAL(p
,2,server_type
& ~SV_TYPE_LOCAL_LIST_ONLY
);
143 SCVAL(p
,6,lp_major_announce_version()); /* Major version. */
144 SCVAL(p
,7,lp_minor_announce_version()); /* Minor version. */
145 SSVAL(p
,8,announce_interval
); /* In seconds - according to spec. */
148 p
+= push_string_check(p
, server_name
, 15, STR_ASCII
|STR_UPPER
|STR_TERMINATE
);
149 p
+= push_string_check(p
, server_comment
, sizeof(outbuf
)- (p
- outbuf
), STR_ASCII
|STR_UPPER
|STR_TERMINATE
);
151 send_mailslot(False
,LANMAN_MAILSLOT
, outbuf
, PTR_DIFF(p
,outbuf
),
152 from_name
, 0x0, to_name
, to_type
, to_ip
, subrec
->myip
,
156 /****************************************************************************
157 We are a local master browser. Announce this to WORKGROUP<1e>.
158 ****************************************************************************/
160 static void send_local_master_announcement(struct subnet_record
*subrec
, struct work_record
*work
,
161 struct server_record
*servrec
)
163 /* Ensure we don't have the prohibited bit set. */
164 uint32 type
= servrec
->serv
.type
& ~SV_TYPE_LOCAL_LIST_ONLY
;
166 DEBUG(3,("send_local_master_announcement: type %x for name %s on subnet %s for workgroup %s\n",
167 type
, global_myname(), subrec
->subnet_name
, work
->work_group
));
169 send_announcement(subrec
, ANN_LocalMasterAnnouncement
,
170 global_myname(), /* From nbt name. */
171 work
->work_group
, 0x1e, /* To nbt name. */
172 subrec
->bcast_ip
, /* To ip. */
173 work
->announce_interval
, /* Time until next announce. */
174 global_myname(), /* Name to announce. */
175 type
, /* Type field. */
176 servrec
->serv
.comment
);
179 /****************************************************************************
180 Announce the workgroup WORKGROUP to MSBROWSE<01>.
181 ****************************************************************************/
183 static void send_workgroup_announcement(struct subnet_record
*subrec
, struct work_record
*work
)
185 DEBUG(3,("send_workgroup_announcement: on subnet %s for workgroup %s\n",
186 subrec
->subnet_name
, work
->work_group
));
188 send_announcement(subrec
, ANN_DomainAnnouncement
,
189 global_myname(), /* From nbt name. */
190 MSBROWSE
, 0x1, /* To nbt name. */
191 subrec
->bcast_ip
, /* To ip. */
192 work
->announce_interval
, /* Time until next announce. */
193 work
->work_group
, /* Name to announce. */
194 SV_TYPE_DOMAIN_ENUM
|SV_TYPE_NT
, /* workgroup announce flags. */
195 global_myname()); /* From name as comment. */
198 /****************************************************************************
199 Announce the given host to WORKGROUP<1d>.
200 ****************************************************************************/
202 static void send_host_announcement(struct subnet_record
*subrec
, struct work_record
*work
,
203 struct server_record
*servrec
)
205 /* Ensure we don't have the prohibited bits set. */
206 uint32 type
= servrec
->serv
.type
& ~SV_TYPE_LOCAL_LIST_ONLY
;
208 DEBUG(3,("send_host_announcement: type %x for host %s on subnet %s for workgroup %s\n",
209 type
, servrec
->serv
.name
, subrec
->subnet_name
, work
->work_group
));
211 send_announcement(subrec
, ANN_HostAnnouncement
,
212 servrec
->serv
.name
, /* From nbt name. */
213 work
->work_group
, 0x1d, /* To nbt name. */
214 subrec
->bcast_ip
, /* To ip. */
215 work
->announce_interval
, /* Time until next announce. */
216 servrec
->serv
.name
, /* Name to announce. */
217 type
, /* Type field. */
218 servrec
->serv
.comment
);
221 /****************************************************************************
222 Announce the given LanMan host
223 ****************************************************************************/
225 static void send_lm_host_announcement(struct subnet_record
*subrec
, struct work_record
*work
,
226 struct server_record
*servrec
, int lm_interval
)
228 /* Ensure we don't have the prohibited bits set. */
229 uint32 type
= servrec
->serv
.type
& ~SV_TYPE_LOCAL_LIST_ONLY
;
231 DEBUG(3,("send_lm_host_announcement: type %x for host %s on subnet %s for workgroup %s, ttl: %d\n",
232 type
, servrec
->serv
.name
, subrec
->subnet_name
, work
->work_group
, lm_interval
));
234 send_lm_announcement(subrec
, ANN_HostAnnouncement
,
235 servrec
->serv
.name
, /* From nbt name. */
236 work
->work_group
, 0x00, /* To nbt name. */
237 subrec
->bcast_ip
, /* To ip. */
238 lm_interval
, /* Time until next announce. */
239 servrec
->serv
.name
, /* Name to announce (fstring not netbios name struct). */
240 type
, /* Type field. */
241 servrec
->serv
.comment
);
244 /****************************************************************************
245 Announce a server record.
246 ****************************************************************************/
248 static void announce_server(struct subnet_record
*subrec
, struct work_record
*work
,
249 struct server_record
*servrec
)
251 /* Only do domain announcements if we are a master and it's
252 our primary name we're being asked to announce. */
254 if (AM_LOCAL_MASTER_BROWSER(work
) && strequal(global_myname(),servrec
->serv
.name
)) {
255 send_local_master_announcement(subrec
, work
, servrec
);
256 send_workgroup_announcement(subrec
, work
);
258 send_host_announcement(subrec
, work
, servrec
);
262 /****************************************************************************
263 Go through all my registered names on all broadcast subnets and announce
264 them if the timeout requires it.
265 **************************************************************************/
267 void announce_my_server_names(time_t t
)
269 struct subnet_record
*subrec
;
271 for (subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_EXCLUDING_UNICAST(subrec
)) {
272 struct work_record
*work
= find_workgroup_on_subnet(subrec
, lp_workgroup());
275 struct server_record
*servrec
;
277 if (work
->needannounce
) {
278 /* Drop back to a max 3 minute announce. This is to prevent a
279 single lost packet from breaking things for too long. */
281 work
->announce_interval
= MIN(work
->announce_interval
,
282 CHECK_TIME_MIN_HOST_ANNCE
*60);
283 work
->lastannounce_time
= t
- (work
->announce_interval
+1);
284 work
->needannounce
= False
;
287 /* Announce every minute at first then progress to every 12 mins */
288 if ((t
- work
->lastannounce_time
) < work
->announce_interval
)
291 if (work
->announce_interval
< (CHECK_TIME_MAX_HOST_ANNCE
* 60))
292 work
->announce_interval
+= 60;
294 work
->lastannounce_time
= t
;
296 for (servrec
= work
->serverlist
; servrec
; servrec
= servrec
->next
) {
297 if (is_myname(servrec
->serv
.name
))
298 announce_server(subrec
, work
, servrec
);
304 /****************************************************************************
305 Go through all my registered names on all broadcast subnets and announce
306 them as a LanMan server if the timeout requires it.
307 **************************************************************************/
309 void announce_my_lm_server_names(time_t t
)
311 struct subnet_record
*subrec
;
312 static time_t last_lm_announce_time
=0;
313 int announce_interval
= lp_lm_interval();
314 int lm_announce
= lp_lm_announce();
316 if ((announce_interval
<= 0) || (lm_announce
<= 0)) {
317 /* user absolutely does not want LM announcements to be sent. */
321 if ((lm_announce
>= 2) && (!found_lm_clients
)) {
322 /* has been set to 2 (Auto) but no LM clients detected (yet). */
326 /* Otherwise: must have been set to 1 (Yes), or LM clients *have*
329 for (subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_EXCLUDING_UNICAST(subrec
)) {
330 struct work_record
*work
= find_workgroup_on_subnet(subrec
, lp_workgroup());
333 struct server_record
*servrec
;
335 if (last_lm_announce_time
&& ((t
- last_lm_announce_time
) < announce_interval
))
338 last_lm_announce_time
= t
;
340 for (servrec
= work
->serverlist
; servrec
; servrec
= servrec
->next
) {
341 if (is_myname(servrec
->serv
.name
))
342 /* skipping equivalent of announce_server() */
343 send_lm_host_announcement(subrec
, work
, servrec
, announce_interval
);
349 /* Announce timer. Moved into global static so it can be reset
350 when a machine becomes a local master browser. */
351 static time_t announce_timer_last
=0;
353 /****************************************************************************
354 Reset the announce_timer so that a local master browser announce will be done
356 ****************************************************************************/
358 void reset_announce_timer(void)
360 announce_timer_last
= time(NULL
) - (CHECK_TIME_MST_ANNOUNCE
* 60);
363 /****************************************************************************
364 Announce myself as a local master browser to a domain master browser.
365 **************************************************************************/
367 void announce_myself_to_domain_master_browser(time_t t
)
369 struct subnet_record
*subrec
;
370 struct work_record
*work
;
372 if(!we_are_a_wins_client()) {
373 DEBUG(10,("announce_myself_to_domain_master_browser: no unicast subnet, ignoring.\n"));
377 if (!announce_timer_last
)
378 announce_timer_last
= t
;
380 if ((t
-announce_timer_last
) < (CHECK_TIME_MST_ANNOUNCE
* 60)) {
381 DEBUG(10,("announce_myself_to_domain_master_browser: t (%d) - last(%d) < %d\n",
382 (int)t
, (int)announce_timer_last
,
383 CHECK_TIME_MST_ANNOUNCE
* 60 ));
387 announce_timer_last
= t
;
389 /* Look over all our broadcast subnets to see if any of them
390 has the state set as local master browser. */
392 for (subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_EXCLUDING_UNICAST(subrec
)) {
393 for (work
= subrec
->workgrouplist
; work
; work
= work
->next
) {
394 if (AM_LOCAL_MASTER_BROWSER(work
)) {
395 DEBUG(4,( "announce_myself_to_domain_master_browser: I am a local master browser for \
396 workgroup %s on subnet %s\n", work
->work_group
, subrec
->subnet_name
));
398 /* Look in nmbd_browsersync.c for the rest of this code. */
399 announce_and_sync_with_domain_master_browser(subrec
, work
);
405 /****************************************************************************
406 Announce all samba's server entries as 'gone'.
407 This must *only* be called on shutdown.
408 ****************************************************************************/
410 void announce_my_servers_removed(void)
412 int announce_interval
= lp_lm_interval();
413 int lm_announce
= lp_lm_announce();
414 struct subnet_record
*subrec
;
416 for (subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_EXCLUDING_UNICAST(subrec
)) {
417 struct work_record
*work
;
418 for (work
= subrec
->workgrouplist
; work
; work
= work
->next
) {
419 struct server_record
*servrec
;
421 work
->announce_interval
= 0;
422 for (servrec
= work
->serverlist
; servrec
; servrec
= servrec
->next
) {
423 if (!is_myname(servrec
->serv
.name
))
425 servrec
->serv
.type
= 0;
426 if(AM_LOCAL_MASTER_BROWSER(work
))
427 send_local_master_announcement(subrec
, work
, servrec
);
428 send_host_announcement(subrec
, work
, servrec
);
430 if ((announce_interval
<= 0) || (lm_announce
<= 0)) {
431 /* user absolutely does not want LM announcements to be sent. */
435 if ((lm_announce
>= 2) && (!found_lm_clients
)) {
436 /* has been set to 2 (Auto) but no LM clients detected (yet). */
441 * lm announce was set or we have seen lm announcements, so do
442 * a lm announcement of host removed.
445 send_lm_host_announcement(subrec
, work
, servrec
, 0);
451 /****************************************************************************
452 Do all the "remote" announcements. These are used to put ourselves
453 on a remote browse list. They are done blind, no checking is done to
454 see if there is actually a local master browser at the other end.
455 **************************************************************************/
457 void announce_remote(time_t t
)
461 static time_t last_time
= 0;
465 int stype
= lp_default_server_announce();
466 TALLOC_CTX
*frame
= NULL
;
468 if (last_time
&& (t
< (last_time
+ REMOTE_ANNOUNCE_INTERVAL
)))
473 s
= lp_remote_announce();
477 comment
= string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH
);
479 frame
= talloc_stackframe();
480 for (ptr
=s
; next_token_talloc(frame
,&ptr
,&s2
,NULL
); ) {
481 /* The entries are of the form a.b.c.d/WORKGROUP with
482 WORKGROUP being optional */
487 pwgroup
= strchr_m(s2
,'/');
490 if (!pwgroup
|| !*pwgroup
)
491 wgroup
= lp_workgroup();
495 addr
= interpret_addr2(s2
);
497 /* Announce all our names including aliases */
498 /* Give the ip address as the address of our first
501 for(i
=0; my_netbios_names(i
); i
++) {
502 const char *name
= my_netbios_names(i
);
504 DEBUG(5,("announce_remote: Doing remote announce for server %s to IP %s.\n",
505 name
, inet_ntoa(addr
) ));
507 send_announcement(FIRST_SUBNET
, ANN_HostAnnouncement
,
508 name
, /* From nbt name. */
509 wgroup
, 0x1d, /* To nbt name. */
511 REMOTE_ANNOUNCE_INTERVAL
, /* Time until next announce. */
512 name
, /* Name to announce. */
513 stype
, /* Type field. */
520 /****************************************************************************
521 Implement the 'remote browse sync' feature Andrew added.
522 These are used to put our browse lists into remote browse lists.
523 **************************************************************************/
525 void browse_sync_remote(time_t t
)
529 static time_t last_time
= 0;
532 struct work_record
*work
;
536 TALLOC_CTX
*frame
= NULL
;
538 if (last_time
&& (t
< (last_time
+ REMOTE_ANNOUNCE_INTERVAL
)))
543 s
= lp_remote_browse_sync();
548 * We only do this if we are the local master browser
549 * for our workgroup on the firsst subnet.
552 if((work
= find_workgroup_on_subnet(FIRST_SUBNET
, lp_workgroup())) == NULL
) {
553 DEBUG(0,("browse_sync_remote: Cannot find workgroup %s on subnet %s\n",
554 lp_workgroup(), FIRST_SUBNET
->subnet_name
));
558 if(!AM_LOCAL_MASTER_BROWSER(work
)) {
559 DEBUG(5,("browse_sync_remote: We can only do this if we are a local master browser \
560 for workgroup %s on subnet %s.\n", lp_workgroup(), FIRST_SUBNET
->subnet_name
));
564 memset(outbuf
,'\0',sizeof(outbuf
));
566 SCVAL(p
,0,ANN_MasterAnnouncement
);
569 unstrcpy(myname
, global_myname());
572 push_ascii(p
, myname
, sizeof(outbuf
)-PTR_DIFF(p
,outbuf
)-1, STR_TERMINATE
);
574 p
= skip_string(outbuf
,sizeof(outbuf
),p
);
576 frame
= talloc_stackframe();
577 for (ptr
=s
; next_token_talloc(frame
,&ptr
,&s2
,NULL
); ) {
578 /* The entries are of the form a.b.c.d */
579 addr
= interpret_addr2(s2
);
581 DEBUG(5,("announce_remote: Doing remote browse sync announce for server %s to IP %s.\n",
582 global_myname(), inet_ntoa(addr
) ));
584 send_mailslot(True
, BROWSE_MAILSLOT
, outbuf
,PTR_DIFF(p
,outbuf
),
585 global_myname(), 0x0, "*", 0x0, addr
, FIRST_SUBNET
->myip
, DGRAM_PORT
);