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