32
30
from ivle import util
34
# Make a Subversion client object (for published)
35
svnclient = pysvn.Client()
37
def url_to_local(urlpath):
32
def url_to_local(config, urlpath):
38
33
"""Given a URL path (part of a URL query string, see below), returns a
40
35
* the username of the student whose directory is being browsed
52
47
Returns (None, None) if the path is empty.
54
See also: ivle.conf.jail_base
49
>>> stubconfig = {'paths': {'jails': {'mounts': '/jails'}}}
51
>>> url_to_local(stubconfig, 'joe/foo/bar/baz')
52
('joe', '/jails/joe/home/joe/foo/bar/baz')
53
>>> url_to_local(stubconfig, 'joe')
54
('joe', '/jails/joe/home/joe')
55
>>> url_to_local(stubconfig, 'joe/')
56
('joe', '/jails/joe/home/joe')
58
We have some protection from various potential attacks. An empty,
59
absolute, or ..-prefixed path yields a special result.
61
>>> url_to_local(stubconfig, '')
63
>>> url_to_local(stubconfig, '/foo')
65
>>> url_to_local(stubconfig, '../bar')
56
69
# First normalise the path
57
70
urlpath = os.path.normpath(urlpath)
58
71
# Now if it begins with ".." or separator, then it's illegal
68
81
# accordance with our directory scheme.
69
82
# (The first time is the name of the jail, the second is the user's home
70
83
# directory within the jail).
71
path = os.path.join(ivle.conf.jail_base, user, 'home', urlpath)
84
path = os.path.join(config['paths']['jails']['mounts'],
85
user, 'home', urlpath)
73
87
return (user, path)
75
def url_to_jailpaths(urlpath):
89
def url_to_jailpaths(config, urlpath):
76
90
"""Given a URL path (part of a URL query string), returns a tuple of
77
91
* the username of the student whose directory is being browsed
78
92
* the absolute path where the jail will be located.
81
95
urlpath: See urlpath in url_to_local.
83
>>> url_to_jailpaths("joe/mydir/myfile")
84
('joe', '/var/lib/ivle/jailmounts/joe', '/home/joe/mydir/myfile')
97
>>> stubconfig = {'paths': {'jails': {'mounts': '/jails'}}}
86
>>> url_to_jailpaths("")
99
>>> url_to_jailpaths(stubconfig, "joe/mydir//myfile/.././myfile")
100
('joe', '/jails/joe', '/home/joe/mydir/myfile')
101
>>> url_to_jailpaths(stubconfig, "")
103
>>> url_to_jailpaths(stubconfig, "../foo")
105
>>> url_to_jailpaths(stubconfig, "/foo")
87
106
(None, None, None)
89
108
# First normalise the path
90
109
urlpath = os.path.normpath(urlpath)
91
# Now if it begins with ".." then it's illegal
92
if urlpath.startswith(".."):
110
# Now if it begins with "..", or is absolute, then it's illegal
111
if urlpath.startswith("..") or os.path.isabs(urlpath):
93
112
return (None, None, None)
94
113
# Note: User can be a group name. There is absolutely no difference in our
95
114
# current directory scheme.
96
115
(user, subpath) = util.split_path(urlpath)
97
116
if user is None: return (None, None, None)
99
jail = os.path.join(ivle.conf.jail_base, user)
100
path = os.path.join('/home', urlpath)
118
jail = os.path.join(config['paths']['jails']['mounts'], user)
119
path = to_home_path(urlpath)
102
121
return (user, jail, path)
104
def svnpublished(path):
105
"""Given a path on the LOCAL file system, determines whether the path has
106
its "ivle:published" property active (in subversion). Returns True
108
# Read SVN properties for this path
110
props = svnclient.propget("ivle:published", path, recurse=False)
111
except pysvn.ClientError:
112
# Not under version control? Then it isn't published.
114
return len(props) > 0
123
def to_home_path(urlpath):
124
"""Given a URL path (eg. joe/foo/bar/baz), returns a path within the home.
126
>>> to_home_path('joe/foo/bar/baz')
127
'/home/joe/foo/bar/baz'
128
>>> to_home_path('joe/foo//bar/baz/../../')
130
>>> to_home_path('joe/foo//bar/baz/../../../../../') is None
134
urlpath = os.path.normpath(urlpath)
135
# If it begins with '..', it's illegal.
136
if urlpath.startswith(".."):
139
return os.path.join('/home', urlpath)
116
141
def published(path):
117
142
"""Given a path on the LOCAL file system, determines whether the path has a