1
# -*- coding: iso-8859-1 -*-
3
MoinMoin server package
5
Supply common server utilities.
7
@copyright: 2004 Nir Soffer
8
@license: GNU GPL, see COPYING for details.
12
from StringIO import StringIO
14
from MoinMoin import log
15
logging = log.getLogger(__name__)
17
from MoinMoin import config
20
def switchUID(uid, gid):
21
""" Switch identity to safe user and group
23
Does not support Windows, because the necessary calls are not available.
24
TODO: can we use win32api calls to achieve the same effect on Windows?
26
Raise RuntimeError if can't switch or trying to switch to root.
28
if uid == 0 or gid == 0:
29
# We will not run as root. If you like to run a web
30
# server as root, then hack this code.
31
raise RuntimeError('will not run as root!')
36
except (OSError, AttributeError):
37
# Either we can't switch, or we are on windows, which does not have
39
raise RuntimeError("can't change uid/gid to %s/%s" %
41
logging.info("Running as uid/gid %d/%d" % (uid, gid))
45
""" Base class for server configuration
47
When you create a server, you should run it with a Config
48
instance. Sub class to define the default values.
50
This class does all error checking needed for config values, and will
51
raise a RuntimeError on any fatal error.
53
# some defaults that should be common for all servers:
54
url_prefix_static = config.url_prefix_static
55
docs = None # document root (if supported)
56
user = None # user we shall use for running (if supported)
57
group = None # group ...
58
port = None # tcp port number (if supported)
61
""" Validate and post process configuration values
63
Will raise RuntimeError for any wrong config value.
65
# Check that docs path is accessible
67
self.docs = os.path.normpath(os.path.abspath(self.docs))
68
if not os.access(self.docs, os.F_OK | os.R_OK | os.X_OK):
69
raise RuntimeError("Can't access docs directory '%s'. Check docs "
70
"setting and permissions." % self.docs)
72
# Don't check uid and gid on windows, those calls are not available.
74
self.uid = self.gid = 0
77
self.uid = os.getuid()
78
self.gid = os.getgid()
80
# If serving privileged port, we must run as root to bind the port.
81
# we will give up root privileges later
82
if self.port and self.port < 1024 and self.uid != 0:
83
raise RuntimeError('Must run as root to serve port number under 1024. '
84
'Run as root or change port setting.')
86
if self.user and self.group and self.uid == 0:
87
# If we run as root to serve privileged port, we change user and group
88
# to a safe setting. Get the uid and gid now, switch later.
91
self.uid = pwd.getpwnam(self.user)[2]
93
raise RuntimeError("Unknown user: '%s', check user setting" % self.user)
95
self.gid = grp.getgrnam(self.group)[2]
97
raise RuntimeError("Unknown group: '%s', check group setting" % self.group)