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

« back to all changes in this revision

Viewing changes to ivle/interpret.py

Move the remaining images to the new framework, in the new ivle.webapp.core
plugin.

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