]> git.donarmstrong.com Git - ca-certificates.git/blob - sbin/update-ca-certificates
Import Debian version 20090624
[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).pem"
64   if ! test -e "$PEM" || [ "$(readlink "$PEM")" != "$CERT" ]
65   then
66     ln -sf "$CERT" "$PEM"
67     echo +$PEM >> "$ADDED"
68   fi
69   cat "$CERT" >> "$TEMPBUNDLE"
70 }
71
72 remove() {
73   CERT="$1"
74   PEM="$ETCCERTSDIR/$(basename "$CERT" .crt).pem"
75   if test -L "$PEM"
76   then
77     rm -f "$PEM"
78     echo -$PEM >> "$REMOVED"
79   fi
80 }
81
82 cd $ETCCERTSDIR
83 if [ "$fresh" = 1 ]; then
84   echo -n "Clearing symlinks in $ETCCERTSDIR..."
85   find . -type l -print | while read symlink
86   do
87      case $(readlink $symlink) in
88      $CERTSDIR*) rm -f $symlink;;
89      esac
90   done
91   find . -type l -print | while read symlink
92   do
93      test -f $symlink || rm -f $symlink
94   done
95   echo "done."
96 fi
97
98 echo -n "Updating certificates in $ETCCERTSDIR... "
99
100 # Handle certificates that should be removed.  This is an explicit act
101 # by prefixing lines in the configuration files with exclamation marks (!).
102 sed -n -e '/^$/d' -e 's/^!//p' $CERTSCONF | while read crt
103 do
104   remove "$CERTSDIR/$crt"
105 done
106
107 sed -e '/^$/d' -e '/^#/d' -e '/^!/d' $CERTSCONF | while read crt
108 do
109   if ! test -f "$CERTSDIR/$crt"
110   then
111     echo "W: $CERTSDIR/$crt not found, but listed in $CERTSCONF." >&2
112     continue
113   fi
114   add "$CERTSDIR/$crt"
115 done
116
117 # Now process certificate authorities installed by the local system
118 # administrator.
119 if [ -d "$LOCALCERTSDIR" ]
120 then
121   find -L "$LOCALCERTSDIR" -type f | while read crt
122   do
123     add "$crt"
124   done
125 fi
126
127 chmod 0644 "$TEMPBUNDLE"
128 mv -f "$TEMPBUNDLE" "$CERTBUNDLE"
129
130 ADDED_CNT=$(wc -l < "$ADDED")
131 REMOVED_CNT=$(wc -l < "$REMOVED")
132
133 if [ "$ADDED_CNT" -gt 0 ] || [ "$REMOVED_CNT" -gt 0 ]
134 then
135   # only run if set of files has changed
136   if [ "$verbose" = 0 ]
137   then
138     c_rehash . > /dev/null 2>&1
139   else
140     c_rehash .
141   fi
142 fi
143
144 echo "$ADDED_CNT added, $REMOVED_CNT removed; done."
145
146 HOOKSDIR=/etc/ca-certificates/update.d
147 echo -n "Running hooks in $HOOKSDIR...."
148 VERBOSE_ARG=
149 [ "$verbose" = 0 ] || VERBOSE_ARG=--verbose
150 eval run-parts $VERB_ARG --test -- $HOOKSDIR | while read hook
151 do
152   ( cat $ADDED
153     cat $REMOVED ) | $hook || echo E: $hook exited with code $?.
154 done
155 echo "done."
156
157 # vim:set et sw=2:
158