~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/chroot.py

(parthm) Better regex compile errors (Parth Malwankar)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Canonical Ltd
 
1
# Copyright (C) 2006-2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Implementation of Transport that prevents access to locations above a set
18
18
root.
19
19
"""
20
 
from urlparse import urlparse
21
20
 
22
 
from bzrlib import errors, urlutils
23
21
from bzrlib.transport import (
24
 
    get_transport,
 
22
    pathfilter,
25
23
    register_transport,
26
 
    Server,
27
 
    Transport,
28
 
    unregister_transport,
29
24
    )
30
 
from bzrlib.transport.decorator import TransportDecorator, DecoratorServer
31
 
from bzrlib.transport.memory import MemoryTransport
32
 
 
33
 
 
34
 
class ChrootServer(Server):
 
25
 
 
26
 
 
27
class ChrootServer(pathfilter.PathFilteringServer):
35
28
    """User space 'chroot' facility.
36
 
    
 
29
 
37
30
    The server's get_url returns the url for a chroot transport mapped to the
38
31
    backing transport. The url is of the form chroot-xxx:/// so parent
39
32
    directories of the backing transport are not visible. The chroot url will
40
33
    not allow '..' sequences to result in requests to the chroot affecting
41
34
    directories outside the backing transport.
 
35
 
 
36
    PathFilteringServer does all the path sanitation needed to enforce a
 
37
    chroot, so this is a simple subclass of PathFilteringServer that ignores
 
38
    filter_func.
42
39
    """
43
40
 
44
41
    def __init__(self, backing_transport):
45
 
        self.backing_transport = backing_transport
 
42
        pathfilter.PathFilteringServer.__init__(self, backing_transport, None)
46
43
 
47
44
    def _factory(self, url):
48
 
        assert url.startswith(self.scheme)
49
45
        return ChrootTransport(self, url)
50
46
 
51
 
    def get_url(self):
52
 
        return self.scheme
53
 
 
54
 
    def setUp(self):
 
47
    def start_server(self):
55
48
        self.scheme = 'chroot-%d:///' % id(self)
56
49
        register_transport(self.scheme, self._factory)
57
50
 
58
 
    def tearDown(self):
59
 
        unregister_transport(self.scheme, self._factory)
60
 
 
61
 
 
62
 
class ChrootTransport(Transport):
 
51
 
 
52
class ChrootTransport(pathfilter.PathFilteringTransport):
63
53
    """A ChrootTransport.
64
54
 
65
55
    Please see ChrootServer for details.
66
56
    """
67
57
 
68
 
    def __init__(self, server, base):
69
 
        self.server = server
70
 
        if not base.endswith('/'):
71
 
            base += '/'
72
 
        Transport.__init__(self, base)
73
 
        self.base_path = self.base[len(self.server.scheme)-1:]
74
 
        self.scheme = self.server.scheme
75
 
 
76
 
    def _call(self, methodname, relpath, *args):
77
 
        method = getattr(self.server.backing_transport, methodname)
78
 
        return method(self._safe_relpath(relpath), *args)
79
 
 
80
 
    def _safe_relpath(self, relpath):
81
 
        safe_relpath = self._combine_paths(self.base_path, relpath)
82
 
        assert safe_relpath.startswith('/')
83
 
        return safe_relpath[1:]
84
 
 
85
 
    # Transport methods
86
 
    def abspath(self, relpath):
87
 
        return self.scheme + self._safe_relpath(relpath)
88
 
 
89
 
    def append_file(self, relpath, f, mode=None):
90
 
        return self._call('append_file', relpath, f, mode)
91
 
 
92
 
    def clone(self, relpath):
93
 
        return ChrootTransport(self.server, self.abspath(relpath))
94
 
 
95
 
    def delete(self, relpath):
96
 
        return self._call('delete', relpath)
97
 
 
98
 
    def delete_tree(self, relpath):
99
 
        return self._call('delete_tree', relpath)
100
 
 
101
 
    def external_url(self):
102
 
        """See bzrlib.transport.Transport.external_url."""
103
 
        # Chroots, like MemoryTransport depend on in-process
104
 
        # state and thus the base cannot simply be handed out.
105
 
        # See the base class docstring for more details and
106
 
        # possible directions. For now we return the chrooted
107
 
        # url. 
108
 
        return self.server.backing_transport.external_url()
109
 
 
110
 
    def get(self, relpath):
111
 
        return self._call('get', relpath)
112
 
 
113
 
    def has(self, relpath):
114
 
        return self._call('has', relpath)
115
 
 
116
 
    def iter_files_recursive(self):
117
 
        backing_transport = self.server.backing_transport.clone(
118
 
            self._safe_relpath('.'))
119
 
        return backing_transport.iter_files_recursive()
120
 
 
121
 
    def listable(self):
122
 
        return self.server.backing_transport.listable()
123
 
 
124
 
    def list_dir(self, relpath):
125
 
        return self._call('list_dir', relpath)
126
 
 
127
 
    def lock_read(self, relpath):
128
 
        return self._call('lock_read', relpath)
129
 
 
130
 
    def lock_write(self, relpath):
131
 
        return self._call('lock_write', relpath)
132
 
 
133
 
    def mkdir(self, relpath, mode=None):
134
 
        return self._call('mkdir', relpath, mode)
135
 
 
136
 
    def put_file(self, relpath, f, mode=None):
137
 
        return self._call('put_file', relpath, f, mode)
138
 
 
139
 
    def rename(self, rel_from, rel_to):
140
 
        return self._call('rename', rel_from, self._safe_relpath(rel_to))
141
 
 
142
 
    def rmdir(self, relpath):
143
 
        return self._call('rmdir', relpath)
144
 
 
145
 
    def stat(self, relpath):
146
 
        return self._call('stat', relpath)
147
 
 
148
 
 
149
 
class TestingChrootServer(ChrootServer):
150
 
 
151
 
    def __init__(self):
152
 
        """TestingChrootServer is not usable until setUp is called."""
153
 
 
154
 
    def setUp(self, backing_server=None):
155
 
        """Setup the Chroot on backing_server."""
156
 
        if backing_server is not None:
157
 
            self.backing_transport = get_transport(backing_server.get_url())
158
 
        else:
159
 
            self.backing_transport = get_transport('.')
160
 
        ChrootServer.setUp(self)
 
58
    def _filter(self, relpath):
 
59
        # A simplified version of PathFilteringTransport's _filter that omits
 
60
        # the call to self.server.filter_func.
 
61
        return self._relpath_from_server_root(relpath)
161
62
 
162
63
 
163
64
def get_test_permutations():
164
65
    """Return the permutations to be used in testing."""
165
 
    return [(ChrootTransport, TestingChrootServer),
166
 
            ]
 
66
    from bzrlib.tests import test_server
 
67
    return [(ChrootTransport, test_server.TestingChrootServer)]