summaryrefslogtreecommitdiff
path: root/bin/pacman-make-keyring
blob: 9b1109c7f2f7bce3b1497a7382a78e53f8c196a7 (plain)
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
#!/usr/bin/make -rRf
# Usage: pacman-make-keyring V=$(date -u +%Y%m%d)

# Copyright 2014, 2016 Luke Shumaker <lukeshu@sbcglobal.net>.
#
# This is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This software 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this manual; if not, see
# <http://www.gnu.org/licenses/>.

ifeq ($(origin V),undefined)
$(info Usage: pacman-make-keyring V=$$(date -u +%Y%m%d))
$(error You must set V= on the command line)
endif
ifeq ($(origin keyserver),undefined)
keyserver = hkp://pgp.cyberbits.eu:11371
endif

bin := $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))
yamldir  := $(shell ruby -e "load '$(bin)/common.rb'; print cfg['yamldir']")
cachedir := $(shell ruby -e "load '$(bin)/common.rb'; print cfg['keyring_cachedir']")
outputdir = $(cachedir)/$(KEYRING_NAME)-keyring-$(V)

KEYRING_NAME = parabola

all: $(KEYRING_NAME)-keyring-$(V).tar.gz
.PHONY: all

export SHELL = /bin/bash -o pipefail
.PHONY: FORCE
.SECONDARY:
.DELETE_ON_ERROR:

dirs = \
	$(outputdir) \
	$(cachedir) \
	$(cachedir)/gpghome \
	$(cachedir)/keys/trusted \
	$(cachedir)/keys/secondary \
	$(cachedir)/keys/revoked

$(dirs):
	mkdir -p $@

$(cachedir)/var.%: FORCE | $(cachedir)
	@$(file >$(@D)/tmp.$(@F),$($*))
	@sed -i 's|^|#|' $(@D)/tmp.$(@F)
	@if cmp -s $(@D)/tmp.$(@F) $@; then \
		rm -f $(@D)/tmp.$(@F) || :; \
	else \
		mv -f $(@D)/tmp.$(@F) $@; \
	fi
-include $(wildcard $(cachedir)/var.*)
$(cachedir)/txt.%: $(cachedir)/var.%
	sed 's|^#||' < $< > $@
var=$(cachedir)/var.

keyring-files = \
	$(outputdir)/Makefile \
	$(outputdir)/${KEYRING_NAME}.gpg \
	$(outputdir)/${KEYRING_NAME}-trusted \
	$(outputdir)/${KEYRING_NAME}-revoked

$(KEYRING_NAME)-keyring-$(V).tar.gz: %.tar.gz: $(keyring-files)
	bsdtar --format=ustar -cf - -C $(cachedir) $(addprefix $*/,$(notdir $^)) | gzip -9 > $@

define Makefile.in
V=@V@

prefix = /usr/local
PREFIX = $$(prefix)

install:
	install -dm755 $$(DESTDIR)$$(PREFIX)/share/pacman/keyrings/
	install -m0644 @KEYRING_NAME@{.gpg,-trusted,-revoked} $$(DESTDIR)$$(PREFIX)/share/pacman/keyrings/

uninstall:
	rm -f $$(DESTDIR)$$(PREFIX)/share/pacman/keyrings/@KEYRING_NAME@{.gpg,-trusted,-revoked}
	rmdir -p --ignore-fail-on-non-empty $$(DESTDIR)$$(PREFIX)/share/pacman/keyrings/

.PHONY: install uninstall
endef

$(outputdir)/Makefile: $(cachedir)/txt.Makefile.in $(var)V $(var)KEYRING_NAME | $(outputdir)
	sed $(foreach v,$(patsubst $(var)%,%,$(filter $(var)%,$^)), -e 's|@$v@|$($v)|' ) < $< > $@


users := $(sort $(shell find $(yamldir))) $(var)users

