2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-22 10:58:55 +00:00

Cleanup, remove unused commands

This commit is contained in:
Alexander Neumann 2014-10-05 16:17:40 +02:00
parent 09702c1c61
commit bb5bfe1ba0
5 changed files with 0 additions and 610 deletions

View File

@ -1,81 +0,0 @@
package main
import (
"fmt"
"os"
"github.com/fd0/khepri"
"github.com/fd0/khepri/backend"
)
const pass = "foobar"
func main() {
if len(os.Args) != 3 {
fmt.Fprintf(os.Stderr, "usage: archive REPO DIR\n")
os.Exit(1)
}
repo := os.Args[1]
dir := os.Args[2]
// fmt.Printf("import %s into backend %s\n", dir, repo)
var (
be backend.Server
key *khepri.Key
)
be, err := backend.OpenLocal(repo)
if err != nil {
fmt.Printf("creating %s\n", repo)
be, err = backend.CreateLocal(repo)
if err != nil {
fmt.Fprintf(os.Stderr, "failed: %v\n", err)
os.Exit(2)
}
key, err = khepri.CreateKey(be, pass)
if err != nil {
fmt.Fprintf(os.Stderr, "failed: %v\n", err)
os.Exit(2)
}
}
key, err = khepri.SearchKey(be, pass)
if err != nil {
fmt.Fprintf(os.Stderr, "failed: %v\n", err)
os.Exit(2)
}
arch, err := khepri.NewArchiver(be, key)
if err != nil {
fmt.Fprintf(os.Stderr, "err: %v\n", err)
}
arch.Error = func(dir string, fi os.FileInfo, err error) error {
fmt.Fprintf(os.Stderr, "error for %s: %v\n%s\n", dir, err, fi)
return err
}
arch.Filter = func(item string, fi os.FileInfo) bool {
// if fi.IsDir() {
// if fi.Name() == ".svn" {
// return false
// }
// } else {
// if filepath.Ext(fi.Name()) == ".bz2" {
// return false
// }
// }
fmt.Printf("%s\n", item)
return true
}
_, blob, err := arch.Import(dir)
if err != nil {
fmt.Fprintf(os.Stderr, "Import() error: %v\n", err)
os.Exit(2)
}
fmt.Printf("saved as %+v\n", blob)
}

View File

@ -1,126 +0,0 @@
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
"time"
"code.google.com/p/go.crypto/ssh/terminal"
"github.com/fd0/khepri"
"github.com/fd0/khepri/backend"
)
func read_password(prompt string) string {
p := os.Getenv("KHEPRI_PASSWORD")
if p != "" {
return p
}
fmt.Print(prompt)
pw, err := terminal.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
fmt.Fprintf(os.Stderr, "unable to read password: %v", err)
os.Exit(2)
}
fmt.Println()
return string(pw)
}
func json_pp(data []byte) error {
var buf bytes.Buffer
err := json.Indent(&buf, data, "", " ")
if err != nil {
return err
}
fmt.Println(string(buf.Bytes()))
return nil
}
type StopWatch struct {
start, last time.Time
}
func NewStopWatch() *StopWatch {
return &StopWatch{
start: time.Now(),
last: time.Now(),
}
}
func (s *StopWatch) Next(format string, data ...interface{}) {
t := time.Now()
d := t.Sub(s.last)
s.last = t
arg := make([]interface{}, len(data)+1)
arg[0] = d
copy(arg[1:], data)
fmt.Printf("[%s]: "+format+"\n", arg...)
}
func main() {
if len(os.Args) != 3 {
fmt.Fprintf(os.Stderr, "usage: cat REPO ID\n")
os.Exit(1)
}
repo := os.Args[1]
id, err := backend.ParseID(filepath.Base(os.Args[2]))
if err != nil {
panic(err)
}
s := NewStopWatch()
be, err := backend.OpenLocal(repo)
if err != nil {
fmt.Fprintf(os.Stderr, "failed: %v\n", err)
os.Exit(1)
}
s.Next("OpenLocal()")
key, err := khepri.SearchKey(be, read_password("Enter Password for Repository: "))
if err != nil {
fmt.Fprintf(os.Stderr, "failed: %v\n", err)
os.Exit(2)
}
s.Next("SearchKey()")
// try all possible types
for _, t := range []backend.Type{backend.Blob, backend.Snapshot, backend.Lock, backend.Tree, backend.Key} {
buf, err := be.Get(t, id)
if err != nil {
continue
}
s.Next("Get(%s, %s)", t, id)
if t == backend.Key {
json_pp(buf)
}
buf2, err := key.Decrypt(buf)
if err != nil {
panic(err)
}
if t == backend.Blob {
// directly output blob
fmt.Println(string(buf2))
} else {
// try to uncompress and print as idented json
err = json_pp(backend.Uncompress(buf2))
if err != nil {
fmt.Fprintf(os.Stderr, "failed: %v\n", err)
}
}
break
}
}

