import ldap3
# ldap3 classes and objects
-from ldap3 import Server, ServerPool, Connection, Reader, ObjectDef
+from ldap3 import Server, ServerPool, Connection, Reader, Writer, 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.3'
+__version__ = '0.4.4'
LOG = logging.getLogger(__name__)
uid = entry['uidNumber'][0]
return uid
+ # -------------------------------------------------------------------------
+ def set_numeric_uid(self, dn, new_uid, simulate=False, base=None):
+
+ person = ObjectDef(['posixAccount', 'shadowAccount'])
+ person += ["uid", "uidNumber", "gidNumber", 'objectClass']
+
+ if base is None:
+ base = self.ldap_base_dn
+
+ read_cursor = Reader(
+ self.ldap_connection,
+ object_def=person, base=base)
+ read_cursor.search_object(entry_dn=dn)
+
+ if not read_cursor.entries:
+ msg = "Did not found Counter LDAP entry {!r}.".format(dn)
+ raise PpMkHomeError(msg)
+
+ entry = read_cursor.entries[0]
+ if self.verbose:
+ LOG.debug("Found entry:\n{}".format(entry))
+
+ writer_cursor = Writer.from_cursor(read_cursor)
+ entry = writer_cursor.entries[0]
+ entry.uidNumber = new_uid
+ if self.verbose > 1:
+ LOG.debug("Writer entry before commit:\n{}".format(entry))
+
+ LOG.info("Setting numeric user Id of {d!r} to {u} ...".format(
+ d=dn, u=new_uid))
+
+ if not simulate:
+ entry.entry_commit_changes()
+ if self.verbose:
+ LOG.debug("Writer entry after commit:\n{}".format(entry))
+
# -------------------------------------------------------------------------
def post_run(self):
"""
import ldap3
-from ldap3 import ObjectDef, AttrDef
+from ldap3 import ObjectDef, AttrDef, Reader, Writer
from ldap3.core.exceptions import LDAPKeyError
from .ldap_app import PpLdapAppError, PpLdapApplication
-__version__ = '0.4.1'
+__version__ = '0.4.2'
LOG = logging.getLogger(__name__)
self.user_entries = self.ldap_search_subtree(person, query_filter)
LOG.debug("Found {} LDAP entries.".format(len(self.user_entries)))
+ # -------------------------------------------------------------------------
+ def set_new_counter(self, new_uid):
+
+ return self.set_numeric_uid(self.dn_counter, new_uid, simulate=self.simulate)
+
# -------------------------------------------------------------------------
def check_numeric_uids(self):
cur_uid = entry['uidNumber'][0]
user_name = entry['uid'][0]
+ dn = entry.entry_dn
if cur_uid == self.initial_uid:
n=user_name, u=new_uid))
uid_counter = self.get_numeric_uid(self.dn_counter)
+ new_uid = uid_counter + 1
+ # Setting uid of user itself
+ self.set_numeric_uid(dn, new_uid, simulate=self.simulate)
+ # Setting uid of the counter
+ self.set_new_counter(new_uid)
if self.verbose:
print('')