Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
试图';吉特拉力';从PHP脚本中,遇到SSH权限问题_Php_Git_Ssh - Fatal编程技术网

试图';吉特拉力';从PHP脚本中,遇到SSH权限问题

试图';吉特拉力';从PHP脚本中,遇到SSH权限问题,php,git,ssh,Php,Git,Ssh,我被这段代码难住了,它在本地工作,但不能远程工作 (它最终将成为WordPress插件的一部分,但因为我现在没有使用任何WordPress特定的东西,所以这不重要。) 上述内容似乎适用于http://和https://repo,但我的repo目前只能通过SSH使用 我已经创建了一个SSH密钥对,因此不需要密码 当我使用PHPCGI从Web服务器运行脚本时,作为运行Web内容的用户,它可以正常工作。(我添加了一个对“exec whoami”的调用,以确认我是以正确的用户身份运行的。) 当我在本地

我被这段代码难住了,它在本地工作,但不能远程工作

(它最终将成为WordPress插件的一部分,但因为我现在没有使用任何WordPress特定的东西,所以这不重要。)

上述内容似乎适用于http://和https://repo,但我的repo目前只能通过SSH使用

我已经创建了一个SSH密钥对,因此不需要密码

当我使用PHPCGI从Web服务器运行脚本时,作为运行Web内容的用户,它可以正常工作。(我添加了一个对“exec whoami”的调用,以确认我是以正确的用户身份运行的。)

当我在本地运行git pull代码时,所有操作都按预期进行:

-bash-4.1$ /usr/bin/php-cgi /home/dsgit1/wp-content/plugins/test/pull.php
X-Powered-By: PHP/5.3.3
Content-type: text/html

Running as user: dsgit1
Cloning into '/home/dsgit1/wp-content/themes/test-theme'...
Checking connectivity... done
。。。正如你所料,回购协议会被放入本地文件夹

我们为每个站点使用mod_suphp和一个单独的本地用户,这就是为什么您看到的是“dsgit1”,而不是“httpd”或“apache”。该代码旨在作为特定的本地用户运行

当我远程运行脚本时(通过在浏览器中输入其URL,或将其提供给wget),我得到以下结果:

Running as user: dsgit1
Cloning into '/home/dsgit1/wp-content/themes/test-theme'...
ssh: connect to host my.git.server port 22: Permission denied
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
出于某种原因,当代码“远程”运行时,它似乎没有获得足够的环境来读取~/.SSH/id_rsa中的SSH私钥。代码以同一本地用户的身份运行,因此它应该对SSH密钥具有读取权限,但不能或不会使用它们

很明显,本地和远程运行代码的方式有一些不同,但我不知道是什么。建议

编辑:/etc/suphp.conf,根据评论员的要求:

[global]
logfile=/var/log/httpd/suphp_log
loglevel=warn
webserver_user=apache
docroot=/
env_path=/bin:/usr/bin
umask=0022
min_uid=500
min_gid=500

; Security options
allow_file_group_writeable=true
allow_file_others_writeable=false
allow_directory_group_writeable=true
allow_directory_others_writeable=false

;Check wheter script is within DOCUMENT_ROOT
check_vhost_docroot=true

;Send minor error messages to browser
errors_to_browser=false
check_parent_owner=false

[handlers]
;Handler for php-scripts
; x-httpd-php=php:/usr/bin/php
x-httpd-php="php:/usr/bin/php-cgi"

;Handler for CGI-scripts
x-suphp-cgi=execute:!self
第二次编辑:这是将我的命令更改为
$output=shell\u exec('ssh-v')后从CLI手动运行的输出gitreader@my.git.serverpwd 2>&1')

-bash-4.1$/usr/bin/php-cgi./wp-content/plugins/autogit/a4.php
X-Powered-By:PHP/5.3.3
内容类型:text/html
以用户身份运行:dsgit1
OpenSSH_5.3p1, OpenSSL 1.0.0-fips 29 Mar 2010
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to my.git.server [172.20.60.96] port 22.
debug1: connect to address 172.20.60.96 port 22: Permission denied
ssh: connect to host my.git.server port 22: Permission denied
OpenSSH_5.3p1、OpenSSL 1.0.0-fips 2010年3月29日
debug1:正在读取配置数据/home/dsgit1/.ssh/config
debug1:为my.git.server应用选项
debug1:读取配置数据/etc/ssh/ssh\u config
debug1:将选项应用于*
debug1:连接到my.git.server[172.20.60.96]端口22。
debug1:已建立连接。
debug1:identity file/home/dsgit1/.ssh/id\u rsa类型1
debug1:远程协议版本2.0,远程软件版本OpenSSH_5.3
debug1:match:OpenSSH_5.3 pat OpenSSH*
debug1:启用协议2.0的兼容模式
debug1:本地版本字符串SSH-2.0-OpenSSH_5.3
debug1:SSH2\u MSG\u KEXINIT已发送
debug1:SSH2\u MSG\u KEXINIT已收到
debug1:kex:server->client aes128 ctr hmac-md5无
debug1:kex:客户端->服务器aes128 ctr hmac-md5无

