doutan5798 2016-07-27 16:43 采纳率: 100%
浏览 171

在git-shell命令中,“无参数运行或使用-c cmd运行”是什么意思?

I'm trying to write an open-source, PHP version, GitLab like code repo management project.

first of all

I add a new line in ~/.ssh/authorized_keys

command="/usr/bin/php /tmp/a.php howard",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDf5gt5+CHrQirFT24snhkoCGsWjiU6qeIv0rPUXTte78udT4+9BAXX3XHhCd4ccpVSf7i56MFssDnD2d6xWONVwughoymrB/8ZwEIydDikExWbO5KkXuVEhM2gJGlZnh/A8YQUBMa5UgUS0vhJhEaDDxJ392u8Xi+cNKXHhNabytnP8STAPPQDzpq/OwkBJ1ZyBzxyQyL/U1ZoqoZiCPewHmoLXWYBhBCfg9vPpoz7MZ4mjV9ON9IZ1z803PsKyHzPD8NUx84rzW2vNCrlEzT5X3gvw8DijESXF8srQsDfjwEh6dnP1LbG6gdx/7QcolDEEAkZD3pRXdos4MhNQWRv

the php code file /tmp/a.php as below:

<?php
if(empty($_SERVER['SSH_CLIENT'])){
        echo "SSH connection only!
";
        exit(1);
}
if(empty($_SERVER['SSH_ORIGINAL_COMMAND'])){
        echo "Hello, {$argv[1]}, welcome to PHPGitLab
";
        exit(0);
}
file_put_contents('/tmp/cmd.log', $_SERVER['SSH_ORIGINAL_COMMAND']);
$user = $argv[1];
$soc = $_SERVER['SSH_ORIGINAL_COMMAND'];
$pattern = "/^(git-upload-pack|git-receive-pack|git-upload-archive) '?\/?(.*?)(?:\.git(\d)?)?'?$/";
preg_match($pattern, $soc, $match);
if(empty($match[1])){
        lg("cannot parse original command: {$soc}");
        exit(1);
}
$verb = $match[1];
if(empty($match[2])){
        lg("cannot parse repo name: {$soc}");
        exit(1);
}
$repo = $match[2];
if(!empty($match[3])){
        define('D', $match[3]);
}
//$aa = stripos($repo, 'upload') ? 'R' : 'W';
//$cmd = "git-shell -c \"{$verb} '/root/{$repo}.git'\"";
$cmd = "git-shell -c \"{$_SERVER['SSH_ORIGINAL_COMMAND']}\"";
lg($cmd);
system($cmd);
function lg($message){
    file_put_contents('/tmp/gitlab.log', $message . "
", FILE_APPEND);
}

Above code is partly translated from gitolite and ignore the PRE_GIT and POST_GIT HOOKS.

When I try to clone repo from this server, I've got nothing and the cli waiting for input until I enter ctrl+C.

In the server, I see the $cmd in log file is

git-shell -c "git-upload-pack 'test.git'"

I try to run this command in linux-shell and get result below:

00ad85271fb369b7bf9c727aa6541d0c3c4efe5cfb38 HEADmulti_ack thin-pack side-band side-band-64k ofs-delta shallow no-progress include-tag multi_ack_detailed agent=git/1.8.3.1
003f85271fb369b7bf9c727aa6541d0c3c4efe5cfb38 refs/heads/master
0000

Also, the linux-shell wait for my input. If I type some chars in command line, I got

fatal: protocol error: bad line length character: xxxx

If I type command

git-shell -c git-upload-pack 'test.git'

(which I think it's a wrong command) in linux-shell, it returns

fatal: Run with no arguments or with -c cmd

instead.

I search in google and find some clue. Someone says that I've open a GIT SSH protocol and these three lines above are the protocol head. But I don't think my php script should reach to protocol level. Also, I didn't find any information or code about protocol in gitolite.

Does somebody knows how to fix this problem? I'll be very appreciated.

By the way, If I delete the command part in authorize_keys file, the clone action can run functionally.

  • 写回答

1条回答 默认 最新

  • douxi4414 2016-08-15 14:52
    关注

    I've got the correct answer!

    The command is correct, but I should not use system() to execute this command, system() can not return Bin data. So I choose passthru() to execute and the script run functionally.

    Hoping this answer can help others.

    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能