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