Example #1
0
    def setupLocale(self):
        # set a locale if one has been specified in the config
        if not self.cfg.locale:
            return

        # workaround to allow unicode encoding conversions in DTML
        import codecs
        dummy = codecs.lookup('iso-8859-1')

        locale_id = self.cfg.locale

        if locale_id is not None:
            try:
                import locale
            except:
                raise ZConfig.ConfigurationError(
                    'The locale module could not be imported.\n'
                    'To use localization options, you must ensure\n'
                    'that the locale module is compiled into your\n'
                    'Python installation.'
                    )
            try:
                locale.setlocale(locale.LC_ALL, locale_id)
            except:
                raise ZConfig.ConfigurationError(
                    'The specified locale "%s" is not supported by your'
                    'system.\nSee your operating system documentation for '
                    'more\ninformation on locale support.' % locale_id
                    )
Example #2
0
 def getinfo(self, key):
     if not key:
         raise ZConfig.ConfigurationError(
             "cannot match a key without a name")
     try:
         return self._keymap[key]
     except KeyError:
         raise ZConfig.ConfigurationError("no key matching " + repr(key))
Example #3
0
def dropPrivileges(cfg):
    # Drop root privileges if we have them and we're on a posix platform.
    # This needs to be a function so it may be used outside of Zope
    # appserver startup (e.g. from zopectl debug)
    if os.name != 'posix':
        return

    if os.getuid() != 0:
        return

    import pwd

    effective_user  = cfg.effective_user
    if effective_user is None:
        msg = ('A user was not specified to setuid to; fix this to '
               'start as root (change the effective-user directive '
               'in zope.conf)')
        logger.critical(msg)
        raise ZConfig.ConfigurationError(msg)

    try:
        uid = int(effective_user)
    except ValueError:
        try:
            pwrec = pwd.getpwnam(effective_user)
        except KeyError:
            msg = "Can't find username %r" % effective_user
            logger.error(msg)
            raise ZConfig.ConfigurationError(msg)
        uid = pwrec[2]
    else:
        try:
            pwrec = pwd.getpwuid(uid)
        except KeyError:
            msg = "Can't find uid %r" % uid
            logger.error(msg)
            raise ZConfig.ConfigurationError(msg)
    gid = pwrec[3]

    if uid == 0:
        msg = 'Cannot start Zope with the effective user as the root user'
        logger.error(msg)
        raise ZConfig.ConfigurationError(msg)

    try:
        import initgroups
        initgroups.initgroups(effective_user, gid)
        os.setgid(gid)
    except OSError:
        logger.exception('Could not set group id of effective user')

    os.setuid(uid)
    logger.info('Set effective user to "%s"' % effective_user)
    return 1 # for unit testing purposes 
Example #4
0
 def __init__(self, info, type_, name, handlers):
     if name or info.allowUnnamed():
         self.name = name
     else:
         raise ZConfig.ConfigurationError(
             repr(type_.name) + " sections may not be unnamed")
     BaseMatcher.__init__(self, info, type_, handlers)
Example #5
0
 def startSection(self, parent, type_, name):
     t = self.schema.gettype(type_)
     if t.isabstract():
         raise ZConfig.ConfigurationError(
             "concrete sections cannot match abstract section types;"
             " found abstract type " + repr(type_))
     return parent.createChildMatcher(t, name)
Example #6
0
 def createChildMatcher(self, type_, name):
     ci = self.type.getsectioninfo(type_.name, name)
     assert not ci.isabstract()
     if not ci.isAllowedName(name):
         raise ZConfig.ConfigurationError(
             "%s is not an allowed name for %s sections" %
             (repr(name), repr(ci.sectiontype.name)))
     return SectionMatcher(ci, type_, name, self.handlers)
Example #7
0
def check_python_version():
    # check for Python version
    python_version = sys.version.split()[0]
    optimum_version = '2.3.4'
    if python_version < '2.3.4':
        raise ZConfig.ConfigurationError(
            'Invalid python version: %s, the optimal version is %s or higher' %
            (python_version, optimum_version))
Example #8
0
 def normalizeURL(self, url):
     if self.isPath(url):
         url = "file://" + urllib.pathname2url(os.path.abspath(url))
     newurl, fragment = ZConfig.url.urldefrag(url)
     if fragment:
         raise ZConfig.ConfigurationError(
             "fragment identifiers are not supported", url)
     return newurl
Example #9
0
    def test_configuration_error_str(self):

        e = ZConfig.ConfigurationError('message')
        self.assertEqual(e.message, 'message')
        self.assertEqual('message', str(e))

        # We can delete the message, for some reason
        del e.message
