[project @ Arch-1:robey@lag.net--2005-master-shake%paramiko--dev--1--patch-29]

a bunch of silly changes where i was trying to fix pychecker warnings before i decided it wasnt worth the effort
This commit is contained in:
Robey Pointer 2005-07-10 09:56:00 +00:00
parent 5ba8b47a45
commit 0ac7b0dcbd
12 changed files with 76 additions and 60 deletions

View File

@ -66,7 +66,7 @@ __license__ = "GNU Lesser General Public License (LGPL)"
import transport, auth_transport, channel, rsakey, dsskey, message
import ssh_exception, file, packet, agent, server
import sftp, sftp_client, sftp_attr, sftp_file, sftp_handle, sftp_server, sftp_si
import sftp_client, sftp_attr, sftp_handle, sftp_server, sftp_si
randpool = transport.randpool
Transport = auth_transport.Transport
@ -80,7 +80,7 @@ BadAuthenticationType = ssh_exception.BadAuthenticationType
SFTP = sftp_client.SFTP
SFTPClient = sftp_client.SFTPClient
SFTPServer = sftp_server.SFTPServer
SFTPError = sftp.SFTPError
from sftp import SFTPError
SFTPAttributes = sftp_attr.SFTPAttributes
SFTPHandle = sftp_handle.SFTPHandle
SFTPServerInterface = sftp_si.SFTPServerInterface

View File

@ -46,6 +46,9 @@ class Transport (BaseTransport):
self.username = None
self.authenticated = False
self.auth_event = None
self.auth_method = ''
self.password = None
self.private_key = None
# for server mode:
self.auth_username = None
self.auth_fail_count = 0

View File

@ -45,15 +45,15 @@ class BER(object):
def decode_next(self):
if self.idx >= len(self.content):
return None
id = ord(self.content[self.idx])
ident = ord(self.content[self.idx])
self.idx += 1
if (id & 31) == 31:
if (ident & 31) == 31:
# identifier > 30
id = 0
ident = 0
while self.idx < len(self.content):
t = ord(self.content[self.idx])
self.idx += 1
id = (id << 7) | (t & 0x7f)
ident = (ident << 7) | (t & 0x7f)
if not (t & 0x80):
break
if self.idx >= len(self.content):
@ -75,29 +75,29 @@ class BER(object):
data = self.content[self.idx : self.idx + size]
self.idx += size
# now switch on id
if id == 0x30:
if ident == 0x30:
# sequence
return self.decode_sequence(data)
elif id == 2:
elif ident == 2:
# int
return util.inflate_long(data)
else:
# 1: boolean (00 false, otherwise true)
raise BERException('Unknown ber encoding type %d (robey is lazy)' % id)
raise BERException('Unknown ber encoding type %d (robey is lazy)' % ident)
def decode_sequence(data):
out = []
b = BER(data)
while 1:
while True:
x = b.decode_next()
if x == None:
if x is None:
return out
out.append(x)
decode_sequence = staticmethod(decode_sequence)
def encode_tlv(self, id, val):
# FIXME: support id > 31 someday
self.content += chr(id)
def encode_tlv(self, ident, val):
# no need to support ident > 31 here
self.content += chr(ident)
if len(val) > 0x7f:
lenstr = util.deflate_long(len(val))
self.content += chr(0x80 + len(lenstr)) + lenstr

View File

