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

« back to all changes in this revision

Viewing changes to ivle/interpret.py

  • Committer: Matt Giuca
  • Date: 2010-07-28 06:09:00 UTC
  • Revision ID: matt.giuca@gmail.com-20100728060900-6a0lcuexcv1juh5r
ivle/webapp/submit/submit.html: Rewrote error message when an offering could not be found to submit to. This can have one of several causes, and the old error assumed it was because you weren't in a subject dir. Now enumerates the possible reasons. (LP: #526853)

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
 
32
31
import pwd
33
32
import subprocess
34
33
import cgi
 
34
import StringIO
35
35
 
36
36
# TODO: Make progressive output work
37
37
# Question: Will having a large buffer size stop progressive output from
38
38
# working on smaller output
39
39
 
40
40
CGI_BLOCK_SIZE = 65535
41
 
 
42
 
uids = {}
43
 
 
44
 
def get_uid(login):
45
 
    """Get the unix uid corresponding to the given login name.
46
 
       If it is not in the dictionary of uids, then consult the
47
 
       database and retrieve an update of the user table."""
48
 
    global uids
49
 
    if login in uids:
50
 
        return uids[login]
51
 
 
52
 
    conn = db.DB()
53
 
    res = conn.get_all('login', ['login', 'unixid'])
54
 
    def repack(flds):
55
 
        return (flds['login'], flds['unixid'])
56
 
    uids = dict(map(repack,res))
57
 
 
58
 
    return uids[login]
59
 
 
60
 
def interpret_file(req, owner, jail_dir, filename, interpreter, gentle=True):
 
41
PATH = "/usr/local/bin:/usr/bin:/bin"
 
42
 
 
43
def interpret_file(req, owner, jail_dir, filename, interpreter, gentle=True,
 
44
    overrides=None):
61
45
    """Serves a file by interpreting it using one of IVLE's builtin
62
46
    interpreters. All interpreters are intended to run in the user's jail. The
63
47
    jail location is provided as an argument to the interpreter but it is up
64
48
    to the individual interpreters to create the jail.
65
49
 
66
50
    req: An IVLE request object.
67
 
    owner: Username of the user who owns the file being served.
 
51
    owner: The user who owns the file being served.
68
52
    jail_dir: Absolute path to the user's jail.
69
53
    filename: Absolute filename within the user's jail.
70
54
    interpreter: A function object to call.
 
55
    gentle: ?
 
56
    overrides: A dict mapping env var names to strings, to override arbitrary
 
57
        environment variables in the resulting CGI environent.
71
58
    """
72
59
    # We can't test here whether or not the target file actually exists,
73
60
    # because the apache user may not have permission. Instead we have to
79
66
        filename_abs = os.path.join(os.sep, filename)
80
67
        filename_rel = filename
81
68
 
82
 
    # Get the UID of the owner of the file
83
69
    # (Note: files are executed by their owners, not the logged in user.
84
70
    # This ensures users are responsible for their own programs and also
85
71
    # allows them to be executed by the public).
86
 
    uid = get_uid(owner)
87
72
 
88
73
    # Split up req.path again, this time with respect to the jail
89
74
    (working_dir, _) = os.path.split(filename_abs)
94
79
    # (Note that paths "relative" to the jail actually begin with a '/' as
95
80
    # they are absolute in the jailspace)
96
81
 
97
 
    return interpreter(uid, jail_dir, working_dir, filename_abs, req,
98
 
                       gentle)
 
82
    return interpreter(owner, jail_dir, working_dir, filename_abs, req,
 
83
                       gentle, overrides=overrides)
99
84
 
100
85
class CGIFlags:
101
86
    """Stores flags regarding the state of reading CGI output.
109
94
        self.linebuf = ""
110
95
        self.headers = {}       # Header names : values
111
96
 
112
 
def execute_cgi(interpreter, trampoline, uid, jail_dir, working_dir,
113
 
                script_path, req, gentle):
 
97
def execute_cgi(interpreter, owner, jail_dir, working_dir, script_path,
 
98
                req, gentle, overrides=None):
114
99
    """
115
100
    trampoline: Full path on the local system to the CGI wrapper program
116
101
        being executed.
117
 
    uid: User ID of the owner of the file.
 
102
    owner: User object of the owner of the file.
118
103
    jail_dir: Absolute path of owner's jail directory.
119
104
    working_dir: Directory containing the script file relative to owner's
120
105
        jail.
121
106
    script_path: CGI script relative to the owner's jail.
122
107
    req: IVLE request object.
 
108
    gentle: ?
 
109
    overrides: A dict mapping env var names to strings, to override arbitrary
 
110
        environment variables in the resulting CGI environent.
123
111
 
124
112
    The called CGI wrapper application shall be called using popen and receive