Example #10
0
 def addSection(self, type_, name, sectvalue):
     if name:
         if name in self._sectionnames:
             raise ZConfig.ConfigurationError(
                 "section names must not be re-used within the"
                 " same container:" + repr(name))
         self._sectionnames[name] = name
     ci = self.type.getsectioninfo(type_, name)
     attr = ci.attribute
     v = self._values[attr]
     if ci.ismulti():
         v.append(sectvalue)
     elif v is None:
         self._values[attr] = sectvalue
     else:  # pragma: no cover
         raise ZConfig.ConfigurationError(
             "too many instances of %s section" % repr(ci.sectiontype.name))
Example #11
0
 def _raise_open_error(self, url, message):
     if url[:7].lower() == "file://":
         what = "file"
         ident = urllib.url2pathname(url[7:])
     else:
         what = "URL"
         ident = url
     raise ZConfig.ConfigurationError(
         "error opening %s %s: %s" % (what, ident, message), url)
Example #12
0
 def finish(self):
     """Check the constraints of the section and convert to an application
     object."""
     values = self._values
     for key, ci in self.type:
         if key:
             key = repr(key)
         else:
             key = "section type " + `ci.sectiontype.name`
         assert ci.attribute is not None
         attr = ci.attribute
         v = values[attr]
         if ci.name == '+' and not ci.issection():
             # v is a dict
             if ci.minOccurs > len(v):
                 raise ZConfig.ConfigurationError(
                     "no keys defined for the %s key/value map; at least %d"
                     " must be specified" % (attr, ci.minOccurs))
         if v is None and ci.minOccurs:
             default = ci.getdefault()
             if default is None:
                 raise ZConfig.ConfigurationError(
                     "no values for %s; %s required" % (key, ci.minOccurs))
             else:
                 v = values[attr] = default[:]
         if ci.ismulti():
             if not v:
                 default = ci.getdefault()
                 if isinstance(default, dict):
                     v.update(default)
                 else:
                     v[:] = default
             if len(v) < ci.minOccurs:
                 raise ZConfig.ConfigurationError(
                     "not enough values for %s; %d found, %d required"
                     % (key, len(v), ci.minOccurs))
         if v is None and not ci.issection():
             if ci.ismulti():
                 v = ci.getdefault()[:]
             else:
                 v = ci.getdefault()
             values[attr] = v
     return self.constuct()
Example #13
0
 def __init__(self, section):
     if not section.address:
         raise ZConfig.ConfigurationError(
             "No 'address' settings found "
             "within the 'http-server' or 'webdav-source-server' section")
     ServerFactory.__init__(self, section.address)
     # webdav-source-server sections won't have webdav_source_clients:
     webdav_clients = getattr(section, "webdav_source_clients", None)
     self.fast_listen = getattr(section, 'fast_listen', True)
     self.webdav_source_clients = webdav_clients
     self.use_wsgi = section.use_wsgi
Example #14
0
 def __call__(self, handlermap):
     d = {}
     for name, callback in handlermap.items():
         n = self._convert(name)
         if n in d:
             raise ZConfig.ConfigurationError(
                 "handler name not unique when converted to a basic-key: " +
                 repr(name))
         d[n] = callback
     L = []
     for handler, value in self._handlers:
         if handler not in d:
             L.append(handler)
     if L:
         raise ZConfig.ConfigurationError("undefined handlers: " +
                                          ", ".join(L))
     for handler, value in self._handlers:
         f = d[handler]
         if f is not None:
             f(value)
Example #15
0
 def getsectioninfo(self, type, name):
     for key, info in self._children:
         if key:
             if key == name:
                 if not info.issection():
                     raise ZConfig.ConfigurationError(
                         "section name %s already in use for key" % key)
                 st = info.sectiontype
                 if st.isabstract():
                     try:
                         st = st.getsubtype(type)
                     except ZConfig.ConfigurationError:
                         raise ZConfig.ConfigurationError(
                             "section type %s not allowed for name %s" %
                             ( ` type `, ` key `))
                 if not st.name == type:
                     raise ZConfig.ConfigurationError(
                         "name %s must be used for a %s section" %
                         ( ` name `, ` st.name `))
                 return info
         # else must be a sectiontype or an abstracttype:
         elif info.sectiontype.name == type:
             if not (name or info.allowUnnamed()):
                 raise ZConfig.ConfigurationError(
                     ` type ` + " sections must be named")
             return info
         elif info.sectiontype.isabstract():
             st = info.sectiontype
             if st.name == type:
                 raise ZConfig.ConfigurationError(
                     "cannot define section with an abstract type")
             try:
                 st = st.getsubtype(type)
             except ZConfig.ConfigurationError:
                 # not this one; maybe a different one
                 pass
             else:
                 return info
     raise ZConfig.ConfigurationError(
         "no matching section defined for type='%s', name='%s'" %
         (type, name))
