sync with the sec_desc parsing fix from APP_HEAD. I will probably need
[Samba.git] / source / nmbd / nmbd_namelistdb.c
blob68b44d618f5c8d9616981a14109f0476349b20b0
1 /*
2 Unix SMB/CIFS implementation.
3 NBT netbios routines and daemon - version 2
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Luke Kenneth Casson Leighton 1994-1998
6 Copyright (C) Jeremy Allison 1994-1998
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "includes.h"
26 extern char **my_netbios_names;
28 uint16 samba_nb_type = 0; /* samba's NetBIOS name type */
31 /* ************************************************************************** **
32 * Set Samba's NetBIOS name type.
33 * ************************************************************************** **
35 void set_samba_nb_type(void)
37 if( lp_wins_support() || wins_srv_count() )
38 samba_nb_type = NB_MFLAG; /* samba is a 'hybrid' node type. */
39 else
40 samba_nb_type = NB_BFLAG; /* samba is broadcast-only node type. */
41 } /* set_samba_nb_type */
43 /* ************************************************************************** **
44 * Convert a NetBIOS name to upper case.
45 * ************************************************************************** **
47 static void upcase_name( struct nmb_name *target, struct nmb_name *source )
49 int i;
51 if( NULL != source )
52 (void)memcpy( target, source, sizeof( struct nmb_name ) );
54 strupper( target->name );
55 strupper( target->scope );
57 /* fudge... We're using a byte-by-byte compare, so we must be sure that
58 * unused space doesn't have garbage in it.
60 for( i = strlen( target->name ); i < sizeof( target->name ); i++ )
61 target->name[i] = '\0';
62 for( i = strlen( target->scope ); i < sizeof( target->scope ); i++ )
63 target->scope[i] = '\0';
64 } /* upcase_name */
66 /* ************************************************************************** **
67 * Add a new or overwrite an existing namelist entry.
68 * ************************************************************************** **
70 static void update_name_in_namelist( struct subnet_record *subrec,
71 struct name_record *namerec )
73 struct name_record *oldrec = NULL;
75 (void)ubi_trInsert( subrec->namelist, namerec, &(namerec->name), &oldrec );
76 if( oldrec )
78 SAFE_FREE( oldrec->data.ip );
79 SAFE_FREE( oldrec );
81 } /* update_name_in_namelist */
83 /* ************************************************************************** **
84 * Remove a name from the namelist.
85 * ************************************************************************** **
87 void remove_name_from_namelist( struct subnet_record *subrec,
88 struct name_record *namerec )
90 (void)ubi_trRemove( subrec->namelist, namerec );
92 SAFE_FREE(namerec->data.ip);
94 ZERO_STRUCTP(namerec);
95 SAFE_FREE(namerec);
97 subrec->namelist_changed = True;
98 } /* remove_name_from_namelist */
100 /* ************************************************************************** **
101 * Find a name in a subnet.
102 * ************************************************************************** **
104 struct name_record *find_name_on_subnet( struct subnet_record *subrec,
105 struct nmb_name *nmbname,
106 BOOL self_only )
108 struct nmb_name uc_name[1];
109 struct name_record *name_ret;
111 upcase_name( uc_name, nmbname );
112 name_ret = (struct name_record *)ubi_trFind( subrec->namelist, uc_name );
113 if( name_ret )
115 /* Self names only - these include permanent names. */
116 if( self_only
117 && (name_ret->data.source != SELF_NAME)
118 && (name_ret->data.source != PERMANENT_NAME) )
120 DEBUG( 9,
121 ( "find_name_on_subnet: on subnet %s - self name %s NOT FOUND\n",
122 subrec->subnet_name, nmb_namestr(nmbname) ) );
123 return( NULL );
125 DEBUG( 9, ("find_name_on_subnet: on subnet %s - found name %s source=%d\n",
126 subrec->subnet_name, nmb_namestr(nmbname), name_ret->data.source) );
127 return( name_ret );
129 DEBUG( 9,
130 ( "find_name_on_subnet: on subnet %s - name %s NOT FOUND\n",
131 subrec->subnet_name, nmb_namestr(nmbname) ) );
132 return( NULL );
133 } /* find_name_on_subnet */
135 /* ************************************************************************** **
136 * Find a name over all known broadcast subnets.
137 * ************************************************************************** **
139 struct name_record *find_name_for_remote_broadcast_subnet(
140 struct nmb_name *nmbname,
141 BOOL self_only )
143 struct subnet_record *subrec;
144 struct name_record *namerec = NULL;
146 for( subrec = FIRST_SUBNET;
147 subrec;
148 subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec) )
150 if( NULL != (namerec = find_name_on_subnet(subrec, nmbname, self_only)) )
151 break;
154 return( namerec );
155 } /* find_name_for_remote_broadcast_subnet */
157 /* ************************************************************************** **
158 * Update the ttl of an entry in a subnet name list.
159 * ************************************************************************** **
161 void update_name_ttl( struct name_record *namerec, int ttl )
163 time_t time_now = time(NULL);
165 if( namerec->data.death_time != PERMANENT_TTL )
166 namerec->data.death_time = time_now + ttl;
168 namerec->data.refresh_time = time_now + MIN((ttl/2), MAX_REFRESH_TIME);
170 namerec->subnet->namelist_changed = True;
171 } /* update_name_ttl */
173 /* ************************************************************************** **
174 * Add an entry to a subnet name list.
175 * ************************************************************************** **
177 struct name_record *add_name_to_subnet( struct subnet_record *subrec,
178 char *name,
179 int type,
180 uint16 nb_flags,
181 int ttl,
182 enum name_source source,
183 int num_ips,
184 struct in_addr *iplist)
186 struct name_record *namerec;
187 time_t time_now = time(NULL);
189 namerec = (struct name_record *)malloc( sizeof(*namerec) );
190 if( NULL == namerec )
192 DEBUG( 0, ( "add_name_to_subnet: malloc fail.\n" ) );
193 return( NULL );
196 memset( (char *)namerec, '\0', sizeof(*namerec) );
197 namerec->data.ip = (struct in_addr *)malloc( sizeof(struct in_addr)
198 * num_ips );
199 if( NULL == namerec->data.ip )
201 DEBUG( 0, ( "add_name_to_subnet: malloc fail when creating ip_flgs.\n" ) );
203 ZERO_STRUCTP(namerec);
204 SAFE_FREE(namerec);
205 return NULL;
208 namerec->subnet = subrec;
210 make_nmb_name(&namerec->name, name, type);
211 upcase_name(&namerec->name, NULL );
213 /* Enter the name as active. */
214 namerec->data.nb_flags = nb_flags | NB_ACTIVE;
215 namerec->data.wins_flags = WINS_ACTIVE;
217 /* If it's our primary name, flag it as so. */
218 if( strequal( my_netbios_names[0], name ) )
219 namerec->data.nb_flags |= NB_PERM;
221 /* Copy the IPs. */
222 namerec->data.num_ips = num_ips;
223 memcpy( (namerec->data.ip), iplist, num_ips * sizeof(struct in_addr) );
225 /* Data source. */
226 namerec->data.source = source;
228 /* Setup the death_time and refresh_time. */
229 if( ttl == PERMANENT_TTL )
230 namerec->data.death_time = PERMANENT_TTL;
231 else
232 namerec->data.death_time = time_now + ttl;
234 namerec->data.refresh_time = time_now + MIN((ttl/2), MAX_REFRESH_TIME);
236 /* Now add the record to the name list. */
237 update_name_in_namelist( subrec, namerec );
239 DEBUG( 3, ( "add_name_to_subnet: Added netbios name %s with first IP %s \
240 ttl=%d nb_flags=%2x to subnet %s\n",
241 nmb_namestr( &namerec->name ),
242 inet_ntoa( *iplist ),
243 ttl,
244 (unsigned int)nb_flags,
245 subrec->subnet_name ) );
247 subrec->namelist_changed = True;
249 return(namerec);
252 /*******************************************************************
253 Utility function automatically called when a name refresh or register
254 succeeds. By definition this is a SELF_NAME (or we wouldn't be registering
255 it).
256 ******************************************************************/
258 void standard_success_register(struct subnet_record *subrec,
259 struct userdata_struct *userdata,
260 struct nmb_name *nmbname, uint16 nb_flags, int ttl,
261 struct in_addr registered_ip)
263 struct name_record *namerec;
265 namerec = find_name_on_subnet( subrec, nmbname, FIND_SELF_NAME );
266 if( NULL == namerec )
267 (void)add_name_to_subnet( subrec, nmbname->name, nmbname->name_type,
268 nb_flags, ttl, SELF_NAME, 1, &registered_ip );
269 else
270 update_name_ttl( namerec, ttl );
273 /*******************************************************************
274 Utility function automatically called when a name refresh or register
275 fails. Note that this is only ever called on a broadcast subnet with
276 one IP address per name. This is why it can just delete the name
277 without enumerating the IP adresses. JRA.
278 ******************************************************************/
280 void standard_fail_register( struct subnet_record *subrec,
281 struct response_record *rrec,
282 struct nmb_name *nmbname )
284 struct name_record *namerec;
286 namerec = find_name_on_subnet( subrec, nmbname, FIND_SELF_NAME );
288 DEBUG( 0, ( "standard_fail_register: Failed to register/refresh name %s \
289 on subnet %s\n",
290 nmb_namestr(nmbname), subrec->subnet_name) );
292 /* Remove the name from the subnet. */
293 if( namerec )
294 remove_name_from_namelist(subrec, namerec);
297 /*******************************************************************
298 Utility function to remove an IP address from a name record.
299 ******************************************************************/
301 static void remove_nth_ip_in_record( struct name_record *namerec, int ind)
303 if( ind != namerec->data.num_ips )
304 memmove( (char *)(&namerec->data.ip[ind]),
305 (char *)(&namerec->data.ip[ind+1]),
306 ( namerec->data.num_ips - ind - 1) * sizeof(struct in_addr) );
308 namerec->data.num_ips--;
309 namerec->subnet->namelist_changed = True;
312 /*******************************************************************
313 Utility function to check if an IP address exists in a name record.
314 ******************************************************************/
316 BOOL find_ip_in_name_record( struct name_record *namerec, struct in_addr ip )
318 int i;
320 for(i = 0; i < namerec->data.num_ips; i++)
321 if(ip_equal( namerec->data.ip[i], ip))
322 return True;
324 return False;
327 /*******************************************************************
328 Utility function to add an IP address to a name record.
329 ******************************************************************/
331 void add_ip_to_name_record( struct name_record *namerec, struct in_addr new_ip )
333 struct in_addr *new_list;
335 /* Don't add one we already have. */
336 if( find_ip_in_name_record( namerec, new_ip ) )
337 return;
339 new_list = (struct in_addr *)malloc( (namerec->data.num_ips + 1)
340 * sizeof(struct in_addr) );
341 if( NULL == new_list )
343 DEBUG(0,("add_ip_to_name_record: Malloc fail !\n"));
344 return;
347 memcpy( (char *)new_list,
348 (char *)namerec->data.ip,
349 namerec->data.num_ips * sizeof(struct in_addr) );
350 new_list[namerec->data.num_ips] = new_ip;
352 SAFE_FREE(namerec->data.ip);
353 namerec->data.ip = new_list;
354 namerec->data.num_ips += 1;
356 namerec->subnet->namelist_changed = True;
359 /*******************************************************************
360 Utility function to remove an IP address from a name record.
361 ******************************************************************/
363 void remove_ip_from_name_record( struct name_record *namerec,
364 struct in_addr remove_ip )
366 /* Try and find the requested ip address - remove it. */
367 int i;
368 int orig_num = namerec->data.num_ips;
370 for(i = 0; i < orig_num; i++)
371 if( ip_equal( remove_ip, namerec->data.ip[i]) )
373 remove_nth_ip_in_record( namerec, i);
374 break;
378 /*******************************************************************
379 Utility function that release_name callers can plug into as the
380 success function when a name release is successful. Used to save
381 duplication of success_function code.
382 ******************************************************************/
384 void standard_success_release( struct subnet_record *subrec,
385 struct userdata_struct *userdata,
386 struct nmb_name *nmbname,
387 struct in_addr released_ip )
389 struct name_record *namerec;
391 namerec = find_name_on_subnet( subrec, nmbname, FIND_ANY_NAME );
393 if( namerec == NULL )
395 DEBUG( 0, ( "standard_success_release: Name release for name %s IP %s \
396 on subnet %s. Name was not found on subnet.\n",
397 nmb_namestr(nmbname),
398 inet_ntoa(released_ip),
399 subrec->subnet_name) );
400 return;
402 else
404 int orig_num = namerec->data.num_ips;
406 remove_ip_from_name_record( namerec, released_ip );
408 if( namerec->data.num_ips == orig_num )
409 DEBUG( 0, ( "standard_success_release: Name release for name %s IP %s \
410 on subnet %s. This ip is not known for this name.\n",
411 nmb_namestr(nmbname),
412 inet_ntoa(released_ip),
413 subrec->subnet_name ) );
416 if( namerec->data.num_ips == 0 )
417 remove_name_from_namelist( subrec, namerec );
420 /*******************************************************************
421 Expires old names in a subnet namelist.
422 ******************************************************************/
424 void expire_names_on_subnet(struct subnet_record *subrec, time_t t)
426 struct name_record *namerec;
427 struct name_record *next_namerec;
429 for( namerec = (struct name_record *)ubi_trFirst( subrec->namelist );
430 namerec;
431 namerec = next_namerec )
433 next_namerec = (struct name_record *)ubi_trNext( namerec );
434 if( (namerec->data.death_time != PERMANENT_TTL)
435 && (namerec->data.death_time < t) )
437 if( namerec->data.source == SELF_NAME )
439 DEBUG( 3, ( "expire_names_on_subnet: Subnet %s not expiring SELF \
440 name %s\n",
441 subrec->subnet_name, nmb_namestr(&namerec->name) ) );
442 namerec->data.death_time += 300;
443 namerec->subnet->namelist_changed = True;
444 continue;
446 DEBUG(3,("expire_names_on_subnet: Subnet %s - removing expired name %s\n",
447 subrec->subnet_name, nmb_namestr(&namerec->name)));
449 remove_name_from_namelist( subrec, namerec );
454 /*******************************************************************
455 Expires old names in all subnet namelists.
456 ******************************************************************/
458 void expire_names(time_t t)
460 struct subnet_record *subrec;
462 for( subrec = FIRST_SUBNET;
463 subrec;
464 subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec) )
466 expire_names_on_subnet( subrec, t );
470 /****************************************************************************
471 Add the magic samba names, useful for finding samba servers.
472 These go directly into the name list for a particular subnet,
473 without going through the normal registration process.
474 When adding them to the unicast subnet, add them as a list of
475 all broadcast subnet IP addresses.
476 **************************************************************************/
478 void add_samba_names_to_subnet( struct subnet_record *subrec )
480 struct in_addr *iplist = &subrec->myip;
481 int num_ips = 1;
483 /* These names are added permanently (ttl of zero) and will NOT be
484 refreshed. */
486 if( (subrec == unicast_subnet)
487 || (subrec == wins_server_subnet)
488 || (subrec == remote_broadcast_subnet) )
490 struct subnet_record *bcast_subrecs;
491 int i;
492 /* Create an IP list containing all our known subnets. */
494 num_ips = iface_count();
495 iplist = (struct in_addr *)malloc( num_ips * sizeof(struct in_addr) );
496 if( NULL == iplist )
498 DEBUG(0,("add_samba_names_to_subnet: Malloc fail !\n"));
499 return;
502 for( bcast_subrecs = FIRST_SUBNET, i = 0;
503 bcast_subrecs;
504 bcast_subrecs = NEXT_SUBNET_EXCLUDING_UNICAST(bcast_subrecs), i++ )
505 iplist[i] = bcast_subrecs->myip;
509 (void)add_name_to_subnet(subrec,"*",0x0,samba_nb_type, PERMANENT_TTL,
510 PERMANENT_NAME, num_ips, iplist);
511 (void)add_name_to_subnet(subrec,"*",0x20,samba_nb_type,PERMANENT_TTL,
512 PERMANENT_NAME, num_ips, iplist);
513 (void)add_name_to_subnet(subrec,"__SAMBA__",0x20,samba_nb_type,PERMANENT_TTL,
514 PERMANENT_NAME, num_ips, iplist);
515 (void)add_name_to_subnet(subrec,"__SAMBA__",0x00,samba_nb_type,PERMANENT_TTL,
516 PERMANENT_NAME, num_ips, iplist);
518 if(iplist != &subrec->myip)
519 SAFE_FREE(iplist);
522 /****************************************************************************
523 Dump the contents of the namelists on all the subnets (including unicast)
524 into a file. Initiated by SIGHUP - used to debug the state of the namelists.
525 **************************************************************************/
527 static void dump_subnet_namelist( struct subnet_record *subrec, XFILE *fp)
529 struct name_record *namerec;
530 char *src_type;
531 struct tm *tm;
532 int i;
534 x_fprintf(fp, "Subnet %s\n----------------------\n", subrec->subnet_name);
535 for( namerec = (struct name_record *)ubi_trFirst( subrec->namelist );
536 namerec;
537 namerec = (struct name_record *)ubi_trNext( namerec ) )
539 x_fprintf(fp,"\tName = %s\t", nmb_namestr(&namerec->name));
540 switch(namerec->data.source)
542 case LMHOSTS_NAME:
543 src_type = "LMHOSTS_NAME";
544 break;
545 case WINS_PROXY_NAME:
546 src_type = "WINS_PROXY_NAME";
547 break;
548 case REGISTER_NAME:
549 src_type = "REGISTER_NAME";
550 break;
551 case SELF_NAME:
552 src_type = "SELF_NAME";
553 break;
554 case DNS_NAME:
555 src_type = "DNS_NAME";
556 break;
557 case DNSFAIL_NAME:
558 src_type = "DNSFAIL_NAME";
559 break;
560 case PERMANENT_NAME:
561 src_type = "PERMANENT_NAME";
562 break;
563 default:
564 src_type = "unknown!";
565 break;
567 x_fprintf(fp,"Source = %s\nb_flags = %x\t", src_type, namerec->data.nb_flags);
569 if(namerec->data.death_time != PERMANENT_TTL)
571 tm = LocalTime(&namerec->data.death_time);
572 x_fprintf(fp, "death_time = %s\t", asctime(tm));
574 else
575 x_fprintf(fp, "death_time = PERMANENT\t");
577 if(namerec->data.refresh_time != PERMANENT_TTL)
579 tm = LocalTime(&namerec->data.refresh_time);
580 x_fprintf(fp, "refresh_time = %s\n", asctime(tm));
582 else
583 x_fprintf(fp, "refresh_time = PERMANENT\n");
585 x_fprintf(fp, "\t\tnumber of IPS = %d", namerec->data.num_ips);
586 for(i = 0; i < namerec->data.num_ips; i++)
587 x_fprintf(fp, "\t%s", inet_ntoa(namerec->data.ip[i]));
589 x_fprintf(fp, "\n\n");
593 /****************************************************************************
594 Dump the contents of the namelists on all the subnets (including unicast)
595 into a file. Initiated by SIGHUP - used to debug the state of the namelists.
596 **************************************************************************/
598 void dump_all_namelists(void)
600 XFILE *fp;
601 struct subnet_record *subrec;
603 fp = x_fopen(lock_path("namelist.debug"),O_WRONLY|O_CREAT|O_TRUNC, 0644);
605 if (!fp)
607 DEBUG(0,("dump_all_namelists: Can't open file %s. Error was %s\n",
608 "namelist.debug",strerror(errno)));
609 return;
612 for( subrec = FIRST_SUBNET;
613 subrec;
614 subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec) )
615 dump_subnet_namelist( subrec, fp );
617 if( !we_are_a_wins_client() )
618 dump_subnet_namelist( unicast_subnet, fp );
620 if( remote_broadcast_subnet->namelist != NULL )
621 dump_subnet_namelist( remote_broadcast_subnet, fp );
623 if( wins_server_subnet != NULL )
624 dump_subnet_namelist( wins_server_subnet, fp );
625 x_fclose( fp );