]> git.decadent.org.uk Git - dak.git/blob - scripts/debian/buildd-remove-keys
ecd859d06192217d48f9b627d9ac4d0ab3729627
[dak.git] / scripts / debian / buildd-remove-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-remove-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         DELF=${!TEMPFILE:-""}
47         if [ -n "${DELF}" ] && [ -f "${DELF}" ]; then
48             rm -f "${DELF}"
49         fi
50     done
51     exit $ERRVAL
52 }
53
54 base="${base}/scripts/builddkeyrings"
55 INCOMING="${base}/incoming"
56 ERRORS="${base}/errors"
57 ADMINS="${base}/adminkeys.gpg"
58 REMOVED="${base}/removed-buildd-keys.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 \*.del | 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 # Tell prepare-dir that there is an update and it can run
78 touch "${STAMPFILE}"
79
80 # Whenever something goes wrong, its put in there.
81 mkdir -p "${ERRORS}"
82
83 # We process all new files in our incoming directory
84 for file in ${KEYS}; do
85     file=${file##*/}
86     # First we want to see if we recognize the filename. The buildd people have
87     # to follow a certain schema:
88     # architecture_builddname.YEAR-MONTH-DAY_HOURMINUTE.del
89     if [[ $file =~ (.*)_(.*).([0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}[0-9]{2}).del ]]; then
90         ARCH=${BASH_REMATCH[1]}
91         BUILDD=${BASH_REMATCH[2]}
92         # Right now timestamp is unused
93         TIMESTAMP=${BASH_REMATCH[3]}
94     else
95         log "Unknown file ${file}, not processing"
96         mv "${INCOMING}/${file}" "${ERRORS}/unknown.${file}.$(date -Is)"
97         continue
98     fi
99
100     # Do we know the architecture?
101     found=0
102     for carch in ${archs}; do
103         if [ "${ARCH}" == "${carch}" ]; then
104             log "Known arch ${ARCH}, buildd ${BUILDD}"
105             found=1
106             break
107         fi
108     done
109
110     if [ ${found} -eq 0 ]; then
111         log "Unknown architecture ${ARCH}"
112         mv "${INCOMING}/${file}" "${ERRORS}/unknownarch.${file}.$(date -Is)"
113         continue
114     fi
115
116     # If we did have a file with this name already somethings wrong
117     if [ -f "${base}/${ARCH}/${file}" ]; then
118         log "Already processed this file"
119         mv "${INCOMING}/${file}" "${ERRORS}/duplicate.${file}.$(date -Is)"
120         continue
121     fi
122
123     # Where we want the status-fd from gpgv turn up
124     GPGSTATUS=$(mktemp -p "${TMPDIR}" GPGSTATUS.XXXXXX)
125     # Same for the loggger-fd
126     GPGLOGS=$(mktemp -p "${TMPDIR}" GPGLOGS.XXXXXX)
127     # And "decrypt" gives us output, the key without the pgp sig around it
128     GPGOUTF=$(mktemp -p "${TMPDIR}" GPGOUTF.XXXXXX)
129
130     # Open the filehandles, assigning them to the two files, so we can let gpg use them
131     exec 4> "${GPGSTATUS}"
132     exec 5> "${GPGLOGS}"
133
134     # So lets run gpg, status/logger into the two files, to "decrypt" the keyfile
135     if ! gpg ${DEFGPGOPT} --keyring "${ADMINS}" --status-fd 4 --logger-fd 5 --decrypt "${INCOMING}/${file}" > "${GPGOUTF}"; then
136         ret=$?
137         log "gpg returned with ${ret}, not removing key using ${file}"
138         DATE=$(date -Is)
139         mv "${INCOMING}/${file}" "${ERRORS}/gpgerror.${file}.${DATE}"
140         mv "${GPGSTATUS}" "${ERRORS}/gpgerror.${file}.gpgstatus.${DATE}"
141         mv "${GPGLOGS}" "${ERRORS}/gpgerror.${file}.gpglogs.${DATE}"
142         rm -f "${GPGOUTF}"
143         continue
144     fi
145
146     # Read in the status output
147     GPGSTAT=$(cat "${GPGSTATUS}")
148     # And check if we like the sig. It has to be both, GOODISG and VALIDSIG or we don't accept it
149     if [[ ${GPGSTAT} =~ "GOODSIG" ]] && [[ ${GPGSTAT} =~ "VALIDSIG" ]]; then
150         log "Signature for ${file} accepted"
151     else
152         log "We are missing one of GOODSIG or VALIDSIG"
153         DATE=$(date -Is)
154         mv "${INCOMING}/${file}" "${ERRORS}/badsig.${file}.${DATE}"
155         mv "${GPGSTATUS}" "${ERRORS}/badsig.${file}.gpgstatus.${DATE}"
156         mv "${GPGLOGS}" "${ERRORS}/badsig.${file}.gpglogs.${DATE}"
157         rm -f "${GPGOUTF}"
158         continue
159     fi
160
161     # So at this point we know we accepted the signature of the file as valid,
162     # that is it is from a key allowed for this architecture. Which only
163     # leaves us with the task of checking if there is a key to remove, and then remove
164     # it. We won't even check they have a key left, so if they want to they can
165     # empty out the set for an architecture
166
167     # Read in the GPGOUTF, but avoid using a subshell like a
168     # while read line otherwise would do
169     exec 4<> "${GPGOUTF}"
170     error=""
171     while read line <&4; do
172         if [[ $line =~ key:.([0-9A-F]{16}) ]]; then
173             KEYID=${BASH_REMATCH[1]}
174         elif [[ $line =~ comment:.(.*) ]]; then
175             COMMENT=${BASH_REMATCH[1]}
176         else
177             echo "Nay"
178         fi
179     done
180
181     # Right, we have the keyid, know the arch, lets see if we can remove it
182     ARCHKEYRING="${base}/${ARCH}/keyring.gpg"
183
184     # Is the key in there?
185     KEYNO=$(gpg ${DEFGPGOPT} --keyring "${ARCHKEYRING}" --with-colons --list-keys ${KEYID} | grep -c '^pub:' || /bin/true )
186
187     if [ $KEYNO -eq 1 ]; then
188         # Right, exactly one there, lets get rid of it
189         # So put it into the removed keyring
190         gpg ${DEFGPGOPT} --keyring "${ARCHKEYRING}" --export ${KEYID} | gpg ${DEFGPGOPT} --keyring "${REMOVED}" --import 2>/dev/null
191         if gpg ${DEFGPGOPT} --keyring "${ARCHKEYRING}" --yes --delete-keys ${KEYID}; then
192             KEYSUBMITTER=$(cat "${GPGSTATUS}"|grep GOODSIG)
193             KEYSUBMITTER=${KEYSUBMITTER##*GOODSIG}
194             log "${KEYSUBMITTER} removed key ${KEYID} for ${ARCH} buildd ${BUILDD}, reason: ${COMMENT}"
195             mv "${INCOMING}/${file}" "${base}/${ARCH}"
196             continue
197         fi
198     else
199         log "Found more (or less) than one key I could delete. Not doing anything"
200         DATE=$(date -Is)
201         mv "${INCOMING}/${file}" "${ERRORS}/toomanykeys.${file}.${DATE}"
202         mv "${GPGSTATUS}" "${ERRORS}/toomanykeys.${file}.gpgstatus.${DATE}"
203         mv "${GPGLOGS}" "${ERRORS}/toomanykeys.${file}.gpglogs.${DATE}"
204         echo "${error}" >> "${ERRORS}/toomanykeys.${file}.error.${DATE}"
205         rm -f "${GPGOUTF}"
206         continue
207     fi
208 done