This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Why does CYGWIN implementation of getgrouplist() verify the supplied user or gid?


Hello,

Per a hint I've got earlier today from this list (and which I appreciate!),
I've tried to use getgrouplist() to obtain group information about a user.
There's a problem with that.

I notice that the actual behavior of this call deviates from
what's documented:  nowhere in the documentation that I found,
it states that the call fails for non-existent user and / or group ID
provided in the parameters;  but only if there is insufficient space
to store the resultant group IDs (one place specifically mentions that
"if user does not refer to a valid user on the system, getgrouplist()
shall return 0, and set the value referenced by ngroups to 0").

I checked the attached test case on Linux, Free BSD and, oh well,
Mac OS X, and the results are pretty consistent:

for non-existent user, the call does _not_ fail (i.e. does not return -1)
but results in the list of GIDs containing only the GID passed in the
parameters, regardless whether or not that GID is valid (i.e. of an
existing group).

If the user does exist, the provided GID gets added to the list, if it
does not happen to be there already (again, the GID is not checked
for validity).

On Cygwin, the call always fails with return code -1 for nonexistent
user name and / or GID.

Note that errnos are not particularly documented for this call, so the
only reason for it to fail, is for insufficient buffer (otherwise, the
failure reasons cannot be distinguished)!  To work around this
behavior I have to bulk the code with a number of '#ifdef CYGWIN'.

Why it's being different on CYGWIN?

Thanks,

Anton Lavrentiev
Contractor NIH/NLM/NCBI


#define _BSD_SOURCE 1
#include <grp.h>
#include <limits.h>
#include <pwd.h>
#include <stdio.h>
#include <unistd.h>


#define BUG
#ifdef BUG
#define GROUP (-1L)
#else
#define GROUP 0
#endif


// Use your login name as a command-line parameter
int main(int argc, char* argv[])
{
    static gid_t groups[NGROUPS_MAX + 1];
    int ngroups = sizeof(groups) / sizeof(groups[0]);
    int rv = getgrouplist(argv[1], (gid_t) GROUP, groups, &ngroups);

    if (rv < 0)
        printf("getgrouplist(): %m, %d\n", ngroups);
    else while (ngroups)
        printf("  %u\n", (unsigned int) groups[--ngroups]);
    return 0;
}


Compilation:
gcc -Wall getgrouplist.c -o getgrouplist


Execution:
./getgrouplist $USER


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]