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

« back to all changes in this revision

Viewing changes to ivle/interpret.py

  • Committer: David Coles
  • Date: 2009-11-27 05:34:33 UTC
  • mto: This revision was merged to the branch mainline in revision 1322.
  • Revision ID: coles.david@gmail.com-20091127053433-8ki9nm6xrkogxq67
Added diagram of system architecture

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
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,
 
91
def execute_cgi(interpreter, uid, jail_dir, working_dir, script_path,
93
92
                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.
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)
134
140
 
135
141
    # 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,
 
142
    pid = subprocess.Popen(
 
143
        [trampoline, str(uid), req.config['paths']['jails']['mounts'],
 
144
         req.config['paths']['jails']['src'],
 
145
         req.config['paths']['jails']['template'],
 
146
         jail_dir, working_dir, interpreter, script_path],
145
147
        stdin=f, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
146
 
        cwd=tramp_dir, env=environ)
 
148
        cwd=tramp_dir)
 
149
 
 
150
    # Restore the environment
 
151
    for k in os.environ.keys():
 
152
        del os.environ[k]
 
153
    for (k,v) in old_env.items():
 
154
        os.environ[k] = v
147
155
 
148
156
    # We don't want any output! Bail out after the process terminates.
149
157
    if noop:
215
223
            if len(split) == 1:
216
224
                split = headers.split('\n', 1)
217
225
 
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:
 
226
        # Is this an internal IVLE error condition?
 
227
        hs = cgiflags.headers
 
228
        if 'X-IVLE-Error-Type' in hs:
 
229
            t = hs['X-IVLE-Error-Type']
 
230
            if t == IVLEError.__name__:
 
231
                raise IVLEError(int(hs['X-IVLE-Error-Code']),
 
232
                                hs['X-IVLE-Error-Message'])
 
233
            else:
224
234
                try:
225
235
                    raise IVLEJailError(hs['X-IVLE-Error-Type'],
226
236
                                        hs['X-IVLE-Error-Message'],
227
237
                                        hs['X-IVLE-Error-Info'])
228
238
                except KeyError:
229
 
                    raise AssertionError("Bad error headers written by CGI.")
 
239
                    raise IVLEError(500, 'bad error headers written by CGI')
230
240
 
231
241
        # Check to make sure the required headers were written
232
242
        if cgiflags.wrote_html_warning or not cgiflags.gentle:
348
358
    # python-server-page
349
359
}
350
360
 
351
 
def cgi_environ(req, script_path, user):
352
 
    """Gets CGI variables from apache and makes a few changes for security and 
353
 
    correctness.
 
361
def fixup_environ(req, script_path):
 
362
    """Assuming os.environ has been written with the CGI variables from
 
363
    apache, make a few changes for security and correctness.
354
364
 
355
365
    Does not modify req, only reads it.
356
366
    """
357
 
    env = {}
 
367
    env = os.environ
358
368
    # Comments here are on the heavy side, explained carefully for security
359
369
    # 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
370
 
366
371
    # Remove DOCUMENT_ROOT and SCRIPT_FILENAME. Not part of CGI spec and
367
372
    # exposes unnecessary details about server.
410
415
    env['SERVER_SOFTWARE'] = "IVLE/" + ivle.__version__
411
416
 
412
417
    # Additional environment variables
413
 
    username = user.login
 
418
    username = split_path(req.path)[0]
414
419
    env['HOME'] = os.path.join('/home', username)
415
420
 
416
 
    return env
417
 
 
418
421
class ExecutionError(Exception):
419
422
    pass
420
423
 
429
432
    tramp_dir = os.path.split(tramp)[0]
430
433
 
431
434
    # Fire up trampoline. Vroom, vroom.
432
 
    cmd_line = [tramp, str(user.unixid), config['paths']['jails']['mounts'],
 
435
    proc = subprocess.Popen(
 
436
        [tramp, str(user.unixid), config['paths']['jails']['mounts'],
433
437
         config['paths']['jails']['src'],
434
438
         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,
 
439
         jail_dir, working_dir, binary] + args,
440
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})
 
441
        stderr=subprocess.PIPE, cwd=tramp_dir, close_fds=True)
446
442
 
447
443
    (stdout, stderr) = proc.communicate()
448
444
    exitcode = proc.returncode
449
445
 
450
446
    if exitcode != 0:
451
 
        raise ExecutionError('subprocess ended with code %d, stderr: "%s"' %
452
 
                             (exitcode, stderr))
 
447
        raise ExecutionError('subprocess ended with code %d, stderr %s' %
 
448
                             (exitcode, proc.stderr.read()))
453
449
    return (stdout, stderr)