from .ldap_app import PpLdapAppError, PpLdapApplication
-__version__ = '0.4.7'
+__version__ = '0.5.1'
LOG = logging.getLogger(__name__)
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
+ self.el_printed = False
description = textwrap.dedent('''\
Home Directory and UIDNumber generation - this script will search for
print('')
# -------------------------------------------------------------------------
- def check_home_dirs(self):
+ def _check_home_dir(self, dn, upper_dir, home_mode=stat.S_IRWXU):
- LOG.info("Checking home directories ...")
- upper_dir = os.pardir + os.sep
- home_mode = stat.S_IRWXU
- el_printed = False
+ user = self.users[dn]
- created = 0
+ uid = user['uidNumber']
+ gid = user['gidNumber']
+ user_name = user['uid']
+ home = user['homeDirectory']
- for dn in sorted(self.users.keys(), key=str.lower):
+ 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))
+ self.el_printed = False
+ return False
- user = self.users[dn]
+ home_relative = os.path.relpath(home, self.home_root_abs)
+ if home_relative.startswith(upper_dir):
+ if self.verbose > 1:
+ LOG.debug("Home directory {h!r} outside {r!r} is not considered.".format(
+ h=home, r=self.home_root_abs))
+ self.el_printed = False
+ return False
+
+ chroot_dir = os.path.join(self.chroot_homedir, os.path.relpath(home, os.sep))
+ if self.verbose > 1:
+ LOG.debug("Checking existence of {!r} ...".format(chroot_dir))
+ if os.path.exists(chroot_dir):
+ if os.path.isdir(chroot_dir):
+ if self.verbose > 2:
+ LOG.debug("Directory {!r} is already existing.".format(chroot_dir))
+ else:
+ LOG.error("Directory {!r} exists, but is NOT a directory.".format(chroot_dir))
+ self.el_printed = False
+ return False
- 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))
- el_printed = False
- continue
- home_relative = os.path.relpath(home, self.home_root_abs)
- if home_relative.startswith(upper_dir):
+ if not self.el_printed:
+ if self.verbose:
+ print("")
+ self.el_printed = True
+
+ 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 not self.simulate:
+ shutil.copytree(self.skel_dir, chroot_dir, symlinks=True)
+
+ LOG.debug("Chowning recursive {c!r} to {u}:{g} (user {n!r}) ...".format(
+ c=chroot_dir, u=uid, g=gid, n=user_name))
+
+ if not self.simulate:
+ for root, dirs, files in os.walk(chroot_dir):
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
+ LOG.debug("Chowning {!r} ...".format(root))
+ os.chown(root, uid, gid, follow_symlinks=False)
+ for file_name in files:
+ fname_abs = os.path.join(root, file_name)
+ if self.verbose > 1:
+ LOG.debug("Chowning {!r} ...".format(fname_abs))
+ os.chown(fname_abs, uid, gid, follow_symlinks=False)
- chroot_dir = os.path.join(
- self.chroot_homedir, os.path.relpath(home, os.sep))
- if self.verbose > 1:
- LOG.debug("Checking existence of {!r} ...".format(chroot_dir))
-
- if os.path.exists(chroot_dir):
- if os.path.isdir(chroot_dir):
- if self.verbose > 2:
- LOG.debug("Directory {!r} is already existing.".format(chroot_dir))
- else:
- LOG.error("Directory {!r} exists, but is NOT a directory.".format(chroot_dir))
- el_printed = False
- continue
+ LOG.debug("Setting permissions of {h!r} to {p:04o} ...".format(h=chroot_dir, p=home_mode))
+ if not self.simulate:
+ os.chmod(chroot_dir, home_mode)
- if not el_printed:
- if self.verbose:
- print("")
- el_printed = True
+ if self.verbose:
+ print("")
- 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))
+ return True
- if not self.simulate:
- shutil.copytree(self.skel_dir, chroot_dir, symlinks=True)
+ # -------------------------------------------------------------------------
+ def check_home_dirs(self):
- LOG.debug("Chowning recursive {c!r} to {u}:{g} (user {n!r}) ...".format(
- c=chroot_dir, u=uid, g=gid, n=user_name))
+ LOG.info("Checking home directories ...")
+ upper_dir = os.pardir + os.sep
+ home_mode = stat.S_IRWXU
+ self.el_printed = False
- if not self.simulate:
- for root, dirs, files in os.walk(chroot_dir):
- if self.verbose > 1:
- LOG.debug("Chowning {!r} ...".format(root))
- os.chown(root, uid, gid, follow_symlinks=False)
- for file_name in files:
- fname_abs = os.path.join(root, file_name)
- if self.verbose > 1:
- LOG.debug("Chowning {!r} ...".format(fname_abs))
- os.chown(fname_abs, uid, gid, follow_symlinks=False)
-
- LOG.debug("Setting permissions of {h!r} to {p:04o} ...".format(
- h=chroot_dir, p=home_mode))
- if not self.simulate:
- os.chmod(chroot_dir, home_mode)
+ created = 0
- if self.verbose:
- print("")
+ for dn in sorted(self.users.keys(), key=str.lower):
+ if self._check_home_dir(dn, upper_dir, home_mode):
+ created += 1
if self.verbose:
print('')