2015-04-21 21:57:12 +02:00
|
|
|
package basic
|
|
|
|
|
|
|
|
import (
|
2015-06-09 03:56:48 +02:00
|
|
|
"bufio"
|
2015-04-21 21:57:12 +02:00
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/base64"
|
2015-06-09 03:56:48 +02:00
|
|
|
"io"
|
2015-04-21 21:57:12 +02:00
|
|
|
"os"
|
2015-06-04 17:46:34 +02:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2015-06-04 18:02:13 +02:00
|
|
|
|
2015-06-09 03:56:48 +02:00
|
|
|
"github.com/docker/distribution/context"
|
2015-06-04 17:46:34 +02:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2015-04-21 21:57:12 +02:00
|
|
|
)
|
|
|
|
|
2015-06-09 03:56:48 +02:00
|
|
|
// htpasswd holds a path to a system .htpasswd file and the machinery to parse it.
|
2015-06-06 07:37:32 +02:00
|
|
|
type htpasswd struct {
|
2015-06-09 03:56:48 +02:00
|
|
|
path string
|
2015-04-21 21:57:12 +02:00
|
|
|
}
|
|
|
|
|
2015-06-09 03:56:48 +02:00
|
|
|
// authType represents a particular hash function used in the htpasswd file.
|
|
|
|
type authType int
|
2015-06-04 18:02:13 +02:00
|
|
|
|
2015-06-04 17:46:34 +02:00
|
|
|
const (
|
2015-06-09 03:56:48 +02:00
|
|
|
authTypePlainText authType = iota // Plain-text password storage (htpasswd -p)
|
|
|
|
authTypeSHA1 // sha hashed password storage (htpasswd -s)
|
|
|
|
authTypeApacheMD5 // apr iterated md5 hashing (htpasswd -m)
|
|
|
|
authTypeBCrypt // BCrypt adapative password hashing (htpasswd -B)
|
|
|
|
authTypeCrypt // System crypt() hashes. (htpasswd -d)
|
2015-06-04 17:46:34 +02:00
|
|
|
)
|
|
|
|
|
2015-06-09 03:56:48 +02:00
|
|
|
var bcryptPrefixRegexp = regexp.MustCompile(`^\$2[ab]?y\$`)
|
|
|
|
|
|
|
|
// detectAuthCredentialType inspects the credential and resolves the encryption scheme.
|
|
|
|
func detectAuthCredentialType(cred string) authType {
|
|
|
|
if strings.HasPrefix(cred, "{SHA}") {
|
|
|
|
return authTypeSHA1
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(cred, "$apr1$") {
|
|
|
|
return authTypeApacheMD5
|
|
|
|
}
|
|
|
|
if bcryptPrefixRegexp.MatchString(cred) {
|
|
|
|
return authTypeBCrypt
|
|
|
|
}
|
|
|
|
// There's just not a great way to distinguish between these next two...
|
|
|
|
if len(cred) == 13 {
|
|
|
|
return authTypeCrypt
|
|
|
|
}
|
|
|
|
return authTypePlainText
|
|
|
|
}
|
|
|
|
|
2015-06-06 07:37:32 +02:00
|
|
|
// String Returns a text representation of the AuthType
|
2015-06-09 03:56:48 +02:00
|
|
|
func (at authType) String() string {
|
2015-06-04 18:02:13 +02:00
|
|
|
switch at {
|
2015-06-09 03:56:48 +02:00
|
|
|
case authTypePlainText:
|
2015-06-04 18:02:13 +02:00
|
|
|
return "plaintext"
|
2015-06-09 03:56:48 +02:00
|
|
|
case authTypeSHA1:
|
2015-06-04 18:02:13 +02:00
|
|
|
return "sha1"
|
2015-06-09 03:56:48 +02:00
|
|
|
case authTypeApacheMD5:
|
2015-06-04 18:02:13 +02:00
|
|
|
return "md5"
|
2015-06-09 03:56:48 +02:00
|
|
|
case authTypeBCrypt:
|
2015-06-04 18:02:13 +02:00
|
|
|
return "bcrypt"
|
2015-06-09 03:56:48 +02:00
|
|
|
case authTypeCrypt:
|
2015-06-04 18:02:13 +02:00
|
|
|
return "system crypt"
|
2015-06-04 17:46:34 +02:00
|
|
|
}
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
|
2015-06-06 07:37:32 +02:00
|
|
|
// NewHTPasswd Create a new HTPasswd with the given path to .htpasswd file.
|
2015-06-06 07:58:45 +02:00
|
|
|
func newHTPasswd(htpath string) *htpasswd {
|
2015-06-06 07:37:32 +02:00
|
|
|
return &htpasswd{path: htpath}
|
2015-04-21 21:57:12 +02:00
|
|
|
}
|
|
|
|
|
2015-06-09 03:56:48 +02:00
|
|
|
// AuthenticateUser checks a given user:password credential against the
|
|
|
|
// receiving HTPasswd's file. If the check passes, nil is returned. Note that
|
|
|
|
// this parses the htpasswd file on each request so ensure that updates are
|
|
|
|
// available.
|
|
|
|
func (htpasswd *htpasswd) authenticateUser(ctx context.Context, username string, password string) error {
|
2015-04-21 21:57:12 +02:00
|
|
|
// Open the file.
|
|
|
|
in, err := os.Open(htpasswd.path)
|
|
|
|
if err != nil {
|
2015-06-09 03:56:48 +02:00
|
|
|
return err
|
2015-04-21 21:57:12 +02:00
|
|
|
}
|
2015-06-09 03:56:48 +02:00
|
|
|
defer in.Close()
|
2015-04-21 21:57:12 +02:00
|
|
|
|
2015-06-09 03:56:48 +02:00
|
|
|
for _, entry := range parseHTPasswd(ctx, in) {
|
|
|
|
if entry.username != username {
|
|
|
|
continue // wrong entry
|
2015-06-04 17:46:34 +02:00
|
|
|
}
|
2015-06-09 03:56:48 +02:00
|
|
|
|
|
|
|
switch t := detectAuthCredentialType(entry.password); t {
|
|
|
|
case authTypeSHA1:
|
|
|
|
sha := sha1.New()
|
|
|
|
sha.Write([]byte(password))
|
|
|
|
hash := base64.StdEncoding.EncodeToString(sha.Sum(nil))
|
|
|
|
|
|
|
|
if entry.password[5:] != hash {
|
|
|
|
return ErrAuthenticationFailure
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
case authTypeBCrypt:
|
|
|
|
err := bcrypt.CompareHashAndPassword([]byte(entry.password), []byte(password))
|
|
|
|
if err != nil {
|
|
|
|
return ErrAuthenticationFailure
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
case authTypePlainText:
|
|
|
|
if password != entry.password {
|
|
|
|
return ErrAuthenticationFailure
|
2015-04-21 21:57:12 +02:00
|
|
|
}
|
2015-06-09 03:56:48 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
context.GetLogger(ctx).Errorf("unsupported basic authentication type: %v", t)
|
2015-04-21 21:57:12 +02:00
|
|
|
}
|
|
|
|
}
|
2015-06-09 03:56:48 +02:00
|
|
|
|
|
|
|
return ErrAuthenticationFailure
|
|
|
|
}
|
|
|
|
|
|
|
|
// htpasswdEntry represents a line in an htpasswd file.
|
|
|
|
type htpasswdEntry struct {
|
|
|
|
username string // username, plain text
|
|
|
|
password string // stores hashed passwd
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseHTPasswd parses the contents of htpasswd. Bad entries are skipped and
|
|
|
|
// logged, so this may return empty. This will read all the entries in the
|
|
|
|
// file, whether or not they are needed.
|
|
|
|
func parseHTPasswd(ctx context.Context, rd io.Reader) []htpasswdEntry {
|
|
|
|
entries := []htpasswdEntry{}
|
|
|
|
scanner := bufio.NewScanner(rd)
|
|
|
|
for scanner.Scan() {
|
|
|
|
t := strings.TrimSpace(scanner.Text())
|
|
|
|
i := strings.Index(t, ":")
|
|
|
|
if i < 0 || i >= len(t) {
|
|
|
|
context.GetLogger(ctx).Errorf("bad entry in htpasswd: %q", t)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
entries = append(entries, htpasswdEntry{
|
|
|
|
username: t[:i],
|
|
|
|
password: t[i+1:],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return entries
|
2015-04-21 21:57:12 +02:00
|
|
|
}
|