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