update-ca-certificates 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #!/bin/sh -e
  2. #
  3. # update-ca-certificates
  4. #
  5. # Copyright (c) 2003 Fumitoshi UKAI <ukai@debian.or.jp>
  6. # Copyright (c) 2009 Philipp Kern <pkern@debian.org>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1301,
  21. # USA.
  22. #
  23. verbose=0
  24. fresh=0
  25. default=0
  26. CERTSCONF=$SYSROOT/etc/ca-certificates.conf
  27. CERTSDIR=$SYSROOT/usr/share/ca-certificates
  28. LOCALCERTSDIR=$SYSROOT/usr/local/share/ca-certificates
  29. CERTBUNDLE=ca-certificates.crt
  30. ETCCERTSDIR=$SYSROOT/etc/ssl/certs
  31. HOOKSDIR=$SYSROOT/etc/ca-certificates/update.d
  32. while [ $# -gt 0 ];
  33. do
  34. case $1 in
  35. --verbose|-v)
  36. verbose=1;;
  37. --fresh|-f)
  38. fresh=1;;
  39. --default|-d)
  40. default=1
  41. fresh=1;;
  42. --certsconf)
  43. shift
  44. CERTSCONF="$1";;
  45. --certsdir)
  46. shift
  47. CERTSDIR="$1";;
  48. --localcertsdir)
  49. shift
  50. LOCALCERTSDIR="$1";;
  51. --certbundle)
  52. shift
  53. CERTBUNDLE="$1";;
  54. --etccertsdir)
  55. shift
  56. ETCCERTSDIR="$1";;
  57. --hooksdir)
  58. shift
  59. HOOKSDIR="$1";;
  60. --help|-h|*)
  61. echo "$0: [--verbose] [--fresh]"
  62. exit;;
  63. esac
  64. shift
  65. done
  66. if [ -z "$SYSROOT" ]; then
  67. local_which () {
  68. if [ $# -lt 1 ]; then
  69. return 1
  70. fi
  71. (
  72. IFS=:
  73. for entry in $PATH; do
  74. if [ -x "$entry/$1" ]; then
  75. echo "$entry/$1"
  76. exit 0
  77. fi
  78. done
  79. exit 1
  80. )
  81. }
  82. case "$0" in
  83. */*)
  84. sbindir=$(cd ${0%/*} && pwd)
  85. ;;
  86. *)
  87. sbindir=$(cd $(dirname $(local_which $0)) && pwd)
  88. ;;
  89. esac
  90. prefix=${sbindir%/*}
  91. SYSROOT=${prefix%/*}
  92. if [ ! -d "$SYSROOT/usr/share/ca-certificates" ]; then
  93. SYSROOT=
  94. fi
  95. fi
  96. if [ ! -s "$CERTSCONF" ]
  97. then
  98. fresh=1
  99. fi
  100. cleanup() {
  101. rm -f "$TEMPBUNDLE"
  102. rm -f "$ADDED"
  103. rm -f "$REMOVED"
  104. }
  105. trap cleanup 0
  106. # Helper files. (Some of them are not simple arrays because we spawn
  107. # subshells later on.)
  108. TEMPBUNDLE="$(mktemp -p${TMPDIR:-/tmp} "${CERTBUNDLE}.tmp.XXXXXX")"
  109. ADDED="$(mktemp -p${TMPDIR:-/tmp} "ca-certificates.tmp.XXXXXX")"
  110. REMOVED="$(mktemp -p${TMPDIR:-/tmp} "ca-certificates.tmp.XXXXXX")"
  111. # Adds a certificate to the list of trusted ones. This includes a symlink
  112. # in /etc/ssl/certs to the certificate file and its inclusion into the
  113. # bundle.
  114. add() {
  115. CERT="$1"
  116. PEM="$ETCCERTSDIR/$(basename "$CERT" .crt | sed -e 's/ /_/g' \
  117. -e 's/[()]/=/g' \
  118. -e 's/,/_/g').pem"
  119. if ! test -e "$PEM" || [ "$(readlink "$PEM")" != "${CERT##$SYSROOT}" ]
  120. then
  121. ln -sf "${CERT##$SYSROOT}" "$PEM"
  122. echo "+$PEM" >> "$ADDED"
  123. fi
  124. # Add trailing newline to certificate, if it is missing (#635570)
  125. sed -e '$a\' "$CERT" >> "$TEMPBUNDLE"
  126. }
  127. remove() {
  128. CERT="$1"
  129. PEM="$ETCCERTSDIR/$(basename "$CERT" .crt).pem"
  130. if test -L "$PEM"
  131. then
  132. rm -f "$PEM"
  133. echo "-$PEM" >> "$REMOVED"
  134. fi
  135. }
  136. cd "$ETCCERTSDIR"
  137. if [ "$fresh" = 1 ]; then
  138. echo "Clearing symlinks in $ETCCERTSDIR..."
  139. find . -type l -print | while read symlink
  140. do
  141. case $(readlink "$symlink") in
  142. $CERTSDIR*|$LOCALCERTSDIR*) rm -f $symlink;;
  143. esac
  144. done
  145. find . -type l -print | while read symlink
  146. do
  147. test -f "$symlink" || rm -f "$symlink"
  148. done
  149. echo "done."
  150. fi
  151. echo "Updating certificates in $ETCCERTSDIR..."
  152. # Add default certificate authorities if requested
  153. if [ "$default" = 1 ]; then
  154. find -L "$CERTSDIR" -type f -name '*.crt' | sort | while read crt
  155. do
  156. add "$crt"
  157. done
  158. fi
  159. # Handle certificates that should be removed. This is an explicit act
  160. # by prefixing lines in the configuration files with exclamation marks (!).
  161. sed -n -e '/^$/d' -e 's/^!//p' "$CERTSCONF" | while read crt
  162. do
  163. remove "$CERTSDIR/$crt"
  164. done
  165. sed -e '/^$/d' -e '/^#/d' -e '/^!/d' "$CERTSCONF" | while read crt
  166. do
  167. if ! test -f "$CERTSDIR/$crt"
  168. then
  169. echo "W: $CERTSDIR/$crt not found, but listed in $CERTSCONF." >&2
  170. continue
  171. fi
  172. add "$CERTSDIR/$crt"
  173. done
  174. # Now process certificate authorities installed by the local system
  175. # administrator.
  176. if [ -d "$LOCALCERTSDIR" ]
  177. then
  178. find -L "$LOCALCERTSDIR" -type f -name '*.crt' | sort | while read crt
  179. do
  180. add "$crt"
  181. done
  182. fi
  183. rm -f "$CERTBUNDLE"
  184. ADDED_CNT=$(wc -l < "$ADDED")
  185. REMOVED_CNT=$(wc -l < "$REMOVED")
  186. if [ "$ADDED_CNT" -gt 0 ] || [ "$REMOVED_CNT" -gt 0 ]
  187. then
  188. # only run if set of files has changed
  189. if [ "$verbose" = 0 ]
  190. then
  191. c_rehash . > /dev/null
  192. else
  193. c_rehash .
  194. fi
  195. fi
  196. chmod 0644 "$TEMPBUNDLE"
  197. mv -f "$TEMPBUNDLE" "$CERTBUNDLE"
  198. # Restore proper SELinux label after moving the file
  199. [ -x /sbin/restorecon ] && /sbin/restorecon "$CERTBUNDLE" >/dev/null 2>&1
  200. echo "$ADDED_CNT added, $REMOVED_CNT removed; done."
  201. if [ -d "$HOOKSDIR" ]
  202. then
  203. echo "Running hooks in $HOOKSDIR..."
  204. eval run-parts --test "$HOOKSDIR" | while read hook
  205. do
  206. ( cat "$ADDED"
  207. cat "$REMOVED" ) | "$hook" || echo "E: $hook exited with code $?."
  208. done
  209. echo "done."
  210. fi
  211. # vim:set et sw=2: