-
Notifications
You must be signed in to change notification settings - Fork 230
/
Copy pathgverify
executable file
·175 lines (144 loc) · 4.77 KB
/
gverify
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env ruby
require 'optparse'
require 'yaml'
require 'fileutils'
require 'pathname'
bold = ["\033[0m", "\033[1m"]
@options = {}
def system!(cmd)
system(cmd) or raise "failed to run #{cmd}"
end
def sanitize(str, where)
raise "unsanitary string in #{where}" if (str =~ /[^\w.-]/)
str
end
def sanitize_path(str, where)
raise "unsanitary string in #{where}" if (str =~ /[^@\w\\ '\/.:+-]/)
str
end
def info(str)
puts str if @options[:verbose]
end
################################
OptionParser.new do |opts|
opts.banner = "Usage: build [options] <build-description>.yml"
opts.on("-v", "--verbose", "be more verbose") do |v|
@options[:verbose] = v
end
@options[:markup] = true
opts.on("-m", "--[no-]markup", "markup the output using ANSI escape codes") do |m|
@options[:markup] = m
end
opts.on("-r REL", "--release REL", "release name") do |v|
@options[:release] = v
end
opts.on("-d DEST", "--destination DEST", "directory to place signature in") do |v|
@options[:destination] = v
end
opts.on("-c SIGNER", "--compare-to SIGNER", "compare other manifests to SIGNER's, if not given pick first") do |v|
@options[:compareto] = v
end
opts.on("-p PROG", "--verify-program PROG", "specify verification program to use (default is gpg)") do |v|
@options[:program] = v
end
end.parse!
base_dir = Pathname.new(__FILE__).expand_path.dirname.parent
build_desc_file = ARGV.shift or raise "must supply YAML build description file"
build_desc = YAML.load_file(build_desc_file)
in_sums = []
result_dir = 'result'
package_name = build_desc["name"] or raise "must supply name"
package_name = sanitize(package_name, "package name")
destination = @options[:destination] || File.join(base_dir, "sigs", package_name)
release = @options[:release] || "current"
release = sanitize(release, "release")
verbose = @options[:verbose]
bold = ['', ''] unless @options[:markup]
program = @options[:program] || "gpg"
release_path = File.join(destination, release)
File.exists?(release_path) or raise "#{release_path} does not exist"
result_file = "#{package_name}-build.assert"
sig_file = "#{result_file}.sig"
current_manifest = nil
if @options[:compareto]
# Load a specific 'golden' manifest to compare to
compareto = @options[:compareto]
result_path = sanitize_path(File.join(release_path, compareto, result_file), "result path")
result = YAML.load_file(result_path)
current_manifest = result['out_manifest']
end
did_fail = false
Dir.foreach(release_path) do |signer_dir|
next if signer_dir == "." or signer_dir == ".."
signer_path = sanitize_path(File.join(release_path, signer_dir), "signer path")
next if !File.directory?(signer_path)
result_path = sanitize_path(File.join(signer_path, result_file), "result path")
sig_path = sanitize_path(File.join(signer_path, sig_file), "result path")
if !File.exist?(result_path)
puts "missing result at #{result_path}"
next
end
if !File.exist?(sig_path)
puts "missing signature at #{sig_path}"
next
end
result = YAML.load_file(result_path)
system("#{program} --keyserver pgp.mit.edu --recv-keys `#{program} --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")
out = `#{program} --quiet --batch --verify \"#{sig_path}\" \"#{result_path}\" 2>&1`
if $? != 0
out.each_line do |line|
if line =~ /^gpg: Signature made/
info(line)
else
puts line
end
end
puts "#{bold[1]}#{signer_dir}: BAD SIGNATURE#{bold[0]}"
puts
did_fail = true
elsif current_manifest and (result['out_manifest'] != current_manifest or result['release'] != release or result['name'] != package_name)
out.each_line do |line|
if line =~ /^gpg: Signature made/
info(line)
elsif line =~ /^gpg: Good signature/
info(line)
elsif line =~ /^gpg: aka/
info(line)
else
puts line
end
end
puts "#{bold[1]}#{signer_dir}: MISMATCH#{bold[0]}"
puts
if verbose
lines1 = current_manifest.each_line
lines2 = result['out_manifest'].each_line
lines1.zip(lines2) do |line|
if line[0] != line[1]
puts "- "+line[0]
puts "+ "+line[1]
end
end
end
did_fail = true
else
out.each_line do |line|
if line =~ /^gpg: Signature made/
info(line)
elsif line =~ /^gpg: Good signature/
info(line)
elsif line =~ /^gpg: aka/
info(line)
else
puts line
end
end
puts "#{bold[1]}#{signer_dir}: OK#{bold[0]}"
puts
end
if !current_manifest
# take first manifest as 'current' to compare against
current_manifest = result['out_manifest']
end
end
exit 1 if did_fail