__init__.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #============================================================================
  2. # This file is part of Pwman3.
  3. #
  4. # Pwman3 is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License, version 2
  6. # as published by the Free Software Foundation;
  7. #
  8. # Pwman3 is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with Pwman3; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. #============================================================================
  17. # Copyright (C) 2012 Oz Nahum <nahumoz@gmail.com>
  18. #============================================================================
  19. # Copyright (C) 2006 Ivan Kelly <ivan@ivankelly.net>
  20. #============================================================================
  21. import os
  22. import pkg_resources
  23. import argparse
  24. from util import config
  25. import sys
  26. import re
  27. import data.factory
  28. from pwman.data.database import __DB_FORMAT__
  29. appname = "pwman3"
  30. try:
  31. version = pkg_resources.get_distribution('pwman3').version
  32. except pkg_resources.DistributionNotFound: # pragma: no cover
  33. version = "0.5"
  34. website = "http://github.com/pwman3/pwman3"
  35. author = "Oz Nahum"
  36. authoremail = "nahumoz@gmail.com"
  37. description = "a command line password management application."
  38. keywords = "password management sqlite crypto"
  39. long_description = ("Pwman3 aims to provide a simple but powerful command line "
  40. "interface for password management.\nIt allows one to store your "
  41. "password in a SQLite database locked by a\nmaster password which "
  42. "can be encrypted with different algorithms (e.g AES, Blowfish, "
  43. "DES3, IDEA, etc.).")
  44. _db_warn = ("pwman3 detected that you are using the old database format"
  45. " which is insecure."
  46. " pwman3 will try to automatically convert the database now."
  47. "\n"
  48. "If you choose not to convert the database, pwman3, will quit."
  49. "\nYou can check the help (pwman3 -h) or look at the manpage how to convert "
  50. " the database manually."
  51. )
  52. def which(cmd):
  53. _, cmdname = os.path.split(cmd)
  54. for path in os.environ["PATH"].split(os.pathsep):
  55. cmd = os.path.join(path, cmdname)
  56. if os.path.isfile(cmd) and os.access(cmd, os.X_OK): # pragma: no cover
  57. return cmd
  58. config_dir = os.path.expanduser("~/.pwman")
  59. default_config = {'Global': {'umask': '0100', 'colors': 'yes',
  60. 'cls_timeout': '5',
  61. 'save': 'True'
  62. },
  63. 'Database': {'type': 'SQLite',
  64. 'filename': os.path.join(config_dir,
  65. "pwman.db")},
  66. 'Encryption': {'algorithm': 'AES'},
  67. 'Readline': {'history': os.path.join(config_dir,
  68. "history")}
  69. }
  70. def parser_options(formatter_class=argparse.HelpFormatter):
  71. parser = argparse.ArgumentParser(prog=appname,
  72. description=description,
  73. formatter_class=formatter_class)
  74. parser.add_argument('-c', '--config', dest='cfile',
  75. default=os.path.expanduser("~/.pwman/config"),
  76. help='cofiguration file to read')
  77. parser.add_argument('-d', '--database', dest='dbase')
  78. parser.add_argument('-e', '--encryption', dest="algo",
  79. help=("Possible options are: AES(default), ARC2, ARC4,"
  80. " Blowfish, CAST, DES, DES3, IDEA, RC5"))
  81. parser.add_argument('-k', '--convert', dest='dbconvert',
  82. action='store_true', default=False,
  83. # os.path.expanduser('~/.pwman/pwman.db'),
  84. help=("Convert old DB format to version >= 0.4."
  85. " The database that will be converted is the"
  86. " one found in the config file, or the one given"
  87. " as command line argument."))
  88. parser.add_argument('-O', '--output', dest='output',
  89. #default=os.path.expanduser('~/.pwman/pwman-newdb.db'),
  90. help=("The name of the newly created database after "
  91. "converting."))
  92. return parser
  93. def get_conf_file(args):
  94. config_dir = os.path.expanduser("~/.pwman")
  95. if not os.path.isdir(config_dir):
  96. os.mkdir(config_dir)
  97. if not os.path.exists(args.cfile):
  98. config.set_defaults(default_config)
  99. else:
  100. config.load(args.cfile)
  101. return config
  102. def set_xsel(config, OSX):
  103. if not OSX:
  104. xselpath = which("xsel")
  105. config.set_value("Global", "xsel", xselpath)
  106. elif OSX:
  107. pbcopypath = which("pbcopy")
  108. config.set_value("Global", "xsel", pbcopypath)
  109. def set_win_colors(config):
  110. if 'win' in sys.platform:
  111. try:
  112. import colorama
  113. colorama.init()
  114. except ImportError:
  115. config.set_value("Global", "colors", 'no')
  116. def set_umask(config):
  117. # set umask before creating/opening any files
  118. try:
  119. umask = config.get_value("Global", "umask")
  120. if re.search(r'^\d{4}$', umask):
  121. os.umask(int(umask))
  122. else:
  123. raise ValueError
  124. except ValueError:
  125. print("Could not determine umask from config!")
  126. sys.exit(2)
  127. def set_db(args):
  128. if args.dbase:
  129. config.set_value("Database", "filename", args.dbase)
  130. config.set_value("Global", "save", "False")
  131. def set_algorithm(args, config):
  132. if args.algo:
  133. config.set_value("Encryption", "algorithm", args.algo)
  134. config.set_value("Global", "save", "False")
  135. def get_conf_options(args, OSX):
  136. config = get_conf_file(args)
  137. xselpath = config.get_value("Global", "xsel")
  138. if not xselpath:
  139. set_xsel(config, OSX)
  140. set_win_colors(config)
  141. set_db(args)
  142. set_umask(config)
  143. set_algorithm(args, config)
  144. dbtype = config.get_value("Database", "type")
  145. if not dbtype:
  146. raise Exception("Could not read the Database type from the config!")
  147. return xselpath, dbtype
  148. def get_db_version(config, dbtype, args):
  149. if os.path.exists(config.get_value("Database", "filename")):
  150. dbver = data.factory.check_db_version(dbtype)
  151. if dbver < 0.4 and not args.dbconvert:
  152. print(_db_warn)
  153. else:
  154. dbver = __DB_FORMAT__
  155. return dbver