~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to ivle/interpret.py

  • Committer: William Grant
  • Date: 2009-01-20 01:33:41 UTC
  • mto: This revision was merged to the branch mainline in revision 1090.
  • Revision ID: grantw@unimelb.edu.au-20090120013341-2f32aruefwotgce0
ivle.database.Offering: Add project_sets referenceset.
www/apps/userservice: Replace usage of ivle.db.get_projectsets_by_offering
   with Storm calls.
ivle.db.get_projectsets_by_offering: Kill. Unused.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
# Runs a student script in a safe execution environment.
23
23
 
24
 
import ivle
25
24
from ivle import studpath
26
 
from ivle.util import IVLEError, IVLEJailError, split_path
 
25
from ivle import db
 
26
from ivle.util import IVLEError, IVLEJailError
 
27
import ivle.conf
27
28
 
28
29
import functools
29
30
 
88
89
        self.linebuf = ""
89
90
        self.headers = {}       # Header names : values
90
91
 
91
 
def execute_cgi(interpreter, uid, jail_dir, working_dir, script_path,
92
 
                req, gentle):
 
92
def execute_cgi(interpreter, trampoline, uid, jail_dir, working_dir,
 
93
                script_path, req, gentle):
93
94
    """
94
95
    trampoline: Full path on the local system to the CGI wrapper program
95
96
        being executed.
105
106
    its environment.
106
107
    """
107
108
 
108
 
    trampoline = os.path.join(req.config['paths']['lib'], 'trampoline')
109
 
 
110
109
    # Support no-op trampoline runs.
111
110
    if interpreter is None:
112
111
        interpreter = '/bin/true'
136
135
        del os.environ[k]
137
136
    for (k,v) in req.get_cgi_environ().items():
138
137
        os.environ[k] = v
139
 
    fixup_environ(req, script_path)
 
138
    fixup_environ(req)
140
139
 
141
140
    # usage: tramp uid jail_dir working_dir script_path
142
141
    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
        [trampoline, str(uid), jail_dir, working_dir, interpreter,
 
143
        script_path],
147
144
        stdin=f, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
148
145
        cwd=tramp_dir)
149
146
 
345
342
    <pre>
346
343
""" % (warning, text))
347
344
 
 
345
location_cgi_python = os.path.join(ivle.conf.lib_path, "trampoline")
 
346
 
348
347
# Mapping of interpreter names (as given in conf/app/server.py) to
349
348
# interpreter functions.
350
349
 
351
350
interpreter_objects = {
352
351
    'cgi-python'
353
 
        : functools.partial(execute_cgi, "/usr/bin/python"),
 
352
        : functools.partial(execute_cgi, "/usr/bin/python",
 
353
            location_cgi_python),
354
354
    'noop'
355
 
        : functools.partial(execute_cgi, None),
 
355
        : functools.partial(execute_cgi, None,
 
356
            location_cgi_python),
356
357
    # Should also have:
357
358
    # cgi-generic
358
359
    # python-server-page
359
360
}
360
361
 
361
 
def fixup_environ(req, script_path):
 
362
def fixup_environ(req):
362
363
    """Assuming os.environ has been written with the CGI variables from
363
364
    apache, make a few changes for security and correctness.
364
365
 
384
385
        del env['PATH']
385
386
    except: pass
386
387
 
 
388
    # Remove SCRIPT_FILENAME. Not part of CGI spec (see SCRIPT_NAME).
 
389
 
 
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'] = ''
 
396
 
387
397
    # CGI specifies that REMOTE_HOST SHOULD be set, and MAY just be set to
388
398
    # REMOTE_ADDR. Since Apache does not appear to set this, set it to
389
399
    # REMOTE_ADDR.
390
400
    if 'REMOTE_HOST' not in env and 'REMOTE_ADDR' in env:
391
401
        env['REMOTE_HOST'] = env['REMOTE_ADDR']
392
402
 
393
 
    env['PATH_INFO'] = ''
394
 
    del env['PATH_TRANSLATED']
395
 
 
396
 
    normuri = os.path.normpath(req.uri)
397
 
    env['SCRIPT_NAME'] = normuri
398
 
 
399
403
    # SCRIPT_NAME is the path to the script WITHOUT PATH_INFO.
400
 
    # We don't care about these if the script is null (ie. noop).
401
 
    # XXX: We check for /home because we don't want to interfere with
402
 
    # CGIRequest, which fileservice still uses.
403
 
    if script_path and script_path.startswith('/home'):
404
 
        normscript = os.path.normpath(script_path)
405
 
 
406
 
        uri_into_jail = studpath.to_home_path(os.path.normpath(req.path))
407
 
 
408
 
        # PATH_INFO is wrong because the script doesn't physically exist.
409
 
        env['PATH_INFO'] = uri_into_jail[len(normscript):]
410
 
        if len(env['PATH_INFO']) > 0:
411
 
            env['SCRIPT_NAME'] = normuri[:-len(env['PATH_INFO'])]
 
404
    script_name = req.uri
 
405
    env['SCRIPT_NAME'] = script_name
412
406
 
413
407
    # SERVER_SOFTWARE is actually not Apache but IVLE, since we are
414
408
    # custom-making the CGI request.
415
 
    env['SERVER_SOFTWARE'] = "IVLE/" + ivle.__version__
 
409
    env['SERVER_SOFTWARE'] = "IVLE/" + str(ivle.conf.ivle_version)
416
410
 
417
411
    # Additional environment variables
418
 
    username = split_path(req.path)[0]
 
412
    username = studpath.url_to_jailpaths(req.path)[0]
419
413
    env['HOME'] = os.path.join('/home', username)
420
 
 
421
 
class ExecutionError(Exception):
422
 
    pass
423
 
 
424
 
def execute_raw(config, user, jail_dir, working_dir, binary, args):
425
 
    '''Execute a binary in a user's jail, returning the raw output.
426
 
 
427
 
    The binary is executed in the given working directory with the given
428
 
    args. A tuple of (stdout, stderr) is returned.
429
 
    '''
430
 
 
431
 
    tramp = os.path.join(config['paths']['lib'], 'trampoline')
432
 
    tramp_dir = os.path.split(tramp)[0]
433
 
 
434
 
    # Fire up trampoline. Vroom, vroom.
435
 
    proc = subprocess.Popen(
436
 
        [tramp, str(user.unixid), config['paths']['jails']['mounts'],
437
 
         config['paths']['jails']['src'],
438
 
         config['paths']['jails']['template'],
439
 
         jail_dir, working_dir, binary] + args,
440
 
        stdin=subprocess.PIPE, stdout=subprocess.PIPE,
441
 
        stderr=subprocess.PIPE, cwd=tramp_dir, close_fds=True)
442
 
 
443
 
    (stdout, stderr) = proc.communicate()
444
 
    exitcode = proc.returncode
445
 
 
446
 
    if exitcode != 0:
447
 
        raise ExecutionError('subprocess ended with code %d, stderr %s' %
448
 
                             (exitcode, proc.stderr.read()))
449
 
    return (stdout, stderr)