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