1997-12-13 17:16:07 +03:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
1997-12-13 17:16:07 +03:00
NBT netbios routines and daemon - version 2
1998-01-22 16:27:43 +03:00
Copyright ( C ) Jeremy Allison 1994 - 1998
1997-12-13 17:16:07 +03:00
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
2007-07-09 23:25:36 +04:00
the Free Software Foundation ; either version 3 of the License , or
1997-12-13 17:16:07 +03:00
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
2007-07-10 04:52:41 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
1997-12-13 17:16:07 +03:00
Revision History :
Handle lmhosts file reading .
*/
# include "includes.h"
2010-05-18 21:40:31 +04:00
# include "../libcli/nbt/libnbt.h"
2010-08-18 17:22:09 +04:00
# include "nmbd/nmbd.h"
1997-12-13 17:16:07 +03:00
/****************************************************************************
Load a lmhosts file .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-27 05:25:01 +04:00
2007-04-20 22:34:33 +04:00
void load_lmhosts_file ( const char * fname )
2007-11-20 02:15:09 +03:00
{
char * name = NULL ;
2003-08-27 05:25:01 +04:00
int name_type ;
2007-10-25 01:16:54 +04:00
struct sockaddr_storage ss ;
2007-11-20 02:15:09 +03:00
TALLOC_CTX * ctx = talloc_init ( " load_lmhosts_file " ) ;
2016-11-23 10:55:16 +03:00
FILE * fp = startlmhosts ( fname ) ;
2003-08-27 05:25:01 +04:00
if ( ! fp ) {
DEBUG ( 2 , ( " load_lmhosts_file: Can't open lmhosts file %s. Error was %s \n " ,
fname , strerror ( errno ) ) ) ;
2007-11-20 02:15:09 +03:00
TALLOC_FREE ( ctx ) ;
2003-08-27 05:25:01 +04:00
return ;
}
2007-11-20 02:15:09 +03:00
while ( getlmhostsent ( ctx , fp , & name , & name_type , & ss ) ) {
2007-10-25 01:16:54 +04:00
struct in_addr ipaddr ;
2003-08-27 05:25:01 +04:00
struct subnet_record * subrec = NULL ;
enum name_source source = LMHOSTS_NAME ;
2007-10-25 01:16:54 +04:00
if ( ss . ss_family ! = AF_INET ) {
2007-11-20 02:15:09 +03:00
TALLOC_FREE ( name ) ;
2007-10-25 01:16:54 +04:00
continue ;
}
ipaddr = ( ( struct sockaddr_in * ) & ss ) - > sin_addr ;
2003-08-27 05:25:01 +04:00
/* We find a relevent subnet to put this entry on, then add it. */
/* Go through all the broadcast subnets and see if the mask matches. */
for ( subrec = FIRST_SUBNET ; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST ( subrec ) ) {
2007-10-11 05:25:16 +04:00
if ( same_net_v4 ( ipaddr , subrec - > bcast_ip , subrec - > mask_ip ) )
2003-08-27 05:25:01 +04:00
break ;
}
2007-11-20 02:15:09 +03:00
2003-08-27 05:25:01 +04:00
/* If none match add the name to the remote_broadcast_subnet. */
if ( subrec = = NULL )
subrec = remote_broadcast_subnet ;
if ( name_type = = - 1 ) {
/* Add the (0) and (0x20) names directly into the namelist for this subnet. */
2015-04-30 06:14:34 +03:00
( void ) add_name_to_subnet ( subrec , name , 0x00 , ( uint16_t ) NB_ACTIVE , PERMANENT_TTL , source , 1 , & ipaddr ) ;
( void ) add_name_to_subnet ( subrec , name , 0x20 , ( uint16_t ) NB_ACTIVE , PERMANENT_TTL , source , 1 , & ipaddr ) ;
2003-08-27 05:25:01 +04:00
} else {
/* Add the given name type to the subnet namelist. */
2015-04-30 06:14:34 +03:00
( void ) add_name_to_subnet ( subrec , name , name_type , ( uint16_t ) NB_ACTIVE , PERMANENT_TTL , source , 1 , & ipaddr ) ;
2003-08-27 05:25:01 +04:00
}
}
2007-11-20 02:15:09 +03:00
TALLOC_FREE ( ctx ) ;
2003-08-27 05:25:01 +04:00
endlmhosts ( fp ) ;
1997-12-13 17:16:07 +03:00
}
/****************************************************************************
Find a name read from the lmhosts file . We secretly check the names on
the remote_broadcast_subnet as if the name was added to a regular broadcast
subnet it will be found by normal name query processing .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
bool find_name_in_lmhosts ( struct nmb_name * nmbname , struct name_record * * namerecp )
1997-12-13 17:16:07 +03:00
{
2003-08-27 05:25:01 +04:00
struct name_record * namerec ;
1997-12-13 17:16:07 +03:00
2003-08-27 05:25:01 +04:00
* namerecp = NULL ;
1997-12-13 17:16:07 +03:00
2003-08-27 05:25:01 +04:00
if ( ( namerec = find_name_on_subnet ( remote_broadcast_subnet , nmbname , FIND_ANY_NAME ) ) = = NULL )
return False ;
1997-12-13 17:16:07 +03:00
2003-08-27 05:25:01 +04:00
if ( ! NAME_IS_ACTIVE ( namerec ) | | ( namerec - > data . source ! = LMHOSTS_NAME ) )
return False ;
1997-12-13 17:16:07 +03:00
2003-08-27 05:25:01 +04:00
* namerecp = namerec ;
return True ;
1997-12-13 17:16:07 +03:00
}