Changeset 126

Show
Ignore:
Timestamp:
06/16/05 03:26:22 (3 years ago)
Author:
athomas
Message:
  • Closes #10, #2
  • <eol> is now always GROUP 10010
  • Miscellaneous changes.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • fwc/trunk/Config.py

    r125 r126  
    3535        def __getattr__(self, name): 
    3636                return self.__store[name].value 
    37  
    3837        __getitem__ = __getattr__ 
    3938 
     
    4544                if not re.match(v.constraint, value): raise Config.InvalidValue('Invalid value "%s" for "%s"' % (value, name)) 
    4645                v.value = value 
    47  
    4846        __setitem__ = __setattr__ 
    4947 
     
    5149                return iter(self.__store) 
    5250 
    53         def __del__(self, key): 
     51        def __delattr__(self, key): 
    5452                del(self.__store[key]) 
    55  
    56         __delitem__ = __del__ 
     53        __delitem__ = __delattr__ 
    5754 
    5855        def iteritems(self): 
  • fwc/trunk/Engine.py

    r125 r126  
    203203                                                JUMP : 'commands', 
    204204                                                JUMP_TO : 'source', 
    205                                                 GROUP : 20
     205                                                GROUP : 5
    206206                                                'port' : { 
    207207                                                        GROUP : 20, 
     
    223223                                                GROUP : 20, 
    224224                                                PORT : { 
     225                                                        GROUP : 5, 
    225226                                                        HELP : help_port, 
    226227                                                        LABEL : 'sports', 
     
    247248                                        UNLESS_VAR : [ 'dport', 'destination' ], 
    248249                                        NETWORK : { 
    249                                                 GROUP : 20
     250                                                GROUP : 5
    250251                                                LABEL : 'destination', 
    251252                                                VAR : 'destination', 
     
    255256                                                        GROUP : 20, 
    256257                                                        PORT : { 
     258                                                                GROUP : 5, 
    257259                                                                LABEL : 'dport', 
    258260                                                                VAR : 'dport', 
     
    269271                                                GROUP : 20, 
    270272                                                PORT : { 
     273                                                        GROUP : 5, 
    271274                                                        LABEL : 'dports', 
    272275                                                        VAR : 'dport', 
     
    380383                                        's|'.join(Resolver.get_object_types()) + 's|all' : { 
    381384                                                ORDER : 10, 
     385                                                GROUP : 10, 
    382386                                                VAR : 'type', 
    383387                                                HELP : lambda ctx: [(x + 's', 'List %s objects' % x) for x in Resolver.get_object_types()] + [ ('all', 'All object types.') ], 
     
    390394                                        }, 
    391395                                        '.+' : { 
     396                                                GROUP : 20, 
    392397                                                ORDER : 20, 
    393398                                                VAR : 'filter', 
     
    450455                                        }, 
    451456                                }, 
    452                                 'unset' : { 
    453                                         GROUP : 10, 
    454                                         HELP : 'Unset a firewall setting.', 
    455                                         IF : have_firewall, 
    456                                         lambda ctx, setting: setting in self.firewall.config and not self.firewall.config.readonly(setting): { 
    457                                                 VAR : 'setting', 
    458                                                 HELP : lambda ctx: [ (v, 'Unset ' + self.firewall.config.help(v)) for v in self.firewall.config if not self.firewall.config.readonly(v) ], 
    459                                                 ACTION : self.__firewall_unset, 
    460                                         } 
    461                                 }, 
     457# We probably don't want to let firewall settings be unset 
     458#                               'unset' : { 
     459#                                       GROUP : 10, 
     460#                                       HELP : 'Unset a firewall setting.', 
     461#                                       IF : have_firewall, 
     462#                                       lambda ctx, setting: setting in self.firewall.config and not self.firewall.config.readonly(setting): { 
     463#                                               VAR : 'setting', 
     464#                                               HELP : lambda ctx: [ (v, 'Unset ' + self.firewall.config.help(v)) for v in self.firewall.config if not self.firewall.config.readonly(v) ], 
     465#                                               ACTION : self.__firewall_unset, 
     466#                                       } 
     467#                               }, 
    462468                                'acquire' : { 
    463469                                        HELP : 'Acquire a firewall for management.', 
     
    523529 
    524530        def __list_firewalls(self, ctx): 
    525                 for fw in sorted(self.__firewalls.values(), lambda a, b: cmp(a.name, b.name)): 
     531                for fw in sorted(self.__firewalls.values(), lambda a, b: cmp(a.config.name, b.config.name)): 
    526532                        if self.firewall == fw: 
    527533                                out = '^B*^B ' 
     
    530536                        out += '^B%s^B ^B%s^B ^B%s^B' % (fw.config.name, fw.config.type, fw.config.ip) 
    531537                        if fw.config.description: 
    532                                 out += ' description ^B"%s"^B' % fw.config.description 
     538                                out += " description ^B'%s'^B" % fw.config.description 
    533539                        cprint(out) 
    534540 
     
    603609                                        source = source, sport = sport, in_if = in_if, 
    604610                                        destination = destination, dport = dport, out_if = out_if, 
     611                                        log = log, 
    605612                                        **argd), 
    606613                                where, index) 
     
    669676 
    670677def check_port_protocol(ctx): 
    671         return 'protocol' in ctx and context['protocol'] in [ 'tcp', 'udp' ] 
     678        return 'protocol' in ctx and ctx['protocol'] in [ 'tcp', 'udp' ] 
    672679 
    673680def have_firewall(ctx): 
     
    715722        help = {} 
    716723        for name, fw in Engine()._Engine__firewalls.iteritems(): 
    717                 if fw.description: 
    718                         help[name] = fw.description 
     724                if fw.config.description: 
     725                        help[name] = fw.config.description 
    719726                else: 
    720                         help[name] = "Firewall %s" % fw.name 
     727                        help[name] = "Firewall %s" % fw.config.name 
    721728                if Engine().firewall == fw: 
    722729                        help[name] += ' ^B(current)^B' 
     
    748755def PORT_RANGE(ctx, str): 
    749756        range = str.split('-') 
    750         return PORT(ctx, range[0]) and (len(range) < 2 or len(range) > 1 and PORT(context, range[1])) 
     757        return PORT(ctx, range[0]) and (len(range) < 2 or len(range) > 1 and PORT(ctx, range[1])) 
    751758 
    752759def RULE(ctx, str): 
  • fwc/trunk/Firewall.py

    r125 r126  
    33from Object import Object 
    44from util import * 
    5 import re 
     5import textwrap 
    66 
    77class Firewall: 
     
    104104 
    105105        def describe_rule(self, rule, pretty = True): 
    106                 description = "^B%s^B" % rule.action 
     106                out = [ "^B%s^B" % rule.action ] 
    107107                if rule.protocol: 
    108108                        try: 
    109                                 description += ' protocol ^B%s^B' % int(rule.protocol
     109                                out.append('protocol ^B%s^B' % int(rule.protocol)
    110110                        except: 
    111                                 description += " ^B" + rule.protocol + "^B" 
     111                                out.append("^B%s^B" % rule.protocol) 
    112112                if rule.source or rule.sport: 
    113                         description += " from" 
     113                        text = "from" 
    114114                        if rule.source: 
    115                                 description += " ^B" + ' '.join(rule.source) + "^B" 
     115                                text += " ^B" + ' '.join(rule.source) + "^B" 
    116116                        if rule.sport: 
    117                                 description += " port ^B" + ' '.join(rule.sport) + "^B" 
     117                                text += " port ^B" + ' '.join(rule.sport) + "^B" 
     118                        out.append(text) 
    118119                if rule.in_if: 
    119                         description += " in ^B%s^B" % ' '.join(rule.in_if
     120                        out.append("in ^B%s^B" % ' '.join(rule.in_if)
    120121                if rule.destination or rule.dport: 
    121                         description += " to" 
     122                        text = "to" 
    122123                        if rule.destination: 
    123                                 description += " ^B" + ' '.join(rule.destination) + "^B" 
     124                                text += " ^B" + ' '.join(rule.destination) + "^B" 
    124125                        if rule.dport: 
    125                                 description += " port ^B" + ' '.join(rule.dport) + "^B" 
     126                                text += " port ^B" + ' '.join(rule.dport) + "^B" 
     127                        out.append(text) 
    126128                if rule.out_if: 
    127                         description += " out ^B%s^B" % ' '.join(rule.out_if
     129                        out.append("out ^B%s^B" % ' '.join(rule.out_if)
    128130                if rule.reject_type: 
    129                         description += " with ^B%s^B" % rule.reject_type 
     131                        text = "with ^B%s^B" % rule.reject_type 
    130132                        if rule.reject_subtype: 
    131                                 description += " ^B%s^B" % rule.reject_subtype 
     133                                text += " ^B%s^B" % rule.reject_subtype 
     134                        out.append(text) 
    132135                if rule.log: 
    133                         description += " log" 
     136                        text = "log" 
    134137                        if type(rule.log) is str: 
    135                                 description += " message ^B'%s'^B" % rule.log 
     138                                text += " message ^B^6'%s'^N" % rule.log 
     139                        out.append(text) 
    136140                if rule.description: 
    137                         description += " description ^B'%s'^B" % rule.description 
     141                        out.append("description ^B^6'%s'^N" % rule.description) 
    138142                if not pretty: 
    139                         description = re.sub(r'\^.', '', description) 
    140                 return description 
     143                        out = [cprintstrip(x) for x in out] 
     144                return out 
    141145 
    142146        def list_rules(self): 
     147                if 'COLS' in os.environ: 
     148                        cols = os.environ['COLS'] 
     149                else: 
     150                        cols = 80 
    143151                for ruleno, rule in enumerate(self.__rules): 
    144                         cprint("^B%3i:^B %s" % (ruleno, self.describe_rule(rule))) 
     152                        prefix = '^B%3i:^B' % ruleno 
     153                        text = '' 
     154                        length = 0 
     155                        for e in self.describe_rule(rule): 
     156                                length += clen(str(e)) + 1 
     157                                if text and length + 4 >= cols: 
     158                                        cprint(prefix + text + ' ^B^4\\^N') 
     159                                        prefix = ' ' * 6 
     160                                        text = '' 
     161                                        length = 6 + clen(str(e)) + 1 
     162                                text += ' ' + str(e) 
     163                        if text: 
     164                                cprint(prefix + text) 
    145165 
    146166        def get_rules(self): 
  • fwc/trunk/LinuxIPTables.py

    r125 r126  
    6363                self.resolver.add_object(Object(Object.INTERFACE, self.config.name, allifs, 'All interfaces on %s' % self.config.name)) 
    6464                self.resolver.add_object(Object(Object.NETWORK, self.config.name, allifs, 'All IP addresses on %s' % self.config.name)) 
    65                 info("Added interface and network objects: %s" % ', '.join(allifs + [self.config.name])) 
     65                info("Added interface and network objects: ^B%s^B" % ', '.join(allifs + [self.config.name])) 
    6666 
    6767        def describe_rule(self, rule, pretty = True): 
    68                 description = Firewall.describe_rule(self, rule, pretty) 
     68                out = Firewall.describe_rule(self, rule, pretty) 
    6969                if rule.state: 
    70                         description += ' state ^B' + ' '.join(rule.state) + '^B' 
    71                 return description 
     70                        out.append('state ^B' + ' '.join(rule.state) + '^B') 
     71                return out 
  • fwc/trunk/util.py

    r125 r126  
    673673        (If you want different formats in different columns, don't use 
    674674        print_table.) sep is the separator between columns.""" 
    675         if table: 
    676                 justs = [if_(isnumber(x), 'rjust', 'ljust') for x in table[0]] 
    677         elif header: 
    678                 justs = ['ljust' for x in header] 
    679         else: 
    680                 justs = [] 
     675        justs = [if_(isnumber(x), 'rjust', 'ljust') for x in table[0]] 
    681676        if header: 
    682677                table = [header] + table 
     
    803798        cwrite(sys.stdout, *(args + ('\n',))) 
    804799 
     800def cprintstrip(*args): 
     801        return __cprint_strip.sub('', ' '.join(map(str, args))) 
     802 
     803def clen(arg): 
     804        return len(cprintstrip(arg)) 
    805805 
    806806def error(*args): 
     
    842842                process = Popen4(command) 
    843843                rc = process.wait() 
     844                if rc != os.EX_OK: return (rc, None) 
    844845                return (rc, process.fromchild.readlines()) 
    845846