2017-03-27 14:15:02 +02:00
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
2017-01-11 13:38:16 +01:00
// released under the MIT license
package irc
import (
"encoding/json"
"fmt"
2017-06-11 18:01:39 +02:00
"sort"
2017-01-11 13:38:16 +01:00
"strings"
2017-09-29 08:07:09 +02:00
"sync"
2017-01-11 13:38:16 +01:00
"time"
2017-06-15 18:14:19 +02:00
"github.com/goshuirc/irc-go/ircfmt"
"github.com/goshuirc/irc-go/ircmatch"
"github.com/goshuirc/irc-go/ircmsg"
2017-06-14 20:00:53 +02:00
"github.com/oragono/oragono/irc/custime"
"github.com/oragono/oragono/irc/sno"
2017-01-11 13:38:16 +01:00
"github.com/tidwall/buntdb"
)
const (
keyKlineEntry = "bans.kline %s"
)
// KLineInfo contains the address itself and expiration time for a given network.
type KLineInfo struct {
// Mask that is blocked.
Mask string
// Matcher, to facilitate fast matching.
Matcher ircmatch . Matcher
// Info contains information on the ban.
Info IPBanInfo
}
// KLineManager manages and klines.
type KLineManager struct {
2017-11-22 10:41:11 +01:00
sync . RWMutex // tier 1
2017-01-11 13:38:16 +01:00
// kline'd entries
entries map [ string ] * KLineInfo
}
// NewKLineManager returns a new KLineManager.
func NewKLineManager ( ) * KLineManager {
var km KLineManager
km . entries = make ( map [ string ] * KLineInfo )
return & km
}
// AllBans returns all bans (for use with APIs, etc).
func ( km * KLineManager ) AllBans ( ) map [ string ] IPBanInfo {
allb := make ( map [ string ] IPBanInfo )
2017-09-29 08:07:09 +02:00
km . RLock ( )
defer km . RUnlock ( )
2017-01-11 13:38:16 +01:00
for name , info := range km . entries {
allb [ name ] = info . Info
}
return allb
}
// AddMask adds to the blocked list.
2017-11-19 01:32:32 +01:00
func ( km * KLineManager ) AddMask ( mask string , length * IPRestrictTime , reason , operReason , operName string ) {
2017-01-11 13:38:16 +01:00
kln := KLineInfo {
Mask : mask ,
Matcher : ircmatch . MakeMatch ( mask ) ,
Info : IPBanInfo {
Time : length ,
Reason : reason ,
OperReason : operReason ,
2017-11-19 01:32:32 +01:00
OperName : operName ,
2017-01-11 13:38:16 +01:00
} ,
}
2017-09-29 08:07:09 +02:00
km . Lock ( )
2017-01-11 13:38:16 +01:00
km . entries [ mask ] = & kln
2017-09-29 08:07:09 +02:00
km . Unlock ( )
2017-01-11 13:38:16 +01:00
}
// RemoveMask removes a mask from the blocked list.
func ( km * KLineManager ) RemoveMask ( mask string ) {
2017-09-29 08:07:09 +02:00
km . Lock ( )
2017-01-11 13:38:16 +01:00
delete ( km . entries , mask )
2017-09-29 08:07:09 +02:00
km . Unlock ( )
2017-01-11 13:38:16 +01:00
}
// CheckMasks returns whether or not the hostmask(s) are banned, and how long they are banned for.
func ( km * KLineManager ) CheckMasks ( masks ... string ) ( isBanned bool , info * IPBanInfo ) {
2017-09-29 08:07:09 +02:00
doCleanup := false
defer func ( ) {
// asynchronously remove expired bans
if doCleanup {
go func ( ) {
km . Lock ( )
defer km . Unlock ( )
for key , entry := range km . entries {
if entry . Info . Time . IsExpired ( ) {
delete ( km . entries , key )
}
}
} ( )
}
} ( )
km . RLock ( )
defer km . RUnlock ( )
2017-01-11 13:38:16 +01:00
for _ , entryInfo := range km . entries {
2017-09-29 08:07:09 +02:00
if entryInfo . Info . Time != nil && entryInfo . Info . Time . IsExpired ( ) {
doCleanup = true
continue
}
matches := false
2017-01-11 13:38:16 +01:00
for _ , mask := range masks {
if entryInfo . Matcher . Match ( mask ) {
matches = true
break
}
}
2017-09-29 08:07:09 +02:00
if matches {
2017-01-11 13:38:16 +01:00
return true , & entryInfo . Info
}
}
// no matches!
return false , nil
}
2017-05-24 08:58:36 +02:00
// KLINE [ANDKILL] [MYSELF] [duration] <mask> [ON <server>] [reason [| oper reason]]
2017-10-14 23:53:13 +02:00
// KLINE LIST
2017-01-11 13:38:16 +01:00
func klineHandler ( server * Server , client * Client , msg ircmsg . IrcMessage ) bool {
// check oper permissions
if ! client . class . Capabilities [ "oper:local_ban" ] {
2018-01-22 12:26:01 +01:00
client . Send ( nil , server . name , ERR_NOPRIVS , client . nick , msg . Command , client . t ( "Insufficient oper privs" ) )
2017-01-11 13:38:16 +01:00
return false
}
currentArg := 0
2017-10-14 23:53:13 +02:00
// if they say LIST, we just list the current klines
if len ( msg . Params ) == currentArg + 1 && strings . ToLower ( msg . Params [ currentArg ] ) == "list" {
bans := server . klines . AllBans ( )
if len ( bans ) == 0 {
client . Notice ( "No KLINEs have been set!" )
}
for key , info := range bans {
2018-01-22 12:26:01 +01:00
client . Notice ( fmt . Sprintf ( client . t ( "Ban - %s - added by %s - %s" ) , key , info . OperName , info . BanMessage ( "%s" ) ) )
2017-10-14 23:53:13 +02:00
}
return false
}
2017-05-24 08:58:36 +02:00
// when setting a ban, if they say "ANDKILL" we should also kill all users who match it
var andKill bool
if len ( msg . Params ) > currentArg + 1 && strings . ToLower ( msg . Params [ currentArg ] ) == "andkill" {
andKill = true
currentArg ++
}
2017-01-11 13:38:16 +01:00
// when setting a ban that covers the oper's current connection, we require them to say
// "KLINE MYSELF" so that we're sure they really mean it.
var klineMyself bool
if len ( msg . Params ) > currentArg + 1 && strings . ToLower ( msg . Params [ currentArg ] ) == "myself" {
klineMyself = true
currentArg ++
}
// duration
2017-03-07 10:56:21 +01:00
duration , err := custime . ParseDuration ( msg . Params [ currentArg ] )
2017-01-11 13:38:16 +01:00
durationIsUsed := err == nil
if durationIsUsed {
currentArg ++
}
// get mask
if len ( msg . Params ) < currentArg + 1 {
2018-01-22 12:26:01 +01:00
client . Send ( nil , server . name , ERR_NEEDMOREPARAMS , client . nick , msg . Command , client . t ( "Not enough parameters" ) )
2017-01-11 13:38:16 +01:00
return false
}
mask := strings . ToLower ( msg . Params [ currentArg ] )
currentArg ++
// check mask
if ! strings . Contains ( mask , "!" ) && ! strings . Contains ( mask , "@" ) {
mask = mask + "!*@*"
} else if ! strings . Contains ( mask , "@" ) {
mask = mask + "@*"
}
matcher := ircmatch . MakeMatch ( mask )
for _ , clientMask := range client . AllNickmasks ( ) {
if ! klineMyself && matcher . Match ( clientMask ) {
2018-01-22 12:26:01 +01:00
client . Send ( nil , server . name , ERR_UNKNOWNERROR , client . nick , msg . Command , client . t ( "This ban matches you. To KLINE yourself, you must use the command: /KLINE MYSELF <arguments>" ) )
2017-01-11 13:38:16 +01:00
return false
}
}
// check remote
if len ( msg . Params ) > currentArg && msg . Params [ currentArg ] == "ON" {
2018-01-22 12:26:01 +01:00
client . Send ( nil , server . name , ERR_UNKNOWNERROR , client . nick , msg . Command , client . t ( "Remote servers not yet supported" ) )
2017-01-11 13:38:16 +01:00
return false
}
2017-11-19 01:32:32 +01:00
// get oper name
operName := client . operName
if operName == "" {
operName = server . name
}
2017-01-11 13:38:16 +01:00
// get comment(s)
reason := "No reason given"
operReason := "No reason given"
if len ( msg . Params ) > currentArg {
tempReason := strings . TrimSpace ( msg . Params [ currentArg ] )
if len ( tempReason ) > 0 && tempReason != "|" {
tempReasons := strings . SplitN ( tempReason , "|" , 2 )
if tempReasons [ 0 ] != "" {
reason = tempReasons [ 0 ]
}
if len ( tempReasons ) > 1 && tempReasons [ 1 ] != "" {
operReason = tempReasons [ 1 ]
} else {
operReason = reason
}
}
}
// assemble ban info
var banTime * IPRestrictTime
if durationIsUsed {
banTime = & IPRestrictTime {
Duration : duration ,
Expires : time . Now ( ) . Add ( duration ) ,
}
}
info := IPBanInfo {
Reason : reason ,
OperReason : operReason ,
2017-11-19 01:32:32 +01:00
OperName : operName ,
2017-01-11 13:38:16 +01:00
Time : banTime ,
}
// save in datastore
err = server . store . Update ( func ( tx * buntdb . Tx ) error {
klineKey := fmt . Sprintf ( keyKlineEntry , mask )
// assemble json from ban info
b , err := json . Marshal ( info )
if err != nil {
return err
}
tx . Set ( klineKey , string ( b ) , nil )
return nil
} )
2017-04-16 03:35:44 +02:00
if err != nil {
2018-01-22 12:26:01 +01:00
client . Notice ( fmt . Sprintf ( client . t ( "Could not successfully save new K-LINE: %s" ) , err . Error ( ) ) )
2017-04-16 03:35:44 +02:00
return false
}
2017-11-19 01:32:32 +01:00
server . klines . AddMask ( mask , banTime , reason , operReason , operName )
2017-01-11 13:38:16 +01:00
2017-06-11 18:17:55 +02:00
var snoDescription string
2017-01-11 13:38:16 +01:00
if durationIsUsed {
2018-01-23 07:50:19 +01:00
client . Notice ( fmt . Sprintf ( client . t ( "Added temporary (%[1]s) K-Line for %[2]s" ) , duration . String ( ) , mask ) )
2017-11-19 01:32:32 +01:00
snoDescription = fmt . Sprintf ( ircfmt . Unescape ( "%s [%s]$r added temporary (%s) K-Line for %s" ) , client . nick , operName , duration . String ( ) , mask )
2017-01-11 13:38:16 +01:00
} else {
2018-01-22 12:26:01 +01:00
client . Notice ( fmt . Sprintf ( client . t ( "Added K-Line for %s" ) , mask ) )
2017-11-19 01:32:32 +01:00
snoDescription = fmt . Sprintf ( ircfmt . Unescape ( "%s [%s]$r added K-Line for %s" ) , client . nick , operName , mask )
2017-01-11 13:38:16 +01:00
}
2017-06-11 18:17:55 +02:00
server . snomasks . Send ( sno . LocalXline , snoDescription )
2017-01-11 13:38:16 +01:00
2017-05-24 08:58:36 +02:00
var killClient bool
if andKill {
var clientsToKill [ ] * Client
2017-06-11 18:01:39 +02:00
var killedClientNicks [ ] string
2017-05-24 08:58:36 +02:00
2017-11-22 10:41:11 +01:00
for _ , mcl := range server . clients . AllClients ( ) {
2017-05-24 08:58:36 +02:00
for _ , clientMask := range mcl . AllNickmasks ( ) {
if matcher . Match ( clientMask ) {
clientsToKill = append ( clientsToKill , mcl )
2017-06-11 18:01:39 +02:00
killedClientNicks = append ( killedClientNicks , mcl . nick )
2017-05-24 08:58:36 +02:00
}
}
}
for _ , mcl := range clientsToKill {
2017-06-11 18:01:39 +02:00
mcl . exitedSnomaskSent = true
2018-01-22 12:26:01 +01:00
mcl . Quit ( fmt . Sprintf ( mcl . t ( "You have been banned from this server (%s)" ) , reason ) )
2017-05-24 08:58:36 +02:00
if mcl == client {
killClient = true
} else {
// if mcl == client, we kill them below
2018-01-21 02:59:52 +01:00
mcl . destroy ( false )
2017-05-24 08:58:36 +02:00
}
}
2017-06-11 18:01:39 +02:00
// send snomask
sort . Strings ( killedClientNicks )
2017-11-19 01:32:32 +01:00
server . snomasks . Send ( sno . LocalKills , fmt . Sprintf ( ircfmt . Unescape ( "%s [%s] killed %d clients with a KLINE $c[grey][$r%s$c[grey]]" ) , client . nick , operName , len ( killedClientNicks ) , strings . Join ( killedClientNicks , ", " ) ) )
2017-05-24 08:58:36 +02:00
}
return killClient
2017-01-11 13:38:16 +01:00
}
func unKLineHandler ( server * Server , client * Client , msg ircmsg . IrcMessage ) bool {
// check oper permissions
if ! client . class . Capabilities [ "oper:local_unban" ] {
2018-01-22 12:26:01 +01:00
client . Send ( nil , server . name , ERR_NOPRIVS , client . nick , msg . Command , client . t ( "Insufficient oper privs" ) )
2017-01-11 13:38:16 +01:00
return false
}
// get host
mask := msg . Params [ 0 ]
if ! strings . Contains ( mask , "!" ) && ! strings . Contains ( mask , "@" ) {
mask = mask + "!*@*"
} else if ! strings . Contains ( mask , "@" ) {
mask = mask + "@*"
}
// save in datastore
err := server . store . Update ( func ( tx * buntdb . Tx ) error {
klineKey := fmt . Sprintf ( keyKlineEntry , mask )
// check if it exists or not
val , err := tx . Get ( klineKey )
if val == "" {
return errNoExistingBan
} else if err != nil {
return err
}
tx . Delete ( klineKey )
return nil
} )
if err != nil {
2018-01-22 12:26:01 +01:00
client . Send ( nil , server . name , ERR_UNKNOWNERROR , client . nick , msg . Command , fmt . Sprintf ( client . t ( "Could not remove ban [%s]" ) , err . Error ( ) ) )
2017-01-11 13:38:16 +01:00
return false
}
server . klines . RemoveMask ( mask )
2018-01-22 12:26:01 +01:00
client . Notice ( fmt . Sprintf ( client . t ( "Removed K-Line for %s" ) , mask ) )
2017-06-11 18:17:55 +02:00
server . snomasks . Send ( sno . LocalXline , fmt . Sprintf ( ircfmt . Unescape ( "%s$r removed K-Line for %s" ) , client . nick , mask ) )
2017-01-11 13:38:16 +01:00
return false
}
func ( s * Server ) loadKLines ( ) {
s . klines = NewKLineManager ( )
// load from datastore
s . store . View ( func ( tx * buntdb . Tx ) error {
//TODO(dan): We could make this safer
tx . AscendKeys ( "bans.kline *" , func ( key , value string ) bool {
// get address name
key = key [ len ( "bans.kline " ) : ]
mask := key
// load ban info
var info IPBanInfo
json . Unmarshal ( [ ] byte ( value ) , & info )
2017-11-19 01:32:32 +01:00
// add oper name if it doesn't exist already
if info . OperName == "" {
info . OperName = s . name
}
2017-01-11 13:38:16 +01:00
// add to the server
2017-11-19 01:32:32 +01:00
s . klines . AddMask ( mask , info . Time , info . Reason , info . OperReason , info . OperName )
2017-01-11 13:38:16 +01:00
return true // true to continue I guess?
} )
return nil
} )
}