103
99
req.throw_error(req.HTTP_NOT_FOUND)
104
100
req.content_type = type
105
101
req.sendfile(filename)
107
def interpret_file(req, owner, filename, interpreter):
108
"""Serves a file by interpreting it using one of IVLE's builtin
109
interpreters. All interpreters are intended to run in the user's jail. The
110
jail location is provided as an argument to the interpreter but it is up
111
to the individual interpreters to create the jail.
113
req: An IVLE request object.
114
owner: Username of the user who owns the file being served.
115
filename: Filename in the local file system.
116
interpreter: A function object to call.
118
# Make sure the file exists (otherwise some interpreters may not actually
120
# Don't test for execute permission, that will only be required for
121
# certain interpreters.
122
if not os.access(filename, os.R_OK):
123
req.throw_error(req.HTTP_NOT_FOUND)
125
# Get the UID of the owner of the file
126
# (Note: files are executed by their owners, not the logged in user.
127
# This ensures users are responsible for their own programs and also
128
# allows them to be executed by the public).
130
(_,_,uid,_,_,_,_) = pwd.getpwnam(owner)
132
# The user does not exist. This should have already failed the
134
req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR)
136
# Split up req.path again, this time with respect to the jail
137
(_, jail_dir, path) = studpath.url_to_jailpaths(req.path)
138
path = os.path.join('/', path)
139
(working_dir, _) = os.path.split(path)
140
# Now jail_dir is the jail directory relative to the jails root.
141
# Note that the trampoline has jails root hard-coded for security.
142
# path is the filename relative to the user's jail.
143
# working_dir is the directory containing the file relative to the user's
145
# (Note that paths "relative" to the jail actually begin with a '/' as
146
# they are absolute in the jailspace)
148
return interpreter(uid, jail_dir, working_dir, path, req)
150
# Used to store mutable data
154
def execute_cgi(trampoline, uid, jail_dir, working_dir, script_path, req):
156
trampoline: Full path on the local system to the CGI wrapper program
158
uid: User ID of the owner of the file.
159
jail_dir: Owner's jail directory relative to the jails root.
160
working_dir: Directory containing the script file relative to owner's
162
script_path: CGI script relative to the owner's jail.
163
req: IVLE request object.
165
The called CGI wrapper application shall be called using popen and receive
166
the HTTP body on stdin. It shall receive the CGI environment variables to
170
# Get the student program's directory and execute it from that context.
171
(tramp_dir, _) = os.path.split(trampoline)
173
# TODO: Don't create a file if the body length is known to be 0
174
# Write the HTTP body to a temporary file so it can be passed as a *real*
181
f.seek(0) # Rewind, for reading
183
# usage: tramp uid jail_dir working_dir script_path
184
pid = subprocess.Popen(
185
[trampoline, str(uid), jail_dir, working_dir, script_path],
186
stdin=f, stdout=subprocess.PIPE, cwd=tramp_dir)
188
# process_cgi_line: Reads a single line of CGI output and processes it.
189
# Prints to req, and also does fancy HTML warnings if Content-Type
192
cgiflags.got_cgi_header = False
193
cgiflags.started_cgi_body = False
194
cgiflags.wrote_html_warning = False
195
def process_cgi_line(line):
196
# FIXME? Issue with binary files (processing per-line?)
197
if cgiflags.started_cgi_body:
198
# FIXME: HTML escape text if wrote_html_warning
202
if line.strip() == "" and cgiflags.got_cgi_header:
203
cgiflags.started_cgi_body = True
204
elif line.startswith("Content-Type:"):
205
req.content_type = line[13:].strip()
206
cgiflags.got_cgi_header = True
207
elif line.startswith("Location:"):
209
cgiflags.got_cgi_header = True
210
elif line.startswith("Status:"):
212
cgiflags.got_cgi_header = True
213
elif cgiflags.got_cgi_header:
216
req.write("Invalid header")
219
# Assume the user is not printing headers and give a warning
221
# User program did not print header.
222
# Make a fancy HTML warning for them.
223
req.content_type = "text/html"
224
req.write("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
225
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
226
<html xmlns="http://www.w3.org/1999/xhtml">
228
<meta http-equiv="Content-Type"
229
content="text/html; charset=utf-8" />
231
<body style="margin: 0; padding: 0; font-family: sans;">
232
<div style="background-color: #faa; border-bottom: 1px solid black;
234
<p><strong>Warning</strong>: You did not print a "Content-Type" header.
235
CGI requires you to print some content type. You may wish to try:</p>
236
<pre style="margin-left: 1em">Content-Type: text/html</pre>
238
<div style="margin: 8px;">
241
cgiflags.got_cgi_header = True
242
cgiflags.wrote_html_warning = True
243
cgiflags.started_cgi_body = True
246
# Read from the process's stdout into req
247
for line in pid.stdout:
248
process_cgi_line(line)
250
# If we wrote an HTML warning header, write the footer
251
if cgiflags.wrote_html_warning:
257
# TODO: Replace mytest with cgi trampoline handler script
258
location_cgi_python = os.path.join(conf.ivlepath, "bin/trampoline-python")
260
# Mapping of interpreter names (as given in conf/app/server.py) to
261
# interpreter functions.
263
interpreter_objects = {
265
: functools.partial(execute_cgi, location_cgi_python),