简体   繁体   English

是否可以将一个Perl脚本的执行权限限制为只能从另一个Perl脚本调用?

[英]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). 我有2个perl脚本,其中一个调用另一个(system()调用)。 I do not want anything else to be able to execute that second perl script. 我不希望其他任何东西能够执行第二个Perl脚本。 Only the first perl script can run the second. 只有第一个perl脚本可以运行第二个。 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). 第二个Perl脚本是一个庞大的脚本,它具有很多包含自身的派生,派生和嵌套系统调用,因此我想避免将它作为子例程(或类似的东西)简单地粘贴在第一个脚本中。 However, it would be OK to wrap that second perl script inside a perl module/package or similar. 但是,可以将第二个perl脚本包装在perl模块/软件包或类似文件中。 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. 但是,我不希望第一个Perl脚本的任何用户能够独立执行第二个Perl脚本。

Is such a thing possible? 这样的事情可能吗?
This is to be done on RHEL6. 这将在RHEL6上完成。

I'm going to tag this with both perl and linux because I'm open to linux based solutions too. 我将同时使用perl和linux对其进行标记,因为我也对基于Linux的解决方案持开放态度。 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. 请注意,尽管我没有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. 为了防止意外滥用,您可以简单地检查父进程ID,并将其命令行执行与您期望的执行进行比较,如下所示,该过程使用ps查找该信息。 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) script1.plsome_other_script.pl (两者相同,但名称不同)

#!/usr/bin/env perl

use warnings;
use strict;

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

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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