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