-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
301 lines (265 loc) · 7.19 KB
/
main.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/chzyer/readline"
homedir "github.com/mitchellh/go-homedir"
"github.com/renatoathaydes/go-hash/gohash_db"
"golang.org/x/crypto/ssh/terminal"
)
// exit gracefully if ctrl-C during password prompt
// https://groups.google.com/forum/#!topic/golang-nuts/kTVAbtee9UA
var initialState *terminal.State
func init() {
log.SetOutput(ioutil.Discard)
log.SetFlags(0)
// remember initial terminal state
var err error
if initialState, err = terminal.GetState(int(syscall.Stdin)); err != nil {
return
}
// and restore it on exit
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, os.Kill)
go func() {
<-c
println("")
_ = terminal.Restore(int(syscall.Stdin), initialState)
os.Exit(0)
}()
}
func getGoHashFilePath() string {
home, err := homedir.Expand("~/.go-hash")
if err != nil {
panic(err)
}
return home
}
func parentDirExists(path string) bool {
_, err := os.Stat(filepath.Dir(path))
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
panic("Cannot read directory path")
}
func isDir(path string) bool {
stat, err := os.Stat(path)
if err == nil {
return stat.IsDir()
}
return false
}
func createPassword() string {
for i := 0; i < 10; i++ {
print("Please enter a master password: ")
pass, err := terminal.ReadPassword(int(syscall.Stdin))
println("")
if err != nil {
panic(err)
}
if len(pass) > 7 {
for i := 0; i < 3; i++ {
print("Re-enter the password: ")
pass2, err := terminal.ReadPassword(int(syscall.Stdin))
println("")
if err != nil {
panic(err)
}
if len(pass2) == 0 {
break
}
if bytes.Equal(pass, pass2) {
return string(pass)
}
println("No match! Try again or just hit Enter to start again.")
}
} else {
println("Password too short! Please use at least 8 characters")
}
}
panic("Too many attempts!")
}
func openDatabase(dbFilePath string) (state State, userPass string) {
for i := 0; i < 5; i++ {
print("Please enter your master password: ")
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
println("")
if err != nil {
panic(err)
}
userPass = string(bytePassword)
state, err = gohash_db.ReadDatabase(dbFilePath, userPass)
if err != nil {
println("✗ Error: " + err.Error())
} else {
// success
fmt.Printf("\n✔ Opened database at %s\n", dbFilePath)
return
}
}
panic("Aborting. Too many attempts!")
}
func splitTrimN(text string, max int) []string {
result := make([]string, max)
parts := strings.SplitN(text, " ", max)
for i, c := range parts {
result[i] = strings.TrimSpace(c)
}
return result
}
func runCliLoop(state *State, dbPath string, userPass string, passwordTimeout *time.Duration) {
grBox := stringBox{value: "default"}
mpBox := stringBox{value: userPass}
userPass = ""
reader := bufio.NewReader(os.Stdin)
prompt := func() string {
var modifier string
if len(grBox.value) > 0 && grBox.value != "default" {
modifier = ":" + grBox.value
}
return fmt.Sprintf("\033[31mgo-hash%s»\033[0m ", modifier)
}
commands := createCommands(state, &grBox, &mpBox)
cli, err := readline.NewEx(&readline.Config{
Prompt: prompt(),
AutoComplete: createCompleter(commands),
InterruptPrompt: "^C",
})
if err != nil {
panic(err)
}
defer cli.Close()
eofCount := 0
idleSince := time.Now()
Loop:
for {
cli.SetPrompt(prompt())
line, err := cli.Readline()
if err != nil {
switch err {
case readline.ErrInterrupt:
if len(line) == 0 {
println("Warning: Received interrupt, exiting.")
break Loop
}
continue
case io.EOF:
eofCount++
if eofCount > 10 { // protect against infinite loop
panic("EOF received several times unexpectedly!")
}
continue // in Windows, we get EOFs all the time
default:
panic(err)
}
}
totalIdleTime := time.Now().Sub(idleSince)
eofCount = 0
parts := splitTrimN(line, 2)
cmd := parts[0]
args := parts[1]
switch cmd {
case "quit":
break Loop
case "exit":
if grBox.value != "default" {
grBox.value = "default"
} else {
break Loop
}
default:
command := commands[cmd]
if command != nil {
if command.requiresPasswordIfIdleTooLong() {
if passwordTimeout != nil && totalIdleTime > *passwordTimeout {
mpBox.value = ""
// prompt for password before allowing command to run
fmt.Printf("⚠ password required (idle %s)\n", totalIdleTime.Round(time.Second))
// if trying to re-open the database fails, the process will exit, otherwise just continue
*state, userPass = openDatabase(dbPath)
mpBox.value = userPass
userPass = ""
}
// reset the idle timer only on commands that are sensitive
idleSince = time.Now()
}
command.run(state, grBox.value, args, reader)
err = gohash_db.WriteDatabase(dbPath, mpBox.value, state)
if err != nil {
println("Error writing to database: " + err.Error())
}
} else if len(cmd) > 0 {
fmt.Printf("Unknown command: '%s'. Type 'help' for usage.\n", cmd)
}
}
}
}
func parseOptions() (dbFilePath string, passwordTimeout *time.Duration) {
var defaultPasswordTimeout = time.Duration(120) * time.Second
if len(os.Args) == 1 { // no args given
return getGoHashFilePath(), &defaultPasswordTimeout
}
if len(os.Args) == 2 && !strings.HasPrefix(os.Args[1], "-") { // one arg, no flag
return os.Args[1], &defaultPasswordTimeout
}
var idleSec uint // password required after inactivity
flag.UintVar(&idleSec, "idle", 120, "password timeout, in seconds (use 0 for no timeout)")
flag.StringVar(&dbFilePath, "db", getGoHashFilePath(), "database file")
flag.Parse()
if len(flag.Args()) > 0 {
fmt.Printf("usage: %s [-db <database filename>] [-idle <password timeout>]", os.Args[0])
os.Exit(2)
}
timeout := time.Duration(idleSec) * time.Second
passwordTimeout = &timeout
if !parentDirExists(dbFilePath) {
panic("The provided file is under a non-existing directory. Please create the directory manually first.")
}
if isDir(dbFilePath) {
panic("The path you provided is a directory. Please provide a file.")
}
return
}
func main() {
var userPass string
var state State
println("Go-Hash version " + gohash_db.DBVersion)
println("")
dbFilePath, passwordTimeout := parseOptions()
dbFile, err := os.Open(dbFilePath)
if err != nil {
if os.IsNotExist(err) {
println("No database exists yet, to create one, you need to provide a strong password first.")
println("A strong password could be a phrase you could remember easily but that is hard to guess.")
println("To make it harder to guess, include both upper and lower-case letters, numbers and special characters like ? and @.")
println("If you forget this password, there's no way to recover it or your data, so be careful!\n")
userPass = createPassword()
} else {
panic(err)
}
state = make(State)
} else {
// the DB exists, check if the user can open it
dbFile.Close()
state, userPass = openDatabase(dbFilePath)
}
if len(state) == 0 {
state["default"] = []LoginInfo{}
}
println("\nWelcome, go-hash at your service.\n")
runCliLoop(&state, dbFilePath, userPass, passwordTimeout)
}