public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* Getting groups you belong to in perl
@ 2014-03-14 10:25 PANEL Vincent (CIS/SIN)
  2014-03-14 11:37 ` Andrey Repin
  0 siblings, 1 reply; 7+ messages in thread
From: PANEL Vincent (CIS/SIN) @ 2014-03-14 10:25 UTC (permalink / raw)
  To: cygwin

Hello,

Don't know if this list is more appropriate than the Perl one but my question is actually about porting a Perl script to Cygwin. I need to check if the current user running the script belongs to a pre-defined group.

Under *nix, I get the list of users belonging to the group and see if the current user is in this list. Cygwin doesn't allow this way of working. I found out by reading the thread "Why mkgroup does not list group members?" on this mailing list (1 message on Mon, 13 May 2013 20:29:52, for instance).

I would like to use perl commands without launching external commands, if possible. The way I've found until now is by using the output of the "id" command but I was wondering if there was another way to do it. How is "id" command working by the way ?

Regards,

________________________________

***** Disclaimer *****
http://www.belgacom.be/maildisclaimer

--
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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Getting groups you belong to in perl
  2014-03-14 10:25 Getting groups you belong to in perl PANEL Vincent (CIS/SIN)
@ 2014-03-14 11:37 ` Andrey Repin
  2014-03-14 13:05   ` PANEL Vincent (CIS/SIN)
  0 siblings, 1 reply; 7+ messages in thread
From: Andrey Repin @ 2014-03-14 11:37 UTC (permalink / raw)
  To: PANEL Vincent (CIS/SIN), cygwin

Greetings, PANEL Vincent (CIS/SIN)!

> Don't know if this list is more appropriate than the Perl one but my
> question is actually about porting a Perl script to Cygwin. I need to check
> if the current user running the script belongs to a pre-defined group.

> Under *nix, I get the list of users belonging to the group and see if the
> current user is in this list.

How exactly you are doing this? (I hope you're not reading it from /etc/group,
because that file may not exist at all, or contain exactly zero relevant
information.)

> Cygwin doesn't allow this way of working.

Oh... ?

> I found out by reading the thread "Why mkgroup does not list group members?"
> on this mailing list (1 message on Mon, 13 May 2013 20:29:52, for instance).

> I would like to use perl commands without launching external commands, if
> possible. The way I've found until now is by using the output of the "id"
> command but I was wondering if there was another way to do it. How is "id"
> command working by the way ?

You can check the sources of it, it's really a very simple tool.
(It's coreutils, by the way.
http://mirrors.kernel.org/sourceware/cygwin/x86/release/coreutils/ )


--
WBR,
Andrey Repin (anrdaemon@yandex.ru) 14.03.2014, <02:12>

Sorry for my terrible english...


--
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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: Getting groups you belong to in perl
  2014-03-14 11:37 ` Andrey Repin
@ 2014-03-14 13:05   ` PANEL Vincent (CIS/SIN)
  2014-03-14 14:00     ` Andrey Repin
  0 siblings, 1 reply; 7+ messages in thread
From: PANEL Vincent (CIS/SIN) @ 2014-03-14 13:05 UTC (permalink / raw)
  To: cygwin

From: Andrey Repin[]
Sent: Thursday 13 March 2014 23:38
To: PANEL Vincent (CIS/SIN);
Subject: Re: Getting groups you belong to in perl

>Greetings, PANEL Vincent (CIS/SIN)!

>> Don't know if this list is more appropriate than the Perl one but my
>> question is actually about porting a Perl script to Cygwin. I need to
>> check if the current user running the script belongs to a pre-defined group.

>> Under *nix, I get the list of users belonging to the group and see if
>> the current user is in this list.

>How exactly you are doing this? (I hope you're not reading it from
>/etc/group, because that file may not exist at all, or contain exactly
>zero relevant
>information.)

I'm using standard perl commands (getgrnam, getlogin, etc... : http://perldoc.perl.org/5.14.2/functions/getgrnam.html) which are probably using standard C libraries in the background (I hope). The doc explicitly says "The $members value returned by getgr*() is a space-separated list of the login names of the members of the group" but it's always empty under Cygwin.

