2008-12-16 09:30:16 +01:00
/*
2002-01-30 06:08:46 +00:00
Unix SMB / CIFS implementation .
2008-12-16 09:30:16 +01:00
a WINS nsswitch module
1999-12-17 06:11:25 +00:00
Copyright ( C ) Andrew Tridgell 1999
2008-12-16 09:30:16 +01:00
1999-12-17 06:11:25 +00: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 19:25:36 +00:00
the Free Software Foundation ; either version 3 of the License , or
1999-12-17 06:11:25 +00:00
( at your option ) any later version .
2008-12-16 09:30:16 +01:00
1999-12-17 06:11:25 +00: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 09:30:16 +01:00
1999-12-17 06:11:25 +00:00
You should have received a copy of the GNU General Public License
2007-07-10 00:52:41 +00:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2008-12-16 09:30:16 +01:00
1999-12-17 06:11:25 +00:00
*/
# include "includes.h"
2017-10-21 14:15:12 +02:00
# include "nsswitch/winbind_client.h"
2015-10-23 12:31:43 +02:00
# include "nsswitch/libwbclient/wbclient.h"
2020-08-07 11:17:34 -07:00
# include "lib/util/string_wrappers.h"
2010-08-18 12:42:49 +02:00
2001-12-12 16:08:32 +00:00
# ifdef HAVE_NS_API_H
# include <ns_daemon.h>
# endif
1999-12-17 06:11:25 +00:00
2018-11-20 14:08:05 +01:00
# ifdef HAVE_PTHREAD_H
2008-11-19 11:22:50 -08:00
# include <pthread.h>
# endif
2018-11-20 14:08:05 +01:00
# ifdef HAVE_PTHREAD
2008-11-19 11:22:50 -08:00
static pthread_mutex_t wins_nss_mutex = PTHREAD_MUTEX_INITIALIZER ;
# endif
1999-12-17 06:11:25 +00:00
# ifndef INADDRSZ
# define INADDRSZ 4
# endif
2022-07-14 18:45:17 +05:30
# ifndef NETDB_INTERNAL
# define NETDB_INTERNAL -1
# endif
# ifndef NETDB_SUCCESS
# define NETDB_SUCCESS 0
# endif
2021-07-01 12:08:16 +02:00
_PUBLIC_ON_LINUX_
2016-09-19 16:17:11 +02:00
NSS_STATUS _nss_wins_gethostbyname_r ( const char * hostname ,
struct hostent * he ,
char * buffer ,
size_t buflen ,
int * errnop ,
int * h_errnop ) ;
NSS_STATUS _nss_wins_gethostbyname2_r ( const char * name ,
int af ,
struct hostent * he ,
char * buffer ,
size_t buflen ,
int * errnop ,
int * h_errnop ) ;
2006-12-20 01:10:04 +00:00
2015-10-23 12:31:43 +02:00
static char * lookup_byname_backend ( const char * name )
1999-12-17 06:11:25 +00:00
{
2015-01-30 14:29:26 +01:00
const char * p ;
2016-04-16 10:57:12 +02:00
char * ip , * ipp ;
2015-01-30 14:29:26 +01:00
size_t nbt_len ;
2015-10-23 12:31:43 +02:00
wbcErr result ;
1999-12-17 06:11:25 +00:00
2015-01-30 14:29:26 +01:00
nbt_len = strlen ( name ) ;
if ( nbt_len > MAX_NETBIOSNAME_LEN - 1 ) {
return NULL ;
}
p = strchr ( name , ' . ' ) ;
if ( p ! = NULL ) {
return NULL ;
}
2018-11-05 08:16:23 +01:00
wbcSetClientProcessName ( " nss_wins " ) ;
2015-10-23 12:31:43 +02:00
result = wbcResolveWinsByName ( name , & ip ) ;
if ( result ! = WBC_ERR_SUCCESS ) {
return NULL ;
1999-12-17 06:11:25 +00:00
}
2016-04-16 10:57:12 +02:00
ipp = strchr ( ip , ' \t ' ) ;
if ( ipp ! = NULL ) {
* ipp = ' \0 ' ;
}
2015-10-23 12:31:43 +02:00
return ip ;
1999-12-17 06:11:25 +00:00
}
2001-12-12 16:08:32 +00:00
# ifdef HAVE_NS_API_H
2003-07-23 06:37:51 +00:00
2015-10-23 12:31:43 +02:00
static char * lookup_byaddr_backend ( const char * ip )
2003-07-23 06:37:51 +00:00
{
2015-10-23 12:31:43 +02:00
wbcErr result ;
char * name = NULL ;
2003-07-23 06:37:51 +00:00
2018-11-05 08:16:23 +01:00
wbcSetClientProcessName ( " nss_wins " ) ;
2015-10-23 12:31:43 +02:00
result = wbcResolveWinsByIP ( ip , & name ) ;
if ( result ! = WBC_ERR_SUCCESS ) {
2011-03-02 10:44:14 +01:00
return NULL ;
}
2003-07-23 06:37:51 +00:00
2015-10-23 12:31:43 +02:00
return name ;
2003-07-23 06:37:51 +00:00
}
2001-12-12 16:08:32 +00:00
/* IRIX version */
int init ( void )
{
2015-10-23 12:31:43 +02:00
bool ok ;
2001-12-22 00:51:32 +00:00
nsd_logprintf ( NSD_LOG_MIN , " entering init (wins) \n " ) ;
2015-10-23 12:31:43 +02:00
ok = nss_wins_init ( ) ;
if ( ! ok ) {
return NSD_ERROR ;
}
2001-12-12 16:08:32 +00:00
return NSD_OK ;
}
int lookup ( nsd_file_t * rq )
{
char * map ;
char * key ;
2001-12-22 00:51:32 +00:00
char * addr ;
int i , count , len , size ;
char response [ 1024 ] ;
2007-10-18 17:40:25 -07:00
bool found = False ;
2001-12-12 16:08:32 +00:00
2001-12-22 00:51:32 +00:00
nsd_logprintf ( NSD_LOG_MIN , " entering lookup (wins) \n " ) ;
2008-12-16 09:30:16 +01:00
if ( ! rq )
2001-12-12 16:08:32 +00: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 00:51:32 +00:00
response [ 0 ] = ' \0 ' ;
len = sizeof ( response ) - 2 ;
2008-12-16 09:30:16 +01:00
/*
2001-12-22 00:51:32 +00:00
* response needs to be a string of the following format
* ip_address [ ip_address ] * \ tname [ alias ] *
*/
2011-05-13 20:21:30 +02:00
if ( strcasecmp_m ( map , " hosts.byaddr " ) = = 0 ) {
2015-10-23 12:31:43 +02:00
char * name ;
name = lookup_byaddr_backend ( key ) ;
if ( name ! = NULL ) {
size = strlen ( key ) + 1 ;
if ( size > len ) {
return NSD_ERROR ;
2001-12-22 00:51:32 +00:00
}
2015-10-23 12:31:43 +02:00
len - = size ;
strncat ( response , key , size ) ;
strncat ( response , " \t " , 1 ) ;
size = strlen ( name ) + 1 ;
if ( size > len ) {
return NSD_ERROR ;
}
len - = size ;
strncat ( response , name , size ) ;
strncat ( response , " " , 1 ) ;
found = True ;
2001-12-22 00:51:32 +00:00
}
2015-10-23 12:31:43 +02:00
response [ strlen ( response ) - 1 ] = ' \n ' ;
2011-05-13 20:21:30 +02:00
} else if ( strcasecmp_m ( map , " hosts.byname " ) = = 0 ) {
2015-10-23 12:31:43 +02:00
char * ip ;
ip = lookup_byname_backend ( key ) ;
if ( ip ! = NULL ) {
size = strlen ( ip ) + 1 ;
if ( size > len ) {
wbcFreeMemory ( ip ) ;
return NSD_ERROR ;
}
len - = size ;
strncat ( response , ip , size ) ;
strncat ( response , " \t " , 1 ) ;
size = strlen ( key ) + 1 ;
wbcFreeMemory ( ip ) ;
if ( size > len ) {
return NSD_ERROR ;
}
strncat ( response , key , size ) ;
strncat ( response , " \n " , 1 ) ;
found = True ;
2008-12-16 09:30:16 +01:00
}
2001-12-12 16:08:32 +00:00
}
2001-12-22 00:51:32 +00: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 16:08:32 +00:00
}
# else
2003-09-08 05:51:57 +00: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 . */
2015-10-23 12:31:43 +02:00
static char * get_static ( char * * buffer , size_t * buflen , size_t len )
2003-09-08 05:51:57 +00: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 09:30:16 +01:00
2003-09-08 05:51:57 +00: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 00:51:32 +00:00
/****************************************************************************
gethostbyname ( ) - we ignore any domain portion of the name and only
handle names that are at most 15 characters long
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2021-07-01 12:08:16 +02:00
_PUBLIC_ON_LINUX_
2001-12-12 16:08:32 +00:00
NSS_STATUS
2016-09-19 16:17:11 +02:00
_nss_wins_gethostbyname_r ( const char * hostname ,
struct hostent * he ,
char * buffer ,
size_t buflen ,
int * errnop ,
int * h_errnop )
1999-12-17 06:11:25 +00:00
{
2008-11-19 11:22:50 -08:00
NSS_STATUS nss_status = NSS_STATUS_SUCCESS ;
2015-10-23 12:31:43 +02:00
char * ip ;
struct in_addr in ;
int i ;
2003-09-08 05:51:57 +00:00
fstring name ;
size_t namelen ;
2015-10-23 12:31:43 +02:00
int rc ;
2008-12-16 09:30:16 +01:00
2018-11-20 14:08:05 +01:00
# ifdef HAVE_PTHREAD
2008-11-19 11:22:50 -08:00
pthread_mutex_lock ( & wins_nss_mutex ) ;
# endif
2001-05-15 22:01:48 +00:00
memset ( he , ' \0 ' , sizeof ( * he ) ) ;
2003-09-08 05:51:57 +00:00
fstrcpy ( name , hostname ) ;
/* Do lookup */
2001-05-15 22:01:48 +00:00
2015-10-23 12:31:43 +02:00
ip = lookup_byname_backend ( name ) ;
if ( ip = = NULL ) {
2016-11-13 17:40:21 +01:00
* h_errnop = HOST_NOT_FOUND ;
2008-11-19 11:22:50 -08:00
nss_status = NSS_STATUS_NOTFOUND ;
goto out ;
}
1999-12-17 06:11:25 +00:00
2015-10-23 12:31:43 +02:00
rc = inet_pton ( AF_INET , ip , & in ) ;
wbcFreeMemory ( ip ) ;
if ( rc = = 0 ) {
2016-09-19 16:17:11 +02:00
* errnop = errno ;
2016-09-20 13:26:52 +02:00
* h_errnop = NETDB_INTERNAL ;
2015-10-23 12:31:43 +02:00
nss_status = NSS_STATUS_TRYAGAIN ;
goto out ;
}
2003-09-08 05:51:57 +00:00
/* Copy h_name */
1999-12-17 06:11:25 +00:00
2003-09-08 05:51:57 +00:00
namelen = strlen ( name ) + 1 ;
2007-09-08 05:18:08 +00:00
if ( ( he - > h_name = get_static ( & buffer , & buflen , namelen ) ) = = NULL ) {
2016-09-19 16:17:11 +02:00
* errnop = EAGAIN ;
2016-09-20 13:26:52 +02:00
* h_errnop = NETDB_INTERNAL ;
2008-11-19 11:22:50 -08:00
nss_status = NSS_STATUS_TRYAGAIN ;
goto out ;
2007-09-08 05:18:08 +00:00
}
2003-09-08 05:51:57 +00:00
memcpy ( he - > h_name , name , namelen ) ;
/* Copy h_addr_list, align to pointer boundary first */
1999-12-17 06:11:25 +00:00
2003-09-08 05:51:57 +00:00
if ( ( i = ( unsigned long ) ( buffer ) % sizeof ( char * ) ) ! = 0 )
i = sizeof ( char * ) - i ;
2007-09-08 05:18:08 +00:00
if ( get_static ( & buffer , & buflen , i ) = = NULL ) {
2016-09-19 16:17:11 +02:00
* errnop = EAGAIN ;
2016-09-20 13:26:52 +02:00
* h_errnop = NETDB_INTERNAL ;
2008-11-19 11:22:50 -08:00
nss_status = NSS_STATUS_TRYAGAIN ;
goto out ;
2007-09-08 05:18:08 +00:00
}
2003-09-08 05:51:57 +00:00
if ( ( he - > h_addr_list = ( char * * ) get_static (
2016-04-16 10:57:12 +02:00
& buffer , & buflen , 2 * sizeof ( char * ) ) ) = = NULL ) {
2016-09-19 16:17:11 +02:00
* errnop = EAGAIN ;
2016-09-20 13:26:52 +02:00
* h_errnop = NETDB_INTERNAL ;
2008-11-19 11:22:50 -08:00
nss_status = NSS_STATUS_TRYAGAIN ;
goto out ;
2007-09-08 05:18:08 +00:00
}
2003-09-08 05:51:57 +00:00
2015-10-23 12:31:43 +02:00
if ( ( he - > h_addr_list [ 0 ] = get_static ( & buffer , & buflen ,
INADDRSZ ) ) = = NULL ) {
2016-09-19 16:17:11 +02:00
* errnop = EAGAIN ;
2016-09-20 13:26:52 +02:00
* h_errnop = NETDB_INTERNAL ;
2015-10-23 12:31:43 +02:00
nss_status = NSS_STATUS_TRYAGAIN ;
goto out ;
1999-12-17 06:11:25 +00:00
}
2016-04-16 10:57:12 +02:00
memcpy ( he - > h_addr_list [ 0 ] , & in , INADDRSZ ) ;
2003-09-08 05:51:57 +00:00
2016-04-16 10:57:12 +02:00
he - > h_addr_list [ 1 ] = NULL ;
2001-05-15 21:53:19 +00:00
2003-09-08 05:51:57 +00: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 11:22:50 -08:00
if ( get_static ( & buffer , & buflen , i ) = = NULL ) {
2016-09-19 16:17:11 +02:00
* errnop = EAGAIN ;
2016-09-20 13:26:52 +02:00
* h_errnop = NETDB_INTERNAL ;
2008-11-19 11:22:50 -08:00
nss_status = NSS_STATUS_TRYAGAIN ;
goto out ;
}
2003-09-08 05:51:57 +00:00
if ( ( he - > h_aliases = ( char * * ) get_static (
2008-11-19 11:22:50 -08:00
& buffer , & buflen , sizeof ( char * ) ) ) = = NULL ) {
2016-09-19 16:17:11 +02:00
* errnop = EAGAIN ;
2016-09-20 13:26:52 +02:00
* h_errnop = NETDB_INTERNAL ;
2008-11-19 11:22:50 -08:00
nss_status = NSS_STATUS_TRYAGAIN ;
goto out ;
}
2003-09-08 05:51:57 +00:00
he - > h_aliases [ 0 ] = NULL ;
1999-12-17 06:11:25 +00:00
2016-09-20 13:26:52 +02:00
* h_errnop = NETDB_SUCCESS ;
2008-11-19 11:22:50 -08:00
nss_status = NSS_STATUS_SUCCESS ;
out :
2018-11-20 14:08:05 +01:00
# ifdef HAVE_PTHREAD
2008-11-19 11:22:50 -08:00
pthread_mutex_unlock ( & wins_nss_mutex ) ;
# endif
return nss_status ;
1999-12-17 06:11:25 +00:00
}
2001-12-12 16:08:32 +00:00
2002-07-15 10:35:28 +00:00
2021-07-01 12:08:16 +02:00
_PUBLIC_ON_LINUX_
2002-07-15 10:35:28 +00:00
NSS_STATUS
2016-09-19 16:17:11 +02:00
_nss_wins_gethostbyname2_r ( const char * name ,
int af ,
struct hostent * he ,
char * buffer ,
size_t buflen ,
int * errnop ,
int * h_errnop )
2002-07-15 10:35:28 +00:00
{
2008-11-19 11:22:50 -08:00
NSS_STATUS nss_status ;
2002-07-15 10:35:28 +00:00
if ( af ! = AF_INET ) {
2016-09-19 16:17:11 +02:00
* errnop = EAFNOSUPPORT ;
2002-07-15 10:35:28 +00:00
* h_errnop = NO_DATA ;
2008-11-19 11:22:50 -08:00
nss_status = NSS_STATUS_UNAVAIL ;
} else {
2016-09-19 16:17:11 +02:00
nss_status = _nss_wins_gethostbyname_r ( name ,
he ,
buffer ,
buflen ,
errnop ,
h_errnop ) ;
2002-07-15 10:35:28 +00:00
}
2008-11-19 11:22:50 -08:00
return nss_status ;
2002-07-15 10:35:28 +00:00
}
# endif