Example #16
0
def verifyPackages(config):
    """
    Verify a given installation has only unique packages defined
    for each release. Build a list of packages to be built for each
    release.
    @param config: ZConfig object containing farb configuration
    """

    # dictionary mapping packagesets to releases
    release_packagesets = {}
    # dictionary mapping packages to release
    release_packages = {}

    # Iterate over installations, finding all referenced package sets,
    # and associating them with releases.
    for install in config.Installations.Installation:
        releaseName = install.release.lower()
        if (not release_packagesets.has_key(releaseName)):
            release_packagesets[releaseName] = []
        # Add all package sets
        for pkgsetName in install.packageset:
            for pkgset in config.PackageSets.PackageSet:
                if (pkgsetName.lower() == pkgset.getSectionName()):
                    # Don't add the same packge set twice
                    if (not release_packagesets[releaseName].count(pkgset)):
                        release_packagesets[releaseName].append(pkgset)

    # Verify that packages only appear *once* in the list of packagesets
    # used in a specified release.
    for releaseName, packageSets in release_packagesets.iteritems():
        if (not release_packages.has_key(releaseName)):
            release_packages[releaseName] = []
        for packageSet in packageSets:
            for pkg in packageSet.Package:
                # If if this package has already been listed
                # by another packageset, in another installation,
                # using the same release, throw an error
                if (_hasPackage(release_packages, releaseName, pkg)):
                    raise ZConfig.ConfigurationError(
                        "Packages may not be listed in more than one package set within the same release. (Port: \"%s\", Package Set: \"%s\", Release: \"%s\")"
                        % (pkg.port, pkgsetName, install.release))
                else:
                    release_packages[releaseName].append(pkg)

    # Now add the package lists to the releases
    for release in config.Releases.Release:
        releaseName = release.getSectionName()
        # Do any installations reference this release?
        if (release_packages.has_key(releaseName)):
            # Don't add an empty list, leave release.packages set to None
            if (len(release_packages[releaseName])):
                release.packages = release_packages[releaseName]
Example #17
0
def releases_handler(section):
    """
    Validate a group of defined releases.
    Instantiate corresponding ReleaseBuilder objects.
    """
    tags = []
    releases = []

    # Global tftproot
    section.tftproot = os.path.join(section.installroot, 'tftproot')

    # Validate release sections and instantiate
    # ReleaseBuilders.
    for release in section.Release:
        if (release.cvstag != None and tags.count(release.cvstag) > 0):
            raise ZConfig.ConfigurationError(
                "CVS Tags may not be re-used in mutiple Release sections. (Tag: \"%s\")"
                % (release.cvstag))
        else:
            tags.append(release.cvstag)

        if (not release.binaryrelease
                and (release.cvsroot == None or release.cvstag == None)):
            raise ZConfig.ConfigurationError(
                "You must either set BinaryRelease to True or set CVSRoot and CVSTag"
            )

        if (release.binaryrelease and (release.iso == None)):
            raise ZConfig.ConfigurationError(
                "ISO must be set if using a BinaryRelease")

        if (not release.useportsnap and release.cvsroot == None):
            raise ZConfig.ConfigurationError(
                "UsePortsnap must be true or CVSRoot needs to be set")

        # The buildroot directory is merely a combination of the buildroot + release name
        release.buildroot = os.path.join(section.buildroot,
                                         release.getSectionName())
        # And the chroots
        release.releaseroot = os.path.join(release.buildroot, 'releaseroot')
        release.pkgroot = os.path.join(release.buildroot, 'pkgroot')
        # And the ports tree
        release.portsdir = os.path.join(release.pkgroot, 'usr', 'ports')
        # And the package dir ...
        release.packagedir = os.path.join(release.portsdir, 'packages')

        # Don't let a ports distribution set be defined
        if release.dists.count('ports') > 0:
            raise ZConfig.ConfigurationError(
                "Do not define the ports distribution in Dists. Ports will be fetched with CVS or Portsnap."
            )

        # Make sure both base and kernels are at least defined in Dists
        if release.dists.count('base') == 0 or release.dists.count(
                'kernels') == 0:
            raise ZConfig.ConfigurationError(
                "At least base and kernels must be included in list Dists.")

    return section
