forked from bogue1979/gpws
/
gpws.go
237 lines (207 loc) · 5.07 KB
/
gpws.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"os"
"code.google.com/p/gopass"
"github.com/atotto/clipboard"
)
var (
StoreFile = flag.String("file", defaultStoreFile, "Password Store File")
del = flag.String("delete", "", "delete Entry")
update = flag.String("update", "", "update Entry")
add = flag.String("add", "", "add Password Entry")
home string = os.Getenv("HOME")
storeDir string = home + "/.gpws"
helpdocument string = `
Examples:
List Entries:
gpws
Add Entry in MyPasswordStore:
gpws -file MyPasswordStore -add entry
Delete entry:
gpws -delete entry
Update entry:
gpws -update entry
Get password for entry into clipboard:
gpws entry
`
)
const (
defaultStoreFile = "Store"
)
func init() {
_, err := os.Stat(storeDir)
if os.IsNotExist(err) {
err = os.Mkdir(storeDir, 0700)
if err != nil {
fmt.Println("Error create storedir:", err)
}
}
}
type record struct {
Name, User string
Pass []byte
}
func Newrecord(name, user string, pass []byte) record {
return record{
Name: name,
User: user,
Pass: pass,
}
}
func RecordInput(masterKey []byte, name string) (record record, err error) {
user, err := UserInput("User: ")
if err != nil {
return record, err
}
plainpass, err := gopass.GetPass("Password: ")
if err != nil {
return record, fmt.Errorf("Error in GetPass: %s", err)
}
ciphertext, err := encrypt(masterKey, plainpass)
if err != nil {
return record, fmt.Errorf("Error in encrypt Password: %s", err)
}
record = Newrecord(name, user, []byte(ciphertext))
return record, nil
}
func UserInput(prompt string) (text string, err error) {
reader := bufio.NewReader(os.Stdin)
fmt.Print(prompt)
text, err = reader.ReadString('\n')
if err != nil {
return text, err
}
return text[:len(text)-1], nil
}
func PaddingKey(masterPass string) (padkey []byte, err error) {
var b bytes.Buffer
b.WriteString(masterPass)
if len(b.String()) > 32 {
return padkey, fmt.Errorf("Password can be max 32 bytes but is %d bytes", len(b.String()))
}
// append a until 32 bytes
for i := len(b.String()); i < 32; i++ {
b.WriteString("a")
}
return b.Bytes(), err
}
func MasterKeys() (masterKey []byte, masterPass string, err error) {
masterPass, err = gopass.GetPass("Gimme MasterPassword: ")
if err != nil {
return masterKey, masterPass, fmt.Errorf("Error in Password input:", err)
}
masterKey, err = PaddingKey(masterPass)
if err != nil {
return masterKey, masterPass, fmt.Errorf("Error in Password input:", err)
}
return masterKey, masterPass, nil
}
func CheckMasterPass(masterKey []byte, masterPass string, cipherpass string) bool {
plainpass, err := decrypt(masterKey, cipherpass)
if err != nil {
fmt.Println("Error decrypting Password in self")
return false
}
if plainpass == masterPass {
return true
}
fmt.Println("Wrong MasterPassword")
return false
}
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
fmt.Print(helpdocument)
}
flag.Parse()
//validate flags
if (*add != "" && *del != "") || (*add != "" && *update != "") || (*update != "" && *del != "") {
fmt.Println("Error: -add, -delete and -update are exclusive parameters\n")
flag.Usage()
os.Exit(1)
}
if (*add != "" || *del != "" || *update != "") && len(flag.Args()) > 0 {
flag.Usage()
os.Exit(1)
}
s := NewPasswordStore(storeDir + "/" + *StoreFile)
defer s.Save(storeDir + "/" + *StoreFile)
// check password
selfentry, err := s.Get("self")
if err != nil {
fmt.Println("Error getting self", err)
os.Exit(1)
}
masterKey, masterPass, err := MasterKeys()
if err != nil {
fmt.Println("Error getting MasterKeys:", err)
os.Exit(1)
}
if CheckMasterPass(masterKey, masterPass, string(selfentry.Pass)) {
fmt.Println("Password OK")
} else {
os.Exit(1)
}
// add
if *add != "" {
if s.Exists(*add) {
fmt.Printf("Entry %s already exists!\n", *add)
os.Exit(0)
}
newrecord, err := RecordInput(masterKey, *add)
if err != nil {
fmt.Println(err)
}
if err = s.Set(newrecord.Name, newrecord); err != nil {
fmt.Println("Error adding Entry:", err)
}
}
//del
if *del != "" {
if *del == "self" {
fmt.Printf("Entry self is Master Key for Password Store\nAbort now\n")
os.Exit(1)
}
err = s.Delete(*del)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
//update
if *update != "" {
if err = s.Delete(*update); err != nil {
fmt.Println(err)
os.Exit(1)
}
newrecord, err := RecordInput(masterKey, *update)
if err != nil {
fmt.Println(err)
}
if err = s.Set(newrecord.Name, newrecord); err != nil {
fmt.Println("Error adding Entry:", err)
}
}
//get
if len(flag.Args()) == 1 {
record, err := s.Get(flag.Arg(0))
if err != nil {
fmt.Println(err)
os.Exit(0)
}
plainpass, err := decrypt(masterKey, string(record.Pass))
fmt.Printf("Username for %s is %s\n copy password into clipboard\n", flag.Arg(0), record.User)
if err = clipboard.WriteAll(plainpass); err != nil {
fmt.Println("Error: copy password into clipboard", err)
os.Exit(1)
}
os.Exit(0)
}
// list
s.Print()
}