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 lp_netbios_name(), 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, lp_netbios_name(), 15, STR_ASCII
|STR_UPPER
|STR_TERMINATE
);
81 send_mailslot(False
, BROWSE_MAILSLOT
, outbuf
,PTR_DIFF(p
,outbuf
),
82 lp_netbios_name(), 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 strlcpy(upper_server_name
, server_name
? server_name
: "", sizeof(upper_server_name
));
109 strupper_m(upper_server_name
);
110 push_string_check(p
+5, upper_server_name
, 16, STR_ASCII
|STR_TERMINATE
);
112 SCVAL(p
,21,SAMBA_MAJOR_NBT_ANNOUNCE_VERSION
); /* Major version. */
113 SCVAL(p
,22,SAMBA_MINOR_NBT_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,SAMBA_MAJOR_NBT_ANNOUNCE_VERSION
); /* Major version. */
144 SCVAL(p
,7,SAMBA_MINOR_NBT_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
, lp_netbios_name(), subrec
->subnet_name
, work
->work_group
));
169 send_announcement(subrec
, ANN_LocalMasterAnnouncement
,
170 lp_netbios_name(), /* 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 lp_netbios_name(), /* 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 lp_netbios_name(), /* 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 lp_netbios_name()); /* 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(lp_netbios_name(),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(talloc_tos());
477 comment
= string_truncate(lp_serverstring(talloc_tos()),
478 MAX_SERVER_STRING_LENGTH
);
480 frame
= talloc_stackframe();
481 for (ptr
=s
; next_token_talloc(frame
,&ptr
,&s2
,NULL
); ) {
482 /* The entries are of the form a.b.c.d/WORKGROUP with
483 WORKGROUP being optional */
488 pwgroup
= strchr_m(s2
,'/');
491 if (!pwgroup
|| !*pwgroup
)
492 wgroup
= lp_workgroup();
496 addr
= interpret_addr2(s2
);
498 /* Announce all our names including aliases */
499 /* Give the ip address as the address of our first
502 for(i
=0; my_netbios_names(i
); i
++) {
503 const char *name
= my_netbios_names(i
);
505 DEBUG(5,("announce_remote: Doing remote announce for server %s to IP %s.\n",
506 name
, inet_ntoa(addr
) ));
508 send_announcement(FIRST_SUBNET
, ANN_HostAnnouncement
,
509 name
, /* From nbt name. */
510 wgroup
, 0x1d, /* To nbt name. */
512 REMOTE_ANNOUNCE_INTERVAL
, /* Time until next announce. */
513 name
, /* Name to announce. */
514 stype
, /* Type field. */
521 /****************************************************************************
522 Implement the 'remote browse sync' feature Andrew added.
523 These are used to put our browse lists into remote browse lists.
524 **************************************************************************/
526 void browse_sync_remote(time_t t
)
530 static time_t last_time
= 0;
533 struct work_record
*work
;
537 TALLOC_CTX
*frame
= NULL
;
539 if (last_time
&& (t
< (last_time
+ REMOTE_ANNOUNCE_INTERVAL
)))
544 s
= lp_remote_browse_sync(talloc_tos());
549 * We only do this if we are the local master browser
550 * for our workgroup on the firsst subnet.
553 if((work
= find_workgroup_on_subnet(FIRST_SUBNET
, lp_workgroup())) == NULL
) {
554 DEBUG(0,("browse_sync_remote: Cannot find workgroup %s on subnet %s\n",
555 lp_workgroup(), FIRST_SUBNET
->subnet_name
));
559 if(!AM_LOCAL_MASTER_BROWSER(work
)) {
560 DEBUG(5,("browse_sync_remote: We can only do this if we are a local master browser \
561 for workgroup %s on subnet %s.\n", lp_workgroup(), FIRST_SUBNET
->subnet_name
));
565 memset(outbuf
,'\0',sizeof(outbuf
));
567 SCVAL(p
,0,ANN_MasterAnnouncement
);
570 unstrcpy(myname
, lp_netbios_name());
573 push_ascii(p
, myname
, sizeof(outbuf
)-PTR_DIFF(p
,outbuf
)-1, STR_TERMINATE
);
575 p
= skip_string(outbuf
,sizeof(outbuf
),p
);
577 frame
= talloc_stackframe();
578 for (ptr
=s
; next_token_talloc(frame
,&ptr
,&s2
,NULL
); ) {
579 /* The entries are of the form a.b.c.d */
580 addr
= interpret_addr2(s2
);
582 DEBUG(5,("announce_remote: Doing remote browse sync announce for server %s to IP %s.\n",
583 lp_netbios_name(), inet_ntoa(addr
) ));
585 send_mailslot(True
, BROWSE_MAILSLOT
, outbuf
,PTR_DIFF(p
,outbuf
),
586 lp_netbios_name(), 0x0, "*", 0x0, addr
, FIRST_SUBNET
->myip
, DGRAM_PORT
);