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

« back to all changes in this revision

Viewing changes to www/common/studpath.py

  • Committer: me at id
  • Date: 2009-01-15 01:29:07 UTC
  • mto: This revision was merged to the branch mainline in revision 1090.
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:branches%2Fstorm:1147
ivle.makeuser: Don't create a local.auth. It wasn't used anywhere, and seems
    to have only been there for historical reasons.
setup.configure: Remove svn_auth_local from the config.py boilerplate; it was
    used solely by ivle.makeuser.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# IVLE - Informatics Virtual Learning Environment
2
 
# Copyright (C) 2007-2008 The University of Melbourne
3
 
#
4
 
# This program is free software; you can redistribute it and/or modify
5
 
# it under the terms of the GNU General Public License as published by
6
 
# the Free Software Foundation; either version 2 of the License, or
7
 
# (at your option) any later version.
8
 
#
9
 
# This program is distributed in the hope that it will be useful,
10
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
# GNU General Public License for more details.
13
 
#
14
 
# You should have received a copy of the GNU General Public License
15
 
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
 
 
18
 
# Module: common.studpath
19
 
# Author: Matt Giuca
20
 
# Date:   14/12/2007
21
 
 
22
 
# Provides functions for translating URLs into physical locations in the
23
 
# student directories in the local file system.
24
 
# Also performs common authorization, disallowing students from visiting paths
25
 
# they dont own.
26
 
 
27
 
import os
28
 
 
29
 
import conf
30
 
from common import util
31
 
 
32
 
def url_to_local(urlpath):
33
 
    """Given a URL path (part of a URL query string, see below), returns a
34
 
    tuple of
35
 
        * the username of the student whose directory is being browsed
36
 
        * the absolute path in the file system where that file will be
37
 
            found within the student directories.
38
 
 
39
 
    urlpath: Part of the URL, but only the part *after* the application. For
40
 
    instance, given the URL "/ivle/browse/joe/home/mydir/myfile", urlpath will
41
 
    be just "joe/home/mydir/myfile". The expected result is something like
42
 
    ("joe", "/home/informatics/jails/joe/home/joe/home/mydir/myfile").
43
 
    Note that the actual location is not guaranteed by this interface (this
44
 
    function serves as a single point of control as to how URLs map onto
45
 
    student directories).
46
 
 
47
 
    Returns (None, None) if the path is empty.
48
 
 
49
 
    See also: conf.jail_base
50
 
    """
51
 
    # First normalise the path
52
 
    urlpath = os.path.normpath(urlpath)
53
 
    # Now if it begins with ".." or separator, then it's illegal
54
 
    if urlpath.startswith("..") or urlpath.startswith(os.sep):
55
 
        return (None, None)
56
 
    # Note: User can be a group name. There is absolutely no difference in our
57
 
    # current directory scheme.
58
 
    (user, subpath) = util.split_path(urlpath)
59
 
    if user is None: return (None, None)
60
 
 
61
 
    # Join the user onto 'home' then the full path specified.
62
 
    # This results in the user's name being repeated twice, which is in
63
 
    # accordance with our directory scheme.
64
 
    # (The first time is the name of the jail, the second is the user's home
65
 
    # directory within the jail).
66
 
    path = os.path.join(conf.jail_base, user, 'home', urlpath)
67
 
 
68
 
    return (user, path)
69
 
 
70
 
def url_to_jailpaths(urlpath):
71
 
    """Given a URL path (part of a URL query string), returns a tuple of
72
 
        * the username of the student whose directory is being browsed
73
 
        * the absolute path where the jail will be located.
74
 
        * the path of the file relative to the jail.
75
 
 
76
 
    urlpath: See urlpath in url_to_local.
77
 
 
78
 
    >>> url_to_jailpaths("joe/home/mydir/myfile")
79
 
    ("joe", "/home/informatics/jails/joe", "home/joe/home/mydir/myfile")
80
 
 
81
 
    >>> url_to_jailpaths("")
82
 
    (None, None, None)
83
 
    """
84
 
    # First normalise the path
85
 
    urlpath = os.path.normpath(urlpath)
86
 
    # Now if it begins with ".." then it's illegal
87
 
    if urlpath.startswith(".."):
88
 
        return (None, None, None)
89
 
    # Note: User can be a group name. There is absolutely no difference in our
90
 
    # current directory scheme.
91
 
    (user, subpath) = util.split_path(urlpath)
92
 
    if user is None: return (None, None, None)
93
 
 
94
 
    jail = os.path.join(conf.jail_base, user)
95
 
    path = os.path.join('home', urlpath)
96
 
 
97
 
    return (user, jail, path)
98
 
 
99
 
def authorize(req):
100
 
    """Given a request, checks whether req.username is allowed to
101
 
    access req.path. Returns None on authorization success. Raises
102
 
    HTTP_FORBIDDEN on failure.
103
 
 
104
 
    This is for general authorization (assuming not in public mode; this is
105
 
    the standard auth code for fileservice, download and serve).
106
 
    """
107
 
    # TODO: Groups
108
 
    # First normalise the path
109
 
    urlpath = os.path.normpath(req.path)
110
 
    # Now if it begins with ".." or separator, then it's illegal
111
 
    if urlpath.startswith("..") or urlpath.startswith(os.sep):
112
 
        req.throw_error(req.HTTP_FORBIDDEN)
113
 
 
114
 
    (owner, _) = util.split_path(urlpath)
115
 
    if req.username != owner:
116
 
        req.throw_error(req.HTTP_FORBIDDEN)