keys.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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,
  39. ) error {
  40. // Let's check the keys individually, and then we can offer to import
  41. // the problematic ones.
  42. problematic := make(pgpKeySet)
  43. args := append(strings.Fields(gpgFlags), "--list-keys")
  44. // Mapping all the keys.
  45. for _, base := range bases {
  46. pkg := base.Pkgbase()
  47. srcinfo := srcinfos[pkg]
  48. for _, key := range srcinfo.ValidPGPKeys {
  49. // If key already marked as problematic, indicate the current
  50. // PKGBUILD requires it.
  51. if problematic.get(key) {
  52. problematic.set(key, base)
  53. continue
  54. }
  55. cmd := exec.Command(gpgBin, append(args, key)...)
  56. if err := cmd.Run(); 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(os.Stdin, 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. if err := cmd.Run(); err != nil {
  83. return errors.New(gotext.Get("problem importing keys"))
  84. }
  85. return nil
  86. }
  87. // formatKeysToImport receives a set of keys and returns a string containing the
  88. // question asking the user wants to import the problematic keys.
  89. func formatKeysToImport(keys pgpKeySet) (string, error) {
  90. if len(keys) == 0 {
  91. return "", errors.New(gotext.Get("no keys to import"))
  92. }
  93. var buffer bytes.Buffer
  94. buffer.WriteString(text.SprintOperationInfo(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("\n" + text.SprintWarn(gotext.Get("%s, required by: %s", text.Cyan(key), text.Cyan(pkglist))))
  102. }
  103. return buffer.String(), nil
  104. }