~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-02 19:16:46 UTC
  • mfrom: (1332.1.4 console-fix)
  • Revision ID: coles.david@gmail.com-20091202191646-e55k83yd7sera1fk
When a console is restarted due to either being reset from the UI or killed 
due to inactivity/resource limits then will restart at the correct working 
directory.

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)
52
51
    Returns (None, None) if the path is empty.
53
52
 
54
53
    >>> stubconfig = {'paths': {'jails': {'mounts': '/jails'}}}
55
 
    >>> url_to_local(stubconfig, '')
56
 
    (None, None)
 
54
 
57
55
    >>> url_to_local(stubconfig, 'joe/foo/bar/baz')
58
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)
59
71
    """
60
72
 
61
73
    # First normalise the path
78
90
 
79
91
    return (user, path)
80
92
 
81
 
def url_to_jailpaths(urlpath):
 
93
def url_to_jailpaths(config, urlpath):
82
94
    """Given a URL path (part of a URL query string), returns a tuple of
83
95
        * the username of the student whose directory is being browsed
84
96
        * the absolute path where the jail will be located.
86
98
 
87
99
    urlpath: See urlpath in url_to_local.
88
100
 
89
 
    >>> url_to_jailpaths("joe/mydir/myfile")
90
 
    ('joe', '/var/lib/ivle/jailmounts/joe', '/home/joe/mydir/myfile')
 
101
    >>> stubconfig = {'paths': {'jails': {'mounts': '/jails'}}}
91
102
 
92
 
    >>> 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")
93
110
    (None, None, None)
94
111
    """
95
112
    # First normalise the path
96
113
    urlpath = os.path.normpath(urlpath)
97
 
    # Now if it begins with ".." then it's illegal
98
 
    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):
99
116
        return (None, None, None)
100
117
    # Note: User can be a group name. There is absolutely no difference in our
101
118
    # current directory scheme.
102
119
    (user, subpath) = util.split_path(urlpath)
103
120
    if user is None: return (None, None, None)
104
121
 
105
 
    jail = os.path.join(ivle.conf.jail_base, user)
106
 
    path = url_path_to_jail_path(urlpath)
 
122
    jail = os.path.join(config['paths']['jails']['mounts'], user)
 
123
    path = to_home_path(urlpath)
107
124
 
108
125
    return (user, jail, path)
109
126
 
110
 
def url_path_to_jail_path(urlpath):
111
 
    """Given a URL path (eg. joe/foo/bar/baz), returns a path within the jail.
 
127
def to_home_path(urlpath):
 
128
    """Given a URL path (eg. joe/foo/bar/baz), returns a path within the home.
112
129
 
113
 
    >>> url_path_to_jail_path('joe/foo/bar/baz')
 
130
    >>> to_home_path('joe/foo/bar/baz')
114
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
115
136
    """
 
137
 
 
138
    urlpath = os.path.normpath(urlpath)
 
139
    # If it begins with '..', it's illegal.
 
140
    if urlpath.startswith(".."):
 
141
        return None
 
142
 
116
143
    return os.path.join('/home', urlpath)
117
144
 
118
145
def svnpublished(path):