Initial commit

This commit is contained in:
Benjamin Renard 2013-06-07 12:13:03 +02:00
commit 9ba2a43a04
3 changed files with 209 additions and 0 deletions

102
LdapServer.py Normal file
View file

@ -0,0 +1,102 @@
#!/usr/bin/python
import sys
import ldap
import ldap.modlist as modlist
import logging
class LdapServer(object):
uri = None
dn = None
pwd = None
v2 = None
con = 0
def __init__(self,uri,dn=None,pwd=None,v2=None):
self.uri = uri
self.dn = dn
self.pwd = pwd
if v2:
self.v2=True
def connect(self):
if self.con == 0:
try:
con = ldap.initialize(self.uri)
if self.v2:
con.protocol_version = ldap.VERSION2
else:
con.protocol_version = ldap.VERSION3
if self.dn:
con.simple_bind_s(self.dn,self.pwd)
self.con = con
except ldap.LDAPError, e:
logging.critical('LdapServer - Error connecting and binding to LDAP server : %s' % e)
sys.exit(1)
def search(self,basedn,filter,attrs,sizelimit=0):
res_id = self.con.search(basedn,ldap.SCOPE_SUBTREE,filter,attrs)
ret = {}
c=0
while 1:
res_type, res_data = self.con.result(res_id,0)
if res_data == [] or sizelimit!=0 and c>sizelimit:
break
else:
if res_type == ldap.RES_SEARCH_ENTRY:
ret[res_data[0][0]]=res_data[0][1]
c=c+1
return ret
def add_object(self,dn,attrs):
ldif = modlist.addModlist(attrs)
try:
logging.debug("LdapServer - Add %s" % dn)
self.con.add_s(dn,ldif)
return True
except ldap.LDAPError, e:
logging.warning("LdapServer - Error adding %s : %s" % (dn,e))
return False
def update_object(self,dn,old,new):
ldif = modlist.modifyModlist(old,new)
if ldif == []:
#logging.debug("LdapServer - No change for %s" % dn)
return True
try:
#logging.debug("LdapServer - Update %s" % dn)
self.con.modify_s(dn,ldif)
return True
except ldap.LDAPError, e:
logging.warning("LdapServer - Error updating %s : %s" % (dn,e))
return False
def drop_object(self,dn):
try:
logging.debug("LdapServer - Delete %s" % dn)
self.con.delete_s(dn)
return True
except ldap.LDAPError, e:
logging.warning("LdapServer - Error deleting %s : %s" % (dn,e))
return False
def get_dn(self,obj):
return obj[0][0]
def get_attr(self,obj,attr,all=None):
if all is not None:
if attr in obj:
return obj[attr]
else:
return []
else:
if attr in obj:
return obj[attr][0]
else:
return None

59
MyDB.py Normal file
View file

@ -0,0 +1,59 @@
#!/usr/bin/python
import MySQLdb
import logging
import sys
class MyDB(object):
host = ""
user = ""
pwd = ""
db = ""
con = 0
def __init__(self,host,user,pwd,db):
self.host = host
self.user = user
self.pwd = pwd
self.db = db
def connect(self):
if self.con == 0:
try:
con = MySQLdb.connect(self.host,self.user,self.pwd,self.db)
self.con = con
except Exception, e:
logging.fatal(e)
sys.exit(1)
def doSQL(self,sql):
cursor = self.con.cursor()
try:
cursor.execute(sql)
self.con.commit()
return True
except Exception, e:
logging.error('Erreur durant la requete sql %s : %s' % (sql,e))
self.con.rollback()
return False
def doSelect(self,sql):
cursor = self.con.cursor()
try:
cursor.execute(sql)
results = cursor.fetchall()
return results
ret=[]
t=0
for row in results:
c=0
for field in row:
ret[t][c]=field
c=c+1
t=t+1
return ret
except Exception, e:
logging.error('Erreur durant la requete sql %s : %s' % (sql,e))
return False

48
PgDB.py Normal file
View file

@ -0,0 +1,48 @@
#!/usr/bin/python
import psycopg2
import logging
import sys
class PgDB(object):
host = ""
user = ""
pwd = ""
db = ""
con = 0
def __init__(self,host,user,pwd,db):
self.host = host
self.user = user
self.pwd = pwd
self.db = db
def connect(self):
if self.con == 0:
try:
con = psycopg2.connect("dbname='%s' user='%s' host='%s' password='%s'" % (self.db,self.user,self.host,self.pwd))
self.con = con
except Exception, e:
logging.fatal(e)
sys.exit(1)
def setEncoding(self,enc):
if self.con:
try:
self.con.set_client_encoding(enc)
return True
except Exception, e:
logging.error(e)
return False
def doSelect(self,sql):
cursor = self.con.cursor()
try:
cursor.execute(sql)
results = cursor.fetchall()
return results
except Exception, e:
logging.error('Erreur durant la requete sql %s : %s' % (sql,e))
return False