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 * Convert a NetBIOS name to upper case.
49 * ************************************************************************** **
51 static void upcase_name( struct nmb_name *target, struct nmb_name *source )
56 (void)memcpy( target, source, sizeof( struct nmb_name ) );
58 strupper( target->name );
59 strupper( target->scope );
61 /* fudge... We're using a byte-by-byte compare, so we must be sure that
62 * unused space doesn't have garbage in it.
64 for( i = strlen( target->name ); i < sizeof( target->name ); i++ )
65 target->name[i] = '\0';
66 for( i = strlen( target->scope ); i < sizeof( target->scope ); i++ )
67 target->scope[i] = '\0';
70 /* ************************************************************************** **
71 * Add a new or overwrite an existing namelist entry.
72 * ************************************************************************** **
74 static void update_name_in_namelist( struct subnet_record *subrec,
75 struct name_record *namerec )
77 struct name_record *oldrec = NULL;
79 (void)ubi_trInsert( subrec->namelist, namerec, &(namerec->name), &oldrec );
83 free( oldrec->data.ip );
86 } /* update_name_in_namelist */
88 /* ************************************************************************** **
89 * Remove a name from the namelist.
90 * ************************************************************************** **
92 void remove_name_from_namelist( struct subnet_record *subrec,
93 struct name_record *namerec )
95 (void)ubi_trRemove( subrec->namelist, namerec );
97 if(namerec->data.ip != NULL)
98 free((char *)namerec->data.ip);
100 ZERO_STRUCTP(namerec);
101 free((char *)namerec);
103 subrec->namelist_changed = True;
104 } /* remove_name_from_namelist */
106 /* ************************************************************************** **
107 * Find a name in a subnet.
108 * ************************************************************************** **
110 struct name_record *find_name_on_subnet( struct subnet_record *subrec,
111 struct nmb_name *nmbname,
114 struct nmb_name uc_name[1];
115 struct name_record *name_ret;
117 upcase_name( uc_name, nmbname );
118 name_ret = (struct name_record *)ubi_trFind( subrec->namelist, uc_name );
121 /* Self names only - these include permanent names. */
123 && (name_ret->data.source != SELF_NAME)
124 && (name_ret->data.source != PERMANENT_NAME) )
127 ( "find_name_on_subnet: on subnet %s - self name %s NOT FOUND\n",
128 subrec->subnet_name, nmb_namestr(nmbname) ) );
131 DEBUG( 9, ("find_name_on_subnet: on subnet %s - found name %s source=%d\n",
132 subrec->subnet_name, nmb_namestr(nmbname), name_ret->data.source) );
136 ( "find_name_on_subnet: on subnet %s - name %s NOT FOUND\n",
137 subrec->subnet_name, nmb_namestr(nmbname) ) );
139 } /* find_name_on_subnet */
141 /* ************************************************************************** **
142 * Find a name over all known broadcast subnets.
143 * ************************************************************************** **
145 struct name_record *find_name_for_remote_broadcast_subnet(
146 struct nmb_name *nmbname,
149 struct subnet_record *subrec;
150 struct name_record *namerec = NULL;
152 for( subrec = FIRST_SUBNET;
154 subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec) )
156 if( NULL != (namerec = find_name_on_subnet(subrec, nmbname, self_only)) )
161 } /* find_name_for_remote_broadcast_subnet */
163 /* ************************************************************************** **
164 * Update the ttl of an entry in a subnet name list.
165 * ************************************************************************** **
167 void update_name_ttl( struct name_record *namerec, int ttl )
169 time_t time_now = time(NULL);
171 if( namerec->data.death_time != PERMANENT_TTL )
172 namerec->data.death_time = time_now + ttl;
174 namerec->data.refresh_time = time_now + MIN((ttl/2), MAX_REFRESH_TIME);
176 namerec->subnet->namelist_changed = True;
177 } /* update_name_ttl */
179 /* ************************************************************************** **
180 * Add an entry to a subnet name list.
181 * ************************************************************************** **
183 struct name_record *add_name_to_subnet( struct subnet_record *subrec,
188 enum name_source source,
190 struct in_addr *iplist)
192 struct name_record *namerec;
193 time_t time_now = time(NULL);
195 namerec = (struct name_record *)malloc( sizeof(*namerec) );
196 if( NULL == namerec )
198 DEBUG( 0, ( "add_name_to_subnet: malloc fail.\n" ) );
202 memset( (char *)namerec, '\0', sizeof(*namerec) );
203 namerec->data.ip = (struct in_addr *)malloc( sizeof(struct in_addr)
205 if( NULL == namerec->data.ip )
207 DEBUG( 0, ( "add_name_to_subnet: malloc fail when creating ip_flgs.\n" ) );
209 ZERO_STRUCTP(namerec);
210 free( (char *)namerec );
214 namerec->subnet = subrec;
216 make_nmb_name( &namerec->name, name, type, scope );
217 upcase_name( &namerec->name, NULL );
219 /* Enter the name as active. */
220 namerec->data.nb_flags = nb_flags | NB_ACTIVE;
222 /* If it's our primary name, flag it as so. */
223 if( strequal( my_netbios_names[0], name ) )
224 namerec->data.nb_flags |= NB_PERM;
227 namerec->data.num_ips = num_ips;
228 memcpy( (namerec->data.ip), iplist, num_ips * sizeof(struct in_addr) );
231 namerec->data.source = source;
233 /* Setup the death_time and refresh_time. */
234 if( ttl == PERMANENT_TTL )
235 namerec->data.death_time = PERMANENT_TTL;
237 namerec->data.death_time = time_now + ttl;
239 namerec->data.refresh_time = time_now + MIN((ttl/2), MAX_REFRESH_TIME);
241 /* Now add the record to the name list. */
242 update_name_in_namelist( subrec, namerec );
244 DEBUG( 3, ( "add_name_to_subnet: Added netbios name %s with first IP %s \
245 ttl=%d nb_flags=%2x to subnet %s\n",
246 nmb_namestr( &namerec->name ),
247 inet_ntoa( *iplist ),
249 (unsigned int)nb_flags,
250 subrec->subnet_name ) );
252 subrec->namelist_changed = True;
257 /*******************************************************************
258 Utility function automatically called when a name refresh or register
259 succeeds. By definition this is a SELF_NAME (or we wouldn't be registering
261 ******************************************************************/
263 void standard_success_register(struct subnet_record *subrec,
264 struct userdata_struct *userdata,
265 struct nmb_name *nmbname, uint16 nb_flags, int ttl,
266 struct in_addr registered_ip)
268 struct name_record *namerec;
270 namerec = find_name_on_subnet( subrec, nmbname, FIND_SELF_NAME );
271 if( NULL == namerec )
272 (void)add_name_to_subnet( subrec, nmbname->name, nmbname->name_type,
273 nb_flags, ttl, SELF_NAME, 1, ®istered_ip );
275 update_name_ttl( namerec, ttl );
278 /*******************************************************************
279 Utility function automatically called when a name refresh or register
280 fails. Note that this is only ever called on a broadcast subnet with
281 one IP address per name. This is why it can just delete the name
282 without enumerating the IP adresses. JRA.
283 ******************************************************************/
285 void standard_fail_register( struct subnet_record *subrec,
286 struct response_record *rrec,
287 struct nmb_name *nmbname )
289 struct name_record *namerec;
291 namerec = find_name_on_subnet( subrec, nmbname, FIND_SELF_NAME );
293 DEBUG( 0, ( "standard_fail_register: Failed to register/refresh name %s \
295 nmb_namestr(nmbname), subrec->subnet_name) );
297 /* Remove the name from the subnet. */
299 remove_name_from_namelist(subrec, namerec);
302 /*******************************************************************
303 Utility function to remove an IP address from a name record.
304 ******************************************************************/
306 static void remove_nth_ip_in_record( struct name_record *namerec, int ind)
308 if( ind != namerec->data.num_ips )
309 memmove( (char *)(&namerec->data.ip[ind]),
310 (char *)(&namerec->data.ip[ind+1]),
311 ( namerec->data.num_ips - ind - 1) * sizeof(struct in_addr) );
313 namerec->data.num_ips--;
314 namerec->subnet->namelist_changed = True;
317 /*******************************************************************
318 Utility function to check if an IP address exists in a name record.
319 ******************************************************************/
321 BOOL find_ip_in_name_record( struct name_record *namerec, struct in_addr ip )
325 for(i = 0; i < namerec->data.num_ips; i++)
326 if(ip_equal( namerec->data.ip[i], ip))
332 /*******************************************************************
333 Utility function to add an IP address to a name record.
334 ******************************************************************/
336 void add_ip_to_name_record( struct name_record *namerec, struct in_addr new_ip )
338 struct in_addr *new_list;
340 /* Don't add one we already have. */
341 if( find_ip_in_name_record( namerec, new_ip ) )
344 new_list = (struct in_addr *)malloc( (namerec->data.num_ips + 1)
345 * sizeof(struct in_addr) );
346 if( NULL == new_list )
348 DEBUG(0,("add_ip_to_name_record: Malloc fail !\n"));
352 memcpy( (char *)new_list,
353 (char *)namerec->data.ip,
354 namerec->data.num_ips * sizeof(struct in_addr) );
355 new_list[namerec->data.num_ips] = new_ip;
357 free((char *)namerec->data.ip);
358 namerec->data.ip = new_list;
359 namerec->data.num_ips += 1;
361 namerec->subnet->namelist_changed = True;
364 /*******************************************************************
365 Utility function to remove an IP address from a name record.
366 ******************************************************************/
368 void remove_ip_from_name_record( struct name_record *namerec,
369 struct in_addr remove_ip )
371 /* Try and find the requested ip address - remove it. */
373 int orig_num = namerec->data.num_ips;
375 for(i = 0; i < orig_num; i++)
376 if( ip_equal( remove_ip, namerec->data.ip[i]) )
378 remove_nth_ip_in_record( namerec, i);
383 /*******************************************************************
384 Utility function that release_name callers can plug into as the
385 success function when a name release is successful. Used to save
386 duplication of success_function code.
387 ******************************************************************/
389 void standard_success_release( struct subnet_record *subrec,
390 struct userdata_struct *userdata,
391 struct nmb_name *nmbname,
392 struct in_addr released_ip )
394 struct name_record *namerec;
396 namerec = find_name_on_subnet( subrec, nmbname, FIND_ANY_NAME );
398 if( namerec == NULL )
400 DEBUG( 0, ( "standard_success_release: Name release for name %s IP %s \
401 on subnet %s. Name was not found on subnet.\n",
402 nmb_namestr(nmbname),
403 inet_ntoa(released_ip),
404 subrec->subnet_name) );
409 int orig_num = namerec->data.num_ips;
411 remove_ip_from_name_record( namerec, released_ip );
413 if( namerec->data.num_ips == orig_num )
414 DEBUG( 0, ( "standard_success_release: Name release for name %s IP %s \
415 on subnet %s. This ip is not known for this name.\n",
416 nmb_namestr(nmbname),
417 inet_ntoa(released_ip),
418 subrec->subnet_name ) );
421 if( namerec->data.num_ips == 0 )
422 remove_name_from_namelist( subrec, namerec );
425 /*******************************************************************
426 Expires old names in a subnet namelist.
427 ******************************************************************/
429 void expire_names_on_subnet(struct subnet_record *subrec, time_t t)
431 struct name_record *namerec;
432 struct name_record *next_namerec;
434 for( namerec = (struct name_record *)ubi_trFirst( subrec->namelist );
436 namerec = next_namerec )
438 next_namerec = (struct name_record *)ubi_trNext( namerec );
439 if( (namerec->data.death_time != PERMANENT_TTL)
440 && (namerec->data.death_time < t) )
442 if( namerec->data.source == SELF_NAME )
444 DEBUG( 3, ( "expire_names_on_subnet: Subnet %s not expiring SELF \
446 subrec->subnet_name, nmb_namestr(&namerec->name) ) );
447 namerec->data.death_time += 300;
448 namerec->subnet->namelist_changed = True;
451 DEBUG(3,("expire_names_on_subnet: Subnet %s - removing expired name %s\n",
452 subrec->subnet_name, nmb_namestr(&namerec->name)));
454 remove_name_from_namelist( subrec, namerec );
459 /*******************************************************************
460 Expires old names in all subnet namelists.
461 ******************************************************************/
463 void expire_names(time_t t)
465 struct subnet_record *subrec;
467 for( subrec = FIRST_SUBNET;
469 subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec) )
471 expire_names_on_subnet( subrec, t );
475 /****************************************************************************
476 Add the magic samba names, useful for finding samba servers.
477 These go directly into the name list for a particular subnet,
478 without going through the normal registration process.
479 When adding them to the unicast subnet, add them as a list of
480 all broadcast subnet IP addresses.
481 **************************************************************************/
483 void add_samba_names_to_subnet( struct subnet_record *subrec )
485 struct in_addr *iplist = &subrec->myip;
488 /* These names are added permanently (ttl of zero) and will NOT be
491 if( (subrec == unicast_subnet)
492 || (subrec == wins_server_subnet)
493 || (subrec == remote_broadcast_subnet) )
495 struct subnet_record *bcast_subrecs;
497 /* Create an IP list containing all our known subnets. */
499 num_ips = iface_count();
500 iplist = (struct in_addr *)malloc( num_ips * sizeof(struct in_addr) );
503 DEBUG(0,("add_samba_names_to_subnet: Malloc fail !\n"));
507 for( bcast_subrecs = FIRST_SUBNET, i = 0;
509 bcast_subrecs = NEXT_SUBNET_EXCLUDING_UNICAST(bcast_subrecs), i++ )
510 iplist[i] = bcast_subrecs->myip;
514 (void)add_name_to_subnet(subrec,"*",0x0,samba_nb_type, PERMANENT_TTL,
515 PERMANENT_NAME, num_ips, iplist);
516 (void)add_name_to_subnet(subrec,"*",0x20,samba_nb_type,PERMANENT_TTL,
517 PERMANENT_NAME, num_ips, iplist);
518 (void)add_name_to_subnet(subrec,"__SAMBA__",0x20,samba_nb_type,PERMANENT_TTL,
519 PERMANENT_NAME, num_ips, iplist);
520 (void)add_name_to_subnet(subrec,"__SAMBA__",0x00,samba_nb_type,PERMANENT_TTL,
521 PERMANENT_NAME, num_ips, iplist);
523 if(iplist != &subrec->myip)
524 free((char *)iplist);
527 /****************************************************************************
528 Dump the contents of the namelists on all the subnets (including unicast)
529 into a file. Initiated by SIGHUP - used to debug the state of the namelists.
530 **************************************************************************/
532 static void dump_subnet_namelist( struct subnet_record *subrec, FILE *fp)
534 struct name_record *namerec;
539 fprintf(fp, "Subnet %s\n----------------------\n", subrec->subnet_name);
540 for( namerec = (struct name_record *)ubi_trFirst( subrec->namelist );
542 namerec = (struct name_record *)ubi_trNext( namerec ) )
544 fprintf(fp,"\tName = %s\t", nmb_namestr(&namerec->name));
545 switch(namerec->data.source)
548 src_type = "LMHOSTS_NAME";
550 case WINS_PROXY_NAME:
551 src_type = "WINS_PROXY_NAME";
554 src_type = "REGISTER_NAME";
557 src_type = "SELF_NAME";
560 src_type = "DNS_NAME";
563 src_type = "DNSFAIL_NAME";
566 src_type = "PERMANENT_NAME";
569 src_type = "unknown!";
572 fprintf(fp,"Source = %s\nb_flags = %x\t", src_type, namerec->data.nb_flags);
574 if(namerec->data.death_time != PERMANENT_TTL)
576 tm = LocalTime(&namerec->data.death_time);
577 fprintf(fp, "death_time = %s\t", asctime(tm));
580 fprintf(fp, "death_time = PERMANENT\t");
582 if(namerec->data.refresh_time != PERMANENT_TTL)
584 tm = LocalTime(&namerec->data.refresh_time);
585 fprintf(fp, "refresh_time = %s\n", asctime(tm));
588 fprintf(fp, "refresh_time = PERMANENT\n");
590 fprintf(fp, "\t\tnumber of IPS = %d", namerec->data.num_ips);
591 for(i = 0; i < namerec->data.num_ips; i++)
592 fprintf(fp, "\t%s", inet_ntoa(namerec->data.ip[i]));
598 /****************************************************************************
599 Dump the contents of the namelists on all the subnets (including unicast)
600 into a file. Initiated by SIGHUP - used to debug the state of the namelists.
601 **************************************************************************/
603 void dump_all_namelists(void)
607 struct subnet_record *subrec;
609 pstrcpy(fname,lp_lockdir());
610 trim_string(fname,NULL,"/");
612 pstrcat(fname,"namelist.debug");
614 fp = sys_fopen(fname,"w");
618 DEBUG(0,("dump_all_namelists: Can't open file %s. Error was %s\n",
619 fname,strerror(errno)));
623 for( subrec = FIRST_SUBNET;
625 subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec) )
626 dump_subnet_namelist( subrec, fp );
628 if( !we_are_a_wins_client() )
629 dump_subnet_namelist( unicast_subnet, fp );
631 if( remote_broadcast_subnet->namelist != NULL )
632 dump_subnet_namelist( remote_broadcast_subnet, fp );
634 if( wins_server_subnet != NULL )
635 dump_subnet_namelist( wins_server_subnet, fp );