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.

import.go 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright (c) 2020 Shivaram Lingamneni <slingamn@cs.stanford.edu>
  2. // released under the MIT license
  3. package irc
  4. import (
  5. "encoding/json"
  6. "fmt"
  7. "io/ioutil"
  8. "log"
  9. "strconv"
  10. "github.com/tidwall/buntdb"
  11. "github.com/oragono/oragono/irc/utils"
  12. )
  13. const (
  14. // produce a hardcoded version of the database schema
  15. // XXX instead of referencing, e.g., keyAccountExists, we should write in the string literal
  16. // (to ensure that no matter what code changes happen elsewhere, we're still producing a
  17. // db of the hardcoded version)
  18. importDBSchemaVersion = 18
  19. )
  20. type userImport struct {
  21. Name string
  22. Hash string
  23. Email string
  24. RegisteredAt int64 `json:"registeredAt"`
  25. Vhost string
  26. AdditionalNicks []string `json:"additionalNicks"`
  27. }
  28. type channelImport struct {
  29. Name string
  30. Founder string
  31. RegisteredAt int64 `json:"registeredAt"`
  32. Topic string
  33. TopicSetBy string `json:"topicSetBy"`
  34. TopicSetAt int64 `json:"topicSetAt"`
  35. Amode map[string]string
  36. Modes string
  37. Key string
  38. Limit int
  39. }
  40. type databaseImport struct {
  41. Version int
  42. Source string
  43. Users map[string]userImport
  44. Channels map[string]channelImport
  45. }
  46. func serializeAmodes(raw map[string]string) (result []byte, err error) {
  47. processed := make(map[string]int, len(raw))
  48. for accountName, mode := range raw {
  49. if len(mode) != 1 {
  50. return nil, fmt.Errorf("invalid mode %s for account %s", mode, accountName)
  51. }
  52. cfname, err := CasefoldName(accountName)
  53. if err != nil {
  54. return nil, fmt.Errorf("invalid amode recipient %s: %w", accountName, err)
  55. }
  56. processed[cfname] = int(mode[0])
  57. }
  58. result, err = json.Marshal(processed)
  59. return
  60. }
  61. func doImportDBGeneric(config *Config, dbImport databaseImport, credsType CredentialsVersion, tx *buntdb.Tx) (err error) {
  62. requiredVersion := 1
  63. if dbImport.Version != requiredVersion {
  64. return fmt.Errorf("unsupported version of the db for import: version %d is required", requiredVersion)
  65. }
  66. tx.Set(keySchemaVersion, strconv.Itoa(importDBSchemaVersion), nil)
  67. tx.Set(keyCloakSecret, utils.GenerateSecretKey(), nil)
  68. for username, userInfo := range dbImport.Users {
  69. cfUsername, err := CasefoldName(username)
  70. if err != nil {
  71. log.Printf("invalid username %s: %v", username, err)
  72. continue
  73. }
  74. credentials := AccountCredentials{
  75. Version: credsType,
  76. PassphraseHash: []byte(userInfo.Hash),
  77. }
  78. marshaledCredentials, err := json.Marshal(&credentials)
  79. if err != nil {
  80. log.Printf("invalid credentials for %s: %v", username, err)
  81. continue
  82. }
  83. tx.Set(fmt.Sprintf(keyAccountExists, cfUsername), "1", nil)
  84. tx.Set(fmt.Sprintf(keyAccountVerified, cfUsername), "1", nil)
  85. tx.Set(fmt.Sprintf(keyAccountName, cfUsername), userInfo.Name, nil)
  86. tx.Set(fmt.Sprintf(keyAccountCallback, cfUsername), "mailto:"+userInfo.Email, nil)
  87. tx.Set(fmt.Sprintf(keyAccountCredentials, cfUsername), string(marshaledCredentials), nil)
  88. tx.Set(fmt.Sprintf(keyAccountRegTime, cfUsername), strconv.FormatInt(userInfo.RegisteredAt, 10), nil)
  89. if userInfo.Vhost != "" {
  90. tx.Set(fmt.Sprintf(keyAccountVHost, cfUsername), userInfo.Vhost, nil)
  91. }
  92. if len(userInfo.AdditionalNicks) != 0 {
  93. tx.Set(fmt.Sprintf(keyAccountAdditionalNicks, cfUsername), marshalReservedNicks(userInfo.AdditionalNicks), nil)
  94. }
  95. }
  96. for chname, chInfo := range dbImport.Channels {
  97. cfchname, err := CasefoldChannel(chname)
  98. if err != nil {
  99. log.Printf("invalid channel name %s: %v", chname, err)
  100. continue
  101. }
  102. cffounder, err := CasefoldName(chInfo.Founder)
  103. if err != nil {
  104. log.Printf("invalid founder %s for channel %s: %v", chInfo.Founder, chname, err)
  105. continue
  106. }
  107. tx.Set(fmt.Sprintf(keyChannelExists, cfchname), "1", nil)
  108. tx.Set(fmt.Sprintf(keyChannelName, cfchname), chname, nil)
  109. tx.Set(fmt.Sprintf(keyChannelRegTime, cfchname), strconv.FormatInt(chInfo.RegisteredAt, 10), nil)
  110. tx.Set(fmt.Sprintf(keyChannelFounder, cfchname), cffounder, nil)
  111. accountChannelsKey := fmt.Sprintf(keyAccountChannels, cffounder)
  112. founderChannels, fcErr := tx.Get(accountChannelsKey)
  113. if fcErr != nil || founderChannels == "" {
  114. founderChannels = cfchname
  115. } else {
  116. founderChannels = fmt.Sprintf("%s,%s", founderChannels, cfchname)
  117. }
  118. tx.Set(accountChannelsKey, founderChannels, nil)
  119. if chInfo.Topic != "" {
  120. tx.Set(fmt.Sprintf(keyChannelTopic, cfchname), chInfo.Topic, nil)
  121. tx.Set(fmt.Sprintf(keyChannelTopicSetTime, cfchname), strconv.FormatInt(chInfo.TopicSetAt, 10), nil)
  122. tx.Set(fmt.Sprintf(keyChannelTopicSetBy, cfchname), chInfo.TopicSetBy, nil)
  123. }
  124. if len(chInfo.Amode) != 0 {
  125. m, err := serializeAmodes(chInfo.Amode)
  126. if err == nil {
  127. tx.Set(fmt.Sprintf(keyChannelAccountToUMode, cfchname), string(m), nil)
  128. } else {
  129. log.Printf("couldn't serialize amodes for %s: %v", chname, err)
  130. }
  131. }
  132. tx.Set(fmt.Sprintf(keyChannelModes, cfchname), chInfo.Modes, nil)
  133. if chInfo.Key != "" {
  134. tx.Set(fmt.Sprintf(keyChannelPassword, cfchname), chInfo.Key, nil)
  135. }
  136. if chInfo.Limit > 0 {
  137. tx.Set(fmt.Sprintf(keyChannelUserLimit, cfchname), strconv.Itoa(chInfo.Limit), nil)
  138. }
  139. }
  140. return nil
  141. }
  142. func doImportDB(config *Config, dbImport databaseImport, tx *buntdb.Tx) (err error) {
  143. switch dbImport.Source {
  144. case "atheme":
  145. return doImportDBGeneric(config, dbImport, CredentialsAtheme, tx)
  146. case "anope":
  147. return doImportDBGeneric(config, dbImport, CredentialsAnope, tx)
  148. default:
  149. return fmt.Errorf("unsupported import source: %s", dbImport.Source)
  150. }
  151. }
  152. func ImportDB(config *Config, infile string) (err error) {
  153. data, err := ioutil.ReadFile(infile)
  154. if err != nil {
  155. return
  156. }
  157. var dbImport databaseImport
  158. err = json.Unmarshal(data, &dbImport)
  159. if err != nil {
  160. return err
  161. }
  162. err = checkDBReadyForInit(config.Datastore.Path)
  163. if err != nil {
  164. return err
  165. }
  166. db, err := buntdb.Open(config.Datastore.Path)
  167. if err != nil {
  168. return err
  169. }
  170. performImport := func(tx *buntdb.Tx) (err error) {
  171. return doImportDB(config, dbImport, tx)
  172. }
  173. return db.Update(performImport)
  174. }