keys.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package main
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "os"
  7. "os/exec"
  8. "strings"
  9. gosrc "github.com/Morganamilo/go-srcinfo"
  10. "github.com/leonelquinteros/gotext"
  11. "github.com/Jguer/yay/v9/pkg/text"
  12. )
  13. // pgpKeySet maps a PGP key with a list of PKGBUILDs that require it.
  14. // This is similar to stringSet, used throughout the code.
  15. type pgpKeySet map[string][]Base
  16. func (set pgpKeySet) toSlice() []string {
  17. slice := make([]string, 0, len(set))
  18. for v := range set {
  19. slice = append(slice, v)
  20. }
  21. return slice
  22. }
  23. func (set pgpKeySet) set(key string, p Base) {
  24. // Using ToUpper to make sure keys with a different case will be
  25. // considered the same.
  26. upperKey := strings.ToUpper(key)
  27. set[upperKey] = append(set[upperKey], p)
  28. }
  29. func (set pgpKeySet) get(key string) bool {
  30. upperKey := strings.ToUpper(key)
  31. _, exists := set[upperKey]
  32. return exists
  33. }
  34. // checkPgpKeys iterates through the keys listed in the PKGBUILDs and if needed,
  35. // asks the user whether yay should try to import them.
  36. func checkPgpKeys(bases []Base, srcinfos map[string]*gosrc.Srcinfo) error {
  37. // Let's check the keys individually, and then we can offer to import
  38. // the problematic ones.
  39. problematic := make(pgpKeySet)
  40. args := append(strings.Fields(config.GpgFlags), "--list-keys")
  41. // Mapping all the keys.
  42. for _, base := range bases {
  43. pkg := base.Pkgbase()
  44. srcinfo := srcinfos[pkg]
  45. for _, key := range srcinfo.ValidPGPKeys {
  46. // If key already marked as problematic, indicate the current
  47. // PKGBUILD requires it.
  48. if problematic.get(key) {
  49. problematic.set(key, base)
  50. continue
  51. }
  52. cmd := exec.Command(config.GpgBin, append(args, key)...)
  53. err := cmd.Run()
  54. if err != nil {
  55. problematic.set(key, base)
  56. }
  57. }
  58. }
  59. // No key issues!
  60. if len(problematic) == 0 {
  61. return nil
  62. }
  63. str, err := formatKeysToImport(problematic)
  64. if err != nil {
  65. return err
  66. }
  67. fmt.Println()
  68. fmt.Println(str)
  69. if continueTask(gotext.Get("Import?"), true) {
  70. return importKeys(problematic.toSlice())
  71. }
  72. return nil
  73. }
  74. // importKeys tries to import the list of keys specified in its argument.
  75. func importKeys(keys []string) error {
  76. args := append(strings.Fields(config.GpgFlags), "--recv-keys")
  77. cmd := exec.Command(config.GpgBin, append(args, keys...)...)
  78. cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
  79. text.OperationInfoln(gotext.Get("Importing keys with gpg..."))
  80. err := cmd.Run()
  81. if err != nil {
  82. return errors.New(gotext.Get("problem importing keys"))
  83. }
  84. return nil
  85. }
  86. // formatKeysToImport receives a set of keys and returns a string containing the
  87. // question asking the user wants to import the problematic keys.
  88. func formatKeysToImport(keys pgpKeySet) (string, error) {
  89. if len(keys) == 0 {
  90. return "", errors.New(gotext.Get("no keys to import"))
  91. }
  92. var buffer bytes.Buffer
  93. buffer.WriteString(bold(green(arrow) + " "))
  94. buffer.WriteString(bold(green(gotext.Get("PGP keys need importing:"))))
  95. for key, bases := range keys {
  96. pkglist := ""
  97. for _, base := range bases {
  98. pkglist += base.String() + " "
  99. }
  100. pkglist = strings.TrimRight(pkglist, " ")
  101. buffer.WriteString(gotext.Get("\n%s %s, required by: %s", yellow(bold(smallArrow)), cyan(key), cyan(pkglist)))
  102. }
  103. return buffer.String(), nil
  104. }