common.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. # Copyright (C) 2003-2007 Robey Pointer <robeypointer@gmail.com>
  2. #
  3. # This file is part of paramiko.
  4. #
  5. # Paramiko is free software; you can redistribute it and/or modify it under the
  6. # terms of the GNU Lesser General Public License as published by the Free
  7. # Software Foundation; either version 2.1 of the License, or (at your option)
  8. # any later version.
  9. #
  10. # Paramiko is distributed in the hope that it will be useful, but WITHOUT ANY
  11. # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. # A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  13. # details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with Paramiko; if not, write to the Free Software Foundation, Inc.,
  17. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. """
  19. Common constants and global variables.
  20. """
  21. import logging
  22. from paramiko.py3compat import byte_chr, PY2, long, b
  23. (
  24. MSG_DISCONNECT,
  25. MSG_IGNORE,
  26. MSG_UNIMPLEMENTED,
  27. MSG_DEBUG,
  28. MSG_SERVICE_REQUEST,
  29. MSG_SERVICE_ACCEPT,
  30. MSG_EXT_INFO,
  31. ) = range(1, 8)
  32. (MSG_KEXINIT, MSG_NEWKEYS) = range(20, 22)
  33. (
  34. MSG_USERAUTH_REQUEST,
  35. MSG_USERAUTH_FAILURE,
  36. MSG_USERAUTH_SUCCESS,
  37. MSG_USERAUTH_BANNER,
  38. ) = range(50, 54)
  39. MSG_USERAUTH_PK_OK = 60
  40. (MSG_USERAUTH_INFO_REQUEST, MSG_USERAUTH_INFO_RESPONSE) = range(60, 62)
  41. (MSG_USERAUTH_GSSAPI_RESPONSE, MSG_USERAUTH_GSSAPI_TOKEN) = range(60, 62)
  42. (
  43. MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
  44. MSG_USERAUTH_GSSAPI_ERROR,
  45. MSG_USERAUTH_GSSAPI_ERRTOK,
  46. MSG_USERAUTH_GSSAPI_MIC,
  47. ) = range(63, 67)
  48. HIGHEST_USERAUTH_MESSAGE_ID = 79
  49. (MSG_GLOBAL_REQUEST, MSG_REQUEST_SUCCESS, MSG_REQUEST_FAILURE) = range(80, 83)
  50. (
  51. MSG_CHANNEL_OPEN,
  52. MSG_CHANNEL_OPEN_SUCCESS,
  53. MSG_CHANNEL_OPEN_FAILURE,
  54. MSG_CHANNEL_WINDOW_ADJUST,
  55. MSG_CHANNEL_DATA,
  56. MSG_CHANNEL_EXTENDED_DATA,
  57. MSG_CHANNEL_EOF,
  58. MSG_CHANNEL_CLOSE,
  59. MSG_CHANNEL_REQUEST,
  60. MSG_CHANNEL_SUCCESS,
  61. MSG_CHANNEL_FAILURE,
  62. ) = range(90, 101)
  63. cMSG_DISCONNECT = byte_chr(MSG_DISCONNECT)
  64. cMSG_IGNORE = byte_chr(MSG_IGNORE)
  65. cMSG_UNIMPLEMENTED = byte_chr(MSG_UNIMPLEMENTED)
  66. cMSG_DEBUG = byte_chr(MSG_DEBUG)
  67. cMSG_SERVICE_REQUEST = byte_chr(MSG_SERVICE_REQUEST)
  68. cMSG_SERVICE_ACCEPT = byte_chr(MSG_SERVICE_ACCEPT)
  69. cMSG_EXT_INFO = byte_chr(MSG_EXT_INFO)
  70. cMSG_KEXINIT = byte_chr(MSG_KEXINIT)
  71. cMSG_NEWKEYS = byte_chr(MSG_NEWKEYS)
  72. cMSG_USERAUTH_REQUEST = byte_chr(MSG_USERAUTH_REQUEST)
  73. cMSG_USERAUTH_FAILURE = byte_chr(MSG_USERAUTH_FAILURE)
  74. cMSG_USERAUTH_SUCCESS = byte_chr(MSG_USERAUTH_SUCCESS)
  75. cMSG_USERAUTH_BANNER = byte_chr(MSG_USERAUTH_BANNER)
  76. cMSG_USERAUTH_PK_OK = byte_chr(MSG_USERAUTH_PK_OK)
  77. cMSG_USERAUTH_INFO_REQUEST = byte_chr(MSG_USERAUTH_INFO_REQUEST)
  78. cMSG_USERAUTH_INFO_RESPONSE = byte_chr(MSG_USERAUTH_INFO_RESPONSE)
  79. cMSG_USERAUTH_GSSAPI_RESPONSE = byte_chr(MSG_USERAUTH_GSSAPI_RESPONSE)
  80. cMSG_USERAUTH_GSSAPI_TOKEN = byte_chr(MSG_USERAUTH_GSSAPI_TOKEN)
  81. cMSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE = byte_chr(
  82. MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE
  83. )
  84. cMSG_USERAUTH_GSSAPI_ERROR = byte_chr(MSG_USERAUTH_GSSAPI_ERROR)
  85. cMSG_USERAUTH_GSSAPI_ERRTOK = byte_chr(MSG_USERAUTH_GSSAPI_ERRTOK)
  86. cMSG_USERAUTH_GSSAPI_MIC = byte_chr(MSG_USERAUTH_GSSAPI_MIC)
  87. cMSG_GLOBAL_REQUEST = byte_chr(MSG_GLOBAL_REQUEST)
  88. cMSG_REQUEST_SUCCESS = byte_chr(MSG_REQUEST_SUCCESS)
  89. cMSG_REQUEST_FAILURE = byte_chr(MSG_REQUEST_FAILURE)
  90. cMSG_CHANNEL_OPEN = byte_chr(MSG_CHANNEL_OPEN)
  91. cMSG_CHANNEL_OPEN_SUCCESS = byte_chr(MSG_CHANNEL_OPEN_SUCCESS)
  92. cMSG_CHANNEL_OPEN_FAILURE = byte_chr(MSG_CHANNEL_OPEN_FAILURE)
  93. cMSG_CHANNEL_WINDOW_ADJUST = byte_chr(MSG_CHANNEL_WINDOW_ADJUST)
  94. cMSG_CHANNEL_DATA = byte_chr(MSG_CHANNEL_DATA)
  95. cMSG_CHANNEL_EXTENDED_DATA = byte_chr(MSG_CHANNEL_EXTENDED_DATA)
  96. cMSG_CHANNEL_EOF = byte_chr(MSG_CHANNEL_EOF)
  97. cMSG_CHANNEL_CLOSE = byte_chr(MSG_CHANNEL_CLOSE)
  98. cMSG_CHANNEL_REQUEST = byte_chr(MSG_CHANNEL_REQUEST)
  99. cMSG_CHANNEL_SUCCESS = byte_chr(MSG_CHANNEL_SUCCESS)
  100. cMSG_CHANNEL_FAILURE = byte_chr(MSG_CHANNEL_FAILURE)
  101. # for debugging:
  102. MSG_NAMES = {
  103. MSG_DISCONNECT: "disconnect",
  104. MSG_IGNORE: "ignore",
  105. MSG_UNIMPLEMENTED: "unimplemented",
  106. MSG_DEBUG: "debug",
  107. MSG_SERVICE_REQUEST: "service-request",
  108. MSG_SERVICE_ACCEPT: "service-accept",
  109. MSG_KEXINIT: "kexinit",
  110. MSG_EXT_INFO: "ext-info",
  111. MSG_NEWKEYS: "newkeys",
  112. 30: "kex30",
  113. 31: "kex31",
  114. 32: "kex32",
  115. 33: "kex33",
  116. 34: "kex34",
  117. 40: "kex40",
  118. 41: "kex41",
  119. MSG_USERAUTH_REQUEST: "userauth-request",
  120. MSG_USERAUTH_FAILURE: "userauth-failure",
  121. MSG_USERAUTH_SUCCESS: "userauth-success",
  122. MSG_USERAUTH_BANNER: "userauth--banner",
  123. MSG_USERAUTH_PK_OK: "userauth-60(pk-ok/info-request)",
  124. MSG_USERAUTH_INFO_RESPONSE: "userauth-info-response",
  125. MSG_GLOBAL_REQUEST: "global-request",
  126. MSG_REQUEST_SUCCESS: "request-success",
  127. MSG_REQUEST_FAILURE: "request-failure",
  128. MSG_CHANNEL_OPEN: "channel-open",
  129. MSG_CHANNEL_OPEN_SUCCESS: "channel-open-success",
  130. MSG_CHANNEL_OPEN_FAILURE: "channel-open-failure",
  131. MSG_CHANNEL_WINDOW_ADJUST: "channel-window-adjust",
  132. MSG_CHANNEL_DATA: "channel-data",
  133. MSG_CHANNEL_EXTENDED_DATA: "channel-extended-data",
  134. MSG_CHANNEL_EOF: "channel-eof",
  135. MSG_CHANNEL_CLOSE: "channel-close",
  136. MSG_CHANNEL_REQUEST: "channel-request",
  137. MSG_CHANNEL_SUCCESS: "channel-success",
  138. MSG_CHANNEL_FAILURE: "channel-failure",
  139. MSG_USERAUTH_GSSAPI_RESPONSE: "userauth-gssapi-response",
  140. MSG_USERAUTH_GSSAPI_TOKEN: "userauth-gssapi-token",
  141. MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE: "userauth-gssapi-exchange-complete",
  142. MSG_USERAUTH_GSSAPI_ERROR: "userauth-gssapi-error",
  143. MSG_USERAUTH_GSSAPI_ERRTOK: "userauth-gssapi-error-token",
  144. MSG_USERAUTH_GSSAPI_MIC: "userauth-gssapi-mic",
  145. }
  146. # authentication request return codes:
  147. AUTH_SUCCESSFUL, AUTH_PARTIALLY_SUCCESSFUL, AUTH_FAILED = range(3)
  148. # channel request failed reasons:
  149. (
  150. OPEN_SUCCEEDED,
  151. OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED,
  152. OPEN_FAILED_CONNECT_FAILED,
  153. OPEN_FAILED_UNKNOWN_CHANNEL_TYPE,
  154. OPEN_FAILED_RESOURCE_SHORTAGE,
  155. ) = range(0, 5)
  156. CONNECTION_FAILED_CODE = {
  157. 1: "Administratively prohibited",
  158. 2: "Connect failed",
  159. 3: "Unknown channel type",
  160. 4: "Resource shortage",
  161. }
  162. (
  163. DISCONNECT_SERVICE_NOT_AVAILABLE,
  164. DISCONNECT_AUTH_CANCELLED_BY_USER,
  165. DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE,
  166. ) = (7, 13, 14)
  167. zero_byte = byte_chr(0)
  168. one_byte = byte_chr(1)
  169. four_byte = byte_chr(4)
  170. max_byte = byte_chr(0xff)
  171. cr_byte = byte_chr(13)
  172. linefeed_byte = byte_chr(10)
  173. crlf = cr_byte + linefeed_byte
  174. if PY2:
  175. cr_byte_value = cr_byte
  176. linefeed_byte_value = linefeed_byte
  177. else:
  178. cr_byte_value = 13
  179. linefeed_byte_value = 10
  180. def asbytes(s):
  181. """
  182. Coerce to bytes if possible or return unchanged.
  183. """
  184. try:
  185. # Attempt to run through our version of b(), which does the Right Thing
  186. # for string/unicode/buffer (Py2) or bytes/str (Py3), and raises
  187. # TypeError if it's not one of those types.
  188. return b(s)
  189. except TypeError:
  190. try:
  191. # If it wasn't a string/byte/buffer type object, try calling an
  192. # asbytes() method, which many of our internal classes implement.
  193. return s.asbytes()
  194. except AttributeError:
  195. # Finally, just do nothing & assume this object is sufficiently
  196. # byte-y or buffer-y that everything will work out (or that callers
  197. # are capable of handling whatever it is.)
  198. return s
  199. xffffffff = long(0xffffffff)
  200. x80000000 = long(0x80000000)
  201. o666 = 438
  202. o660 = 432
  203. o644 = 420
  204. o600 = 384
  205. o777 = 511
  206. o700 = 448
  207. o70 = 56
  208. DEBUG = logging.DEBUG
  209. INFO = logging.INFO
  210. WARNING = logging.WARNING
  211. ERROR = logging.ERROR
  212. CRITICAL = logging.CRITICAL
  213. # Common IO/select/etc sleep period, in seconds
  214. io_sleep = 0.01
  215. DEFAULT_WINDOW_SIZE = 64 * 2 ** 15
  216. DEFAULT_MAX_PACKET_SIZE = 2 ** 15
  217. # lower bound on the max packet size we'll accept from the remote host
  218. # Minimum packet size is 32768 bytes according to
  219. # http://www.ietf.org/rfc/rfc4254.txt
  220. MIN_WINDOW_SIZE = 2 ** 15
  221. # However, according to http://www.ietf.org/rfc/rfc4253.txt it is perfectly
  222. # legal to accept a size much smaller, as OpenSSH client does as size 16384.
  223. MIN_PACKET_SIZE = 2 ** 12
  224. # Max windows size according to http://www.ietf.org/rfc/rfc4254.txt
  225. MAX_WINDOW_SIZE = 2 ** 32 - 1