keys.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/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][]string
  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, p string) {
  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(pkgbuildDirsByBase map[string]string, srcinfos map[string]*gosrc.Srcinfo,
  37. gpgBin, gpgFlags string, noConfirm bool,
  38. ) ([]string, 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 pkg := range pkgbuildDirsByBase {
  45. srcinfo := srcinfos[pkg]
  46. for _, key := range srcinfo.ValidPGPKeys {
  47. // If key already marked as problematic, indicate the current
  48. // PKGBUILD requires it.
  49. if problematic.get(key) {
  50. problematic.set(key, pkg)
  51. continue
  52. }
  53. cmd := exec.Command(gpgBin, append(args, key)...)
  54. if err := cmd.Run(); err != nil {
  55. problematic.set(key, pkg)
  56. }
  57. }
  58. }
  59. // No key issues!
  60. if len(problematic) == 0 {
  61. return []string{}, nil
  62. }
  63. str, err := formatKeysToImport(problematic)
  64. if err != nil {
  65. return nil, err
  66. }
  67. fmt.Println()
  68. fmt.Println(str)
  69. if text.ContinueTask(os.Stdin, gotext.Get("Import?"), true, noConfirm) {
  70. return problematic.toSlice(), importKeys(problematic.toSlice(), gpgBin, gpgFlags)
  71. }
  72. return problematic.toSlice(), nil
  73. }
  74. // importKeys tries to import the list of keys specified in its argument.
  75. func importKeys(keys []string, gpgBin, gpgFlags string) error {
  76. args := append(strings.Fields(gpgFlags), "--recv-keys")
  77. cmd := exec.Command(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. if err := cmd.Run(); err != nil {
  81. return errors.New(gotext.Get("problem importing keys"))
  82. }
  83. return nil
  84. }
  85. // formatKeysToImport receives a set of keys and returns a string containing the
  86. // question asking the user wants to import the problematic keys.
  87. func formatKeysToImport(keys pgpKeySet) (string, error) {
  88. if len(keys) == 0 {
  89. return "", errors.New(gotext.Get("no keys to import"))
  90. }
  91. var buffer bytes.Buffer
  92. buffer.WriteString(text.SprintOperationInfo(gotext.Get("PGP keys need importing:")))
  93. for key, bases := range keys {
  94. pkglist := ""
  95. for _, base := range bases {
  96. pkglist += base + " "
  97. }
  98. pkglist = strings.TrimRight(pkglist, " ")
  99. buffer.WriteString("\n" + text.SprintWarn(gotext.Get("%s, required by: %s", text.Cyan(key), text.Cyan(pkglist))))
  100. }
  101. return buffer.String(), nil
  102. }