# encoding: utf-8 #-- # Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies) # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . #++ require "open-uri" require 'cgi' module Gitorious module SSH class PreReceiveGuard # env is a Hash representing ENV def initialize(env, git_spec) @env = env.dup @query_url = @env['GITORIOUS_WRITABLE_BY_URL'] @git_spec = git_spec end attr_reader :git_spec # extract the target, eg. refs/heads/master def git_target @git_spec.split(/\s/, 3).last.chomp end def local_connection? !@env.include? 'SSH_ORIGINAL_COMMAND' end def merge_request_update? git_target =~ /^refs\/merge-requests\/\d+$/ end def authentication_url @query_url + "&git_path=#{CGI.escape(git_target)}" end def allow_push? return true if local_connection? result = get_via_http(authentication_url) return result == 'true' end def deny_force_pushes? return false if local_connection? return false if merge_request_update? @env['GITORIOUS_DENY_FORCE_PUSHES'] == "true" end def get_via_http(url) open(url).read end def gitorious_says(msg) $stderr.puts $stderr.puts "== YouSource: " + ("=" * 58) $stderr.puts msg $stderr.puts "="*72 $stderr.puts end def null_sha?(sha) sha == "0000000000000000000000000000000000000000" end def deny_merge_request_update_with_sha?(sha) return null_sha?(sha) && merge_request_update? && !local_connection? end def valid_basic_metainfo?(basic_metainfo) begin metahash = YAML::load(basic_metainfo) rescue return false end if !metahash.include?("name") then return false end if !metahash.include?("merge_requests_enabled") then return false end true end def valid_metainfo?(newrev, ref) require File.join(@env['GITORIOUS_BASE_DIR'], 'vendor/grit/lib/grit.rb') repo = Grit::Repo.new(Dir.getwd) commit = repo.commit(newrev) meta_blob = commit.tree/'repository.yml' # FIXME Hardcoded filename desc_blob = commit.tree/'description' # FIXME Hardcoded filename if meta_blob == nil then return false end if !valid_basic_metainfo?(meta_blob.data) then return false end if desc_blob == nil then return false end if desc_blob.data.empty? then return false end true end end end end