66
67
req.content_type = "text/plain"
67
68
req.write_html_head_foot = False
71
(host, port, magic) = start_console(uid, jail_path, working_dir)
73
# Assemble the key and return it.
74
key = cjson.encode({"host": host, "port": port, "magic": magic})
75
req.write(cjson.encode(key.encode("hex")))
77
def handle_chat(req, kind = "chat"):
78
# The request *should* have the following four fields:
79
# host, port, magic: Host and port where the console server lives,
80
# and the secret to use to digitally sign the communication with the
82
# text: Fields to pass along to the console server
83
# It simply acts as a proxy to the console server
84
if req.method != "POST":
85
req.throw_error(req.HTTP_BAD_REQUEST)
86
fields = req.get_fieldstorage()
88
key = cjson.decode(fields.getfirst("key").value.decode("hex"))
92
except AttributeError:
93
# Any of the getfirsts returned None
94
req.throw_error(req.HTTP_BAD_REQUEST)
95
# If text is None, it was probably just an empty line
97
text = fields.getfirst("text").value
98
except AttributeError:
101
msg = {'cmd':kind, 'text':text}
103
response = chat.chat(host, port, msg, magic, decode = False)
104
except socket.error, (enumber, estring):
105
if enumber == errno.ECONNREFUSED:
106
# Timeout: Restart the session
107
jail_path = os.path.join(conf.jail_base, req.user.login)
108
working_dir = os.path.join("/home", req.user.login) # Within jail
110
# Get the UID of the logged-in user
111
uid = req.user.unixid
114
(host, port, magic) = start_console(uid, jail_path, working_dir)
116
# Make a JSON object to tell the browser to restart its console
118
new_key = cjson.encode(
119
{"host": host, "port": port, "magic": magic})
121
"restart": "The IVLE console has timed out due to inactivity",
122
"key": new_key.encode("hex"),
124
response = cjson.encode(json_restart)
126
# Some other error - probably serious
127
raise socket.error, (enumber, estring)
129
req.content_type = "text/plain"
132
def start_console(uid, jail_path, working_dir):
133
"""Starts up a console service for user uid, inside chroot jail jail_path
134
with work directory of working_dir
135
Returns a tupple (host, port, magic)
69
138
# TODO: Figure out the host name the console server is running on.
70
139
host = socket.gethostname()
104
173
raise Exception, "unable to find a free port!"
106
# Assemble the key and return it.
107
key = cjson.encode({"host": host, "port": port, "magic": magic})
108
req.write(cjson.encode(key.encode("hex")))
110
def handle_chat(req, kind = "chat"):
111
# The request *should* have the following four fields:
112
# host, port, magic: Host and port where the console server lives,
113
# and the secret to use to digitally sign the communication with the
115
# text: Fields to pass along to the console server
116
# It simply acts as a proxy to the console server
117
if req.method != "POST":
118
req.throw_error(req.HTTP_BAD_REQUEST)
119
fields = req.get_fieldstorage()
121
key = cjson.decode(fields.getfirst("key").value.decode("hex"))
125
except AttributeError:
126
# Any of the getfirsts returned None
127
req.throw_error(req.HTTP_BAD_REQUEST)
128
# If text is None, it was probably just an empty line
130
text = fields.getfirst("text").value
131
except AttributeError:
134
msg = {'cmd':kind, 'text':text}
135
response = chat.chat(host, port, msg, magic, decode = False)
136
req.content_type = "text/plain"
175
return (host, port, magic)