-
Notifications
You must be signed in to change notification settings - Fork 177
/
database.go
542 lines (480 loc) · 13.6 KB
/
database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
// Copyright (c) 2012-2014 Jeremy Latt
// Copyright (c) 2016 Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
package irc
import (
"encoding/json"
"fmt"
"log"
"os"
"strings"
"time"
"github.com/oragono/oragono/irc/modes"
"github.com/oragono/oragono/irc/utils"
"github.com/tidwall/buntdb"
)
const (
// 'version' of the database schema
keySchemaVersion = "db.version"
// latest schema of the db
latestDbSchema = "7"
)
type SchemaChanger func(*Config, *buntdb.Tx) error
type SchemaChange struct {
InitialVersion string // the change will take this version
TargetVersion string // and transform it into this version
Changer SchemaChanger
}
// maps an initial version to a schema change capable of upgrading it
var schemaChanges map[string]SchemaChange
type incompatibleSchemaError struct {
currentVersion string
requiredVersion string
}
func IncompatibleSchemaError(currentVersion string) (result *incompatibleSchemaError) {
return &incompatibleSchemaError{
currentVersion: currentVersion,
requiredVersion: latestDbSchema,
}
}
func (err *incompatibleSchemaError) Error() string {
return fmt.Sprintf("Database requires update. Expected schema v%s, got v%s", err.requiredVersion, err.currentVersion)
}
// InitDB creates the database, implementing the `oragono initdb` command.
func InitDB(path string) {
_, err := os.Stat(path)
if err == nil {
log.Fatal("Datastore already exists (delete it manually to continue): ", path)
} else if !os.IsNotExist(err) {
log.Fatal("Datastore path is inaccessible: ", err.Error())
}
err = initializeDB(path)
if err != nil {
log.Fatal("Could not save datastore: ", err.Error())
}
}
// internal database initialization code
func initializeDB(path string) error {
store, err := buntdb.Open(path)
if err != nil {
return err
}
defer store.Close()
err = store.Update(func(tx *buntdb.Tx) error {
// set schema version
tx.Set(keySchemaVersion, latestDbSchema, nil)
return nil
})
return err
}
// OpenDatabase returns an existing database, performing a schema version check.
func OpenDatabase(config *Config) (*buntdb.DB, error) {
return openDatabaseInternal(config, config.Datastore.AutoUpgrade)
}
// open the database, giving it at most one chance to auto-upgrade the schema
func openDatabaseInternal(config *Config, allowAutoupgrade bool) (db *buntdb.DB, err error) {
db, err = buntdb.Open(config.Datastore.Path)
if err != nil {
return
}
defer func() {
if err != nil && db != nil {
db.Close()
db = nil
}
}()
// read the current version string
var version string
err = db.View(func(tx *buntdb.Tx) error {
version, err = tx.Get(keySchemaVersion)
return err
})
if err != nil {
return
}
if version == latestDbSchema {
// success
return
}
// XXX quiesce the DB so we can be sure it's safe to make a backup copy
db.Close()
db = nil
if allowAutoupgrade {
err = performAutoUpgrade(version, config)
if err != nil {
return
}
// successful autoupgrade, let's try this again:
return openDatabaseInternal(config, false)
} else {
err = IncompatibleSchemaError(version)
return
}
}
func performAutoUpgrade(currentVersion string, config *Config) (err error) {
path := config.Datastore.Path
log.Printf("attempting to auto-upgrade schema from version %s to %s\n", currentVersion, latestDbSchema)
timestamp := time.Now().UTC().Format("2006-01-02-15:04:05.000Z")
backupPath := fmt.Sprintf("%s.v%s.%s.bak", path, currentVersion, timestamp)
log.Printf("making a backup of current database at %s\n", backupPath)
err = utils.CopyFile(path, backupPath)
if err != nil {
return err
}
err = UpgradeDB(config)
if err != nil {
// database upgrade is a single transaction, so we don't need to restore the backup;
// we can just delete it
os.Remove(backupPath)
}
return err
}
// UpgradeDB upgrades the datastore to the latest schema.
func UpgradeDB(config *Config) (err error) {
store, err := buntdb.Open(config.Datastore.Path)
if err != nil {
return err
}
defer store.Close()
var version string
err = store.Update(func(tx *buntdb.Tx) error {
for {
version, _ = tx.Get(keySchemaVersion)
change, schemaNeedsChange := schemaChanges[version]
if !schemaNeedsChange {
if version == latestDbSchema {
// success!
break
}
// unable to upgrade to the desired version, roll back
return IncompatibleSchemaError(version)
}
log.Println("attempting to update schema from version " + version)
err := change.Changer(config, tx)
if err != nil {
return err
}
_, _, err = tx.Set(keySchemaVersion, change.TargetVersion, nil)
if err != nil {
return err
}
log.Println("successfully updated schema to version " + change.TargetVersion)
}
return nil
})
if err != nil {
log.Printf("database upgrade failed and was rolled back: %v\n", err)
}
return err
}
func schemaChangeV1toV2(config *Config, tx *buntdb.Tx) error {
// == version 1 -> 2 ==
// account key changes and account.verified key bugfix.
var keysToRemove []string
newKeys := make(map[string]string)
tx.AscendKeys("account *", func(key, value string) bool {
keysToRemove = append(keysToRemove, key)
splitkey := strings.Split(key, " ")
// work around bug
if splitkey[2] == "exists" {
// manually create new verified key
newVerifiedKey := fmt.Sprintf("%s.verified %s", splitkey[0], splitkey[1])
newKeys[newVerifiedKey] = "1"
} else if splitkey[1] == "%s" {
return true
}
newKey := fmt.Sprintf("%s.%s %s", splitkey[0], splitkey[2], splitkey[1])
newKeys[newKey] = value
return true
})
for _, key := range keysToRemove {
tx.Delete(key)
}
for key, value := range newKeys {
tx.Set(key, value, nil)
}
return nil
}
// 1. channel founder names should be casefolded
// 2. founder should be explicitly granted the ChannelFounder user mode
// 3. explicitly initialize stored channel modes to the server default values
func schemaChangeV2ToV3(config *Config, tx *buntdb.Tx) error {
var channels []string
prefix := "channel.exists "
tx.AscendGreaterOrEqual("", prefix, func(key, value string) bool {
if !strings.HasPrefix(key, prefix) {
return false
}
chname := strings.TrimPrefix(key, prefix)
channels = append(channels, chname)
return true
})
// founder names should be casefolded
// founder should be explicitly granted the ChannelFounder user mode
for _, channel := range channels {
founderKey := "channel.founder " + channel
founder, _ := tx.Get(founderKey)
if founder != "" {
founder, err := CasefoldName(founder)
if err == nil {
tx.Set(founderKey, founder, nil)
accountToUmode := map[string]modes.Mode{
founder: modes.ChannelFounder,
}
atustr, _ := json.Marshal(accountToUmode)
tx.Set("channel.accounttoumode "+channel, string(atustr), nil)
}
}
}
// explicitly store the channel modes
defaultModes := config.Channels.defaultModes
modeStrings := make([]string, len(defaultModes))
for i, mode := range defaultModes {
modeStrings[i] = string(mode)
}
defaultModeString := strings.Join(modeStrings, "")
for _, channel := range channels {
tx.Set("channel.modes "+channel, defaultModeString, nil)
}
return nil
}
// 1. ban info format changed (from `legacyBanInfo` below to `IPBanInfo`)
// 2. dlines against individual IPs are normalized into dlines against the appropriate /128 network
func schemaChangeV3ToV4(config *Config, tx *buntdb.Tx) error {
type ipRestrictTime struct {
Duration time.Duration
Expires time.Time
}
type legacyBanInfo struct {
Reason string `json:"reason"`
OperReason string `json:"oper_reason"`
OperName string `json:"oper_name"`
Time *ipRestrictTime `json:"time"`
}
now := time.Now()
legacyToNewInfo := func(old legacyBanInfo) (new_ IPBanInfo) {
new_.Reason = old.Reason
new_.OperReason = old.OperReason
new_.OperName = old.OperName
if old.Time == nil {
new_.TimeCreated = now
new_.Duration = 0
} else {
new_.TimeCreated = old.Time.Expires.Add(-1 * old.Time.Duration)
new_.Duration = old.Time.Duration
}
return
}
var keysToDelete []string
prefix := "bans.dline "
dlines := make(map[string]IPBanInfo)
tx.AscendGreaterOrEqual("", prefix, func(key, value string) bool {
if !strings.HasPrefix(key, prefix) {
return false
}
keysToDelete = append(keysToDelete, key)
var lbinfo legacyBanInfo
id := strings.TrimPrefix(key, prefix)
err := json.Unmarshal([]byte(value), &lbinfo)
if err != nil {
log.Printf("error unmarshaling legacy dline: %v\n", err)
return true
}
// legacy keys can be either an IP or a CIDR
hostNet, err := utils.NormalizedNetFromString(id)
if err != nil {
log.Printf("error unmarshaling legacy dline network: %v\n", err)
return true
}
dlines[utils.NetToNormalizedString(hostNet)] = legacyToNewInfo(lbinfo)
return true
})
setOptions := func(info IPBanInfo) *buntdb.SetOptions {
if info.Duration == 0 {
return nil
}
ttl := info.TimeCreated.Add(info.Duration).Sub(now)
return &buntdb.SetOptions{Expires: true, TTL: ttl}
}
// store the new dlines
for id, info := range dlines {
b, err := json.Marshal(info)
if err != nil {
log.Printf("error marshaling migrated dline: %v\n", err)
continue
}
tx.Set(fmt.Sprintf("bans.dlinev2 %s", id), string(b), setOptions(info))
}
// same operations against klines
prefix = "bans.kline "
klines := make(map[string]IPBanInfo)
tx.AscendGreaterOrEqual("", prefix, func(key, value string) bool {
if !strings.HasPrefix(key, prefix) {
return false
}
keysToDelete = append(keysToDelete, key)
mask := strings.TrimPrefix(key, prefix)
var lbinfo legacyBanInfo
err := json.Unmarshal([]byte(value), &lbinfo)
if err != nil {
log.Printf("error unmarshaling legacy kline: %v\n", err)
return true
}
klines[mask] = legacyToNewInfo(lbinfo)
return true
})
for mask, info := range klines {
b, err := json.Marshal(info)
if err != nil {
log.Printf("error marshaling migrated kline: %v\n", err)
continue
}
tx.Set(fmt.Sprintf("bans.klinev2 %s", mask), string(b), setOptions(info))
}
// clean up all the old entries
for _, key := range keysToDelete {
tx.Delete(key)
}
return nil
}
// create new key tracking channels that belong to an account
func schemaChangeV4ToV5(config *Config, tx *buntdb.Tx) error {
founderToChannels := make(map[string][]string)
prefix := "channel.founder "
tx.AscendGreaterOrEqual("", prefix, func(key, value string) bool {
if !strings.HasPrefix(key, prefix) {
return false
}
channel := strings.TrimPrefix(key, prefix)
founderToChannels[value] = append(founderToChannels[value], channel)
return true
})
for founder, channels := range founderToChannels {
tx.Set(fmt.Sprintf("account.channels %s", founder), strings.Join(channels, ","), nil)
}
return nil
}
// custom nick enforcement was a separate db key, now it's part of settings
func schemaChangeV5ToV6(config *Config, tx *buntdb.Tx) error {
accountToEnforcement := make(map[string]NickEnforcementMethod)
prefix := "account.customenforcement "
tx.AscendGreaterOrEqual("", prefix, func(key, value string) bool {
if !strings.HasPrefix(key, prefix) {
return false
}
account := strings.TrimPrefix(key, prefix)
method, err := nickReservationFromString(value)
if err == nil {
accountToEnforcement[account] = method
} else {
log.Printf("skipping corrupt custom enforcement value for %s\n", account)
}
return true
})
for account, method := range accountToEnforcement {
var settings AccountSettings
settings.NickEnforcement = method
text, err := json.Marshal(settings)
if err != nil {
return err
}
tx.Delete(prefix + account)
tx.Set(fmt.Sprintf("account.settings %s", account), string(text), nil)
}
return nil
}
type maskInfoV7 struct {
TimeCreated time.Time
CreatorNickmask string
CreatorAccount string
}
func schemaChangeV6ToV7(config *Config, tx *buntdb.Tx) error {
now := time.Now().UTC()
var channels []string
prefix := "channel.exists "
tx.AscendGreaterOrEqual("", prefix, func(key, value string) bool {
if !strings.HasPrefix(key, prefix) {
return false
}
channels = append(channels, strings.TrimPrefix(key, prefix))
return true
})
converter := func(key string) {
oldRawValue, err := tx.Get(key)
if err != nil {
return
}
var masks []string
err = json.Unmarshal([]byte(oldRawValue), &masks)
if err != nil {
return
}
newCookedValue := make(map[string]maskInfoV7)
for _, mask := range masks {
normalizedMask, err := CanonicalizeMaskWildcard(mask)
if err != nil {
continue
}
newCookedValue[normalizedMask] = maskInfoV7{
TimeCreated: now,
CreatorNickmask: "*",
CreatorAccount: "*",
}
}
newRawValue, err := json.Marshal(newCookedValue)
if err != nil {
return
}
tx.Set(key, string(newRawValue), nil)
}
prefixes := []string{
"channel.banlist %s",
"channel.exceptlist %s",
"channel.invitelist %s",
}
for _, channel := range channels {
for _, prefix := range prefixes {
converter(fmt.Sprintf(prefix, channel))
}
}
return nil
}
func init() {
allChanges := []SchemaChange{
{
InitialVersion: "1",
TargetVersion: "2",
Changer: schemaChangeV1toV2,
},
{
InitialVersion: "2",
TargetVersion: "3",
Changer: schemaChangeV2ToV3,
},
{
InitialVersion: "3",
TargetVersion: "4",
Changer: schemaChangeV3ToV4,
},
{
InitialVersion: "4",
TargetVersion: "5",
Changer: schemaChangeV4ToV5,
},
{
InitialVersion: "5",
TargetVersion: "6",
Changer: schemaChangeV5ToV6,
},
{
InitialVersion: "6",
TargetVersion: "7",
Changer: schemaChangeV6ToV7,
},
}
// build the index
schemaChanges = make(map[string]SchemaChange)
for _, change := range allChanges {
schemaChanges[change.InitialVersion] = change
}
}