40
39
CGI_BLOCK_SIZE = 65535
45
"""Get the unix uid corresponding to the given login name.
46
If it is not in the dictionary of uids, then consult the
47
database and retrieve an update of the user table."""
53
res = conn.get_all('login', ['login', 'unixid'])
55
return (flds['login'], flds['unixid'])
56
uids = dict(map(repack,res))
60
41
def interpret_file(req, owner, jail_dir, filename, interpreter, gentle=True):
61
42
"""Serves a file by interpreting it using one of IVLE's builtin
62
43
interpreters. All interpreters are intended to run in the user's jail. The
64
45
to the individual interpreters to create the jail.
66
47
req: An IVLE request object.
67
owner: Username of the user who owns the file being served.
48
owner: The user who owns the file being served.
68
49
jail_dir: Absolute path to the user's jail.
69
50
filename: Absolute filename within the user's jail.
70
51
interpreter: A function object to call.
79
60
filename_abs = os.path.join(os.sep, filename)
80
61
filename_rel = filename
82
# Get the UID of the owner of the file
83
63
# (Note: files are executed by their owners, not the logged in user.
84
64
# This ensures users are responsible for their own programs and also
85
65
# allows them to be executed by the public).
88
67
# Split up req.path again, this time with respect to the jail
89
68
(working_dir, _) = os.path.split(filename_abs)
94
73
# (Note that paths "relative" to the jail actually begin with a '/' as
95
74
# they are absolute in the jailspace)
97
return interpreter(uid, jail_dir, working_dir, filename_abs, req,
76
return interpreter(owner.unixid, jail_dir, working_dir, filename_abs, req,
408
# Remove SCRIPT_FILENAME. Not part of CGI spec (see SCRIPT_NAME).
410
# PATH_INFO is wrong because the script doesn't physically exist.
411
# Apache makes it relative to the "serve" app. It should actually be made
412
# relative to the student's script. intepretservice does that in the jail,
413
# so here we just clear it.
414
env['PATH_INFO'] = ''
415
env['PATH_TRANSLATED'] = ''
417
387
# CGI specifies that REMOTE_HOST SHOULD be set, and MAY just be set to
418
388
# REMOTE_ADDR. Since Apache does not appear to set this, set it to
420
390
if 'REMOTE_HOST' not in env and 'REMOTE_ADDR' in env:
421
391
env['REMOTE_HOST'] = env['REMOTE_ADDR']
393
env['PATH_INFO'] = ''
394
del env['PATH_TRANSLATED']
396
normuri = os.path.normpath(req.uri)
397
env['SCRIPT_NAME'] = normuri
423
399
# SCRIPT_NAME is the path to the script WITHOUT PATH_INFO.
424
script_name = req.uri
425
env['SCRIPT_NAME'] = script_name
400
# We don't care about these if the script is null (ie. noop).
401
# XXX: We check for /home because we don't want to interfere with
402
# CGIRequest, which fileservice still uses.
403
if script_path and script_path.startswith('/home'):
404
normscript = os.path.normpath(script_path)
406
uri_into_jail = studpath.url_to_jailpaths(os.path.normpath(req.path))[2]
408
# PATH_INFO is wrong because the script doesn't physically exist.
409
env['PATH_INFO'] = uri_into_jail[len(normscript):]
410
if len(env['PATH_INFO']) > 0:
411
env['SCRIPT_NAME'] = normuri[:-len(env['PATH_INFO'])]
427
413
# SERVER_SOFTWARE is actually not Apache but IVLE, since we are
428
414
# custom-making the CGI request.
431
417
# Additional environment variables
432
418
username = studpath.url_to_jailpaths(req.path)[0]
433
419
env['HOME'] = os.path.join('/home', username)
421
class ExecutionError(Exception):
424
def execute_raw(user, jail_dir, working_dir, binary, args):
425
'''Execute a binary in a user's jail, returning the raw output.
427
The binary is executed in the given working directory with the given
428
args. A tuple of (stdout, stderr) is returned.
431
tramp = location_cgi_python
432
tramp_dir = os.path.split(location_cgi_python)[0]
434
# Fire up trampoline. Vroom, vroom.
435
proc = subprocess.Popen(
436
[tramp, str(user.unixid), jail_dir, working_dir, binary] + args,
437
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
438
stderr=subprocess.PIPE, cwd=tramp_dir, close_fds=True)
439
exitcode = proc.wait()
442
raise ExecutionError('subprocess ended with code %d, stderr %s' %
443
(exitcode, proc.stderr.read()))
444
return (proc.stdout.read(), proc.stderr.read())