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
# Now jail_dir is the jail directory relative to the jails root.
66
# Note that the trampoline has jails root hard-coded for security.
67
# path is the filename relative to the user's jail.
68
# working_dir is the directory containing the file relative to the user's
70
# (Note that paths "relative" to the jail actually begin with a '/' as
71
# they are absolute in the jailspace)
73
return interpreter(uid, jail_dir, working_dir, path, req)
75
# Used to store mutable data
79
def execute_cgi(trampoline, uid, jail_dir, working_dir, script_path, req):
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: Owner's jail directory relative to the jails root.
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, script_path],
111
stdin=f, stdout=subprocess.PIPE, cwd=tramp_dir)
113
# process_cgi_line: Reads a single line of CGI output and processes it.
114
# Prints to req, and also does fancy HTML warnings if Content-Type
117
cgiflags.got_cgi_header = False
118
cgiflags.started_cgi_body = False
119
cgiflags.wrote_html_warning = False
120
def process_cgi_line(line):
121
# FIXME? Issue with binary files (processing per-line?)
122
if cgiflags.started_cgi_body:
123
# FIXME: HTML escape text if wrote_html_warning
127
if line.strip() == "" and cgiflags.got_cgi_header:
128
cgiflags.started_cgi_body = True
129
elif line.startswith("Content-Type:"):
130
req.content_type = line[13:].strip()
131
cgiflags.got_cgi_header = True
132
elif line.startswith("Location:"):
134
cgiflags.got_cgi_header = True
135
elif line.startswith("Status:"):
137
cgiflags.got_cgi_header = True
138
elif cgiflags.got_cgi_header:
141
req.write("Invalid header")
144
# Assume the user is not printing headers and give a warning
146
# User program did not print header.
147
# Make a fancy HTML warning for them.
148
req.content_type = "text/html"
149
req.write("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
150
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
151
<html xmlns="http://www.w3.org/1999/xhtml">
153
<meta http-equiv="Content-Type"
154
content="text/html; charset=utf-8" />
156
<body style="margin: 0; padding: 0; font-family: sans;">
157
<div style="background-color: #faa; border-bottom: 1px solid black;
159
<p><strong>Warning</strong>: You did not print a "Content-Type" header.
160
CGI requires you to print some content type. You may wish to try:</p>
161
<pre style="margin-left: 1em">Content-Type: text/html</pre>
163
<div style="margin: 8px;">
166
cgiflags.got_cgi_header = True
167
cgiflags.wrote_html_warning = True
168
cgiflags.started_cgi_body = True
171
# Read from the process's stdout into req
172
for line in pid.stdout:
173
process_cgi_line(line)
175
# If we wrote an HTML warning header, write the footer
176
if cgiflags.wrote_html_warning:
182
# TODO: Replace mytest with cgi trampoline handler script
183
location_cgi_python = os.path.join(conf.ivlepath, "bin/trampoline-python")
185
# Mapping of interpreter names (as given in conf/app/server.py) to
186
# interpreter functions.
188
interpreter_objects = {
190
: functools.partial(execute_cgi, location_cgi_python),