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 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况