Beispiel #1
0
    def _delete_home(self, domdir, uid, gid):
        """Delete a user's home directory.

        Arguments:

        `domdir` : basestring
          The directory of the domain the user belongs to
          (commonly AccountObj.domain.directory)
        `uid` : int
          The user's UID (commonly AccountObj.uid)
        `gid` : int
          The user's GID (commonly AccountObj.gid)
        """
        assert all(isinstance(xid, int)
                   for xid in (uid, gid)) and isinstance(domdir, str)
        if uid < MIN_UID or gid < MIN_GID:
            raise VMMError(
                _("UID '%(uid)u' and/or GID '%(gid)u' are less "
                  "than %(min_uid)u/%(min_gid)u.") % {
                      "uid": uid,
                      "gid": gid,
                      "min_gid": MIN_GID,
                      "min_uid": MIN_UID
                  },
                MAILDIR_PERM_MISMATCH,
            )
        if domdir.count(".."):
            raise VMMError(
                _('Found ".." in domain directory path: %s') % domdir,
                FOUND_DOTS_IN_PATH,
            )
        if not lisdir(domdir):
            raise VMMError(
                _("No such directory: %s") % domdir, NO_SUCH_DIRECTORY)
        os.chdir(domdir)
        userdir = "%s" % uid
        if not lisdir(userdir):
            self._warnings.append(
                _("No such directory: %s") % os.path.join(domdir, userdir))
            return
        mdstat = os.lstat(userdir)
        if (mdstat.st_uid, mdstat.st_gid) != (uid, gid):
            raise VMMError(
                _("Detected owner/group mismatch in home "
                  "directory."),
                MAILDIR_PERM_MISMATCH,
            )
        rmtree(userdir, ignore_errors=True)
Beispiel #2
0
    def _delete_domain_dir(self, domdir, gid):
        """Delete a domain's directory.

        Arguments:

        `domdir` : basestring
          The domain's directory (commonly DomainObj.directory)
        `gid` : int
          The domain's GID (commonly DomainObj.gid)
        """
        assert isinstance(domdir, str) and isinstance(gid, int)
        if gid < MIN_GID:
            raise VMMError(
                _("GID '%(gid)u' is less than '%(min_gid)u'.") % {
                    "gid": gid,
                    "min_gid": MIN_GID
                },
                DOMAINDIR_GROUP_MISMATCH,
            )
        if domdir.count(".."):
            raise VMMError(
                _('Found ".." in domain directory path: %s') % domdir,
                FOUND_DOTS_IN_PATH,
            )
        if not lisdir(domdir):
            self._warnings.append(_("No such directory: %s") % domdir)
            return
        dirst = os.lstat(domdir)
        if dirst.st_gid != gid:
            raise VMMError(
                _("Detected group mismatch in domain directory: "
                  "%s") % domdir,
                DOMAINDIR_GROUP_MISMATCH,
            )
        rmtree(domdir, ignore_errors=True)
Beispiel #3
0
 def _make_domain_dir(self, domain):
     """Create a directory for the `domain` and its accounts."""
     cwd = os.getcwd()
     hashdir, domdir = domain.directory.split(os.path.sep)[-2:]
     dir_created = False
     os.chdir(self._cfg.dget("misc.base_directory"))
     old_umask = os.umask(0o022)
     if not os.path.exists(hashdir):
         os.mkdir(hashdir, 0o711)
         os.chown(hashdir, 0, 0)
         dir_created = True
     if not dir_created and not lisdir(hashdir):
         raise VMMError(
             _("'%s' is not a directory.") % hashdir, NO_SUCH_DIRECTORY)
     if os.path.exists(domain.directory):
         raise VMMError(
             _("The file/directory '%s' already exists.") %
             domain.directory,
             VMM_ERROR,
         )
     os.mkdir(os.path.join(hashdir, domdir),
              self._cfg.dget("domain.directory_mode"))
     os.chown(domain.directory, 0, domain.gid)
     os.umask(old_umask)
     os.chdir(cwd)
Beispiel #4
0
def is_dir(path):
    """Check if the expanded path is a directory.  When the expanded path
    is a directory the expanded path will be returned.  Otherwise a
    ConfigValueError will be raised.
    """
    path = expand_path(path)
    if lisdir(path):
        return path
    raise ConfigValueError(_("No such directory: %s") % get_unicode(path))
Beispiel #5
0
 def _make_home(self, account):
     """Create a home directory for the new Account *account*."""
     domdir = account.domain.directory
     if not lisdir(domdir):
         self._make_domain_dir(account.domain)
     os.umask(0o007)
     uid = account.uid
     os.chdir(domdir)
     os.mkdir("%s" % uid, self._cfg.dget("account.directory_mode"))
     os.chown("%s" % uid, uid, account.gid)
Beispiel #6
0
 def __init__(self, account):
     """
     Creates a new mailbox instance.
     Use one of the `Maildir`, `SingleDbox` or `MultiDbox` classes.
     """
     assert isinstance(account, Account) and lisdir(account.home)
     self._user = account
     self._boxes = []
     self._root = self._user.mail_location.directory
     self._sep = "/"
     os.chdir(self._user.home)
Beispiel #7
0
    def _get_disk_usage(self, directory):
        """Estimate file space usage for the given directory.

        Arguments:

        `directory` : basestring
          The directory to summarize recursively disk usage for
        """
        if lisdir(directory):
            return (Popen(
                [self._cfg.dget("bin.du"), "-hs", directory],
                stdout=PIPE).communicate()[0].decode().split("\t")[0])
        else:
            self._warnings.append(_("No such directory: %s") % directory)
            return 0
Beispiel #8
0
 def _chkenv(self):
     """Make sure our base_directory is a directory and that all
     required executables exists and are executable.
     If not, a VMMError will be raised"""
     dir_created = False
     basedir = self._cfg.dget("misc.base_directory")
     if not os.path.exists(basedir):
         old_umask = os.umask(0o006)
         os.makedirs(basedir, 0o771)
         os.chown(basedir, 0, 0)
         os.umask(old_umask)
         dir_created = True
     if not dir_created and not lisdir(basedir):
         raise VMMError(
             _("'%(path)s' is not a directory.\n(%(cfg_file)s: "
               "section 'misc', option 'base_directory')") % {
                   "path": basedir,
                   "cfg_file": self._cfg_fname
               },
             NO_SUCH_DIRECTORY,
         )
     for opt, val in self._cfg.items("bin"):
         try:
             exec_ok(val)
         except VMMError as err:
             if err.code in (NO_SUCH_BINARY, NOT_EXECUTABLE):
                 raise VMMError(
                     err.msg + _("\n(%(cfg_file)s: section "
                                 "'bin', option '%(option)s')") % {
                                     "cfg_file": self._cfg_fname,
                                     "option": opt
                                 },
                     err.code,
                 )
             else:
                 raise