/usr/lib/python3/dist-packages/asyncssh/misc.py is in python3-asyncssh 1.11.1-1.
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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | # Copyright (c) 2013-2017 by Ron Frederick <ronf@timeheart.net>.
# All rights reserved.
#
# This program and the accompanying materials are made available under
# the terms of the Eclipse Public License v1.0 which accompanies this
# distribution and is available at:
#
# http://www.eclipse.org/legal/epl-v10.html
#
# Contributors:
# Ron Frederick - initial implementation, API, and documentation
"""Miscellaneous utility classes and functions"""
import asyncio
import functools
import ipaddress
import os
import platform
import socket
from collections import OrderedDict
from random import SystemRandom
import asyncssh
from .constants import DEFAULT_LANG
# Provide globals to test if we're on various Python versions
python344 = platform.python_version_tuple() >= ('3', '4', '4')
python35 = platform.python_version_tuple() >= ('3', '5', '0')
python352 = platform.python_version_tuple() >= ('3', '5', '2')
# Define a version of randrange which is based on SystemRandom(), so that
# we get back numbers suitable for cryptographic use.
_random = SystemRandom()
randrange = _random.randrange
# Avoid deprecation warning for asyncio.async()
if python344:
create_task = asyncio.ensure_future
else: # pragma: no cover
create_task = asyncio.async # pylint: disable=no-member
def all_ints(seq):
"""Return if a sequence contains all integers"""
return all(isinstance(i, int) for i in seq)
# Default file names in .ssh directory to read private keys from
_DEFAULT_KEY_FILES = ('id_ed25519', 'id_ecdsa', 'id_rsa', 'id_dsa')
def load_default_keypairs(passphrase=None):
"""Return a list of default keys from the user's home directory"""
result = []
for file in _DEFAULT_KEY_FILES:
try:
file = os.path.join(os.path.expanduser('~'), '.ssh', file)
result.extend(asyncssh.load_keypairs(file, passphrase))
except asyncssh.KeyImportError as exc:
# Ignore encrypted default keys if a passphrase isn't provided
if not str(exc).startswith('Passphrase'):
raise
except OSError:
pass
return result
# Punctuation to map when creating handler names
_HANDLER_PUNCTUATION = (('@', '_at_'), ('.', '_dot_'), ('-', '_'))
def map_handler_name(name):
"""Map punctuation so a string can be used as a handler name"""
for old, new in _HANDLER_PUNCTUATION:
name = name.replace(old, new)
return name
def _normalize_scoped_ip(addr):
"""Normalize scoped IP address
The ipaddress module doesn't handle scoped addresses properly,
so we strip off the CIDR suffix here and normalize scoped IP
addresses using socket.inet_pton before we pass them into
ipaddress.
"""
for family in (socket.AF_INET, socket.AF_INET6):
try:
return socket.inet_ntop(family, socket.inet_pton(family, addr))
except (ValueError, socket.error):
pass
return addr
def ip_address(addr):
"""Wrapper for ipaddress.ip_address which supports scoped addresses"""
return ipaddress.ip_address(_normalize_scoped_ip(addr))
def ip_network(addr):
"""Wrapper for ipaddress.ip_network which supports scoped addresses"""
idx = addr.find('/')
if idx >= 0:
addr, mask = addr[:idx], addr[idx:]
else:
mask = ''
return ipaddress.ip_network(_normalize_scoped_ip(addr) + mask)
if python352:
async_iterator = lambda iter: iter
else:
async_iterator = asyncio.coroutine
def async_context_manager(coro):
"""Decorator for methods returning asynchronous context managers
This function can be used as a decorator for coroutines which
return objects intended to be used as Python 3.5 asynchronous
context managers. The object returned should implement __aenter__
and __aexit__ methods to run when the async context is entered
and exited.
This wrapper also allows non-async context managers to be defined
on the returned object, as well as the use of "await" or "yield
from" on the function being decorated for backward compatibility
with the API defined by older versions of AsyncSSH.
"""
class AsyncContextManager:
"""Async context manager wrapper for Python 3.5 and later"""
def __init__(self, coro):
self._coro = coro
self._result = None
def __iter__(self):
return (yield from self._coro)
def __await__(self):
return (yield from self._coro)
@asyncio.coroutine
def __aenter__(self):
self._result = yield from self._coro
return (yield from self._result.__aenter__())
@asyncio.coroutine
def __aexit__(self, *exc_info):
yield from self._result.__aexit__(*exc_info)
self._result = None
@functools.wraps(coro)
def coro_wrapper(*args, **kwargs):
"""Return an async context manager wrapper for this coroutine"""
return AsyncContextManager(asyncio.coroutine(coro)(*args, **kwargs))
if python35:
return coro_wrapper
else:
return coro
class Record:
"""General-purpose record type with fixed set of fields"""
__slots__ = OrderedDict()
def __init__(self, *args, **kwargs):
for k, v in self.__slots__.items():
setattr(self, k, v)
for k, v in zip(self.__slots__, args):
setattr(self, k, v)
for k, v in kwargs.items():
setattr(self, k, v)
def __repr__(self):
return '%s(%s)' % (type(self).__name__,
', '.join('%s=%r' % (k, getattr(self, k))
for k in self.__slots__))
class Error(Exception):
"""General SSH error"""
def __init__(self, errtype, code, reason, lang):
super().__init__('%s Error: %s' % (errtype, reason))
self.code = code
self.reason = reason
self.lang = lang
class DisconnectError(Error):
"""SSH disconnect error
This exception is raised when a serious error occurs which causes
the SSH connection to be disconnected. Exception codes should be
taken from :ref:`disconnect reason codes <DisconnectReasons>`.
:param int code:
Disconnect reason, taken from :ref:`disconnect reason
codes <DisconnectReasons>`
:param str reason:
A human-readable reason for the disconnect
:param str lang:
The language the reason is in
"""
def __init__(self, code, reason, lang=DEFAULT_LANG):
super().__init__('Disconnect', code, reason, lang)
class ChannelOpenError(Error):
"""SSH channel open error
This exception is raised by connection handlers to report
channel open failures.
:param int code:
Channel open failure reason, taken from :ref:`channel open
failure reason codes <ChannelOpenFailureReasons>`
:param str reason:
A human-readable reason for the channel open failure
:param str lang:
The language the reason is in
"""
def __init__(self, code, reason, lang=DEFAULT_LANG):
super().__init__('Channel Open', code, reason, lang)
class PasswordChangeRequired(Exception):
"""SSH password change required
This exception is raised during password validation on the
server to indicate that a password change is required. It
shouuld be raised when the password provided is valid but
expired, to trigger the client to provide a new password.
:param str prompt:
The prompt requesting that the user enter a new password
:param str lang:
The language that the prompt is in
"""
def __init__(self, prompt, lang=DEFAULT_LANG):
super().__init__('Password change required: %s' % prompt)
self.prompt = prompt
self.lang = lang
class BreakReceived(Exception):
"""SSH break request received
This exception is raised on an SSH server stdin stream when the
client sends a break on the channel.
:param int msec:
The duration of the break in milliseconds
"""
def __init__(self, msec):
super().__init__('Break for %s msec' % msec)
self.msec = msec
class SignalReceived(Exception):
"""SSH signal request received
This exception is raised on an SSH server stdin stream when the
client sends a signal on the channel.
:param str signal:
The name of the signal sent by the client
"""
def __init__(self, signal):
super().__init__('Signal: %s' % signal)
self.signal = signal
class TerminalSizeChanged(Exception):
"""SSH terminal size change notification received
This exception is raised on an SSH server stdin stream when the
client sends a terminal size change on the channel.
:param int width:
The new terminal width
:param int height:
The new terminal height
:param int pixwidth:
The new terminal width in pixels
:param int pixheight:
The new terminal height in pixels
"""
def __init__(self, width, height, pixwidth, pixheight):
super().__init__('Terminal size change: (%s, %s, %s, %s)' %
(width, height, pixwidth, pixheight))
self.width = width
self.height = height
self.pixwidth = pixwidth
self.pixheight = pixheight
|