136
134
del os.environ[k]
137
135
for (k,v) in req.get_cgi_environ().items():
138
136
os.environ[k] = v
139
fixup_environ(req, script_path)
141
139
# usage: tramp uid jail_dir working_dir script_path
142
cmd_line = [trampoline, str(uid), req.config['paths']['jails']['mounts'],
143
req.config['paths']['jails']['src'],
144
req.config['paths']['jails']['template'],
145
jail_dir, working_dir, interpreter, script_path]
146
# Popen doesn't like unicode strings. It hateses them.
147
cmd_line = [(s.encode('utf-8') if isinstance(s, unicode) else s)
149
pid = subprocess.Popen(cmd_line,
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,
150
144
stdin=f, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
349
343
""" % (warning, text))
345
location_cgi_python = os.path.join(ivle.conf.lib_path, "trampoline")
351
347
# Mapping of interpreter names (as given in conf/app/server.py) to
352
348
# interpreter functions.
354
350
interpreter_objects = {
356
: functools.partial(execute_cgi, "/usr/bin/python"),
352
: functools.partial(execute_cgi, "/usr/bin/python",
353
location_cgi_python),
358
: functools.partial(execute_cgi, None),
355
: functools.partial(execute_cgi, None,
356
location_cgi_python),
359
357
# Should also have:
361
359
# python-server-page
364
def fixup_environ(req, script_path):
362
def fixup_environ(req):
365
363
"""Assuming os.environ has been written with the CGI variables from
366
364
apache, make a few changes for security and correctness.
388
# Remove SCRIPT_FILENAME. Not part of CGI spec (see SCRIPT_NAME).
390
# PATH_INFO is wrong because the script doesn't physically exist.
391
# Apache makes it relative to the "serve" app. It should actually be made
392
# relative to the student's script. intepretservice does that in the jail,
393
# so here we just clear it.
394
env['PATH_INFO'] = ''
395
env['PATH_TRANSLATED'] = ''
390
397
# CGI specifies that REMOTE_HOST SHOULD be set, and MAY just be set to
391
398
# REMOTE_ADDR. Since Apache does not appear to set this, set it to
393
400
if 'REMOTE_HOST' not in env and 'REMOTE_ADDR' in env:
394
401
env['REMOTE_HOST'] = env['REMOTE_ADDR']
396
env['PATH_INFO'] = ''
397
del env['PATH_TRANSLATED']
399
normuri = os.path.normpath(req.uri)
400
env['SCRIPT_NAME'] = normuri
402
403
# SCRIPT_NAME is the path to the script WITHOUT PATH_INFO.
403
# We don't care about these if the script is null (ie. noop).
404
# XXX: We check for /home because we don't want to interfere with
405
# CGIRequest, which fileservice still uses.
406
if script_path and script_path.startswith('/home'):
407
normscript = os.path.normpath(script_path)
409
uri_into_jail = studpath.to_home_path(os.path.normpath(req.path))
411
# PATH_INFO is wrong because the script doesn't physically exist.
412
env['PATH_INFO'] = uri_into_jail[len(normscript):]
413
if len(env['PATH_INFO']) > 0:
414
env['SCRIPT_NAME'] = normuri[:-len(env['PATH_INFO'])]
404
script_name = req.uri
405
env['SCRIPT_NAME'] = script_name
416
407
# SERVER_SOFTWARE is actually not Apache but IVLE, since we are
417
408
# custom-making the CGI request.
418
env['SERVER_SOFTWARE'] = "IVLE/" + ivle.__version__
409
env['SERVER_SOFTWARE'] = "IVLE/" + str(ivle.conf.ivle_version)
420
411
# Additional environment variables
421
username = split_path(req.path)[0]
412
username = studpath.url_to_jailpaths(req.path)[0]
422
413
env['HOME'] = os.path.join('/home', username)
424
415
class ExecutionError(Exception):
427
def execute_raw(config, user, jail_dir, working_dir, binary, args):
418
def execute_raw(user, jail_dir, working_dir, binary, args):
428
419
'''Execute a binary in a user's jail, returning the raw output.
430
421
The binary is executed in the given working directory with the given
431
422
args. A tuple of (stdout, stderr) is returned.
434
tramp = os.path.join(config['paths']['lib'], 'trampoline')
435
tramp_dir = os.path.split(tramp)[0]
425
tramp = location_cgi_python
426
tramp_dir = os.path.split(location_cgi_python)[0]
437
428
# Fire up trampoline. Vroom, vroom.
438
cmd_line = [tramp, str(user.unixid), config['paths']['jails']['mounts'],
439
config['paths']['jails']['src'],
440
config['paths']['jails']['template'],
441
jail_dir, working_dir, binary] + args
442
# Popen doesn't like unicode strings. It hateses them.
443
cmd_line = [(s.encode('utf-8') if isinstance(s, unicode) else s)
445
proc = subprocess.Popen(cmd_line,
429
proc = subprocess.Popen(
430
[tramp, str(user.unixid), ivle.conf.jail_base,
431
ivle.conf.jail_src_base, ivle.conf.jail_system, jail_dir,
432
working_dir, binary] + args,
446
433
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
447
434
stderr=subprocess.PIPE, cwd=tramp_dir, close_fds=True)
449
(stdout, stderr) = proc.communicate()
450
exitcode = proc.returncode
435
exitcode = proc.wait()
452
437
if exitcode != 0:
453
438
raise ExecutionError('subprocess ended with code %d, stderr %s' %
454
439
(exitcode, proc.stderr.read()))
455
return (stdout, stderr)
440
return (proc.stdout.read(), proc.stderr.read())