Revisión 150
Añadido por jredrejo hace alrededor de 14 años
LdapConnection.py | ||
---|---|---|
implements(ILdapConnection)
|
||
|
||
def __init__(self,session):
|
||
self.host = ""
|
||
self.user = ""
|
||
self.passwd = ""
|
||
pass
|
||
self.host = ""
|
||
self.user = ""
|
||
self.passwd = ""
|
||
pass
|
||
|
||
def setCredentials(self,host,user,passwd):
|
||
self.host = host
|
||
self.user = user
|
||
self.passwd = passwd
|
||
self.host = host
|
||
self.user = user
|
||
self.passwd = passwd
|
||
|
||
def validation(self):
|
||
if self.host == "":
|
||
return "host"
|
||
if self.host == "":
|
||
return "host"
|
||
|
||
if self.user == "":
|
||
return "user"
|
||
if self.user == "":
|
||
return "user"
|
||
|
||
if self.passwd == "":
|
||
return "password"
|
||
if self.passwd == "":
|
||
return "password"
|
||
|
||
return "OK"
|
||
return "OK"
|
||
|
||
def process(self):
|
||
val = self.validation()
|
||
|
||
if val != "OK":
|
||
return val
|
||
val = self.validation()
|
||
|
||
if val != "OK":
|
||
return val
|
||
|
||
auth = self.connect()
|
||
return auth
|
||
auth = self.connect()
|
||
return auth
|
||
|
||
def connect(self):
|
||
self.connection=ldap.open(self.host)
|
||
try:
|
||
self.connection.simple_bind_s("cn="+self.user+",ou=People,dc=instituto,dc=extremadura,dc=es",self.passwd)
|
||
except ldap.INVALID_CREDENTIALS:
|
||
logging.getLogger().debug('LDAP user or password incorrect')
|
||
return False
|
||
except ldap.LDAPError:
|
||
logging.getLogger().debug('LDAP error connect')
|
||
return False
|
||
self.connection=ldap.open(self.host)
|
||
try:
|
||
self.connection.simple_bind_s("cn="+self.user+",ou=People,dc=instituto,dc=extremadura,dc=es",self.passwd)
|
||
except ldap.INVALID_CREDENTIALS:
|
||
logging.getLogger().debug('LDAP user or password incorrect')
|
||
return False
|
||
except ldap.CONFIDENTIALITY_REQUIRED:
|
||
try:
|
||
#self.connection.set_option(ldap.OPT_X_TLS_DEMAND, True)
|
||
self.connection=ldap.initialize("ldaps://" +self.host)
|
||
self.connection.simple_bind_s("cn="+self.user+",ou=People,dc=instituto,dc=extremadura,dc=es",self.passwd)
|
||
return True
|
||
except ldap.LDAPError,e:
|
||
logging.getLogger().debug('A secure connection with the ldap server could not be established')
|
||
return False
|
||
|
||
except ldap.LDAPError,e:
|
||
logging.getLogger().debug('LDAP error %s' % e.message["desc"])
|
||
return False
|
||
|
||
return True
|
||
return True
|
||
|
||
def getConnect(self):
|
||
return self.connection
|
||
|
||
return self.connection
|
||
|
||
def search(self,baseDN,filter,retrieveAttributes):
|
||
|
||
try:
|
||
ldap_result_id = self.connection.search(baseDN+",dc=instituto,dc=extremadura,dc=es", ldap.SCOPE_SUBTREE, filter, retrieveAttributes)
|
||
result_set = []
|
||
while 1:
|
||
result_type, result_data = self.connection.result(ldap_result_id, 0)
|
||
if (result_data == []):
|
||
break
|
||
else:
|
||
if result_type == ldap.RES_SEARCH_ENTRY:
|
||
result_set.append(result_data)
|
||
return result_set
|
||
except ldap.LDAPError, e:
|
||
logging.getLogger().debug('LDAP error search')
|
||
|
||
"""result = con.search_s( base_dn, ldap.SCOPE_SUBTREE, filter, attrs )
|
||
return result"""
|
||
|
||
try:
|
||
ldap_result_id = self.connection.search(baseDN+",dc=instituto,dc=extremadura,dc=es", ldap.SCOPE_SUBTREE, filter, retrieveAttributes)
|
||
result_set = []
|
||
while 1:
|
||
result_type, result_data = self.connection.result(ldap_result_id, 0)
|
||
if (result_data == []):
|
||
break
|
||
else:
|
||
if result_type == ldap.RES_SEARCH_ENTRY:
|
||
result_set.append(result_data)
|
||
return result_set
|
||
except ldap.LDAPError, e:
|
||
logging.getLogger().debug('LDAP error search')
|
||
|
||
"""result = con.search_s( base_dn, ldap.SCOPE_SUBTREE, filter, attrs )
|
||
return result"""
|
||
|
||
def add(self,baseDN,attr):
|
||
try:
|
||
self.connection.add_s(baseDN+",dc=instituto,dc=extremadura,dc=es", attr)
|
||
|
||
except ldap.ALREADY_EXISTS:
|
||
logging.getLogger().debug("LDAP already exists %s" % (baseDN))
|
||
except ldap.OPERATIONS_ERROR:
|
||
logging.getLogger().debug("LDAP operation error %s" % (baseDN))
|
||
except ldap.NO_SUCH_OBJECT:
|
||
logging.getLogger().debug("LDAP no such object %s" % (baseDN))
|
||
|
||
return True
|
||
try:
|
||
self.connection.add_s(baseDN+",dc=instituto,dc=extremadura,dc=es", attr)
|
||
|
||
except ldap.ALREADY_EXISTS:
|
||
logging.getLogger().debug("LDAP already exists %s" % (baseDN))
|
||
except ldap.OPERATIONS_ERROR:
|
||
logging.getLogger().debug("LDAP operation error %s" % (baseDN))
|
||
except ldap.NO_SUCH_OBJECT:
|
||
logging.getLogger().debug("LDAP no such object %s" % (baseDN))
|
||
|
||
return True
|
||
|
||
def modify(self,baseDN,attr):
|
||
try:
|
||
self.connection.modify_s(baseDN+",dc=instituto,dc=extremadura,dc=es", attr)
|
||
|
||
except ldap.OPERATIONS_ERROR:
|
||
print "error"
|
||
except ldap.NO_SUCH_OBJECT:
|
||
print "no_such_object"
|
||
|
||
return True
|
||
|
||
try:
|
||
self.connection.modify_s(baseDN+",dc=instituto,dc=extremadura,dc=es", attr)
|
||
|
||
except ldap.OPERATIONS_ERROR:
|
||
print "error"
|
||
except ldap.NO_SUCH_OBJECT:
|
||
print "no_such_object"
|
||
|
||
return True
|
||
|
||
def delete(self,baseDN):
|
||
try:
|
||
self.connection.delete_s(baseDN+",dc=instituto,dc=extremadura,dc=es")
|
||
|
||
except ldap.OPERATIONS_ERROR:
|
||
print "error"
|
||
except ldap.NO_SUCH_OBJECT:
|
||
print "no_such_object"
|
||
|
||
return True
|
||
|
||
try:
|
||
self.connection.delete_s(baseDN+",dc=instituto,dc=extremadura,dc=es")
|
||
|
||
except ldap.OPERATIONS_ERROR:
|
||
print "error"
|
||
except ldap.NO_SUCH_OBJECT:
|
||
print "no_such_object"
|
||
|
||
return True
|
||
|
||
"""def searchClassroomComputers(self,classroom):
|
||
|
||
''' How many groups? '''
|
||
base_dn = 'cn=THINCLIENTS,cn=DHCP Config,dc=instituto,dc=extremadura,dc=es'
|
||
filter = '(cn=group*)'
|
||
attrs = ['cn']
|
||
groups = self.search(self,base_dn,filter,attrs)
|
||
''' How many groups? '''
|
||
base_dn = 'cn=THINCLIENTS,cn=DHCP Config,dc=instituto,dc=extremadura,dc=es'
|
||
filter = '(cn=group*)'
|
||
attrs = ['cn']
|
||
groups = self.search(self,base_dn,filter,attrs)
|
||
|
||
numberDesktops=0;
|
||
for i in range(0,len(groups)):
|
||
''' search computers of different groups '''
|
||
base_dn = 'cn='+groups[i][0]['cn'][0]+',cn=THINCLIENTS,cn=DHCP Config,dc=instituto,dc=extremadura,dc=es'
|
||
filter = '(cn='+classroom+'-o*)'
|
||
attrs = ['cn','dhcpHWAddress']
|
||
computers = self.search(self,base_dn,filter,attrs)
|
||
numberDesktops=0;
|
||
for i in range(0,len(groups)):
|
||
''' search computers of different groups '''
|
||
base_dn = 'cn='+groups[i][0]['cn'][0]+',cn=THINCLIENTS,cn=DHCP Config,dc=instituto,dc=extremadura,dc=es'
|
||
filter = '(cn='+classroom+'-o*)'
|
||
attrs = ['cn','dhcpHWAddress']
|
||
computers = self.search(self,base_dn,filter,attrs)
|
||
|
||
for j in range(0,len(computers)):
|
||
self.Desktops[numberDesktops] = {'desktop':computers[j][1]['cn'][0] , 'mac':computers[j][1]['dhcpHWAddress'][0]}
|
||
numberDesktops = numberDesktops + 1"""
|
||
for j in range(0,len(computers)):
|
||
self.Desktops[numberDesktops] = {'desktop':computers[j][1]['cn'][0] , 'mac':computers[j][1]['dhcpHWAddress'][0]}
|
||
numberDesktops = numberDesktops + 1"""
|
Exportar a: Unified diff
Añadida autenticación con ldaps (cambiada indentación en este fichero a 4 espacios en lugar de tabulaciones)