public
Description: Gitorious aims to provide a great way of doing distributed opensource code collaboration.
Homepage: http://gitorious.org/projects/gitorious
Clone URL: git://github.com/dysinger/gitorious.git
gitorious / script / gitorious
100755 73 lines (61 sloc) 2.193 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env ruby
 
require "yaml"
if File.symlink?(__FILE__)
  $:.unshift File.dirname(File.readlink(__FILE__)) + "/../lib/gitorious/ssh"
  BASE_DIR = File.dirname(File.readlink(__FILE__)) + "/../"
  conf_file = File.join(BASE_DIR, "config/gitorious.yml")
else
  $:.unshift File.dirname(__FILE__) + "/../lib/gitorious/ssh"
  BASE_DIR = File.dirname(__FILE__) + "/../"
  conf_file = File.join(BASE_DIR, "config/gitorious.yml")
end
 
GitoriousConfig = YAML.load_file(conf_file)
 
ENV["PATH"] = "/usr/local/bin/:/opt/local/bin:#{ENV["PATH"]}"
 
require "logger"
require "strainer"
require "client"
 
File.umask(0022)
original_command = ENV["SSH_ORIGINAL_COMMAND"]
user = ARGV[0]
 
logger = Logger.new(File.join(BASE_DIR, "log", "gitorious_auth.log"))
logger.formatter = Logger::Formatter.new
logger.level = Logger::INFO
logger.formatter.datetime_format = "%Y-%m-%d %H:%M:%S"
logger.info("Connection from #{ENV['SSH_CLIENT'].inspect} (#{user || nil}): #{original_command || nil}")
 
$stderr.puts "original_command: #{original_command.inspect}" if $DEBUG
if original_command.nil? || original_command.strip.empty?
  $stderr.puts "Need SSH_ORIGINAL_COMMAND"
  exit!(1)
end
 
$stderr.puts "user: #{user.inspect}" if $DEBUG
if user.nil? || user.strip.empty?
  $stderr.puts "Need user arg"
  exit!(1)
end
 
begin
  strainer = Gitorious::SSH::Strainer.new(original_command).parse!
  client = Gitorious::SSH::Client.new(strainer, user)
 
  # The meat of it all; do the permission check
  # replace process with git-shell if everything is fine
  args = client.to_git_shell_argument
  args.include?('git-receive-pack') && client.assure_user_can_write!
  $stderr.puts "git-shell -c #{args.inspect}" if $DEBUG
  exec("git-shell", "-c", args)
  
  unless $?.success?
    $stderr.puts "Failed to execute git command"
    exit!(1)
  end
rescue Gitorious::SSH::AccessDeniedError => e
  $stderr.puts "Access denied or bad repository path"
  exit!(1)
rescue Gitorious::SSH::BadCommandError => e
  $stderr.puts "Access denied or bad command"
  exit!(1)
rescue Object => e
  if $DEBUG
    $stderr.puts "#{e.class.name} #{e.message}"
    $stderr.puts e.backtrace.join(" \n")
  end
  $stderr.puts "fatal error"
  exit(1)
end