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. */
46 DEBUG(10,("set_samba_nb_type: %x\n", samba_nb_type));
47 } /* set_samba_nb_type */
49 /* ************************************************************************** **
50 * Convert a NetBIOS name to upper case.
51 * ************************************************************************** **
53 static void upcase_name( struct nmb_name *target, struct nmb_name *source )
58 (void)memcpy( target, source, sizeof( struct nmb_name ) );
60 strupper( target->name );
61 strupper( target->scope );
63 /* fudge... We're using a byte-by-byte compare, so we must be sure that
64 * unused space doesn't have garbage in it.
66 for( i = strlen( target->name ); i < sizeof( target->name ); i++ )
67 target->name[i] = '\0';
68 for( i = strlen( target->scope ); i < sizeof( target->scope ); i++ )
69 target->scope[i] = '\0';
72 /* ************************************************************************** **
73 * Add a new or overwrite an existing namelist entry.
74 * ************************************************************************** **
76 static void update_name_in_namelist( struct subnet_record *subrec,
77 struct name_record *namerec )
79 struct name_record *oldrec = NULL;
81 (void)ubi_trInsert( subrec->namelist, namerec, &(namerec->name), &oldrec );
85 free( oldrec->data.ip );
88 } /* update_name_in_namelist */
90 /* ************************************************************************** **
91 * Remove a name from the namelist.
92 * ************************************************************************** **
94 void remove_name_from_namelist( struct subnet_record *subrec,
95 struct name_record *namerec )
97 (void)ubi_trRemove( subrec->namelist, namerec );
99 if(namerec->data.ip != NULL)
100 free((char *)namerec->data.ip);
102 ZERO_STRUCTP(namerec);
103 free((char *)namerec);
105 subrec->namelist_changed = True;
106 } /* remove_name_from_namelist */
108 /* ************************************************************************** **
109 * Find a name in a subnet.
110 * ************************************************************************** **
112 struct name_record *find_name_on_subnet( struct subnet_record *subrec,
113 struct nmb_name *nmbname,
116 struct nmb_name uc_name[1];
117 struct name_record *name_ret;
119 upcase_name( uc_name, nmbname );
120 name_ret = (struct name_record *)ubi_trFind( subrec->namelist, uc_name );
123 /* Self names only - these include permanent names. */
125 && (name_ret->data.source != SELF_NAME)
126 && (name_ret->data.source != PERMANENT_NAME) )
129 ( "find_name_on_subnet: on subnet %s - self name %s NOT FOUND\n",
130 subrec->subnet_name, nmb_namestr(nmbname) ) );
133 DEBUG( 9, ("find_name_on_subnet: on subnet %s - found name %s source=%d\n",
134 subrec->subnet_name, nmb_namestr(nmbname), name_ret->data.source) );
138 ( "find_name_on_subnet: on subnet %s - name %s NOT FOUND\n",
139 subrec->subnet_name, nmb_namestr(nmbname) ) );
141 } /* find_name_on_subnet */
143 /* ************************************************************************** **
144 * Find a name over all known broadcast subnets.
145 * ************************************************************************** **
147 struct name_record *find_name_for_remote_broadcast_subnet(
148 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)) )
163 } /* find_name_for_remote_broadcast_subnet */
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 + MIN((ttl/2), MAX_REFRESH_TIME);
178 namerec->subnet->namelist_changed = True;
179 } /* update_name_ttl */
181 /* ************************************************************************** **
182 * Add an entry to a subnet name list.
183 * ************************************************************************** **
185 struct name_record *add_name_to_subnet( struct subnet_record *subrec,
190 enum name_source source,
192 struct in_addr *iplist)
194 struct name_record *namerec;
195 time_t time_now = time(NULL);
197 namerec = (struct name_record *)malloc( sizeof(*namerec) );
198 if( NULL == namerec )
200 DEBUG( 0, ( "add_name_to_subnet: malloc fail.\n" ) );
204 bzero( (char *)namerec, sizeof(*namerec) );
205 namerec->data.ip = (struct in_addr *)malloc( sizeof(struct in_addr)
207 if( NULL == namerec->data.ip )
209 DEBUG( 0, ( "add_name_to_subnet: malloc fail when creating ip_flgs.\n" ) );
211 ZERO_STRUCTP(namerec);
212 free( (char *)namerec );
216 namerec->subnet = subrec;
218 make_nmb_name( &namerec->name, name, type, scope );
219 upcase_name( &namerec->name, NULL );
221 /* Enter the name as active. */
222 namerec->data.nb_flags = nb_flags | NB_ACTIVE;
224 /* If it's our primary name, flag it as so. */
225 if( strequal( my_netbios_names[0], name ) )
226 namerec->data.nb_flags |= NB_PERM;
229 namerec->data.num_ips = num_ips;
230 memcpy( (namerec->data.ip), iplist, num_ips * sizeof(struct in_addr) );
233 namerec->data.source = source;
235 /* Setup the death_time and refresh_time. */
236 if( ttl == PERMANENT_TTL )
237 namerec->data.death_time = PERMANENT_TTL;
239 namerec->data.death_time = time_now + ttl;
241 namerec->data.refresh_time = time_now + MIN((ttl/2), MAX_REFRESH_TIME);
243 /* Now add the record to the name list. */
244 update_name_in_namelist( subrec, namerec );
246 DEBUG( 3, ( "add_name_to_subnet: Added netbios name %s with first IP %s \
247 ttl=%d nb_flags=%2x to subnet %s\n",
248 nmb_namestr( &namerec->name ),
249 inet_ntoa( *iplist ),
251 (unsigned int)nb_flags,
252 subrec->subnet_name ) );
254 subrec->namelist_changed = True;
259 /*******************************************************************
260 Utility function automatically called when a name refresh or register
261 succeeds. By definition this is a SELF_NAME (or we wouldn't be registering
263 ******************************************************************/
265 void standard_success_register(struct subnet_record *subrec,
266 struct userdata_struct *userdata,
267 struct nmb_name *nmbname, uint16 nb_flags, int ttl,
268 struct in_addr registered_ip)
270 struct name_record *namerec;
272 namerec = find_name_on_subnet( subrec, nmbname, FIND_SELF_NAME );
273 if( NULL == namerec )
274 (void)add_name_to_subnet( subrec, nmbname->name, nmbname->name_type,
275 nb_flags, ttl, SELF_NAME, 1, ®istered_ip );
277 update_name_ttl( namerec, ttl );
280 /*******************************************************************
281 Utility function automatically called when a name refresh or register
282 fails. Note that this is only ever called on a broadcast subnet with
283 one IP address per name. This is why it can just delete the name
284 without enumerating the IP adresses. JRA.
285 ******************************************************************/
287 void standard_fail_register( struct subnet_record *subrec,
288 struct response_record *rrec,
289 struct nmb_name *nmbname )
291 struct name_record *namerec;
293 namerec = find_name_on_subnet( subrec, nmbname, FIND_SELF_NAME );
295 DEBUG( 0, ( "standard_fail_register: Failed to register/refresh name %s \
297 nmb_namestr(nmbname), subrec->subnet_name) );
299 /* Remove the name from the subnet. */
301 remove_name_from_namelist(subrec, namerec);
304 /*******************************************************************
305 Utility function to remove an IP address from a name record.
306 ******************************************************************/
308 static void remove_nth_ip_in_record( struct name_record *namerec, int ind)
310 if( ind != namerec->data.num_ips )
311 memmove( (char *)(&namerec->data.ip[ind]),
312 (char *)(&namerec->data.ip[ind+1]),
313 ( namerec->data.num_ips - ind - 1) * sizeof(struct in_addr) );
315 namerec->data.num_ips--;
316 namerec->subnet->namelist_changed = True;
319 /*******************************************************************
320 Utility function to check if an IP address exists in a name record.
321 ******************************************************************/
323 BOOL find_ip_in_name_record( struct name_record *namerec, struct in_addr ip )
327 for(i = 0; i < namerec->data.num_ips; i++)
328 if(ip_equal( namerec->data.ip[i], ip))
334 /*******************************************************************
335 Utility function to add an IP address to a name record.
336 ******************************************************************/
338 void add_ip_to_name_record( struct name_record *namerec, struct in_addr new_ip )
340 struct in_addr *new_list;
342 /* Don't add one we already have. */
343 if( find_ip_in_name_record( namerec, new_ip ) )
346 new_list = (struct in_addr *)malloc( (namerec->data.num_ips + 1)
347 * sizeof(struct in_addr) );
348 if( NULL == new_list )
350 DEBUG(0,("add_ip_to_name_record: Malloc fail !\n"));
354 memcpy( (char *)new_list,
355 (char *)namerec->data.ip,
356 namerec->data.num_ips * sizeof(struct in_addr) );
357 new_list[namerec->data.num_ips] = new_ip;
359 free((char *)namerec->data.ip);
360 namerec->data.ip = new_list;
361 namerec->data.num_ips += 1;
363 namerec->subnet->namelist_changed = True;
366 /*******************************************************************
367 Utility function to remove an IP address from a name record.
368 ******************************************************************/
370 void remove_ip_from_name_record( struct name_record *namerec,
371 struct in_addr remove_ip )
373 /* Try and find the requested ip address - remove it. */
375 int orig_num = namerec->data.num_ips;
377 for(i = 0; i < orig_num; i++)
378 if( ip_equal( remove_ip, namerec->data.ip[i]) )
380 remove_nth_ip_in_record( namerec, i);
385 /*******************************************************************
386 Utility function that release_name callers can plug into as the
387 success function when a name release is successful. Used to save
388 duplication of success_function code.
389 ******************************************************************/
391 void standard_success_release( struct subnet_record *subrec,
392 struct userdata_struct *userdata,
393 struct nmb_name *nmbname,
394 struct in_addr released_ip )
396 struct name_record *namerec;
398 namerec = find_name_on_subnet( subrec, nmbname, FIND_ANY_NAME );
400 if( namerec == NULL )
402 DEBUG( 0, ( "standard_success_release: Name release for name %s IP %s \
403 on subnet %s. Name was not found on subnet.\n",
404 nmb_namestr(nmbname),
405 inet_ntoa(released_ip),
406 subrec->subnet_name) );
411 int orig_num = namerec->data.num_ips;
413 remove_ip_from_name_record( namerec, released_ip );
415 if( namerec->data.num_ips == orig_num )
416 DEBUG( 0, ( "standard_success_release: Name release for name %s IP %s \
417 on subnet %s. This ip is not known for this name.\n",
418 nmb_namestr(nmbname),
419 inet_ntoa(released_ip),
420 subrec->subnet_name ) );
423 if( namerec->data.num_ips == 0 )
424 remove_name_from_namelist( subrec, namerec );
427 /*******************************************************************
428 Expires old names in a subnet namelist.
429 ******************************************************************/
431 void expire_names_on_subnet(struct subnet_record *subrec, time_t t)
433 struct name_record *namerec;
434 struct name_record *next_namerec;
436 for( namerec = (struct name_record *)ubi_trFirst( subrec->namelist );
438 namerec = next_namerec )
440 next_namerec = (struct name_record *)ubi_trNext( namerec );
441 if( (namerec->data.death_time != PERMANENT_TTL)
442 && (namerec->data.death_time < t) )
444 if( namerec->data.source == SELF_NAME )
446 DEBUG( 3, ( "expire_names_on_subnet: Subnet %s not expiring SELF \
448 subrec->subnet_name, nmb_namestr(&namerec->name) ) );
449 namerec->data.death_time += 300;
450 namerec->subnet->namelist_changed = True;
453 DEBUG(3,("expire_names_on_subnet: Subnet %s - removing expired name %s\n",
454 subrec->subnet_name, nmb_namestr(&namerec->name)));
456 remove_name_from_namelist( subrec, namerec );
461 /*******************************************************************
462 Expires old names in all subnet namelists.
463 ******************************************************************/
465 void expire_names(time_t t)
467 struct subnet_record *subrec;
469 for( subrec = FIRST_SUBNET;
471 subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec) )
473 expire_names_on_subnet( subrec, t );
477 /****************************************************************************
478 Add the magic samba names, useful for finding samba servers.
479 These go directly into the name list for a particular subnet,
480 without going through the normal registration process.
481 When adding them to the unicast subnet, add them as a list of
482 all broadcast subnet IP addresses.
483 **************************************************************************/
485 void add_samba_names_to_subnet( struct subnet_record *subrec )
487 struct in_addr *iplist = &subrec->myip;
490 /* These names are added permanently (ttl of zero) and will NOT be
493 if( (subrec == unicast_subnet)
494 || (subrec == wins_server_subnet)
495 || (subrec == remote_broadcast_subnet) )
497 struct subnet_record *bcast_subrecs;
499 /* Create an IP list containing all our known subnets. */
501 num_ips = iface_count();
502 iplist = (struct in_addr *)malloc( num_ips * sizeof(struct in_addr) );
505 DEBUG(0,("add_samba_names_to_subnet: Malloc fail !\n"));
509 for( bcast_subrecs = FIRST_SUBNET, i = 0;
511 bcast_subrecs = NEXT_SUBNET_EXCLUDING_UNICAST(bcast_subrecs), i++ )
512 iplist[i] = bcast_subrecs->myip;
516 (void)add_name_to_subnet(subrec,"*",0x0,samba_nb_type, PERMANENT_TTL,
517 PERMANENT_NAME, num_ips, iplist);
518 (void)add_name_to_subnet(subrec,"*",0x20,samba_nb_type,PERMANENT_TTL,
519 PERMANENT_NAME, num_ips, iplist);
520 (void)add_name_to_subnet(subrec,"__SAMBA__",0x20,samba_nb_type,PERMANENT_TTL,
521 PERMANENT_NAME, num_ips, iplist);
522 (void)add_name_to_subnet(subrec,"__SAMBA__",0x00,samba_nb_type,PERMANENT_TTL,
523 PERMANENT_NAME, num_ips, iplist);
525 if(iplist != &subrec->myip)
526 free((char *)iplist);
529 /****************************************************************************
530 Dump the contents of the namelists on all the subnets (including unicast)
531 into a file. Initiated by SIGHUP - used to debug the state of the namelists.
532 **************************************************************************/
534 static void dump_subnet_namelist( struct subnet_record *subrec, FILE *fp)
536 struct name_record *namerec;
541 fprintf(fp, "Subnet %s\n----------------------\n", subrec->subnet_name);
542 for( namerec = (struct name_record *)ubi_trFirst( subrec->namelist );
544 namerec = (struct name_record *)ubi_trNext( namerec ) )
546 fprintf(fp,"\tName = %s\t", nmb_namestr(&namerec->name));
547 switch(namerec->data.source)
550 src_type = "LMHOSTS_NAME";
552 case WINS_PROXY_NAME:
553 src_type = "WINS_PROXY_NAME";
556 src_type = "REGISTER_NAME";
559 src_type = "SELF_NAME";
562 src_type = "DNS_NAME";
565 src_type = "DNSFAIL_NAME";
568 src_type = "PERMANENT_NAME";
571 src_type = "unknown!";
574 fprintf(fp,"Source = %s\nb_flags = %x\t", src_type, namerec->data.nb_flags);
576 if(namerec->data.death_time != PERMANENT_TTL)
578 tm = LocalTime(&namerec->data.death_time);
579 fprintf(fp, "death_time = %s\t", asctime(tm));
582 fprintf(fp, "death_time = PERMANENT\t");
584 if(namerec->data.refresh_time != PERMANENT_TTL)
586 tm = LocalTime(&namerec->data.refresh_time);
587 fprintf(fp, "refresh_time = %s\n", asctime(tm));
590 fprintf(fp, "refresh_time = PERMANENT\n");
592 fprintf(fp, "\t\tnumber of IPS = %d", namerec->data.num_ips);
593 for(i = 0; i < namerec->data.num_ips; i++)
594 fprintf(fp, "\t%s", inet_ntoa(namerec->data.ip[i]));
600 /****************************************************************************
601 Dump the contents of the namelists on all the subnets (including unicast)
602 into a file. Initiated by SIGHUP - used to debug the state of the namelists.
603 **************************************************************************/
605 void dump_all_namelists(void)
609 struct subnet_record *subrec;
611 pstrcpy(fname,lp_lockdir());
612 trim_string(fname,NULL,"/");
614 pstrcat(fname,"namelist.debug");
616 fp = sys_fopen(fname,"w");
620 DEBUG(0,("dump_all_namelists: Can't open file %s. Error was %s\n",
621 fname,strerror(errno)));
625 for( subrec = FIRST_SUBNET;
627 subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec) )
628 dump_subnet_namelist( subrec, fp );
630 if( !we_are_a_wins_client() )
631 dump_subnet_namelist( unicast_subnet, fp );
633 if( remote_broadcast_subnet->namelist != NULL )
634 dump_subnet_namelist( remote_broadcast_subnet, fp );
636 if( wins_server_subnet != NULL )
637 dump_subnet_namelist( wins_server_subnet, fp );