def _read_rules(self):
        """Read in rules that were added by ufw."""
        rfns = [self.files["rules"]]
        if self.use_ipv6():
            rfns.append(self.files["rules6"])

        for f in rfns:
            try:
                orig = ufw.util.open_file_read(f)
            except Exception:
                err_msg = _("Couldn't open '%s' for reading") % (f)
                raise UFWError(err_msg)

            pat_tuple = re.compile(r"^### tuple ###\s*")
            for line in orig:
                if pat_tuple.match(line):
                    tupl = pat_tuple.sub("", line)
                    tmp = re.split(r"\s+", tupl.strip())
                    if len(tmp) < 6 or len(tmp) > 9:
                        warn_msg = _("Skipping malformed tuple (bad length): %s") % (tupl)
                        warn(warn_msg)
                        continue
                    else:
                        # set direction to "in" to support upgrades
                        # from old format, which only had 6 or 8 fields
                        type = "in"
                        interface = ""
                        if len(tmp) == 7 or len(tmp) == 9:
                            if "_" in tmp[-1]:
                                (type, interface) = tmp[-1].split("_")
                            else:
                                type = tmp[-1]
                        try:
                            if len(tmp) < 8:
                                rule = UFWRule(tmp[0], tmp[1], tmp[2], tmp[3], tmp[4], tmp[5], type)
                            else:
                                rule = UFWRule(tmp[0], tmp[1], tmp[2], tmp[3], tmp[4], tmp[5], type)
                                # Removed leading [sd]app_ and unescape spaces
                                pat_space = re.compile("%20")
                                if tmp[6] != "-":
                                    rule.dapp = pat_space.sub(" ", tmp[6])
                                if tmp[7] != "-":
                                    rule.sapp = pat_space.sub(" ", tmp[7])
                            if interface != "":
                                rule.set_interface(type, interface)

                        except UFWError:
                            warn_msg = _("Skipping malformed tuple: %s") % (tupl)
                            warn(warn_msg)
                            continue
                        if f == self.files["rules6"]:
                            rule.set_v6(True)
                            self.rules6.append(rule)
                        else:
                            rule.set_v6(False)
                            self.rules.append(rule)

            orig.close()
Beispiel #2
0
    def _read_rules(self):
        '''Read in rules that were added by ufw'''
        rfns = [self.files['rules']]
        if self.use_ipv6():
            rfns.append(self.files['rules6'])

        for f in rfns:
            try:
                orig = ufw.util.open_file_read(f)
            except Exception:
                err_msg = _("Couldn't open '%s' for reading") % (f)
                raise UFWError(err_msg)

            pat_tuple = re.compile(r'^### tuple ###\s*')
            for line in orig:
                if pat_tuple.match(line):
                    tupl = pat_tuple.sub('', line)
                    tmp = re.split(r'\s+', tupl.strip())
                    if len(tmp) < 6 or len(tmp) > 9:
                        wmsg = _("Skipping malformed tuple (bad length): %s") \
                                 % (tupl)
                        warn(wmsg)
                        continue
                    else:
                        # set direction to "in" to support upgrades
                        # from old format, which only had 6 or 8 fields
                        dtype = "in"
                        interface = ""
                        if len(tmp) == 7 or len(tmp) == 9:
                            if '_' in tmp[-1]:
                                (dtype, interface) = tmp[-1].split('_')
                            else:
                                dtype = tmp[-1]
                        try:
                            if len(tmp) < 8:
                                rule = UFWRule(tmp[0], tmp[1], tmp[2], tmp[3],
                                               tmp[4], tmp[5], dtype)
                            else:
                                rule = UFWRule(tmp[0], tmp[1], tmp[2], tmp[3],
                                               tmp[4], tmp[5], dtype)
                                # Removed leading [sd]app_ and unescape spaces
                                pat_space = re.compile('%20')
                                if tmp[6] != "-":
                                    rule.dapp = pat_space.sub(' ', tmp[6])
                                if tmp[7] != "-":
                                    rule.sapp = pat_space.sub(' ', tmp[7])
                            if interface != "":
                                rule.set_interface(dtype, interface)

                        except UFWError:
                            warn_msg = _("Skipping malformed tuple: %s") % \
                                        (tupl)
                            warn(warn_msg)
                            continue
                        if f == self.files['rules6']:
                            rule.set_v6(True)
                            self.rules6.append(rule)
                        else:
                            rule.set_v6(False)
                            self.rules.append(rule)

            orig.close()