forked from dciangot/dodas-IAMClientRec
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinput_unix.go
106 lines (77 loc) · 2.68 KB
/
input_unix.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
// +build !windows,linux darwin
package main
import (
"bytes"
"errors"
"fmt"
"syscall"
"golang.org/x/sys/unix"
"github.com/awnumar/memguard"
"github.com/gookit/color"
"github.com/rs/zerolog/log"
)
var (
errPasswordMismatch = errors.New("The two password inserted are not the same.")
)
// passwordReader is an io.Reader that reads from a specific file descriptor.
type passwordReader int
func (r passwordReader) Read(buf []byte) (int, error) {
return unix.Read(int(r), buf)
}
func readPassword(fd int) (passwordReader, *unix.Termios, error) {
termios, err := unix.IoctlGetTermios(fd, ioctlReadTermios)
if err != nil {
return -1, nil, fmt.Errorf("readPassword %w", err)
}
newState := *termios
newState.Lflag &^= unix.ECHO
newState.Lflag |= unix.ICANON | unix.ISIG
newState.Iflag |= unix.ICRNL
if err := unix.IoctlSetTermios(fd, ioctlWriteTermios, &newState); err != nil {
return -1, nil, fmt.Errorf("readPassword %w", err)
}
return passwordReader(fd), termios, nil
}
func (t *GetInputWrapper) GetPassword(question string, only4Decription bool) (password *memguard.Enclave, err error) {
fmt.Print(question)
readPasswdFd, termios, errCreateReader := readPassword(syscall.Stdin)
if errCreateReader != nil {
return nil, fmt.Errorf("get password %w", errCreateReader)
}
defer unix.IoctlSetTermios(int(readPasswdFd), ioctlWriteTermios, termios) // nolint: errcheck
passEnclave, errEclBuf := memguard.NewBufferFromReaderUntil(readPasswdFd, '\n')
if errEclBuf != nil {
return nil, fmt.Errorf("get password enclave %w", errEclBuf)
}
for passEnclave.Size() == 0 {
unix.IoctlSetTermios(int(readPasswdFd), ioctlWriteTermios, termios) // nolint: errcheck
fmt.Printf("\n%s Sorry, but an empty password is not allowed...\n", color.Red.Sprint("[X]==>"))
fmt.Print(question)
readPasswdFd, termios, errCreateReader = readPassword(syscall.Stdin)
if errCreateReader != nil {
return nil, fmt.Errorf("get password %w", errCreateReader)
}
passEnclave, errEclBuf = memguard.NewBufferFromReaderUntil(readPasswdFd, '\n')
if errEclBuf != nil {
return nil, fmt.Errorf("get password enclave %w", errEclBuf)
}
}
fmt.Println()
if only4Decription {
password = passEnclave.Seal()
return password, nil
}
passMsg := fmt.Sprintf("%s Please, insert the password again: ", color.Yellow.Sprint("==>"))
fmt.Print(passMsg)
passEnclave2, err := memguard.NewBufferFromReaderUntil(readPasswdFd, '\n')
if err != nil {
return nil, fmt.Errorf("get password check %w", err)
}
fmt.Println()
if bytes.Equal(passEnclave.Bytes(), passEnclave2.Bytes()) {
password = passEnclave.Seal()
return password, nil
}
log.Err(errPasswordMismatch).Msg("GetPassword")
return nil, errPasswordMismatch
}