gpt4 book ai didi

php - 尝试从 PHP 脚本访问 'git pull',遇到 SSH 权限问题

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

我被这段代码难住了,它可以在本地工作,但不能在远程工作。
<?php shell_exec('/usr/bin/git pull ssh://my.git.server/git/test-theme /home/dsgit1/wp-content/themes/test-theme' 2>&1); ?>
(它最终将成为 WordPress 插件的一部分,但由于我目前没有使用任何 WordPress 特定的东西,所以这无关紧要。)

以上似乎适用于 http://和 https://repos,但我的 repo 目前只能通过 SSH 获得。

我创建了一个 SSH key 对,因此不需要密码。

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

putenv("PATH=/usr/bin:/bin");
print "Running as user: ";
passthru('whoami');
// then the above shell_exec()

当我在本地运行 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

...正如您所期望的那样,repo 被 pull 入本地文件夹。

我们为每个站点使用 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 key 具有读取权限,但它不能或不会使用它们。

代码在本地与远程运行的方式显然存在一些差异,但我不知道是什么。建议?

编辑:/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_exec('ssh -v gitreader@my.git.server pwd 2>&1'); 之后,这是来自 CLI 的手动运行的输出。
-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>

它登录,运行一个命令并显示其输出,然后退出。主机已经在 known_hosts 中,因此不需要输入。

这是从 curl 调用的相同内容的输出:

以用户身份运行:dsgit1

OpenSSH_5.3p1,OpenSSL 1.0.0-fips 2010 年 3 月 29 日
debug1:读取配置数据/etc/ssh/ssh_config
debug1:为 * 应用选项
debug1:连接到 my.git.server [172.20.60.96] 端口 22。
debug1:连接到地址 172.20.60.96 端口 22:权限被拒绝
ssh:连接到主机 my.git.server 端口 22:权限被拒绝

最佳答案

当谈到 mod_suphp 时,我遇到过这种挣扎。我不喜欢那个 apt,我的脚本总是调用来检查 mod_suphp 是否被禁用或卸载(这总是每次都能解决问题)。

关于php - 尝试从 PHP 脚本访问 'git pull',遇到 SSH 权限问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20523448/

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