2016-06-15 21:50:56 +10:00
|
|
|
// Copyright (c) 2012-2014 Jeremy Latt
|
2017-03-27 22:15:02 +10:00
|
|
|
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
|
2016-06-15 21:50:56 +10:00
|
|
|
// released under the MIT license
|
|
|
|
|
2014-03-06 13:10:54 -08:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2020-05-13 03:41:00 -04:00
|
|
|
"sync"
|
2016-09-17 21:23:04 +10:00
|
|
|
|
2021-05-25 00:34:38 -04:00
|
|
|
"github.com/ergochat/ergo/irc/caps"
|
|
|
|
"github.com/ergochat/ergo/irc/modes"
|
|
|
|
"github.com/ergochat/ergo/irc/utils"
|
2014-03-06 13:10:54 -08:00
|
|
|
)
|
|
|
|
|
2017-11-22 04:41:11 -05:00
|
|
|
// ClientManager keeps track of clients by nick, enforcing uniqueness of casefolded nicks
|
|
|
|
type ClientManager struct {
|
|
|
|
sync.RWMutex // tier 2
|
2017-11-22 16:35:38 -05:00
|
|
|
byNick map[string]*Client
|
2019-01-30 18:59:49 -05:00
|
|
|
bySkeleton map[string]*Client
|
2014-03-06 13:10:54 -08:00
|
|
|
}
|
|
|
|
|
2019-03-11 19:24:45 -04:00
|
|
|
// Initialize initializes a ClientManager.
|
|
|
|
func (clients *ClientManager) Initialize() {
|
|
|
|
clients.byNick = make(map[string]*Client)
|
|
|
|
clients.bySkeleton = make(map[string]*Client)
|
2014-03-06 13:10:54 -08:00
|
|
|
}
|
|
|
|
|
2017-11-22 04:41:11 -05:00
|
|
|
// Get retrieves a client from the manager, if they exist.
|
|
|
|
func (clients *ClientManager) Get(nick string) *Client {
|
2016-10-16 20:14:56 +10:00
|
|
|
casefoldedName, err := CasefoldName(nick)
|
|
|
|
if err == nil {
|
2017-11-22 04:41:11 -05:00
|
|
|
clients.RLock()
|
|
|
|
defer clients.RUnlock()
|
|
|
|
cli := clients.byNick[casefoldedName]
|
2016-11-16 03:05:33 +10:00
|
|
|
return cli
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-29 23:40:49 -04:00
|
|
|
func (clients *ClientManager) removeInternal(client *Client, oldcfnick, oldskeleton string) (err error) {
|
2017-11-22 16:35:38 -05:00
|
|
|
// requires holding the writable Lock()
|
2019-01-30 18:59:49 -05:00
|
|
|
if oldcfnick == "*" || oldcfnick == "" {
|
|
|
|
return errNickMissing
|
|
|
|
}
|
|
|
|
|
2017-11-22 04:41:11 -05:00
|
|
|
currentEntry, present := clients.byNick[oldcfnick]
|
|
|
|
if present {
|
|
|
|
if currentEntry == client {
|
|
|
|
delete(clients.byNick, oldcfnick)
|
|
|
|
} else {
|
|
|
|
// this shouldn't happen, but we can ignore it
|
2018-12-31 11:33:42 -05:00
|
|
|
client.server.logger.Warning("internal", "clients for nick out of sync", oldcfnick)
|
2018-11-26 05:23:27 -05:00
|
|
|
err = errNickMissing
|
2017-11-22 04:41:11 -05:00
|
|
|
}
|
2019-01-30 18:59:49 -05:00
|
|
|
} else {
|
|
|
|
err = errNickMissing
|
|
|
|
}
|
|
|
|
|
|
|
|
currentEntry, present = clients.bySkeleton[oldskeleton]
|
|
|
|
if present {
|
|
|
|
if currentEntry == client {
|
|
|
|
delete(clients.bySkeleton, oldskeleton)
|
|
|
|
} else {
|
|
|
|
client.server.logger.Warning("internal", "clients for skeleton out of sync", oldskeleton)
|
|
|
|
err = errNickMissing
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = errNickMissing
|
2014-03-06 13:10:54 -08:00
|
|
|
}
|
2019-01-30 18:59:49 -05:00
|
|
|
|
2017-11-22 04:41:11 -05:00
|
|
|
return
|
2014-03-06 13:10:54 -08:00
|
|
|
}
|
|
|
|
|
2017-03-25 09:19:13 +10:00
|
|
|
// Remove removes a client from the lookup set.
|
2017-11-22 04:41:11 -05:00
|
|
|
func (clients *ClientManager) Remove(client *Client) error {
|
|
|
|
clients.Lock()
|
|
|
|
defer clients.Unlock()
|
|
|
|
|
2020-08-29 23:40:49 -04:00
|
|
|
oldcfnick, oldskeleton := client.uniqueIdentifiers()
|
|
|
|
return clients.removeInternal(client, oldcfnick, oldskeleton)
|
2018-11-26 05:23:27 -05:00
|
|
|
}
|
|
|
|
|
2017-11-22 04:41:11 -05:00
|
|
|
// SetNick sets a client's nickname, validating it against nicknames in use
|
2020-10-06 18:04:29 -04:00
|
|
|
// XXX: dryRun validates a client's ability to claim a nick, without
|
|
|
|
// actually claiming it
|
2023-02-04 21:50:14 -08:00
|
|
|
func (clients *ClientManager) SetNick(client *Client, session *Session, newNick string, dryRun bool) (setNick string, err error, awayChanged bool) {
|
2020-02-20 18:19:17 -05:00
|
|
|
config := client.server.Config()
|
2019-05-24 13:09:56 -04:00
|
|
|
|
2020-03-19 05:09:25 -04:00
|
|
|
var newCfNick, newSkeleton string
|
2016-11-16 12:02:22 +10:00
|
|
|
|
2020-02-18 19:38:42 -05:00
|
|
|
client.stateMutex.RLock()
|
|
|
|
account := client.account
|
|
|
|
accountName := client.accountName
|
|
|
|
settings := client.accountSettings
|
|
|
|
registered := client.registered
|
|
|
|
realname := client.realname
|
|
|
|
client.stateMutex.RUnlock()
|
|
|
|
|
2021-12-06 14:17:45 -05:00
|
|
|
// these restrictions have grandfather exceptions for nicknames registered
|
|
|
|
// on previous versions of Ergo:
|
|
|
|
if newNick != accountName {
|
|
|
|
// can't contain "disfavored" characters like <, or start with a $ because
|
2022-01-19 04:14:00 -05:00
|
|
|
// it collides with the massmessage mask syntax. '0' conflicts with the use of 0
|
|
|
|
// as a placeholder in WHOX (#1896):
|
|
|
|
if strings.ContainsAny(newNick, disfavoredNameCharacters) || strings.HasPrefix(newNick, "$") ||
|
|
|
|
newNick == "0" {
|
2021-12-06 14:17:45 -05:00
|
|
|
return "", errNicknameInvalid, false
|
|
|
|
}
|
2020-12-07 03:51:52 -05:00
|
|
|
}
|
|
|
|
|
2020-03-16 07:54:50 -04:00
|
|
|
// recompute always-on status, because client.alwaysOn is not set for unregistered clients
|
|
|
|
var alwaysOn, useAccountName bool
|
|
|
|
if account != "" {
|
|
|
|
alwaysOn = persistenceEnabled(config.Accounts.Multiclient.AlwaysOn, settings.AlwaysOn)
|
2020-03-16 23:25:50 -04:00
|
|
|
useAccountName = alwaysOn || config.Accounts.NickReservation.ForceNickEqualsAccount
|
2020-03-16 07:54:50 -04:00
|
|
|
}
|
2020-02-18 19:38:42 -05:00
|
|
|
|
2020-03-16 07:54:50 -04:00
|
|
|
if useAccountName {
|
2022-04-24 00:31:20 -04:00
|
|
|
if registered && newNick != accountName {
|
2020-05-19 14:12:20 -04:00
|
|
|
return "", errNickAccountMismatch, false
|
2020-03-16 07:54:50 -04:00
|
|
|
}
|
|
|
|
newNick = accountName
|
2020-03-19 05:09:25 -04:00
|
|
|
newCfNick = account
|
2020-03-16 07:54:50 -04:00
|
|
|
newSkeleton, err = Skeleton(newNick)
|
|
|
|
if err != nil {
|
2020-05-19 14:12:20 -04:00
|
|
|
return "", errNicknameInvalid, false
|
2020-03-16 07:54:50 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
newNick = strings.TrimSpace(newNick)
|
|
|
|
if len(newNick) == 0 {
|
2020-05-19 14:12:20 -04:00
|
|
|
return "", errNickMissing, false
|
2020-03-16 07:54:50 -04:00
|
|
|
}
|
|
|
|
|
2020-10-07 08:54:46 -04:00
|
|
|
if account == "" && config.Accounts.NickReservation.ForceGuestFormat && !dryRun {
|
2020-03-19 05:09:25 -04:00
|
|
|
newCfNick, err = CasefoldName(newNick)
|
2020-03-19 05:08:53 -04:00
|
|
|
if err != nil {
|
2020-05-19 14:12:20 -04:00
|
|
|
return "", errNicknameInvalid, false
|
2020-03-19 05:08:53 -04:00
|
|
|
}
|
2020-03-19 05:09:25 -04:00
|
|
|
if !config.Accounts.NickReservation.guestRegexpFolded.MatchString(newCfNick) {
|
2020-03-19 05:08:53 -04:00
|
|
|
newNick = strings.Replace(config.Accounts.NickReservation.GuestFormat, "*", newNick, 1)
|
2020-03-19 05:09:25 -04:00
|
|
|
newCfNick = "" // re-fold it below
|
2020-03-19 05:08:53 -04:00
|
|
|
}
|
2020-03-16 07:54:50 -04:00
|
|
|
}
|
|
|
|
|
2020-03-19 05:09:25 -04:00
|
|
|
if newCfNick == "" {
|
|
|
|
newCfNick, err = CasefoldName(newNick)
|
2020-03-19 05:08:53 -04:00
|
|
|
}
|
2020-03-16 07:54:50 -04:00
|
|
|
if err != nil {
|
2020-05-19 14:12:20 -04:00
|
|
|
return "", errNicknameInvalid, false
|
2020-03-16 07:54:50 -04:00
|
|
|
}
|
2020-03-19 05:09:25 -04:00
|
|
|
if len(newNick) > config.Limits.NickLen || len(newCfNick) > config.Limits.NickLen {
|
2020-05-19 14:12:20 -04:00
|
|
|
return "", errNicknameInvalid, false
|
2020-03-16 07:54:50 -04:00
|
|
|
}
|
|
|
|
newSkeleton, err = Skeleton(newNick)
|
|
|
|
if err != nil {
|
2020-05-19 14:12:20 -04:00
|
|
|
return "", errNicknameInvalid, false
|
2020-03-16 07:54:50 -04:00
|
|
|
}
|
|
|
|
|
2020-09-09 04:01:46 -04:00
|
|
|
if config.isRelaymsgIdentifier(newNick) {
|
|
|
|
return "", errNicknameInvalid, false
|
2020-06-08 10:19:28 +10:00
|
|
|
}
|
|
|
|
|
2020-08-22 22:43:21 -04:00
|
|
|
if restrictedCasefoldedNicks.Has(newCfNick) || restrictedSkeletons.Has(newSkeleton) {
|
2020-05-19 14:12:20 -04:00
|
|
|
return "", errNicknameInvalid, false
|
2020-03-16 07:54:50 -04:00
|
|
|
}
|
|
|
|
|
2020-03-19 05:09:25 -04:00
|
|
|
reservedAccount, method := client.server.accounts.EnforcementStatus(newCfNick, newSkeleton)
|
2020-03-16 07:54:50 -04:00
|
|
|
if method == NickEnforcementStrict && reservedAccount != "" && reservedAccount != account {
|
2020-05-19 14:12:20 -04:00
|
|
|
return "", errNicknameReserved, false
|
2020-03-16 07:54:50 -04:00
|
|
|
}
|
2020-02-18 19:38:42 -05:00
|
|
|
}
|
|
|
|
|
2019-05-19 04:27:44 -04:00
|
|
|
var bouncerAllowed bool
|
2020-02-20 23:55:42 -05:00
|
|
|
if config.Accounts.Multiclient.Enabled {
|
2020-03-16 07:54:50 -04:00
|
|
|
if useAccountName {
|
2020-02-18 19:38:42 -05:00
|
|
|
bouncerAllowed = true
|
2019-05-19 04:27:44 -04:00
|
|
|
} else {
|
2020-02-20 23:55:42 -05:00
|
|
|
if config.Accounts.Multiclient.AllowedByDefault && settings.AllowBouncer != MulticlientDisallowedByUser {
|
2019-05-19 04:27:44 -04:00
|
|
|
bouncerAllowed = true
|
2020-02-20 23:55:42 -05:00
|
|
|
} else if settings.AllowBouncer == MulticlientAllowedByUser {
|
2019-05-19 04:27:44 -04:00
|
|
|
bouncerAllowed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-11 05:30:40 -05:00
|
|
|
|
2017-11-22 04:41:11 -05:00
|
|
|
clients.Lock()
|
|
|
|
defer clients.Unlock()
|
2016-11-16 12:02:22 +10:00
|
|
|
|
2020-03-19 05:09:25 -04:00
|
|
|
currentClient := clients.byNick[newCfNick]
|
2017-11-22 04:41:11 -05:00
|
|
|
// the client may just be changing case
|
2020-10-07 08:54:46 -04:00
|
|
|
if currentClient != nil && currentClient != client {
|
2019-04-12 00:08:46 -04:00
|
|
|
// these conditions forbid reattaching to an existing session:
|
2020-10-07 08:54:46 -04:00
|
|
|
if registered || !bouncerAllowed || account == "" || account != currentClient.Account() ||
|
|
|
|
dryRun || session == nil {
|
2020-05-19 14:12:20 -04:00
|
|
|
return "", errNicknameInUse, false
|
2019-04-12 00:08:46 -04:00
|
|
|
}
|
2020-04-23 01:38:12 -04:00
|
|
|
// check TLS modes
|
|
|
|
if client.HasMode(modes.TLS) != currentClient.HasMode(modes.TLS) {
|
|
|
|
if useAccountName {
|
|
|
|
// #955: this is fatal because they can't fix it by trying a different nick
|
2020-05-19 14:12:20 -04:00
|
|
|
return "", errInsecureReattach, false
|
2020-04-23 01:38:12 -04:00
|
|
|
} else {
|
2020-05-19 14:12:20 -04:00
|
|
|
return "", errNicknameInUse, false
|
2020-04-23 01:38:12 -04:00
|
|
|
}
|
|
|
|
}
|
2023-02-04 21:50:14 -08:00
|
|
|
reattachSuccessful, numSessions, lastSeen, wasAway, nowAway := currentClient.AddSession(session)
|
2020-02-18 19:38:42 -05:00
|
|
|
if !reattachSuccessful {
|
2020-05-19 14:12:20 -04:00
|
|
|
return "", errNicknameInUse, false
|
2019-04-12 00:08:46 -04:00
|
|
|
}
|
2020-02-18 19:38:42 -05:00
|
|
|
if numSessions == 1 {
|
2020-03-06 04:21:21 -05:00
|
|
|
invisible := currentClient.HasMode(modes.Invisible)
|
2021-02-06 22:45:34 -05:00
|
|
|
operator := currentClient.HasMode(modes.Operator)
|
2020-02-18 19:38:42 -05:00
|
|
|
client.server.stats.AddRegistered(invisible, operator)
|
|
|
|
}
|
2020-02-27 02:13:31 -05:00
|
|
|
session.autoreplayMissedSince = lastSeen
|
2020-07-12 16:31:51 -04:00
|
|
|
// TODO: transition mechanism for #1065, clean this up eventually:
|
|
|
|
if currentClient.Realname() == "" {
|
|
|
|
currentClient.SetRealname(realname)
|
|
|
|
}
|
2019-05-08 18:14:49 -04:00
|
|
|
// successful reattach!
|
2023-02-04 21:50:14 -08:00
|
|
|
return newNick, nil, wasAway != nowAway
|
2020-05-17 13:39:37 -04:00
|
|
|
} else if currentClient == client && currentClient.Nick() == newNick {
|
2020-05-19 14:12:20 -04:00
|
|
|
return "", errNoop, false
|
2016-11-16 12:02:22 +10:00
|
|
|
}
|
2019-01-30 18:59:49 -05:00
|
|
|
// analogous checks for skeletons
|
|
|
|
skeletonHolder := clients.bySkeleton[newSkeleton]
|
|
|
|
if skeletonHolder != nil && skeletonHolder != client {
|
2020-05-19 14:12:20 -04:00
|
|
|
return "", errNicknameInUse, false
|
2019-01-30 18:59:49 -05:00
|
|
|
}
|
2020-03-16 07:54:50 -04:00
|
|
|
|
2020-10-06 18:04:29 -04:00
|
|
|
if dryRun {
|
|
|
|
return "", nil, false
|
|
|
|
}
|
|
|
|
|
2020-08-29 23:40:49 -04:00
|
|
|
formercfnick, formerskeleton := client.uniqueIdentifiers()
|
2020-08-06 03:16:58 -04:00
|
|
|
if changeSuccess := client.SetNick(newNick, newCfNick, newSkeleton); !changeSuccess {
|
|
|
|
return "", errClientDestroyed, false
|
|
|
|
}
|
2020-08-29 23:40:49 -04:00
|
|
|
clients.removeInternal(client, formercfnick, formerskeleton)
|
2020-03-19 05:09:25 -04:00
|
|
|
clients.byNick[newCfNick] = client
|
2019-01-30 18:59:49 -05:00
|
|
|
clients.bySkeleton[newSkeleton] = client
|
2020-05-19 14:12:20 -04:00
|
|
|
return newNick, nil, false
|
2017-11-22 04:41:11 -05:00
|
|
|
}
|
2016-11-16 12:02:22 +10:00
|
|
|
|
2017-11-22 04:41:11 -05:00
|
|
|
func (clients *ClientManager) AllClients() (result []*Client) {
|
|
|
|
clients.RLock()
|
|
|
|
defer clients.RUnlock()
|
|
|
|
result = make([]*Client, len(clients.byNick))
|
|
|
|
i := 0
|
2017-11-22 16:35:38 -05:00
|
|
|
for _, client := range clients.byNick {
|
2017-11-22 04:41:11 -05:00
|
|
|
result[i] = client
|
|
|
|
i++
|
2016-11-16 12:02:22 +10:00
|
|
|
}
|
2017-11-22 04:41:11 -05:00
|
|
|
return
|
2016-11-16 12:02:22 +10:00
|
|
|
}
|
|
|
|
|
2019-04-28 01:50:43 +10:00
|
|
|
// AllWithCapsNotify returns all clients with the given capabilities, and that support cap-notify.
|
|
|
|
func (clients *ClientManager) AllWithCapsNotify(capabs ...caps.Capability) (sessions []*Session) {
|
2019-04-28 15:57:42 +10:00
|
|
|
capabs = append(capabs, caps.CapNotify)
|
2019-04-28 01:50:43 +10:00
|
|
|
clients.RLock()
|
|
|
|
defer clients.RUnlock()
|
|
|
|
for _, client := range clients.byNick {
|
|
|
|
for _, session := range client.Sessions() {
|
2019-04-28 15:57:42 +10:00
|
|
|
// cap-notify is implicit in cap version 302 and above
|
2019-04-28 01:50:43 +10:00
|
|
|
if session.capabilities.HasAll(capabs...) || 302 <= session.capVersion {
|
|
|
|
sessions = append(sessions, session)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-25 09:19:13 +10:00
|
|
|
// FindAll returns all clients that match the given userhost mask.
|
2017-11-22 04:41:11 -05:00
|
|
|
func (clients *ClientManager) FindAll(userhost string) (set ClientSet) {
|
2014-03-06 13:10:54 -08:00
|
|
|
set = make(ClientSet)
|
2016-09-17 21:23:04 +10:00
|
|
|
|
2019-07-14 16:17:37 -04:00
|
|
|
userhost, err := CanonicalizeMaskWildcard(userhost)
|
2016-10-11 23:51:46 +10:00
|
|
|
if err != nil {
|
|
|
|
return set
|
|
|
|
}
|
2020-05-13 03:41:00 -04:00
|
|
|
matcher, err := utils.CompileGlob(userhost, false)
|
|
|
|
if err != nil {
|
|
|
|
// not much we can do here
|
|
|
|
return
|
|
|
|
}
|
2016-09-17 21:23:04 +10:00
|
|
|
|
2017-11-22 04:41:11 -05:00
|
|
|
clients.RLock()
|
|
|
|
defer clients.RUnlock()
|
|
|
|
for _, client := range clients.byNick {
|
2020-05-13 03:41:00 -04:00
|
|
|
if matcher.MatchString(client.NickMaskCasefolded()) {
|
2016-09-17 21:23:04 +10:00
|
|
|
set.Add(client)
|
2014-03-06 13:10:54 -08:00
|
|
|
}
|
|
|
|
}
|
2016-09-17 21:23:04 +10:00
|
|
|
|
|
|
|
return set
|
2014-03-06 13:10:54 -08:00
|
|
|
}
|
2021-04-06 00:46:07 -04:00
|
|
|
|
|
|
|
// Determine the canonical / unfolded form of a nick, if a client matching it
|
|
|
|
// is present (or always-on).
|
|
|
|
func (clients *ClientManager) UnfoldNick(cfnick string) (nick string) {
|
|
|
|
clients.RLock()
|
|
|
|
c := clients.byNick[cfnick]
|
|
|
|
clients.RUnlock()
|
|
|
|
if c != nil {
|
|
|
|
return c.Nick()
|
|
|
|
} else {
|
|
|
|
return cfnick
|
|
|
|
}
|
|
|
|
}
|