Example #18
0
 def __init__(self, section):
     from ZServer import HTTPServer
     if not section.address:
         raise ZConfig.ConfigurationError(
             "No 'address' settings found "
             "within the 'http-server' or 'webdav-source-server' section")
     ServerFactory.__init__(self, section.address)
     self.server_class = HTTPServer.zhttp_server
     self.force_connection_close = section.force_connection_close
     # webdav-source-server sections won't have webdav_source_clients:
     webdav_clients = getattr(section, "webdav_source_clients", None)
     self.webdav_source_clients = webdav_clients
     self.use_wsgi = section.use_wsgi
Example #19
0
 def run(self):
     # the mainloop.
     try:
         from App.config import getConfiguration
         config = getConfiguration()
         import ZServer
         if config.twisted_servers and config.servers:
             raise ZConfig.ConfigurationError(
                 "You can't run both ZServer servers and twisted servers.")
         if config.twisted_servers:
             if not _use_twisted:
                 raise ZConfig.ConfigurationError(
                     "You do not have twisted installed.")
             twisted.internet.reactor.run()
             # Storing the exit code in the ZServer even for twisted,
             # but hey, it works...
             sys.exit(ZServer.exit_code)
         else:
             import Lifetime
             Lifetime.loop()
             sys.exit(ZServer.exit_code)
     finally:
         self.shutdown()
Example #20
0
 def setupServers(self):
     socket_err = (
         'There was a problem starting a server of type "%s". '
         'This may mean that your user does not have permission to '
         'bind to the port which the server is trying to use or the '
         'port may already be in use by another application. '
         '(%s)')
     servers = []
     for server in self.cfg.servers:
         # create the server from the server factory
         # set up in the config
         try:
             servers.append(server.create())
         except socket.error, e:
             raise ZConfig.ConfigurationError(socket_err %
                                              (server.servertype(), e[1]))
Example #21
0
    def normalizeURL(self, url):
        """Return a URL for *url*

        If *url* refers to an existing file, the corresponding
        ``file:`` URL is returned. Otherwise *url* is checked
        for sanity: if it does not have a schema, :exc:`ValueError` is
        raised, and if it does have a fragment identifier,
        :exc:`~.ConfigurationError` is raised.

        This uses :meth:`isPath` to determine whether *url* is
        a URL of a filesystem path.
        """
        if self.isPath(url):
            url = "file://" + pathname2url(os.path.abspath(url))
        newurl, fragment = ZConfig.url.urldefrag(url)
        if fragment:
            raise ZConfig.ConfigurationError(
                "fragment identifiers are not supported", url)
        return newurl
Example #22
0
 def finish(self):
     if self.sectitems or self.keypairs:
         raise ZConfig.ConfigurationError(
             "not all command line options were consumed")
Example #23
0
 def __init__(self, section):
     if not section.address:
         raise ZConfig.ConfigurationError(
             "No 'address' settings found within the 'ftp-server' section")
     ServerFactory.__init__(self, section.address)
Example #24
0
    def addValue(self, key, value, position):
        try:
            realkey = self.type.keytype(key)
        except ValueError as e:
            raise ZConfig.DataConversionError(e, key, position)
        arbkey_info = None
        for i in range(len(self.type)):
            k, ci = self.type[i]
            if k == realkey:
                break
            if ci.name == "+" and not ci.issection():
                arbkey_info = k, ci
        else:
            if arbkey_info is None:
                raise ZConfig.ConfigurationError(
                    repr(key) + " is not a known key name")
            k, ci = arbkey_info
        if ci.issection():  # pragma: no cover
            if ci.name:
                extra = " in %s sections" % repr(self.type.name)
            else:
                extra = ""
            raise ZConfig.ConfigurationError("%s is not a valid key name%s" %
                                             (repr(key), extra))

        ismulti = ci.ismulti()
        attr = ci.attribute
        assert attr is not None
        v = self._values[attr]
        if v is None:
            if k == '+':
                v = {}  # pragma: no cover
            elif ismulti:
                v = []  # pragma: no cover
            self._values[attr] = v
        elif not ismulti:
            if k != '+':
                raise ZConfig.ConfigurationError(
                    repr(key) + " does not support multiple values")
        elif len(v) == ci.maxOccurs:  # pragma: no cover
            # This code may be impossible to hit. Previously it would
            # have raised a NameError because it used an unbound
            # local.
            raise ZConfig.ConfigurationError("too many values for " + repr(ci))

        value = ValueInfo(value, position)
        if k == '+':
            if ismulti:
                if realkey in v:
                    v[realkey].append(value)
                else:
                    v[realkey] = [value]
            else:
                if realkey in v:  # pragma: no cover
                    raise ZConfig.ConfigurationError("too many values for " +
                                                     repr(key))
                v[realkey] = value
        elif ismulti:
            v.append(value)
        else:
            self._values[attr] = value