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
# NOTE: This script currently disables cookies. This means students will be
25
# unable to write session-based or stateful web applications. This is done for
26
# security reasons (we do not want the students to see the IVLE cookie of
27
# whoever is visiting their pages).
28
# This can be resolved but needs careful sanitisation. See fixup_environ.
30
from common import studpath
40
# TODO: Make progressive output work
41
# Question: Will having a large buffer size stop progressive output from
42
# working on smaller output
44
CGI_BLOCK_SIZE = 65535
49
"""Get the unix uid corresponding to the given login name.
50
If it is not in the dictionary of uids, then consult the
51
database and retrieve an update of the user table."""
57
res = conn.get_all('login', ['login', 'unixid'])
59
return (flds['login'], flds['unixid'])
60
uids = dict(map(repack,res))
64
def interpret_file(req, owner, jail_dir, filename, interpreter):
65
"""Serves a file by interpreting it using one of IVLE's builtin
66
interpreters. All interpreters are intended to run in the user's jail. The
67
jail location is provided as an argument to the interpreter but it is up
68
to the individual interpreters to create the jail.
70
req: An IVLE request object.
71
owner: Username of the user who owns the file being served.
72
jail_dir: Absolute path to the user's jail.
73
filename: Absolute filename within the user's jail.
74
interpreter: A function object to call.
76
# We can't test here whether or not the target file actually exists,
77
# because the apache user may not have permission. Instead we have to
78
# rely on the interpreter generating an error.
79
if filename.startswith(os.sep):
80
filename_abs = filename
81
filename_rel = filename[1:]
83
filename_abs = os.path.join(os.sep, filename)
84
filename_rel = filename
86
# Get the UID of the owner of the file
87
# (Note: files are executed by their owners, not the logged in user.
88
# This ensures users are responsible for their own programs and also
89
# allows them to be executed by the public).
92
# Split up req.path again, this time with respect to the jail
93
(working_dir, _) = os.path.split(filename_abs)
94
# jail_dir is the absolute jail directory.
95
# path is the filename relative to the user's jail.
96
# working_dir is the directory containing the file relative to the user's
98
# (Note that paths "relative" to the jail actually begin with a '/' as
99
# they are absolute in the jailspace)
101
return interpreter(uid, jail_dir, working_dir, filename_abs, req)
104
"""Stores flags regarding the state of reading CGI output."""
106
self.started_cgi_body = False
107
self.got_cgi_headers = False
108
self.wrote_html_warning = False
110
self.headers = {} # Header names : values
112
def execute_cgi(interpreter, trampoline, uid, jail_dir, working_dir,
115
trampoline: Full path on the local system to the CGI wrapper program
117
uid: User ID of the owner of the file.
118
jail_dir: Absolute path of owner's jail directory.
119
working_dir: Directory containing the script file relative to owner's
121
script_path: CGI script relative to the owner's jail.
122
req: IVLE request object.
124
The called CGI wrapper application shall be called using popen and receive
125
the HTTP body on stdin. It shall receive the CGI environment variables to
129
# Get the student program's directory and execute it from that context.
130
(tramp_dir, _) = os.path.split(trampoline)
132
# TODO: Don't create a file if the body length is known to be 0
133
# Write the HTTP body to a temporary file so it can be passed as a *real*
140
f.seek(0) # Rewind, for reading
142
# Set up the environment
143
# This automatically asks mod_python to load up the CGI variables into the
144
# environment (which is a good first approximation)
145
old_env = os.environ.copy()
146
for k in os.environ.keys():
148
for (k,v) in req.get_cgi_environ().items():
152
# usage: tramp uid jail_dir working_dir script_path
153
pid = subprocess.Popen(
154
[trampoline, str(uid), jail_dir, working_dir, interpreter,
156
stdin=f, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
159
# Restore the environment
160
for k in os.environ.keys():
162
for (k,v) in old_env.items():
165
# process_cgi_line: Reads a single line of CGI output and processes it.
166
# Prints to req, and also does fancy HTML warnings if Content-Type
168
cgiflags = CGIFlags()
170
# Read from the process's stdout into req
171
data = pid.stdout.read(CGI_BLOCK_SIZE)
173
process_cgi_output(req, data, cgiflags)
174
data = pid.stdout.read(CGI_BLOCK_SIZE)
176
# If we haven't processed headers yet, now is a good time
177
if not cgiflags.started_cgi_body:
178
process_cgi_output(req, '\n', cgiflags)
180
# If we wrote an HTML warning header, write the footer
181
if cgiflags.wrote_html_warning:
187
def process_cgi_output(req, data, cgiflags):
188
"""Processes a chunk of CGI output. data is a string of arbitrary length;
189
some arbitrary chunk of output written by the CGI script."""
190
if cgiflags.started_cgi_body:
191
if cgiflags.wrote_html_warning:
192
# HTML escape text if wrote_html_warning
193
req.write(cgi.escape(data))
197
# Break data into lines of CGI header data.
198
linebuf = cgiflags.linebuf + data
199
# First see if we can split all header data
200
# We need to get the double CRLF- or LF-terminated headers, whichever
201
# is smaller, as either sequence may appear somewhere in the body.
202
usplit = linebuf.split('\n\n', 1)
203
wsplit = linebuf.split('\r\n\r\n', 1)
204
split = len(usplit[0]) > len(wsplit[0]) and wsplit or usplit
206
# Haven't seen all headers yet. Buffer and come back later.
207
cgiflags.linebuf = linebuf
212
cgiflags.linebuf = ""
213
cgiflags.started_cgi_body = True
214
# Process all the header lines
215
split = headers.split('\r\n', 1)
217
split = headers.split('\n', 1)
219
process_cgi_header_line(req, split[0], cgiflags)
220
if len(split) == 1: break
222
if cgiflags.wrote_html_warning:
223
# We're done with headers. Treat the rest as data.
224
data = headers + '\n' + data
226
split = headers.split('\r\n', 1)
228
split = headers.split('\n', 1)
230
# Check to make sure the required headers were written
231
if cgiflags.wrote_html_warning:
232
# We already reported an error, that's enough
234
elif "Content-Type" in cgiflags.headers:
236
elif "Location" in cgiflags.headers:
237
if ("Status" in cgiflags.headers and req.status >= 300
238
and req.status < 400):
241
message = """You did not write a valid status code for
242
the given location. To make a redirect, you may wish to try:</p>
243
<pre style="margin-left: 1em">Status: 302 Found
244
Location: <redirect address></pre>"""
245
write_html_warning(req, message)
246
cgiflags.wrote_html_warning = True
248
message = """You did not print a Content-Type header.
249
CGI requires that you print a "Content-Type". You may wish to try:</p>
250
<pre style="margin-left: 1em">Content-Type: text/html</pre>"""
251
write_html_warning(req, message)
252
cgiflags.wrote_html_warning = True
254
# Call myself to flush out the extra bit of data we read
255
process_cgi_output(req, data, cgiflags)
257
def process_cgi_header_line(req, line, cgiflags):
258
"""Process a line of CGI header data. line is a string representing a
259
complete line of text, stripped and without the newline.
262
name, value = line.split(':', 1)
264
# No colon. The user did not write valid headers.
265
if len(cgiflags.headers) == 0:
266
# First line was not a header line. We can assume this is not
268
message = """You did not print a CGI header.
269
CGI requires that you print a "Content-Type". You may wish to try:</p>
270
<pre style="margin-left: 1em">Content-Type: text/html</pre>"""
272
# They printed some header at least, but there was an invalid
274
message = """You printed an invalid CGI header. You need to leave
275
a blank line after the headers, before writing the page contents."""
276
write_html_warning(req, message)
277
cgiflags.wrote_html_warning = True
278
# Handle the rest of this line as normal data
279
process_cgi_output(req, line + '\n', cgiflags)
283
value = value.strip()
284
if name == "Content-Type":
285
req.content_type = value
286
elif name == "Location":
288
elif name == "Status":
289
# Must be an integer, followed by a space, and then the status line
290
# which we ignore (seems like Apache has no way to send a custom
293
req.status = int(value.split(' ', 1)[0])
295
message = """The "Status" CGI header was invalid. You need to
296
print a number followed by a message, such as "302 Found"."""
297
write_html_warning(req, message)
298
cgiflags.wrote_html_warning = True
299
# Handle the rest of this line as normal data
300
process_cgi_output(req, line + '\n', cgiflags)
302
# Generic HTTP header
303
# FIXME: Security risk letting users write arbitrary headers?
304
req.headers_out[name] = value
305
cgiflags.headers[name] = value
307
def write_html_warning(req, text):
308
"""Prints an HTML warning about invalid CGI interaction on the part of the
309
user. text may contain HTML markup."""
310
req.content_type = "text/html"
311
req.write("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
312
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
313
<html xmlns="http://www.w3.org/1999/xhtml">
315
<meta http-equiv="Content-Type"
316
content="text/html; charset=utf-8" />
318
<body style="margin: 0; padding: 0; font-family: sans-serif;">
319
<div style="background-color: #faa; border-bottom: 1px solid black;
321
<p><strong>Warning</strong>: %s
323
<div style="margin: 8px;">
327
location_cgi_python = os.path.join(conf.ivle_install_dir,
330
# Mapping of interpreter names (as given in conf/app/server.py) to
331
# interpreter functions.
333
interpreter_objects = {
335
: functools.partial(execute_cgi, "/usr/bin/python",
336
location_cgi_python),
342
def fixup_environ(req):
343
"""Assuming os.environ has been written with the CGI variables from
344
apache, make a few changes for security and correctness.
346
Does not modify req, only reads it.
349
# Comments here are on the heavy side, explained carefully for security
350
# reasons. Please read carefully before making changes.
352
# Remove HTTP_COOKIE. It is a security risk to have students see the IVLE
353
# cookie of their visitors.
355
del env['HTTP_COOKIE']
358
# Remove DOCUMENT_ROOT and SCRIPT_FILENAME. Not part of CGI spec and
359
# exposes unnecessary details about server.
361
del env['DOCUMENT_ROOT']
364
del env['SCRIPT_FILENAME']
367
# Remove PATH. The PATH here is the path on the server machine; not useful
368
# inside the jail. It may be a good idea to add another path, reflecting
369
# the inside of the jail, but not done at this stage.
374
# Remove SCRIPT_FILENAME. Not part of CGI spec (see SCRIPT_NAME).
376
# PATH_INFO is wrong because the script doesn't physically exist.
377
# Apache makes it relative to the "serve" app. It should actually be made
378
# relative to the student's script.
379
# TODO: At this stage, it is not possible to add a path after the script,
380
# so PATH_INFO is always "".
382
env['PATH_INFO'] = path_info
384
# PATH_TRANSLATED currently points to a non-existant location within the
385
# local web server directory. Instead make it represent a path within the
387
(username, _, path_translated) = studpath.url_to_jailpaths(req.path)
388
if len(path_translated) == 0 or path_translated[0] != os.sep:
389
path_translated = os.sep + path_translated
390
env['PATH_TRANSLATED'] = path_translated
392
# CGI specifies that REMOTE_HOST SHOULD be set, and MAY just be set to
393
# REMOTE_ADDR. Since Apache does not appear to set this, set it to
395
if 'REMOTE_HOST' not in env and 'REMOTE_ADDR' in env:
396
env['REMOTE_HOST'] = env['REMOTE_ADDR']
398
# SCRIPT_NAME is the path to the script WITHOUT PATH_INFO.
399
script_name = req.uri
400
if len(path_info) > 0:
401
script_name = script_name[:-len(path_info)]
402
env['SCRIPT_NAME'] = script_name
404
# SERVER_SOFTWARE is actually not Apache but IVLE, since we are
405
# custom-making the CGI request.
406
env['SERVER_SOFTWARE'] = "IVLE/" + str(conf.ivle_version)
408
# Additional environment variables
409
env['HOME'] = os.path.join('/home', username)