debug1:SSH2_MSG_KEX_DH_GEX_请求(1024我在mod_suphp方面遇到过这种困难。我不喜欢那种apt,我的脚本总是打电话检查mod_suphp是否被禁用或卸载(每次都能解决问题)。

我认为我已经解决了根本问题,但它并不漂亮

我将命令更改为简单的ssh命令,并将~/.ssh中相关文件的权限显式更改为660:

shell\u exec('ssh-v-F/home/dsgit1/.ssh/configgitreader@my.git.serverpwd 2>&1')

我仍然收到“权限被拒绝”错误

我在audit.log中发现了SELinux错误,并发现SELinux上下文使得PHP进程无法读取SSH文件。(在~/.SSH中的文件都是SSH\u home\t类型。)

暂时禁用SELinux,并显式添加-F以指定要使用的配置文件,解决了SSH的问题。这应该可以通过Git_SSH环境变量和包装器脚本扩展到Git,但需要将SELinux保持在许可模式

使用此行创建自定义SELinux策略应永久解决此问题,同时允许我保持启用SELinux:

允许httpd\u t ssh\u home\u t:文件读取;


可能还有很多其他的安全问题,我的同事和我将讨论这些问题(并且可能最终决定这些问题比通过SSH自动访问Git更重要).

您对~/.ssh/id_rsa有什么权限?
ls-l~/.ssh/id_rsa
您的PHP在哪个组下运行?您也可以发布suPHP配置吗?服务器是RHEL,因此每个用户都有自己的组,名称相同(在本例中为用户dsgit1,组dsgit1).id_rsa是模式600,对于该用户:
-rw------+1 dsgit1 dsgit1 1675 Dec 6 10:58 id_rsa
在问题中添加了/etc/suphp.conf。我们知道ssh失败了。我们如何删除git作为潜在问题源。如果执行
,会发生什么?如果存在“真实性”连接过程中的用户提示命令将失败,因为
shell\u exec
不支持用户输入。您可能还需要添加
-v
调试开关来帮助调试。
不幸的是,这可能不是一个选项。我们不能使用直接mod\u php,因为我们必须作为单独的用户运行每个站点(出于政策原因)。切换到mod_fcgid可能也不可行,因为它需要在一个有大约30个网站的生产服务器上工作;如果没有很好的理由,切换将是一个困难的论据。(老实说,这里的git/php/ssh还不够好;我们可能会放弃这个小项目,而不是尝试重建整个服务器。)
-bash-4.1$ /usr/bin/php-cgi ./wp-content/plugins/autogit/a4.php
X-Powered-By: PHP/5.3.3
Content-type: text/html

Running as user: dsgit1
<pre>OpenSSH_5.3p1, OpenSSL 1.0.0-fips 29 Mar 2010
debug1: Reading configuration data /home/dsgit1/.ssh/config
debug1: Applying options for my.git.server
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to my.git.server [172.20.60.96] port 22.
debug1: Connection established.
debug1: identity file /home/dsgit1/.ssh/id_rsa type 1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.3
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Host 'my.git.server' is known and matches the RSA host key.
debug1: Found key in /home/dsgit1/.ssh/known_hosts:3
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password,keyboard-interactive
debug1: Next authentication method: publickey
debug1: Offering public key: /home/dsgit1/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 277
debug1: read PEM private key done: type RSA
debug1: Authentication succeeded (publickey).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending command: pwd
debug1: channel 0: forcing write
/home/gitreader
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: client_input_channel_req: channel 0 rtype eow@openssh.com reply 0
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 1 clearing O_NONBLOCK
Transferred: sent 2288, received 2360 bytes, in 0.2 seconds
Bytes per second: sent 12073.1, received 12453.0
debug1: Exit status 0
</pre>
OpenSSH_5.3p1, OpenSSL 1.0.0-fips 29 Mar 2010 debug1: Reading configuration data /etc/ssh/ssh_config debug1: Applying options for * debug1: Connecting to my.git.server [172.20.60.96] port 22. debug1: connect to address 172.20.60.96 port 22: Permission denied ssh: connect to host my.git.server port 22: Permission denied