gpt4 book ai didi

perl - LWP 无法在 CGI 脚本中运行

转载 作者:行者123 更新时间:2023-12-02 09:11:26 26 4
gpt4 key购买 nike

我有一个 CGI 脚本来从 BibBase 加载出版物:

#!/usr/bin/perl

use LWP::UserAgent;

my $url = 'https://bibbase.org/show?bib=http://www.example.com/pubs.bib';
my $ua = LWP::UserAgent->new;
my $can_accept = HTTP::Message::decodable;
my $response = $ua->get($url, 'Accept-Encoding' => $can_accept);

print "Content-type: text/html\n\n";
print $response->decoded_content;

(这是从 BibBase 复制的,但 URL 是硬编码的。)

我有三个运行 RHEL7 和 Apache 2.4 的 Web 服务器,它们由 Puppet 以相同的方式配置。在所有三个上,我都可以在命令行上运行脚本并获得预期结果:

[root@server1 cgi-bin]# ./bibbase_proxy2.cgi | head
Content-type: text/html

<img src="//bibbase.org/img/ajax-loader.gif" id="spinner" style="display: none;" alt="Loading.." />

<div id="bibbase">

<script type="text/javascript">
var bibbase = {
params: {"bib":"http://www.example.com/pubs.bib","host":"bibbase.org"},

当我尝试使用 CGI 运行脚本时,我得到三个不同的结果:

  • 服务器1
    /usr/share/perl5/LWP/Protocol/http.pm 第 31 行无法识别协议(protocol) tcp。
  • 服务器2
    无法连接到 bibbase.org:443/usr/share/perl5/LWP/Protocol/http.pm 第 51 行出现系统错误。
  • 服务器3
    没有 http 输出,错误日志显示 AH01215: Out of memory!

我找不到这三个服务器之间的任何不同之处,也无法弄清楚为什么该脚本在命令行上运行良好,而在作为 CGI 运行时却不起作用。

我的 selinux 处于宽容模式,它正在记录传出请求,所以我知道脚本已经达到了这个程度:

type=AVC msg=audit(1532465859.921:331235): avc:  denied  { name_connect } for  pid=161178 comm="perl" dest=80 scontext=system_u:system_r:httpd_sys_script_t:s0 tcontext=system_u:object_r:http_port_t:s0 tclass=tcp_socket 

为了进行测试,我已将 selinux 设置为禁用并重新启动服务器。

最佳答案

SE-Linux denied the TCP connection .

avc:  denied  { name_connect }

The default access controls for networking by SELinux are based on the labels assigned to TCP and UDP ports and sockets. For instance, the TCP port 80 is labeled with http_port_t (and class tcp_socket). Access towards this port is then governed through SELinux access controls, such as name_connect and name_bind.

When an application is connecting to a port, the name_connect permission is checked. However, when an application binds to the port, the name_bind permission is checked.

无论是否处于宽容模式,Perl 的行为就像 TCP 连接被拒绝一样。 无法识别的协议(protocol) tcp 表示 getprotobyname("tcp") failed inside IO::Socket::IP 。这非常非常不寻常。发生这种情况的方法之一就是通过 SELinux 的拒绝。

我不是 SELinux 专家,但根据 RedHat 和 Gentoo some SELinux aware applications will ignore the global permissive setting并独自行动。 RHEL 7 Apache appears to be one of them 。看来have its own domain which must be set permissive .


On all three I can run the script on the command line and get the expected results:

造成这种情况的原因有两个,而且都与用户有关。

当您运行程序时,您将以自己的用户身份使用自己的配置、权限和环境变量运行。事实上,您以 root 身份运行它,这通常会绕过限制。当它在服务器上运行时,它会以不同的用户身份运行,可能是受到严格限制的 Web 服务器用户。

为了进行实际测试,您需要以与 Web 服务器相同的用户身份运行它。您可以使用 sudo -u 来实现此目的。例如,如果用户是 apache...

sudo -u apache ./bibbase_proxy2.cgi

顺便说一句不要以 root 身份测试软件!这不仅不会给您带来合理的结果,而且如果软件中存在错误,也没有任何保护措施可以阻止它不会破坏您的系统。


第二个问题是#!/usr/bin/env perl。这意味着运行 PATH 中的任何 perl。对于不同的用户,PATH 会有所不同。运行 ./bibbase_proxy2.cgi 可能会在命令行上使用一个 Perl 运行,并通过 Web 服务器运行另一个 Perl。

在服务器环境中,使用 Perl 的硬编码路径,例如 #!/usr/bin/perl

关于perl - LWP 无法在 CGI 脚本中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51523854/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com