View File

@ -1,233 +0,0 @@
package main
import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"code.google.com/p/go.crypto/scrypt"
"code.google.com/p/go.crypto/ssh/terminal"
"github.com/jessevdk/go-flags"
)
const (
scrypt_N = 65536
scrypt_r = 8
scrypt_p = 1
aesKeySize = 32 // for AES256
)
var Opts struct {
Password string `short:"p" long:"password" description:"Password for the file"`
Keys string `short:"k" long:"keys" description:"Keys for the file (encryption_key || sign_key, hex-encoded)"`
Salt string `short:"s" long:"salt" description:"Salt to use (hex-encoded)"`
}
func newIV() ([]byte, error) {
buf := make([]byte, aes.BlockSize)
_, err := io.ReadFull(rand.Reader, buf)
if err != nil {
return nil, err
}
return buf, nil
}
func pad(plaintext []byte) []byte {
l := aes.BlockSize - (len(plaintext) % aes.BlockSize)
if l == 0 {
l = aes.BlockSize
}
if l <= 0 || l > aes.BlockSize {
panic("invalid padding size")
}
return append(plaintext, bytes.Repeat([]byte{byte(l)}, l)...)
}
func unpad(plaintext []byte) []byte {
l := len(plaintext)
pad := plaintext[l-1]
if pad > aes.BlockSize {
panic(errors.New("padding > BlockSize"))
}
if pad == 0 {
panic(errors.New("invalid padding 0"))
}
for i := l - int(pad); i < l; i++ {
if plaintext[i] != pad {
panic(errors.New("invalid padding!"))
}
}
return plaintext[:l-int(pad)]
}
// Encrypt encrypts and signs data. Returned is IV || Ciphertext || HMAC. For
// the hash function, SHA256 is used, so the overhead is 16+32=48 byte.
func Encrypt(ekey, skey []byte, plaintext []byte) ([]byte, error) {
iv, err := newIV()
if err != nil {
panic(fmt.Sprintf("unable to generate new random iv: %v", err))
}
c, err := aes.NewCipher(ekey)
if err != nil {
panic(fmt.Sprintf("unable to create cipher: %v", err))
}
e := cipher.NewCBCEncrypter(c, iv)
p := pad(plaintext)
ciphertext := make([]byte, len(p))
e.CryptBlocks(ciphertext, p)
ciphertext = append(iv, ciphertext...)
hm := hmac.New(sha256.New, skey)
n, err := hm.Write(ciphertext)
if err != nil || n != len(ciphertext) {
panic(fmt.Sprintf("unable to calculate hmac of ciphertext: %v", err))
}
return hm.Sum(ciphertext), nil
}
// Decrypt verifes and decrypts the ciphertext. Ciphertext must be in the form
// IV || Ciphertext || HMAC.
func Decrypt(ekey, skey []byte, ciphertext []byte) ([]byte, error) {
hm := hmac.New(sha256.New, skey)
// extract hmac
l := len(ciphertext) - hm.Size()
ciphertext, mac := ciphertext[:l], ciphertext[l:]
// calculate new hmac
n, err := hm.Write(ciphertext)
if err != nil || n != len(ciphertext) {
panic(fmt.Sprintf("unable to calculate hmac of ciphertext, err %v", err))
}
// verify hmac
mac2 := hm.Sum(nil)
if !hmac.Equal(mac, mac2) {
panic("HMAC verification failed")
}
// extract iv
iv, ciphertext := ciphertext[:aes.BlockSize], ciphertext[aes.BlockSize:]
// decrypt data
c, err := aes.NewCipher(ekey)
if err != nil {
panic(fmt.Sprintf("unable to create cipher: %v", err))
}
// decrypt
e := cipher.NewCBCDecrypter(c, iv)
plaintext := make([]byte, len(ciphertext))
e.CryptBlocks(plaintext, ciphertext)
// remove padding and return
return unpad(plaintext), nil
}
func errx(code int, format string, data ...interface{}) {
if len(format) > 0 && format[len(format)-1] != '\n' {
format += "\n"
}
fmt.Fprintf(os.Stderr, format, data...)
os.Exit(code)
}
func read_password(prompt string) string {
p := os.Getenv("KHEPRI_PASSWORD")
if p != "" {
return p
}
fmt.Print(prompt)
pw, err := terminal.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
errx(2, "unable to read password: %v", err)
}
fmt.Println()
return string(pw)
}
func main() {
args, err := flags.Parse(&Opts)
if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp {
os.Exit(0)
}
var keys []byte
if Opts.Password == "" && Opts.Keys == "" {
Opts.Password = read_password("password: ")
salt, err := hex.DecodeString(Opts.Salt)
if err != nil {
errx(1, "unable to hex-decode salt: %v", err)
}
keys, err = scrypt.Key([]byte(Opts.Password), salt, scrypt_N, scrypt_r, scrypt_p, 2*aesKeySize)
if err != nil {
errx(1, "scrypt: %v", err)
}
}
if Opts.Keys != "" {
keys, err = hex.DecodeString(Opts.Keys)
if err != nil {
errx(1, "unable to hex-decode keys: %v", err)
}
}
if len(keys) != 2*aesKeySize {
errx(2, "key length is not 512")
}
encrypt_key := keys[:aesKeySize]
sign_key := keys[aesKeySize:]
for _, filename := range args {
f, err := os.Open(filename)
defer f.Close()
if err != nil {
errx(3, "%v\n", err)
}
buf, err := ioutil.ReadAll(f)
if err != nil {
errx(3, "%v\n", err)
}
buf, err = Decrypt(encrypt_key, sign_key, buf)
if err != nil {
errx(3, "%v\n", err)
}
_, err = os.Stdout.Write(buf)
if err != nil {
errx(3, "%v\n", err)
}
}
}

