89
89
self.headers = {} # Header names : values
91
def execute_cgi(interpreter, trampoline, uid, jail_dir, working_dir,
92
script_path, req, gentle):
91
def execute_cgi(interpreter, uid, jail_dir, working_dir, script_path,
94
94
trampoline: Full path on the local system to the CGI wrapper program
137
139
fixup_environ(req, script_path)
139
141
# usage: tramp uid jail_dir working_dir script_path
140
pid = subprocess.Popen(
141
[trampoline, str(uid), jail_dir, working_dir, interpreter,
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,
143
150
stdin=f, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
342
349
""" % (warning, text))
344
location_cgi_python = os.path.join(ivle.conf.lib_path, "trampoline")
346
351
# Mapping of interpreter names (as given in conf/app/server.py) to
347
352
# interpreter functions.
349
354
interpreter_objects = {
351
: functools.partial(execute_cgi, "/usr/bin/python",
352
location_cgi_python),
356
: functools.partial(execute_cgi, "/usr/bin/python"),
354
: functools.partial(execute_cgi, None,
355
location_cgi_python),
358
: functools.partial(execute_cgi, None),
356
359
# Should also have:
358
361
# python-server-page
413
416
# SERVER_SOFTWARE is actually not Apache but IVLE, since we are
414
417
# custom-making the CGI request.
415
env['SERVER_SOFTWARE'] = "IVLE/" + str(ivle.conf.ivle_version)
418
env['SERVER_SOFTWARE'] = "IVLE/" + ivle.__version__
417
420
# Additional environment variables
418
username = studpath.url_to_jailpaths(req.path)[0]
421
username = split_path(req.path)[0]
419
422
env['HOME'] = os.path.join('/home', username)
421
424
class ExecutionError(Exception):
424
def execute_raw(user, jail_dir, working_dir, binary, args):
427
def execute_raw(config, user, jail_dir, working_dir, binary, args):
425
428
'''Execute a binary in a user's jail, returning the raw output.
427
430
The binary is executed in the given working directory with the given
428
431
args. A tuple of (stdout, stderr) is returned.
431
tramp = location_cgi_python
432
tramp_dir = os.path.split(location_cgi_python)[0]
434
tramp = os.path.join(config['paths']['lib'], 'trampoline')
435
tramp_dir = os.path.split(tramp)[0]
434
437
# Fire up trampoline. Vroom, vroom.
435
proc = subprocess.Popen(
436
[tramp, str(user.unixid), jail_dir, working_dir, binary] + args,
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,
437
446
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
438
447
stderr=subprocess.PIPE, cwd=tramp_dir, close_fds=True)
439
exitcode = proc.wait()
449
(stdout, stderr) = proc.communicate()
450
exitcode = proc.returncode
441
452
if exitcode != 0:
442
453
raise ExecutionError('subprocess ended with code %d, stderr %s' %
443
454
(exitcode, proc.stderr.read()))
444
return (proc.stdout.read(), proc.stderr.read())
455
return (stdout, stderr)