You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

database.go 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (c) 2012-2014 Jeremy Latt
  2. // Copyright (c) 2016 Daniel Oaks <daniel@danieloaks.net>
  3. // released under the MIT license
  4. package irc
  5. import (
  6. "encoding/base64"
  7. "fmt"
  8. "log"
  9. "os"
  10. "strings"
  11. "github.com/tidwall/buntdb"
  12. )
  13. const (
  14. // 'version' of the database schema
  15. keySchemaVersion = "db.version"
  16. // latest schema of the db
  17. latestDbSchema = "2"
  18. // key for the primary salt used by the ircd
  19. keySalt = "crypto.salt"
  20. )
  21. // InitDB creates the database.
  22. func InitDB(path string) {
  23. // prepare kvstore db
  24. //TODO(dan): fail if already exists instead? don't want to overwrite good data
  25. os.Remove(path)
  26. store, err := buntdb.Open(path)
  27. if err != nil {
  28. log.Fatal(fmt.Sprintf("Failed to open datastore: %s", err.Error()))
  29. }
  30. defer store.Close()
  31. err = store.Update(func(tx *buntdb.Tx) error {
  32. // set base db salt
  33. salt, err := NewSalt()
  34. encodedSalt := base64.StdEncoding.EncodeToString(salt)
  35. if err != nil {
  36. log.Fatal("Could not generate cryptographically-secure salt for the user:", err.Error())
  37. }
  38. tx.Set(keySalt, encodedSalt, nil)
  39. // set schema version
  40. tx.Set(keySchemaVersion, "2", nil)
  41. return nil
  42. })
  43. if err != nil {
  44. log.Fatal("Could not save datastore:", err.Error())
  45. }
  46. }
  47. // UpgradeDB upgrades the datastore to the latest schema.
  48. func UpgradeDB(path string) {
  49. store, err := buntdb.Open(path)
  50. if err != nil {
  51. log.Fatal(fmt.Sprintf("Failed to open datastore: %s", err.Error()))
  52. }
  53. defer store.Close()
  54. err = store.Update(func(tx *buntdb.Tx) error {
  55. version, _ := tx.Get(keySchemaVersion)
  56. // == version 1 -> 2 ==
  57. // account key changes and account.verified key bugfix.
  58. if version == "1" {
  59. log.Println("Updating store v1 to v2")
  60. var keysToRemove []string
  61. newKeys := make(map[string]string)
  62. tx.AscendKeys("account *", func(key, value string) bool {
  63. keysToRemove = append(keysToRemove, key)
  64. splitkey := strings.Split(key, " ")
  65. // work around bug
  66. if splitkey[2] == "exists" {
  67. // manually create new verified key
  68. newVerifiedKey := fmt.Sprintf("%s.verified %s", splitkey[0], splitkey[1])
  69. newKeys[newVerifiedKey] = "1"
  70. } else if splitkey[1] == "%s" {
  71. return true
  72. }
  73. newKey := fmt.Sprintf("%s.%s %s", splitkey[0], splitkey[2], splitkey[1])
  74. newKeys[newKey] = value
  75. return true
  76. })
  77. for _, key := range keysToRemove {
  78. tx.Delete(key)
  79. }
  80. for key, value := range newKeys {
  81. tx.Set(key, value, nil)
  82. }
  83. tx.Set(keySchemaVersion, "2", nil)
  84. }
  85. return nil
  86. })
  87. if err != nil {
  88. log.Fatal("Could not update datastore:", err.Error())
  89. }
  90. return
  91. }