~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-20 05:13:13 UTC
  • Revision ID: matt.giuca@gmail.com-20100720051313-phpr6n47fewmhnbj
ivle/interpret: execute_cgi and interpret_file now take an 'overrides' argument, which is a dict containing env vars to override in the CGI request.

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
CGI_BLOCK_SIZE = 65535
40
40
PATH = "/usr/local/bin:/usr/bin:/bin"
41
41
 
42
 
def interpret_file(req, owner, jail_dir, filename, interpreter, gentle=True):
 
42
def interpret_file(req, owner, jail_dir, filename, interpreter, gentle=True,
 
43
    overrides=None):
43
44
    """Serves a file by interpreting it using one of IVLE's builtin
44
45
    interpreters. All interpreters are intended to run in the user's jail. The
45
46
    jail location is provided as an argument to the interpreter but it is up
50
51
    jail_dir: Absolute path to the user's jail.
51
52
    filename: Absolute filename within the user's jail.
52
53
    interpreter: A function object to call.
 
54
    gentle: ?
 
55
    overrides: A dict mapping env var names to strings, to override arbitrary
 
56
        environment variables in the resulting CGI environent.
53
57
    """
54
58
    # We can't test here whether or not the target file actually exists,
55
59
    # because the apache user may not have permission. Instead we have to
75
79
    # they are absolute in the jailspace)
76
80
 
77
81
    return interpreter(owner, jail_dir, working_dir, filename_abs, req,
78
 
                       gentle)
 
82
                       gentle, overrides=overrides)
79
83
 
80
84
class CGIFlags:
81
85
    """Stores flags regarding the state of reading CGI output.
90
94
        self.headers = {}       # Header names : values
91
95
 
92
96
def execute_cgi(interpreter, owner, jail_dir, working_dir, script_path,
93
 
                req, gentle):
 
97
                req, gentle, overrides=None):
94
98
    """
95
99
    trampoline: Full path on the local system to the CGI wrapper program
96
100
        being executed.
100
104
        jail.
101
105
    script_path: CGI script relative to the owner's jail.
102
106
    req: IVLE request object.
 
107
    gentle: ?
 
108
    overrides: A dict mapping env var names to strings, to override arbitrary
 
109
        environment variables in the resulting CGI environent.
103
110
 
104
111
    The called CGI wrapper application shall be called using popen and receive
105
112
    the HTTP body on stdin. It shall receive the CGI environment variables to
130
137
        f.seek(0)       # Rewind, for reading
131
138
 
132
139
    # Set up the environment
133
 
    environ = cgi_environ(req, script_path, owner)
 
140
    environ = cgi_environ(req, script_path, owner, overrides=overrides)
134
141
 
135
142
    # usage: tramp uid jail_dir working_dir script_path
136
143
    cmd_line = [trampoline, str(owner.unixid),
368
375
    # python-server-page
369
376
}
370
377
 
371
 
def cgi_environ(req, script_path, user):
 
378
def cgi_environ(req, script_path, user, overrides=None):
372
379
    """Gets CGI variables from apache and makes a few changes for security and 
373
380
    correctness.
374
381
 
375
382
    Does not modify req, only reads it.
 
383
 
 
384
    overrides: A dict mapping env var names to strings, to override arbitrary
 
385
        environment variables in the resulting CGI environent.
376
386
    """
377
387
    env = {}
378
388
    # Comments here are on the heavy side, explained carefully for security
433
443
    username = user.login
434
444
    env['HOME'] = os.path.join('/home', username)
435
445
 
 
446
    if overrides is not None:
 
447
        env.update(overrides)
436
448
    return env
437
449
 
438
450
class ExecutionError(Exception):