import ldap3
# ldap3 classes and objects
-from ldap3 import Server, ServerPool, Connection, Reader
+from ldap3 import Server, ServerPool, Connection, Reader, ObjectDef
# ldap3 constants
from ldap3 import IP_V4_PREFERRED, ROUND_ROBIN, AUTO_BIND_NONE, ALL_ATTRIBUTES
from ldap3 import SUBTREE
from .cfg_app import PpCfgAppError, PpConfigApplication
-__version__ = '0.4.1'
+__version__ = '0.4.3'
LOG = logging.getLogger(__name__)
cursor.search()
return cursor.entries
+ # -------------------------------------------------------------------------
+ def ldap_search_object(self, obj_def, object_dn, base=None):
+
+ if base is None:
+ base = self.ldap_base_dn
+
+ cursor = Reader(
+ self.ldap_connection,
+ object_def=obj_def, base=base)
+
+ if self.verbose > 1:
+ LOG.debug("LDAP-Reader:\n{}".format(cursor))
+
+ cursor.search_object(entry_dn=object_dn)
+ return cursor.entries
+
+ # -------------------------------------------------------------------------
+ def get_numeric_uid(self, dn, base=None):
+
+ person = ObjectDef(['posixAccount', 'shadowAccount'])
+ person += ["uid", "uidNumber", "gidNumber"]
+
+ entries = self.ldap_search_object(person, dn)
+ LOG.debug("Found {} LDAP entries.".format(len(entries)))
+
+ if not entries:
+ LOG.error("No LDAP entry found for DN {!r}.".format(dn))
+ return None
+
+ entry = entries[0]
+
+ uid = entry['uidNumber'][0]
+ return uid
+
# -------------------------------------------------------------------------
def post_run(self):
"""
from .ldap_app import PpLdapAppError, PpLdapApplication
-__version__ = '0.3.6'
+__version__ = '0.4.1'
LOG = logging.getLogger(__name__)
default_home_root = os.sep + 'home'
# /etc/skel
default_skel_dir = os.sep + os.path.join('etc', 'skel')
+ default_dn_counter = 'uid=uidNumber,ou=ldapTool,ou=Applications,o=Pixelpark,o=isp'
# -------------------------------------------------------------------------
def __init__(self, appname=None, version=__version__):
self.user_entries = []
self.home_root_real = os.path.join(self.chroot_homedir, self.home_root_rel)
self.skel_dir = self.default_skel_dir
+ self.dn_counter = self.default_dn_counter
description = textwrap.dedent('''\
Home Directory and UIDNumber generation - this script will search for
LOG.info("Checking UID's for new Users ...")
+ uid_counter = self.get_numeric_uid(self.dn_counter)
+ LOG.debug("Current UID counter: {}".format(uid_counter))
+
+ el_printed = False
+
+ i = 0
+ for entry in self.user_entries:
+
+ cur_uid = entry['uidNumber'][0]
+ user_name = entry['uid'][0]
+
+ if cur_uid == self.initial_uid:
+
+ i += 1
+ new_uid = uid_counter + 1
+ LOG.info("Setting numeric UID of user {n!r} to {u}...".format(
+ n=user_name, u=new_uid))
+
+ uid_counter = self.get_numeric_uid(self.dn_counter)
+
+ if self.verbose:
+ print('')
+ if i:
+ if i > 1:
+ LOG.debug("Total {} numeric user Ids set.".format(i))
+ else:
+ LOG.debug("Total one numeric user Id set.")
+ else:
+ LOG.debug("No numeric user Ids set.")
+
+
+ if self.verbose:
+ print('')
+
# -------------------------------------------------------------------------
def check_home_dirs(self):