6
# 1. execute in a chroot jail
7
# 2. enforce resource limits
8
# 3. allow multiple connections
9
# 4. enforce some kind of auth.
11
# Another thing is to examine the commonality and difference between
12
# the console app and the python evaluator in the tutorial system.
34
def auth_lines(lines, magic):
38
sum = md5.new(txt + magic).digest().encode('hex')
40
raise Exception, "digest failed!"
43
shutup_shop_on_timeout = False
46
def timeout(signum, frame):
47
if shutup_shop_on_timeout:
50
raise Exception, 'Timeout!'
52
# signal.signal(signal.SIGALRM, timeout)
54
# sok = socket.socket(socket.AF_INET)
55
# sok.bind(('localhost',9998))
57
# (new_sok,addr) = sok.accept()
59
# c = codeop.CommandCompiler()
63
# globs['__builtins__'] = globals()['__builtins__']
65
# out = cStringIO.StringIO()
68
# for line in req_lines(new_sok):
73
# src = src + '\n' + line
77
# res = eval(cmd, globs, locos)
79
# new_sok.send(cjson.encode((out.getvalue(),res)) + '\n')
80
# out = cStringIO.StringIO()
84
# sok.shutdown(socket.SHUT_RDWR)
86
if __name__ == "__main__":
88
uid = int(sys.argv[1])
90
print >> sys.stderr, "uid must be an integer."
95
port = int(sys.argv[4])
97
print >> sys.stderr, "port must be an integer."
100
# magic = raw_input()
107
print >> sys.stderr, "fork #1 failed: %d (%s)" % (e.errno, e.strerror)
110
# Okay, now decouple from the parent environment
121
print >> sys.stderr, "fork #2 failed: %d (%s)" % (e.errno, e.strerror)
124
# establish the chrooted environment
125
jail.setup(uid, jail, cwd)
127
signal.signal(signal.SIGALRM, timeout)
129
main_sok = socket.socket(socket.AF_INET)
130
main_sok.bind(('localhost',port))
133
comp = codeop.CommandCompiler()
136
globs['__builtins__'] = globals()['__builtins__']
141
global shutup_shop_on_timeout
142
shutup_shop_on_timeout = True
143
signal.alarm(30 * 60) # timeout after 30 minutes
145
(sok,addr) = main_sok.accept()
148
shutup_shop_on_timeout = False
150
# FIXME do checks on addr
154
# for line in auth_lines(req_lines(new_sok), magic):
155
for line in req_lines(new_sok):
160
src = src + '\n' + line
165
res = eval(cmd, globs, locos)
167
rval = (out.getvalue(), res, None)
168
sok.send(cjson.encode(rval) + '\n')
169
out = cStringIO.StringIO()
173
rval = (None, None, str(e))
174
sok.send(cjson.encode(rval) + '\n')
175
out = cStringIO.StringIO()
182
main_sok.shutdown(socket.SHUT_RDWR)