# Path: sshproxy/config.py
# class ConfigSection(object):
# class Config(object):
# class SSHproxyConfigSection(ConfigSection):
#     def __init__(self, config, section):
#     def init_section(self):
#     def get_default(self, option, default=None):
#     def __getitem__(self, option):
#     def __setitem__(self, option, value):
#     def __delitem__(self, option):
#     def keys(self):
#     def has_key(self, option):
#     def get(self, option, default=None, raw=False):
#     def set(self, option, value):
#     def pop(self, option):
#     def items(self):
#     def write(self):
#     def __str__(self):
#     def register(cls, force=False):
#     def register_handler(cls, name, handler, force=False):
#     def get_handler(cls, name):
#     def __init__(self, inifile):
#     def check_mode(self):
#     def __call__(self, section=None):
#     def __getitem__(self, section):
#     def __setitem__(self, section, options):
#     def __delitem__(self, section):
#     def pop(self, section):
#     def keys(self):
#     def has_key(self, section):
#     def defaults(self):
#     def touch(self):
#     def write(self, inifile=None):
#     def __str__(self):
# def path(path):
# 
# Path: sshproxy/acl.py
# class ACLDB(Registry):
#     _class_id = 'ACLDB'
#     _singleton = True
#     
#     def __reginit__(self):
#         self.rules = OrderedDict()
#         self.load_rules()
# 
#     def load_rules(self):
#         pass
# 
#     def save_rules(self):
#         pass
# 
#     def reload_rules(self):
#         self.rules = OrderedDict()
#         self.load_rules()
# 
#     def add_rule(self, acl, rule=None, index=None):
#         if rule is None:
#             rule = ACLRule(acl, 'False')
#         elif not isinstance(rule, ACLRule):
#             rule = ACLRule(acl, str(rule))
# 
#         if not self.rules.get(acl, None):
#             self.rules[acl] = ACList()
# 
#         if index is None:
#             self.rules[acl].append(rule)
#             return len(self.rules[acl]) - 1
#         else:
#             self.rules[acl].insert(index, rule)
#             return index
# 
#     def set_rule(self, acl, rule, index):
#         if not self.rules.get(acl, None):
#             return False
# 
#         try:
#             self.rules[acl][index].rule = str(rule)
#             return True
#         except IndexError:
#             return False
# 
#     def del_rule(self, acl, index):
#         if not self.rules.get(acl, None):
#             return False
# 
#         if index is None:
#             while len(self.rules[acl]):
#                 self.del_rule(acl, 0)
#             return True
#         else:
#             try:
#                 del self.rules[acl][index]
#             except IndexError:
#                 return False
# 
#         if not len(self.rules[acl]):
#             del self.rules[acl]
#         return True
# 
#     def list_rules(self, name=None):
#         aclrules = ACList()
#         for rulename in self.rules.keys():
#             for rule in self.rules[rulename]:
#                 if name in (None, rulename):
#                     aclrules.append(rule)
#         return aclrules
# 
#     def get_namespace(self, **namespaces):
#         namespace = dict(self.rules)
#         namespace['proxy'] = ProxyNamespace()
#         for ns in namespaces:
#             if not namespace.has_key(ns):
#                 namespace[ns] = ACLTags()
#             namespace[ns].update(namespaces[ns])
# 
#         return namespace
# 
#     def eval(self, expression, **namespaces):
#         """
#         Evaluate an ACL expression in the namespaces context.
#         """
# 
#         aclparser = ACLRuleParser(
#                         namespace=self.get_namespace(**namespaces))
#         return aclparser.eval(expression)
# 
# 
#     def check(self, acl, **namespaces):
#         """
#         Check an ACL rule in the namespaces context.
#         """
# 
#         try:
#             namespace = self.get_namespace(**namespaces)
# 
#             result = None
#             match = ''
# 
#             if isinstance(acl, ACLRule):
#                 match = repr(acl.rule)
#                 result = acl.eval(namespace)
#             else:
#                 if self.rules.has_key(acl):
#                     for rule in self.rules[acl]:
#                         match = repr(rule.rule)
#                         if rule.eval(namespace):
#                             result = True
#                             break
#                         else:
#                             result = False
# 
#             if result is None:
#                 result = False
#                 log.info('ACL %s not found' % str(acl))
#             else:
#                 log.info('ACL %s %s %s' % (acl, result, match))
#                 pass
#             return result
#         except:
#             log.exception('Error while checking ACL %s' % str(acl))
#             raise


from sshproxy.config import ConfigSection, get_config
from sshproxy.acl import ACLDB
from config import MySQLConfigSection
from mysql import MySQLDB, Q
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA



class MySQLACLConfigSection(MySQLConfigSection):
    section_id = 'acl_db.mysql'

MySQLACLConfigSection.register()


class MySQLACLDB(ACLDB, MySQLDB):
    _db_handler = 'acl_db'
    def __reginit__(self):
        self.open_db()
        ACLDB.__reginit__(self)

    def load_rules(self):
        query = """select name, rule from aclrules
                    order by weight asc"""
        for acl, rule in self.sql_list(query):
            self.add_rule(acl=acl, rule=rule.strip(), updatedb=False)

    def save_rules(self):
        pass

    def add_rule(self, acl, rule=None, index=None, updatedb=True):

