db.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of gostfix project https://git.semlanik.org/semlanik/gostfix
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. package db
  26. import (
  27. "context"
  28. "crypto/sha1"
  29. "encoding/hex"
  30. "errors"
  31. "fmt"
  32. "log"
  33. "os"
  34. "os/exec"
  35. "strings"
  36. "time"
  37. common "git.semlanik.org/semlanik/gostfix/common"
  38. bcrypt "golang.org/x/crypto/bcrypt"
  39. bson "go.mongodb.org/mongo-driver/bson"
  40. "go.mongodb.org/mongo-driver/bson/primitive"
  41. mongo "go.mongodb.org/mongo-driver/mongo"
  42. options "go.mongodb.org/mongo-driver/mongo/options"
  43. config "git.semlanik.org/semlanik/gostfix/config"
  44. )
  45. type Storage struct {
  46. db *mongo.Database
  47. usersCollection *mongo.Collection
  48. tokensCollection *mongo.Collection
  49. emailsCollection *mongo.Collection
  50. allEmailsCollection *mongo.Collection
  51. }
  52. func qualifiedMailCollection(user string) string {
  53. sum := sha1.Sum([]byte(user))
  54. return "mb" + hex.EncodeToString(sum[:])
  55. }
  56. func NewStorage() (s *Storage, err error) {
  57. fullUrl := "mongodb://"
  58. if config.ConfigInstance().MongoUser != "" {
  59. fullUrl += config.ConfigInstance().MongoUser
  60. if config.ConfigInstance().MongoPassword != "" {
  61. fullUrl += ":" + config.ConfigInstance().MongoPassword
  62. }
  63. fullUrl += "@"
  64. }
  65. fullUrl += config.ConfigInstance().MongoAddress
  66. client, err := mongo.NewClient(options.Client().ApplyURI(fullUrl))
  67. if err != nil {
  68. return nil, err
  69. }
  70. ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
  71. defer cancel()
  72. err = client.Connect(ctx)
  73. if err != nil {
  74. return nil, err
  75. }
  76. db := client.Database("gostfix")
  77. index := mongo.IndexModel{
  78. Keys: bson.M{
  79. "user": 1,
  80. },
  81. Options: options.Index().SetUnique(true),
  82. }
  83. s = &Storage{
  84. db: db,
  85. usersCollection: db.Collection("users"),
  86. tokensCollection: db.Collection("tokens"),
  87. emailsCollection: db.Collection("emails"),
  88. allEmailsCollection: db.Collection("allEmails"),
  89. }
  90. //Initial database setup
  91. s.usersCollection.Indexes().CreateOne(context.Background(), index)
  92. s.tokensCollection.Indexes().CreateOne(context.Background(), index)
  93. s.emailsCollection.Indexes().CreateOne(context.Background(), index)
  94. return
  95. }
  96. func (s *Storage) AddUser(user, password, fullName string) error {
  97. hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
  98. if err != nil {
  99. return err
  100. }
  101. hashString := string(hash)
  102. userInfo := bson.M{
  103. "user": user,
  104. "password": hashString,
  105. "fullName": fullName,
  106. }
  107. _, err = s.usersCollection.InsertOne(context.Background(), userInfo)
  108. if err != nil {
  109. return err
  110. }
  111. err = s.addEmail(user, user, true)
  112. if err != nil {
  113. s.usersCollection.DeleteOne(context.Background(), bson.M{"user": user})
  114. return err
  115. }
  116. //TODO: Update postfix virtual map here
  117. return nil
  118. }
  119. func (s *Storage) AddEmail(user string, email string) error {
  120. return s.addEmail(user, email, false)
  121. }
  122. func (s *Storage) addEmail(user string, email string, upsert bool) error {
  123. result := struct {
  124. User string
  125. }{}
  126. err := s.usersCollection.FindOne(context.Background(), bson.M{"user": user}).Decode(&result)
  127. if err != nil {
  128. return err
  129. }
  130. emails, err := s.GetAllEmails()
  131. if err != nil {
  132. return err
  133. }
  134. for _, existingEmail := range emails {
  135. if existingEmail == email {
  136. return errors.New("Email exists")
  137. }
  138. }
  139. file, err := os.OpenFile(config.ConfigInstance().VMailboxMaps, os.O_APPEND|os.O_WRONLY, 0664)
  140. if err != nil {
  141. return errors.New("Unable to add email to maps" + err.Error())
  142. }
  143. emailParts := strings.Split(email, "@")
  144. if len(emailParts) != 2 {
  145. return errors.New("Invalid email format")
  146. }
  147. _, err = file.WriteString(email + " " + emailParts[1] + "/" + emailParts[0] + "\n")
  148. if err != nil {
  149. return errors.New("Unable to add email to maps" + err.Error())
  150. }
  151. cmd := exec.Command("postmap", config.ConfigInstance().VMailboxMaps)
  152. err = cmd.Run()
  153. if err != nil {
  154. return errors.New("Unable to execute postmap")
  155. }
  156. _, err = s.emailsCollection.UpdateOne(context.Background(),
  157. bson.M{"user": user},
  158. bson.M{"$addToSet": bson.M{"email": email}},
  159. options.Update().SetUpsert(upsert))
  160. return err
  161. }
  162. func (s *Storage) RemoveEmail(user string, email string) error {
  163. _, err := s.emailsCollection.UpdateOne(context.Background(),
  164. bson.M{"user": user},
  165. bson.M{"$pull": bson.M{"email": email}})
  166. //TODO: Update postfix virtual map here
  167. return err
  168. }
  169. func (s *Storage) CheckUser(user, password string) error {
  170. log.Printf("Check user: %s %s", user, password)
  171. result := struct {
  172. User string
  173. Password string
  174. }{}
  175. err := s.usersCollection.FindOne(context.Background(), bson.M{"user": user}).Decode(&result)
  176. if err != nil {
  177. return errors.New("Invalid user or password")
  178. }
  179. if bcrypt.CompareHashAndPassword([]byte(result.Password), []byte(password)) != nil {
  180. return errors.New("Invalid user or password")
  181. }
  182. return nil
  183. }
  184. func (s *Storage) AddToken(user, token string) error {
  185. log.Printf("Add token: %s\n", user)
  186. s.tokensCollection.UpdateOne(context.Background(),
  187. bson.M{"user": user},
  188. bson.M{
  189. "$addToSet": bson.M{
  190. "token": bson.M{
  191. "token": token,
  192. "expire": time.Now().Add(time.Hour * 24).Unix(),
  193. },
  194. },
  195. },
  196. options.Update().SetUpsert(true))
  197. s.CleanupTokens(user)
  198. return nil
  199. }
  200. func (s *Storage) CheckToken(user, token string) error {
  201. if token == "" {
  202. return errors.New("Invalid token")
  203. }
  204. cur, err := s.tokensCollection.Aggregate(context.Background(),
  205. bson.A{
  206. bson.M{"$match": bson.M{"user": user}},
  207. bson.M{"$unwind": "$token"},
  208. bson.M{"$match": bson.M{"token.token": token}},
  209. })
  210. if err != nil {
  211. log.Fatalln(err)
  212. return err
  213. }
  214. ok := false
  215. defer cur.Close(context.Background())
  216. if cur.Next(context.Background()) {
  217. result := struct {
  218. Token struct {
  219. Expire int64
  220. }
  221. }{}
  222. err = cur.Decode(&result)
  223. ok = err == nil && result.Token.Expire >= time.Now().Unix()
  224. }
  225. if ok {
  226. //TODO: Renew token
  227. return nil
  228. }
  229. return errors.New("Token expired")
  230. }
  231. func (s *Storage) RemoveToken(user, token string) error {
  232. s.CleanupTokens(user)
  233. _, err := s.tokensCollection.UpdateOne(context.Background(), bson.M{"user": user}, bson.M{"$pull": bson.M{"token": bson.M{"token": token}}})
  234. if err != nil {
  235. log.Printf("Unable to remove token %s", err)
  236. }
  237. return err
  238. }
  239. func (s *Storage) CleanupTokens(user string) {
  240. log.Printf("Cleanup tokens: %s\n", user)
  241. cur, err := s.tokensCollection.Aggregate(context.Background(),
  242. bson.A{
  243. bson.M{"$match": bson.M{"user": user}},
  244. bson.M{"$unwind": "$token"},
  245. })
  246. if err != nil {
  247. log.Fatalln(err)
  248. }
  249. type tokenMetadata struct {
  250. Expire int64
  251. Token string
  252. }
  253. tokensToKeep := bson.A{}
  254. defer cur.Close(context.Background())
  255. for cur.Next(context.Background()) {
  256. result := struct {
  257. Token *tokenMetadata
  258. }{
  259. Token: &tokenMetadata{},
  260. }
  261. err = cur.Decode(&result)
  262. if err == nil && result.Token.Expire >= time.Now().Unix() {
  263. tokensToKeep = append(tokensToKeep, result.Token)
  264. } else {
  265. log.Printf("Expired token found for %s : %d", user, result.Token.Expire)
  266. }
  267. }
  268. _, err = s.tokensCollection.UpdateOne(context.Background(), bson.M{"user": user}, bson.M{"$set": bson.M{"token": tokensToKeep}})
  269. return
  270. }
  271. func (s *Storage) SaveMail(email, folder string, m *common.Mail, read bool) error {
  272. result := &struct {
  273. User string
  274. }{}
  275. s.emailsCollection.FindOne(context.Background(), bson.M{"email": email}).Decode(result)
  276. mailsCollection := s.db.Collection(qualifiedMailCollection(result.User))
  277. mailsCollection.InsertOne(context.Background(), &struct {
  278. Email string
  279. Mail *common.Mail
  280. Folder string
  281. Read bool
  282. Trash bool
  283. }{
  284. Email: email,
  285. Mail: m,
  286. Folder: folder,
  287. Read: read,
  288. Trash: false,
  289. }, options.InsertOne().SetBypassDocumentValidation(true))
  290. return nil
  291. }
  292. func (s *Storage) MoveMail(user string, mailId string, folder string) error {
  293. mailsCollection := s.db.Collection(qualifiedMailCollection(user))
  294. oId, err := primitive.ObjectIDFromHex(mailId)
  295. if err != nil {
  296. return err
  297. }
  298. if folder == common.Trash {
  299. _, err = mailsCollection.UpdateOne(context.Background(), bson.M{"_id": oId}, bson.M{"$set": bson.M{"trash": true}})
  300. } else {
  301. _, err = mailsCollection.UpdateOne(context.Background(), bson.M{"_id": oId}, bson.M{"$set": bson.M{"folder": folder, "trash": false}})
  302. }
  303. return err
  304. }
  305. func (s *Storage) RestoreMail(user string, mailId string) error {
  306. mailsCollection := s.db.Collection(qualifiedMailCollection(user))
  307. oId, err := primitive.ObjectIDFromHex(mailId)
  308. if err != nil {
  309. return err
  310. }
  311. //TODO: Legacy for old databases remove soon
  312. metadata, err := s.GetMail(user, mailId)
  313. if metadata.Folder == common.Trash {
  314. _, err = mailsCollection.UpdateOne(context.Background(), bson.M{"_id": oId}, bson.M{"$set": bson.M{"folder": common.Inbox}})
  315. }
  316. _, err = mailsCollection.UpdateOne(context.Background(), bson.M{"_id": oId}, bson.M{"$set": bson.M{"trash": false}})
  317. return err
  318. }
  319. func (s *Storage) DeleteMail(user string, mailId string) error {
  320. mailsCollection := s.db.Collection(qualifiedMailCollection(user))
  321. oId, err := primitive.ObjectIDFromHex(mailId)
  322. if err != nil {
  323. return err
  324. }
  325. _, err = mailsCollection.DeleteOne(context.Background(), bson.M{"_id": oId})
  326. return err
  327. }
  328. func (s *Storage) GetMailList(user, email, folder string, frame common.Frame) ([]*common.MailMetadata, error) {
  329. mailsCollection := s.db.Collection(qualifiedMailCollection(user))
  330. matchFilter := bson.M{"email": email}
  331. if folder == common.Trash {
  332. matchFilter["$or"] = bson.A{
  333. bson.M{"trash": true},
  334. bson.M{"folder": folder}, //TODO: Legacy for old databases remove soon
  335. }
  336. } else {
  337. matchFilter["folder"] = folder
  338. matchFilter["$or"] = bson.A{
  339. bson.M{"trash": false},
  340. bson.M{"trash": bson.M{"$exists": false}}, //TODO: Legacy for old databases remove soon
  341. }
  342. }
  343. request := bson.A{
  344. bson.M{"$match": matchFilter},
  345. bson.M{"$sort": bson.M{"mail.header.date": -1}},
  346. }
  347. if frame.Skip > 0 {
  348. request = append(request, bson.M{"$skip": frame.Skip})
  349. }
  350. fmt.Printf("Trying limit number of mails: %v\n", frame)
  351. if frame.Limit > 0 {
  352. fmt.Printf("Limit number of mails: %v\n", frame)
  353. request = append(request, bson.M{"$limit": frame.Limit})
  354. }
  355. cur, err := mailsCollection.Aggregate(context.Background(), request)
  356. if err != nil {
  357. log.Println(err.Error())
  358. return nil, err
  359. }
  360. var headers []*common.MailMetadata
  361. for cur.Next(context.Background()) {
  362. result := &common.MailMetadata{}
  363. err = cur.Decode(result)
  364. if err != nil {
  365. log.Printf("Unable to read database mail record: %s", err)
  366. continue
  367. }
  368. // fmt.Printf("Add mail: %s", result.Id)
  369. headers = append(headers, result)
  370. }
  371. // fmt.Printf("Mails read from database: %v", headers)
  372. return headers, nil
  373. }
  374. func (s *Storage) GetUserInfo(user string) (*common.UserInfo, error) {
  375. result := &common.UserInfo{}
  376. err := s.usersCollection.FindOne(context.Background(), bson.M{"user": user}).Decode(result)
  377. return result, err
  378. }
  379. func (s *Storage) GetEmailStats(user string, email string, folder string) (unread, total int, err error) {
  380. mailsCollection := s.db.Collection(qualifiedMailCollection(user))
  381. result := &struct {
  382. Total int
  383. Unread int
  384. }{}
  385. matchFilter := bson.M{"email": email}
  386. if folder == common.Trash {
  387. matchFilter["$or"] = bson.A{
  388. bson.M{"trash": true},
  389. bson.M{"folder": folder}, //TODO: Legacy for old databases remove soon
  390. }
  391. } else {
  392. matchFilter["folder"] = folder
  393. matchFilter["$or"] = bson.A{
  394. bson.M{"trash": false},
  395. bson.M{"trash": bson.M{"$exists": false}}, //TODO: Legacy for old databases remove soon
  396. }
  397. }
  398. unreadMatchFilter := matchFilter
  399. unreadMatchFilter["read"] = false
  400. cur, err := mailsCollection.Aggregate(context.Background(), bson.A{bson.M{"$match": unreadMatchFilter}, bson.M{"$count": "unread"}})
  401. if err == nil && cur.Next(context.Background()) {
  402. cur.Decode(result)
  403. } else {
  404. return 0, 0, err
  405. }
  406. cur, err = mailsCollection.Aggregate(context.Background(), bson.A{bson.M{"$match": matchFilter}, bson.M{"$count": "total"}})
  407. if err == nil && cur.Next(context.Background()) {
  408. cur.Decode(result)
  409. } else {
  410. return 0, 0, err
  411. }
  412. return result.Unread, result.Total, err
  413. }
  414. func (s *Storage) GetMail(user string, id string) (metadata *common.MailMetadata, err error) {
  415. mailsCollection := s.db.Collection(qualifiedMailCollection(user))
  416. oId, err := primitive.ObjectIDFromHex(id)
  417. if err != nil {
  418. return nil, err
  419. }
  420. metadata = &common.MailMetadata{
  421. Mail: common.NewMail(),
  422. }
  423. err = mailsCollection.FindOne(context.Background(), bson.M{"_id": oId}).Decode(metadata)
  424. if err != nil {
  425. return nil, err
  426. }
  427. return metadata, nil
  428. }
  429. func (s *Storage) SetRead(user string, id string, read bool) error {
  430. mailsCollection := s.db.Collection(qualifiedMailCollection(user))
  431. oId, err := primitive.ObjectIDFromHex(id)
  432. if err != nil {
  433. return err
  434. }
  435. _, err = mailsCollection.UpdateOne(context.Background(), bson.M{"_id": oId}, bson.M{"$set": bson.M{"read": read}})
  436. return err
  437. }
  438. func (s *Storage) GetAttachment(user string, attachmentId string) (filePath string, err error) {
  439. return "", nil
  440. }
  441. func (s *Storage) GetUsers() (users []string, err error) {
  442. return nil, nil
  443. }
  444. func (s *Storage) GetEmails(user string) (emails []string, err error) {
  445. result := &struct {
  446. Email []string
  447. }{}
  448. err = s.emailsCollection.FindOne(context.Background(), bson.M{"user": user}).Decode(result)
  449. if err != nil {
  450. return nil, err
  451. }
  452. return result.Email, nil
  453. }
  454. func (s *Storage) GetAllEmails() (emails []string, err error) {
  455. cur, err := s.allEmailsCollection.Find(context.Background(), bson.M{})
  456. if cur.Next(context.Background()) {
  457. result := struct {
  458. Emails []string
  459. }{}
  460. err = cur.Decode(&result)
  461. if err == nil {
  462. return result.Emails, nil
  463. }
  464. }
  465. return nil, err
  466. }
  467. func (s *Storage) CheckEmailExists(email string) bool {
  468. result := s.allEmailsCollection.FindOne(context.Background(), bson.M{"emails": email})
  469. return result.Err() == nil
  470. }
  471. func (s *Storage) GetFolders(email string) (folders []*common.Folder) {
  472. folders = []*common.Folder{
  473. &common.Folder{Name: common.Inbox, Custom: false},
  474. &common.Folder{Name: common.Sent, Custom: false},
  475. &common.Folder{Name: common.Trash, Custom: false},
  476. &common.Folder{Name: common.Spam, Custom: false},
  477. }
  478. return
  479. }