@ -60,6 +60,7 @@ class Channel (object):
@type chanid: int
"""
self.chanid = chanid
self.remote_chanid = 0
self.transport = None
self.active = False
self.eof_received = 0
@ -73,6 +74,12 @@ class Channel (object):
self.in_buffer_cv = threading.Condition(self.lock)
self.in_stderr_buffer_cv = threading.Condition(self.lock)
self.out_buffer_cv = threading.Condition(self.lock)
self.in_window_size = 0
self.out_window_size = 0
self.in_max_packet_size = 0
self.out_max_packet_size = 0
self.in_window_threshold = 0
self.in_window_sofar = 0
self.status_event = threading.Event()
self.name = str(chanid)
self.logger = util.get_logger('paramiko.chan.' + str(chanid))
@ -1116,6 +1123,8 @@ class ChannelFile (BufferedFile):
self.channel.sendall(data)
return len(data)
seek = BufferedFile.seek
class ChannelStderrFile (ChannelFile):
def __init__(self, channel, mode = 'r', bufsize = -1):

View File

@ -56,6 +56,7 @@ class DSSKey (PKey):
self.g = msg.get_mpint()
self.y = msg.get_mpint()
self.size = util.bit_length(self.p)
self.x = 0L
def __str__(self):
m = Message()
@ -84,16 +85,16 @@ class DSSKey (PKey):
def can_sign(self):
return hasattr(self, 'x')
def sign_ssh_data(self, randpool, data):
hash = SHA.new(data).digest()
def sign_ssh_data(self, rpool, data):
digest = SHA.new(data).digest()
dss = DSA.construct((long(self.y), long(self.g), long(self.p), long(self.q), long(self.x)))
# generate a suitable k
qsize = len(util.deflate_long(self.q, 0))
while 1:
k = util.inflate_long(randpool.get_bytes(qsize), 1)
k = util.inflate_long(rpool.get_bytes(qsize), 1)
if (k > 2) and (k < self.q):
break
r, s = dss.sign(util.inflate_long(hash, 1), k)
r, s = dss.sign(util.inflate_long(digest, 1), k)
m = Message()
m.add_string('ssh-dss')
m.add_string(util.deflate_long(r, 0) + util.deflate_long(s, 0))

View File

@ -96,30 +96,30 @@ class KexGex (object):
self.x = x
def _parse_kexdh_gex_request(self, m):
min = m.get_int()
preferred = m.get_int()
max = m.get_int()
minbits = m.get_int()
preferredbits = m.get_int()
maxbits = m.get_int()
# smoosh the user's preferred size into our own limits
if preferred > self.max_bits:
preferred = self.max_bits
if preferred < self.min_bits:
preferred = self.min_bits
if preferredbits > self.max_bits:
preferredbits = self.max_bits
if preferredbits < self.min_bits:
preferredbits = self.min_bits
# fix min/max if they're inconsistent. technically, we could just pout
# and hang up, but there's no harm in giving them the benefit of the
# doubt and just picking a bitsize for them.
if min > preferred:
min = preferred
if max < preferred:
max = preferred
if minbits > preferredbits:
minbits = preferredbits
if maxbits < preferredbits:
maxbits = preferredbits
# now save a copy
self.min_bits = min
self.preferred_bits = preferred
self.max_bits = max
self.min_bits = minbits
self.preferred_bits = preferredbits
self.max_bits = maxbits
# generate prime
pack = self.transport._get_modulus_pack()
if pack is None:
raise SSHException('Can\'t do server-side gex with no modulus pack')
self.g, self.p = pack.get_modulus(min, preferred, max)
self.g, self.p = pack.get_modulus(minbits, preferredbits, maxbits)
m = Message()
m.add_byte(chr(_MSG_KEXDH_GEX_GROUP))
m.add_mpint(self.p)

View File

@ -41,6 +41,9 @@ class KexGroup1(object):
def __init__(self, transport):
self.transport = transport
self.x = 0L
self.e = 0L
self.f = 0L
def start_kex(self):
self._generate_x()

View File

@ -194,7 +194,7 @@ class Packetizer (object):
n = 0
if self.__closed:
n = -1
except Exception, x:
except Exception:
# could be: (32, 'Broken pipe')
n = -1
if n < 0:
@ -210,13 +210,13 @@ class Packetizer (object):
way, but is only used for initial banner negotiation so it's not worth
optimising.
"""
buffer = ''
while not '\n' in buffer:
buffer += self._read_timeout(timeout)
buffer = buffer[:-1]
if (len(buffer) > 0) and (buffer[-1] == '\r'):
buffer = buffer[:-1]
return buffer
buf = ''
while not '\n' in buf:
buf += self._read_timeout(timeout)
buf = buf[:-1]
if (len(buf) > 0) and (buf[-1] == '\r'):
buf = buf[:-1]
return buf
def send_message(self, data):
"""
@ -275,9 +275,9 @@ class Packetizer (object):
leftover = header[4:]
if (packet_size - len(leftover)) % self.__block_size_in != 0:
raise SSHException('Invalid packet blocking')
buffer = self.read_all(packet_size + self.__mac_size_in - len(leftover))
packet = buffer[:packet_size - len(leftover)]
post_packet = buffer[packet_size - len(leftover):]
buf = self.read_all(packet_size + self.__mac_size_in - len(leftover))
packet = buf[:packet_size - len(leftover)]
post_packet = buf[packet_size - len(leftover):]
if self.__block_engine_in != None:
packet = self.__block_engine_in.decrypt(packet)
if self.__dump_packets:

View File

@ -1,5 +1,3 @@
#!/usr/bin/python
# Copyright (C) 2003-2005 Robey Pointer <robey@lag.net>
#
# This file is part of paramiko.
@ -42,7 +40,7 @@ def _generate_prime(bits, randpool):
if util.bit_length(n) == bits:
return n
def _roll_random(randpool, n):
def _roll_random(rpool, n):
"returns a random # from 0 to N-1"
bits = util.bit_length(n-1)
bytes = (bits + 7) // 8
@ -55,7 +53,7 @@ def _roll_random(randpool, n):
# fits, so i can't guarantee that this loop will ever finish, but the odds
# of it looping forever should be infinitesimal.
while True:
x = randpool.get_bytes(bytes)
x = rpool.get_bytes(bytes)
if hbyte_mask > 0:
x = chr(ord(x[0]) & hbyte_mask) + x[1:]
num = util.inflate_long(x, 1)
@ -69,11 +67,11 @@ class ModulusPack (object):
on systems that have such a file.
"""
def __init__(self, randpool):
def __init__(self, rpool):
# pack is a hash of: bits -> [ (generator, modulus) ... ]
self.pack = {}
self.discarded = []
self.randpool = randpool
self.randpool = rpool
def _parse_modulus(self, line):
timestamp, type, tests, tries, size, generator, modulus = line.split()

View File

@ -76,10 +76,10 @@ class RSAKey (PKey):
def can_sign(self):
return hasattr(self, 'd')
def sign_ssh_data(self, randpool, data):
hash = SHA.new(data).digest()
def sign_ssh_data(self, rpool, data):
digest = SHA.new(data).digest()
rsa = RSA.construct((long(self.n), long(self.e), long(self.d)))
sig = util.deflate_long(rsa.sign(self._pkcs1imify(hash), '')[0], 0)
sig = util.deflate_long(rsa.sign(self._pkcs1imify(digest), '')[0], 0)
m = Message()
m.add_string('ssh-rsa')
m.add_string(sig)

View File

@ -92,6 +92,8 @@ class SFTPError (Exception):
class BaseSFTP (object):
def __init__(self):
self.logger = util.get_logger('paramiko.sftp')
self.sock = None
self.ultra_debug = False
### internals...

View File

@ -1,5 +1,3 @@
#!/usr/bin/python
# Copyright (C) 2003-2005 Robey Pointer <robey@lag.net>
#
# This file is part of paramiko.
@ -54,10 +52,12 @@ class SFTPHandle (object):
using the default implementations of L{read} and L{write}, this
method's default implementation should be fine also.
"""
if hasattr(self, 'readfile') and (self.readfile is not None):
self.readfile.close()
if hasattr(self, 'writefile') and (self.writefile is not None):
self.writefile.close()
readfile = getattr(self, 'readfile', None)
if readfile is not None:
readfile.close()
writefile = getattr(self, 'writefile', None)
if writefile is not None:
writefile.close()
def read(self, offset, length):
"""