Example #1
0
 def configure_ipsec(self, obj):
     leftpeer = obj['local_public_ip']
     rightpeer = obj['peer_gateway_ip']
     peerlist = obj['peer_guest_cidr_list'].lstrip().rstrip().replace(
         ',', ' ')
     vpnconffile = "%s/ipsec.vpn-%s.conf" % (self.VPNCONFDIR, rightpeer)
     vpnsecretsfile = "%s/ipsec.vpn-%s.secrets" % (self.VPNCONFDIR,
                                                   rightpeer)
     if rightpeer in self.confips:
         self.confips.remove(rightpeer)
     file = CsFile(vpnconffile)
     file.search("conn ", "conn vpn-%s" % rightpeer)
     file.addeq(" left=%s" % leftpeer)
     file.addeq(" leftsubnet=%s" % obj['local_guest_cidr'])
     file.addeq(" leftnexthop=%s" % obj['local_public_gateway'])
     file.addeq(" right=%s" % rightpeer)
     file.addeq(" rightsubnets={%s}" % peerlist)
     file.addeq(" type=tunnel")
     file.addeq(" authby=secret")
     file.addeq(" keyexchange=ike")
     file.addeq(" ike=%s" % obj['ike_policy'])
     file.addeq(" ikelifetime=%s" %
                self.convert_sec_to_h(obj['ike_lifetime']))
     file.addeq(" esp=%s" % obj['esp_policy'])
     file.addeq(" salifetime=%s" %
                self.convert_sec_to_h(obj['esp_lifetime']))
     file.addeq(" pfs=%s" % CsHelper.bool_to_yn(obj['dpd']))
     file.addeq(" keyingtries=2")
     file.addeq(" auto=start")
     if 'encap' not in obj:
         obj['encap'] = False
     file.addeq(" forceencaps=%s" % CsHelper.bool_to_yn(obj['encap']))
     if obj['dpd']:
         file.addeq("  dpddelay=30")
         file.addeq("  dpdtimeout=120")
         file.addeq("  dpdaction=restart")
     secret = CsFile(vpnsecretsfile)
     secret.search(
         "%s " % leftpeer,
         "%s %s: PSK \"%s\"" % (leftpeer, rightpeer, obj['ipsec_psk']))
     if secret.is_changed() or file.is_changed():
         secret.commit()
         file.commit()
         logging.info("Configured vpn %s %s", leftpeer, rightpeer)
         CsHelper.execute("ipsec auto --rereadall")
         CsHelper.execute("ipsec auto --add vpn-%s" % rightpeer)
         if not obj['passive']:
             CsHelper.execute("ipsec auto --up vpn-%s" % rightpeer)
     os.chmod(vpnsecretsfile, 0o400)
Example #2
0
 def configure_ipsec(self, obj):
     leftpeer = obj['local_public_ip']
     rightpeer = obj['peer_gateway_ip']
     peerlist = obj['peer_guest_cidr_list'].lstrip().rstrip().replace(',', ' ')
     vpnconffile = "%s/ipsec.vpn-%s.conf" % (self.VPNCONFDIR, rightpeer)
     vpnsecretsfile = "%s/ipsec.vpn-%s.secrets" % (self.VPNCONFDIR, rightpeer)
     if rightpeer in self.confips:
         self.confips.remove(rightpeer)
     file = CsFile(vpnconffile)
     file.search("conn ", "conn vpn-%s" % rightpeer)
     file.addeq(" left=%s" % leftpeer)
     file.addeq(" leftsubnet=%s" % obj['local_guest_cidr'])
     file.addeq(" leftnexthop=%s" % obj['local_public_gateway'])
     file.addeq(" right=%s" % rightpeer)
     file.addeq(" rightsubnets={%s}" % peerlist)
     file.addeq(" type=tunnel")
     file.addeq(" authby=secret")
     file.addeq(" keyexchange=ike")
     file.addeq(" ike=%s" % obj['ike_policy'])
     file.addeq(" ikelifetime=%s" % self.convert_sec_to_h(obj['ike_lifetime']))
     file.addeq(" esp=%s" % obj['esp_policy'])
     file.addeq(" salifetime=%s" % self.convert_sec_to_h(obj['esp_lifetime']))
     file.addeq(" pfs=%s" % CsHelper.bool_to_yn(obj['dpd']))
     file.addeq(" keyingtries=2")
     file.addeq(" auto=start")
     if 'encap' not in obj:
         obj['encap']=False
     file.addeq(" forceencaps=%s" % CsHelper.bool_to_yn(obj['encap']))
     if obj['dpd']:
         file.addeq("  dpddelay=30")
         file.addeq("  dpdtimeout=120")
         file.addeq("  dpdaction=restart")
     secret = CsFile(vpnsecretsfile)
     secret.search("%s " % leftpeer, "%s %s: PSK \"%s\"" % (leftpeer, rightpeer, obj['ipsec_psk']))
     if secret.is_changed() or file.is_changed():
         secret.commit()
         file.commit()
         logging.info("Configured vpn %s %s", leftpeer, rightpeer)
         CsHelper.execute("ipsec auto --rereadall")
         CsHelper.execute("ipsec auto --add vpn-%s" % rightpeer)
         if not obj['passive']:
             CsHelper.execute("ipsec auto --up vpn-%s" % rightpeer)
     os.chmod(vpnsecretsfile, 0o400)
Example #3
0
    def configure_ipsec(self, obj):
        leftpeer = obj['local_public_ip']
        rightpeer = obj['peer_gateway_ip']
        peerlist = obj['peer_guest_cidr_list'].replace(' ', '')
        vpnconffile = "%s/ipsec.vpn-%s.conf" % (self.VPNCONFDIR, rightpeer)
        vpnsecretsfile = "%s/ipsec.vpn-%s.secrets" % (self.VPNCONFDIR,
                                                      rightpeer)
        ikepolicy = obj['ike_policy'].replace(';', '-')
        esppolicy = obj['esp_policy'].replace(';', '-')

        pfs = 'no'
        if 'modp' in esppolicy:
            pfs = 'yes'

        if rightpeer in self.confips:
            self.confips.remove(rightpeer)
        file = CsFile(vpnconffile)
        file.add("#conn for vpn-%s" % rightpeer, 0)
        file.search("conn ", "conn vpn-%s" % rightpeer)
        file.addeq(" left=%s" % leftpeer)
        file.addeq(" leftsubnet=%s" % obj['local_guest_cidr'])
        file.addeq(" leftnexthop=%s" % obj['local_public_gateway'])
        file.addeq(" right=%s" % rightpeer)
        file.addeq(" rightsubnet=%s" % peerlist)
        file.addeq(" type=tunnel")
        file.addeq(" authby=secret")
        file.addeq(" keyexchange=ike")
        file.addeq(" ike=%s" % ikepolicy)
        file.addeq(" ikelifetime=%s" %
                   self.convert_sec_to_h(obj['ike_lifetime']))
        file.addeq(" esp=%s" % esppolicy)
        file.addeq(" lifetime=%s" % self.convert_sec_to_h(obj['esp_lifetime']))
        file.addeq(" pfs=%s" % pfs)
        file.addeq(" keyingtries=2")
        file.addeq(" auto=start")
        if 'encap' not in obj:
            obj['encap'] = False
        file.addeq(" forceencaps=%s" % CsHelper.bool_to_yn(obj['encap']))
        if obj['dpd']:
            file.addeq(" dpddelay=30")
            file.addeq(" dpdtimeout=120")
            file.addeq(" dpdaction=restart")
        secret = CsFile(vpnsecretsfile)
        secret.search(
            "%s " % leftpeer,
            "%s %s : PSK \"%s\"" % (leftpeer, rightpeer, obj['ipsec_psk']))
        if secret.is_changed() or file.is_changed():
            secret.commit()
            file.commit()
            logging.info("Configured vpn %s %s", leftpeer, rightpeer)
            CsHelper.execute("ipsec rereadsecrets")

        # This will load the new config and start the connection when needed since auto=start in the config
        CsHelper.execute("ipsec reload")
        os.chmod(vpnsecretsfile, 0400)
Example #4
0
    def configure_ipsec(self, obj):
        leftpeer = obj['local_public_ip']
        rightpeer = obj['peer_gateway_ip']
        peerlist = obj['peer_guest_cidr_list'].replace(' ', '')
        vpnconffile = "%s/ipsec.vpn-%s.conf" % (self.VPNCONFDIR, rightpeer)
        vpnsecretsfile = "%s/ipsec.vpn-%s.secrets" % (self.VPNCONFDIR, rightpeer)
        ikepolicy=obj['ike_policy'].replace(';','-')
        esppolicy=obj['esp_policy'].replace(';','-')

        pfs='no'
        if 'modp' in esppolicy:
            pfs='yes'

        if rightpeer in self.confips:
            self.confips.remove(rightpeer)
        file = CsFile(vpnconffile)
        file.add("#conn for vpn-%s" % rightpeer, 0)
        file.search("conn ", "conn vpn-%s" % rightpeer)
        file.addeq(" left=%s" % leftpeer)
        file.addeq(" leftsubnet=%s" % obj['local_guest_cidr'])
        file.addeq(" leftnexthop=%s" % obj['local_public_gateway'])
        file.addeq(" right=%s" % rightpeer)
        file.addeq(" rightsubnet=%s" % peerlist)
        file.addeq(" type=tunnel")
        file.addeq(" authby=secret")
        file.addeq(" keyexchange=ike")
        file.addeq(" ike=%s" % ikepolicy)
        file.addeq(" ikelifetime=%s" % self.convert_sec_to_h(obj['ike_lifetime']))
        file.addeq(" esp=%s" % esppolicy)
        file.addeq(" lifetime=%s" % self.convert_sec_to_h(obj['esp_lifetime']))
        file.addeq(" pfs=%s" % pfs)
        file.addeq(" keyingtries=2")
        file.addeq(" auto=start")
        if 'encap' not in obj:
            obj['encap']=False
        file.addeq(" forceencaps=%s" % CsHelper.bool_to_yn(obj['encap']))
        if obj['dpd']:
            file.addeq(" dpddelay=30")
            file.addeq(" dpdtimeout=120")
            file.addeq(" dpdaction=restart")
        secret = CsFile(vpnsecretsfile)
        secret.search("%s " % leftpeer, "%s %s : PSK \"%s\"" % (leftpeer, rightpeer, obj['ipsec_psk']))
        if secret.is_changed() or file.is_changed():
            secret.commit()
            file.commit()
            logging.info("Configured vpn %s %s", leftpeer, rightpeer)
            CsHelper.execute("ipsec rereadsecrets")

        # This will load the new config and start the connection when needed since auto=start in the config
        CsHelper.execute("ipsec reload")
        os.chmod(vpnsecretsfile, 0400)