1753935 Members
9977 Online
108811 Solutions
New Discussion юеВ

Perl Question

 
RadhikaHemanth
Occasional Contributor

Perl Question

Hi I am using
1) $)=(getgrnam('vsm'))[2];
2) $>=(getpwnam('postgres'))[2];
in my application.
Can anyone help me understand these two statements
4 REPLIES 4
H.Merijn Brand (procura
Honored Contributor

Re: Perl Question

don't assign to $) and $> as they are predefined magic variables

$ man perlvar
:
$) The effective gid of this process. If you are on a machine
that supports membership in multiple groups simultaneously,
gives a space separated list of groups you are in. The first
number is the one returned by getegid (), and the subsequent
ones by getgroups (), one of which may be the same as the first
number.

Similarly, a value assigned to $) must also be a space-
separated list of numbers. The first number sets the effective
gid, and the rest (if any) are passed to setgroups (). To get
the effect of an empty list for setgroups (), just repeat the
new effective gid; that is, to force an effective gid of 5 and
an effectively empty setgroups () list, say " $) = "5 5" ".

You can change both the effective gid and the real gid at the
same time by using POSIX::setgid () (use only a single numeric
argument). Changes to $) require a check to $! to detect any
possible errors after an attempted change.

(Mnemonic: parentheses are used to group things. The effective
gid is the group that's right for you, if you're running
setgid.)

$<, $>, $( and $) can be set only on machines that support the
corresponding set[re][ug]id() routine. $( and $) can be
swapped only on machines supporting setregid ().
:
$> The effective uid of this process. Example:

$< = $>; # set real to effective uid
($<,$>) = ($>,$<); # swap real and effective uid

You can change both the effective uid and the real uid at the
same time by using POSIX::setuid (). Changes to $> require a
check to $! to detect any possible errors after an attempted
change.

(Mnemonic: it's the uid you went to, if you're running setuid.)
$< and $> can be swapped only on machines supporting
setreuid ().

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Ralph Grothe
Honored Contributor

Re: Perl Question

Hi Radhika,

as Merijn already wrote,
the only need to assign to these Perl special variables was if you wanted to drop privileges in an e.g. forked off child process.
Your assignments would slice off the 2nd element of the lists returned from getgrnam() for a group 'vsm' and getpwnam() for a user 'postgres', which as you can find out by "perldoc -f getpwent" should return the gid and the uid respectively.
Besides, the slicing looks redundant anyway because getgrnam() would return the gid, and getpwnam() the uid if both were used in scalar context, as in your example.
Madness, thy name is system administration
RadhikaHemanth
Occasional Contributor

Re: Perl Question

Hi
Thanks for that detailed information but I wanted to know the use of these statements in short I dont much understand about effective uid setreid and all .
Can you simply tell what are these statements meant for
RadhikaHemanth
Occasional Contributor

Re: Perl Question

As I have understood is it used for setting the group name and user name to vsm and postgres respectively whenever it is required