2019-10-10 23:37:27 +03: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 23:10:47 +03:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2019-10-10 23:37:27 +03:00
|
|
|
"fmt"
|
2020-05-11 23:10:47 +03:00
|
|
|
"os"
|
2020-05-21 22:09:49 +03:00
|
|
|
"os/signal"
|
2019-10-10 23:37:27 +03:00
|
|
|
|
2023-02-25 02:09:12 +03:00
|
|
|
"github.com/spf13/cobra"
|
2021-03-29 21:39:08 +03:00
|
|
|
"golang.org/x/term"
|
2023-08-14 18:41:15 +03:00
|
|
|
|
|
|
|
caddycmd "github.com/caddyserver/caddy/v2/cmd"
|
|
|
|
|
|
|
|
"github.com/caddyserver/caddy/v2"
|
2019-10-10 23:37:27 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
caddycmd.RegisterCommand(caddycmd.Command{
|
|
|
|
Name: "hash-password",
|
2024-02-12 22:33:54 +03:00
|
|
|
Usage: "[--plaintext <password>] [--algorithm <name>]",
|
2019-10-10 23:37:27 +03: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 23:10:47 +03:00
|
|
|
--plaintext, when omitted, will be read from stdin. If
|
|
|
|
Caddy is attached to a controlling tty, the plaintext will
|
|
|
|
not be echoed.
|
|
|
|
|
2024-02-12 22:33:54 +03:00
|
|
|
--algorithm currently only supports 'bcrypt', and is the default.
|
2019-10-10 23:37:27 +03:00
|
|
|
`,
|
2023-02-25 02:09:12 +03:00
|
|
|
CobraFunc: func(cmd *cobra.Command) {
|
|
|
|
cmd.Flags().StringP("plaintext", "p", "", "The plaintext password")
|
|
|
|
cmd.Flags().StringP("algorithm", "a", "bcrypt", "Name of the hash algorithm")
|
|
|
|
cmd.RunE = caddycmd.WrapCommandFuncForCobra(cmdHashPassword)
|
|
|
|
},
|
2019-10-10 23:37:27 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdHashPassword(fs caddycmd.Flags) (int, error) {
|
2020-05-11 23:10:47 +03:00
|
|
|
var err error
|
|
|
|
|
2019-10-10 23:37:27 +03:00
|
|
|
algorithm := fs.String("algorithm")
|
|
|
|
plaintext := []byte(fs.String("plaintext"))
|
|
|
|
|
2019-10-29 00:08:45 +03:00
|
|
|
if len(plaintext) == 0 {
|
2020-05-21 22:09:49 +03:00
|
|
|
fd := int(os.Stdin.Fd())
|
2021-03-29 21:39:08 +03:00
|
|
|
if term.IsTerminal(fd) {
|
2020-05-21 22:09:49 +03:00
|
|
|
// ensure the terminal state is restored on SIGINT
|
2021-03-29 21:39:08 +03:00
|
|
|
state, _ := term.GetState(fd)
|
2020-12-01 18:27:46 +03:00
|
|
|
c := make(chan os.Signal, 1)
|
2020-05-21 22:09:49 +03:00
|
|
|
signal.Notify(c, os.Interrupt)
|
|
|
|
go func() {
|
|
|
|
<-c
|
2021-03-29 21:39:08 +03:00
|
|
|
_ = term.Restore(fd, state)
|
2020-05-21 22:09:49 +03:00
|
|
|
os.Exit(caddy.ExitCodeFailedStartup)
|
|
|
|
}()
|
|
|
|
defer signal.Stop(c)
|
|
|
|
|
|
|
|
fmt.Fprint(os.Stderr, "Enter password: ")
|
2021-03-29 21:39:08 +03:00
|
|
|
plaintext, err = term.ReadPassword(fd)
|
2020-05-21 22:09:49 +03:00
|
|
|
fmt.Fprintln(os.Stderr)
|
2020-05-11 23:10:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup, err
|
|
|
|
}
|
|
|
|
|
2020-05-21 22:09:49 +03:00
|
|
|
fmt.Fprint(os.Stderr, "Confirm password: ")
|
2021-03-29 21:39:08 +03:00
|
|
|
confirmation, err := term.ReadPassword(fd)
|
2020-05-21 22:09:49 +03:00
|
|
|
fmt.Fprintln(os.Stderr)
|
2020-05-11 23:10:47 +03: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 00:08:45 +03:00
|
|
|
}
|
|
|
|
|
2019-10-10 23:37:27 +03:00
|
|
|
var hash []byte
|
2022-09-05 22:32:58 +03:00
|
|
|
var hashString string
|
2019-10-10 23:37:27 +03:00
|
|
|
switch algorithm {
|
|
|
|
case "bcrypt":
|
2024-02-12 22:33:54 +03:00
|
|
|
hash, err = BcryptHash{}.Hash(plaintext)
|
2022-09-05 22:32:58 +03:00
|
|
|
hashString = string(hash)
|
2019-10-10 23:37:27 +03:00
|
|
|
default:
|
|
|
|
return caddy.ExitCodeFailedStartup, fmt.Errorf("unrecognized hash algorithm: %s", algorithm)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup, err
|
|
|
|
}
|
|
|
|
|
2022-09-05 22:32:58 +03:00
|
|
|
fmt.Println(hashString)
|
2019-10-10 23:37:27 +03:00
|
|
|
|
|
|
|
return 0, nil
|
|
|
|
}
|