keys.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/v11/pkg/dep"
  12. "github.com/Jguer/yay/v11/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. if err := cmd.Run(); err != nil {
  56. problematic.set(key, base)
  57. }
  58. }
  59. }
  60. // No key issues!
  61. if len(problematic) == 0 {
  62. return nil
  63. }
  64. str, err := formatKeysToImport(problematic)
  65. if err != nil {
  66. return err
  67. }
  68. fmt.Println()
  69. fmt.Println(str)
  70. if text.ContinueTask(gotext.Get("Import?"), true, noConfirm) {
  71. return importKeys(problematic.toSlice(), gpgBin, gpgFlags)
  72. }
  73. return nil
  74. }
  75. // importKeys tries to import the list of keys specified in its argument.
  76. func importKeys(keys []string, gpgBin, gpgFlags string) error {
  77. args := append(strings.Fields(gpgFlags), "--recv-keys")
  78. cmd := exec.Command(gpgBin, append(args, keys...)...)
  79. cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
  80. text.OperationInfoln(gotext.Get("Importing keys with gpg..."))
  81. if err := cmd.Run(); 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(text.SprintOperationInfo(gotext.Get("PGP keys need importing:")))
  94. for key, bases := range keys {
  95. pkglist := ""
  96. for _, base := range bases {
  97. pkglist += base.String() + " "
  98. }
  99. pkglist = strings.TrimRight(pkglist, " ")
  100. buffer.WriteString("\n" + text.SprintWarn(gotext.Get("%s, required by: %s", text.Cyan(key), text.Cyan(pkglist))))
  101. }
  102. return buffer.String(), nil
  103. }