12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015 |
- // Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
- // released under the MIT license
-
- package irc
-
- import (
- "bytes"
- "crypto/x509"
- "encoding/json"
- "fmt"
- "sort"
- "strconv"
- "strings"
- "sync"
- "time"
- "unicode"
-
- "github.com/oragono/oragono/irc/connection_limits"
- "github.com/oragono/oragono/irc/email"
- "github.com/oragono/oragono/irc/migrations"
- "github.com/oragono/oragono/irc/modes"
- "github.com/oragono/oragono/irc/passwd"
- "github.com/oragono/oragono/irc/utils"
- "github.com/tidwall/buntdb"
- )
-
- const (
- keyAccountExists = "account.exists %s"
- keyAccountVerified = "account.verified %s"
- keyAccountUnregistered = "account.unregistered %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"
- keyAccountAdditionalNicks = "account.additionalnicks %s"
- keyAccountSettings = "account.settings %s"
- keyAccountVHost = "account.vhost %s"
- keyCertToAccount = "account.creds.certfp %s"
- keyAccountChannels = "account.channels %s" // channels registered to the account
- keyAccountJoinedChannels = "account.joinedto %s" // channels a persistent client has joined
- keyAccountLastSeen = "account.lastseen %s"
- keyAccountModes = "account.modes %s" // user modes for the always-on client as a string
- keyAccountRealname = "account.realname %s" // client realname stored as string
- keyAccountSuspended = "account.suspended %s" // client realname stored as string
-
- maxCertfpsPerAccount = 5
- )
-
- // 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 {
- sync.RWMutex // tier 2
- serialCacheUpdateMutex sync.Mutex // tier 3
-
- server *Server
- // track clients logged in to accounts
- accountToClients map[string][]*Client
- nickToAccount map[string]string
- skeletonToAccount map[string]string
- accountToMethod map[string]NickEnforcementMethod
- registerThrottle connection_limits.GenericThrottle
- }
-
- func (am *AccountManager) Initialize(server *Server) {
- am.accountToClients = make(map[string][]*Client)
- am.nickToAccount = make(map[string]string)
- am.skeletonToAccount = make(map[string]string)
- am.accountToMethod = make(map[string]NickEnforcementMethod)
- am.server = server
-
- config := server.Config()
- am.buildNickToAccountIndex(config)
- am.createAlwaysOnClients(config)
- am.resetRegisterThrottle(config)
- }
-
- func (am *AccountManager) resetRegisterThrottle(config *Config) {
- am.Lock()
- defer am.Unlock()
-
- am.registerThrottle = connection_limits.GenericThrottle{
- Duration: config.Accounts.Registration.Throttling.Duration,
- Limit: config.Accounts.Registration.Throttling.MaxAttempts,
- }
- }
-
- func (am *AccountManager) touchRegisterThrottle() (throttled bool) {
- am.Lock()
- defer am.Unlock()
- throttled, _ = am.registerThrottle.Touch()
- return
- }
-
- func (am *AccountManager) createAlwaysOnClients(config *Config) {
- if config.Accounts.Multiclient.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 && account.Suspended == nil) &&
- persistenceEnabled(config.Accounts.Multiclient.AlwaysOn, account.Settings.AlwaysOn) {
- am.server.AddAlwaysOnClient(
- account,
- am.loadChannels(accountName),
- am.loadLastSeen(accountName),
- am.loadModes(accountName),
- am.loadRealname(accountName),
- )
- }
- }
- }
-
- func (am *AccountManager) buildNickToAccountIndex(config *Config) {
- if !config.Accounts.NickReservation.Enabled {
- return
- }
-
- nickToAccount := make(map[string]string)
- skeletonToAccount := make(map[string]string)
- accountToMethod := make(map[string]NickEnforcementMethod)
- 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
- }
-
- account := strings.TrimPrefix(key, existsPrefix)
- if _, err := tx.Get(fmt.Sprintf(keyAccountVerified, account)); err == nil {
- nickToAccount[account] = account
- 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
- }
- }
- if rawNicks, err := tx.Get(fmt.Sprintf(keyAccountAdditionalNicks, account)); err == nil {
- additionalNicks := unmarshalReservedNicks(rawNicks)
- for _, nick := range additionalNicks {
- cfnick, _ := CasefoldName(nick)
- nickToAccount[cfnick] = account
- skeleton, _ := Skeleton(nick)
- skeletonToAccount[skeleton] = account
- }
- }
-
- 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
- } else if err != nil {
- am.server.logger.Error("internal", "corrupt account creds", account)
- }
- }
-
- return true
- })
- return err
- })
-
- if config.Accounts.NickReservation.Method == NickEnforcementStrict {
- unregisteredPrefix := fmt.Sprintf(keyAccountUnregistered, "")
- am.server.store.View(func(tx *buntdb.Tx) error {
- tx.AscendGreaterOrEqual("", unregisteredPrefix, func(key, value string) bool {
- if !strings.HasPrefix(key, unregisteredPrefix) {
- return false
- }
- account := strings.TrimPrefix(key, unregisteredPrefix)
- accountName := value
- nickToAccount[account] = account
- skeleton, _ := Skeleton(accountName)
- skeletonToAccount[skeleton] = account
- return true
- })
- return nil
- })
- }
-
- if err != nil {
- am.server.logger.Error("internal", "couldn't read reserved nicks", err.Error())
- } else {
- am.Lock()
- am.nickToAccount = nickToAccount
- am.skeletonToAccount = skeletonToAccount
- am.accountToMethod = accountToMethod
- am.Unlock()
- }
- }
-
- func (am *AccountManager) NickToAccount(nick string) string {
- cfnick, err := CasefoldName(nick)
- if err != nil {
- return ""
- }
- skel, err := Skeleton(nick)
- if err != nil {
- return ""
- }
-
- am.RLock()
- defer am.RUnlock()
- account := am.nickToAccount[cfnick]
- if account != "" {
- return account
- }
- return am.skeletonToAccount[skel]
- }
-
- // 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
- }
-
- // Given a nick, looks up the account that owns it and the method (none/timeout/strict)
- // used to enforce ownership.
- func (am *AccountManager) EnforcementStatus(cfnick, skeleton string) (account string, method NickEnforcementMethod) {
- config := am.server.Config()
- if !config.Accounts.NickReservation.Enabled {
- return "", NickEnforcementNone
- }
-
- am.RLock()
- defer am.RUnlock()
-
- finalEnforcementMethod := func(account_ string) (result NickEnforcementMethod) {
- storedMethod := am.accountToMethod[account_]
- return configuredEnforcementMethod(config, storedMethod)
- }
-
- nickAccount := am.nickToAccount[cfnick]
- skelAccount := am.skeletonToAccount[skeleton]
- if nickAccount == "" && skelAccount == "" {
- return "", NickEnforcementNone
- } 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:
- // two people have competing claims on (this casefolding of) this nick!
- nickMethod := finalEnforcementMethod(nickAccount)
- skelMethod := finalEnforcementMethod(skelAccount)
- switch {
- case skelMethod == NickEnforcementNone:
- return nickAccount, nickMethod
- case nickMethod == NickEnforcementNone:
- return skelAccount, skelMethod
- default:
- // nobody can use this nick
- return "!", NickEnforcementStrict
- }
- }
- }
-
- // Sets a custom enforcement method for an account and stores it in the database.
- func (am *AccountManager) SetEnforcementStatus(account string, method NickEnforcementMethod) (finalSettings AccountSettings, err error) {
- config := am.server.Config()
- if !(config.Accounts.NickReservation.Enabled && config.Accounts.NickReservation.AllowCustomEnforcement) {
- err = errFeatureDisabled
- return
- }
-
- setter := func(in AccountSettings) (out AccountSettings, err error) {
- out = in
- out.NickEnforcement = method
- return out, nil
- }
-
- _, err = am.ModifyAccountSettings(account, setter)
- if err != nil {
- return
- }
-
- // this update of the data plane is racey, but it's probably fine
- am.Lock()
- defer am.Unlock()
-
- if method == NickEnforcementOptional {
- delete(am.accountToMethod, account)
- } else {
- am.accountToMethod[account] = method
- }
-
- return
- }
-
- 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]
- }
-
- func (am *AccountManager) Register(client *Client, account string, callbackNamespace string, callbackValue string, passphrase string, certfp string) error {
- casefoldedAccount, err := CasefoldName(account)
- skeleton, skerr := Skeleton(account)
- if err != nil || skerr != nil || account == "" || account == "*" {
- return errAccountCreation
- }
-
- if restrictedCasefoldedNicks.Has(casefoldedAccount) || restrictedSkeletons.Has(skeleton) {
- return errAccountAlreadyRegistered
- }
-
- config := am.server.Config()
-
- // final "is registration allowed" check:
- if !(config.Accounts.Registration.Enabled || callbackNamespace == "admin") || am.server.Defcon() <= 4 {
- return errFeatureDisabled
- }
-
- if client != nil && client.Account() != "" {
- return errAccountAlreadyLoggedIn
- }
-
- if client != nil && am.touchRegisterThrottle() {
- am.server.logger.Warning("accounts", "global registration throttle exceeded by client", client.Nick())
- return errLimitExceeded
- }
-
- // if nick reservation is enabled, don't let people reserve nicknames
- // that they would not be eligible to take, e.g.,
- // 1. a nickname that someone else is currently holding
- // 2. a nickname confusable with an existing reserved nickname
- // this has a lot of weird edge cases because of force-guest-format
- // and the possibility of registering a nickname on an "unregistered connection"
- // (i.e., pre-handshake).
- if client != nil && config.Accounts.NickReservation.Enabled {
- _, nickAcquireError, _ := am.server.clients.SetNick(client, nil, account, true)
- if !(nickAcquireError == nil || nickAcquireError == errNoop) {
- return errAccountMustHoldNick
- }
- }
-
- // can't register a guest nickname
- if config.Accounts.NickReservation.guestRegexpFolded.MatchString(casefoldedAccount) {
- return errAccountAlreadyRegistered
- }
-
- accountKey := fmt.Sprintf(keyAccountExists, casefoldedAccount)
- unregisteredKey := fmt.Sprintf(keyAccountUnregistered, casefoldedAccount)
- accountNameKey := fmt.Sprintf(keyAccountName, casefoldedAccount)
- callbackKey := fmt.Sprintf(keyAccountCallback, casefoldedAccount)
- registeredTimeKey := fmt.Sprintf(keyAccountRegTime, casefoldedAccount)
- credentialsKey := fmt.Sprintf(keyAccountCredentials, casefoldedAccount)
- verificationCodeKey := fmt.Sprintf(keyAccountVerificationCode, casefoldedAccount)
- certFPKey := fmt.Sprintf(keyCertToAccount, certfp)
-
- 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()
- if err != nil {
- return err
- }
-
- registeredTimeStr := strconv.FormatInt(time.Now().UnixNano(), 10)
- callbackSpec := fmt.Sprintf("%s:%s", callbackNamespace, callbackValue)
-
- var setOptions *buntdb.SetOptions
- ttl := time.Duration(config.Accounts.Registration.VerifyTimeout)
- if ttl != 0 {
- setOptions = &buntdb.SetOptions{Expires: true, TTL: ttl}
- }
-
- 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(account) != "" {
- return errAccountAlreadyRegistered
- }
-
- return am.server.store.Update(func(tx *buntdb.Tx) error {
- if _, err := tx.Get(unregisteredKey); err == nil {
- return errAccountAlreadyUnregistered
- }
-
- _, err = am.loadRawAccount(tx, casefoldedAccount)
- if err != errAccountDoesNotExist {
- return errAccountAlreadyRegistered
- }
-
- 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)
- if certfp != "" {
- tx.Set(certFPKey, casefoldedAccount, setOptions)
- }
- return nil
- })
- }()
-
- if err != nil {
- return err
- }
-
- code, err := am.dispatchCallback(client, account, callbackNamespace, callbackValue)
- if err != nil {
- am.Unregister(casefoldedAccount, true)
- return errCallbackFailed
- } else {
- return am.server.store.Update(func(tx *buntdb.Tx) error {
- _, _, err = tx.Set(verificationCodeKey, code, setOptions)
- return err
- })
- }
- }
-
- // 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) > 300 {
- return errAccountBadPassphrase
- }
- // we use * as a placeholder in some places, if it's gotten this far then fail
- if passphrase == "*" {
- return errAccountBadPassphrase
- }
- // for now, just enforce that spaces are not allowed
- for _, r := range passphrase {
- if unicode.IsSpace(r) {
- return errAccountBadPassphrase
- }
- }
- return nil
- }
-
- // 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
- }
-
- var creds AccountCredentials
- err = json.Unmarshal([]byte(credStr), &creds)
- if err != nil {
- return err
- }
-
- if !hasPrivs && creds.Empty() {
- return errCredsExternallyManaged
- }
-
- err = creds.SetPassphrase(password, am.server.Config().Accounts.Registration.BcryptCost)
- if err != nil {
- return err
- }
-
- 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
- }
-
- 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
- }
-
- func (am *AccountManager) saveModes(account string, uModes modes.Modes) {
- modeStr := uModes.String()
- key := fmt.Sprintf(keyAccountModes, account)
- am.server.store.Update(func(tx *buntdb.Tx) error {
- tx.Set(key, modeStr, nil)
- return nil
- })
- }
-
- func (am *AccountManager) loadModes(account string) (uModes modes.Modes) {
- key := fmt.Sprintf(keyAccountModes, account)
- var modeStr string
- am.server.store.View(func(tx *buntdb.Tx) error {
- modeStr, _ = tx.Get(key)
- return nil
- })
- for _, m := range modeStr {
- uModes = append(uModes, modes.Mode(m))
- }
- return
- }
-
- func (am *AccountManager) saveLastSeen(account string, lastSeen map[string]time.Time) {
- key := fmt.Sprintf(keyAccountLastSeen, account)
- var val string
- if len(lastSeen) != 0 {
- text, _ := json.Marshal(lastSeen)
- val = string(text)
- }
- 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) loadLastSeen(account string) (lastSeen map[string]time.Time) {
- key := fmt.Sprintf(keyAccountLastSeen, account)
- var lsText string
- am.server.store.Update(func(tx *buntdb.Tx) error {
- lsText, _ = tx.Get(key)
- return nil
- })
- if lsText == "" {
- return nil
- }
- err := json.Unmarshal([]byte(lsText), &lastSeen)
- if err != nil {
- return nil
- }
- return
- }
-
- func (am *AccountManager) saveRealname(account string, realname string) {
- key := fmt.Sprintf(keyAccountRealname, account)
- am.server.store.Update(func(tx *buntdb.Tx) error {
- if realname != "" {
- tx.Set(key, realname, nil)
- } else {
- tx.Delete(key)
- }
- return nil
- })
- }
-
- func (am *AccountManager) loadRealname(account string) (realname string) {
- key := fmt.Sprintf(keyAccountRealname, account)
- am.server.store.Update(func(tx *buntdb.Tx) error {
- realname, _ = tx.Get(key)
- return nil
- })
- return
- }
-
- 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)
- if err != nil {
- return err
- }
-
- if !hasPrivs && creds.Empty() {
- return errCredsExternallyManaged
- }
-
- if add {
- err = creds.AddCertfp(certfp)
- } else {
- err = creds.RemoveCertfp(certfp)
- }
- if err != nil {
- return err
- }
-
- if creds.Empty() && !hasPrivs {
- return errEmptyCredentials
- }
-
- newCredStr, err := creds.Serialize()
- if err != nil {
- return err
- }
-
- 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)
- return err
- })
-
- return err
- }
-
- func (am *AccountManager) dispatchCallback(client *Client, account string, callbackNamespace string, callbackValue string) (string, error) {
- if callbackNamespace == "*" || callbackNamespace == "none" || callbackNamespace == "admin" {
- return "", nil
- } else if callbackNamespace == "mailto" {
- return am.dispatchMailtoCallback(client, account, callbackValue)
- } else {
- return "", fmt.Errorf("Callback not implemented: %s", callbackNamespace)
- }
- }
-
- func (am *AccountManager) dispatchMailtoCallback(client *Client, account string, callbackValue string) (code string, err error) {
- config := am.server.Config().Accounts.Registration.EmailVerification
- code = utils.GenerateSecretToken()
-
- subject := config.VerifyMessageSubject
- if subject == "" {
- subject = fmt.Sprintf(client.t("Verify your account on %s"), am.server.name)
- }
-
- var message bytes.Buffer
- fmt.Fprintf(&message, "From: %s\r\n", config.Sender)
- fmt.Fprintf(&message, "To: %s\r\n", callbackValue)
- if config.DKIM.Domain != "" {
- fmt.Fprintf(&message, "Message-ID: <%s@%s>\r\n", utils.GenerateSecretKey(), config.DKIM.Domain)
- }
- fmt.Fprintf(&message, "Date: %s\r\n", time.Now().UTC().Format(time.RFC1123Z))
- fmt.Fprintf(&message, "Subject: %s\r\n", subject)
- message.WriteString("\r\n") // blank line: end headers, begin message body
- fmt.Fprintf(&message, client.t("Account: %s"), account)
- message.WriteString("\r\n")
- fmt.Fprintf(&message, client.t("Verification code: %s"), code)
- message.WriteString("\r\n")
- message.WriteString("\r\n")
- message.WriteString(client.t("To verify your account, issue the following command:"))
- message.WriteString("\r\n")
- fmt.Fprintf(&message, "/MSG NickServ VERIFY %s %s\r\n", account, code)
-
- err = email.SendMail(config, callbackValue, message.Bytes())
- if err != nil {
- am.server.logger.Error("internal", "Failed to dispatch e-mail to", callbackValue, err.Error())
- }
- return
- }
-
- func (am *AccountManager) Verify(client *Client, account string, code string) error {
- casefoldedAccount, err := CasefoldName(account)
- var skeleton string
- if err != nil || account == "" || account == "*" {
- return errAccountVerificationFailed
- }
-
- if client != nil && client.Account() != "" {
- return errAccountAlreadyLoggedIn
- }
-
- verifiedKey := fmt.Sprintf(keyAccountVerified, casefoldedAccount)
- accountKey := fmt.Sprintf(keyAccountExists, casefoldedAccount)
- accountNameKey := fmt.Sprintf(keyAccountName, casefoldedAccount)
- registeredTimeKey := fmt.Sprintf(keyAccountRegTime, casefoldedAccount)
- verificationCodeKey := fmt.Sprintf(keyAccountVerificationCode, casefoldedAccount)
- callbackKey := fmt.Sprintf(keyAccountCallback, casefoldedAccount)
- credentialsKey := fmt.Sprintf(keyAccountCredentials, casefoldedAccount)
-
- var raw rawClientAccount
-
- func() {
- am.serialCacheUpdateMutex.Lock()
- defer am.serialCacheUpdateMutex.Unlock()
-
- // do a final check for confusability (in case someone already verified
- // a confusable identifier):
- var unfoldedName string
- err = am.server.store.View(func(tx *buntdb.Tx) error {
- unfoldedName, err = tx.Get(accountNameKey)
- return err
- })
- if err != nil {
- err = errAccountDoesNotExist
- return
- }
- skeleton, err = Skeleton(unfoldedName)
- if err != nil {
- err = errAccountDoesNotExist
- return
- }
- err = func() error {
- am.RLock()
- defer am.RUnlock()
- if _, ok := am.skeletonToAccount[skeleton]; ok {
- return errConfusableIdentifier
- }
- return nil
- }()
- if err != nil {
- return
- }
-
- err = am.server.store.Update(func(tx *buntdb.Tx) error {
- raw, err = am.loadRawAccount(tx, casefoldedAccount)
- if err == errAccountDoesNotExist {
- return errAccountDoesNotExist
- } else if err != nil {
- return errAccountVerificationFailed
- } else if raw.Verified {
- return errAccountAlreadyVerified
- }
-
- // 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
- if storedCode == "" || utils.SecretTokensMatch(storedCode, code) {
- success = true
- }
- }
- if !success {
- return errAccountVerificationInvalidCode
- }
-
- // verify the account
- tx.Set(verifiedKey, "1", nil)
- // don't need the code anymore
- tx.Delete(verificationCodeKey)
- // 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)
- tx.Set(callbackKey, raw.Callback, nil)
- 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)
- for _, cert := range creds.Certfps {
- certFPKey := fmt.Sprintf(keyCertToAccount, cert)
- tx.Set(certFPKey, casefoldedAccount, nil)
- }
-
- return nil
- })
-
- if err == nil {
- am.Lock()
- am.nickToAccount[casefoldedAccount] = casefoldedAccount
- am.skeletonToAccount[skeleton] = casefoldedAccount
- am.Unlock()
- }
- }()
-
- if err != nil {
- return err
- }
-
- nick := "[server admin]"
- if client != nil {
- nick = client.Nick()
- }
- am.server.logger.Info("accounts", "client", nick, "registered account", account)
- raw.Verified = true
- clientAccount, err := am.deserializeRawAccount(raw, casefoldedAccount)
- if err != nil {
- return err
- }
- if client != nil {
- am.Login(client, clientAccount)
- if client.AlwaysOn() {
- client.markDirty(IncludeRealname)
- }
- }
- // we may need to do nick enforcement here:
- _, method := am.EnforcementStatus(casefoldedAccount, skeleton)
- if method == NickEnforcementStrict {
- currentClient := am.server.clients.Get(casefoldedAccount)
- if currentClient != nil && currentClient != client && currentClient.Account() != casefoldedAccount {
- am.server.RandomlyRename(currentClient)
- }
- }
- return nil
- }
-
- // 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
- }
-
- func marshalReservedNicks(nicks []string) string {
- return strings.Join(nicks, ",")
- }
-
- func unmarshalReservedNicks(nicks string) (result []string) {
- if nicks == "" {
- return
- }
- return strings.Split(nicks, ",")
- }
-
- func (am *AccountManager) SetNickReserved(client *Client, nick string, saUnreserve bool, reserve bool) error {
- cfnick, err := CasefoldName(nick)
- skeleton, skerr := Skeleton(nick)
- // garbage nick, or garbage options, or disabled
- nrconfig := am.server.Config().Accounts.NickReservation
- if err != nil || skerr != nil || cfnick == "" || (reserve && saUnreserve) || !nrconfig.Enabled {
- return errAccountNickReservationFailed
- }
-
- // the cache is in sync with the DB while we hold serialCacheUpdateMutex
- am.serialCacheUpdateMutex.Lock()
- defer am.serialCacheUpdateMutex.Unlock()
-
- // find the affected account, which is usually the client's:
- account := client.Account()
- if saUnreserve {
- // unless this is a sadrop:
- account := func() string {
- am.RLock()
- defer am.RUnlock()
- return am.nickToAccount[cfnick]
- }()
- if account == "" {
- // nothing to do
- return nil
- }
- }
- if account == "" {
- return errAccountNotLoggedIn
- }
-
- am.Lock()
- accountForNick := am.nickToAccount[cfnick]
- var accountForSkeleton string
- if reserve {
- accountForSkeleton = am.skeletonToAccount[skeleton]
- }
- am.Unlock()
-
- if reserve && (accountForNick != "" || accountForSkeleton != "") {
- return errNicknameReserved
- } else if !reserve && !saUnreserve && accountForNick != account {
- return errNicknameReserved
- } else if !reserve && cfnick == account {
- return errAccountCantDropPrimaryNick
- }
-
- nicksKey := fmt.Sprintf(keyAccountAdditionalNicks, account)
- unverifiedAccountKey := fmt.Sprintf(keyAccountExists, cfnick)
- err = am.server.store.Update(func(tx *buntdb.Tx) error {
- 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
- }
- }
-
- rawNicks, err := tx.Get(nicksKey)
- if err != nil && err != buntdb.ErrNotFound {
- return err
- }
-
- nicks := unmarshalReservedNicks(rawNicks)
-
- if reserve {
- if len(nicks) >= nrconfig.AdditionalNickLimit {
- return errAccountTooManyNicks
- }
- nicks = append(nicks, nick)
- } else {
- // compute (original reserved nicks) minus cfnick
- var newNicks []string
- for _, reservedNick := range nicks {
- cfreservednick, _ := CasefoldName(reservedNick)
- if cfreservednick != cfnick {
- newNicks = append(newNicks, reservedNick)
- } else {
- // found the original, unfolded version of the nick we're dropping;
- // recompute the true skeleton from it
- skeleton, _ = Skeleton(reservedNick)
- }
- }
- nicks = newNicks
- }
-
- marshaledNicks := marshalReservedNicks(nicks)
- _, _, err = tx.Set(nicksKey, string(marshaledNicks), nil)
- return err
- })
-
- if err == errAccountTooManyNicks || err == errNicknameReserved {
- return err
- } else if err != nil {
- return errAccountNickReservationFailed
- }
-
- // success
- am.Lock()
- defer am.Unlock()
- if reserve {
- am.nickToAccount[cfnick] = account
- am.skeletonToAccount[skeleton] = account
- } else {
- delete(am.nickToAccount, cfnick)
- delete(am.skeletonToAccount, skeleton)
- }
- return nil
- }
-
- func (am *AccountManager) checkPassphrase(accountName, passphrase string) (account ClientAccount, err error) {
- account, err = am.LoadAccount(accountName)
- if err != nil {
- return
- }
-
- if !account.Verified {
- err = errAccountUnverified
- return
- } else if account.Suspended != nil {
- err = errAccountSuspended
- return
- }
-
- switch account.Credentials.Version {
- case 0:
- err = am.checkLegacyPassphrase(migrations.CheckOragonoPassphraseV0, accountName, account.Credentials.PassphraseHash, passphrase)
- case 1:
- if passwd.CompareHashAndPassword(account.Credentials.PassphraseHash, []byte(passphrase)) != nil {
- err = errAccountInvalidCredentials
- }
- case -1:
- err = am.checkLegacyPassphrase(migrations.CheckAthemePassphrase, accountName, account.Credentials.PassphraseHash, passphrase)
- case -2:
- err = am.checkLegacyPassphrase(migrations.CheckAnopePassphrase, accountName, account.Credentials.PassphraseHash, passphrase)
- default:
- err = errAccountInvalidCredentials
- }
- return
- }
-
- func (am *AccountManager) checkLegacyPassphrase(check migrations.PassphraseCheck, account string, hash []byte, passphrase string) (err error) {
- err = check(hash, []byte(passphrase))
- if err != nil {
- if err == migrations.ErrHashInvalid {
- am.server.logger.Error("internal", "invalid legacy credentials for account", account)
- }
- return errAccountInvalidCredentials
- }
- // re-hash the passphrase with the latest algorithm
- err = am.setPassword(account, passphrase, true)
- if err != nil {
- am.server.logger.Error("internal", "could not upgrade user password", err.Error())
- }
- return nil
- }
-
- func (am *AccountManager) loadWithAutocreation(accountName string, autocreate bool) (account ClientAccount, err error) {
- account, err = am.LoadAccount(accountName)
- if err == errAccountDoesNotExist && autocreate {
- err = am.SARegister(accountName, "")
- if err != nil {
- return
- }
- account, err = am.LoadAccount(accountName)
- }
- return
- }
-
- func (am *AccountManager) AuthenticateByPassphrase(client *Client, accountName string, passphrase string) (err error) {
- // XXX check this now, so we don't allow a redundant login for an always-on client
- // even for a brief period. the other potential source of nick-account conflicts
- // is from force-nick-equals-account, but those will be caught later by
- // fixupNickEqualsAccount and if there is a conflict, they will be logged out.
- if client.registered {
- if clientAlready := am.server.clients.Get(accountName); clientAlready != nil && clientAlready.AlwaysOn() {
- return errNickAccountMismatch
- }
- }
-
- if throttled, remainingTime := client.checkLoginThrottle(); throttled {
- return &ThrottleError{remainingTime}
- }
-
- var account ClientAccount
-
- defer func() {
- if err == nil {
- am.Login(client, account)
- }
- }()
-
- config := am.server.Config()
- if config.Accounts.AuthScript.Enabled {
- var output AuthScriptOutput
- output, err = CheckAuthScript(am.server.semaphores.AuthScript, config.Accounts.AuthScript.ScriptConfig,
- AuthScriptInput{AccountName: accountName, Passphrase: passphrase, IP: client.IP().String()})
- if err != nil {
- am.server.logger.Error("internal", "failed shell auth invocation", err.Error())
- } else if output.Success {
- if output.AccountName != "" {
- accountName = output.AccountName
- }
- account, err = am.loadWithAutocreation(accountName, config.Accounts.AuthScript.Autocreate)
- return
- }
- }
-
- account, err = am.checkPassphrase(accountName, passphrase)
- return err
- }
-
- // AllNicks returns the uncasefolded nicknames for all accounts, including additional (grouped) nicks.
- func (am *AccountManager) AllNicks() (result []string) {
- accountNamePrefix := fmt.Sprintf(keyAccountName, "")
- accountAdditionalNicksPrefix := fmt.Sprintf(keyAccountAdditionalNicks, "")
-
- am.server.store.View(func(tx *buntdb.Tx) error {
- // Account names
- err := tx.AscendGreaterOrEqual("", accountNamePrefix, func(key, value string) bool {
- if !strings.HasPrefix(key, accountNamePrefix) {
- return false
- }
- result = append(result, value)
- return true
- })
- if err != nil {
- return err
- }
-
- // Additional nicks
- return tx.AscendGreaterOrEqual("", accountAdditionalNicksPrefix, func(key, value string) bool {
- if !strings.HasPrefix(key, accountAdditionalNicksPrefix) {
- return false
- }
- additionalNicks := unmarshalReservedNicks(value)
- for _, additionalNick := range additionalNicks {
- result = append(result, additionalNick)
- }
- return true
- })
- })
-
- sort.Strings(result)
- return
- }
-
- func (am *AccountManager) LoadAccount(accountName string) (result ClientAccount, err error) {
- casefoldedAccount, err := CasefoldName(accountName)
- if err != nil {
- err = errAccountDoesNotExist
- return
- }
-
- var raw rawClientAccount
- am.server.store.View(func(tx *buntdb.Tx) error {
- raw, err = am.loadRawAccount(tx, casefoldedAccount)
- return nil
- })
- if err != nil {
- return
- }
-
- result, err = am.deserializeRawAccount(raw, casefoldedAccount)
- return
- }
-
- // look up the unfolded version of an account name, possibly after deletion
- func (am *AccountManager) AccountToAccountName(account string) (result string) {
- casefoldedAccount, err := CasefoldName(account)
- if err != nil {
- return
- }
-
- unregisteredKey := fmt.Sprintf(keyAccountUnregistered, casefoldedAccount)
- accountNameKey := fmt.Sprintf(keyAccountName, casefoldedAccount)
-
- am.server.store.View(func(tx *buntdb.Tx) error {
- if name, err := tx.Get(accountNameKey); err == nil {
- result = name
- return nil
- }
- if name, err := tx.Get(unregisteredKey); err == nil {
- result = name
- }
- return nil
- })
-
- return
- }
-
- func (am *AccountManager) deserializeRawAccount(raw rawClientAccount, cfName string) (result ClientAccount, err error) {
- result.Name = raw.Name
- result.NameCasefolded = cfName
- regTimeInt, _ := strconv.ParseInt(raw.RegisteredAt, 10, 64)
- result.RegisteredAt = time.Unix(0, regTimeInt).UTC()
- e := json.Unmarshal([]byte(raw.Credentials), &result.Credentials)
- if e != nil {
- am.server.logger.Error("internal", "could not unmarshal credentials", e.Error())
- err = errAccountDoesNotExist
- return
- }
- result.AdditionalNicks = unmarshalReservedNicks(raw.AdditionalNicks)
- result.Verified = raw.Verified
- if raw.VHost != "" {
- e := json.Unmarshal([]byte(raw.VHost), &result.VHost)
- if e != nil {
- am.server.logger.Warning("internal", "could not unmarshal vhost for account", result.Name, e.Error())
- // pretend they have no vhost and move on
- }
- }
- 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())
- }
- }
- if raw.Suspended != "" {
- sus := new(AccountSuspension)
- e := json.Unmarshal([]byte(raw.Suspended), sus)
- if e != nil {
- am.server.logger.Error("internal", "corrupt suspension data", result.Name, e.Error())
- } else {
- result.Suspended = sus
- }
- }
- 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)
- callbackKey := fmt.Sprintf(keyAccountCallback, casefoldedAccount)
- nicksKey := fmt.Sprintf(keyAccountAdditionalNicks, casefoldedAccount)
- vhostKey := fmt.Sprintf(keyAccountVHost, casefoldedAccount)
- settingsKey := fmt.Sprintf(keyAccountSettings, casefoldedAccount)
- suspendedKey := fmt.Sprintf(keyAccountSuspended, casefoldedAccount)
-
- _, e := tx.Get(accountKey)
- if e == buntdb.ErrNotFound {
- err = errAccountDoesNotExist
- return
- }
-
- result.Name, _ = tx.Get(accountNameKey)
- result.RegisteredAt, _ = tx.Get(registeredTimeKey)
- result.Credentials, _ = tx.Get(credentialsKey)
- result.Callback, _ = tx.Get(callbackKey)
- result.AdditionalNicks, _ = tx.Get(nicksKey)
- result.VHost, _ = tx.Get(vhostKey)
- result.Settings, _ = tx.Get(settingsKey)
- result.Suspended, _ = tx.Get(suspendedKey)
-
- if _, e = tx.Get(verifiedKey); e == nil {
- result.Verified = true
- }
-
- return
- }
-
- type AccountSuspension struct {
- AccountName string `json:"AccountName,omitempty"`
- TimeCreated time.Time
- Duration time.Duration
- OperName string
- Reason string
- }
-
- func (am *AccountManager) Suspend(accountName string, duration time.Duration, operName, reason string) (err error) {
- account, err := CasefoldName(accountName)
- if err != nil {
- return errAccountDoesNotExist
- }
-
- suspension := AccountSuspension{
- TimeCreated: time.Now().UTC(),
- Duration: duration,
- OperName: operName,
- Reason: reason,
- }
- suspensionStr, err := json.Marshal(suspension)
- if err != nil {
- am.server.logger.Error("internal", "suspension json unserializable", err.Error())
- return errAccountDoesNotExist
- }
-
- existsKey := fmt.Sprintf(keyAccountExists, account)
- suspensionKey := fmt.Sprintf(keyAccountSuspended, account)
- var setOptions *buntdb.SetOptions
- if duration != time.Duration(0) {
- setOptions = &buntdb.SetOptions{Expires: true, TTL: duration}
- }
- err = am.server.store.Update(func(tx *buntdb.Tx) error {
- _, err := tx.Get(existsKey)
- if err != nil {
- return errAccountDoesNotExist
- }
- _, _, err = tx.Set(suspensionKey, string(suspensionStr), setOptions)
- return err
- })
-
- if err == errAccountDoesNotExist {
- return err
- } else if err != nil {
- am.server.logger.Error("internal", "couldn't persist suspension", account, err.Error())
- } // keep going
-
- am.Lock()
- clients := am.accountToClients[account]
- delete(am.accountToClients, account)
- am.Unlock()
-
- // kill clients, sending them the reason
- suspension.AccountName = accountName
- for _, client := range clients {
- client.Logout()
- client.Quit(suspensionToString(client, suspension), nil)
- client.destroy(nil)
- }
- return nil
- }
-
- func (am *AccountManager) killClients(clients []*Client) {
- for _, client := range clients {
- client.Logout()
- client.Quit(client.t("You are no longer authorized to be on this server"), nil)
- client.destroy(nil)
- }
- }
-
- func (am *AccountManager) Unsuspend(accountName string) (err error) {
- cfaccount, err := CasefoldName(accountName)
- if err != nil {
- return errAccountDoesNotExist
- }
-
- existsKey := fmt.Sprintf(keyAccountExists, cfaccount)
- suspensionKey := fmt.Sprintf(keyAccountSuspended, cfaccount)
- err = am.server.store.Update(func(tx *buntdb.Tx) error {
- _, err := tx.Get(existsKey)
- if err != nil {
- return errAccountDoesNotExist
- }
- _, err = tx.Delete(suspensionKey)
- if err != nil {
- return errNoop
- }
- return nil
- })
-
- return err
- }
-
- func (am *AccountManager) ListSuspended() (result []AccountSuspension) {
- var names []string
- var raw []string
-
- prefix := fmt.Sprintf(keyAccountSuspended, "")
- am.server.store.View(func(tx *buntdb.Tx) error {
- err := tx.AscendGreaterOrEqual("", prefix, func(key, value string) bool {
- if !strings.HasPrefix(key, prefix) {
- return false
- }
- raw = append(raw, value)
- cfname := strings.TrimPrefix(key, prefix)
- name, _ := tx.Get(fmt.Sprintf(keyAccountName, cfname))
- names = append(names, name)
- return true
- })
- return err
- })
-
- result = make([]AccountSuspension, 0, len(raw))
- for i := 0; i < len(raw); i++ {
- var sus AccountSuspension
- err := json.Unmarshal([]byte(raw[i]), &sus)
- if err != nil {
- am.server.logger.Error("internal", "corrupt data for suspension", names[i], err.Error())
- continue
- }
- sus.AccountName = names[i]
- result = append(result, sus)
- }
- return
- }
-
- // renames an account (within very restrictive limits); see #1380
- func (am *AccountManager) Rename(oldName, newName string) (err error) {
- accountData, err := am.LoadAccount(oldName)
- if err != nil {
- return
- }
- newCfName, err := CasefoldName(newName)
- if err != nil {
- return errNicknameInvalid
- }
- if newCfName != accountData.NameCasefolded {
- return errInvalidAccountRename
- }
- key := fmt.Sprintf(keyAccountName, accountData.NameCasefolded)
- err = am.server.store.Update(func(tx *buntdb.Tx) error {
- tx.Set(key, newName, nil)
- return nil
- })
- if err != nil {
- return err
- }
-
- am.RLock()
- defer am.RUnlock()
- for _, client := range am.accountToClients[accountData.NameCasefolded] {
- client.setAccountName(newName)
- }
- return nil
- }
-
- func (am *AccountManager) Unregister(account string, erase bool) error {
- config := am.server.Config()
- 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)
- callbackKey := fmt.Sprintf(keyAccountCallback, casefoldedAccount)
- verificationCodeKey := fmt.Sprintf(keyAccountVerificationCode, casefoldedAccount)
- verifiedKey := fmt.Sprintf(keyAccountVerified, casefoldedAccount)
- nicksKey := fmt.Sprintf(keyAccountAdditionalNicks, casefoldedAccount)
- settingsKey := fmt.Sprintf(keyAccountSettings, casefoldedAccount)
- vhostKey := fmt.Sprintf(keyAccountVHost, casefoldedAccount)
- channelsKey := fmt.Sprintf(keyAccountChannels, casefoldedAccount)
- joinedChannelsKey := fmt.Sprintf(keyAccountJoinedChannels, casefoldedAccount)
- lastSeenKey := fmt.Sprintf(keyAccountLastSeen, casefoldedAccount)
- unregisteredKey := fmt.Sprintf(keyAccountUnregistered, casefoldedAccount)
- modesKey := fmt.Sprintf(keyAccountModes, casefoldedAccount)
- realnameKey := fmt.Sprintf(keyAccountRealname, casefoldedAccount)
- suspendedKey := fmt.Sprintf(keyAccountSuspended, casefoldedAccount)
-
- var clients []*Client
- defer func() {
- am.killClients(clients)
- }()
-
- var registeredChannels []string
- // on our way out, unregister all the account's channels and delete them from the db
- defer func() {
- for _, channelName := range registeredChannels {
- err := am.server.channels.SetUnregistered(channelName, casefoldedAccount)
- if err != nil {
- am.server.logger.Error("internal", "couldn't unregister channel", channelName, err.Error())
- }
- }
- }()
-
- var credText string
- var rawNicks string
-
- am.serialCacheUpdateMutex.Lock()
- defer am.serialCacheUpdateMutex.Unlock()
-
- var accountName string
- var channelsStr string
- keepProtections := false
- am.server.store.Update(func(tx *buntdb.Tx) error {
- // get the unfolded account name; for an active account, this is
- // stored under accountNameKey, for an unregistered account under unregisteredKey
- accountName, _ = tx.Get(accountNameKey)
- if accountName == "" {
- accountName, _ = tx.Get(unregisteredKey)
- }
- if erase {
- tx.Delete(unregisteredKey)
- } else {
- if _, err := tx.Get(verifiedKey); err == nil {
- tx.Set(unregisteredKey, accountName, nil)
- keepProtections = true
- }
- }
- tx.Delete(accountKey)
- tx.Delete(accountNameKey)
- tx.Delete(verifiedKey)
- tx.Delete(registeredTimeKey)
- tx.Delete(callbackKey)
- tx.Delete(verificationCodeKey)
- tx.Delete(settingsKey)
- rawNicks, _ = tx.Get(nicksKey)
- tx.Delete(nicksKey)
- credText, err = tx.Get(credentialsKey)
- tx.Delete(credentialsKey)
- tx.Delete(vhostKey)
- channelsStr, _ = tx.Get(channelsKey)
- tx.Delete(channelsKey)
- tx.Delete(joinedChannelsKey)
- tx.Delete(lastSeenKey)
- tx.Delete(modesKey)
- tx.Delete(realnameKey)
- tx.Delete(suspendedKey)
-
- return nil
- })
-
- if err == nil {
- var creds AccountCredentials
- 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
- })
- }
- }
- }
-
- skeleton, _ := Skeleton(accountName)
- additionalNicks := unmarshalReservedNicks(rawNicks)
- registeredChannels = unmarshalRegisteredChannels(channelsStr)
-
- am.Lock()
- defer am.Unlock()
-
- clients = am.accountToClients[casefoldedAccount]
- delete(am.accountToClients, casefoldedAccount)
- // protect the account name itself where applicable, but not any grouped nicks
- if !(keepProtections && config.Accounts.NickReservation.Method == NickEnforcementStrict) {
- delete(am.nickToAccount, casefoldedAccount)
- delete(am.skeletonToAccount, skeleton)
- }
- for _, nick := range additionalNicks {
- delete(am.nickToAccount, nick)
- additionalSkel, _ := Skeleton(nick)
- delete(am.skeletonToAccount, additionalSkel)
- }
-
- if err != nil && !erase {
- return errAccountDoesNotExist
- }
-
- return nil
- }
-
- 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)
- }
-
- func (am *AccountManager) AuthenticateByCertificate(client *Client, certfp string, peerCerts []*x509.Certificate, authzid string) (err error) {
- if certfp == "" {
- return errAccountInvalidCredentials
- }
-
- var clientAccount ClientAccount
-
- defer func() {
- if err != nil {
- return
- } else if !clientAccount.Verified {
- err = errAccountUnverified
- return
- } else if clientAccount.Suspended != nil {
- err = errAccountSuspended
- return
- }
- // TODO(#1109) clean this check up?
- if client.registered {
- if clientAlready := am.server.clients.Get(clientAccount.Name); clientAlready != nil && clientAlready.AlwaysOn() {
- err = errNickAccountMismatch
- return
- }
- }
- am.Login(client, clientAccount)
- return
- }()
-
- config := am.server.Config()
- if config.Accounts.AuthScript.Enabled {
- var output AuthScriptOutput
- output, err = CheckAuthScript(am.server.semaphores.AuthScript, config.Accounts.AuthScript.ScriptConfig,
- AuthScriptInput{Certfp: certfp, IP: client.IP().String(), peerCerts: peerCerts})
- if err != nil {
- am.server.logger.Error("internal", "failed shell auth invocation", err.Error())
- } else if output.Success && output.AccountName != "" {
- clientAccount, err = am.loadWithAutocreation(output.AccountName, config.Accounts.AuthScript.Autocreate)
- return
- }
- }
-
- var account string
- certFPKey := fmt.Sprintf(keyCertToAccount, certfp)
-
- err = am.server.store.View(func(tx *buntdb.Tx) error {
- account, _ = tx.Get(certFPKey)
- if account == "" {
- return errAccountInvalidCredentials
- }
- return nil
- })
-
- if err != nil {
- return err
- }
-
- if authzid != "" && authzid != account {
- return errAuthzidAuthcidMismatch
- }
-
- // ok, we found an account corresponding to their certificate
- clientAccount, err = am.LoadAccount(account)
- return err
- }
-
- 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
- }
-
- // represents someone's status in hostserv
- type VHostInfo struct {
- ApprovedVHost string
- Enabled bool
- }
-
- // callback type implementing the actual business logic of vhost operations
- type vhostMunger func(input VHostInfo) (output VHostInfo, err error)
-
- func (am *AccountManager) VHostSet(account string, vhost string) (result VHostInfo, err error) {
- munger := func(input VHostInfo) (output VHostInfo, err error) {
- output = input
- output.Enabled = true
- output.ApprovedVHost = vhost
- return
- }
-
- return am.performVHostChange(account, munger)
- }
-
- func (am *AccountManager) VHostSetEnabled(client *Client, enabled bool) (result VHostInfo, err error) {
- munger := func(input VHostInfo) (output VHostInfo, err error) {
- if input.ApprovedVHost == "" {
- err = errNoVhost
- return
- }
- output = input
- output.Enabled = enabled
- return
- }
-
- return am.performVHostChange(client.Account(), munger)
- }
-
- func (am *AccountManager) performVHostChange(account string, munger vhostMunger) (result VHostInfo, err error) {
- account, err = CasefoldName(account)
- if err != nil || account == "" {
- err = errAccountDoesNotExist
- return
- }
-
- if am.server.Defcon() <= 3 {
- err = errFeatureDisabled
- return
- }
-
- clientAccount, err := am.LoadAccount(account)
- if err != nil {
- err = errAccountDoesNotExist
- return
- } else if !clientAccount.Verified {
- err = errAccountUnverified
- return
- }
-
- result, err = munger(clientAccount.VHost)
- if err != nil {
- return
- }
-
- vhtext, err := json.Marshal(result)
- if err != nil {
- err = errAccountUpdateFailed
- return
- }
- vhstr := string(vhtext)
-
- key := fmt.Sprintf(keyAccountVHost, account)
- err = am.server.store.Update(func(tx *buntdb.Tx) error {
- _, _, err := tx.Set(key, vhstr, nil)
- return err
- })
-
- if err != nil {
- err = errAccountUpdateFailed
- return
- }
-
- am.applyVhostToClients(account, result)
- return result, nil
- }
-
- 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
- if !am.server.Config().Accounts.VHosts.Enabled {
- return
- }
-
- vhost := ""
- if info.Enabled {
- vhost = info.ApprovedVHost
- }
- oldNickmask := client.NickMaskString()
- updated := client.SetVHost(vhost)
- if updated && client.Registered() {
- // TODO: doing I/O here is kind of a kludge
- client.sendChghost(oldNickmask, client.Hostname())
- }
- }
-
- 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) {
- client.Login(account)
-
- am.applyVHostInfo(client, account.VHost)
-
- casefoldedAccount := client.Account()
- am.Lock()
- defer am.Unlock()
- am.accountToClients[casefoldedAccount] = append(am.accountToClients[casefoldedAccount], client)
- }
-
- func (am *AccountManager) Logout(client *Client) {
- am.Lock()
- defer am.Unlock()
-
- casefoldedAccount := client.Account()
- if casefoldedAccount == "" {
- return
- }
-
- client.Logout()
-
- 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
- }
-
- var (
- // 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.
- EnabledSaslMechanisms = map[string]func(*Server, *Client, string, []byte, *ResponseBuffer) bool{
- "PLAIN": authPlainHandler,
- "EXTERNAL": authExternalHandler,
- }
- )
-
- type CredentialsVersion int
-
- const (
- CredentialsLegacy CredentialsVersion = 0
- CredentialsSHA3Bcrypt CredentialsVersion = 1
- // negative numbers for migration
- CredentialsAtheme = -1
- CredentialsAnope = -2
- )
-
- // AccountCredentials stores the various methods for verifying accounts.
- type AccountCredentials struct {
- Version CredentialsVersion
- PassphraseHash []byte
- 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) {
- // 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
- }
-
- 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
- }
-
- type MulticlientAllowedSetting int
-
- const (
- MulticlientAllowedServerDefault MulticlientAllowedSetting = iota
- MulticlientDisallowedByUser
- MulticlientAllowedByUser
- )
-
- // 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
- }
-
- // XXX: AllowBouncer cannot be renamed AllowMulticlient because it is stored in
- // persistent JSON blobs in the database
- type AccountSettings struct {
- AutoreplayLines *int
- NickEnforcement NickEnforcementMethod
- AllowBouncer MulticlientAllowedSetting
- ReplayJoins ReplayJoinsSetting
- AlwaysOn PersistentStatus
- AutoreplayMissed bool
- DMHistory HistoryStatus
- AutoAway PersistentStatus
- }
-
- // ClientAccount represents a user account.
- type ClientAccount struct {
- // Name of the account.
- Name string
- NameCasefolded string
- RegisteredAt time.Time
- Credentials AccountCredentials
- Verified bool
- Suspended *AccountSuspension
- AdditionalNicks []string
- VHost VHostInfo
- Settings AccountSettings
- }
-
- // convenience for passing around raw serialized account data
- type rawClientAccount struct {
- Name string
- RegisteredAt string
- Credentials string
- Callback string
- Verified bool
- AdditionalNicks string
- VHost string
- Settings string
- Suspended string
- }
|