Commit 8e149c4b authored by Hanno Schlichting's avatar Hanno Schlichting

Move ZServer related starter logic into ZServer project.

parent efe61189
This diff is collapsed.
......@@ -130,7 +130,7 @@ def root_handler(cfg):
if cfg.trusted_proxies:
mapped = []
for name in cfg.trusted_proxies:
mapped.extend(_name2Ips(name))
mapped.extend(_name_to_ips(name))
config.TRUSTED_PROXIES = tuple(mapped)
from ZPublisher import HTTPRequest
......@@ -150,13 +150,13 @@ def handleConfig(cfg, multihandler):
return multihandler(handlers)
def _name2Ips(host, isIp_=re.compile(r'(\d+\.){3}').match):
def _name_to_ips(host, _is_ip=re.compile(r'(\d+\.){3}').match):
"""Map a name *host* to the sequence of its ip addresses.
use *host* itself (as sequence) if it already is an ip address.
Thus, if only a specific interface on a host is trusted,
identify it by its ip (and not the host name).
"""
if isIp_(host):
if _is_ip(host):
return [host]
return gethostbyaddr(host)[2]
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################
import string, math
def format(text, max_width=80, indent=0, trailing_lines_indent_more=0):
text = string.expandtabs(string.replace(text, '\r', ''))
lines = string.split(text, '\n')
aggregate = []
for line in lines:
if len(line) <= max_width-1:
# this line is short enough to output
aggregate.append(line)
else:
lines = splitlongline(line, max_width)
aggregate.extend(lines)
out = []
i = 0
for line in aggregate:
spaces = ' ' * indent
if i != 0 and trailing_lines_indent_more:
spaces = spaces + (' ' * trailing_lines_indent_more)
out.append('%s%s' % (spaces, line))
i = i + 1
return string.join(out, '\n')
def splitword(word, max_width=80, linepos=0):
# some lines may have single words that exceed the max_width
# We want to break apart long words into as many chunks as necessary
if len(word) <= max_width:
return [word]
first_chunk_len = max_width-1-linepos
firstchunk = word[:first_chunk_len]
word = word[first_chunk_len:]
numchunks = int(math.ceil(len(word) / float(max_width-1)))
index = 0
tmp = [firstchunk]
for chunknum in range(numchunks):
chunk = word[index:index+max_width-1]
tmp.append(chunk)
index = index + max_width-1
return tmp
def splitlongline(line, max_width=80):
# split a "long" line defined by max_width into a list of lines
line = string.strip(line)
words = string.split(line, ' ')
wordnum = 0
# iterate over all the words in the line, extending the word list
# necessary for too-long words
aggregate = []
linelen = 0
wordnum = 0
while words:
word = words.pop(0)
if not word: continue
if len(word) > max_width:
new_words = splitword(word, max_width, linelen)
word = new_words[0]
for new_word in new_words[1:]:
words.insert(wordnum, new_word)
wordnum = wordnum + 1
if words:
next_word = words[0]
else:
next_word = None
if next_word is None:
aggregate.append(word)
wordnum = wordnum + 1
continue
maybe_len = linelen + len(word) + len(next_word)
if maybe_len >= max_width-1:
aggregate.append(word)
aggregate.append(None)
linelen = 0
else:
aggregate.append(word)
linelen = linelen + len(word) + 1
wordnum = wordnum + 1
s = ""
last = None
for item in aggregate:
if item is None:
s = '%s\n' % s
elif last is None:
s = '%s%s' % (s, item)
else:
s = '%s %s' % (s, item)
last = item
return string.split(s, '\n')
long = """
To turn a component into a product you must fulfill many contracts. For the most part these contracts are not yet defined in terms of interfaces. Instead you must subclass from base classes that implement the contracts. This makes building products confusing, and this is an area that we are actively working on improving. Hereisalonglinethatshouldbecaughtandbrokenupbytheformatter,hopefullyitllgetsplitupwhenirunitthroughthere."""
long2 = """
Hereisalonglinethatshouldbecaughtandbrokenupbytheformatter,hopefullyitllgetsplitupwhenirunitthroughthere."""
long3 = """
To turn a component into a product you must fulfill many contracts. For the most part these contracts are not yet defined in terms of interfaces.
Instead you must subclass from base classes that implement the contracts. This makes building products confusing, and this is an area that we are
actively working on improving. Hereisalonglinethatshouldbecaughtandbrokenupbytheformatter,hopefullyitllgetsplitupwhenirunitthroughthere."""
if __name__ == '__main__':
print format(long, 60, 10)
print format(long)
print format(long2, 60, 10)
print format(long2)
print format(long3, 60, 10)
print format(long3)
......@@ -12,29 +12,9 @@
#
##############################################################################
"""Utility function for file locking.
from zope.deferredimport import deprecated
This module provides a platform-specific function which uses the
best-available strategy for locking a file object.
"""
try:
import fcntl
except ImportError:
# Try windows-specific code:
try:
# We expect this module to exist, but the LockFile function may not.
from ZODB.winlock import LockFile
except ImportError:
# we don't understand any kind of locking, forget it
def lock_file(file):
pass
else:
# Windows
def lock_file(file):
un = file.fileno()
LockFile(un, 0, 0, 1, 0) # just lock the first byte, who cares
else:
# Unix-specific locking:
def lock_file(file):
fcntl.flock(file, fcntl.LOCK_EX | fcntl.LOCK_NB)
# BBB Zope 5.0
deprecated(
'Please import from ZServer.Zope2.Startup.utils',
lock_file='ZServer.Zope2.Startup.utils:lock_file')
#!/usr/bin/env python
##############################################################################
#
# Copyright (c) 2002 Zope Foundation and Contributors.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################
"""Zope user bootstrap system"""
import sys, sha, binascii, random, getopt, getpass, os
try:
from crypt import crypt
except ImportError:
crypt = None
def generate_salt():
"""Generate a salt value for the crypt function."""
salt_choices = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789./")
return random.choice(salt_choices)+random.choice(salt_choices)
def generate_passwd(password, encoding):
encoding=encoding.upper()
if encoding == 'SHA':
pw = '{SHA}' + binascii.b2a_base64(sha.new(password).digest())[:-1]
elif encoding == 'CRYPT':
pw = '{CRYPT}' + crypt(password, generate_salt())
elif encoding == 'CLEARTEXT':
pw = password
return pw
def write_generated_password(home, ac_path, username):
pw_choices = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789!")
acfile=open(ac_path, 'w')
pw = ''
for i in range(8):
pw = pw + random.choice(pw_choices)
acfile.write('%s:%s' % (username, generate_passwd(pw, 'SHA')))
acfile.close()
os.system('chmod 644 %s' % ac_path)
return pw
def write_access(home, user='', group=''):
ac_path=os.path.join(home, 'access')
if not os.path.exists(ac_path):
print '-'*78
print 'creating default access file'
pw = write_generated_password(home, ac_path, 'emergency')
print """Note:
The emergency user name and password are 'emergency'
and '%s'.
You can change the emergency name and password with the
zpasswd script. To find out more, type:
%s zpasswd.py
""" % (pw, sys.executable)
import do; do.ch(ac_path, user, group)
def write_inituser(home, user='', group=''):
ac_path=os.path.join(home, 'inituser')
if not os.path.exists(ac_path):
print '-'*78
print 'creating default inituser file'
pw = write_generated_password(home, ac_path, 'admin')
print """Note:
The initial user name and password are 'admin'
and '%s'.
You can change the name and password through the web
interface or using the 'zpasswd.py' script.
""" % pw
import do; do.ch(ac_path, user, group)
class CommandLineError(Exception):
pass
def main(argv):
short_options = ':u:p:e:d:'
long_options = ['username=',
'password=',
'encoding=',
'domains=']
usage = """Usage: %s [options] filename
If this program is called without command-line options, it will prompt
for all necessary information. The available options are:
-u / --username=
Set the username to be used for the initial user or the emergency user
-p / --password=
Set the password
-e / --encoding=
Set the encryption/encoding rules. Defaults to SHA-1. OPTIONAL
-d / --domains=
Set the domain names that the user user can log in from. Defaults to
any. OPTIONAL.
Filename is required and should be the name of the file to store the
information in (usually "inituser" or "access").
Copyright (C) 1999, 2000 Digital Creations, Inc.
""" % argv[0]
try:
if len(argv) < 2:
raise CommandLineError, "Not enough arguments!"
optlist, args = getopt.getopt(sys.argv[1:], short_options, long_options)
if len(args) != 1:
raise CommandLineError, "Only one filename allowed!"
access_file = open(args[0], 'w')
if len(optlist) > 0:
# Set the sane defaults
username = ''
encoding = 'SHA'
domains = ''
for opt in optlist:
if (opt[0] == '-u') or (opt[0] == '--username'):
username = opt[1]
elif (opt[0] == '-p') or (opt[0] == '--password'):
password = opt[1]
elif (opt[0] == '-e') or (opt[0] == '--encoding'):
encoding = opt[1]
elif (opt[0] == '-d') or (opt[0] == '--domains'):
domains = ":" + opt[1]
# Verify that we got what we need
if not username or not password:
raise CommandLineError, "Must specify username and password."
access_file.write(username + ':' +
generate_passwd(password, encoding) +
domains)
else:
# Run through the prompts
while 1:
username = raw_input("Username: ")
if username != '':
break
while 1:
password = getpass.getpass("Password: ")
verify = getpass.getpass("Verify password: ")
if verify == password:
break
else:
password = verify = ''
print "Password mismatch, please try again..."
while 1:
print """
Please choose a format from:
SHA - SHA-1 hashed password (default)
CRYPT - UNIX-style crypt password
CLEARTEXT - no protection
"""
encoding = raw_input("Encoding: ")
if encoding == '':
encoding = 'SHA'
break
if encoding.upper() in ['SHA', 'CRYPT', 'CLEARTEXT']:
break
domains = raw_input("Domain restrictions: ")
if domains: domains = ":" + domains
access_file.write(username + ":" +
generate_passwd(password, encoding) +
domains)
except CommandLineError, v:
sys.stderr.write(usage)
sys.stderr.write("\n\n%s" % v)
sys.exit(1)
# If called from the command line
if __name__=='__main__':
main(sys.argv)
##############################################################################
#
# Copyright (c) 2002 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
import logging
import sys
import re
from socket import gethostbyaddr
from ZConfig import ConfigurationError
from zope.event import notify
from zope.processlifetime import ProcessStarting
logger = logging.getLogger("Zope")
class WSGIStarter(object):
"""This is a class which starts Zope as a WSGI app."""
wsgi = True
def prepare(self):
self.setupLocale()
self.setupSecurityOptions()
self.setupPublisher()
self.setupInterpreter()
self.startZope()
from App.config import getConfiguration
config = getConfiguration() # NOQA
notify(ProcessStarting())
logger.info('Ready to handle requests')
def setConfiguration(self, cfg):
self.cfg = cfg
def setupInterpreter(self):
# make changes to the python interpreter environment
sys.setcheckinterval(self.cfg.python_check_interval)
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('utf-8') # NOQA
locale_id = self.cfg.locale
if locale_id is not None:
try:
import locale
except:
raise 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 ConfigurationError(
'The specified locale "%s" is not supported by your'
'system.\nSee your operating system documentation for '
'more\ninformation on locale support.' % locale_id)
def setupPublisher(self):
import ZPublisher.HTTPRequest
import ZPublisher.Publish
ZPublisher.Publish.set_default_debug_mode(self.cfg.debug_mode)
ZPublisher.Publish.set_default_authentication_realm(
self.cfg.http_realm)
if self.cfg.trusted_proxies:
mapped = []
for name in self.cfg.trusted_proxies:
mapped.extend(_name_to_ips(name))
ZPublisher.HTTPRequest.trusted_proxies = tuple(mapped)
def setupSecurityOptions(self):
import AccessControl
AccessControl.setImplementation(
self.cfg.security_policy_implementation)
AccessControl.setDefaultBehaviors(
not self.cfg.skip_ownership_checking,
not self.cfg.skip_authentication_checking,
self.cfg.verbose_security)
def startZope(self):
# Import Zope
import Zope2
Zope2.startup()
def _name_to_ips(host, _is_ip=re.compile(r'(\d+\.){3}').match):
'''map a name *host* to the sequence of its ip addresses;
use *host* itself (as sequence) if it already is an ip address.
Thus, if only a specific interface on a host is trusted,
identify it by its ip (and not the host name).
'''
if _is_ip(host):
return [host]
return gethostbyaddr(host)[2]
This diff is collapsed.
......@@ -20,19 +20,10 @@ import unittest
import ZConfig
import Products
from Zope2.Startup import datatypes
from Zope2.Startup.options import ZopeOptions
LIFETIME = True
try:
import Lifetime # NOQA
except ImportError:
LIFETIME = False
_SCHEMA = {}
TEMPNAME = tempfile.mktemp()
TEMPPRODUCTS = os.path.join(TEMPNAME, "Products")
TEMPVAR = os.path.join(TEMPNAME, "var")
......@@ -99,9 +90,9 @@ class WSGIStartupTestCase(unittest.TestCase):
<filestorage>
path <<INSTANCE_HOME>>/var/Data.fs
</filestorage>
mount-point /
cache-size 5000
pool-size 7
mount-point /
cache-size 5000
pool-size 7
</zodb_db>
""")
self.assertEqual(conf.databases[0].config.cache_size, 5000)
......@@ -130,105 +121,3 @@ class WSGIStartupTestCase(unittest.TestCase):
default-zpublisher-encoding iso-8859-15
""")
self.assertEqual(conf.default_zpublisher_encoding, 'iso-8859-15')
if LIFETIME:
class ZServerStartupTestCase(unittest.TestCase):
def tearDown(self):
Products.__path__ = [d for d in Products.__path__
if os.path.exists(d)]
@property
def schema(self):
return getSchema('zopeschema.xml')
def load_config_text(self, text):
# We have to create a directory of our own since the existence
# of the directory is checked. This handles this in a
# platform-independent way.
schema = self.schema
sio = cStringIO.StringIO(
text.replace("<<INSTANCE_HOME>>", TEMPNAME))
os.mkdir(TEMPNAME)
os.mkdir(TEMPPRODUCTS)
os.mkdir(TEMPVAR)
try:
conf, handler = ZConfig.loadConfigFile(schema, sio)
finally:
os.rmdir(TEMPPRODUCTS)
os.rmdir(TEMPVAR)
os.rmdir(TEMPNAME)
self.assertEqual(conf.instancehome, TEMPNAME)
return conf, handler
def test_cgi_environment(self):
conf, handler = self.load_config_text("""\
# instancehome is here since it's required
instancehome <<INSTANCE_HOME>>
<cgi-environment>
HEADER value
ANOTHER value2
</cgi-environment>
""")
items = conf.cgi_environment.items()
items.sort()
self.assertEqual(
items, [("ANOTHER", "value2"), ("HEADER", "value")])
def test_ms_public_header(self):
from Zope2.Startup import config
from Zope2.Startup.handlers import handleConfig
default_setting = config.ZSERVER_ENABLE_MS_PUBLIC_HEADER
try:
conf, handler = self.load_config_text("""\
instancehome <<INSTANCE_HOME>>
enable-ms-public-header true
""")
handleConfig(None, handler)
self.assertTrue(config.ZSERVER_ENABLE_MS_PUBLIC_HEADER)
conf, handler = self.load_config_text("""\
instancehome <<INSTANCE_HOME>>
enable-ms-public-header false
""")
handleConfig(None, handler)
self.assertFalse(config.ZSERVER_ENABLE_MS_PUBLIC_HEADER)
finally:
config.ZSERVER_ENABLE_MS_PUBLIC_HEADER = default_setting
def test_path(self):
p1 = tempfile.mktemp()
p2 = tempfile.mktemp()
try:
os.mkdir(p1)
os.mkdir(p2)
conf, handler = self.load_config_text("""\
# instancehome is here since it's required
instancehome <<INSTANCE_HOME>>
path %s
path %s
""" % (p1, p2))
items = conf.path
self.assertEqual(items, [p1, p2])
finally:
if os.path.exists(p1):
os.rmdir(p1)
if os.path.exists(p2):
os.rmdir(p2)
def test_access_and_trace_logs(self):
fn = tempfile.mktemp()
conf, handler = self.load_config_text("""
instancehome <<INSTANCE_HOME>>
<logger access>
<logfile>
path %s
</logfile>
</logger>
""" % fn)
self.assert_(isinstance(conf.access, datatypes.LoggerFactory))
self.assertEqual(conf.access.name, "access")
self.assertEqual(conf.access.handler_factories[0].section.path, fn)
self.assert_(conf.trace is None)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment