delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2014/03/21/05:35:33

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:message-id:date:from:mime-version:to:subject
:references:in-reply-to:content-type:content-transfer-encoding;
q=dns; s=default; b=k3BqoKq8Hzj+6tWuIksJG8fqeoxe0MoiO10DTsQqnVu
dlnCw9ivWmST9tcQ4DMOuiXZ+clsr77Mk19xQB0uiNYvFakZvMI4KqHEbJ3RkYnh
FkqjL7vSMtw3HD7FQW4JXsowkF3Cde2Z2meIEOxL8mLknqAsYAP4ayAxte5hogAU
=
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:message-id:date:from:mime-version:to:subject
:references:in-reply-to:content-type:content-transfer-encoding;
s=default; bh=J7GLmWZnawukD9NjAOfXuQ2PWts=; b=Ok82qCU5FFskeEY+p
EaRsw1DBCQ/2arxzUzqxuzO0ny5IEruKhRalGJ6qwhOejyla6oSogSXhdBS4zcQr
YU1r61Qfrl/0ioGcbB+r6846D6JhzD0ZC5jYQEUTqThxLdIAigMl0kfPmlI+6sI1
nnnHtj5Q/RRacNvcsv/vBDBmaQ=
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
Authentication-Results: sourceware.org; auth=none
X-Virus-Found: No
X-Spam-SWARE-Status: No, score=-1.2 required=5.0 tests=AWL,BAYES_00,T_RP_MATCHES_RCVD autolearn=ham version=3.3.2
X-HELO: Ishtar.tlinx.org
Message-ID: <532C07C6.9060506@tlinx.org>
Date: Fri, 21 Mar 2014 02:35:02 -0700
From: Linda Walsh <cygwin AT tlinx DOT org>
User-Agent: Thunderbird
MIME-Version: 1.0
To: cygwin AT cygwin DOT com
Subject: Re: Getting groups you belong to in perl
References: <09C6BA32B7B1654B8AB0CAF234F2A1C114E65CBA AT A04066 DOT BGC DOT NET> <1797360578 DOT 20140314023749 AT yandex DOT ru> <09C6BA32B7B1654B8AB0CAF234F2A1C114E65FAE AT A04066 DOT BGC DOT NET> <1247713652 DOT 20140314170446 AT yandex DOT ru> <20140314163026 DOT GB2355 AT calimero DOT vinschen DOT de> <692018215 DOT 20140314203823 AT yandex DOT ru>
In-Reply-To: <692018215.20140314203823@yandex.ru>
X-IsSubscribed: yes

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

- Raw text -


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