@marcel91200 / caddy Mirror · read-only / commits / bb67e19d
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

cmd: hash-password: Fix broken terminal state on SIGINT (#3416)

* caddyauth: Fix hash-password broken terminal state on SIGINT * caddycmd: Move TrapSignals calls to only subcommands that run long

Francis Lavoie committed May 21, 2020 at 15:09 UTC bb67e19d7bacd70a13647538c8e9820eb42090b2
5 files changed +26 -9
cmd/commandfuncs.go
+2
@@ -148,6 +148,8 @@ func cmdStart(fl Flags) (int, error) {
148 }
149
150 func cmdRun(fl Flags) (int, error) {
151 + caddy.TrapSignals()
152 +
153 runCmdConfigFlag := fl.String("config")
154 runCmdConfigAdapterFlag := fl.String("adapter")
155 runCmdResumeFlag := fl.Bool("resume")
cmd/main.go
-2
@@ -51,8 +51,6 @@ func init() {
51 // Main implements the main function of the caddy command.
52 // Call this if Caddy is to be the main() if your program.
53 func Main() {
54 - caddy.TrapSignals()
55 -
54 switch len(os.Args) {
55 case 0:
56 fmt.Printf("[FATAL] no arguments provided by OS; args[0] must be command\n")
modules/caddyhttp/caddyauth/command.go
+20 -7
@@ -21,6 +21,7 @@ import (
21 "flag"
22 "fmt"
23 "os"
24 + "os/signal"
25
26 "github.com/caddyserver/caddy/v2"
27 caddycmd "github.com/caddyserver/caddy/v2/cmd"
@@ -67,17 +68,29 @@ func cmdHashPassword(fs caddycmd.Flags) (int, error) {
68 salt := []byte(fs.String("salt"))
69
70 if len(plaintext) == 0 {
70 - if terminal.IsTerminal(int(os.Stdin.Fd())) {
71 - fmt.Print("Enter password: ")
72 - plaintext, err = terminal.ReadPassword(int(os.Stdin.Fd()))
73 - fmt.Println()
71 + fd := int(os.Stdin.Fd())
72 + if terminal.IsTerminal(fd) {
73 + // ensure the terminal state is restored on SIGINT
74 + state, _ := terminal.GetState(fd)
75 + c := make(chan os.Signal)
76 + signal.Notify(c, os.Interrupt)
77 + go func() {
78 + <-c
79 + _ = terminal.Restore(fd, state)
80 + os.Exit(caddy.ExitCodeFailedStartup)
81 + }()
82 + defer signal.Stop(c)
83 +
84 + fmt.Fprint(os.Stderr, "Enter password: ")
85 + plaintext, err = terminal.ReadPassword(fd)
86 + fmt.Fprintln(os.Stderr)
87 if err != nil {
88 return caddy.ExitCodeFailedStartup, err
89 }
90
78 - fmt.Print("Confirm password: ")
79 - confirmation, err := terminal.ReadPassword(int(os.Stdin.Fd()))
80 - fmt.Println()
91 + fmt.Fprint(os.Stderr, "Confirm password: ")
92 + confirmation, err := terminal.ReadPassword(fd)
93 + fmt.Fprintln(os.Stderr)
94 if err != nil {
95 return caddy.ExitCodeFailedStartup, err
96 }
modules/caddyhttp/fileserver/command.go
+2
@@ -61,6 +61,8 @@ respond with a file listing.`,
61 }
62
63 func cmdFileServer(fs caddycmd.Flags) (int, error) {
64 + caddy.TrapSignals()
65 +
66 domain := fs.String("domain")
67 root := fs.String("root")
68 listen := fs.String("listen")
modules/caddyhttp/reverseproxy/command.go
+2
@@ -66,6 +66,8 @@ default, all incoming headers are passed through unmodified.)
66 }
67
68 func cmdReverseProxy(fs caddycmd.Flags) (int, error) {
69 + caddy.TrapSignals()
70 +
71 from := fs.String("from")
72 to := fs.String("to")
73 changeHost := fs.Bool("change-host-header")