config.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. // License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package ssh
  3. import (
  4. "archive/tar"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "io/fs"
  9. "os"
  10. "path"
  11. "path/filepath"
  12. "strings"
  13. "time"
  14. "kitty/tools/config"
  15. "kitty/tools/utils"
  16. "kitty/tools/utils/paths"
  17. "kitty/tools/utils/shlex"
  18. "github.com/bmatcuk/doublestar/v4"
  19. "golang.org/x/sys/unix"
  20. )
  21. var _ = fmt.Print
  22. type EnvInstruction struct {
  23. key, val string
  24. delete_on_remote, copy_from_local, literal_quote bool
  25. }
  26. func quote_for_sh(val string, literal_quote bool) string {
  27. if literal_quote {
  28. return utils.QuoteStringForSH(val)
  29. }
  30. // See https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html
  31. b := strings.Builder{}
  32. b.Grow(len(val) + 16)
  33. b.WriteRune('"')
  34. runes := []rune(val)
  35. for i, ch := range runes {
  36. if ch == '\\' || ch == '`' || ch == '"' || (ch == '$' && i+1 < len(runes) && runes[i+1] == '(') {
  37. // special chars are escaped
  38. // $( is escaped to prevent execution
  39. b.WriteRune('\\')
  40. }
  41. b.WriteRune(ch)
  42. }
  43. b.WriteRune('"')
  44. return b.String()
  45. }
  46. func (self *EnvInstruction) Serialize(for_python bool, get_local_env func(string) (string, bool)) string {
  47. var unset func() string
  48. var export func(string) string
  49. if for_python {
  50. dumps := func(x ...any) string {
  51. ans, _ := json.Marshal(x)
  52. return utils.UnsafeBytesToString(ans)
  53. }
  54. export = func(val string) string {
  55. if val == "" {
  56. return fmt.Sprintf("export %s", dumps(self.key))
  57. }
  58. return fmt.Sprintf("export %s", dumps(self.key, val, self.literal_quote))
  59. }
  60. unset = func() string {
  61. return fmt.Sprintf("unset %s", dumps(self.key))
  62. }
  63. } else {
  64. kq := utils.QuoteStringForSH(self.key)
  65. unset = func() string {
  66. return fmt.Sprintf("unset %s", kq)
  67. }
  68. export = func(val string) string {
  69. return fmt.Sprintf("export %s=%s", kq, quote_for_sh(val, self.literal_quote))
  70. }
  71. }
  72. if self.delete_on_remote {
  73. return unset()
  74. }
  75. if self.copy_from_local {
  76. val, found := get_local_env(self.key)
  77. if !found {
  78. return ""
  79. }
  80. return export(val)
  81. }
  82. return export(self.val)
  83. }
  84. func final_env_instructions(for_python bool, get_local_env func(string) (string, bool), env ...*EnvInstruction) string {
  85. seen := make(map[string]int, len(env))
  86. ans := make([]string, 0, len(env))
  87. for _, ei := range env {
  88. q := ei.Serialize(for_python, get_local_env)
  89. if q != "" {
  90. if pos, found := seen[ei.key]; found {
  91. ans[pos] = q
  92. } else {
  93. seen[ei.key] = len(ans)
  94. ans = append(ans, q)
  95. }
  96. }
  97. }
  98. return strings.Join(ans, "\n")
  99. }
  100. type CopyInstruction struct {
  101. local_path, arcname string
  102. exclude_patterns []string
  103. }
  104. func ParseEnvInstruction(spec string) (ans []*EnvInstruction, err error) {
  105. const COPY_FROM_LOCAL string = "_kitty_copy_env_var_"
  106. ei := &EnvInstruction{}
  107. found := false
  108. ei.key, ei.val, found = strings.Cut(spec, "=")
  109. ei.key = strings.TrimSpace(ei.key)
  110. if found {
  111. ei.val = strings.TrimSpace(ei.val)
  112. if ei.val == COPY_FROM_LOCAL {
  113. ei.val = ""
  114. ei.copy_from_local = true
  115. }
  116. } else {
  117. ei.delete_on_remote = true
  118. }
  119. if ei.key == "" {
  120. err = fmt.Errorf("The env directive must not be empty")
  121. }
  122. ans = []*EnvInstruction{ei}
  123. return
  124. }
  125. var paths_ctx *paths.Ctx
  126. func resolve_file_spec(spec string, is_glob bool) ([]string, error) {
  127. if paths_ctx == nil {
  128. paths_ctx = &paths.Ctx{}
  129. }
  130. ans := os.ExpandEnv(paths_ctx.ExpandHome(spec))
  131. if !filepath.IsAbs(ans) {
  132. ans = paths_ctx.AbspathFromHome(ans)
  133. }
  134. if is_glob {
  135. files, err := doublestar.FilepathGlob(ans)
  136. if err != nil {
  137. return nil, fmt.Errorf("%s is not a valid glob pattern with error: %w", spec, err)
  138. }
  139. if len(files) == 0 {
  140. return nil, fmt.Errorf("%s matches no files", spec)
  141. }
  142. return files, nil
  143. }
  144. err := unix.Access(ans, unix.R_OK)
  145. if err != nil {
  146. if errors.Is(err, os.ErrNotExist) {
  147. return nil, fmt.Errorf("%s does not exist", spec)
  148. }
  149. return nil, fmt.Errorf("Cannot read from: %s with error: %w", spec, err)
  150. }
  151. return []string{ans}, nil
  152. }
  153. func get_arcname(loc, dest, home string) (arcname string) {
  154. if dest != "" {
  155. arcname = dest
  156. } else {
  157. arcname = filepath.Clean(loc)
  158. if strings.HasPrefix(arcname, home) {
  159. ra, err := filepath.Rel(home, arcname)
  160. if err == nil {
  161. arcname = ra
  162. }
  163. }
  164. }
  165. prefix := "home/"
  166. if strings.HasPrefix(arcname, "/") {
  167. prefix = "root"
  168. }
  169. return prefix + arcname
  170. }
  171. func ParseCopyInstruction(spec string) (ans []*CopyInstruction, err error) {
  172. args, err := shlex.Split("copy " + spec)
  173. if err != nil {
  174. return nil, err
  175. }
  176. opts, args, err := parse_copy_args(args)
  177. if err != nil {
  178. return nil, err
  179. }
  180. locations := make([]string, 0, len(args))
  181. for _, arg := range args {
  182. locs, err := resolve_file_spec(arg, opts.Glob)
  183. if err != nil {
  184. return nil, err
  185. }
  186. locations = append(locations, locs...)
  187. }
  188. if len(locations) == 0 {
  189. return nil, fmt.Errorf("No files to copy specified")
  190. }
  191. if len(locations) > 1 && opts.Dest != "" {
  192. return nil, fmt.Errorf("Specifying a remote location with more than one file is not supported")
  193. }
  194. home := paths_ctx.HomePath()
  195. ans = make([]*CopyInstruction, 0, len(locations))
  196. for _, loc := range locations {
  197. ci := CopyInstruction{local_path: loc, exclude_patterns: opts.Exclude}
  198. if opts.SymlinkStrategy != "preserve" {
  199. ci.local_path, err = filepath.EvalSymlinks(loc)
  200. if err != nil {
  201. return nil, fmt.Errorf("Failed to resolve symlinks in %#v with error: %w", loc, err)
  202. }
  203. }
  204. if opts.SymlinkStrategy == "resolve" {
  205. ci.arcname = get_arcname(ci.local_path, opts.Dest, home)
  206. } else {
  207. ci.arcname = get_arcname(loc, opts.Dest, home)
  208. }
  209. ans = append(ans, &ci)
  210. }
  211. return
  212. }
  213. type file_unique_id struct {
  214. dev, inode uint64
  215. }
  216. func excluded(pattern, path string) bool {
  217. if !strings.ContainsRune(pattern, '/') {
  218. path = filepath.Base(path)
  219. }
  220. if matched, err := doublestar.PathMatch(pattern, path); matched && err == nil {
  221. return true
  222. }
  223. return false
  224. }
  225. func get_file_data(callback func(h *tar.Header, data []byte) error, seen map[file_unique_id]string, local_path, arcname string, exclude_patterns []string) error {
  226. s, err := os.Lstat(local_path)
  227. if err != nil {
  228. return err
  229. }
  230. u, ok := s.Sys().(unix.Stat_t)
  231. cb := func(h *tar.Header, data []byte, arcname string) error {
  232. h.Name = arcname
  233. if h.Typeflag == tar.TypeDir {
  234. h.Name = strings.TrimRight(h.Name, "/") + "/"
  235. }
  236. h.Size = int64(len(data))
  237. h.Mode = int64(s.Mode().Perm())
  238. h.ModTime = s.ModTime()
  239. h.Format = tar.FormatPAX
  240. if ok {
  241. h.AccessTime = time.Unix(0, u.Atim.Nano())
  242. h.ChangeTime = time.Unix(0, u.Ctim.Nano())
  243. }
  244. return callback(h, data)
  245. }
  246. // we only copy regular files, directories and symlinks
  247. switch s.Mode().Type() {
  248. case fs.ModeSymlink:
  249. target, err := os.Readlink(local_path)
  250. if err != nil {
  251. return err
  252. }
  253. err = cb(&tar.Header{
  254. Typeflag: tar.TypeSymlink,
  255. Linkname: target,
  256. }, nil, arcname)
  257. if err != nil {
  258. return err
  259. }
  260. case fs.ModeDir:
  261. local_path = filepath.Clean(local_path)
  262. type entry struct {
  263. path, arcname string
  264. }
  265. stack := []entry{{local_path, arcname}}
  266. for len(stack) > 0 {
  267. x := stack[0]
  268. stack = stack[1:]
  269. entries, err := os.ReadDir(x.path)
  270. if err != nil {
  271. if x.path == local_path {
  272. return err
  273. }
  274. continue
  275. }
  276. err = cb(&tar.Header{Typeflag: tar.TypeDir}, nil, x.arcname)
  277. if err != nil {
  278. return err
  279. }
  280. for _, e := range entries {
  281. entry_path := filepath.Join(x.path, e.Name())
  282. aname := path.Join(x.arcname, e.Name())
  283. ok := true
  284. for _, pat := range exclude_patterns {
  285. if excluded(pat, entry_path) {
  286. ok = false
  287. break
  288. }
  289. }
  290. if !ok {
  291. continue
  292. }
  293. if e.IsDir() {
  294. stack = append(stack, entry{entry_path, aname})
  295. } else {
  296. err = get_file_data(callback, seen, entry_path, aname, exclude_patterns)
  297. if err != nil {
  298. return err
  299. }
  300. }
  301. }
  302. }
  303. case 0: // Regular file
  304. fid := file_unique_id{dev: uint64(u.Dev), inode: uint64(u.Ino)}
  305. if prev, ok := seen[fid]; ok { // Hard link
  306. err = cb(&tar.Header{Typeflag: tar.TypeLink, Linkname: prev}, nil, arcname)
  307. if err != nil {
  308. return err
  309. }
  310. }
  311. seen[fid] = arcname
  312. data, err := os.ReadFile(local_path)
  313. if err != nil {
  314. return err
  315. }
  316. err = cb(&tar.Header{Typeflag: tar.TypeReg}, data, arcname)
  317. if err != nil {
  318. return err
  319. }
  320. }
  321. return nil
  322. }
  323. func (ci *CopyInstruction) get_file_data(callback func(h *tar.Header, data []byte) error, seen map[file_unique_id]string) (err error) {
  324. ep := ci.exclude_patterns
  325. for _, folder_name := range []string{"__pycache__", ".DS_Store"} {
  326. ep = append(ep, "**/"+folder_name, "**/"+folder_name+"/**")
  327. }
  328. return get_file_data(callback, seen, ci.local_path, ci.arcname, ep)
  329. }
  330. type ConfigSet struct {
  331. all_configs []*Config
  332. }
  333. func config_for_hostname(hostname_to_match, username_to_match string, cs *ConfigSet) *Config {
  334. matcher := func(q *Config) bool {
  335. for _, pat := range strings.Split(q.Hostname, " ") {
  336. upat := "*"
  337. if strings.Contains(pat, "@") {
  338. upat, pat, _ = strings.Cut(pat, "@")
  339. }
  340. var host_matched, user_matched bool
  341. if matched, err := filepath.Match(pat, hostname_to_match); matched && err == nil {
  342. host_matched = true
  343. }
  344. if matched, err := filepath.Match(upat, username_to_match); matched && err == nil {
  345. user_matched = true
  346. }
  347. if host_matched && user_matched {
  348. return true
  349. }
  350. }
  351. return false
  352. }
  353. for _, c := range utils.Reversed(cs.all_configs) {
  354. if matcher(c) {
  355. return c
  356. }
  357. }
  358. return cs.all_configs[0]
  359. }
  360. func (self *ConfigSet) line_handler(key, val string) error {
  361. c := self.all_configs[len(self.all_configs)-1]
  362. if key == "hostname" {
  363. c = NewConfig()
  364. self.all_configs = append(self.all_configs, c)
  365. }
  366. return c.Parse(key, val)
  367. }
  368. func load_config(hostname_to_match string, username_to_match string, overrides []string, paths ...string) (*Config, []config.ConfigLine, error) {
  369. ans := &ConfigSet{all_configs: []*Config{NewConfig()}}
  370. p := config.ConfigParser{LineHandler: ans.line_handler}
  371. err := p.LoadConfig("ssh.conf", paths, nil)
  372. if err != nil {
  373. return nil, nil, err
  374. }
  375. final_conf := config_for_hostname(hostname_to_match, username_to_match, ans)
  376. bad_lines := p.BadLines()
  377. if len(overrides) > 0 {
  378. h := final_conf.Hostname
  379. override_parser := config.ConfigParser{LineHandler: final_conf.Parse}
  380. if err = override_parser.ParseOverrides(overrides...); err != nil {
  381. return nil, nil, err
  382. }
  383. bad_lines = append(bad_lines, override_parser.BadLines()...)
  384. final_conf.Hostname = h
  385. }
  386. return final_conf, bad_lines, nil
  387. }