from .ldap_app import PpLdapAppError, PpLdapApplication
-__version__ = '0.4.2'
+__version__ = '0.4.3'
LOG = logging.getLogger(__name__)
self.home_root_rel = os.path.relpath(self.home_root_abs, os.sep)
self.simulate = False
self.user_entries = []
+ self.users = {}
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
raise PpMkHomeError(msg)
self.skel_dir = v
+ if 'dn_counter' in section:
+ self.dn_counter = section['dn_counter'].strip()
+
self.home_root_rel = os.path.relpath(self.home_root_abs, os.sep)
self.home_root_real = os.path.join(self.chroot_homedir, self.home_root_rel)
self.user_entries = self.ldap_search_subtree(person, query_filter)
LOG.debug("Found {} LDAP entries.".format(len(self.user_entries)))
+ for entry in self.user_entries:
+ dn = entry.entry_dn
+ self.users[dn] = {
+ 'uid': entry['uid'][0],
+ 'uidNumber': entry['uidNumber'][0],
+ 'gidNumber': entry['gidNumber'][0],
+ 'homeDirectory': entry['homeDirectory'][0],
+ }
+
# -------------------------------------------------------------------------
def set_new_counter(self, new_uid):
LOG.info("Checking UID's for new Users ...")
uid_counter = self.get_numeric_uid(self.dn_counter)
+ if uid_counter is None:
+ LOG.error("Did not found current numeric UID of the counter.")
+ self.exit(5)
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]
- dn = entry.entry_dn
+ for dn in self.users.keys():
+
+ user = self.users[dn]
+
+ uid = user['uidNumber']
+ gid = user['gidNumber']
+ user_name = user['uid']
+ home = user['homeDirectory']
- if cur_uid == self.initial_uid:
+ if uid == self.initial_uid:
i += 1
new_uid = uid_counter + 1
# Setting uid of the counter
self.set_new_counter(new_uid)
+ user['uidNumber'] = new_uid
+
if self.verbose:
print('')
if i:
if i > 1:
- LOG.debug("Total {} numeric user Ids set.".format(i))
+ LOG.info("Total {} numeric user Ids set.".format(i))
else:
- LOG.debug("Total one numeric user Id set.")
+ LOG.info("Total one numeric user Id set.")
else:
- LOG.debug("No numeric user Ids set.")
-
+ LOG.info("No numeric user Ids set.")
if self.verbose:
print('')
home_mode = stat.S_IRWXU
el_printed = False
- i = 0
- for entry in self.user_entries:
+ created = 0
- i += 1
- if self.verbose > 3:
- LOG.debug("Found {c}:\n{e}".format(c=entry.__class__.__name__, e=pp(entry)))
- dn = entry.entry_dn
- LOG.debug("Checking home of {!r} ...".format(dn))
+ for dn in sorted(self.users.keys(), key=str.lower):
- try:
- home_dir = entry['homeDirectory']
- except LDAPKeyError as e:
- LOG.debug("LDAP user {!r} has no home directory.".format(dn))
+ user = self.users[dn]
- home = entry['homeDirectory'][0]
- LOG.debug("Checking home directory {!r} ...".format(home))
+ uid = user['uidNumber']
+ gid = user['gidNumber']
+ user_name = user['uid']
+ home = user['homeDirectory']
+
+ LOG.debug("Checking home directory {h!r} of {d!r} ...".format(
+ h=home, d=dn))
if not os.path.isabs(home):
LOG.warn("Home directory {h!r} of user {u!r} is not absolute.".format(
h=home, u=dn))
continue
home_relative = os.path.relpath(home, self.home_root_abs)
if home_relative.startswith(upper_dir):
- if self.verbose:
- LOG.warn("Home directory {h!r} outside {r!r} is not considered.".format(
+ if self.verbose > 1:
+ LOG.debug("Home directory {h!r} outside {r!r} is not considered.".format(
h=home, r=self.home_root_abs))
el_printed = False
continue
if self.verbose:
print("")
el_printed = True
- uid = entry['uidNumber'][0]
- gid = entry['gidNumber'][0]
- user_name = entry['uid'][0]
+ created += 1
LOG.info("Creating home directory {!r} ....".format(chroot_dir))
LOG.debug("Copying recursive {s!r} to {c!r} ....".format(
s=self.skel_dir, c=chroot_dir))
if self.verbose:
print("")
- if i >= 2:
- break
+ if self.verbose:
+ print('')
+ if created:
+ if created > 1:
+ LOG.info("Total {} home directories created.".format(created))
+ else:
+ LOG.info("Total one home directory created.")
+ else:
+ LOG.info("No home directories created.")
# =============================================================================