keys.go 3.2 KB

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