View File

@ -1,110 +0,0 @@
package main
import (
"encoding/json"
"fmt"
"os"
"code.google.com/p/go.crypto/ssh/terminal"
"github.com/fd0/khepri"
"github.com/fd0/khepri/backend"
)
func read_password(prompt string) string {
p := os.Getenv("KHEPRI_PASSWORD")
if p != "" {
return p
}
fmt.Print(prompt)
pw, err := terminal.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
fmt.Fprintf(os.Stderr, "unable to read password: %v", err)
os.Exit(2)
}
fmt.Println()
return string(pw)
}
func list(be backend.Server, key *khepri.Key, t backend.Type) {
ids, err := be.List(t)
if err != nil {
fmt.Fprintf(os.Stderr, "failed: %v\n", err)
os.Exit(3)
}
for _, id := range ids {
buf, err := be.Get(t, id)
if err != nil {
fmt.Fprintf(os.Stderr, "unable to get snapshot %s: %v\n", id, err)
continue
}
if t != backend.Key && t != backend.Blob {
buf, err = key.Decrypt(buf)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
continue
}
}
if t == backend.Snapshot {
var sn khepri.Snapshot
err = json.Unmarshal(backend.Uncompress(buf), &sn)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
continue
}
fmt.Printf("%s %s\n", id, sn.String())
} else if t == backend.Blob {
fmt.Printf("%s %d bytes (encrypted)\n", id, len(buf))
} else if t == backend.Tree {
fmt.Printf("%s\n", backend.Hash(buf))
} else if t == backend.Key {
k := &khepri.Key{}
err = json.Unmarshal(buf, k)
if err != nil {
fmt.Fprintf(os.Stderr, "unable to unmashal key: %v\n", err)
continue
}
fmt.Println(key)
} else if t == backend.Lock {
fmt.Printf("lock: %v\n", id)
}
}
}
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "usage: archive REPO\n")
os.Exit(1)
}
repo := os.Args[1]
be, err := backend.OpenLocal(repo)
if err != nil {
fmt.Fprintf(os.Stderr, "failed: %v\n", err)
os.Exit(1)
}
key, err := khepri.SearchKey(be, read_password("Enter Password for Repository: "))
if err != nil {
fmt.Fprintf(os.Stderr, "failed: %v\n", err)
os.Exit(2)
}
fmt.Printf("keys:\n")
list(be, key, backend.Key)
fmt.Printf("---\nlocks:\n")
list(be, key, backend.Lock)
fmt.Printf("---\nsnapshots:\n")
list(be, key, backend.Snapshot)
fmt.Printf("---\ntrees:\n")
list(be, key, backend.Tree)
fmt.Printf("---\nblobs:\n")
list(be, key, backend.Blob)
}

View File

@ -1,60 +0,0 @@
package main
import (
"crypto/sha256"
"fmt"
"io"
"os"
"github.com/fd0/khepri/chunker"
)
func main() {
count, bytes := 0, 0
min := 0
max := 0
var (
err error
file *os.File = os.Stdin
)
if len(os.Args) > 1 {
file, err = os.Open(os.Args[1])
if err != nil {
panic(err)
}
}
ch := chunker.New(file)
for {
chunk, err := ch.Next()
if err == io.EOF {
break
}
if err != nil {
panic(err)
}
fmt.Printf("%d %016x %02x\n", chunk.Length, chunk.Cut, sha256.Sum256(chunk.Data))
count++
bytes += chunk.Length
if chunk.Length == chunker.MaxSize {
max++
} else if chunk.Length == chunker.MinSize {
min++
}
}
var avg int
if count > 0 {
avg = bytes / count
}
fmt.Fprintf(os.Stderr, "%d chunks from %d bytes, average size %d (%d min size, %d max size chunks)\n",
count, bytes, avg, min, max)
}