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 |
||
98
by mattgiuca
common/interpret.py: Now passes the location of the python interpreter to the |
78 |
def execute_cgi(interpreter, trampoline, uid, jail_dir, working_dir, |
79 |
script_path, req): |
|
93
by mattgiuca
New directory hierarchy. |
80 |
"""
|
81 |
trampoline: Full path on the local system to the CGI wrapper program
|
|
82 |
being executed.
|
|
83 |
uid: User ID of the owner of the file.
|
|
84 |
jail_dir: Absolute path of owner's jail directory.
|
|
85 |
working_dir: Directory containing the script file relative to owner's
|
|
86 |
jail.
|
|
87 |
script_path: CGI script relative to the owner's jail.
|
|
88 |
req: IVLE request object.
|
|
89 |
||
90 |
The called CGI wrapper application shall be called using popen and receive
|
|
91 |
the HTTP body on stdin. It shall receive the CGI environment variables to
|
|
92 |
its environment.
|
|
93 |
"""
|
|
94 |
||
95 |
# Get the student program's directory and execute it from that context.
|
|
96 |
(tramp_dir, _) = os.path.split(trampoline) |
|
97 |
||
98 |
# TODO: Don't create a file if the body length is known to be 0
|
|
99 |
# Write the HTTP body to a temporary file so it can be passed as a *real*
|
|
100 |
# file to popen.
|
|
101 |
f = os.tmpfile() |
|
102 |
body = req.read() |
|
103 |
if body is not None: |
|
104 |
f.write(body) |
|
105 |
f.flush() |
|
106 |
f.seek(0) # Rewind, for reading |
|
107 |
||
108 |
# usage: tramp uid jail_dir working_dir script_path
|
|
109 |
pid = subprocess.Popen( |
|
98
by mattgiuca
common/interpret.py: Now passes the location of the python interpreter to the |
110 |
[trampoline, str(uid), jail_dir, working_dir, interpreter, |
111 |
script_path], |
|
93
by mattgiuca
New directory hierarchy. |
112 |
stdin=f, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, |
113 |
cwd=tramp_dir) |
|
114 |
||
115 |
# process_cgi_line: Reads a single line of CGI output and processes it.
|
|
116 |
# Prints to req, and also does fancy HTML warnings if Content-Type
|
|
117 |
# omitted.
|
|
118 |
cgiflags = Dummy() |
|
119 |
cgiflags.got_cgi_header = False |
|
120 |
cgiflags.started_cgi_body = False |
|
121 |
cgiflags.wrote_html_warning = False |
|
122 |
def process_cgi_line(line): |
|
123 |
# FIXME? Issue with binary files (processing per-line?)
|
|
124 |
if cgiflags.started_cgi_body: |
|
125 |
# FIXME: HTML escape text if wrote_html_warning
|
|
126 |
req.write(line) |
|
127 |
else: |
|
128 |
# Read CGI headers
|
|
129 |
if line.strip() == "" and cgiflags.got_cgi_header: |
|
130 |
cgiflags.started_cgi_body = True |
|
131 |
elif line.startswith("Content-Type:"): |
|
132 |
req.content_type = line[13:].strip() |
|
133 |
cgiflags.got_cgi_header = True |
|
134 |
elif line.startswith("Location:"): |
|
135 |
# TODO
|
|
136 |
cgiflags.got_cgi_header = True |
|
137 |
elif line.startswith("Status:"): |
|
138 |
# TODO
|
|
139 |
cgiflags.got_cgi_header = True |
|
140 |
elif cgiflags.got_cgi_header: |
|
141 |
# Invalid header
|
|
142 |
# TODO
|
|
143 |
req.write("Invalid header") |
|
144 |
pass
|
|
145 |
else: |
|
146 |
# Assume the user is not printing headers and give a warning
|
|
147 |
# about that.
|
|
148 |
# User program did not print header.
|
|
149 |
# Make a fancy HTML warning for them.
|
|
150 |
req.content_type = "text/html" |
|
151 |
req.write("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" |
|
152 |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
|
153 |
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
154 |
<head>
|
|
155 |
<meta http-equiv="Content-Type"
|
|
156 |
content="text/html; charset=utf-8" />
|
|
157 |
</head>
|
|
158 |
<body style="margin: 0; padding: 0; font-family: sans;">
|
|
159 |
<div style="background-color: #faa; border-bottom: 1px solid black;
|
|
160 |
padding: 8px;">
|
|
161 |
<p><strong>Warning</strong>: You did not print a "Content-Type" header.
|
|
162 |
CGI requires you to print some content type. You may wish to try:</p>
|
|
163 |
<pre style="margin-left: 1em">Content-Type: text/html</pre>
|
|
164 |
</div>
|
|
165 |
<div style="margin: 8px;">
|
|
166 |
<pre>
|
|
167 |
""") |
|
168 |
cgiflags.got_cgi_header = True |
|
169 |
cgiflags.wrote_html_warning = True |
|
170 |
cgiflags.started_cgi_body = True |
|
171 |
req.write(line) |
|
172 |
||
173 |
# Read from the process's stdout into req
|
|
174 |
for line in pid.stdout: |
|
175 |
process_cgi_line(line) |
|
176 |
||
177 |
# If we wrote an HTML warning header, write the footer
|
|
178 |
if cgiflags.wrote_html_warning: |
|
179 |
req.write("""</pre> |
|
180 |
</div>
|
|
181 |
</body>
|
|
182 |
</html>""") |
|
183 |
||
184 |
# TODO: Replace mytest with cgi trampoline handler script
|
|
97
by mattgiuca
Moved template.py and setup.py to better places. |
185 |
location_cgi_python = os.path.join(conf.ivle_install_dir, |
186 |
"bin/trampoline") |
|
93
by mattgiuca
New directory hierarchy. |
187 |
|
188 |
# Mapping of interpreter names (as given in conf/app/server.py) to
|
|
189 |
# interpreter functions.
|
|
190 |
||
191 |
interpreter_objects = { |
|
192 |
'cgi-python'
|
|
98
by mattgiuca
common/interpret.py: Now passes the location of the python interpreter to the |
193 |
: functools.partial(execute_cgi, "/usr/bin/python", |
194 |
location_cgi_python), |
|
93
by mattgiuca
New directory hierarchy. |
195 |
# Should also have:
|
196 |
# cgi-generic
|
|
197 |
# python-server-page
|
|
198 |
}
|
|
199 |