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

« back to all changes in this revision

Viewing changes to lib/common/interpret.py

  • Committer: mattgiuca
  • Date: 2008-07-21 05:25:48 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:920
userdb: Added to table "offering" fields max_students_per_group and
    max_groups_per_student.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# IVLE
 
2
# Copyright (C) 2007-2008 The University of Melbourne
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
17
 
 
18
# Module: Interpret
 
19
# Author: Matt Giuca
 
20
# Date: 18/1/2008
 
21
 
 
22
# Runs a student script in a safe execution environment.
 
23
#
 
24
# NOTE: This script currently disables cookies. This means students will be
 
25
# unable to write session-based or stateful web applications. This is done for
 
26
# security reasons (we do not want the students to see the IVLE cookie of
 
27
# whoever is visiting their pages).
 
28
# This can be resolved but needs careful sanitisation. See fixup_environ.
 
29
 
 
30
from common import studpath
 
31
from common import db
 
32
from common.util import IVLEError, IVLEJailError
 
33
import conf
 
34
import functools
 
35
 
 
36
import os
 
37
import pwd
 
38
import subprocess
 
39
import cgi
 
40
 
 
41
# TODO: Make progressive output work
 
42
# Question: Will having a large buffer size stop progressive output from
 
43
# working on smaller output
 
44
 
 
45
CGI_BLOCK_SIZE = 65535
 
46
 
 
47
uids = {}
 
48
 
 
49
def get_uid(login):
 
50
    """Get the unix uid corresponding to the given login name.
 
51
       If it is not in the dictionary of uids, then consult the
 
52
       database and retrieve an update of the user table."""
 
53
    global uids
 
54
    if login in uids:
 
55
        return uids[login]
 
56
 
 
57
    conn = db.DB()
 
58
    res = conn.get_all('login', ['login', 'unixid'])
 
59
    def repack(flds):
 
60
        return (flds['login'], flds['unixid'])
 
61
    uids = dict(map(repack,res))
 
62
 
 
63
    return uids[login]
 
64
 
 
65
def interpret_file(req, owner, jail_dir, filename, interpreter, gentle=True):
 
66
    """Serves a file by interpreting it using one of IVLE's builtin
 
67
    interpreters. All interpreters are intended to run in the user's jail. The
 
68
    jail location is provided as an argument to the interpreter but it is up
 
69
    to the individual interpreters to create the jail.
 
70
 
 
71
    req: An IVLE request object.
 
72
    owner: Username of the user who owns the file being served.
 
73
    jail_dir: Absolute path to the user's jail.
 
74
    filename: Absolute filename within the user's jail.
 
75
    interpreter: A function object to call.
 
76
    """
 
77
    # We can't test here whether or not the target file actually exists,
 
78
    # because the apache user may not have permission. Instead we have to
 
79
    # rely on the interpreter generating an error.
 
80
    if filename.startswith(os.sep):
 
81
        filename_abs = filename
 
82
        filename_rel = filename[1:]
 
83
    else:
 
84
        filename_abs = os.path.join(os.sep, filename)
 
85
        filename_rel = filename
 
86
 
 
87
    # Get the UID of the owner of the file
 
88
    # (Note: files are executed by their owners, not the logged in user.
 
89
    # This ensures users are responsible for their own programs and also
 
90
    # allows them to be executed by the public).
 
91
    uid = get_uid(owner)
 
92
 
 
93
    # Split up req.path again, this time with respect to the jail
 
94
    (working_dir, _) = os.path.split(filename_abs)
 
95
    # jail_dir is the absolute jail directory.
 
96
    # path is the filename relative to the user's jail.
 
97
    # working_dir is the directory containing the file relative to the user's
 
98
    # jail.
 
99
    # (Note that paths "relative" to the jail actually begin with a '/' as
 
100
    # they are absolute in the jailspace)
 
101
 
 
102
    return interpreter(uid, jail_dir, working_dir, filename_abs, req,
 
103
                       gentle)
 
104
 
 
105
class CGIFlags:
 
106
    """Stores flags regarding the state of reading CGI output.
 
107
       If this is to be gentle, detection of invalid headers will result in an
 
108
       HTML warning."""
 
109
    def __init__(self, begentle=True):
 
110
        self.gentle = begentle
 
111
        self.started_cgi_body = False
 
112
        self.got_cgi_headers = False
 
113
        self.wrote_html_warning = False
 
114
        self.linebuf = ""
 
115
        self.headers = {}       # Header names : values
 
116
 
 
117
def execute_cgi(interpreter, trampoline, uid, jail_dir, working_dir,
 
118
                script_path, req, gentle):
 
