2016-06-15 13:50:56 +02:00
|
|
|
// Copyright (c) 2012-2014 Jeremy Latt
|
|
|
|
// 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-06-15 13:50:56 +02:00
|
|
|
|
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-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
|
2016-09-05 10:45:42 +02:00
|
|
|
salt, err := NewSalt()
|
|
|
|
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
|
|
|
|
tx.Set(keySchemaVersion, "1", nil)
|
2016-09-05 10:45:42 +02:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Could not save bunt store:", err.Error())
|
|
|
|
}
|
2014-03-02 00:02:24 +01:00
|
|
|
}
|
|
|
|
|
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-09-17 13:23:04 +02:00
|
|
|
return
|
2014-03-02 00:02:24 +01:00
|
|
|
}
|