Benjamin Renard commited on 2013-06-07 10:13:03
Showing 3 changed files, with 209 additions and 0 deletions.
| ... | ... |
@@ -0,0 +1,102 @@ |
| 1 |
+#!/usr/bin/python |
|
| 2 |
+ |
|
| 3 |
+import sys |
|
| 4 |
+import ldap |
|
| 5 |
+import ldap.modlist as modlist |
|
| 6 |
+import logging |
|
| 7 |
+ |
|
| 8 |
+class LdapServer(object): |
|
| 9 |
+ |
|
| 10 |
+ uri = None |
|
| 11 |
+ dn = None |
|
| 12 |
+ pwd = None |
|
| 13 |
+ v2 = None |
|
| 14 |
+ |
|
| 15 |
+ con = 0 |
|
| 16 |
+ |
|
| 17 |
+ def __init__(self,uri,dn=None,pwd=None,v2=None): |
|
| 18 |
+ self.uri = uri |
|
| 19 |
+ self.dn = dn |
|
| 20 |
+ self.pwd = pwd |
|
| 21 |
+ if v2: |
|
| 22 |
+ self.v2=True |
|
| 23 |
+ |
|
| 24 |
+ def connect(self): |
|
| 25 |
+ if self.con == 0: |
|
| 26 |
+ try: |
|
| 27 |
+ con = ldap.initialize(self.uri) |
|
| 28 |
+ if self.v2: |
|
| 29 |
+ con.protocol_version = ldap.VERSION2 |
|
| 30 |
+ else: |
|
| 31 |
+ con.protocol_version = ldap.VERSION3 |
|
| 32 |
+ |
|
| 33 |
+ if self.dn: |
|
| 34 |
+ con.simple_bind_s(self.dn,self.pwd) |
|
| 35 |
+ |
|
| 36 |
+ self.con = con |
|
| 37 |
+ except ldap.LDAPError, e: |
|
| 38 |
+ logging.critical('LdapServer - Error connecting and binding to LDAP server : %s' % e)
|
|
| 39 |
+ sys.exit(1) |
|
| 40 |
+ |
|
| 41 |
+ def search(self,basedn,filter,attrs,sizelimit=0): |
|
| 42 |
+ res_id = self.con.search(basedn,ldap.SCOPE_SUBTREE,filter,attrs) |
|
| 43 |
+ ret = {}
|
|
| 44 |
+ c=0 |
|
| 45 |
+ while 1: |
|
| 46 |
+ res_type, res_data = self.con.result(res_id,0) |
|
| 47 |
+ if res_data == [] or sizelimit!=0 and c>sizelimit: |
|
| 48 |
+ break |
|
| 49 |
+ else: |
|
| 50 |
+ if res_type == ldap.RES_SEARCH_ENTRY: |
|
| 51 |
+ ret[res_data[0][0]]=res_data[0][1] |
|
| 52 |
+ c=c+1 |
|
| 53 |
+ return ret |
|
| 54 |
+ |
|
| 55 |
+ def add_object(self,dn,attrs): |
|
| 56 |
+ ldif = modlist.addModlist(attrs) |
|
| 57 |
+ try: |
|
| 58 |
+ logging.debug("LdapServer - Add %s" % dn)
|
|
| 59 |
+ self.con.add_s(dn,ldif) |
|
| 60 |
+ return True |
|
| 61 |
+ except ldap.LDAPError, e: |
|
| 62 |
+ logging.warning("LdapServer - Error adding %s : %s" % (dn,e))
|
|
| 63 |
+ |
|
| 64 |
+ return False |
|
| 65 |
+ |
|
| 66 |
+ def update_object(self,dn,old,new): |
|
| 67 |
+ ldif = modlist.modifyModlist(old,new) |
|
| 68 |
+ if ldif == []: |
|
| 69 |
+ #logging.debug("LdapServer - No change for %s" % dn)
|
|
| 70 |
+ return True |
|
| 71 |
+ try: |
|
| 72 |
+ #logging.debug("LdapServer - Update %s" % dn)
|
|
| 73 |
+ self.con.modify_s(dn,ldif) |
|
| 74 |
+ return True |
|
| 75 |
+ except ldap.LDAPError, e: |
|
| 76 |
+ logging.warning("LdapServer - Error updating %s : %s" % (dn,e))
|
|
| 77 |
+ return False |
|
| 78 |
+ |
|
| 79 |
+ def drop_object(self,dn): |
|
| 80 |
+ try: |
|
| 81 |
+ logging.debug("LdapServer - Delete %s" % dn)
|
|
| 82 |
+ self.con.delete_s(dn) |
|
| 83 |
+ return True |
|
| 84 |
+ except ldap.LDAPError, e: |
|
| 85 |
+ logging.warning("LdapServer - Error deleting %s : %s" % (dn,e))
|
|
| 86 |
+ |
|
| 87 |
+ return False |
|
| 88 |
+ |
|
| 89 |
+ def get_dn(self,obj): |
|
| 90 |
+ return obj[0][0] |
|
| 91 |
+ |
|
| 92 |
+ def get_attr(self,obj,attr,all=None): |
|
| 93 |
+ if all is not None: |
|
| 94 |
+ if attr in obj: |
|
| 95 |
+ return obj[attr] |
|
| 96 |
+ else: |
|
| 97 |
+ return [] |
|
| 98 |
+ else: |
|
| 99 |
+ if attr in obj: |
|
| 100 |
+ return obj[attr][0] |
|
| 101 |
+ else: |
|
| 102 |
+ return None |
| ... | ... |
@@ -0,0 +1,59 @@ |
| 1 |
+#!/usr/bin/python |
|
| 2 |
+ |
|
| 3 |
+import MySQLdb |
|
| 4 |
+import logging |
|
| 5 |
+import sys |
|
| 6 |
+ |
|
| 7 |
+class MyDB(object): |
|
| 8 |
+ |
|
| 9 |
+ host = "" |
|
| 10 |
+ user = "" |
|
| 11 |
+ pwd = "" |
|
| 12 |
+ db = "" |
|
| 13 |
+ |
|
| 14 |
+ con = 0 |
|
| 15 |
+ |
|
| 16 |
+ def __init__(self,host,user,pwd,db): |
|
| 17 |
+ self.host = host |
|
| 18 |
+ self.user = user |
|
| 19 |
+ self.pwd = pwd |
|
| 20 |
+ self.db = db |
|
| 21 |
+ |
|
| 22 |
+ def connect(self): |
|
| 23 |
+ if self.con == 0: |
|
| 24 |
+ try: |
|
| 25 |
+ con = MySQLdb.connect(self.host,self.user,self.pwd,self.db) |
|
| 26 |
+ self.con = con |
|
| 27 |
+ except Exception, e: |
|
| 28 |
+ logging.fatal(e) |
|
| 29 |
+ sys.exit(1) |
|
| 30 |
+ |
|
| 31 |
+ def doSQL(self,sql): |
|
| 32 |
+ cursor = self.con.cursor() |
|
| 33 |
+ try: |
|
| 34 |
+ cursor.execute(sql) |
|
| 35 |
+ self.con.commit() |
|
| 36 |
+ return True |
|
| 37 |
+ except Exception, e: |
|
| 38 |
+ logging.error('Erreur durant la requete sql %s : %s' % (sql,e))
|
|
| 39 |
+ self.con.rollback() |
|
| 40 |
+ return False |
|
| 41 |
+ |
|
| 42 |
+ def doSelect(self,sql): |
|
| 43 |
+ cursor = self.con.cursor() |
|
| 44 |
+ try: |
|
| 45 |
+ cursor.execute(sql) |
|
| 46 |
+ results = cursor.fetchall() |
|
| 47 |
+ return results |
|
| 48 |
+ ret=[] |
|
| 49 |
+ t=0 |
|
| 50 |
+ for row in results: |
|
| 51 |
+ c=0 |
|
| 52 |
+ for field in row: |
|
| 53 |
+ ret[t][c]=field |
|
| 54 |
+ c=c+1 |
|
| 55 |
+ t=t+1 |
|
| 56 |
+ return ret |
|
| 57 |
+ except Exception, e: |
|
| 58 |
+ logging.error('Erreur durant la requete sql %s : %s' % (sql,e))
|
|
| 59 |
+ return False |
| ... | ... |
@@ -0,0 +1,48 @@ |
| 1 |
+#!/usr/bin/python |
|
| 2 |
+ |
|
| 3 |
+import psycopg2 |
|
| 4 |
+import logging |
|
| 5 |
+import sys |
|
| 6 |
+ |
|
| 7 |
+class PgDB(object): |
|
| 8 |
+ |
|
| 9 |
+ host = "" |
|
| 10 |
+ user = "" |
|
| 11 |
+ pwd = "" |
|
| 12 |
+ db = "" |
|
| 13 |
+ |
|
| 14 |
+ con = 0 |
|
| 15 |
+ |
|
| 16 |
+ def __init__(self,host,user,pwd,db): |
|
| 17 |
+ self.host = host |
|
| 18 |
+ self.user = user |
|
| 19 |
+ self.pwd = pwd |
|
| 20 |
+ self.db = db |
|
| 21 |
+ |
|
| 22 |
+ def connect(self): |
|
| 23 |
+ if self.con == 0: |
|
| 24 |
+ try: |
|
| 25 |
+ con = psycopg2.connect("dbname='%s' user='%s' host='%s' password='%s'" % (self.db,self.user,self.host,self.pwd))
|
|
| 26 |
+ self.con = con |
|
| 27 |
+ except Exception, e: |
|
| 28 |
+ logging.fatal(e) |
|
| 29 |
+ sys.exit(1) |
|
| 30 |
+ |
|
| 31 |
+ def setEncoding(self,enc): |
|
| 32 |
+ if self.con: |
|
| 33 |
+ try: |
|
| 34 |
+ self.con.set_client_encoding(enc) |
|
| 35 |
+ return True |
|
| 36 |
+ except Exception, e: |
|
| 37 |
+ logging.error(e) |
|
| 38 |
+ return False |
|
| 39 |
+ |
|
| 40 |
+ def doSelect(self,sql): |
|
| 41 |
+ cursor = self.con.cursor() |
|
| 42 |
+ try: |
|
| 43 |
+ cursor.execute(sql) |
|
| 44 |
+ results = cursor.fetchall() |
|
| 45 |
+ return results |
|
| 46 |
+ except Exception, e: |
|
| 47 |
+ logging.error('Erreur durant la requete sql %s : %s' % (sql,e))
|
|
| 48 |
+ return False |
|
| 0 | 49 |