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
13 from optparse import OptionParser
15 from sfatables import commands, matches, targets
17 def load_extensions(module, list):
19 module_path = ".".join(module.split('.')[:-1])
21 commands = __import__(module,fromlist=[module_path])
23 for command_name in commands.all:
24 command_module = getattr(commands, command_name)
25 command = getattr(command_module, command_name)
26 command_dict[command.key]=command()
30 def create_parser(command_dict):
31 parser = OptionParser(usage="sfatables [command] [chain] [match] [target]",
32 description='See "man sfatables" for more detail.')
34 for k in command_dict.keys():
35 command = command_dict[k]
36 for (short_option,long_option) in command.options:
37 parser.add_option(short_option,long_option,dest=command.key,help=command.help,metavar=command.help.upper())
43 command_dict = load_extensions("sfatables.commands")
44 command_parser = create_parser(command_dict)
45 (options, args) = command_parser.parse_args()
47 if (len(options.keys()) != 1):
48 raise Exception("sfatables takes one command at a time.\n")
51 selected_command = command_dict[options.keys()[0]]
56 if (selected_command.matches):
57 match_dict = load_extensions("sfatables.matches")
58 match_parser = create_parser(match_dict)
59 (options, args) = match_parser.parse_args(args[2:])
61 if (selected_command.targets):
62 match_dict = load_extensions("sfatables.targets")
63 target_parser = create_parser(match_dict)
64 (options, args) = target_parser.parse_args(args[5:])
66 command(options, match_options, target_options)
68 if __name__=='__main__':