125
113
    the HTTP body on stdin. It shall receive the CGI environment variables to
126
114
    its environment.
127
115
    """
128
116
 
 
117
    trampoline = os.path.join(req.config['paths']['lib'], 'trampoline')
 
118
 
129
119
    # Support no-op trampoline runs.
130
120
    if interpreter is None:
131
121
        interpreter = '/bin/true'
148
138
        f.seek(0)       # Rewind, for reading
149
139
 
150
140
    # Set up the environment
151
 
    # This automatically asks mod_python to load up the CGI variables into the
152
 
    # environment (which is a good first approximation)
153
 
    old_env = os.environ.copy()
154
 
    for k in os.environ.keys():
155
 
        del os.environ[k]
156
 
    for (k,v) in req.get_cgi_environ().items():
157
 
        os.environ[k] = v
158
 
    fixup_environ(req)
 
141
    environ = cgi_environ(req, script_path, owner, overrides=overrides)
159
142
 
160
143
    # usage: tramp uid jail_dir working_dir script_path
161
 
    pid = subprocess.Popen(
162
 
        [trampoline, str(uid), jail_dir, working_dir, interpreter,
163
 
        script_path],
 
144
    cmd_line = [trampoline, str(owner.unixid),
 
145
            req.config['paths']['jails']['mounts'],
 
146
            req.config['paths']['jails']['src'],
 
147
            req.config['paths']['jails']['template'],
 
148
            jail_dir, working_dir, interpreter, script_path]
 
149
    # Popen doesn't like unicode strings. It hateses them.
 
150
    cmd_line = [(s.encode('utf-8') if isinstance(s, unicode) else s)
 
151
                for s in cmd_line]
 
152
    pid = subprocess.Popen(cmd_line,
164
153
        stdin=f, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
165
 
        cwd=tramp_dir)
166
 
 
167
 
    # Restore the environment
168
 
    for k in os.environ.keys():
169
 
        del os.environ[k]
170
 
    for (k,v) in old_env.items():
171
 
        os.environ[k] = v
 
154
        cwd=tramp_dir, env=environ)
172
155
 
173
156
    # We don't want any output! Bail out after the process terminates.
174
157
    if noop:
240
223
            if len(split) == 1:
241
224
                split = headers.split('\n', 1)
242
225
 
243
 
        # Is this an internal IVLE error condition?
244
 
        hs = cgiflags.headers
245
 
        if 'X-IVLE-Error-Type' in hs:
246
 
            t = hs['X-IVLE-Error-Type']
247
 
            if t == IVLEError.__name__:
248
 
                raise IVLEError(int(hs['X-IVLE-Error-Code']),
249
 
                                hs['X-IVLE-Error-Message'])
250
 
            else:
 
226
        # If not executing in gentle mode (which presents CGI violations
 
227
        # to users nicely), check if this an internal IVLE error
 
228
        # condition.
 
229
        if not cgiflags.gentle:
 
230
            hs = cgiflags.headers
 
231
            if 'X-IVLE-Error-Type' in hs:
251
232
                try:
252
233
                    raise IVLEJailError(hs['X-IVLE-Error-Type'],
253
234
                                        hs['X-IVLE-Error-Message'],
254
235
                                        hs['X-IVLE-Error-Info'])
255
236
                except KeyError:
256
 
                    raise IVLEError(500, 'bad error headers written by CGI')
 
237
                    raise AssertionError("Bad error headers written by CGI.")
257
238
 
258
239
        # Check to make sure the required headers were written
259
240
        if cgiflags.wrote_html_warning or not cgiflags.gentle:
313
294
        process_cgi_output(req, line + '\n', cgiflags)
314
295
        return
315
296
 
 
297
    # Check if CGI field-name is valid
 
298
    CGI_SEPERATORS = set(['(', ')', '<', '>', '@', ',', ';', ':', '\\', '"',
 
299
            '/', '[', ']', '?', '=', '{', '}', ' ', '\t'])
 
300
    if any((char in CGI_SEPERATORS for char in name)):
 
301
        warning = "Warning"
 
302
        if not cgiflags.gentle:
 
303
            message = """An unexpected server error has occured."""
 
304
            warning = "Error"
 
305
        else:
 
306
            # Header contained illegal characters
 
307
            message = """You printed an invalid CGI header. CGI header
 
308
            field-names can not contain any of the following characters: 
 
309
            <code>( ) &lt; &gt; @ , ; : \\ " / [ ] ? = { } <em>SPACE 
 
310
            TAB</em></code>."""
 
311
        write_html_warning(req, message, warning=warning)
 
312
        cgiflags.wrote_html_warning = True
 
313
        # Handle the rest of this line as normal data
 
314
        process_cgi_output(req, line + '\n', cgiflags)
 
315
        return
 
316
 
316
317
    # Read CGI headers
317
318
    value = value.strip()
318
319
    if name == "Content-Type":
362
363
    <pre>
363
364
""" % (warning, text))
364
365
 
