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

lncli: read password from stdin till EOF in unlock #8

Closed
wants to merge 1 commit into from
Closed
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
29 changes: 23 additions & 6 deletions cmd/lncli/cmd_walletunlocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
Expand Down Expand Up @@ -469,7 +470,9 @@ var unlockCommand = cli.Command{
"a file that can be read by another user. " +
"This flag should only be used in " +
"combination with some sort of password " +
"manager or secrets vault.",
"manager or secrets vault. The password will " +
"be read from standard input until EOF " +
"(End of File) is encountered.",
},
statelessInitFlag,
},
Expand All @@ -491,11 +494,25 @@ func unlock(ctx *cli.Context) error {
// password manager. If the user types the password instead, it will be
// echoed in the console.
case ctx.IsSet("stdin"):
reader := bufio.NewReader(os.Stdin)
pw, err = reader.ReadBytes('\n')

// Remove carriage return and newline characters.
pw = bytes.Trim(pw, "\r\n")
// Copy the password from standard input to the buffer until
// EOF (End of File) is encountered.
var buf bytes.Buffer
if _, err := io.Copy(&buf, os.Stdin); err != nil {
return err
}
pw = buf.Bytes()

// Remove carriage return.
pw = bytes.Trim(pw, "\r")

// Check if the provided password has leading or trailing
// newline characters, then issue a warning since they will
// not be trimmed.
if !bytes.Equal(pw, bytes.Trim(pw, "\n")) {
fmt.Printf("WARNING: Password contains leading or" +
"trailing newline characters and they will " +
"not be trimmed.\n")
}

// Read the password from a terminal by default. This requires the
// terminal to be a real tty and will fail if a string is piped into
Expand Down
Loading