~ubuntu-branches/ubuntu/natty/moin/natty-updates

« back to all changes in this revision

Viewing changes to MoinMoin/server/__init__.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-06-22 21:17:13 UTC
  • mto: This revision was merged to the branch mainline in revision 18.
  • Revision ID: james.westby@ubuntu.com-20080622211713-inlv5k4eifxckelr
ImportĀ upstreamĀ versionĀ 1.7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: iso-8859-1 -*-
 
2
"""
 
3
    MoinMoin server package
 
4
 
 
5
    Supply common server utilities.
 
6
 
 
7
    @copyright: 2004 Nir Soffer
 
8
    @license: GNU GPL, see COPYING for details.
 
9
"""
 
10
 
 
11
import os
 
12
from StringIO import StringIO
 
13
 
 
14
from MoinMoin import log
 
15
logging = log.getLogger(__name__)
 
16
 
 
17
from MoinMoin import config
 
18
 
 
19
 
 
20
def switchUID(uid, gid):
 
21
    """ Switch identity to safe user and group
 
22
 
 
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?
 
25
 
 
26
    Raise RuntimeError if can't switch or trying to switch to root.
 
27
    """
 
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!')
 
32
 
 
33
    try:
 
34
        os.setgid(gid)
 
35
        os.setuid(uid)
 
36
    except (OSError, AttributeError):
 
37
        # Either we can't switch, or we are on windows, which does not have
 
38
        # those calls.
 
39
        raise RuntimeError("can't change uid/gid to %s/%s" %
 
40
                           (uid, gid))
 
41
    logging.info("Running as uid/gid %d/%d" % (uid, gid))
 
42
 
 
43
 
 
44
class Config:
 
45
    """ Base class for server configuration
 
46
 
 
47
    When you create a server, you should run it with a Config
 
48
    instance. Sub class to define the default values.
 
49
 
 
50
    This class does all error checking needed for config values, and will
 
51
    raise a RuntimeError on any fatal error.
 
52
    """
 
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)
 
59
 
 
60
    def __init__(self):
 
61
        """ Validate and post process configuration values
 
62
 
 
63
        Will raise RuntimeError for any wrong config value.
 
64
        """
 
65
        # Check that docs path is accessible
 
66
        if self.docs:
 
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)
 
71
 
 
72
        # Don't check uid and gid on windows, those calls are not available.
 
73
        if os.name == 'nt':
 
74
            self.uid = self.gid = 0
 
75
            return
 
76
 
 
77
        self.uid = os.getuid()
 
78
        self.gid = os.getgid()
 
79
 
 
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.')
 
85
 
 
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.
 
89
            import pwd, grp
 
90
            try:
 
91
                self.uid = pwd.getpwnam(self.user)[2]
 
92
            except KeyError:
 
93
                raise RuntimeError("Unknown user: '%s', check user setting" % self.user)
 
94
            try:
 
95
                self.gid = grp.getgrnam(self.group)[2]
 
96
            except KeyError:
 
97
                raise RuntimeError("Unknown group: '%s', check group setting" % self.group)
 
98