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