blob: 5e982ecf38f922d99a656caf637d0b2e74afd419 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001'''
2BitBake 'Fetch' implementations
3
4This implementation is for Secure Shell (SSH), and attempts to comply with the
5IETF secsh internet draft:
6 http://tools.ietf.org/wg/secsh/draft-ietf-secsh-scp-sftp-ssh-uri/
7
8 Currently does not support the sftp parameters, as this uses scp
9 Also does not support the 'fingerprint' connection parameter.
10
11 Please note that '/' is used as host, path separator not ':' as you may
12 be used to, also '~' can be used to specify user HOME, but again after '/'
13
14 Example SRC_URI:
15 SRC_URI = "ssh://user@host.example.com/dir/path/file.txt"
16 SRC_URI = "ssh://user@host.example.com/~/file.txt"
17'''
18
19# Copyright (C) 2006 OpenedHand Ltd.
20#
21#
22# Based in part on svk.py:
23# Copyright (C) 2006 Holger Hans Peter Freyther
24# Based on svn.py:
25# Copyright (C) 2003, 2004 Chris Larson
26# Based on functions from the base bb module:
27# Copyright 2003 Holger Schurig
28#
29#
Brad Bishopc342db32019-05-15 21:57:59 -040030# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -050031#
Patrick Williamsc124f4f2015-09-15 14:41:29 -050032
33import re, os
Patrick Williamsc124f4f2015-09-15 14:41:29 -050034from bb.fetch2 import FetchMethod
Patrick Williamsc124f4f2015-09-15 14:41:29 -050035from bb.fetch2 import runfetchcmd
36
37
38__pattern__ = re.compile(r'''
39 \s* # Skip leading whitespace
40 ssh:// # scheme
41 ( # Optional username/password block
42 (?P<user>\S+) # username
43 (:(?P<pass>\S+))? # colon followed by the password (optional)
44 )?
45 (?P<cparam>(;[^;]+)*)? # connection parameters block (optional)
46 @
47 (?P<host>\S+?) # non-greedy match of the host
48 (:(?P<port>[0-9]+))? # colon followed by the port (optional)
49 /
50 (?P<path>[^;]+) # path on the remote system, may be absolute or relative,
51 # and may include the use of '~' to reference the remote home
52 # directory
53 (?P<sparam>(;[^;]+)*)? # parameters block (optional)
54 $
55''', re.VERBOSE)
56
57class SSH(FetchMethod):
58 '''Class to fetch a module or modules via Secure Shell'''
59
60 def supports(self, urldata, d):
Andrew Geissler82c905d2020-04-13 13:39:40 -050061 return __pattern__.match(urldata.url) is not None
Patrick Williamsc124f4f2015-09-15 14:41:29 -050062
63 def supports_checksum(self, urldata):
64 return False
65
66 def urldata_init(self, urldata, d):
67 if 'protocol' in urldata.parm and urldata.parm['protocol'] == 'git':
68 raise bb.fetch2.ParameterError(
69 "Invalid protocol - if you wish to fetch from a git " +
70 "repository using ssh, you need to use " +
71 "git:// prefix with protocol=ssh", urldata.url)
72 m = __pattern__.match(urldata.url)
73 path = m.group('path')
74 host = m.group('host')
Brad Bishop6e60e8b2018-02-01 10:27:11 -050075 urldata.localpath = os.path.join(d.getVar('DL_DIR'),
Patrick Williamsc124f4f2015-09-15 14:41:29 -050076 os.path.basename(os.path.normpath(path)))
77
78 def download(self, urldata, d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050079 dldir = d.getVar('DL_DIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050080
81 m = __pattern__.match(urldata.url)
82 path = m.group('path')
83 host = m.group('host')
84 port = m.group('port')
85 user = m.group('user')
86 password = m.group('pass')
87
88 if port:
89 portarg = '-P %s' % port
90 else:
91 portarg = ''
92
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
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500102 cmd = 'scp -B -r %s %s %s/' % (
103 portarg,
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600104 fr,
105 dldir
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500106 )
107
108 bb.fetch2.check_network_access(d, cmd, urldata.url)
109
110 runfetchcmd(cmd, d)
111