-
Notifications
You must be signed in to change notification settings - Fork 58
/
functions
executable file
·169 lines (140 loc) · 3.41 KB
/
functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/bin/bash
# Derek Moore <[email protected]>
# Binaries copied during install
BINARIES="
create-root-ca
create-signing-ca
create-server
create-client
revoke-cert
functions
"
override() {
target=${1#USER_}
NAME=`echo $2 | sed 's/[^A-Za-z0-9-]/-/g'`
eval "${target}=\$${1}"
}
fullpath() {
cd $(dirname $1)
echo $PWD/$(basename $1)
}
##
## template TEMPLATE_FILE DESTINATION_FILE
##
## Performs variable interpolation on TEMPLATE_FILE and copies
## the result to DESTINATION_FILE. All strings in TEMPLATE_FILE
## that match {{CA_*}} will be replaced with the contents of the
## environment variable with the same name.
##
template() {
local REGEX=""
for VAR in ${!CA_*}; do
REGEX="${REGEX}s#{{\s*${VAR}\s*}}#${!VAR}#g; "
done
sed -e "${REGEX}" < $1 > $2
}
##
## init_ca_home CA_PATH
##
## Initializes a CA directory skeleton at PATH
##
init_ca_home() {
HOME=$1
# Ensure we're initializing an empty directory
if [ -d "${HOME}" ]; then
echo "${HOME} already exists, exiting"
exit 1
fi
if ! mkdir -p "${HOME}"; then
echo "Unable to create ${HOME}, exiting"
exit 1
fi
pushd ${HOME} > /dev/null
# Create top-level CA structure
mkdir -p ca
mkdir -p ca/db
mkdir -p ca/private
mkdir -p archive
mkdir -p bin
mkdir -p certs
mkdir -p crl
mkdir -p conf
mkdir -p csr
mkdir -p private
# Secure key directories
chmod 0700 ca/private
chmod 0700 private
# Create empty databases
touch ca/db/certificate.db
touch ca/db/certificate.db.attr
echo 01 > ca/db/crt.srl
echo 01 > ca/db/crl.srl
popd > /dev/null
}
##
## generate_conf CONF
##
## Generate the ca.conf for new root and signing CAs
##
generate_conf() {
DEST=$1
echo -n "Short label for new CA [${CA_NAME}]: "
read NAME
if [ -n "${NAME}" ]; then
CA_NAME=${NAME}
CA_NAME=`echo ${CA_NAME} | sed 's/[^A-Za-z0-9-]/-/g'`
fi
echo -n "Domain name for new CA [${CA_DOMAIN}]: "
read DOMAIN
if [ -n "${DOMAIN}" ]; then
CA_DOMAIN=${DOMAIN}
elif [ -z "${CA_DOMAIN}" ]; then
echo "Domain is required."
exit 1
fi
echo
echo "CRL URL will be http://${CA_DOMAIN}/ca/${CA_NAME}.crl"
echo
echo -n "Default country code for new certificates [${CA_CERT_C}]: "
read CERT_C
if [ -n "${CERT_C}" ]; then
CA_CERT_C=${CERT_C}
fi
echo -n "Default state for new certificates [${CA_CERT_ST}]: "
read CERT_ST
if [ -n "${CERT_ST}" ]; then
CA_CERT_ST=${CERT_ST}
fi
echo -n "Default city for new certificates [${CA_CERT_L}]: "
read CERT_L
if [ -n "${CERT_L}" ]; then
CA_CERT_L=${CERT_L}
fi
echo -n "Default org for new certificates [${CA_CERT_O}]: "
read CERT_O
if [ -n "${CERT_O}" ]; then
CA_CERT_O=${CERT_O}
fi
echo -n "Default org unit for new certificates [${CA_CERT_OU}]: "
read CERT_OU
if [ -n "${CERT_OU}" ]; then
CA_CERT_OU=${CERT_OU}
fi
echo
CA_CERT_CN="${CA_CERT_O} Certificate Authority"
echo -n "Common Name for CA certificate [${CA_CERT_CN}]: "
read CERT_CN
if [ -n "${CERT_CN}" ]; then
CA_CERT_CN=${CERT_CN}
fi
cat > ${DEST} << EOF
CA_DOMAIN="${CA_DOMAIN}"
CA_NAME="${CA_NAME}"
CA_CERT_C="${CA_CERT_C}"
CA_CERT_ST="${CA_CERT_ST}"
CA_CERT_L="${CA_CERT_L}"
CA_CERT_O="${CA_CERT_O}"
CA_CERT_OU="${CA_CERT_OU}"
CA_CERT_CN="${CA_CERT_CN}"
EOF
}