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

« back to all changes in this revision

Viewing changes to www/bin/python-server

  • Committer: mattgiuca
  • Date: 2007-12-20 03:14:17 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:93
New directory hierarchy.
Renamed src to www.
Added console, trampoline (currently empty).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
# Notes
 
4
# -----
 
5
#
 
6
# 1. execute in a chroot jail
 
7
# 2. enforce resource limits
 
8
# 3. allow multiple connections
 
9
# 4. enforce some kind of auth.
 
10
#
 
11
# Another thing is to examine the commonality and difference between
 
12
# the console app and the python evaluator in the tutorial system.
 
13
 
 
14
import socket
 
15
import cStringIO
 
16
import codeop
 
17
import sys
 
18
import cjson
 
19
import signal
 
20
 
 
21
def recv_lines(sok):
 
22
    buf = ''
 
23
    s = sok.recv(4096)
 
24
    while len(s) > 0:
 
25
        buf = buf + s
 
26
        i = buf.find('\r\n')
 
27
        while i != -1:
 
28
            l = buf[0:i]
 
29
            yield l
 
30
            buf = buf[i+2:]
 
31
            i = buf.find('\r\n')
 
32
        s = sok.recv(4096)
 
33
 
 
34
def auth_lines(lines, magic):
 
35
    for line in lines:
 
36
        digest = line[0:32]
 
37
        txt = line[32:]
 
38
        sum = md5.new(txt + magic).digest().encode('hex')
 
39
        if sum != digest:
 
40
            raise Exception, "digest failed!"
 
41
        yeild txt
 
42
 
 
43
shutup_shop_on_timeout = False
 
44
keep_going = True
 
45
 
 
46
def timeout(signum, frame):
 
47
    if shutup_shop_on_timeout:
 
48
        keep_going = False
 
49
        return
 
50
    raise Exception, 'Timeout!'
 
51
 
 
52
# signal.signal(signal.SIGALRM, timeout)
 
53
 
54
# sok = socket.socket(socket.AF_INET)
 
55
# sok.bind(('localhost',9998))
 
56
# sok.listen(1)
 
57
# (new_sok,addr) = sok.accept()
 
58
 
59
# c = codeop.CommandCompiler()
 
60
 
61
# globs = {}
 
62
# locos = {}
 
63
# globs['__builtins__'] = globals()['__builtins__']
 
64
 
65
# out = cStringIO.StringIO()
 
66
# sys.stdout = out
 
67
# first = True
 
68
# for line in req_lines(new_sok):
 
69
#     if first:
 
70
#         src = line
 
71
#         first = False
 
72
#     else:
 
73
#         src = src + '\n' + line
 
74
#     cmd = c(src)
 
75
#     if cmd is not None:
 
76
#         signal.alarm(5)
 
77
#         res = eval(cmd, globs, locos)
 
78
#         signal.alarm(0)
 
79
#         new_sok.send(cjson.encode((out.getvalue(),res)) + '\n')
 
80
#         out = cStringIO.StringIO()
 
81
#         sys.stdout = out
 
82
#         first = True
 
83
 
84
# sok.shutdown(socket.SHUT_RDWR)
 
85
 
 
86
if __name__ == "__main__":
 
87
    try:
 
88
        uid = int(sys.argv[1])
 
89
    except ValueError, v:
 
90
        print >> sys.stderr, "uid must be an integer."
 
91
        sys.exit(1)
 
92
    jail = sys.argv[2]
 
93
    cwd = sys.argv[3]
 
94
    try:
 
95
        port = int(sys.argv[4])
 
96
    except ValueError, v:
 
97
        print >> sys.stderr, "port must be an integer."
 
98
        sys.exit(1)
 
99
    magic = sys.argv[5]
 
100
    # magic = raw_input()
 
101
 
 
102
    try:
 
103
        pid = os.fork()
 
104
        if pid > 0:
 
105
            sys.exit(0)
 
106
    except OSError, e:
 
107
        print >> sys.stderr, "fork #1 failed: %d (%s)" % (e.errno, e.strerror)
 
108
        sys.exit(1)
 
109
 
 
110
    # Okay, now decouple from the parent environment
 
111
    os.chdir('/')
 
112
    os.setsid()
 
113
    os.umask(0)
 
114
 
 
115
    # do second fork
 
116
    try:
 
117
        pid = os.fork()
 
118
        if pid > 0:
 
119
            sys.exit(0)
 
120
    except OSError, e:
 
121
        print >> sys.stderr, "fork #2 failed: %d (%s)" % (e.errno, e.strerror)
 
122
        sys.exit(1)
 
123
 
 
124
    # establish the chrooted environment
 
125
    jail.setup(uid, jail, cwd)
 
126
 
 
127
    signal.signal(signal.SIGALRM, timeout)
 
128
 
 
129
    main_sok = socket.socket(socket.AF_INET)
 
130
    main_sok.bind(('localhost',port))
 
131
    main_sok.listen(1)
 
132
 
 
133
    comp = codeop.CommandCompiler()
 
134
    globs = {}
 
135
    locos = {}
 
136
    globs['__builtins__'] = globals()['__builtins__']
 
137
 
 
138
    global keep_going
 
139
    keep_going = True
 
140
    while keep_going:
 
141
        global shutup_shop_on_timeout
 
142
        shutup_shop_on_timeout = True
 
143
        signal.alarm(30 * 60) # timeout after 30 minutes
 
144
 
 
145
        (sok,addr) = main_sok.accept()
 
146
 
 
147
        signal.alarm(0)
 
148
        shutup_shop_on_timeout = False
 
149
 
 
150
        # FIXME do checks on addr
 
151
 
 
152
        try:
 
153
            first = True
 
154
            # for line in auth_lines(req_lines(new_sok), magic):
 
155
            for line in req_lines(new_sok):
 
156
                if first:
 
157
                    src = line
 
158
                    first = False
 
159
                else:
 
160
                    src = src + '\n' + line
 
161
                try:
 
162
                    cmd = comp(src)
 
163
                    if cmd is not None:
 
164
                        signal.alarm(5)
 
165
                        res = eval(cmd, globs, locos)
 
166
                        signal.alarm(0)
 
167
                        rval = (out.getvalue(), res, None)
 
168
                        sok.send(cjson.encode(rval) + '\n')
 
169
                        out = cStringIO.StringIO()
 
170
                        sys.stdout = out
 
171
                        first = True
 
172
                    else:
 
173
                        sok.send(cjson.encode(None) + '\n')
 
174
                except Exception, e:
 
175
                        rval = (None, None, str(e))
 
176
                        sok.send(cjson.encode(rval) + '\n')
 
177
                        out = cStringIO.StringIO()
 
178
                        sys.stdout = out
 
179
                        first = True
 
180
        finally:
 
181
            sok.close()
 
182
 
 
183
 
 
184
    main_sok.shutdown(socket.SHUT_RDWR)