1
0
mirror of https://github.com/vimagick/dockerfiles.git synced 2024-12-27 02:09:06 +02:00
dockerfiles/strongswan/init.sh

267 lines
7.3 KiB
Bash
Raw Normal View History

2016-06-30 17:46:01 +02:00
#!/bin/sh -e
2016-06-27 21:16:42 +02:00
#
# gen config files for strongswan
#
# - VPN_DNS
2016-06-27 22:39:35 +02:00
# - VPN_DOMAIN
2016-06-30 17:46:01 +02:00
# - VPN_NETWORK
2016-06-30 18:09:28 +02:00
# - LAN_NETWORK
2016-06-27 21:16:42 +02:00
# - VPN_P12_PASSWORD
#
if [ -e /etc/ipsec.d/ipsec.conf ]
then
2016-06-27 22:30:22 +02:00
echo "Initialized!"
2016-06-27 21:16:42 +02:00
exit 0
2016-06-27 22:30:22 +02:00
else
2016-06-30 17:46:01 +02:00
echo "Initializing..."
2016-06-27 21:16:42 +02:00
fi
cat > /etc/ipsec.d/ipsec.conf <<_EOF_
config setup
uniqueids=never
2016-06-27 22:30:22 +02:00
charondebug="cfg 2, dmn 2, ike 2, net 2"
2016-06-27 21:16:42 +02:00
conn %default
keyexchange=ike
dpdaction=clear
dpddelay=300s
rekey=no
left=%any
leftca=ca.cert.pem
leftcert=server.cert.pem
2016-06-27 22:39:35 +02:00
leftsubnet=0.0.0.0/0
right=%any
rightdns=${VPN_DNS}
2016-06-30 17:46:01 +02:00
rightsourceip=${VPN_NETWORK}
2016-06-30 18:09:28 +02:00
rightsubnets=${LAN_NETWORK}
2016-06-27 21:16:42 +02:00
conn IPSec-IKEv2
keyexchange=ikev2
ike=aes256-sha256-modp1024,3des-sha1-modp1024,aes256-sha1-modp1024!
esp=aes256-sha256,3des-sha1,aes256-sha1!
leftid="${VPN_DOMAIN}"
leftsendcert=always
leftauth=pubkey
rightauth=pubkey
2016-06-27 22:30:22 +02:00
rightid="client@${VPN_DOMAIN}"
2016-06-27 21:16:42 +02:00
rightcert=client.cert.pem
auto=add
_EOF_
cat > /etc/ipsec.d/ipsec.secrets <<_EOF_
: RSA server.pem
_EOF_
# gen ca key and cert
ipsec pki --gen --outform pem > /etc/ipsec.d/private/ca.pem
ipsec pki --self \
--in /etc/ipsec.d/private/ca.pem \
2016-06-27 22:30:22 +02:00
--dn "C=CN, O=strongSwan, CN=strongSwan Root CA" \
2016-06-27 21:16:42 +02:00
--ca \
--lifetime 3650 \
--outform pem > /etc/ipsec.d/cacerts/ca.cert.pem
# gen server key and cert
ipsec pki --gen --outform pem > /etc/ipsec.d/private/server.pem
ipsec pki --pub --in /etc/ipsec.d/private/server.pem |
ipsec pki --issue --lifetime 1200 --cacert /etc/ipsec.d/cacerts/ca.cert.pem \
2016-06-27 22:30:22 +02:00
--cakey /etc/ipsec.d/private/ca.pem --dn "C=CN, O=strongSwan, CN=${VPN_DOMAIN}" \
2016-06-27 21:16:42 +02:00
--san="${VPN_DOMAIN}" --flag serverAuth --flag ikeIntermediate \
--outform pem > /etc/ipsec.d/certs/server.cert.pem
# gen client key and cert
ipsec pki --gen --outform pem > /etc/ipsec.d/private/client.pem
ipsec pki --pub --in /etc/ipsec.d/private/client.pem |
ipsec pki --issue \
--cacert /etc/ipsec.d/cacerts/ca.cert.pem \
2016-06-27 22:30:22 +02:00
--cakey /etc/ipsec.d/private/ca.pem --dn "C=CN, O=strongSwan, CN=client@${VPN_DOMAIN}" \
--san="client@${VPN_DOMAIN}" \
2016-06-27 21:16:42 +02:00
--outform pem > /etc/ipsec.d/certs/client.cert.pem
openssl pkcs12 -export \
-inkey /etc/ipsec.d/private/client.pem \
-in /etc/ipsec.d/certs/client.cert.pem \
2016-06-27 22:30:22 +02:00
-name "client@${VPN_DOMAIN}" \
2016-06-27 21:16:42 +02:00
-certfile /etc/ipsec.d/cacerts/ca.cert.pem \
2016-06-27 22:30:22 +02:00
-caname "strongSwan Root CA" \
2016-06-27 21:16:42 +02:00
-out /etc/ipsec.d/client.cert.p12 \
-passout pass:${VPN_P12_PASSWORD}
# gen mobileconfig for mac
UUID1=$(uuidgen)
UUID2=$(uuidgen)
UUID3=$(uuidgen)
UUID4=$(uuidgen)
UUID5=$(uuidgen)
UUID6=$(uuidgen)
cat > /etc/ipsec.d/client.mobileconfig <<_EOF_
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>PayloadContent</key>
<array>
<dict>
<key>Password</key>
<string>${VPN_P12_PASSWORD}</string>
<key>PayloadCertificateFileName</key>
<string>client.cert.p12</string>
<key>PayloadContent</key>
<data>
$(base64 /etc/ipsec.d/client.cert.p12)
</data>
<key>PayloadDescription</key>
<string>添加 PKCS#12 格式的证书</string>
<key>PayloadDisplayName</key>
<string>client.cert.p12</string>
<key>PayloadIdentifier</key>
<string>com.apple.security.pkcs12.${UUID1}</string>
<key>PayloadType</key>
<string>com.apple.security.pkcs12</string>
<key>PayloadUUID</key>
<string>${UUID1}</string>
<key>PayloadVersion</key>
<integer>1</integer>
</dict>
<dict>
<key>PayloadCertificateFileName</key>
<string>ca.cer</string>
<key>PayloadContent</key>
<data>
$(base64 /etc/ipsec.d/cacerts/ca.cert.pem)
</data>
<key>PayloadDescription</key>
<string>添加 CA 根证书</string>
<key>PayloadDisplayName</key>
2016-06-27 22:30:22 +02:00
<string>strongSwan Root CA</string>
2016-06-27 21:16:42 +02:00
<key>PayloadIdentifier</key>
<string>com.apple.security.root.${UUID2}</string>
<key>PayloadType</key>
<string>com.apple.security.root</string>
<key>PayloadUUID</key>
<string>${UUID2}</string>
<key>PayloadVersion</key>
<integer>1</integer>
</dict>
<dict>
<key>IKEv2</key>
<dict>
<key>AuthenticationMethod</key>
<string>Certificate</string>
<key>ChildSecurityAssociationParameters</key>
<dict>
<key>DiffieHellmanGroup</key>
<integer>2</integer>
<key>EncryptionAlgorithm</key>
<string>3DES</string>
<key>IntegrityAlgorithm</key>
<string>SHA1-96</string>
<key>LifeTimeInMinutes</key>
<integer>1440</integer>
</dict>
<key>DeadPeerDetectionRate</key>
<string>Medium</string>
<key>DisableMOBIKE</key>
<integer>0</integer>
<key>DisableRedirect</key>
<integer>0</integer>
<key>EnableCertificateRevocationCheck</key>
<integer>0</integer>
<key>EnablePFS</key>
<integer>0</integer>
<key>IKESecurityAssociationParameters</key>
<dict>
<key>DiffieHellmanGroup</key>
<integer>2</integer>
<key>EncryptionAlgorithm</key>
<string>3DES</string>
<key>IntegrityAlgorithm</key>
<string>SHA1-96</string>
<key>LifeTimeInMinutes</key>
<integer>1440</integer>
</dict>
<key>LocalIdentifier</key>
2016-06-27 22:30:22 +02:00
<string>client@${VPN_DOMAIN}</string>
2016-06-27 21:16:42 +02:00
<key>PayloadCertificateUUID</key>
<string>${UUID1}</string>
<key>RemoteAddress</key>
<string>${VPN_DOMAIN}</string>
<key>RemoteIdentifier</key>
<string>${VPN_DOMAIN}</string>
<key>UseConfigurationAttributeInternalIPSubnet</key>
<integer>0</integer>
</dict>
<key>IPv4</key>
<dict>
<key>OverridePrimary</key>
<integer>1</integer>
</dict>
<key>PayloadDescription</key>
<string>Configures VPN settings</string>
<key>PayloadDisplayName</key>
<string>VPN</string>
<key>PayloadIdentifier</key>
<string>com.apple.vpn.managed.${UUID4}</string>
<key>PayloadType</key>
<string>com.apple.vpn.managed</string>
<key>PayloadUUID</key>
<string>${UUID4}</string>
<key>PayloadVersion</key>
<real>1</real>
<key>Proxies</key>
<dict>
<key>HTTPEnable</key>
<integer>0</integer>
<key>HTTPSEnable</key>
<integer>0</integer>
</dict>
<key>UserDefinedName</key>
<string>VPN (IKEv2)</string>
<key>VPNType</key>
<string>IKEv2</string>
</dict>
<dict>
<key>PayloadCertificateFileName</key>
<string>server.cer</string>
<key>PayloadContent</key>
<data>
$(base64 /etc/ipsec.d/certs/server.cert.pem)
</data>
<key>PayloadDescription</key>
<string>添加 PKCS#1 格式的证书</string>
<key>PayloadDisplayName</key>
<string>${VPN_DOMAIN}</string>
<key>PayloadIdentifier</key>
<string>com.apple.security.pkcs1.${UUID5}</string>
<key>PayloadType</key>
<string>com.apple.security.pkcs1</string>
<key>PayloadUUID</key>
<string>${UUID5}</string>
<key>PayloadVersion</key>
<integer>1</integer>
</dict>
</array>
<key>PayloadDisplayName</key>
<string>VPN</string>
<key>PayloadIdentifier</key>
<string>com.github.vimagick.strongswan</string>
<key>PayloadRemovalDisallowed</key>
<false/>
<key>PayloadType</key>
<string>Configuration</string>
<key>PayloadUUID</key>
<string>${UUID6}</string>
<key>PayloadVersion</key>
<integer>1</integer>
</dict>
</plist>
_EOF_