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

« back to all changes in this revision

Viewing changes to ivle/interpret.py

  • Committer: Nick Chadwick
  • Date: 2009-02-22 06:28:23 UTC
  • mto: (1099.1.180 new-dispatch)
  • mto: This revision was merged to the branch mainline in revision 1100.
  • Revision ID: chadnickbok@gmail.com-20090222062823-civ3uryciiskxnfr
Modified views, so that the list of permissions is now stored on the 
object for easy retrieval.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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(
358
358
    # python-server-page
359
359
}
360
360
 
361
 
def fixup_environ(req):
 
361
def fixup_environ(req, script_path):
362
362
    """Assuming os.environ has been written with the CGI variables from
363
363
    apache, make a few changes for security and correctness.
364
364
 
384
384
        del env['PATH']
385
385
    except: pass
386
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
 
 
396
387
    # CGI specifies that REMOTE_HOST SHOULD be set, and MAY just be set to
397
388
    # REMOTE_ADDR. Since Apache does not appear to set this, set it to
398
389
    # REMOTE_ADDR.
399
390
    if 'REMOTE_HOST' not in env and 'REMOTE_ADDR' in env:
400
391
        env['REMOTE_HOST'] = env['REMOTE_ADDR']
401
392
 
 
393
    env['PATH_INFO'] = ''
 
394
    del env['PATH_TRANSLATED']
 
395
 
 
396
    normuri = os.path.normpath(req.uri)
 
397
    env['SCRIPT_NAME'] = normuri
 
398
 
402
399
    # SCRIPT_NAME is the path to the script WITHOUT PATH_INFO.
403
 
    script_name = req.uri
404
 
    env['SCRIPT_NAME'] = script_name
 
400
    # We don't care about these if the script is null (ie. noop).
 
401
    # XXX: We check for /home because we don't want to interfere with
 
402
    # CGIRequest, which fileservice still uses.
 
403
    if script_path and script_path.startswith('/home'):
 
404
        normscript = os.path.normpath(script_path)
 
405
 
 
406
        uri_into_jail = studpath.url_to_jailpaths(os.path.normpath(req.path))[2]
 
407
 
 
408
        # PATH_INFO is wrong because the script doesn't physically exist.
 
409
        env['PATH_INFO'] = uri_into_jail[len(normscript):]
 
410
        if len(env['PATH_INFO']) > 0:
 
411
            env['SCRIPT_NAME'] = normuri[:-len(env['PATH_INFO'])]
405
412
 
406
413
    # SERVER_SOFTWARE is actually not Apache but IVLE, since we are
407
414
    # custom-making the CGI request.
410
417
    # Additional environment variables
411
418
    username = studpath.url_to_jailpaths(req.path)[0]
412
419
    env['HOME'] = os.path.join('/home', username)
 
420
 
 
421
class ExecutionError(Exception):
 
422
    pass
 
423
 
 
424
def execute_raw(user, jail_dir, working_dir, binary, args):
 
425
    '''Execute a binary in a user's jail, returning the raw output.
 
426
 
 
427
    The binary is executed in the given working directory with the given
 
428
    args. A tuple of (stdout, stderr) is returned.
 
429
    '''
 
430
 
 
431
    tramp = location_cgi_python
 
432
    tramp_dir = os.path.split(location_cgi_python)[0]
 
433
 
 
434
    # Fire up trampoline. Vroom, vroom.
 
435
    proc = subprocess.Popen(
 
436
        [tramp, str(user.unixid), jail_dir, working_dir, binary] + args,
 
437
        stdin=subprocess.PIPE, stdout=subprocess.PIPE,
 
438
        stderr=subprocess.PIPE, cwd=tramp_dir, close_fds=True)
 
439
    exitcode = proc.wait()
 
440
 
 
441
    if exitcode != 0:
 
442
        raise ExecutionError('subprocess ended with code %d, stderr %s' %
 
443
                             (exitcode, proc.stderr.read()))
 
444
    return (proc.stdout.read(), proc.stderr.read())