2014-11-25 22:52:53 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-08-28 22:19:48 +02:00
|
|
|
"restic"
|
2016-08-21 17:46:23 +02:00
|
|
|
|
2016-09-01 22:17:37 +02:00
|
|
|
"restic/errors"
|
2016-02-14 15:29:28 +01:00
|
|
|
"restic/repository"
|
2014-11-25 22:52:53 +01:00
|
|
|
)
|
|
|
|
|
2015-06-21 13:02:56 +02:00
|
|
|
type CmdKey struct {
|
2015-06-21 15:01:52 +02:00
|
|
|
global *GlobalOptions
|
|
|
|
newPassword string
|
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() {
|
2014-12-07 16:30:52 +01:00
|
|
|
_, err := parser.AddCommand("key",
|
|
|
|
"manage keys",
|
|
|
|
"The key command manages keys (passwords) of a repository",
|
2015-06-21 13:02:56 +02:00
|
|
|
&CmdKey{global: &globalOpts})
|
2014-12-07 16:30:52 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-11-30 22:39:58 +01:00
|
|
|
}
|
|
|
|
|
2015-06-21 13:02:56 +02:00
|
|
|
func (cmd CmdKey) listKeys(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
|
|
|
|
2016-09-01 16:04:29 +02:00
|
|
|
plen, err := s.PrefixLength(restic.KeyFile)
|
2014-11-25 22:52:53 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-28 11:50:23 +01:00
|
|
|
done := make(chan struct{})
|
|
|
|
defer close(done)
|
|
|
|
|
2016-09-01 16:04:29 +02:00
|
|
|
for id := range s.List(restic.KeyFile, done) {
|
2015-05-17 20:48:59 +02:00
|
|
|
k, err := repository.LoadKey(s, id.String())
|
2014-11-25 22:52:53 +01:00
|
|
|
if err != nil {
|
2015-06-21 13:27:56 +02:00
|
|
|
cmd.global.Warnf("LoadKey() failed: %v\n", err)
|
2015-03-28 11:50:23 +01:00
|
|
|
continue
|
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 = " "
|
|
|
|
}
|
2015-05-17 20:48:59 +02:00
|
|
|
tab.Rows = append(tab.Rows, []interface{}{current, id.String()[:plen],
|
2014-11-25 22:52:53 +01:00
|
|
|
k.Username, k.Hostname, k.Created.Format(TimeFormat)})
|
2015-03-28 11:50:23 +01:00
|
|
|
}
|
2014-11-25 22:52:53 +01:00
|
|
|
|
2015-06-21 13:27:56 +02:00
|
|
|
return tab.Write(cmd.global.stdout)
|
2014-11-25 22:52:53 +01:00
|
|
|
}
|
|
|
|
|
2015-06-21 15:01:52 +02:00
|
|
|
func (cmd CmdKey) getNewPassword() string {
|
|
|
|
if cmd.newPassword != "" {
|
|
|
|
return cmd.newPassword
|
2014-11-25 23:07:00 +01:00
|
|
|
}
|
|
|
|
|
2015-06-21 15:01:52 +02:00
|
|
|
return cmd.global.ReadPasswordTwice(
|
|
|
|
"enter password for new key: ",
|
|
|
|
"enter password again: ")
|
2015-05-09 21:50:10 +02:00
|
|
|
}
|
|
|
|
|
2015-06-21 13:02:56 +02:00
|
|
|
func (cmd CmdKey) addKey(repo *repository.Repository) error {
|
2015-06-21 15:01:52 +02:00
|
|
|
id, err := repository.AddKey(repo, cmd.getNewPassword(), 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
|
|
|
}
|
|
|
|
|
2015-06-21 13:27:56 +02:00
|
|
|
cmd.global.Verbosef("saved new key as %s\n", id)
|
2014-11-25 23:07:00 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-21 13:02:56 +02:00
|
|
|
func (cmd CmdKey) deleteKey(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
|
|
|
}
|
|
|
|
|
2016-09-01 16:04:29 +02:00
|
|
|
err := repo.Backend().Remove(restic.KeyFile, name)
|
2014-11-25 23:18:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-21 13:27:56 +02:00
|
|
|
cmd.global.Verbosef("removed key %v\n", name)
|
2014-11-25 23:18:02 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-21 13:02:56 +02:00
|
|
|
func (cmd CmdKey) changePassword(repo *repository.Repository) error {
|
2015-06-21 15:01:52 +02:00
|
|
|
id, err := repository.AddKey(repo, cmd.getNewPassword(), 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
|
|
|
}
|
|
|
|
|
2016-09-01 16:04:29 +02:00
|
|
|
err = repo.Backend().Remove(restic.KeyFile, repo.KeyName())
|
2014-11-25 23:23:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-21 13:27:56 +02:00
|
|
|
cmd.global.Verbosef("saved new key as %s\n", id)
|
2014-11-25 23:23:09 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-07 16:30:52 +01:00
|
|
|
func (cmd CmdKey) Usage() string {
|
2015-04-25 02:42:52 -04:00
|
|
|
return "[list|add|rm|passwd] [ID]"
|
2014-12-07 16:30:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cmd CmdKey) Execute(args []string) error {
|
2014-11-25 23:18:02 +01:00
|
|
|
if len(args) < 1 || (args[0] == "rm" && len(args) != 2) {
|
2016-09-01 22:17:37 +02:00
|
|
|
return errors.Fatalf("wrong number of arguments, Usage: %s", cmd.Usage())
|
2014-12-07 16:30:52 +01:00
|
|
|
}
|
|
|
|
|
2015-06-27 14:36:46 +02:00
|
|
|
repo, err := cmd.global.OpenRepository()
|
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
|
|
|
|
}
|
|
|
|
|
2015-06-27 14:36:46 +02:00
|
|
|
return cmd.listKeys(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
|
|
|
|
}
|
|
|
|
|
2015-06-27 14:36:46 +02:00
|
|
|
return cmd.addKey(repo)
|
2014-11-25 23:18:02 +01:00
|
|
|
case "rm":
|
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
|
|
|
|
}
|
|
|
|
|
2015-06-27 14:36:46 +02:00
|
|
|
return cmd.deleteKey(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
|
|
|
|
}
|
|
|
|
|
2015-06-27 14:36:46 +02:00
|
|
|
return cmd.changePassword(repo)
|
2014-11-25 22:52:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|