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 |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 34 | from bb.fetch2 import check_network_access, FetchMethod, ParameterError, runfetchcmd |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 35 | |
| 36 | |
| 37 | __pattern__ = re.compile(r''' |
| 38 | \s* # Skip leading whitespace |
| 39 | ssh:// # scheme |
| 40 | ( # Optional username/password block |
| 41 | (?P<user>\S+) # username |
| 42 | (:(?P<pass>\S+))? # colon followed by the password (optional) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 43 | (?P<cparam>(;[^;]+)*)? # connection parameters block (optional) |
| 44 | @ |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame^] | 45 | )? |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 46 | (?P<host>\S+?) # non-greedy match of the host |
| 47 | (:(?P<port>[0-9]+))? # colon followed by the port (optional) |
| 48 | / |
| 49 | (?P<path>[^;]+) # path on the remote system, may be absolute or relative, |
| 50 | # and may include the use of '~' to reference the remote home |
| 51 | # directory |
| 52 | (?P<sparam>(;[^;]+)*)? # parameters block (optional) |
| 53 | $ |
| 54 | ''', re.VERBOSE) |
| 55 | |
| 56 | class SSH(FetchMethod): |
| 57 | '''Class to fetch a module or modules via Secure Shell''' |
| 58 | |
| 59 | def supports(self, urldata, d): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 60 | return __pattern__.match(urldata.url) is not None |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 61 | |
| 62 | def supports_checksum(self, urldata): |
| 63 | return False |
| 64 | |
| 65 | def urldata_init(self, urldata, d): |
| 66 | if 'protocol' in urldata.parm and urldata.parm['protocol'] == 'git': |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 67 | raise ParameterError( |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 68 | "Invalid protocol - if you wish to fetch from a git " + |
| 69 | "repository using ssh, you need to use " + |
| 70 | "git:// prefix with protocol=ssh", urldata.url) |
| 71 | m = __pattern__.match(urldata.url) |
| 72 | path = m.group('path') |
| 73 | host = m.group('host') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 74 | urldata.localpath = os.path.join(d.getVar('DL_DIR'), |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 75 | os.path.basename(os.path.normpath(path))) |
| 76 | |
| 77 | def download(self, urldata, d): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 78 | dldir = d.getVar('DL_DIR') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 79 | |
| 80 | m = __pattern__.match(urldata.url) |
| 81 | path = m.group('path') |
| 82 | host = m.group('host') |
| 83 | port = m.group('port') |
| 84 | user = m.group('user') |
| 85 | password = m.group('pass') |
| 86 | |
| 87 | if port: |
| 88 | portarg = '-P %s' % port |
| 89 | else: |
| 90 | portarg = '' |
| 91 | |
| 92 | if user: |
| 93 | fr = user |
| 94 | if password: |
| 95 | fr += ':%s' % password |
| 96 | fr += '@%s' % host |
| 97 | else: |
| 98 | fr = host |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame^] | 99 | |
| 100 | if path[0] != '~': |
| 101 | path = '/%s' % path |
| 102 | path = path.replace("%3A", ":") |
| 103 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 104 | fr += ':%s' % path |
| 105 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 106 | cmd = 'scp -B -r %s %s %s/' % ( |
| 107 | portarg, |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 108 | fr, |
| 109 | dldir |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 110 | ) |
| 111 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 112 | check_network_access(d, cmd, urldata.url) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 113 | |
| 114 | runfetchcmd(cmd, d) |
| 115 | |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame^] | 116 | def checkstatus(self, fetch, urldata, d): |
| 117 | """ |
| 118 | Check the status of the url |
| 119 | """ |
| 120 | m = __pattern__.match(urldata.url) |
| 121 | path = m.group('path') |
| 122 | host = m.group('host') |
| 123 | port = m.group('port') |
| 124 | user = m.group('user') |
| 125 | password = m.group('pass') |
| 126 | |
| 127 | if port: |
| 128 | portarg = '-P %s' % port |
| 129 | else: |
| 130 | portarg = '' |
| 131 | |
| 132 | if user: |
| 133 | fr = user |
| 134 | if password: |
| 135 | fr += ':%s' % password |
| 136 | fr += '@%s' % host |
| 137 | else: |
| 138 | fr = host |
| 139 | |
| 140 | if path[0] != '~': |
| 141 | path = '/%s' % path |
| 142 | path = path.replace("%3A", ":") |
| 143 | |
| 144 | cmd = 'ssh -o BatchMode=true %s %s [ -f %s ]' % ( |
| 145 | portarg, |
| 146 | fr, |
| 147 | path |
| 148 | ) |
| 149 | |
| 150 | check_network_access(d, cmd, urldata.url) |
| 151 | |
| 152 | if runfetchcmd(cmd, d): |
| 153 | return True |
| 154 | |
| 155 | return False |