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
110
89
self.headers = {} # Header names : values
112
def execute_cgi(interpreter, trampoline, uid, jail_dir, working_dir,
113
script_path, req, gentle):
91
def execute_cgi(interpreter, owner, jail_dir, working_dir, script_path,
115
94
trampoline: Full path on the local system to the CGI wrapper program
117
uid: User ID of the owner of the file.
96
owner: User object of the owner of the file.
118
97
jail_dir: Absolute path of owner's jail directory.
119
98
working_dir: Directory containing the script file relative to owner's
155
136
del os.environ[k]
156
137
for (k,v) in req.get_cgi_environ().items():
157
138
os.environ[k] = v
139
fixup_environ(req, script_path, owner)
160
141
# usage: tramp uid jail_dir working_dir script_path
161
pid = subprocess.Popen(
162
[trampoline, str(uid), jail_dir, working_dir, interpreter,
142
cmd_line = [trampoline, str(owner.unixid),
143
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]
147
# Popen doesn't like unicode strings. It hateses them.
148
cmd_line = [(s.encode('utf-8') if isinstance(s, unicode) else s)
150
pid = subprocess.Popen(cmd_line,
164
151
stdin=f, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
363
350
""" % (warning, text))
365
location_cgi_python = os.path.join(ivle.conf.lib_path, "trampoline")
367
352
# Mapping of interpreter names (as given in conf/app/server.py) to
368
353
# interpreter functions.
370
355
interpreter_objects = {
372
: functools.partial(execute_cgi, "/usr/bin/python",
373
location_cgi_python),
357
: functools.partial(execute_cgi, "/usr/bin/python"),
375
: functools.partial(execute_cgi, None,
376
location_cgi_python),
359
: functools.partial(execute_cgi, None),
377
360
# Should also have:
379
362
# python-server-page
382
def fixup_environ(req):
365
def fixup_environ(req, script_path, user):
383
366
"""Assuming os.environ has been written with the CGI variables from
384
367
apache, make a few changes for security and correctness.
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
391
# CGI specifies that REMOTE_HOST SHOULD be set, and MAY just be set to
418
392
# REMOTE_ADDR. Since Apache does not appear to set this, set it to
420
394
if 'REMOTE_HOST' not in env and 'REMOTE_ADDR' in env:
421
395
env['REMOTE_HOST'] = env['REMOTE_ADDR']
397
env['PATH_INFO'] = ''
398
del env['PATH_TRANSLATED']
400
normuri = os.path.normpath(req.uri)
401
env['SCRIPT_NAME'] = normuri
423
403
# SCRIPT_NAME is the path to the script WITHOUT PATH_INFO.
424
script_name = req.uri
425
env['SCRIPT_NAME'] = script_name
404
# We don't care about these if the script is null (ie. noop).
405
# XXX: We check for /home because we don't want to interfere with
406
# CGIRequest, which fileservice still uses.
407
if script_path and script_path.startswith('/home'):
408
normscript = os.path.normpath(script_path)
410
uri_into_jail = studpath.to_home_path(os.path.normpath(req.path))
412
# PATH_INFO is wrong because the script doesn't physically exist.
413
env['PATH_INFO'] = uri_into_jail[len(normscript):]
414
if len(env['PATH_INFO']) > 0:
415
env['SCRIPT_NAME'] = normuri[:-len(env['PATH_INFO'])]
427
417
# SERVER_SOFTWARE is actually not Apache but IVLE, since we are
428
418
# custom-making the CGI request.
429
env['SERVER_SOFTWARE'] = "IVLE/" + str(ivle.conf.ivle_version)
419
env['SERVER_SOFTWARE'] = "IVLE/" + ivle.__version__
431
421
# Additional environment variables
432
username = studpath.url_to_jailpaths(req.path)[0]
422
username = user.login
433
423
env['HOME'] = os.path.join('/home', username)
425
class ExecutionError(Exception):
428
def execute_raw(config, user, jail_dir, working_dir, binary, args):
429
'''Execute a binary in a user's jail, returning the raw output.
431
The binary is executed in the given working directory with the given
432
args. A tuple of (stdout, stderr) is returned.
435
tramp = os.path.join(config['paths']['lib'], 'trampoline')
436
tramp_dir = os.path.split(tramp)[0]
438
# Fire up trampoline. Vroom, vroom.
439
cmd_line = [tramp, str(user.unixid), config['paths']['jails']['mounts'],
440
config['paths']['jails']['src'],
441
config['paths']['jails']['template'],
442
jail_dir, working_dir, binary] + args
443
# Popen doesn't like unicode strings. It hateses them.
444
cmd_line = [(s.encode('utf-8') if isinstance(s, unicode) else s)
446
proc = subprocess.Popen(cmd_line,
447
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
448
stderr=subprocess.PIPE, cwd=tramp_dir, close_fds=True,
449
env={'HOME': os.path.join('/home', user.login),
450
'PATH': os.environ['PATH'],
452
'LOGNAME': user.login})
454
(stdout, stderr) = proc.communicate()
455
exitcode = proc.returncode
458
raise ExecutionError('subprocess ended with code %d, stderr: "%s"' %
460
return (stdout, stderr)