introduced LdapUserDirError exception

instead of returning an empty string for a systemuser2dn lookup, now a
LdapUserDirError is returned.
This commit is contained in:
2012-12-07 21:59:51 +01:00
parent 65d13458be
commit c74b73594c
3 changed files with 24 additions and 11 deletions

View File

@@ -1,7 +1,7 @@
#!/usr/bin/python
import logging
from ldapuserdir import LdapUserDir, __version__ as libversion
from ldapuserdir import LdapUserDir, __version__ as libversion, LdapUserDirError
import ldap
import sys
import os
@@ -312,8 +312,12 @@ try:
elif mode == 'userlist':
l.list_users_etcpwd(userfilter, verbose = flag_verbose)
elif mode == 'user_to_group':
sys.stdout.write("\n".join(l.get_groups_for_user(user_to_group))
+ "\n")
try:
sys.stdout.write("\n".join(l.get_groups_for_user(user_to_group))
+ "\n")
except LdapUserDirError, err:
sys.stderr.write('Error: ' + str(err) +'\n')
sys.exit(1)
elif mode == 'add':
l.add_groupmembers(group, usernames)
elif mode == 'del':
@@ -323,8 +327,8 @@ except ldap.INVALID_CREDENTIALS, e:
except ldap.LDAPError, e:
sys.stderr.write('Unhandled LDAP error: %s\n' % str(e))
sys.exit(1)
except:
sys.stderr.write('Unhandled Exception!!!!!!!!\n')
raise
except Exception, err:
sys.stderr.write('Unhandled Exception (%s): %s\n' % (type(err), str(err)))
sys.exit(1)
sys.exit(0)

View File

@@ -1,2 +1,2 @@
from ldapuserdir import LdapUserDir
from ldapuserdir import LdapUserDir, LdapUserDirError
from version import __version__

View File

@@ -16,6 +16,7 @@ import os
import sys
import re
import logging
import time
##############################################################
# definitions of the search strings
@@ -35,6 +36,11 @@ searches_nomssfu= dict({
'get_groups_for_user' : '(&(objectClass=Group)(cn=%s)(member=%s))'})
##############################################################
class LdapUserDirError(Exception):
def __init__(self, errmsg):
super(LdapUserDirError, self).__init__(errmsg)
class LdapUserDir(object):
""" A class to interact with a LDAP based user and group directory
"""
@@ -111,6 +117,7 @@ class LdapUserDir(object):
self._ldap = ldap.initialize(self.serverurl, trace_level=0,
trace_file=sys.stderr)
except ldap.SERVER_DOWN:
self.logger.warning("ldap initialization error")
time.sleep(1)
if attempts >= recon_attempts:
@@ -170,9 +177,9 @@ class LdapUserDir(object):
"""Converts a user's system username to the dn of the ldap directory
by performing a search on ldap
@param uname The system username
@returns The DN of the user or '' if no matching record was found
@returns The DN of the user
@exception may throw an ldap.LDAPError exception
@exception may throw an ldap.LDAPError or LdapUserDir("No such user")
"""
#try:
srch = self.searches['systemuser2dn'] % uname
@@ -180,8 +187,10 @@ class LdapUserDir(object):
r = self.search_s_reconn(self.user_ou, ldap.SCOPE_SUBTREE, srch)
#except ldap.LDAPError, e:
# print e
if len(r) == 0:
return ''
raise LdapUserDirError("No such user")
self.logger.debug('systemuser2dn: dn = %s' % r[0][0])
return r[0][0]