in reply to Re: Retrieve unix group membership for a uid in a perl CGI
in thread Retrieve unix group membership for a uid in a perl CGI

This misses the user's own group. Try
my $username = "kvale"; my @groups = ($username); open GROUP, "</etc/group" or die "Could not find /etc/group: $!\n"; while (<GROUP>) { my ($group,$x,$gid,$list)=split(':',$_); push(@groups,$group) if ($list=~/$username/); } print "Groups: ", join " ", @groups, "\n";

-Mark

Replies are listed 'Best First'.
Re: Re: Re: Retrieve unix group membership for a uid in a perl CGI
by Fletch (Bishop) on Apr 09, 2004 at 18:31 UTC

    Actually yours (possibly mistakenly) assumes that the user's primary group is named after their username. While this is a common default value used by some variants of adduser or the like, it's not necessarily always the case. Which is of course why you should use getpwname to retrieve their primary gid and then translate that with getgrgid.

Re: Re: Re: Retrieve unix group membership for a uid in a perl CGI
by matija (Priest) on Apr 09, 2004 at 18:24 UTC
    Huh? I don't see any difference between my loop and yours...

    All you've done is provide the opening of the file, which I left as an exercise for the student

      Not true. I added the username to the @groups, which your parse misses. My /etc/group file looks something like
      kvale:x:501: cvs::502:kvale
      with no kvale user at the end of the kvale group. Note my solution is just a hack, because giving each user their own group is the default on my system.

      -Mark