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

« back to all changes in this revision

Viewing changes to ivle/interpret.py

  • Committer: William Grant
  • Date: 2010-05-07 07:16:52 UTC
  • Revision ID: grantw@unimelb.edu.au-20100507071652-4z2zidt9le05ueix
Add unique indices on assessed(loginid, projectid) and assessed(groupid, projectid), where they should have been from the start.

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
24
25
from ivle import studpath
25
 
from ivle import db
26
 
from ivle.util import IVLEError, IVLEJailError
27
 
import ivle.conf
 
26
from ivle.util import IVLEJailError, split_path
28
27
 
29
28
import functools
30
29
 
38
37
# working on smaller output
39
38
 
40
39
CGI_BLOCK_SIZE = 65535
 
40
PATH = "/usr/local/bin:/usr/bin:/bin"
41
41
 
42
42
def interpret_file(req, owner, jail_dir, filename, interpreter, gentle=True):
43
43
    """Serves a file by interpreting it using one of IVLE's builtin
74
74
    # (Note that paths "relative" to the jail actually begin with a '/' as
75
75
    # they are absolute in the jailspace)
76
76
 
77
 
    return interpreter(owner.unixid, jail_dir, working_dir, filename_abs, req,
 
77
    return interpreter(owner, jail_dir, working_dir, filename_abs, req,
78
78
                       gentle)
79
79
 
80
80
class CGIFlags:
89
89
        self.linebuf = ""
90
90
        self.headers = {}       # Header names : values
91
91
 
92
 
def execute_cgi(interpreter, trampoline, uid, jail_dir, working_dir,
93
 
                script_path, req, gentle):
 
92
def execute_cgi(interpreter, owner, jail_dir, working_dir, script_path,
 
93
                req, gentle):
94
94
    """
95
95
    trampoline: Full path on the local system to the CGI wrapper program
96
96
        being executed.
97
 
    uid: User ID of the owner of the file.
 
97
    owner: User object of the owner of the file.
98
98
    jail_dir: Absolute path of owner's jail directory.
99
99
    working_dir: Directory containing the script file relative to owner's
100
100
        jail.
106
106
    its environment.
107
107
    """
108
108
 
 
109
    trampoline = os.path.join(req.config['paths']['lib'], 'trampoline')
 
110
 
109
111
    # Support no-op trampoline runs.
110
112
    if interpreter is None:
111
113
        interpreter = '/bin/true'
128
130
        f.seek(0)       # Rewind, for reading
129
131
 
130
132
    # Set up the environment
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():
135
 
        del os.environ[k]
136
 
    for (k,v) in req.get_cgi_environ().items():
137
 
        os.environ[k] = v
138
 
    fixup_environ(req)
 
133
    environ = cgi_environ(req, script_path, owner)
139
134
 
140
135
    # usage: tramp uid jail_dir working_dir script_path
141
 
    pid = subprocess.Popen(
142
 
        [trampoline, str(uid), jail_dir, working_dir, interpreter,
143
 
        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]
 
141
    # Popen doesn't like unicode strings. It hateses them.
 
142
    cmd_line = [(s.encode('utf-8') if isinstance(s, unicode) else s)
 
143
                for s in cmd_line]
 
144
    pid = subprocess.Popen(cmd_line,
144
145
        stdin=f, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
145
 
        cwd=tramp_dir)
146
 
 
147
 
    # Restore the environment
148
 
    for k in os.environ.keys():
149
 
        del os.environ[k]
150
 
    for (k,v) in old_env.items():
151
 
        os.environ[k] = v
 
146
        cwd=tramp_dir, env=environ)
152
147
 
153
148
    # We don't want any output! Bail out after the process terminates.
154
149
    if noop:
220
215
            if len(split) == 1:
221
216
                split = headers.split('\n', 1)
222
217
 
223
 
        # Is this an internal IVLE error condition?
224
 
        hs = cgiflags.headers
225
 
        if 'X-IVLE-Error-Type' in hs:
226
 
            t = hs['X-IVLE-Error-Type']
227
 
            if t == IVLEError.__name__:
228
 
                raise IVLEError(int(hs['X-IVLE-Error-Code']),
229
 
                                hs['X-IVLE-Error-Message'])
230
 
            else:
 
218
        # If not executing in gentle mode (which presents CGI violations
 
219
        # to users nicely), check if this an internal IVLE error
 
220
        # condition.
 
221
        if not cgiflags.gentle:
 
222
            hs = cgiflags.headers
 
223
            if 'X-IVLE-Error-Type' in hs:
231
224
                try:
232
225
                    raise IVLEJailError(hs['X-IVLE-Error-Type'],
233
226
                                        hs['X-IVLE-Error-Message'],
234
227
                                        hs['X-IVLE-Error-Info'])
235
228
                except KeyError:
236
 
                    raise IVLEError(500, 'bad error headers written by CGI')
 
229
                    raise AssertionError("Bad error headers written by CGI.")
237
230
 
238
231
        # Check to make sure the required headers were written
239
232
        if cgiflags.wrote_html_warning or not cgiflags.gentle:
342
335
    <pre>
343
336
""" % (warning, text))
344
337
 
345
 
location_cgi_python = os.path.join(ivle.conf.lib_path, "trampoline")
346
 
 
347
338
# Mapping of interpreter names (as given in conf/app/server.py) to
348
339
# interpreter functions.
349
340
 
