2 Unix SMB/Netbios implementation.
4 NBT netbios routines and daemon - version 2
5 Copyright (C) Andrew Tridgell 1994-1998
6 Copyright (C) Luke Kenneth Casson Leighton 1994-1998
7 Copyright (C) Jeremy Allison 1994-1998
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 extern int DEBUGLEVEL;
30 extern char **my_netbios_names;
32 uint16 samba_nb_type = 0; /* samba's NetBIOS name type */
35 /****************************************************************************
36 Set Samba's NetBIOS name type.
37 ****************************************************************************/
39 void set_samba_nb_type(void)
41 if( lp_wins_support() || (*lp_wins_server()) )
42 samba_nb_type = NB_MFLAG; /* samba is a 'hybrid' node type */
44 samba_nb_type = NB_BFLAG; /* samba is broadcast-only node type */
47 /****************************************************************************
48 Returns True if the netbios name is ^1^2__MSBROWSE__^2^1.
50 Note: This name is registered if as a master browser or backup browser
51 you are responsible for a workgroup (when you announce a domain by
52 broadcasting on your local subnet, you announce it as coming from this
53 name: see announce_host()).
55 **************************************************************************/
57 BOOL ms_browser_name( char *name, int type )
59 return( strequal( name, MSBROWSE ) && (type == 0x01) );
62 /****************************************************************************
63 Add a netbios name into a namelist.
64 **************************************************************************/
66 static void add_name_to_namelist(struct subnet_record *subrec,
67 struct name_record *namerec)
69 struct name_record *namerec2;
71 if (!subrec->namelist)
73 subrec->namelist = namerec;
79 for( namerec2 = subrec->namelist; namerec2->next; namerec2 = namerec2->next )
82 namerec2->next = namerec;
84 namerec->prev = namerec2;
85 namerec->subnet = subrec;
87 subrec->namelist_changed = True;
90 /****************************************************************************
91 Remove a name from the namelist.
92 **************************************************************************/
94 void remove_name_from_namelist( struct subnet_record *subrec,
95 struct name_record *namerec )
98 namerec->next->prev = namerec->prev;
100 namerec->prev->next = namerec->next;
102 if(namerec == subrec->namelist)
103 subrec->namelist = namerec->next;
105 if(namerec->data.ip != NULL)
106 free((char *)namerec->data.ip);
107 free((char *)namerec);
109 subrec->namelist_changed = True;
113 /****************************************************************************
114 Find a name in a subnet.
115 **************************************************************************/
117 struct name_record *find_name_on_subnet(struct subnet_record *subrec,
118 struct nmb_name *nmbname, BOOL self_only)
120 struct name_record *namerec = subrec->namelist;
121 struct name_record *name_ret;
123 for (name_ret = namerec; name_ret; name_ret = name_ret->next)
125 if (nmb_name_equal(&name_ret->name, nmbname))
127 /* Self names only - these include permanent names. */
128 if (self_only && (name_ret->data.source != SELF_NAME) &&
129 (name_ret->data.source != PERMANENT_NAME) )
133 DEBUG(9,("find_name_on_subnet: on subnet %s - found name %s source=%d\n",
134 subrec->subnet_name, namestr(nmbname), name_ret->data.source));
138 DEBUG(9,("find_name_on_subnet: on subnet %s - name %s NOT FOUND\n",
139 subrec->subnet_name, namestr(nmbname)));
143 /****************************************************************************
144 Find a name over all known broadcast subnets.
145 **************************************************************************/
148 *find_name_for_remote_broadcast_subnet( struct nmb_name *nmbname,
151 struct subnet_record *subrec;
152 struct name_record *namerec = NULL;
154 for( subrec = FIRST_SUBNET;
156 subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec) )
158 if( NULL != (namerec = find_name_on_subnet(subrec, nmbname, self_only)) )
165 /****************************************************************************
166 Update the ttl of an entry in a subnet name list.
167 ****************************************************************************/
169 void update_name_ttl( struct name_record *namerec, int ttl )
171 time_t time_now = time(NULL);
173 if(namerec->data.death_time != PERMANENT_TTL)
174 namerec->data.death_time = time_now + ttl;
176 namerec->data.refresh_time = time_now + (ttl/2);
178 namerec->subnet->namelist_changed = True;
181 /****************************************************************************
182 Add an entry to a subnet name list.
183 ****************************************************************************/
185 struct name_record *add_name_to_subnet(struct subnet_record *subrec,
186 char *name, int type, uint16 nb_flags, int ttl,
187 enum name_source source, int num_ips, struct in_addr *iplist)
189 struct name_record *namerec;
190 time_t time_now = time(NULL);
192 if((namerec = (struct name_record *)malloc(sizeof(*namerec))) == NULL)
194 DEBUG(0,("add_name_to_subnet: malloc fail.\n"));
198 bzero((char *)namerec,sizeof(*namerec));
200 namerec->subnet = subrec;
202 namerec->data.num_ips = num_ips;
203 namerec->data.ip = (struct in_addr *)malloc( sizeof(struct in_addr)
204 * namerec->data.num_ips );
205 if (!namerec->data.ip)
207 DEBUG(0,("add_name_to_subnet: malloc fail when creating ip_flgs.\n"));
208 free((char *)namerec);
212 bzero( (char *)namerec->data.ip,
213 sizeof(struct in_addr) * namerec->data.num_ips );
215 memcpy( &namerec->data.ip[0], iplist, num_ips * sizeof(struct in_addr) );
217 make_nmb_name( &namerec->name, name, type, scope );
219 /* Setup the death_time and refresh_time. */
220 if(ttl == PERMANENT_TTL)
221 namerec->data.death_time = PERMANENT_TTL;
223 namerec->data.death_time = time_now + ttl;
225 namerec->data.refresh_time = time_now + (ttl/2);
227 /* Enter the name as active. */
228 namerec->data.nb_flags = nb_flags | NB_ACTIVE;
230 /* If it's our primary name, flag it as so. */
231 if(strequal(my_netbios_names[0],name))
232 namerec->data.nb_flags |= NB_PERM;
234 namerec->data.source = source;
236 add_name_to_namelist(subrec,namerec);
238 DEBUG( 3, ( "add_name_to_subnet: Added netbios name %s with first IP %s \
239 ttl=%d nb_flags=%2x to subnet %s\n",
240 namestr(&namerec->name),
243 (unsigned int)nb_flags,
244 subrec->subnet_name) );
246 subrec->namelist_changed = True;
251 /*******************************************************************
252 Utility function automatically called when a name refresh or register
253 succeeds. By definition this is a SELF_NAME (or we wouldn't be registering
255 ******************************************************************/
257 void standard_success_register(struct subnet_record *subrec,
258 struct userdata_struct *userdata,
259 struct nmb_name *nmbname, uint16 nb_flags, int ttl,
260 struct in_addr registered_ip)
262 struct name_record *namerec;
264 namerec = find_name_on_subnet( subrec, nmbname, FIND_SELF_NAME );
265 if( NULL == namerec )
266 add_name_to_subnet( subrec, nmbname->name, nmbname->name_type,
267 nb_flags, ttl, SELF_NAME, 1, ®istered_ip );
269 update_name_ttl( namerec, ttl );
272 /*******************************************************************
273 Utility function automatically called when a name refresh or register
275 ******************************************************************/
277 void standard_fail_register( struct subnet_record *subrec,
278 struct response_record *rrec,
279 struct nmb_name *nmbname )
281 struct name_record *namerec;
283 namerec = find_name_on_subnet( subrec, nmbname, FIND_SELF_NAME );
285 DEBUG( 0, ( "standard_fail_register: Failed to register/refresh name %s \
287 namestr(nmbname), subrec->subnet_name) );
289 /* Remove the name from the subnet. */
291 remove_name_from_namelist(subrec, namerec);
294 /*******************************************************************
295 Utility function to remove an IP address from a name record.
296 ******************************************************************/
298 static void remove_nth_ip_in_record( struct name_record *namerec, int ind)
300 if( ind != namerec->data.num_ips )
301 memmove( (char *)(&namerec->data.ip[ind]),
302 (char *)(&namerec->data.ip[ind+1]),
303 ( namerec->data.num_ips - ind - 1) * sizeof(struct in_addr) );
305 namerec->data.num_ips--;
306 namerec->subnet->namelist_changed = True;
309 /*******************************************************************
310 Utility function to check if an IP address exists in a name record.
311 ******************************************************************/
313 BOOL find_ip_in_name_record( struct name_record *namerec, struct in_addr ip )
317 for(i = 0; i < namerec->data.num_ips; i++)
318 if(ip_equal( namerec->data.ip[i], ip))
324 /*******************************************************************
325 Utility function to add an IP address to a name record.
326 ******************************************************************/
328 void add_ip_to_name_record(struct name_record *namerec, struct in_addr new_ip)
330 struct in_addr *new_list;
332 /* Don't add one we already have. */
333 if(find_ip_in_name_record( namerec, new_ip))
336 new_list = (struct in_addr *)malloc( (namerec->data.num_ips + 1)
337 * sizeof(struct in_addr) );
338 if( NULL == new_list )
340 DEBUG(0,("add_ip_to_name_record: Malloc fail !\n"));
344 memcpy( (char *)new_list,
345 (char *)namerec->data.ip,
346 namerec->data.num_ips * sizeof(struct in_addr) );
347 new_list[namerec->data.num_ips] = new_ip;
349 free((char *)namerec->data.ip);
350 namerec->data.ip = new_list;
351 namerec->data.num_ips += 1;
353 namerec->subnet->namelist_changed = True;
356 /*******************************************************************
357 Utility function to remove an IP address from a name record.
358 ******************************************************************/
360 void remove_ip_from_name_record( struct name_record *namerec,
361 struct in_addr remove_ip )
363 /* Try and find the requested ip address - remove it. */
365 int orig_num = namerec->data.num_ips;
367 for(i = 0; i < orig_num; i++)
368 if( ip_equal( remove_ip, namerec->data.ip[i]) )
370 remove_nth_ip_in_record( namerec, i);
375 /*******************************************************************
376 Utility function that release_name callers can plug into as the
377 success function when a name release is successful. Used to save
378 duplication of success_function code.
379 ******************************************************************/
381 void standard_success_release( struct subnet_record *subrec,
382 struct userdata_struct *userdata,
383 struct nmb_name *nmbname,
384 struct in_addr released_ip )
386 struct name_record *namerec;
388 namerec = find_name_on_subnet( subrec, nmbname, FIND_ANY_NAME );
390 if( namerec == NULL )
392 DEBUG( 0, ( "standard_success_release: Name release for name %s IP %s \
393 on subnet %s. Name was not found on subnet.\n",
395 inet_ntoa(released_ip),
396 subrec->subnet_name) );
401 int orig_num = namerec->data.num_ips;
403 remove_ip_from_name_record( namerec, released_ip );
405 if( namerec->data.num_ips == orig_num )
406 DEBUG( 0, ( "standard_success_release: Name release for name %s IP %s \
407 on subnet %s. This ip is not known for this name.\n",
409 inet_ntoa(released_ip),
410 subrec->subnet_name ) );
413 if( namerec->data.num_ips == 0 )
414 remove_name_from_namelist( subrec, namerec );
417 /*******************************************************************
418 Expires old names in a subnet namelist.
419 ******************************************************************/
421 void expire_names_on_subnet(struct subnet_record *subrec, time_t t)
423 struct name_record *namerec;
424 struct name_record *next_namerec;
426 for (namerec = subrec->namelist; namerec; namerec = next_namerec)
428 next_namerec = namerec->next;
429 if( (namerec->data.death_time != PERMANENT_TTL)
430 && (namerec->data.death_time < t) )
432 if (namerec->data.source == SELF_NAME)
434 DEBUG( 3, ( "expire_names_on_subnet: Subnet %s not expiring SELF \
436 subrec->subnet_name, namestr(&namerec->name) ) );
437 namerec->data.death_time += 300;
438 namerec->subnet->namelist_changed = True;
441 DEBUG(3,("expire_names_on_subnet: Subnet %s - removing expired name %s\n",
442 subrec->subnet_name, namestr(&namerec->name)));
444 remove_name_from_namelist(subrec, namerec);
449 /*******************************************************************
450 Expires old names in all subnet namelists.
451 ******************************************************************/
453 void expire_names(time_t t)
455 struct subnet_record *subrec;
457 for( subrec = FIRST_SUBNET;
459 subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec) )
461 expire_names_on_subnet( subrec, t );
465 /****************************************************************************
466 Add the magic samba names, useful for finding samba servers.
467 These go directly into the name list for a particular subnet,
468 without going through the normal registration process.
469 When adding them to the unicast subnet, add them as a list of
470 all broadcast subnet IP addresses.
471 **************************************************************************/
473 void add_samba_names_to_subnet( struct subnet_record *subrec )
475 struct in_addr *iplist = &subrec->myip;
478 /* These names are added permanently (ttl of zero) and will NOT be
481 if((subrec == unicast_subnet) || (subrec == wins_server_subnet) ||
482 (subrec == remote_broadcast_subnet) )
484 struct subnet_record *bcast_subrecs;
486 /* Create an IP list containing all our known subnets. */
488 num_ips = iface_count();
489 iplist = (struct in_addr *)malloc( num_ips * sizeof(struct in_addr) );
492 DEBUG(0,("add_samba_names_to_subnet: Malloc fail !\n"));
496 for( bcast_subrecs = FIRST_SUBNET, i = 0;
498 bcast_subrecs = NEXT_SUBNET_EXCLUDING_UNICAST(bcast_subrecs), i++ )
499 iplist[i] = bcast_subrecs->myip;
503 add_name_to_subnet(subrec,"*",0x0,samba_nb_type, PERMANENT_TTL,
504 PERMANENT_NAME, num_ips, iplist);
505 add_name_to_subnet(subrec,"*",0x20,samba_nb_type,PERMANENT_TTL,
506 PERMANENT_NAME, num_ips, iplist);
507 add_name_to_subnet(subrec,"__SAMBA__",0x20,samba_nb_type,PERMANENT_TTL,
508 PERMANENT_NAME, num_ips, iplist);
509 add_name_to_subnet(subrec,"__SAMBA__",0x00,samba_nb_type,PERMANENT_TTL,
510 PERMANENT_NAME, num_ips, iplist);
512 if(iplist != &subrec->myip)
513 free((char *)iplist);
516 /****************************************************************************
517 Dump the contents of the namelists on all the subnets (including unicast)
518 into a file. Initiated by SIGHUP - used to debug the state of the namelists.
519 **************************************************************************/
521 static void dump_subnet_namelist( struct subnet_record *subrec, FILE *fp)
523 struct name_record *namerec;
528 fprintf(fp, "Subnet %s\n----------------------\n", subrec->subnet_name);
529 for (namerec = subrec->namelist; namerec; namerec = namerec->next)
531 fprintf(fp,"\tName = %s\t", namestr(&namerec->name));
532 switch(namerec->data.source)
535 src_type = "LMHOSTS_NAME";
537 case WINS_PROXY_NAME:
538 src_type = "WINS_PROXY_NAME";
541 src_type = "REGISTER_NAME";
544 src_type = "SELF_NAME";
547 src_type = "DNS_NAME";
550 src_type = "DNSFAIL_NAME";
553 src_type = "PERMANENT_NAME";
556 src_type = "unknown!";
559 fprintf(fp,"Source = %s\nb_flags = %x\t", src_type, namerec->data.nb_flags);
561 if(namerec->data.death_time != PERMANENT_TTL)
563 tm = LocalTime(&namerec->data.death_time);
564 fprintf(fp, "death_time = %s\t", asctime(tm));
567 fprintf(fp, "death_time = PERMANENT\t");
569 if(namerec->data.refresh_time != PERMANENT_TTL)
571 tm = LocalTime(&namerec->data.refresh_time);
572 fprintf(fp, "refresh_time = %s\n", asctime(tm));
575 fprintf(fp, "refresh_time = PERMANENT\n");
577 fprintf(fp, "\t\tnumber of IPS = %d", namerec->data.num_ips);
578 for(i = 0; i < namerec->data.num_ips; i++)
579 fprintf(fp, "\t%s", inet_ntoa(namerec->data.ip[i]));
585 /****************************************************************************
586 Dump the contents of the namelists on all the subnets (including unicast)
587 into a file. Initiated by SIGHUP - used to debug the state of the namelists.
588 **************************************************************************/
590 void dump_all_namelists(void)
594 struct subnet_record *subrec;
596 pstrcpy(fname,lp_lockdir());
597 trim_string(fname,NULL,"/");
599 pstrcat(fname,"namelist.debug");
601 fp = fopen(fname,"w");
605 DEBUG(0,("dump_all_namelists: Can't open file %s. Error was %s\n",
606 fname,strerror(errno)));
610 for( subrec = FIRST_SUBNET;
612 subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec) )
613 dump_subnet_namelist( subrec, fp);
615 if(!we_are_a_wins_client())
616 dump_subnet_namelist(unicast_subnet, fp);
618 if(remote_broadcast_subnet->namelist != NULL)
619 dump_subnet_namelist(remote_broadcast_subnet, fp);
621 if(wins_server_subnet != NULL)
622 dump_subnet_namelist( wins_server_subnet, fp);