Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | ''' |
| 2 | BitBake 'Fetch' implementations |
| 3 | |
| 4 | This implementation is for Secure Shell (SSH), and attempts to comply with the |
| 5 | IETF 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 Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame^] | 30 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 31 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 32 | |
| 33 | import re, os |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 34 | from bb.fetch2 import FetchMethod |
| 35 | from bb.fetch2 import FetchError |
| 36 | from bb.fetch2 import logger |
| 37 | from bb.fetch2 import runfetchcmd |
| 38 | |
| 39 | |
| 40 | __pattern__ = re.compile(r''' |
| 41 | \s* # Skip leading whitespace |
| 42 | ssh:// # scheme |
| 43 | ( # Optional username/password block |
| 44 | (?P<user>\S+) # username |
| 45 | (:(?P<pass>\S+))? # colon followed by the password (optional) |
| 46 | )? |
| 47 | (?P<cparam>(;[^;]+)*)? # connection parameters block (optional) |
| 48 | @ |
| 49 | (?P<host>\S+?) # non-greedy match of the host |
| 50 | (:(?P<port>[0-9]+))? # colon followed by the port (optional) |
| 51 | / |
| 52 | (?P<path>[^;]+) # path on the remote system, may be absolute or relative, |
| 53 | # and may include the use of '~' to reference the remote home |
| 54 | # directory |
| 55 | (?P<sparam>(;[^;]+)*)? # parameters block (optional) |
| 56 | $ |
| 57 | ''', re.VERBOSE) |
| 58 | |
| 59 | class SSH(FetchMethod): |
| 60 | '''Class to fetch a module or modules via Secure Shell''' |
| 61 | |
| 62 | def supports(self, urldata, d): |
| 63 | return __pattern__.match(urldata.url) != None |
| 64 | |
| 65 | def supports_checksum(self, urldata): |
| 66 | return False |
| 67 | |
| 68 | def urldata_init(self, urldata, d): |
| 69 | if 'protocol' in urldata.parm and urldata.parm['protocol'] == 'git': |
| 70 | raise bb.fetch2.ParameterError( |
| 71 | "Invalid protocol - if you wish to fetch from a git " + |
| 72 | "repository using ssh, you need to use " + |
| 73 | "git:// prefix with protocol=ssh", urldata.url) |
| 74 | m = __pattern__.match(urldata.url) |
| 75 | path = m.group('path') |
| 76 | host = m.group('host') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 77 | urldata.localpath = os.path.join(d.getVar('DL_DIR'), |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 78 | os.path.basename(os.path.normpath(path))) |
| 79 | |
| 80 | def download(self, urldata, d): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 81 | dldir = d.getVar('DL_DIR') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 82 | |
| 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 | |
| 90 | if port: |
| 91 | portarg = '-P %s' % port |
| 92 | else: |
| 93 | portarg = '' |
| 94 | |
| 95 | if user: |
| 96 | fr = user |
| 97 | if password: |
| 98 | fr += ':%s' % password |
| 99 | fr += '@%s' % host |
| 100 | else: |
| 101 | fr = host |
| 102 | fr += ':%s' % path |
| 103 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 104 | cmd = 'scp -B -r %s %s %s/' % ( |
| 105 | portarg, |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 106 | fr, |
| 107 | dldir |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 108 | ) |
| 109 | |
| 110 | bb.fetch2.check_network_access(d, cmd, urldata.url) |
| 111 | |
| 112 | runfetchcmd(cmd, d) |
| 113 | |