Debian Codegraph @

sbin/update-ca-certificates

raw
1 #!/bin/sh -e
2 #
3 # update-ca-certificates
4 #
5 # Copyright (c) 2003 Fumitoshi UKAI <[email protected]>
6 # Copyright (c) 2009 Philipp Kern <[email protected]>
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 default=0
27 CERTSCONF=/etc/ca-certificates.conf
28 CERTSDIR=/usr/share/ca-certificates
29 LOCALCERTSDIR=/usr/local/share/ca-certificates
30 CERTBUNDLE=ca-certificates.crt
31 ETCCERTSDIR=/etc/ssl/certs
32 HOOKSDIR=/etc/ca-certificates/update.d
33
34 while [ $# -gt 0 ];
35 do
36 case $1 in
37 --verbose|-v)
38 verbose=1;;
39 --fresh|-f)
40 fresh=1;;
41 --default|-d)
42 default=1
43 fresh=1;;
44 --certsconf)
45 shift
46 CERTSCONF="$1";;
47 --certsdir)
48 shift
49 CERTSDIR="$1";;
50 --localcertsdir)
51 shift
52 LOCALCERTSDIR="$1";;
53 --certbundle)
54 shift
55 CERTBUNDLE="$1";;
56 --etccertsdir)
57 shift
58 ETCCERTSDIR="$1";;
59 --hooksdir)
60 shift
61 HOOKSDIR="$1";;
62 --sysroot)
63 shift
64 SYSROOT="$1"
65 CERTSCONF="$1/${CERTSCONF}"
66 CERTSDIR="$1/${CERTSDIR}"
67 LOCALCERTSDIR="$1/${LOCALCERTSDIR}"
68 ETCCERTSDIR="$1/${ETCCERTSDIR}"
69 HOOKSDIR="$1/${HOOKSDIR}";;
70 --help|-h|*)
71 echo "$0: [--verbose] [--fresh]"
72 exit;;
73 esac
74 shift
75 done
76
77 if [ ! -s "$CERTSCONF" ]
78 then
79 fresh=1
80 fi
81
82 cleanup() {
83 rm -f "$TEMPBUNDLE"
84 rm -f "$ADDED"
85 rm -f "$REMOVED"
86 }
87 trap cleanup 0
88
89 # Helper files. (Some of them are not simple arrays because we spawn
90 # subshells later on.)
91 TEMPBUNDLE="${ETCCERTSDIR}/${CERTBUNDLE}.new"
92 ADDED="$(mktemp -p "${TMPDIR:-/tmp}" "ca-certificates.tmp.XXXXXX")"
93 REMOVED="$(mktemp -p "${TMPDIR:-/tmp}" "ca-certificates.tmp.XXXXXX")"
94
95 # Adds a certificate to the list of trusted ones. This includes a symlink
96 # in /etc/ssl/certs to the certificate file and its inclusion into the
97 # bundle.
98 add() {
99 CERT="$1"
100 PEM="$ETCCERTSDIR/$(basename "$CERT" .crt | sed -e 's/ /_/g' \
101 -e 's/[()]/=/g' \
102 -e 's/,/_/g').pem"
103 if ! test -e "$PEM" || [ "$(readlink "$PEM")" != "$CERT" ]
104 then
105 ln -sf "$CERT" "$PEM"
106 echo "+$PEM" >> "$ADDED"
107 fi
108 # Add trailing newline to certificate, if it is missing (#635570)
109 sed -e '$a\' "$CERT" >> "$TEMPBUNDLE"
110 }
111
112 remove() {
113 CERT="$1"
114 PEM="$ETCCERTSDIR/$(basename "$CERT" .crt).pem"
115 if test -L "$PEM"
116 then
117 rm -f "$PEM"
118 echo "-$PEM" >> "$REMOVED"
119 fi
120 }
121
122 cd "$ETCCERTSDIR"
123 if [ "$fresh" = 1 ]; then
124 echo "Clearing symlinks in $ETCCERTSDIR..."
125 find . -type l -print | while read -r symlink
126 do
127 case $(readlink "$symlink") in
128 $CERTSDIR*|$LOCALCERTSDIR*) rm -f "$symlink";;
129 esac
130 done
131 find . -type l -print | while read -r symlink
132 do
133 test -f "$symlink" || rm -f "$symlink"
134 done
135 echo "done."
136 fi
137
138 echo "Updating certificates in $ETCCERTSDIR..."
139
140 # Add default certificate authorities if requested
141 if [ "$default" = 1 ]; then
142 find -L "$CERTSDIR" -type f -name '*.crt' | sort | while read -r crt
143 do
144 add "$crt"
145 done
146 fi
147
148 # Handle certificates that should be removed. This is an explicit act
149 # by prefixing lines in the configuration files with exclamation marks (!).
150 sed -n -e '/^$/d' -e 's/^!//p' "$CERTSCONF" | while read -r crt
151 do
152 remove "$CERTSDIR/$crt"
153 done
154
155 sed -e '/^$/d' -e '/^#/d' -e '/^!/d' "$CERTSCONF" | while read -r crt
156 do
157 if ! test -f "$CERTSDIR/$crt"
158 then
159 echo "W: $CERTSDIR/$crt not found, but listed in $CERTSCONF." >&2
160 continue
161 fi
162 add "$CERTSDIR/$crt"
163 done
164
165 # Now process certificate authorities installed by the local system
166 # administrator.
167 if [ -d "$LOCALCERTSDIR" ]
168 then
169 find -L "$LOCALCERTSDIR" -type f -name '*.crt' | sort | while read -r crt
170 do
171 add "$crt"
172 done
173 fi
174
175 ADDED_CNT=$(wc -l < "$ADDED")
176 REMOVED_CNT=$(wc -l < "$REMOVED")
177
178 if [ "$ADDED_CNT" -gt 0 ] || [ "$REMOVED_CNT" -gt 0 ]
179 then
180 # only run if set of files has changed
181 # Remove orphan symlinks found in ETCCERTSDIR to prevent `openssl rehash`
182 # from exiting with an error. See #895482, #895473.
183 find "$ETCCERTSDIR" -type l ! -exec test -e {} \; -print | while read -r orphan
184 do
185 rm -f "$orphan"
186 if [ "$verbose" = 1 ]; then
187 echo "Removed orphan symlink $orphan"
188 fi
189 done
190 if [ "$verbose" = 0 ]
191 then
192 openssl rehash . > /dev/null
193 else
194 openssl rehash -v .
195 fi
196 fi
197
198 # chmod and mv only if TEMPBUNDLE exists or install may fail, #996005
199 if [ -f "$TEMPBUNDLE" ]
200 then
201 chmod 0644 "$TEMPBUNDLE"
202 mv -f "$TEMPBUNDLE" "$CERTBUNDLE"
203 # Restore proper SELinux label after moving the file
204 [ -x /sbin/restorecon ] && /sbin/restorecon "$CERTBUNDLE" >/dev/null 2>&1
205 fi
206
207 echo "$ADDED_CNT added, $REMOVED_CNT removed; done."
208
209 if [ -d "$HOOKSDIR" ]
210 then
211
212 echo "Running hooks in $HOOKSDIR..."
213 VERBOSE_ARG=
214 [ "$verbose" = 0 ] || VERBOSE_ARG="--verbose"
215 eval run-parts "$VERBOSE_ARG" --test -- "$HOOKSDIR" | while read -r hook
216 do
217 ( cat "$ADDED"
218 cat "$REMOVED" ) | "$hook" || echo "E: $hook exited with code $?."
219 done
220 echo "done."
221
222 fi
223
224 # vim:set et sw=2: