2019-10-11 04:37:27 +08: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-12 04:10:47 +08:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2019-10-11 04:37:27 +08:00
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
2020-05-12 04:10:47 +08:00
|
|
|
"os"
|
2020-05-22 03:09:49 +08:00
|
|
|
"os/signal"
|
2019-10-11 04:37:27 +08:00
|
|
|
|
|
|
|
"github.com/caddyserver/caddy/v2"
|
|
|
|
caddycmd "github.com/caddyserver/caddy/v2/cmd"
|
2023-02-25 07:09:12 +08:00
|
|
|
"github.com/spf13/cobra"
|
2021-03-30 02:39:08 +08:00
|
|
|
"golang.org/x/term"
|
2019-10-11 04:37:27 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
caddycmd.RegisterCommand(caddycmd.Command{
|
|
|
|
Name: "hash-password",
|
2020-05-12 04:10:47 +08:00
|
|
|
Usage: "[--algorithm <name>] [--salt <string>] [--plaintext <password>]",
|
2019-10-11 04:37:27 +08: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-12 04:10:47 +08: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-06 03:32:58 +08:00
|
|
|
--algorithm may be bcrypt or scrypt. If scrypt, the default
|
2019-10-11 04:37:27 +08:00
|
|
|
parameters are used.
|
|
|
|
|
|
|
|
Use the --salt flag for algorithms which require a salt to
|
|
|
|
be provided (scrypt).
|
2022-09-06 03:32:58 +08:00
|
|
|
|
|
|
|
Note that scrypt is deprecated. Please use 'bcrypt' instead.
|
2019-10-11 04:37:27 +08:00
|
|
|
`,
|
2023-02-25 07:09:12 +08: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-11 04:37:27 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdHashPassword(fs caddycmd.Flags) (int, error) {
|
2020-05-12 04:10:47 +08:00
|
|
|
var err error
|
|
|
|
|
2019-10-11 04:37:27 +08:00
|
|
|
algorithm := fs.String("algorithm")
|
|
|
|
plaintext := []byte(fs.String("plaintext"))
|
|
|
|
salt := []byte(fs.String("salt"))
|
|
|
|
|
2019-10-29 05:08:45 +08:00
|
|
|
if len(plaintext) == 0 {
|
2020-05-22 03:09:49 +08:00
|
|
|
fd := int(os.Stdin.Fd())
|
2021-03-30 02:39:08 +08:00
|
|
|
if term.IsTerminal(fd) {
|
2020-05-22 03:09:49 +08:00
|
|
|
// ensure the terminal state is restored on SIGINT
|
2021-03-30 02:39:08 +08:00
|
|
|
state, _ := term.GetState(fd)
|
2020-12-01 23:27:46 +08:00
|
|
|
c := make(chan os.Signal, 1)
|
2020-05-22 03:09:49 +08:00
|
|
|
signal.Notify(c, os.Interrupt)
|
|
|
|
go func() {
|
|
|
|
<-c
|
2021-03-30 02:39:08 +08:00
|
|
|
_ = term.Restore(fd, state)
|
2020-05-22 03:09:49 +08:00
|
|
|
os.Exit(caddy.ExitCodeFailedStartup)
|
|
|
|
}()
|
|
|
|
defer signal.Stop(c)
|
|
|
|
|
|
|
|
fmt.Fprint(os.Stderr, "Enter password: ")
|
2021-03-30 02:39:08 +08:00
|
|
|
plaintext, err = term.ReadPassword(fd)
|
2020-05-22 03:09:49 +08:00
|
|
|
fmt.Fprintln(os.Stderr)
|
2020-05-12 04:10:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup, err
|
|
|
|
}
|
|
|
|
|
2020-05-22 03:09:49 +08:00
|
|
|
fmt.Fprint(os.Stderr, "Confirm password: ")
|
2021-03-30 02:39:08 +08:00
|
|
|
confirmation, err := term.ReadPassword(fd)
|
2020-05-22 03:09:49 +08:00
|
|
|
fmt.Fprintln(os.Stderr)
|
2020-05-12 04:10:47 +08: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-29 05:08:45 +08:00
|
|
|
}
|
|
|
|
|
2019-10-11 04:37:27 +08:00
|
|
|
var hash []byte
|
2022-09-06 03:32:58 +08:00
|
|
|
var hashString string
|
2019-10-11 04:37:27 +08:00
|
|
|
switch algorithm {
|
|
|
|
case "bcrypt":
|
2020-11-01 00:51:05 +08:00
|
|
|
hash, err = BcryptHash{}.Hash(plaintext, nil)
|
2022-09-06 03:32:58 +08:00
|
|
|
hashString = string(hash)
|
2019-10-11 04:37:27 +08:00
|
|
|
case "scrypt":
|
|
|
|
def := ScryptHash{}
|
|
|
|
def.SetDefaults()
|
2020-11-01 00:51:05 +08:00
|
|
|
hash, err = def.Hash(plaintext, salt)
|
2022-09-06 03:32:58 +08:00
|
|
|
hashString = base64.StdEncoding.EncodeToString(hash)
|
2019-10-11 04:37:27 +08:00
|
|
|
default:
|
|
|
|
return caddy.ExitCodeFailedStartup, fmt.Errorf("unrecognized hash algorithm: %s", algorithm)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup, err
|
|
|
|
}
|
|
|
|
|
2022-09-06 03:32:58 +08:00
|
|
|
fmt.Println(hashString)
|
2019-10-11 04:37:27 +08:00
|
|
|
|
|
|
|
return 0, nil
|
|
|
|
}
|