Debian Codegraph @

cmd/ro/gopass/gopass.go

raw symbols SCIP
1 // Author: [email protected] (John Doak)
2
3 /*
4 gopass is a library for getting hidden input from a terminal.
5
6 This library's main use is to allow a user to enter a password at the
7 command line without having it echoed to the screen.
8
9 The libary currently supports unix systems by manipulating stty.
10
11 This code is based upon code by RogerV in the golang-nuts thread:
12 https://groups.google.com/group/golang-nuts/browse_thread/thread/40cc41e9d9fc9247
13 */
14 package gopass
15
16 import (
17 "bufio"
18 "fmt"
19 "os"
20 "os/signal"
21 "strings"
22 "syscall"
23 )
24
25 const (
26 sttyArg0 = "/bin/stty"
27 exec_cwdir = ""
28 )
29
30 // Tells the terminal to turn echo off.
31 var sttyArgvEOff []string = []string{"stty", "-echo"}
32
33 // Tells the terminal to turn echo on.
34 var sttyArgvEOn []string = []string{"stty", "echo"}
35
36 var ws syscall.WaitStatus = 0
37
38 // GetPass gets input hidden from the terminal from a user.
39 // This is accomplished by turning off terminal echo,
40 // reading input from the user and finally turning on terminal echo.
41 // prompt is a string to display before the user's input.
42 func GetPass(prompt string) (passwd string, err error) {
43 sig := make(chan os.Signal, 10)
44 brk := make(chan bool)
45
46 // Display the prompt.
47 fmt.Print(prompt)
48
49 // File descriptors for stdin, stdout, and stderr.
50 fd := []uintptr{os.Stdin.Fd(), os.Stdout.Fd(), os.Stderr.Fd()}
51
52 // Setup notifications of termination signals to channel sig, create a process to
53 // watch for these signals so we can turn back on echo if need be.
54 signal.Notify(sig, syscall.SIGHUP, syscall.SIGINT, syscall.SIGKILL, syscall.SIGQUIT,
55 syscall.SIGTERM)
56 go catchSignal(fd, sig, brk)
57
58 // Turn off the terminal echo.
59 pid, err := echoOff(fd)
60 if err != nil {
61 return "", err
62 }
63
64 // Turn on the terminal echo and stop listening for signals.
65 defer close(brk)
66 defer echoOn(fd)
67
68 rd := bufio.NewReader(os.Stdin)
69 syscall.Wait4(pid, &ws, 0, nil)
70
71 line, err := rd.ReadString('\n')
72 if err == nil {
73 passwd = strings.TrimSpace(line)
74 } else {
75 err = fmt.Errorf("failed during password entry: %s", err)
76 }
77
78 // Carraige return after the user input.
79 fmt.Println("")
80
81 return passwd, err
82 }
83
84 func echoOff(fd []uintptr) (int, error) {
85 pid, err := syscall.ForkExec(sttyArg0, sttyArgvEOff, &syscall.ProcAttr{Dir: exec_cwdir, Files: fd})
86 if err != nil {
87 return 0, fmt.Errorf("failed turning off console echo for password entry:\n\t%s", err)
88 }
89 return pid, nil
90 }
91
92 // echoOn turns back on the terminal echo.
93 func echoOn(fd []uintptr) {
94 // Turn on the terminal echo.
95 pid, e := syscall.ForkExec(sttyArg0, sttyArgvEOn, &syscall.ProcAttr{Dir: exec_cwdir, Files: fd})
96 if e == nil {
97 syscall.Wait4(pid, &ws, 0, nil)
98 }
99 }
100
101 // catchSignal tries to catch SIGKILL, SIGQUIT and SIGINT so that we can turn terminal
102 // echo back on before the program ends. Otherwise the user is left with echo off on
103 // their terminal.
104 func catchSignal(fd []uintptr, sig chan os.Signal, brk chan bool) {
105 select {
106 case <-sig:
107 echoOn(fd)
108 os.Exit(-1)
109 case <-brk:
110 }
111 }