2018-03-11 04:00:45 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
|
2018-07-16 16:28:18 +02:00
|
|
|
gosrc "github.com/Morganamilo/go-srcinfo"
|
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.
|
2018-08-10 05:40:27 +02:00
|
|
|
type pgpKeySet map[string][]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)
|
|
|
|
}
|
|
|
|
return slice
|
|
|
|
}
|
|
|
|
|
2018-08-10 05:40:27 +02:00
|
|
|
func (set pgpKeySet) set(key string, p 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)
|
2018-08-10 05:40:27 +02:00
|
|
|
set[key] = 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]
|
|
|
|
return exists
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2018-08-10 05:40:27 +02:00
|
|
|
func checkPgpKeys(bases []Base, srcinfos map[string]*gosrc.Srcinfo) 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)
|
2018-03-17 21:40:24 +01:00
|
|
|
args := append(strings.Fields(config.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
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command(config.GpgBin, append(args, key)...)
|
|
|
|
err := cmd.Run()
|
|
|
|
if 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)
|
|
|
|
|
2018-07-22 01:46:55 +02:00
|
|
|
if continueTask(bold(green("Import?")), true) {
|
2018-03-17 21:40:24 +01:00
|
|
|
return importKeys(problematic.toSlice())
|
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.
|
|
|
|
func importKeys(keys []string) error {
|
|
|
|
args := append(strings.Fields(config.GpgFlags), "--recv-keys")
|
2018-03-11 04:00:45 +01:00
|
|
|
cmd := exec.Command(config.GpgBin, append(args, keys...)...)
|
|
|
|
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
|
|
|
|
|
2018-04-11 05:43:51 +02:00
|
|
|
fmt.Printf("%s %s...\n", bold(cyan("::")), bold("Importing keys with gpg..."))
|
2018-03-11 04:00:45 +01:00
|
|
|
err := cmd.Run()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s Problem importing keys", bold(red(arrow+" Error:")))
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
return "", fmt.Errorf("%s No keys to import", bold(red(arrow+" Error:")))
|
|
|
|
}
|
|
|
|
|
|
|
|
var buffer bytes.Buffer
|
2018-04-11 05:43:51 +02:00
|
|
|
buffer.WriteString(bold(green(arrow)))
|
|
|
|
buffer.WriteString(bold(green(" PGP keys need importing:")))
|
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
|
|
|
}
|
2018-08-10 05:40:27 +02:00
|
|
|
pkglist = strings.TrimRight(pkglist, " ")
|
2018-04-11 05:43:51 +02:00
|
|
|
buffer.WriteString(fmt.Sprintf("\n%s %s, required by: %s", yellow(bold(smallArrow)), cyan(key), cyan(pkglist)))
|
2018-03-11 04:00:45 +01:00
|
|
|
}
|
|
|
|
return buffer.String(), nil
|
|
|
|
}
|