1
1
#! /bin/bash
2
2
3
+ usage () {
4
+ echo " $( basename $0 ) [--skip-login-keychain] [--skip-system-keychain] [-h|--help] [\` which brew\` ]"
5
+ }
6
+
3
7
skip_system_keychain=false
4
8
skip_login_keychain=false
5
9
@@ -11,9 +15,17 @@ while [ ! $# -eq 0 ]; do
11
15
--skip-system-keychain)
12
16
skip_system_keychain=true
13
17
;;
18
+ -h|--help)
19
+ usage
20
+ exit 1
21
+ ;;
14
22
* brew)
15
23
brew=$1
16
24
;;
25
+ * )
26
+ echo " Unknown argument: $1 " >&2
27
+ exit 1
28
+ ;;
17
29
esac
18
30
shift
19
31
done
24
36
25
37
if [[ ! -x " ${brew} " ]]; then
26
38
echo " Homebrew not in PATH or given arguments, cannot continue"
39
+ usage
27
40
exit 1
28
41
fi
29
42
30
- openssl=$( $brew list openssl | grep bin/openssl | head -n 1)
43
+ exitcode=0
44
+ err () {
45
+ if [[ -d $1 ]]; then
46
+ rm -r " ${1} "
47
+ fi
31
48
32
- [[ " ${openssl} " = " " ]] && echo " Homebrew openssl not found" && exit 1
49
+ exitcode=$(( $exitcode + 1 ))
50
+ }
33
51
34
- c_rehash=$( $brew list openssl | grep bin/c_rehash | head -n 1)
52
+ genbundle () {
53
+ local sslimpl=$1
35
54
36
- [[ " ${c_rehash} " = " " ]] && echo " Homebrew c_rehash (openssl) not found" && exit 1
55
+ local list=$( $brew list $sslimpl 2> /dev/null)
56
+ [[ -z $list ]] && continue
37
57
38
- openssldir =$( $openssl version -d | cut -d ' " ' -f 2 )
58
+ local openssl =$( echo " $list " | grep bin/openssl | head -n 1 )
39
59
40
- [[ " ${openssldir } " = " " ]] && echo " openssl directory not found" && exit 1
60
+ [[ " ${openssl } " = " " ]] && echo " Homebrew $sslimpl not found" && err && return 1
41
61
42
- tmpdir =$( /usr/bin/mktemp -d -t openssl_osx_ca )
62
+ local openssldir =$( $openssl version -d | cut -d ' " ' -f 2 )
43
63
44
- [[ " ${tmpdir } " = " " ]] && echo " mktemp failed " && exit 1
64
+ [[ " ${openssldir } " = " " ]] && echo " $sslimpl directory not found " && err && return 1
45
65
46
- certs=" ${tmpdir} /cert.pem"
47
- if ! $skip_system_keychain ; then
48
- security find-certificate -a -p /Library/Keychains/System.keychain > $certs
49
- fi
50
- security find-certificate -a -p /System/Library/Keychains/SystemRootCertificates.keychain >> $certs
51
- if [[ -f ~ /Library/Keychains/login.keychain ]] && ! $skip_login_keychain ; then
52
- security find-certificate -a -p ~ /Library/Keychains/login.keychain >> $certs
53
- fi
66
+ local tmpdir=$( /usr/bin/mktemp -d -t openssl_osx_ca)
54
67
55
- d1=$( $openssl md5 ${openssldir} /cert.pem | awk ' {print $2}' )
56
- d2=$( $openssl md5 ${tmpdir} /cert.pem | awk ' {print $2}' )
68
+ [[ " ${tmpdir} " = " " ]] && echo " mktemp failed" && err " ${tmpdir} " && return 1
57
69
58
- if [[ " ${d1} " = " ${d2} " ]]; then
59
- logger -t " $( basename $0 ) " " ${openssldir} /cert.pem up to date"
60
- else
61
- if ! $c_rehash -- $tmpdir > /dev/null; then
62
- logger -t " $( basename $0 ) " " ${openssldir} /cert.pem updated failed, see cron"
70
+ local certs=" ${tmpdir} /cert.pem"
71
+ if ! $skip_system_keychain ; then
72
+ security find-certificate -a -p /Library/Keychains/System.keychain > $certs
73
+ fi
74
+ security find-certificate -a -p /System/Library/Keychains/SystemRootCertificates.keychain >> $certs
75
+ if [[ -f ~ /Library/Keychains/login.keychain ]] && ! $skip_login_keychain ; then
76
+ security find-certificate -a -p ~ /Library/Keychains/login.keychain >> $certs
77
+ fi
63
78
64
- echo " rehash failed to verify, something is wrong"
65
- echo " check ${tmpdir} /cert.pem for problems"
66
- exit 1
79
+ d1=$( $openssl md5 ${openssldir} /cert.pem | awk ' {print $2}' )
80
+ d2=$( $openssl md5 ${tmpdir} /cert.pem | awk ' {print $2}' )
81
+
82
+ if [[ " ${d1} " = " ${d2} " ]]; then
83
+ logger -t " $( basename $0 ) " " ${openssldir} /cert.pem up to date"
84
+ else
85
+
86
+ # Note: there's no c_rehash bundled with libressl, the bundle is still
87
+ # produced, so carry on as normal and just produce a bundle in the standard
88
+ # location.
89
+ local c_rehash=$( echo " $list " | grep bin/c_rehash | head -n 1)
90
+ if [[ -n $c_rehash ]]; then
91
+ if ! $c_rehash -- $tmpdir > /dev/null; then
92
+ logger -t " $( basename $0 ) " " ${openssldir} /cert.pem updated failed, see cron"
93
+
94
+ echo " rehash failed to verify, something is wrong"
95
+ echo " check ${tmpdir} /cert.pem for problems"
96
+ err " ${tmpdir} " && return 1
97
+ fi
98
+ fi
99
+
100
+ # XXX: I don't think this is atomic on OSX, but it's as close as we're going to
101
+ # get without a lot more work.
102
+ mv -f ${tmpdir} /* ${openssldir} /
103
+
104
+ logger -t " $( basename $0 ) " " ${openssldir} /cert.pem updated"
67
105
fi
68
106
69
- # XXX: I don't think this is atomic on OSX, but it's as close as we're going to
70
- # get without a lot more work.
71
- mv -f ${tmpdir} /* ${openssldir} /
107
+ rm -r " ${tmpdir} "
108
+ }
72
109
73
- logger -t " $( basename $0 ) " " ${openssldir} /cert.pem updated"
74
- fi
110
+ for sslimpl in openssl libressl; do
111
+ genbundle $sslimpl
112
+ done
75
113
76
- rm -r " ${tmpdir} "
114
+ exit $exitcode
0 commit comments