You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

accounts.go 59KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052
  1. // Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
  2. // released under the MIT license
  3. package irc
  4. import (
  5. "bytes"
  6. "crypto/x509"
  7. "encoding/json"
  8. "fmt"
  9. "sort"
  10. "strconv"
  11. "strings"
  12. "sync"
  13. "sync/atomic"
  14. "time"
  15. "unicode"
  16. "github.com/oragono/oragono/irc/connection_limits"
  17. "github.com/oragono/oragono/irc/email"
  18. "github.com/oragono/oragono/irc/migrations"
  19. "github.com/oragono/oragono/irc/modes"
  20. "github.com/oragono/oragono/irc/passwd"
  21. "github.com/oragono/oragono/irc/utils"
  22. "github.com/tidwall/buntdb"
  23. )
  24. const (
  25. keyAccountExists = "account.exists %s"
  26. keyAccountVerified = "account.verified %s"
  27. keyAccountUnregistered = "account.unregistered %s"
  28. keyAccountCallback = "account.callback %s"
  29. keyAccountVerificationCode = "account.verificationcode %s"
  30. keyAccountName = "account.name %s" // stores the 'preferred name' of the account, not casemapped
  31. keyAccountRegTime = "account.registered.time %s"
  32. keyAccountCredentials = "account.credentials %s"
  33. keyAccountAdditionalNicks = "account.additionalnicks %s"
  34. keyAccountSettings = "account.settings %s"
  35. keyAccountVHost = "account.vhost %s"
  36. keyCertToAccount = "account.creds.certfp %s"
  37. keyAccountChannels = "account.channels %s" // channels registered to the account
  38. keyAccountJoinedChannels = "account.joinedto %s" // channels a persistent client has joined
  39. keyAccountLastSeen = "account.lastseen %s"
  40. keyAccountModes = "account.modes %s" // user modes for the always-on client as a string
  41. keyAccountRealname = "account.realname %s" // client realname stored as string
  42. keyVHostQueueAcctToId = "vhostQueue %s"
  43. vhostRequestIdx = "vhostQueue"
  44. maxCertfpsPerAccount = 5
  45. )
  46. // everything about accounts is persistent; therefore, the database is the authoritative
  47. // source of truth for all account information. anything on the heap is just a cache
  48. type AccountManager struct {
  49. // XXX these are up here so they can be aligned to a 64-bit boundary, please forgive me
  50. // autoincrementing ID for vhost requests:
  51. vhostRequestID uint64
  52. vhostRequestPendingCount uint64
  53. sync.RWMutex // tier 2
  54. serialCacheUpdateMutex sync.Mutex // tier 3
  55. vHostUpdateMutex sync.Mutex // tier 3
  56. server *Server
  57. // track clients logged in to accounts
  58. accountToClients map[string][]*Client
  59. nickToAccount map[string]string
  60. skeletonToAccount map[string]string
  61. accountToMethod map[string]NickEnforcementMethod
  62. registerThrottle connection_limits.GenericThrottle
  63. }
  64. func (am *AccountManager) Initialize(server *Server) {
  65. am.accountToClients = make(map[string][]*Client)
  66. am.nickToAccount = make(map[string]string)
  67. am.skeletonToAccount = make(map[string]string)
  68. am.accountToMethod = make(map[string]NickEnforcementMethod)
  69. am.server = server
  70. config := server.Config()
  71. am.buildNickToAccountIndex(config)
  72. am.initVHostRequestQueue(config)
  73. am.createAlwaysOnClients(config)
  74. am.resetRegisterThrottle(config)
  75. }
  76. func (am *AccountManager) resetRegisterThrottle(config *Config) {
  77. am.Lock()
  78. defer am.Unlock()
  79. am.registerThrottle = connection_limits.GenericThrottle{
  80. Duration: config.Accounts.Registration.Throttling.Duration,
  81. Limit: config.Accounts.Registration.Throttling.MaxAttempts,
  82. }
  83. }
  84. func (am *AccountManager) touchRegisterThrottle() (throttled bool) {
  85. am.Lock()
  86. defer am.Unlock()
  87. throttled, _ = am.registerThrottle.Touch()
  88. return
  89. }
  90. func (am *AccountManager) createAlwaysOnClients(config *Config) {
  91. if config.Accounts.Multiclient.AlwaysOn == PersistentDisabled {
  92. return
  93. }
  94. verifiedPrefix := fmt.Sprintf(keyAccountVerified, "")
  95. am.serialCacheUpdateMutex.Lock()
  96. defer am.serialCacheUpdateMutex.Unlock()
  97. var accounts []string
  98. am.server.store.View(func(tx *buntdb.Tx) error {
  99. err := tx.AscendGreaterOrEqual("", verifiedPrefix, func(key, value string) bool {
  100. if !strings.HasPrefix(key, verifiedPrefix) {
  101. return false
  102. }
  103. account := strings.TrimPrefix(key, verifiedPrefix)
  104. accounts = append(accounts, account)
  105. return true
  106. })
  107. return err
  108. })
  109. for _, accountName := range accounts {
  110. account, err := am.LoadAccount(accountName)
  111. if err == nil && account.Verified &&
  112. persistenceEnabled(config.Accounts.Multiclient.AlwaysOn, account.Settings.AlwaysOn) {
  113. am.server.AddAlwaysOnClient(
  114. account,
  115. am.loadChannels(accountName),
  116. am.loadLastSeen(accountName),
  117. am.loadModes(accountName),
  118. am.loadRealname(accountName),
  119. )
  120. }
  121. }
  122. }
  123. func (am *AccountManager) buildNickToAccountIndex(config *Config) {
  124. if !config.Accounts.NickReservation.Enabled {
  125. return
  126. }
  127. nickToAccount := make(map[string]string)
  128. skeletonToAccount := make(map[string]string)
  129. accountToMethod := make(map[string]NickEnforcementMethod)
  130. existsPrefix := fmt.Sprintf(keyAccountExists, "")
  131. am.serialCacheUpdateMutex.Lock()
  132. defer am.serialCacheUpdateMutex.Unlock()
  133. err := am.server.store.View(func(tx *buntdb.Tx) error {
  134. err := tx.AscendGreaterOrEqual("", existsPrefix, func(key, value string) bool {
  135. if !strings.HasPrefix(key, existsPrefix) {
  136. return false
  137. }
  138. account := strings.TrimPrefix(key, existsPrefix)
  139. if _, err := tx.Get(fmt.Sprintf(keyAccountVerified, account)); err == nil {
  140. nickToAccount[account] = account
  141. accountName, err := tx.Get(fmt.Sprintf(keyAccountName, account))
  142. if err != nil {
  143. am.server.logger.Error("internal", "missing account name for", account)
  144. } else {
  145. skeleton, _ := Skeleton(accountName)
  146. skeletonToAccount[skeleton] = account
  147. }
  148. }
  149. if rawNicks, err := tx.Get(fmt.Sprintf(keyAccountAdditionalNicks, account)); err == nil {
  150. additionalNicks := unmarshalReservedNicks(rawNicks)
  151. for _, nick := range additionalNicks {
  152. cfnick, _ := CasefoldName(nick)
  153. nickToAccount[cfnick] = account
  154. skeleton, _ := Skeleton(nick)
  155. skeletonToAccount[skeleton] = account
  156. }
  157. }
  158. if rawPrefs, err := tx.Get(fmt.Sprintf(keyAccountSettings, account)); err == nil {
  159. var prefs AccountSettings
  160. err := json.Unmarshal([]byte(rawPrefs), &prefs)
  161. if err == nil && prefs.NickEnforcement != NickEnforcementOptional {
  162. accountToMethod[account] = prefs.NickEnforcement
  163. } else if err != nil {
  164. am.server.logger.Error("internal", "corrupt account creds", account)
  165. }
  166. }
  167. return true
  168. })
  169. return err
  170. })
  171. if config.Accounts.NickReservation.Method == NickEnforcementStrict {
  172. unregisteredPrefix := fmt.Sprintf(keyAccountUnregistered, "")
  173. am.server.store.View(func(tx *buntdb.Tx) error {
  174. tx.AscendGreaterOrEqual("", unregisteredPrefix, func(key, value string) bool {
  175. if !strings.HasPrefix(key, unregisteredPrefix) {
  176. return false
  177. }
  178. account := strings.TrimPrefix(key, unregisteredPrefix)
  179. accountName := value
  180. nickToAccount[account] = account
  181. skeleton, _ := Skeleton(accountName)
  182. skeletonToAccount[skeleton] = account
  183. return true
  184. })
  185. return nil
  186. })
  187. }
  188. if err != nil {
  189. am.server.logger.Error("internal", "couldn't read reserved nicks", err.Error())
  190. } else {
  191. am.Lock()
  192. am.nickToAccount = nickToAccount
  193. am.skeletonToAccount = skeletonToAccount
  194. am.accountToMethod = accountToMethod
  195. am.Unlock()
  196. }
  197. }
  198. func (am *AccountManager) initVHostRequestQueue(config *Config) {
  199. if !config.Accounts.VHosts.Enabled {
  200. return
  201. }
  202. am.vHostUpdateMutex.Lock()
  203. defer am.vHostUpdateMutex.Unlock()
  204. // the db maps the account name to the autoincrementing integer ID of its request
  205. // create an numerically ordered index on ID, so we can list the oldest requests
  206. // finally, collect the integer id of the newest request and the total request count
  207. var total uint64
  208. var lastIDStr string
  209. err := am.server.store.Update(func(tx *buntdb.Tx) error {
  210. err := tx.CreateIndex(vhostRequestIdx, fmt.Sprintf(keyVHostQueueAcctToId, "*"), buntdb.IndexInt)
  211. if err != nil {
  212. return err
  213. }
  214. return tx.Descend(vhostRequestIdx, func(key, value string) bool {
  215. if lastIDStr == "" {
  216. lastIDStr = value
  217. }
  218. total++
  219. return true
  220. })
  221. })
  222. if err != nil {
  223. am.server.logger.Error("internal", "could not create vhost queue index", err.Error())
  224. }
  225. lastID, _ := strconv.ParseUint(lastIDStr, 10, 64)
  226. am.server.logger.Debug("services", fmt.Sprintf("vhost queue length is %d, autoincrementing id is %d", total, lastID))
  227. atomic.StoreUint64(&am.vhostRequestID, lastID)
  228. atomic.StoreUint64(&am.vhostRequestPendingCount, total)
  229. }
  230. func (am *AccountManager) NickToAccount(nick string) string {
  231. cfnick, err := CasefoldName(nick)
  232. if err != nil {
  233. return ""
  234. }
  235. am.RLock()
  236. defer am.RUnlock()
  237. return am.nickToAccount[cfnick]
  238. }
  239. // given an account, combine stored enforcement method with the config settings
  240. // to compute the actual enforcement method
  241. func configuredEnforcementMethod(config *Config, storedMethod NickEnforcementMethod) (result NickEnforcementMethod) {
  242. if !config.Accounts.NickReservation.Enabled {
  243. return NickEnforcementNone
  244. }
  245. result = storedMethod
  246. // if they don't have a custom setting, or customization is disabled, use the default
  247. if result == NickEnforcementOptional || !config.Accounts.NickReservation.AllowCustomEnforcement {
  248. result = config.Accounts.NickReservation.Method
  249. }
  250. if result == NickEnforcementOptional {
  251. // enforcement was explicitly enabled neither in the config or by the user
  252. result = NickEnforcementNone
  253. }
  254. return
  255. }
  256. // Given a nick, looks up the account that owns it and the method (none/timeout/strict)
  257. // used to enforce ownership.
  258. func (am *AccountManager) EnforcementStatus(cfnick, skeleton string) (account string, method NickEnforcementMethod) {
  259. config := am.server.Config()
  260. if !config.Accounts.NickReservation.Enabled {
  261. return "", NickEnforcementNone
  262. }
  263. am.RLock()
  264. defer am.RUnlock()
  265. finalEnforcementMethod := func(account_ string) (result NickEnforcementMethod) {
  266. storedMethod := am.accountToMethod[account_]
  267. return configuredEnforcementMethod(config, storedMethod)
  268. }
  269. nickAccount := am.nickToAccount[cfnick]
  270. skelAccount := am.skeletonToAccount[skeleton]
  271. if nickAccount == "" && skelAccount == "" {
  272. return "", NickEnforcementNone
  273. } else if nickAccount != "" && (skelAccount == nickAccount || skelAccount == "") {
  274. return nickAccount, finalEnforcementMethod(nickAccount)
  275. } else if skelAccount != "" && nickAccount == "" {
  276. return skelAccount, finalEnforcementMethod(skelAccount)
  277. } else {
  278. // nickAccount != skelAccount and both are nonempty:
  279. // two people have competing claims on (this casefolding of) this nick!
  280. nickMethod := finalEnforcementMethod(nickAccount)
  281. skelMethod := finalEnforcementMethod(skelAccount)
  282. switch {
  283. case skelMethod == NickEnforcementNone:
  284. return nickAccount, nickMethod
  285. case nickMethod == NickEnforcementNone:
  286. return skelAccount, skelMethod
  287. default:
  288. // nobody can use this nick
  289. return "!", NickEnforcementStrict
  290. }
  291. }
  292. }
  293. // Sets a custom enforcement method for an account and stores it in the database.
  294. func (am *AccountManager) SetEnforcementStatus(account string, method NickEnforcementMethod) (finalSettings AccountSettings, err error) {
  295. config := am.server.Config()
  296. if !(config.Accounts.NickReservation.Enabled && config.Accounts.NickReservation.AllowCustomEnforcement) {
  297. err = errFeatureDisabled
  298. return
  299. }
  300. setter := func(in AccountSettings) (out AccountSettings, err error) {
  301. out = in
  302. out.NickEnforcement = method
  303. return out, nil
  304. }
  305. _, err = am.ModifyAccountSettings(account, setter)
  306. if err != nil {
  307. return
  308. }
  309. // this update of the data plane is racey, but it's probably fine
  310. am.Lock()
  311. defer am.Unlock()
  312. if method == NickEnforcementOptional {
  313. delete(am.accountToMethod, account)
  314. } else {
  315. am.accountToMethod[account] = method
  316. }
  317. return
  318. }
  319. func (am *AccountManager) AccountToClients(account string) (result []*Client) {
  320. cfaccount, err := CasefoldName(account)
  321. if err != nil {
  322. return
  323. }
  324. am.RLock()
  325. defer am.RUnlock()
  326. return am.accountToClients[cfaccount]
  327. }
  328. func (am *AccountManager) Register(client *Client, account string, callbackNamespace string, callbackValue string, passphrase string, certfp string) error {
  329. casefoldedAccount, err := CasefoldName(account)
  330. skeleton, skerr := Skeleton(account)
  331. if err != nil || skerr != nil || account == "" || account == "*" {
  332. return errAccountCreation
  333. }
  334. if restrictedCasefoldedNicks.Has(casefoldedAccount) || restrictedSkeletons.Has(skeleton) {
  335. return errAccountAlreadyRegistered
  336. }
  337. config := am.server.Config()
  338. // final "is registration allowed" check:
  339. if !(config.Accounts.Registration.Enabled || callbackNamespace == "admin") || am.server.Defcon() <= 4 {
  340. return errFeatureDisabled
  341. }
  342. if client != nil && client.Account() != "" {
  343. return errAccountAlreadyLoggedIn
  344. }
  345. if client != nil && am.touchRegisterThrottle() {
  346. am.server.logger.Warning("accounts", "global registration throttle exceeded by client", client.Nick())
  347. return errLimitExceeded
  348. }
  349. // if nick reservation is enabled, don't let people reserve nicknames
  350. // that they would not be eligible to take, e.g.,
  351. // 1. a nickname that someone else is currently holding
  352. // 2. a nickname confusable with an existing reserved nickname
  353. // this has a lot of weird edge cases because of force-guest-format
  354. // and the possibility of registering a nickname on an "unregistered connection"
  355. // (i.e., pre-handshake).
  356. if client != nil && config.Accounts.NickReservation.Enabled {
  357. _, nickAcquireError, _ := am.server.clients.SetNick(client, nil, account, true)
  358. if !(nickAcquireError == nil || nickAcquireError == errNoop) {
  359. return errAccountMustHoldNick
  360. }
  361. }
  362. // can't register a guest nickname
  363. if config.Accounts.NickReservation.guestRegexpFolded.MatchString(casefoldedAccount) {
  364. return errAccountAlreadyRegistered
  365. }
  366. accountKey := fmt.Sprintf(keyAccountExists, casefoldedAccount)
  367. unregisteredKey := fmt.Sprintf(keyAccountUnregistered, casefoldedAccount)
  368. accountNameKey := fmt.Sprintf(keyAccountName, casefoldedAccount)
  369. callbackKey := fmt.Sprintf(keyAccountCallback, casefoldedAccount)
  370. registeredTimeKey := fmt.Sprintf(keyAccountRegTime, casefoldedAccount)
  371. credentialsKey := fmt.Sprintf(keyAccountCredentials, casefoldedAccount)
  372. verificationCodeKey := fmt.Sprintf(keyAccountVerificationCode, casefoldedAccount)
  373. certFPKey := fmt.Sprintf(keyCertToAccount, certfp)
  374. var creds AccountCredentials
  375. creds.Version = 1
  376. err = creds.SetPassphrase(passphrase, am.server.Config().Accounts.Registration.BcryptCost)
  377. if err != nil {
  378. return err
  379. }
  380. creds.AddCertfp(certfp)
  381. credStr, err := creds.Serialize()
  382. if err != nil {
  383. return err
  384. }
  385. registeredTimeStr := strconv.FormatInt(time.Now().UnixNano(), 10)
  386. callbackSpec := fmt.Sprintf("%s:%s", callbackNamespace, callbackValue)
  387. var setOptions *buntdb.SetOptions
  388. ttl := time.Duration(config.Accounts.Registration.VerifyTimeout)
  389. if ttl != 0 {
  390. setOptions = &buntdb.SetOptions{Expires: true, TTL: ttl}
  391. }
  392. err = func() error {
  393. am.serialCacheUpdateMutex.Lock()
  394. defer am.serialCacheUpdateMutex.Unlock()
  395. // can't register an account with the same name as a registered nick
  396. if am.NickToAccount(casefoldedAccount) != "" {
  397. return errAccountAlreadyRegistered
  398. }
  399. return am.server.store.Update(func(tx *buntdb.Tx) error {
  400. if _, err := tx.Get(unregisteredKey); err == nil {
  401. return errAccountAlreadyUnregistered
  402. }
  403. _, err = am.loadRawAccount(tx, casefoldedAccount)
  404. if err != errAccountDoesNotExist {
  405. return errAccountAlreadyRegistered
  406. }
  407. if certfp != "" {
  408. // make sure certfp doesn't already exist because that'd be silly
  409. _, err := tx.Get(certFPKey)
  410. if err != buntdb.ErrNotFound {
  411. return errCertfpAlreadyExists
  412. }
  413. }
  414. tx.Set(accountKey, "1", setOptions)
  415. tx.Set(accountNameKey, account, setOptions)
  416. tx.Set(registeredTimeKey, registeredTimeStr, setOptions)
  417. tx.Set(credentialsKey, credStr, setOptions)
  418. tx.Set(callbackKey, callbackSpec, setOptions)
  419. if certfp != "" {
  420. tx.Set(certFPKey, casefoldedAccount, setOptions)
  421. }
  422. return nil
  423. })
  424. }()
  425. if err != nil {
  426. return err
  427. }
  428. code, err := am.dispatchCallback(client, account, callbackNamespace, callbackValue)
  429. if err != nil {
  430. am.Unregister(casefoldedAccount, true)
  431. return errCallbackFailed
  432. } else {
  433. return am.server.store.Update(func(tx *buntdb.Tx) error {
  434. _, _, err = tx.Set(verificationCodeKey, code, setOptions)
  435. return err
  436. })
  437. }
  438. }
  439. // validatePassphrase checks whether a passphrase is allowed by our rules
  440. func validatePassphrase(passphrase string) error {
  441. // sanity check the length
  442. if len(passphrase) == 0 || len(passphrase) > 300 {
  443. return errAccountBadPassphrase
  444. }
  445. // we use * as a placeholder in some places, if it's gotten this far then fail
  446. if passphrase == "*" {
  447. return errAccountBadPassphrase
  448. }
  449. // for now, just enforce that spaces are not allowed
  450. for _, r := range passphrase {
  451. if unicode.IsSpace(r) {
  452. return errAccountBadPassphrase
  453. }
  454. }
  455. return nil
  456. }
  457. // changes the password for an account
  458. func (am *AccountManager) setPassword(account string, password string, hasPrivs bool) (err error) {
  459. cfAccount, err := CasefoldName(account)
  460. if err != nil {
  461. return errAccountDoesNotExist
  462. }
  463. credKey := fmt.Sprintf(keyAccountCredentials, cfAccount)
  464. var credStr string
  465. am.server.store.View(func(tx *buntdb.Tx) error {
  466. // no need to check verification status here or below;
  467. // you either need to be auth'ed to the account or be an oper to do this
  468. credStr, err = tx.Get(credKey)
  469. return nil
  470. })
  471. if err != nil {
  472. return errAccountDoesNotExist
  473. }
  474. var creds AccountCredentials
  475. err = json.Unmarshal([]byte(credStr), &creds)
  476. if err != nil {
  477. return err
  478. }
  479. if !hasPrivs && creds.Empty() {
  480. return errCredsExternallyManaged
  481. }
  482. err = creds.SetPassphrase(password, am.server.Config().Accounts.Registration.BcryptCost)
  483. if err != nil {
  484. return err
  485. }
  486. if creds.Empty() && !hasPrivs {
  487. return errEmptyCredentials
  488. }
  489. newCredStr, err := creds.Serialize()
  490. if err != nil {
  491. return err
  492. }
  493. err = am.server.store.Update(func(tx *buntdb.Tx) error {
  494. curCredStr, err := tx.Get(credKey)
  495. if credStr != curCredStr {
  496. return errCASFailed
  497. }
  498. _, _, err = tx.Set(credKey, newCredStr, nil)
  499. return err
  500. })
  501. return err
  502. }
  503. func (am *AccountManager) saveChannels(account string, channels []string) {
  504. channelsStr := strings.Join(channels, ",")
  505. key := fmt.Sprintf(keyAccountJoinedChannels, account)
  506. am.server.store.Update(func(tx *buntdb.Tx) error {
  507. tx.Set(key, channelsStr, nil)
  508. return nil
  509. })
  510. }
  511. func (am *AccountManager) loadChannels(account string) (channels []string) {
  512. key := fmt.Sprintf(keyAccountJoinedChannels, account)
  513. var channelsStr string
  514. am.server.store.View(func(tx *buntdb.Tx) error {
  515. channelsStr, _ = tx.Get(key)
  516. return nil
  517. })
  518. if channelsStr != "" {
  519. return strings.Split(channelsStr, ",")
  520. }
  521. return
  522. }
  523. func (am *AccountManager) saveModes(account string, uModes modes.Modes) {
  524. modeStr := uModes.String()
  525. key := fmt.Sprintf(keyAccountModes, account)
  526. am.server.store.Update(func(tx *buntdb.Tx) error {
  527. tx.Set(key, modeStr, nil)
  528. return nil
  529. })
  530. }
  531. func (am *AccountManager) loadModes(account string) (uModes modes.Modes) {
  532. key := fmt.Sprintf(keyAccountModes, account)
  533. var modeStr string
  534. am.server.store.View(func(tx *buntdb.Tx) error {
  535. modeStr, _ = tx.Get(key)
  536. return nil
  537. })
  538. for _, m := range modeStr {
  539. uModes = append(uModes, modes.Mode(m))
  540. }
  541. return
  542. }
  543. func (am *AccountManager) saveLastSeen(account string, lastSeen map[string]time.Time) {
  544. key := fmt.Sprintf(keyAccountLastSeen, account)
  545. var val string
  546. if len(lastSeen) != 0 {
  547. text, _ := json.Marshal(lastSeen)
  548. val = string(text)
  549. }
  550. am.server.store.Update(func(tx *buntdb.Tx) error {
  551. if val != "" {
  552. tx.Set(key, val, nil)
  553. } else {
  554. tx.Delete(key)
  555. }
  556. return nil
  557. })
  558. }
  559. func (am *AccountManager) loadLastSeen(account string) (lastSeen map[string]time.Time) {
  560. key := fmt.Sprintf(keyAccountLastSeen, account)
  561. var lsText string
  562. am.server.store.Update(func(tx *buntdb.Tx) error {
  563. lsText, _ = tx.Get(key)
  564. return nil
  565. })
  566. if lsText == "" {
  567. return nil
  568. }
  569. err := json.Unmarshal([]byte(lsText), &lastSeen)
  570. if err != nil {
  571. return nil
  572. }
  573. return
  574. }
  575. func (am *AccountManager) saveRealname(account string, realname string) {
  576. key := fmt.Sprintf(keyAccountRealname, account)
  577. am.server.store.Update(func(tx *buntdb.Tx) error {
  578. if realname != "" {
  579. tx.Set(key, realname, nil)
  580. } else {
  581. tx.Delete(key)
  582. }
  583. return nil
  584. })
  585. }
  586. func (am *AccountManager) loadRealname(account string) (realname string) {
  587. key := fmt.Sprintf(keyAccountRealname, account)
  588. am.server.store.Update(func(tx *buntdb.Tx) error {
  589. realname, _ = tx.Get(key)
  590. return nil
  591. })
  592. return
  593. }
  594. func (am *AccountManager) addRemoveCertfp(account, certfp string, add bool, hasPrivs bool) (err error) {
  595. certfp, err = utils.NormalizeCertfp(certfp)
  596. if err != nil {
  597. return err
  598. }
  599. cfAccount, err := CasefoldName(account)
  600. if err != nil {
  601. return errAccountDoesNotExist
  602. }
  603. credKey := fmt.Sprintf(keyAccountCredentials, cfAccount)
  604. var credStr string
  605. am.server.store.View(func(tx *buntdb.Tx) error {
  606. credStr, err = tx.Get(credKey)
  607. return nil
  608. })
  609. if err != nil {
  610. return errAccountDoesNotExist
  611. }
  612. var creds AccountCredentials
  613. err = json.Unmarshal([]byte(credStr), &creds)
  614. if err != nil {
  615. return err
  616. }
  617. if !hasPrivs && creds.Empty() {
  618. return errCredsExternallyManaged
  619. }
  620. if add {
  621. err = creds.AddCertfp(certfp)
  622. } else {
  623. err = creds.RemoveCertfp(certfp)
  624. }
  625. if err != nil {
  626. return err
  627. }
  628. if creds.Empty() && !hasPrivs {
  629. return errEmptyCredentials
  630. }
  631. newCredStr, err := creds.Serialize()
  632. if err != nil {
  633. return err
  634. }
  635. certfpKey := fmt.Sprintf(keyCertToAccount, certfp)
  636. err = am.server.store.Update(func(tx *buntdb.Tx) error {
  637. curCredStr, err := tx.Get(credKey)
  638. if credStr != curCredStr {
  639. return errCASFailed
  640. }
  641. if add {
  642. _, err = tx.Get(certfpKey)
  643. if err != buntdb.ErrNotFound {
  644. return errCertfpAlreadyExists
  645. }
  646. tx.Set(certfpKey, cfAccount, nil)
  647. } else {
  648. tx.Delete(certfpKey)
  649. }
  650. _, _, err = tx.Set(credKey, newCredStr, nil)
  651. return err
  652. })
  653. return err
  654. }
  655. func (am *AccountManager) dispatchCallback(client *Client, account string, callbackNamespace string, callbackValue string) (string, error) {
  656. if callbackNamespace == "*" || callbackNamespace == "none" || callbackNamespace == "admin" {
  657. return "", nil
  658. } else if callbackNamespace == "mailto" {
  659. return am.dispatchMailtoCallback(client, account, callbackValue)
  660. } else {
  661. return "", fmt.Errorf("Callback not implemented: %s", callbackNamespace)
  662. }
  663. }
  664. func (am *AccountManager) dispatchMailtoCallback(client *Client, account string, callbackValue string) (code string, err error) {
  665. config := am.server.Config().Accounts.Registration.EmailVerification
  666. code = utils.GenerateSecretToken()
  667. subject := config.VerifyMessageSubject
  668. if subject == "" {
  669. subject = fmt.Sprintf(client.t("Verify your account on %s"), am.server.name)
  670. }
  671. var message bytes.Buffer
  672. fmt.Fprintf(&message, "From: %s\r\n", config.Sender)
  673. fmt.Fprintf(&message, "To: %s\r\n", callbackValue)
  674. if config.DKIM.Domain != "" {
  675. fmt.Fprintf(&message, "Message-ID: <%s@%s>\r\n", utils.GenerateSecretKey(), config.DKIM.Domain)
  676. }
  677. fmt.Fprintf(&message, "Date: %s\r\n", time.Now().UTC().Format(time.RFC1123Z))
  678. fmt.Fprintf(&message, "Subject: %s\r\n", subject)
  679. message.WriteString("\r\n") // blank line: end headers, begin message body
  680. fmt.Fprintf(&message, client.t("Account: %s"), account)
  681. message.WriteString("\r\n")
  682. fmt.Fprintf(&message, client.t("Verification code: %s"), code)
  683. message.WriteString("\r\n")
  684. message.WriteString("\r\n")
  685. message.WriteString(client.t("To verify your account, issue the following command:"))
  686. message.WriteString("\r\n")
  687. fmt.Fprintf(&message, "/MSG NickServ VERIFY %s %s\r\n", account, code)
  688. err = email.SendMail(config, callbackValue, message.Bytes())
  689. if err != nil {
  690. am.server.logger.Error("internal", "Failed to dispatch e-mail to", callbackValue, err.Error())
  691. }
  692. return
  693. }
  694. func (am *AccountManager) Verify(client *Client, account string, code string) error {
  695. casefoldedAccount, err := CasefoldName(account)
  696. var skeleton string
  697. if err != nil || account == "" || account == "*" {
  698. return errAccountVerificationFailed
  699. }
  700. if client != nil && client.Account() != "" {
  701. return errAccountAlreadyLoggedIn
  702. }
  703. verifiedKey := fmt.Sprintf(keyAccountVerified, casefoldedAccount)
  704. accountKey := fmt.Sprintf(keyAccountExists, casefoldedAccount)
  705. accountNameKey := fmt.Sprintf(keyAccountName, casefoldedAccount)
  706. registeredTimeKey := fmt.Sprintf(keyAccountRegTime, casefoldedAccount)
  707. verificationCodeKey := fmt.Sprintf(keyAccountVerificationCode, casefoldedAccount)
  708. callbackKey := fmt.Sprintf(keyAccountCallback, casefoldedAccount)
  709. credentialsKey := fmt.Sprintf(keyAccountCredentials, casefoldedAccount)
  710. var raw rawClientAccount
  711. func() {
  712. am.serialCacheUpdateMutex.Lock()
  713. defer am.serialCacheUpdateMutex.Unlock()
  714. err = am.server.store.Update(func(tx *buntdb.Tx) error {
  715. raw, err = am.loadRawAccount(tx, casefoldedAccount)
  716. if err == errAccountDoesNotExist {
  717. return errAccountDoesNotExist
  718. } else if err != nil {
  719. return errAccountVerificationFailed
  720. } else if raw.Verified {
  721. return errAccountAlreadyVerified
  722. }
  723. // actually verify the code
  724. // a stored code of "" means a none callback / no code required
  725. success := false
  726. storedCode, err := tx.Get(verificationCodeKey)
  727. if err == nil {
  728. // this is probably unnecessary
  729. if storedCode == "" || utils.SecretTokensMatch(storedCode, code) {
  730. success = true
  731. }
  732. }
  733. if !success {
  734. return errAccountVerificationInvalidCode
  735. }
  736. // verify the account
  737. tx.Set(verifiedKey, "1", nil)
  738. // don't need the code anymore
  739. tx.Delete(verificationCodeKey)
  740. // re-set all other keys, removing the TTL
  741. tx.Set(accountKey, "1", nil)
  742. tx.Set(accountNameKey, raw.Name, nil)
  743. tx.Set(registeredTimeKey, raw.RegisteredAt, nil)
  744. tx.Set(callbackKey, raw.Callback, nil)
  745. tx.Set(credentialsKey, raw.Credentials, nil)
  746. var creds AccountCredentials
  747. // XXX we shouldn't do (de)serialization inside the txn,
  748. // but this is like 2 usec on my system
  749. json.Unmarshal([]byte(raw.Credentials), &creds)
  750. for _, cert := range creds.Certfps {
  751. certFPKey := fmt.Sprintf(keyCertToAccount, cert)
  752. tx.Set(certFPKey, casefoldedAccount, nil)
  753. }
  754. return nil
  755. })
  756. if err == nil {
  757. skeleton, _ = Skeleton(raw.Name)
  758. am.Lock()
  759. am.nickToAccount[casefoldedAccount] = casefoldedAccount
  760. am.skeletonToAccount[skeleton] = casefoldedAccount
  761. am.Unlock()
  762. }
  763. }()
  764. if err != nil {
  765. return err
  766. }
  767. nick := "[server admin]"
  768. if client != nil {
  769. nick = client.Nick()
  770. }
  771. am.server.logger.Info("accounts", "client", nick, "registered account", casefoldedAccount)
  772. raw.Verified = true
  773. clientAccount, err := am.deserializeRawAccount(raw, casefoldedAccount)
  774. if err != nil {
  775. return err
  776. }
  777. if client != nil {
  778. am.Login(client, clientAccount)
  779. if client.AlwaysOn() {
  780. client.markDirty(IncludeRealname)
  781. }
  782. }
  783. // we may need to do nick enforcement here:
  784. _, method := am.EnforcementStatus(casefoldedAccount, skeleton)
  785. if method == NickEnforcementStrict {
  786. currentClient := am.server.clients.Get(casefoldedAccount)
  787. if currentClient != nil && currentClient != client && currentClient.Account() != casefoldedAccount {
  788. am.server.RandomlyRename(currentClient)
  789. }
  790. }
  791. return nil
  792. }
  793. // register and verify an account, for internal use
  794. func (am *AccountManager) SARegister(account, passphrase string) (err error) {
  795. err = am.Register(nil, account, "admin", "", passphrase, "")
  796. if err == nil {
  797. err = am.Verify(nil, account, "")
  798. }
  799. return
  800. }
  801. func marshalReservedNicks(nicks []string) string {
  802. return strings.Join(nicks, ",")
  803. }
  804. func unmarshalReservedNicks(nicks string) (result []string) {
  805. if nicks == "" {
  806. return
  807. }
  808. return strings.Split(nicks, ",")
  809. }
  810. func (am *AccountManager) SetNickReserved(client *Client, nick string, saUnreserve bool, reserve bool) error {
  811. cfnick, err := CasefoldName(nick)
  812. skeleton, skerr := Skeleton(nick)
  813. // garbage nick, or garbage options, or disabled
  814. nrconfig := am.server.Config().Accounts.NickReservation
  815. if err != nil || skerr != nil || cfnick == "" || (reserve && saUnreserve) || !nrconfig.Enabled {
  816. return errAccountNickReservationFailed
  817. }
  818. // the cache is in sync with the DB while we hold serialCacheUpdateMutex
  819. am.serialCacheUpdateMutex.Lock()
  820. defer am.serialCacheUpdateMutex.Unlock()
  821. // find the affected account, which is usually the client's:
  822. account := client.Account()
  823. if saUnreserve {
  824. // unless this is a sadrop:
  825. account = am.NickToAccount(cfnick)
  826. if account == "" {
  827. // nothing to do
  828. return nil
  829. }
  830. }
  831. if account == "" {
  832. return errAccountNotLoggedIn
  833. }
  834. am.Lock()
  835. accountForNick := am.nickToAccount[cfnick]
  836. var accountForSkeleton string
  837. if reserve {
  838. accountForSkeleton = am.skeletonToAccount[skeleton]
  839. }
  840. am.Unlock()
  841. if reserve && (accountForNick != "" || accountForSkeleton != "") {
  842. return errNicknameReserved
  843. } else if !reserve && !saUnreserve && accountForNick != account {
  844. return errNicknameReserved
  845. } else if !reserve && cfnick == account {
  846. return errAccountCantDropPrimaryNick
  847. }
  848. nicksKey := fmt.Sprintf(keyAccountAdditionalNicks, account)
  849. unverifiedAccountKey := fmt.Sprintf(keyAccountExists, cfnick)
  850. err = am.server.store.Update(func(tx *buntdb.Tx) error {
  851. if reserve {
  852. // unverified accounts don't show up in NickToAccount yet (which is intentional),
  853. // however you shouldn't be able to reserve a nick out from under them
  854. _, err := tx.Get(unverifiedAccountKey)
  855. if err == nil {
  856. return errNicknameReserved
  857. }
  858. }
  859. rawNicks, err := tx.Get(nicksKey)
  860. if err != nil && err != buntdb.ErrNotFound {
  861. return err
  862. }
  863. nicks := unmarshalReservedNicks(rawNicks)
  864. if reserve {
  865. if len(nicks) >= nrconfig.AdditionalNickLimit {
  866. return errAccountTooManyNicks
  867. }
  868. nicks = append(nicks, nick)
  869. } else {
  870. // compute (original reserved nicks) minus cfnick
  871. var newNicks []string
  872. for _, reservedNick := range nicks {
  873. cfreservednick, _ := CasefoldName(reservedNick)
  874. if cfreservednick != cfnick {
  875. newNicks = append(newNicks, reservedNick)
  876. } else {
  877. // found the original, unfolded version of the nick we're dropping;
  878. // recompute the true skeleton from it
  879. skeleton, _ = Skeleton(reservedNick)
  880. }
  881. }
  882. nicks = newNicks
  883. }
  884. marshaledNicks := marshalReservedNicks(nicks)
  885. _, _, err = tx.Set(nicksKey, string(marshaledNicks), nil)
  886. return err
  887. })
  888. if err == errAccountTooManyNicks || err == errNicknameReserved {
  889. return err
  890. } else if err != nil {
  891. return errAccountNickReservationFailed
  892. }
  893. // success
  894. am.Lock()
  895. defer am.Unlock()
  896. if reserve {
  897. am.nickToAccount[cfnick] = account
  898. am.skeletonToAccount[skeleton] = account
  899. } else {
  900. delete(am.nickToAccount, cfnick)
  901. delete(am.skeletonToAccount, skeleton)
  902. }
  903. return nil
  904. }
  905. func (am *AccountManager) checkPassphrase(accountName, passphrase string) (account ClientAccount, err error) {
  906. account, err = am.LoadAccount(accountName)
  907. if err != nil {
  908. return
  909. }
  910. if !account.Verified {
  911. err = errAccountUnverified
  912. return
  913. }
  914. switch account.Credentials.Version {
  915. case 0:
  916. err = am.checkLegacyPassphrase(migrations.CheckOragonoPassphraseV0, accountName, account.Credentials.PassphraseHash, passphrase)
  917. case 1:
  918. if passwd.CompareHashAndPassword(account.Credentials.PassphraseHash, []byte(passphrase)) != nil {
  919. err = errAccountInvalidCredentials
  920. }
  921. case -1:
  922. err = am.checkLegacyPassphrase(migrations.CheckAthemePassphrase, accountName, account.Credentials.PassphraseHash, passphrase)
  923. default:
  924. err = errAccountInvalidCredentials
  925. }
  926. return
  927. }
  928. func (am *AccountManager) checkLegacyPassphrase(check migrations.PassphraseCheck, account string, hash []byte, passphrase string) (err error) {
  929. err = check(hash, []byte(passphrase))
  930. if err != nil {
  931. if err == migrations.ErrHashInvalid {
  932. am.server.logger.Error("internal", "invalid legacy credentials for account", account)
  933. }
  934. return errAccountInvalidCredentials
  935. }
  936. // re-hash the passphrase with the latest algorithm
  937. err = am.setPassword(account, passphrase, true)
  938. if err != nil {
  939. am.server.logger.Error("internal", "could not upgrade user password", err.Error())
  940. }
  941. return nil
  942. }
  943. func (am *AccountManager) loadWithAutocreation(accountName string, autocreate bool) (account ClientAccount, err error) {
  944. account, err = am.LoadAccount(accountName)
  945. if err == errAccountDoesNotExist && autocreate {
  946. err = am.SARegister(accountName, "")
  947. if err != nil {
  948. return
  949. }
  950. account, err = am.LoadAccount(accountName)
  951. }
  952. return
  953. }
  954. func (am *AccountManager) AuthenticateByPassphrase(client *Client, accountName string, passphrase string) (err error) {
  955. // XXX check this now, so we don't allow a redundant login for an always-on client
  956. // even for a brief period. the other potential source of nick-account conflicts
  957. // is from force-nick-equals-account, but those will be caught later by
  958. // fixupNickEqualsAccount and if there is a conflict, they will be logged out.
  959. if client.registered {
  960. if clientAlready := am.server.clients.Get(accountName); clientAlready != nil && clientAlready.AlwaysOn() {
  961. return errNickAccountMismatch
  962. }
  963. }
  964. if throttled, remainingTime := client.checkLoginThrottle(); throttled {
  965. return &ThrottleError{remainingTime}
  966. }
  967. var account ClientAccount
  968. defer func() {
  969. if err == nil {
  970. am.Login(client, account)
  971. }
  972. }()
  973. config := am.server.Config()
  974. if config.Accounts.AuthScript.Enabled {
  975. var output AuthScriptOutput
  976. output, err = CheckAuthScript(am.server.semaphores.AuthScript, config.Accounts.AuthScript.ScriptConfig,
  977. AuthScriptInput{AccountName: accountName, Passphrase: passphrase, IP: client.IP().String()})
  978. if err != nil {
  979. am.server.logger.Error("internal", "failed shell auth invocation", err.Error())
  980. } else if output.Success {
  981. if output.AccountName != "" {
  982. accountName = output.AccountName
  983. }
  984. account, err = am.loadWithAutocreation(accountName, config.Accounts.AuthScript.Autocreate)
  985. return
  986. }
  987. }
  988. account, err = am.checkPassphrase(accountName, passphrase)
  989. return err
  990. }
  991. // AllNicks returns the uncasefolded nicknames for all accounts, including additional (grouped) nicks.
  992. func (am *AccountManager) AllNicks() (result []string) {
  993. accountNamePrefix := fmt.Sprintf(keyAccountName, "")
  994. accountAdditionalNicksPrefix := fmt.Sprintf(keyAccountAdditionalNicks, "")
  995. am.server.store.View(func(tx *buntdb.Tx) error {
  996. // Account names
  997. err := tx.AscendGreaterOrEqual("", accountNamePrefix, func(key, value string) bool {
  998. if !strings.HasPrefix(key, accountNamePrefix) {
  999. return false
  1000. }
  1001. result = append(result, value)
  1002. return true
  1003. })
  1004. if err != nil {
  1005. return err
  1006. }
  1007. // Additional nicks
  1008. return tx.AscendGreaterOrEqual("", accountAdditionalNicksPrefix, func(key, value string) bool {
  1009. if !strings.HasPrefix(key, accountAdditionalNicksPrefix) {
  1010. return false
  1011. }
  1012. additionalNicks := unmarshalReservedNicks(value)
  1013. for _, additionalNick := range additionalNicks {
  1014. result = append(result, additionalNick)
  1015. }
  1016. return true
  1017. })
  1018. })
  1019. sort.Strings(result)
  1020. return
  1021. }
  1022. func (am *AccountManager) LoadAccount(accountName string) (result ClientAccount, err error) {
  1023. casefoldedAccount, err := CasefoldName(accountName)
  1024. if err != nil {
  1025. err = errAccountDoesNotExist
  1026. return
  1027. }
  1028. var raw rawClientAccount
  1029. am.server.store.View(func(tx *buntdb.Tx) error {
  1030. raw, err = am.loadRawAccount(tx, casefoldedAccount)
  1031. return nil
  1032. })
  1033. if err != nil {
  1034. return
  1035. }
  1036. result, err = am.deserializeRawAccount(raw, casefoldedAccount)
  1037. return
  1038. }
  1039. // look up the unfolded version of an account name, possibly after deletion
  1040. func (am *AccountManager) AccountToAccountName(account string) (result string) {
  1041. casefoldedAccount, err := CasefoldName(account)
  1042. if err != nil {
  1043. return
  1044. }
  1045. unregisteredKey := fmt.Sprintf(keyAccountUnregistered, casefoldedAccount)
  1046. accountNameKey := fmt.Sprintf(keyAccountName, casefoldedAccount)
  1047. am.server.store.View(func(tx *buntdb.Tx) error {
  1048. if name, err := tx.Get(accountNameKey); err == nil {
  1049. result = name
  1050. return nil
  1051. }
  1052. if name, err := tx.Get(unregisteredKey); err == nil {
  1053. result = name
  1054. }
  1055. return nil
  1056. })
  1057. return
  1058. }
  1059. func (am *AccountManager) deserializeRawAccount(raw rawClientAccount, cfName string) (result ClientAccount, err error) {
  1060. result.Name = raw.Name
  1061. result.NameCasefolded = cfName
  1062. regTimeInt, _ := strconv.ParseInt(raw.RegisteredAt, 10, 64)
  1063. result.RegisteredAt = time.Unix(0, regTimeInt).UTC()
  1064. e := json.Unmarshal([]byte(raw.Credentials), &result.Credentials)
  1065. if e != nil {
  1066. am.server.logger.Error("internal", "could not unmarshal credentials", e.Error())
  1067. err = errAccountDoesNotExist
  1068. return
  1069. }
  1070. result.AdditionalNicks = unmarshalReservedNicks(raw.AdditionalNicks)
  1071. result.Verified = raw.Verified
  1072. if raw.VHost != "" {
  1073. e := json.Unmarshal([]byte(raw.VHost), &result.VHost)
  1074. if e != nil {
  1075. am.server.logger.Warning("internal", "could not unmarshal vhost for account", result.Name, e.Error())
  1076. // pretend they have no vhost and move on
  1077. }
  1078. }
  1079. if raw.Settings != "" {
  1080. e := json.Unmarshal([]byte(raw.Settings), &result.Settings)
  1081. if e != nil {
  1082. am.server.logger.Warning("internal", "could not unmarshal settings for account", result.Name, e.Error())
  1083. }
  1084. }
  1085. return
  1086. }
  1087. func (am *AccountManager) loadRawAccount(tx *buntdb.Tx, casefoldedAccount string) (result rawClientAccount, err error) {
  1088. accountKey := fmt.Sprintf(keyAccountExists, casefoldedAccount)
  1089. accountNameKey := fmt.Sprintf(keyAccountName, casefoldedAccount)
  1090. registeredTimeKey := fmt.Sprintf(keyAccountRegTime, casefoldedAccount)
  1091. credentialsKey := fmt.Sprintf(keyAccountCredentials, casefoldedAccount)
  1092. verifiedKey := fmt.Sprintf(keyAccountVerified, casefoldedAccount)
  1093. callbackKey := fmt.Sprintf(keyAccountCallback, casefoldedAccount)
  1094. nicksKey := fmt.Sprintf(keyAccountAdditionalNicks, casefoldedAccount)
  1095. vhostKey := fmt.Sprintf(keyAccountVHost, casefoldedAccount)
  1096. settingsKey := fmt.Sprintf(keyAccountSettings, casefoldedAccount)
  1097. _, e := tx.Get(accountKey)
  1098. if e == buntdb.ErrNotFound {
  1099. err = errAccountDoesNotExist
  1100. return
  1101. }
  1102. result.Name, _ = tx.Get(accountNameKey)
  1103. result.RegisteredAt, _ = tx.Get(registeredTimeKey)
  1104. result.Credentials, _ = tx.Get(credentialsKey)
  1105. result.Callback, _ = tx.Get(callbackKey)
  1106. result.AdditionalNicks, _ = tx.Get(nicksKey)
  1107. result.VHost, _ = tx.Get(vhostKey)
  1108. result.Settings, _ = tx.Get(settingsKey)
  1109. if _, e = tx.Get(verifiedKey); e == nil {
  1110. result.Verified = true
  1111. }
  1112. return
  1113. }
  1114. func (am *AccountManager) Suspend(accountName string) (err error) {
  1115. account, err := CasefoldName(accountName)
  1116. if err != nil {
  1117. return errAccountDoesNotExist
  1118. }
  1119. existsKey := fmt.Sprintf(keyAccountExists, account)
  1120. verifiedKey := fmt.Sprintf(keyAccountVerified, account)
  1121. err = am.server.store.Update(func(tx *buntdb.Tx) error {
  1122. _, err := tx.Get(existsKey)
  1123. if err != nil {
  1124. return errAccountDoesNotExist
  1125. }
  1126. _, err = tx.Delete(verifiedKey)
  1127. return err
  1128. })
  1129. if err == errAccountDoesNotExist {
  1130. return err
  1131. } else if err != nil {
  1132. am.server.logger.Error("internal", "couldn't persist suspension", account, err.Error())
  1133. } // keep going
  1134. am.Lock()
  1135. clients := am.accountToClients[account]
  1136. delete(am.accountToClients, account)
  1137. am.Unlock()
  1138. am.killClients(clients)
  1139. return nil
  1140. }
  1141. func (am *AccountManager) killClients(clients []*Client) {
  1142. for _, client := range clients {
  1143. client.Logout()
  1144. client.Quit(client.t("You are no longer authorized to be on this server"), nil)
  1145. client.destroy(nil)
  1146. }
  1147. }
  1148. func (am *AccountManager) Unsuspend(account string) (err error) {
  1149. cfaccount, err := CasefoldName(account)
  1150. if err != nil {
  1151. return errAccountDoesNotExist
  1152. }
  1153. existsKey := fmt.Sprintf(keyAccountExists, cfaccount)
  1154. verifiedKey := fmt.Sprintf(keyAccountVerified, cfaccount)
  1155. err = am.server.store.Update(func(tx *buntdb.Tx) error {
  1156. _, err := tx.Get(existsKey)
  1157. if err != nil {
  1158. return errAccountDoesNotExist
  1159. }
  1160. tx.Set(verifiedKey, "1", nil)
  1161. return nil
  1162. })
  1163. if err != nil {
  1164. return errAccountDoesNotExist
  1165. }
  1166. return nil
  1167. }
  1168. func (am *AccountManager) Unregister(account string, erase bool) error {
  1169. config := am.server.Config()
  1170. casefoldedAccount, err := CasefoldName(account)
  1171. if err != nil {
  1172. return errAccountDoesNotExist
  1173. }
  1174. accountKey := fmt.Sprintf(keyAccountExists, casefoldedAccount)
  1175. accountNameKey := fmt.Sprintf(keyAccountName, casefoldedAccount)
  1176. registeredTimeKey := fmt.Sprintf(keyAccountRegTime, casefoldedAccount)
  1177. credentialsKey := fmt.Sprintf(keyAccountCredentials, casefoldedAccount)
  1178. callbackKey := fmt.Sprintf(keyAccountCallback, casefoldedAccount)
  1179. verificationCodeKey := fmt.Sprintf(keyAccountVerificationCode, casefoldedAccount)
  1180. verifiedKey := fmt.Sprintf(keyAccountVerified, casefoldedAccount)
  1181. nicksKey := fmt.Sprintf(keyAccountAdditionalNicks, casefoldedAccount)
  1182. settingsKey := fmt.Sprintf(keyAccountSettings, casefoldedAccount)
  1183. vhostKey := fmt.Sprintf(keyAccountVHost, casefoldedAccount)
  1184. vhostQueueKey := fmt.Sprintf(keyVHostQueueAcctToId, casefoldedAccount)
  1185. channelsKey := fmt.Sprintf(keyAccountChannels, casefoldedAccount)
  1186. joinedChannelsKey := fmt.Sprintf(keyAccountJoinedChannels, casefoldedAccount)
  1187. lastSeenKey := fmt.Sprintf(keyAccountLastSeen, casefoldedAccount)
  1188. unregisteredKey := fmt.Sprintf(keyAccountUnregistered, casefoldedAccount)
  1189. modesKey := fmt.Sprintf(keyAccountModes, casefoldedAccount)
  1190. var clients []*Client
  1191. defer func() {
  1192. am.killClients(clients)
  1193. }()
  1194. var registeredChannels []string
  1195. // on our way out, unregister all the account's channels and delete them from the db
  1196. defer func() {
  1197. for _, channelName := range registeredChannels {
  1198. err := am.server.channels.SetUnregistered(channelName, casefoldedAccount)
  1199. if err != nil {
  1200. am.server.logger.Error("internal", "couldn't unregister channel", channelName, err.Error())
  1201. }
  1202. }
  1203. }()
  1204. var credText string
  1205. var rawNicks string
  1206. am.serialCacheUpdateMutex.Lock()
  1207. defer am.serialCacheUpdateMutex.Unlock()
  1208. var accountName string
  1209. var channelsStr string
  1210. keepProtections := false
  1211. am.server.store.Update(func(tx *buntdb.Tx) error {
  1212. // get the unfolded account name; for an active account, this is
  1213. // stored under accountNameKey, for an unregistered account under unregisteredKey
  1214. accountName, _ = tx.Get(accountNameKey)
  1215. if accountName == "" {
  1216. accountName, _ = tx.Get(unregisteredKey)
  1217. }
  1218. if erase {
  1219. tx.Delete(unregisteredKey)
  1220. } else {
  1221. if _, err := tx.Get(verifiedKey); err == nil {
  1222. tx.Set(unregisteredKey, accountName, nil)
  1223. keepProtections = true
  1224. }
  1225. }
  1226. tx.Delete(accountKey)
  1227. tx.Delete(accountNameKey)
  1228. tx.Delete(verifiedKey)
  1229. tx.Delete(registeredTimeKey)
  1230. tx.Delete(callbackKey)
  1231. tx.Delete(verificationCodeKey)
  1232. tx.Delete(settingsKey)
  1233. rawNicks, _ = tx.Get(nicksKey)
  1234. tx.Delete(nicksKey)
  1235. credText, err = tx.Get(credentialsKey)
  1236. tx.Delete(credentialsKey)
  1237. tx.Delete(vhostKey)
  1238. channelsStr, _ = tx.Get(channelsKey)
  1239. tx.Delete(channelsKey)
  1240. tx.Delete(joinedChannelsKey)
  1241. tx.Delete(lastSeenKey)
  1242. tx.Delete(modesKey)
  1243. _, err := tx.Delete(vhostQueueKey)
  1244. am.decrementVHostQueueCount(casefoldedAccount, err)
  1245. return nil
  1246. })
  1247. if err == nil {
  1248. var creds AccountCredentials
  1249. if err := json.Unmarshal([]byte(credText), &creds); err == nil {
  1250. for _, cert := range creds.Certfps {
  1251. certFPKey := fmt.Sprintf(keyCertToAccount, cert)
  1252. am.server.store.Update(func(tx *buntdb.Tx) error {
  1253. if account, err := tx.Get(certFPKey); err == nil && account == casefoldedAccount {
  1254. tx.Delete(certFPKey)
  1255. }
  1256. return nil
  1257. })
  1258. }
  1259. }
  1260. }
  1261. skeleton, _ := Skeleton(accountName)
  1262. additionalNicks := unmarshalReservedNicks(rawNicks)
  1263. registeredChannels = unmarshalRegisteredChannels(channelsStr)
  1264. am.Lock()
  1265. defer am.Unlock()
  1266. clients = am.accountToClients[casefoldedAccount]
  1267. delete(am.accountToClients, casefoldedAccount)
  1268. // protect the account name itself where applicable, but not any grouped nicks
  1269. if !(keepProtections && config.Accounts.NickReservation.Method == NickEnforcementStrict) {
  1270. delete(am.nickToAccount, casefoldedAccount)
  1271. delete(am.skeletonToAccount, skeleton)
  1272. }
  1273. for _, nick := range additionalNicks {
  1274. delete(am.nickToAccount, nick)
  1275. additionalSkel, _ := Skeleton(nick)
  1276. delete(am.skeletonToAccount, additionalSkel)
  1277. }
  1278. if err != nil && !erase {
  1279. return errAccountDoesNotExist
  1280. }
  1281. return nil
  1282. }
  1283. func unmarshalRegisteredChannels(channelsStr string) (result []string) {
  1284. if channelsStr != "" {
  1285. result = strings.Split(channelsStr, ",")
  1286. }
  1287. return
  1288. }
  1289. func (am *AccountManager) ChannelsForAccount(account string) (channels []string) {
  1290. cfaccount, err := CasefoldName(account)
  1291. if err != nil {
  1292. return
  1293. }
  1294. var channelStr string
  1295. key := fmt.Sprintf(keyAccountChannels, cfaccount)
  1296. am.server.store.View(func(tx *buntdb.Tx) error {
  1297. channelStr, _ = tx.Get(key)
  1298. return nil
  1299. })
  1300. return unmarshalRegisteredChannels(channelStr)
  1301. }
  1302. func (am *AccountManager) AuthenticateByCertificate(client *Client, certfp string, peerCerts []*x509.Certificate, authzid string) (err error) {
  1303. if certfp == "" {
  1304. return errAccountInvalidCredentials
  1305. }
  1306. var clientAccount ClientAccount
  1307. defer func() {
  1308. if err != nil {
  1309. return
  1310. } else if !clientAccount.Verified {
  1311. err = errAccountUnverified
  1312. return
  1313. }
  1314. // TODO(#1109) clean this check up?
  1315. if client.registered {
  1316. if clientAlready := am.server.clients.Get(clientAccount.Name); clientAlready != nil && clientAlready.AlwaysOn() {
  1317. err = errNickAccountMismatch
  1318. return
  1319. }
  1320. }
  1321. am.Login(client, clientAccount)
  1322. return
  1323. }()
  1324. config := am.server.Config()
  1325. if config.Accounts.AuthScript.Enabled {
  1326. var output AuthScriptOutput
  1327. output, err = CheckAuthScript(am.server.semaphores.AuthScript, config.Accounts.AuthScript.ScriptConfig,
  1328. AuthScriptInput{Certfp: certfp, IP: client.IP().String(), peerCerts: peerCerts})
  1329. if err != nil {
  1330. am.server.logger.Error("internal", "failed shell auth invocation", err.Error())
  1331. } else if output.Success && output.AccountName != "" {
  1332. clientAccount, err = am.loadWithAutocreation(output.AccountName, config.Accounts.AuthScript.Autocreate)
  1333. return
  1334. }
  1335. }
  1336. var account string
  1337. certFPKey := fmt.Sprintf(keyCertToAccount, certfp)
  1338. err = am.server.store.View(func(tx *buntdb.Tx) error {
  1339. account, _ = tx.Get(certFPKey)
  1340. if account == "" {
  1341. return errAccountInvalidCredentials
  1342. }
  1343. return nil
  1344. })
  1345. if err != nil {
  1346. return err
  1347. }
  1348. if authzid != "" && authzid != account {
  1349. return errAuthzidAuthcidMismatch
  1350. }
  1351. // ok, we found an account corresponding to their certificate
  1352. clientAccount, err = am.LoadAccount(account)
  1353. return err
  1354. }
  1355. type settingsMunger func(input AccountSettings) (output AccountSettings, err error)
  1356. func (am *AccountManager) ModifyAccountSettings(account string, munger settingsMunger) (newSettings AccountSettings, err error) {
  1357. casefoldedAccount, err := CasefoldName(account)
  1358. if err != nil {
  1359. return newSettings, errAccountDoesNotExist
  1360. }
  1361. // TODO implement this in general via a compare-and-swap API
  1362. accountData, err := am.LoadAccount(casefoldedAccount)
  1363. if err != nil {
  1364. return
  1365. } else if !accountData.Verified {
  1366. return newSettings, errAccountUnverified
  1367. }
  1368. newSettings, err = munger(accountData.Settings)
  1369. if err != nil {
  1370. return
  1371. }
  1372. text, err := json.Marshal(newSettings)
  1373. if err != nil {
  1374. return
  1375. }
  1376. key := fmt.Sprintf(keyAccountSettings, casefoldedAccount)
  1377. serializedValue := string(text)
  1378. err = am.server.store.Update(func(tx *buntdb.Tx) (err error) {
  1379. _, _, err = tx.Set(key, serializedValue, nil)
  1380. return
  1381. })
  1382. if err != nil {
  1383. err = errAccountUpdateFailed
  1384. return
  1385. }
  1386. // success, push new settings into the client objects
  1387. am.Lock()
  1388. defer am.Unlock()
  1389. for _, client := range am.accountToClients[casefoldedAccount] {
  1390. client.SetAccountSettings(newSettings)
  1391. }
  1392. return
  1393. }
  1394. // represents someone's status in hostserv
  1395. type VHostInfo struct {
  1396. ApprovedVHost string
  1397. Enabled bool
  1398. RequestedVHost string
  1399. RejectedVHost string
  1400. RejectionReason string
  1401. LastRequestTime time.Time
  1402. }
  1403. // pair type, <VHostInfo, accountName>
  1404. type PendingVHostRequest struct {
  1405. VHostInfo
  1406. Account string
  1407. }
  1408. type vhostThrottleExceeded struct {
  1409. timeRemaining time.Duration
  1410. }
  1411. func (vhe *vhostThrottleExceeded) Error() string {
  1412. return fmt.Sprintf("Wait at least %v and try again", vhe.timeRemaining)
  1413. }
  1414. func (vh *VHostInfo) checkThrottle(cooldown time.Duration) (err error) {
  1415. if cooldown == 0 {
  1416. return nil
  1417. }
  1418. now := time.Now().UTC()
  1419. elapsed := now.Sub(vh.LastRequestTime)
  1420. if elapsed > cooldown {
  1421. // success
  1422. vh.LastRequestTime = now
  1423. return nil
  1424. } else {
  1425. return &vhostThrottleExceeded{timeRemaining: cooldown - elapsed}
  1426. }
  1427. }
  1428. // callback type implementing the actual business logic of vhost operations
  1429. type vhostMunger func(input VHostInfo) (output VHostInfo, err error)
  1430. func (am *AccountManager) VHostSet(account string, vhost string) (result VHostInfo, err error) {
  1431. munger := func(input VHostInfo) (output VHostInfo, err error) {
  1432. output = input
  1433. output.Enabled = true
  1434. output.ApprovedVHost = vhost
  1435. return
  1436. }
  1437. return am.performVHostChange(account, munger)
  1438. }
  1439. func (am *AccountManager) VHostRequest(account string, vhost string, cooldown time.Duration) (result VHostInfo, err error) {
  1440. munger := func(input VHostInfo) (output VHostInfo, err error) {
  1441. output = input
  1442. // you can update your existing request, but if you were approved or rejected,
  1443. // you can't spam a new request
  1444. if output.RequestedVHost == "" {
  1445. err = output.checkThrottle(cooldown)
  1446. }
  1447. if err != nil {
  1448. return
  1449. }
  1450. output.RequestedVHost = vhost
  1451. output.RejectedVHost = ""
  1452. output.RejectionReason = ""
  1453. output.LastRequestTime = time.Now().UTC()
  1454. return
  1455. }
  1456. return am.performVHostChange(account, munger)
  1457. }
  1458. func (am *AccountManager) VHostApprove(account string) (result VHostInfo, err error) {
  1459. munger := func(input VHostInfo) (output VHostInfo, err error) {
  1460. output = input
  1461. output.Enabled = true
  1462. output.ApprovedVHost = input.RequestedVHost
  1463. output.RequestedVHost = ""
  1464. output.RejectionReason = ""
  1465. return
  1466. }
  1467. return am.performVHostChange(account, munger)
  1468. }
  1469. func (am *AccountManager) VHostReject(account string, reason string) (result VHostInfo, err error) {
  1470. munger := func(input VHostInfo) (output VHostInfo, err error) {
  1471. output = input
  1472. output.RejectedVHost = output.RequestedVHost
  1473. output.RequestedVHost = ""
  1474. output.RejectionReason = reason
  1475. return
  1476. }
  1477. return am.performVHostChange(account, munger)
  1478. }
  1479. func (am *AccountManager) VHostSetEnabled(client *Client, enabled bool) (result VHostInfo, err error) {
  1480. munger := func(input VHostInfo) (output VHostInfo, err error) {
  1481. if input.ApprovedVHost == "" {
  1482. err = errNoVhost
  1483. return
  1484. }
  1485. output = input
  1486. output.Enabled = enabled
  1487. return
  1488. }
  1489. return am.performVHostChange(client.Account(), munger)
  1490. }
  1491. func (am *AccountManager) performVHostChange(account string, munger vhostMunger) (result VHostInfo, err error) {
  1492. account, err = CasefoldName(account)
  1493. if err != nil || account == "" {
  1494. err = errAccountDoesNotExist
  1495. return
  1496. }
  1497. if am.server.Defcon() <= 3 {
  1498. err = errFeatureDisabled
  1499. return
  1500. }
  1501. am.vHostUpdateMutex.Lock()
  1502. defer am.vHostUpdateMutex.Unlock()
  1503. clientAccount, err := am.LoadAccount(account)
  1504. if err != nil {
  1505. err = errAccountDoesNotExist
  1506. return
  1507. } else if !clientAccount.Verified {
  1508. err = errAccountUnverified
  1509. return
  1510. }
  1511. result, err = munger(clientAccount.VHost)
  1512. if err != nil {
  1513. return
  1514. }
  1515. vhtext, err := json.Marshal(result)
  1516. if err != nil {
  1517. err = errAccountUpdateFailed
  1518. return
  1519. }
  1520. vhstr := string(vhtext)
  1521. key := fmt.Sprintf(keyAccountVHost, account)
  1522. queueKey := fmt.Sprintf(keyVHostQueueAcctToId, account)
  1523. err = am.server.store.Update(func(tx *buntdb.Tx) error {
  1524. if _, _, err := tx.Set(key, vhstr, nil); err != nil {
  1525. return err
  1526. }
  1527. // update request queue
  1528. if clientAccount.VHost.RequestedVHost == "" && result.RequestedVHost != "" {
  1529. id := atomic.AddUint64(&am.vhostRequestID, 1)
  1530. if _, _, err = tx.Set(queueKey, strconv.FormatUint(id, 10), nil); err != nil {
  1531. return err
  1532. }
  1533. atomic.AddUint64(&am.vhostRequestPendingCount, 1)
  1534. } else if clientAccount.VHost.RequestedVHost != "" && result.RequestedVHost == "" {
  1535. _, err = tx.Delete(queueKey)
  1536. am.decrementVHostQueueCount(account, err)
  1537. }
  1538. return nil
  1539. })
  1540. if err != nil {
  1541. err = errAccountUpdateFailed
  1542. return
  1543. }
  1544. am.applyVhostToClients(account, result)
  1545. return result, nil
  1546. }
  1547. // XXX annoying helper method for keeping the queue count in sync with the DB
  1548. // `err` is the buntdb error returned from deleting the queue key
  1549. func (am *AccountManager) decrementVHostQueueCount(account string, err error) {
  1550. if err == nil {
  1551. // successfully deleted a queue entry, do a 2's complement decrement:
  1552. atomic.AddUint64(&am.vhostRequestPendingCount, ^uint64(0))
  1553. } else if err != buntdb.ErrNotFound {
  1554. am.server.logger.Error("internal", "buntdb dequeue error", account, err.Error())
  1555. }
  1556. }
  1557. func (am *AccountManager) VHostListRequests(limit int) (requests []PendingVHostRequest, total int) {
  1558. am.vHostUpdateMutex.Lock()
  1559. defer am.vHostUpdateMutex.Unlock()
  1560. total = int(atomic.LoadUint64(&am.vhostRequestPendingCount))
  1561. prefix := fmt.Sprintf(keyVHostQueueAcctToId, "")
  1562. accounts := make([]string, 0, limit)
  1563. err := am.server.store.View(func(tx *buntdb.Tx) error {
  1564. return tx.Ascend(vhostRequestIdx, func(key, value string) bool {
  1565. accounts = append(accounts, strings.TrimPrefix(key, prefix))
  1566. return len(accounts) < limit
  1567. })
  1568. })
  1569. if err != nil {
  1570. am.server.logger.Error("internal", "couldn't traverse vhost queue", err.Error())
  1571. return
  1572. }
  1573. for _, account := range accounts {
  1574. accountInfo, err := am.LoadAccount(account)
  1575. if err == nil {
  1576. requests = append(requests, PendingVHostRequest{
  1577. Account: account,
  1578. VHostInfo: accountInfo.VHost,
  1579. })
  1580. } else {
  1581. am.server.logger.Error("internal", "corrupt account", account, err.Error())
  1582. }
  1583. }
  1584. return
  1585. }
  1586. func (am *AccountManager) applyVHostInfo(client *Client, info VHostInfo) {
  1587. // if hostserv is disabled in config, then don't grant vhosts
  1588. // that were previously approved while it was enabled
  1589. if !am.server.Config().Accounts.VHosts.Enabled {
  1590. return
  1591. }
  1592. vhost := ""
  1593. if info.Enabled {
  1594. vhost = info.ApprovedVHost
  1595. }
  1596. oldNickmask := client.NickMaskString()
  1597. updated := client.SetVHost(vhost)
  1598. if updated && client.Registered() {
  1599. // TODO: doing I/O here is kind of a kludge
  1600. client.sendChghost(oldNickmask, client.Hostname())
  1601. }
  1602. }
  1603. func (am *AccountManager) applyVhostToClients(account string, result VHostInfo) {
  1604. am.RLock()
  1605. clients := am.accountToClients[account]
  1606. am.RUnlock()
  1607. for _, client := range clients {
  1608. am.applyVHostInfo(client, result)
  1609. }
  1610. }
  1611. func (am *AccountManager) Login(client *Client, account ClientAccount) {
  1612. client.Login(account)
  1613. am.applyVHostInfo(client, account.VHost)
  1614. casefoldedAccount := client.Account()
  1615. am.Lock()
  1616. defer am.Unlock()
  1617. am.accountToClients[casefoldedAccount] = append(am.accountToClients[casefoldedAccount], client)
  1618. }
  1619. func (am *AccountManager) Logout(client *Client) {
  1620. am.Lock()
  1621. defer am.Unlock()
  1622. casefoldedAccount := client.Account()
  1623. if casefoldedAccount == "" {
  1624. return
  1625. }
  1626. client.Logout()
  1627. clients := am.accountToClients[casefoldedAccount]
  1628. if len(clients) <= 1 {
  1629. delete(am.accountToClients, casefoldedAccount)
  1630. return
  1631. }
  1632. remainingClients := make([]*Client, len(clients)-1)
  1633. remainingPos := 0
  1634. for currentPos := 0; currentPos < len(clients); currentPos++ {
  1635. if clients[currentPos] != client {
  1636. remainingClients[remainingPos] = clients[currentPos]
  1637. remainingPos++
  1638. }
  1639. }
  1640. am.accountToClients[casefoldedAccount] = remainingClients
  1641. }
  1642. var (
  1643. // EnabledSaslMechanisms contains the SASL mechanisms that exist and that we support.
  1644. // This can be moved to some other data structure/place if we need to load/unload mechs later.
  1645. EnabledSaslMechanisms = map[string]func(*Server, *Client, string, []byte, *ResponseBuffer) bool{
  1646. "PLAIN": authPlainHandler,
  1647. "EXTERNAL": authExternalHandler,
  1648. }
  1649. )
  1650. type CredentialsVersion int
  1651. const (
  1652. CredentialsLegacy CredentialsVersion = 0
  1653. CredentialsSHA3Bcrypt CredentialsVersion = 1
  1654. // negative numbers for migration
  1655. CredentialsAtheme = -1
  1656. )
  1657. // AccountCredentials stores the various methods for verifying accounts.
  1658. type AccountCredentials struct {
  1659. Version CredentialsVersion
  1660. PassphraseHash []byte
  1661. Certfps []string
  1662. }
  1663. func (ac *AccountCredentials) Empty() bool {
  1664. return len(ac.PassphraseHash) == 0 && len(ac.Certfps) == 0
  1665. }
  1666. // helper to assemble the serialized JSON for an account's credentials
  1667. func (ac *AccountCredentials) Serialize() (result string, err error) {
  1668. ac.Version = 1
  1669. credText, err := json.Marshal(*ac)
  1670. if err != nil {
  1671. return "", err
  1672. }
  1673. return string(credText), nil
  1674. }
  1675. func (ac *AccountCredentials) SetPassphrase(passphrase string, bcryptCost uint) (err error) {
  1676. if passphrase == "" {
  1677. ac.PassphraseHash = nil
  1678. return nil
  1679. }
  1680. if validatePassphrase(passphrase) != nil {
  1681. return errAccountBadPassphrase
  1682. }
  1683. ac.PassphraseHash, err = passwd.GenerateFromPassword([]byte(passphrase), int(bcryptCost))
  1684. if err != nil {
  1685. return errAccountBadPassphrase
  1686. }
  1687. return nil
  1688. }
  1689. func (ac *AccountCredentials) AddCertfp(certfp string) (err error) {
  1690. // XXX we require that certfp is already normalized (rather than normalize here
  1691. // and pass back the normalized version as an additional return parameter);
  1692. // this is just a final sanity check:
  1693. if len(certfp) != 64 {
  1694. return utils.ErrInvalidCertfp
  1695. }
  1696. for _, current := range ac.Certfps {
  1697. if certfp == current {
  1698. return errNoop
  1699. }
  1700. }
  1701. if maxCertfpsPerAccount <= len(ac.Certfps) {
  1702. return errLimitExceeded
  1703. }
  1704. ac.Certfps = append(ac.Certfps, certfp)
  1705. return nil
  1706. }
  1707. func (ac *AccountCredentials) RemoveCertfp(certfp string) (err error) {
  1708. found := false
  1709. newList := make([]string, 0, len(ac.Certfps))
  1710. for _, current := range ac.Certfps {
  1711. if current == certfp {
  1712. found = true
  1713. } else {
  1714. newList = append(newList, current)
  1715. }
  1716. }
  1717. if !found {
  1718. // this is important because it prevents you from deleting someone else's
  1719. // fingerprint record
  1720. return errNoop
  1721. }
  1722. ac.Certfps = newList
  1723. return nil
  1724. }
  1725. type MulticlientAllowedSetting int
  1726. const (
  1727. MulticlientAllowedServerDefault MulticlientAllowedSetting = iota
  1728. MulticlientDisallowedByUser
  1729. MulticlientAllowedByUser
  1730. )
  1731. // controls whether/when clients without event-playback support see fake
  1732. // PRIVMSGs for JOINs
  1733. type ReplayJoinsSetting uint
  1734. const (
  1735. ReplayJoinsCommandsOnly = iota // replay in HISTORY or CHATHISTORY output
  1736. ReplayJoinsAlways // replay in HISTORY, CHATHISTORY, or autoreplay
  1737. ReplayJoinsNever // never replay
  1738. )
  1739. func replayJoinsSettingFromString(str string) (result ReplayJoinsSetting, err error) {
  1740. switch strings.ToLower(str) {
  1741. case "commands-only":
  1742. result = ReplayJoinsCommandsOnly
  1743. case "always":
  1744. result = ReplayJoinsAlways
  1745. case "never":
  1746. result = ReplayJoinsNever
  1747. default:
  1748. err = errInvalidParams
  1749. }
  1750. return
  1751. }
  1752. // XXX: AllowBouncer cannot be renamed AllowMulticlient because it is stored in
  1753. // persistent JSON blobs in the database
  1754. type AccountSettings struct {
  1755. AutoreplayLines *int
  1756. NickEnforcement NickEnforcementMethod
  1757. AllowBouncer MulticlientAllowedSetting
  1758. ReplayJoins ReplayJoinsSetting
  1759. AlwaysOn PersistentStatus
  1760. AutoreplayMissed bool
  1761. DMHistory HistoryStatus
  1762. AutoAway PersistentStatus
  1763. }
  1764. // ClientAccount represents a user account.
  1765. type ClientAccount struct {
  1766. // Name of the account.
  1767. Name string
  1768. NameCasefolded string
  1769. RegisteredAt time.Time
  1770. Credentials AccountCredentials
  1771. Verified bool
  1772. AdditionalNicks []string
  1773. VHost VHostInfo
  1774. Settings AccountSettings
  1775. }
  1776. // convenience for passing around raw serialized account data
  1777. type rawClientAccount struct {
  1778. Name string
  1779. RegisteredAt string
  1780. Credentials string
  1781. Callback string
  1782. Verified bool
  1783. AdditionalNicks string
  1784. VHost string
  1785. Settings string
  1786. }