2017-03-27 14:15:02 +02:00
|
|
|
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
|
2016-09-04 11:25:33 +02:00
|
|
|
// released under the MIT license
|
|
|
|
|
|
|
|
package irc
|
|
|
|
|
2016-09-06 08:31:59 +02:00
|
|
|
import (
|
2016-09-07 12:46:01 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2018-02-20 10:20:30 +01:00
|
|
|
"net/smtp"
|
2016-09-07 12:46:01 +02:00
|
|
|
"strconv"
|
2018-02-11 11:30:40 +01:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2018-04-19 08:48:19 +02:00
|
|
|
"sync/atomic"
|
2016-09-06 08:31:59 +02:00
|
|
|
"time"
|
2018-08-06 04:51:39 +02:00
|
|
|
"unicode"
|
2016-09-06 08:31:59 +02:00
|
|
|
|
2017-09-29 04:07:52 +02:00
|
|
|
"github.com/oragono/oragono/irc/caps"
|
2020-02-11 12:35:17 +01:00
|
|
|
"github.com/oragono/oragono/irc/ldap"
|
2018-02-11 11:30:40 +01:00
|
|
|
"github.com/oragono/oragono/irc/passwd"
|
2018-11-26 11:23:27 +01:00
|
|
|
"github.com/oragono/oragono/irc/utils"
|
2016-09-07 12:46:01 +02:00
|
|
|
"github.com/tidwall/buntdb"
|
2016-09-06 08:31:59 +02:00
|
|
|
)
|
2016-09-04 11:25:33 +02:00
|
|
|
|
2017-03-11 13:01:40 +01:00
|
|
|
const (
|
2018-02-20 10:20:30 +01:00
|
|
|
keyAccountExists = "account.exists %s"
|
|
|
|
keyAccountVerified = "account.verified %s"
|
|
|
|
keyAccountCallback = "account.callback %s"
|
|
|
|
keyAccountVerificationCode = "account.verificationcode %s"
|
|
|
|
keyAccountName = "account.name %s" // stores the 'preferred name' of the account, not casemapped
|
|
|
|
keyAccountRegTime = "account.registered.time %s"
|
|
|
|
keyAccountCredentials = "account.credentials %s"
|
2018-03-02 23:04:24 +01:00
|
|
|
keyAccountAdditionalNicks = "account.additionalnicks %s"
|
2019-05-19 10:27:44 +02:00
|
|
|
keyAccountSettings = "account.settings %s"
|
2018-04-19 08:48:19 +02:00
|
|
|
keyAccountVHost = "account.vhost %s"
|
2018-02-20 10:20:30 +01:00
|
|
|
keyCertToAccount = "account.creds.certfp %s"
|
2020-02-19 01:38:42 +01:00
|
|
|
keyAccountChannels = "account.channels %s" // channels registered to the account
|
|
|
|
keyAccountJoinedChannels = "account.joinedto %s" // channels a persistent client has joined
|
2020-02-20 08:33:49 +01:00
|
|
|
keyAccountLastSignoff = "account.lastsignoff %s"
|
2018-04-19 08:48:19 +02:00
|
|
|
|
|
|
|
keyVHostQueueAcctToId = "vhostQueue %s"
|
|
|
|
vhostRequestIdx = "vhostQueue"
|
2019-12-29 17:59:49 +01:00
|
|
|
|
|
|
|
maxCertfpsPerAccount = 5
|
2017-03-11 13:01:40 +01:00
|
|
|
)
|
|
|
|
|
2018-02-11 11:30:40 +01:00
|
|
|
// everything about accounts is persistent; therefore, the database is the authoritative
|
|
|
|
// source of truth for all account information. anything on the heap is just a cache
|
|
|
|
type AccountManager struct {
|
2018-04-19 08:48:19 +02:00
|
|
|
// XXX these are up here so they can be aligned to a 64-bit boundary, please forgive me
|
|
|
|
// autoincrementing ID for vhost requests:
|
|
|
|
vhostRequestID uint64
|
|
|
|
vhostRequestPendingCount uint64
|
|
|
|
|
2018-02-11 11:30:40 +01:00
|
|
|
sync.RWMutex // tier 2
|
|
|
|
serialCacheUpdateMutex sync.Mutex // tier 3
|
2018-04-19 08:48:19 +02:00
|
|
|
vHostUpdateMutex sync.Mutex // tier 3
|
2018-02-11 11:30:40 +01:00
|
|
|
|
|
|
|
server *Server
|
|
|
|
// track clients logged in to accounts
|
2019-01-31 00:59:49 +01:00
|
|
|
accountToClients map[string][]*Client
|
|
|
|
nickToAccount map[string]string
|
|
|
|
skeletonToAccount map[string]string
|
2019-05-19 10:27:44 +02:00
|
|
|
accountToMethod map[string]NickEnforcementMethod
|
2018-02-11 11:30:40 +01:00
|
|
|
}
|
|
|
|
|
2019-03-12 00:24:45 +01:00
|
|
|
func (am *AccountManager) Initialize(server *Server) {
|
|
|
|
am.accountToClients = make(map[string][]*Client)
|
|
|
|
am.nickToAccount = make(map[string]string)
|
|
|
|
am.skeletonToAccount = make(map[string]string)
|
2019-05-19 10:27:44 +02:00
|
|
|
am.accountToMethod = make(map[string]NickEnforcementMethod)
|
2019-03-12 00:24:45 +01:00
|
|
|
am.server = server
|
2018-02-11 11:30:40 +01:00
|
|
|
|
2019-12-22 02:26:40 +01:00
|
|
|
config := server.Config()
|
|
|
|
am.buildNickToAccountIndex(config)
|
|
|
|
am.initVHostRequestQueue(config)
|
2020-02-19 01:38:42 +01:00
|
|
|
am.createAlwaysOnClients(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *AccountManager) createAlwaysOnClients(config *Config) {
|
|
|
|
if config.Accounts.Bouncer.AlwaysOn == PersistentDisabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
verifiedPrefix := fmt.Sprintf(keyAccountVerified, "")
|
|
|
|
|
|
|
|
am.serialCacheUpdateMutex.Lock()
|
|
|
|
defer am.serialCacheUpdateMutex.Unlock()
|
|
|
|
|
|
|
|
var accounts []string
|
|
|
|
|
|
|
|
am.server.store.View(func(tx *buntdb.Tx) error {
|
|
|
|
err := tx.AscendGreaterOrEqual("", verifiedPrefix, func(key, value string) bool {
|
|
|
|
if !strings.HasPrefix(key, verifiedPrefix) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
account := strings.TrimPrefix(key, verifiedPrefix)
|
|
|
|
accounts = append(accounts, account)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, accountName := range accounts {
|
|
|
|
account, err := am.LoadAccount(accountName)
|
|
|
|
if err == nil && account.Verified &&
|
|
|
|
persistenceEnabled(config.Accounts.Bouncer.AlwaysOn, account.Settings.AlwaysOn) {
|
2020-02-20 08:33:49 +01:00
|
|
|
am.server.AddAlwaysOnClient(account, am.loadChannels(accountName), am.loadLastSignoff(accountName))
|
2020-02-19 01:38:42 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-11 11:30:40 +01:00
|
|
|
}
|
|
|
|
|
2019-12-22 02:19:19 +01:00
|
|
|
func (am *AccountManager) buildNickToAccountIndex(config *Config) {
|
|
|
|
if !config.Accounts.NickReservation.Enabled {
|
2018-02-11 11:30:40 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-02 16:08:44 +01:00
|
|
|
nickToAccount := make(map[string]string)
|
2019-01-31 00:59:49 +01:00
|
|
|
skeletonToAccount := make(map[string]string)
|
2019-05-19 10:27:44 +02:00
|
|
|
accountToMethod := make(map[string]NickEnforcementMethod)
|
2018-02-11 11:30:40 +01:00
|
|
|
existsPrefix := fmt.Sprintf(keyAccountExists, "")
|
|
|
|
|
|
|
|
am.serialCacheUpdateMutex.Lock()
|
|
|
|
defer am.serialCacheUpdateMutex.Unlock()
|
|
|
|
|
|
|
|
err := am.server.store.View(func(tx *buntdb.Tx) error {
|
|
|
|
err := tx.AscendGreaterOrEqual("", existsPrefix, func(key, value string) bool {
|
|
|
|
if !strings.HasPrefix(key, existsPrefix) {
|
|
|
|
return false
|
|
|
|
}
|
2019-01-02 16:08:44 +01:00
|
|
|
|
|
|
|
account := strings.TrimPrefix(key, existsPrefix)
|
|
|
|
if _, err := tx.Get(fmt.Sprintf(keyAccountVerified, account)); err == nil {
|
|
|
|
nickToAccount[account] = account
|
2019-01-31 00:59:49 +01:00
|
|
|
accountName, err := tx.Get(fmt.Sprintf(keyAccountName, account))
|
|
|
|
if err != nil {
|
|
|
|
am.server.logger.Error("internal", "missing account name for", account)
|
|
|
|
} else {
|
|
|
|
skeleton, _ := Skeleton(accountName)
|
|
|
|
skeletonToAccount[skeleton] = account
|
|
|
|
}
|
2018-02-11 11:30:40 +01:00
|
|
|
}
|
2019-01-02 16:08:44 +01:00
|
|
|
if rawNicks, err := tx.Get(fmt.Sprintf(keyAccountAdditionalNicks, account)); err == nil {
|
2018-03-02 23:04:24 +01:00
|
|
|
additionalNicks := unmarshalReservedNicks(rawNicks)
|
|
|
|
for _, nick := range additionalNicks {
|
2019-01-31 00:59:49 +01:00
|
|
|
cfnick, _ := CasefoldName(nick)
|
|
|
|
nickToAccount[cfnick] = account
|
|
|
|
skeleton, _ := Skeleton(nick)
|
|
|
|
skeletonToAccount[skeleton] = account
|
2019-01-02 16:08:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-19 10:27:44 +02:00
|
|
|
if rawPrefs, err := tx.Get(fmt.Sprintf(keyAccountSettings, account)); err == nil {
|
|
|
|
var prefs AccountSettings
|
|
|
|
err := json.Unmarshal([]byte(rawPrefs), &prefs)
|
|
|
|
if err == nil && prefs.NickEnforcement != NickEnforcementOptional {
|
|
|
|
accountToMethod[account] = prefs.NickEnforcement
|
2019-07-12 17:49:01 +02:00
|
|
|
} else if err != nil {
|
2019-05-19 10:27:44 +02:00
|
|
|
am.server.logger.Error("internal", "corrupt account creds", account)
|
2018-03-02 23:04:24 +01:00
|
|
|
}
|
|
|
|
}
|
2019-05-19 10:27:44 +02:00
|
|
|
|
2018-02-11 11:30:40 +01:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2018-12-31 17:33:42 +01:00
|
|
|
am.server.logger.Error("internal", "couldn't read reserved nicks", err.Error())
|
2018-02-11 11:30:40 +01:00
|
|
|
} else {
|
|
|
|
am.Lock()
|
2019-01-02 16:08:44 +01:00
|
|
|
am.nickToAccount = nickToAccount
|
2019-01-31 00:59:49 +01:00
|
|
|
am.skeletonToAccount = skeletonToAccount
|
2019-01-02 16:08:44 +01:00
|
|
|
am.accountToMethod = accountToMethod
|
2018-02-11 11:30:40 +01:00
|
|
|
am.Unlock()
|
|
|
|
}
|
2018-04-19 08:48:19 +02:00
|
|
|
}
|
2018-02-11 11:30:40 +01:00
|
|
|
|
2019-12-22 02:26:40 +01:00
|
|
|
func (am *AccountManager) initVHostRequestQueue(config *Config) {
|
|
|
|
if !config.Accounts.VHosts.Enabled {
|
2018-04-19 08:48:19 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
am.vHostUpdateMutex.Lock()
|
|
|
|
defer am.vHostUpdateMutex.Unlock()
|
|
|
|
|
|
|
|
// the db maps the account name to the autoincrementing integer ID of its request
|
|
|
|
// create an numerically ordered index on ID, so we can list the oldest requests
|
|
|
|
// finally, collect the integer id of the newest request and the total request count
|
|
|
|
var total uint64
|
|
|
|
var lastIDStr string
|
|
|
|
err := am.server.store.Update(func(tx *buntdb.Tx) error {
|
|
|
|
err := tx.CreateIndex(vhostRequestIdx, fmt.Sprintf(keyVHostQueueAcctToId, "*"), buntdb.IndexInt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tx.Descend(vhostRequestIdx, func(key, value string) bool {
|
|
|
|
if lastIDStr == "" {
|
|
|
|
lastIDStr = value
|
|
|
|
}
|
|
|
|
total++
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
am.server.logger.Error("internal", "could not create vhost queue index", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
lastID, _ := strconv.ParseUint(lastIDStr, 10, 64)
|
|
|
|
am.server.logger.Debug("services", fmt.Sprintf("vhost queue length is %d, autoincrementing id is %d", total, lastID))
|
|
|
|
|
|
|
|
atomic.StoreUint64(&am.vhostRequestID, lastID)
|
|
|
|
atomic.StoreUint64(&am.vhostRequestPendingCount, total)
|
2018-02-11 11:30:40 +01:00
|
|
|
}
|
|
|
|
|
2018-03-02 23:04:24 +01:00
|
|
|
func (am *AccountManager) NickToAccount(nick string) string {
|
|
|
|
cfnick, err := CasefoldName(nick)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2018-02-11 11:30:40 +01:00
|
|
|
am.RLock()
|
|
|
|
defer am.RUnlock()
|
|
|
|
return am.nickToAccount[cfnick]
|
|
|
|
}
|
|
|
|
|
2019-05-19 10:27:44 +02:00
|
|
|
// given an account, combine stored enforcement method with the config settings
|
|
|
|
// to compute the actual enforcement method
|
|
|
|
func configuredEnforcementMethod(config *Config, storedMethod NickEnforcementMethod) (result NickEnforcementMethod) {
|
|
|
|
if !config.Accounts.NickReservation.Enabled {
|
|
|
|
return NickEnforcementNone
|
|
|
|
}
|
|
|
|
result = storedMethod
|
|
|
|
// if they don't have a custom setting, or customization is disabled, use the default
|
|
|
|
if result == NickEnforcementOptional || !config.Accounts.NickReservation.AllowCustomEnforcement {
|
|
|
|
result = config.Accounts.NickReservation.Method
|
|
|
|
}
|
|
|
|
if result == NickEnforcementOptional {
|
|
|
|
// enforcement was explicitly enabled neither in the config or by the user
|
|
|
|
result = NickEnforcementNone
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-02 16:08:44 +01:00
|
|
|
// Given a nick, looks up the account that owns it and the method (none/timeout/strict)
|
|
|
|
// used to enforce ownership.
|
2019-05-19 10:27:44 +02:00
|
|
|
func (am *AccountManager) EnforcementStatus(cfnick, skeleton string) (account string, method NickEnforcementMethod) {
|
2019-01-02 16:08:44 +01:00
|
|
|
config := am.server.Config()
|
|
|
|
if !config.Accounts.NickReservation.Enabled {
|
2019-05-19 10:27:44 +02:00
|
|
|
return "", NickEnforcementNone
|
2019-01-02 16:08:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
am.RLock()
|
|
|
|
defer am.RUnlock()
|
|
|
|
|
2019-05-19 10:27:44 +02:00
|
|
|
finalEnforcementMethod := func(account_ string) (result NickEnforcementMethod) {
|
|
|
|
storedMethod := am.accountToMethod[account_]
|
|
|
|
return configuredEnforcementMethod(config, storedMethod)
|
2019-01-04 05:44:01 +01:00
|
|
|
}
|
2019-01-31 00:59:49 +01:00
|
|
|
|
|
|
|
nickAccount := am.nickToAccount[cfnick]
|
|
|
|
skelAccount := am.skeletonToAccount[skeleton]
|
|
|
|
if nickAccount == "" && skelAccount == "" {
|
2019-05-19 10:27:44 +02:00
|
|
|
return "", NickEnforcementNone
|
2019-01-31 23:34:06 +01:00
|
|
|
} else if nickAccount != "" && (skelAccount == nickAccount || skelAccount == "") {
|
|
|
|
return nickAccount, finalEnforcementMethod(nickAccount)
|
|
|
|
} else if skelAccount != "" && nickAccount == "" {
|
|
|
|
return skelAccount, finalEnforcementMethod(skelAccount)
|
|
|
|
} else {
|
|
|
|
// nickAccount != skelAccount and both are nonempty:
|
2019-01-31 00:59:49 +01:00
|
|
|
// two people have competing claims on (this casefolding of) this nick!
|
|
|
|
nickMethod := finalEnforcementMethod(nickAccount)
|
|
|
|
skelMethod := finalEnforcementMethod(skelAccount)
|
|
|
|
switch {
|
2019-05-19 10:27:44 +02:00
|
|
|
case skelMethod == NickEnforcementNone:
|
2019-01-31 00:59:49 +01:00
|
|
|
return nickAccount, nickMethod
|
2019-05-19 10:27:44 +02:00
|
|
|
case nickMethod == NickEnforcementNone:
|
2019-01-31 00:59:49 +01:00
|
|
|
return skelAccount, skelMethod
|
|
|
|
default:
|
|
|
|
// nobody can use this nick
|
2019-05-19 10:27:44 +02:00
|
|
|
return "!", NickEnforcementStrict
|
2019-01-31 00:59:49 +01:00
|
|
|
}
|
2019-01-02 16:08:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets a custom enforcement method for an account and stores it in the database.
|
2019-05-19 10:27:44 +02:00
|
|
|
func (am *AccountManager) SetEnforcementStatus(account string, method NickEnforcementMethod) (finalSettings AccountSettings, err error) {
|
2019-01-02 16:08:44 +01:00
|
|
|
config := am.server.Config()
|
|
|
|
if !(config.Accounts.NickReservation.Enabled && config.Accounts.NickReservation.AllowCustomEnforcement) {
|
2019-05-19 10:27:44 +02:00
|
|
|
err = errFeatureDisabled
|
|
|
|
return
|
2019-01-02 16:08:44 +01:00
|
|
|
}
|
|
|
|
|
2019-05-19 10:27:44 +02:00
|
|
|
setter := func(in AccountSettings) (out AccountSettings, err error) {
|
|
|
|
out = in
|
|
|
|
out.NickEnforcement = method
|
|
|
|
return out, nil
|
2019-01-02 16:08:44 +01:00
|
|
|
}
|
|
|
|
|
2019-05-19 10:27:44 +02:00
|
|
|
_, err = am.ModifyAccountSettings(account, setter)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-01-02 16:08:44 +01:00
|
|
|
|
2019-05-19 10:27:44 +02:00
|
|
|
// this update of the data plane is racey, but it's probably fine
|
2019-01-02 16:08:44 +01:00
|
|
|
am.Lock()
|
|
|
|
defer am.Unlock()
|
|
|
|
|
2019-05-19 10:27:44 +02:00
|
|
|
if method == NickEnforcementOptional {
|
|
|
|
delete(am.accountToMethod, account)
|
|
|
|
} else {
|
|
|
|
am.accountToMethod[account] = method
|
2019-01-02 16:08:44 +01:00
|
|
|
}
|
|
|
|
|
2019-05-19 10:27:44 +02:00
|
|
|
return
|
2019-01-02 16:08:44 +01:00
|
|
|
}
|
|
|
|
|
2018-04-19 08:48:19 +02:00
|
|
|
func (am *AccountManager) AccountToClients(account string) (result []*Client) {
|
|
|
|
cfaccount, err := CasefoldName(account)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
am.RLock()
|
|
|
|
defer am.RUnlock()
|
|
|
|
return am.accountToClients[cfaccount]
|
|
|
|
}
|
|
|
|
|
2018-02-11 11:30:40 +01:00
|
|
|
func (am *AccountManager) Register(client *Client, account string, callbackNamespace string, callbackValue string, passphrase string, certfp string) error {
|
|
|
|
casefoldedAccount, err := CasefoldName(account)
|
2019-01-31 00:59:49 +01:00
|
|
|
skeleton, skerr := Skeleton(account)
|
|
|
|
if err != nil || skerr != nil || account == "" || account == "*" {
|
2018-02-11 11:30:40 +01:00
|
|
|
return errAccountCreation
|
|
|
|
}
|
|
|
|
|
2019-05-24 19:09:56 +02:00
|
|
|
if restrictedCasefoldedNicks[casefoldedAccount] || restrictedSkeletons[skeleton] {
|
2019-01-31 00:59:49 +01:00
|
|
|
return errAccountAlreadyRegistered
|
|
|
|
}
|
|
|
|
|
2018-08-06 04:51:39 +02:00
|
|
|
config := am.server.AccountConfig()
|
2019-02-15 01:51:55 +01:00
|
|
|
|
|
|
|
// final "is registration allowed" check, probably redundant:
|
|
|
|
if !(config.Registration.Enabled || callbackNamespace == "admin") {
|
|
|
|
return errFeatureDisabled
|
|
|
|
}
|
|
|
|
|
2019-02-06 01:03:42 +01:00
|
|
|
// if nick reservation is enabled, you can only register your current nickname
|
|
|
|
// as an account; this prevents "land-grab" situations where someone else
|
|
|
|
// registers your nick out from under you and then NS GHOSTs you
|
|
|
|
// n.b. client is nil during a SAREGISTER:
|
2019-02-10 11:53:36 +01:00
|
|
|
if config.NickReservation.Enabled && client != nil && client.NickCasefolded() != casefoldedAccount {
|
2019-02-06 01:03:42 +01:00
|
|
|
return errAccountMustHoldNick
|
|
|
|
}
|
|
|
|
|
|
|
|
// can't register a guest nickname
|
2018-08-06 04:51:39 +02:00
|
|
|
renamePrefix := strings.ToLower(config.NickReservation.RenamePrefix)
|
2018-02-18 10:46:14 +01:00
|
|
|
if renamePrefix != "" && strings.HasPrefix(casefoldedAccount, renamePrefix) {
|
|
|
|
return errAccountAlreadyRegistered
|
|
|
|
}
|
|
|
|
|
2018-02-11 11:30:40 +01:00
|
|
|
accountKey := fmt.Sprintf(keyAccountExists, casefoldedAccount)
|
|
|
|
accountNameKey := fmt.Sprintf(keyAccountName, casefoldedAccount)
|
2018-02-20 10:20:30 +01:00
|
|
|
callbackKey := fmt.Sprintf(keyAccountCallback, casefoldedAccount)
|
2018-02-11 11:30:40 +01:00
|
|
|
registeredTimeKey := fmt.Sprintf(keyAccountRegTime, casefoldedAccount)
|
|
|
|
credentialsKey := fmt.Sprintf(keyAccountCredentials, casefoldedAccount)
|
2018-02-20 10:20:30 +01:00
|
|
|
verificationCodeKey := fmt.Sprintf(keyAccountVerificationCode, casefoldedAccount)
|
2018-02-11 11:30:40 +01:00
|
|
|
certFPKey := fmt.Sprintf(keyCertToAccount, certfp)
|
|
|
|
|
2019-12-29 17:59:49 +01:00
|
|
|
var creds AccountCredentials
|
|
|
|
creds.Version = 1
|
|
|
|
err = creds.SetPassphrase(passphrase, am.server.Config().Accounts.Registration.BcryptCost)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
creds.AddCertfp(certfp)
|
|
|
|
credStr, err := creds.Serialize()
|
2018-02-11 11:30:40 +01:00
|
|
|
if err != nil {
|
2018-08-06 04:51:39 +02:00
|
|
|
return err
|
2018-02-11 11:30:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
registeredTimeStr := strconv.FormatInt(time.Now().Unix(), 10)
|
2018-02-20 10:20:30 +01:00
|
|
|
callbackSpec := fmt.Sprintf("%s:%s", callbackNamespace, callbackValue)
|
2018-02-11 11:30:40 +01:00
|
|
|
|
|
|
|
var setOptions *buntdb.SetOptions
|
2020-02-20 06:09:39 +01:00
|
|
|
ttl := time.Duration(config.Registration.VerifyTimeout)
|
2018-02-11 11:30:40 +01:00
|
|
|
if ttl != 0 {
|
|
|
|
setOptions = &buntdb.SetOptions{Expires: true, TTL: ttl}
|
|
|
|
}
|
|
|
|
|
2018-03-11 18:59:02 +01:00
|
|
|
err = func() error {
|
|
|
|
am.serialCacheUpdateMutex.Lock()
|
|
|
|
defer am.serialCacheUpdateMutex.Unlock()
|
|
|
|
|
|
|
|
// can't register an account with the same name as a registered nick
|
|
|
|
if am.NickToAccount(casefoldedAccount) != "" {
|
2018-02-11 11:30:40 +01:00
|
|
|
return errAccountAlreadyRegistered
|
|
|
|
}
|
|
|
|
|
2018-03-11 18:59:02 +01:00
|
|
|
return am.server.store.Update(func(tx *buntdb.Tx) error {
|
|
|
|
_, err := am.loadRawAccount(tx, casefoldedAccount)
|
|
|
|
if err != errAccountDoesNotExist {
|
|
|
|
return errAccountAlreadyRegistered
|
2018-02-11 11:30:40 +01:00
|
|
|
}
|
|
|
|
|
2018-03-11 18:59:02 +01:00
|
|
|
if certfp != "" {
|
|
|
|
// make sure certfp doesn't already exist because that'd be silly
|
|
|
|
_, err := tx.Get(certFPKey)
|
|
|
|
if err != buntdb.ErrNotFound {
|
|
|
|
return errCertfpAlreadyExists
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.Set(accountKey, "1", setOptions)
|
|
|
|
tx.Set(accountNameKey, account, setOptions)
|
|
|
|
tx.Set(registeredTimeKey, registeredTimeStr, setOptions)
|
|
|
|
tx.Set(credentialsKey, credStr, setOptions)
|
|
|
|
tx.Set(callbackKey, callbackSpec, setOptions)
|
2020-01-06 21:00:26 +01:00
|
|
|
if certfp != "" {
|
|
|
|
tx.Set(certFPKey, casefoldedAccount, setOptions)
|
|
|
|
}
|
2018-03-11 18:59:02 +01:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}()
|
2018-02-11 11:30:40 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-20 10:20:30 +01:00
|
|
|
code, err := am.dispatchCallback(client, casefoldedAccount, callbackNamespace, callbackValue)
|
|
|
|
if err != nil {
|
|
|
|
am.Unregister(casefoldedAccount)
|
|
|
|
return errCallbackFailed
|
|
|
|
} else {
|
|
|
|
return am.server.store.Update(func(tx *buntdb.Tx) error {
|
|
|
|
_, _, err = tx.Set(verificationCodeKey, code, setOptions)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-06 04:51:39 +02:00
|
|
|
// validatePassphrase checks whether a passphrase is allowed by our rules
|
|
|
|
func validatePassphrase(passphrase string) error {
|
|
|
|
// sanity check the length
|
|
|
|
if len(passphrase) == 0 || len(passphrase) > 600 {
|
|
|
|
return errAccountBadPassphrase
|
2019-05-29 10:25:20 +02:00
|
|
|
}
|
|
|
|
// we use * as a placeholder in some places, if it's gotten this far then fail
|
|
|
|
if passphrase == "*" {
|
|
|
|
return errAccountBadPassphrase
|
2018-08-06 04:51:39 +02:00
|
|
|
}
|
|
|
|
// for now, just enforce that spaces are not allowed
|
|
|
|
for _, r := range passphrase {
|
|
|
|
if unicode.IsSpace(r) {
|
|
|
|
return errAccountBadPassphrase
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-29 17:59:49 +01:00
|
|
|
// changes the password for an account
|
|
|
|
func (am *AccountManager) setPassword(account string, password string, hasPrivs bool) (err error) {
|
|
|
|
cfAccount, err := CasefoldName(account)
|
|
|
|
if err != nil {
|
|
|
|
return errAccountDoesNotExist
|
|
|
|
}
|
|
|
|
|
|
|
|
credKey := fmt.Sprintf(keyAccountCredentials, cfAccount)
|
|
|
|
var credStr string
|
|
|
|
am.server.store.View(func(tx *buntdb.Tx) error {
|
|
|
|
// no need to check verification status here or below;
|
|
|
|
// you either need to be auth'ed to the account or be an oper to do this
|
|
|
|
credStr, err = tx.Get(credKey)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return errAccountDoesNotExist
|
|
|
|
}
|
|
|
|
|
2018-08-06 04:51:39 +02:00
|
|
|
var creds AccountCredentials
|
2019-12-29 17:59:49 +01:00
|
|
|
err = json.Unmarshal([]byte(credStr), &creds)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-08-06 04:51:39 +02:00
|
|
|
}
|
|
|
|
|
2020-02-11 12:35:17 +01:00
|
|
|
if !hasPrivs && creds.Empty() {
|
|
|
|
return errCredsExternallyManaged
|
|
|
|
}
|
|
|
|
|
2019-12-29 17:59:49 +01:00
|
|
|
err = creds.SetPassphrase(password, am.server.Config().Accounts.Registration.BcryptCost)
|
2018-08-06 04:51:39 +02:00
|
|
|
if err != nil {
|
2019-12-29 17:59:49 +01:00
|
|
|
return err
|
2018-08-06 04:51:39 +02:00
|
|
|
}
|
2019-12-29 17:59:49 +01:00
|
|
|
|
|
|
|
if creds.Empty() && !hasPrivs {
|
|
|
|
return errEmptyCredentials
|
|
|
|
}
|
|
|
|
|
|
|
|
newCredStr, err := creds.Serialize()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = am.server.store.Update(func(tx *buntdb.Tx) error {
|
|
|
|
curCredStr, err := tx.Get(credKey)
|
|
|
|
if credStr != curCredStr {
|
|
|
|
return errCASFailed
|
|
|
|
}
|
|
|
|
_, _, err = tx.Set(credKey, newCredStr, nil)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
2018-08-06 04:51:39 +02:00
|
|
|
}
|
|
|
|
|
2020-02-19 01:38:42 +01:00
|
|
|
func (am *AccountManager) saveChannels(account string, channels []string) {
|
|
|
|
channelsStr := strings.Join(channels, ",")
|
|
|
|
key := fmt.Sprintf(keyAccountJoinedChannels, account)
|
|
|
|
am.server.store.Update(func(tx *buntdb.Tx) error {
|
|
|
|
tx.Set(key, channelsStr, nil)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *AccountManager) loadChannels(account string) (channels []string) {
|
|
|
|
key := fmt.Sprintf(keyAccountJoinedChannels, account)
|
|
|
|
var channelsStr string
|
|
|
|
am.server.store.View(func(tx *buntdb.Tx) error {
|
|
|
|
channelsStr, _ = tx.Get(key)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if channelsStr != "" {
|
|
|
|
return strings.Split(channelsStr, ",")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-20 08:33:49 +01:00
|
|
|
func (am *AccountManager) saveLastSignoff(account string, lastSignoff time.Time) {
|
|
|
|
key := fmt.Sprintf(keyAccountLastSignoff, account)
|
|
|
|
var val string
|
|
|
|
if !lastSignoff.IsZero() {
|
|
|
|
val = strconv.FormatInt(lastSignoff.UnixNano(), 10)
|
|
|
|
}
|
|
|
|
am.server.store.Update(func(tx *buntdb.Tx) error {
|
|
|
|
if val != "" {
|
|
|
|
tx.Set(key, val, nil)
|
|
|
|
} else {
|
|
|
|
tx.Delete(key)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *AccountManager) loadLastSignoff(account string) (lastSignoff time.Time) {
|
|
|
|
key := fmt.Sprintf(keyAccountLastSignoff, account)
|
|
|
|
var lsText string
|
|
|
|
am.server.store.View(func(tx *buntdb.Tx) error {
|
|
|
|
lsText, _ = tx.Get(key)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
lsNum, err := strconv.ParseInt(lsText, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return time.Unix(0, lsNum)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-29 17:59:49 +01:00
|
|
|
func (am *AccountManager) addRemoveCertfp(account, certfp string, add bool, hasPrivs bool) (err error) {
|
|
|
|
certfp, err = utils.NormalizeCertfp(certfp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cfAccount, err := CasefoldName(account)
|
|
|
|
if err != nil {
|
|
|
|
return errAccountDoesNotExist
|
|
|
|
}
|
|
|
|
|
|
|
|
credKey := fmt.Sprintf(keyAccountCredentials, cfAccount)
|
|
|
|
var credStr string
|
|
|
|
am.server.store.View(func(tx *buntdb.Tx) error {
|
|
|
|
credStr, err = tx.Get(credKey)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return errAccountDoesNotExist
|
|
|
|
}
|
|
|
|
|
|
|
|
var creds AccountCredentials
|
|
|
|
err = json.Unmarshal([]byte(credStr), &creds)
|
2018-08-06 04:51:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-29 17:59:49 +01:00
|
|
|
|
2020-02-11 12:35:17 +01:00
|
|
|
if !hasPrivs && creds.Empty() {
|
|
|
|
return errCredsExternallyManaged
|
|
|
|
}
|
|
|
|
|
2019-12-29 17:59:49 +01:00
|
|
|
if add {
|
|
|
|
err = creds.AddCertfp(certfp)
|
|
|
|
} else {
|
|
|
|
err = creds.RemoveCertfp(certfp)
|
|
|
|
}
|
2018-08-06 04:51:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-29 17:59:49 +01:00
|
|
|
if creds.Empty() && !hasPrivs {
|
|
|
|
return errEmptyCredentials
|
|
|
|
}
|
|
|
|
|
|
|
|
newCredStr, err := creds.Serialize()
|
2018-08-06 04:51:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-29 17:59:49 +01:00
|
|
|
certfpKey := fmt.Sprintf(keyCertToAccount, certfp)
|
|
|
|
err = am.server.store.Update(func(tx *buntdb.Tx) error {
|
|
|
|
curCredStr, err := tx.Get(credKey)
|
|
|
|
if credStr != curCredStr {
|
|
|
|
return errCASFailed
|
|
|
|
}
|
|
|
|
if add {
|
|
|
|
_, err = tx.Get(certfpKey)
|
|
|
|
if err != buntdb.ErrNotFound {
|
|
|
|
return errCertfpAlreadyExists
|
|
|
|
}
|
|
|
|
tx.Set(certfpKey, cfAccount, nil)
|
|
|
|
} else {
|
|
|
|
tx.Delete(certfpKey)
|
|
|
|
}
|
|
|
|
_, _, err = tx.Set(credKey, newCredStr, nil)
|
2018-08-06 04:51:39 +02:00
|
|
|
return err
|
|
|
|
})
|
2019-12-29 17:59:49 +01:00
|
|
|
|
|
|
|
return err
|
2018-08-06 04:51:39 +02:00
|
|
|
}
|
|
|
|
|
2018-02-20 10:20:30 +01:00
|
|
|
func (am *AccountManager) dispatchCallback(client *Client, casefoldedAccount string, callbackNamespace string, callbackValue string) (string, error) {
|
2019-02-05 06:19:03 +01:00
|
|
|
if callbackNamespace == "*" || callbackNamespace == "none" || callbackNamespace == "admin" {
|
2018-02-20 10:20:30 +01:00
|
|
|
return "", nil
|
|
|
|
} else if callbackNamespace == "mailto" {
|
|
|
|
return am.dispatchMailtoCallback(client, casefoldedAccount, callbackValue)
|
|
|
|
} else {
|
2019-05-10 07:44:14 +02:00
|
|
|
return "", fmt.Errorf("Callback not implemented: %s", callbackNamespace)
|
2018-02-20 10:20:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *AccountManager) dispatchMailtoCallback(client *Client, casefoldedAccount string, callbackValue string) (code string, err error) {
|
|
|
|
config := am.server.AccountConfig().Registration.Callbacks.Mailto
|
2018-11-26 11:23:27 +01:00
|
|
|
code = utils.GenerateSecretToken()
|
2018-02-20 10:20:30 +01:00
|
|
|
|
|
|
|
subject := config.VerifyMessageSubject
|
|
|
|
if subject == "" {
|
|
|
|
subject = fmt.Sprintf(client.t("Verify your account on %s"), am.server.name)
|
|
|
|
}
|
|
|
|
messageStrings := []string{
|
|
|
|
fmt.Sprintf("From: %s\r\n", config.Sender),
|
|
|
|
fmt.Sprintf("To: %s\r\n", callbackValue),
|
|
|
|
fmt.Sprintf("Subject: %s\r\n", subject),
|
|
|
|
"\r\n", // end headers, begin message body
|
|
|
|
fmt.Sprintf(client.t("Account: %s"), casefoldedAccount) + "\r\n",
|
|
|
|
fmt.Sprintf(client.t("Verification code: %s"), code) + "\r\n",
|
|
|
|
"\r\n",
|
2019-12-17 01:50:15 +01:00
|
|
|
client.t("To verify your account, issue the following command:") + "\r\n",
|
2018-02-20 10:20:30 +01:00
|
|
|
fmt.Sprintf("/MSG NickServ VERIFY %s %s", casefoldedAccount, code) + "\r\n",
|
|
|
|
}
|
|
|
|
|
|
|
|
var message []byte
|
|
|
|
for i := 0; i < len(messageStrings); i++ {
|
|
|
|
message = append(message, []byte(messageStrings[i])...)
|
|
|
|
}
|
|
|
|
addr := fmt.Sprintf("%s:%d", config.Server, config.Port)
|
|
|
|
var auth smtp.Auth
|
|
|
|
if config.Username != "" && config.Password != "" {
|
|
|
|
auth = smtp.PlainAuth("", config.Username, config.Password, config.Server)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: this will never send the password in plaintext over a nonlocal link,
|
|
|
|
// but it might send the email in plaintext, regardless of the value of
|
|
|
|
// config.TLS.InsecureSkipVerify
|
|
|
|
err = smtp.SendMail(addr, auth, config.Sender, []string{callbackValue}, message)
|
|
|
|
if err != nil {
|
2018-12-31 17:33:42 +01:00
|
|
|
am.server.logger.Error("internal", "Failed to dispatch e-mail", err.Error())
|
2018-02-20 10:20:30 +01:00
|
|
|
}
|
|
|
|
return
|
2018-02-11 11:30:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (am *AccountManager) Verify(client *Client, account string, code string) error {
|
|
|
|
casefoldedAccount, err := CasefoldName(account)
|
|
|
|
if err != nil || account == "" || account == "*" {
|
|
|
|
return errAccountVerificationFailed
|
|
|
|
}
|
|
|
|
|
|
|
|
verifiedKey := fmt.Sprintf(keyAccountVerified, casefoldedAccount)
|
|
|
|
accountKey := fmt.Sprintf(keyAccountExists, casefoldedAccount)
|
|
|
|
accountNameKey := fmt.Sprintf(keyAccountName, casefoldedAccount)
|
|
|
|
registeredTimeKey := fmt.Sprintf(keyAccountRegTime, casefoldedAccount)
|
2018-02-20 10:20:30 +01:00
|
|
|
verificationCodeKey := fmt.Sprintf(keyAccountVerificationCode, casefoldedAccount)
|
|
|
|
callbackKey := fmt.Sprintf(keyAccountCallback, casefoldedAccount)
|
2018-02-11 11:30:40 +01:00
|
|
|
credentialsKey := fmt.Sprintf(keyAccountCredentials, casefoldedAccount)
|
|
|
|
|
|
|
|
var raw rawClientAccount
|
|
|
|
|
|
|
|
func() {
|
|
|
|
am.serialCacheUpdateMutex.Lock()
|
|
|
|
defer am.serialCacheUpdateMutex.Unlock()
|
|
|
|
|
2018-02-20 10:20:30 +01:00
|
|
|
err = am.server.store.Update(func(tx *buntdb.Tx) error {
|
2018-02-11 11:30:40 +01:00
|
|
|
raw, err = am.loadRawAccount(tx, casefoldedAccount)
|
|
|
|
if err == errAccountDoesNotExist {
|
|
|
|
return errAccountDoesNotExist
|
|
|
|
} else if err != nil {
|
|
|
|
return errAccountVerificationFailed
|
|
|
|
} else if raw.Verified {
|
|
|
|
return errAccountAlreadyVerified
|
|
|
|
}
|
|
|
|
|
2018-02-20 10:20:30 +01:00
|
|
|
// actually verify the code
|
|
|
|
// a stored code of "" means a none callback / no code required
|
|
|
|
success := false
|
|
|
|
storedCode, err := tx.Get(verificationCodeKey)
|
|
|
|
if err == nil {
|
|
|
|
// this is probably unnecessary
|
2018-11-26 11:23:27 +01:00
|
|
|
if storedCode == "" || utils.SecretTokensMatch(storedCode, code) {
|
2018-02-20 10:20:30 +01:00
|
|
|
success = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !success {
|
|
|
|
return errAccountVerificationInvalidCode
|
|
|
|
}
|
2018-02-11 11:30:40 +01:00
|
|
|
|
|
|
|
// verify the account
|
|
|
|
tx.Set(verifiedKey, "1", nil)
|
2018-02-20 10:20:30 +01:00
|
|
|
// don't need the code anymore
|
|
|
|
tx.Delete(verificationCodeKey)
|
2018-02-11 11:30:40 +01:00
|
|
|
// re-set all other keys, removing the TTL
|
|
|
|
tx.Set(accountKey, "1", nil)
|
|
|
|
tx.Set(accountNameKey, raw.Name, nil)
|
|
|
|
tx.Set(registeredTimeKey, raw.RegisteredAt, nil)
|
2018-02-20 10:20:30 +01:00
|
|
|
tx.Set(callbackKey, raw.Callback, nil)
|
2018-02-11 11:30:40 +01:00
|
|
|
tx.Set(credentialsKey, raw.Credentials, nil)
|
|
|
|
|
|
|
|
var creds AccountCredentials
|
|
|
|
// XXX we shouldn't do (de)serialization inside the txn,
|
|
|
|
// but this is like 2 usec on my system
|
|
|
|
json.Unmarshal([]byte(raw.Credentials), &creds)
|
2019-12-29 17:59:49 +01:00
|
|
|
for _, cert := range creds.Certfps {
|
|
|
|
certFPKey := fmt.Sprintf(keyCertToAccount, cert)
|
2018-02-11 11:30:40 +01:00
|
|
|
tx.Set(certFPKey, casefoldedAccount, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
2019-01-31 00:59:49 +01:00
|
|
|
skeleton, _ := Skeleton(raw.Name)
|
2018-02-11 11:30:40 +01:00
|
|
|
am.Lock()
|
|
|
|
am.nickToAccount[casefoldedAccount] = casefoldedAccount
|
2019-01-31 00:59:49 +01:00
|
|
|
am.skeletonToAccount[skeleton] = casefoldedAccount
|
2018-02-11 11:30:40 +01:00
|
|
|
am.Unlock()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-13 08:42:35 +01:00
|
|
|
nick := "[server admin]"
|
|
|
|
if client != nil {
|
|
|
|
nick = client.Nick()
|
|
|
|
}
|
|
|
|
am.server.logger.Info("accounts", "client", nick, "registered account", casefoldedAccount)
|
2018-04-19 08:48:19 +02:00
|
|
|
raw.Verified = true
|
2020-02-19 01:38:42 +01:00
|
|
|
clientAccount, err := am.deserializeRawAccount(raw, casefoldedAccount)
|
2018-04-19 08:48:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-05 06:19:03 +01:00
|
|
|
if client != nil {
|
|
|
|
am.Login(client, clientAccount)
|
|
|
|
}
|
2018-02-11 11:30:40 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-11 12:35:17 +01:00
|
|
|
// register and verify an account, for internal use
|
|
|
|
func (am *AccountManager) SARegister(account, passphrase string) (err error) {
|
|
|
|
err = am.Register(nil, account, "admin", "", passphrase, "")
|
|
|
|
if err == nil {
|
|
|
|
err = am.Verify(nil, account, "")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-02 23:04:24 +01:00
|
|
|
func marshalReservedNicks(nicks []string) string {
|
|
|
|
return strings.Join(nicks, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
func unmarshalReservedNicks(nicks string) (result []string) {
|
|
|
|
if nicks == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return strings.Split(nicks, ",")
|
|
|
|
}
|
|
|
|
|
2018-03-14 11:50:26 +01:00
|
|
|
func (am *AccountManager) SetNickReserved(client *Client, nick string, saUnreserve bool, reserve bool) error {
|
2018-03-02 23:04:24 +01:00
|
|
|
cfnick, err := CasefoldName(nick)
|
2019-01-31 00:59:49 +01:00
|
|
|
skeleton, skerr := Skeleton(nick)
|
2018-03-14 11:50:26 +01:00
|
|
|
// garbage nick, or garbage options, or disabled
|
|
|
|
nrconfig := am.server.AccountConfig().NickReservation
|
2019-01-31 00:59:49 +01:00
|
|
|
if err != nil || skerr != nil || cfnick == "" || (reserve && saUnreserve) || !nrconfig.Enabled {
|
2018-03-02 23:04:24 +01:00
|
|
|
return errAccountNickReservationFailed
|
2018-02-11 11:30:40 +01:00
|
|
|
}
|
|
|
|
|
2018-03-14 11:50:26 +01:00
|
|
|
// the cache is in sync with the DB while we hold serialCacheUpdateMutex
|
2018-03-02 23:04:24 +01:00
|
|
|
am.serialCacheUpdateMutex.Lock()
|
|
|
|
defer am.serialCacheUpdateMutex.Unlock()
|
|
|
|
|
2018-03-14 11:50:26 +01:00
|
|
|
// find the affected account, which is usually the client's:
|
|
|
|
account := client.Account()
|
|
|
|
if saUnreserve {
|
|
|
|
// unless this is a sadrop:
|
|
|
|
account = am.NickToAccount(cfnick)
|
|
|
|
if account == "" {
|
|
|
|
// nothing to do
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if account == "" {
|
|
|
|
return errAccountNotLoggedIn
|
|
|
|
}
|
|
|
|
|
2019-01-31 00:59:49 +01:00
|
|
|
am.Lock()
|
|
|
|
accountForNick := am.nickToAccount[cfnick]
|
|
|
|
var accountForSkeleton string
|
|
|
|
if reserve {
|
|
|
|
accountForSkeleton = am.skeletonToAccount[skeleton]
|
|
|
|
}
|
|
|
|
am.Unlock()
|
|
|
|
|
|
|
|
if reserve && (accountForNick != "" || accountForSkeleton != "") {
|
2018-03-02 23:04:24 +01:00
|
|
|
return errNicknameReserved
|
2018-03-14 11:50:26 +01:00
|
|
|
} else if !reserve && !saUnreserve && accountForNick != account {
|
|
|
|
return errNicknameReserved
|
2018-03-02 23:04:24 +01:00
|
|
|
} else if !reserve && cfnick == account {
|
|
|
|
return errAccountCantDropPrimaryNick
|
|
|
|
}
|
|
|
|
|
|
|
|
nicksKey := fmt.Sprintf(keyAccountAdditionalNicks, account)
|
2018-03-11 19:12:07 +01:00
|
|
|
unverifiedAccountKey := fmt.Sprintf(keyAccountExists, cfnick)
|
2018-03-02 23:04:24 +01:00
|
|
|
err = am.server.store.Update(func(tx *buntdb.Tx) error {
|
2018-03-11 19:12:07 +01:00
|
|
|
if reserve {
|
|
|
|
// unverified accounts don't show up in NickToAccount yet (which is intentional),
|
|
|
|
// however you shouldn't be able to reserve a nick out from under them
|
|
|
|
_, err := tx.Get(unverifiedAccountKey)
|
|
|
|
if err == nil {
|
|
|
|
return errNicknameReserved
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-02 23:04:24 +01:00
|
|
|
rawNicks, err := tx.Get(nicksKey)
|
|
|
|
if err != nil && err != buntdb.ErrNotFound {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
nicks := unmarshalReservedNicks(rawNicks)
|
|
|
|
|
|
|
|
if reserve {
|
2018-03-14 11:50:26 +01:00
|
|
|
if len(nicks) >= nrconfig.AdditionalNickLimit {
|
2018-03-02 23:04:24 +01:00
|
|
|
return errAccountTooManyNicks
|
|
|
|
}
|
2019-01-31 00:59:49 +01:00
|
|
|
nicks = append(nicks, nick)
|
2018-03-02 23:04:24 +01:00
|
|
|
} else {
|
2019-01-31 00:59:49 +01:00
|
|
|
// compute (original reserved nicks) minus cfnick
|
2018-03-02 23:04:24 +01:00
|
|
|
var newNicks []string
|
|
|
|
for _, reservedNick := range nicks {
|
2019-01-31 00:59:49 +01:00
|
|
|
cfreservednick, _ := CasefoldName(reservedNick)
|
|
|
|
if cfreservednick != cfnick {
|
2018-03-02 23:04:24 +01:00
|
|
|
newNicks = append(newNicks, reservedNick)
|
2019-01-31 00:59:49 +01:00
|
|
|
} else {
|
|
|
|
// found the original, unfolded version of the nick we're dropping;
|
|
|
|
// recompute the true skeleton from it
|
|
|
|
skeleton, _ = Skeleton(reservedNick)
|
2018-03-02 23:04:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
nicks = newNicks
|
|
|
|
}
|
|
|
|
|
|
|
|
marshaledNicks := marshalReservedNicks(nicks)
|
|
|
|
_, _, err = tx.Set(nicksKey, string(marshaledNicks), nil)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
2018-03-11 19:12:07 +01:00
|
|
|
if err == errAccountTooManyNicks || err == errNicknameReserved {
|
2018-03-02 23:04:24 +01:00
|
|
|
return err
|
|
|
|
} else if err != nil {
|
|
|
|
return errAccountNickReservationFailed
|
|
|
|
}
|
|
|
|
|
|
|
|
// success
|
|
|
|
am.Lock()
|
|
|
|
defer am.Unlock()
|
|
|
|
if reserve {
|
|
|
|
am.nickToAccount[cfnick] = account
|
2019-01-31 00:59:49 +01:00
|
|
|
am.skeletonToAccount[skeleton] = account
|
2018-03-02 23:04:24 +01:00
|
|
|
} else {
|
|
|
|
delete(am.nickToAccount, cfnick)
|
2019-01-31 00:59:49 +01:00
|
|
|
delete(am.skeletonToAccount, skeleton)
|
2018-03-02 23:04:24 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-29 00:21:41 +01:00
|
|
|
func (am *AccountManager) checkPassphrase(accountName, passphrase string) (account ClientAccount, err error) {
|
|
|
|
account, err = am.LoadAccount(accountName)
|
2018-02-11 11:30:40 +01:00
|
|
|
if err != nil {
|
2018-11-29 00:21:41 +01:00
|
|
|
return
|
2018-02-11 11:30:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if !account.Verified {
|
2018-11-29 00:21:41 +01:00
|
|
|
err = errAccountUnverified
|
|
|
|
return
|
2018-02-11 11:30:40 +01:00
|
|
|
}
|
|
|
|
|
2018-08-06 04:51:39 +02:00
|
|
|
switch account.Credentials.Version {
|
|
|
|
case 0:
|
|
|
|
err = handleLegacyPasswordV0(am.server, accountName, account.Credentials, passphrase)
|
|
|
|
case 1:
|
2019-01-02 16:29:42 +01:00
|
|
|
if passwd.CompareHashAndPassword(account.Credentials.PassphraseHash, []byte(passphrase)) != nil {
|
|
|
|
err = errAccountInvalidCredentials
|
|
|
|
}
|
2018-08-06 04:51:39 +02:00
|
|
|
default:
|
|
|
|
err = errAccountInvalidCredentials
|
2018-04-01 09:12:41 +02:00
|
|
|
}
|
2018-11-29 00:21:41 +01:00
|
|
|
return
|
|
|
|
}
|
2018-08-06 04:51:39 +02:00
|
|
|
|
2020-02-11 12:35:17 +01:00
|
|
|
func (am *AccountManager) AuthenticateByPassphrase(client *Client, accountName string, passphrase string) (err error) {
|
2020-02-09 09:17:10 +01:00
|
|
|
var account ClientAccount
|
|
|
|
|
2020-02-11 12:35:17 +01:00
|
|
|
defer func() {
|
2020-02-09 10:12:42 +01:00
|
|
|
if err == nil {
|
|
|
|
am.Login(client, account)
|
|
|
|
}
|
2020-02-11 12:35:17 +01:00
|
|
|
}()
|
2020-02-09 09:17:10 +01:00
|
|
|
|
2020-02-11 12:35:17 +01:00
|
|
|
ldapConf := am.server.Config().Accounts.LDAP
|
|
|
|
if ldapConf.Enabled {
|
|
|
|
err = ldap.CheckLDAPPassphrase(ldapConf, accountName, passphrase, am.server.logger)
|
|
|
|
if err == nil {
|
|
|
|
account, err = am.LoadAccount(accountName)
|
|
|
|
// autocreate if necessary:
|
|
|
|
if err == errAccountDoesNotExist && ldapConf.Autocreate {
|
|
|
|
err = am.SARegister(accountName, "")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
account, err = am.LoadAccount(accountName)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2018-02-11 11:30:40 +01:00
|
|
|
}
|
|
|
|
|
2020-02-11 12:35:17 +01:00
|
|
|
account, err = am.checkPassphrase(accountName, passphrase)
|
|
|
|
return err
|
2018-02-11 11:30:40 +01:00
|
|
|
}
|
|
|
|
|
2018-03-02 23:04:24 +01:00
|
|
|
func (am *AccountManager) LoadAccount(accountName string) (result ClientAccount, err error) {
|
|
|
|
casefoldedAccount, err := CasefoldName(accountName)
|
|
|
|
if err != nil {
|
|
|
|
err = errAccountDoesNotExist
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-11 11:30:40 +01:00
|
|
|
var raw rawClientAccount
|
|
|
|
am.server.store.View(func(tx *buntdb.Tx) error {
|
|
|
|
raw, err = am.loadRawAccount(tx, casefoldedAccount)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-19 01:38:42 +01:00
|
|
|
result, err = am.deserializeRawAccount(raw, casefoldedAccount)
|
2018-04-19 08:48:19 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-19 01:38:42 +01:00
|
|
|
func (am *AccountManager) deserializeRawAccount(raw rawClientAccount, cfName string) (result ClientAccount, err error) {
|
2018-02-11 11:30:40 +01:00
|
|
|
result.Name = raw.Name
|
2020-02-19 01:38:42 +01:00
|
|
|
result.NameCasefolded = cfName
|
2018-02-11 11:30:40 +01:00
|
|
|
regTimeInt, _ := strconv.ParseInt(raw.RegisteredAt, 10, 64)
|
2019-12-24 21:03:37 +01:00
|
|
|
result.RegisteredAt = time.Unix(regTimeInt, 0).UTC()
|
2018-02-11 11:30:40 +01:00
|
|
|
e := json.Unmarshal([]byte(raw.Credentials), &result.Credentials)
|
|
|
|
if e != nil {
|
2018-12-31 17:33:42 +01:00
|
|
|
am.server.logger.Error("internal", "could not unmarshal credentials", e.Error())
|
2018-02-11 11:30:40 +01:00
|
|
|
err = errAccountDoesNotExist
|
|
|
|
return
|
|
|
|
}
|
2018-03-02 23:04:24 +01:00
|
|
|
result.AdditionalNicks = unmarshalReservedNicks(raw.AdditionalNicks)
|
2018-02-11 11:30:40 +01:00
|
|
|
result.Verified = raw.Verified
|
2018-04-19 08:48:19 +02:00
|
|
|
if raw.VHost != "" {
|
|
|
|
e := json.Unmarshal([]byte(raw.VHost), &result.VHost)
|
|
|
|
if e != nil {
|
2018-12-31 17:33:42 +01:00
|
|
|
am.server.logger.Warning("internal", "could not unmarshal vhost for account", result.Name, e.Error())
|
2018-04-19 08:48:19 +02:00
|
|
|
// pretend they have no vhost and move on
|
|
|
|
}
|
|
|
|
}
|
2019-05-19 10:27:44 +02:00
|
|
|
if raw.Settings != "" {
|
|
|
|
e := json.Unmarshal([]byte(raw.Settings), &result.Settings)
|
|
|
|
if e != nil {
|
|
|
|
am.server.logger.Warning("internal", "could not unmarshal settings for account", result.Name, e.Error())
|
|
|
|
}
|
|
|
|
}
|
2018-02-11 11:30:40 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *AccountManager) loadRawAccount(tx *buntdb.Tx, casefoldedAccount string) (result rawClientAccount, err error) {
|
|
|
|
accountKey := fmt.Sprintf(keyAccountExists, casefoldedAccount)
|
|
|
|
accountNameKey := fmt.Sprintf(keyAccountName, casefoldedAccount)
|
|
|
|
registeredTimeKey := fmt.Sprintf(keyAccountRegTime, casefoldedAccount)
|
|
|
|
credentialsKey := fmt.Sprintf(keyAccountCredentials, casefoldedAccount)
|
|
|
|
verifiedKey := fmt.Sprintf(keyAccountVerified, casefoldedAccount)
|
2018-02-20 10:20:30 +01:00
|
|
|
callbackKey := fmt.Sprintf(keyAccountCallback, casefoldedAccount)
|
2018-03-02 23:04:24 +01:00
|
|
|
nicksKey := fmt.Sprintf(keyAccountAdditionalNicks, casefoldedAccount)
|
2018-04-19 08:48:19 +02:00
|
|
|
vhostKey := fmt.Sprintf(keyAccountVHost, casefoldedAccount)
|
2019-05-19 10:27:44 +02:00
|
|
|
settingsKey := fmt.Sprintf(keyAccountSettings, casefoldedAccount)
|
2018-02-11 11:30:40 +01:00
|
|
|
|
|
|
|
_, e := tx.Get(accountKey)
|
|
|
|
if e == buntdb.ErrNotFound {
|
|
|
|
err = errAccountDoesNotExist
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-20 10:20:30 +01:00
|
|
|
result.Name, _ = tx.Get(accountNameKey)
|
|
|
|
result.RegisteredAt, _ = tx.Get(registeredTimeKey)
|
|
|
|
result.Credentials, _ = tx.Get(credentialsKey)
|
|
|
|
result.Callback, _ = tx.Get(callbackKey)
|
2018-03-02 23:04:24 +01:00
|
|
|
result.AdditionalNicks, _ = tx.Get(nicksKey)
|
2018-04-19 08:48:19 +02:00
|
|
|
result.VHost, _ = tx.Get(vhostKey)
|
2019-05-19 10:27:44 +02:00
|
|
|
result.Settings, _ = tx.Get(settingsKey)
|
2018-02-20 10:20:30 +01:00
|
|
|
|
2018-02-11 11:30:40 +01:00
|
|
|
if _, e = tx.Get(verifiedKey); e == nil {
|
|
|
|
result.Verified = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *AccountManager) Unregister(account string) error {
|
2019-02-14 20:00:54 +01:00
|
|
|
config := am.server.Config()
|
2018-02-11 11:30:40 +01:00
|
|
|
casefoldedAccount, err := CasefoldName(account)
|
|
|
|
if err != nil {
|
|
|
|
return errAccountDoesNotExist
|
|
|
|
}
|
|
|
|
|
|
|
|
accountKey := fmt.Sprintf(keyAccountExists, casefoldedAccount)
|
|
|
|
accountNameKey := fmt.Sprintf(keyAccountName, casefoldedAccount)
|
|
|
|
registeredTimeKey := fmt.Sprintf(keyAccountRegTime, casefoldedAccount)
|
|
|
|
credentialsKey := fmt.Sprintf(keyAccountCredentials, casefoldedAccount)
|
2018-02-20 10:20:30 +01:00
|
|
|
callbackKey := fmt.Sprintf(keyAccountCallback, casefoldedAccount)
|
|
|
|
verificationCodeKey := fmt.Sprintf(keyAccountVerificationCode, casefoldedAccount)
|
2018-02-11 11:30:40 +01:00
|
|
|
verifiedKey := fmt.Sprintf(keyAccountVerified, casefoldedAccount)
|
2018-03-02 23:04:24 +01:00
|
|
|
nicksKey := fmt.Sprintf(keyAccountAdditionalNicks, casefoldedAccount)
|
2019-05-19 10:27:44 +02:00
|
|
|
settingsKey := fmt.Sprintf(keyAccountSettings, casefoldedAccount)
|
2018-04-19 08:48:19 +02:00
|
|
|
vhostKey := fmt.Sprintf(keyAccountVHost, casefoldedAccount)
|
|
|
|
vhostQueueKey := fmt.Sprintf(keyVHostQueueAcctToId, casefoldedAccount)
|
2019-02-06 10:32:04 +01:00
|
|
|
channelsKey := fmt.Sprintf(keyAccountChannels, casefoldedAccount)
|
2020-02-19 01:38:42 +01:00
|
|
|
joinedChannelsKey := fmt.Sprintf(keyAccountJoinedChannels, casefoldedAccount)
|
2020-02-20 08:33:49 +01:00
|
|
|
lastSignoffKey := fmt.Sprintf(keyAccountLastSignoff, casefoldedAccount)
|
2018-02-11 11:30:40 +01:00
|
|
|
|
|
|
|
var clients []*Client
|
|
|
|
|
2019-02-06 10:32:04 +01:00
|
|
|
var registeredChannels []string
|
2019-02-12 05:30:49 +01:00
|
|
|
// on our way out, unregister all the account's channels and delete them from the db
|
2019-02-06 10:32:04 +01:00
|
|
|
defer func() {
|
2019-02-12 05:30:49 +01:00
|
|
|
for _, channelName := range registeredChannels {
|
2019-03-12 00:24:45 +01:00
|
|
|
am.server.channels.SetUnregistered(channelName, casefoldedAccount)
|
2019-02-12 05:30:49 +01:00
|
|
|
}
|
2019-02-06 10:32:04 +01:00
|
|
|
}()
|
|
|
|
|
2018-03-02 23:04:24 +01:00
|
|
|
var credText string
|
|
|
|
var rawNicks string
|
2018-02-11 11:30:40 +01:00
|
|
|
|
2018-03-02 23:04:24 +01:00
|
|
|
am.serialCacheUpdateMutex.Lock()
|
|
|
|
defer am.serialCacheUpdateMutex.Unlock()
|
2018-02-11 11:30:40 +01:00
|
|
|
|
2019-01-31 00:59:49 +01:00
|
|
|
var accountName string
|
2019-02-06 10:32:04 +01:00
|
|
|
var channelsStr string
|
2018-03-02 23:04:24 +01:00
|
|
|
am.server.store.Update(func(tx *buntdb.Tx) error {
|
|
|
|
tx.Delete(accountKey)
|
2019-01-31 00:59:49 +01:00
|
|
|
accountName, _ = tx.Get(accountNameKey)
|
2018-03-02 23:04:24 +01:00
|
|
|
tx.Delete(accountNameKey)
|
|
|
|
tx.Delete(verifiedKey)
|
|
|
|
tx.Delete(registeredTimeKey)
|
|
|
|
tx.Delete(callbackKey)
|
|
|
|
tx.Delete(verificationCodeKey)
|
2019-05-19 10:27:44 +02:00
|
|
|
tx.Delete(settingsKey)
|
2018-03-02 23:04:24 +01:00
|
|
|
rawNicks, _ = tx.Get(nicksKey)
|
|
|
|
tx.Delete(nicksKey)
|
|
|
|
credText, err = tx.Get(credentialsKey)
|
|
|
|
tx.Delete(credentialsKey)
|
2018-04-19 08:48:19 +02:00
|
|
|
tx.Delete(vhostKey)
|
2019-02-06 10:32:04 +01:00
|
|
|
channelsStr, _ = tx.Get(channelsKey)
|
|
|
|
tx.Delete(channelsKey)
|
2020-02-19 01:38:42 +01:00
|
|
|
tx.Delete(joinedChannelsKey)
|
2020-02-20 08:33:49 +01:00
|
|
|
tx.Delete(lastSignoffKey)
|
2019-02-06 10:32:04 +01:00
|
|
|
|
2018-04-19 08:48:19 +02:00
|
|
|
_, err := tx.Delete(vhostQueueKey)
|
2018-04-23 08:38:35 +02:00
|
|
|
am.decrementVHostQueueCount(casefoldedAccount, err)
|
2018-03-02 23:04:24 +01:00
|
|
|
return nil
|
|
|
|
})
|
2018-02-11 11:30:40 +01:00
|
|
|
|
2018-03-02 23:04:24 +01:00
|
|
|
if err == nil {
|
|
|
|
var creds AccountCredentials
|
2019-12-29 17:59:49 +01:00
|
|
|
if err = json.Unmarshal([]byte(credText), &creds); err == nil {
|
|
|
|
for _, cert := range creds.Certfps {
|
|
|
|
certFPKey := fmt.Sprintf(keyCertToAccount, cert)
|
|
|
|
am.server.store.Update(func(tx *buntdb.Tx) error {
|
|
|
|
if account, err := tx.Get(certFPKey); err == nil && account == casefoldedAccount {
|
|
|
|
tx.Delete(certFPKey)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2018-02-11 11:30:40 +01:00
|
|
|
}
|
2018-03-02 23:04:24 +01:00
|
|
|
}
|
2018-02-11 11:30:40 +01:00
|
|
|
|
2019-01-31 00:59:49 +01:00
|
|
|
skeleton, _ := Skeleton(accountName)
|
2018-03-02 23:04:24 +01:00
|
|
|
additionalNicks := unmarshalReservedNicks(rawNicks)
|
2019-02-06 10:32:04 +01:00
|
|
|
registeredChannels = unmarshalRegisteredChannels(channelsStr)
|
2018-02-11 11:30:40 +01:00
|
|
|
|
2018-03-02 23:04:24 +01:00
|
|
|
am.Lock()
|
|
|
|
defer am.Unlock()
|
|
|
|
|
|
|
|
clients = am.accountToClients[casefoldedAccount]
|
|
|
|
delete(am.accountToClients, casefoldedAccount)
|
|
|
|
delete(am.nickToAccount, casefoldedAccount)
|
2019-01-31 00:59:49 +01:00
|
|
|
delete(am.skeletonToAccount, skeleton)
|
2018-03-02 23:04:24 +01:00
|
|
|
for _, nick := range additionalNicks {
|
|
|
|
delete(am.nickToAccount, nick)
|
2019-01-31 00:59:49 +01:00
|
|
|
additionalSkel, _ := Skeleton(nick)
|
|
|
|
delete(am.skeletonToAccount, additionalSkel)
|
2018-03-02 23:04:24 +01:00
|
|
|
}
|
2018-02-11 11:30:40 +01:00
|
|
|
for _, client := range clients {
|
2019-02-14 20:00:54 +01:00
|
|
|
if config.Accounts.RequireSasl.Enabled {
|
2019-04-12 06:08:46 +02:00
|
|
|
client.Quit(client.t("You are no longer authorized to be on this server"), nil)
|
2019-02-14 20:00:54 +01:00
|
|
|
// destroy acquires a semaphore so we can't call it while holding a lock
|
2019-05-22 03:40:25 +02:00
|
|
|
go client.destroy(nil)
|
2019-02-14 20:00:54 +01:00
|
|
|
} else {
|
|
|
|
am.logoutOfAccount(client)
|
|
|
|
}
|
2018-02-11 11:30:40 +01:00
|
|
|
}
|
|
|
|
|
2018-02-12 07:09:30 +01:00
|
|
|
if err != nil {
|
|
|
|
return errAccountDoesNotExist
|
|
|
|
}
|
2019-02-06 10:32:04 +01:00
|
|
|
|
2018-02-11 11:30:40 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-06 10:32:04 +01:00
|
|
|
func unmarshalRegisteredChannels(channelsStr string) (result []string) {
|
|
|
|
if channelsStr != "" {
|
|
|
|
result = strings.Split(channelsStr, ",")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *AccountManager) ChannelsForAccount(account string) (channels []string) {
|
|
|
|
cfaccount, err := CasefoldName(account)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var channelStr string
|
|
|
|
key := fmt.Sprintf(keyAccountChannels, cfaccount)
|
|
|
|
am.server.store.View(func(tx *buntdb.Tx) error {
|
|
|
|
channelStr, _ = tx.Get(key)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return unmarshalRegisteredChannels(channelStr)
|
|
|
|
}
|
|
|
|
|
2020-02-19 03:42:27 +01:00
|
|
|
func (am *AccountManager) AuthenticateByCertFP(client *Client, certfp, authzid string) error {
|
|
|
|
if certfp == "" {
|
2018-02-11 11:30:40 +01:00
|
|
|
return errAccountInvalidCredentials
|
|
|
|
}
|
|
|
|
|
|
|
|
var account string
|
2020-02-19 03:42:27 +01:00
|
|
|
certFPKey := fmt.Sprintf(keyCertToAccount, certfp)
|
2018-02-11 11:30:40 +01:00
|
|
|
|
2019-05-19 10:27:44 +02:00
|
|
|
err := am.server.store.View(func(tx *buntdb.Tx) error {
|
2018-02-11 11:30:40 +01:00
|
|
|
account, _ = tx.Get(certFPKey)
|
|
|
|
if account == "" {
|
|
|
|
return errAccountInvalidCredentials
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-25 21:06:26 +01:00
|
|
|
if authzid != "" && authzid != account {
|
|
|
|
return errAuthzidAuthcidMismatch
|
|
|
|
}
|
|
|
|
|
2018-02-11 11:30:40 +01:00
|
|
|
// ok, we found an account corresponding to their certificate
|
2019-05-19 10:27:44 +02:00
|
|
|
clientAccount, err := am.LoadAccount(account)
|
2018-04-19 08:48:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-05-19 10:27:44 +02:00
|
|
|
} else if !clientAccount.Verified {
|
|
|
|
return errAccountUnverified
|
2018-04-19 08:48:19 +02:00
|
|
|
}
|
|
|
|
am.Login(client, clientAccount)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-19 10:27:44 +02:00
|
|
|
type settingsMunger func(input AccountSettings) (output AccountSettings, err error)
|
|
|
|
|
|
|
|
func (am *AccountManager) ModifyAccountSettings(account string, munger settingsMunger) (newSettings AccountSettings, err error) {
|
|
|
|
casefoldedAccount, err := CasefoldName(account)
|
|
|
|
if err != nil {
|
|
|
|
return newSettings, errAccountDoesNotExist
|
|
|
|
}
|
|
|
|
// TODO implement this in general via a compare-and-swap API
|
|
|
|
accountData, err := am.LoadAccount(casefoldedAccount)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
} else if !accountData.Verified {
|
|
|
|
return newSettings, errAccountUnverified
|
|
|
|
}
|
|
|
|
newSettings, err = munger(accountData.Settings)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
text, err := json.Marshal(newSettings)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
key := fmt.Sprintf(keyAccountSettings, casefoldedAccount)
|
|
|
|
serializedValue := string(text)
|
|
|
|
err = am.server.store.Update(func(tx *buntdb.Tx) (err error) {
|
|
|
|
_, _, err = tx.Set(key, serializedValue, nil)
|
|
|
|
return
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
err = errAccountUpdateFailed
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// success, push new settings into the client objects
|
|
|
|
am.Lock()
|
|
|
|
defer am.Unlock()
|
|
|
|
for _, client := range am.accountToClients[casefoldedAccount] {
|
|
|
|
client.SetAccountSettings(newSettings)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-19 08:48:19 +02:00
|
|
|
// represents someone's status in hostserv
|
|
|
|
type VHostInfo struct {
|
|
|
|
ApprovedVHost string
|
|
|
|
Enabled bool
|
2020-01-12 04:43:40 +01:00
|
|
|
Forbidden bool
|
2018-04-19 08:48:19 +02:00
|
|
|
RequestedVHost string
|
|
|
|
RejectedVHost string
|
|
|
|
RejectionReason string
|
|
|
|
LastRequestTime time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// pair type, <VHostInfo, accountName>
|
|
|
|
type PendingVHostRequest struct {
|
|
|
|
VHostInfo
|
|
|
|
Account string
|
|
|
|
}
|
|
|
|
|
2020-01-29 04:27:56 +01:00
|
|
|
type vhostThrottleExceeded struct {
|
|
|
|
timeRemaining time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vhe *vhostThrottleExceeded) Error() string {
|
|
|
|
return fmt.Sprintf("Wait at least %v and try again", vhe.timeRemaining)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vh *VHostInfo) checkThrottle(cooldown time.Duration) (err error) {
|
|
|
|
if cooldown == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now().UTC()
|
|
|
|
elapsed := now.Sub(vh.LastRequestTime)
|
|
|
|
if elapsed > cooldown {
|
|
|
|
// success
|
|
|
|
vh.LastRequestTime = now
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return &vhostThrottleExceeded{timeRemaining: cooldown - elapsed}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 08:48:19 +02:00
|
|
|
// callback type implementing the actual business logic of vhost operations
|
|
|
|
type vhostMunger func(input VHostInfo) (output VHostInfo, err error)
|
|
|
|
|
2020-02-02 04:19:33 +01:00
|
|
|
func (am *AccountManager) VHostSet(account string, vhost string) (result VHostInfo, err error) {
|
2018-04-19 08:48:19 +02:00
|
|
|
munger := func(input VHostInfo) (output VHostInfo, err error) {
|
|
|
|
output = input
|
|
|
|
output.Enabled = true
|
|
|
|
output.ApprovedVHost = vhost
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return am.performVHostChange(account, munger)
|
|
|
|
}
|
|
|
|
|
2020-01-29 04:27:56 +01:00
|
|
|
func (am *AccountManager) VHostRequest(account string, vhost string, cooldown time.Duration) (result VHostInfo, err error) {
|
2018-04-19 08:48:19 +02:00
|
|
|
munger := func(input VHostInfo) (output VHostInfo, err error) {
|
|
|
|
output = input
|
2020-02-02 05:51:29 +01:00
|
|
|
if input.Forbidden {
|
|
|
|
err = errVhostsForbidden
|
|
|
|
return
|
|
|
|
}
|
2020-01-29 04:27:56 +01:00
|
|
|
// you can update your existing request, but if you were approved or rejected,
|
|
|
|
// you can't spam a new request
|
|
|
|
if output.RequestedVHost == "" {
|
|
|
|
err = output.checkThrottle(cooldown)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-04-19 08:48:19 +02:00
|
|
|
output.RequestedVHost = vhost
|
|
|
|
output.RejectedVHost = ""
|
|
|
|
output.RejectionReason = ""
|
|
|
|
output.LastRequestTime = time.Now().UTC()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return am.performVHostChange(account, munger)
|
|
|
|
}
|
|
|
|
|
2020-02-02 04:19:33 +01:00
|
|
|
func (am *AccountManager) VHostTake(account string, vhost string, cooldown time.Duration) (result VHostInfo, err error) {
|
|
|
|
munger := func(input VHostInfo) (output VHostInfo, err error) {
|
|
|
|
output = input
|
2020-02-02 05:51:29 +01:00
|
|
|
if input.Forbidden {
|
|
|
|
err = errVhostsForbidden
|
|
|
|
return
|
|
|
|
}
|
2020-02-02 04:19:33 +01:00
|
|
|
// if you have a request pending, you can cancel it using take;
|
|
|
|
// otherwise, you're subject to the same throttling as if you were making a request
|
|
|
|
if output.RequestedVHost == "" {
|
|
|
|
err = output.checkThrottle(cooldown)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
output.ApprovedVHost = vhost
|
|
|
|
output.RequestedVHost = ""
|
|
|
|
output.RejectedVHost = ""
|
|
|
|
output.RejectionReason = ""
|
|
|
|
output.LastRequestTime = time.Now().UTC()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return am.performVHostChange(account, munger)
|
|
|
|
}
|
|
|
|
|
2018-04-23 08:38:35 +02:00
|
|
|
func (am *AccountManager) VHostApprove(account string) (result VHostInfo, err error) {
|
2018-04-19 08:48:19 +02:00
|
|
|
munger := func(input VHostInfo) (output VHostInfo, err error) {
|
|
|
|
output = input
|
|
|
|
output.Enabled = true
|
|
|
|
output.ApprovedVHost = input.RequestedVHost
|
|
|
|
output.RequestedVHost = ""
|
|
|
|
output.RejectionReason = ""
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return am.performVHostChange(account, munger)
|
|
|
|
}
|
|
|
|
|
2018-04-23 08:38:35 +02:00
|
|
|
func (am *AccountManager) VHostReject(account string, reason string) (result VHostInfo, err error) {
|
2018-04-19 08:48:19 +02:00
|
|
|
munger := func(input VHostInfo) (output VHostInfo, err error) {
|
|
|
|
output = input
|
|
|
|
output.RejectedVHost = output.RequestedVHost
|
|
|
|
output.RequestedVHost = ""
|
|
|
|
output.RejectionReason = reason
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return am.performVHostChange(account, munger)
|
|
|
|
}
|
|
|
|
|
2018-04-23 08:38:35 +02:00
|
|
|
func (am *AccountManager) VHostSetEnabled(client *Client, enabled bool) (result VHostInfo, err error) {
|
2018-04-19 08:48:19 +02:00
|
|
|
munger := func(input VHostInfo) (output VHostInfo, err error) {
|
2019-05-22 05:55:04 +02:00
|
|
|
if input.ApprovedVHost == "" {
|
|
|
|
err = errNoVhost
|
|
|
|
return
|
|
|
|
}
|
2018-04-19 08:48:19 +02:00
|
|
|
output = input
|
|
|
|
output.Enabled = enabled
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return am.performVHostChange(client.Account(), munger)
|
|
|
|
}
|
2018-02-11 11:30:40 +01:00
|
|
|
|
2020-01-12 04:43:40 +01:00
|
|
|
func (am *AccountManager) VHostForbid(account string, forbid bool) (result VHostInfo, err error) {
|
|
|
|
munger := func(input VHostInfo) (output VHostInfo, err error) {
|
|
|
|
output = input
|
|
|
|
output.Forbidden = forbid
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return am.performVHostChange(account, munger)
|
|
|
|
}
|
|
|
|
|
2018-04-23 08:38:35 +02:00
|
|
|
func (am *AccountManager) performVHostChange(account string, munger vhostMunger) (result VHostInfo, err error) {
|
2018-04-19 08:48:19 +02:00
|
|
|
account, err = CasefoldName(account)
|
|
|
|
if err != nil || account == "" {
|
2018-04-23 08:38:35 +02:00
|
|
|
err = errAccountDoesNotExist
|
|
|
|
return
|
2018-04-19 08:48:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
am.vHostUpdateMutex.Lock()
|
|
|
|
defer am.vHostUpdateMutex.Unlock()
|
|
|
|
|
|
|
|
clientAccount, err := am.LoadAccount(account)
|
|
|
|
if err != nil {
|
2018-04-23 08:38:35 +02:00
|
|
|
err = errAccountDoesNotExist
|
|
|
|
return
|
2018-04-19 08:48:19 +02:00
|
|
|
} else if !clientAccount.Verified {
|
2018-04-23 08:38:35 +02:00
|
|
|
err = errAccountUnverified
|
|
|
|
return
|
2018-04-19 08:48:19 +02:00
|
|
|
}
|
|
|
|
|
2018-04-23 08:38:35 +02:00
|
|
|
result, err = munger(clientAccount.VHost)
|
2018-04-19 08:48:19 +02:00
|
|
|
if err != nil {
|
2018-04-23 08:38:35 +02:00
|
|
|
return
|
2018-04-19 08:48:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
vhtext, err := json.Marshal(result)
|
|
|
|
if err != nil {
|
2018-04-23 08:38:35 +02:00
|
|
|
err = errAccountUpdateFailed
|
|
|
|
return
|
2018-04-19 08:48:19 +02:00
|
|
|
}
|
|
|
|
vhstr := string(vhtext)
|
|
|
|
|
|
|
|
key := fmt.Sprintf(keyAccountVHost, account)
|
|
|
|
queueKey := fmt.Sprintf(keyVHostQueueAcctToId, account)
|
|
|
|
err = am.server.store.Update(func(tx *buntdb.Tx) error {
|
|
|
|
if _, _, err := tx.Set(key, vhstr, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// update request queue
|
|
|
|
if clientAccount.VHost.RequestedVHost == "" && result.RequestedVHost != "" {
|
|
|
|
id := atomic.AddUint64(&am.vhostRequestID, 1)
|
|
|
|
if _, _, err = tx.Set(queueKey, strconv.FormatUint(id, 10), nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
atomic.AddUint64(&am.vhostRequestPendingCount, 1)
|
|
|
|
} else if clientAccount.VHost.RequestedVHost != "" && result.RequestedVHost == "" {
|
|
|
|
_, err = tx.Delete(queueKey)
|
2018-04-23 08:38:35 +02:00
|
|
|
am.decrementVHostQueueCount(account, err)
|
2018-04-19 08:48:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2018-04-23 08:38:35 +02:00
|
|
|
err = errAccountUpdateFailed
|
|
|
|
return
|
2018-04-19 08:48:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
am.applyVhostToClients(account, result)
|
2018-04-23 08:38:35 +02:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX annoying helper method for keeping the queue count in sync with the DB
|
|
|
|
// `err` is the buntdb error returned from deleting the queue key
|
|
|
|
func (am *AccountManager) decrementVHostQueueCount(account string, err error) {
|
|
|
|
if err == nil {
|
|
|
|
// successfully deleted a queue entry, do a 2's complement decrement:
|
|
|
|
atomic.AddUint64(&am.vhostRequestPendingCount, ^uint64(0))
|
|
|
|
} else if err != buntdb.ErrNotFound {
|
|
|
|
am.server.logger.Error("internal", "buntdb dequeue error", account, err.Error())
|
|
|
|
}
|
2018-02-11 11:30:40 +01:00
|
|
|
}
|
|
|
|
|
2018-04-19 08:48:19 +02:00
|
|
|
func (am *AccountManager) VHostListRequests(limit int) (requests []PendingVHostRequest, total int) {
|
|
|
|
am.vHostUpdateMutex.Lock()
|
|
|
|
defer am.vHostUpdateMutex.Unlock()
|
|
|
|
|
|
|
|
total = int(atomic.LoadUint64(&am.vhostRequestPendingCount))
|
|
|
|
|
|
|
|
prefix := fmt.Sprintf(keyVHostQueueAcctToId, "")
|
|
|
|
accounts := make([]string, 0, limit)
|
|
|
|
err := am.server.store.View(func(tx *buntdb.Tx) error {
|
|
|
|
return tx.Ascend(vhostRequestIdx, func(key, value string) bool {
|
|
|
|
accounts = append(accounts, strings.TrimPrefix(key, prefix))
|
|
|
|
return len(accounts) < limit
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
am.server.logger.Error("internal", "couldn't traverse vhost queue", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, account := range accounts {
|
|
|
|
accountInfo, err := am.LoadAccount(account)
|
|
|
|
if err == nil {
|
|
|
|
requests = append(requests, PendingVHostRequest{
|
|
|
|
Account: account,
|
|
|
|
VHostInfo: accountInfo.VHost,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
am.server.logger.Error("internal", "corrupt account", account, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *AccountManager) applyVHostInfo(client *Client, info VHostInfo) {
|
|
|
|
// if hostserv is disabled in config, then don't grant vhosts
|
|
|
|
// that were previously approved while it was enabled
|
2018-04-23 08:38:35 +02:00
|
|
|
if !am.server.AccountConfig().VHosts.Enabled {
|
2018-04-19 08:48:19 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vhost := ""
|
2020-01-12 04:43:40 +01:00
|
|
|
if info.Enabled && !info.Forbidden {
|
2018-04-19 08:48:19 +02:00
|
|
|
vhost = info.ApprovedVHost
|
|
|
|
}
|
|
|
|
oldNickmask := client.NickMaskString()
|
|
|
|
updated := client.SetVHost(vhost)
|
|
|
|
if updated {
|
|
|
|
// TODO: doing I/O here is kind of a kludge
|
2019-02-15 19:27:02 +01:00
|
|
|
go client.sendChghost(oldNickmask, client.Hostname())
|
2018-04-19 08:48:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *AccountManager) applyVhostToClients(account string, result VHostInfo) {
|
|
|
|
am.RLock()
|
|
|
|
clients := am.accountToClients[account]
|
|
|
|
am.RUnlock()
|
|
|
|
|
|
|
|
for _, client := range clients {
|
|
|
|
am.applyVHostInfo(client, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *AccountManager) Login(client *Client, account ClientAccount) {
|
2020-02-19 01:38:42 +01:00
|
|
|
client.Login(account)
|
2018-04-19 08:48:19 +02:00
|
|
|
|
2019-04-15 00:13:01 +02:00
|
|
|
client.nickTimer.Touch(nil)
|
2019-01-02 16:08:44 +01:00
|
|
|
|
2018-04-19 08:48:19 +02:00
|
|
|
am.applyVHostInfo(client, account.VHost)
|
2018-03-02 23:04:24 +01:00
|
|
|
|
|
|
|
casefoldedAccount := client.Account()
|
2018-04-19 08:48:19 +02:00
|
|
|
am.Lock()
|
|
|
|
defer am.Unlock()
|
2018-02-11 11:30:40 +01:00
|
|
|
am.accountToClients[casefoldedAccount] = append(am.accountToClients[casefoldedAccount], client)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *AccountManager) Logout(client *Client) {
|
|
|
|
am.Lock()
|
|
|
|
defer am.Unlock()
|
|
|
|
|
2018-03-02 23:04:24 +01:00
|
|
|
casefoldedAccount := client.Account()
|
|
|
|
if casefoldedAccount == "" {
|
2018-02-11 11:30:40 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-02 23:04:24 +01:00
|
|
|
am.logoutOfAccount(client)
|
|
|
|
|
2018-02-11 11:30:40 +01:00
|
|
|
clients := am.accountToClients[casefoldedAccount]
|
|
|
|
if len(clients) <= 1 {
|
|
|
|
delete(am.accountToClients, casefoldedAccount)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
remainingClients := make([]*Client, len(clients)-1)
|
|
|
|
remainingPos := 0
|
|
|
|
for currentPos := 0; currentPos < len(clients); currentPos++ {
|
|
|
|
if clients[currentPos] != client {
|
|
|
|
remainingClients[remainingPos] = clients[currentPos]
|
|
|
|
remainingPos++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
am.accountToClients[casefoldedAccount] = remainingClients
|
|
|
|
}
|
|
|
|
|
2016-09-05 14:35:13 +02:00
|
|
|
var (
|
2016-09-06 08:31:59 +02:00
|
|
|
// EnabledSaslMechanisms contains the SASL mechanisms that exist and that we support.
|
|
|
|
// This can be moved to some other data structure/place if we need to load/unload mechs later.
|
2018-02-05 15:21:08 +01:00
|
|
|
EnabledSaslMechanisms = map[string]func(*Server, *Client, string, []byte, *ResponseBuffer) bool{
|
2016-09-06 08:31:59 +02:00
|
|
|
"PLAIN": authPlainHandler,
|
|
|
|
"EXTERNAL": authExternalHandler,
|
|
|
|
}
|
2016-09-05 14:35:13 +02:00
|
|
|
)
|
|
|
|
|
2018-02-11 11:30:40 +01:00
|
|
|
// AccountCredentials stores the various methods for verifying accounts.
|
|
|
|
type AccountCredentials struct {
|
2018-08-06 04:51:39 +02:00
|
|
|
Version uint
|
|
|
|
PassphraseSalt []byte // legacy field, not used by v1 and later
|
2018-02-11 11:30:40 +01:00
|
|
|
PassphraseHash []byte
|
2019-12-29 17:59:49 +01:00
|
|
|
Certfps []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ac *AccountCredentials) Empty() bool {
|
|
|
|
return len(ac.PassphraseHash) == 0 && len(ac.Certfps) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper to assemble the serialized JSON for an account's credentials
|
|
|
|
func (ac *AccountCredentials) Serialize() (result string, err error) {
|
|
|
|
ac.Version = 1
|
|
|
|
credText, err := json.Marshal(*ac)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(credText), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ac *AccountCredentials) SetPassphrase(passphrase string, bcryptCost uint) (err error) {
|
|
|
|
if passphrase == "" {
|
|
|
|
ac.PassphraseHash = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if validatePassphrase(passphrase) != nil {
|
|
|
|
return errAccountBadPassphrase
|
|
|
|
}
|
|
|
|
|
|
|
|
ac.PassphraseHash, err = passwd.GenerateFromPassword([]byte(passphrase), int(bcryptCost))
|
|
|
|
if err != nil {
|
|
|
|
return errAccountBadPassphrase
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ac *AccountCredentials) AddCertfp(certfp string) (err error) {
|
2020-01-06 18:21:52 +01:00
|
|
|
// XXX we require that certfp is already normalized (rather than normalize here
|
|
|
|
// and pass back the normalized version as an additional return parameter);
|
|
|
|
// this is just a final sanity check:
|
|
|
|
if len(certfp) != 64 {
|
|
|
|
return utils.ErrInvalidCertfp
|
|
|
|
}
|
|
|
|
|
2019-12-29 17:59:49 +01:00
|
|
|
for _, current := range ac.Certfps {
|
|
|
|
if certfp == current {
|
|
|
|
return errNoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if maxCertfpsPerAccount <= len(ac.Certfps) {
|
|
|
|
return errLimitExceeded
|
|
|
|
}
|
|
|
|
|
|
|
|
ac.Certfps = append(ac.Certfps, certfp)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ac *AccountCredentials) RemoveCertfp(certfp string) (err error) {
|
|
|
|
found := false
|
|
|
|
newList := make([]string, 0, len(ac.Certfps))
|
|
|
|
for _, current := range ac.Certfps {
|
|
|
|
if current == certfp {
|
|
|
|
found = true
|
|
|
|
} else {
|
|
|
|
newList = append(newList, current)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
// this is important because it prevents you from deleting someone else's
|
|
|
|
// fingerprint record
|
|
|
|
return errNoop
|
|
|
|
}
|
|
|
|
ac.Certfps = newList
|
|
|
|
return nil
|
2018-02-11 11:30:40 +01:00
|
|
|
}
|
|
|
|
|
2019-05-19 10:27:44 +02:00
|
|
|
type BouncerAllowedSetting int
|
|
|
|
|
|
|
|
const (
|
|
|
|
BouncerAllowedServerDefault BouncerAllowedSetting = iota
|
|
|
|
BouncerDisallowedByUser
|
|
|
|
BouncerAllowedByUser
|
|
|
|
)
|
|
|
|
|
2019-12-18 23:38:14 +01:00
|
|
|
// controls whether/when clients without event-playback support see fake
|
|
|
|
// PRIVMSGs for JOINs
|
|
|
|
type ReplayJoinsSetting uint
|
|
|
|
|
|
|
|
const (
|
|
|
|
ReplayJoinsCommandsOnly = iota // replay in HISTORY or CHATHISTORY output
|
|
|
|
ReplayJoinsAlways // replay in HISTORY, CHATHISTORY, or autoreplay
|
|
|
|
ReplayJoinsNever // never replay
|
|
|
|
)
|
|
|
|
|
|
|
|
func replayJoinsSettingFromString(str string) (result ReplayJoinsSetting, err error) {
|
|
|
|
switch strings.ToLower(str) {
|
|
|
|
case "commands-only":
|
|
|
|
result = ReplayJoinsCommandsOnly
|
|
|
|
case "always":
|
|
|
|
result = ReplayJoinsAlways
|
|
|
|
case "never":
|
|
|
|
result = ReplayJoinsNever
|
|
|
|
default:
|
|
|
|
err = errInvalidParams
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-19 10:27:44 +02:00
|
|
|
type AccountSettings struct {
|
2020-02-19 01:38:42 +01:00
|
|
|
AutoreplayLines *int
|
|
|
|
NickEnforcement NickEnforcementMethod
|
|
|
|
AllowBouncer BouncerAllowedSetting
|
|
|
|
ReplayJoins ReplayJoinsSetting
|
|
|
|
AlwaysOn PersistentStatus
|
|
|
|
AutoreplayMissed bool
|
|
|
|
DMHistory HistoryStatus
|
2019-05-19 10:27:44 +02:00
|
|
|
}
|
|
|
|
|
2016-09-05 14:35:13 +02:00
|
|
|
// ClientAccount represents a user account.
|
|
|
|
type ClientAccount struct {
|
2016-09-04 11:25:33 +02:00
|
|
|
// Name of the account.
|
2019-12-25 21:56:57 +01:00
|
|
|
Name string
|
|
|
|
NameCasefolded string
|
2018-03-02 23:04:24 +01:00
|
|
|
RegisteredAt time.Time
|
|
|
|
Credentials AccountCredentials
|
|
|
|
Verified bool
|
|
|
|
AdditionalNicks []string
|
2018-04-19 08:48:19 +02:00
|
|
|
VHost VHostInfo
|
2019-05-19 10:27:44 +02:00
|
|
|
Settings AccountSettings
|
2016-09-04 11:25:33 +02:00
|
|
|
}
|
2016-09-06 08:31:59 +02:00
|
|
|
|
2018-02-11 11:30:40 +01:00
|
|
|
// convenience for passing around raw serialized account data
|
|
|
|
type rawClientAccount struct {
|
2018-03-02 23:04:24 +01:00
|
|
|
Name string
|
|
|
|
RegisteredAt string
|
|
|
|
Credentials string
|
|
|
|
Callback string
|
|
|
|
Verified bool
|
|
|
|
AdditionalNicks string
|
2018-04-19 08:48:19 +02:00
|
|
|
VHost string
|
2019-05-19 10:27:44 +02:00
|
|
|
Settings string
|
2017-09-11 01:16:13 +02:00
|
|
|
}
|
|
|
|
|
2018-03-02 23:04:24 +01:00
|
|
|
// logoutOfAccount logs the client out of their current account.
|
|
|
|
func (am *AccountManager) logoutOfAccount(client *Client) {
|
2018-02-11 11:30:40 +01:00
|
|
|
if client.Account() == "" {
|
2017-09-11 01:16:13 +02:00
|
|
|
// already logged out
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-19 01:38:42 +01:00
|
|
|
client.Logout()
|
2019-04-15 00:13:01 +02:00
|
|
|
go client.nickTimer.Touch(nil)
|
2017-09-11 01:16:13 +02:00
|
|
|
|
|
|
|
// dispatch account-notify
|
2018-02-20 10:20:30 +01:00
|
|
|
// TODO: doing the I/O here is kind of a kludge, let's move this somewhere else
|
2018-03-02 23:04:24 +01:00
|
|
|
go func() {
|
|
|
|
for friend := range client.Friends(caps.AccountNotify) {
|
|
|
|
friend.Send(nil, client.NickMaskString(), "ACCOUNT", "*")
|
|
|
|
}
|
|
|
|
}()
|
2017-03-08 12:50:12 +01:00
|
|
|
}
|