VirtualMailManager/Handler.py
branchv0.6.x
changeset 290 e2785e04f92e
parent 285 d30a94f5aef5
child 293 a77b67673aa6
equal deleted inserted replaced
289:142f188f7552 290:e2785e04f92e
    68         self._Cfg = None
    68         self._Cfg = None
    69         self._dbh = None
    69         self._dbh = None
    70 
    70 
    71         if os.geteuid():
    71         if os.geteuid():
    72             raise NotRootError(_(u"You are not root.\n\tGood bye!\n"),
    72             raise NotRootError(_(u"You are not root.\n\tGood bye!\n"),
    73                 ERR.CONF_NOPERM)
    73                                ERR.CONF_NOPERM)
    74         if self.__chkCfgFile():
    74         if self.__chkCfgFile():
    75             self._Cfg = Cfg(self._cfgFileName)
    75             self._Cfg = Cfg(self._cfgFileName)
    76             self._Cfg.load()
    76             self._Cfg.load()
    77         if not skip_some_checks:
    77         if not skip_some_checks:
    78             self._Cfg.check()
    78             self._Cfg.check()
    83             tmp = os.path.join(path, 'vmm.cfg')
    83             tmp = os.path.join(path, 'vmm.cfg')
    84             if os.path.isfile(tmp):
    84             if os.path.isfile(tmp):
    85                 self._cfgFileName = tmp
    85                 self._cfgFileName = tmp
    86                 break
    86                 break
    87         if not len(self._cfgFileName):
    87         if not len(self._cfgFileName):
    88             raise VMMError(
    88             raise VMMError(_(u"No 'vmm.cfg' found in: "
    89                 _(u"No “vmm.cfg” found in: /root:/usr/local/etc:/etc"),
    89                              u"/root:/usr/local/etc:/etc"), ERR.CONF_NOFILE)
    90                 ERR.CONF_NOFILE)
       
    91 
    90 
    92     def __chkCfgFile(self):
    91     def __chkCfgFile(self):
    93         """Checks the configuration file, returns bool"""
    92         """Checks the configuration file, returns bool"""
    94         self.__findCfgFile()
    93         self.__findCfgFile()
    95         fstat = os.stat(self._cfgFileName)
    94         fstat = os.stat(self._cfgFileName)
    96         fmode = int(oct(fstat.st_mode & 0777))
    95         fmode = int(oct(fstat.st_mode & 0777))
    97         if fmode % 100 and fstat.st_uid != fstat.st_gid or \
    96         if fmode % 100 and fstat.st_uid != fstat.st_gid or \
    98            fmode % 10 and fstat.st_uid == fstat.st_gid:
    97            fmode % 10 and fstat.st_uid == fstat.st_gid:
    99             raise PermissionError(_(u"wrong permissions for '%(file)s': \
    98             raise PermissionError(_(u"wrong permissions for '%(file)s': "
   100 %(perms)s\n`chmod 0600 %(file)s` would be great.") %
    99                                     u"%(perms)s\n`chmod 0600 %(file)s` would "
   101                                   {'file': self._cfgFileName, 'perms': fmode},
   100                                     u"be great.") % {'file': self._cfgFileName,
   102                                   ERR.CONF_WRONGPERM)
   101                                   'perms': fmode}, ERR.CONF_WRONGPERM)
   103         else:
   102         else:
   104             return True
   103             return True
   105 
   104 
   106     def _chkenv(self):
   105     def _chkenv(self):
   107         """"""
   106         """"""
   110             old_umask = os.umask(0006)
   109             old_umask = os.umask(0006)
   111             os.makedirs(basedir, 0771)
   110             os.makedirs(basedir, 0771)
   112             os.chown(basedir, 0, 0)
   111             os.chown(basedir, 0, 0)
   113             os.umask(old_umask)
   112             os.umask(old_umask)
   114         elif not os.path.isdir(basedir):
   113         elif not os.path.isdir(basedir):
   115             raise VMMError(_(u'“%s” is not a directory.\n\
   114             raise VMMError(_(u"'%s' is not a directory.\n(vmm.cfg: section "
   116 (vmm.cfg: section "misc", option "base_directory")') %
   115                              u"'misc', option 'base_directory')") % basedir,
   117                                  basedir, ERR.NO_SUCH_DIRECTORY)
   116                            ERR.NO_SUCH_DIRECTORY)
   118         for opt, val in self._Cfg.items('bin'):
   117         for opt, val in self._Cfg.items('bin'):
   119             try:
   118             try:
   120                 exec_ok(val)
   119                 exec_ok(val)
   121             except VMMError, e:
   120             except VMMError, err:
   122                 if e.code is ERR.NO_SUCH_BINARY:
   121                 if err.code is ERR.NO_SUCH_BINARY:
   123                     raise VMMError(_(u'“%(binary)s” doesn\'t exist.\n\
   122                     raise VMMError(_(u"'%(binary)s' doesn't exist.\n(vmm.cfg: "
   124 (vmm.cfg: section "bin", option "%(option)s")') %
   123                                      u"section 'bin', option '%(option)s')") %
   125                                        {'binary': val, 'option': opt},
   124                                    {'binary': val, 'option': opt}, err.code)
   126                                        ERR.NO_SUCH_BINARY)
   125                 elif err.code is ERR.NOT_EXECUTABLE:
   127                 elif e.code is ERR.NOT_EXECUTABLE:
   126                     raise VMMError(_(u"'%(binary)s' is not executable.\n"
   128                     raise VMMError(_(u'“%(binary)s” is not executable.\
   127                                      u"(vmm.cfg: section 'bin', option "
   129 \n(vmm.cfg: section "bin", option "%(option)s")') %
   128                                      u"'%(option)s')") % {'binary': val,
   130                                        {'binary': val, 'option': opt},
   129                                    'option': opt}, err.code)
   131                                        ERR.NOT_EXECUTABLE)
       
   132                 else:
   130                 else:
   133                     raise
   131                     raise
   134 
   132 
   135     def __dbConnect(self):
   133     def __dbConnect(self):
   136         """Creates a pyPgSQL.PgSQL.connection instance."""
   134         """Creates a pyPgSQL.PgSQL.connection instance."""
   486         from VirtualMailManager.Domain import search
   484         from VirtualMailManager.Domain import search
   487         like = False
   485         like = False
   488         if pattern and (pattern.startswith('%') or pattern.endswith('%')):
   486         if pattern and (pattern.startswith('%') or pattern.endswith('%')):
   489             like = True
   487             like = True
   490             if not re.match(RE_DOMAIN_SEARCH, pattern.strip('%')):
   488             if not re.match(RE_DOMAIN_SEARCH, pattern.strip('%')):
   491                 raise VMMError(
   489                 raise VMMError(_(u"The pattern '%s' contains invalid "
   492                         _(u"The pattern '%s' contains invalid characters.") %
   490                                  u"characters.") % pattern, ERR.DOMAIN_INVALID)
   493                                pattern, ERR.DOMAIN_INVALID)
       
   494         self.__dbConnect()
   491         self.__dbConnect()
   495         return search(self._dbh, pattern=pattern, like=like)
   492         return search(self._dbh, pattern=pattern, like=like)
   496 
   493 
   497     def user_add(self, emailaddress, password):
   494     def user_add(self, emailaddress, password):
   498         """Wrapper around Account.set_password() and Account.save()."""
   495         """Wrapper around Account.set_password() and Account.save()."""
   512         if warnings:
   509         if warnings:
   513             self.__warnings.append(_('Ignored destination addresses:'))
   510             self.__warnings.append(_('Ignored destination addresses:'))
   514             self.__warnings.extend(('  * %s' % w for w in warnings))
   511             self.__warnings.extend(('  * %s' % w for w in warnings))
   515         for destination in destinations:
   512         for destination in destinations:
   516             if get_gid(self._dbh, destination.domainname) and \
   513             if get_gid(self._dbh, destination.domainname) and \
   517               not self._chk_other_address_types(destination, TYPE_RELOCATED):
   514                not self._chk_other_address_types(destination, TYPE_RELOCATED):
   518                 self.__warnings.append(
   515                 self.__warnings.append(_(u"The destination account/alias '%s' "
   519                     _(u"The destination account/alias '%s' doesn't exist.") %
   516                                          u"doesn't exist.") % destination)
   520                                        destination)
       
   521 
   517 
   522     def user_delete(self, emailaddress, force=None):
   518     def user_delete(self, emailaddress, force=None):
   523         """Wrapper around Account.delete(...)"""
   519         """Wrapper around Account.delete(...)"""
   524         if force not in (None, 'delalias'):
   520         if force not in (None, 'delalias'):
   525             raise VMMError(_(u"Invalid argument: '%s'") % force,
   521             raise VMMError(_(u"Invalid argument: '%s'") % force,
   641         acc.disable(service)
   637         acc.disable(service)
   642 
   638 
   643     def user_enable(self, emailaddress, service=None):
   639     def user_enable(self, emailaddress, service=None):
   644         """Wrapper for Account.enable(service)"""
   640         """Wrapper for Account.enable(service)"""
   645         if service not in (None, 'all', 'imap', 'pop3', 'smtp', 'sieve'):
   641         if service not in (None, 'all', 'imap', 'pop3', 'smtp', 'sieve'):
   646             raise VMMError(_(u"could not accept service: '%s'") % service,
   642             raise VMMError(_(u"Could not accept service: '%s'") % service,
   647                            ERR.INVALID_AGUMENT)
   643                            ERR.INVALID_AGUMENT)
   648         acc = self.__getAccount(emailaddress)
   644         acc = self.__getAccount(emailaddress)
   649         if not acc:
   645         if not acc:
   650             raise VMMError(_(u"The account '%s' doesn't exist.") %
   646             raise VMMError(_(u"The account '%s' doesn't exist.") %
   651                            acc.address, ERR.NO_SUCH_ACCOUNT)
   647                            acc.address, ERR.NO_SUCH_ACCOUNT)