>> Cygwin doesn't allow this way of working.

>Oh... ?

Yes indeed, the standard perl functions work but they always return an empty list of group members, similar to mkgroup.

>> I found out by reading the thread "Why mkgroup does not list group members?"
>> on this mailing list (1 message on Mon, 13 May 2013 20:29:52, for instance).

>> I would like to use perl commands without launching external
>> commands, if possible. The way I've found until now is by using the output of the "id"
>> command but I was wondering if there was another way to do it. How is "id"
>> command working by the way ?

>You can check the sources of it, it's really a very simple tool.
>(It's coreutils, by the way.
>http://mirrors.kernel.org/sourceware/cygwin/x86/release/coreutils/ )

I will do it.

--
WBR,
Andrey Repin () 14.03.2014, <02:12>

Sorry for my terrible english...


________________________________

***** Disclaimer *****
http://www.belgacom.be/maildisclaimer

--
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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Getting groups you belong to in perl
  2014-03-14 13:05   ` PANEL Vincent (CIS/SIN)
@ 2014-03-14 14:00     ` Andrey Repin
  2014-03-14 16:50       ` Corinna Vinschen
  0 siblings, 1 reply; 7+ messages in thread
From: Andrey Repin @ 2014-03-14 14:00 UTC (permalink / raw)
  To: PANEL Vincent (CIS/SIN), cygwin

Greetings, PANEL Vincent (CIS/SIN)!

>>> Don't know if this list is more appropriate than the Perl one but my
>>> question is actually about porting a Perl script to Cygwin. I need to
>>> check if the current user running the script belongs to a pre-defined group.

>>> Under *nix, I get the list of users belonging to the group and see if
>>> the current user is in this list.

>>How exactly you are doing this? (I hope you're not reading it from
>>/etc/group, because that file may not exist at all, or contain exactly
>>zero relevant
>>information.)

> I'm using standard perl commands (getgrnam, getlogin, etc... :
> http://perldoc.perl.org/5.14.2/functions/getgrnam.html) which are probably
> using standard C libraries in the background (I hope). The doc explicitly
> says "The $members value returned by getgr*() is a space-separated list of
> the login names of the members of the group" but it's always empty under
> Cygwin. 

The functionality you're looking for may(or may not) be available soon(tm).
Corinna doing some rework of the core user/group handling as we speak.
Check the "Testers needed: New passwd/group handling in Cygwin" thread(s).
Do note that in the light of upcoming 1.29 release, snapshots do not contain
the code in question. You'd need to rebuild DLL from CVS to get them.

>>> Cygwin doesn't allow this way of working.

>>Oh... ?

> Yes indeed, the standard perl functions work but they always return an empty
> list of group members, similar to mkgroup.

Makes sense, now, that you explained it.


--
WBR,
Andrey Repin (anrdaemon@yandex.ru) 14.03.2014, <16:58>

Sorry for my terrible english...


--
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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Getting groups you belong to in perl
  2014-03-14 14:00     ` Andrey Repin
@ 2014-03-14 16:50       ` Corinna Vinschen
  2014-03-14 18:45         ` Andrey Repin
  0 siblings, 1 reply; 7+ messages in thread
From: Corinna Vinschen @ 2014-03-14 16:50 UTC (permalink / raw)
  To: cygwin

[-- Attachment #1: Type: text/plain, Size: 2132 bytes --]

On Mar 14 17:04, Andrey Repin wrote:
> Greetings, PANEL Vincent (CIS/SIN)!
> 
> >>> Don't know if this list is more appropriate than the Perl one but my
> >>> question is actually about porting a Perl script to Cygwin. I need to
> >>> check if the current user running the script belongs to a pre-defined group.
> 
> >>> Under *nix, I get the list of users belonging to the group and see if
> >>> the current user is in this list.
> 
> >>How exactly you are doing this? (I hope you're not reading it from
> >>/etc/group, because that file may not exist at all, or contain exactly
> >>zero relevant
> >>information.)
> 
> > I'm using standard perl commands (getgrnam, getlogin, etc... :
> > http://perldoc.perl.org/5.14.2/functions/getgrnam.html) which are probably
> > using standard C libraries in the background (I hope). The doc explicitly
> > says "The $members value returned by getgr*() is a space-separated list of
> > the login names of the members of the group" but it's always empty under
> > Cygwin. 
> 
> The functionality you're looking for may(or may not) be available soon(tm).
> Corinna doing some rework of the core user/group handling as we speak.

No, sorry.  You don't know it yet, but you really don't want that.  The
cost to generate full gr_mem info from the Windows user DBs in terms of
performance is prohibitive.  And it's really not required information
because there are other ways to fetch this info only if it's really
required:

- For the current user, use getgroups(2):

    http://linux.die.net/man/2/getgroups

- For another account use getgrouplist(3):

    http://linux.die.net/man/3/getgrouplist

> Check the "Testers needed: New passwd/group handling in Cygwin" thread(s).
> Do note that in the light of upcoming 1.29 release, snapshots do not contain
> the code in question. You'd need to rebuild DLL from CVS to get them.

Or just use the older 2014-03-05 snapshot for the time being.


Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Maintainer                 cygwin AT cygwin DOT com
Red Hat

[-- Attachment #2: Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Getting groups you belong to in perl
  2014-03-14 16:50       ` Corinna Vinschen
@ 2014-03-14 18:45         ` Andrey Repin
  2014-03-21 11:13           ` Linda Walsh
  0 siblings, 1 reply; 7+ messages in thread
From: Andrey Repin @ 2014-03-14 18:45 UTC (permalink / raw)
  To: Corinna Vinschen

Greetings, Corinna Vinschen!

>> >>> Don't know if this list is more appropriate than the Perl one but my
>> >>> question is actually about porting a Perl script to Cygwin. I need to
>> >>> check if the current user running the script belongs to a pre-defined group.
>> 
>> >>> Under *nix, I get the list of users belonging to the group and see if
>> >>> the current user is in this list.
>> 
>> >>How exactly you are doing this? (I hope you're not reading it from
>> >>/etc/group, because that file may not exist at all, or contain exactly
>> >>zero relevant
>> >>information.)
>> 
>> > I'm using standard perl commands (getgrnam, getlogin, etc... :
>> > http://perldoc.perl.org/5.14.2/functions/getgrnam.html) which are probably
>> > using standard C libraries in the background (I hope). The doc explicitly
>> > says "The $members value returned by getgr*() is a space-separated list of
>> > the login names of the members of the group" but it's always empty under
>> > Cygwin. 
>> 
>> The functionality you're looking for may(or may not) be available soon(tm).
>> Corinna doing some rework of the core user/group handling as we speak.

> No, sorry.  You don't know it yet, but you really don't want that.  The
> cost to generate full gr_mem info from the Windows user DBs in terms of
> performance is prohibitive.

I've had an itch about it, but I have no tools to check this functionality
myself. Thanks for confirmation.

> And it's really not required information
> because there are other ways to fetch this info only if it's really
> required:

> - For the current user, use getgroups(2):

>     http://linux.die.net/man/2/getgroups

> - For another account use getgrouplist(3):

>     http://linux.die.net/man/3/getgrouplist

>> Check the "Testers needed: New passwd/group handling in Cygwin" thread(s).
>> Do note that in the light of upcoming 1.29 release, snapshots do not contain
>> the code in question. You'd need to rebuild DLL from CVS to get them.

> Or just use the older 2014-03-05 snapshot for the time being.

Indeed, I did mean "current snapshots". Sorry.


--
WBR,
Andrey Repin (anrdaemon@yandex.ru) 14.03.2014, <20:37>

Sorry for my terrible english...


--
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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Getting groups you belong to in perl
  2014-03-14 18:45         ` Andrey Repin
@ 2014-03-21 11:13           ` Linda Walsh
  0 siblings, 0 replies; 7+ messages in thread
From: Linda Walsh @ 2014-03-21 11:13 UTC (permalink / raw)
  To: cygwin

Andrey Repin wrote:
> Greetings, Corinna Vinschen!
> 
>>>>>> Don't know if this list is more appropriate than the Perl one but my
>>>>>> question is actually about porting a Perl script to Cygwin. I need to
>>>>>> check if the current user running the script belongs to a pre-defined group.
----
FWIW, I run a bash script at logon to populate a bash array
called $_GROUPS_.  I mention it, because you could do similarly
in perl if other methods don't work.



> echo ${_GROUPS_[@]}
1053 517 512 545 11612288 518 519 513 544 520 201 260
echo -E ${!_GROUPS_[@]}
Bliss\Trusted Local Net Users Bliss\Cert Publishers Bliss\Domain Admins Users 
High Mandatory Level Bliss\Schema Admins Bliss\Enterprise Admins None 
Administrators Bliss\Group Policy Creator Owners lawgroup
--
It uses the cygwin 'id' program.

The array in the env is a convenience -- not a secure list.
Better to parse the output of id.

My bash routine is called via my 'bash_env' because bash currently
does not propagate arrays -- so the array is stored in a
string that gets re-expanded w/each call.

Theoretically the parsing of 'id' only happens at login.
I put them in an array, because if they are just in a 'string',
it's hard to tell the groups apart as they have spaces
in the names as well as spaces between the names...

---
The bash code ( I use to parse "id", FWIW):
----
# need to parse output of id, as it is only place we can notice groups
# with spaces in them! NOTE -- this must be done during 'bash_env' time

if [[ -z ${_GPSAFE_:-""} ]] 2>/dev/null ; then
   unset _GROUPS_
   typeset -Ax _GROUPS_

   function _idparse {
     # parse output of id:
     # uid=##(name) gid=##(name) groups=##(name)[,##(name)]...
     # Note, names may be 'absent' (in which case there are no parens).
     # grouplist is *comma* separated! -- but only on output of 'id';
     # I don't "idnames" can start with a number... (we'll assume not)
     # if group has no name, put it's number in as it's name
     # else you wouldn't see you are in those groups

     while read id ; do
       [[ $id =~ .?id= ]] && continue; #skip over uid/gid entries
       id="${id/\(/ }"
       id="${id/\)/}"
       # next line should work -- another bash bug...
       #[[ $id =~ ^[0-9]+$ ]] && id="$id $id"  #dup ID into missing name field
       gid="${id%% *}"
       name="${id#$gid}"
       name=${name##\ }
       [[ -z $name ]] && name="$gid"
       _GROUPS_[$name]="$gid"
     done  < <(id|sed -r '
       s/\) gid/\)\ngid/ ; s/\) groups/\)\ngroups/ ; s/\\/\\\\/g ;
       s/\),([0-9])/)\n\1/g ; s/\b([0-9]+),/\1\n/g ; s/groups=//')
   }
   _idparse

   unset _GPSAFE_ 2>/dev/null ||:
   typeset -xr _GPSAFE_=$(typeset -p _GROUPS_) ||: # save away for future
else
   eval  $_GPSAFE_
fi
-----------------------------------------------
Then I use it:

if [[ -n "${_GROUPS_[wheel]:-""}" ||
       -n "${_GROUPS_[root]:-""}" ||
       -n "${_GROUPS_[Administrators]:-""}" ]] ; then
   _path_prepend PATH /usr/local/sbin /usr/sbin /sbin
fi

----
It's a hack for convenience, though if the perl routines don't work,
it might be the quickest solution....

You could also look in the code of 'id' to see how it gets
the groups (since it works... ;-))....




--
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

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2014-03-21  9:35 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-03-14 10:25 Getting groups you belong to in perl PANEL Vincent (CIS/SIN)
2014-03-14 11:37 ` Andrey Repin
2014-03-14 13:05   ` PANEL Vincent (CIS/SIN)
2014-03-14 14:00     ` Andrey Repin
2014-03-14 16:50       ` Corinna Vinschen
2014-03-14 18:45         ` Andrey Repin
2014-03-21 11:13           ` Linda Walsh

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).