2019-10-10 15:37:27 -05:00
|
|
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package caddyauth
|
|
|
|
|
|
|
|
import (
|
2020-05-11 15:10:47 -05:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2019-10-10 15:37:27 -05:00
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
2020-05-11 15:10:47 -05:00
|
|
|
"os"
|
2020-05-21 14:09:49 -05:00
|
|
|
"os/signal"
|
2019-10-10 15:37:27 -05:00
|
|
|
|
|
|
|
"github.com/caddyserver/caddy/v2"
|
|
|
|
caddycmd "github.com/caddyserver/caddy/v2/cmd"
|
2023-02-24 18:09:12 -05:00
|
|
|
"github.com/spf13/cobra"
|
2021-03-29 13:39:08 -05:00
|
|
|
"golang.org/x/term"
|
2019-10-10 15:37:27 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
caddycmd.RegisterCommand(caddycmd.Command{
|
|
|
|
Name: "hash-password",
|
2020-05-11 15:10:47 -05:00
|
|
|
Usage: "[--algorithm <name>] [--salt <string>] [--plaintext <password>]",
|
2019-10-10 15:37:27 -05:00
|
|
|
Short: "Hashes a password and writes base64",
|
|
|
|
Long: `
|
|
|
|
Convenient way to hash a plaintext password. The resulting
|
|
|
|
hash is written to stdout as a base64 string.
|
|
|
|
|
2020-05-11 15:10:47 -05:00
|
|
|
--plaintext, when omitted, will be read from stdin. If
|
|
|
|
Caddy is attached to a controlling tty, the plaintext will
|
|
|
|
not be echoed.
|
|
|
|
|
2022-09-05 14:32:58 -05:00
|
|
|
--algorithm may be bcrypt or scrypt. If scrypt, the default
|
2019-10-10 15:37:27 -05:00
|
|
|
parameters are used.
|
|
|
|
|
|
|
|
Use the --salt flag for algorithms which require a salt to
|
|
|
|
be provided (scrypt).
|
2022-09-05 14:32:58 -05:00
|
|
|
|
|
|
|
Note that scrypt is deprecated. Please use 'bcrypt' instead.
|
2019-10-10 15:37:27 -05:00
|
|
|
`,
|
2023-02-24 18:09:12 -05:00
|
|
|
CobraFunc: func(cmd *cobra.Command) {
|
|
|
|
cmd.Flags().StringP("plaintext", "p", "", "The plaintext password")
|
|
|
|
cmd.Flags().StringP("salt", "s", "", "The password salt")
|
|
|
|
cmd.Flags().StringP("algorithm", "a", "bcrypt", "Name of the hash algorithm")
|
|
|
|
cmd.RunE = caddycmd.WrapCommandFuncForCobra(cmdHashPassword)
|
|
|
|
},
|
2019-10-10 15:37:27 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdHashPassword(fs caddycmd.Flags) (int, error) {
|
2020-05-11 15:10:47 -05:00
|
|
|
var err error
|
|
|
|
|
2019-10-10 15:37:27 -05:00
|
|
|
algorithm := fs.String("algorithm")
|
|
|
|
plaintext := []byte(fs.String("plaintext"))
|
|
|
|
salt := []byte(fs.String("salt"))
|
|
|
|
|
2019-10-28 16:08:45 -05:00
|
|
|
if len(plaintext) == 0 {
|
2020-05-21 14:09:49 -05:00
|
|
|
fd := int(os.Stdin.Fd())
|
2021-03-29 13:39:08 -05:00
|
|
|
if term.IsTerminal(fd) {
|
2020-05-21 14:09:49 -05:00
|
|
|
// ensure the terminal state is restored on SIGINT
|
2021-03-29 13:39:08 -05:00
|
|
|
state, _ := term.GetState(fd)
|
2020-12-01 10:27:46 -05:00
|
|
|
c := make(chan os.Signal, 1)
|
2020-05-21 14:09:49 -05:00
|
|
|
signal.Notify(c, os.Interrupt)
|
|
|
|
go func() {
|
|
|
|
<-c
|
2021-03-29 13:39:08 -05:00
|
|
|
_ = term.Restore(fd, state)
|
2020-05-21 14:09:49 -05:00
|
|
|
os.Exit(caddy.ExitCodeFailedStartup)
|
|
|
|
}()
|
|
|
|
defer signal.Stop(c)
|
|
|
|
|
|
|
|
fmt.Fprint(os.Stderr, "Enter password: ")
|
2021-03-29 13:39:08 -05:00
|
|
|
plaintext, err = term.ReadPassword(fd)
|
2020-05-21 14:09:49 -05:00
|
|
|
fmt.Fprintln(os.Stderr)
|
2020-05-11 15:10:47 -05:00
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup, err
|
|
|
|
}
|
|
|
|
|
2020-05-21 14:09:49 -05:00
|
|
|
fmt.Fprint(os.Stderr, "Confirm password: ")
|
2021-03-29 13:39:08 -05:00
|
|
|
confirmation, err := term.ReadPassword(fd)
|
2020-05-21 14:09:49 -05:00
|
|
|
fmt.Fprintln(os.Stderr)
|
2020-05-11 15:10:47 -05:00
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(plaintext, confirmation) {
|
|
|
|
return caddy.ExitCodeFailedStartup, fmt.Errorf("password does not match")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rd := bufio.NewReader(os.Stdin)
|
|
|
|
plaintext, err = rd.ReadBytes('\n')
|
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup, err
|
|
|
|
}
|
|
|
|
|
|
|
|
plaintext = plaintext[:len(plaintext)-1] // Trailing newline
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(plaintext) == 0 {
|
|
|
|
return caddy.ExitCodeFailedStartup, fmt.Errorf("plaintext is required")
|
|
|
|
}
|
2019-10-28 16:08:45 -05:00
|
|
|
}
|
|
|
|
|
2019-10-10 15:37:27 -05:00
|
|
|
var hash []byte
|
2022-09-05 14:32:58 -05:00
|
|
|
var hashString string
|
2019-10-10 15:37:27 -05:00
|
|
|
switch algorithm {
|
|
|
|
case "bcrypt":
|
2020-10-31 11:51:05 -05:00
|
|
|
hash, err = BcryptHash{}.Hash(plaintext, nil)
|
2022-09-05 14:32:58 -05:00
|
|
|
hashString = string(hash)
|
2019-10-10 15:37:27 -05:00
|
|
|
case "scrypt":
|
|
|
|
def := ScryptHash{}
|
|
|
|
def.SetDefaults()
|
2020-10-31 11:51:05 -05:00
|
|
|
hash, err = def.Hash(plaintext, salt)
|
2022-09-05 14:32:58 -05:00
|
|
|
hashString = base64.StdEncoding.EncodeToString(hash)
|
2019-10-10 15:37:27 -05:00
|
|
|
default:
|
|
|
|
return caddy.ExitCodeFailedStartup, fmt.Errorf("unrecognized hash algorithm: %s", algorithm)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup, err
|
|
|
|
}
|
|
|
|
|
2022-09-05 14:32:58 -05:00
|
|
|
fmt.Println(hashString)
|
2019-10-10 15:37:27 -05:00
|
|
|
|
|
|
|
return 0, nil
|
|
|
|
}
|