37
37
# working on smaller output
39
39
CGI_BLOCK_SIZE = 65535
40
PATH = "/usr/local/bin:/usr/bin:/bin"
41
42
def interpret_file(req, owner, jail_dir, filename, interpreter, gentle=True):
42
43
"""Serves a file by interpreting it using one of IVLE's builtin
73
74
# (Note that paths "relative" to the jail actually begin with a '/' as
74
75
# they are absolute in the jailspace)
76
return interpreter(owner.unixid, jail_dir, working_dir, filename_abs, req,
77
return interpreter(owner, jail_dir, working_dir, filename_abs, req,
89
90
self.headers = {} # Header names : values
91
def execute_cgi(interpreter, uid, jail_dir, working_dir, script_path,
92
def execute_cgi(interpreter, owner, jail_dir, working_dir, script_path,
94
95
trampoline: Full path on the local system to the CGI wrapper program
96
uid: User ID of the owner of the file.
97
owner: User object of the owner of the file.
97
98
jail_dir: Absolute path of owner's jail directory.
98
99
working_dir: Directory containing the script file relative to owner's
129
130
f.seek(0) # Rewind, for reading
131
132
# Set up the environment
132
# This automatically asks mod_python to load up the CGI variables into the
133
# environment (which is a good first approximation)
134
old_env = os.environ.copy()
135
for k in os.environ.keys():
137
for (k,v) in req.get_cgi_environ().items():
139
fixup_environ(req, script_path)
133
environ = cgi_environ(req, script_path, owner)
141
135
# usage: tramp uid jail_dir working_dir script_path
142
pid = subprocess.Popen(
143
[trampoline, str(uid), req.config['paths']['jails']['mounts'],
144
req.config['paths']['jails']['src'],
145
req.config['paths']['jails']['template'],
146
jail_dir, working_dir, interpreter, script_path],
136
cmd_line = [trampoline, str(owner.unixid),
137
req.config['paths']['jails']['mounts'],
138
req.config['paths']['jails']['src'],
139
req.config['paths']['jails']['template'],
140
jail_dir, working_dir, interpreter, script_path]
141
# Popen doesn't like unicode strings. It hateses them.
142
cmd_line = [(s.encode('utf-8') if isinstance(s, unicode) else s)
144
pid = subprocess.Popen(cmd_line,
147
145
stdin=f, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
150
# Restore the environment
151
for k in os.environ.keys():
153
for (k,v) in old_env.items():
146
cwd=tramp_dir, env=environ)
156
148
# We don't want any output! Bail out after the process terminates.
223
215
if len(split) == 1:
224
216
split = headers.split('\n', 1)
226
# Is this an internal IVLE error condition?
227
hs = cgiflags.headers
228
if 'X-IVLE-Error-Type' in hs:
229
t = hs['X-IVLE-Error-Type']
230
if t == IVLEError.__name__:
231
raise IVLEError(int(hs['X-IVLE-Error-Code']),
232
hs['X-IVLE-Error-Message'])
218
# If not executing in gentle mode (which presents CGI violations
219
# to users nicely), check if this an internal IVLE error
221
if not cgiflags.gentle:
222
hs = cgiflags.headers
223
if 'X-IVLE-Error-Type' in hs:
235
225
raise IVLEJailError(hs['X-IVLE-Error-Type'],
236
226
hs['X-IVLE-Error-Message'],
237
227
hs['X-IVLE-Error-Info'])
239
raise IVLEError(500, 'bad error headers written by CGI')
229
raise AssertionError("Bad error headers written by CGI.")
241
231
# Check to make sure the required headers were written
242
232
if cgiflags.wrote_html_warning or not cgiflags.gentle:
296
286
process_cgi_output(req, line + '\n', cgiflags)
289
# Check if CGI field-name is valid
290
CGI_SEPERATORS = set(['(', ')', '<', '>', '@', ',', ';', ':', '\\', '"',
291
'/', '[', ']', '?', '=', '{', '}', ' ', '\t'])
292
if any((char in CGI_SEPERATORS for char in name)):
294
if not cgiflags.gentle:
295
message = """An unexpected server error has occured."""
298
# Header contained illegal characters
299
message = """You printed an invalid CGI header. CGI header
300
field-names can not contain any of the following characters:
301
<code>( ) < > @ , ; : \\ " / [ ] ? = { } <em>SPACE
303
write_html_warning(req, message, warning=warning)
304
cgiflags.wrote_html_warning = True
305
# Handle the rest of this line as normal data
306
process_cgi_output(req, line + '\n', cgiflags)
299
309
# Read CGI headers
300
310
value = value.strip()
301
311
if name == "Content-Type":
358
368
# python-server-page
361
def fixup_environ(req, script_path):
362
"""Assuming os.environ has been written with the CGI variables from
363
apache, make a few changes for security and correctness.
371
def cgi_environ(req, script_path, user):
372
"""Gets CGI variables from apache and makes a few changes for security and
365
375
Does not modify req, only reads it.
368
378
# Comments here are on the heavy side, explained carefully for security
369
379
# reasons. Please read carefully before making changes.
381
# This automatically asks mod_python to load up the CGI variables into the
382
# environment (which is a good first approximation)
383
for (k,v) in req.get_cgi_environ().items():
371
386
# Remove DOCUMENT_ROOT and SCRIPT_FILENAME. Not part of CGI spec and
372
387
# exposes unnecessary details about server.
415
430
env['SERVER_SOFTWARE'] = "IVLE/" + ivle.__version__
417
432
# Additional environment variables
418
username = split_path(req.path)[0]
433
username = user.login
419
434
env['HOME'] = os.path.join('/home', username)
421
438
class ExecutionError(Exception):
432
449
tramp_dir = os.path.split(tramp)[0]
434
451
# Fire up trampoline. Vroom, vroom.
435
proc = subprocess.Popen(
436
[tramp, str(user.unixid), config['paths']['jails']['mounts'],
452
cmd_line = [tramp, str(user.unixid), config['paths']['jails']['mounts'],
437
453
config['paths']['jails']['src'],
438
454
config['paths']['jails']['template'],
439
jail_dir, working_dir, binary] + args,
455
jail_dir, working_dir, binary] + args
456
# Popen doesn't like unicode strings. It hateses them.
457
cmd_line = [(s.encode('utf-8') if isinstance(s, unicode) else s)
459
proc = subprocess.Popen(cmd_line,
440
460
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
441
stderr=subprocess.PIPE, cwd=tramp_dir, close_fds=True)
461
stderr=subprocess.PIPE, cwd=tramp_dir, close_fds=True,
462
env={'HOME': os.path.join('/home', user.login),
465
'LOGNAME': user.login})
443
467
(stdout, stderr) = proc.communicate()
444
468
exitcode = proc.returncode
446
470
if exitcode != 0:
447
raise ExecutionError('subprocess ended with code %d, stderr %s' %
448
(exitcode, proc.stderr.read()))
471
raise ExecutionError('subprocess ended with code %d, stderr: "%s"' %
449
473
return (stdout, stderr)