Changeset 125

Show
Ignore:
Timestamp:
06/15/05 20:01:56 (3 years ago)
Author:
athomas
Message:
  • Added a dedicated Config class with read only attributes, descriptions, etc.
  • Added LinuxIPTables. Acquisition of Linux firewalls is working.
  • Firewall grammar merging is now working.
Files:

Legend:

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

    r124 r125  
    33import socket 
    44from ConfigParser import RawConfigParser 
     5from Config import Config 
    56from string import Template 
    67from CLY.Parser import Parser 
    78from CLY.Symbols import * 
    89from util import * 
    9 from Rule import Rule 
    1010from Resolver import Resolver 
    1111from Firewall import Firewall 
    12 from IPTables import IPTables 
     12from LinuxIPTables import LinuxIPTables 
    1313from Object import Object 
    1414from Singleton import Singleton 
     
    1717class Engine(Singleton): 
    1818        version = '0.1' 
    19         firewall_types = ('IPTables',) 
     19        firewall_types = {'linux' : 'LinuxIPTables'} 
    2020 
    2121        class Error(Exception): pass 
     
    2424                self.__config = RawConfigParser() 
    2525                self.__config.read(['/etc/fwcrc', os.path.expanduser('~/.fwcrc')]) 
     26                self.config = Config() 
     27                self.config.define('prompt', 'fwc> ', 'FWC prompt') 
     28                # TODO: Merge loaded config into self.config 
    2629                self.__firewalls = {} 
    2730                # Grammar hooks. 
     
    5457                                VAR : 'where', 
    5558                                HELP : { 
    56                                         'top' : 'Insert rule at top of ruleset.', 
    57                                         'bottom' : 'Insert rule at bottom of ruleset (default).', 
     59                                        'top' : 'Insert rule at top of policy.', 
     60                                        'bottom' : 'Insert rule at bottom of policy (default).', 
    5861                                }, 
    5962                                JUMP : RETURN, 
     
    122125                                IF : have_firewall, 
    123126                                GLOBAL_LABEL : 'commands', 
    124                                 MERGE : [ self.__placement_rules, self.__protocol_rules ], 
     127                                MERGE : [ self.__placement_rules, self.__protocol_rules, self.__hooks['match'] ], 
    125128                                VAR : 'action', 
    126129                                ACTION : { 
     
    168171                                        }, 
    169172 
     173                                }, 
     174                                'in' : { 
     175                                        GROUP : 20, 
     176                                        HELP : 'Match packets coming in an interface.', 
     177                                        lambda ctx, arg: INTERFACE(ctx, arg) and 'in_if' not in ctx or arg not in ctx['in_if'] : { 
     178                                                HELP : lambda ctx: [(k, v) for k, v in help_interface(ctx).iteritems() if 'in_if' not in ctx or k not in ctx['in_if']], 
     179                                                VAR : 'in_if', 
     180                                                LABEL : 'in-if', 
     181                                                JUMP_TO : 'in-if', 
     182                                                JUMP : 'commands', 
     183                                        }, 
     184                                }, 
     185                                'out' : { 
     186                                        GROUP : 20, 
     187                                        HELP : 'Match packets going out an interface.', 
     188                                        INTERFACE : { 
     189                                                HELP : help_interface, 
     190                                                VAR : 'out_if', 
     191                                                LABEL : 'out-if', 
     192                                                JUMP_TO : 'out-if', 
     193                                                JUMP : 'commands', 
     194                                        }, 
    170195                                }, 
    171196                                'from' : { 
     
    279304                        'object' : { 
    280305                                IF : have_firewall, 
    281                                 HELP : 'Ruleset object manipulation.', 
     306                                HELP : 'Firewall object manipulation.', 
    282307                                'import' : { 
    283308                                        LABEL : 'import', 
     
    299324                                }, 
    300325                                'create' : { 
    301                                         HELP : 'Create a ruleset object.', 
     326                                        HELP : 'Create a policy object.', 
    302327                                        '|'.join(Resolver.get_object_types()) : { 
    303328                                                VAR : 'type', 
     
    334359                                }, 
    335360                                'delete' : { 
    336                                         HELP : 'Delete a ruleset object.', 
     361                                        HELP : 'Delete a policy object.', 
    337362                                        's|'.join(Resolver.get_object_types()) + 's' : { 
    338363                                                VAR : 'type', 
     
    375400                                GROUP : 20, 
    376401                                IF : have_firewall, 
    377                                 HELP : 'Remove rule(s) from the ruleset.', 
     402                                HELP : 'Remove rule(s) from the policy.', 
    378403                                LABEL : 'rule', 
    379404                                RULE : { 
     
    387412                                GROUP : 20, 
    388413                                IF : have_firewall, 
    389                                 HELP : 'List ruleset.', 
    390                                 ACTION : self.__list_ruleset
     414                                HELP : 'List policy.', 
     415                                ACTION : self.__list_policy
    391416                        }, 
    392417                        'move' : { 
     
    407432                        'firewall' : { 
    408433                                HELP : "Commands for firewall management.", 
     434                                'set' : { 
     435                                        GROUP : 10, 
     436                                        IF : have_firewall, 
     437                                        HELP : 'Display or set firewall settings', 
     438                                        ACTION : { 
     439                                                HELP : 'List current settings.', 
     440                                                ACTION : self.__list_firewall_settings, 
     441                                        }, 
     442                                        lambda ctx, setting: setting in self.firewall.config and not self.firewall.config.readonly(setting): { 
     443                                                VAR : 'setting', 
     444                                                HELP : lambda ctx: [ (v, 'Set ' + self.firewall.config.help(v)) for v in self.firewall.config if not self.firewall.config.readonly(v) ], 
     445                                                lambda ctx, value: self.firewall.config.validate(ctx['setting'], value) : { 
     446                                                        VAR : 'value', 
     447                                                        HELP : ('<value>', 'Value for setting'), 
     448                                                        ACTION : self.__firewall_set, 
     449                                                }, 
     450                                        }, 
     451                                }, 
     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                                }, 
    409462                                'acquire' : { 
    410463                                        HELP : 'Acquire a firewall for management.', 
     
    412465                                                VAR : 'name', 
    413466                                                HELP : ('<name>', 'Name of firewall.'), 
    414                                                 LABEL : 'acquire', 
    415                                                 IP : { 
    416                                                         UNLESS : lambda ctx: 'ip' in ctx, 
    417                                                         VAR : 'ip', 
    418                                                         HELP : lambda ctx: ('<ip>', 'Management IP address of firewall%s.' % (name_to_ip(ctx['name']) and ' (' + name_to_ip(ctx['name']) + ')' or '')), 
     467                                                FIREWALL_TYPE : { 
     468                                                        VAR : 'type', 
    419469                                                        JUMP : 'acquire', 
    420                                                 }, 
    421                                                 FIREWALL_TYPE : { 
    422                                                         IF : lambda ctx: 'ip' in ctx or name_to_ip(ctx['name']), 
    423                                                         VAR : 'type', 
    424470                                                        HELP : help_firewall_type, 
    425                                                         ACTION : lambda ctx, **args: self.acquire_firewall(**args), 
     471                                                        LABEL : 'acquire', 
     472                                                        'description' : { 
     473                                                                UNLESS_VAR : 'description', 
     474                                                                HELP : 'Set description of firewall.', 
     475                                                                '.*' : { 
     476                                                                        HELP : ('<description>', 'Description of firewall'), 
     477                                                                        VAR : 'description', 
     478                                                                        JUMP : 'acquire', 
     479                                                                }, 
     480                                                        }, 
     481                                                        IP : { 
     482                                                                UNLESS : lambda ctx: 'ip' in ctx, 
     483                                                                VAR : 'ip', 
     484                                                                HELP : lambda ctx: ('<ip>', 'Management IP address of firewall%s.' % (name_to_ip(ctx['name']) and ' (' + name_to_ip(ctx['name']) + ')' or '')), 
     485                                                                JUMP : 'acquire', 
     486                                                        }, 
     487                                                        ACTION : { 
     488                                                                IF : lambda ctx: 'type' in ctx and ('ip' in ctx or name_to_ip(ctx['name'])), 
     489                                                                ACTION : lambda ctx, **args: self.acquire_firewall(**args), 
     490                                                        }, 
    426491                                                }, 
    427492                                        }, 
     
    429494                                'list' : { 
    430495                                        HELP : "List available firewalls.", 
     496                                        ACTION : self.__list_firewalls, 
    431497                                }, 
    432498                                'switch' : { 
    433                                         HELP : 'Switch firewall.', 
     499                                        HELP : 'Switch active firewall.', 
    434500                                        FIREWALL : { 
    435501                                                VAR : 'firewall', 
     
    439505                                        }, 
    440506                                }, 
    441                                 FIREWALL : { 
    442                                         GROUP : 10, 
    443                                         VAR : 'firewall', 
    444                                         HELP : help_firewall, 
    445                                         'list' : { 
    446                                                 HELP : 'List settings for this firewall.', 
    447                                                 ACTION : lambda ctx, firewall: self.__list_firewall_settings(firewall), 
    448                                         }, 
    449                                 }, 
    450507                        }, 
    451508                } 
     
    453510                self._parser = Parser(self.__grammar) 
    454511 
    455  
    456         def __list_objects(self, context, type = 'all', filter = '*'): 
     512        def __firewall_set(self, ctx, setting, value): 
     513                try: 
     514                        self.firewall.config[setting] = value 
     515                except Config.Error, e: 
     516                        error(e) 
     517 
     518        def __firewall_unset(self, ctx, setting): 
     519                try: 
     520                        del(self.firewall.config[setting]) 
     521                except Config.Error, e: 
     522                        error(e) 
     523 
     524        def __list_firewalls(self, ctx): 
     525                for fw in sorted(self.__firewalls.values(), lambda a, b: cmp(a.name, b.name)): 
     526                        if self.firewall == fw: 
     527                                out = '^B*^B ' 
     528                        else: 
     529                                out = '  ' 
     530                        out += '^B%s^B ^B%s^B ^B%s^B' % (fw.config.name, fw.config.type, fw.config.ip) 
     531                        if fw.config.description: 
     532                                out += ' description ^B"%s"^B' % fw.config.description 
     533                        cprint(out) 
     534 
     535        def __list_objects(self, ctx, type = 'all', filter = '*'): 
    457536                if type == 'all': 
    458537                        type = Resolver.get_object_types() 
     
    468547 
    469548        def __list_firewall_settings(self, firewall): 
    470                 heading = ['^B^USetting^N', '^B^UValue^N'
     549                heading = ['^B^USetting^N', '^B^UValue^N', '^B^UDescription^N'
    471550                rows = [] 
    472                 firewall = self.__firewalls[firewall] 
    473                 for var in firewall.listv(): 
    474                         rows.append([var, firewall.rawgetv(var)]) 
     551                for var in self.firewall.config: 
     552                        rows.append([var, self.firewall.config[var], self.firewall.config.help(var)]) 
    475553                rows.sort(lambda a, b: cmp(a[0], b[0])) 
    476554                print_table(rows, heading) 
    477555                 
    478556        def __firewall_class(self, type): 
    479                 for i in self.firewall_types: 
    480                         if i.lower() == type: 
    481                                 type = i 
    482                                 break 
    483557                from DynamicLoader import loadClass 
    484                 return loadClass('%s.%s' % (type, type)) 
    485  
    486         def acquire_firewall(self, name, type, ip = None): 
     558                return loadClass('%s.%s' % (self.firewall_types[type], self.firewall_types[type])) 
     559 
     560        def acquire_firewall(self, name, type, ip = None, description = ''): 
    487561                if name in self.__firewalls: 
    488562                        raise Engine.Error("Firewall '%s' already acquired" % name) 
     
    501575                        fw = FirewallClass(name, ip, self) 
    502576                        fw.acquire() 
     577                        fw.config.description = description 
    503578                        self.set_firewall(fw) 
     579                except KeyboardInterrupt: 
     580                        warning("User aborted acquisition of firewall") 
    504581                except Exception, e: 
    505582                        error(e) 
    506583 
    507         def quit(self, context = None): 
     584        def quit(self, ctx = None): 
    508585                info("Firewall console exit.") 
    509586                sys.exit(0) 
    510587 
    511         def __insert_rule(self, context, action, source = [], sport = [], destination = [], dport = [], protocol = None, description = None, where = 'bottom', index = None, state = 'new', log = None, reject_type = None, reject_subtype = None): 
     588        def __insert_rule(self, ctx, source = [], sport = [], in_if = [], destination = [], dport = [], out_if = [], log = None, index = None, where = 'bottom', **argd): 
    512589                if log: 
    513590                        if log == 'log': 
     
    516593                                log = log[1] 
    517594                if index != None: index = int(index) 
    518                 self.firewall.add(Rule(action, tolist(source), tolist(sport), tolist(destination), tolist(dport), protocol, description, state, log, reject_type, reject_subtype), where, index) 
    519  
    520         def __remove_rule(self, context, rules): 
     595                try: 
     596                        source = tolist(source) 
     597                        sport = tolist(sport) 
     598                        in_if = tolist(in_if) 
     599                        destination = tolist(destination) 
     600                        dport = tolist(dport) 
     601                        out_if = tolist(out_if) 
     602                        self.firewall.add(self.firewall.Rule( 
     603                                        source = source, sport = sport, in_if = in_if, 
     604                                        destination = destination, dport = dport, out_if = out_if, 
     605                                        **argd), 
     606                                where, index) 
     607                except Firewall.Error, e: 
     608                        error(e) 
     609 
     610        def __remove_rule(self, ctx, rules): 
    521611                self.firewall.remove(map(int, rules)) 
    522612 
    523         def __move_rule(self, context, old, where = 'top', index = None): 
     613        def __move_rule(self, ctx, old, where = 'top', index = None): 
    524614                if index and old == index: return 
    525615                try: 
     
    532622                        raise 
    533623 
    534         def __list_ruleset(self, context): 
    535                 for ruleno, rule in enumerate(self.firewall.get_rules()): 
    536                         cmd = "^B%s^B" % rule.action 
    537                         if rule.state != 'new': 
    538                                 cmd += " state ^B" + rule.state + "^B" 
    539                         if rule.protocol: 
    540                                 try: 
    541                                         cmd += ' protocol ^B%s^B' % int(rule.protocol) 
    542                                 except: 
    543                                         cmd += " ^B" + rule.protocol + "^B" 
    544                         if rule.source or rule.sport: 
    545                                 cmd += " from" 
    546                                 if rule.source: 
    547                                         cmd += " ^B" + ' '.join(rule.source) + "^B" 
    548                                 if rule.sport: 
    549                                         cmd += " port ^B" + ' '.join(rule.sport) + "^B" 
    550                         if rule.destination or rule.dport: 
    551                                 cmd += " to" 
    552                                 if rule.destination: 
    553                                         cmd += " ^B" + ' '.join(rule.destination) + "^B" 
    554                                 if rule.dport: 
    555                                         cmd += " port ^B" + ' '.join(rule.dport) + "^B" 
    556                         if rule.reject_type: 
    557                                 cmd += " with ^B%s^B" % rule.reject_type 
    558                                 if rule.reject_subtype: 
    559                                         cmd += " ^B%s^B" % rule.reject_subtype 
    560                         if rule.log: 
    561                                 cmd += " log" 
    562                                 if type(rule.log) is str: 
    563                                         cmd += " message ^B'%s'^B" % rule.log 
    564                         if rule.description: 
    565                                 cmd += " description ^B'%s'^B" % rule.description 
    566                         cprint("^B%3i:^B %s" % (ruleno, cmd)) 
    567  
    568         def __create_object(self, context, type, name, value, description = None): 
     624        def __list_policy(self, ctx): 
     625                self.firewall.list_rules() 
     626 
     627        def __create_object(self, ctx, type, name, value, description = None): 
    569628                try: 
    570629                        self.resolver.add_object(Object(type, name, value, description)) 
     
    588647                                self.unhook(hook, grammar) 
    589648                        self.resolver = None 
     649                        self.prompt = 'fwc> ' 
    590650 
    591651                self.firewall = firewall 
    592652                if self.firewall: 
    593653                        self.resolver = firewall.resolver 
    594                         self.__firewalls[firewall.name] = firewall 
     654                        self.__firewalls[firewall.config.name] = firewall 
    595655                        for hook, grammar in firewall.hooks.iteritems(): 
    596656                                self.hook(hook, grammar) 
    597                 info("Current firewall: " + self.firewall.name) 
     657                        self.prompt = 'fwc:%s> ' % firewall.config.name 
     658                info("Current firewall: ^B" + firewall.config.name + "^B") 
    598659 
    599660        def hook(self, hook, grammar): 
     
    605666                self.__hooks[hook].remove(grammar) 
    606667 
    607         def rawgetv(self, section, key, default = None): 
    608                 try: 
    609                         return self.__config.get(section, key) 
    610                 except: 
    611                         return default 
    612  
    613         def getv(self, section, key, default = None): 
    614                 try: 
    615                         return self.expandv(section, self.__config.get(section, key)) 
    616                 except: 
    617                         return default 
    618  
    619         def hasv(self, section, key): 
    620                 return self.__config.has_option(section, key) 
    621  
    622         def setv(self, section, key, value): 
    623                 return self.__config.set(section, key, value) 
    624  
    625         def setdefaultv(self, section, key, value): 
    626                 if not self.hasv(section, key): 
    627                         self.setv(section, key, value) 
    628  
    629         def listv(self, section): 
    630                 return self.__config.options(section) 
    631  
    632         def expandv(self, section, text): 
    633                 vars = dict(self.__config.items(section)) 
    634                 lasttext = '' 
    635                 while '$' in text and lasttext != text: 
    636                         lasttext = text[:] 
    637                         text = Template(text).safe_substitute(vars) 
    638                 return text 
    639  
    640668  
    641669 
    642 def check_port_protocol(context): 
    643         return 'protocol' in context and context['protocol'] in [ 'tcp', 'udp' ] 
    644  
    645 def have_firewall(context): 
     670def check_port_protocol(ctx): 
     671        return 'protocol' in ctx and context['protocol'] in [ 'tcp', 'udp' ] 
     672 
     673def have_firewall(ctx): 
    646674        """ Return true if the current firewall is have_firewall. """ 
    647675        return Engine().firewall 
    648676 
    649 def help_object(context, type, include_generic = True): 
     677def help_object(ctx, type, include_generic = True): 
    650678        if include_generic: 
    651679                help = { '<%s>' % type : '%s object.' % type.title() } 
     
    659687        return help 
    660688 
    661 def help_network(context): 
     689def help_network(ctx): 
    662690        help = { '<network>' : 'Network address.'} 
    663         for o in Engine().firewall.resolver.get_objects('network'): 
     691        for o in Engine().firewall.resolver.get_objects(Object.NETWORK): 
    664692                if o.description: 
    665693                        help[o.name] = "%s (%s)" % (o.description, ', '.join(tolist(o.value))) 
     
    668696        return help 
    669697                 
    670 def help_port(context): 
     698def help_interface(ctx): 
     699        help = { '<interface>' : 'Interface name.'} 
     700        for o in Engine().firewall.resolver.get_objects(Object.INTERFACE): 
     701                if o.description: 
     702                        help[o.name] = "%s (%s)" % (o.description, ', '.join(tolist(o.value))) 
     703                else: 
     704                        help[o.name] = o.value 
     705        return help 
     706                 
     707def help_port(ctx): 
    671708        help = { '<port>' : 'Port.'} 
    672         for o in Engine().firewall.resolver.get_objects('port'): 
     709        for o in Engine().firewall.resolver.get_objects(Object.PORT): 
    673710                text = o.description or "Port object %s" % o.name 
    674711                help[o.name] = text + " (%s)" % ', '.join(tolist(o.value)) 
    675712        return help 
    676713 
    677 def help_firewall(context): 
     714def help_firewall(ctx): 
    678715        help = {} 
    679716        for name, fw in Engine()._Engine__firewalls.iteritems(): 
    680                 help[name] = "Firewall %s" % fw.name 
     717                if fw.description: 
     718                        help[name] = fw.description 
     719                else: 
     720                        help[name] = "Firewall %s" % fw.name 
    681721                if Engine().firewall == fw: 
    682722                        help[name] += ' ^B(current)^B' 
     
    691731  
    692732 
    693 def IP(context, str): 
     733def IP(ctx, str): 
    694734        return re.match(Object.IP_PATTERN, str) 
    695735 
    696 def NETWORK(context, str): 
     736def INTERFACE(ctx, str): 
     737        return re.match(Object.INTERFACE_PATTERN, str) \ 
     738                or str in help_object(ctx, 'interface') 
     739         
     740def NETWORK(ctx, str): 
    697741        return re.match(Object.NETWORK_PATTERN, str) \ 
    698                 or str in help_object(context, 'network') 
    699  
    700 def PORT(context, str): 
     742                or str in help_object(ctx, 'network') 
     743 
     744def PORT(ctx, str): 
    701745        return re.match(Object.PORT_PATTERN, str) \ 
    702                 or str in help_object(context, 'port') 
    703  
    704 def PORT_RANGE(context, str): 
     746                or str in help_object(ctx, 'port') 
     747 
     748def PORT_RANGE(ctx, str): 
    705749        range = str.split('-') 
    706         return PORT(context, range[0]) and (len(range) < 2 or len(range) > 1 and PORT(context, range[1])) 
    707  
    708 def RULE(context, str): 
     750        return PORT(ctx, range[0]) and (len(range) < 2 or len(range) > 1 and PORT(context, range[1])) 
     751 
     752def RULE(ctx, str): 
    709753        try: 
    710754                return int(str) >= 0 and int(str) < len(Engine().firewall.get_rules()) 
     
    712756                return False 
    713757 
    714 def FIREWALL(context, arg): 
    715         return arg in help_firewall(context
    716  
    717 def help_firewall_type(context): 
     758def FIREWALL(ctx, arg): 
     759        return arg in help_firewall(ctx
     760 
     761def help_firewall_type(ctx): 
    718762        help = {} 
    719         for type in Engine().firewall_types
     763        for type in Engine().firewall_types.keys()
    720764                help[type.lower()] = 'Firewall type %s' % type 
    721765        return help 
    722766 
    723 def FIREWALL_TYPE(context, arg): 
    724         return arg in map(str.lower, Engine().firewall_types) 
     767def FIREWALL_TYPE(ctx, arg): 
     768        return arg in Engine().firewall_types 
  • fwc/trunk/Firewall.py

    r124 r125  
    1 from ConfigReader import ConfigReader 
     1from Config import Config 
    22from Resolver import Resolver 
    3 from Rule import Rule 
    43from Object import Object 
    5 from util import abstract 
     4from util import * 
     5import re 
    66 
    7 class Firewall(ConfigReader)
     7class Firewall
    88        """ 
    99                The Firewall class contains all  
     
    1414        class InvalidRule(Error): pass 
    1515 
    16         def __init__(self, name, ip, engine, hooks = {}): 
    17                 ConfigReader.__init__(self, 'firewall-' + name, engine) 
     16        class Rule: 
     17                def __init__(self, action = None, source = [], sport = [], destination = [], dport = [], protocol = [], description = None, log = None, reject_type = None, reject_subtype = None, in_if = None, out_if = None): 
     18                        self.action = action 
     19                        self.source = source 
     20                        self.sport = sport 
     21                        self.dport = dport 
     22                        self.destination = destination 
     23                        self.source = source 
     24                        self.protocol = protocol 
     25                        self.description = description 
     26                        self.log = log 
     27                        self.reject_type = reject_type 
     28                        self.reject_subtype = reject_subtype 
     29                        self.in_if = in_if 
     30                        self.out_if = out_if 
     31 
     32        def __init__(self, type, name, ip, engine, hooks = {}): 
     33                self.config = Config() 
     34                self.config.define('type', type, 'Type of firewall', readonly = 1) 
     35                self.config.define('name', name, 'Firewall name', readonly = 1) 
     36                self.config.define('ip', ip, 'Management IP address of firewall') 
     37                self.config.define('description', '', 'Description of firewall') 
    1838                self.__rules = [] 
    1939                self.resolver = Resolver() 
    20                 self.name, self.ip, self.engine, self.hooks = name, ip, engine, hooks 
     40                self.engine, self.hooks = engine, hooks 
    2141 
    2242        def resolve_rule(self, rule): 
     
    4868                                raise Firewall.InvalidIndex("invalid location '%s' for rule addition" % where) 
    4969                except IndexError: 
    50                         raise Firewall.InvalidIndex("Invalid ruleset index %s" % index) 
     70                        raise Firewall.InvalidIndex("Invalid policy index %s" % index) 
    5171 
    5272        def move(self, old, new): 
     
    6484                                raise 
    6585                except IndexError: 
    66                         raise InvalidRule("Invalid ruleset index %s or %s" % (old, new)) 
     86                        raise InvalidRule("Invalid policy index %s or %s" % (old, new)) 
    6787 
    6888        def remove(self, index): 
     
    83103                return self.__rules[rule] 
    84104 
     105        def describe_rule(self, rule, pretty = True): 
     106                description = "^B%s^B" % rule.action 
     107                if rule.protocol: 
     108                        try: 
     109                                description += ' protocol ^B%s^B' % int(rule.protocol) 
     110                        except: 
     111                                description += " ^B" + rule.protocol + "^B" 
     112                if rule.source or rule.sport: 
     113                        description += " from" 
     114                        if rule.source: 
     115                                description += " ^B" + ' '.join(rule.source) + "^B" 
     116                        if rule.sport: 
     117                                description += " port ^B" + ' '.join(rule.sport) + "^B" 
     118                if rule.in_if: 
     119                        description += " in ^B%s^B" % ' '.join(rule.in_if) 
     120                if rule.destination or rule.dport: 
     121                        description += " to" 
     122                        if rule.destination: 
     123                                description += " ^B" + ' '.join(rule.destination) + "^B" 
     124                        if rule.dport: 
     125                                description += " port ^B" + ' '.join(rule.dport) + "^B" 
     126                if rule.out_if: 
     127                        description += " out ^B%s^B" % ' '.join(rule.out_if) 
     128                if rule.reject_type: 
     129                        description += " with ^B%s^B" % rule.reject_type 
     130                        if rule.reject_subtype: 
     131                                description += " ^B%s^B" % rule.reject_subtype 
     132                if rule.log: 
     133                        description += " log" 
     134                        if type(rule.log) is str: 
     135                                description += " message ^B'%s'^B" % rule.log 
     136                if rule.description: 
     137                        description += " description ^B'%s'^B" % rule.description 
     138                if not pretty: 
     139                        description = re.sub(r'\^.', '', description) 
     140                return description 
     141 
     142        def list_rules(self): 
     143                for ruleno, rule in enumerate(self.__rules): 
     144                        cprint("^B%3i:^B %s" % (ruleno, self.describe_rule(rule))) 
     145 
    85146        def get_rules(self): 
    86147                return self.__rules 
     
    91152        @abstract 
    92153        def install(self): 
    93                 """ Install ruleset on the given firewall. """ 
     154                """ Install policy on the given firewall. """ 
    94155 
    95156        @abstract 
  • fwc/trunk/fwc

    r123 r125  
    1010from CLY.Interactive import interact 
    1111from util import * 
    12 from Rule import Rule 
    1312from Object import Object 
    1413from Firewall import Firewall 
     
    2827 
    2928while True: 
    30         result = interact(engine._parser, engine.getv('default', 'prompt', 'fwc> ')
     29        result = interact(engine._parser, engine.config.prompt
    3130 
    3231        if result.state == Result.NOP: 
  • fwc/trunk/LinuxIPTables.py

    r124 r125  
    11from Interface import Interface 
    22from Firewall import Firewall 
    3 from ConfigReader import ConfigReader 
    43from CLY.Symbols import * 
    54from Object import Object 
     
    76import os 
    87 
    9 class IPTables(Firewall): 
     8class LinuxIPTables(Firewall): 
     9        class Rule(Firewall.Rule): 
     10                def __init__(self, state = [], **dargs): 
     11                        Firewall.Rule.__init__(self, **dargs) 
     12                        if state: state = tolist(state) 
     13                        self.state = state 
     14 
    1015        def __init__(self, name, ip, engine): 
    11                 Firewall.__init__(self, name, ip, engine, { 
     16                self.__states = { 
     17                        'new' : 'Match new connections.', 
     18                        'established' : 'Match established connections.', 
     19                        'invalid' : 'Match invalid connections.', 
     20                        'related' : 'Match related connections.', 
     21                } 
     22                Firewall.__init__(self, 'linux', name, ip, engine, { 
    1223                        'match' : { 
    1324                                'state' : { 
     
    1526                                        RANGE : 1, 
    1627                                        HELP : 'Match connections in this state (default: new).', 
    17                                         'new|established|invalid|related' : {