Skip to content

Instantly share code, notes, and snippets.

Last active November 26, 2018 11:01
What would you like to do?
setuidgid w. support for supplementary groups
#! /usr/bin/perl
use POSIX qw(setuid setgid);
use Unix::Groups qw(setgroups);
die "usage: setusergroups username child\n"
unless @ARGV >= 2;
my $username = shift @ARGV;
# get user entry
my @userent = getpwnam($username)
or die "unknown user: $username\n";
# build list of supp. groups
my @supp_groups;
while (my @e = getgrent) {
if (grep { $_ eq $username } split /\s+/, $e[3]) {
push @supp_groups, $e[2];
# setgid
or die "setgid failed:$!";
# setgroups!
or die "setgroups failed:$!";
# setuid
or die "setuid failed:$!";
# exec
exec @ARGV
or die "failed to exec: $ARGV[0]:$!";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment