2020-07-11 00:48:30 +02:00
|
|
|
package pgp
|
2018-03-11 04:00:45 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-05-04 09:24:32 +02:00
|
|
|
"errors"
|
2018-03-11 04:00:45 +01:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
|
2018-07-16 16:28:18 +02:00
|
|
|
gosrc "github.com/Morganamilo/go-srcinfo"
|
2020-05-04 09:24:32 +02:00
|
|
|
"github.com/leonelquinteros/gotext"
|
|
|
|
|
2021-09-08 22:28:08 +02:00
|
|
|
"github.com/Jguer/yay/v11/pkg/dep"
|
|
|
|
"github.com/Jguer/yay/v11/pkg/text"
|
2018-03-11 04:00:45 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// pgpKeySet maps a PGP key with a list of PKGBUILDs that require it.
|
|
|
|
// This is similar to stringSet, used throughout the code.
|
2020-07-10 02:36:45 +02:00
|
|
|
type pgpKeySet map[string][]dep.Base
|
2018-03-11 04:00:45 +01:00
|
|
|
|
|
|
|
func (set pgpKeySet) toSlice() []string {
|
|
|
|
slice := make([]string, 0, len(set))
|
|
|
|
for v := range set {
|
|
|
|
slice = append(slice, v)
|
|
|
|
}
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2018-03-11 04:00:45 +01:00
|
|
|
return slice
|
|
|
|
}
|
|
|
|
|
2020-07-10 02:36:45 +02:00
|
|
|
func (set pgpKeySet) set(key string, p dep.Base) {
|
2018-03-11 04:00:45 +01:00
|
|
|
// Using ToUpper to make sure keys with a different case will be
|
|
|
|
// considered the same.
|
|
|
|
upperKey := strings.ToUpper(key)
|
2020-05-02 16:17:20 +02:00
|
|
|
set[upperKey] = append(set[upperKey], p)
|
2018-03-11 04:00:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (set pgpKeySet) get(key string) bool {
|
|
|
|
upperKey := strings.ToUpper(key)
|
|
|
|
_, exists := set[upperKey]
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2018-03-11 04:00:45 +01:00
|
|
|
return exists
|
|
|
|
}
|
|
|
|
|
2020-07-11 00:48:30 +02:00
|
|
|
// CheckPgpKeys iterates through the keys listed in the PKGBUILDs and if needed,
|
2018-03-17 21:40:24 +01:00
|
|
|
// asks the user whether yay should try to import them.
|
2020-07-11 00:48:30 +02:00
|
|
|
func CheckPgpKeys(bases []dep.Base, srcinfos map[string]*gosrc.Srcinfo,
|
2022-08-14 00:56:23 +02:00
|
|
|
gpgBin, gpgFlags string, noConfirm bool,
|
|
|
|
) error {
|
2018-03-11 04:00:45 +01:00
|
|
|
// Let's check the keys individually, and then we can offer to import
|
|
|
|
// the problematic ones.
|
|
|
|
problematic := make(pgpKeySet)
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2020-07-11 00:48:30 +02:00
|
|
|
args := append(strings.Fields(gpgFlags), "--list-keys")
|
2018-03-11 04:00:45 +01:00
|
|
|
|
|
|
|
// Mapping all the keys.
|
2018-08-10 05:40:27 +02:00
|
|
|
for _, base := range bases {
|
|
|
|
pkg := base.Pkgbase()
|
|
|
|
srcinfo := srcinfos[pkg]
|
2018-03-17 01:10:38 +01:00
|
|
|
|
2018-07-16 16:28:18 +02:00
|
|
|
for _, key := range srcinfo.ValidPGPKeys {
|
2018-03-11 04:00:45 +01:00
|
|
|
// If key already marked as problematic, indicate the current
|
|
|
|
// PKGBUILD requires it.
|
|
|
|
if problematic.get(key) {
|
2018-08-10 05:40:27 +02:00
|
|
|
problematic.set(key, base)
|
2018-03-11 04:00:45 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-07-11 00:48:30 +02:00
|
|
|
cmd := exec.Command(gpgBin, append(args, key)...)
|
2021-08-11 20:13:28 +02:00
|
|
|
if err := cmd.Run(); err != nil {
|
2018-08-10 05:40:27 +02:00
|
|
|
problematic.set(key, base)
|
2018-03-11 04:00:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No key issues!
|
|
|
|
if len(problematic) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-10 05:40:27 +02:00
|
|
|
str, err := formatKeysToImport(problematic)
|
2018-03-11 04:00:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-11 05:43:51 +02:00
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
fmt.Println(str)
|
|
|
|
|
2022-08-14 00:56:23 +02:00
|
|
|
if text.ContinueTask(os.Stdin, gotext.Get("Import?"), true, noConfirm) {
|
2020-07-11 00:48:30 +02:00
|
|
|
return importKeys(problematic.toSlice(), gpgBin, gpgFlags)
|
2018-03-11 04:00:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-17 21:40:24 +01:00
|
|
|
// importKeys tries to import the list of keys specified in its argument.
|
2020-07-11 00:48:30 +02:00
|
|
|
func importKeys(keys []string, gpgBin, gpgFlags string) error {
|
|
|
|
args := append(strings.Fields(gpgFlags), "--recv-keys")
|
|
|
|
cmd := exec.Command(gpgBin, append(args, keys...)...)
|
2018-03-11 04:00:45 +01:00
|
|
|
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
|
|
|
|
|
2020-05-04 09:24:32 +02:00
|
|
|
text.OperationInfoln(gotext.Get("Importing keys with gpg..."))
|
2021-08-11 20:13:28 +02:00
|
|
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
2020-05-04 09:24:32 +02:00
|
|
|
return errors.New(gotext.Get("problem importing keys"))
|
2018-03-11 04:00:45 +01:00
|
|
|
}
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2018-03-11 04:00:45 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// formatKeysToImport receives a set of keys and returns a string containing the
|
|
|
|
// question asking the user wants to import the problematic keys.
|
2018-08-10 05:40:27 +02:00
|
|
|
func formatKeysToImport(keys pgpKeySet) (string, error) {
|
2018-03-11 04:00:45 +01:00
|
|
|
if len(keys) == 0 {
|
2020-05-04 09:24:32 +02:00
|
|
|
return "", errors.New(gotext.Get("no keys to import"))
|
2018-03-11 04:00:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var buffer bytes.Buffer
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2020-07-11 00:48:30 +02:00
|
|
|
buffer.WriteString(text.SprintOperationInfo(gotext.Get("PGP keys need importing:")))
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2018-08-10 05:40:27 +02:00
|
|
|
for key, bases := range keys {
|
2018-03-11 04:00:45 +01:00
|
|
|
pkglist := ""
|
2018-08-10 05:40:27 +02:00
|
|
|
for _, base := range bases {
|
2018-08-11 06:39:24 +02:00
|
|
|
pkglist += base.String() + " "
|
2018-03-11 04:00:45 +01:00
|
|
|
}
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2019-04-23 18:03:09 +02:00
|
|
|
pkglist = strings.TrimRight(pkglist, " ")
|
2020-07-11 00:48:30 +02:00
|
|
|
buffer.WriteString("\n" + text.SprintWarn(gotext.Get("%s, required by: %s", text.Cyan(key), text.Cyan(pkglist))))
|
2018-03-11 04:00:45 +01:00
|
|
|
}
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2018-03-11 04:00:45 +01:00
|
|
|
return buffer.String(), nil
|
|
|
|
}
|