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

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