2014-11-25 22:52:53 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-03-08 20:17:30 +01:00
|
|
|
"context"
|
2014-11-25 22:52:53 +01:00
|
|
|
"fmt"
|
2017-07-23 14:21:03 +02:00
|
|
|
|
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
"github.com/restic/restic/internal/repository"
|
2017-07-24 17:42:25 +02:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2017-03-08 20:17:30 +01:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2014-11-25 22:52:53 +01:00
|
|
|
)
|
|
|
|
|
2016-09-17 12:36:05 +02:00
|
|
|
var cmdKey = &cobra.Command{
|
2017-08-17 11:03:26 -03:00
|
|
|
Use: "key [list|add|remove|passwd] [ID]",
|
2017-09-11 09:32:44 -07:00
|
|
|
Short: "Manage keys (passwords)",
|
2016-09-17 12:36:05 +02:00
|
|
|
Long: `
|
2017-02-13 16:05:25 +01:00
|
|
|
The "key" command manages keys (passwords) for accessing the repository.
|
2016-09-17 12:36:05 +02:00
|
|
|
`,
|
2017-08-06 21:02:16 +02:00
|
|
|
DisableAutoGenTag: true,
|
2016-09-17 12:36:05 +02:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runKey(globalOptions, args)
|
|
|
|
},
|
2015-06-21 13:02:56 +02:00
|
|
|
}
|
2014-12-07 16:30:52 +01:00
|
|
|
|
2014-11-30 22:39:58 +01:00
|
|
|
func init() {
|
2016-09-17 12:36:05 +02:00
|
|
|
cmdRoot.AddCommand(cmdKey)
|
2014-11-30 22:39:58 +01:00
|
|
|
}
|
|
|
|
|
2017-03-08 20:17:30 +01:00
|
|
|
func listKeys(ctx context.Context, s *repository.Repository) error {
|
2014-11-25 22:52:53 +01:00
|
|
|
tab := NewTable()
|
2014-11-27 23:26:19 +01:00
|
|
|
tab.Header = fmt.Sprintf(" %-10s %-10s %-10s %s", "ID", "User", "Host", "Created")
|
|
|
|
tab.RowFormat = "%s%-10s %-10s %-10s %s"
|
2014-11-25 22:52:53 +01:00
|
|
|
|
2018-01-21 17:25:36 +01:00
|
|
|
err := s.List(ctx, restic.KeyFile, func(id restic.ID, size int64) error {
|
2017-06-04 11:16:55 +02:00
|
|
|
k, err := repository.LoadKey(ctx, s, id.String())
|
2014-11-25 22:52:53 +01:00
|
|
|
if err != nil {
|
2016-09-17 12:36:05 +02:00
|
|
|
Warnf("LoadKey() failed: %v\n", err)
|
2018-01-21 17:25:36 +01:00
|
|
|
return nil
|
2014-11-25 22:52:53 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 23:26:19 +01:00
|
|
|
var current string
|
2015-05-17 20:48:59 +02:00
|
|
|
if id.String() == s.KeyName() {
|
2014-11-27 23:26:19 +01:00
|
|
|
current = "*"
|
|
|
|
} else {
|
|
|
|
current = " "
|
|
|
|
}
|
2016-09-17 12:36:05 +02:00
|
|
|
tab.Rows = append(tab.Rows, []interface{}{current, id.Str(),
|
2014-11-25 22:52:53 +01:00
|
|
|
k.Username, k.Hostname, k.Created.Format(TimeFormat)})
|
2018-01-21 17:25:36 +01:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-03-28 11:50:23 +01:00
|
|
|
}
|
2014-11-25 22:52:53 +01:00
|
|
|
|
2016-09-17 12:36:05 +02:00
|
|
|
return tab.Write(globalOptions.stdout)
|
2014-11-25 22:52:53 +01:00
|
|
|
}
|
|
|
|
|
2016-09-17 12:36:05 +02:00
|
|
|
// testKeyNewPassword is used to set a new password during integration testing.
|
|
|
|
var testKeyNewPassword string
|
|
|
|
|
|
|
|
func getNewPassword(gopts GlobalOptions) (string, error) {
|
|
|
|
if testKeyNewPassword != "" {
|
|
|
|
return testKeyNewPassword, nil
|
2014-11-25 23:07:00 +01:00
|
|
|
}
|
|
|
|
|
2017-07-24 23:01:16 +02:00
|
|
|
// Since we already have an open repository, temporary remove the password
|
|
|
|
// to prompt the user for the passwd.
|
2017-07-24 22:00:44 +02:00
|
|
|
newopts := gopts
|
|
|
|
newopts.password = ""
|
|
|
|
|
|
|
|
return ReadPasswordTwice(newopts,
|
2015-06-21 15:01:52 +02:00
|
|
|
"enter password for new key: ",
|
|
|
|
"enter password again: ")
|
2015-05-09 21:50:10 +02:00
|
|
|
}
|
|
|
|
|
2016-09-17 12:36:05 +02:00
|
|
|
func addKey(gopts GlobalOptions, repo *repository.Repository) error {
|
|
|
|
pw, err := getNewPassword(gopts)
|
2016-09-12 14:08:51 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
use global context for check, debug, dump, find, forget, init, key,
list, mount, tag, unlock commands
gh-1434
2017-12-06 07:02:55 -05:00
|
|
|
id, err := repository.AddKey(gopts.ctx, repo, pw, repo.Key())
|
2014-11-25 23:07:00 +01:00
|
|
|
if err != nil {
|
2016-09-01 22:17:37 +02:00
|
|
|
return errors.Fatalf("creating new key failed: %v\n", err)
|
2014-11-25 23:07:00 +01:00
|
|
|
}
|
|
|
|
|
2016-09-17 12:36:05 +02:00
|
|
|
Verbosef("saved new key as %s\n", id)
|
2014-11-25 23:07:00 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
use global context for check, debug, dump, find, forget, init, key,
list, mount, tag, unlock commands
gh-1434
2017-12-06 07:02:55 -05:00
|
|
|
func deleteKey(ctx context.Context, repo *repository.Repository, name string) error {
|
2015-05-09 13:32:52 +02:00
|
|
|
if name == repo.KeyName() {
|
2016-09-01 22:17:37 +02:00
|
|
|
return errors.Fatal("refusing to remove key currently used to access repository")
|
2014-11-25 23:18:02 +01:00
|
|
|
}
|
|
|
|
|
2017-01-25 17:48:35 +01:00
|
|
|
h := restic.Handle{Type: restic.KeyFile, Name: name}
|
use global context for check, debug, dump, find, forget, init, key,
list, mount, tag, unlock commands
gh-1434
2017-12-06 07:02:55 -05:00
|
|
|
err := repo.Backend().Remove(ctx, h)
|
2014-11-25 23:18:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-17 12:36:05 +02:00
|
|
|
Verbosef("removed key %v\n", name)
|
2014-11-25 23:18:02 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-17 12:36:05 +02:00
|
|
|
func changePassword(gopts GlobalOptions, repo *repository.Repository) error {
|
|
|
|
pw, err := getNewPassword(gopts)
|
2016-09-12 14:08:51 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
use global context for check, debug, dump, find, forget, init, key,
list, mount, tag, unlock commands
gh-1434
2017-12-06 07:02:55 -05:00
|
|
|
id, err := repository.AddKey(gopts.ctx, repo, pw, repo.Key())
|
2014-11-25 23:23:09 +01:00
|
|
|
if err != nil {
|
2016-09-01 22:17:37 +02:00
|
|
|
return errors.Fatalf("creating new key failed: %v\n", err)
|
2014-11-25 23:23:09 +01:00
|
|
|
}
|
|
|
|
|
2017-01-25 17:48:35 +01:00
|
|
|
h := restic.Handle{Type: restic.KeyFile, Name: repo.KeyName()}
|
use global context for check, debug, dump, find, forget, init, key,
list, mount, tag, unlock commands
gh-1434
2017-12-06 07:02:55 -05:00
|
|
|
err = repo.Backend().Remove(gopts.ctx, h)
|
2014-11-25 23:23:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-17 12:36:05 +02:00
|
|
|
Verbosef("saved new key as %s\n", id)
|
2014-11-25 23:23:09 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-17 12:36:05 +02:00
|
|
|
func runKey(gopts GlobalOptions, args []string) error {
|
2017-08-17 11:03:26 -03:00
|
|
|
if len(args) < 1 || (args[0] == "remove" && len(args) != 2) || (args[0] != "remove" && len(args) != 1) {
|
2017-02-10 19:39:49 +01:00
|
|
|
return errors.Fatal("wrong number of arguments")
|
2014-12-07 16:30:52 +01:00
|
|
|
}
|
|
|
|
|
2017-03-08 20:17:30 +01:00
|
|
|
ctx, cancel := context.WithCancel(gopts.ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
2016-09-17 12:36:05 +02:00
|
|
|
repo, err := OpenRepository(gopts)
|
2014-12-07 16:30:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-11-25 22:52:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch args[0] {
|
|
|
|
case "list":
|
2015-06-27 15:06:41 +02:00
|
|
|
lock, err := lockRepo(repo)
|
|
|
|
defer unlockRepo(lock)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-08 20:17:30 +01:00
|
|
|
return listKeys(ctx, repo)
|
2014-11-25 23:07:00 +01:00
|
|
|
case "add":
|
2015-06-27 15:06:41 +02:00
|
|
|
lock, err := lockRepo(repo)
|
|
|
|
defer unlockRepo(lock)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-17 12:36:05 +02:00
|
|
|
return addKey(gopts, repo)
|
2017-08-17 11:03:26 -03:00
|
|
|
case "remove":
|
2015-06-27 15:06:41 +02:00
|
|
|
lock, err := lockRepoExclusive(repo)
|
|
|
|
defer unlockRepo(lock)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-01 16:04:29 +02:00
|
|
|
id, err := restic.Find(repo.Backend(), restic.KeyFile, args[1])
|
2014-11-25 23:18:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
use global context for check, debug, dump, find, forget, init, key,
list, mount, tag, unlock commands
gh-1434
2017-12-06 07:02:55 -05:00
|
|
|
return deleteKey(gopts.ctx, repo, id)
|
2015-04-25 02:42:52 -04:00
|
|
|
case "passwd":
|
2015-06-27 15:06:41 +02:00
|
|
|
lock, err := lockRepoExclusive(repo)
|
|
|
|
defer unlockRepo(lock)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-17 12:36:05 +02:00
|
|
|
return changePassword(gopts, repo)
|
2014-11-25 22:52:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|