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->ip != NULL)
106 free((char *)namerec->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->source != SELF_NAME) &&
129 (name_ret->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->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 **************************************************************************/
147 struct name_record *find_name_for_remote_broadcast_subnet( struct nmb_name *nmbname,
150 struct subnet_record *subrec;
151 struct name_record *namerec = NULL;
153 for( subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
155 if((namerec = find_name_on_subnet(subrec, nmbname, self_only))!= NULL)
162 /****************************************************************************
163 Update the ttl of an entry in a subnet name list.
164 ****************************************************************************/
166 void update_name_ttl(struct name_record *namerec, int ttl)
168 time_t time_now = time(NULL);
170 if(namerec->death_time != PERMANENT_TTL)
171 namerec->death_time = time_now + ttl;
173 namerec->refresh_time = time_now + (ttl/2);
175 namerec->subnet->namelist_changed = True;
178 /****************************************************************************
179 Add an entry to a subnet name list.
180 ****************************************************************************/
182 struct name_record *add_name_to_subnet(struct subnet_record *subrec,
183 char *name, int type, uint16 nb_flags, int ttl,
184 enum name_source source, int num_ips, struct in_addr *iplist)
186 struct name_record *namerec;
187 time_t time_now = time(NULL);
189 if((namerec = (struct name_record *)malloc(sizeof(*namerec))) == NULL)
191 DEBUG(0,("add_name_to_subnet: malloc fail.\n"));
195 bzero((char *)namerec,sizeof(*namerec));
197 namerec->subnet = subrec;
199 namerec->num_ips = num_ips;
200 namerec->ip = (struct in_addr *)malloc(sizeof(struct in_addr) * namerec->num_ips);
203 DEBUG(0,("add_name_to_subnet: malloc fail when creating ip_flgs.\n"));
204 free((char *)namerec);
208 bzero((char *)namerec->ip, sizeof(struct in_addr) * namerec->num_ips);
210 memcpy(&namerec->ip[0], iplist, num_ips * sizeof(struct in_addr));
212 make_nmb_name(&namerec->name,name,type,scope);
214 /* Setup the death_time and refresh_time. */
215 if(ttl == PERMANENT_TTL)
216 namerec->death_time = PERMANENT_TTL;
218 namerec->death_time = time_now + ttl;
220 namerec->refresh_time = time_now + (ttl/2);
222 /* Enter the name as active. */
223 namerec->nb_flags = nb_flags | NB_ACTIVE;
225 /* If it's our primary name, flag it as so. */
226 if(strequal(my_netbios_names[0],name))
227 namerec->nb_flags |= NB_PERM;
229 namerec->source = source;
231 add_name_to_namelist(subrec,namerec);
233 DEBUG(3,("add_name_to_subnet: Added netbios name %s with first IP %s ttl=%d nb_flags=%2x to subnet %s\n",
234 namestr(&namerec->name),inet_ntoa(*iplist),ttl,(unsigned int)nb_flags,
235 subrec->subnet_name));
237 subrec->namelist_changed = True;
242 /*******************************************************************
243 Utility function automatically called when a name refresh or register
244 succeeds. By definition this is a SELF_NAME (or we wouldn't be registering
246 ******************************************************************/
248 void standard_success_register(struct subnet_record *subrec,
249 struct userdata_struct *userdata,
250 struct nmb_name *nmbname, uint16 nb_flags, int ttl,
251 struct in_addr registered_ip)
253 struct name_record *namerec = find_name_on_subnet(subrec, nmbname, FIND_SELF_NAME);
256 add_name_to_subnet(subrec, nmbname->name, nmbname->name_type,
257 nb_flags, ttl, SELF_NAME, 1, ®istered_ip);
259 update_name_ttl(namerec, ttl);
262 /*******************************************************************
263 Utility function automatically called when a name refresh or register
265 ******************************************************************/
267 void standard_fail_register(struct subnet_record *subrec,
268 struct response_record *rrec, struct nmb_name *nmbname)
270 struct name_record *namerec = find_name_on_subnet(subrec, nmbname, FIND_SELF_NAME);
272 DEBUG(0,("standard_fail_register: Failed to register/refresh name %s on subnet %s\n",
273 namestr(nmbname), subrec->subnet_name));
275 /* Remove the name from the subnet. */
277 remove_name_from_namelist(subrec, namerec);
280 /*******************************************************************
281 Utility function to remove an IP address from a name record.
282 ******************************************************************/
284 static void remove_nth_ip_in_record( struct name_record *namerec, int ind)
286 if(ind != namerec->num_ips)
287 memmove( (char *)(&namerec->ip[ind]), (char *)(&namerec->ip[ind+1]),
288 ( namerec->num_ips - ind - 1) * sizeof(struct in_addr));
291 namerec->subnet->namelist_changed = True;
294 /*******************************************************************
295 Utility function to check if an IP address exists in a name record.
296 ******************************************************************/
298 BOOL find_ip_in_name_record(struct name_record *namerec, struct in_addr ip)
302 for(i = 0; i < namerec->num_ips; i++)
303 if(ip_equal( namerec->ip[i], ip))
309 /*******************************************************************
310 Utility function to add an IP address to a name record.
311 ******************************************************************/
313 void add_ip_to_name_record(struct name_record *namerec, struct in_addr new_ip)
315 struct in_addr *new_list;
317 /* Don't add one we already have. */
318 if(find_ip_in_name_record( namerec, new_ip))
321 if((new_list = (struct in_addr *)malloc( (namerec->num_ips + 1)*sizeof(struct in_addr)) )== NULL)
323 DEBUG(0,("add_ip_to_name_record: Malloc fail !\n"));
327 memcpy((char *)new_list, (char *)namerec->ip, namerec->num_ips *sizeof(struct in_addr));
328 new_list[namerec->num_ips] = new_ip;
330 free((char *)namerec->ip);
331 namerec->ip = new_list;
332 namerec->num_ips += 1;
334 namerec->subnet->namelist_changed = True;
337 /*******************************************************************
338 Utility function to remove an IP address from a name record.
339 ******************************************************************/
341 void remove_ip_from_name_record( struct name_record *namerec, struct in_addr remove_ip)
343 /* Try and find the requested ip address - remove it. */
345 int orig_num = namerec->num_ips;
347 for(i = 0; i < orig_num; i++)
348 if( ip_equal( remove_ip, namerec->ip[i]) )
350 remove_nth_ip_in_record( namerec, i);
355 /*******************************************************************
356 Utility function that release_name callers can plug into as the
357 success function when a name release is successful. Used to save
358 duplication of success_function code.
359 ******************************************************************/
361 void standard_success_release(struct subnet_record *subrec,
362 struct userdata_struct *userdata,
363 struct nmb_name *nmbname, struct in_addr released_ip)
365 struct name_record *namerec = find_name_on_subnet(subrec, nmbname, FIND_ANY_NAME);
369 DEBUG(0,("standard_success_release: Name release for name %s IP %s on subnet %s. Name \
370 was not found on subnet.\n", namestr(nmbname), inet_ntoa(released_ip), subrec->subnet_name));
375 int orig_num = namerec->num_ips;
377 remove_ip_from_name_record( namerec, released_ip);
379 if(namerec->num_ips == orig_num)
380 DEBUG(0,("standard_success_release: Name release for name %s IP %s on subnet %s. This ip \
381 is not known for this name.\n", namestr(nmbname), inet_ntoa(released_ip), subrec->subnet_name ));
384 if (namerec->num_ips == 0)
385 remove_name_from_namelist(subrec, namerec);
388 /*******************************************************************
389 Expires old names in a subnet namelist.
390 ******************************************************************/
392 void expire_names_on_subnet(struct subnet_record *subrec, time_t t)
394 struct name_record *namerec;
395 struct name_record *next_namerec;
397 for (namerec = subrec->namelist; namerec; namerec = next_namerec)
399 next_namerec = namerec->next;
400 if ((namerec->death_time != PERMANENT_TTL) && (namerec->death_time < t))
402 if (namerec->source == SELF_NAME)
404 DEBUG(3,("expire_names_on_subnet: Subnet %s not expiring SELF name %s\n",
405 subrec->subnet_name, namestr(&namerec->name)));
406 namerec->death_time += 300;
407 namerec->subnet->namelist_changed = True;
410 DEBUG(3,("expire_names_on_subnet: Subnet %s - removing expired name %s\n",
411 subrec->subnet_name, namestr(&namerec->name)));
413 remove_name_from_namelist(subrec, namerec);
418 /*******************************************************************
419 Expires old names in all subnet namelists.
420 ******************************************************************/
422 void expire_names(time_t t)
424 struct subnet_record *subrec;
426 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
428 expire_names_on_subnet(subrec, t);
432 /****************************************************************************
433 Add the magic samba names, useful for finding samba servers.
434 These go directly into the name list for a particular subnet,
435 without going through the normal registration process.
436 When adding them to the unicast subnet, add them as a list of
437 all broadcast subnet IP addresses.
438 **************************************************************************/
440 void add_samba_names_to_subnet(struct subnet_record *subrec)
442 struct in_addr *iplist = &subrec->myip;
445 /* These names are added permanently (ttl of zero) and will NOT be
448 if((subrec == unicast_subnet) || (subrec == wins_server_subnet))
450 struct subnet_record *bcast_subrecs;
452 /* Create an IP list containing all our known subnets. */
454 num_ips = iface_count();
455 if((iplist = (struct in_addr *)malloc( num_ips * sizeof(struct in_addr) )) == NULL)
457 DEBUG(0,("add_samba_names_to_subnet: Malloc fail !\n"));
461 for(bcast_subrecs = FIRST_SUBNET, i = 0; bcast_subrecs;
462 bcast_subrecs = NEXT_SUBNET_EXCLUDING_UNICAST(bcast_subrecs), i++)
463 iplist[i] = bcast_subrecs->myip;
467 add_name_to_subnet(subrec,"*",0x0,samba_nb_type, PERMANENT_TTL,
468 PERMANENT_NAME, num_ips, iplist);
469 add_name_to_subnet(subrec,"*",0x20,samba_nb_type,PERMANENT_TTL,
470 PERMANENT_NAME, num_ips, iplist);
471 add_name_to_subnet(subrec,"__SAMBA__",0x20,samba_nb_type,PERMANENT_TTL,
472 PERMANENT_NAME, num_ips, iplist);
473 add_name_to_subnet(subrec,"__SAMBA__",0x00,samba_nb_type,PERMANENT_TTL,
474 PERMANENT_NAME, num_ips, iplist);
476 if(iplist != &subrec->myip)
477 free((char *)iplist);
480 /****************************************************************************
481 Dump the contents of the namelists on all the subnets (including unicast)
482 into a file. Initiated by SIGHUP - used to debug the state of the namelists.
483 **************************************************************************/
485 static void dump_subnet_namelist( struct subnet_record *subrec, FILE *fp)
487 struct name_record *namerec;
492 fprintf(fp, "Subnet %s\n----------------------\n", subrec->subnet_name);
493 for (namerec = subrec->namelist; namerec; namerec = namerec->next)
495 fprintf(fp,"\tName = %s\t", namestr(&namerec->name));
496 switch(namerec->source)
499 src_type = "LMHOSTS_NAME";
501 case WINS_PROXY_NAME:
502 src_type = "WINS_PROXY_NAME";
505 src_type = "REGISTER_NAME";
508 src_type = "SELF_NAME";
511 src_type = "DNS_NAME";
514 src_type = "DNSFAIL_NAME";
517 src_type = "PERMANENT_NAME";
520 src_type = "unknown!";
523 fprintf(fp, "Source = %s\nb_flags = %x\t", src_type, namerec->nb_flags);
525 if(namerec->death_time != PERMANENT_TTL)
527 tm = LocalTime(&namerec->death_time);
528 fprintf(fp, "death_time = %s\t", asctime(tm));
531 fprintf(fp, "death_time = PERMANENT\t");
533 if(namerec->refresh_time != PERMANENT_TTL)
535 tm = LocalTime(&namerec->refresh_time);
536 fprintf(fp, "refresh_time = %s\n", asctime(tm));
539 fprintf(fp, "refresh_time = PERMANENT\n");
541 fprintf(fp, "\t\tnumber of IPS = %d", namerec->num_ips);
542 for(i = 0; i < namerec->num_ips; i++)
543 fprintf(fp, "\t%s", inet_ntoa(namerec->ip[i]));
549 /****************************************************************************
550 Dump the contents of the namelists on all the subnets (including unicast)
551 into a file. Initiated by SIGHUP - used to debug the state of the namelists.
552 **************************************************************************/
554 void dump_all_namelists()
558 struct subnet_record *subrec;
560 pstrcpy(fname,lp_lockdir());
561 trim_string(fname,NULL,"/");
563 strcat(fname,"namelist.debug");
565 fp = fopen(fname,"w");
569 DEBUG(0,("dump_all_namelists: Can't open file %s. Error was %s\n",
570 fname,strerror(errno)));
574 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
575 dump_subnet_namelist( subrec, fp);
577 if(!we_are_a_wins_client())
578 dump_subnet_namelist(unicast_subnet, fp);
580 if(remote_broadcast_subnet->namelist != NULL)
581 dump_subnet_namelist(remote_broadcast_subnet, fp);
583 if(wins_server_subnet != NULL)
584 dump_subnet_namelist( wins_server_subnet, fp);