2008-12-16 11:30:16 +03:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
2008-12-16 11:30:16 +03:00
a WINS nsswitch module
1999-12-17 09:11:25 +03:00
Copyright ( C ) Andrew Tridgell 1999
2008-12-16 11:30:16 +03:00
1999-12-17 09:11:25 +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
1999-12-17 09:11:25 +03:00
( at your option ) any later version .
2008-12-16 11:30:16 +03:00
1999-12-17 09:11:25 +03:00
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 .
2008-12-16 11:30:16 +03:00
1999-12-17 09:11:25 +03:00
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/>.
2008-12-16 11:30:16 +03:00
1999-12-17 09:11:25 +03:00
*/
# include "includes.h"
2010-08-18 14:42:49 +04:00
# include "nsswitch/winbind_nss.h"
2001-12-12 19:08:32 +03:00
# ifdef HAVE_NS_API_H
# include <ns_daemon.h>
# endif
1999-12-17 09:11:25 +03:00
2008-11-19 22:22:50 +03:00
# if HAVE_PTHREAD_H
# include <pthread.h>
# endif
# if HAVE_PTHREAD
static pthread_mutex_t wins_nss_mutex = PTHREAD_MUTEX_INITIALIZER ;
# endif
1999-12-17 09:11:25 +03:00
# ifndef INADDRSZ
# define INADDRSZ 4
# endif
2001-12-12 19:08:32 +03:00
static int initialised ;
2006-12-20 04:10:04 +03:00
NSS_STATUS _nss_wins_gethostbyname_r ( const char * hostname , struct hostent * he ,
char * buffer , size_t buflen , int * h_errnop ) ;
NSS_STATUS _nss_wins_gethostbyname2_r ( const char * name , int af , struct hostent * he ,
char * buffer , size_t buflen , int * h_errnop ) ;
2001-12-12 19:08:32 +03:00
static void nss_wins_init ( void )
{
initialised = 1 ;
2010-10-29 08:06:36 +04:00
lp_set_cmdline ( " log level " , " 0 " ) ;
2001-12-22 03:51:32 +03:00
TimeInit ( ) ;
2001-12-12 19:08:32 +03:00
setup_logging ( " nss_wins " , False ) ;
2014-07-30 18:57:06 +04:00
lp_load_global ( get_dyn_CONFIGFILE ( ) ) ;
2001-12-12 19:08:32 +03:00
load_interfaces ( ) ;
2001-12-22 03:51:32 +03:00
}
static struct in_addr * lookup_byname_backend ( const char * name , int * count )
1999-12-17 09:11:25 +03:00
{
2015-01-30 16:29:26 +03:00
TALLOC_CTX * frame ;
2011-06-03 17:49:55 +04:00
struct sockaddr_storage * address = NULL ;
2004-07-25 17:14:00 +04:00
struct in_addr * ret = NULL ;
2011-06-03 17:49:55 +04:00
NTSTATUS status ;
2015-01-30 16:29:26 +03:00
const char * p ;
size_t nbt_len ;
2011-01-04 20:48:47 +03:00
int j ;
1999-12-17 09:11:25 +03:00
if ( ! initialised ) {
2001-12-12 19:08:32 +03:00
nss_wins_init ( ) ;
1999-12-17 09:11:25 +03:00
}
* count = 0 ;
2015-01-30 16:29:26 +03:00
nbt_len = strlen ( name ) ;
if ( nbt_len > MAX_NETBIOSNAME_LEN - 1 ) {
return NULL ;
}
p = strchr ( name , ' . ' ) ;
if ( p ! = NULL ) {
return NULL ;
}
frame = talloc_stackframe ( ) ;
2002-07-15 14:35:28 +04:00
/* always try with wins first */
2011-06-03 17:49:55 +04:00
status = resolve_wins ( name , 0x00 , talloc_tos ( ) ,
& address , count ) ;
if ( NT_STATUS_IS_OK ( status ) ) {
2004-12-07 21:25:53 +03:00
if ( ( ret = SMB_MALLOC_P ( struct in_addr ) ) = = NULL ) {
2011-05-16 16:48:39 +04:00
TALLOC_FREE ( frame ) ;
2003-06-25 21:41:05 +04:00
return NULL ;
}
2011-06-03 17:49:55 +04:00
if ( address [ 0 ] . ss_family ! = AF_INET ) {
2008-03-06 13:37:01 +03:00
free ( ret ) ;
2011-05-16 16:48:39 +04:00
TALLOC_FREE ( frame ) ;
2007-10-25 01:16:54 +04:00
return NULL ;
}
2011-06-03 17:49:55 +04:00
* ret = ( ( struct sockaddr_in * ) ( void * ) address )
2011-01-01 17:08:42 +03:00
- > sin_addr ;
2011-05-16 16:48:39 +04:00
TALLOC_FREE ( frame ) ;
2002-07-15 14:35:28 +04:00
return ret ;
1999-12-17 09:11:25 +03:00
}
/* uggh, we have to broadcast to each interface in turn */
2002-07-15 14:35:28 +04:00
for ( j = iface_count ( ) - 1 ; j > = 0 ; j - - ) {
2007-10-11 05:25:16 +04:00
const struct in_addr * bcast = iface_n_bcast_v4 ( j ) ;
2007-10-25 01:16:54 +04:00
struct sockaddr_storage ss ;
struct sockaddr_storage * pss ;
2010-12-28 15:47:35 +03:00
2007-10-11 05:25:16 +04:00
if ( ! bcast ) {
continue ;
}
2007-10-25 01:16:54 +04:00
in_addr_to_sockaddr_storage ( & ss , * bcast ) ;
2011-01-04 20:48:47 +03:00
status = name_query ( name , 0x00 , True , True , & ss ,
2011-05-16 16:48:39 +04:00
talloc_tos ( ) , & pss , count , NULL ) ;
2011-05-16 01:26:10 +04:00
if ( NT_STATUS_IS_OK ( status ) & & ( * count > 0 ) ) {
2008-05-06 19:02:31 +04:00
if ( ( ret = SMB_MALLOC_P ( struct in_addr ) ) = = NULL ) {
2011-05-16 16:48:39 +04:00
TALLOC_FREE ( frame ) ;
2008-05-06 19:02:31 +04:00
return NULL ;
}
2007-10-25 01:16:54 +04:00
* ret = ( ( struct sockaddr_in * ) pss ) - > sin_addr ;
break ;
}
1999-12-17 09:11:25 +03:00
}
2011-05-16 16:48:39 +04:00
TALLOC_FREE ( frame ) ;
1999-12-17 09:11:25 +03:00
return ret ;
}
2001-12-12 19:08:32 +03:00
# ifdef HAVE_NS_API_H
2003-07-23 10:37:51 +04:00
2010-12-28 13:55:47 +03:00
static struct node_status * lookup_byaddr_backend ( char * addr , int * count )
2003-07-23 10:37:51 +04:00
{
2008-02-27 12:06:53 +03:00
struct sockaddr_storage ss ;
2003-07-23 10:37:51 +04:00
struct nmb_name nname ;
2011-03-02 12:44:14 +03:00
struct node_status * result ;
NTSTATUS status ;
2003-07-23 10:37:51 +04:00
if ( ! initialised ) {
nss_wins_init ( ) ;
}
make_nmb_name ( & nname , " * " , 0 ) ;
2008-02-27 12:06:53 +03:00
if ( ! interpret_string_addr ( & ss , addr , AI_NUMERICHOST ) ) {
return NULL ;
}
2011-03-02 12:44:14 +03:00
status = node_status_query ( NULL , & nname , & ss , & result , count , NULL ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
return NULL ;
}
2003-07-23 10:37:51 +04:00
2011-03-02 12:44:14 +03:00
return result ;
2003-07-23 10:37:51 +04:00
}
2001-12-12 19:08:32 +03:00
/* IRIX version */
int init ( void )
{
2001-12-22 03:51:32 +03:00
nsd_logprintf ( NSD_LOG_MIN , " entering init (wins) \n " ) ;
2001-12-12 19:08:32 +03:00
nss_wins_init ( ) ;
return NSD_OK ;
}
int lookup ( nsd_file_t * rq )
{
char * map ;
char * key ;
2001-12-22 03:51:32 +03:00
char * addr ;
2001-12-12 19:08:32 +03:00
struct in_addr * ip_list ;
2010-12-28 13:55:47 +03:00
struct node_status * status ;
2001-12-22 03:51:32 +03:00
int i , count , len , size ;
char response [ 1024 ] ;
2007-10-19 04:40:25 +04:00
bool found = False ;
2001-12-12 19:08:32 +03:00
2001-12-22 03:51:32 +03:00
nsd_logprintf ( NSD_LOG_MIN , " entering lookup (wins) \n " ) ;
2008-12-16 11:30:16 +03:00
if ( ! rq )
2001-12-12 19:08:32 +03:00
return NSD_ERROR ;
map = nsd_attr_fetch_string ( rq - > f_attrs , " table " , ( char * ) 0 ) ;
if ( ! map ) {
rq - > f_status = NS_FATAL ;
return NSD_ERROR ;
}
key = nsd_attr_fetch_string ( rq - > f_attrs , " key " , ( char * ) 0 ) ;
if ( ! key | | ! * key ) {
rq - > f_status = NS_FATAL ;
return NSD_ERROR ;
}
2001-12-22 03:51:32 +03:00
response [ 0 ] = ' \0 ' ;
len = sizeof ( response ) - 2 ;
2008-12-16 11:30:16 +03:00
/*
2001-12-22 03:51:32 +03:00
* response needs to be a string of the following format
* ip_address [ ip_address ] * \ tname [ alias ] *
*/
2011-05-13 22:21:30 +04:00
if ( strcasecmp_m ( map , " hosts.byaddr " ) = = 0 ) {
2001-12-22 03:51:32 +03:00
if ( status = lookup_byaddr_backend ( key , & count ) ) {
size = strlen ( key ) + 1 ;
if ( size > len ) {
2011-03-02 12:44:14 +03:00
talloc_free ( status ) ;
2001-12-22 03:51:32 +03:00
return NSD_ERROR ;
}
len - = size ;
strncat ( response , key , size ) ;
strncat ( response , " \t " , 1 ) ;
for ( i = 0 ; i < count ; i + + ) {
/* ignore group names */
if ( status [ i ] . flags & 0x80 ) continue ;
if ( status [ i ] . type = = 0x20 ) {
size = sizeof ( status [ i ] . name ) + 1 ;
if ( size > len ) {
2011-03-02 12:44:14 +03:00
talloc_free ( status ) ;
2001-12-22 03:51:32 +03:00
return NSD_ERROR ;
}
len - = size ;
strncat ( response , status [ i ] . name , size ) ;
strncat ( response , " " , 1 ) ;
found = True ;
}
}
response [ strlen ( response ) - 1 ] = ' \n ' ;
2011-03-02 12:44:14 +03:00
talloc_free ( status ) ;
2001-12-22 03:51:32 +03:00
}
2011-05-13 22:21:30 +04:00
} else if ( strcasecmp_m ( map , " hosts.byname " ) = = 0 ) {
2001-12-22 03:51:32 +03:00
if ( ip_list = lookup_byname_backend ( key , & count ) ) {
for ( i = count ; i ; i - - ) {
addr = inet_ntoa ( ip_list [ i - 1 ] ) ;
size = strlen ( addr ) + 1 ;
if ( size > len ) {
free ( ip_list ) ;
return NSD_ERROR ;
}
len - = size ;
if ( i ! = 0 )
response [ strlen ( response ) - 1 ] = ' ' ;
strncat ( response , addr , size ) ;
strncat ( response , " \t " , 1 ) ;
}
size = strlen ( key ) + 1 ;
if ( size > len ) {
free ( ip_list ) ;
return NSD_ERROR ;
2008-12-16 11:30:16 +03:00
}
2001-12-22 03:51:32 +03:00
strncat ( response , key , size ) ;
strncat ( response , " \n " , 1 ) ;
found = True ;
free ( ip_list ) ;
}
2001-12-12 19:08:32 +03:00
}
2001-12-22 03:51:32 +03:00
if ( found ) {
nsd_logprintf ( NSD_LOG_LOW , " lookup (wins %s) %s \n " , map , response ) ;
nsd_set_result ( rq , NS_SUCCESS , response , strlen ( response ) , VOLATILE ) ;
return NSD_OK ;
}
nsd_logprintf ( NSD_LOG_LOW , " lookup (wins) not found \n " ) ;
rq - > f_status = NS_NOTFOUND ;
return NSD_NEXT ;
2001-12-12 19:08:32 +03:00
}
# else
2003-09-08 09:51:57 +04:00
/* Allocate some space from the nss static buffer. The buffer and buflen
are the pointers passed in by the C library to the _nss_ * _ *
functions . */
2003-12-11 18:35:11 +03:00
static char * get_static ( char * * buffer , size_t * buflen , int len )
2003-09-08 09:51:57 +04:00
{
char * result ;
/* Error check. We return false if things aren't set up right, or
there isn ' t enough buffer space left . */
2008-12-16 11:30:16 +03:00
2003-09-08 09:51:57 +04:00
if ( ( buffer = = NULL ) | | ( buflen = = NULL ) | | ( * buflen < len ) ) {
return NULL ;
}
/* Return an index into the static buffer */
result = * buffer ;
* buffer + = len ;
* buflen - = len ;
return result ;
}
2001-12-22 03:51:32 +03:00
/****************************************************************************
gethostbyname ( ) - we ignore any domain portion of the name and only
handle names that are at most 15 characters long
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-12-12 19:08:32 +03:00
NSS_STATUS
2003-09-08 09:51:57 +04:00
_nss_wins_gethostbyname_r ( const char * hostname , struct hostent * he ,
char * buffer , size_t buflen , int * h_errnop )
1999-12-17 09:11:25 +03:00
{
2008-11-19 22:22:50 +03:00
NSS_STATUS nss_status = NSS_STATUS_SUCCESS ;
1999-12-17 09:11:25 +03:00
struct in_addr * ip_list ;
int i , count ;
2003-09-08 09:51:57 +04:00
fstring name ;
size_t namelen ;
2010-03-20 17:23:17 +03:00
TALLOC_CTX * frame ;
2008-12-16 11:30:16 +03:00
2008-11-19 22:22:50 +03:00
# if HAVE_PTHREAD
pthread_mutex_lock ( & wins_nss_mutex ) ;
# endif
2010-03-20 17:23:17 +03:00
frame = talloc_stackframe ( ) ;
2001-05-16 02:01:48 +04:00
memset ( he , ' \0 ' , sizeof ( * he ) ) ;
2003-09-08 09:51:57 +04:00
fstrcpy ( name , hostname ) ;
/* Do lookup */
2001-05-16 02:01:48 +04:00
2001-12-22 03:51:32 +03:00
ip_list = lookup_byname_backend ( name , & count ) ;
1999-12-17 09:11:25 +03:00
2008-11-19 22:22:50 +03:00
if ( ! ip_list ) {
nss_status = NSS_STATUS_NOTFOUND ;
goto out ;
}
1999-12-17 09:11:25 +03:00
2003-09-08 09:51:57 +04:00
/* Copy h_name */
1999-12-17 09:11:25 +03:00
2003-09-08 09:51:57 +04:00
namelen = strlen ( name ) + 1 ;
2007-09-08 09:18:08 +04:00
if ( ( he - > h_name = get_static ( & buffer , & buflen , namelen ) ) = = NULL ) {
free ( ip_list ) ;
2008-11-19 22:22:50 +03:00
nss_status = NSS_STATUS_TRYAGAIN ;
goto out ;
2007-09-08 09:18:08 +04:00
}
2003-09-08 09:51:57 +04:00
memcpy ( he - > h_name , name , namelen ) ;
/* Copy h_addr_list, align to pointer boundary first */
1999-12-17 09:11:25 +03:00
2003-09-08 09:51:57 +04:00
if ( ( i = ( unsigned long ) ( buffer ) % sizeof ( char * ) ) ! = 0 )
i = sizeof ( char * ) - i ;
2007-09-08 09:18:08 +04:00
if ( get_static ( & buffer , & buflen , i ) = = NULL ) {
free ( ip_list ) ;
2008-11-19 22:22:50 +03:00
nss_status = NSS_STATUS_TRYAGAIN ;
goto out ;
2007-09-08 09:18:08 +04:00
}
2003-09-08 09:51:57 +04:00
if ( ( he - > h_addr_list = ( char * * ) get_static (
2007-09-08 09:18:08 +04:00
& buffer , & buflen , ( count + 1 ) * sizeof ( char * ) ) ) = = NULL ) {
free ( ip_list ) ;
2008-11-19 22:22:50 +03:00
nss_status = NSS_STATUS_TRYAGAIN ;
goto out ;
2007-09-08 09:18:08 +04:00
}
2003-09-08 09:51:57 +04:00
for ( i = 0 ; i < count ; i + + ) {
if ( ( he - > h_addr_list [ i ] = get_static ( & buffer , & buflen ,
2007-09-08 09:18:08 +04:00
INADDRSZ ) ) = = NULL ) {
free ( ip_list ) ;
2008-11-19 22:22:50 +03:00
nss_status = NSS_STATUS_TRYAGAIN ;
goto out ;
2007-09-08 09:18:08 +04:00
}
2003-09-08 09:51:57 +04:00
memcpy ( he - > h_addr_list [ i ] , & ip_list [ i ] , INADDRSZ ) ;
1999-12-17 09:11:25 +03:00
}
2003-09-08 09:51:57 +04:00
he - > h_addr_list [ count ] = NULL ;
2007-09-08 09:18:08 +04:00
free ( ip_list ) ;
2001-05-16 01:53:19 +04:00
2003-09-08 09:51:57 +04:00
/* Set h_addr_type and h_length */
he - > h_addrtype = AF_INET ;
he - > h_length = INADDRSZ ;
/* Set h_aliases */
if ( ( i = ( unsigned long ) ( buffer ) % sizeof ( char * ) ) ! = 0 )
i = sizeof ( char * ) - i ;
2008-11-19 22:22:50 +03:00
if ( get_static ( & buffer , & buflen , i ) = = NULL ) {
nss_status = NSS_STATUS_TRYAGAIN ;
goto out ;
}
2003-09-08 09:51:57 +04:00
if ( ( he - > h_aliases = ( char * * ) get_static (
2008-11-19 22:22:50 +03:00
& buffer , & buflen , sizeof ( char * ) ) ) = = NULL ) {
nss_status = NSS_STATUS_TRYAGAIN ;
goto out ;
}
2003-09-08 09:51:57 +04:00
he - > h_aliases [ 0 ] = NULL ;
1999-12-17 09:11:25 +03:00
2008-11-19 22:22:50 +03:00
nss_status = NSS_STATUS_SUCCESS ;
out :
2010-03-20 17:23:17 +03:00
TALLOC_FREE ( frame ) ;
2008-11-19 22:22:50 +03:00
# if HAVE_PTHREAD
pthread_mutex_unlock ( & wins_nss_mutex ) ;
# endif
return nss_status ;
1999-12-17 09:11:25 +03:00
}
2001-12-12 19:08:32 +03:00
2002-07-15 14:35:28 +04:00
NSS_STATUS
_nss_wins_gethostbyname2_r ( const char * name , int af , struct hostent * he ,
2003-09-08 09:51:57 +04:00
char * buffer , size_t buflen , int * h_errnop )
2002-07-15 14:35:28 +04:00
{
2008-11-19 22:22:50 +03:00
NSS_STATUS nss_status ;
2002-07-15 14:35:28 +04:00
if ( af ! = AF_INET ) {
* h_errnop = NO_DATA ;
2008-11-19 22:22:50 +03:00
nss_status = NSS_STATUS_UNAVAIL ;
} else {
nss_status = _nss_wins_gethostbyname_r (
name , he , buffer , buflen , h_errnop ) ;
2002-07-15 14:35:28 +04:00
}
2008-11-19 22:22:50 +03:00
return nss_status ;
2002-07-15 14:35:28 +04:00
}
# endif