]> git.decadent.org.uk Git - dak.git/blob - scripts/debian/buildd-add-keys
Merge branch 'master' of ssh://ftp-master.debian.org/srv/ftp.debian.org/git/dak
[dak.git] / scripts / debian / buildd-add-keys
1 #!/bin/bash
2 # No way I try to deal with a crippled sh just for POSIX foo.
3
4 # Copyright (C) 2011 Joerg Jaspert <joerg@debian.org>
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; version 2.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # 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, write to the Free Software
17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19
20 # exit on errors
21 set -e
22 # make sure to only use defined variables
23 set -u
24 # ERR traps should be inherited from functions too.
25 set -E
26
27 # import the general variable set.
28 export SCRIPTVARS=/srv/ftp-master.debian.org/dak/config/debian/vars
29 . $SCRIPTVARS
30
31 umask 027
32
33 # And use one locale, no matter what the caller has set
34 export LANG=C
35 export LC_ALL=C
36 PROGRAM="buildd-add-keys"
37
38 # common functions are "outsourced"
39 . "${configdir}/common"
40
41 function cleanup() {
42     ERRVAL=$?
43     trap - ERR EXIT TERM HUP INT QUIT
44
45     for TEMPFILE in GPGSTATUS GPGLOGS GPGOUTF TEMPKEYDATA; do
46         TFILE=${TEMPFILE:=$TEMPFILE}
47         DELF=${!TFILE:-""}
48         if [ -n "${DELF}" ] && [ -f "${DELF}" ]; then
49             rm -f "${DELF}"
50         fi
51     done
52     exit $ERRVAL
53 }
54
55 base="${base}/scripts/builddkeyrings"
56 INCOMING="${base}/incoming"
57 ERRORS="${base}/errors"
58 ADMINS="${base}/adminkeys.gpg"
59 STAMPFILE="${base}/updatedkeyring"
60
61 # Default options for our gpg calls
62 DEFGPGOPT="--no-default-keyring --batch --no-tty --no-options --exit-on-status-write-error --no-greeting"
63
64 if ! [ -d "${INCOMING}" ]; then
65     log "Missing incoming dir, nothing to do"
66     exit 1
67 fi
68
69 cd "${INCOMING}"
70 KEYS=$(find . -maxdepth 1 -mindepth 1 -type f -name \*.key | sed -e "s,./,," | xargs)
71 if [ -z "${KEYS}" ]; then
72     exit 0
73 fi
74
75 trap cleanup ERR EXIT TERM HUP INT QUIT
76
77 # Whenever something goes wrong, its put in there.
78 mkdir -p "${ERRORS}"
79
80 # We process all new files in our incoming directory
81 for file in ${KEYS}; do
82     file=${file##*/}
83     # First we want to see if we recognize the filename. The buildd people have
84     # to follow a certain schema:
85     # architecture_builddname.YEAR-MONTH-DAY_HOURMINUTE.key
86     if [[ $file =~ (.*)_(.*).([0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}[0-9]{2}).key ]]; then
87         ARCH=${BASH_REMATCH[1]}
88         BUILDD=${BASH_REMATCH[2]}
89         # Right now timestamp is unused
90         TIMESTAMP=${BASH_REMATCH[3]}
91     else
92         log "Unknown file ${file}, not processing"
93         mv "${INCOMING}/${file}" "${ERRORS}/unknown.${file}.$(date -Is)"
94         continue
95     fi
96
97     # Do we know the architecture?
98     found=0
99     for carch in ${archs}; do
100         if [ "${ARCH}" == "${carch}" ]; then
101             log "Known arch ${ARCH}, buildd ${BUILDD}"
102             found=1
103             break
104         fi
105     done
106
107     if [ ${found} -eq 0 ]; then
108         log "Unknown architecture ${ARCH}"
109         mv "${INCOMING}/${file}" "${ERRORS}/unknownarch.${file}.$(date -Is)"
110         continue
111     fi
112
113     # If we did have a file with this name already somethings wrong
114     if [ -f "${base}/${ARCH}/${file}" ]; then
115         log "Already processed this file"
116         mv "${INCOMING}/${file}" "${ERRORS}/duplicate.${file}.$(date -Is)"
117         continue
118     fi
119
120     # Where we want the status-fd from gpgv turn up
121     GPGSTATUS=$(mktemp -p "${TMPDIR}" GPGSTATUS.XXXXXX)
122     # Same for the loggger-fd
123     GPGLOGS=$(mktemp -p "${TMPDIR}" GPGLOGS.XXXXXX)
124     # And "decrypt" gives us output, the key without the pgp sig around it
125     GPGOUTF=$(mktemp -p "${TMPDIR}" GPGOUTF.XXXXXX)
126
127     # Open the filehandles, assigning them to the two files, so we can let gpg use them
128     exec 4> "${GPGSTATUS}"
129     exec 5> "${GPGLOGS}"
130
131     # So lets run gpg, status/logger into the two files, to "decrypt" the keyfile
132     if ! gpg ${DEFGPGOPT} --keyring "${ADMINS}" --status-fd 4 --logger-fd 5 --decrypt "${INCOMING}/${file}" > "${GPGOUTF}"; then
133         ret=$?
134         log "gpg returned with ${ret}, not adding key from file ${file}"
135         DATE=$(date -Is)
136         mv "${INCOMING}/${file}" "${ERRORS}/gpgerror.${file}.${DATE}"
137         mv "${GPGSTATUS}" "${ERRORS}/gpgerror.${file}.gpgstatus.${DATE}"
138         mv "${GPGLOGS}" "${ERRORS}/gpgerror.${file}.gpglogs.${DATE}"
139         rm -f "${GPGOUTF}"
140         continue
141     fi
142
143     # Read in the status output
144     GPGSTAT=$(cat "${GPGSTATUS}")
145     # And check if we like the sig. It has to be both, GOODISG and VALIDSIG or we don't accept it
146     if [[ ${GPGSTAT} =~ "GOODSIG" ]] && [[ ${GPGSTAT} =~ "VALIDSIG" ]]; then
147         log "Signature for ${file} accepted"
148     else
149         log "We are missing one of GOODSIG or VALIDSIG"
150         DATE=$(date -Is)
151         mv "${INCOMING}/${file}" "${ERRORS}/badsig.${file}.${DATE}"
152         mv "${GPGSTATUS}" "${ERRORS}/badsig.${file}.gpgstatus.${DATE}"
153         mv "${GPGLOGS}" "${ERRORS}/badsig.${file}.gpglogs.${DATE}"
154         rm -f "${GPGOUTF}"
155         continue
156     fi
157
158     # So at this point we know we accepted the signature of the file as valid,
159     # that is it is from a key allowed for this architecture. Which only
160     # leaves us with the task of checking if the key fulfills the requirements
161     # before we add it to the architectures keyring.
162
163     # Those currently are:
164     # - keysize 4096 or larger
165     # - RSA key, no encryption capability
166     # - UID matching "buildd autosigning key BUILDDNAME <buildd_ARCH-BUILDDNAME@buildd.debian.org>
167     # - expire within a 120 days
168     # - maximum 2 keys per architecture and buildd
169
170     TEMPKEYDATA=$(mktemp -p "${TMPDIR}" BDKEYS.XXXXXX)
171
172     gpg ${DEFGPGOPT} --with-colons "${GPGOUTF}" > "${TEMPKEYDATA}"
173
174     # Read in the TEMPKEYDATAFILE, but avoid using a subshell like a
175     # while read line otherwise would do
176     exec 4<> "${TEMPKEYDATA}"
177     error=""
178     while read line <&4; do
179         #pub:-:4096:1:FAB983612A6554FA:2011-03-24:2011-07-22::-:buildd autosigning key poulenc <buildd_powerpc-poulenc@buildd.debian.org>:
180
181         # Besides fiddling out the data we need to check later, this regex also check:
182         # - the keytype (:1:, 1 there means RSA)
183         # - the UID
184         # - that the key does have an expiration date (or it wont match, the second date
185         #   field would be empty
186         regex="^pub:-:([0-9]{4}):1:([0-9A-F]{16}):([0-9]{4}-[0-9]{2}-[0-9]{2}):([0-9]{4}-[0-9]{2}-[0-9]{2})::-:buildd autosigning key ${BUILDD} <buildd_${ARCH}-${BUILDD}@buildd.debian.org>:$"
187         if [[ $line =~ $regex ]]; then
188             KEYSIZE=${BASH_REMATCH[1]}
189             KEYID=${BASH_REMATCH[2]}
190             KEYCREATE=${BASH_REMATCH[3]}
191             KEYEXPIRE=${BASH_REMATCH[4]}
192
193             # We do want 4096 or anything above
194             if [ ${KEYSIZE} -lt 4096 ]; then
195                 log "Keysize ${KEYSIZE} too small"
196                 error="${error} Keysize ${KEYSIZE} too small"
197                 continue
198             fi
199
200             # We want a maximum lifetime of 120 days, so check that.
201             # Easiest to compare in epoch, so lets see, 120 days midnight from now,
202             # compared with their set expiration date at midnight
203             # maxdate should turn out higher. just in case we make it 121 for this check
204             maxdate=$(date -d '121 day 00:00:00' +%s)
205             theirexpire=$(date -d "${KEYEXPIRE} 00:00:00" +%s)
206             if [ ${theirexpire} -gt ${maxdate} ]; then
207                 log "Key expiry ${KEYEXPIRE} wrong"
208                 error="${error} Key expiry ${KEYEXPIRE} wrong"
209                 continue
210             fi
211         else
212             log "Unknown line $line, sod off"
213             error="${error} Unknown line $line, sod off"
214             continue
215         fi
216     done
217     if [ -n "${error}" ]; then
218         log ${error}
219         DATE=$(date -Is)
220         mv "${INCOMING}/${file}" "${ERRORS}/badkey.${file}.${DATE}"
221         mv "${GPGSTATUS}" "${ERRORS}/badkey.${file}.gpgstatus.${DATE}"
222         mv "${GPGLOGS}" "${ERRORS}/badkey.${file}.gpglogs.${DATE}"
223         echo "${error}" >> "${ERRORS}/badkey.${file}.error.${DATE}"
224         rm -f "${GPGOUTF}"
225         continue
226     fi
227
228     # And now lets check how many keys this buildd already has. 2 is the maximum, so key
229     # rollover works. 3 won't, they have to rm one first
230     # We need to check for the amount of keys
231     ARCHKEYRING="${base}/${ARCH}/keyring.gpg"
232
233     KEYNO=$(gpg ${DEFGPGOPT} --keyring "${ARCHKEYRING}" --with-colons --list-keys "buildd_${ARCH}-${BUILDD}@buildd.debian.org" | grep -c '^pub:' || /bin/true )
234     if [ ${KEYNO} -gt 2 ]; then
235         DATE=$(date -Is)
236         mv "${INCOMING}/${file}" "${ERRORS}/toomany.${file}.${DATE}"
237         mv "${GPGSTATUS}" "${ERRORS}/toomany.${file}.gpgstatus.${DATE}"
238         mv "${GPGLOGS}" "${ERRORS}/toomany.${file}.gpglogs.${DATE}"
239         rm -f "${GPGOUTF}"
240         continue
241     fi
242
243     # Right. At this point everything should be in order, which means we should put the key into
244     # the keyring
245     log "Accepting key ${KEYID} for ${ARCH} buildd ${BUILDD}, expire ${KEYEXPIRE}"
246     gpg ${DEFGPGOPT} --status-fd 4 --logger-fd 5 --keyring "${ARCHKEYRING}" --import "${GPGOUTF}" 2>/dev/null
247
248     mv "${INCOMING}/${file}" "${base}/${ARCH}"
249 done