#!/bin/sh

version=2.4
osrelease=$(lsb_release -cs)

kill -TERM $(ps ax | awk '/\/usr\/sbin\/openvpn/ {print $1}') >/dev/null  2>&1
sleep 2

mv /etc/openvpn/passwd /etc/openvpn/sig.passwd

for f in sig-clientes.conf F454C2A6821CDD.pem cacert.pem clientes.pem update-resolv-conf openvpn.tar.gz openvpn.deb; do
	find /etc/openvpn -name $f -delete
done

apt-mark unhold openvpn
wget -O - https://swupdate.openvpn.net/repos/repo-public.gpg|apt-key add -
echo "deb http://build.openvpn.net/debian/openvpn/$version $osrelease main" > /etc/apt/sources.list.d/openvpn-$osrelease.list
apt-get update && apt-get -y install openvpn

case $osrelease in
    bionic|xenial)
	ovpnclientconfdir=/etc/openvpn
        srvenablecmd="systemctl enable openvpn@sig.service"
	srvrestartcmd="systemctl restart openvpn@sig.service"
    ;;
    trusty)
        ovpnclientconfdir=/etc/openvpn
        srvenablecmd="update-rc.d openvpn enable"
        srvrestartcmd="service openvpn restart"
    ;;
esac

cat <<EOF> $ovpnclientconfdir/sig.conf
client
dev tun
proto udp
port 1195
topology subnet
pull-filter ignore route-gateway
redirect-gateway !ipv4
float
remote vpn.sigsolucoes.net.br
remote vpn.siginformatica.com.br
remote 179.184.162.87
resolv-retry infinite
nobind
persist-key
persist-tun
keepalive 10 120
verify-x509-name vpn-clientes.sigsolucoes.net.br name
#remote-cert-tls server
#key-direction 1
auth SHA1
auth-nocache
auth-retry nointeract
auth-user-pass sig.passwd
#cipher AES-256-CBC
script-security 2
comp-lzo yes
verb 3

