1/* 2 Unix SMB/Netbios implementation. 3 Version 1.9. 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 8 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. 13 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. 18 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. 22 23*/ 24 25#include "includes.h" 26 27extern int DEBUGLEVEL; 28 29extern char **my_netbios_names; 30 31uint16 samba_nb_type = 0; /* samba's NetBIOS name type */ 32 33 34/* ************************************************************************** ** 35 * Set Samba's NetBIOS name type. 36 * ************************************************************************** ** 37 */ 38void set_samba_nb_type(void) 39 { 40 if( lp_wins_support() || (*lp_wins_server()) ) 41 samba_nb_type = NB_MFLAG; /* samba is a 'hybrid' node type. */ 42 else 43 samba_nb_type = NB_BFLAG; /* samba is broadcast-only node type. */ 44 } /* set_samba_nb_type */ 45 46/* ************************************************************************** ** 47 * Convert a NetBIOS name to upper case. 48 * ************************************************************************** ** 49 */ 50static void upcase_name( struct nmb_name *target, struct nmb_name *source ) 51 { 52 int i; 53 54 if( NULL != source ) 55 (void)memcpy( target, source, sizeof( struct nmb_name ) ); 56 57 strupper( target->name ); 58 strupper( target->scope ); 59 60 /* fudge... We're using a byte-by-byte compare, so we must be sure that 61 * unused space doesn't have garbage in it. 62 */ 63 for( i = strlen( target->name ); i < sizeof( target->name ); i++ ) 64 target->name[i] = '\0'; 65 for( i = strlen( target->scope ); i < sizeof( target->scope ); i++ ) 66 target->scope[i] = '\0'; 67 } /* upcase_name */ 68 69/* ************************************************************************** ** 70 * Add a new or overwrite an existing namelist entry. 71 * ************************************************************************** ** 72 */ 73static void update_name_in_namelist( struct subnet_record *subrec, 74 struct name_record *namerec ) 75 { 76 struct name_record *oldrec = NULL; 77 78 (void)ubi_trInsert( subrec->namelist, namerec, &(namerec->name), &oldrec ); 79 if( oldrec ) 80 { 81 if( oldrec->data.ip ) 82 free( oldrec->data.ip ); 83 free( oldrec ); 84 } 85 } /* update_name_in_namelist */ 86 87/* ************************************************************************** ** 88 * Remove a name from the namelist. 89 * ************************************************************************** ** 90 */ 91void remove_name_from_namelist( struct subnet_record *subrec, 92 struct name_record *namerec ) 93 { 94 (void)ubi_trRemove( subrec->namelist, namerec ); 95 96 if(namerec->data.ip != NULL) 97 free((char *)namerec->data.ip); 98 99 ZERO_STRUCTP(namerec); 100 free((char *)namerec); 101 102 subrec->namelist_changed = True; 103 } /* remove_name_from_namelist */ 104 105/* ************************************************************************** ** 106 * Find a name in a subnet. 107 * ************************************************************************** ** 108 */ 109struct name_record *find_name_on_subnet( struct subnet_record *subrec, 110 struct nmb_name *nmbname, 111 BOOL self_only ) 112 { 113 struct nmb_name uc_name[1]; 114 struct name_record *name_ret; 115 116 upcase_name( uc_name, nmbname ); 117 name_ret = (struct name_record *)ubi_trFind( subrec->namelist, uc_name ); 118 if( name_ret ) 119 { 120 /* Self names only - these include permanent names. */ 121 if( self_only 122 && (name_ret->data.source != SELF_NAME) 123 && (name_ret->data.source != PERMANENT_NAME) ) 124 { 125 DEBUG( 9, 126 ( "find_name_on_subnet: on subnet %s - self name %s NOT FOUND\n", 127 subrec->subnet_name, nmb_namestr(nmbname) ) ); 128 return( NULL ); 129 } 130 DEBUG( 9, ("find_name_on_subnet: on subnet %s - found name %s source=%d\n", 131 subrec->subnet_name, nmb_namestr(nmbname), name_ret->data.source) ); 132 return( name_ret ); 133 } 134 DEBUG( 9, 135 ( "find_name_on_subnet: on subnet %s - name %s NOT FOUND\n", 136 subrec->subnet_name, nmb_namestr(nmbname) ) ); 137 return( NULL ); 138 } /* find_name_on_subnet */ 139 140/* ************************************************************************** ** 141 * Find a name over all known broadcast subnets. 142 * ************************************************************************** ** 143 */ 144struct name_record *find_name_for_remote_broadcast_subnet( 145 struct nmb_name *nmbname, 146 BOOL self_only ) 147 { 148 struct subnet_record *subrec; 149 struct name_record *namerec = NULL; 150 151 for( subrec = FIRST_SUBNET; 152 subrec; 153 subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec) ) 154 { 155 if( NULL != (namerec = find_name_on_subnet(subrec, nmbname, self_only)) ) 156 break; 157 } 158 159 return( namerec ); 160 } /* find_name_for_remote_broadcast_subnet */ 161 162/* ************************************************************************** ** 163 * Update the ttl of an entry in a subnet name list. 164 * ************************************************************************** ** 165 */ 166void update_name_ttl( struct name_record *namerec, int ttl ) 167{ 168 time_t time_now = time(NULL); 169 170 if( namerec->data.death_time != PERMANENT_TTL ) 171 namerec->data.death_time = time_now + ttl; 172 173 namerec->data.refresh_time = time_now + MIN((ttl/2), MAX_REFRESH_TIME); 174 175 namerec->subnet->namelist_changed = True; 176} /* update_name_ttl */ 177 178/* ************************************************************************** ** 179 * Add an entry to a subnet name list. 180 * ************************************************************************** ** 181 */ 182struct name_record *add_name_to_subnet( struct subnet_record *subrec, 183 char *name, 184 int type, 185 uint16 nb_flags, 186 int ttl, 187 enum name_source source, 188 int num_ips, 189 struct in_addr *iplist) 190{ 191 struct name_record *namerec; 192 time_t time_now = time(NULL); 193 194 namerec = (struct name_record *)malloc( sizeof(*namerec) ); 195 if( NULL == namerec ) 196 { 197 DEBUG( 0, ( "add_name_to_subnet: malloc fail.\n" ) ); 198 return( NULL ); 199 } 200 201 memset( (char *)namerec, '\0', sizeof(*namerec) ); 202 namerec->data.ip = (struct in_addr *)malloc( sizeof(struct in_addr) 203 * num_ips ); 204 if( NULL == namerec->data.ip ) 205 { 206 DEBUG( 0, ( "add_name_to_subnet: malloc fail when creating ip_flgs.\n" ) ); 207 208 ZERO_STRUCTP(namerec); 209 free( (char *)namerec ); 210 return NULL; 211 } 212 213 namerec->subnet = subrec; 214 215 make_nmb_name( &namerec->name, name, type ); 216 upcase_name( &namerec->name, NULL ); 217 218 /* Enter the name as active. */ 219 namerec->data.nb_flags = nb_flags | NB_ACTIVE; 220 221 /* If it's our primary name, flag it as so. */ 222 if( strequal( my_netbios_names[0], name ) ) 223 namerec->data.nb_flags |= NB_PERM; 224 225 /* Copy the IPs. */ 226 namerec->data.num_ips = num_ips; 227 memcpy( (namerec->data.ip), iplist, num_ips * sizeof(struct in_addr) ); 228 229 /* Data source. */ 230 namerec->data.source = source; 231 232 /* Setup the death_time and refresh_time. */ 233 if( ttl == PERMANENT_TTL ) 234 namerec->data.death_time = PERMANENT_TTL; 235 else 236 namerec->data.death_time = time_now + ttl; 237 238 namerec->data.refresh_time = time_now + MIN((ttl/2), MAX_REFRESH_TIME); 239 240 /* Now add the record to the name list. */ 241 update_name_in_namelist( subrec, namerec ); 242 243 DEBUG( 3, ( "add_name_to_subnet: Added netbios name %s with first IP %s \ 244ttl=%d nb_flags=%2x to subnet %s\n", 245 nmb_namestr( &namerec->name ), 246 inet_ntoa( *iplist ), 247 ttl, 248 (unsigned int)nb_flags, 249 subrec->subnet_name ) ); 250 251 subrec->namelist_changed = True; 252 253 return(namerec); 254} 255 256/******************************************************************* 257 Utility function automatically called when a name refresh or register 258 succeeds. By definition this is a SELF_NAME (or we wouldn't be registering 259 it). 260 ******************************************************************/ 261 262void standard_success_register(struct subnet_record *subrec, 263 struct userdata_struct *userdata, 264 struct nmb_name *nmbname, uint16 nb_flags, int ttl, 265 struct in_addr registered_ip) 266{ 267 struct name_record *namerec; 268 269 namerec = find_name_on_subnet( subrec, nmbname, FIND_SELF_NAME ); 270 if( NULL == namerec ) 271 (void)add_name_to_subnet( subrec, nmbname->name, nmbname->name_type, 272 nb_flags, ttl, SELF_NAME, 1, ®istered_ip ); 273 else 274 update_name_ttl( namerec, ttl ); 275} 276 277/******************************************************************* 278 Utility function automatically called when a name refresh or register 279 fails. Note that this is only ever called on a broadcast subnet with 280 one IP address per name. This is why it can just delete the name 281 without enumerating the IP adresses. JRA. 282 ******************************************************************/ 283 284void standard_fail_register( struct subnet_record *subrec, 285 struct response_record *rrec, 286 struct nmb_name *nmbname ) 287{ 288 struct name_record *namerec; 289 290 namerec = find_name_on_subnet( subrec, nmbname, FIND_SELF_NAME ); 291 292 DEBUG( 0, ( "standard_fail_register: Failed to register/refresh name %s \ 293on subnet %s\n", 294 nmb_namestr(nmbname), subrec->subnet_name) ); 295 296 /* Remove the name from the subnet. */ 297 if( namerec ) 298 remove_name_from_namelist(subrec, namerec); 299} 300 301/******************************************************************* 302 Utility function to remove an IP address from a name record. 303 ******************************************************************/ 304 305static void remove_nth_ip_in_record( struct name_record *namerec, int ind) 306{ 307 if( ind != namerec->data.num_ips ) 308 memmove( (char *)(&namerec->data.ip[ind]), 309 (char *)(&namerec->data.ip[ind+1]), 310 ( namerec->data.num_ips - ind - 1) * sizeof(struct in_addr) ); 311 312 namerec->data.num_ips--; 313 namerec->subnet->namelist_changed = True; 314} 315 316/******************************************************************* 317 Utility function to check if an IP address exists in a name record. 318 ******************************************************************/ 319 320BOOL find_ip_in_name_record( struct name_record *namerec, struct in_addr ip ) 321{ 322 int i; 323 324 for(i = 0; i < namerec->data.num_ips; i++) 325 if(ip_equal( namerec->data.ip[i], ip)) 326 return True; 327 328 return False; 329} 330 331/******************************************************************* 332 Utility function to add an IP address to a name record. 333 ******************************************************************/ 334 335void add_ip_to_name_record( struct name_record *namerec, struct in_addr new_ip ) 336{ 337 struct in_addr *new_list; 338 339 /* Don't add one we already have. */ 340 if( find_ip_in_name_record( namerec, new_ip ) ) 341 return; 342 343 new_list = (struct in_addr *)malloc( (namerec->data.num_ips + 1) 344 * sizeof(struct in_addr) ); 345 if( NULL == new_list ) 346 { 347 DEBUG(0,("add_ip_to_name_record: Malloc fail !\n")); 348 return; 349 } 350 351 memcpy( (char *)new_list, 352 (char *)namerec->data.ip, 353 namerec->data.num_ips * sizeof(struct in_addr) ); 354 new_list[namerec->data.num_ips] = new_ip; 355 356 free((char *)namerec->data.ip); 357 namerec->data.ip = new_list; 358 namerec->data.num_ips += 1; 359 360 namerec->subnet->namelist_changed = True; 361} 362 363/******************************************************************* 364 Utility function to remove an IP address from a name record. 365 ******************************************************************/ 366 367void remove_ip_from_name_record( struct name_record *namerec, 368 struct in_addr remove_ip ) 369{ 370 /* Try and find the requested ip address - remove it. */ 371 int i; 372 int orig_num = namerec->data.num_ips; 373 374 for(i = 0; i < orig_num; i++) 375 if( ip_equal( remove_ip, namerec->data.ip[i]) ) 376 { 377 remove_nth_ip_in_record( namerec, i); 378 break; 379 } 380} 381 382/******************************************************************* 383 Utility function that release_name callers can plug into as the 384 success function when a name release is successful. Used to save 385 duplication of success_function code. 386 ******************************************************************/ 387 388void standard_success_release( struct subnet_record *subrec, 389 struct userdata_struct *userdata, 390 struct nmb_name *nmbname, 391 struct in_addr released_ip ) 392{ 393 struct name_record *namerec; 394 395 namerec = find_name_on_subnet( subrec, nmbname, FIND_ANY_NAME ); 396 397 if( namerec == NULL ) 398 { 399 DEBUG( 0, ( "standard_success_release: Name release for name %s IP %s \ 400on subnet %s. Name was not found on subnet.\n", 401 nmb_namestr(nmbname), 402 inet_ntoa(released_ip), 403 subrec->subnet_name) ); 404 return; 405 } 406 else 407 { 408 int orig_num = namerec->data.num_ips; 409 410 remove_ip_from_name_record( namerec, released_ip ); 411 412 if( namerec->data.num_ips == orig_num ) 413 DEBUG( 0, ( "standard_success_release: Name release for name %s IP %s \ 414on subnet %s. This ip is not known for this name.\n", 415 nmb_namestr(nmbname), 416 inet_ntoa(released_ip), 417 subrec->subnet_name ) ); 418 } 419 420 if( namerec->data.num_ips == 0 ) 421 remove_name_from_namelist( subrec, namerec ); 422} 423 424/******************************************************************* 425 Expires old names in a subnet namelist. 426 ******************************************************************/ 427 428void expire_names_on_subnet(struct subnet_record *subrec, time_t t) 429{ 430 struct name_record *namerec; 431 struct name_record *next_namerec; 432 433 for( namerec = (struct name_record *)ubi_trFirst( subrec->namelist ); 434 namerec; 435 namerec = next_namerec ) 436 { 437 next_namerec = (struct name_record *)ubi_trNext( namerec ); 438 if( (namerec->data.death_time != PERMANENT_TTL) 439 && (namerec->data.death_time < t) ) 440 { 441 if( namerec->data.source == SELF_NAME ) 442 { 443 DEBUG( 3, ( "expire_names_on_subnet: Subnet %s not expiring SELF \ 444name %s\n", 445 subrec->subnet_name, nmb_namestr(&namerec->name) ) ); 446 namerec->data.death_time += 300; 447 namerec->subnet->namelist_changed = True; 448 continue; 449 } 450 DEBUG(3,("expire_names_on_subnet: Subnet %s - removing expired name %s\n", 451 subrec->subnet_name, nmb_namestr(&namerec->name))); 452 453 remove_name_from_namelist( subrec, namerec ); 454 } 455 } 456} 457 458/******************************************************************* 459 Expires old names in all subnet namelists. 460 ******************************************************************/ 461 462void expire_names(time_t t) 463{ 464 struct subnet_record *subrec; 465 466 for( subrec = FIRST_SUBNET; 467 subrec; 468 subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec) ) 469 { 470 expire_names_on_subnet( subrec, t ); 471 } 472} 473 474/**************************************************************************** 475 Add the magic samba names, useful for finding samba servers. 476 These go directly into the name list for a particular subnet, 477 without going through the normal registration process. 478 When adding them to the unicast subnet, add them as a list of 479 all broadcast subnet IP addresses. 480**************************************************************************/ 481 482void add_samba_names_to_subnet( struct subnet_record *subrec ) 483{ 484 struct in_addr *iplist = &subrec->myip; 485 int num_ips = 1; 486 487 /* These names are added permanently (ttl of zero) and will NOT be 488 refreshed. */ 489 490 if( (subrec == unicast_subnet) 491 || (subrec == wins_server_subnet) 492 || (subrec == remote_broadcast_subnet) ) 493 { 494 struct subnet_record *bcast_subrecs; 495 int i; 496 /* Create an IP list containing all our known subnets. */ 497 498 num_ips = iface_count(); 499 iplist = (struct in_addr *)malloc( num_ips * sizeof(struct in_addr) ); 500 if( NULL == iplist ) 501 { 502 DEBUG(0,("add_samba_names_to_subnet: Malloc fail !\n")); 503 return; 504 } 505 506 for( bcast_subrecs = FIRST_SUBNET, i = 0; 507 bcast_subrecs; 508 bcast_subrecs = NEXT_SUBNET_EXCLUDING_UNICAST(bcast_subrecs), i++ ) 509 iplist[i] = bcast_subrecs->myip; 510 511 } 512 513 (void)add_name_to_subnet(subrec,"*",0x0,samba_nb_type, PERMANENT_TTL, 514 PERMANENT_NAME, num_ips, iplist); 515 (void)add_name_to_subnet(subrec,"*",0x20,samba_nb_type,PERMANENT_TTL, 516 PERMANENT_NAME, num_ips, iplist); 517 (void)add_name_to_subnet(subrec,"__SAMBA__",0x20,samba_nb_type,PERMANENT_TTL, 518 PERMANENT_NAME, num_ips, iplist); 519 (void)add_name_to_subnet(subrec,"__SAMBA__",0x00,samba_nb_type,PERMANENT_TTL, 520 PERMANENT_NAME, num_ips, iplist); 521 522 if(iplist != &subrec->myip) 523 free((char *)iplist); 524} 525 526/**************************************************************************** 527 Dump the contents of the namelists on all the subnets (including unicast) 528 into a file. Initiated by SIGHUP - used to debug the state of the namelists. 529**************************************************************************/ 530 531static void dump_subnet_namelist( struct subnet_record *subrec, FILE *fp) 532{ 533 struct name_record *namerec; 534 char *src_type; 535 struct tm *tm; 536 int i; 537 538 fprintf(fp, "Subnet %s\n----------------------\n", subrec->subnet_name); 539 for( namerec = (struct name_record *)ubi_trFirst( subrec->namelist ); 540 namerec; 541 namerec = (struct name_record *)ubi_trNext( namerec ) ) 542 { 543 fprintf(fp,"\tName = %s\t", nmb_namestr(&namerec->name)); 544 switch(namerec->data.source) 545 { 546 case LMHOSTS_NAME: 547 src_type = "LMHOSTS_NAME"; 548 break; 549 case WINS_PROXY_NAME: 550 src_type = "WINS_PROXY_NAME"; 551 break; 552 case REGISTER_NAME: 553 src_type = "REGISTER_NAME"; 554 break; 555 case SELF_NAME: 556 src_type = "SELF_NAME"; 557 break; 558 case DNS_NAME: 559 src_type = "DNS_NAME"; 560 break; 561 case DNSFAIL_NAME: 562 src_type = "DNSFAIL_NAME"; 563 break; 564 case PERMANENT_NAME: 565 src_type = "PERMANENT_NAME"; 566 break; 567 default: 568 src_type = "unknown!"; 569 break; 570 } 571 fprintf(fp,"Source = %s\nb_flags = %x\t", src_type, namerec->data.nb_flags); 572 573 if(namerec->data.death_time != PERMANENT_TTL) 574 { 575 tm = LocalTime(&namerec->data.death_time); 576 fprintf(fp, "death_time = %s\t", asctime(tm)); 577 } 578 else 579 fprintf(fp, "death_time = PERMANENT\t"); 580 581 if(namerec->data.refresh_time != PERMANENT_TTL) 582 { 583 tm = LocalTime(&namerec->data.refresh_time); 584 fprintf(fp, "refresh_time = %s\n", asctime(tm)); 585 } 586 else 587 fprintf(fp, "refresh_time = PERMANENT\n"); 588 589 fprintf(fp, "\t\tnumber of IPS = %d", namerec->data.num_ips); 590 for(i = 0; i < namerec->data.num_ips; i++) 591 fprintf(fp, "\t%s", inet_ntoa(namerec->data.ip[i])); 592 593 fprintf(fp, "\n\n"); 594 } 595} 596 597/**************************************************************************** 598 Dump the contents of the namelists on all the subnets (including unicast) 599 into a file. Initiated by SIGHUP - used to debug the state of the namelists. 600**************************************************************************/ 601 602void dump_all_namelists(void) 603{ 604 pstring fname; 605 FILE *fp; 606 struct subnet_record *subrec; 607 608 pstrcpy(fname,lp_lockdir()); 609 trim_string(fname,NULL,"/"); 610 pstrcat(fname,"/"); 611 pstrcat(fname,"namelist.debug"); 612 613 fp = sys_fopen(fname,"w"); 614 615 if (!fp) 616 { 617 DEBUG(0,("dump_all_namelists: Can't open file %s. Error was %s\n", 618 fname,strerror(errno))); 619 return; 620 } 621 622 for( subrec = FIRST_SUBNET; 623 subrec; 624 subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec) ) 625 dump_subnet_namelist( subrec, fp ); 626 627 if( !we_are_a_wins_client() ) 628 dump_subnet_namelist( unicast_subnet, fp ); 629 630 if( remote_broadcast_subnet->namelist != NULL ) 631 dump_subnet_namelist( remote_broadcast_subnet, fp ); 632 633 if( wins_server_subnet != NULL ) 634 dump_subnet_namelist( wins_server_subnet, fp ); 635 fclose( fp ); 636} 637