32
30
from ivle import util
34
# Make a Subversion client object (for published)
35
svnclient = pysvn.Client()
37
32
def url_to_local(config, urlpath):
38
33
"""Given a URL path (part of a URL query string, see below), returns a
52
47
Returns (None, None) if the path is empty.
54
49
>>> stubconfig = {'paths': {'jails': {'mounts': '/jails'}}}
55
>>> url_to_local(stubconfig, '')
57
51
>>> url_to_local(stubconfig, 'joe/foo/bar/baz')
58
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')
61
69
# First normalise the path
79
87
return (user, path)
81
def url_to_jailpaths(urlpath):
89
def url_to_jailpaths(config, urlpath):
82
90
"""Given a URL path (part of a URL query string), returns a tuple of
83
91
* the username of the student whose directory is being browsed
84
92
* the absolute path where the jail will be located.
87
95
urlpath: See urlpath in url_to_local.
89
>>> url_to_jailpaths("joe/mydir/myfile")
90
('joe', '/var/lib/ivle/jailmounts/joe', '/home/joe/mydir/myfile')
97
>>> stubconfig = {'paths': {'jails': {'mounts': '/jails'}}}
92
>>> 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")
93
106
(None, None, None)
95
108
# First normalise the path
96
109
urlpath = os.path.normpath(urlpath)
97
# Now if it begins with ".." then it's illegal
98
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):
99
112
return (None, None, None)
100
113
# Note: User can be a group name. There is absolutely no difference in our
101
114
# current directory scheme.
102
115
(user, subpath) = util.split_path(urlpath)
103
116
if user is None: return (None, None, None)
105
jail = os.path.join(ivle.conf.jail_base, user)
118
jail = os.path.join(config['paths']['jails']['mounts'], user)
106
119
path = to_home_path(urlpath)
108
121
return (user, jail, path)
113
126
>>> to_home_path('joe/foo/bar/baz')
114
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(".."):
116
139
return os.path.join('/home', urlpath)
118
def svnpublished(path):
119
"""Given a path on the LOCAL file system, determines whether the path has
120
its "ivle:published" property active (in subversion). Returns True
122
# Read SVN properties for this path
124
props = svnclient.propget("ivle:published", path, recurse=False)
125
except pysvn.ClientError:
126
# Not under version control? Then it isn't published.
128
return len(props) > 0
130
141
def published(path):
131
142
"""Given a path on the LOCAL file system, determines whether the path has a
132
143
'.published' file. Returns True or False."""