<ca>
-----BEGIN CERTIFICATE-----
[payload removed: 1632 characters in 26 lines]
-----END CERTIFICATE-----
</ca>
<cert>
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number: 9652433096355749085 (0x85f454c2a6821cdd)
    Signature Algorithm: sha1WithRSAEncryption
        Issuer: C=BR, ST=MG, L=Sete Lagoas, O=SIG Solucoes em Informatica Ltda, CN=SIG Solucoes em Informatica Ltda Authority Certificate
        Validity
            Not Before: Apr 17 15:50:41 2015 GMT
            Not After : Apr 14 12:50:33 2025 GMT
        Subject: C=BR, ST=MG, L=Sete Lagoas, O=SIG Solucoes em Informatica Ltda, CN=clientes
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
                Public-Key: (2048 bit)
                Modulus:
                    00:c5:2e:2b:94:94:d5:70:73:58:b1:7a:83:fd:ff:
                    59:4c:f1:06:96:b4:10:0c:fa:b0:b3:8e:9d:2d:0c:
                    c9:04:0e:72:95:f5:44:b4:31:5d:5c:5b:84:7e:ff:
                    6f:a4:47:31:6b:45:e8:f1:49:ff:5e:20:a3:1b:0d:
                    9f:5f:2d:9e:da:66:21:9c:46:06:5b:a9:24:82:92:
                    3c:68:57:04:9a:f0:89:90:a1:6d:b6:e8:15:3a:26:
                    c3:35:12:31:6a:09:9d:01:f6:67:cb:72:9d:46:98:
                    79:b9:34:6a:e5:c2:76:40:94:d1:dd:9c:a7:b8:55:
                    89:dd:be:bc:d8:dd:62:df:2f:97:fe:3a:bc:0b:01:
                    eb:9d:8a:4e:b5:d6:81:57:b9:2d:19:22:72:d4:bc:
                    95:eb:be:e2:d5:dc:7a:b6:be:31:e8:24:bf:10:30:
                    ea:e4:52:37:e3:7f:d5:66:33:96:ce:48:57:6f:64:
                    4d:58:22:a3:86:d4:cf:57:6c:e1:60:e4:28:8b:15:
                    2d:0b:4a:d2:13:e5:8f:18:a5:7b:38:ad:2e:5f:37:
                    65:7f:b5:f1:07:f9:8e:eb:99:02:cc:d6:bf:2f:37:
                    6e:d6:8a:20:60:97:57:93:65:f0:69:ad:bb:ae:f7:
                    9b:44:e5:c8:09:93:26:69:91:54:ef:d0:b0:70:e4:
                    ba:3d
                Exponent: 65537 (0x10001)
        X509v3 extensions:
            X509v3 Authority Key Identifier: 
                keyid:70:5B:E4:60:96:2B:DD:07:94:CA:79:AF:21:40:1F:93:2F:26:A6:A3

            X509v3 Subject Key Identifier: 
                A2:8C:27:A9:DA:AF:17:B0:98:C7:2B:40:BA:96:5B:AA:C1:97:70:A8
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Basic Constraints: 
                CA:FALSE
    Signature Algorithm: sha1WithRSAEncryption
         57:45:1a:6b:b5:b9:f8:dd:c5:ab:36:4f:71:07:5e:ba:5d:2e:
         68:98:22:e8:54:ba:b0:ce:7f:9a:84:9f:f1:43:26:6a:20:04:
         7a:8e:d5:88:44:e1:1e:21:1d:56:2a:e2:48:25:3d:9f:3e:e6:
         6c:59:08:f8:c5:57:a3:27:1f:82:44:05:0d:79:94:d3:5d:02:
         aa:da:21:52:12:0f:2b:53:ac:8a:ba:51:64:f0:64:b0:74:5f:
         21:7c:39:83:e6:9d:b1:0c:b4:d8:0a:ac:05:95:0e:3e:5f:19:
         f8:e1:a8:f6:f8:2f:f6:d3:15:ab:5b:e3:19:17:d4:58:f4:1b:
         69:9d:6c:c3:57:ba:47:a7:b6:19:0d:e6:75:a5:dc:da:e0:4e:
         ae:d5:ca:5e:ae:c1:58:fb:71:e4:24:dd:6c:f6:a3:b2:4c:0b:
         51:9a:53:53:11:dc:83:93:a0:41:f6:18:f9:15:35:d9:73:ab:
         a8:3e:d0:fd:a2:13:7f:f0:d1:2b:89:1a:af:67:b9:4e:59:5c:
         c8:80:ee:b6:c2:ea:a5:07:df:59:a5:00:8a:fd:3c:4c:9e:ac:
         e4:4d:e5:75:8e:ad:20:86:1d:05:a1:f7:c7:33:ca:73:66:38:
         a0:fc:5e:75:f9:b1:8d:65:fe:88:4f:ce:22:68:f9:51:a3:7a:
         23:4d:72:35
-----BEGIN CERTIFICATE-----
[payload removed: 1384 characters in 22 lines]
-----END CERTIFICATE-----
</cert>
<key>
-----BEGIN PRIVATE KEY-----
[payload removed: 1624 characters in 26 lines]
-----END PRIVATE KEY-----
</key>
#<tls-auth>
#
# 2048 bit OpenVPN static key
#
#-----BEGIN OpenVPN Static key V1-----
#3e16f8e09f03d369ed2736fd21a7cbe6
#8873da5b0bb8c58078b0df17adc8bfb3
#c9222c3bcee6b01677b88f33bd10b5fb
#794b15b2883d86d01e9f7cf081d0e24f
#6ac47c0464c967c24f2e514b8e5a73be
#33bf527ce37cfc7fd30eba6a47e8b7d9
#4e0851882e4888fda4ec711bedad537e
#e4aca005f1c59c368f163b3de317a7f8
#c8af29f37e7c093db142d327f4e5f1d2
#3e1d04d9e57e394b7df250f34b2fdf86
#265a5c04c7643c5670dfd8fb01caf114
#83590fa4a56f9e9b1cdf4264ff5201be
#7bc687c41c863ccb683a1608249b1a69
#c98a150342caa284e3d7be17bd83d39a
#2af9cc76c83626415422c7773c1ace87
#d91c99beff5a8ba53f4ec56507a827ea
#-----END OpenVPN Static key V1-----
#</tls-auth>

EOF
chown root:root $ovpnclientconfdir/sig.conf
chmod 660 $ovpnclientconfdir/sig.conf

chown root:root $ovpnclientconfdir/sig.passwd
chmod 600 $ovpnclientconfdir/sig.passwd

sudo $srvenablecmd
sudo $srvrestartcmd

exit $?
