Skip to content

Commit

Permalink
caddyauth: Fix hash-password broken terminal state on SIGINT
Browse files Browse the repository at this point in the history
  • Loading branch information
francislavoie committed May 15, 2020
1 parent bde3823 commit d1936ac
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions modules/caddyhttp/caddyauth/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"flag"
"fmt"
"os"
"os/signal"

"github.com/caddyserver/caddy/v2"
caddycmd "github.com/caddyserver/caddy/v2/cmd"
Expand Down Expand Up @@ -67,17 +68,28 @@ func cmdHashPassword(fs caddycmd.Flags) (int, error) {
salt := []byte(fs.String("salt"))

if len(plaintext) == 0 {
if terminal.IsTerminal(int(os.Stdin.Fd())) {
fmt.Print("Enter password: ")
plaintext, err = terminal.ReadPassword(int(os.Stdin.Fd()))
fmt.Println()
fd := int(os.Stdin.Fd())
if terminal.IsTerminal(fd) {
// ensure the terminal state is restored on SIGINT
state, _ := terminal.GetState(fd)
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt)
go func() {
<-c
_ = terminal.Restore(fd, state)
os.Exit(1)
}()

fmt.Fprint(os.Stderr, "Enter password: ")
plaintext, err = terminal.ReadPassword(fd)
fmt.Fprintln(os.Stderr)
if err != nil {
return caddy.ExitCodeFailedStartup, err
}

fmt.Print("Confirm password: ")
confirmation, err := terminal.ReadPassword(int(os.Stdin.Fd()))
fmt.Println()
fmt.Fprint(os.Stderr, "Confirm password: ")
confirmation, err := terminal.ReadPassword(fd)
fmt.Fprintln(os.Stderr)
if err != nil {
return caddy.ExitCodeFailedStartup, err
}
Expand Down

0 comments on commit d1936ac

Please sign in to comment.