Here's my concept code -- is this the right way? $switchRealUID will be true by default.

sub switchUsers { my ($uname,$gname)=@_; $>=0; if($> != 0) { mlog('',"requested to switch to user/group '$uname/$gname' but canno +t set effective uid to 0 -- quitting; uid is $>"); die "requested to switch to user/group '$uname/$gname' but cannot se +t effective uid to 0 -- quitting; uid is $>\n"; } $<=0; if($gname) { my $gid = getgrnam($gname); if(defined $gid) { if($switchRealUID) { $(=$gid; if($(+0==$gid) { mlog('',"Switched real gid to $gid ($gname)"); } else { mlog('',"Failed to switch real gid to $gid ($gname) -- real uid=$ +("); } } $)=$gid; if($)+0==$gid) { mlog('',"Switched effective gid to $gid ($gname)"); } else { mlog('',"Failed to switch effective gid to $gid ($gname) -- effect +ive gid=$) -- quitting"); die "Failed to switch effective gid to $gid ($gname) -- effective +gid=$) -- quitting"; } } else { mlog('',"Could not find gid for group '$gname' -- not switching eff +ective gid -- quitting"); die "Could not find gid for group '$gname' -- not switching effecti +ve gid -- quitting"; } } if($uname) { my $uid = getpwnam($uname); if(defined $uid) { if($switchRealUID) { $<=$uid; if($<==$uid) { mlog('',"Switched real uid to $uid ($uname)"); } else { mlog('',"Failed to switch real uid to $uid ($uname) -- real uid=$ +<"); } } $>=$uid; if($>==$uid) { mlog('',"Switched effective uid to $uid ($uname)"); } else { mlog('',"Failed to switch effective uid to $uid ($uname) -- real u +id=$< -- quitting"); die "Failed to switch effective uid to $uid ($uname) -- real uid=$ +< -- quitting"; } } else { mlog('',"Could not find uid for user '$uname' -- not switching effe +ctive uid -- quitting"); die "Could not find uid for user '$uname' -- not switching effectiv +e uid -- quitting"; } } }

In reply to Re: Changing user and GROUP id for security? by jhanna
in thread Changing user and GROUP id for security? by jhanna

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.