getpwnam_r returns EINVAL on FreeBSD 8.3
Dan Lists
lists.dan at gmail.com
Mon Dec 3 23:54:26 UTC 2012
After upgrading a server from FreeBSD 7.3 to FreeBSD 8.3 I noticed
this bug. Since upgrading, getpwnam_r is acting inconsistently. If I
look up a user that does not exist and the name is 16 characters or
less, getpwnam_r returns 0 and the result is NULL. If the name is
more than 16 characters, getpwnam_r returns EINVAL. Everything works
correctly for users that exist.
This only happens when the nsswitch.conf passwd: line contains files.
You need to use files if you are using another module such as msql or
ldap. The problem exists without the other modules listed. For
example:
passwd: files
Below is a simple test program. Set passwd: to files in nsswitch.conf
and run the program. Any idea how to fix this bug with getpwnam_r?
#include <stdio.h>
#include <sys/types.h>
#include <pwd.h>
main()
{
lookup("doesnotexistXXXX");
lookup("doesnotexistXXXXy");
}
int lookup( char *name)
{
struct passwd pwd;
char buffer[1024];
struct passwd *result;
int err;
printf("\nLooking up: %s\n", name);
err = getpwnam_r(name, &pwd, buffer, sizeof(buffer), &result);
if( err != 0 ){
printf("Return code: %d\n", err);
}else if( result == 0 ){
printf("Returned no result!\n");
}else{
printf("Returned: %s (%d)\n", result->pw_name, result->pw_uid);
}
}
More information about the freebsd-questions
mailing list