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
|
#! /bin/bash
# Usage: $iron vpn-client <servername> <clientname>
# Summary: create OpenVPN client config
# Help: OpenVPN client config generator
APPNAME="iron"
if [ "$#" != "2" ]; then
echo "Usage: $APPNAME vpn-client <servername> <clientname>"
exit 1
fi
servername=$1
vpn_client=$2
CFG_FILE=$HOME/.${APPNAME}/vpn/${servername}/${servername}.cfg
CFGDIR=$(dirname ${CFG_FILE})
CURRDIR=$(pwd)
EASY_RSA_HOME=/usr/share/easy-rsa/
test -f $CFG_FILE || echo "No such config: [${CFG_FILE}]"
test -f $CFG_FILE || exit 1
source ${CFG_FILE}
function ovpn_generate_client_nopass()
{
cd $CFGDIR
client_dn=$1
export KEY_CN=${client_dn}
${EASY_RSA_HOME}/easyrsa build-client-full ${client_dn} nopass
}
function ovpn_generate_client_assembly()
{
cd $CFGDIR
mkdir -p "$CFGDIR/server/${code_organization}-client/${vpn_client}"
cp $CFGDIR/pki/ca.crt $CFGDIR/server/${code_organization}-client/${vpn_client}
cp $CFGDIR/pki/issued/${vpn_client}.crt $CFGDIR/server/${code_organization}-client/${vpn_client}
cp $CFGDIR/pki/private/${vpn_client}.key $CFGDIR/server/${code_organization}-client/${vpn_client}
cat<<__EOT__>$CFGDIR/server/${code_organization}-client/${vpn_client}/${code_organization}.conf
client
dev tun
proto tcp
remote ${code_organization} 1194
resolv-retry infinite
nobind
comp-lzo
persist-key
persist-tun
askpass /etc/openvpn/passwordfile
ca /etc/openvpn/${vpn_client}/ca.crt
cert /etc/openvpn/${vpn_client}/${vpn_client}.crt
key /etc/openvpn/${vpn_client}/${vpn_client}.key
verb 3
log-append /var/log/openvpn.${code_organization}-client.log
__EOT__
cd ./server/${code_organization}-client/${vpn_client}
tar cvfz $CURRDIR/${code_organization}-${vpn_client}.tar.gz .
}
function ovpn_generate_client_android()
{
ovpnfile=$CFGDIR/server/${code_organization}-client/${vpn_client}/${code_organization}-${vpn_client}.ovpn
cd $CFGDIR
mkdir -p "$CFGDIR/server/${code_organization}-client/${vpn_client}"
cp $CFGDIR/pki/ca.crt $CFGDIR/server/${code_organization}-client/${vpn_client}
cp $CFGDIR/pki/issued/${vpn_client}.crt $CFGDIR/server/${code_organization}-client/${vpn_client}
cp $CFGDIR/pki/private/${vpn_client}.key $CFGDIR/server/${code_organization}-client/${vpn_client}
cat<<__EOT__>$ovpnfile
client
dev tun
proto tcp
remote ${code_organization} 1194
resolv-retry infinite
nobind
comp-lzo
persist-key
persist-tun
askpass /etc/openvpn/passwordfile
verb 3
log-append /var/log/openvpn.${code_organization}-client.log
#ca /etc/openvpn/${vpn_client}/ca.crt
#cert /etc/openvpn/${vpn_client}/${vpn_client}.crt
#key /etc/openvpn/${vpn_client}/${vpn_client}.key
__EOT__
echo "<ca>" >> $ovpnfile
cat $CFGDIR/server/${code_organization}-client/${vpn_client}/ca.crt >> $ovpnfile
echo "</ca>" >> $ovpnfile
echo "<cert>" >> $ovpnfile
cat $CFGDIR/server/${code_organization}-client/${vpn_client}/${vpn_client}.crt \
| sed -n '/-----BEGIN CERTIFICATE-----/,/-----END CERTIFICATE-----/p' \
>> $ovpnfile
echo "</cert>" >> $ovpnfile
echo "<key>" >> $ovpnfile
cat $CFGDIR/server/${code_organization}-client/${vpn_client}/${vpn_client}.key \
>> $ovpnfile
echo "</key>" >> $ovpnfile
}
# MAIN
client_password=$(uuidgen)
client_credentials="$CFGDIR/client-${vpn_client}-credentials.txt"
test -d ${CFGDIR} || mkdir -p ${CFGDIR}
echo "credentials for ${code_organization}:${vpn_client}:[${client_password}]">>$client_credentials
ovpn_generate_client_nopass ${vpn_client}
openssl rsa -aes256 -in $CFGDIR/pki/private/${vpn_client}.key\
-out $CFGDIR/pki/private/encrypted-${vpn_client}.key \
-passout pass:${client_password}\
&& \
mv $CFGDIR/pki/private/encrypted-${vpn_client}.key \
$CFGDIR/pki/private/${vpn_client}.key
ovpn_generate_client_android
ovpn_generate_client_assembly
|