365
 
location_cgi_python = os.path.join(ivle.conf.lib_path, "trampoline")
366
 
 
367
366
# Mapping of interpreter names (as given in conf/app/server.py) to
368
367
# interpreter functions.
369
368
 
370
369
interpreter_objects = {
371
370
    'cgi-python'
372
 
        : functools.partial(execute_cgi, "/usr/bin/python",
373
 
            location_cgi_python),
 
371
        : functools.partial(execute_cgi, "/usr/bin/python"),
374
372
    'noop'
375
 
        : functools.partial(execute_cgi, None,
376
 
            location_cgi_python),
 
373
        : functools.partial(execute_cgi, None),
377
374
    # Should also have:
378
375
    # cgi-generic
379
376
    # python-server-page
380
377
}
381
378
 
382
 
def fixup_environ(req):
383
 
    """Assuming os.environ has been written with the CGI variables from
384
 
    apache, make a few changes for security and correctness.
 
379
def cgi_environ(req, script_path, user, overrides=None):
 
380
    """Gets CGI variables from apache and makes a few changes for security and 
 
381
    correctness.
385
382
 
386
383
    Does not modify req, only reads it.
 
384
 
 
385
    overrides: A dict mapping env var names to strings, to override arbitrary
 
386
        environment variables in the resulting CGI environent.
387
387
    """
388
 
    env = os.environ
 
388
    env = {}
389
389
    # Comments here are on the heavy side, explained carefully for security
390
390
    # reasons. Please read carefully before making changes.
 
391
    
 
392
    # This automatically asks mod_python to load up the CGI variables into the
 
393
    # environment (which is a good first approximation)
 
394
    for (k,v) in req.get_cgi_environ().items():
 
395
        env[k] = v
391
396
 
392
397
    # Remove DOCUMENT_ROOT and SCRIPT_FILENAME. Not part of CGI spec and
393
398
    # exposes unnecessary details about server.
405
410
        del env['PATH']
406
411
    except: pass
407
412
 
408
 
    # Remove SCRIPT_FILENAME. Not part of CGI spec (see SCRIPT_NAME).
409
 
 
410
 
    # PATH_INFO is wrong because the script doesn't physically exist.
411
 
    # Apache makes it relative to the "serve" app. It should actually be made
412
 
    # relative to the student's script. intepretservice does that in the jail,
413
 
    # so here we just clear it.
414
 
    env['PATH_INFO'] = ''
415
 
    env['PATH_TRANSLATED'] = ''
416
 
 
417
413
    # CGI specifies that REMOTE_HOST SHOULD be set, and MAY just be set to
418
414
    # REMOTE_ADDR. Since Apache does not appear to set this, set it to
419
415
    # REMOTE_ADDR.
420
416
    if 'REMOTE_HOST' not in env and 'REMOTE_ADDR' in env:
421
417
        env['REMOTE_HOST'] = env['REMOTE_ADDR']
422
418
 
 
419
    env['PATH_INFO'] = ''
 
420
    del env['PATH_TRANSLATED']
 
421
 
 
422
    normuri = os.path.normpath(req.uri)
 
423
    env['SCRIPT_NAME'] = normuri
 
424
 
423
425
    # SCRIPT_NAME is the path to the script WITHOUT PATH_INFO.
424
 
    script_name = req.uri
425
 
    env['SCRIPT_NAME'] = script_name
 
426
    # We don't care about these if the script is null (ie. noop).
 
427
    # XXX: We check for /home because we don't want to interfere with
 
428
    # CGIRequest, which fileservice still uses.
 
429
    if script_path and script_path.startswith('/home'):
 
430
        normscript = os.path.normpath(script_path)
 
431
 
 
432
        uri_into_jail = studpath.to_home_path(os.path.normpath(req.path))
 
433
 
 
434
        # PATH_INFO is wrong because the script doesn't physically exist.
 
435
        env['PATH_INFO'] = uri_into_jail[len(normscript):]
 
436
        if len(env['PATH_INFO']) > 0:
 
437
            env['SCRIPT_NAME'] = normuri[:-len(env['PATH_INFO'])]
426
438
 
427
439
    # SERVER_SOFTWARE is actually not Apache but IVLE, since we are
428
440
    # custom-making the CGI request.
429
 
    env['SERVER_SOFTWARE'] = "IVLE/" + str(ivle.conf.ivle_version)
 
441
    env['SERVER_SOFTWARE'] = "IVLE/" + ivle.__version__
430
442
 
431
443
    # Additional environment variables
432
 
    username = studpath.url_to_jailpaths(req.path)[0]
 
444
    username = user.login
433
445
    env['HOME'] = os.path.join('/home', username)
 
446
 
 
447
    if overrides is not None:
 
448
        env.update(overrides)
 
449
    return env
 
450
 
 
451
class ExecutionError(Exception):
 
452
    pass
 
453
 
 
454
def execute_raw(config, user, jail_dir, working_dir, binary, args):
 
455
    '''Execute a binary in a user's jail, returning the raw output.
 
456
 
 
457
    The binary is executed in the given working directory with the given
 
458
    args. A tuple of (stdout, stderr) is returned.
 
459
    '''
 
460
 
 
461
    tramp = os.path.join(config['paths']['lib'], 'trampoline')
 
462
    tramp_dir = os.path.split(tramp)[0]
 
463
 
 
464
    # Fire up trampoline. Vroom, vroom.
 
465
    cmd_line = [tramp, str(user.unixid), config['paths']['jails']['mounts'],
 
466
         config['paths']['jails']['src'],
 
467
         config['paths']['jails']['template'],
 
468
         jail_dir, working_dir, binary] + args
 
469
    # Popen doesn't like unicode strings. It hateses them.
 
470
    cmd_line = [(s.encode('utf-8') if isinstance(s, unicode) else s)
 
471
                for s in cmd_line]
 
472
    proc = subprocess.Popen(cmd_line,
 
473
        stdin=subprocess.PIPE, stdout=subprocess.PIPE,
 
474
        stderr=subprocess.PIPE, cwd=tramp_dir, close_fds=True,
 
475
        env={'HOME': os.path.join('/home', user.login),
 
476
             'PATH': PATH,
 
477
             'USER': user.login,
 
478
             'LOGNAME': user.login})
 
479
 
 
480
    (stdout, stderr) = proc.communicate()
 
481
    exitcode = proc.returncode
 
482
 
 
483
    if exitcode != 0:
 
484
        raise ExecutionError('subprocess ended with code %d, stderr: "%s"' %
 
485
                             (exitcode, stderr))
 
486
    return (stdout, stderr)
 
487
 
 
488
def jail_call(req, cgi_script, script_name, query_string=None,
 
489
    request_method="GET", extra_overrides=None):
 
490
    """
 
