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

« back to all changes in this revision

Viewing changes to ivle/interpret.py

Minor updates to the sql for the userdb

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.util import IVLEError, IVLEJailError
 
26
import ivle.conf
27
27
 
28
28
import functools
29
29
 
88
88
        self.linebuf = ""
89
89
        self.headers = {}       # Header names : values
90
90
 
91
 
def execute_cgi(interpreter, uid, jail_dir, working_dir, script_path,
92
 
                req, gentle):
 
91
def execute_cgi(interpreter, trampoline, uid, jail_dir, working_dir,
 
92
                script_path, req, gentle):
93
93
    """
94
94
    trampoline: Full path on the local system to the CGI wrapper program
95
95
        being executed.
105
105
    its environment.
106
106
    """
107
107
 
108
 
    trampoline = os.path.join(req.config['paths']['lib'], 'trampoline')
109
 
 
110
108
    # Support no-op trampoline runs.
111
109
    if interpreter is None:
112
110
        interpreter = '/bin/true'
140
138
 
141
139
    # usage: tramp uid jail_dir working_dir script_path
142
140
    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],
 
141
        [trampoline, str(uid), jail_dir, working_dir, interpreter,
 
142
        script_path],
147
143
        stdin=f, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
148
144
        cwd=tramp_dir)
149
145
 
345
341
    <pre>
346
342
""" % (warning, text))
347
343
 
 
344
location_cgi_python = os.path.join(ivle.conf.lib_path, "trampoline")
 
345
 
348
346
# Mapping of interpreter names (as given in conf/app/server.py) to
349
347
# interpreter functions.
350
348
 
351
349
interpreter_objects = {
352
350
    'cgi-python'
353
 
        : functools.partial(execute_cgi, "/usr/bin/python"),
 
351
        : functools.partial(execute_cgi, "/usr/bin/python",
 
352
            location_cgi_python),
354
353
    'noop'
355
 
        : functools.partial(execute_cgi, None),
 
354
        : functools.partial(execute_cgi, None,
 
355
            location_cgi_python),
356
356
    # Should also have:
357
357
    # cgi-generic
358
358
    # python-server-page
403
403
    if script_path and script_path.startswith('/home'):
404
404
        normscript = os.path.normpath(script_path)
405
405
 
406
 
        uri_into_jail = studpath.to_home_path(os.path.normpath(req.path))
 
406
        uri_into_jail = studpath.url_to_jailpaths(os.path.normpath(req.path))[2]
407
407
 
408
408
        # PATH_INFO is wrong because the script doesn't physically exist.
409
409
        env['PATH_INFO'] = uri_into_jail[len(normscript):]
412
412
 
413
413
    # SERVER_SOFTWARE is actually not Apache but IVLE, since we are
414
414
    # custom-making the CGI request.
415
 
    env['SERVER_SOFTWARE'] = "IVLE/" + ivle.__version__
 
415
    env['SERVER_SOFTWARE'] = "IVLE/" + str(ivle.conf.ivle_version)
416
416
 
417
417
    # Additional environment variables
418
 
    username = split_path(req.path)[0]
 
418
    username = studpath.url_to_jailpaths(req.path)[0]
419
419
    env['HOME'] = os.path.join('/home', username)
420
420
 
421
421
class ExecutionError(Exception):
422
422
    pass
423
423
 
424
 
def execute_raw(config, user, jail_dir, working_dir, binary, args):
 
424
def execute_raw(user, jail_dir, working_dir, binary, args):
425
425
    '''Execute a binary in a user's jail, returning the raw output.
426
426
 
427
427
    The binary is executed in the given working directory with the given
428
428
    args. A tuple of (stdout, stderr) is returned.
429
429
    '''
430
430
 
431
 
    tramp = os.path.join(config['paths']['lib'], 'trampoline')
432
 
    tramp_dir = os.path.split(tramp)[0]
 
431
    tramp = location_cgi_python
 
432
    tramp_dir = os.path.split(location_cgi_python)[0]
433
433
 
434
434
    # Fire up trampoline. Vroom, vroom.
435
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,
 
436
        [tramp, str(user.unixid), jail_dir, working_dir, binary] + args,
440
437
        stdin=subprocess.PIPE, stdout=subprocess.PIPE,
441
438
        stderr=subprocess.PIPE, cwd=tramp_dir, close_fds=True)
442
 
 
443
 
    (stdout, stderr) = proc.communicate()
444
 
    exitcode = proc.returncode
 
439
    exitcode = proc.wait()
445
440
 
446
441
    if exitcode != 0:
447
442
        raise ExecutionError('subprocess ended with code %d, stderr %s' %
448
443
                             (exitcode, proc.stderr.read()))
449
 
    return (stdout, stderr)
 
444
    return (proc.stdout.read(), proc.stderr.read())