73
73
# (Note that paths "relative" to the jail actually begin with a '/' as
74
74
# they are absolute in the jailspace)
76
return interpreter(owner.unixid, jail_dir, working_dir, filename_abs, req,
76
return interpreter(owner, jail_dir, working_dir, filename_abs, req,
89
89
self.headers = {} # Header names : values
91
def execute_cgi(interpreter, uid, jail_dir, working_dir, script_path,
91
def execute_cgi(interpreter, owner, jail_dir, working_dir, script_path,
94
94
trampoline: Full path on the local system to the CGI wrapper program
96
uid: User ID of the owner of the file.
96
owner: User object of the owner of the file.
97
97
jail_dir: Absolute path of owner's jail directory.
98
98
working_dir: Directory containing the script file relative to owner's
136
136
del os.environ[k]
137
137
for (k,v) in req.get_cgi_environ().items():
138
138
os.environ[k] = v
139
fixup_environ(req, script_path)
139
fixup_environ(req, script_path, owner)
141
141
# usage: tramp uid jail_dir working_dir script_path
142
pid = subprocess.Popen(
143
[trampoline, str(uid), 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],
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,
147
151
stdin=f, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
358
362
# python-server-page
361
def fixup_environ(req, script_path):
365
def fixup_environ(req, script_path, user):
362
366
"""Assuming os.environ has been written with the CGI variables from
363
367
apache, make a few changes for security and correctness.
415
419
env['SERVER_SOFTWARE'] = "IVLE/" + ivle.__version__
417
421
# Additional environment variables
418
username = split_path(req.path)[0]
422
username = user.login
419
423
env['HOME'] = os.path.join('/home', username)
421
425
class ExecutionError(Exception):
432
436
tramp_dir = os.path.split(tramp)[0]
434
438
# Fire up trampoline. Vroom, vroom.
435
proc = subprocess.Popen(
436
[tramp, str(user.unixid), config['paths']['jails']['mounts'],
439
cmd_line = [tramp, str(user.unixid), config['paths']['jails']['mounts'],
437
440
config['paths']['jails']['src'],
438
441
config['paths']['jails']['template'],
439
jail_dir, working_dir, binary] + args,
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,
440
447
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
441
stderr=subprocess.PIPE, cwd=tramp_dir, close_fds=True)
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})
443
454
(stdout, stderr) = proc.communicate()
444
455
exitcode = proc.returncode
446
457
if exitcode != 0:
447
raise ExecutionError('subprocess ended with code %d, stderr %s' %
448
(exitcode, proc.stderr.read()))
458
raise ExecutionError('subprocess ended with code %d, stderr: "%s"' %
449
460
return (stdout, stderr)