ssh.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # ex:ts=4:sw=4:sts=4:et
  2. # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
  3. '''
  4. BitBake 'Fetch' implementations
  5. This implementation is for Secure Shell (SSH), and attempts to comply with the
  6. IETF secsh internet draft:
  7. http://tools.ietf.org/wg/secsh/draft-ietf-secsh-scp-sftp-ssh-uri/
  8. Currently does not support the sftp parameters, as this uses scp
  9. Also does not support the 'fingerprint' connection parameter.
  10. Please note that '/' is used as host, path separator not ':' as you may
  11. be used to, also '~' can be used to specify user HOME, but again after '/'
  12. Example SRC_URI:
  13. SRC_URI = "ssh://user@host.example.com/dir/path/file.txt"
  14. SRC_URI = "ssh://user@host.example.com/~/file.txt"
  15. '''
  16. # Copyright (C) 2006 OpenedHand Ltd.
  17. #
  18. #
  19. # Based in part on svk.py:
  20. # Copyright (C) 2006 Holger Hans Peter Freyther
  21. # Based on svn.py:
  22. # Copyright (C) 2003, 2004 Chris Larson
  23. # Based on functions from the base bb module:
  24. # Copyright 2003 Holger Schurig
  25. #
  26. #
  27. # SPDX-License-Identifier: GPL-2.0-only
  28. #
  29. # This program is free software; you can redistribute it and/or modify
  30. # it under the terms of the GNU General Public License version 2 as
  31. # published by the Free Software Foundation.
  32. #
  33. # This program is distributed in the hope that it will be useful,
  34. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  36. # GNU General Public License for more details.
  37. #
  38. # You should have received a copy of the GNU General Public License along
  39. # with this program; if not, write to the Free Software Foundation, Inc.,
  40. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  41. import re, os
  42. from bb.fetch2 import FetchMethod
  43. from bb.fetch2 import FetchError
  44. from bb.fetch2 import logger
  45. from bb.fetch2 import runfetchcmd
  46. __pattern__ = re.compile(r'''
  47. \s* # Skip leading whitespace
  48. ssh:// # scheme
  49. ( # Optional username/password block
  50. (?P<user>\S+) # username
  51. (:(?P<pass>\S+))? # colon followed by the password (optional)
  52. )?
  53. (?P<cparam>(;[^;]+)*)? # connection parameters block (optional)
  54. @
  55. (?P<host>\S+?) # non-greedy match of the host
  56. (:(?P<port>[0-9]+))? # colon followed by the port (optional)
  57. /
  58. (?P<path>[^;]+) # path on the remote system, may be absolute or relative,
  59. # and may include the use of '~' to reference the remote home
  60. # directory
  61. (?P<sparam>(;[^;]+)*)? # parameters block (optional)
  62. $
  63. ''', re.VERBOSE)
  64. class SSH(FetchMethod):
  65. '''Class to fetch a module or modules via Secure Shell'''
  66. def supports(self, urldata, d):
  67. return __pattern__.match(urldata.url) != None
  68. def supports_checksum(self, urldata):
  69. return False
  70. def urldata_init(self, urldata, d):
  71. if 'protocol' in urldata.parm and urldata.parm['protocol'] == 'git':
  72. raise bb.fetch2.ParameterError(
  73. "Invalid protocol - if you wish to fetch from a git " +
  74. "repository using ssh, you need to use " +
  75. "git:// prefix with protocol=ssh", urldata.url)
  76. m = __pattern__.match(urldata.url)
  77. path = m.group('path')
  78. host = m.group('host')
  79. urldata.localpath = os.path.join(d.getVar('DL_DIR'),
  80. os.path.basename(os.path.normpath(path)))
  81. def download(self, urldata, d):
  82. dldir = d.getVar('DL_DIR')
  83. m = __pattern__.match(urldata.url)
  84. path = m.group('path')
  85. host = m.group('host')
  86. port = m.group('port')
  87. user = m.group('user')
  88. password = m.group('pass')
  89. if port:
  90. portarg = '-P %s' % port
  91. else:
  92. portarg = ''
  93. if user:
  94. fr = user
  95. if password:
  96. fr += ':%s' % password
  97. fr += '@%s' % host
  98. else:
  99. fr = host
  100. fr += ':%s' % path
  101. cmd = 'scp -B -r %s %s %s/' % (
  102. portarg,
  103. fr,
  104. dldir
  105. )
  106. bb.fetch2.check_network_access(d, cmd, urldata.url)
  107. runfetchcmd(cmd, d)