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);
114 free((char *)namerec);
116 subrec->namelist_changed = True;
117 } /* remove_name_from_namelist */
119 /* ************************************************************************** **
120 * Find a name in a subnet.
121 * ************************************************************************** **
123 struct name_record *find_name_on_subnet( struct subnet_record *subrec,
124 struct nmb_name *nmbname,
127 struct nmb_name uc_name[1];
128 struct name_record *name_ret;
130 upcase_name( uc_name, nmbname );
131 name_ret = (struct name_record *)ubi_trFind( subrec->namelist, uc_name );
134 /* Self names only - these include permanent names. */
136 && (name_ret->data.source != SELF_NAME)
137 && (name_ret->data.source != PERMANENT_NAME) )
140 ( "find_name_on_subnet: on subnet %s - self name %s NOT FOUND\n",
141 subrec->subnet_name, namestr(nmbname) ) );
144 DEBUG( 9, ("find_name_on_subnet: on subnet %s - found name %s source=%d\n",
145 subrec->subnet_name, namestr(nmbname), name_ret->data.source) );
149 ( "find_name_on_subnet: on subnet %s - name %s NOT FOUND\n",
150 subrec->subnet_name, namestr(nmbname) ) );
152 } /* find_name_on_subnet */
154 /* ************************************************************************** **
155 * Find a name over all known broadcast subnets.
156 * ************************************************************************** **
158 struct name_record *find_name_for_remote_broadcast_subnet(
159 struct nmb_name *nmbname,
162 struct subnet_record *subrec;
163 struct name_record *namerec = NULL;
165 for( subrec = FIRST_SUBNET;
167 subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec) )
169 if( NULL != (namerec = find_name_on_subnet(subrec, nmbname, self_only)) )
174 } /* find_name_for_remote_broadcast_subnet */
176 /* ************************************************************************** **
177 * Update the ttl of an entry in a subnet name list.
178 * ************************************************************************** **
180 void update_name_ttl( struct name_record *namerec, int ttl )
182 time_t time_now = time(NULL);
184 if( namerec->data.death_time != PERMANENT_TTL )
185 namerec->data.death_time = time_now + ttl;
187 namerec->data.refresh_time = time_now + (ttl/2);
189 namerec->subnet->namelist_changed = True;
190 } /* update_name_ttl */
192 /* ************************************************************************** **
193 * Add an entry to a subnet name list.
194 * ************************************************************************** **
196 struct name_record *add_name_to_subnet( struct subnet_record *subrec,
201 enum name_source source,
203 struct in_addr *iplist)
205 struct name_record *namerec;
206 time_t time_now = time(NULL);
208 namerec = (struct name_record *)malloc( sizeof(*namerec) );
209 if( NULL == namerec )
211 DEBUG( 0, ( "add_name_to_subnet: malloc fail.\n" ) );
215 bzero( (char *)namerec, sizeof(*namerec) );
216 namerec->data.ip = (struct in_addr *)malloc( sizeof(struct in_addr)
218 if( NULL == namerec->data.ip )
220 DEBUG( 0, ( "add_name_to_subnet: malloc fail when creating ip_flgs.\n" ) );
221 free( (char *)namerec );
225 namerec->subnet = subrec;
227 make_nmb_name( &namerec->name, name, type, scope );
228 upcase_name( &namerec->name, NULL );
230 /* Enter the name as active. */
231 namerec->data.nb_flags = nb_flags | NB_ACTIVE;
233 /* If it's our primary name, flag it as so. */
234 if( strequal( my_netbios_names[0], name ) )
235 namerec->data.nb_flags |= NB_PERM;
238 namerec->data.num_ips = num_ips;
239 memcpy( (namerec->data.ip), iplist, num_ips * sizeof(struct in_addr) );
242 namerec->data.source = source;
244 /* Setup the death_time and refresh_time. */
245 if( ttl == PERMANENT_TTL )
246 namerec->data.death_time = PERMANENT_TTL;
248 namerec->data.death_time = time_now + ttl;
250 namerec->data.refresh_time = time_now + (ttl/2);
252 /* Now add the record to the name list. */
253 update_name_in_namelist( subrec, namerec );
255 DEBUG( 3, ( "add_name_to_subnet: Added netbios name %s with first IP %s \
256 ttl=%d nb_flags=%2x to subnet %s\n",
257 namestr( &namerec->name ),
258 inet_ntoa( *iplist ),
260 (unsigned int)nb_flags,
261 subrec->subnet_name ) );
263 subrec->namelist_changed = True;
268 /*******************************************************************
269 Utility function automatically called when a name refresh or register
270 succeeds. By definition this is a SELF_NAME (or we wouldn't be registering
272 ******************************************************************/
274 void standard_success_register(struct subnet_record *subrec,
275 struct userdata_struct *userdata,
276 struct nmb_name *nmbname, uint16 nb_flags, int ttl,
277 struct in_addr registered_ip)
279 struct name_record *namerec;
281 namerec = find_name_on_subnet( subrec, nmbname, FIND_SELF_NAME );
282 if( NULL == namerec )
283 (void)add_name_to_subnet( subrec, nmbname->name, nmbname->name_type,
284 nb_flags, ttl, SELF_NAME, 1, ®istered_ip );
286 update_name_ttl( namerec, ttl );
289 /*******************************************************************
290 Utility function automatically called when a name refresh or register
291 fails. Note that this is only ever called on a broadcast subnet with
292 one IP address per name. This is why it can just delete the name
293 without enumerating the IP adresses. JRA.
294 ******************************************************************/
296 void standard_fail_register( struct subnet_record *subrec,
297 struct response_record *rrec,
298 struct nmb_name *nmbname )
300 struct name_record *namerec;
302 namerec = find_name_on_subnet( subrec, nmbname, FIND_SELF_NAME );
304 DEBUG( 0, ( "standard_fail_register: Failed to register/refresh name %s \
306 namestr(nmbname), subrec->subnet_name) );
308 /* Remove the name from the subnet. */
310 remove_name_from_namelist(subrec, namerec);
313 /*******************************************************************
314 Utility function to remove an IP address from a name record.
315 ******************************************************************/
317 static void remove_nth_ip_in_record( struct name_record *namerec, int ind)
319 if( ind != namerec->data.num_ips )
320 memmove( (char *)(&namerec->data.ip[ind]),
321 (char *)(&namerec->data.ip[ind+1]),
322 ( namerec->data.num_ips - ind - 1) * sizeof(struct in_addr) );
324 namerec->data.num_ips--;
325 namerec->subnet->namelist_changed = True;
328 /*******************************************************************
329 Utility function to check if an IP address exists in a name record.
330 ******************************************************************/
332 BOOL find_ip_in_name_record( struct name_record *namerec, struct in_addr ip )
336 for(i = 0; i < namerec->data.num_ips; i++)
337 if(ip_equal( namerec->data.ip[i], ip))
343 /*******************************************************************
344 Utility function to add an IP address to a name record.
345 ******************************************************************/
347 void add_ip_to_name_record( struct name_record *namerec, struct in_addr new_ip )
349 struct in_addr *new_list;
351 /* Don't add one we already have. */
352 if( find_ip_in_name_record( namerec, new_ip ) )
355 new_list = (struct in_addr *)malloc( (namerec->data.num_ips + 1)
356 * sizeof(struct in_addr) );
357 if( NULL == new_list )
359 DEBUG(0,("add_ip_to_name_record: Malloc fail !\n"));
363 memcpy( (char *)new_list,
364 (char *)namerec->data.ip,
365 namerec->data.num_ips * sizeof(struct in_addr) );
366 new_list[namerec->data.num_ips] = new_ip;
368 free((char *)namerec->data.ip);
369 namerec->data.ip = new_list;
370 namerec->data.num_ips += 1;
372 namerec->subnet->namelist_changed = True;
375 /*******************************************************************
376 Utility function to remove an IP address from a name record.
377 ******************************************************************/
379 void remove_ip_from_name_record( struct name_record *namerec,
380 struct in_addr remove_ip )
382 /* Try and find the requested ip address - remove it. */
384 int orig_num = namerec->data.num_ips;
386 for(i = 0; i < orig_num; i++)
387 if( ip_equal( remove_ip, namerec->data.ip[i]) )
389 remove_nth_ip_in_record( namerec, i);
394 /*******************************************************************
395 Utility function that release_name callers can plug into as the
396 success function when a name release is successful. Used to save
397 duplication of success_function code.
398 ******************************************************************/
400 void standard_success_release( struct subnet_record *subrec,
401 struct userdata_struct *userdata,
402 struct nmb_name *nmbname,
403 struct in_addr released_ip )
405 struct name_record *namerec;
407 namerec = find_name_on_subnet( subrec, nmbname, FIND_ANY_NAME );
409 if( namerec == NULL )
411 DEBUG( 0, ( "standard_success_release: Name release for name %s IP %s \
412 on subnet %s. Name was not found on subnet.\n",
414 inet_ntoa(released_ip),
415 subrec->subnet_name) );
420 int orig_num = namerec->data.num_ips;
422 remove_ip_from_name_record( namerec, released_ip );
424 if( namerec->data.num_ips == orig_num )
425 DEBUG( 0, ( "standard_success_release: Name release for name %s IP %s \
426 on subnet %s. This ip is not known for this name.\n",
428 inet_ntoa(released_ip),
429 subrec->subnet_name ) );
432 if( namerec->data.num_ips == 0 )
433 remove_name_from_namelist( subrec, namerec );
436 /*******************************************************************
437 Expires old names in a subnet namelist.
438 ******************************************************************/
440 void expire_names_on_subnet(struct subnet_record *subrec, time_t t)
442 struct name_record *namerec;
443 struct name_record *next_namerec;
445 for( namerec = (struct name_record *)ubi_trFirst( subrec->namelist );
447 namerec = next_namerec )
449 next_namerec = (struct name_record *)ubi_trNext( namerec );
450 if( (namerec->data.death_time != PERMANENT_TTL)
451 && (namerec->data.death_time < t) )
453 if( namerec->data.source == SELF_NAME )
455 DEBUG( 3, ( "expire_names_on_subnet: Subnet %s not expiring SELF \
457 subrec->subnet_name, namestr(&namerec->name) ) );
458 namerec->data.death_time += 300;
459 namerec->subnet->namelist_changed = True;
462 DEBUG(3,("expire_names_on_subnet: Subnet %s - removing expired name %s\n",
463 subrec->subnet_name, namestr(&namerec->name)));
465 remove_name_from_namelist( subrec, namerec );
470 /*******************************************************************
471 Expires old names in all subnet namelists.
472 ******************************************************************/
474 void expire_names(time_t t)
476 struct subnet_record *subrec;
478 for( subrec = FIRST_SUBNET;
480 subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec) )
482 expire_names_on_subnet( subrec, t );
486 /****************************************************************************
487 Add the magic samba names, useful for finding samba servers.
488 These go directly into the name list for a particular subnet,
489 without going through the normal registration process.
490 When adding them to the unicast subnet, add them as a list of
491 all broadcast subnet IP addresses.
492 **************************************************************************/
494 void add_samba_names_to_subnet( struct subnet_record *subrec )
496 struct in_addr *iplist = &subrec->myip;
499 /* These names are added permanently (ttl of zero) and will NOT be
502 if( (subrec == unicast_subnet)
503 || (subrec == wins_server_subnet)
504 || (subrec == remote_broadcast_subnet) )
506 struct subnet_record *bcast_subrecs;
508 /* Create an IP list containing all our known subnets. */
510 num_ips = iface_count();
511 iplist = (struct in_addr *)malloc( num_ips * sizeof(struct in_addr) );
514 DEBUG(0,("add_samba_names_to_subnet: Malloc fail !\n"));
518 for( bcast_subrecs = FIRST_SUBNET, i = 0;
520 bcast_subrecs = NEXT_SUBNET_EXCLUDING_UNICAST(bcast_subrecs), i++ )
521 iplist[i] = bcast_subrecs->myip;
525 (void)add_name_to_subnet(subrec,"*",0x0,samba_nb_type, PERMANENT_TTL,
526 PERMANENT_NAME, num_ips, iplist);
527 (void)add_name_to_subnet(subrec,"*",0x20,samba_nb_type,PERMANENT_TTL,
528 PERMANENT_NAME, num_ips, iplist);
529 (void)add_name_to_subnet(subrec,"__SAMBA__",0x20,samba_nb_type,PERMANENT_TTL,
530 PERMANENT_NAME, num_ips, iplist);
531 (void)add_name_to_subnet(subrec,"__SAMBA__",0x00,samba_nb_type,PERMANENT_TTL,
532 PERMANENT_NAME, num_ips, iplist);
534 if(iplist != &subrec->myip)
535 free((char *)iplist);
538 /****************************************************************************
539 Dump the contents of the namelists on all the subnets (including unicast)
540 into a file. Initiated by SIGHUP - used to debug the state of the namelists.
541 **************************************************************************/
543 static void dump_subnet_namelist( struct subnet_record *subrec, FILE *fp)
545 struct name_record *namerec;
550 fprintf(fp, "Subnet %s\n----------------------\n", subrec->subnet_name);
551 for( namerec = (struct name_record *)ubi_trFirst( subrec->namelist );
553 namerec = (struct name_record *)ubi_trNext( namerec ) )
555 fprintf(fp,"\tName = %s\t", namestr(&namerec->name));
556 switch(namerec->data.source)
559 src_type = "LMHOSTS_NAME";
561 case WINS_PROXY_NAME:
562 src_type = "WINS_PROXY_NAME";
565 src_type = "REGISTER_NAME";
568 src_type = "SELF_NAME";
571 src_type = "DNS_NAME";
574 src_type = "DNSFAIL_NAME";
577 src_type = "PERMANENT_NAME";
580 src_type = "unknown!";
583 fprintf(fp,"Source = %s\nb_flags = %x\t", src_type, namerec->data.nb_flags);
585 if(namerec->data.death_time != PERMANENT_TTL)
587 tm = LocalTime(&namerec->data.death_time);
588 fprintf(fp, "death_time = %s\t", asctime(tm));
591 fprintf(fp, "death_time = PERMANENT\t");
593 if(namerec->data.refresh_time != PERMANENT_TTL)
595 tm = LocalTime(&namerec->data.refresh_time);
596 fprintf(fp, "refresh_time = %s\n", asctime(tm));
599 fprintf(fp, "refresh_time = PERMANENT\n");
601 fprintf(fp, "\t\tnumber of IPS = %d", namerec->data.num_ips);
602 for(i = 0; i < namerec->data.num_ips; i++)
603 fprintf(fp, "\t%s", inet_ntoa(namerec->data.ip[i]));
609 /****************************************************************************
610 Dump the contents of the namelists on all the subnets (including unicast)
611 into a file. Initiated by SIGHUP - used to debug the state of the namelists.
612 **************************************************************************/
614 void dump_all_namelists(void)
618 struct subnet_record *subrec;
620 pstrcpy(fname,lp_lockdir());
621 trim_string(fname,NULL,"/");
623 pstrcat(fname,"namelist.debug");
625 fp = fopen(fname,"w");
629 DEBUG(0,("dump_all_namelists: Can't open file %s. Error was %s\n",
630 fname,strerror(errno)));
634 for( subrec = FIRST_SUBNET;
636 subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec) )
637 dump_subnet_namelist( subrec, fp );
639 if( !we_are_a_wins_client() )
640 dump_subnet_namelist( unicast_subnet, fp );
642 if( remote_broadcast_subnet->namelist != NULL )
643 dump_subnet_namelist( remote_broadcast_subnet, fp );
645 if( wins_server_subnet != NULL )
646 dump_subnet_namelist( wins_server_subnet, fp );