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, trampoline, uid, jail_dir, working_dir,
92
script_path, req, gentle):
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
127
130
f.seek(0) # Rewind, for reading
129
132
# Set up the environment
130
# This automatically asks mod_python to load up the CGI variables into the
131
# environment (which is a good first approximation)
132
old_env = os.environ.copy()
133
for k in os.environ.keys():
135
for (k,v) in req.get_cgi_environ().items():
137
fixup_environ(req, script_path)
133
environ = cgi_environ(req, script_path, owner)
139
135
# usage: tramp uid jail_dir working_dir script_path
140
pid = subprocess.Popen(
141
[trampoline, str(uid), ivle.conf.jail_base, ivle.conf.jail_src_base,
142
ivle.conf.jail_system, jail_dir, working_dir, interpreter,
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,
144
145
stdin=f, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
147
# Restore the environment
148
for k in os.environ.keys():
150
for (k,v) in old_env.items():
146
cwd=tramp_dir, env=environ)
153
148
# We don't want any output! Bail out after the process terminates.
220
215
if len(split) == 1:
221
216
split = headers.split('\n', 1)
223
# Is this an internal IVLE error condition?
224
hs = cgiflags.headers
225
if 'X-IVLE-Error-Type' in hs:
226
t = hs['X-IVLE-Error-Type']
227
if t == IVLEError.__name__:
228
raise IVLEError(int(hs['X-IVLE-Error-Code']),
229
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:
232
225
raise IVLEJailError(hs['X-IVLE-Error-Type'],
233
226
hs['X-IVLE-Error-Message'],
234
227
hs['X-IVLE-Error-Info'])
236
raise IVLEError(500, 'bad error headers written by CGI')
229
raise AssertionError("Bad error headers written by CGI.")
238
231
# Check to make sure the required headers were written
239
232
if cgiflags.wrote_html_warning or not cgiflags.gentle:
343
336
""" % (warning, text))
345
location_cgi_python = os.path.join(ivle.conf.lib_path, "trampoline")
347
338
# Mapping of interpreter names (as given in conf/app/server.py) to
348
339
# interpreter functions.
350
341
interpreter_objects = {
352
: functools.partial(execute_cgi, "/usr/bin/python",
353
location_cgi_python),
343
: functools.partial(execute_cgi, "/usr/bin/python"),
355
: functools.partial(execute_cgi, None,
356
location_cgi_python),
345
: functools.partial(execute_cgi, None),
357
346
# Should also have:
359
348
# python-server-page
362
def fixup_environ(req, script_path):
363
"""Assuming os.environ has been written with the CGI variables from
364
apache, make a few changes for security and correctness.
351
def cgi_environ(req, script_path, user):
352
"""Gets CGI variables from apache and makes a few changes for security and
366
355
Does not modify req, only reads it.
369
358
# Comments here are on the heavy side, explained carefully for security
370
359
# reasons. Please read carefully before making changes.
361
# This automatically asks mod_python to load up the CGI variables into the
362
# environment (which is a good first approximation)
363
for (k,v) in req.get_cgi_environ().items():
372
366
# Remove DOCUMENT_ROOT and SCRIPT_FILENAME. Not part of CGI spec and
373
367
# exposes unnecessary details about server.
414
408
# SERVER_SOFTWARE is actually not Apache but IVLE, since we are
415
409
# custom-making the CGI request.
416
env['SERVER_SOFTWARE'] = "IVLE/" + str(ivle.conf.ivle_version)
410
env['SERVER_SOFTWARE'] = "IVLE/" + ivle.__version__
418
412
# Additional environment variables
419
username = studpath.url_to_jailpaths(req.path)[0]
413
username = user.login
420
414
env['HOME'] = os.path.join('/home', username)
422
418
class ExecutionError(Exception):
425
def execute_raw(user, jail_dir, working_dir, binary, args):
421
def execute_raw(config, user, jail_dir, working_dir, binary, args):
426
422
'''Execute a binary in a user's jail, returning the raw output.
428
424
The binary is executed in the given working directory with the given
429
425
args. A tuple of (stdout, stderr) is returned.
432
tramp = location_cgi_python
433
tramp_dir = os.path.split(location_cgi_python)[0]
428
tramp = os.path.join(config['paths']['lib'], 'trampoline')
429
tramp_dir = os.path.split(tramp)[0]
435
431
# Fire up trampoline. Vroom, vroom.
436
proc = subprocess.Popen(
437
[tramp, str(user.unixid), ivle.conf.jail_base,
438
ivle.conf.jail_src_base, ivle.conf.jail_system, jail_dir,
439
working_dir, binary] + args,
432
cmd_line = [tramp, str(user.unixid), config['paths']['jails']['mounts'],
433
config['paths']['jails']['src'],
434
config['paths']['jails']['template'],
435
jail_dir, working_dir, binary] + args
436
# Popen doesn't like unicode strings. It hateses them.
437
cmd_line = [(s.encode('utf-8') if isinstance(s, unicode) else s)
439
proc = subprocess.Popen(cmd_line,
440
440
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
441
stderr=subprocess.PIPE, cwd=tramp_dir, close_fds=True)
442
exitcode = proc.wait()
441
stderr=subprocess.PIPE, cwd=tramp_dir, close_fds=True,
442
env={'HOME': os.path.join('/home', user.login),
445
'LOGNAME': user.login})
447
(stdout, stderr) = proc.communicate()
448
exitcode = proc.returncode
444
450
if exitcode != 0:
445
raise ExecutionError('subprocess ended with code %d, stderr %s' %
446
(exitcode, proc.stderr.read()))
447
return (proc.stdout.read(), proc.stderr.read())
451
raise ExecutionError('subprocess ended with code %d, stderr: "%s"' %
453
return (stdout, stderr)