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

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