pwman3 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 argparse
  25. import sys
  26. import re
  27. _saveconfig = True
  28. _db_warn = ("\n*** WARNNING: You are using the old database format"
  29. " which is insecure."
  30. " Please upgrade to the new database "
  31. " format. Do note: support for this DB format will be dropped in"
  32. " v0.5. This database format is on hold. No bugs are fixead"
  33. " Check the help (pwman3 -h) or look at the manpage which"
  34. " explains how to proceed. ***")
  35. def parser_options():
  36. parser = argparse.ArgumentParser(description=('pwman3 - a command line '
  37. 'password manager.'))
  38. parser.add_argument('-c', '--config', dest='cfile',
  39. default=os.path.expanduser("~/.pwman/config"),
  40. help='cofiguration file to read')
  41. parser.add_argument('-d', '--database', dest='dbase')
  42. parser.add_argument('-e', '--encryption', dest="algo",
  43. help=("Possible options are: AES(default), ARC2, ARC4,"
  44. " Blowfish, CAST, DES, DES3, IDEA, RC5"))
  45. parser.add_argument('-k', '--convert', dest='dbconvert',
  46. action='store_true', default=False,
  47. # os.path.expanduser('~/.pwman/pwman.db'),
  48. help=("Convert old DB format to version >= 0.4."
  49. " The database that will be converted is the"
  50. " one found in the config file, or the one given"
  51. " as command line argument."))
  52. parser.add_argument('-t', '--test', help=("Run pwman from current"
  53. " directory"
  54. " without installation"),
  55. action="store_true")
  56. return parser
  57. def get_conf():
  58. config_dir = os.path.expanduser("~/.pwman")
  59. if not os.path.isdir(config_dir):
  60. os.mkdir(config_dir)
  61. if not os.path.exists(args.cfile):
  62. config.set_defaults(default_config)
  63. else:
  64. config.load(args.cfile)
  65. return config
  66. def set_xsel(config, OSX):
  67. if not OSX:
  68. xselpath = which("xsel")
  69. config.set_value("Global", "xsel", xselpath)
  70. elif OSX:
  71. pbcopypath = which("pbcopy")
  72. config.set_value("Global", "xsel", pbcopypath)
  73. def set_win_colors(config):
  74. if 'win' in sys.platform:
  75. try:
  76. import colorama
  77. colorama.init()
  78. except ImportError:
  79. config.set_value("Global", "colors", 'no')
  80. def set_umask(config):
  81. # set umask before creating/opening any files
  82. try:
  83. umask = config.get_value("Global", "umask")
  84. if re.search(r'^\d{4}$', umask):
  85. os.umask(int(umask))
  86. else:
  87. raise ValueError
  88. except ValueError:
  89. print("Could not determine umask from config!")
  90. sys.exit(2)
  91. def set_db(args):
  92. global _saveconfig
  93. if args.dbase:
  94. config.set_value("Database", "filename", args.dbase)
  95. _saveconfig = False
  96. def set_algorithm(args):
  97. global _saveconfig
  98. if args.algo:
  99. config.set_value("Encryption", "algorithm", args.algo)
  100. _saveconfig = False
  101. def get_conf_options(args, OSX):
  102. config = get_conf()
  103. xselpath = config.get_value("Global", "xselpath")
  104. if not xselpath:
  105. set_xsel(config, OSX)
  106. set_win_colors(config)
  107. set_db(args)
  108. set_umask(config)
  109. dbtype = config.get_value("Database", "type")
  110. if not dbtype:
  111. print("Could not read the Database type from the config!")
  112. sys.exit(1)
  113. return xselpath, dbtype
  114. def get_db_version(config, dbtype):
  115. if os.path.exists(config.get_value("Database", "filename")):
  116. dbver = pwman.data.factory.check_db_version(dbtype)
  117. if dbver < 0.4:
  118. print(_db_warn)
  119. sys.exit(1)
  120. else:
  121. dbver = 0.4
  122. return dbver
  123. def main(args):
  124. PwmanCliNew, OSX = get_ui_platform(sys.platform)
  125. xselpath, dbtype = get_conf_options(args, OSX)
  126. enc = CryptoEngine.get()
  127. dbver = get_db_version(config, dbtype)
  128. if args.dbconvert:
  129. dbconvertor = PwmanConvertDB(args, config)
  130. status = dbconvertor.run()
  131. sys.exit(status)
  132. db = pwman.data.factory.create(dbtype, dbver)
  133. cli = PwmanCliNew(db, xselpath, CLICallback)
  134. try:
  135. cli.cmdloop()
  136. except KeyboardInterrupt, e:
  137. print(e)
  138. if _saveconfig:
  139. try:
  140. config.save(args.cfile)
  141. except Exception, e:
  142. print ("Error: %s" % e)
  143. sys.exit(-1)
  144. if __name__ == '__main__':
  145. args = parser_options().parse_args()
  146. if args.test:
  147. sys.path.insert(0, os.getcwd())
  148. from pwman import default_config, which
  149. from pwman.ui import get_ui_platform
  150. from pwman.ui.tools import CLICallback
  151. import pwman.util.config as config
  152. import pwman.data.factory
  153. from pwman.data.convertdb import PwmanConvertDB
  154. from pwman.util.crypto import CryptoEngine
  155. main(args)