491
    Makes a call to a CGI script inside the jail from outside the jail.
 
492
    This can be used to allow Python scripts to access jail-only functions and
 
493
    data without having to perform a full API request.
 
494
 
 
495
    req: A Request object (will not be written to or attributes modified).
 
496
    cgi_script: Path to cgi script outside of jail.
 
497
        eg: os.path.join(req.config['paths']['share'],
 
498
                         'services/fileservice')
 
499
    script_name: Name to set as SCRIPT_NAME for the CGI environment.
 
500
        eg: "/fileservice/"
 
501
    query_string: Query string to set as QUERY_STRING for the CGI environment.
 
502
        eg: "action=svnrepostat&path=/users/studenta/"
 
503
    request_method: Method to set as REQUEST_METHOD for the CGI environment.
 
504
        eg: "POST". Defaults to "GET".
 
505
    extra_overrides: A dict mapping env var names to strings, to override
 
506
        arbitrary environment variables in the resulting CGI environent.
 
507
 
 
508
    Returns a triple (status_code, content_type, contents).
 
509
    """
 
510
    interp_object = interpreter_objects["cgi-python"]
 
511
    user_jail_dir = os.path.join(req.config['paths']['jails']['mounts'],
 
512
                                 req.user.login)
 
513
    overrides = {
 
514
        "SCRIPT_NAME": script_name,
 
515
        "QUERY_STRING": query_string,
 
516
        "REQUEST_URI": "%s%s%s" % (script_name, "?" if query_string else "",
 
517
                                   query_string),
 
518
        "REQUEST_METHOD": request_method,
 
519
    }
 
520
    if extra_overrides is not None:
 
521
        overrides.update(extra_overrides)
 
522
    result = DummyReq(req)
 
523
    interpret_file(result, req.user, user_jail_dir, cgi_script, interp_object,
 
524
                   gentle=False, overrides=overrides)
 
525
    return result.status, result.content_type, result.getvalue()
 
526
 
 
527
class DummyReq(StringIO.StringIO):
 
528
    """A dummy request object, built from a real request object, which can be
 
529
    used like a req but doesn't mutate the existing request.
 
530
    (Used for reading CGI responses as strings rather than forwarding their
 
531
    output to the current request.)
 
532
    """
 
533
    def __init__(self, req):
 
534
        StringIO.StringIO.__init__(self)
 
535
        self._real_req = req
 
536
    def get_cgi_environ(self):
 
537
        return self._real_req.get_cgi_environ()
 
538
    def __getattr__(self, name):
 
539
        return getattr(self._real_req, name)