logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

Privileges::Drop - A module to make it simple to drop all privileges, even POSIX groups.

Author

       Troels Liebe Bentsen <tlb@rapanden.dk>

Description

       This module tries to simplify the process of dropping privileges. This can be useful when your Perl
       program needs to bind to privileged ports, etc. This module is much like Proc::UID, except that it's
       implemented in pure Perl.  Special care has been taken to also drop saved uid on platforms that support
       this, currently only test on on Linux.

Methods

       drop_uidgid($uid, $gid, @groups)
           Drops privileges and sets euid/uid to $uid and egid/gid to $gid.

           Supplementary groups can be set in @groups.

       drop_privileges($user)
           Drops  privileges  to the $user, looking up gid and uid with getpwname and calling drop_uidgid() with
           these arguments.

           The environment variables USER, LOGNAME, HOME and SHELL are  also  set  to  the  values  returned  by
           getpwname.

           Returns the $uid and $gid on success and dies on error.

           NOTE: If drop_privileges() is called when you don't have root privileges it will just return undef;

Name

       Privileges::Drop - A module to make it simple to drop all privileges, even POSIX groups.

Notes

       As  this  module  only  uses  Perl's  built-in functions, it relies on them to work correctly. That means
       setting $GID and $EGID should also call setgroups(), something that might not have been the  case  before
       Perl 5.004. So if you are running an older version, Proc::UID might be a better choice.

Synopsis

         use Privileges::Drop;

         # Do privileged stuff

         # Drops privileges and sets euid/uid to 1000 and egid/gid to 1000.
         drop_uidgid(1000, 1000);

         # Drop privileges to user nobody looking up gid and uid with getpwname
         # This also set the environment variables USER, LOGNAME, HOME and SHELL.
         drop_privileges('nobody');

See Also