buildd-remove-keys 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #!/bin/bash
  2. # No way I try to deal with a crippled sh just for POSIX foo.
  3. # Copyright (C) 2011 Joerg Jaspert <joerg@debian.org>
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License as
  7. # published by the Free Software Foundation; version 2.
  8. #
  9. # This program is distributed in the hope that it will be useful, but
  10. # WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. # exit on errors
  18. set -e
  19. # make sure to only use defined variables
  20. set -u
  21. # ERR traps should be inherited from functions too.
  22. set -E
  23. # import the general variable set.
  24. export SCRIPTVARS=/srv/ftp-master.debian.org/dak/config/debian/vars
  25. . $SCRIPTVARS
  26. umask 027
  27. # And use one locale, no matter what the caller has set
  28. export LANG=C
  29. export LC_ALL=C
  30. PROGRAM="buildd-remove-keys"
  31. # common functions are "outsourced"
  32. . "${configdir}/common"
  33. function cleanup() {
  34. ERRVAL=$?
  35. trap - ERR EXIT TERM HUP INT QUIT
  36. for TEMPFILE in GPGSTATUS GPGLOGS GPGOUTF TEMPKEYDATA; do
  37. DELF=${!TEMPFILE:-""}
  38. if [ -n "${DELF}" ] && [ -f "${DELF}" ]; then
  39. rm -f "${DELF}"
  40. fi
  41. done
  42. exit $ERRVAL
  43. }
  44. buildkeybase="${base}/scripts/builddkeyrings"
  45. INCOMING="${buildkeybase}/incoming"
  46. ERRORS="${buildkeybase}/errors"
  47. ADMINS="${buildkeybase}/adminkeys.gpg"
  48. REMOVED="${buildkeybase}/removed-buildd-keys.gpg"
  49. STAMPFILE="${buildkeybase}/updatedkeyring"
  50. # Default options for our gpg calls
  51. DEFGPGOPT="--no-default-keyring --batch --no-tty --no-options --exit-on-status-write-error --no-greeting"
  52. if ! [ -d "${INCOMING}" ]; then
  53. log "Missing incoming dir, nothing to do"
  54. exit 1
  55. fi
  56. cd "${INCOMING}"
  57. KEYS=$(find . -maxdepth 1 -mindepth 1 -type f -name \*.del | sed -e "s,./,," | xargs)
  58. if [ -z "${KEYS}" ]; then
  59. exit 0
  60. fi
  61. trap cleanup ERR EXIT TERM HUP INT QUIT
  62. # Tell prepare-dir that there is an update and it can run
  63. touch "${STAMPFILE}"
  64. # Whenever something goes wrong, its put in there.
  65. mkdir -p "${ERRORS}"
  66. # We process all new files in our incoming directory
  67. for file in ${KEYS}; do
  68. file=${file##*/}
  69. # First we want to see if we recognize the filename. The buildd people have
  70. # to follow a certain schema:
  71. # architecture_builddname.YEAR-MONTH-DAY_HOURMINUTE.del
  72. if [[ $file =~ (.*)_(.*).([0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}[0-9]{2}).del ]]; then
  73. ARCH=${BASH_REMATCH[1]}
  74. BUILDD=${BASH_REMATCH[2]}
  75. # Right now timestamp is unused
  76. TIMESTAMP=${BASH_REMATCH[3]}
  77. else
  78. log "Unknown file ${file}, not processing"
  79. mv "${INCOMING}/${file}" "${ERRORS}/unknown.${file}.$(date -Is)"
  80. continue
  81. fi
  82. # Do we know the architecture?
  83. found=0
  84. for carch in ${archs}; do
  85. if [ "${ARCH}" == "${carch}" ]; then
  86. log "Known arch ${ARCH}, buildd ${BUILDD}"
  87. found=1
  88. break
  89. fi
  90. done
  91. if [ ${found} -eq 0 ]; then
  92. log "Unknown architecture ${ARCH}"
  93. mv "${INCOMING}/${file}" "${ERRORS}/unknownarch.${file}.$(date -Is)"
  94. continue
  95. fi
  96. # If we did have a file with this name already somethings wrong
  97. if [ -f "${buildkeybase}/${ARCH}/${file}" ]; then
  98. log "Already processed this file"
  99. mv "${INCOMING}/${file}" "${ERRORS}/duplicate.${file}.$(date -Is)"
  100. continue
  101. fi
  102. # Where we want the status-fd from gpgv turn up
  103. GPGSTATUS=$(mktemp -p "${TMPDIR}" GPGSTATUS.XXXXXX)
  104. # Same for the loggger-fd
  105. GPGLOGS=$(mktemp -p "${TMPDIR}" GPGLOGS.XXXXXX)
  106. # And "decrypt" gives us output, the key without the pgp sig around it
  107. GPGOUTF=$(mktemp -p "${TMPDIR}" GPGOUTF.XXXXXX)
  108. # Open the filehandles, assigning them to the two files, so we can let gpg use them
  109. exec 4> "${GPGSTATUS}"
  110. exec 5> "${GPGLOGS}"
  111. # So lets run gpg, status/logger into the two files, to "decrypt" the keyfile
  112. set +e
  113. gpg ${DEFGPGOPT} --keyring "${ADMINS}" --status-fd 4 --logger-fd 5 --decrypt "${INCOMING}/${file}" > "${GPGOUTF}"
  114. ret=$?
  115. set -e
  116. if [[ ${ret} -ne 0 ]]; then
  117. log "gpg returned with ${ret}, not removing key using ${file}"
  118. DATE=$(date -Is)
  119. mv "${INCOMING}/${file}" "${ERRORS}/gpgerror.${file}.${DATE}"
  120. mv "${GPGSTATUS}" "${ERRORS}/gpgerror.${file}.gpgstatus.${DATE}"
  121. mv "${GPGLOGS}" "${ERRORS}/gpgerror.${file}.gpglogs.${DATE}"
  122. rm -f "${GPGOUTF}"
  123. continue
  124. fi
  125. # Read in the status output
  126. GPGSTAT=$(cat "${GPGSTATUS}")
  127. # And check if we like the sig. It has to be both, GOODISG and VALIDSIG or we don't accept it
  128. if [[ ${GPGSTAT} =~ "GOODSIG" ]] && [[ ${GPGSTAT} =~ "VALIDSIG" ]]; then
  129. log "Signature for ${file} accepted"
  130. else
  131. log "We are missing one of GOODSIG or VALIDSIG"
  132. DATE=$(date -Is)
  133. mv "${INCOMING}/${file}" "${ERRORS}/badsig.${file}.${DATE}"
  134. mv "${GPGSTATUS}" "${ERRORS}/badsig.${file}.gpgstatus.${DATE}"
  135. mv "${GPGLOGS}" "${ERRORS}/badsig.${file}.gpglogs.${DATE}"
  136. rm -f "${GPGOUTF}"
  137. continue
  138. fi
  139. # So at this point we know we accepted the signature of the file as valid,
  140. # that is it is from a key allowed for this architecture. Which only
  141. # leaves us with the task of checking if there is a key to remove, and then remove
  142. # it. We won't even check they have a key left, so if they want to they can
  143. # empty out the set for an architecture
  144. # Read in the GPGOUTF, but avoid using a subshell like a
  145. # while read line otherwise would do
  146. exec 4<> "${GPGOUTF}"
  147. error=""
  148. while read line <&4; do
  149. if [[ $line =~ key:.([0-9A-F]{16}) ]]; then
  150. KEYID=${BASH_REMATCH[1]}
  151. elif [[ $line =~ comment:.(.*) ]]; then
  152. COMMENT=${BASH_REMATCH[1]}
  153. else
  154. echo "Nay"
  155. fi
  156. done
  157. COMMENT=${COMMENT:-"The bad ${KEYSUBMITTER} hasn't supplied a comment"}
  158. # Right, we have the keyid, know the arch, lets see if we can remove it
  159. ARCHKEYRING="${buildkeybase}/${ARCH}/keyring.gpg"
  160. # Is the key in there?
  161. KEYNO=$(gpg ${DEFGPGOPT} --keyring "${ARCHKEYRING}" --with-colons --list-keys ${KEYID} | grep -c '^pub:' || /bin/true )
  162. if [ $KEYNO -eq 1 ]; then
  163. # Right, exactly one there, lets get rid of it
  164. # So put it into the removed keyring
  165. gpg ${DEFGPGOPT} --keyring "${ARCHKEYRING}" --export ${KEYID} | gpg ${DEFGPGOPT} --keyring "${REMOVED}" --import 2>/dev/null
  166. if gpg ${DEFGPGOPT} --keyring "${ARCHKEYRING}" --yes --delete-keys ${KEYID}; then
  167. KEYSUBMITTER=$(cat "${GPGSTATUS}"|grep GOODSIG)
  168. KEYSUBMITTER=${KEYSUBMITTER##*GOODSIG}
  169. log "${KEYSUBMITTER} removed key ${KEYID} for ${ARCH} buildd ${BUILDD}, reason: ${COMMENT}"
  170. mv "${INCOMING}/${file}" "${buildkeybase}/${ARCH}"
  171. continue
  172. fi
  173. else
  174. log "Found more (or less) than one key I could delete. Not doing anything"
  175. DATE=$(date -Is)
  176. mv "${INCOMING}/${file}" "${ERRORS}/toomanykeys.${file}.${DATE}"
  177. mv "${GPGSTATUS}" "${ERRORS}/toomanykeys.${file}.gpgstatus.${DATE}"
  178. mv "${GPGLOGS}" "${ERRORS}/toomanykeys.${file}.gpglogs.${DATE}"
  179. echo "${error}" >> "${ERRORS}/toomanykeys.${file}.error.${DATE}"
  180. rm -f "${GPGOUTF}"
  181. continue
  182. fi
  183. done