Advertisement
Guest User

Untitled

a guest
Apr 26th, 2021
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.82 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Copyright (c) 2019, Jozef Sudolsky, ELBIA, s. r. o.
  4. # All rights reserved.
  5. # BSD Licence
  6. #
  7. # Redistribution and use in source and binary forms, with or without
  8. # modification, are permitted provided that the following conditions are met:
  9. #
  10. #     1. Redistributions of source code must retain the above copyright notice,
  11. #        this list of conditions and the following disclaimer.
  12. #
  13. #     2. Redistributions in binary form must reproduce the above copyright
  14. #        notice, this list of conditions and the following disclaimer in the
  15. #        documentation and/or other materials provided with the distribution.
  16. #
  17. #     3. Neither the name of dovecot_control_files.py nor the names of its
  18. #        contributors may be used to endorse or promote products derived from
  19. #        this software without specific prior written permission.
  20. #
  21. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  25. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. # POSSIBILITY OF SUCH DAMAGE.
  32. ################################################################################
  33. # Dovecot's 'mail_location' path without formatting:
  34. # - only Maildir format is supported
  35. # - only this path formatting is supported: /path/%d/%n
  36. # - moving CONTROL files back to mail_location is NOT supported
  37. mail_location = "/var/mail/vhosts"
  38. # New path for CONTROL files without formatting, must be empty.
  39. new_control_location = "/flash/dovecot_control"
  40.  
  41. # One of:
  42. # - copy = only copy CONTROL files to new_control_location
  43. # - delete = only delete CONTROL files from mail_location (usefull when you first want to only copy files to see if everything is ok and delete them from mailboxes afterwards)
  44. # - move = copy + delete
  45. operation_mode = "copy"
  46.  
  47. # UID of newly created files/directories (usually UID of user postfix, see Dovecot configuration)
  48. uid = 102
  49. # GID of newly created files/directories (usually GID of group mail, see Dovecot configuration)
  50. gid = 8
  51. #################################STOP#HERE######################################
  52. # Tested with Dovecot 2.3.4.
  53. # Usage:
  54. # 1.) Stop Dovecot.
  55. # 2.) Check, if it's really stopped.
  56. # 3.) Re-check, if it's really stopped.
  57. # 4.) Run this script (see configuration above).
  58. # 5.) Check that everything is ok.
  59. # 6.) Update Dovecot's configuration and set CONTROL argument for mail_location setting (can be there multiple times, depends on your configuration).
  60. # 7.) Start Dovecot and pray for everything is ok.
  61. # 8.) ???
  62. # 9.) Profit.
  63. ################################################################################
  64. import os
  65. import sys
  66. import shutil
  67.  
  68. if __name__ == "__main__":
  69.     if not os.path.isdir(mail_location):
  70.         print("ERROR: 'mail_location' path doesn't exists")
  71.         sys.exit(1)
  72.     if not os.path.isdir(new_control_location) and operation_mode != "delete":
  73.         print("ERROR: 'new_control_location' path doesn't exists")
  74.         sys.exit(1)
  75.     if operation_mode not in ("copy", "delete", "move"):
  76.         print("ERROR: invalid 'operation_mode' setting")
  77.         sys.exit(1)
  78.     for i in os.listdir(mail_location):
  79.         if operation_mode in ("copy", "move"):
  80.             os.mkdir(os.path.join(new_control_location, i))
  81.             os.chown(os.path.join(new_control_location, i), uid, gid)
  82.             os.chmod(os.path.join(new_control_location, i), 0770)
  83.         for j in os.listdir(os.path.join(mail_location, i)):
  84.             if operation_mode in ("copy", "move"):
  85.                 os.mkdir(os.path.join(new_control_location, i, j))
  86.                 os.chown(os.path.join(new_control_location, i, j), uid, gid)
  87.                 os.chmod(os.path.join(new_control_location, i, j), 0770)
  88.                 os.mkdir(os.path.join(new_control_location, i, j, ".INBOX"))
  89.                 os.chown(os.path.join(new_control_location, i, j, ".INBOX"), uid, gid)
  90.                 os.chmod(os.path.join(new_control_location, i, j, ".INBOX"), 0770)
  91.             if os.path.isfile(os.path.join(mail_location, i, j, "dovecot-uidlist")):
  92.                 if operation_mode in ("copy", "move"):
  93.                     shutil.copy2(os.path.join(mail_location, i, j, "dovecot-uidlist"), os.path.join(new_control_location, i, j, ".INBOX", "dovecot-uidlist"))
  94.                     s = os.stat(os.path.join(mail_location, i, j, "dovecot-uidlist"))
  95.                     os.chown(os.path.join(new_control_location, i, j, ".INBOX", "dovecot-uidlist"), s.st_uid, s.st_gid)
  96.                 if operation_mode in ("delete", "move"):
  97.                     os.remove(os.path.join(mail_location, i, j, "dovecot-uidlist"))
  98.             if os.path.isfile(os.path.join(mail_location, i, j, "dovecot-keywords")):
  99.                 if operation_mode in ("copy", "move"):
  100.                     shutil.copy2(os.path.join(mail_location, i, j, "dovecot-keywords"), os.path.join(new_control_location, i, j, ".INBOX", "dovecot-keywords"))
  101.                     s = os.stat(os.path.join(mail_location, i, j, "dovecot-keywords"))
  102.                     os.chown(os.path.join(new_control_location, i, j, ".INBOX", "dovecot-keywords"), s.st_uid, s.st_gid)
  103.                 if operation_mode in ("delete", "move"):
  104.                     os.remove(os.path.join(mail_location, i, j, "dovecot-keywords"))
  105.             if os.path.isfile(os.path.join(mail_location, i, j, "subscriptions")):
  106.                 if operation_mode in ("copy", "move"):
  107.                     shutil.copy2(os.path.join(mail_location, i, j, "subscriptions"), os.path.join(new_control_location, i, j, "subscriptions"))
  108.                     s = os.stat(os.path.join(mail_location, i, j, "subscriptions"))
  109.                     os.chown(os.path.join(new_control_location, i, j, "subscriptions"), s.st_uid, s.st_gid)
  110.                 if operation_mode in ("delete", "move"):
  111.                     os.remove(os.path.join(mail_location, i, j, "subscriptions"))
  112.             if os.path.isfile(os.path.join(mail_location, i, j, "dovecot-uidvalidity")):
  113.                 if operation_mode in ("copy", "move"):
  114.                     shutil.copy2(os.path.join(mail_location, i, j, "dovecot-uidvalidity"), os.path.join(new_control_location, i, j, "dovecot-uidvalidity"))
  115.                     s = os.stat(os.path.join(mail_location, i, j, "dovecot-uidvalidity"))
  116.                     os.chown(os.path.join(new_control_location, i, j, "dovecot-uidvalidity"), s.st_uid, s.st_gid)
  117.                 f = open(os.path.join(mail_location, i, j, "dovecot-uidvalidity"), "r")
  118.                 d = f.read().strip()
  119.                 f.close()
  120.                 if operation_mode in ("delete", "move"):
  121.                     os.remove(os.path.join(mail_location, i, j, "dovecot-uidvalidity"))
  122.                 if os.path.isfile(os.path.join(mail_location, i, j, "dovecot-uidvalidity.%s" % d)):
  123.                     if operation_mode in ("copy", "move"):
  124.                         shutil.copy2(os.path.join(mail_location, i, j, "dovecot-uidvalidity.%s" % d), os.path.join(new_control_location, i, j, "dovecot-uidvalidity.%s" % d))
  125.                         s = os.stat(os.path.join(mail_location, i, j, "dovecot-uidvalidity.%s" % d))
  126.                         os.chown(os.path.join(new_control_location, i, j, "dovecot-uidvalidity.%s" % d), s.st_uid, s.st_gid)
  127.                     if operation_mode in ("delete", "move"):
  128.                         os.remove(os.path.join(mail_location, i, j, "dovecot-uidvalidity.%s" % d))
  129.             if not os.path.isdir(os.path.join(mail_location, i, j)):
  130.                 continue
  131.             for k in os.listdir(os.path.join(mail_location, i, j)):
  132.                 if k[0] == "." and os.path.isdir(os.path.join(mail_location, i, j, k)):
  133.                     if operation_mode in ("copy", "move"):
  134.                         os.mkdir(os.path.join(new_control_location, i, j, k))
  135.                         os.chown(os.path.join(new_control_location, i, j, k), uid, gid)
  136.                         os.chmod(os.path.join(new_control_location, i, j, k), 0770)
  137.                     if os.path.isfile(os.path.join(mail_location, i, j, k, "dovecot-uidlist")):
  138.                         if operation_mode in ("copy", "move"):
  139.                             shutil.copy2(os.path.join(mail_location, i, j, k, "dovecot-uidlist"), os.path.join(new_control_location, i, j, k, "dovecot-uidlist"))
  140.                             s = os.stat(os.path.join(mail_location, i, j, k, "dovecot-uidlist"))
  141.                             os.chown(os.path.join(new_control_location, i, j, k, "dovecot-uidlist"), s.st_uid, s.st_gid)
  142.                         if operation_mode in ("delete", "move"):
  143.                             os.remove(os.path.join(mail_location, i, j, k, "dovecot-uidlist"))
  144.                     if os.path.isfile(os.path.join(mail_location, i, j, k, "dovecot-keywords")):
  145.                         if operation_mode in ("copy", "move"):
  146.                             shutil.copy2(os.path.join(mail_location, i, j, k, "dovecot-keywords"), os.path.join(new_control_location, i, j, k, "dovecot-keywords"))
  147.                             s = os.stat(os.path.join(mail_location, i, j, k, "dovecot-keywords"))
  148.                             os.chown(os.path.join(new_control_location, i, j, k, "dovecot-keywords"), s.st_uid, s.st_gid)
  149.                         if operation_mode in ("delete", "move"):
  150.                             os.remove(os.path.join(mail_location, i, j, k, "dovecot-keywords"))
  151.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement