pwman3 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 PwmanCliMac as PwmanCli
  53. from pwman.ui.mac import PwmanCliMacNew as PwmanCliNew
  54. OSX = True
  55. elif 'win' in sys.platform:
  56. from pwman.ui.cli import PwmanCli
  57. from pwman.ui.win import PwmanCliWinNew as PwmanCliNew
  58. OSX = False
  59. else:
  60. from pwman.ui.cli import PwmanCliOld as PwmanCli
  61. from pwman.ui.cli import PwmanCliNew
  62. OSX = False
  63. from pwman.ui.tools import CLICallback
  64. import pwman.util.config as config
  65. import pwman.data.factory
  66. from pwman.data.convertdb import PwmanConvertDB
  67. try:
  68. config_dir = os.path.expanduser("~/.pwman")
  69. if not os.path.isdir(config_dir):
  70. os.mkdir(config_dir)
  71. if not os.path.exists(args.cfile):
  72. config.set_defaults(default_config)
  73. else:
  74. config.load(args.cfile)
  75. xselpath = config.get_value("Global", "xselpath")
  76. if not OSX:
  77. xselpath = which("xsel")
  78. config.set_value("Global", "xsel", xselpath)
  79. elif OSX:
  80. pbcopypath = which("pbcopy")
  81. config.set_value("Global", "xsel", pbcopypath)
  82. if 'win' in sys.platform:
  83. try:
  84. import colorama
  85. colorama.init()
  86. except ImportError:
  87. config.set_value("Global", "colors", 'no')
  88. if args.dbase:
  89. config.set_value("Database", "filename", args.dbase)
  90. _saveconfig = False
  91. if args.algo:
  92. config.set_value("Encryption", "algorithm", args.algo)
  93. _saveconfig = False
  94. # set umask before creating/opening any files
  95. umask = int(config.get_value("Global", "umask"))
  96. os.umask(umask)
  97. enc = CryptoEngine.get()
  98. dbtype = config.get_value("Database", "type")
  99. # if it is done here, we could do the following:
  100. # if db.ver == 0.4 :
  101. # db = pwman.data.factory.create(dbtyp, new_version)
  102. # else:
  103. # we use the old code untouched ... insecure, but
  104. # keeps backwards compatibility ...
  105. # if the database file exists check it's version
  106. # else: force version 0.4
  107. if os.path.exists(config.get_value("Database", "filename")):
  108. dbver = pwman.data.factory.check_db_version(dbtype)
  109. dbver = float(dbver.strip("\'"))
  110. else:
  111. dbver = 0.4
  112. # the method create could create an old instance that
  113. # accepts cPickle object or new style instance that
  114. # accepts only strings.
  115. # The user should be STRONGLY Prompted to CONVERT the
  116. # database to the new format using a command line tool.
  117. # version 0.5 pwman will depreciate that old and insecure
  118. # code ...
  119. if args.dbconvert:
  120. dbconvertor = PwmanConvertDB(args, config)
  121. status = dbconvertor.run()
  122. sys.exit(status)
  123. db = pwman.data.factory.create(dbtype, dbver)
  124. if dbver >= 0.4:
  125. cli = PwmanCliNew(db, xselpath, CLICallback)
  126. elif dbver < 0.4:
  127. cli = PwmanCli(db, xselpath)
  128. except SystemExit, e:
  129. sys.exit(e)
  130. try:
  131. try:
  132. cli.cmdloop()
  133. except KeyboardInterrupt, e:
  134. print e
  135. finally:
  136. try:
  137. if _saveconfig:
  138. config.save(args.cfile)
  139. except Exception, e:
  140. print "Error: %s" % (e)
  141. sys.exit(-1)