xaizek / rocketgit (License: AGPLv3+) (since 2018-12-09)
Light and fast Git hosting solution suitable to serve both as a hub or as a personal code storage with its tickets, pull requests, API and much more.
<root> / scripts / ssh.php (757a20ad467f526d4f03803b0f703c70ad42edd6) (2,103B) (mode 100644) [raw]
<?php
error_reporting(E_ALL);

$_start = microtime(TRUE);

require_once("/etc/gg/config.php");

$INC = dirname(__FILE__) . "/../inc";
require_once($INC . "/xlog.inc.php");
require_once($INC . "/db.inc.php");
require_once($INC . "/repo.inc.php");

$sql_debug = $gg_db_debug;

function fatal($str)
{
	xlog($str);
	fwrite(STDERR, "FATAL ERROR: " . $str . "\n");
	exit(1);
}

xlog("Start...");

umask(0022);

$ssh_conn = @$_SERVER['SSH_CONNECTION'];
xlog("SSH_CONNECTION: $ssh_conn.");

// first parameter must be uid of the user
$uid = @$_SERVER['argv'][1];
if (empty($uid))
	fatal("uid not provided!");
xlog("uid is $uid.");

$cmd = trim(@$_SERVER['SSH_ORIGINAL_COMMAND']);
if (empty($cmd))
	fatal("No SSH_ORIGINAL_COMMAND provided!");
xlog("SSH_ORIGINAL_COMMAND is [$cmd].");

// extract command
if (strncmp($cmd, "git-upload-pack", 15) == 0) {
	$op = "git-upload-pack";
	$perms = "R";
} else if (strncmp($cmd, "git-receive-pack", 16) == 0) {
	$op = "git-receive-pack";
	$perms = "W";
} else {
	fatal("Unknown command!");
}
xlog("real operation is $op, perms is [$perms].");

// extract repository name
$repo = substr($cmd, strlen($op));
$repo = trim($repo, "' ");
if (empty($repo))
	fatal("Repo is invalid!");
// security checks
if (preg_match('/\.\./', $repo))
	fatal("Repo must not contain [..]!");
xlog("repo is [$repo].");

// Check if repository exists
$path = $gg_base_repo . "/" . $repo;
if (!file_exists($path))
	fatal("Cannot find repo $path!");
xlog("path is [$path].");

// check access - uid is allowed to access this repo?
$repo_id = sprintf("%u", @file_get_contents($path . "/gg/repo_id"));
if ($repo_id == 0)
	fatal("Invalid repo!");
xlog("repo_id is [$repo_id]");

$db = sql_open($gg_db);
if ($db === FALSE)
	fatal("Internal error (db)!");

if (!repo_allow($db, $repo_id, $uid, $perms))
	fatal("You do not have access to this repository!");

$run = "git-shell -c \"" . $op . " '" . escapeshellcmd($path) . "'\"";
xlog("Running [$run]...");
passthru($run, $ret);
xlog("[$run] returned $ret.");

$diff = sprintf("%u", (microtime(TRUE) - $_start) * 1000);
xlog("Took " . $diff . "ms.");
?>
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/rocketgit

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/rocketgit

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master