350
341
interpreter_objects = {
351
342
    'cgi-python'
352
 
        : functools.partial(execute_cgi, "/usr/bin/python",
353
 
            location_cgi_python),
 
343
        : functools.partial(execute_cgi, "/usr/bin/python"),
354
344
    'noop'
355
 
        : functools.partial(execute_cgi, None,
356
 
            location_cgi_python),
 
345
        : functools.partial(execute_cgi, None),
357
346
    # Should also have:
358
347
    # cgi-generic
359
348
    # python-server-page
360
349
}
361
350
 
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.
 
351
def cgi_environ(req, script_path, user):
 
352
    """Gets CGI variables from apache and makes a few changes for security and 
 
353
    correctness.
365
354
 
366
355
    Does not modify req, only reads it.
367
356
    """
368
 
    env = os.environ
 
357
    env = {}
369
358
    # Comments here are on the heavy side, explained carefully for security
370
359
    # reasons. Please read carefully before making changes.
 
360
    
 
361
    # This automatically asks mod_python to load up the CGI variables into the
 
362
    # environment (which is a good first approximation)
 
363
    for (k,v) in req.get_cgi_environ().items():
 
364
        env[k] = v
371
365
 
372
366
    # Remove DOCUMENT_ROOT and SCRIPT_FILENAME. Not part of CGI spec and
373
367
    # exposes unnecessary details about server.
385
379
        del env['PATH']
386
380
    except: pass
387
381
 
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
 
 
397
382
    # CGI specifies that REMOTE_HOST SHOULD be set, and MAY just be set to
398
383
    # REMOTE_ADDR. Since Apache does not appear to set this, set it to
399
384
    # REMOTE_ADDR.
400
385
    if 'REMOTE_HOST' not in env and 'REMOTE_ADDR' in env:
401
386
        env['REMOTE_HOST'] = env['REMOTE_ADDR']
402
387
 
 
388
    env['PATH_INFO'] = ''
 
389
    del env['PATH_TRANSLATED']
 
390
 
 
391
    normuri = os.path.normpath(req.uri)
 
392
    env['SCRIPT_NAME'] = normuri
 
393
 
403
394
    # SCRIPT_NAME is the path to the script WITHOUT PATH_INFO.
404
 
    script_name = req.uri
405
 
    env['SCRIPT_NAME'] = script_name
 
395
    # We don't care about these if the script is null (ie. noop).
 
396
    # XXX: We check for /home because we don't want to interfere with
 
397
    # CGIRequest, which fileservice still uses.
 
398
    if script_path and script_path.startswith('/home'):
 
399
        normscript = os.path.normpath(script_path)
 
400
 
 
401
        uri_into_jail = studpath.to_home_path(os.path.normpath(req.path))
 
402
 
 
403
        # PATH_INFO is wrong because the script doesn't physically exist.
 
404
        env['PATH_INFO'] = uri_into_jail[len(normscript):]
 
405
        if len(env['PATH_INFO']) > 0:
 
406
            env['SCRIPT_NAME'] = normuri[:-len(env['PATH_INFO'])]
406
407
 
407
408
    # SERVER_SOFTWARE is actually not Apache but IVLE, since we are
408
409
    # custom-making the CGI request.
409
 
    env['SERVER_SOFTWARE'] = "IVLE/" + str(ivle.conf.ivle_version)
 
410
    env['SERVER_SOFTWARE'] = "IVLE/" + ivle.__version__
410
411
 
411
412
    # Additional environment variables
412
 
    username = studpath.url_to_jailpaths(req.path)[0]
 
413
    username = user.login
413
414
    env['HOME'] = os.path.join('/home', username)
 
415
 
 
416
    return env
 
417
 
 
418
class ExecutionError(Exception):
 
419
    pass
 
420
 
 
421
def execute_raw(config, user, jail_dir, working_dir, binary, args):
 
422
    '''Execute a binary in a user's jail, returning the raw output.
 
423
 
 
424
    The binary is executed in the given working directory with the given
 
425
    args. A tuple of (stdout, stderr) is returned.
 
426
    '''
 
427
 
 
428
    tramp = os.path.join(config['paths']['lib'], 'trampoline')
 
429
    tramp_dir = os.path.split(tramp)[0]
 
430
 
 
431
    # Fire up trampoline. Vroom, vroom.
 
432
    cmd_line = [tramp, str(user.unixid), config['paths']['jails']['mounts'],
 
433
         config['paths']['jails']['src'],
 
434
         config['paths']['jails']['template'],
 
435
         jail_dir, working_dir, binary] + args
 
436
    # Popen doesn't like unicode strings. It hateses them.
 
437
    cmd_line = [(s.encode('utf-8') if isinstance(s, unicode) else s)
 
438
                for s in cmd_line]
 
439
    proc = subprocess.Popen(cmd_line,
 
440
        stdin=subprocess.PIPE, stdout=subprocess.PIPE,
 
441
        stderr=subprocess.PIPE, cwd=tramp_dir, close_fds=True,
 
442
        env={'HOME': os.path.join('/home', user.login),
 
443
             'PATH': PATH,
 
444
             'USER': user.login,
 
445
             'LOGNAME': user.login})
 
446
 
 
447
    (stdout, stderr) = proc.communicate()
 
448
    exitcode = proc.returncode
 
449
 
 
450
    if exitcode != 0:
 
451
        raise ExecutionError('subprocess ended with code %d, stderr: "%s"' %
 
452
                             (exitcode, stderr))
 
453
    return (stdout, stderr)