]> git.draconx.ca Git - homepage.git/blob - lib/gpg-wkd.rb
Update GPG instructions for packages.
[homepage.git] / lib / gpg-wkd.rb
1 # Nick's web site: Export public keys for the Web Key Directory
2 #
3 # Copyright © 2022 Nick Bowler
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
18 module WKD
19     require 'open3'
20
21     @@gpg2 = "/usr/bin/gpg"
22     @@wksclient = "/usr/libexec/gpg-wks-client"
23
24     def WKD.gpg2; @@gpg2 end
25     def WKD.gpg2=(x); @@gpg2 = x end
26     def WKD.wksclient; @@wksclient end
27     def WKD.wksclient=(x); @@wksclient = x end
28
29     # Return a list list of all UIDs known from the given GPG keyrings.
30     def WKD.uids_from_keyrings(*args)
31         uids = {}
32
33         Open3.popen2(@@gpg2,
34             "--no-default-keyring", "--with-colons", "--list-keys",
35             *args.map { |x| "--keyring=" + (x['/'] ? x : "./" + x) }
36         ) do |stdin, stdout, result|
37             stdin.close
38             stdout.each do |line|
39                 fields = line.split(":")
40                 next if fields[0] != "uid"
41                 fields[9].gsub!(/\\x../) { |x| x[2..].hex.chr }
42                 uids[fields[9]] = true
43             end
44             stdout.close
45
46             raise "gpg failed" unless result.value.success?
47         end
48
49         return uids.keys
50     end
51
52     # Given a list of UIDs, return a dictionary where the keys are UIDs
53     # and the values are the WKS hash.
54     def WKD.hashes_from_uids(*args)
55         wkd_hash = {}
56
57         consume_output = Proc.new do |s|
58             while l = s.slice!(/([^\n]*)\n/) do
59                 hash, uid = l.chomp.split(nil, 2)
60                 wkd_hash[uid] = hash
61             end
62         end
63
64         Open3.popen2(@@wksclient,
65             "--print-wkd-hash"
66         ) do |stdin, stdout, result|
67             buf = ""
68             args.flatten.each do |uid|
69                 stdin.puts(uid)
70                 stdin.flush
71
72                 loop do
73                     buf += stdout.read_nonblock(100)
74                     consume_output.call(buf)
75                 rescue EOFError, IO::WaitReadable
76                     break
77                 end
78             end
79             stdin.close
80
81             loop do
82                 buf += stdout.readpartial(100)
83                 consume_output.call(buf)
84             rescue EOFError
85                 break
86             end
87             stdout.close
88
89             raise "gpg-wks-client failed" unless result.value.success?
90         end
91
92         return wkd_hash
93     end
94
95 end
96
97 # Call during preprocessing to create items for each unique UID found in the
98 # given keyring items.  The items have the identifier /gpg/UID and the content
99 # is the same UID.  The items are created with the :keyrings attribute set to
100 # the list of keyring files and :wkd_hash is for the Web Key Directory.
101 def create_wkd_items(keyring_items)
102     keyring_files = {}
103     [*keyring_items].each { |item| keyring_files[item.raw_filename] = true }
104
105     wkd = WKD.hashes_from_uids(WKD.uids_from_keyrings(*keyring_files.keys))
106     wkd.each do |uid, hash|
107         attrs = {
108             keyrings: keyring_files.keys,
109             wkd_hash: hash
110         }
111         @items.create(uid, attrs, "/gpg/" + uid)
112     end
113 end
114
115 # Convert items created by create_wkd_items into real GPG keyrings.
116 class WKDExport < Nanoc::Filter
117     identifier :wkd_export
118     type :text => :binary
119
120     def run(content, params = {})
121         args = @item[:keyrings].map { |x|
122             "--keyring=" + (x['/'] ? x : "./" + x)
123         };
124         args.append("--armor") if params[:armor]
125
126         dummy, result = Open3.capture2(WKD.gpg2, "--export",
127             "--no-default-keyring", "--output=" + output_filename,
128             *args, content.chomp)
129         raise "gpg failed" unless result.success?
130     end
131 end