]> git.pld-linux.org Git - packages/openssl.git/blame - openssl-c_rehash.sh
sslv2/sslv3/zlib bconds; discussion about their default state in progress on devel...
[packages/openssl.git] / openssl-c_rehash.sh
CommitLineData
59039eaf
JR
1#!/bin/sh
2#
3# Ben Secrest <blsecres@gmail.com>
4#
5# sh c_rehash script, scan all files in a directory
6# and add symbolic links to their hash values.
7#
8# based on the c_rehash perl script distributed with openssl
9#
10# LICENSE: See OpenSSL license
11# ^^acceptable?^^
12#
13
14# default certificate location
15DIR=/etc/openssl
16
17# for filetype bitfield
18IS_CERT=$(( 1 << 0 ))
19IS_CRL=$(( 1 << 1 ))
20
21
22# check to see if a file is a certificate file or a CRL file
23# arguments:
24# 1. the filename to be scanned
25# returns:
26# bitfield of file type; uses ${IS_CERT} and ${IS_CRL}
27#
28check_file()
29{
30 local IS_TYPE=0
31
32 # make IFS a newline so we can process grep output line by line
33 local OLDIFS=${IFS}
c4e41901 34 IFS=$( printf "\n" )
59039eaf
JR
35
36 # XXX: could be more efficient to have two 'grep -m' but is -m portable?
37 for LINE in $( grep '^-----BEGIN .*-----' ${1} )
38 do
39 if echo ${LINE} \
40 | grep -q -E '^-----BEGIN (X509 |TRUSTED )?CERTIFICATE-----'
41 then
42 IS_TYPE=$(( ${IS_TYPE} | ${IS_CERT} ))
43
44 if [ $(( ${IS_TYPE} & ${IS_CRL} )) -ne 0 ]
45 then
46 break
47 fi
48 elif echo ${LINE} | grep -q '^-----BEGIN X509 CRL-----'
49 then
50 IS_TYPE=$(( ${IS_TYPE} | ${IS_CRL} ))
51
52 if [ $(( ${IS_TYPE} & ${IS_CERT} )) -ne 0 ]
53 then
54 break
55 fi
56 fi
57 done
58
59 # restore IFS
60 IFS=${OLDIFS}
61
62 return ${IS_TYPE}
63}
64
65
66#
67# use openssl to fingerprint a file
68# arguments:
69# 1. the filename to fingerprint
70# 2. the method to use (x509, crl)
71# returns:
72# none
73# assumptions:
74# user will capture output from last stage of pipeline
75#
76fingerprint()
77{
78 ${SSL_CMD} ${2} -fingerprint -noout -in ${1} | sed 's/^.*=//' | tr -d ':'
79}
80
81
82#
83# link_hash - create links to certificate files
84# arguments:
85# 1. the filename to create a link for
86# 2. the type of certificate being linked (x509, crl)
87# returns:
88# 0 on success, 1 otherwise
89#
90link_hash()
91{
92 local FINGERPRINT=$( fingerprint ${1} ${2} )
93 local HASH=$( ${SSL_CMD} ${2} -hash -noout -in ${1} )
94 local SUFFIX=0
95 local LINKFILE=''
96 local TAG=''
97
98 if [ ${2} = "crl" ]
99 then
100 TAG='r'
101 fi
102
103 LINKFILE=${HASH}.${TAG}${SUFFIX}
104
105 while [ -f ${LINKFILE} ]
106 do
107 if [ ${FINGERPRINT} = $( fingerprint ${LINKFILE} ${2} ) ]
108 then
408d1966 109 echo "WARNING: Skipping duplicate file ${1}" >&2
59039eaf
JR
110 return 1
111 fi
112
113 SUFFIX=$(( ${SUFFIX} + 1 ))
114 LINKFILE=${HASH}.${TAG}${SUFFIX}
115 done
116
408d1966 117 echo "${1} => ${LINKFILE}"
59039eaf
JR
118
119 # assume any system with a POSIX shell will either support symlinks or
120 # do something to handle this gracefully
121 ln -s ${1} ${LINKFILE}
122
123 return 0
124}
125
126
127# hash_dir create hash links in a given directory
128hash_dir()
129{
408d1966 130 echo "Doing ${1}"
59039eaf
JR
131
132 cd ${1}
133
0dcbb866 134 ls -1 * 2>/dev/null | while read FILE
59039eaf 135 do
59039eaf
JR
136 if echo ${FILE} | grep -q -E '^[[:xdigit:]]{8}\.r?[[:digit:]]+$' \
137 && [ -h "${FILE}" ]
138 then
139 rm ${FILE}
140 fi
141 done
142
0dcbb866 143 ls -1 *.pem 2>/dev/null | while read FILE
59039eaf 144 do
59039eaf
JR
145 check_file ${FILE}
146 local FILE_TYPE=${?}
147 local TYPE_STR=''
148
149 if [ $(( ${FILE_TYPE} & ${IS_CERT} )) -ne 0 ]
150 then
151 TYPE_STR='x509'
152 elif [ $(( ${FILE_TYPE} & ${IS_CRL} )) -ne 0 ]
153 then
154 TYPE_STR='crl'
155 else
408d1966 156 echo "WARNING: ${FILE} does not contain a certificate or CRL: skipping" >&2
59039eaf
JR
157 continue
158 fi
159
160 link_hash ${FILE} ${TYPE_STR}
161 done
162}
163
164
165# choose the name of an ssl application
166if [ -n "${OPENSSL}" ]
167then
c4bb71d1 168 SSL_CMD=$(which ${OPENSSL} 2>/dev/null)
59039eaf 169else
c4bb71d1 170 SSL_CMD=/usr/bin/openssl
59039eaf 171 OPENSSL=${SSL_CMD}
cada8ea8 172 export OPENSSL
59039eaf
JR
173fi
174
175# fix paths
176PATH=${PATH}:${DIR}/bin
177export PATH
178
179# confirm existance/executability of ssl command
c4bb71d1 180if ! [ -x ${SSL_CMD} ]
59039eaf 181then
408d1966 182 echo "${0}: rehashing skipped ('openssl' program not available)" >&2
59039eaf
JR
183 exit 0
184fi
185
186# determine which directories to process
4d0769bb 187old_IFS=$IFS
59039eaf
JR
188if [ ${#} -gt 0 ]
189then
4d0769bb 190 IFS=':'
59039eaf
JR
191 DIRLIST=${*}
192elif [ -n "${SSL_CERT_DIR}" ]
193then
4d0769bb 194 DIRLIST=$SSL_CERT_DIR
59039eaf
JR
195else
196 DIRLIST=${DIR}/certs
197fi
198
4d0769bb
JR
199IFS=':'
200
59039eaf
JR
201# process directories
202for CERT_DIR in ${DIRLIST}
203do
204 if [ -d ${CERT_DIR} -a -w ${CERT_DIR} ]
205 then
4d0769bb 206 IFS=$old_IFS
59039eaf 207 hash_dir ${CERT_DIR}
4d0769bb 208 IFS=':'
59039eaf
JR
209 fi
210done
This page took 0.045058 seconds and 4 git commands to generate.