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

93 by mattgiuca
New directory hierarchy.
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: 20/12/2007
21
22
# Runs a student script in a safe execution environment.
23
24
from common import studpath
25
import conf
26
import functools
27
28
import os
29
import pwd
30
import subprocess
31
32
def interpret_file(req, owner, filename, interpreter):
33
    """Serves a file by interpreting it using one of IVLE's builtin
34
    interpreters. All interpreters are intended to run in the user's jail. The
35
    jail location is provided as an argument to the interpreter but it is up
36
    to the individual interpreters to create the jail.
37
38
    req: An IVLE request object.
39
    owner: Username of the user who owns the file being served.
40
    filename: Filename in the local file system.
41
    interpreter: A function object to call.
42
    """
43
    # Make sure the file exists (otherwise some interpreters may not actually
44
    # complain).
45
    # Don't test for execute permission, that will only be required for
46
    # certain interpreters.
47
    if not os.access(filename, os.R_OK):
48
        req.throw_error(req.HTTP_NOT_FOUND)
49
50
    # Get the UID of the owner of the file
51
    # (Note: files are executed by their owners, not the logged in user.
52
    # This ensures users are responsible for their own programs and also
53
    # allows them to be executed by the public).
54
    try:
55
        (_,_,uid,_,_,_,_) = pwd.getpwnam(owner)
56
    except KeyError:
57
        # The user does not exist. This should have already failed the
58
        # previous test.
59
        req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR)
60
61
    # Split up req.path again, this time with respect to the jail
62
    (_, jail_dir, path) = studpath.url_to_jailpaths(req.path)
63
    path = os.path.join('/', path)
64
    (working_dir, _) = os.path.split(path)
65
    # jail_dir is the absolute jail directory.
66
    # path is the filename relative to the user's jail.
67
    # working_dir is the directory containing the file relative to the user's
68
    # jail.
69
    # (Note that paths "relative" to the jail actually begin with a '/' as
70
    # they are absolute in the jailspace)
71
72
    return interpreter(uid, jail_dir, working_dir, path, req)
73
74
# Used to store mutable data
75
class Dummy:
76
    pass
77
78
def execute_cgi(trampoline, uid, jail_dir, working_dir, script_path, req):
79
    """
80
    trampoline: Full path on the local system to the CGI wrapper program
81
        being executed.
82
    uid: User ID of the owner of the file.
83
    jail_dir: Absolute path of owner's jail directory.
84
    working_dir: Directory containing the script file relative to owner's
85
        jail.
86
    script_path: CGI script relative to the owner's jail.
87
    req: IVLE request object.
88
89
    The called CGI wrapper application shall be called using popen and receive
90
    the HTTP body on stdin. It shall receive the CGI environment variables to
91
    its environment.
92
    """
93
94
    # Get the student program's directory and execute it from that context.
95
    (tramp_dir, _) = os.path.split(trampoline)
96
97
    # TODO: Don't create a file if the body length is known to be 0
98
    # Write the HTTP body to a temporary file so it can be passed as a *real*
99
    # file to popen.
100
    f = os.tmpfile()
101
    body = req.read()
102
    if body is not None:
103
        f.write(body)
104
        f.flush()
105
        f.seek(0)       # Rewind, for reading
106
107
    # usage: tramp uid jail_dir working_dir script_path
108
    pid = subprocess.Popen(
109
        [trampoline, str(uid), jail_dir, working_dir, script_path],
110
        stdin=f, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
111
        cwd=tramp_dir)
112
113
    # process_cgi_line: Reads a single line of CGI output and processes it.
114
    # Prints to req, and also does fancy HTML warnings if Content-Type
115
    # omitted.
116
    cgiflags = Dummy()
117
    cgiflags.got_cgi_header = False
118
    cgiflags.started_cgi_body = False
119
    cgiflags.wrote_html_warning = False
120
    def process_cgi_line(line):
121
        # FIXME? Issue with binary files (processing per-line?)
122
        if cgiflags.started_cgi_body:
123
            # FIXME: HTML escape text if wrote_html_warning
124
            req.write(line)
125
        else:
126
            # Read CGI headers
127
            if line.strip() == "" and cgiflags.got_cgi_header:
128
                cgiflags.started_cgi_body = True
129
            elif line.startswith("Content-Type:"):
130
                req.content_type = line[13:].strip()
131
                cgiflags.got_cgi_header = True
132
            elif line.startswith("Location:"):
133
                # TODO
134
                cgiflags.got_cgi_header = True
135
            elif line.startswith("Status:"):
136
                # TODO
137
                cgiflags.got_cgi_header = True
138
            elif cgiflags.got_cgi_header:
139
                # Invalid header
140
                # TODO
141
                req.write("Invalid header")
142
                pass
143
            else:
144
                # Assume the user is not printing headers and give a warning
145
                # about that.
146
                # User program did not print header.
147
                # Make a fancy HTML warning for them.
148
                req.content_type = "text/html"
149
                req.write("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
150
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
151
<html xmlns="http://www.w3.org/1999/xhtml">
152
<head>
153
  <meta http-equiv="Content-Type"
154
    content="text/html; charset=utf-8" />
155
</head>
156
<body style="margin: 0; padding: 0; font-family: sans;">
157
  <div style="background-color: #faa; border-bottom: 1px solid black;
158
    padding: 8px;">
159
    <p><strong>Warning</strong>: You did not print a "Content-Type" header.
160
    CGI requires you to print some content type. You may wish to try:</p>
161
    <pre style="margin-left: 1em">Content-Type: text/html</pre>
162
  </div>
163
  <div style="margin: 8px;">
164
    <pre>
165
""")
166
                cgiflags.got_cgi_header = True
167
                cgiflags.wrote_html_warning = True
168
                cgiflags.started_cgi_body = True
169
                req.write(line)
170
171
    # Read from the process's stdout into req
172
    for line in pid.stdout:
173
        process_cgi_line(line)
174
175
    # If we wrote an HTML warning header, write the footer
176
    if cgiflags.wrote_html_warning:
177
        req.write("""</pre>
178
  </div>
179
</body>
180
</html>""")
181
182
# TODO: Replace mytest with cgi trampoline handler script
183
location_cgi_python = os.path.join(conf.ivlepath, "bin/trampoline-python")
184
185
# Mapping of interpreter names (as given in conf/app/server.py) to
186
# interpreter functions.
187
188
interpreter_objects = {
189
    'cgi-python'
190
        : functools.partial(execute_cgi, location_cgi_python),
191
    # Should also have:
192
    # cgi-generic
193
    # python-server-page
194
}
195