getpwnam_r returns EINVAL on FreeBSD 8.3

Dan Lists lists.dan at gmail.com
Wed Dec 5 17:27:14 UTC 2012


On Mon, Dec 3, 2012 at 5:54 PM, Dan Lists <lists.dan at gmail.com> wrote:
> 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.

I was incorrect.  The behavior of getpwnam_r is the same on 7.3 and
8.3.  The software I was testing acted differently on the two
versions.

> 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

I would like to emphasize that this does NOT happen when passwd: is
set to compat.  I believe this is a bug.  getpwnam_r should have the
same return values (or errno for getpwnam) whether nsswitch.conf has
compat or files.  If there is a really good reason for them to be
different, it should be documented.

> 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