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

« back to all changes in this revision

Viewing changes to ivle/interpret.py

  • Committer: William Grant
  • Date: 2009-06-29 01:55:55 UTC
  • mto: This revision was merged to the branch mainline in revision 1322.
  • Revision ID: grantw@unimelb.edu.au-20090629015555-49xxv0piiieunx8e
Add docs on configuring Apache.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
# Runs a student script in a safe execution environment.
23
23
 
24
24
from ivle import studpath
25
 
from ivle.util import IVLEError, IVLEJailError
 
25
from ivle.util import IVLEError, IVLEJailError, split_path
26
26
import ivle.conf
27
27
 
28
28
import functools
134
134
        del os.environ[k]
135
135
    for (k,v) in req.get_cgi_environ().items():
136
136
        os.environ[k] = v
137
 
    fixup_environ(req)
 
137
    fixup_environ(req, script_path)
138
138
 
139
139
    # usage: tramp uid jail_dir working_dir script_path
140
140
    pid = subprocess.Popen(
141
 
        [trampoline, str(uid), jail_dir, working_dir, interpreter,
 
141
        [trampoline, str(uid), ivle.conf.jail_base, ivle.conf.jail_src_base,
 
142
         ivle.conf.jail_system, jail_dir, working_dir, interpreter,
142
143
        script_path],
143
144
        stdin=f, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
144
145
        cwd=tramp_dir)
358
359
    # python-server-page
359
360
}
360
361
 
361
 
def fixup_environ(req):
 
362
def fixup_environ(req, script_path):
362
363
    """Assuming os.environ has been written with the CGI variables from
363
364
    apache, make a few changes for security and correctness.
364
365
 
384
385
        del env['PATH']
385
386
    except: pass
386
387
 
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
 
 
396
388
    # CGI specifies that REMOTE_HOST SHOULD be set, and MAY just be set to
397
389
    # REMOTE_ADDR. Since Apache does not appear to set this, set it to
398
390
    # REMOTE_ADDR.
399
391
    if 'REMOTE_HOST' not in env and 'REMOTE_ADDR' in env:
400
392
        env['REMOTE_HOST'] = env['REMOTE_ADDR']
401
393
 
 
394
    env['PATH_INFO'] = ''
 
395
    del env['PATH_TRANSLATED']
 
396
 
 
397
    normuri = os.path.normpath(req.uri)
 
398
    env['SCRIPT_NAME'] = normuri
 
399
 
402
400
    # SCRIPT_NAME is the path to the script WITHOUT PATH_INFO.
403
 
    script_name = req.uri
404
 
    env['SCRIPT_NAME'] = script_name
 
401
    # We don't care about these if the script is null (ie. noop).
 
402
    # XXX: We check for /home because we don't want to interfere with
 
403
    # CGIRequest, which fileservice still uses.
 
404
    if script_path and script_path.startswith('/home'):
 
405
        normscript = os.path.normpath(script_path)
 
406
 
 
407
        uri_into_jail = studpath.to_home_path(os.path.normpath(req.path))
 
408
 
 
409
        # PATH_INFO is wrong because the script doesn't physically exist.
 
410
        env['PATH_INFO'] = uri_into_jail[len(normscript):]
 
411
        if len(env['PATH_INFO']) > 0:
 
412
            env['SCRIPT_NAME'] = normuri[:-len(env['PATH_INFO'])]
405
413
 
406
414
    # SERVER_SOFTWARE is actually not Apache but IVLE, since we are
407
415
    # custom-making the CGI request.
408
416
    env['SERVER_SOFTWARE'] = "IVLE/" + str(ivle.conf.ivle_version)
409
417
 
410
418
    # Additional environment variables
411
 
    username = studpath.url_to_jailpaths(req.path)[0]
 
419
    username = split_path(req.path)[0]
412
420
    env['HOME'] = os.path.join('/home', username)
413
421
 
414
422
class ExecutionError(Exception):
426
434
 
427
435
    # Fire up trampoline. Vroom, vroom.
428
436
    proc = subprocess.Popen(
429
 
        [tramp, str(user.unixid), jail_dir, working_dir, binary] + args,
 
437
        [tramp, str(user.unixid), ivle.conf.jail_base,
 
438
         ivle.conf.jail_src_base, ivle.conf.jail_system, jail_dir,
 
439
         working_dir, binary] + args,
430
440
        stdin=subprocess.PIPE, stdout=subprocess.PIPE,
431
441
        stderr=subprocess.PIPE, cwd=tramp_dir, close_fds=True)
432
 
    exitcode = proc.wait()
 
442
 
 
443
    (stdout, stderr) = proc.communicate()
 
444
    exitcode = proc.returncode
433
445
 
434
446
    if exitcode != 0:
435
447
        raise ExecutionError('subprocess ended with code %d, stderr %s' %
436
448
                             (exitcode, proc.stderr.read()))
437
 
    return (proc.stdout.read(), proc.stderr.read())
 
449
    return (stdout, stderr)