# Assemble the list of .asc files needed to generate the keyring
$(cachedir)/deps.mk: ${users} $(var)outputdir $(var)cachedir $(var)KEYRING_NAME| $(cachedir)
	{ \
		echo $(outputdir)/${KEYRING_NAME}.gpg: $$($(bin)/pgp-list-keyids | sed -r 's|(\S+) .*|$$(cachedir)/keys/\1.asc|') && \
		echo $(cachedir)/stamp.ownertrust: $$($(bin)/pgp-list-keyids | sed -rn 's|^(trusted/\S+) .*|$$(cachedir)/keys/\1.asc|p') && \
		$(bin)/pgp-list-keyids | sed -rn 's|^trusted/(\S+) (.*)|keyid.\1 = \2|p' && \
		$(bin)/uid-map | sed 's|.*|trusted:&\nsecondary:&\nrevoked:&|' | sed -r 's|(.*):(.*):(.*)|$$(cachedir)/keys/\1/\3.asc: $$(yamldir)/\2.yml|' && \
	:; }> $@
-include $(cachedir)/deps.mk

# The remainder of file is mostly just a translation of the shell
# script `update-keys`.
#
#     https://git.archlinux.org/archlinux-keyring.git/tree/update-keys

export LANG=C

KEYSERVER = ${keyserver}

GPG = gpg --quiet --batch --no-tty --no-permission-warning --keyserver ${KEYSERVER} --homedir $(cachedir)/gpghome

define gpg-init
%echo Generating Parabola Keyring keychain master key...
Key-Type: RSA
Key-Length: 1024
Key-Usage: sign
Name-Real: Parabola Keyring Keychain Master Key
Name-Email: parabola-keyring@localhost
Expire-Date: 0
%no-protection
%commit
%echo Done
endef
$(cachedir)/stamp.gpg-init: $(cachedir)/txt.gpg-init $(var)GPG | $(cachedir)/gpghome
	${GPG} --gen-key < $<
	touch $@

# The appropriate ${uid}.yml file is added as a dependency to
# ${username}.yml by deps.mk
keyid=$(keyid.$(patsubst %.asc,%,$(notdir $@)))

# In 'update-keys', this is the 'master-keyids' loop
$(outputdir)/${KEYRING_NAME}-trusted: ${users} | $(outputdir)
	$(bin)/pgp-list-keyids | sed -rn 's|^trusted/\S+ (\S+)|\1:4:|p' > $@
$(cachedir)/keys/trusted/%.asc  : $(cachedir)/stamp.gpg-init   | $(cachedir)/keys/trusted
	${GPG} --recv-keys ${keyid}
	printf 'minimize\nquit\ny\n' | ${GPG} --command-fd 0 --edit-key ${keyid}
	printf 'y\ny\n' | ${GPG} --command-fd 0 --lsign-key ${keyid}
	${GPG} --armor --no-emit-version --export ${keyid} > $@

$(cachedir)/stamp.ownertrust: $(outputdir)/${KEYRING_NAME}-trusted $(cachedir)/deps.mk
	${GPG} --import-ownertrust < $<
	touch $@

# In 'update-keys', this is the 'packager-keyids' loop
$(cachedir)/keys/secondary/%.asc: $(cachedir)/stamp.ownertrust | $(cachedir)/keys/secondary
	${GPG} --recv-keys ${keyid}
	printf 'clean\nquit\ny\n' | ${GPG} --command-fd 0 --edit-key ${keyid}
	${GPG} --list-keys --with-colons ${keyid} | grep -q '^pub:f:' # make sure it is trusted
	${GPG} --armor --no-emit-version --export ${keyid} > $@

# In 'update-keys', this is the 'packager-revoked-keyids' loop
$(outputdir)/${KEYRING_NAME}-revoked: ${users} | $(outputdir)
	$(bin)/pgp-list-keyids | sed -rn 's|^revoked/\S+ ||p' > $@
$(cachedir)/keys/revoked/%.asc  : $(cachedir)/stamp.ownertrust | $(cachedir)/keys/revoked
	${GPG} --recv-keys ${keyid}
	printf 'clean\nquit\ny\n' | ${GPG} --command-fd 0 --edit-key ${keyid}
	! ${GPG} --list-keys --with-colons ${keyid} | grep -q '^pub:f:' # make sure it isn't trusted
	${GPG} --armor --no-emit-version --export ${keyid} > $@

$(outputdir)/${KEYRING_NAME}.gpg: $(cachedir)/deps.mk | $(outputdir)
	cat $(filter %.asc,$^) > $@