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