1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
1 |
# IVLE
|
2 |
# Copyright (C) 2007-2008 The University of Melbourne
|
|
3 |
#
|
|
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.
|
|
8 |
#
|
|
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.
|
|
13 |
#
|
|
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
|
|
17 |
||
18 |
# Module: Interpret
|
|
19 |
# Author: Matt Giuca
|
|
20 |
# Date: 18/1/2008
|
|
21 |
||
22 |
# Runs a student script in a safe execution environment.
|
|
23 |
||
1276
by William Grant
Drop ivle.conf usage from ivle.interpret. |
24 |
import ivle |
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
25 |
from ivle import studpath |
1779
by William Grant
Remove IVLEError support; only fileservice used it, and the last invocation is GONE. |
26 |
from ivle.util import IVLEJailError, split_path |
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
27 |
|
28 |
import functools |
|
29 |
||
30 |
import os |
|
31 |
import pwd |
|
32 |
import subprocess |
|
33 |
import cgi |
|
1805
by Matt Giuca
ivle.interpret: Added 'jail_call' function, which allows regular IVLE web app code to call jail CGI functions and get their contents. |
34 |
import StringIO |
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
35 |
|
36 |
# TODO: Make progressive output work
|
|
37 |
# Question: Will having a large buffer size stop progressive output from
|
|
38 |
# working on smaller output
|
|
39 |
||
40 |
CGI_BLOCK_SIZE = 65535 |
|
1776
by David Coles
interpret: Hard code PATH for interpret_raw since it's not always set correctly in Apache threads |
41 |
PATH = "/usr/local/bin:/usr/bin:/bin" |
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
42 |
|
1802
by Matt Giuca
ivle/interpret: execute_cgi and interpret_file now take an 'overrides' argument, which is a dict containing env vars to override in the CGI request. |
43 |
def interpret_file(req, owner, jail_dir, filename, interpreter, gentle=True, |
44 |
overrides=None): |
|
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
45 |
"""Serves a file by interpreting it using one of IVLE's builtin
|
46 |
interpreters. All interpreters are intended to run in the user's jail. The
|
|
47 |
jail location is provided as an argument to the interpreter but it is up
|
|
48 |
to the individual interpreters to create the jail.
|
|
49 |
||
50 |
req: An IVLE request object.
|
|
1080.1.66
by William Grant
ivle.interpret.interpret_file: Take a User object as the owner, not a login. |
51 |
owner: The user who owns the file being served.
|
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
52 |
jail_dir: Absolute path to the user's jail.
|
53 |
filename: Absolute filename within the user's jail.
|
|
54 |
interpreter: A function object to call.
|
|
1802
by Matt Giuca
ivle/interpret: execute_cgi and interpret_file now take an 'overrides' argument, which is a dict containing env vars to override in the CGI request. |
55 |
gentle: ?
|
56 |
overrides: A dict mapping env var names to strings, to override arbitrary
|
|
57 |
environment variables in the resulting CGI environent.
|
|
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
58 |
"""
|
59 |
# We can't test here whether or not the target file actually exists,
|
|
60 |
# because the apache user may not have permission. Instead we have to
|
|
61 |
# rely on the interpreter generating an error.
|
|
62 |
if filename.startswith(os.sep): |
|
63 |
filename_abs = filename |
|
64 |
filename_rel = filename[1:] |
|
65 |
else: |
|
66 |
filename_abs = os.path.join(os.sep, filename) |
|
67 |
filename_rel = filename |
|
68 |
||
69 |
# (Note: files are executed by their owners, not the logged in user.
|
|
70 |
# This ensures users are responsible for their own programs and also
|
|
71 |
# allows them to be executed by the public).
|
|
72 |
||
73 |
# Split up req.path again, this time with respect to the jail
|
|
74 |
(working_dir, _) = os.path.split(filename_abs) |
|
75 |
# jail_dir is the absolute jail directory.
|
|
76 |
# path is the filename relative to the user's jail.
|
|
77 |
# working_dir is the directory containing the file relative to the user's
|
|
78 |
# jail.
|
|
79 |
# (Note that paths "relative" to the jail actually begin with a '/' as
|
|
80 |
# they are absolute in the jailspace)
|
|
81 |
||
1770
by David Coles
interpret: Make fixup_env use a user object rather than path munging... |
82 |
return interpreter(owner, jail_dir, working_dir, filename_abs, req, |
1802
by Matt Giuca
ivle/interpret: execute_cgi and interpret_file now take an 'overrides' argument, which is a dict containing env vars to override in the CGI request. |
83 |
gentle, overrides=overrides) |
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
84 |
|
85 |
class CGIFlags: |
|
86 |
"""Stores flags regarding the state of reading CGI output.
|
|
87 |
If this is to be gentle, detection of invalid headers will result in an
|
|
88 |
HTML warning."""
|
|
89 |
def __init__(self, begentle=True): |
|
90 |
self.gentle = begentle |
|
91 |
self.started_cgi_body = False |
|
92 |
self.got_cgi_headers = False |
|
93 |
self.wrote_html_warning = False |
|
94 |
self.linebuf = "" |
|
95 |
self.headers = {} # Header names : values |
|
96 |
||
1770
by David Coles
interpret: Make fixup_env use a user object rather than path munging... |
97 |
def execute_cgi(interpreter, owner, jail_dir, working_dir, script_path, |
1802
by Matt Giuca
ivle/interpret: execute_cgi and interpret_file now take an 'overrides' argument, which is a dict containing env vars to override in the CGI request. |
98 |
req, gentle, overrides=None): |
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
99 |
"""
|
100 |
trampoline: Full path on the local system to the CGI wrapper program
|
|
101 |
being executed.
|
|
1770
by David Coles
interpret: Make fixup_env use a user object rather than path munging... |
102 |
owner: User object of the owner of the file.
|
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
103 |
jail_dir: Absolute path of owner's jail directory.
|
104 |
working_dir: Directory containing the script file relative to owner's
|
|
105 |
jail.
|
|
106 |
script_path: CGI script relative to the owner's jail.
|
|
107 |
req: IVLE request object.
|
|
1802
by Matt Giuca
ivle/interpret: execute_cgi and interpret_file now take an 'overrides' argument, which is a dict containing env vars to override in the CGI request. |
108 |
gentle: ?
|
109 |
overrides: A dict mapping env var names to strings, to override arbitrary
|
|
110 |
environment variables in the resulting CGI environent.
|
|
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
111 |
|
112 |
The called CGI wrapper application shall be called using popen and receive
|
|
113 |
the HTTP body on stdin. It shall receive the CGI environment variables to
|
|
114 |
its environment.
|
|
115 |
"""
|
|
116 |
||
1276
by William Grant
Drop ivle.conf usage from ivle.interpret. |
117 |
trampoline = os.path.join(req.config['paths']['lib'], 'trampoline') |
118 |
||
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
119 |
# Support no-op trampoline runs.
|
120 |
if interpreter is None: |
|
121 |
interpreter = '/bin/true' |
|
122 |
script_path = '' |
|
123 |
noop = True |
|
124 |
else: |
|
125 |
noop = False |
|
126 |
||
127 |
# Get the student program's directory and execute it from that context.
|
|
128 |
(tramp_dir, _) = os.path.split(trampoline) |
|
129 |
||
130 |
# TODO: Don't create a file if the body length is known to be 0
|
|
131 |
# Write the HTTP body to a temporary file so it can be passed as a *real*
|
|
132 |
# file to popen.
|
|
133 |
f = os.tmpfile() |
|
134 |
body = req.read() if not noop else None |
|
135 |
if body is not None: |
|
136 |
f.write(body) |
|
137 |
f.flush() |
|
138 |
f.seek(0) # Rewind, for reading |
|
139 |
||
140 |
# Set up the environment
|
|
1802
by Matt Giuca
ivle/interpret: execute_cgi and interpret_file now take an 'overrides' argument, which is a dict containing env vars to override in the CGI request. |
141 |
environ = cgi_environ(req, script_path, owner, overrides=overrides) |
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
142 |
|
143 |
# usage: tramp uid jail_dir working_dir script_path
|
|
1770
by David Coles
interpret: Make fixup_env use a user object rather than path munging... |
144 |
cmd_line = [trampoline, str(owner.unixid), |
145 |
req.config['paths']['jails']['mounts'], |
|
146 |
req.config['paths']['jails']['src'], |
|
147 |
req.config['paths']['jails']['template'], |
|
148 |
jail_dir, working_dir, interpreter, script_path] |
|
1646
by Matt Giuca
ivle.interpret: Fixed calls to Popen: Unicode strings are encoded as UTF-8. This fixes serve of non-executable files, but serve-of-python, diff, log and download still break on files with a unicode filename. |
149 |
# Popen doesn't like unicode strings. It hateses them.
|
150 |
cmd_line = [(s.encode('utf-8') if isinstance(s, unicode) else s) |
|
151 |
for s in cmd_line] |
|
152 |
pid = subprocess.Popen(cmd_line, |
|
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
153 |
stdin=f, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, |
1777
by David Coles
interpret: Don't mutate os.environ for execute_cgi, Set an environ on subprocess.Popen instead. |
154 |
cwd=tramp_dir, env=environ) |
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
155 |
|
156 |
# We don't want any output! Bail out after the process terminates.
|
|
157 |
if noop: |
|
158 |
pid.communicate() |
|
159 |
return
|
|
160 |
||
161 |
# process_cgi_line: Reads a single line of CGI output and processes it.
|
|
162 |
# Prints to req, and also does fancy HTML warnings if Content-Type
|
|
163 |
# omitted.
|
|
164 |
cgiflags = CGIFlags(gentle) |
|
165 |
||
166 |
# Read from the process's stdout into req
|
|
167 |
data = pid.stdout.read(CGI_BLOCK_SIZE) |
|
168 |
while len(data) > 0: |
|
169 |
process_cgi_output(req, data, cgiflags) |
|
170 |
data = pid.stdout.read(CGI_BLOCK_SIZE) |
|
171 |
||
172 |
# If we haven't processed headers yet, now is a good time
|
|
173 |
if not cgiflags.started_cgi_body: |
|
174 |
process_cgi_output(req, '\n', cgiflags) |
|
175 |
||
176 |
# If we wrote an HTML warning header, write the footer
|
|
177 |
if cgiflags.wrote_html_warning: |
|
178 |
req.write("""</pre> |
|
179 |
</div>
|
|
180 |
</body>
|
|
181 |
</html>""") |
|
182 |
||
183 |
def process_cgi_output(req, data, cgiflags): |
|
184 |
"""Processes a chunk of CGI output. data is a string of arbitrary length;
|
|
185 |
some arbitrary chunk of output written by the CGI script."""
|
|
186 |
if cgiflags.started_cgi_body: |
|
187 |
if cgiflags.wrote_html_warning: |
|
188 |
# HTML escape text if wrote_html_warning
|
|
189 |
req.write(cgi.escape(data)) |
|
190 |
else: |
|
191 |
req.write(data) |
|
192 |
else: |
|
193 |
# Break data into lines of CGI header data.
|
|
194 |
linebuf = cgiflags.linebuf + data |
|
195 |
# First see if we can split all header data
|
|
196 |
# We need to get the double CRLF- or LF-terminated headers, whichever
|
|
197 |
# is smaller, as either sequence may appear somewhere in the body.
|
|
198 |
usplit = linebuf.split('\n\n', 1) |
|
199 |
wsplit = linebuf.split('\r\n\r\n', 1) |
|
200 |
split = len(usplit[0]) > len(wsplit[0]) and wsplit or usplit |
|
201 |
if len(split) == 1: |
|
202 |
# Haven't seen all headers yet. Buffer and come back later.
|
|
203 |
cgiflags.linebuf = linebuf |
|
204 |
return
|
|
205 |
||
206 |
headers = split[0] |
|
207 |
data = split[1] |
|
208 |
cgiflags.linebuf = "" |
|
209 |
cgiflags.started_cgi_body = True |
|
210 |
# Process all the header lines
|
|
211 |
split = headers.split('\r\n', 1) |
|
212 |
if len(split) == 1: |
|
213 |
split = headers.split('\n', 1) |
|
214 |
while True: |
|
215 |
process_cgi_header_line(req, split[0], cgiflags) |
|
216 |
if len(split) == 1: break |
|
217 |
headers = split[1] |
|
218 |
if cgiflags.wrote_html_warning: |
|
219 |
# We're done with headers. Treat the rest as data.
|
|
220 |
data = headers + '\n' + data |
|
221 |
break
|
|
222 |
split = headers.split('\r\n', 1) |
|
223 |
if len(split) == 1: |
|
224 |
split = headers.split('\n', 1) |
|
225 |
||
1780
by William Grant
Don't try to create IVLEJailErrors out of CGI headers when we're executing student code. |
226 |
# If not executing in gentle mode (which presents CGI violations
|
227 |
# to users nicely), check if this an internal IVLE error
|
|
228 |
# condition.
|
|
229 |
if not cgiflags.gentle: |
|
230 |
hs = cgiflags.headers |
|
231 |
if 'X-IVLE-Error-Type' in hs: |
|
232 |
try: |
|
233 |
raise IVLEJailError(hs['X-IVLE-Error-Type'], |
|
234 |
hs['X-IVLE-Error-Message'], |
|
235 |
hs['X-IVLE-Error-Info']) |
|
236 |
except KeyError: |
|
237 |
raise AssertionError("Bad error headers written by CGI.") |
|
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
238 |
|
239 |
# Check to make sure the required headers were written
|
|
240 |
if cgiflags.wrote_html_warning or not cgiflags.gentle: |
|
241 |
# We already reported an error, that's enough
|
|
242 |
pass
|
|
243 |
elif "Content-Type" in cgiflags.headers: |
|
244 |
pass
|
|
245 |
elif "Location" in cgiflags.headers: |
|
246 |
if ("Status" in cgiflags.headers and req.status >= 300 |
|
247 |
and req.status < 400): |
|
248 |
pass
|
|
249 |
else: |
|
250 |
message = """You did not write a valid status code for |
|
251 |
the given location. To make a redirect, you may wish to try:</p>
|
|
252 |
<pre style="margin-left: 1em">Status: 302 Found
|
|
253 |
Location: <redirect address></pre>"""
|
|
254 |
write_html_warning(req, message) |
|
255 |
cgiflags.wrote_html_warning = True |
|
256 |
else: |
|
257 |
message = """You did not print a Content-Type header. |
|
258 |
CGI requires that you print a "Content-Type". You may wish to try:</p>
|
|
259 |
<pre style="margin-left: 1em">Content-Type: text/html</pre>"""
|
|
260 |
write_html_warning(req, message) |
|
261 |
cgiflags.wrote_html_warning = True |
|
262 |
||
263 |
# Call myself to flush out the extra bit of data we read
|
|
264 |
process_cgi_output(req, data, cgiflags) |
|
265 |
||
266 |
def process_cgi_header_line(req, line, cgiflags): |
|
267 |
"""Process a line of CGI header data. line is a string representing a
|
|
268 |
complete line of text, stripped and without the newline.
|
|
269 |
"""
|
|
270 |
try: |
|
271 |
name, value = line.split(':', 1) |
|
272 |
except ValueError: |
|
273 |
# No colon. The user did not write valid headers.
|
|
274 |
# If we are being gentle, we want to help the user understand what
|
|
275 |
# went wrong. Otherwise, just admit we screwed up.
|
|
276 |
warning = "Warning" |
|
277 |
if not cgiflags.gentle: |
|
278 |
message = """An unexpected server error has occured.""" |
|
279 |
warning = "Error" |
|
280 |
elif len(cgiflags.headers) == 0: |
|
281 |
# First line was not a header line. We can assume this is not
|
|
282 |
# a CGI app.
|
|
283 |
message = """You did not print a CGI header. |
|
284 |
CGI requires that you print a "Content-Type". You may wish to try:</p>
|
|
285 |
<pre style="margin-left: 1em">Content-Type: text/html</pre>"""
|
|
286 |
else: |
|
287 |
# They printed some header at least, but there was an invalid
|
|
288 |
# header.
|
|
289 |
message = """You printed an invalid CGI header. You need to leave |
|
290 |
a blank line after the headers, before writing the page contents."""
|
|
291 |
write_html_warning(req, message, warning=warning) |
|
292 |
cgiflags.wrote_html_warning = True |
|
293 |
# Handle the rest of this line as normal data
|
|
294 |
process_cgi_output(req, line + '\n', cgiflags) |
|
295 |
return
|
|
296 |
||
1799
by David Coles
Show warning for CGI header field-names which contain restricted characters. |
297 |
# Check if CGI field-name is valid
|
298 |
CGI_SEPERATORS = set(['(', ')', '<', '>', '@', ',', ';', ':', '\\', '"', |
|
299 |
'/', '[', ']', '?', '=', '{', '}', ' ', '\t']) |
|
300 |
if any((char in CGI_SEPERATORS for char in name)): |
|
301 |
warning = "Warning" |
|
302 |
if not cgiflags.gentle: |
|
303 |
message = """An unexpected server error has occured.""" |
|
304 |
warning = "Error" |
|
305 |
else: |
|
306 |
# Header contained illegal characters
|
|
307 |
message = """You printed an invalid CGI header. CGI header |
|
308 |
field-names can not contain any of the following characters:
|
|
309 |
<code>( ) < > @ , ; : \\ " / [ ] ? = { } <em>SPACE |
|
310 |
TAB</em></code>."""
|
|
311 |
write_html_warning(req, message, warning=warning) |
|
312 |
cgiflags.wrote_html_warning = True |
|
313 |
# Handle the rest of this line as normal data
|
|
314 |
process_cgi_output(req, line + '\n', cgiflags) |
|
315 |
return
|
|
316 |
||
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
317 |
# Read CGI headers
|
318 |
value = value.strip() |
|
319 |
if name == "Content-Type": |
|
320 |
req.content_type = value |
|
321 |
elif name == "Location": |
|
322 |
req.location = value |
|
323 |
elif name == "Status": |
|
324 |
# Must be an integer, followed by a space, and then the status line
|
|
325 |
# which we ignore (seems like Apache has no way to send a custom
|
|
326 |
# status line).
|
|
327 |
try: |
|
328 |
req.status = int(value.split(' ', 1)[0]) |
|
329 |
except ValueError: |
|
330 |
if not cgiflags.gentle: |
|
331 |
# This isn't user code, so it should be good.
|
|
332 |
# Get us out of here!
|
|
333 |
raise
|
|
334 |
message = """The "Status" CGI header was invalid. You need to |
|
335 |
print a number followed by a message, such as "302 Found"."""
|
|
336 |
write_html_warning(req, message) |
|
337 |
cgiflags.wrote_html_warning = True |
|
338 |
# Handle the rest of this line as normal data
|
|
339 |
process_cgi_output(req, line + '\n', cgiflags) |
|
340 |
else: |
|
341 |
# Generic HTTP header
|
|
342 |
# FIXME: Security risk letting users write arbitrary headers?
|
|
343 |
req.headers_out.add(name, value) |
|
344 |
cgiflags.headers[name] = value # FIXME: Only the last header will end up here. |
|
345 |
||
346 |
def write_html_warning(req, text, warning="Warning"): |
|
347 |
"""Prints an HTML warning about invalid CGI interaction on the part of the
|
|
348 |
user. text may contain HTML markup."""
|
|
349 |
req.content_type = "text/html" |
|
350 |
req.write("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" |
|
351 |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
|
352 |
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
353 |
<head>
|
|
354 |
<meta http-equiv="Content-Type"
|
|
355 |
content="text/html; charset=utf-8" />
|
|
356 |
</head>
|
|
357 |
<body style="margin: 0; padding: 0; font-family: sans-serif;">
|
|
358 |
<div style="background-color: #faa; border-bottom: 1px solid black;
|
|
359 |
padding: 8px;">
|
|
360 |
<p><strong>%s</strong>: %s |
|
361 |
</div>
|
|
362 |
<div style="margin: 8px;">
|
|
363 |
<pre>
|
|
364 |
""" % (warning, text)) |
|
365 |
||
366 |
# Mapping of interpreter names (as given in conf/app/server.py) to
|
|
367 |
# interpreter functions.
|
|
368 |
||
369 |
interpreter_objects = { |
|
370 |
'cgi-python'
|
|
1276
by William Grant
Drop ivle.conf usage from ivle.interpret. |
371 |
: functools.partial(execute_cgi, "/usr/bin/python"), |
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
372 |
'noop'
|
1276
by William Grant
Drop ivle.conf usage from ivle.interpret. |
373 |
: functools.partial(execute_cgi, None), |
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
374 |
# Should also have:
|
375 |
# cgi-generic
|
|
376 |
# python-server-page
|
|
377 |
}
|
|
378 |
||
1802
by Matt Giuca
ivle/interpret: execute_cgi and interpret_file now take an 'overrides' argument, which is a dict containing env vars to override in the CGI request. |
379 |
def cgi_environ(req, script_path, user, overrides=None): |
1777
by David Coles
interpret: Don't mutate os.environ for execute_cgi, Set an environ on subprocess.Popen instead. |
380 |
"""Gets CGI variables from apache and makes a few changes for security and
|
381 |
correctness.
|
|
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
382 |
|
383 |
Does not modify req, only reads it.
|
|
1802
by Matt Giuca
ivle/interpret: execute_cgi and interpret_file now take an 'overrides' argument, which is a dict containing env vars to override in the CGI request. |
384 |
|
385 |
overrides: A dict mapping env var names to strings, to override arbitrary
|
|
386 |
environment variables in the resulting CGI environent.
|
|
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
387 |
"""
|
1777
by David Coles
interpret: Don't mutate os.environ for execute_cgi, Set an environ on subprocess.Popen instead. |
388 |
env = {} |
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
389 |
# Comments here are on the heavy side, explained carefully for security
|
390 |
# reasons. Please read carefully before making changes.
|
|
1777
by David Coles
interpret: Don't mutate os.environ for execute_cgi, Set an environ on subprocess.Popen instead. |
391 |
|
392 |
# This automatically asks mod_python to load up the CGI variables into the
|
|
393 |
# environment (which is a good first approximation)
|
|
394 |
for (k,v) in req.get_cgi_environ().items(): |
|
395 |
env[k] = v |
|
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
396 |
|
397 |
# Remove DOCUMENT_ROOT and SCRIPT_FILENAME. Not part of CGI spec and
|
|
398 |
# exposes unnecessary details about server.
|
|
399 |
try: |
|
400 |
del env['DOCUMENT_ROOT'] |
|
401 |
except: pass |
|
402 |
try: |
|
403 |
del env['SCRIPT_FILENAME'] |
|
404 |
except: pass |
|
405 |
||
406 |
# Remove PATH. The PATH here is the path on the server machine; not useful
|
|
407 |
# inside the jail. It may be a good idea to add another path, reflecting
|
|
408 |
# the inside of the jail, but not done at this stage.
|
|
409 |
try: |
|
410 |
del env['PATH'] |
|
411 |
except: pass |
|
412 |
||
413 |
# CGI specifies that REMOTE_HOST SHOULD be set, and MAY just be set to
|
|
414 |
# REMOTE_ADDR. Since Apache does not appear to set this, set it to
|
|
415 |
# REMOTE_ADDR.
|
|
416 |
if 'REMOTE_HOST' not in env and 'REMOTE_ADDR' in env: |
|
417 |
env['REMOTE_HOST'] = env['REMOTE_ADDR'] |
|
418 |
||
1099.3.14
by William Grant
ivle.interpret.fixup_environ() now sets PATH_INFO appropriately. |
419 |
env['PATH_INFO'] = '' |
420 |
del env['PATH_TRANSLATED'] |
|
421 |
||
422 |
normuri = os.path.normpath(req.uri) |
|
423 |
env['SCRIPT_NAME'] = normuri |
|
424 |
||
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
425 |
# SCRIPT_NAME is the path to the script WITHOUT PATH_INFO.
|
1099.3.14
by William Grant
ivle.interpret.fixup_environ() now sets PATH_INFO appropriately. |
426 |
# We don't care about these if the script is null (ie. noop).
|
427 |
# XXX: We check for /home because we don't want to interfere with
|
|
428 |
# CGIRequest, which fileservice still uses.
|
|
429 |
if script_path and script_path.startswith('/home'): |
|
430 |
normscript = os.path.normpath(script_path) |
|
431 |
||
1270
by William Grant
Rename to to_home_path, and use it in ivle.interpret. |
432 |
uri_into_jail = studpath.to_home_path(os.path.normpath(req.path)) |
1099.3.14
by William Grant
ivle.interpret.fixup_environ() now sets PATH_INFO appropriately. |
433 |
|
434 |
# PATH_INFO is wrong because the script doesn't physically exist.
|
|
435 |
env['PATH_INFO'] = uri_into_jail[len(normscript):] |
|
436 |
if len(env['PATH_INFO']) > 0: |
|
437 |
env['SCRIPT_NAME'] = normuri[:-len(env['PATH_INFO'])] |
|
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
438 |
|
439 |
# SERVER_SOFTWARE is actually not Apache but IVLE, since we are
|
|
440 |
# custom-making the CGI request.
|
|
1274
by William Grant
Move ivle.conf.ivle_version to ivle.__version__. |
441 |
env['SERVER_SOFTWARE'] = "IVLE/" + ivle.__version__ |
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
442 |
|
443 |
# Additional environment variables
|
|
1770
by David Coles
interpret: Make fixup_env use a user object rather than path munging... |
444 |
username = user.login |
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
445 |
env['HOME'] = os.path.join('/home', username) |
1099.1.47
by William Grant
ivle.interpret#execute_raw: Add. Executes a script in a user's jail with |
446 |
|
1802
by Matt Giuca
ivle/interpret: execute_cgi and interpret_file now take an 'overrides' argument, which is a dict containing env vars to override in the CGI request. |
447 |
if overrides is not None: |
448 |
env.update(overrides) |
|
1777
by David Coles
interpret: Don't mutate os.environ for execute_cgi, Set an environ on subprocess.Popen instead. |
449 |
return env |
450 |
||
1099.1.47
by William Grant
ivle.interpret#execute_raw: Add. Executes a script in a user's jail with |
451 |
class ExecutionError(Exception): |
452 |
pass
|
|
453 |
||
1276
by William Grant
Drop ivle.conf usage from ivle.interpret. |
454 |
def execute_raw(config, user, jail_dir, working_dir, binary, args): |
1099.1.47
by William Grant
ivle.interpret#execute_raw: Add. Executes a script in a user's jail with |
455 |
'''Execute a binary in a user's jail, returning the raw output.
|
456 |
||
457 |
The binary is executed in the given working directory with the given
|
|
458 |
args. A tuple of (stdout, stderr) is returned.
|
|
459 |
'''
|
|
460 |
||
1276
by William Grant
Drop ivle.conf usage from ivle.interpret. |
461 |
tramp = os.path.join(config['paths']['lib'], 'trampoline') |
462 |
tramp_dir = os.path.split(tramp)[0] |
|
1099.1.47
by William Grant
ivle.interpret#execute_raw: Add. Executes a script in a user's jail with |
463 |
|
464 |
# Fire up trampoline. Vroom, vroom.
|
|
1646
by Matt Giuca
ivle.interpret: Fixed calls to Popen: Unicode strings are encoded as UTF-8. This fixes serve of non-executable files, but serve-of-python, diff, log and download still break on files with a unicode filename. |
465 |
cmd_line = [tramp, str(user.unixid), config['paths']['jails']['mounts'], |
1276
by William Grant
Drop ivle.conf usage from ivle.interpret. |
466 |
config['paths']['jails']['src'], |
467 |
config['paths']['jails']['template'], |
|
1646
by Matt Giuca
ivle.interpret: Fixed calls to Popen: Unicode strings are encoded as UTF-8. This fixes serve of non-executable files, but serve-of-python, diff, log and download still break on files with a unicode filename. |
468 |
jail_dir, working_dir, binary] + args |
469 |
# Popen doesn't like unicode strings. It hateses them.
|
|
470 |
cmd_line = [(s.encode('utf-8') if isinstance(s, unicode) else s) |
|
471 |
for s in cmd_line] |
|
472 |
proc = subprocess.Popen(cmd_line, |
|
1099.1.47
by William Grant
ivle.interpret#execute_raw: Add. Executes a script in a user's jail with |
473 |
stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
1760
by William Grant
ivle.interpret.execute_raw now sets a clean environment, in particular with HOME set correctly. |
474 |
stderr=subprocess.PIPE, cwd=tramp_dir, close_fds=True, |
475 |
env={'HOME': os.path.join('/home', user.login), |
|
1776
by David Coles
interpret: Hard code PATH for interpret_raw since it's not always set correctly in Apache threads |
476 |
'PATH': PATH, |
1760
by William Grant
ivle.interpret.execute_raw now sets a clean environment, in particular with HOME set correctly. |
477 |
'USER': user.login, |
478 |
'LOGNAME': user.login}) |
|
1164
by William Grant
ivle.interpret.execute_raw() no longer breaks with lots of data. |
479 |
|
480 |
(stdout, stderr) = proc.communicate() |
|
481 |
exitcode = proc.returncode |
|
1099.1.47
by William Grant
ivle.interpret#execute_raw: Add. Executes a script in a user's jail with |
482 |
|
483 |
if exitcode != 0: |
|
1772
by David Coles
interpret: Fix execute_raw's printing of stderr - can't read pipe after communicate returns. |
484 |
raise ExecutionError('subprocess ended with code %d, stderr: "%s"' % |
485 |
(exitcode, stderr)) |
|
1164
by William Grant
ivle.interpret.execute_raw() no longer breaks with lots of data. |
486 |
return (stdout, stderr) |
1805
by Matt Giuca
ivle.interpret: Added 'jail_call' function, which allows regular IVLE web app code to call jail CGI functions and get their contents. |
487 |
|
488 |
def jail_call(req, cgi_script, script_name, query_string=None, |
|
489 |
request_method="GET", extra_overrides=None): |
|
490 |
"""
|
|
491 |
Makes a call to a CGI script inside the jail from outside the jail.
|
|
492 |
This can be used to allow Python scripts to access jail-only functions and
|
|
493 |
data without having to perform a full API request.
|
|
494 |
||
495 |
req: A Request object (will not be written to or attributes modified).
|
|
496 |
cgi_script: Path to cgi script outside of jail.
|
|
497 |
eg: os.path.join(req.config['paths']['share'],
|
|
498 |
'services/fileservice')
|
|
499 |
script_name: Name to set as SCRIPT_NAME for the CGI environment.
|
|
500 |
eg: "/fileservice/"
|
|
501 |
query_string: Query string to set as QUERY_STRING for the CGI environment.
|
|
502 |
eg: "action=svnrepostat&path=/users/studenta/"
|
|
503 |
request_method: Method to set as REQUEST_METHOD for the CGI environment.
|
|
504 |
eg: "POST". Defaults to "GET".
|
|
505 |
extra_overrides: A dict mapping env var names to strings, to override
|
|
506 |
arbitrary environment variables in the resulting CGI environent.
|
|
507 |
||
508 |
Returns a triple (status_code, content_type, contents).
|
|
509 |
"""
|
|
510 |
interp_object = interpreter_objects["cgi-python"] |
|
511 |
user_jail_dir = os.path.join(req.config['paths']['jails']['mounts'], |
|
512 |
req.user.login) |
|
513 |
overrides = { |
|
514 |
"SCRIPT_NAME": script_name, |
|
515 |
"QUERY_STRING": query_string, |
|
516 |
"REQUEST_URI": "%s%s%s" % (script_name, "?" if query_string else "", |
|
517 |
query_string), |
|
518 |
"REQUEST_METHOD": request_method, |
|
519 |
}
|
|
520 |
if extra_overrides is not None: |
|
521 |
overrides.update(extra_overrides) |
|
522 |
result = DummyReq(req) |
|
523 |
interpret_file(result, req.user, user_jail_dir, cgi_script, interp_object, |
|
524 |
gentle=False, overrides=overrides) |
|
525 |
return result.status, result.content_type, result.getvalue() |
|
526 |
||
527 |
class DummyReq(StringIO.StringIO): |
|
528 |
"""A dummy request object, built from a real request object, which can be
|
|
529 |
used like a req but doesn't mutate the existing request.
|
|
530 |
(Used for reading CGI responses as strings rather than forwarding their
|
|
531 |
output to the current request.)
|
|
532 |
"""
|
|
533 |
def __init__(self, req): |
|
534 |
StringIO.StringIO.__init__(self) |
|
535 |
self._real_req = req |
|
536 |
def get_cgi_environ(self): |
|
537 |
return self._real_req.get_cgi_environ() |
|
538 |
def __getattr__(self, name): |
|
539 |
return getattr(self._real_req, name) |