Created
November 15, 2012 17:45
-
-
Save pogoapp/4080041 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
require 'redis' | |
class GitAction | |
attr_reader :user_id, :repo, :cmd | |
def initialize(user_id, ssh_command) | |
@user_id = user_id | |
# input e.g. "git-upload-pack '/test.git'" or "git-upload-pack 'test.git'" | |
@cmd, repo_str = ssh_command.split(' ', 2) | |
repo_fname = File.basename(repo_str).tr("'", '') | |
unless cmd.match(/^git[ -]upload-pack$/) || cmd.match(/^git[ -]receive-pack$/) | |
$stderr.puts "Only git commands are allowed, not: #{cmd}" | |
Process.exit(2) | |
end | |
# FIXME: junky regex | |
unless repo_fname.match(/\w+\.git/) | |
$stderr.puts "Repository parameter incorrect: #{repo_fname}" | |
Process.exit(2) | |
end | |
@repo = File.basename(repo_fname, '.git') | |
ensure_setup if valid_user? | |
true | |
end | |
def valid_user? | |
repo_user_ids.include?(user_id) | |
end | |
# TODO: read-only users? | |
def allowed? | |
valid_user? | |
end | |
def repo_user_ids | |
@repo_user_ids ||= redis.smembers(repo) || [] | |
end | |
def ensure_setup | |
`GIT_TEMPLATE_DIR=/app/repo-template git init --bare #{path} &> /dev/null` unless Dir.exists?(path) | |
`ln -nfs /app/hooks #{path}/hooks` | |
end | |
def repo_dir | |
'/app/share/gitolite/repositories' | |
end | |
def path | |
"#{repo_dir}/#{repo}.git" | |
end | |
def redis | |
@redis ||= begin | |
uri = URI.parse(ENV['REDISDB_URL']) | |
uri.port ||= 6379 | |
redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password, :db => 4) | |
end | |
end | |
end | |
### | |
unless ssh_command = ENV["SSH_ORIGINAL_COMMAND"] | |
$stderr.puts "SSH not allowed to the git account." | |
Process.exit(1) | |
end | |
user_id, name = *ARGV | |
begin | |
git_action = GitAction.new(user_id, ssh_command) | |
if git_action.allowed? | |
ENV['GL_REPO'] = git_action.repo | |
ENV['GL_USER'] = name | |
exec "/usr/bin/#{git_action.cmd} #{git_action.path}" | |
Process.exit(0) | |
else | |
$stderr.puts "#{name} does not have permission to #{git_action.cmd}" | |
Process.exit(5) | |
end | |
rescue => e | |
$stderr.puts "Error!" | |
Process.exit(4) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment