Ejemplo n.º 1
0
class WriteGenericLinks(Transaction, LoggerChild):
    def __init__(self, links, logger):
        LoggerChild.__init__(self, logger)
        self.filename = GENERIC_LINKS_XML
        self.links = links
        self.old = File(self.filename + ".old", False)

    def prepare(self):
        filename = self.filename + ".new"
        generic_links = BaseGenericLinks(filename)
        generic_links.setLinks(self.links)
        generic_links.write()
        self.new = File(filename, True)

    def save(self):
        umask(0077)
        self.old.copyFrom(self.filename)

    def apply(self):
        self.error("Write new generic links")
        self.new.renameTo(self.filename)

    def rollback(self):
        if self.old.exist:
            self.old.renameTo(self.filename)
        else:
            unlinkQuiet(self.filename)

    def cleanup(self):
        self.old.unlink(quiet=True)
        self.new.unlink(quiet=True)
Ejemplo n.º 2
0
class RemoveTemplate(Transaction, LoggerChild):
    def __init__(self, logger, filename):
        LoggerChild.__init__(self, logger)
        self.filename = filename
        self.copy = File(self.filename + ".copy", False)

    def save(self):
        umask(0077)
        self.copy.copyFrom(self.filename)

    def apply(self):
        self.critical("Remove multisite template")
        try:
            unlink(self.filename)
        except IOError, err:
            raise RulesetError(tr("Unable to delete the multisite template: %s!"), exceptionAsUnicode(err))
Ejemplo n.º 3
0
 def __init__(self, logger, component, netcfg, ruleset, template, use_template):
     LoggerChild.__init__(self, logger)
     self.component = component
     self.netcfg = netcfg
     self.ruleset = ruleset  # Ruleset name (str)
     self.template = template
     self.use_template = use_template
     self.filename = rulesetFilename("ruleset", self.ruleset)
     self.old = File(self.filename + ".old", False)
Ejemplo n.º 4
0
class WriteIptablesRules(Transaction, LoggerChild):
    def __init__(self, logger, config, default_decisions, acls, nats, custom_rules,
    options, apply_rules):
        LoggerChild.__init__(self, logger)
        self.generator = IptablesGenerator(logger, default_decisions, options, config, apply_rules)
        self.acls = acls
        self.nats = nats
        self.custom_rules = custom_rules
        self.options = options
        self.keep_files = True
        self.old_rules = None
        self.new_rules = None

    def prepare(self):
        self.info("Create the new iptables rules")
        umask(0077)
        filename = self.generator.writeRules(self.acls, self.nats, self.custom_rules)
        self.new_rules = File(filename, True)

    def save(self):
        self.info("Save the current iptables rules")
        loadKernelModules(self, self.options.ipv6)
        filename = iptablesSave(self, ipv6=self.options.ipv6)
        self.old_rules = File(filename, True)

    def apply(self):
        pass

    def rollback(self):
        self.error("Restore the old iptables rules")

        # Restore old rules
        iptablesRestore(self, self.old_rules.filename, ipv6=self.options.ipv6, check_error=False)

    def cleanup(self):
        if self.keep_files:
            return
        if self.old_rules:
            self.old_rules.unlink(quiet=True)
        if self.new_rules:
            self.new_rules.unlink(quiet=True)
Ejemplo n.º 5
0
class ReplaceTemplate(Transaction, LoggerChild):
    def __init__(self, logger, component, netcfg, ruleset, template, use_template):
        LoggerChild.__init__(self, logger)
        self.component = component
        self.netcfg = netcfg
        self.ruleset = ruleset  # Ruleset name (str)
        self.template = template
        self.use_template = use_template
        self.filename = rulesetFilename("ruleset", self.ruleset)
        self.old = File(self.filename + ".old", False)

    def save(self):
        umask(0077)
        self.old.copyFrom(self.filename)

    def apply(self):
        self.debug("Replace multisite templates in ruleset %s" % self.ruleset)
        logger = self.getLogger()
        ruleset = Ruleset(self.component, logger, self.netcfg)
        ruleset.load(logger, "ruleset", self.ruleset)
        if self.use_template:
            template = self.template
        else:
            template = None
        replaceTemplate(self.getLogger(), ruleset, template)
        ruleset.write(self.filename)

    def rollback(self):
        self.old.renameTo(self.filename)

    def cleanup(self):
        self.old.unlink(quiet=True)
