pwman3 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. import os
  23. import os.path
  24. import argparse
  25. import sys
  26. _saveconfig = True
  27. parser = argparse.ArgumentParser(description=('pwman3 - a command line '
  28. 'password manager.'))
  29. parser.add_argument('-c', '--config', dest='cfile',
  30. default=os.path.expanduser("~/.pwman/config"),
  31. help='cofiguration file to read')
  32. parser.add_argument('-d', '--database', dest='dbase')
  33. parser.add_argument('-e', '--encryption', dest="algo",
  34. help=("Possible options are: AES(default), ARC2, ARC4, "
  35. "Blowfish, CAST, DES, DES3, IDEA, RC5"))
  36. parser.add_argument('-k', '--convert', dest='dbconvert',
  37. action='store_true', default=False,
  38. # os.path.expanduser('~/.pwman/pwman.db'),
  39. help=("Convert old DB format to version >= 0.4."
  40. " The database that will be converted is the"
  41. " one found in the config file, or the one given"
  42. " as command line argument.")
  43. )
  44. parser.add_argument('-t', '--test', help="Run pwman from current directory \
  45. without installation", action="store_true")
  46. args = parser.parse_args()
  47. if args.test:
  48. sys.path.insert(0, os.getcwd())
  49. from pwman.util.crypto import CryptoEngine
  50. from pwman import default_config, which
  51. if 'darwin' in sys.platform:
  52. from pwman.ui.mac import PwmanCliMacNew as PwmanCliNew
  53. OSX = True
  54. elif 'win' in sys.platform:
  55. from pwman.ui.win import PwmanCliWinNew as PwmanCliNew
  56. OSX = False
  57. else:
  58. from pwman.ui.cli import PwmanCliNew
  59. OSX = False
  60. from pwman.ui.tools import CLICallback
  61. import pwman.util.config as config
  62. import pwman.data.factory
  63. from pwman.data.convertdb import PwmanConvertDB
  64. try:
  65. config_dir = os.path.expanduser("~/.pwman")
  66. if not os.path.isdir(config_dir):
  67. os.mkdir(config_dir)
  68. if not os.path.exists(args.cfile):
  69. config.set_defaults(default_config)
  70. else:
  71. config.load(args.cfile)
  72. xselpath = config.get_value("Global", "xselpath")
  73. if not OSX:
  74. xselpath = which("xsel")
  75. config.set_value("Global", "xsel", xselpath)
  76. elif OSX:
  77. pbcopypath = which("pbcopy")
  78. config.set_value("Global", "xsel", pbcopypath)
  79. if 'win' in sys.platform:
  80. try:
  81. import colorama
  82. colorama.init()
  83. except ImportError:
  84. config.set_value("Global", "colors", 'no')
  85. if args.dbase:
  86. config.set_value("Database", "filename", args.dbase)
  87. _saveconfig = False
  88. if args.algo:
  89. config.set_value("Encryption", "algorithm", args.algo)
  90. _saveconfig = False
  91. # set umask before creating/opening any files
  92. umask = int(config.get_value("Global", "umask"))
  93. os.umask(umask)
  94. enc = CryptoEngine.get()
  95. dbtype = config.get_value("Database", "type")
  96. # if it is done here, we could do the following:
  97. # if db.ver == 0.4 :
  98. # db = pwman.data.factory.create(dbtyp, new_version)
  99. # else:
  100. # we use the old code untouched ... insecure, but
  101. # keeps backwards compatibility ...
  102. # if the database file exists check it's version
  103. # else: force version 0.4
  104. if os.path.exists(config.get_value("Database", "filename")):
  105. dbver = pwman.data.factory.check_db_version(dbtype)
  106. dbver = float(dbver.strip("\'"))
  107. else:
  108. dbver = 0.4
  109. # the method create could create an old instance that
  110. # accepts cPickle object or new style instance that
  111. # accepts only strings.
  112. # The user should be STRONGLY Prompted to CONVERT the
  113. # database to the new format using a command line tool.
  114. # version 0.5 pwman will depreciate that old and insecure
  115. # code ...
  116. if args.dbconvert:
  117. dbconvertor = PwmanConvertDB(args, config)
  118. status = dbconvertor.run()
  119. sys.exit(status)
  120. db = pwman.data.factory.create(dbtype, dbver)
  121. if dbver >= 0.4:
  122. cli = PwmanCliNew(db, xselpath, CLICallback)
  123. elif dbver < 0.4:
  124. cli = PwmanCli(db, xselpath)
  125. print cli._dbwarning
  126. sys.exit(0)
  127. except SystemExit, e:
  128. sys.exit(e)
  129. try:
  130. try:
  131. cli.cmdloop()
  132. except KeyboardInterrupt, e:
  133. print e
  134. finally:
  135. try:
  136. if _saveconfig:
  137. config.save(args.cfile)
  138. except Exception, e:
  139. print "Error: %s" % (e)
  140. sys.exit(-1)