mirror of
https://github.com/mjl-/mox.git
synced 2025-06-28 10:18:15 +03:00

to facilitate migrations from/to other mail setups. a domain can be added in "disabled" mode (or can be disabled/enabled later on). you can configure a disabled domain, but incoming/outgoing messages involving the domain are rejected with temporary error codes (as this may occur during a migration, remote servers will try again, hopefully to the correct machine or after this machine has been configured correctly). also, no acme tls certs will be requested for disabled domains (the autoconfig/mta-sts dns records may still point to the current/previous machine). accounts with addresses at disabled domains can still login, unless logins are disabled for their accounts. an account now has an option to disable logins. you can specify an error message to show. this will be shown in smtp, imap and the web interfaces. it could contain a message about migrations, and possibly a URL to a page with information about how to migrate. incoming/outgoing email involving accounts with login disabled are still accepted/delivered as normal (unless the domain involved in the messages is disabled too). account operations by the admin, such as importing/exporting messages still works. in the admin web interface, listings of domains/accounts show if they are disabled. domains & accounts can be enabled/disabled through the config file, cli commands and admin web interface. for issue #175 by RobSlgm
129 lines
3.6 KiB
Go
129 lines
3.6 KiB
Go
package webauth
|
|
|
|
import (
|
|
"context"
|
|
cryptorand "crypto/rand"
|
|
"encoding/base64"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
"golang.org/x/text/secure/precis"
|
|
|
|
"github.com/mjl-/mox/mlog"
|
|
"github.com/mjl-/mox/mox-"
|
|
"github.com/mjl-/mox/store"
|
|
)
|
|
|
|
// Admin is for admin logins, with authentication by password, and sessions only
|
|
// stored in memory only, with lifetime 12 hour after last use, with a maximum of
|
|
// 10 active sessions.
|
|
var Admin SessionAuth = &adminSessionAuth{
|
|
sessions: map[store.SessionToken]adminSession{},
|
|
}
|
|
|
|
// Good chance of fitting one working day.
|
|
const adminSessionLifetime = 12 * time.Hour
|
|
|
|
type adminSession struct {
|
|
sessionToken store.SessionToken
|
|
csrfToken store.CSRFToken
|
|
expires time.Time
|
|
}
|
|
|
|
type adminSessionAuth struct {
|
|
sync.Mutex
|
|
sessions map[store.SessionToken]adminSession
|
|
}
|
|
|
|
func (a *adminSessionAuth) login(ctx context.Context, log mlog.Log, username, password string) (valid, disabled bool, name string, rerr error) {
|
|
a.Lock()
|
|
defer a.Unlock()
|
|
|
|
p := mox.ConfigDirPath(mox.Conf.Static.AdminPasswordFile)
|
|
buf, err := os.ReadFile(p)
|
|
if err != nil {
|
|
return false, false, "", fmt.Errorf("reading password file: %v", err)
|
|
}
|
|
passwordhash := strings.TrimSpace(string(buf))
|
|
// Transform with precis, if valid. ../rfc/8265:679
|
|
pw, err := precis.OpaqueString.String(password)
|
|
if err == nil {
|
|
password = pw
|
|
}
|
|
if err := bcrypt.CompareHashAndPassword([]byte(passwordhash), []byte(password)); err != nil {
|
|
return false, false, "", nil
|
|
}
|
|
|
|
return true, false, "", nil
|
|
}
|
|
|
|
func (a *adminSessionAuth) add(ctx context.Context, log mlog.Log, accountName string, loginAddress string) (sessionToken store.SessionToken, csrfToken store.CSRFToken, rerr error) {
|
|
a.Lock()
|
|
defer a.Unlock()
|
|
|
|
// Cleanup expired sessions.
|
|
for st, s := range a.sessions {
|
|
if time.Until(s.expires) < 0 {
|
|
delete(a.sessions, st)
|
|
}
|
|
}
|
|
|
|
// Ensure we have at most 10 sessions.
|
|
if len(a.sessions) > 10 {
|
|
var oldest *store.SessionToken
|
|
for _, s := range a.sessions {
|
|
if oldest == nil || s.expires.Before(a.sessions[*oldest].expires) {
|
|
oldest = &s.sessionToken
|
|
}
|
|
}
|
|
delete(a.sessions, *oldest)
|
|
}
|
|
|
|
// Generate new tokens.
|
|
var sessionData, csrfData [16]byte
|
|
if _, err := cryptorand.Read(sessionData[:]); err != nil {
|
|
return "", "", err
|
|
}
|
|
if _, err := cryptorand.Read(csrfData[:]); err != nil {
|
|
return "", "", err
|
|
}
|
|
sessionToken = store.SessionToken(base64.RawURLEncoding.EncodeToString(sessionData[:]))
|
|
csrfToken = store.CSRFToken(base64.RawURLEncoding.EncodeToString(csrfData[:]))
|
|
|
|
// Register session.
|
|
a.sessions[sessionToken] = adminSession{sessionToken, csrfToken, time.Now().Add(adminSessionLifetime)}
|
|
return sessionToken, csrfToken, nil
|
|
}
|
|
|
|
func (a *adminSessionAuth) use(ctx context.Context, log mlog.Log, accountName string, sessionToken store.SessionToken, csrfToken store.CSRFToken) (loginAddress string, rerr error) {
|
|
a.Lock()
|
|
defer a.Unlock()
|
|
|
|
s, ok := a.sessions[sessionToken]
|
|
if !ok {
|
|
return "", fmt.Errorf("unknown session (due to server restart or 10 new admin sessions)")
|
|
} else if time.Until(s.expires) < 0 {
|
|
return "", fmt.Errorf("session expired (after 12 hours inactivity)")
|
|
} else if csrfToken != "" && csrfToken != s.csrfToken {
|
|
return "", fmt.Errorf("mismatch between csrf and session tokens")
|
|
}
|
|
s.expires = time.Now().Add(adminSessionLifetime)
|
|
a.sessions[sessionToken] = s
|
|
return "", nil
|
|
}
|
|
|
|
func (a *adminSessionAuth) remove(ctx context.Context, log mlog.Log, accountName string, sessionToken store.SessionToken) error {
|
|
a.Lock()
|
|
defer a.Unlock()
|
|
|
|
if _, ok := a.sessions[sessionToken]; !ok {
|
|
return fmt.Errorf("unknown session")
|
|
}
|
|
delete(a.sessions, sessionToken)
|
|
return nil
|
|
}
|