diff --git a/LdapServer.py b/LdapServer.py index 0f5e0c2..81258f9 100644 --- a/LdapServer.py +++ b/LdapServer.py @@ -1,6 +1,5 @@ #!/usr/bin/python -import sys import ldap import ldap.modlist as modlist import logging @@ -14,13 +13,20 @@ class LdapServer(object): con = 0 - def __init__(self,uri,dn=None,pwd=None,v2=None): + def __init__(self,uri,dn=None,pwd=None,v2=None,raiseOnError=False): self.uri = uri self.dn = dn self.pwd = pwd + self.raiseOnError = raiseOnError if v2: self.v2=True + def _error(self,error,level=logging.WARNING): + if self.raiseOnError: + raise 'LdapServer - Error connecting and binding to LDAP server : %s' % e + else: + logging.log(level,error) + def connect(self): if self.con == 0: try: @@ -34,9 +40,11 @@ class LdapServer(object): con.simple_bind_s(self.dn,self.pwd) self.con = con + return True except ldap.LDAPError, e: - logging.critical('LdapServer - Error connecting and binding to LDAP server : %s' % e) - sys.exit(1) + self._error('LdapServer - Error connecting and binding to LDAP server : %s' % e,logging.CRITICAL) + return False + return True def search(self,basedn,filter,attrs,sizelimit=0): res_id = self.con.search(basedn,ldap.SCOPE_SUBTREE,filter,attrs) @@ -59,21 +67,19 @@ class LdapServer(object): self.con.add_s(dn,ldif) return True except ldap.LDAPError, e: - logging.warning("LdapServer - Error adding %s : %s" % (dn,e)) + self._error("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)) + self._error("LdapServer - Error updating %s : %s" % (dn,e)) return False def drop_object(self,dn): @@ -82,7 +88,7 @@ class LdapServer(object): self.con.delete_s(dn) return True except ldap.LDAPError, e: - logging.warning("LdapServer - Error deleting %s : %s" % (dn,e)) + self._error("LdapServer - Error deleting %s : %s" % (dn,e)) return False