You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gverify 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/usr/bin/ruby
  2. require 'optparse'
  3. require 'yaml'
  4. require 'fileutils'
  5. require 'pathname'
  6. bold = ["\033[0m", "\033[1m"]
  7. @options = {}
  8. def system!(cmd)
  9. system(cmd) or raise "failed to run #{cmd}"
  10. end
  11. def sanitize(str, where)
  12. raise "unsanitary string in #{where}" if (str =~ /[^\w.-]/)
  13. str
  14. end
  15. def sanitize_path(str, where)
  16. raise "unsanitary string in #{where}" if (str =~ /[^@\w\/.:+-]/)
  17. str
  18. end
  19. def info(str)
  20. puts str if @options[:verbose]
  21. end
  22. ################################
  23. OptionParser.new do |opts|
  24. opts.banner = "Usage: build [options] <build-description>.yml"
  25. opts.on("-v", "--verbose", "be more verbose") do |v|
  26. @options[:verbose] = v
  27. end
  28. @options[:markup] = true
  29. opts.on("-m", "--[no-]markup", "markup the output using ANSI escape codes") do |m|
  30. @options[:markup] = m
  31. end
  32. opts.on("-r REL", "--release REL", "release name") do |v|
  33. @options[:release] = v
  34. end
  35. opts.on("-d DEST", "--destination DEST", "directory to place signature in") do |v|
  36. @options[:destination] = v
  37. end
  38. opts.on("-c SIGNER", "--compare-to SIGNER", "compare other manifests to SIGNER's, if not given pick first") do |v|
  39. @options[:compareto] = v
  40. end
  41. end.parse!
  42. base_dir = Pathname.new(__FILE__).expand_path.dirname.parent
  43. build_desc_file = ARGV.shift or raise "must supply YAML build description file"
  44. build_desc = YAML.load_file(build_desc_file)
  45. in_sums = []
  46. result_dir = 'result'
  47. package_name = build_desc["name"] or raise "must supply name"
  48. package_name = sanitize(package_name, "package name")
  49. destination = @options[:destination] || File.join(base_dir, "sigs", package_name)
  50. release = @options[:release] || "current"
  51. release = sanitize(release, "release")
  52. verbose = @options[:verbose]
  53. bold = ['', ''] unless @options[:markup]
  54. release_path = File.join(destination, release)
  55. File.exists?(release_path) or raise "#{release_path} does not exist"
  56. result_file = "#{package_name}-build.assert"
  57. sig_file = "#{result_file}.sig"
  58. current_manifest = nil
  59. if @options[:compareto]
  60. # Load a specific 'golden' manifest to compare to
  61. compareto = @options[:compareto]
  62. result_path = sanitize_path(File.join(release_path, compareto, result_file), "result path")
  63. result = YAML.load_file(result_path)
  64. current_manifest = result['out_manifest']
  65. end
  66. did_fail = false
  67. Dir.foreach(release_path) do |signer_dir|
  68. next if signer_dir == "." or signer_dir == ".."
  69. signer_path = sanitize_path(File.join(release_path, signer_dir), "signer path")
  70. next if !File.directory?(signer_path)
  71. result_path = sanitize_path(File.join(signer_path, result_file), "result path")
  72. sig_path = sanitize_path(File.join(signer_path, sig_file), "result path")
  73. if !File.exist?(result_path)
  74. puts "missing result at #{result_path}"
  75. next
  76. end
  77. if !File.exist?(sig_path)
  78. puts "missing signature at #{sig_path}"
  79. next
  80. end
  81. result = YAML.load_file(result_path)
  82. system("gpg --keyserver pgp.mit.edu --recv-keys `gpg --quiet --batch --verify \"#{File.join(signer_path, 'signature.pgp')}\" \"#{result_path}\" 2>&1 | head -n1 | grep \"key ID\" | awk '{ print $15 }'` > /dev/null 2>&1")
  83. out = `gpg --quiet --batch --verify \"#{sig_path}\" \"#{result_path}\" 2>&1`
  84. if $? != 0
  85. out.each_line do |line|
  86. if line =~ /^gpg: Signature made/
  87. info(line)
  88. else
  89. puts line
  90. end
  91. end
  92. puts "#{bold[1]}#{signer_dir}: BAD SIGNATURE#{bold[0]}"
  93. puts
  94. did_fail = true
  95. elsif current_manifest and (result['out_manifest'] != current_manifest or result['release'] != release or result['name'] != package_name)
  96. out.each_line do |line|
  97. if line =~ /^gpg: Signature made/
  98. info(line)
  99. elsif line =~ /^gpg: Good signature/
  100. info(line)
  101. elsif line =~ /^gpg: aka/
  102. info(line)
  103. else
  104. puts line
  105. end
  106. end
  107. puts "#{bold[1]}#{signer_dir}: MISMATCH#{bold[0]}"
  108. puts
  109. if verbose
  110. lines1 = current_manifest.each_line
  111. lines2 = result['out_manifest'].each_line
  112. lines1.zip(lines2) do |line|
  113. if line[0] != line[1]
  114. puts "- "+line[0]
  115. puts "+ "+line[1]
  116. end
  117. end
  118. end
  119. did_fail = true
  120. else
  121. out.each_line do |line|
  122. if line =~ /^gpg: Signature made/
  123. info(line)
  124. elsif line =~ /^gpg: Good signature/
  125. info(line)
  126. elsif line =~ /^gpg: aka/
  127. info(line)
  128. else
  129. puts line
  130. end
  131. end
  132. puts "#{bold[1]}#{signer_dir}: OK#{bold[0]}"
  133. puts
  134. end
  135. if !current_manifest
  136. # take first manifest as 'current' to compare against
  137. current_manifest = result['out_manifest']
  138. end
  139. end
  140. exit 1 if did_fail