import textwrap
import copy
import shutil
+import stat
# Third party modules
import six
from .ldap_app import PpLdapAppError, PpLdapApplication
-__version__ = '0.3.5'
+__version__ = '0.3.6'
LOG = logging.getLogger(__name__)
default_chroot_homedir = os.sep + os.path.join('mnt', 'nfs')
# /home
default_home_root = os.sep + 'home'
+ # /etc/skel
+ default_skel_dir = os.sep + os.path.join('etc', 'skel')
# -------------------------------------------------------------------------
def __init__(self, appname=None, version=__version__):
self.simulate = False
self.user_entries = []
self.home_root_real = os.path.join(self.chroot_homedir, self.home_root_rel)
+ self.skel_dir = self.default_skel_dir
description = textwrap.dedent('''\
Home Directory and UIDNumber generation - this script will search for
raise PpMkHomeError(msg)
self.home_root_abs = v
+ if 'skel_dir' in section:
+ v = section['skel_dir']
+ if not os.path.isabs(v):
+ msg = (
+ "The skeleton directory must be an "
+ "absolute pathname (found [{s}]/skel_dir "
+ "=> {v!r} in configuration.").format(s=section_name, v=v)
+ raise PpMkHomeError(msg)
+ self.skel_dir = v
+
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)
LOG.error(msg)
self.exit(1)
+ if not os.path.isdir(self.skel_dir):
+ msg = "The skeleton directory {!r} does not exists or is not a directory.".format(
+ self.skel_dir)
+ LOG.error(msg)
+ self.exit(1)
+
if self.simulate:
LOG.warn("Simulation mode - nothing is really done.")
LOG.info("Checking home directories ...")
upper_dir = os.pardir + os.sep
+ home_mode = stat.S_IRWXU
+ el_printed = False
i = 0
for entry in self.user_entries:
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 self.verbose:
- LOG.warn("Home directory {h!r} ({rel!r}) outside {r!r} is not considered.".format(
- h=home, rel=home_relative, r=self.home_root_abs))
+ LOG.warn("Home directory {h!r} outside {r!r} is not considered.".format(
+ h=home, r=self.home_root_abs))
+ el_printed = False
continue
chroot_dir = os.path.join(
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.info("Creating home directory {!r} ....".format(chroot_dir))
+ if not el_printed:
+ if self.verbose:
+ print("")
+ el_printed = True
+ uid = entry['uidNumber'][0]
+ gid = entry['gidNumber'][0]
+ user_name = entry['uid'][0]
- if i >= 5:
+ 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("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)
+
+ if self.verbose:
+ print("")
+
+ if i >= 2:
break
# =============================================================================