37
37
# working on smaller output
39
39
CGI_BLOCK_SIZE = 65535
40
PATH = "/usr/local/bin:/usr/bin:/bin"
42
41
def interpret_file(req, owner, jail_dir, filename, interpreter, gentle=True):
43
42
"""Serves a file by interpreting it using one of IVLE's builtin
74
73
# (Note that paths "relative" to the jail actually begin with a '/' as
75
74
# they are absolute in the jailspace)
77
return interpreter(owner, jail_dir, working_dir, filename_abs, req,
76
return interpreter(owner.unixid, jail_dir, working_dir, filename_abs, req,
90
89
self.headers = {} # Header names : values
92
def execute_cgi(interpreter, owner, jail_dir, working_dir, script_path,
91
def execute_cgi(interpreter, uid, jail_dir, working_dir, script_path,
95
94
trampoline: Full path on the local system to the CGI wrapper program
97
owner: User object of the owner of the file.
96
uid: User ID of the owner of the file.
98
97
jail_dir: Absolute path of owner's jail directory.
99
98
working_dir: Directory containing the script file relative to owner's
130
129
f.seek(0) # Rewind, for reading
132
131
# Set up the environment
133
environ = cgi_environ(req, script_path, owner)
132
# This automatically asks mod_python to load up the CGI variables into the
133
# environment (which is a good first approximation)
134
old_env = os.environ.copy()
135
for k in os.environ.keys():
137
for (k,v) in req.get_cgi_environ().items():
139
fixup_environ(req, script_path)
135
141
# usage: tramp uid jail_dir working_dir script_path
136
cmd_line = [trampoline, str(owner.unixid),
137
req.config['paths']['jails']['mounts'],
138
req.config['paths']['jails']['src'],
139
req.config['paths']['jails']['template'],
140
jail_dir, working_dir, interpreter, 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]
141
146
# Popen doesn't like unicode strings. It hateses them.
142
147
cmd_line = [(s.encode('utf-8') if isinstance(s, unicode) else s)
143
148
for s in cmd_line]
144
149
pid = subprocess.Popen(cmd_line,
145
150
stdin=f, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
146
cwd=tramp_dir, env=environ)
153
# Restore the environment
154
for k in os.environ.keys():
156
for (k,v) in old_env.items():
148
159
# We don't want any output! Bail out after the process terminates.
218
229
# Is this an internal IVLE error condition?
219
230
hs = cgiflags.headers
220
231
if 'X-IVLE-Error-Type' in hs:
222
raise IVLEJailError(hs['X-IVLE-Error-Type'],
223
hs['X-IVLE-Error-Message'],
224
hs['X-IVLE-Error-Info'])
226
raise AssertionError("Bad error headers written by CGI.")
232
t = hs['X-IVLE-Error-Type']
233
if t == IVLEError.__name__:
234
raise IVLEError(int(hs['X-IVLE-Error-Code']),
235
hs['X-IVLE-Error-Message'])
238
raise IVLEJailError(hs['X-IVLE-Error-Type'],
239
hs['X-IVLE-Error-Message'],
240
hs['X-IVLE-Error-Info'])
242
raise IVLEError(500, 'bad error headers written by CGI')
228
244
# Check to make sure the required headers were written
229
245
if cgiflags.wrote_html_warning or not cgiflags.gentle:
345
361
# python-server-page
348
def cgi_environ(req, script_path, user):
349
"""Gets CGI variables from apache and makes a few changes for security and
364
def fixup_environ(req, script_path):
365
"""Assuming os.environ has been written with the CGI variables from
366
apache, make a few changes for security and correctness.
352
368
Does not modify req, only reads it.
355
371
# Comments here are on the heavy side, explained carefully for security
356
372
# reasons. Please read carefully before making changes.
358
# This automatically asks mod_python to load up the CGI variables into the
359
# environment (which is a good first approximation)
360
for (k,v) in req.get_cgi_environ().items():
363
374
# Remove DOCUMENT_ROOT and SCRIPT_FILENAME. Not part of CGI spec and
364
375
# exposes unnecessary details about server.
407
418
env['SERVER_SOFTWARE'] = "IVLE/" + ivle.__version__
409
420
# Additional environment variables
410
username = user.login
421
username = split_path(req.path)[0]
411
422
env['HOME'] = os.path.join('/home', username)
415
424
class ExecutionError(Exception):
435
444
for s in cmd_line]
436
445
proc = subprocess.Popen(cmd_line,
437
446
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
438
stderr=subprocess.PIPE, cwd=tramp_dir, close_fds=True,
439
env={'HOME': os.path.join('/home', user.login),
442
'LOGNAME': user.login})
447
stderr=subprocess.PIPE, cwd=tramp_dir, close_fds=True)
444
449
(stdout, stderr) = proc.communicate()
445
450
exitcode = proc.returncode
447
452
if exitcode != 0:
448
raise ExecutionError('subprocess ended with code %d, stderr: "%s"' %
453
raise ExecutionError('subprocess ended with code %d, stderr %s' %
454
(exitcode, proc.stderr.read()))
450
455
return (stdout, stderr)