~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to ivle/studpath.py

  • Committer: David Coles
  • Date: 2009-12-10 01:18:36 UTC
  • Revision ID: coles.david@gmail.com-20091210011836-6kk2omcmr9hvphj0
Correct documentation's system diagram (console communication goes via Application Slaves)

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
import stat
29
29
import pysvn
30
30
 
31
 
import ivle.conf
32
31
from ivle import util
33
32
 
34
33
# Make a Subversion client object (for published)
35
34
svnclient = pysvn.Client()
36
35
 
37
 
def url_to_local(urlpath):
 
36
def url_to_local(config, urlpath):
38
37
    """Given a URL path (part of a URL query string, see below), returns a
39
38
    tuple of
40
39
        * the username of the student whose directory is being browsed
51
50
 
52
51
    Returns (None, None) if the path is empty.
53
52
 
54
 
    See also: ivle.conf.jail_base
 
53
    >>> stubconfig = {'paths': {'jails': {'mounts': '/jails'}}}
 
54
 
 
55
    >>> url_to_local(stubconfig, 'joe/foo/bar/baz')
 
56
    ('joe', '/jails/joe/home/joe/foo/bar/baz')
 
57
    >>> url_to_local(stubconfig, 'joe')
 
58
    ('joe', '/jails/joe/home/joe')
 
59
    >>> url_to_local(stubconfig, 'joe/')
 
60
    ('joe', '/jails/joe/home/joe')
 
61
 
 
62
    We have some protection from various potential attacks. An empty,
 
63
    absolute, or ..-prefixed path yields a special result.
 
64
 
 
65
    >>> url_to_local(stubconfig, '')
 
66
    (None, None)
 
67
    >>> url_to_local(stubconfig, '/foo')
 
68
    (None, None)
 
69
    >>> url_to_local(stubconfig, '../bar')
 
70
    (None, None)
55
71
    """
 
72
 
56
73
    # First normalise the path
57
74
    urlpath = os.path.normpath(urlpath)
58
75
    # Now if it begins with ".." or separator, then it's illegal
68
85
    # accordance with our directory scheme.
69
86
    # (The first time is the name of the jail, the second is the user's home
70
87
    # directory within the jail).
71
 
    path = os.path.join(ivle.conf.jail_base, user, 'home', urlpath)
 
88
    path = os.path.join(config['paths']['jails']['mounts'],
 
89
                        user, 'home', urlpath)
72
90
 
73
91
    return (user, path)
74
92
 
75
 
def url_to_jailpaths(urlpath):
 
93
def url_to_jailpaths(config, urlpath):
76
94
    """Given a URL path (part of a URL query string), returns a tuple of
77
95
        * the username of the student whose directory is being browsed
78
96
        * the absolute path where the jail will be located.
80
98
 
81
99
    urlpath: See urlpath in url_to_local.
82
100
 
83
 
    >>> url_to_jailpaths("joe/mydir/myfile")
84
 
    ('joe', '/var/lib/ivle/jailmounts/joe', '/home/joe/mydir/myfile')
 
101
    >>> stubconfig = {'paths': {'jails': {'mounts': '/jails'}}}
85
102
 
86
 
    >>> url_to_jailpaths("")
 
103
    >>> url_to_jailpaths(stubconfig, "joe/mydir//myfile/.././myfile")
 
104
    ('joe', '/jails/joe', '/home/joe/mydir/myfile')
 
105
    >>> url_to_jailpaths(stubconfig, "")
 
106
    (None, None, None)
 
107
    >>> url_to_jailpaths(stubconfig, "../foo")
 
108
    (None, None, None)
 
109
    >>> url_to_jailpaths(stubconfig, "/foo")
87
110
    (None, None, None)
88
111
    """
89
112
    # First normalise the path
90
113
    urlpath = os.path.normpath(urlpath)
91
 
    # Now if it begins with ".." then it's illegal
92
 
    if urlpath.startswith(".."):
 
114
    # Now if it begins with "..", or is absolute, then it's illegal
 
115
    if urlpath.startswith("..") or os.path.isabs(urlpath):
93
116
        return (None, None, None)
94
117
    # Note: User can be a group name. There is absolutely no difference in our
95
118
    # current directory scheme.
96
119
    (user, subpath) = util.split_path(urlpath)
97
120
    if user is None: return (None, None, None)
98
121
 
99
 
    jail = os.path.join(ivle.conf.jail_base, user)
100
 
    path = os.path.join('/home', urlpath)
 
122
    jail = os.path.join(config['paths']['jails']['mounts'], user)
 
123
    path = to_home_path(urlpath)
101
124
 
102
125
    return (user, jail, path)
103
126
 
 
127
def to_home_path(urlpath):
 
128
    """Given a URL path (eg. joe/foo/bar/baz), returns a path within the home.
 
129
 
 
130
    >>> to_home_path('joe/foo/bar/baz')
 
131
    '/home/joe/foo/bar/baz'
 
132
    >>> to_home_path('joe/foo//bar/baz/../../')
 
133
    '/home/joe/foo'
 
134
    >>> to_home_path('joe/foo//bar/baz/../../../../../') is None
 
135
    True
 
136
    """
 
137
 
 
138
    urlpath = os.path.normpath(urlpath)
 
139
    # If it begins with '..', it's illegal.
 
140
    if urlpath.startswith(".."):
 
141
        return None
 
142
 
 
143
    return os.path.join('/home', urlpath)
 
144
 
104
145
def svnpublished(path):
105
146
    """Given a path on the LOCAL file system, determines whether the path has
106
147
    its "ivle:published" property active (in subversion). Returns True
162
203
    Same interface as "authorize" - None on success, HTTP_FORBIDDEN exception
163
204
    raised on failure.
164
205
    """
165
 
    _, path = url_to_local(req.path)
 
206
    _, path = url_to_local(req.config, req.path)
166
207
 
167
208
    # Walk up the tree, and find the deepest directory.
168
209
    while not os.path.isdir(path):