Now comes the question of how to determine the calling user. `sudo -E` allows us to pass environmental variables through sudo. Here is an example:
ben@******:~$ sudo -E whoamiThe problem with doing this in Perl however seems to be that when you do a system() call, Perl opens a new subshell, which for some reason recognizes the $USER environmental variable as 'root', though it keeps some of the other variables.
root
ben@******:~$ sudo -E echo $USER
ben
To work around this issue, we can do as follows:
#!/usr/bin/perlNow the subscript can determine who the "backup" user is in a fairly agnostic way, and just grant permissions, rather than smiting the directory itself. It performs the minimum necessary action with superuser privileges, which should be the goal.
# script.pl
use strict;
$ENV{'IAM'} = $ENV{'USER'};
system('sudo -E /usr/local/bin/subscript.pl');
...
#!/usr/bin/perl
# subscript.pl
use strict;
my $iam = `echo -n \$IAM`;
say $iam;
You can even `chmod` subscript.pl to 750 and then just add an entry to your /etc/sudoers file like this:
backup ALL=(ALL) SETENV:NOPASSWD: /usr/local/bin/subscript.plI'm certain it would be possible to lock this process down even more if necessary. Your mileage may vary with older versions of sudo.

