2016-06-15 13:50:56 +02:00
|
|
|
// Copyright (c) 2012-2014 Jeremy Latt
|
2017-03-27 14:15:02 +02:00
|
|
|
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
|
2016-06-15 13:50:56 +02:00
|
|
|
// released under the MIT license
|
|
|
|
|
2014-03-06 22:10:54 +01:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
2014-03-07 01:51:33 +01:00
|
|
|
"regexp"
|
2014-03-06 22:10:54 +01:00
|
|
|
"strings"
|
2019-10-10 10:17:44 +02:00
|
|
|
"time"
|
2016-09-17 13:23:04 +02:00
|
|
|
|
2017-06-15 18:14:19 +02:00
|
|
|
"github.com/goshuirc/irc-go/ircmatch"
|
2019-04-12 06:08:46 +02:00
|
|
|
|
2017-09-29 04:07:52 +02:00
|
|
|
"github.com/oragono/oragono/irc/caps"
|
2019-04-12 06:08:46 +02:00
|
|
|
"github.com/oragono/oragono/irc/modes"
|
2016-11-15 18:05:33 +01:00
|
|
|
|
2017-06-15 18:14:19 +02:00
|
|
|
"sync"
|
2014-03-06 22:10:54 +01:00
|
|
|
)
|
|
|
|
|
2017-11-22 10:41:11 +01:00
|
|
|
// ClientManager keeps track of clients by nick, enforcing uniqueness of casefolded nicks
|
|
|
|
type ClientManager struct {
|
|
|
|
sync.RWMutex // tier 2
|
2017-11-22 22:35:38 +01:00
|
|
|
byNick map[string]*Client
|
2019-01-31 00:59:49 +01:00
|
|
|
bySkeleton map[string]*Client
|
2014-03-06 22:10:54 +01:00
|
|
|
}
|
|
|
|
|
2019-03-12 00:24:45 +01:00
|
|
|
// Initialize initializes a ClientManager.
|
|
|
|
func (clients *ClientManager) Initialize() {
|
|
|
|
clients.byNick = make(map[string]*Client)
|
|
|
|
clients.bySkeleton = make(map[string]*Client)
|
2014-03-06 22:10:54 +01:00
|
|
|
}
|
|
|
|
|
2017-11-22 10:41:11 +01:00
|
|
|
// Count returns how many clients are in the manager.
|
|
|
|
func (clients *ClientManager) Count() int {
|
|
|
|
clients.RLock()
|
|
|
|
defer clients.RUnlock()
|
|
|
|
count := len(clients.byNick)
|
2016-11-15 18:05:33 +01:00
|
|
|
return count
|
2016-11-06 02:05:29 +01:00
|
|
|
}
|
|
|
|
|
2017-11-22 10:41:11 +01:00
|
|
|
// Get retrieves a client from the manager, if they exist.
|
|
|
|
func (clients *ClientManager) Get(nick string) *Client {
|
2016-10-16 12:14:56 +02:00
|
|
|
casefoldedName, err := CasefoldName(nick)
|
|
|
|
if err == nil {
|
2017-11-22 10:41:11 +01:00
|
|
|
clients.RLock()
|
|
|
|
defer clients.RUnlock()
|
|
|
|
cli := clients.byNick[casefoldedName]
|
2016-11-15 18:05:33 +01:00
|
|
|
return cli
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-26 11:23:27 +01:00
|
|
|
func (clients *ClientManager) removeInternal(client *Client) (err error) {
|
2017-11-22 22:35:38 +01:00
|
|
|
// requires holding the writable Lock()
|
2019-01-31 00:59:49 +01:00
|
|
|
oldcfnick, oldskeleton := client.uniqueIdentifiers()
|
|
|
|
if oldcfnick == "*" || oldcfnick == "" {
|
|
|
|
return errNickMissing
|
|
|
|
}
|
|
|
|
|
2017-11-22 10:41:11 +01: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 17:33:42 +01:00
|
|
|
client.server.logger.Warning("internal", "clients for nick out of sync", oldcfnick)
|
2018-11-26 11:23:27 +01:00
|
|
|
err = errNickMissing
|
2017-11-22 10:41:11 +01:00
|
|
|
}
|
2019-01-31 00:59:49 +01: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 22:10:54 +01:00
|
|
|
}
|
2019-01-31 00:59:49 +01:00
|
|
|
|
2017-11-22 10:41:11 +01:00
|
|
|
return
|
2014-03-06 22:10:54 +01:00
|
|
|
}
|
|
|
|
|
2017-03-25 00:19:13 +01:00
|
|
|
// Remove removes a client from the lookup set.
|
2017-11-22 10:41:11 +01:00
|
|
|
func (clients *ClientManager) Remove(client *Client) error {
|
|
|
|
clients.Lock()
|
|
|
|
defer clients.Unlock()
|
|
|
|
|
2018-11-26 11:23:27 +01:00
|
|
|
return clients.removeInternal(client)
|
|
|
|
}
|
|
|
|
|
2019-05-22 03:40:25 +02:00
|
|
|
// Handles a RESUME by attaching a session to a designated client. It is the
|
|
|
|
// caller's responsibility to verify that the resume is allowed (checking tokens,
|
|
|
|
// TLS status, etc.) before calling this.
|
|
|
|
func (clients *ClientManager) Resume(oldClient *Client, session *Session) (err error) {
|
2018-11-26 11:23:27 +01:00
|
|
|
clients.Lock()
|
|
|
|
defer clients.Unlock()
|
|
|
|
|
2019-05-22 03:40:25 +02:00
|
|
|
cfnick := oldClient.NickCasefolded()
|
|
|
|
if _, ok := clients.byNick[cfnick]; !ok {
|
|
|
|
return errNickMissing
|
2018-11-26 11:23:27 +01:00
|
|
|
}
|
|
|
|
|
2020-02-19 01:38:42 +01:00
|
|
|
success, _, _ := oldClient.AddSession(session)
|
|
|
|
if !success {
|
2019-05-22 03:40:25 +02:00
|
|
|
return errNickMissing
|
|
|
|
}
|
2018-11-26 11:23:27 +01:00
|
|
|
|
2014-03-06 22:10:54 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-22 10:41:11 +01:00
|
|
|
// SetNick sets a client's nickname, validating it against nicknames in use
|
2020-02-19 01:38:42 +01:00
|
|
|
func (clients *ClientManager) SetNick(client *Client, session *Session, newNick string) (setNick string, err error) {
|
2020-02-21 00:19:17 +01:00
|
|
|
config := client.server.Config()
|
2019-05-24 19:09:56 +02:00
|
|
|
|
2020-03-19 10:09:25 +01:00
|
|
|
var newCfNick, newSkeleton string
|
2016-11-16 03:02:22 +01:00
|
|
|
|
2020-02-19 01:38:42 +01:00
|
|
|
client.stateMutex.RLock()
|
|
|
|
account := client.account
|
|
|
|
accountName := client.accountName
|
|
|
|
settings := client.accountSettings
|
|
|
|
registered := client.registered
|
|
|
|
realname := client.realname
|
|
|
|
client.stateMutex.RUnlock()
|
|
|
|
|
2020-03-16 12:54:50 +01: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-17 04:25:50 +01:00
|
|
|
useAccountName = alwaysOn || config.Accounts.NickReservation.ForceNickEqualsAccount
|
2020-03-16 12:54:50 +01:00
|
|
|
}
|
2020-02-19 01:38:42 +01:00
|
|
|
|
2020-03-16 12:54:50 +01:00
|
|
|
if useAccountName {
|
|
|
|
if registered && newNick != accountName && newNick != "" {
|
|
|
|
return "", errNickAccountMismatch
|
|
|
|
}
|
|
|
|
newNick = accountName
|
2020-03-19 10:09:25 +01:00
|
|
|
newCfNick = account
|
2020-03-16 12:54:50 +01:00
|
|
|
newSkeleton, err = Skeleton(newNick)
|
|
|
|
if err != nil {
|
|
|
|
return "", errNicknameInvalid
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
newNick = strings.TrimSpace(newNick)
|
|
|
|
if len(newNick) == 0 {
|
|
|
|
return "", errNickMissing
|
|
|
|
}
|
|
|
|
|
2020-03-17 04:25:50 +01:00
|
|
|
if account == "" && config.Accounts.NickReservation.ForceGuestFormat {
|
2020-03-19 10:09:25 +01:00
|
|
|
newCfNick, err = CasefoldName(newNick)
|
2020-03-19 10:08:53 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", errNicknameInvalid
|
|
|
|
}
|
2020-03-19 10:09:25 +01:00
|
|
|
if !config.Accounts.NickReservation.guestRegexpFolded.MatchString(newCfNick) {
|
2020-03-19 10:08:53 +01:00
|
|
|
newNick = strings.Replace(config.Accounts.NickReservation.GuestFormat, "*", newNick, 1)
|
2020-03-19 10:09:25 +01:00
|
|
|
newCfNick = "" // re-fold it below
|
2020-03-19 10:08:53 +01:00
|
|
|
}
|
2020-03-16 12:54:50 +01:00
|
|
|
}
|
|
|
|
|
2020-03-19 10:09:25 +01:00
|
|
|
if newCfNick == "" {
|
|
|
|
newCfNick, err = CasefoldName(newNick)
|
2020-03-19 10:08:53 +01:00
|
|
|
}
|
2020-03-16 12:54:50 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", errNicknameInvalid
|
|
|
|
}
|
2020-03-19 10:09:25 +01:00
|
|
|
if len(newNick) > config.Limits.NickLen || len(newCfNick) > config.Limits.NickLen {
|
2020-03-16 12:54:50 +01:00
|
|
|
return "", errNicknameInvalid
|
|
|
|
}
|
|
|
|
newSkeleton, err = Skeleton(newNick)
|
|
|
|
if err != nil {
|
|
|
|
return "", errNicknameInvalid
|
|
|
|
}
|
|
|
|
|
2020-03-19 10:09:25 +01:00
|
|
|
if restrictedCasefoldedNicks[newCfNick] || restrictedSkeletons[newSkeleton] {
|
2020-03-16 12:54:50 +01:00
|
|
|
return "", errNicknameInvalid
|
|
|
|
}
|
|
|
|
|
2020-03-19 10:09:25 +01:00
|
|
|
reservedAccount, method := client.server.accounts.EnforcementStatus(newCfNick, newSkeleton)
|
2020-03-16 12:54:50 +01:00
|
|
|
if method == NickEnforcementStrict && reservedAccount != "" && reservedAccount != account {
|
|
|
|
return "", errNicknameReserved
|
|
|
|
}
|
2020-02-19 01:38:42 +01:00
|
|
|
}
|
|
|
|
|
2019-05-19 10:27:44 +02:00
|
|
|
var bouncerAllowed bool
|
2020-02-21 05:55:42 +01:00
|
|
|
if config.Accounts.Multiclient.Enabled {
|
2020-03-16 12:54:50 +01:00
|
|
|
if useAccountName {
|
2020-02-19 01:38:42 +01:00
|
|
|
bouncerAllowed = true
|
2019-05-19 10:27:44 +02:00
|
|
|
} else {
|
2020-02-21 05:55:42 +01:00
|
|
|
if config.Accounts.Multiclient.AllowedByDefault && settings.AllowBouncer != MulticlientDisallowedByUser {
|
2019-05-19 10:27:44 +02:00
|
|
|
bouncerAllowed = true
|
2020-02-21 05:55:42 +01:00
|
|
|
} else if settings.AllowBouncer == MulticlientAllowedByUser {
|
2019-05-19 10:27:44 +02:00
|
|
|
bouncerAllowed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-11 11:30:40 +01:00
|
|
|
|
2017-11-22 10:41:11 +01:00
|
|
|
clients.Lock()
|
|
|
|
defer clients.Unlock()
|
2016-11-16 03:02:22 +01:00
|
|
|
|
2020-03-19 10:09:25 +01:00
|
|
|
currentClient := clients.byNick[newCfNick]
|
2017-11-22 10:41:11 +01:00
|
|
|
// the client may just be changing case
|
2019-05-09 11:03:21 +02:00
|
|
|
if currentClient != nil && currentClient != client && session != nil {
|
2019-04-12 06:08:46 +02:00
|
|
|
// these conditions forbid reattaching to an existing session:
|
2020-04-23 07:38:12 +02:00
|
|
|
if registered || !bouncerAllowed || account == "" || account != currentClient.Account() {
|
2020-02-19 01:38:42 +01:00
|
|
|
return "", errNicknameInUse
|
2019-04-12 06:08:46 +02:00
|
|
|
}
|
2020-04-23 07:38:12 +02: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
|
|
|
|
return "", errInsecureReattach
|
|
|
|
} else {
|
|
|
|
return "", errNicknameInUse
|
|
|
|
}
|
|
|
|
}
|
2020-02-27 08:13:31 +01:00
|
|
|
reattachSuccessful, numSessions, lastSeen := currentClient.AddSession(session)
|
2020-02-19 01:38:42 +01:00
|
|
|
if !reattachSuccessful {
|
|
|
|
return "", errNicknameInUse
|
2019-04-12 06:08:46 +02:00
|
|
|
}
|
2020-02-19 01:38:42 +01:00
|
|
|
if numSessions == 1 {
|
2020-03-06 10:21:21 +01:00
|
|
|
invisible := currentClient.HasMode(modes.Invisible)
|
|
|
|
operator := currentClient.HasMode(modes.Operator) || currentClient.HasMode(modes.LocalOperator)
|
2020-02-19 01:38:42 +01:00
|
|
|
client.server.stats.AddRegistered(invisible, operator)
|
|
|
|
}
|
2020-02-27 08:13:31 +01:00
|
|
|
session.autoreplayMissedSince = lastSeen
|
2020-02-19 01:38:42 +01:00
|
|
|
// XXX SetNames only changes names if they are unset, so the realname change only
|
|
|
|
// takes effect on first attach to an always-on client (good), but the user/ident
|
|
|
|
// change is always a no-op (bad). we could make user/ident act the same way as
|
|
|
|
// realname, but then we'd have to send CHGHOST and i don't want to deal with that
|
|
|
|
// for performance reasons
|
|
|
|
currentClient.SetNames("user", realname, true)
|
2019-05-09 00:14:49 +02:00
|
|
|
// successful reattach!
|
2020-02-19 01:38:42 +01:00
|
|
|
return newNick, nil
|
2016-11-16 03:02:22 +01:00
|
|
|
}
|
2019-01-31 00:59:49 +01:00
|
|
|
// analogous checks for skeletons
|
|
|
|
skeletonHolder := clients.bySkeleton[newSkeleton]
|
|
|
|
if skeletonHolder != nil && skeletonHolder != client {
|
2020-02-19 01:38:42 +01:00
|
|
|
return "", errNicknameInUse
|
2019-01-31 00:59:49 +01:00
|
|
|
}
|
2020-03-16 12:54:50 +01:00
|
|
|
|
2019-01-02 03:16:29 +01:00
|
|
|
clients.removeInternal(client)
|
2020-03-19 10:09:25 +01:00
|
|
|
clients.byNick[newCfNick] = client
|
2019-01-31 00:59:49 +01:00
|
|
|
clients.bySkeleton[newSkeleton] = client
|
2020-03-19 10:09:25 +01:00
|
|
|
client.updateNick(newNick, newCfNick, newSkeleton)
|
2020-02-19 01:38:42 +01:00
|
|
|
return newNick, nil
|
2017-11-22 10:41:11 +01:00
|
|
|
}
|
2016-11-16 03:02:22 +01:00
|
|
|
|
2017-11-22 10:41:11 +01:00
|
|
|
func (clients *ClientManager) AllClients() (result []*Client) {
|
|
|
|
clients.RLock()
|
|
|
|
defer clients.RUnlock()
|
|
|
|
result = make([]*Client, len(clients.byNick))
|
|
|
|
i := 0
|
2017-11-22 22:35:38 +01:00
|
|
|
for _, client := range clients.byNick {
|
2017-11-22 10:41:11 +01:00
|
|
|
result[i] = client
|
|
|
|
i++
|
2016-11-16 03:02:22 +01:00
|
|
|
}
|
2017-11-22 10:41:11 +01:00
|
|
|
return
|
2016-11-16 03:02:22 +01:00
|
|
|
}
|
|
|
|
|
2017-03-25 00:19:13 +01:00
|
|
|
// AllWithCaps returns all clients with the given capabilities.
|
2019-04-12 06:08:46 +02:00
|
|
|
func (clients *ClientManager) AllWithCaps(capabs ...caps.Capability) (sessions []*Session) {
|
2017-11-22 10:41:11 +01:00
|
|
|
clients.RLock()
|
|
|
|
defer clients.RUnlock()
|
2019-04-12 06:08:46 +02:00
|
|
|
for _, client := range clients.byNick {
|
|
|
|
for _, session := range client.Sessions() {
|
|
|
|
if session.capabilities.HasAll(capabs...) {
|
|
|
|
sessions = append(sessions, session)
|
2016-10-22 14:18:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 06:08:46 +02:00
|
|
|
return
|
2016-10-22 14:18:41 +02:00
|
|
|
}
|
|
|
|
|
2019-04-27 17:50:43 +02: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 07:57:42 +02:00
|
|
|
capabs = append(capabs, caps.CapNotify)
|
2019-04-27 17:50:43 +02:00
|
|
|
clients.RLock()
|
|
|
|
defer clients.RUnlock()
|
|
|
|
for _, client := range clients.byNick {
|
|
|
|
for _, session := range client.Sessions() {
|
2019-04-28 07:57:42 +02:00
|
|
|
// cap-notify is implicit in cap version 302 and above
|
2019-04-27 17:50:43 +02:00
|
|
|
if session.capabilities.HasAll(capabs...) || 302 <= session.capVersion {
|
|
|
|
sessions = append(sessions, session)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-25 00:19:13 +01:00
|
|
|
// FindAll returns all clients that match the given userhost mask.
|
2017-11-22 10:41:11 +01:00
|
|
|
func (clients *ClientManager) FindAll(userhost string) (set ClientSet) {
|
2014-03-06 22:10:54 +01:00
|
|
|
set = make(ClientSet)
|
2016-09-17 13:23:04 +02:00
|
|
|
|
2019-07-14 22:17:37 +02:00
|
|
|
userhost, err := CanonicalizeMaskWildcard(userhost)
|
2016-10-11 15:51:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return set
|
|
|
|
}
|
|
|
|
matcher := ircmatch.MakeMatch(userhost)
|
2016-09-17 13:23:04 +02:00
|
|
|
|
2017-11-22 10:41:11 +01:00
|
|
|
clients.RLock()
|
|
|
|
defer clients.RUnlock()
|
|
|
|
for _, client := range clients.byNick {
|
2018-12-30 12:45:23 +01:00
|
|
|
if matcher.Match(client.NickMaskCasefolded()) {
|
2016-09-17 13:23:04 +02:00
|
|
|
set.Add(client)
|
2014-03-06 22:10:54 +01:00
|
|
|
}
|
|
|
|
}
|
2016-09-17 13:23:04 +02:00
|
|
|
|
|
|
|
return set
|
2014-03-06 22:10:54 +01:00
|
|
|
}
|
|
|
|
|
2014-03-08 02:09:49 +01:00
|
|
|
//
|
|
|
|
// usermask to regexp
|
|
|
|
//
|
|
|
|
|
2016-09-17 13:23:04 +02:00
|
|
|
//TODO(dan): move this over to generally using glob syntax instead?
|
|
|
|
// kinda more expected in normal ban/etc masks, though regex is useful (probably as an extban?)
|
2017-10-05 15:29:34 +02:00
|
|
|
|
2019-10-10 10:17:44 +02:00
|
|
|
type MaskInfo struct {
|
|
|
|
TimeCreated time.Time
|
|
|
|
CreatorNickmask string
|
|
|
|
CreatorAccount string
|
|
|
|
}
|
|
|
|
|
2017-10-05 15:29:34 +02:00
|
|
|
// UserMaskSet holds a set of client masks and lets you match hostnames to them.
|
2014-03-08 02:09:49 +01:00
|
|
|
type UserMaskSet struct {
|
2017-10-23 01:50:16 +02:00
|
|
|
sync.RWMutex
|
2019-10-10 10:17:44 +02:00
|
|
|
masks map[string]MaskInfo
|
2014-03-08 02:09:49 +01:00
|
|
|
regexp *regexp.Regexp
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUserMaskSet() *UserMaskSet {
|
2019-10-10 10:17:44 +02:00
|
|
|
return new(UserMaskSet)
|
2014-03-08 02:09:49 +01:00
|
|
|
}
|
|
|
|
|
2017-10-05 15:29:34 +02:00
|
|
|
// Add adds the given mask to this set.
|
2019-10-13 12:07:30 +02:00
|
|
|
func (set *UserMaskSet) Add(mask, creatorNickmask, creatorAccount string) (maskAdded string, err error) {
|
2019-10-10 10:17:44 +02:00
|
|
|
casefoldedMask, err := CanonicalizeMaskWildcard(mask)
|
2016-10-11 15:51:46 +02:00
|
|
|
if err != nil {
|
2019-10-13 12:07:30 +02:00
|
|
|
return
|
2016-10-11 15:51:46 +02:00
|
|
|
}
|
2017-10-23 01:50:16 +02:00
|
|
|
|
|
|
|
set.Lock()
|
2019-10-10 10:17:44 +02:00
|
|
|
if set.masks == nil {
|
|
|
|
set.masks = make(map[string]MaskInfo)
|
2017-11-13 09:12:17 +01:00
|
|
|
}
|
2019-10-10 10:17:44 +02:00
|
|
|
_, present := set.masks[casefoldedMask]
|
2019-10-13 12:07:30 +02:00
|
|
|
if !present {
|
|
|
|
maskAdded = casefoldedMask
|
2019-10-10 10:17:44 +02:00
|
|
|
set.masks[casefoldedMask] = MaskInfo{
|
|
|
|
TimeCreated: time.Now().UTC(),
|
|
|
|
CreatorNickmask: creatorNickmask,
|
|
|
|
CreatorAccount: creatorAccount,
|
2014-03-09 04:22:04 +01:00
|
|
|
}
|
|
|
|
}
|
2019-10-10 10:17:44 +02:00
|
|
|
set.Unlock()
|
|
|
|
|
2019-10-13 12:07:30 +02:00
|
|
|
if !present {
|
2017-11-13 09:12:17 +01:00
|
|
|
set.setRegexp()
|
|
|
|
}
|
2014-03-09 04:22:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-05 15:29:34 +02:00
|
|
|
// Remove removes the given mask from this set.
|
2019-10-13 12:07:30 +02:00
|
|
|
func (set *UserMaskSet) Remove(mask string) (maskRemoved string, err error) {
|
|
|
|
mask, err = CanonicalizeMaskWildcard(mask)
|
2019-10-10 10:17:44 +02:00
|
|
|
if err != nil {
|
2019-10-13 12:07:30 +02:00
|
|
|
return
|
2019-10-10 10:17:44 +02:00
|
|
|
}
|
|
|
|
|
2017-10-23 01:50:16 +02:00
|
|
|
set.Lock()
|
2019-10-13 12:07:30 +02:00
|
|
|
_, removed := set.masks[mask]
|
2017-11-13 09:12:17 +01:00
|
|
|
if removed {
|
2019-10-13 12:07:30 +02:00
|
|
|
maskRemoved = mask
|
2017-11-13 09:12:17 +01:00
|
|
|
delete(set.masks, mask)
|
|
|
|
}
|
2017-10-23 01:50:16 +02:00
|
|
|
set.Unlock()
|
|
|
|
|
2017-11-13 09:12:17 +01:00
|
|
|
if removed {
|
2017-10-23 01:50:16 +02:00
|
|
|
set.setRegexp()
|
2014-03-08 02:35:58 +01:00
|
|
|
}
|
2017-11-13 09:12:17 +01:00
|
|
|
return
|
2014-03-08 02:09:49 +01:00
|
|
|
}
|
|
|
|
|
2019-10-10 10:17:44 +02:00
|
|
|
func (set *UserMaskSet) SetMasks(masks map[string]MaskInfo) {
|
|
|
|
set.Lock()
|
|
|
|
set.masks = masks
|
|
|
|
set.Unlock()
|
|
|
|
set.setRegexp()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (set *UserMaskSet) Masks() (result map[string]MaskInfo) {
|
|
|
|
set.RLock()
|
|
|
|
defer set.RUnlock()
|
|
|
|
|
|
|
|
result = make(map[string]MaskInfo, len(set.masks))
|
|
|
|
for mask, info := range set.masks {
|
|
|
|
result[mask] = info
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-05 15:29:34 +02:00
|
|
|
// Match matches the given n!u@h.
|
2016-10-11 15:51:46 +02:00
|
|
|
func (set *UserMaskSet) Match(userhost string) bool {
|
2017-10-23 01:50:16 +02:00
|
|
|
set.RLock()
|
|
|
|
regexp := set.regexp
|
|
|
|
set.RUnlock()
|
|
|
|
|
|
|
|
if regexp == nil {
|
2014-03-08 02:09:49 +01:00
|
|
|
return false
|
|
|
|
}
|
2017-10-23 01:50:16 +02:00
|
|
|
return regexp.MatchString(userhost)
|
2014-03-08 02:09:49 +01:00
|
|
|
}
|
|
|
|
|
2017-10-23 01:50:16 +02:00
|
|
|
func (set *UserMaskSet) Length() int {
|
|
|
|
set.RLock()
|
|
|
|
defer set.RUnlock()
|
|
|
|
return len(set.masks)
|
|
|
|
}
|
|
|
|
|
2017-10-05 15:29:34 +02:00
|
|
|
// setRegexp generates a regular expression from the set of user mask
|
2014-03-09 04:22:04 +01:00
|
|
|
// strings. Masks are split at the two types of wildcards, `*` and
|
|
|
|
// `?`. All the pieces are meta-escaped. `*` is replaced with `.*`,
|
|
|
|
// the regexp equivalent. Likewise, `?` is replaced with `.`. The
|
|
|
|
// parts are re-joined and finally all masks are joined into a big
|
|
|
|
// or-expression.
|
2014-03-08 02:09:49 +01:00
|
|
|
func (set *UserMaskSet) setRegexp() {
|
2017-10-23 01:50:16 +02:00
|
|
|
var re *regexp.Regexp
|
2014-03-08 02:09:49 +01:00
|
|
|
|
2017-10-23 01:50:16 +02:00
|
|
|
set.RLock()
|
2014-03-08 02:09:49 +01:00
|
|
|
maskExprs := make([]string, len(set.masks))
|
|
|
|
index := 0
|
|
|
|
for mask := range set.masks {
|
2016-10-11 15:51:46 +02:00
|
|
|
manyParts := strings.Split(mask, "*")
|
2014-03-08 02:09:49 +01:00
|
|
|
manyExprs := make([]string, len(manyParts))
|
|
|
|
for mindex, manyPart := range manyParts {
|
|
|
|
oneParts := strings.Split(manyPart, "?")
|
|
|
|
oneExprs := make([]string, len(oneParts))
|
|
|
|
for oindex, onePart := range oneParts {
|
|
|
|
oneExprs[oindex] = regexp.QuoteMeta(onePart)
|
|
|
|
}
|
|
|
|
manyExprs[mindex] = strings.Join(oneExprs, ".")
|
|
|
|
}
|
|
|
|
maskExprs[index] = strings.Join(manyExprs, ".*")
|
2017-10-23 01:50:16 +02:00
|
|
|
index++
|
2014-03-08 02:09:49 +01:00
|
|
|
}
|
2017-10-23 01:50:16 +02:00
|
|
|
set.RUnlock()
|
|
|
|
|
|
|
|
if index > 0 {
|
2020-05-06 07:07:06 +02:00
|
|
|
expr := "^(" + strings.Join(maskExprs, "|") + ")$"
|
2017-10-23 01:50:16 +02:00
|
|
|
re, _ = regexp.Compile(expr)
|
|
|
|
}
|
|
|
|
|
|
|
|
set.Lock()
|
|
|
|
set.regexp = re
|
|
|
|
set.Unlock()
|
2014-03-08 02:09:49 +01:00
|
|
|
}
|