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

« back to all changes in this revision

Viewing changes to lib/common/interpret.py

  • Committer: drtomc
  • Date: 2008-02-18 01:50:43 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:488
caps.py: Added more capabilities and descriptions.
    (Following meeting mgiuca, conway, sb)

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
# This can be resolved but needs careful sanitisation. See fixup_environ.
29
29
 
30
30
from common import studpath
31
 
from common import db
32
31
import conf
33
32
import functools
34
33
 
43
42
 
44
43
CGI_BLOCK_SIZE = 65535
45
44
 
46
 
uids = {}
47
 
 
48
 
def get_uid(login):
49
 
    """Get the unix uid corresponding to the given login name.
50
 
       If it is not in the dictionary of uids, then consult the
51
 
       database and retrieve an update of the user table."""
52
 
    global uids
53
 
    if login in uids:
54
 
        return uids[login]
55
 
 
56
 
    conn = db.DB()
57
 
    res = conn.get_all('login', ['login', 'unixid'])
58
 
    def repack(flds):
59
 
        return (flds['login'], flds['unixid'])
60
 
    uids = dict(map(repack,res))
61
 
 
62
 
    return uids[login]
63
 
 
64
45
def interpret_file(req, owner, jail_dir, filename, interpreter):
65
46
    """Serves a file by interpreting it using one of IVLE's builtin
66
47
    interpreters. All interpreters are intended to run in the user's jail. The
73
54
    filename: Absolute filename within the user's jail.
74
55
    interpreter: A function object to call.
75
56
    """
76
 
    # We can't test here whether or not the target file actually exists,
77
 
    # because the apache user may not have permission. Instead we have to
78
 
    # rely on the interpreter generating an error.
 
57
    # Make sure the file exists (otherwise some interpreters may not actually
 
58
    # complain).
 
59
    # Don't test for execute permission, that will only be required for
 
60
    # certain interpreters.
79
61
    if filename.startswith(os.sep):
80
62
        filename_abs = filename
81
63
        filename_rel = filename[1:]
83
65
        filename_abs = os.path.join(os.sep, filename)
84
66
        filename_rel = filename
85
67
 
 
68
    if not os.access(os.path.join(jail_dir, filename_rel), os.R_OK):
 
69
        req.throw_error(req.HTTP_NOT_FOUND)
 
70
 
86
71
    # Get the UID of the owner of the file
87
72
    # (Note: files are executed by their owners, not the logged in user.
88
73
    # This ensures users are responsible for their own programs and also
89
74
    # allows them to be executed by the public).
90
 
    uid = get_uid(owner)
 
75
    uid = req.get_session()['unixid']
91
76
 
92
77
    # Split up req.path again, this time with respect to the jail
93
78
    (working_dir, _) = os.path.split(filename_abs)
197
182
        # Break data into lines of CGI header data. 
198
183
        linebuf = cgiflags.linebuf + data
199
184
        # First see if we can split all header data
200
 
        # We need to get the double CRLF- or LF-terminated headers, whichever
201
 
        # is smaller, as either sequence may appear somewhere in the body.
202
 
        usplit = linebuf.split('\n\n', 1)
203
 
        wsplit = linebuf.split('\r\n\r\n', 1)
204
 
        split = len(usplit[0]) > len(wsplit[0]) and wsplit or usplit
 
185
        split = linebuf.split('\r\n\r\n', 1)
 
186
        if len(split) == 1:
 
187
            # Allow UNIX newlines instead
 
188
            split = linebuf.split('\n\n', 1)
205
189
        if len(split) == 1:
206
190
            # Haven't seen all headers yet. Buffer and come back later.
207
191
            cgiflags.linebuf = linebuf