2 # SFAtables is a tool for restricting access to an SFA aggregate in a generic
5 # It is modeled using abstractions in iptables. Specifically, 'matches' specify
6 # criteria for matching certain requests, 'targets' specify actions that treat
7 # requests in a certain way, and 'chains' are used to group related
14 from optparse import OptionParser
16 from sfatables import commands, matches, targets
17 from sfatables.xmlextension import Xmlextension
19 def load_commands(module, list):
22 for command_name in list:
23 command_module = __import__(".".join([module,command_name]),fromlist=[module])
24 command = getattr(command_module, command_name)
25 command_dict[command_name]=command()
29 def load_xml_extensions(module, list):
33 module = Xmlextension(ext_name)
34 ext_dict[ext_name]=module
39 def create_parser(command_dict):
40 parser = OptionParser(usage="sfatables [command] [chain] [match] [target]",
41 description='See "man sfatables" for more detail.')
43 for k in command_dict.keys():
44 command = command_dict[k]
45 for (short_option,long_option) in command.options:
46 parser.add_option(short_option,long_option,dest=command.type,action=command.action,const=k,help=command.help,metavar="CHAIN")
50 def xml_ext_create_parser(ext_dict):
51 parser = OptionParser(usage="sfatables [command] [chain] [match] [target]",
52 description='See "man sfatables" for more detail.')
54 for k in ext_dict.keys():
56 for arg in command.arguments:
57 parser.add_option(None,"--"+arg,dest=arg,help=command.help,metavar=command.operand)
62 def partition(sep, lst):
77 # Segment command line into three blobs, one each for the command, match and target respectively.
79 pargs = partition('--', sys.argv[1:])
81 command_dict = load_commands("sfatables.commands",commands.all)
82 command_parser = create_parser(command_dict)
83 (options, args) = command_parser.parse_args()
85 command = command_dict[options.command]
89 raise Exception("Must specify match for this command")
90 match_dict = load_xml_extensions("sfatables.matches",matches.all)
91 match_parser = create_parser(match_dict)
92 (match_options, args) = match_parser.parse_args(pargs[1])
98 raise Exception("Must specify a target for this command")
99 match_dict = load_xml_extensions("sfatables.targets",targets.all)
100 target_parser = create_parser(match_dict)
101 (target_options, args) = target_parser.parse_args(pargs[2])
103 target_options = None
105 command(options, match_options, target_options)
107 if __name__=='__main__':