创建新的 Expect 对象时,cgi-perl 文件中出现 Apache [PTY 错误]

2024-04-22

我有一个 perl 脚本:

#!/usr/bin/perl -w
use DateTime;
use Expect; 
use IO::Pty;
use CGI::Fast;


while($q = new CGI::Fast){
    my $ip = $q->param('ip');
    my $folder = $q->param('folder');
    my $username = $q->param('username');
    my $password = $q->param('password');
    print "Content-type: text/html\r\n\r\n";
    print "<head>\n<title>FastCGI</title>\n\</head>";
    print "<h3> $ip - $folder - $username - $password </h3>";

my $ssh = new Expect;

if($ssh->spawn("ssh -q -l $username $ip")){
    print "<h4>Connexion OK</h4>";
    } else {
        print "Error\n";
        die "Connexion failed, $!";
    }
}

该脚本的执行在我的 Apache 错误日志中创建了一些错误:

[error] [client x.x.x.x] pty_allocate(nonfatal): posix_openpt(): Permission denied at /usr/local/lib/perl5/site_perl/5.10.0/i386-linux-thread-multi/IO/Pty.pm line 24., referer: http://y.y.y.y/login
[error] [client x.x.x.x] pty_allocate(nonfatal): getpt(): No such file or directory at /usr/local/lib/perl5/site_perl/5.10.0/i386-linux-thread-multi/IO/Pty.pm line 24., referer: http://y.y.y.y/login
[error] [client x.x.x.x] pty_allocate(nonfatal): openpty(): No such file or directory at /usr/local/lib/perl5/site_perl/5.10.0/i386-linux-thread-multi/IO/Pty.pm line 24., referer: http://y.y.y.y/login
[error] [client x.x.x.x] pty_allocate(nonfatal): open(/dev/ptmx): Permission denied at /usr/local/lib/perl5/site_perl/5.10.0/i386-linux-thread-multi/IO/Pty.pm line 24., referer: http://y.y.y.y/login
[error] [client x.x.x.x] Cannot open a pty at /var/www/cgi-bin/cgi2.pl line 18, referer: http://y.y.y.y/login

我理解这个错误,因为它说它无法打开 PTY(使用新期待命令)。

这真的是权限问题(以及如何解决这个问题)还是不可能使用Expectcgi 文件中的命令?

感谢您的建议....


这是因为 httpd_sys_script_t 没有读取/写入 pty 的 selinux 权限,但以下 selinux 策略将允许它:

policy_module(httpd_pty,1.0)
require {
    type httpd_sys_cript_t;
    type ptmx_t;
    class chr_file { read write };
}
allow httpd_sys_script_t ptmx_t:chr_file { read write };

您也许可以更改为class chr_file rw_chr_file_perms;, and allow httpd_sys_script_t ptmx_t:chr_file rw_chr_file_perms;,具体取决于您的 selinux 策略的最新版本。上面的宏适用于 rhel5,这一行的宏适用于 rhel6。

或者,来自 #selinux on freenode 的建议:

mkdir ~/myhttpd
cd ~/myhttpd
echo "policy_module(myhttpd,1.0.0) optional_policy(\` apache_content_template(myscript)')" > myhttpd.te
echo "/home/httpd/foo/cgi-bin/test.pl -- gen_context(system_u:object_r:httpd_myscript_script_exec_t,s0)" > myhttpd.fc
make -f /usr/share/selinux/devel/Makefile myhttpd.pp
sudo semodule -i myhttpd.pp

基本上,apache 策略有一种方法来创建您自己的内容类型。在上面的代码片段中为您的脚本创建内容类型。然后使用新的 avc 拒绝并添加到上面的策略文件 myhttpd.te 中。这将阻止您允许所有 httpd 进程访问 pty,仅访问您指定的进程。之后您可能会执行以下操作:

allow httpd_myscript_script_t ptmx_t:chr_file rw_chr_file_perms;

添加到 myhttpd.te (或任何您想要调用的模块)的末尾,然后重新编译和加载(上面的 make 和 semodule )。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

创建新的 Expect 对象时,cgi-perl 文件中出现 Apache [PTY 错误] 的相关文章

随机推荐