delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2013/05/13/16:51:56

X-Recipient: archive-cygwin AT delorie DOT com
DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:from:to:subject:date:message-id:content-type
:content-transfer-encoding:mime-version; q=dns; s=default; b=FPD
01R2oLKxzJRUh7piU6c8AAyZX/fFGoO4j9wgEmF3MFfVFpbjCSyiEAccaIzVnnH5
CpvBzPnKlLoyJ0oLGapmtflTebm3cnFWdFm3tANeF28iYwAHSCXhvY6R8JrkeKsg
qCrn72dVpD9EPhxU2DOBnuFeXU6zZ+aCaFwaoR2g=
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:from:to:subject:date:message-id:content-type
:content-transfer-encoding:mime-version; s=default; bh=139JSg23Q
yusW0CCXsLXPXbiYfc=; b=pwAYHKuzV29bcK9T4vyExMLhIDfBVHyJmAgvtX9R7
mjLdKk50WHk2wJR3DAKaGJJgELRrbZA8itS/GVndNZP8p2baoJnWz2w3Sqwh4cQ0
rkbV+ukaiEJDQ+8qRQOS6v/VBCOybGMkX6FSPzasjopXSAIayXZSv36h8C8PFc7T
Io=
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
X-Spam-SWARE-Status: No, score=-3.5 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_MED,RCVD_IN_HOSTKARMA_W,RP_MATCHES_RCVD,SPF_HELO_PASS,SPF_PASS autolearn=ham version=3.3.1
X-IronPortListener: Outbound_SMTP
X-IronPort-Anti-Spam-Filtered: true
X-IronPort-Anti-Spam-Result: AiAFAEVQkVGcKEez/2dsb2JhbABagwfAaIELFm0HgiEBBBIoUQEVFQwBBy8TJgEEGxqHap0ZhFSaRI53SgEMglVhA44bj0qKfIFXgTiBawcXHg
From: "Lavrentiev, Anton (NIH/NLM/NCBI) [C]" <lavr AT ncbi DOT nlm DOT nih DOT gov>
To: "cygwin AT cygwin DOT com" <cygwin AT cygwin DOT com>
Subject: Why does CYGWIN implementation of getgrouplist() verify the supplied user or gid?
Date: Mon, 13 May 2013 20:51:35 +0000
Message-ID: <5F8AAC04F9616747BC4CC0E803D5907D0AF79776@MLBXv04.nih.gov>
MIME-Version: 1.0
X-MIME-Autoconverted: from quoted-printable to 8bit by delorie.com id r4DKpoZR013760

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


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019