Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

caddyauth: Fix hash-password broken terminal state on SIGINT #3416

Merged
merged 2 commits into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/commandfuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 0 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
27 changes: 20 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,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)
}()
francislavoie marked this conversation as resolved.
Show resolved Hide resolved
defer signal.Stop(c)

fmt.Fprint(os.Stderr, "Enter password: ")
francislavoie marked this conversation as resolved.
Show resolved Hide resolved
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
2 changes: 2 additions & 0 deletions modules/caddyhttp/fileserver/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions modules/caddyhttp/reverseproxy/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down