From 7920b99762fbac04ff678f97f154ed5b25b10e16 Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Fri, 15 May 2020 11:36:43 -0400 Subject: [PATCH 1/2] caddyauth: Fix hash-password broken terminal state on SIGINT --- modules/caddyhttp/caddyauth/command.go | 27 +++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/modules/caddyhttp/caddyauth/command.go b/modules/caddyhttp/caddyauth/command.go index 2c08815715b..f3e1809da59 100644 --- a/modules/caddyhttp/caddyauth/command.go +++ b/modules/caddyhttp/caddyauth/command.go @@ -21,6 +21,7 @@ import ( "flag" "fmt" "os" + "os/signal" "github.com/caddyserver/caddy/v2" caddycmd "github.com/caddyserver/caddy/v2/cmd" @@ -67,17 +68,29 @@ 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(caddy.ExitCodeFailedStartup) + }() + defer signal.Stop(c) + + 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 } From c26575b632f7e62012c043892d4d1ef5e20715e3 Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Mon, 18 May 2020 15:17:16 -0400 Subject: [PATCH 2/2] caddycmd: Move TrapSignals calls to only subcommands that run long --- cmd/commandfuncs.go | 2 ++ cmd/main.go | 2 -- modules/caddyhttp/fileserver/command.go | 2 ++ modules/caddyhttp/reverseproxy/command.go | 2 ++ 4 files changed, 6 insertions(+), 2 deletions(-) diff --git a/cmd/commandfuncs.go b/cmd/commandfuncs.go index a8247895801..875fb5a9982 100644 --- a/cmd/commandfuncs.go +++ b/cmd/commandfuncs.go @@ -148,6 +148,8 @@ func cmdStart(fl Flags) (int, error) { } func cmdRun(fl Flags) (int, error) { + caddy.TrapSignals() + runCmdConfigFlag := fl.String("config") runCmdConfigAdapterFlag := fl.String("adapter") runCmdResumeFlag := fl.Bool("resume") diff --git a/cmd/main.go b/cmd/main.go index 4fd8d681555..00d5d138525 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -51,8 +51,6 @@ func init() { // Main implements the main function of the caddy command. // Call this if Caddy is to be the main() if your program. func Main() { - caddy.TrapSignals() - switch len(os.Args) { case 0: fmt.Printf("[FATAL] no arguments provided by OS; args[0] must be command\n") diff --git a/modules/caddyhttp/fileserver/command.go b/modules/caddyhttp/fileserver/command.go index 53aa7aa5b5a..6a44b3ca70b 100644 --- a/modules/caddyhttp/fileserver/command.go +++ b/modules/caddyhttp/fileserver/command.go @@ -61,6 +61,8 @@ respond with a file listing.`, } func cmdFileServer(fs caddycmd.Flags) (int, error) { + caddy.TrapSignals() + domain := fs.String("domain") root := fs.String("root") listen := fs.String("listen") diff --git a/modules/caddyhttp/reverseproxy/command.go b/modules/caddyhttp/reverseproxy/command.go index 6de052e7208..a5939a2ad0b 100644 --- a/modules/caddyhttp/reverseproxy/command.go +++ b/modules/caddyhttp/reverseproxy/command.go @@ -66,6 +66,8 @@ default, all incoming headers are passed through unmodified.) } func cmdReverseProxy(fs caddycmd.Flags) (int, error) { + caddy.TrapSignals() + from := fs.String("from") to := fs.String("to") changeHost := fs.Bool("change-host-header")