]> git.donarmstrong.com Git - ca-certificates.git/blob - sbin/update-ca-certificates
fc083d77dfce5f6a08ca5ca47282a3ba420752a8
[ca-certificates.git] / sbin / update-ca-certificates
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
24 verbose=0
25 fresh=0
26 while [ $# -gt 0 ];
27 do
28   case $1 in
29   --verbose|-v)
30         verbose=1;;
31   --fresh|-f)
32         fresh=1;;
33   --help|-h|*)
34         echo "$0: [--verbose] [--fresh]"
35         exit;;
36   esac
37   shift
38 done
39
40 CERTSCONF=/etc/ca-certificates.conf
41 CERTSDIR=/usr/share/ca-certificates
42 LOCALCERTSDIR=/usr/local/share/ca-certificates
43 CERTBUNDLE=ca-certificates.crt
44 ETCCERTSDIR=/etc/ssl/certs
45
46 cleanup() {
47   rm -f "$TEMPBUNDLE"
48   rm -f "$ADDED"
49   rm -f "$REMOVED"
50 }
51 trap cleanup 0
52
53 # Helper files.  (Some of them are not simple arrays because we spawn
54 # subshells later on.)
55 TEMPBUNDLE="$(mktemp -t "${CERTBUNDLE}.tmp.XXXXXX")"
56 ADDED="$(mktemp -t "ca-certificates.tmp.XXXXXX")"
57 REMOVED="$(mktemp -t "ca-certificates.tmp.XXXXXX")"
58
59 # Adds a certificate to the list of trusted ones.  This includes a symlink
60 # in /etc/ssl/certs to the certificate file and its inclusion into the
61 # bundle.
62 add() {
63   CERT="$1"
64   PEM="$ETCCERTSDIR/$(basename "$CERT" .crt | sed -e 's/ /_/g' \
65                                                   -e 's/[()]/=/g' \
66                                                   -e 's/,/_/g').pem"
67   if ! test -e "$PEM" || [ "$(readlink "$PEM")" != "$CERT" ]
68   then
69     ln -sf "$CERT" "$PEM"
70     echo +$PEM >> "$ADDED"
71   fi
72   cat "$CERT" >> "$TEMPBUNDLE"
73 }
74
75 remove() {
76   CERT="$1"
77   PEM="$ETCCERTSDIR/$(basename "$CERT" .crt).pem"
78   if test -L "$PEM"
79   then
80     rm -f "$PEM"
81     echo -$PEM >> "$REMOVED"
82   fi
83 }
84
85 cd $ETCCERTSDIR
86 if [ "$fresh" = 1 ]; then
87   echo -n "Clearing symlinks in $ETCCERTSDIR..."
88   find . -type l -print | while read symlink
89   do
90      case $(readlink $symlink) in
91      $CERTSDIR*) rm -f $symlink;;
92      esac
93   done
94   find . -type l -print | while read symlink
95   do
96      test -f $symlink || rm -f $symlink
97   done
98   echo "done."
99 fi
100
101 echo -n "Updating certificates in $ETCCERTSDIR... "
102
103 # Handle certificates that should be removed.  This is an explicit act
104 # by prefixing lines in the configuration files with exclamation marks (!).
105 sed -n -e '/^$/d' -e 's/^!//p' $CERTSCONF | while read crt
106 do
107   remove "$CERTSDIR/$crt"
108 done
109
110 sed -e '/^$/d' -e '/^#/d' -e '/^!/d' $CERTSCONF | while read crt
111 do
112   if ! test -f "$CERTSDIR/$crt"
113   then
114     echo "W: $CERTSDIR/$crt not found, but listed in $CERTSCONF." >&2
115     continue
116   fi
117   add "$CERTSDIR/$crt"
118 done
119
120 # Now process certificate authorities installed by the local system
121 # administrator.
122 if [ -d "$LOCALCERTSDIR" ]
123 then
124   find -L "$LOCALCERTSDIR" -type f -name '*.crt' | while read crt
125   do
126     add "$crt"
127   done
128 fi
129
130 chmod 0644 "$TEMPBUNDLE"
131 mv -f "$TEMPBUNDLE" "$CERTBUNDLE"
132
133 ADDED_CNT=$(wc -l < "$ADDED")
134 REMOVED_CNT=$(wc -l < "$REMOVED")
135
136 if [ "$ADDED_CNT" -gt 0 ] || [ "$REMOVED_CNT" -gt 0 ]
137 then
138   # only run if set of files has changed
139   if [ "$verbose" = 0 ]
140   then
141     c_rehash . > /dev/null
142   else
143     c_rehash .
144   fi
145 fi
146
147 echo "$ADDED_CNT added, $REMOVED_CNT removed; done."
148
149 HOOKSDIR=/etc/ca-certificates/update.d
150 echo -n "Running hooks in $HOOKSDIR...."
151 VERBOSE_ARG=
152 [ "$verbose" = 0 ] || VERBOSE_ARG=--verbose
153 eval run-parts $VERBOSE_ARG --test -- $HOOKSDIR | while read hook
154 do
155   ( cat $ADDED
156     cat $REMOVED ) | $hook || echo E: $hook exited with code $?.
157 done
158 echo "done."
159
160 # vim:set et sw=2:
161