95
90
self.headers = {} # Header names : values
97
def execute_cgi(interpreter, owner, jail_dir, working_dir, script_path,
98
req, gentle, overrides=None):
92
def execute_cgi(interpreter, trampoline, uid, jail_dir, working_dir,
93
script_path, req, gentle):
100
95
trampoline: Full path on the local system to the CGI wrapper program
102
owner: User object of the owner of the file.
97
uid: User ID of the owner of the file.
103
98
jail_dir: Absolute path of owner's jail directory.
104
99
working_dir: Directory containing the script file relative to owner's
106
101
script_path: CGI script relative to the owner's jail.
107
102
req: IVLE request object.
109
overrides: A dict mapping env var names to strings, to override arbitrary
110
environment variables in the resulting CGI environent.
112
104
The called CGI wrapper application shall be called using popen and receive
113
105
the HTTP body on stdin. It shall receive the CGI environment variables to
117
trampoline = os.path.join(req.config['paths']['lib'], 'trampoline')
119
109
# Support no-op trampoline runs.
120
110
if interpreter is None:
121
111
interpreter = '/bin/true'
138
128
f.seek(0) # Rewind, for reading
140
130
# Set up the environment
141
environ = cgi_environ(req, script_path, owner, overrides=overrides)
131
# This automatically asks mod_python to load up the CGI variables into the
132
# environment (which is a good first approximation)
133
old_env = os.environ.copy()
134
for k in os.environ.keys():
136
for (k,v) in req.get_cgi_environ().items():
143
140
# usage: tramp uid jail_dir working_dir script_path
144
cmd_line = [trampoline, str(owner.unixid),
145
req.config['paths']['jails']['mounts'],
146
req.config['paths']['jails']['src'],
147
req.config['paths']['jails']['template'],
148
jail_dir, working_dir, interpreter, script_path]
149
# Popen doesn't like unicode strings. It hateses them.
150
cmd_line = [(s.encode('utf-8') if isinstance(s, unicode) else s)
152
pid = subprocess.Popen(cmd_line,
141
pid = subprocess.Popen(
142
[trampoline, str(uid), jail_dir, working_dir, interpreter,
153
144
stdin=f, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
154
cwd=tramp_dir, env=environ)
147
# Restore the environment
148
for k in os.environ.keys():
150
for (k,v) in old_env.items():
156
153
# We don't want any output! Bail out after the process terminates.
294
293
process_cgi_output(req, line + '\n', cgiflags)
297
# Check if CGI field-name is valid
298
CGI_SEPERATORS = set(['(', ')', '<', '>', '@', ',', ';', ':', '\\', '"',
299
'/', '[', ']', '?', '=', '{', '}', ' ', '\t'])
300
if any((char in CGI_SEPERATORS for char in name)):
302
if not cgiflags.gentle:
303
message = """An unexpected server error has occured."""
306
# Header contained illegal characters
307
message = """You printed an invalid CGI header. CGI header
308
field-names can not contain any of the following characters:
309
<code>( ) < > @ , ; : \\ " / [ ] ? = { } <em>SPACE
311
write_html_warning(req, message, warning=warning)
312
cgiflags.wrote_html_warning = True
313
# Handle the rest of this line as normal data
314
process_cgi_output(req, line + '\n', cgiflags)
317
296
# Read CGI headers
318
297
value = value.strip()
319
298
if name == "Content-Type":
364
343
""" % (warning, text))
345
location_cgi_python = os.path.join(ivle.conf.lib_path, "trampoline")
366
347
# Mapping of interpreter names (as given in conf/app/server.py) to
367
348
# interpreter functions.
369
350
interpreter_objects = {
371
: functools.partial(execute_cgi, "/usr/bin/python"),
352
: functools.partial(execute_cgi, "/usr/bin/python",
353
location_cgi_python),
373
: functools.partial(execute_cgi, None),
355
: functools.partial(execute_cgi, None,
356
location_cgi_python),
374
357
# Should also have:
376
359
# python-server-page
379
def cgi_environ(req, script_path, user, overrides=None):
380
"""Gets CGI variables from apache and makes a few changes for security and
362
def fixup_environ(req):
363
"""Assuming os.environ has been written with the CGI variables from
364
apache, make a few changes for security and correctness.
383
366
Does not modify req, only reads it.
385
overrides: A dict mapping env var names to strings, to override arbitrary
386
environment variables in the resulting CGI environent.
389
369
# Comments here are on the heavy side, explained carefully for security
390
370
# reasons. Please read carefully before making changes.
392
# This automatically asks mod_python to load up the CGI variables into the
393
# environment (which is a good first approximation)
394
for (k,v) in req.get_cgi_environ().items():
397
372
# Remove DOCUMENT_ROOT and SCRIPT_FILENAME. Not part of CGI spec and
398
373
# exposes unnecessary details about server.
388
# Remove SCRIPT_FILENAME. Not part of CGI spec (see SCRIPT_NAME).
390
# PATH_INFO is wrong because the script doesn't physically exist.
391
# Apache makes it relative to the "serve" app. It should actually be made
392
# relative to the student's script. intepretservice does that in the jail,
393
# so here we just clear it.
394
env['PATH_INFO'] = ''
395
env['PATH_TRANSLATED'] = ''
413
397
# CGI specifies that REMOTE_HOST SHOULD be set, and MAY just be set to
414
398
# REMOTE_ADDR. Since Apache does not appear to set this, set it to
416
400
if 'REMOTE_HOST' not in env and 'REMOTE_ADDR' in env:
417
401
env['REMOTE_HOST'] = env['REMOTE_ADDR']
419
env['PATH_INFO'] = ''
420
del env['PATH_TRANSLATED']
422
normuri = os.path.normpath(req.uri)
423
env['SCRIPT_NAME'] = normuri
425
403
# SCRIPT_NAME is the path to the script WITHOUT PATH_INFO.
426
# We don't care about these if the script is null (ie. noop).
427
# XXX: We check for /home because we don't want to interfere with
428
# CGIRequest, which fileservice still uses.
429
if script_path and script_path.startswith('/home'):
430
normscript = os.path.normpath(script_path)
432
uri_into_jail = studpath.to_home_path(os.path.normpath(req.path))
434
# PATH_INFO is wrong because the script doesn't physically exist.
435
env['PATH_INFO'] = uri_into_jail[len(normscript):]
436
if len(env['PATH_INFO']) > 0:
437
env['SCRIPT_NAME'] = normuri[:-len(env['PATH_INFO'])]
404
script_name = req.uri
405
env['SCRIPT_NAME'] = script_name
439
407
# SERVER_SOFTWARE is actually not Apache but IVLE, since we are
440
408
# custom-making the CGI request.
441
env['SERVER_SOFTWARE'] = "IVLE/" + ivle.__version__
409
env['SERVER_SOFTWARE'] = "IVLE/" + str(ivle.conf.ivle_version)
443
411
# Additional environment variables
444
username = user.login
412
username = studpath.url_to_jailpaths(req.path)[0]
445
413
env['HOME'] = os.path.join('/home', username)
447
if overrides is not None:
448
env.update(overrides)
451
class ExecutionError(Exception):
454
def execute_raw(config, user, jail_dir, working_dir, binary, args):
455
'''Execute a binary in a user's jail, returning the raw output.
457
The binary is executed in the given working directory with the given
458
args. A tuple of (stdout, stderr) is returned.
461
tramp = os.path.join(config['paths']['lib'], 'trampoline')
462
tramp_dir = os.path.split(tramp)[0]
464
# Fire up trampoline. Vroom, vroom.
465
cmd_line = [tramp, str(user.unixid), config['paths']['jails']['mounts'],
466
config['paths']['jails']['src'],
467
config['paths']['jails']['template'],
468
jail_dir, working_dir, binary] + args
469
# Popen doesn't like unicode strings. It hateses them.
470
cmd_line = [(s.encode('utf-8') if isinstance(s, unicode) else s)
472
proc = subprocess.Popen(cmd_line,
473
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
474
stderr=subprocess.PIPE, cwd=tramp_dir, close_fds=True,
475
env={'HOME': os.path.join('/home', user.login),
478
'LOGNAME': user.login})
480
(stdout, stderr) = proc.communicate()
481
exitcode = proc.returncode
484
raise ExecutionError('subprocess ended with code %d, stderr: "%s"' %
486
return (stdout, stderr)
488
def jail_call(req, cgi_script, script_name, query_string=None,
489
request_method="GET", extra_overrides=None):
491
Makes a call to a CGI script inside the jail from outside the jail.
492
This can be used to allow Python scripts to access jail-only functions and
493
data without having to perform a full API request.
495
req: A Request object (will not be written to or attributes modified).
496
cgi_script: Path to cgi script outside of jail.
497
eg: os.path.join(req.config['paths']['share'],
498
'services/fileservice')
499
script_name: Name to set as SCRIPT_NAME for the CGI environment.
501
query_string: Query string to set as QUERY_STRING for the CGI environment.
502
eg: "action=svnrepostat&path=/users/studenta/"
503
request_method: Method to set as REQUEST_METHOD for the CGI environment.
504
eg: "POST". Defaults to "GET".
505
extra_overrides: A dict mapping env var names to strings, to override
506
arbitrary environment variables in the resulting CGI environent.
508
Returns a triple (status_code, content_type, contents).
510
interp_object = interpreter_objects["cgi-python"]
511
user_jail_dir = os.path.join(req.config['paths']['jails']['mounts'],
514
"SCRIPT_NAME": script_name,
515
"QUERY_STRING": query_string,
516
"REQUEST_URI": "%s%s%s" % (script_name, "?" if query_string else "",
518
"REQUEST_METHOD": request_method,
520
if extra_overrides is not None:
521
overrides.update(extra_overrides)
522
result = DummyReq(req)
523
interpret_file(result, req.user, user_jail_dir, cgi_script, interp_object,
524
gentle=False, overrides=overrides)
525
return result.status, result.content_type, result.getvalue()
527
class DummyReq(StringIO.StringIO):
528
"""A dummy request object, built from a real request object, which can be
529
used like a req but doesn't mutate the existing request.
530
(Used for reading CGI responses as strings rather than forwarding their
531
output to the current request.)
533
def __init__(self, req):
534
StringIO.StringIO.__init__(self)
536
def get_cgi_environ(self):
537
return self._real_req.get_cgi_environ()
538
def __getattr__(self, name):
539
return getattr(self._real_req, name)