# Path: sshproxy/config.py
# class ConfigSection(object):
# class Config(object):
# class SSHproxyConfigSection(ConfigSection):
#     def __init__(self, config, section):
#     def init_section(self):
#     def get_default(self, option, default=None):
#     def __getitem__(self, option):
#     def __setitem__(self, option, value):
#     def __delitem__(self, option):
#     def keys(self):
#     def has_key(self, option):
#     def get(self, option, default=None, raw=False):
#     def set(self, option, value):
#     def pop(self, option):
#     def items(self):
#     def write(self):
#     def __str__(self):
#     def register(cls, force=False):
#     def register_handler(cls, name, handler, force=False):
#     def get_handler(cls, name):
#     def __init__(self, inifile):
#     def check_mode(self):
#     def __call__(self, section=None):
#     def __getitem__(self, section):
#     def __setitem__(self, section, options):
#     def __delitem__(self, section):
#     def pop(self, section):
#     def keys(self):
#     def has_key(self, section):
#     def defaults(self):
#     def touch(self):
#     def write(self, inifile=None):
#     def __str__(self):
# def path(path):
# 
# Path: sshproxy/acl.py
# class ACLDB(Registry):
#     _class_id = 'ACLDB'
#     _singleton = True
#     
#     def __reginit__(self):
#         self.rules = OrderedDict()
#         self.load_rules()
# 
#     def load_rules(self):
#         pass
# 
#     def save_rules(self):
#         pass
# 
#     def reload_rules(self):
#         self.rules = OrderedDict()
#         self.load_rules()
# 
#     def add_rule(self, acl, rule=None, index=None):
#         if rule is None:
#             rule = ACLRule(acl, 'False')
#         elif not isinstance(rule, ACLRule):
#             rule = ACLRule(acl, str(rule))
# 
#         if not self.rules.get(acl, None):
#             self.rules[acl] = ACList()
# 
#         if index is None:
#             self.rules[acl].append(rule)
#             return len(self.rules[acl]) - 1
#         else:
#             self.rules[acl].insert(index, rule)
#             return index
# 
#     def set_rule(self, acl, rule, index):
#         if not self.rules.get(acl, None):
#             return False
# 
#         try:
#             self.rules[acl][index].rule = str(rule)
#             return True
#         except IndexError:
#             return False
# 
#     def del_rule(self, acl, index):
#         if not self.rules.get(acl, None):
#             return False
# 
#         if index is None:
#             while len(self.rules[acl]):
#                 self.del_rule(acl, 0)
#             return True
#         else:
#             try:
#                 del self.rules[acl][index]
#             except IndexError:
#                 return False
# 
#         if not len(self.rules[acl]):
#             del self.rules[acl]
#         return True
# 
#     def list_rules(self, name=None):
#         aclrules = ACList()
#         for rulename in self.rules.keys():
#             for rule in self.rules[rulename]:
#                 if name in (None, rulename):
#                     aclrules.append(rule)
#         return aclrules
# 
#     def get_namespace(self, **namespaces):
#         namespace = dict(self.rules)
#         namespace['proxy'] = ProxyNamespace()
#         for ns in namespaces:
#             if not namespace.has_key(ns):
#                 namespace[ns] = ACLTags()
#             namespace[ns].update(namespaces[ns])
# 
#         return namespace
# 
#     def eval(self, expression, **namespaces):
#         """
#         Evaluate an ACL expression in the namespaces context.
#         """
# 
#         aclparser = ACLRuleParser(
#                         namespace=self.get_namespace(**namespaces))
#         return aclparser.eval(expression)
# 
# 
#     def check(self, acl, **namespaces):
#         """
#         Check an ACL rule in the namespaces context.
#         """
# 
#         try:
#             namespace = self.get_namespace(**namespaces)
# 
#             result = None
#             match = ''
# 
#             if isinstance(acl, ACLRule):
#                 match = repr(acl.rule)
#                 result = acl.eval(namespace)
#             else:
#                 if self.rules.has_key(acl):
#                     for rule in self.rules[acl]:
#                         match = repr(rule.rule)
#                         if rule.eval(namespace):
#                             result = True
#                             break
#                         else:
#                             result = False
# 
#             if result is None:
#                 result = False
#                 log.info('ACL %s not found' % str(acl))
#             else:
#                 log.info('ACL %s %s %s' % (acl, result, match))
#                 pass
#             return result
#         except:
#             log.exception('Error while checking ACL %s' % str(acl))
#             raise


# Path: lib/mysql_db/acl.py
from sshproxy.config import ConfigSection, get_config
from sshproxy.acl import ACLDB
from config import MySQLConfigSection
from mysql import MySQLDB, Q

# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA



class MySQLACLConfigSection(MySQLConfigSection):
    section_id = 'acl_db.mysql'

MySQLACLConfigSection.register()


class MySQLACLDB(ACLDB, MySQLDB):
    _db_handler = 'acl_db'
    def __reginit__(self):
        self.open_db()
        ACLDB.__reginit__(self)

    def load_rules(self):
        query = """select name, rule from aclrules
                    order by weight asc"""
        for acl, rule in self.sql_list(query):
            self.add_rule(acl=acl, rule=rule.strip(), updatedb=False)

    def save_rules(self):
        pass

    def add_rule(self, acl, rule=None, index=None, updatedb=True):

        index = ACLDB.add_rule(self, acl, rule, index)