2
# Copyright (C) 2007-2008 The University of Melbourne
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.
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.
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
22
# Runs a student script in a safe execution environment.
24
from common import studpath
33
# TODO: Make progressive output work
34
# Question: Will having a large buffer size stop progressive output from
35
# working on smaller output
37
CGI_BLOCK_SIZE = 65535
39
def interpret_file(req, owner, filename, interpreter):
40
"""Serves a file by interpreting it using one of IVLE's builtin
41
interpreters. All interpreters are intended to run in the user's jail. The
42
jail location is provided as an argument to the interpreter but it is up
43
to the individual interpreters to create the jail.
45
req: An IVLE request object.
46
owner: Username of the user who owns the file being served.
47
filename: Filename in the local file system.
48
interpreter: A function object to call.
50
# Make sure the file exists (otherwise some interpreters may not actually
52
# Don't test for execute permission, that will only be required for
53
# certain interpreters.
54
if not os.access(filename, os.R_OK):
55
req.throw_error(req.HTTP_NOT_FOUND)
57
# Get the UID of the owner of the file
58
# (Note: files are executed by their owners, not the logged in user.
59
# This ensures users are responsible for their own programs and also
60
# allows them to be executed by the public).
62
(_,_,uid,_,_,_,_) = pwd.getpwnam(owner)
64
# The user does not exist. This should have already failed the
66
req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR)
68
# Split up req.path again, this time with respect to the jail
69
(_, jail_dir, path) = studpath.url_to_jailpaths(req.path)
70
path = os.path.join('/', path)
71
(working_dir, _) = os.path.split(path)
72
# jail_dir is the absolute jail directory.
73
# path is the filename relative to the user's jail.
74
# working_dir is the directory containing the file relative to the user's
76
# (Note that paths "relative" to the jail actually begin with a '/' as
77
# they are absolute in the jailspace)
79
return interpreter(uid, jail_dir, working_dir, path, req)
82
"""Stores flags regarding the state of reading CGI output."""
84
self.started_cgi_body = False
85
self.got_cgi_headers = False
86
self.wrote_html_warning = False
88
self.headers = {} # Header names : values
90
def execute_cgi(interpreter, trampoline, uid, jail_dir, working_dir,
93
trampoline: Full path on the local system to the CGI wrapper program
95
uid: User ID of the owner of the file.
96
jail_dir: Absolute path of owner's jail directory.
97
working_dir: Directory containing the script file relative to owner's
99
script_path: CGI script relative to the owner's jail.
100
req: IVLE request object.
102
The called CGI wrapper application shall be called using popen and receive
103
the HTTP body on stdin. It shall receive the CGI environment variables to
107
# Get the student program's directory and execute it from that context.
108
(tramp_dir, _) = os.path.split(trampoline)
110
# TODO: Don't create a file if the body length is known to be 0
111
# Write the HTTP body to a temporary file so it can be passed as a *real*
118
f.seek(0) # Rewind, for reading
120
# usage: tramp uid jail_dir working_dir script_path
121
pid = subprocess.Popen(
122
[trampoline, str(uid), jail_dir, working_dir, interpreter,
124
stdin=f, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
127
# process_cgi_line: Reads a single line of CGI output and processes it.
128
# Prints to req, and also does fancy HTML warnings if Content-Type
130
cgiflags = CGIFlags()
132
# Read from the process's stdout into req
133
data = pid.stdout.read(CGI_BLOCK_SIZE)
135
process_cgi_output(req, data, cgiflags)
136
data = pid.stdout.read(CGI_BLOCK_SIZE)
138
# If we haven't processed headers yet, now is a good time
139
if not cgiflags.started_cgi_body:
140
process_cgi_output(req, '\n', cgiflags)
142
# If we wrote an HTML warning header, write the footer
143
if cgiflags.wrote_html_warning:
149
def process_cgi_output(req, data, cgiflags):
150
"""Processes a chunk of CGI output. data is a string of arbitrary length;
151
some arbitrary chunk of output written by the CGI script."""
152
if cgiflags.started_cgi_body:
153
if cgiflags.wrote_html_warning:
154
# HTML escape text if wrote_html_warning
155
req.write(cgi.escape(data))
159
# Break data into lines of CGI header data.
160
linebuf = cgiflags.linebuf + data
161
# First see if we can split all header data
162
split = linebuf.split('\r\n\r\n', 1)
164
# Allow UNIX newlines instead
165
split = linebuf.split('\n\n', 1)
167
# Haven't seen all headers yet. Buffer and come back later.
168
cgiflags.linebuf = linebuf
173
cgiflags.linebuf = ""
174
cgiflags.started_cgi_body = True
175
# Process all the header lines
176
split = headers.split('\r\n', 1)
178
split = headers.split('\n', 1)
180
process_cgi_header_line(req, split[0], cgiflags)
181
if len(split) == 1: break
183
if cgiflags.wrote_html_warning:
184
# We're done with headers. Treat the rest as data.
185
data = headers + '\n' + data
187
split = headers.split('\r\n', 1)
189
split = headers.split('\n', 1)
191
# Check to make sure the required headers were written
192
if cgiflags.wrote_html_warning:
193
# We already reported an error, that's enough
195
elif "Content-Type" in cgiflags.headers:
197
elif "Location" in cgiflags.headers:
198
if ("Status" in cgiflags.headers and req.status >= 300
199
and req.status < 400):
202
message = """You did not write a valid status code for
203
the given location. To make a redirect, you may wish to try:</p>
204
<pre style="margin-left: 1em">Status: 302 Found
205
Location: <redirect address></pre>"""
206
write_html_warning(req, message)
207
cgiflags.wrote_html_warning = True
209
message = """You did not print a Content-Type header.
210
CGI requires that you print a "Content-Type". You may wish to try:</p>
211
<pre style="margin-left: 1em">Content-Type: text/html</pre>"""
212
write_html_warning(req, message)
213
cgiflags.wrote_html_warning = True
215
# Call myself to flush out the extra bit of data we read
216
process_cgi_output(req, data, cgiflags)
218
def process_cgi_header_line(req, line, cgiflags):
219
"""Process a line of CGI header data. line is a string representing a
220
complete line of text, stripped and without the newline.
223
name, value = line.split(':', 1)
225
# No colon. The user did not write valid headers.
226
if len(cgiflags.headers) == 0:
227
# First line was not a header line. We can assume this is not
229
message = """You did not print a CGI header.
230
CGI requires that you print a "Content-Type". You may wish to try:</p>
231
<pre style="margin-left: 1em">Content-Type: text/html</pre>"""
233
# They printed some header at least, but there was an invalid
235
message = """You printed an invalid CGI header. You need to leave
236
a blank line after the headers, before writing the page contents."""
237
write_html_warning(req, message)
238
cgiflags.wrote_html_warning = True
239
# Handle the rest of this line as normal data
240
process_cgi_output(req, line + '\n', cgiflags)
244
value = value.strip()
245
if name == "Content-Type":
246
req.content_type = value
247
elif name == "Location":
249
elif name == "Status":
250
# Must be an integer, followed by a space, and then the status line
251
# which we ignore (seems like Apache has no way to send a custom
254
req.status = int(value.split(' ', 1)[0])
256
message = """The "Status" CGI header was invalid. You need to
257
print a number followed by a message, such as "302 Found"."""
258
write_html_warning(req, message)
259
cgiflags.wrote_html_warning = True
260
# Handle the rest of this line as normal data
261
process_cgi_output(req, line + '\n', cgiflags)
263
# Generic HTTP header
264
# FIXME: Security risk letting users write arbitrary headers?
265
req.headers_out[name] = value
266
cgiflags.headers[name] = value
268
def write_html_warning(req, text):
269
"""Prints an HTML warning about invalid CGI interaction on the part of the
270
user. text may contain HTML markup."""
271
req.content_type = "text/html"
272
req.write("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
273
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
274
<html xmlns="http://www.w3.org/1999/xhtml">
276
<meta http-equiv="Content-Type"
277
content="text/html; charset=utf-8" />
279
<body style="margin: 0; padding: 0; font-family: sans-serif;">
280
<div style="background-color: #faa; border-bottom: 1px solid black;
282
<p><strong>Warning</strong>: %s
284
<div style="margin: 8px;">
288
location_cgi_python = os.path.join(conf.ivle_install_dir,
291
# Mapping of interpreter names (as given in conf/app/server.py) to
292
# interpreter functions.
294
interpreter_objects = {
296
: functools.partial(execute_cgi, "/usr/bin/python",
297
location_cgi_python),