buildd-remove-keys 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. base="${base}/scripts/builddkeyrings"
  45. INCOMING="${base}/incoming"
  46. ERRORS="${base}/errors"
  47. ADMINS="${base}/adminkeys.gpg"
  48. REMOVED="${base}/removed-buildd-keys.gpg"
  49. STAMPFILE="${base}/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 "${base}/${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. if ! gpg ${DEFGPGOPT} --keyring "${ADMINS}" --status-fd 4 --logger-fd 5 --decrypt "${INCOMING}/${file}" > "${GPGOUTF}"; then
  113. ret=$?
  114. log "gpg returned with ${ret}, not removing key using ${file}"
  115. DATE=$(date -Is)
  116. mv "${INCOMING}/${file}" "${ERRORS}/gpgerror.${file}.${DATE}"
  117. mv "${GPGSTATUS}" "${ERRORS}/gpgerror.${file}.gpgstatus.${DATE}"
  118. mv "${GPGLOGS}" "${ERRORS}/gpgerror.${file}.gpglogs.${DATE}"
  119. rm -f "${GPGOUTF}"
  120. continue
  121. fi
  122. # Read in the status output
  123. GPGSTAT=$(cat "${GPGSTATUS}")
  124. # And check if we like the sig. It has to be both, GOODISG and VALIDSIG or we don't accept it
  125. if [[ ${GPGSTAT} =~ "GOODSIG" ]] && [[ ${GPGSTAT} =~ "VALIDSIG" ]]; then
  126. log "Signature for ${file} accepted"
  127. else
  128. log "We are missing one of GOODSIG or VALIDSIG"
  129. DATE=$(date -Is)
  130. mv "${INCOMING}/${file}" "${ERRORS}/badsig.${file}.${DATE}"
  131. mv "${GPGSTATUS}" "${ERRORS}/badsig.${file}.gpgstatus.${DATE}"
  132. mv "${GPGLOGS}" "${ERRORS}/badsig.${file}.gpglogs.${DATE}"
  133. rm -f "${GPGOUTF}"
  134. continue
  135. fi
  136. # So at this point we know we accepted the signature of the file as valid,
  137. # that is it is from a key allowed for this architecture. Which only
  138. # leaves us with the task of checking if there is a key to remove, and then remove
  139. # it. We won't even check they have a key left, so if they want to they can
  140. # empty out the set for an architecture
  141. # Read in the GPGOUTF, but avoid using a subshell like a
  142. # while read line otherwise would do
  143. exec 4<> "${GPGOUTF}"
  144. error=""
  145. while read line <&4; do
  146. if [[ $line =~ key:.([0-9A-F]{16}) ]]; then
  147. KEYID=${BASH_REMATCH[1]}
  148. elif [[ $line =~ comment:.(.*) ]]; then
  149. COMMENT=${BASH_REMATCH[1]}
  150. else
  151. echo "Nay"
  152. fi
  153. done
  154. COMMENT=${COMMENT:-"The bad ${KEYSUBMITTER} hasn't supplied a comment"}
  155. # Right, we have the keyid, know the arch, lets see if we can remove it
  156. ARCHKEYRING="${base}/${ARCH}/keyring.gpg"
  157. # Is the key in there?
  158. KEYNO=$(gpg ${DEFGPGOPT} --keyring "${ARCHKEYRING}" --with-colons --list-keys ${KEYID} | grep -c '^pub:' || /bin/true )
  159. if [ $KEYNO -eq 1 ]; then
  160. # Right, exactly one there, lets get rid of it
  161. # So put it into the removed keyring
  162. gpg ${DEFGPGOPT} --keyring "${ARCHKEYRING}" --export ${KEYID} | gpg ${DEFGPGOPT} --keyring "${REMOVED}" --import 2>/dev/null
  163. if gpg ${DEFGPGOPT} --keyring "${ARCHKEYRING}" --yes --delete-keys ${KEYID}; then
  164. KEYSUBMITTER=$(cat "${GPGSTATUS}"|grep GOODSIG)
  165. KEYSUBMITTER=${KEYSUBMITTER##*GOODSIG}
  166. log "${KEYSUBMITTER} removed key ${KEYID} for ${ARCH} buildd ${BUILDD}, reason: ${COMMENT}"
  167. mv "${INCOMING}/${file}" "${base}/${ARCH}"
  168. continue
  169. fi
  170. else
  171. log "Found more (or less) than one key I could delete. Not doing anything"
  172. DATE=$(date -Is)
  173. mv "${INCOMING}/${file}" "${ERRORS}/toomanykeys.${file}.${DATE}"
  174. mv "${GPGSTATUS}" "${ERRORS}/toomanykeys.${file}.gpgstatus.${DATE}"
  175. mv "${GPGLOGS}" "${ERRORS}/toomanykeys.${file}.gpglogs.${DATE}"
  176. echo "${error}" >> "${ERRORS}/toomanykeys.${file}.error.${DATE}"
  177. rm -f "${GPGOUTF}"
  178. continue
  179. fi
  180. done