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

93 by mattgiuca
New directory hierarchy.
1
#!/usr/bin/python
2
# IVLE
3
# Copyright (C) 2007-2008 The University of Melbourne
4
#
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19
# CGI Trampoline
20
# Author: Tom Conway
21
# Date: 13/12/2007
22
23
# Usage: trampoline.py <uid> <jail-dir> <cwd-within-jail> <script-name> [<interp>]
24
#   <uid>       The user id of the user to do the evaluation
25
#               At the moment this is the user-name, but it would be
26
#               nice if it were made to be the numeric uid, since the
27
#               trampoline has to read /etc/passwd to figure this out
28
#               at the moment. OTOH, the server invoking the trampoline
29
#               may know it already, and in any case can cache it between
30
#               requests, which the trampoline cannot.
31
#   <jail-dir>  The directory that the trampoline should chroot to
32
#   <cwd-within-jail> The directory within the jail that should be
33
#               made the current working directory for the script.
34
#               This should be relative to / within the jail.
35
#   <script-name> The path (within the jail) to the script to be executed.
36
#   <interp>    If given, the interpreter to invoke the script with.
37
38
39
import os
40
import sys
41
import re
42
import resource
43
44
def throttle():
45
    Kb = 1024
46
    Mb = 1024 * 1024
47
48
    limits = [(resource.RLIMIT_CORE,     (0,0)), \
49
              (resource.RLIMIT_CPU,      (1,2)), \
50
              (resource.RLIMIT_FSIZE,    (5 * Mb, 5 * Mb)), \
51
              (resource.RLIMIT_DATA,     (20 * Mb, 24 * Mb)), \
52
              (resource.RLIMIT_STACK,    (8 * Mb, 9 * Mb)), \
53
              (resource.RLIMIT_NPROC,    (10, 10)), \
54
              (resource.RLIMIT_NOFILE,   (10, 12))]
55
56
    for (r,l) in limits:
57
        resource.setrlimit(r,l)
58
59
def runscript(uid, jail, cwd, script, interp):
60
    if uid == 0:
61
        sys.exit(sys.argv[0] + ": cannot run scripts as root!\n")
62
    if not jail.startswith("/home/mgiuca/playground"):
63
        sys.exit(sys.argv[0] + ": invalid jail location!\n")
64
    os.chroot(jail)
65
    os.chdir(cwd)
66
    os.setuid(uid)
67
    throttle()
68
    if interp:
69
        os.execl(interp, script)
70
    else
71
        os.execl(script)
72
73
if len(sys.argv) == 5:
74
    runscript(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], None)
75
else:
76
    runscript(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5])
77