Changeset 118

Show
Ignore:
Timestamp:
05/16/05 08:26:48 (4 years ago)
Author:
athomas
Message:

More fixes, made object stuff more generic.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • fwc/trunk/fwc

    r117 r118  
    3434# Help extractors 
    3535  
     36def help_object(context, type): 
     37        help = { '<%s>' % type : '%s object.' % type.title() } 
     38        for o in resolver.get_objects(type): 
     39                if o.description: 
     40                        help[o.name] = '%s (%s)' % (o.description, o.value) 
     41                else: 
     42                        help[o.name] = '%s object (%s)' % (type.title(), o.value) 
     43        return help 
     44 
    3645def help_network(context): 
    3746        help = { '<network>' : 'Network address.'} 
     
    6069def NETWORK(context, str): 
    6170        return re.match(Object.NETWORK_PATTERN, str) \ 
    62                 or str in help_network(context
     71                or str in help_object(context, 'network'
    6372 
    6473def PORT(context, str): 
    6574        return re.match(Object.PORT_PATTERN, str) \ 
    66                 or str in help_port(context
     75                or str in help_object(context, 'port'
    6776 
    6877def PORT_RANGE(context, str): 
     
    198207 
    199208protocol_rules = { 
    200         lambda ctx, foo: [x.name for x in resolver.get_objects('protocol')] : { 
     209        lambda ctx, token: resolver.have_object(Object.PROTOCOL, token) : { 
    201210                GROUP : 30, 
    202211                UNLESS_VAR : 'protocol', 
     
    323332                                                JUMP : 'commands', 
    324333                                                JUMP_TO : 'sport', 
    325                                                 HELP : help_port
     334                                                HELP : lambda ctx: help_object(ctx, 'port')
    326335                                        }, 
    327336                                        IF : check_port_protocol, 
     
    413422        }, 
    414423        'object' : { 
    415                 HELP : 'Ruleset manipulation objects.', 
     424                HELP : 'Ruleset object manipulation.', 
    416425                'import' : { 
    417426                        LABEL : 'import', 
    418                         '|'.join(Resolver.get_object_types()) : { 
     427                        'all' : { 
     428                                RANGE : 1, 
     429                                GROUP : 10, 
     430                                HELP : 'Also import aliases.', 
     431                                VAR : 'aliases', 
     432                                JUMP : 'import', 
     433                        }, 
     434                        's|'.join(Resolver.get_object_types()) + 's' : { 
     435                                GROUP : 20, 
    419436                                VAR : 'import_types', 
    420                                 HELP : lambda ctx: [(x, 'Import system %s objects' % x) for x in Resolver.get_object_types()], 
    421                                 ACTION : lambda x, import_types: resolver.populate_defaults(tolist(import_types)), 
     437                                HELP : lambda ctx: [(x + 's', 'Import system %s objects' % x) for x in Resolver.get_object_types()], 
     438                                ACTION : lambda ctx, import_types, **args: resolver.populate_defaults(tolist(import_types), with_aliases = 'aliases' in ctx), 
    422439                        }, 
    423440                        HELP : 'Import system objects.', 
    424                         ACTION : lambda x: resolver.populate_defaults(), 
     441                        ACTION : lambda ctx, **args: resolver.populate_defaults(with_aliases = 'aliases' in ctx), 
    425442                }, 
    426443                'create' : { 
    427444                        HELP : 'Create a ruleset object.', 
    428                         Object.NETWORK : { 
     445                        '|'.join(Resolver.get_object_types()) : { 
    429446                                VAR : 'type', 
    430                                 HELP : 'Create network object.'
     447                                HELP : lambda ctx: [(x, 'Create %s object' % x) for x in Resolver.get_object_types()]
    431448                                Object.NAME_PATTERN : { 
    432449                                        VAR : 'name', 
    433450                                        HELP : { '<name>' : 'Name of object to create.' }, 
    434                                         Object.NETWORK_PATTERN : { 
     451                                        LABEL : 'create', 
     452                                        lambda ctx, token: resolver.have_object(ctx['type'], token) or re.match(Object.TYPE_PATTERNS[ctx['type']], token) : { 
    435453                                                VAR : 'value', 
    436                                                 HELP : { '<network>' : 'Network.' }, 
    437                                                 ACTION : create_object, 
    438                                                 '.+' : { 
    439                                                         VAR : 'description', 
    440                                                         HELP : { '<description>' : 'Description of object.' }, 
     454                                                HELP : lambda ctx: help_object(ctx, ctx['type']), 
     455                                                ACTION : { 
     456                                                        IF_VAR : 'value', 
    441457                                                        ACTION : create_object, 
    442458                                                }, 
    443                                         }, 
    444                                 }, 
    445                         }, 
    446                         Object.PORT : { 
    447                                 VAR : 'type', 
    448                                 HELP : 'Create port object.', 
    449                                 Object.NAME_PATTERN : { 
    450                                         VAR : 'name', 
    451                                         HELP : { '<name>' : 'Name of object to create.' }, 
    452                                         Object.PORT_PATTERN : { 
    453                                                 VAR : 'value', 
    454                                                 HELP : { '<port>' : 'Port.' }, 
    455                                                 ACTION : create_object, 
    456                                                 '.+' : { 
    457                                                         VAR : 'description', 
    458                                                         HELP : { '<description>' : 'Description of object.' }, 
    459                                                         ACTION : create_object, 
     459                                                JUMP : 'create', 
     460                                                'description' : { 
     461                                                        '.+' : { 
     462                                                                VAR : 'description', 
     463                                                                HELP : { '<description>' : 'Description of object.' }, 
     464                                                                JUMP : 'create', 
     465                                                        }, 
     466                                                        HELP : 'Optional description of object.' 
    460467                                                }, 
    461468                                        }, 
     
    467474                        '|'.join(Resolver.get_object_types()) : { 
    468475                                VAR : 'type', 
    469                                 HELP : { 
    470                                         'network' : 'Remove network object.', 
    471                                         'port' : 'Remove port object.', 
    472                                 }, 
     476                                HELP : lambda ctx: [(x + 's', 'Remove %s objects' % x) for x in Resolver.get_object_types()], 
    473477                        }, 
    474478                }, 
     
    479483                                ACTION : list_objects, 
    480484                        }, 
    481                         '|'.join(Resolver.get_object_types()) : { 
     485                        's|'.join(Resolver.get_object_types()) + 's' : { 
    482486                                VAR : 'type', 
    483                                 HELP : lambda ctx: [(x, 'List %s objects' % x) for x in Resolver.get_object_types()], 
     487                                HELP : lambda ctx: [(x + 's', 'List %s objects' % x) for x in Resolver.get_object_types()], 
    484488                                ACTION : list_objects, 
    485489                        }, 
     
    487491        }, 
    488492        'delete' : { 
    489                 GROUP : 10, 
     493                GROUP : 20, 
    490494                IF : have_modifiable_firewall, 
    491495                HELP : 'Remove rule(s) from the ruleset.', 
     
    499503        }, 
    500504        'list' : { 
    501                 GROUP : 10, 
     505                GROUP : 20, 
    502506                IF : have_firewall, 
    503507                HELP : 'List ruleset.', 
     
    505509        }, 
    506510        'move' : { 
    507                 GROUP : 10, 
     511                GROUP : 20, 
    508512                IF : have_modifiable_firewall, 
    509513                HELP : 'Move rule.', 
  • fwc/trunk/Object.py

    r117 r118  
    88        PORT_PATTERN = r'\d{1,5}' 
    99        PROTOCOL_PATTERN = r'\d{1,3}' 
     10 
     11        TYPE_PATTERNS = { 
     12                'network' : NETWORK_PATTERN, 
     13                'port' : PORT_PATTERN, 
     14                'protocol' : PROTOCOL_PATTERN, 
     15        } 
    1016 
    1117        # Regex matching a valid object name 
  • fwc/trunk/Resolver.py

    r117 r118  
    4848                self.add_object(Object(Object.NETWORK, 'any', '0.0.0.0/0', 'Any network address')) 
    4949                self.add_object(Object(Object.NETWORK, 'localhost', '127.0.0.1', 'Local host')) 
    50                 self.add_object(Object(Object.PORT, 'any', '0-65535', 'Any port')) 
     50                self.add_object(Object(Object.PORT, 'any', '0', 'Any port')) 
    5151                # Default protocols 
    5252                self.add_object(Object(Object.PROTOCOL, 'tcp', '6', 'Transmission Control Protocol')) 
    5353                self.add_object(Object(Object.PROTOCOL, 'udp', '17', 'User Datagram Protocol')) 
    5454                self.add_object(Object(Object.PROTOCOL, 'icmp', '1', 'Internet Control Message Protocol')) 
     55 
     56        def have_object(self, type, name): 
     57                if type in self.__objects and name in self.__objects[type]: 
     58                        return self.__objects[type][name] 
     59                return None 
    5560 
    5661        def resolve_object(self, type, name): 
     
    8691                        raise Resolver.InvalidObject("Object '%s:%s' not in ruleset" % (object.type, object.name)) 
    8792 
    88         def populate_defaults(self, types = get_object_types()): 
     93        def populate_defaults(self, types = get_object_types(), with_aliases = False): 
    8994                """ Populate object database from system. """ 
    9095                for type in types: 
     96                        if type[-1] == 's': type = type[0:-1] 
    9197                        objects = 0 
    9298                        if type == Object.PORT: 
     
    103109                                                name = tokens.pop(0) 
    104110                                                port, proto = tokens.pop(0).split('/') 
    105                                                 tokens.insert(0, name) 
     111                                                if with_aliases: 
     112                                                        tokens.insert(0, name) 
     113                                                else: 
     114                                                        tokens = [ name ] 
    106115                                                for token in tokens: 
    107116                                                        try: 
     
    127136                                                ip, names = host.split(None, 1) 
    128137                                                names = names.split() 
     138                                                if not with_aliases: 
     139                                                        names = names[0:1] 
    129140                                                for name in names: 
    130141                                                        try: 
     
    163174                                        pass 
    164175                                info("added", objects, "protocol objects") 
     176                        else: 
     177                                raise Resolver.Error("can't import unknown object type '%s'" % type) 
    165178 
    166179        @staticmethod 
     
    170183 
    171184        def get_objects(self, type): 
     185                if type[-1] == 's': type = type[0:-1] 
    172186                return self.__objects[type].values() 
    173187