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

913 by dcoles
Serve: Broke apart Serve into two parts - a download service (basically already
1
#!/usr/bin/python
2
3
# IVLE - Informatics Virtual Learning Environment
4
# Copyright (C) 2007-2008 The University of Melbourne
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
20
# Script: interpretservice
21
# Author: David Coles
22
# Date:   6/3/2007
23
24
# A CGI script for interpreting files.
25
26
import mimetypes
27
import os
28
import sys
29
import StringIO
30
import urlparse
31
import subprocess
32
33
from common import (cgirequest, studpath)
963 by mattgiuca
interpretservice: Now checks the MIME type of the file being served, and if it
34
import conf
35
import conf.mimetypes, conf.app, conf.app.server
36
37
serveservice_path = "/opt/ivle/scripts/serveservice"
913 by dcoles
Serve: Broke apart Serve into two parts - a download service (basically already
38
1063 by wagrant
Set PATH_INFO and PATH_TRANSLATED properly for student CGI scripts.
39
def determine_file_type(filename):
40
    filetype = mimetypes.guess_type(filename)[0]
41
    if filetype is None:
42
         filetype = conf.mimetypes.default_mimetype
43
    return filetype
44
913 by dcoles
Serve: Broke apart Serve into two parts - a download service (basically already
45
req = cgirequest.CGIRequest()
46
req.install_error_handler()
47
1063 by wagrant
Set PATH_INFO and PATH_TRANSLATED properly for student CGI scripts.
48
python = "/usr/bin/python"
49
913 by dcoles
Serve: Broke apart Serve into two parts - a download service (basically already
50
# Work out the parts of the URL
1063 by wagrant
Set PATH_INFO and PATH_TRANSLATED properly for student CGI scripts.
51
urlpath = urlparse.urlparse(req.path)[2]
52
filename = studpath.url_to_jailpaths(urlpath)[2]
53
path_info = ''
54
55
if filename is None:
56
    req.throw_error(req.HTTP_NOT_FOUND, "The path specified is invalid.")
57
58
if not os.access(filename, os.F_OK):
59
    # The given path doesn't exist. CGI lets us backtrack and put the path
60
    # elements through which we pass into PATH_INFO, so we try that.
61
    while not os.access(filename, os.F_OK):
62
        filename, path_info_frag = os.path.split(filename)
63
64
        # os.path.join tacks a / on the end even if the second fragment is
65
        # empty, which we don't want.
66
        if len(path_info) > 0:
67
            path_info = os.path.join(path_info_frag, path_info)
68
        else:
69
            path_info = path_info_frag
70
71
    # PATH_INFO is meant to start with a /.
72
    path_info = os.path.join(os.sep, path_info)
73
74
    # We now have a file that exists, but is it something that we're allowed
75
    # to execute? If not, we should 404 anyway.
76
    if determine_file_type(filename) not in conf.app.server.interpreters:
77
        req.throw_error(req.HTTP_NOT_FOUND,
78
            "The specified file (%s) does not exist." % urlpath)
79
966 by dcoles
Serve: Fixed up CWD and sys.path so that they refer to the script files
80
path = os.path.split(filename)[0]
913 by dcoles
Serve: Broke apart Serve into two parts - a download service (basically already
81
966 by dcoles
Serve: Fixed up CWD and sys.path so that they refer to the script files
82
# Everything should be done from the same directory as the script
993 by wagrant
interpretservice: Don't die horribly if we can't chdir into the path's parent.
83
try:
84
    os.chdir(path)
85
except OSError:
1063 by wagrant
Set PATH_INFO and PATH_TRANSLATED properly for student CGI scripts.
86
    req.throw_error(req.HTTP_NOT_FOUND,
87
        "The specified file (%s) does not exist." % urlpath)
993 by wagrant
interpretservice: Don't die horribly if we can't chdir into the path's parent.
88
966 by dcoles
Serve: Fixed up CWD and sys.path so that they refer to the script files
89
# Search the path for modules first
90
sys.path[0] = path
962 by dcoles
Serve: Fix the working directory used by interpretservice so that paths are
91
1063 by wagrant
Set PATH_INFO and PATH_TRANSLATED properly for student CGI scripts.
92
# Now we have lots of fun mangling environment variables to fix PATH_INFO
93
# and related stuff.
94
os.environ['PATH_INFO'] = path_info
95
if len(path_info) > 0:
96
    os.environ['SCRIPT_NAME'] = os.environ['SCRIPT_NAME'][:-len(path_info)]
97
98
# Set PATH_TRANSLATED. We trim the leading / from path_info or things go bad.
99
path_translated = studpath.url_to_jailpaths(path_info[1:])[2]
100
if path_translated is not None:
101
    if len(path_translated) == 0 or path_translated[0] != os.sep:
102
        path_translated = os.sep + path_translated
103
    os.environ['PATH_TRANSLATED'] = path_translated
104
105
if not os.access(filename, os.R_OK):
913 by dcoles
Serve: Broke apart Serve into two parts - a download service (basically already
106
    req.throw_error(req.HTTP_NOT_FOUND,
107
        "The specified file (%s) does not exist." % urlpath)
963 by mattgiuca
interpretservice: Now checks the MIME type of the file being served, and if it
108
elif os.path.isdir(filename):
109
    # 403 Forbidden error for visiting a directory
110
    # (Not giving a directory listing, since this can be seen by
111
    # the world at large. Directory contents are private).
112
    req.throw_error(req.HTTP_FORBIDDEN,
113
        "The path specified is a directory.")
1063 by wagrant
Set PATH_INFO and PATH_TRANSLATED properly for student CGI scripts.
114
elif determine_file_type(filename) in conf.app.server.interpreters:
963 by mattgiuca
interpretservice: Now checks the MIME type of the file being served, and if it
115
    # We'll save on a fork and execute in this python process
116
    # Let exceptions blow up normally again
117
    sys.excepthook = sys.__excepthook__
118
    execfile(filename, {})
119
    # Non-Python process should probably use something like
120
    # subprocess.call([python, filename])
913 by dcoles
Serve: Broke apart Serve into two parts - a download service (basically already
121
else:
963 by mattgiuca
interpretservice: Now checks the MIME type of the file being served, and if it
122
    # Otherwise, use the blacklist/whitelist to see if this file should be
123
    # served or disallowed
124
    if (conf.app.server.blacklist_served_filetypes and \
1063 by wagrant
Set PATH_INFO and PATH_TRANSLATED properly for student CGI scripts.
125
            determine_file_type(filename) in \
126
            conf.app.server.served_filetypes_blacklist) or \
963 by mattgiuca
interpretservice: Now checks the MIME type of the file being served, and if it
127
        (conf.app.server.served_filetypes_whitelist and \
1063 by wagrant
Set PATH_INFO and PATH_TRANSLATED properly for student CGI scripts.
128
            determine_file_type(filename) not in \
129
            conf.app.server.served_filetypes_whitelist):
913 by dcoles
Serve: Broke apart Serve into two parts - a download service (basically already
130
        req.throw_error(req.HTTP_FORBIDDEN,
963 by mattgiuca
interpretservice: Now checks the MIME type of the file being served, and if it
131
            "Files of this type are not allowed to be served.")
132
133
    execfile(serveservice_path)