Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

import.go 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 = 19
  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. Certfps []string
  28. }
  29. type channelImport struct {
  30. Name string
  31. Founder string
  32. RegisteredAt int64 `json:"registeredAt"`
  33. Topic string
  34. TopicSetBy string `json:"topicSetBy"`
  35. TopicSetAt int64 `json:"topicSetAt"`
  36. Amode map[string]string
  37. Modes string
  38. Key string
  39. Limit int
  40. }
  41. type databaseImport struct {
  42. Version int
  43. Source string
  44. Users map[string]userImport
  45. Channels map[string]channelImport
  46. }
  47. func serializeAmodes(raw map[string]string, validCfUsernames utils.StringSet) (result []byte, err error) {
  48. processed := make(map[string]int, len(raw))
  49. for accountName, mode := range raw {
  50. if len(mode) != 1 {
  51. return nil, fmt.Errorf("invalid mode %s for account %s", mode, accountName)
  52. }
  53. cfname, err := CasefoldName(accountName)
  54. if err != nil || !validCfUsernames.Has(cfname) {
  55. log.Printf("skipping invalid amode recipient %s\n", accountName)
  56. } else {
  57. processed[cfname] = int(mode[0])
  58. }
  59. }
  60. result, err = json.Marshal(processed)
  61. return
  62. }
  63. func doImportDBGeneric(config *Config, dbImport databaseImport, credsType CredentialsVersion, tx *buntdb.Tx) (err error) {
  64. requiredVersion := 1
  65. if dbImport.Version != requiredVersion {
  66. return fmt.Errorf("unsupported version of the db for import: version %d is required", requiredVersion)
  67. }
  68. tx.Set(keySchemaVersion, strconv.Itoa(importDBSchemaVersion), nil)
  69. tx.Set(keyCloakSecret, utils.GenerateSecretKey(), nil)
  70. cfUsernames := make(utils.StringSet)
  71. for username, userInfo := range dbImport.Users {
  72. cfUsername, err := CasefoldName(username)
  73. if err != nil {
  74. log.Printf("invalid username %s: %v", username, err)
  75. continue
  76. }
  77. var certfps []string
  78. for _, certfp := range userInfo.Certfps {
  79. normalizedCertfp, err := utils.NormalizeCertfp(certfp)
  80. if err == nil {
  81. certfps = append(certfps, normalizedCertfp)
  82. } else {
  83. log.Printf("invalid certfp %s for %s\n", username, certfp)
  84. }
  85. }
  86. credentials := AccountCredentials{
  87. Version: credsType,
  88. PassphraseHash: []byte(userInfo.Hash),
  89. Certfps: certfps,
  90. }
  91. marshaledCredentials, err := json.Marshal(&credentials)
  92. if err != nil {
  93. log.Printf("invalid credentials for %s: %v", username, err)
  94. continue
  95. }
  96. tx.Set(fmt.Sprintf(keyAccountExists, cfUsername), "1", nil)
  97. tx.Set(fmt.Sprintf(keyAccountVerified, cfUsername), "1", nil)
  98. tx.Set(fmt.Sprintf(keyAccountName, cfUsername), userInfo.Name, nil)
  99. tx.Set(fmt.Sprintf(keyAccountCallback, cfUsername), "mailto:"+userInfo.Email, nil)
  100. tx.Set(fmt.Sprintf(keyAccountCredentials, cfUsername), string(marshaledCredentials), nil)
  101. tx.Set(fmt.Sprintf(keyAccountRegTime, cfUsername), strconv.FormatInt(userInfo.RegisteredAt, 10), nil)
  102. if userInfo.Vhost != "" {
  103. tx.Set(fmt.Sprintf(keyAccountVHost, cfUsername), userInfo.Vhost, nil)
  104. }
  105. if len(userInfo.AdditionalNicks) != 0 {
  106. tx.Set(fmt.Sprintf(keyAccountAdditionalNicks, cfUsername), marshalReservedNicks(userInfo.AdditionalNicks), nil)
  107. }
  108. for _, certfp := range certfps {
  109. tx.Set(fmt.Sprintf(keyCertToAccount, certfp), cfUsername, nil)
  110. }
  111. cfUsernames.Add(cfUsername)
  112. }
  113. for chname, chInfo := range dbImport.Channels {
  114. cfchname, err := CasefoldChannel(chname)
  115. if err != nil {
  116. log.Printf("invalid channel name %s: %v", chname, err)
  117. continue
  118. }
  119. cffounder, err := CasefoldName(chInfo.Founder)
  120. if err != nil {
  121. log.Printf("invalid founder %s for channel %s: %v", chInfo.Founder, chname, err)
  122. continue
  123. }
  124. tx.Set(fmt.Sprintf(keyChannelExists, cfchname), "1", nil)
  125. tx.Set(fmt.Sprintf(keyChannelName, cfchname), chname, nil)
  126. tx.Set(fmt.Sprintf(keyChannelRegTime, cfchname), strconv.FormatInt(chInfo.RegisteredAt, 10), nil)
  127. tx.Set(fmt.Sprintf(keyChannelFounder, cfchname), cffounder, nil)
  128. accountChannelsKey := fmt.Sprintf(keyAccountChannels, cffounder)
  129. founderChannels, fcErr := tx.Get(accountChannelsKey)
  130. if fcErr != nil || founderChannels == "" {
  131. founderChannels = cfchname
  132. } else {
  133. founderChannels = fmt.Sprintf("%s,%s", founderChannels, cfchname)
  134. }
  135. tx.Set(accountChannelsKey, founderChannels, nil)
  136. if chInfo.Topic != "" {
  137. tx.Set(fmt.Sprintf(keyChannelTopic, cfchname), chInfo.Topic, nil)
  138. tx.Set(fmt.Sprintf(keyChannelTopicSetTime, cfchname), strconv.FormatInt(chInfo.TopicSetAt, 10), nil)
  139. tx.Set(fmt.Sprintf(keyChannelTopicSetBy, cfchname), chInfo.TopicSetBy, nil)
  140. }
  141. if len(chInfo.Amode) != 0 {
  142. m, err := serializeAmodes(chInfo.Amode, cfUsernames)
  143. if err == nil {
  144. tx.Set(fmt.Sprintf(keyChannelAccountToUMode, cfchname), string(m), nil)
  145. } else {
  146. log.Printf("couldn't serialize amodes for %s: %v", chname, err)
  147. }
  148. }
  149. tx.Set(fmt.Sprintf(keyChannelModes, cfchname), chInfo.Modes, nil)
  150. if chInfo.Key != "" {
  151. tx.Set(fmt.Sprintf(keyChannelPassword, cfchname), chInfo.Key, nil)
  152. }
  153. if chInfo.Limit > 0 {
  154. tx.Set(fmt.Sprintf(keyChannelUserLimit, cfchname), strconv.Itoa(chInfo.Limit), nil)
  155. }
  156. }
  157. return nil
  158. }
  159. func doImportDB(config *Config, dbImport databaseImport, tx *buntdb.Tx) (err error) {
  160. switch dbImport.Source {
  161. case "atheme":
  162. return doImportDBGeneric(config, dbImport, CredentialsAtheme, tx)
  163. case "anope":
  164. return doImportDBGeneric(config, dbImport, CredentialsAnope, tx)
  165. default:
  166. return fmt.Errorf("unsupported import source: %s", dbImport.Source)
  167. }
  168. }
  169. func ImportDB(config *Config, infile string) (err error) {
  170. data, err := ioutil.ReadFile(infile)
  171. if err != nil {
  172. return
  173. }
  174. var dbImport databaseImport
  175. err = json.Unmarshal(data, &dbImport)
  176. if err != nil {
  177. return err
  178. }
  179. err = checkDBReadyForInit(config.Datastore.Path)
  180. if err != nil {
  181. return err
  182. }
  183. db, err := buntdb.Open(config.Datastore.Path)
  184. if err != nil {
  185. return err
  186. }
  187. performImport := func(tx *buntdb.Tx) (err error) {
  188. return doImportDB(config, dbImport, tx)
  189. }
  190. return db.Update(performImport)
  191. }