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 Daniel Oaks <daniel@danieloaks.net>
|
2016-06-15 13:50:56 +02:00
|
|
|
// released under the MIT license
|
|
|
|
|
2014-03-02 00:02:24 +01:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
2016-09-05 10:45:42 +02:00
|
|
|
"encoding/base64"
|
2014-03-08 03:14:02 +01:00
|
|
|
"fmt"
|
2014-03-02 00:02:24 +01:00
|
|
|
"log"
|
|
|
|
"os"
|
2016-11-06 04:47:13 +01:00
|
|
|
"strings"
|
2016-06-15 13:50:56 +02:00
|
|
|
|
2017-10-05 16:03:53 +02:00
|
|
|
"github.com/oragono/oragono/irc/passwd"
|
|
|
|
|
2016-09-05 10:45:42 +02:00
|
|
|
"github.com/tidwall/buntdb"
|
2014-03-02 00:02:24 +01:00
|
|
|
)
|
|
|
|
|
2016-09-05 10:45:42 +02:00
|
|
|
const (
|
2016-09-17 13:23:04 +02:00
|
|
|
// 'version' of the database schema
|
|
|
|
keySchemaVersion = "db.version"
|
2016-11-06 04:47:13 +01:00
|
|
|
// latest schema of the db
|
|
|
|
latestDbSchema = "2"
|
2016-09-05 10:45:42 +02:00
|
|
|
// key for the primary salt used by the ircd
|
|
|
|
keySalt = "crypto.salt"
|
|
|
|
)
|
|
|
|
|
2016-09-17 13:23:04 +02:00
|
|
|
// InitDB creates the database.
|
|
|
|
func InitDB(path string) {
|
2016-09-05 10:45:42 +02:00
|
|
|
// prepare kvstore db
|
2016-09-17 13:23:04 +02:00
|
|
|
//TODO(dan): fail if already exists instead? don't want to overwrite good data
|
|
|
|
os.Remove(path)
|
|
|
|
store, err := buntdb.Open(path)
|
2016-09-05 10:45:42 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(fmt.Sprintf("Failed to open datastore: %s", err.Error()))
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
|
|
|
err = store.Update(func(tx *buntdb.Tx) error {
|
2016-09-17 13:23:04 +02:00
|
|
|
// set base db salt
|
2017-10-05 16:03:53 +02:00
|
|
|
salt, err := passwd.NewSalt()
|
2016-09-05 10:45:42 +02:00
|
|
|
encodedSalt := base64.StdEncoding.EncodeToString(salt)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Could not generate cryptographically-secure salt for the user:", err.Error())
|
|
|
|
}
|
|
|
|
tx.Set(keySalt, encodedSalt, nil)
|
2016-09-17 13:23:04 +02:00
|
|
|
|
|
|
|
// set schema version
|
2016-11-06 04:47:13 +01:00
|
|
|
tx.Set(keySchemaVersion, "2", nil)
|
2016-09-05 10:45:42 +02:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2016-11-06 04:47:13 +01:00
|
|
|
log.Fatal("Could not save datastore:", err.Error())
|
2016-09-05 10:45:42 +02:00
|
|
|
}
|
2014-03-02 00:02:24 +01:00
|
|
|
}
|
|
|
|
|
2017-10-05 15:29:34 +02:00
|
|
|
// OpenDatabase returns an existing database, performing a schema version check.
|
2017-09-28 07:30:53 +02:00
|
|
|
func OpenDatabase(path string) (*buntdb.DB, error) {
|
|
|
|
// open data store
|
|
|
|
db, err := buntdb.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// check db version
|
|
|
|
err = db.View(func(tx *buntdb.Tx) error {
|
|
|
|
version, _ := tx.Get(keySchemaVersion)
|
|
|
|
if version != latestDbSchema {
|
2017-10-05 15:29:34 +02:00
|
|
|
return fmt.Errorf("Database must be updated. Expected schema v%s, got v%s", latestDbSchema, version)
|
2017-09-28 07:30:53 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
// close the db
|
|
|
|
db.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
2016-09-17 13:23:04 +02:00
|
|
|
// UpgradeDB upgrades the datastore to the latest schema.
|
2014-03-08 03:14:02 +01:00
|
|
|
func UpgradeDB(path string) {
|
2016-11-06 04:47:13 +01:00
|
|
|
store, err := buntdb.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(fmt.Sprintf("Failed to open datastore: %s", err.Error()))
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
|
|
|
err = store.Update(func(tx *buntdb.Tx) error {
|
|
|
|
version, _ := tx.Get(keySchemaVersion)
|
|
|
|
|
|
|
|
// == version 1 -> 2 ==
|
|
|
|
// account key changes and account.verified key bugfix.
|
|
|
|
if version == "1" {
|
|
|
|
log.Println("Updating store v1 to v2")
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.Set(keySchemaVersion, "2", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Could not update datastore:", err.Error())
|
|
|
|
}
|
|
|
|
|
2016-09-17 13:23:04 +02:00
|
|
|
return
|
2014-03-02 00:02:24 +01:00
|
|
|
}
|