简体   繁体   中英

Can execute permissions of a perl script be restricted to call from another perl script?

I have 2 perl scripts, one of them calls the other (system() call). I do not want anything else to be able to execute that second perl script. Only the first perl script can run the second. The second perl script is a giant with lots of includes and forking and nested system calls of its own, so I'd like to avoid simply sticking it inside the first one as a subroutine (or anything like that). However, it would be OK to wrap that second perl script inside a perl module/package or similar. But the thing is I do not want any of the users of the first perl script to be able to execute the second perl script independently.

Is such a thing possible?
This is to be done on RHEL6.

I'm going to tag this with both perl and linux because I'm open to linux based solutions too. And I'll tag with permissions because that's at the heart of what I'm talking about. Note though that I do not have root.

To guard against accidental misuse, you could simply check the parent process id and compare its command line execution to what you expect, something like the following, which uses ps to find that information. But as long as the source code of your script is visible to other users, you can never truly prevent them from just copying/modifying it to suit their needs, so you may want to put a warning in the error message of why you think this is such a bad idea.

script1.pl and some_other_script.pl (both identical, with different names)

#!/usr/bin/env perl

use warnings;
use strict;

(system('./script2.pl') == 0)
    or die "Unable to run script2.pl!";

script2.pl

#!/usr/bin/env perl

use warnings;
use strict;

chomp(my $ppid = `ps -o ppid= -p $$`);
chomp(my $parent_command = `ps -o command= $ppid`);

die "script2.pl must only be called from script1.pl!"
    unless $parent_command =~ m|perl\s+script1\.pl$|; # as suggested by @zdim

print "Have some pi : 3.14159\n";

output

$ perl script1.pl
Have some pi : 3.14159

$ perl script2.pl
script2.pl must only be called from script1.pl! at script2.pl line 9.

$ perl some_other_script.pl
script2.pl must only be called from script1.pl! at ./script2.pl line 9.
Unable to run script2.pl! at some_other_script.pl line 6.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM