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

« back to all changes in this revision

Viewing changes to ivle/interpret.py

  • Committer: Matt Giuca
  • Date: 2009-02-25 02:17:36 UTC
  • mfrom: (1109)
  • mto: This revision was merged to the branch mainline in revision 1119.
  • Revision ID: matt.giuca@gmail.com-20090225021736-fnnpc7stnemede00
MergedĀ fromĀ trunk.

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'
139
137
    fixup_environ(req, script_path)
140
138
 
141
139
    # usage: tramp uid jail_dir working_dir 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]
146
 
    # Popen doesn't like unicode strings. It hateses them.
147
 
    cmd_line = [(s.encode('utf-8') if isinstance(s, unicode) else s)
148
 
                for s in cmd_line]
149
 
    pid = subprocess.Popen(cmd_line,
 
140
    pid = subprocess.Popen(
 
141
        [trampoline, str(uid), ivle.conf.jail_base, ivle.conf.jail_src_base,
 
142
         ivle.conf.jail_system, jail_dir, working_dir, interpreter,
 
143
        script_path],
150
144
        stdin=f, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
151
145
        cwd=tramp_dir)
152
146
 
348
342
    <pre>
349
343
""" % (warning, text))
350
344
 
 
345
location_cgi_python = os.path.join(ivle.conf.lib_path, "trampoline")
 
346
 
351
347
# Mapping of interpreter names (as given in conf/app/server.py) to
352
348
# interpreter functions.
353
349
 
354
350
interpreter_objects = {
355
351
    'cgi-python'
356
 
        : functools.partial(execute_cgi, "/usr/bin/python"),
 
352
        : functools.partial(execute_cgi, "/usr/bin/python",
 
353
            location_cgi_python),
357
354
    'noop'
358
 
        : functools.partial(execute_cgi, None),
 
355
        : functools.partial(execute_cgi, None,
 
356
            location_cgi_python),
359
357
    # Should also have:
360
358
    # cgi-generic
361
359
    # python-server-page
406
404
    if script_path and script_path.startswith('/home'):
407
405
        normscript = os.path.normpath(script_path)
408
406
 
409
 
        uri_into_jail = studpath.to_home_path(os.path.normpath(req.path))
 
407
        uri_into_jail = studpath.url_to_jailpaths(os.path.normpath(req.path))[2]
410
408
 
411
409
        # PATH_INFO is wrong because the script doesn't physically exist.
412
410
        env['PATH_INFO'] = uri_into_jail[len(normscript):]
415
413
 
416
414
    # SERVER_SOFTWARE is actually not Apache but IVLE, since we are
417
415
    # custom-making the CGI request.
418
 
    env['SERVER_SOFTWARE'] = "IVLE/" + ivle.__version__
 
416
    env['SERVER_SOFTWARE'] = "IVLE/" + str(ivle.conf.ivle_version)
419
417
 
420
418
    # Additional environment variables
421
 
    username = split_path(req.path)[0]
 
419
    username = studpath.url_to_jailpaths(req.path)[0]
422
420
    env['HOME'] = os.path.join('/home', username)
423
421
 
424
422
class ExecutionError(Exception):
425
423
    pass
426
424
 
427
 
def execute_raw(config, user, jail_dir, working_dir, binary, args):
 
425
def execute_raw(user, jail_dir, working_dir, binary, args):
428
426
    '''Execute a binary in a user's jail, returning the raw output.
429
427
 
430
428
    The binary is executed in the given working directory with the given
431
429
    args. A tuple of (stdout, stderr) is returned.
432
430
    '''
433
431
 
434
 
    tramp = os.path.join(config['paths']['lib'], 'trampoline')
435
 
    tramp_dir = os.path.split(tramp)[0]
 
432
    tramp = location_cgi_python
 
433
    tramp_dir = os.path.split(location_cgi_python)[0]
436
434
 
437
435
    # Fire up trampoline. Vroom, vroom.
438
 
    cmd_line = [tramp, str(user.unixid), config['paths']['jails']['mounts'],
439
 
         config['paths']['jails']['src'],
440
 
         config['paths']['jails']['template'],
441
 
         jail_dir, working_dir, binary] + args
442
 
    # Popen doesn't like unicode strings. It hateses them.
443
 
    cmd_line = [(s.encode('utf-8') if isinstance(s, unicode) else s)
444
 
                for s in cmd_line]
445
 
    proc = subprocess.Popen(cmd_line,
 
436
    proc = subprocess.Popen(
 
437
        [tramp, str(user.unixid), ivle.conf.jail_base,
 
438
         ivle.conf.jail_src_base, ivle.conf.jail_system, jail_dir,
 
439
         working_dir, binary] + args,
446
440
        stdin=subprocess.PIPE, stdout=subprocess.PIPE,
447
441
        stderr=subprocess.PIPE, cwd=tramp_dir, close_fds=True)
448
 
 
449
 
    (stdout, stderr) = proc.communicate()
450
 
    exitcode = proc.returncode
 
442
    exitcode = proc.wait()
451
443
 
452
444
    if exitcode != 0:
453
445
        raise ExecutionError('subprocess ended with code %d, stderr %s' %
454
446
                             (exitcode, proc.stderr.read()))
455
 
    return (stdout, stderr)
 
447
    return (proc.stdout.read(), proc.stderr.read())