119
    """
 
120
    trampoline: Full path on the local system to the CGI wrapper program
 
121
        being executed.
 
122
    uid: User ID of the owner of the file.
 
123
    jail_dir: Absolute path of owner's jail directory.
 
124
    working_dir: Directory containing the script file relative to owner's
 
125
        jail.
 
126
    script_path: CGI script relative to the owner's jail.
 
127
    req: IVLE request object.
 
128
 
 
129
    The called CGI wrapper application shall be called using popen and receive
 
130
    the HTTP body on stdin. It shall receive the CGI environment variables to
 
131
    its environment.
 
132
    """
 
133
 
 
134
    # Get the student program's directory and execute it from that context.
 
135
    (tramp_dir, _) = os.path.split(trampoline)
 
136
 
 
137
    # TODO: Don't create a file if the body length is known to be 0
 
138
    # Write the HTTP body to a temporary file so it can be passed as a *real*
 
139
    # file to popen.
 
140
    f = os.tmpfile()
 
141
    body = req.read()
 
142
    if body is not None:
 
143
        f.write(body)
 
144
        f.flush()
 
145
        f.seek(0)       # Rewind, for reading
 
146
 
 
147
    # Set up the environment
 
148
    # This automatically asks mod_python to load up the CGI variables into the
 
149
    # environment (which is a good first approximation)
 
150
    old_env = os.environ.copy()
 
151
    for k in os.environ.keys():
 
152
        del os.environ[k]
 
153
    for (k,v) in req.get_cgi_environ().items():
 
154
        os.environ[k] = v
 
155
    fixup_environ(req)
 
156
 
 
157
    # usage: tramp uid jail_dir working_dir script_path
 
158
    pid = subprocess.Popen(
 
159
        [trampoline, str(uid), jail_dir, working_dir, interpreter,
 
160
        script_path],
 
161
        stdin=f, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
 
162
        cwd=tramp_dir)
 
163
 
 
164
    # Restore the environment
 
165
    for k in os.environ.keys():
 
166
        del os.environ[k]
 
167
    for (k,v) in old_env.items():
 
168
        os.environ[k] = v
 
169
 
 
170
    # process_cgi_line: Reads a single line of CGI output and processes it.
 
171
    # Prints to req, and also does fancy HTML warnings if Content-Type
 
172
    # omitted.
 
173
    cgiflags = CGIFlags(gentle)
 
174
 
 
175
    # Read from the process's stdout into req
 
176
    data = pid.stdout.read(CGI_BLOCK_SIZE)
 
177
    while len(data) > 0:
 
178
        process_cgi_output(req, data, cgiflags)
 
179
        data = pid.stdout.read(CGI_BLOCK_SIZE)
 
180
 
 
181
    # If we haven't processed headers yet, now is a good time
 
182
    if not cgiflags.started_cgi_body:
 
183
        process_cgi_output(req, '\n', cgiflags)
 
184
 
 
185
    # If we wrote an HTML warning header, write the footer
 
186
    if cgiflags.wrote_html_warning:
 
187
        req.write("""</pre>
 
188
  </div>
 
189
</body>
 
190
</html>""")
 
191
 
 
192
def process_cgi_output(req, data, cgiflags):
 
193
    """Processes a chunk of CGI output. data is a string of arbitrary length;
 
194
    some arbitrary chunk of output written by the CGI script."""
 
195
    if cgiflags.started_cgi_body:
 
196
        if cgiflags.wrote_html_warning:
 
197
            # HTML escape text if wrote_html_warning
 
198
            req.write(cgi.escape(data))
 
199
        else:
 
200
            req.write(data)
 
201
    else:
 
202
        # Break data into lines of CGI header data. 
 
203
        linebuf = cgiflags.linebuf + data
 
204
        # First see if we can split all header data
 
205
        # We need to get the double CRLF- or LF-terminated headers, whichever
 
206
        # is smaller, as either sequence may appear somewhere in the body.
 
207
        usplit = linebuf.split('\n\n', 1)
 
208
        wsplit = linebuf.split('\r\n\r\n', 1)
 
209
        split = len(usplit[0]) > len(wsplit[0]) and wsplit or usplit
 
210
        if len(split) == 1:
 
211
            # Haven't seen all headers yet. Buffer and come back later.
 
212
            cgiflags.linebuf = linebuf
 
213
            return
 
214
 
 
215
        headers = split[0]
 
216
        data = split[1]
 
217
        cgiflags.linebuf = ""
 
218
        cgiflags.started_cgi_body = True
 
219
        # Process all the header lines
 
220
        split = headers.split('\r\n', 1)
 
221
        if len(split) == 1:
 
222
            split = headers.split('\n', 1)
 
223
        while True:
 
224
            process_cgi_header_line(req, split[0], cgiflags)
 
225
            if len(split) == 1: break
 
226
            headers = split[1]
 
227
            if cgiflags.wrote_html_warning:
 
