pwman3 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #!/usr/bin/env python
  2. #============================================================================
  3. # This file is part of Pwman3.
  4. #
  5. # Pwman3 is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License, version 2
  7. # as published by the Free Software Foundation;
  8. #
  9. # Pwman3 is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with Pwman3; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. #============================================================================
  18. # Copyright (C) 2012 Oz Nahum <nahumoz@gmail.com>
  19. #============================================================================
  20. # Copyright (C) 2006 Ivan Kelly <ivan@ivankelly.net>
  21. #============================================================================
  22. from __future__ import print_function
  23. import os
  24. import sys
  25. import re
  26. import shutil
  27. from pwman import default_config, which
  28. from pwman import parser_options
  29. from pwman.ui import get_ui_platform
  30. from pwman.ui.tools import CLICallback
  31. import pwman.util.config as config
  32. import pwman.data.factory
  33. from pwman.data.convertdb import PwmanConvertDB
  34. from pwman.util.crypto import CryptoEngine
  35. _db_warn = ("pwman3 detected that you are using the old database format"
  36. " which is insecure."
  37. " pwman3 will try to automatically convert the database now."
  38. "\n"
  39. "If you choose not to convert the database, pwman3, will quit."
  40. "\nYou can check the help (pwman3 -h) or look at the manpage how to convert "
  41. " the database manually."
  42. )
  43. def get_conf(args):
  44. config_dir = os.path.expanduser("~/.pwman")
  45. if not os.path.isdir(config_dir):
  46. os.mkdir(config_dir)
  47. if not os.path.exists(args.cfile):
  48. config.set_defaults(default_config)
  49. else:
  50. config.load(args.cfile)
  51. return config
  52. def set_xsel(config, OSX):
  53. if not OSX:
  54. xselpath = which("xsel")
  55. config.set_value("Global", "xsel", xselpath)
  56. elif OSX:
  57. pbcopypath = which("pbcopy")
  58. config.set_value("Global", "xsel", pbcopypath)
  59. def set_win_colors(config):
  60. if 'win' in sys.platform:
  61. try:
  62. import colorama
  63. colorama.init()
  64. except ImportError:
  65. config.set_value("Global", "colors", 'no')
  66. def set_umask(config):
  67. # set umask before creating/opening any files
  68. try:
  69. umask = config.get_value("Global", "umask")
  70. if re.search(r'^\d{4}$', umask):
  71. os.umask(int(umask))
  72. else:
  73. raise ValueError
  74. except ValueError:
  75. print("Could not determine umask from config!")
  76. sys.exit(2)
  77. def set_db(args):
  78. if args.dbase:
  79. config.set_value("Database", "filename", args.dbase)
  80. config.set_value("Global", "save", "False")
  81. def set_algorithm(args, config):
  82. if args.algo:
  83. config.set_value("Encryption", "algorithm", args.algo)
  84. config.set_value("Global", "save", "False")
  85. def get_conf_options(args, OSX):
  86. config = get_conf(args)
  87. xselpath = config.get_value("Global", "xsel")
  88. if not xselpath:
  89. set_xsel(config, OSX)
  90. set_win_colors(config)
  91. set_db(args)
  92. set_umask(config)
  93. set_algorithm(args, config)
  94. dbtype = config.get_value("Database", "type")
  95. if not dbtype:
  96. print("Could not read the Database type from the config!")
  97. sys.exit(1)
  98. return xselpath, dbtype
  99. def get_db_version(config, dbtype, args):
  100. if os.path.exists(config.get_value("Database", "filename")):
  101. dbver = pwman.data.factory.check_db_version(dbtype)
  102. if dbver < 0.4 and not args.dbconvert:
  103. print(_db_warn)
  104. else:
  105. dbver = 0.4
  106. return dbver
  107. def auto_convert():
  108. try:
  109. #1) Display a message saying that the database will be converted
  110. # This step is done already in get_db_version
  111. dbconvertor = PwmanConvertDB(args, config)
  112. #2) copy the old database : cp ~/.pwman3/pwman.db ~/.pwman3/pwman.backup-2013-11-23-23:15.db
  113. #3) Display a message about the backup file path
  114. # These steps are done by PwmanConvertDB.backup_old_db()
  115. dbconvertor.backup_old_db()
  116. #4) convert the old database to the new format in ~/.pwman3/pwman.db
  117. # This step is done by PwmanConvertDB.create_new_db()
  118. # PwmanConvertDB.convert_nodes()
  119. # PwmanConvertDB.save_new_nodes_to_db()
  120. # PwmanConvertDB.save_old_key()
  121. dbconvertor.read_old_db()
  122. dbconvertor.create_new_db()
  123. dbconvertor.convert_nodes()
  124. dbconvertor.save_new_nodes_to_db()
  125. dbconvertor.save_old_key()
  126. #5) Display a message about the result of the conversion
  127. # add message here ...
  128. #5b) close connection
  129. dbconvertor.db.close()
  130. print("Your datbase %s was successfully converted " % dbconvertor.dbname)
  131. #5c) rename the newly created db to the old name!
  132. shutil.move(dbconvertor.newdb_name, dbconvertor.dbname)
  133. #6) Start the pwman3 normally if all went ok
  134. return True
  135. except Exception, e:
  136. raise e
  137. def main(args):
  138. PwmanCliNew, OSX = get_ui_platform(sys.platform)
  139. xselpath, dbtype = get_conf_options(args, OSX)
  140. enc = CryptoEngine.get()
  141. dbver = get_db_version(config, dbtype, args)
  142. if args.dbconvert:
  143. dbconvertor = PwmanConvertDB(args, config)
  144. dbconvertor.run()
  145. sys.exit(0)
  146. if dbver < 0.4:
  147. ans = raw_input("Would you like to proceed with the conversion of the database"
  148. " before starting pwman3 [y/N]?")
  149. if ans.lower() != "y":
  150. sys.exit(1)
  151. auto_convert()
  152. dbver = 0.4
  153. db = pwman.data.factory.create(dbtype, dbver)
  154. cli = PwmanCliNew(db, xselpath, CLICallback)
  155. try:
  156. cli.cmdloop()
  157. except KeyboardInterrupt, e:
  158. print(e)
  159. if config.get_value("Global", "save") == "True":
  160. try:
  161. config.save(args.cfile)
  162. except Exception, e:
  163. print ("Error: %s" % e)
  164. sys.exit(-1)
  165. if __name__ == '__main__':
  166. args = parser_options().parse_args()
  167. main(args)