-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
205 lines (184 loc) · 4.67 KB
/
cmd.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
package main
import (
"fmt"
"io"
"os"
"os/exec"
"strings"
"syscall"
"golang.org/x/crypto/ssh/terminal"
)
var options = struct {
algorithm string
urls []string
putToClipboard bool
password []byte
}{"scrypt", make([]string, 0, 1), false, nil}
var xclipPath string = ""
// Parse arguments and do stuff.
func main() {
args := os.Args
stdoutIsTty := terminal.IsTerminal(syscall.Stdin)
options.putToClipboard = stdoutIsTty
dashdash := false
for i := 1; i < len(args); i++ {
c := args[i]
if dashdash {
options.urls = append(options.urls, c)
continue
}
if c == "-a" {
i++
if i == len(args) {
argerror("Expected argument after -a")
return
}
options.algorithm = args[i]
continue
}
const pf = "--clipboard="
if strings.HasPrefix(c, pf) {
switch val := c[len(pf):]; val {
case "always":
options.putToClipboard = true
case "no":
options.putToClipboard = false
case "auto":
options.putToClipboard = stdoutIsTty
default:
argerror(fmt.Sprintf("Invalid value %v for --clipboard. Expected always/no/auto.\n", val))
return
}
continue
}
if c == "-p" {
i++
if i == len(args) {
argerror("Expected argument after -p")
return
}
options.password = []byte(args[i])
continue
}
if c == "--help" {
fmt.Fprint(os.Stdout, "Usage: go-ecbpass [-a <pbkdf2|scrypt>] [--clipboard=<always|no|auto>] [-p <password>] [urls..]\n"+
"For more info, see man go-ecbpass\n")
os.Exit(0)
}
if c == "--" {
dashdash = true
continue
}
if strings.HasPrefix(c, "-") {
if strings.HasPrefix(c, "--") {
argerror("Unknown option " + c[2:])
return
}
argerror("Unknown option " + c[1:])
return
}
options.urls = append(options.urls, c)
}
if len(options.urls) == 0 {
argerror("Need to provide one url.")
return
}
if options.putToClipboard && len(options.urls) > 1 {
options.putToClipboard = false
fmt.Fprint(os.Stderr, "Generated password will not be put to clipboard, because multiplt urls are provided on the command line.\n")
fmt.Fprint(os.Stderr, "Specify --clipboard=no to slience this warning.\n")
}
if options.putToClipboard {
_xclipPath, err := exec.LookPath("xclip")
xclipPath = _xclipPath
if err != nil {
xclipPath = ""
options.putToClipboard = false
if options.password == nil {
fmt.Fprint(os.Stderr, "xclip command not found. Unable to put stuff to clipboard. \033[1;31mGenerated password will be printed out\033[0m.\n")
} else {
fmt.Fprint(os.Stderr, "xclip command not found. Unable to put stuff to clipboard. Generated password will be printed out.\n")
}
}
}
if options.password == nil {
if !stdoutIsTty {
fmt.Fprintf(os.Stderr, "Password need to be provided on the command line, because stdin is not a terminal.\n")
os.Exit(1)
return
}
fmt.Fprintf(os.Stderr, "[go-ecbpass] master password: ")
os.Stderr.Sync()
pw, err := terminal.ReadPassword(syscall.Stdin)
os.Stderr.WriteString("\n")
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to read password: %v.\n", err.Error())
os.Exit(1)
return
}
options.password = pw
hint := Hashhint(pw)
fmt.Fprintf(os.Stderr, " \033[32mPassword hint: %v\033[0m\n", hint)
}
for _, url := range options.urls {
doUrl(url)
}
}
func argerror(err string) {
fmt.Fprintf(os.Stderr, "go-ecbpass: invalid argument: %v.\n", err)
os.Exit(1)
}
func doUrl(url string) {
salt, err := UrlToSalt(url)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing url: %v... Using the provided string verbatim as salt.\n", err.Error())
salt = []byte(url)
}
var cryptFunc (func(password []byte, salt []byte) (result string)) = nil
switch options.algorithm {
case "pbkdf2":
cryptFunc = PBKDF2
case "scrypt":
cryptFunc = Scrypt
default:
fmt.Fprintf(os.Stderr, "Unknow algorithm %v.\n", options.algorithm)
os.Exit(1)
return
}
if len(options.urls) == 0 {
// interactive
fmt.Fprint(os.Stderr, "Calculating, please wait...")
}
result := cryptFunc(options.password, salt)
fmt.Fprint(os.Stderr, "\033[2K\r")
if options.putToClipboard {
err := putStringToClipboard(result)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to run xclip: %v.", err.Error())
os.Exit(1)
} else {
fmt.Fprint(os.Stderr, "Password copied.\n")
}
} else {
os.Stdout.WriteString(result + "\n")
}
}
func putStringToClipboard(str string) (err error) {
cmd := exec.Command(xclipPath, "-selection", "clipboard", "-in")
cmd.Stdout = nil
cmd.Stderr = nil
stdin, err := cmd.StdinPipe()
if err != nil {
return err
}
cmd.Start()
_, err = io.WriteString(stdin, str)
closeErr := stdin.Close()
if err != nil {
return err
}
if closeErr != nil {
return closeErr
}
return cmd.Wait()
}