[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[sshproxy-dev] Changes to sshproxy/SSHproxy/pwdb/mysql.py
From: |
David Guerizec |
Subject: |
[sshproxy-dev] Changes to sshproxy/SSHproxy/pwdb/mysql.py |
Date: |
Mon, 26 Sep 2005 05:05:31 -0400 |
Index: sshproxy/SSHproxy/pwdb/mysql.py
diff -u sshproxy/SSHproxy/pwdb/mysql.py:1.3 sshproxy/SSHproxy/pwdb/mysql.py:1.4
--- sshproxy/SSHproxy/pwdb/mysql.py:1.3 Fri Sep 23 15:35:45 2005
+++ sshproxy/SSHproxy/pwdb/mysql.py Mon Sep 26 09:05:30 2005
@@ -57,17 +57,108 @@
port=port,
location=location,
user_list=user_list))
- #print 'site ' + name + ' grants access to ' + user_list.__repr__()
+ #print 'site ' + name + ' grants access to ' + user_list.__repr__()
simple.SimplePwDB.__init__(self, site_list)
- #def __del__(self):
- # db.close()
+# def __del__(self):
+# db.close()
+
+ def list_profiles(self):
+ q_getprofile = """
+ select id, name from profile
+ """
+ profile = db.cursor()
+ profile.execute(q_getprofile)
+ p = []
+ for id, name in profile.fetchall():
+ p.append({ 'id': id, 'name': name })
+ profile.close()
+ return p
+
+ def get_profile(self, name):
+ q_getprofile = """
+ select id, name from profile where name = '%s'
+ """
+ profile = db.cursor()
+ profile.execute(q_getprofile % name)
+ p = profile.fetchone()
+ if not p or not len(p):
+ return None
+ profile.close()
+ return { 'id': p[0], 'name': p[1] }
+
+ def add_profile(self, name):
+ q_addprofile = """
+ insert into profile (name) values ('%s')
+ """
+ if self.get_profile(name):
+ return None
+ profile = db.cursor()
+ profile.execute(q_addprofile % name)
+ profile.close()
+ return 1
+
+ def list_groups(self):
+ q_getgroup = """
+ select id, name from sgroup
+ """
+ group = db.cursor()
+ group.execute(q_getgroup)
+ p = []
+ for id, name in group.fetchall():
+ p.append({ 'id': id, 'name': name })
+ group.close()
+ return p
+
+ def get_group(self, name):
+ q_getgroup = """
+ select id, name from sgroup where name = '%s'
+ """
+ group = db.cursor()
+ group.execute(q_getgroup % name)
+ p = group.fetchone()
+ if not p or not len(p):
+ return None
+ group.close()
+ return { 'id': p[0], 'name': p[1] }
+
+ def add_group(self, name):
+ q_addgroup = """
+ insert into sgroup (name) values ('%s')
+ """
+ if self.get_group(name):
+ return None
+ group = db.cursor()
+ group.execute(q_addgroup % name)
+ group.close()
+ return 1
+
+
+ def is_allowed(self, username, password=None, key=None):
+ """Check is a user is allowed to connect to the proxy."""
+ if password is None and key is None:
+ return None
+ if key is None:
+ q_access = """
+ select id from login where uid = '%s' and `password` = '%s'
+ """ % (username, password)
+ else:
+ q_access = """
+ select id from login where uid = '%s' and `key` = '%s'
+ """ % (username, key)
+ logins = db.cursor()
+ logins.execute(q_access)
+ login = logins.fetchone()[0]
+ logins.close()
+ if login:
+ self.login = username
+ return login
def get_site(self, sid):
user = None
- if sid.find('@') >= 0:
- user, sid = sid.split('@')
+ if sid.find('@') >= 0:
+ user, sid = sid.split('@')
if not user:
q_user = """
select uid from site,user where site.id = user.site_id and
site.name = '%s'
@@ -80,26 +171,23 @@
return None, None
user = user[0]
users.close()
- if not self.can_connect(user, sid):
- print 'User \'%s\' is not allowed to connect to \'%s\'' % (user,
sid)
+ if not self.can_connect(user, sid):
+ print 'User \'%s\' is not allowed to connect to \'%s\'' % (user,
sid)
return None, None
- return self.sites[sid].default_user(), self.sites[sid]
+ return self.sites[sid].default_user(), self.sites[sid]
- def is_allowed(self, user, passwd):
- q_access = """
- select count(*) from login where uid = '%s' and `password` = '%s'"""
- logins = db.cursor()
- logins.execute(q_access % (user, passwd))
- login = logins.fetchone()[0]
- logins.close()
- if login:
- self.login = user
- return login
-
def can_connect(self, user, site):
q_group = """
select count(*)
- from
login,login_profile,profile,profile_sgroup,sgroup,sgroup_site,site,user
+ from
+ login,
+ login_profile,
+ profile,
+ profile_sgroup,
+ sgroup,
+ sgroup_site,
+ site,
+ user
where login.uid = '%s'
and login.id = login_profile.login_id
and login_profile.profile_id = profile.id
@@ -111,9 +199,10 @@
and user.site_id = site.id
and user.uid = '%s'
"""
- group = db.cursor()
- group.execute(q_group % (self.login, site, user))
- gr = group.fetchone()[0]
+ group = db.cursor()
+ group.execute(q_group % (self.login, site, user))
+ gr = group.fetchone()[0]
print 'requete can_connect: %d' % gr
- group.close()
- return gr
+ group.close()
+ return gr
+