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
32
def interpret_file(req, owner, filename, interpreter):
33
"""Serves a file by interpreting it using one of IVLE's builtin
34
interpreters. All interpreters are intended to run in the user's jail. The
35
jail location is provided as an argument to the interpreter but it is up
36
to the individual interpreters to create the jail.
38
req: An IVLE request object.
39
owner: Username of the user who owns the file being served.
40
filename: Filename in the local file system.
41
interpreter: A function object to call.
43
# Make sure the file exists (otherwise some interpreters may not actually
45
# Don't test for execute permission, that will only be required for
46
# certain interpreters.
47
if not os.access(filename, os.R_OK):
48
req.throw_error(req.HTTP_NOT_FOUND)
50
# Get the UID of the owner of the file
51
# (Note: files are executed by their owners, not the logged in user.
52
# This ensures users are responsible for their own programs and also
53
# allows them to be executed by the public).
55
(_,_,uid,_,_,_,_) = pwd.getpwnam(owner)
57
# The user does not exist. This should have already failed the
59
req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR)
61
# Split up req.path again, this time with respect to the jail
62
(_, jail_dir, path) = studpath.url_to_jailpaths(req.path)
63
path = os.path.join('/', path)
64
(working_dir, _) = os.path.split(path)
65
# jail_dir is the absolute jail directory.
66
# path is the filename relative to the user's jail.
67
# working_dir is the directory containing the file relative to the user's
69
# (Note that paths "relative" to the jail actually begin with a '/' as
70
# they are absolute in the jailspace)
72
return interpreter(uid, jail_dir, working_dir, path, req)
74
# Used to store mutable data
78
def execute_cgi(interpreter, trampoline, uid, jail_dir, working_dir,
81
trampoline: Full path on the local system to the CGI wrapper program
83
uid: User ID of the owner of the file.
84
jail_dir: Absolute path of owner's jail directory.
85
working_dir: Directory containing the script file relative to owner's
87
script_path: CGI script relative to the owner's jail.
88
req: IVLE request object.
90
The called CGI wrapper application shall be called using popen and receive
91
the HTTP body on stdin. It shall receive the CGI environment variables to
95
# Get the student program's directory and execute it from that context.
96
(tramp_dir, _) = os.path.split(trampoline)
98
# TODO: Don't create a file if the body length is known to be 0
99
# Write the HTTP body to a temporary file so it can be passed as a *real*
106
f.seek(0) # Rewind, for reading
108
# usage: tramp uid jail_dir working_dir script_path
109
pid = subprocess.Popen(
110
[trampoline, str(uid), jail_dir, working_dir, interpreter,
112
stdin=f, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
115
# process_cgi_line: Reads a single line of CGI output and processes it.
116
# Prints to req, and also does fancy HTML warnings if Content-Type
119
cgiflags.got_cgi_header = False
120
cgiflags.started_cgi_body = False
121
cgiflags.wrote_html_warning = False
122
def process_cgi_line(line):
123
# FIXME? Issue with binary files (processing per-line?)
124
if cgiflags.started_cgi_body:
125
# FIXME: HTML escape text if wrote_html_warning
129
if line.strip() == "" and cgiflags.got_cgi_header:
130
cgiflags.started_cgi_body = True
131
elif line.startswith("Content-Type:"):
132
req.content_type = line[13:].strip()
133
cgiflags.got_cgi_header = True
134
elif line.startswith("Location:"):
136
cgiflags.got_cgi_header = True
137
elif line.startswith("Status:"):
139
cgiflags.got_cgi_header = True
140
elif cgiflags.got_cgi_header:
143
req.write("Invalid header")
146
# Assume the user is not printing headers and give a warning
148
# User program did not print header.
149
# Make a fancy HTML warning for them.
150
req.content_type = "text/html"
151
req.write("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
152
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
153
<html xmlns="http://www.w3.org/1999/xhtml">
155
<meta http-equiv="Content-Type"
156
content="text/html; charset=utf-8" />
158
<body style="margin: 0; padding: 0; font-family: sans;">
159
<div style="background-color: #faa; border-bottom: 1px solid black;
161
<p><strong>Warning</strong>: You did not print a "Content-Type" header.
162
CGI requires you to print some content type. You may wish to try:</p>
163
<pre style="margin-left: 1em">Content-Type: text/html</pre>
165
<div style="margin: 8px;">
168
cgiflags.got_cgi_header = True
169
cgiflags.wrote_html_warning = True
170
cgiflags.started_cgi_body = True
173
# Read from the process's stdout into req
174
for line in pid.stdout:
175
process_cgi_line(line)
177
# If we wrote an HTML warning header, write the footer
178
if cgiflags.wrote_html_warning:
184
# TODO: Replace mytest with cgi trampoline handler script
185
location_cgi_python = os.path.join(conf.ivle_install_dir,
188
# Mapping of interpreter names (as given in conf/app/server.py) to
189
# interpreter functions.
191
interpreter_objects = {
193
: functools.partial(execute_cgi, "/usr/bin/python",
194
location_cgi_python),