228
                # We're done with headers. Treat the rest as data.
 
229
                data = headers + '\n' + data
 
230
                break
 
231
            split = headers.split('\r\n', 1)
 
232
            if len(split) == 1:
 
233
                split = headers.split('\n', 1)
 
234
 
 
235
        # Is this an internal IVLE error condition?
 
236
        hs = cgiflags.headers
 
237
        if 'X-IVLE-Error-Type' in hs:
 
238
            t = hs['X-IVLE-Error-Type']
 
239
            if t == IVLEError.__name__:
 
240
                raise IVLEError(int(hs['X-IVLE-Error-Code']),
 
241
                                hs['X-IVLE-Error-Message'])
 
242
            else:
 
243
                try:
 
244
                    raise IVLEJailError(hs['X-IVLE-Error-Type'],
 
245
                                        hs['X-IVLE-Error-Message'],
 
246
                                        hs['X-IVLE-Error-Info'])
 
247
                except KeyError:
 
248
                    raise IVLEError(500, 'bad error headers written by CGI')
 
249
 
 
250
        # Check to make sure the required headers were written
 
251
        if cgiflags.wrote_html_warning or not cgiflags.gentle:
 
252
            # We already reported an error, that's enough
 
253
            pass
 
254
        elif "Content-Type" in cgiflags.headers:
 
255
            pass
 
256
        elif "Location" in cgiflags.headers:
 
257
            if ("Status" in cgiflags.headers and req.status >= 300
 
258
                and req.status < 400):
 
259
                pass
 
260
            else:
 
261
                message = """You did not write a valid status code for
 
262
the given location. To make a redirect, you may wish to try:</p>
 
263
<pre style="margin-left: 1em">Status: 302 Found
 
264
Location: &lt;redirect address&gt;</pre>"""
 
265
                write_html_warning(req, message)
 
266
                cgiflags.wrote_html_warning = True
 
267
        else:
 
268
            message = """You did not print a Content-Type header.
 
269
CGI requires that you print a "Content-Type". You may wish to try:</p>
 
270
<pre style="margin-left: 1em">Content-Type: text/html</pre>"""
 
271
            write_html_warning(req, message)
 
272
            cgiflags.wrote_html_warning = True
 
273
 
 
274
        # Call myself to flush out the extra bit of data we read
 
275
        process_cgi_output(req, data, cgiflags)
 
276
 
 
277
def process_cgi_header_line(req, line, cgiflags):
 
278
    """Process a line of CGI header data. line is a string representing a
 
279
    complete line of text, stripped and without the newline.
 
280
    """
 
281
    try:
 
282
        name, value = line.split(':', 1)
 
283
    except ValueError:
 
284
        # If we are being gentle, we want to help the user understand what
 
285
        # went wrong. Otherwise, we bail out.
 
286
        if not cgiflags.gentle:
 
287
            raise
 
288
        # No colon. The user did not write valid headers.
 
289
        if len(cgiflags.headers) == 0:
 
290
            # First line was not a header line. We can assume this is not
 
291
            # a CGI app.
 
292
            message = """You did not print a CGI header.
 
293
CGI requires that you print a "Content-Type". You may wish to try:</p>
 
294
<pre style="margin-left: 1em">Content-Type: text/html</pre>"""
 
295
        else:
 
296
            # They printed some header at least, but there was an invalid
 
297
            # header.
 
298
            message = """You printed an invalid CGI header. You need to leave
 
299
a blank line after the headers, before writing the page contents."""
 
300
        write_html_warning(req, message)
 
301
        cgiflags.wrote_html_warning = True
 
302
        # Handle the rest of this line as normal data
 
303
        process_cgi_output(req, line + '\n', cgiflags)
 
304
        return
 
305
 
 
306
    # Read CGI headers
 
307
    value = value.strip()
 
308
    if name == "Content-Type":
 
309
        req.content_type = value
 
310
    elif name == "Location":
 
311
        req.location = value
 
312
    elif name == "Status":
 
313
        # Must be an integer, followed by a space, and then the status line
 
314
        # which we ignore (seems like Apache has no way to send a custom
 
315
        # status line).
 
316
        try:
 
317
            req.status = int(value.split(' ', 1)[0])
 
318
        except ValueError:
 
319
            if not cgiflags.gentle:
 
320
                # This isn't user code, so it should be good.
 
321
                # Get us out of here!
 
322
                raise
 
323
            message = """The "Status" CGI header was invalid. You need to
 
324
print a number followed by a message, such as "302 Found"."""
 
325
            write_html_warning(req, message)
 
326
            cgiflags.wrote_html_warning = True
 
327
            # Handle the rest of this line as normal data
 
328
            process_cgi_output(req, line + '\n', cgiflags)
 
329
    else:
 
