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. */
45 } /* set_samba_nb_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) );
60 } /* ms_browser_name */
62 /* ************************************************************************** **
63 * Convert a NetBIOS name to upper case.
64 * ************************************************************************** **
66 static void upcase_name( struct nmb_name *target, struct nmb_name *source )
71 (void)memcpy( target, source, sizeof( struct nmb_name ) );
73 strupper( target->name );
74 strupper( target->scope );
76 /* fudge... We're using a byte-by-byte compare, so we must be sure that
77 * unused space doesn't have garbage in it.
79 for( i = strlen( target->name ); i < sizeof( target->name ); i++ )
80 target->name[i] = '\0';
81 for( i = strlen( target->scope ); i < sizeof( target->scope ); i++ )
82 target->scope[i] = '\0';
85 /* ************************************************************************** **
86 * Add a new or overwrite an existing namelist entry.
87 * ************************************************************************** **
89 static void update_name_in_namelist( struct subnet_record *subrec,
90 struct name_record *namerec )
92 struct name_record *oldrec = NULL;
94 (void)ubi_trInsert( subrec->namelist, namerec, &(namerec->name), &oldrec );
98 free( oldrec->data.ip );
101 } /* update_name_in_namelist */
103 /* ************************************************************************** **
104 * Remove a name from the namelist.
105 * ************************************************************************** **
107 void remove_name_from_namelist( struct subnet_record *subrec,
108 struct name_record *namerec )
110 (void)ubi_trRemove( subrec->namelist, namerec );
112 if(namerec->data.ip != NULL)
113 free((char *)namerec->data.ip);
115 ZERO_STRUCTP(namerec);
116 free((char *)namerec);
118 subrec->namelist_changed = True;
119 } /* remove_name_from_namelist */
121 /* ************************************************************************** **
122 * Find a name in a subnet.
123 * ************************************************************************** **
125 struct name_record *find_name_on_subnet( struct subnet_record *subrec,
126 struct nmb_name *nmbname,
129 struct nmb_name uc_name[1];
130 struct name_record *name_ret;
132 upcase_name( uc_name, nmbname );
133 name_ret = (struct name_record *)ubi_trFind( subrec->namelist, uc_name );
136 /* Self names only - these include permanent names. */
138 && (name_ret->data.source != SELF_NAME)
139 && (name_ret->data.source != PERMANENT_NAME) )
142 ( "find_name_on_subnet: on subnet %s - self name %s NOT FOUND\n",
143 subrec->subnet_name, namestr(nmbname) ) );
146 DEBUG( 9, ("find_name_on_subnet: on subnet %s - found name %s source=%d\n",
147 subrec->subnet_name, namestr(nmbname), name_ret->data.source) );
151 ( "find_name_on_subnet: on subnet %s - name %s NOT FOUND\n",
152 subrec->subnet_name, namestr(nmbname) ) );
154 } /* find_name_on_subnet */
156 /* ************************************************************************** **
157 * Find a name over all known broadcast subnets.
158 * ************************************************************************** **
160 struct name_record *find_name_for_remote_broadcast_subnet(
161 struct nmb_name *nmbname,
164 struct subnet_record *subrec;
165 struct name_record *namerec = NULL;
167 for( subrec = FIRST_SUBNET;
169 subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec) )
171 if( NULL != (namerec = find_name_on_subnet(subrec, nmbname, self_only)) )
176 } /* find_name_for_remote_broadcast_subnet */
178 /* ************************************************************************** **
179 * Update the ttl of an entry in a subnet name list.
180 * ************************************************************************** **
182 void update_name_ttl( struct name_record *namerec, int ttl )
184 time_t time_now = time(NULL);
186 if( namerec->data.death_time != PERMANENT_TTL )
187 namerec->data.death_time = time_now + ttl;
189 namerec->data.refresh_time = time_now + MIN((ttl/2), MAX_REFRESH_TIME);
191 namerec->subnet->namelist_changed = True;
192 } /* update_name_ttl */
194 /* ************************************************************************** **
195 * Add an entry to a subnet name list.
196 * ************************************************************************** **
198 struct name_record *add_name_to_subnet( struct subnet_record *subrec,
203 enum name_source source,
205 struct in_addr *iplist)
207 struct name_record *namerec;
208 time_t time_now = time(NULL);
210 namerec = (struct name_record *)malloc( sizeof(*namerec) );
211 if( NULL == namerec )
213 DEBUG( 0, ( "add_name_to_subnet: malloc fail.\n" ) );
217 bzero( (char *)namerec, sizeof(*namerec) );
218 namerec->data.ip = (struct in_addr *)malloc( sizeof(struct in_addr)
220 if( NULL == namerec->data.ip )
222 DEBUG( 0, ( "add_name_to_subnet: malloc fail when creating ip_flgs.\n" ) );
224 ZERO_STRUCTP(namerec);
225 free( (char *)namerec );
229 namerec->subnet = subrec;
231 make_nmb_name( &namerec->name, name, type, scope );
232 upcase_name( &namerec->name, NULL );
234 /* Enter the name as active. */
235 namerec->data.nb_flags = nb_flags | NB_ACTIVE;
237 /* If it's our primary name, flag it as so. */
238 if( strequal( my_netbios_names[0], name ) )
239 namerec->data.nb_flags |= NB_PERM;
242 namerec->data.num_ips = num_ips;
243 memcpy( (namerec->data.ip), iplist, num_ips * sizeof(struct in_addr) );
246 namerec->data.source = source;
248 /* Setup the death_time and refresh_time. */
249 if( ttl == PERMANENT_TTL )
250 namerec->data.death_time = PERMANENT_TTL;
252 namerec->data.death_time = time_now + ttl;
254 namerec->data.refresh_time = time_now + MIN((ttl/2), MAX_REFRESH_TIME);
256 /* Now add the record to the name list. */
257 update_name_in_namelist( subrec, namerec );
259 DEBUG( 3, ( "add_name_to_subnet: Added netbios name %s with first IP %s \
260 ttl=%d nb_flags=%2x to subnet %s\n",
261 namestr( &namerec->name ),
262 inet_ntoa( *iplist ),
264 (unsigned int)nb_flags,
265 subrec->subnet_name ) );
267 subrec->namelist_changed = True;
272 /*******************************************************************
273 Utility function automatically called when a name refresh or register
274 succeeds. By definition this is a SELF_NAME (or we wouldn't be registering
276 ******************************************************************/
278 void standard_success_register(struct subnet_record *subrec,
279 struct userdata_struct *userdata,
280 struct nmb_name *nmbname, uint16 nb_flags, int ttl,
281 struct in_addr registered_ip)
283 struct name_record *namerec;
285 namerec = find_name_on_subnet( subrec, nmbname, FIND_SELF_NAME );
286 if( NULL == namerec )
287 (void)add_name_to_subnet( subrec, nmbname->name, nmbname->name_type,
288 nb_flags, ttl, SELF_NAME, 1, ®istered_ip );
290 update_name_ttl( namerec, ttl );
293 /*******************************************************************
294 Utility function automatically called when a name refresh or register
295 fails. Note that this is only ever called on a broadcast subnet with
296 one IP address per name. This is why it can just delete the name
297 without enumerating the IP adresses. JRA.
298 ******************************************************************/
300 void standard_fail_register( struct subnet_record *subrec,
301 struct response_record *rrec,
302 struct nmb_name *nmbname )
304 struct name_record *namerec;
306 namerec = find_name_on_subnet( subrec, nmbname, FIND_SELF_NAME );
308 DEBUG( 0, ( "standard_fail_register: Failed to register/refresh name %s \
310 namestr(nmbname), subrec->subnet_name) );
312 /* Remove the name from the subnet. */
314 remove_name_from_namelist(subrec, namerec);
317 /*******************************************************************
318 Utility function to remove an IP address from a name record.
319 ******************************************************************/
321 static void remove_nth_ip_in_record( struct name_record *namerec, int ind)
323 if( ind != namerec->data.num_ips )
324 memmove( (char *)(&namerec->data.ip[ind]),
325 (char *)(&namerec->data.ip[ind+1]),
326 ( namerec->data.num_ips - ind - 1) * sizeof(struct in_addr) );
328 namerec->data.num_ips--;
329 namerec->subnet->namelist_changed = True;
332 /*******************************************************************
333 Utility function to check if an IP address exists in a name record.
334 ******************************************************************/
336 BOOL find_ip_in_name_record( struct name_record *namerec, struct in_addr ip )
340 for(i = 0; i < namerec->data.num_ips; i++)
341 if(ip_equal( namerec->data.ip[i], ip))
347 /*******************************************************************
348 Utility function to add an IP address to a name record.
349 ******************************************************************/
351 void add_ip_to_name_record( struct name_record *namerec, struct in_addr new_ip )
353 struct in_addr *new_list;
355 /* Don't add one we already have. */
356 if( find_ip_in_name_record( namerec, new_ip ) )
359 new_list = (struct in_addr *)malloc( (namerec->data.num_ips + 1)
360 * sizeof(struct in_addr) );
361 if( NULL == new_list )
363 DEBUG(0,("add_ip_to_name_record: Malloc fail !\n"));
367 memcpy( (char *)new_list,
368 (char *)namerec->data.ip,
369 namerec->data.num_ips * sizeof(struct in_addr) );
370 new_list[namerec->data.num_ips] = new_ip;
372 free((char *)namerec->data.ip);
373 namerec->data.ip = new_list;
374 namerec->data.num_ips += 1;
376 namerec->subnet->namelist_changed = True;
379 /*******************************************************************
380 Utility function to remove an IP address from a name record.
381 ******************************************************************/
383 void remove_ip_from_name_record( struct name_record *namerec,
384 struct in_addr remove_ip )
386 /* Try and find the requested ip address - remove it. */
388 int orig_num = namerec->data.num_ips;
390 for(i = 0; i < orig_num; i++)
391 if( ip_equal( remove_ip, namerec->data.ip[i]) )
393 remove_nth_ip_in_record( namerec, i);
398 /*******************************************************************
399 Utility function that release_name callers can plug into as the
400 success function when a name release is successful. Used to save
401 duplication of success_function code.
402 ******************************************************************/
404 void standard_success_release( struct subnet_record *subrec,
405 struct userdata_struct *userdata,
406 struct nmb_name *nmbname,
407 struct in_addr released_ip )
409 struct name_record *namerec;
411 namerec = find_name_on_subnet( subrec, nmbname, FIND_ANY_NAME );
413 if( namerec == NULL )
415 DEBUG( 0, ( "standard_success_release: Name release for name %s IP %s \
416 on subnet %s. Name was not found on subnet.\n",
418 inet_ntoa(released_ip),
419 subrec->subnet_name) );
424 int orig_num = namerec->data.num_ips;
426 remove_ip_from_name_record( namerec, released_ip );
428 if( namerec->data.num_ips == orig_num )
429 DEBUG( 0, ( "standard_success_release: Name release for name %s IP %s \
430 on subnet %s. This ip is not known for this name.\n",
432 inet_ntoa(released_ip),
433 subrec->subnet_name ) );
436 if( namerec->data.num_ips == 0 )
437 remove_name_from_namelist( subrec, namerec );
440 /*******************************************************************
441 Expires old names in a subnet namelist.
442 ******************************************************************/
444 void expire_names_on_subnet(struct subnet_record *subrec, time_t t)
446 struct name_record *namerec;
447 struct name_record *next_namerec;
449 for( namerec = (struct name_record *)ubi_trFirst( subrec->namelist );
451 namerec = next_namerec )
453 next_namerec = (struct name_record *)ubi_trNext( namerec );
454 if( (namerec->data.death_time != PERMANENT_TTL)
455 && (namerec->data.death_time < t) )
457 if( namerec->data.source == SELF_NAME )
459 DEBUG( 3, ( "expire_names_on_subnet: Subnet %s not expiring SELF \
461 subrec->subnet_name, namestr(&namerec->name) ) );
462 namerec->data.death_time += 300;
463 namerec->subnet->namelist_changed = True;
466 DEBUG(3,("expire_names_on_subnet: Subnet %s - removing expired name %s\n",
467 subrec->subnet_name, namestr(&namerec->name)));
469 remove_name_from_namelist( subrec, namerec );
474 /*******************************************************************
475 Expires old names in all subnet namelists.
476 ******************************************************************/
478 void expire_names(time_t t)
480 struct subnet_record *subrec;
482 for( subrec = FIRST_SUBNET;
484 subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec) )
486 expire_names_on_subnet( subrec, t );
490 /****************************************************************************
491 Add the magic samba names, useful for finding samba servers.
492 These go directly into the name list for a particular subnet,
493 without going through the normal registration process.
494 When adding them to the unicast subnet, add them as a list of
495 all broadcast subnet IP addresses.
496 **************************************************************************/
498 void add_samba_names_to_subnet( struct subnet_record *subrec )
500 struct in_addr *iplist = &subrec->myip;
503 /* These names are added permanently (ttl of zero) and will NOT be
506 if( (subrec == unicast_subnet)
507 || (subrec == wins_server_subnet)
508 || (subrec == remote_broadcast_subnet) )
510 struct subnet_record *bcast_subrecs;
512 /* Create an IP list containing all our known subnets. */
514 num_ips = iface_count();
515 iplist = (struct in_addr *)malloc( num_ips * sizeof(struct in_addr) );
518 DEBUG(0,("add_samba_names_to_subnet: Malloc fail !\n"));
522 for( bcast_subrecs = FIRST_SUBNET, i = 0;
524 bcast_subrecs = NEXT_SUBNET_EXCLUDING_UNICAST(bcast_subrecs), i++ )
525 iplist[i] = bcast_subrecs->myip;
529 (void)add_name_to_subnet(subrec,"*",0x0,samba_nb_type, PERMANENT_TTL,
530 PERMANENT_NAME, num_ips, iplist);
531 (void)add_name_to_subnet(subrec,"*",0x20,samba_nb_type,PERMANENT_TTL,
532 PERMANENT_NAME, num_ips, iplist);
533 (void)add_name_to_subnet(subrec,"__SAMBA__",0x20,samba_nb_type,PERMANENT_TTL,
534 PERMANENT_NAME, num_ips, iplist);
535 (void)add_name_to_subnet(subrec,"__SAMBA__",0x00,samba_nb_type,PERMANENT_TTL,
536 PERMANENT_NAME, num_ips, iplist);
538 if(iplist != &subrec->myip)
539 free((char *)iplist);
542 /****************************************************************************
543 Dump the contents of the namelists on all the subnets (including unicast)
544 into a file. Initiated by SIGHUP - used to debug the state of the namelists.
545 **************************************************************************/
547 static void dump_subnet_namelist( struct subnet_record *subrec, FILE *fp)
549 struct name_record *namerec;
554 fprintf(fp, "Subnet %s\n----------------------\n", subrec->subnet_name);
555 for( namerec = (struct name_record *)ubi_trFirst( subrec->namelist );
557 namerec = (struct name_record *)ubi_trNext( namerec ) )
559 fprintf(fp,"\tName = %s\t", namestr(&namerec->name));
560 switch(namerec->data.source)
563 src_type = "LMHOSTS_NAME";
565 case WINS_PROXY_NAME:
566 src_type = "WINS_PROXY_NAME";
569 src_type = "REGISTER_NAME";
572 src_type = "SELF_NAME";
575 src_type = "DNS_NAME";
578 src_type = "DNSFAIL_NAME";
581 src_type = "PERMANENT_NAME";
584 src_type = "unknown!";
587 fprintf(fp,"Source = %s\nb_flags = %x\t", src_type, namerec->data.nb_flags);
589 if(namerec->data.death_time != PERMANENT_TTL)
591 tm = LocalTime(&namerec->data.death_time);
592 fprintf(fp, "death_time = %s\t", asctime(tm));
595 fprintf(fp, "death_time = PERMANENT\t");
597 if(namerec->data.refresh_time != PERMANENT_TTL)
599 tm = LocalTime(&namerec->data.refresh_time);
600 fprintf(fp, "refresh_time = %s\n", asctime(tm));
603 fprintf(fp, "refresh_time = PERMANENT\n");
605 fprintf(fp, "\t\tnumber of IPS = %d", namerec->data.num_ips);
606 for(i = 0; i < namerec->data.num_ips; i++)
607 fprintf(fp, "\t%s", inet_ntoa(namerec->data.ip[i]));
613 /****************************************************************************
614 Dump the contents of the namelists on all the subnets (including unicast)
615 into a file. Initiated by SIGHUP - used to debug the state of the namelists.
616 **************************************************************************/
618 void dump_all_namelists(void)
622 struct subnet_record *subrec;
624 pstrcpy(fname,lp_lockdir());
625 trim_string(fname,NULL,"/");
627 pstrcat(fname,"namelist.debug");
629 fp = fopen(fname,"w");
633 DEBUG(0,("dump_all_namelists: Can't open file %s. Error was %s\n",
634 fname,strerror(errno)));
638 for( subrec = FIRST_SUBNET;
640 subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec) )
641 dump_subnet_namelist( subrec, fp );
643 if( !we_are_a_wins_client() )
644 dump_subnet_namelist( unicast_subnet, fp );
646 if( remote_broadcast_subnet->namelist != NULL )
647 dump_subnet_namelist( remote_broadcast_subnet, fp );
649 if( wins_server_subnet != NULL )
650 dump_subnet_namelist( wins_server_subnet, fp );