3
# IVLE - Informatics Virtual Learning Environment
4
# Copyright (C) 2007-2008 The University of Melbourne
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.
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.
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
20
# Script: interpretservice
24
# A CGI script for interpreting files.
33
from ivle import (cgirequest, studpath)
35
import ivle.conf.mimetypes
37
import ivle.conf.app.server
39
# XXX: using dirname because we don't have path config in the jailconf.
40
serveservice_path = os.path.join(os.path.dirname(__file__), 'serveservice')
42
def determine_file_type(filename):
43
filetype = mimetypes.guess_type(filename)[0]
45
filetype = ivle.conf.mimetypes.default_mimetype
48
req = cgirequest.CGIRequest()
49
req.install_error_handler()
51
python = "/usr/bin/python"
53
# Work out the parts of the URL
54
urlpath = urlparse.urlparse(req.path)[2]
55
filename = studpath.url_to_jailpaths(urlpath)[2]
59
req.throw_error(req.HTTP_NOT_FOUND, "The path specified is invalid.")
61
if not os.access(filename, os.F_OK):
62
# The given path doesn't exist. CGI lets us backtrack and put the path
63
# elements through which we pass into PATH_INFO, so we try that.
64
while not os.access(filename, os.F_OK):
65
filename, path_info_frag = os.path.split(filename)
67
# os.path.join tacks a / on the end even if the second fragment is
68
# empty, which we don't want.
69
if len(path_info) > 0:
70
path_info = os.path.join(path_info_frag, path_info)
72
path_info = path_info_frag
74
# PATH_INFO is meant to start with a /.
75
path_info = os.path.join(os.sep, path_info)
77
# We now have a file that exists, but is it something that we're allowed
78
# to execute? If not, we should 404 anyway.
79
if determine_file_type(filename) not in ivle.conf.app.server.interpreters:
80
req.throw_error(req.HTTP_NOT_FOUND,
81
"The specified file (%s) does not exist." % urlpath)
83
path = os.path.split(filename)[0]
85
# Everything should be done from the same directory as the script
89
req.throw_error(req.HTTP_NOT_FOUND,
90
"The specified file (%s) does not exist." % urlpath)
92
# Search the path for modules first
95
# Now we have lots of fun mangling environment variables to fix PATH_INFO
97
os.environ['PATH_INFO'] = path_info
98
if len(path_info) > 0:
99
os.environ['SCRIPT_NAME'] = os.environ['SCRIPT_NAME'][:-len(path_info)]
101
# Set PATH_TRANSLATED. We trim the leading / from path_info or things go bad.
102
path_translated = studpath.url_to_jailpaths(path_info[1:])[2]
103
if path_translated is not None:
104
if len(path_translated) == 0 or path_translated[0] != os.sep:
105
path_translated = os.sep + path_translated
106
os.environ['PATH_TRANSLATED'] = path_translated
108
if not os.access(filename, os.R_OK):
109
req.throw_error(req.HTTP_NOT_FOUND,
110
"The specified file (%s) does not exist." % urlpath)
111
elif os.path.isdir(filename):
112
# 403 Forbidden error for visiting a directory
113
# (Not giving a directory listing, since this can be seen by
114
# the world at large. Directory contents are private).
115
req.throw_error(req.HTTP_FORBIDDEN,
116
"The path specified is a directory.")
117
elif determine_file_type(filename) in ivle.conf.app.server.interpreters:
118
# We'll save on a fork and execute in this python process
119
# Let exceptions blow up normally again
120
sys.excepthook = sys.__excepthook__
121
execfile(filename, {})
122
# Non-Python process should probably use something like
123
# subprocess.call([python, filename])
125
# Otherwise, use the blacklist/whitelist to see if this file should be
126
# served or disallowed
127
if (ivle.conf.app.server.blacklist_served_filetypes and \
128
determine_file_type(filename) in \
129
ivle.conf.app.server.served_filetypes_blacklist) or \
130
(ivle.conf.app.server.served_filetypes_whitelist and \
131
determine_file_type(filename) not in \
132
ivle.conf.app.server.served_filetypes_whitelist):
133
req.throw_error(req.HTTP_FORBIDDEN,
134
"Files of this type are not allowed to be served.")
136
execfile(serveservice_path)