2001-12-03 06:53:57 +03:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
2001-12-03 06:53:57 +03:00
nss tester for winbindd
Copyright ( C ) Andrew Tridgell 2001
2003-06-16 07:30:53 +04:00
Copyright ( C ) Tim Potter 2003
2001-12-03 06:53:57 +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
2001-12-03 06:53:57 +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/>.
2001-12-03 06:53:57 +03:00
*/
# include "includes.h"
2003-01-03 11:28:12 +03:00
static const char * so_path = " /lib/libnss_winbind.so " ;
static const char * nss_name = " winbind " ;
2001-12-03 06:53:57 +03:00
static int nss_errno ;
2001-12-09 09:51:27 +03:00
static NSS_STATUS last_error ;
2001-12-09 10:18:59 +03:00
static int total_errors ;
2001-12-03 06:53:57 +03:00
static void * find_fn ( const char * name )
{
2007-12-05 04:05:33 +03:00
char * s ;
2001-12-03 06:53:57 +03:00
static void * h ;
void * res ;
2001-12-07 01:42:27 +03:00
2007-12-05 04:05:33 +03:00
if ( asprintf ( & s , " _nss_%s_%s " , nss_name , name ) < 0 ) {
exit ( 1 ) ;
}
2001-12-07 01:42:27 +03:00
2001-12-03 06:53:57 +03:00
if ( ! h ) {
2008-11-01 05:49:49 +03:00
h = dlopen ( so_path , RTLD_LAZY ) ;
2001-12-03 06:53:57 +03:00
}
if ( ! h ) {
printf ( " Can't open shared library %s \n " , so_path ) ;
exit ( 1 ) ;
}
2008-11-01 05:49:49 +03:00
res = dlsym ( h , s ) ;
2001-12-03 06:53:57 +03:00
if ( ! res ) {
2001-12-07 01:42:27 +03:00
printf ( " Can't find function %s \n " , s ) ;
2003-06-16 07:30:53 +04:00
total_errors + + ;
2007-12-05 04:05:33 +03:00
SAFE_FREE ( s ) ;
2001-12-09 09:10:40 +03:00
return NULL ;
2001-12-03 06:53:57 +03:00
}
2007-12-05 04:05:33 +03:00
SAFE_FREE ( s ) ;
2001-12-03 06:53:57 +03:00
return res ;
}
2001-12-09 09:10:40 +03:00
static void report_nss_error ( const char * who , NSS_STATUS status )
2001-12-03 06:53:57 +03:00
{
2001-12-09 09:51:27 +03:00
last_error = status ;
2001-12-09 10:18:59 +03:00
total_errors + + ;
2001-12-09 09:51:27 +03:00
printf ( " ERROR %s: NSS_STATUS=%d %d (nss_errno=%d) \n " ,
who , status , NSS_STATUS_SUCCESS , nss_errno ) ;
2001-12-03 06:53:57 +03:00
}
static struct passwd * nss_getpwent ( void )
{
NSS_STATUS ( * _nss_getpwent_r ) ( struct passwd * , char * ,
2006-07-11 22:01:26 +04:00
size_t , int * ) =
( NSS_STATUS ( * ) ( struct passwd * , char * ,
size_t , int * ) ) find_fn ( " getpwent_r " ) ;
2001-12-03 06:53:57 +03:00
static struct passwd pwd ;
static char buf [ 1000 ] ;
NSS_STATUS status ;
2001-12-09 09:10:40 +03:00
2003-06-16 07:30:53 +04:00
if ( ! _nss_getpwent_r )
return NULL ;
2001-12-03 06:53:57 +03:00
status = _nss_getpwent_r ( & pwd , buf , sizeof ( buf ) , & nss_errno ) ;
if ( status = = NSS_STATUS_NOTFOUND ) {
return NULL ;
}
2001-12-09 09:10:40 +03:00
if ( status ! = NSS_STATUS_SUCCESS ) {
report_nss_error ( " getpwent " , status ) ;
2001-12-03 06:53:57 +03:00
return NULL ;
}
return & pwd ;
}
static struct passwd * nss_getpwnam ( const char * name )
{
NSS_STATUS ( * _nss_getpwnam_r ) ( const char * , struct passwd * , char * ,
2006-07-11 22:01:26 +04:00
size_t , int * ) =
( NSS_STATUS ( * ) ( const char * , struct passwd * , char * ,
size_t , int * ) ) find_fn ( " getpwnam_r " ) ;
2001-12-03 06:53:57 +03:00
static struct passwd pwd ;
static char buf [ 1000 ] ;
NSS_STATUS status ;
2003-06-16 07:30:53 +04:00
if ( ! _nss_getpwnam_r )
return NULL ;
2001-12-03 06:53:57 +03:00
status = _nss_getpwnam_r ( name , & pwd , buf , sizeof ( buf ) , & nss_errno ) ;
if ( status = = NSS_STATUS_NOTFOUND ) {
return NULL ;
}
2001-12-09 09:10:40 +03:00
if ( status ! = NSS_STATUS_SUCCESS ) {
report_nss_error ( " getpwnam " , status ) ;
2001-12-03 06:53:57 +03:00
return NULL ;
}
return & pwd ;
}
static struct passwd * nss_getpwuid ( uid_t uid )
{
NSS_STATUS ( * _nss_getpwuid_r ) ( uid_t , struct passwd * , char * ,
2006-07-11 22:01:26 +04:00
size_t , int * ) =
( NSS_STATUS ( * ) ( uid_t , struct passwd * , char * ,
size_t , int * ) ) find_fn ( " getpwuid_r " ) ;
2001-12-03 06:53:57 +03:00
static struct passwd pwd ;
static char buf [ 1000 ] ;
NSS_STATUS status ;
2003-06-16 07:30:53 +04:00
if ( ! _nss_getpwuid_r )
return NULL ;
2001-12-03 06:53:57 +03:00
status = _nss_getpwuid_r ( uid , & pwd , buf , sizeof ( buf ) , & nss_errno ) ;
if ( status = = NSS_STATUS_NOTFOUND ) {
return NULL ;
}
2001-12-09 09:10:40 +03:00
if ( status ! = NSS_STATUS_SUCCESS ) {
report_nss_error ( " getpwuid " , status ) ;
2001-12-03 06:53:57 +03:00
return NULL ;
}
return & pwd ;
}
static void nss_setpwent ( void )
{
2006-07-11 22:01:26 +04:00
NSS_STATUS ( * _nss_setpwent ) ( void ) =
( NSS_STATUS ( * ) ( void ) ) find_fn ( " setpwent " ) ;
2001-12-09 09:10:40 +03:00
NSS_STATUS status ;
2003-06-16 07:30:53 +04:00
if ( ! _nss_setpwent )
return ;
2001-12-09 09:10:40 +03:00
status = _nss_setpwent ( ) ;
if ( status ! = NSS_STATUS_SUCCESS ) {
report_nss_error ( " setpwent " , status ) ;
}
2001-12-03 06:53:57 +03:00
}
static void nss_endpwent ( void )
{
2006-07-11 22:01:26 +04:00
NSS_STATUS ( * _nss_endpwent ) ( void ) =
( NSS_STATUS ( * ) ( void ) ) find_fn ( " endpwent " ) ;
2001-12-09 09:10:40 +03:00
NSS_STATUS status ;
2003-06-16 07:30:53 +04:00
if ( ! _nss_endpwent )
return ;
2001-12-09 09:10:40 +03:00
status = _nss_endpwent ( ) ;
if ( status ! = NSS_STATUS_SUCCESS ) {
report_nss_error ( " endpwent " , status ) ;
}
2001-12-03 06:53:57 +03:00
}
static struct group * nss_getgrent ( void )
{
NSS_STATUS ( * _nss_getgrent_r ) ( struct group * , char * ,
2006-07-11 22:01:26 +04:00
size_t , int * ) =
( NSS_STATUS ( * ) ( struct group * , char * ,
size_t , int * ) ) find_fn ( " getgrent_r " ) ;
2001-12-03 06:53:57 +03:00
static struct group grp ;
2001-12-09 09:51:27 +03:00
static char * buf ;
static int buflen = 1024 ;
2001-12-03 06:53:57 +03:00
NSS_STATUS status ;
2001-12-09 09:51:27 +03:00
2003-06-16 07:30:53 +04:00
if ( ! _nss_getgrent_r )
return NULL ;
if ( ! buf )
2006-07-11 22:01:26 +04:00
buf = SMB_MALLOC_ARRAY ( char , buflen ) ;
2001-12-09 09:51:27 +03:00
again :
status = _nss_getgrent_r ( & grp , buf , buflen , & nss_errno ) ;
if ( status = = NSS_STATUS_TRYAGAIN ) {
buflen * = 2 ;
2006-07-11 22:01:26 +04:00
buf = SMB_REALLOC_ARRAY ( buf , char , buflen ) ;
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
if ( ! buf ) {
return NULL ;
}
2001-12-09 09:51:27 +03:00
goto again ;
}
2001-12-03 06:53:57 +03:00
if ( status = = NSS_STATUS_NOTFOUND ) {
2007-01-09 16:24:33 +03:00
SAFE_FREE ( buf ) ;
2001-12-03 06:53:57 +03:00
return NULL ;
}
2001-12-09 09:10:40 +03:00
if ( status ! = NSS_STATUS_SUCCESS ) {
report_nss_error ( " getgrent " , status ) ;
2007-01-09 16:24:33 +03:00
SAFE_FREE ( buf ) ;
2001-12-03 06:53:57 +03:00
return NULL ;
}
return & grp ;
}
static struct group * nss_getgrnam ( const char * name )
{
NSS_STATUS ( * _nss_getgrnam_r ) ( const char * , struct group * , char * ,
2006-07-11 22:01:26 +04:00
size_t , int * ) =
( NSS_STATUS ( * ) ( const char * , struct group * , char * ,
size_t , int * ) ) find_fn ( " getgrnam_r " ) ;
2001-12-03 06:53:57 +03:00
static struct group grp ;
2001-12-09 09:51:27 +03:00
static char * buf ;
static int buflen = 1000 ;
2001-12-03 06:53:57 +03:00
NSS_STATUS status ;
2001-12-09 09:51:27 +03:00
2003-06-16 07:30:53 +04:00
if ( ! _nss_getgrnam_r )
return NULL ;
if ( ! buf )
2006-07-11 22:01:26 +04:00
buf = SMB_MALLOC_ARRAY ( char , buflen ) ;
2001-12-09 09:51:27 +03:00
again :
status = _nss_getgrnam_r ( name , & grp , buf , buflen , & nss_errno ) ;
if ( status = = NSS_STATUS_TRYAGAIN ) {
buflen * = 2 ;
2006-07-11 22:01:26 +04:00
buf = SMB_REALLOC_ARRAY ( buf , char , buflen ) ;
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
if ( ! buf ) {
return NULL ;
}
2001-12-09 09:51:27 +03:00
goto again ;
}
2001-12-03 06:53:57 +03:00
if ( status = = NSS_STATUS_NOTFOUND ) {
2007-03-08 01:29:21 +03:00
SAFE_FREE ( buf ) ;
2001-12-03 06:53:57 +03:00
return NULL ;
}
2001-12-09 09:10:40 +03:00
if ( status ! = NSS_STATUS_SUCCESS ) {
report_nss_error ( " getgrnam " , status ) ;
2007-03-08 01:29:21 +03:00
SAFE_FREE ( buf ) ;
2001-12-03 06:53:57 +03:00
return NULL ;
}
return & grp ;
}
static struct group * nss_getgrgid ( gid_t gid )
{
NSS_STATUS ( * _nss_getgrgid_r ) ( gid_t , struct group * , char * ,
2006-07-11 22:01:26 +04:00
size_t , int * ) =
( NSS_STATUS ( * ) ( gid_t , struct group * , char * ,
size_t , int * ) ) find_fn ( " getgrgid_r " ) ;
2001-12-03 06:53:57 +03:00
static struct group grp ;
2001-12-09 09:51:27 +03:00
static char * buf ;
static int buflen = 1000 ;
2001-12-03 06:53:57 +03:00
NSS_STATUS status ;
2003-06-16 07:30:53 +04:00
if ( ! _nss_getgrgid_r )
return NULL ;
if ( ! buf )
2006-07-11 22:01:26 +04:00
buf = SMB_MALLOC_ARRAY ( char , buflen ) ;
2003-06-16 07:30:53 +04:00
2001-12-09 09:51:27 +03:00
again :
status = _nss_getgrgid_r ( gid , & grp , buf , buflen , & nss_errno ) ;
if ( status = = NSS_STATUS_TRYAGAIN ) {
buflen * = 2 ;
2006-07-11 22:01:26 +04:00
buf = SMB_REALLOC_ARRAY ( buf , char , buflen ) ;
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
if ( ! buf ) {
return NULL ;
}
2001-12-09 09:51:27 +03:00
goto again ;
}
2001-12-03 06:53:57 +03:00
if ( status = = NSS_STATUS_NOTFOUND ) {
2007-03-08 01:29:21 +03:00
SAFE_FREE ( buf ) ;
2001-12-03 06:53:57 +03:00
return NULL ;
}
2001-12-09 09:10:40 +03:00
if ( status ! = NSS_STATUS_SUCCESS ) {
report_nss_error ( " getgrgid " , status ) ;
2007-03-08 01:29:21 +03:00
SAFE_FREE ( buf ) ;
2001-12-03 06:53:57 +03:00
return NULL ;
}
return & grp ;
}
static void nss_setgrent ( void )
{
2006-07-11 22:01:26 +04:00
NSS_STATUS ( * _nss_setgrent ) ( void ) =
( NSS_STATUS ( * ) ( void ) ) find_fn ( " setgrent " ) ;
2001-12-09 09:10:40 +03:00
NSS_STATUS status ;
2003-06-16 07:30:53 +04:00
if ( ! _nss_setgrent )
return ;
2001-12-09 09:10:40 +03:00
status = _nss_setgrent ( ) ;
if ( status ! = NSS_STATUS_SUCCESS ) {
report_nss_error ( " setgrent " , status ) ;
}
2001-12-03 06:53:57 +03:00
}
static void nss_endgrent ( void )
{
2006-07-11 22:01:26 +04:00
NSS_STATUS ( * _nss_endgrent ) ( void ) =
( NSS_STATUS ( * ) ( void ) ) find_fn ( " endgrent " ) ;
2001-12-09 09:10:40 +03:00
NSS_STATUS status ;
2003-06-16 07:30:53 +04:00
if ( ! _nss_endgrent )
return ;
2001-12-09 09:10:40 +03:00
status = _nss_endgrent ( ) ;
if ( status ! = NSS_STATUS_SUCCESS ) {
report_nss_error ( " endgrent " , status ) ;
}
2001-12-03 06:53:57 +03:00
}
static int nss_initgroups ( char * user , gid_t group , gid_t * * groups , long int * start , long int * size )
{
NSS_STATUS ( * _nss_initgroups ) ( char * , gid_t , long int * ,
long int * , gid_t * * , long int , int * ) =
2006-07-11 22:01:26 +04:00
( NSS_STATUS ( * ) ( char * , gid_t , long int * ,
long int * , gid_t * * ,
long int , int * ) ) find_fn ( " initgroups_dyn " ) ;
2001-12-03 06:53:57 +03:00
NSS_STATUS status ;
2003-06-16 07:30:53 +04:00
if ( ! _nss_initgroups )
return NSS_STATUS_UNAVAIL ;
2001-12-09 09:10:40 +03:00
2001-12-03 06:53:57 +03:00
status = _nss_initgroups ( user , group , start , size , groups , 0 , & nss_errno ) ;
2001-12-09 09:10:40 +03:00
if ( status ! = NSS_STATUS_SUCCESS ) {
report_nss_error ( " initgroups " , status ) ;
}
2001-12-03 06:53:57 +03:00
return status ;
}
static void print_passwd ( struct passwd * pwd )
{
2003-07-22 08:31:20 +04:00
printf ( " %s:%s:%lu:%lu:%s:%s:%s \n " ,
2001-12-03 06:53:57 +03:00
pwd - > pw_name ,
pwd - > pw_passwd ,
2003-07-22 08:31:20 +04:00
( unsigned long ) pwd - > pw_uid ,
( unsigned long ) pwd - > pw_gid ,
2001-12-03 06:53:57 +03:00
pwd - > pw_gecos ,
pwd - > pw_dir ,
pwd - > pw_shell ) ;
}
static void print_group ( struct group * grp )
{
int i ;
2003-07-22 08:31:20 +04:00
printf ( " %s:%s:%lu: " ,
2001-12-03 06:53:57 +03:00
grp - > gr_name ,
grp - > gr_passwd ,
2003-07-22 08:31:20 +04:00
( unsigned long ) grp - > gr_gid ) ;
2001-12-03 06:53:57 +03:00
if ( ! grp - > gr_mem [ 0 ] ) {
printf ( " \n " ) ;
return ;
}
for ( i = 0 ; grp - > gr_mem [ i + 1 ] ; i + + ) {
printf ( " %s, " , grp - > gr_mem [ i ] ) ;
}
printf ( " %s \n " , grp - > gr_mem [ i ] ) ;
}
2003-06-20 06:31:15 +04:00
static void nss_test_initgroups ( char * name , gid_t gid )
2001-12-03 06:53:57 +03:00
{
long int size = 16 ;
long int start = 1 ;
gid_t * groups = NULL ;
int i ;
2001-12-09 09:10:40 +03:00
NSS_STATUS status ;
2001-12-03 06:53:57 +03:00
2005-05-07 10:59:00 +04:00
groups = SMB_MALLOC_ARRAY ( gid_t , size ) ;
2001-12-03 06:53:57 +03:00
groups [ 0 ] = gid ;
2001-12-09 09:10:40 +03:00
status = nss_initgroups ( name , gid , & groups , & start , & size ) ;
if ( status = = NSS_STATUS_UNAVAIL ) {
printf ( " No initgroups fn \n " ) ;
return ;
}
2001-12-03 06:53:57 +03:00
for ( i = 0 ; i < start - 1 ; i + + ) {
2003-07-22 08:31:20 +04:00
printf ( " %lu, " , ( unsigned long ) groups [ i ] ) ;
2001-12-03 06:53:57 +03:00
}
2003-07-22 08:31:20 +04:00
printf ( " %lu \n " , ( unsigned long ) groups [ i ] ) ;
2001-12-03 06:53:57 +03:00
}
2003-06-20 06:31:15 +04:00
static void nss_test_users ( void )
2001-12-03 06:53:57 +03:00
{
struct passwd * pwd ;
nss_setpwent ( ) ;
/* loop over all users */
while ( ( pwd = nss_getpwent ( ) ) ) {
printf ( " Testing user %s \n " , pwd - > pw_name ) ;
printf ( " getpwent: " ) ; print_passwd ( pwd ) ;
pwd = nss_getpwuid ( pwd - > pw_uid ) ;
2001-12-19 11:40:50 +03:00
if ( ! pwd ) {
total_errors + + ;
printf ( " ERROR: can't getpwuid \n " ) ;
continue ;
}
2001-12-03 06:53:57 +03:00
printf ( " getpwuid: " ) ; print_passwd ( pwd ) ;
2001-12-03 14:11:14 +03:00
pwd = nss_getpwnam ( pwd - > pw_name ) ;
2001-12-19 11:40:50 +03:00
if ( ! pwd ) {
total_errors + + ;
printf ( " ERROR: can't getpwnam \n " ) ;
continue ;
}
2001-12-03 14:11:14 +03:00
printf ( " getpwnam: " ) ; print_passwd ( pwd ) ;
2003-06-20 06:31:15 +04:00
printf ( " initgroups: " ) ; nss_test_initgroups ( pwd - > pw_name , pwd - > pw_gid ) ;
2001-12-03 06:53:57 +03:00
printf ( " \n " ) ;
}
nss_endpwent ( ) ;
}
2003-06-20 06:31:15 +04:00
static void nss_test_groups ( void )
2001-12-03 06:53:57 +03:00
{
struct group * grp ;
nss_setgrent ( ) ;
/* loop over all groups */
while ( ( grp = nss_getgrent ( ) ) ) {
printf ( " Testing group %s \n " , grp - > gr_name ) ;
printf ( " getgrent: " ) ; print_group ( grp ) ;
grp = nss_getgrnam ( grp - > gr_name ) ;
2001-12-19 11:40:50 +03:00
if ( ! grp ) {
total_errors + + ;
printf ( " ERROR: can't getgrnam \n " ) ;
continue ;
}
2001-12-03 06:53:57 +03:00
printf ( " getgrnam: " ) ; print_group ( grp ) ;
grp = nss_getgrgid ( grp - > gr_gid ) ;
2001-12-19 11:40:50 +03:00
if ( ! grp ) {
total_errors + + ;
printf ( " ERROR: can't getgrgid \n " ) ;
continue ;
}
2001-12-03 06:53:57 +03:00
printf ( " getgrgid: " ) ; print_group ( grp ) ;
printf ( " \n " ) ;
}
nss_endgrent ( ) ;
}
2003-06-20 06:31:15 +04:00
static void nss_test_errors ( void )
2001-12-09 09:51:27 +03:00
{
struct passwd * pwd ;
struct group * grp ;
pwd = getpwnam ( " nosuchname " ) ;
if ( pwd | | last_error ! = NSS_STATUS_NOTFOUND ) {
2001-12-09 10:18:59 +03:00
total_errors + + ;
2001-12-09 09:51:27 +03:00
printf ( " ERROR Non existant user gave error %d \n " , last_error ) ;
}
pwd = getpwuid ( 0xFFF0 ) ;
if ( pwd | | last_error ! = NSS_STATUS_NOTFOUND ) {
2001-12-09 10:18:59 +03:00
total_errors + + ;
2001-12-09 09:51:27 +03:00
printf ( " ERROR Non existant uid gave error %d \n " , last_error ) ;
}
grp = getgrnam ( " nosuchgroup " ) ;
if ( grp | | last_error ! = NSS_STATUS_NOTFOUND ) {
2001-12-09 10:18:59 +03:00
total_errors + + ;
2001-12-09 09:51:27 +03:00
printf ( " ERROR Non existant group gave error %d \n " , last_error ) ;
}
grp = getgrgid ( 0xFFF0 ) ;
if ( grp | | last_error ! = NSS_STATUS_NOTFOUND ) {
2001-12-09 10:18:59 +03:00
total_errors + + ;
2001-12-09 09:51:27 +03:00
printf ( " ERROR Non existant gid gave error %d \n " , last_error ) ;
}
}
2001-12-03 06:53:57 +03:00
int main ( int argc , char * argv [ ] )
{
if ( argc > 1 ) so_path = argv [ 1 ] ;
2001-12-07 01:42:27 +03:00
if ( argc > 2 ) nss_name = argv [ 2 ] ;
2001-12-03 06:53:57 +03:00
2003-06-20 06:31:15 +04:00
nss_test_users ( ) ;
nss_test_groups ( ) ;
nss_test_errors ( ) ;
2001-12-03 06:53:57 +03:00
2001-12-09 10:18:59 +03:00
printf ( " total_errors=%d \n " , total_errors ) ;
return total_errors ;
2001-12-03 06:53:57 +03:00
}