330
        # Generic HTTP header
 
331
        # FIXME: Security risk letting users write arbitrary headers?
 
332
        req.headers_out[name] = value
 
333
    cgiflags.headers[name] = value
 
334
 
 
335
def write_html_warning(req, text):
 
336
    """Prints an HTML warning about invalid CGI interaction on the part of the
 
337
    user. text may contain HTML markup."""
 
338
    req.content_type = "text/html"
 
339
    req.write("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 
340
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
341
<html xmlns="http://www.w3.org/1999/xhtml">
 
342
<head>
 
343
  <meta http-equiv="Content-Type"
 
344
    content="text/html; charset=utf-8" />
 
345
</head>
 
346
<body style="margin: 0; padding: 0; font-family: sans-serif;">
 
347
  <div style="background-color: #faa; border-bottom: 1px solid black;
 
348
    padding: 8px;">
 
349
    <p><strong>Warning</strong>: %s
 
350
  </div>
 
351
  <div style="margin: 8px;">
 
352
    <pre>
 
353
""" % text)
 
354
 
 
355
location_cgi_python = os.path.join(conf.ivle_install_dir,
 
356
    "bin/trampoline")
 
357
 
 
358
# Mapping of interpreter names (as given in conf/app/server.py) to
 
359
# interpreter functions.
 
360
 
 
361
interpreter_objects = {
 
362
    'cgi-python'
 
363
        : functools.partial(execute_cgi, "/usr/bin/python",
 
364
            location_cgi_python),
 
365
    # Should also have:
 
366
    # cgi-generic
 
367
    # python-server-page
 
368
}
 
369
 
 
370
def fixup_environ(req):
 
371
    """Assuming os.environ has been written with the CGI variables from
 
372
    apache, make a few changes for security and correctness.
 
373
 
 
374
    Does not modify req, only reads it.
 
375
    """
 
376
    env = os.environ
 
377
    # Comments here are on the heavy side, explained carefully for security
 
378
    # reasons. Please read carefully before making changes.
 
379
 
 
380
    # Remove DOCUMENT_ROOT and SCRIPT_FILENAME. Not part of CGI spec and
 
381
    # exposes unnecessary details about server.
 
382
    try:
 
383
        del env['DOCUMENT_ROOT']
 
384
    except: pass
 
385
    try:
 
386
        del env['SCRIPT_FILENAME']
 
387
    except: pass
 
388
 
 
389
    # Remove PATH. The PATH here is the path on the server machine; not useful
 
390
    # inside the jail. It may be a good idea to add another path, reflecting
 
391
    # the inside of the jail, but not done at this stage.
 
392
    try:
 
393
        del env['PATH']
 
394
    except: pass
 
395
 
 
396
    # Remove SCRIPT_FILENAME. Not part of CGI spec (see SCRIPT_NAME).
 
397
 
 
398
    # PATH_INFO is wrong because the script doesn't physically exist.
 
399
    # Apache makes it relative to the "serve" app. It should actually be made
 
400
    # relative to the student's script.
 
401
    # TODO: At this stage, it is not possible to add a path after the script,
 
402
    # so PATH_INFO is always "".
 
403
    path_info = ""
 
404
    env['PATH_INFO'] = path_info
 
405
 
 
406
    # PATH_TRANSLATED currently points to a non-existant location within the
 
407
    # local web server directory. Instead make it represent a path within the
 
408
    # student jail.
 
409
    (username, _, path_translated) = studpath.url_to_jailpaths(req.path)
 
410
    if len(path_translated) == 0 or path_translated[0] != os.sep:
 
411
        path_translated = os.sep + path_translated
 
412
    env['PATH_TRANSLATED'] = path_translated
 
413
 
 
414
    # CGI specifies that REMOTE_HOST SHOULD be set, and MAY just be set to
 
415
    # REMOTE_ADDR. Since Apache does not appear to set this, set it to
 
416
    # REMOTE_ADDR.
 
417
    if 'REMOTE_HOST' not in env and 'REMOTE_ADDR' in env:
 
418
        env['REMOTE_HOST'] = env['REMOTE_ADDR']
 
419
 
 
420
    # SCRIPT_NAME is the path to the script WITHOUT PATH_INFO.
 
421
    script_name = req.uri
 
422
    if len(path_info) > 0:
 
423
        script_name = script_name[:-len(path_info)]
 
424
    env['SCRIPT_NAME'] = script_name
 
425
 
 
426
    # SERVER_SOFTWARE is actually not Apache but IVLE, since we are
 
427
    # custom-making the CGI request.
 
428
    env['SERVER_SOFTWARE'] = "IVLE/" + str(conf.ivle_version)
 
429
 
 
430
    # Additional environment variables
 
431
    env['HOME'] = os.path.join('/home', username)