/usr/share/pyshared/Codeville/agent.py is in codeville 0.8.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | # Written by Ross Cohen
# see LICENSE.txt for license information
from bencode import bdecode, bencode
from crypt import crypt
from errno import EINTR
import os
try:
import select
except ImportError:
import selectpoll as select
import hashlib
import socket
import SRP
from cStringIO import StringIO
import struct
AUTH_UNUSED = 0
AUTH_SOCK = 1
AUTH_CONNECTION = 2
class Agent:
def __init__(self):
self.auth_path = None
self.auth_file = None
self.poll_obj = select.poll()
self.hstatus = {}
self.identities = {}
self.allow_shutdown = 0
return
def listen_sock(self, auth_path, auth_file):
if self.auth_file is not None:
print 'Auth socket already set'
return None
self.auth_path = auth_path
self.auth_file = auth_file
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.bind(auth_file)
sock.listen(8)
self._new_socket(AUTH_SOCK, sock)
return sock
def listen(self):
if self.auth_file is None:
print 'No socket to listen on'
return 1
self.shutdown_flag = 0
while not self.shutdown_flag:
try:
sock_list = self.poll_obj.poll()
except select.error, reason:
if reason[0] != EINTR:
raise
else:
self._after_poll(sock_list)
self.cleanup()
return 0
def cleanup(self):
for fd, status in self.hstatus.items():
del self.hstatus[fd]
self.poll_obj.unregister(fd)
status['sock'].close()
os.unlink(self.auth_file)
if self.auth_path is not None:
auth_path = self.auth_path
self.auth_path = None
os.rmdir(auth_path)
return
def _process_message(self, sock):
status = self.hstatus[sock.fileno()]
input = status['input']
input.seek(status['in_offset'])
data = input.read(4)
if len(data) != 4:
return
msg_len, = struct.unpack('<i', data)
if msg_len <= 0 or msg_len > 1024:
self._write_error(sock, 'Bad message lenth')
self._close_socket(sock)
return
data = input.read(msg_len)
if len(data) < msg_len:
return
status['in_offset'] += 4 + msg_len
try:
msg = bdecode(data)
except ValueError:
self._write_error(sock, 'Bad message')
self._close_socket(sock)
return
type = msg['type']
if type == 'CDV_AGENT_ADD_PASSWORD':
id = hashlib.sha1('password' + msg['password']).digest()
self.identities[id] = msg['password']
self._write_answer(sock, bencode({'id': id}))
elif type == 'CDV_AGENT_ADD_SECRET':
id = hashlib.sha1('public hash check' + msg['secret']).digest()
self.identities[id] = msg['secret']
self._write_answer(sock, bencode({'id': id}))
elif type == 'CDV_AGENT_ADD_ENCRYPTED_SECRET':
if not self.identities.has_key(msg['id']):
self._write_error(sock, 'No such identity')
return
secret = crypt(msg['secret'], self.identities[msg['id']])[0]
id = hashlib.sha1('public hash check' + secret).digest()
if id != msg['secret_id']:
self._write_error(sock, 'id does not match')
return
self.identities[id] = secret
self._write_answer(sock, bencode({}))
elif type == 'CDV_AGENT_QUERY_IDENTITY':
known = 0
if self.identities.has_key(msg['id']):
known = 1
self._write_answer(sock, bencode({'known': known}))
elif type == 'CDV_AGENT_SRP_PRIVATE_KEY':
x = SRP.private_key(msg['user'], msg['s'], self.identities[msg['id']])
self._write_answer(sock, bencode({'x': x}))
elif type == 'CDV_AGENT_SESSION_KEY':
if not self.identities.has_key(msg['id']):
self._write_error(sock, 'No such identity')
return
if len(msg['salt1']) < 20 or len(msg['salt2']) < 20:
self._write_error(sock, 'Bad salts')
return
if msg['salt1'] == msg['salt2']:
self._write_error(sock, 'Bad salts')
return
base = 'session key' + self.identities[msg['id']] + \
msg['salt1'] + msg['salt2']
key = hashlib.sha1(base).digest()
answer = {'key': key}
self._write_answer(sock, bencode(answer))
elif type == 'CDV_AGENT_SHUTDOWN' and self.allow_shutdown:
self.shutdown_flag = 1
else:
self._write_error(sock, 'Unknown command: ' + type)
return
def _after_poll(self, sock_list):
for fd, event in sock_list:
status = self.hstatus[fd]
sock = status['sock']
if status['type'] == AUTH_UNUSED:
return
elif status['type'] == AUTH_SOCK:
tries = 3
while tries:
try:
nsock = sock.accept()
except socket.error, reason:
if reason[0] != EINTR:
raise
tries -= 1
else:
break
if tries == 0:
raise
# XXX: python doesn't support SO_PEERCRED
#sock.getsockopt(socket.SOL_SOCKET, socket.SO_PEERCRED)
self._new_socket(AUTH_CONNECTION, nsock[0])
elif status['type'] == AUTH_CONNECTION:
if event & select.POLLHUP:
self._close_socket(sock)
return
if event & select.POLLIN:
data = sock.recv(1024)
if len(data) == 0:
self._close_socket(sock)
return
status['input'].seek(0, 2)
status['input'].write(data)
self._process_message(sock)
if event & select.POLLOUT:
self._flush_socket(sock)
return
def _new_socket(self, type, sock):
sock.setblocking(0)
self.hstatus[sock.fileno()] = {'type': type,
'input': StringIO(),
'output': StringIO(),
'in_offset': 0,
'out_offset': 0,
'sock': sock}
flags = select.POLLIN
if type != AUTH_SOCK:
flags |= select.POLLHUP
self.poll_obj.register(sock, flags)
return
def _close_socket(self, sock):
fileno = sock.fileno()
self.poll_obj.unregister(fileno)
del self.hstatus[fileno]
sock.close()
return
def _write_answer(self, sock, data):
data = struct.pack('<i', len(data)) + data
status = self.hstatus[sock.fileno()]
status['output'].seek(0, 2)
written = 0
if status['out_offset'] == status['output'].tell():
written = sock.send(data)
if written != len(data):
status['output'].write(data[written:])
self.poll_obj.register(sock.fileno(),
select.POLLIN|select.POLLOUT|select.POLLHUP)
return
def _write_error(self, sock, msg):
self._write_answer(sock, bencode({'error': msg}))
return
def _flush_socket(self, sock):
status = self.hstatus[sock.fileno()]
status['output'].seek(status['out_offset'])
data = status['output'].read(1024)
while len(data):
written = sock.send(data)
status['out_offset'] += written
if written < len(data):
break
data = status['output'].read(1024)
if len(data) == 0:
self.poll_obj.register(sock.fileno(), select.POLLIN|select.POLLHUP)
return
|