Ejemplo n.º 6
0
class ProductionRuleset(Transaction):
    def __init__(self, new_ruleset):
        Transaction.__init__(self)
        self.filename = PRODUCTION_RULESET
        self.new_ruleset = new_ruleset
        self.old_production = File(self.filename + ".old", False)

    def save(self):
        self.old_production.copyFrom(self.filename)

    def apply(self):
        copyfile(self.new_ruleset, self.filename)

    def rollback(self):
        if self.old_production.exist:
            self.old_production.renameTo(self.filename)
        else:
            unlinkQuiet(self.filename)

    def cleanup(self):
        self.old_production.unlink(quiet=True)
Ejemplo n.º 7
0
 def __init__(self, logger, ufwi_ruleset_rules):
     LoggerChild.__init__(self, logger)
     self.ufwi_ruleset_rules = ufwi_ruleset_rules
     self.filename = RULES_FILENAME
     self.old_rules = File(self.filename + ".old", False)
     self.new_rules = File(self.filename + ".new", False)
Ejemplo n.º 8
0
class WriteRules(Transaction, LoggerChild):
    def __init__(self, logger, ufwi_ruleset_rules):
        LoggerChild.__init__(self, logger)
        self.ufwi_ruleset_rules = ufwi_ruleset_rules
        self.filename = RULES_FILENAME
        self.old_rules = File(self.filename + ".old", False)
        self.new_rules = File(self.filename + ".new", False)

    def prepare(self):
        self.info("Write new rules")
        umask(0077)
        with self.new_rules.open("wb") as fp:
            pickle.dump(self.ufwi_ruleset_rules, fp, pickle.HIGHEST_PROTOCOL)

    def save(self):
        self.info("Keep current rules")
        umask(0077)
        self.old_rules.copyFrom(self.filename)

    def apply(self):
        self.error("Write rules to disk")
        try:
            self.new_rules.renameTo(self.filename)
        except OSError:
            self.error("No new rules to rename.")

    def rollback(self):
        self.error("Restore old rules")
        try:
            self.old_rules.renameTo(self.filename)
        except OSError:
            self.error("No old rules to rename.")


    def cleanup(self):
        self.error("Remove temp files")
        try:
            self.old_rules.unlink(quiet=True)
        except OSError:
            self.error("No old rules to clean.")
        try:
            self.new_rules.unlink(quiet=True)
        except OSError:
            self.error("No new rules to clean.")
Ejemplo n.º 9
0
 def __init__(self, new_ruleset):
     Transaction.__init__(self)
     self.filename = PRODUCTION_RULESET
     self.new_ruleset = new_ruleset
     self.old_production = File(self.filename + ".old", False)
Ejemplo n.º 10
0
 def __init__(self, logger, filename, content):
     LoggerChild.__init__(self, logger)
     self.filename = filename
     self.new = File(self.filename + ".new", False)
     self.old = File(self.filename + ".old", False)
     self.content = content
Ejemplo n.º 11
0
class WriteTemplate(Transaction, LoggerChild):
    def __init__(self, logger, filename, content):
        LoggerChild.__init__(self, logger)
        self.filename = filename
        self.new = File(self.filename + ".new", False)
        self.old = File(self.filename + ".old", False)
        self.content = content

    def prepare(self):
        umask(0077)
        with self.new.open("wb") as f:
            f.write(self.content)

    def save(self):
        umask(0077)
        self.old.copyFrom(self.filename)

    def apply(self):
        self.critical("Write the new multisite template")
        self.new.renameTo(self.filename)

    def rollback(self):
        if self.old.exist:
            self.old.renameTo(self.filename)
        else:
            unlinkQuiet(self.filename)

    def cleanup(self):
        self.old.unlink(quiet=True)
        self.new.unlink(quiet=True)
Ejemplo n.º 12
0
 def __init__(self, logger, filename):
     LoggerChild.__init__(self, logger)
     self.filename = filename
     self.copy = File(self.filename + ".copy", False)
Ejemplo n.º 13
0
 def save(self):
     self.info("Save the current iptables rules")
     loadKernelModules(self, self.options.ipv6)
     filename = iptablesSave(self, ipv6=self.options.ipv6)
     self.old_rules = File(filename, True)
Ejemplo n.º 14
0
 def prepare(self):
     self.info("Create the new iptables rules")
     umask(0077)
     filename = self.generator.writeRules(self.acls, self.nats, self.custom_rules)
     self.new_rules = File(filename, True)
Ejemplo n.º 15
0
 def prepare(self):
     filename = self.filename + ".new"
     generic_links = BaseGenericLinks(filename)
     generic_links.setLinks(self.links)
     generic_links.write()
     self.new = File(filename, True)
Ejemplo n.º 16
0
 def __init__(self, links, logger):
     LoggerChild.__init__(self, logger)
     self.filename = GENERIC_LINKS_XML
     self.links = links
     self.old = File(self.filename + ".old", False)