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

« back to all changes in this revision

Viewing changes to ivle/interpret.py

  • Committer: William Grant
  • Date: 2010-02-26 13:27:55 UTC
  • Revision ID: grantw@unimelb.edu.au-20100226132755-6aya9set3q8nkgyv
Tags: 1.0
Bump version to 1.0. This is it.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
import ivle
25
25
from ivle import studpath
26
 
from ivle.util import IVLEJailError, split_path
 
26
from ivle.util import IVLEError, IVLEJailError, split_path
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
130
129
        f.seek(0)       # Rewind, for reading
131
130
 
132
131
    # Set up the environment
133
 
    environ = cgi_environ(req, script_path, owner)
 
132
    # This automatically asks mod_python to load up the CGI variables into the
 
133
    # environment (which is a good first approximation)
 
134
    old_env = os.environ.copy()
 
135
    for k in os.environ.keys():
 
136
        del os.environ[k]
 
137
    for (k,v) in req.get_cgi_environ().items():
 
138
        os.environ[k] = v
 
139
    fixup_environ(req, script_path, owner)
134
140
 
135
141
    # usage: tramp uid jail_dir working_dir script_path
136
142
    cmd_line = [trampoline, str(owner.unixid),
143
149
                for s in cmd_line]
144
150
    pid = subprocess.Popen(cmd_line,
145
151
        stdin=f, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
146
 
        cwd=tramp_dir, env=environ)
 
152
        cwd=tramp_dir)
 
153
 
 
154
    # Restore the environment
 
155
    for k in os.environ.keys():
 
156
        del os.environ[k]
 
157
    for (k,v) in old_env.items():
 
158
        os.environ[k] = v
147
159
 
148
160
    # We don't want any output! Bail out after the process terminates.
149
161
    if noop:
215
227
            if len(split) == 1:
216
228
                split = headers.split('\n', 1)
217
229
 
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:
 
230
        # Is this an internal IVLE error condition?
 
231
        hs = cgiflags.headers
 
232
        if 'X-IVLE-Error-Type' in hs:
 
233
            t = hs['X-IVLE-Error-Type']
 
234
            if t == IVLEError.__name__:
 
235
                raise IVLEError(int(hs['X-IVLE-Error-Code']),
 
236
                                hs['X-IVLE-Error-Message'])
 
237
            else:
224
238
                try:
225
239
                    raise IVLEJailError(hs['X-IVLE-Error-Type'],
226
240
                                        hs['X-IVLE-Error-Message'],
227
241
                                        hs['X-IVLE-Error-Info'])
228
242
                except KeyError:
229
 
                    raise AssertionError("Bad error headers written by CGI.")
 
243
                    raise IVLEError(500, 'bad error headers written by CGI')
230
244
 
231
245
        # Check to make sure the required headers were written
232
246
        if cgiflags.wrote_html_warning or not cgiflags.gentle:
348
362
    # python-server-page
349
363
}
350
364
 
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
def fixup_environ(req, script_path, user):
 
366
    """Assuming os.environ has been written with the CGI variables from
 
367
    apache, make a few changes for security and correctness.
354
368
 
355
369
    Does not modify req, only reads it.
356
370
    """
357
 
    env = {}
 
371
    env = os.environ
358
372
    # Comments here are on the heavy side, explained carefully for security
359
373
    # 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
365
374
 
366
375
    # Remove DOCUMENT_ROOT and SCRIPT_FILENAME. Not part of CGI spec and
367
376
    # exposes unnecessary details about server.
413
422
    username = user.login
414
423
    env['HOME'] = os.path.join('/home', username)
415
424
 
416
 
    return env
417
 
 
418
425
class ExecutionError(Exception):
419
426
    pass
420
427
 
440
447
        stdin=subprocess.PIPE, stdout=subprocess.PIPE,
441
448
        stderr=subprocess.PIPE, cwd=tramp_dir, close_fds=True,
442
449
        env={'HOME': os.path.join('/home', user.login),
443
 
             'PATH': PATH,
 
450
             'PATH': os.environ['PATH'],
444
451
             'USER': user.login,
445
452
             'LOGNAME': user.login})
446
453