sftp.py 4.2 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 SFTP Fetch implementation
  5. Class for fetching files via SFTP. It tries to adhere to the (now
  6. expired) IETF Internet Draft for "Uniform Resource Identifier (URI)
  7. Scheme for Secure File Transfer Protocol (SFTP) and Secure Shell
  8. (SSH)" (SECSH URI).
  9. It uses SFTP (as to adhere to the SECSH URI specification). It only
  10. supports key based authentication, not password. This class, unlike
  11. the SSH fetcher, does not support fetching a directory tree from the
  12. remote.
  13. http://tools.ietf.org/html/draft-ietf-secsh-scp-sftp-ssh-uri-04
  14. https://www.iana.org/assignments/uri-schemes/prov/sftp
  15. https://tools.ietf.org/html/draft-ietf-secsh-filexfer-13
  16. Please note that '/' is used as host path seperator, and not ":"
  17. as you may be used to from the scp/sftp commands. You can use a
  18. ~ (tilde) to specify a path relative to your home directory.
  19. (The /~user/ syntax, for specyfing a path relative to another
  20. user's home directory is not supported.) Note that the tilde must
  21. still follow the host path seperator ("/"). See exampels below.
  22. Example SRC_URIs:
  23. SRC_URI = "sftp://host.example.com/dir/path.file.txt"
  24. A path relative to your home directory.
  25. SRC_URI = "sftp://host.example.com/~/dir/path.file.txt"
  26. You can also specify a username (specyfing password in the
  27. URI is not supported, use SSH keys to authenticate):
  28. SRC_URI = "sftp://user@host.example.com/dir/path.file.txt"
  29. """
  30. # Copyright (C) 2013, Olof Johansson <olof.johansson@axis.com>
  31. #
  32. # Based in part on bb.fetch2.wget:
  33. # Copyright (C) 2003, 2004 Chris Larson
  34. #
  35. # SPDX-License-Identifier: GPL-2.0-only
  36. #
  37. # This program is free software; you can redistribute it and/or modify
  38. # it under the terms of the GNU General Public License version 2 as
  39. # published by the Free Software Foundation.
  40. #
  41. # This program is distributed in the hope that it will be useful,
  42. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  43. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  44. # GNU General Public License for more details.
  45. #
  46. # You should have received a copy of the GNU General Public License along
  47. # with this program; if not, write to the Free Software Foundation, Inc.,
  48. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  49. #
  50. # Based on functions from the base bb module, Copyright 2003 Holger Schurig
  51. import os
  52. import bb
  53. import urllib.request, urllib.parse, urllib.error
  54. from bb.fetch2 import URI
  55. from bb.fetch2 import FetchMethod
  56. from bb.fetch2 import runfetchcmd
  57. class SFTP(FetchMethod):
  58. """Class to fetch urls via 'sftp'"""
  59. def supports(self, ud, d):
  60. """
  61. Check to see if a given url can be fetched with sftp.
  62. """
  63. return ud.type in ['sftp']
  64. def recommends_checksum(self, urldata):
  65. return True
  66. def urldata_init(self, ud, d):
  67. if 'protocol' in ud.parm and ud.parm['protocol'] == 'git':
  68. raise bb.fetch2.ParameterError(
  69. "Invalid protocol - if you wish to fetch from a " +
  70. "git repository using ssh, you need to use the " +
  71. "git:// prefix with protocol=ssh", ud.url)
  72. if 'downloadfilename' in ud.parm:
  73. ud.basename = ud.parm['downloadfilename']
  74. else:
  75. ud.basename = os.path.basename(ud.path)
  76. ud.localfile = d.expand(urllib.parse.unquote(ud.basename))
  77. def download(self, ud, d):
  78. """Fetch urls"""
  79. urlo = URI(ud.url)
  80. basecmd = 'sftp -oBatchMode=yes'
  81. port = ''
  82. if urlo.port:
  83. port = '-P %d' % urlo.port
  84. urlo.port = None
  85. dldir = d.getVar('DL_DIR')
  86. lpath = os.path.join(dldir, ud.localfile)
  87. user = ''
  88. if urlo.userinfo:
  89. user = urlo.userinfo + '@'
  90. path = urlo.path
  91. # Supoprt URIs relative to the user's home directory, with
  92. # the tilde syntax. (E.g. <sftp://example.com/~/foo.diff>).
  93. if path[:3] == '/~/':
  94. path = path[3:]
  95. remote = '%s%s:%s' % (user, urlo.hostname, path)
  96. cmd = '%s %s %s %s' % (basecmd, port, remote, lpath)
  97. bb.fetch2.check_network_access(d, cmd, ud.url)
  98. runfetchcmd(cmd, d)
  99. return True