__init__.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. appname = "pwman3"
  29. try:
  30. version = pkg_resources.get_distribution('pwman3').version
  31. except pkg_resources.DistributionNotFound: # pragma: no cover
  32. version = "0.5"
  33. website = "http://github.com/pwman3/pwman3"
  34. author = "Oz Nahum"
  35. authoremail = "nahumoz@gmail.com"
  36. description = "a command line password management application."
  37. keywords = "password management sqlite crypto"
  38. long_description = ("Pwman3 aims to provide a simple but powerful command line "
  39. "interface for password management.\nIt allows one to store your "
  40. "password in a SQLite database locked by a\nmaster password which "
  41. "can be encrypted with different algorithms (e.g AES, Blowfish, "
  42. "DES3, IDEA, etc.).")
  43. _db_warn = ("pwman3 detected that you are using the old database format"
  44. " which is insecure."
  45. " pwman3 will try to automatically convert the database now."
  46. "\n"
  47. "If you choose not to convert the database, pwman3, will quit."
  48. "\nYou can check the help (pwman3 -h) or look at the manpage how to convert "
  49. " the database manually."
  50. )
  51. def which(cmd):
  52. _, cmdname = os.path.split(cmd)
  53. for path in os.environ["PATH"].split(os.pathsep):
  54. cmd = os.path.join(path, cmdname)
  55. if os.path.isfile(cmd) and os.access(cmd, os.X_OK): # pragma: no cover
  56. return cmd
  57. config_dir = os.path.expanduser("~/.pwman")
  58. default_config = {'Global': {'umask': '0100', 'colors': 'yes',
  59. 'cls_timeout': '5',
  60. 'save': 'True'
  61. },
  62. 'Database': {'type': 'SQLite',
  63. 'filename': os.path.join(config_dir,
  64. "pwman.db")},
  65. 'Encryption': {'algorithm': 'AES'},
  66. 'Readline': {'history': os.path.join(config_dir,
  67. "history")}
  68. }
  69. def parser_options(formatter_class=argparse.HelpFormatter):
  70. parser = argparse.ArgumentParser(description=('pwman3 - a command line '
  71. 'password manager.'),
  72. formatter_class=formatter_class)
  73. parser.add_argument('-c', '--config', dest='cfile',
  74. default=os.path.expanduser("~/.pwman/config"),
  75. help='cofiguration file to read')
  76. parser.add_argument('-d', '--database', dest='dbase')
  77. parser.add_argument('-e', '--encryption', dest="algo",
  78. help=("Possible options are: AES(default), ARC2, ARC4,"
  79. " Blowfish, CAST, DES, DES3, IDEA, RC5"))
  80. parser.add_argument('-k', '--convert', dest='dbconvert',
  81. action='store_true', default=False,
  82. # os.path.expanduser('~/.pwman/pwman.db'),
  83. help=("Convert old DB format to version >= 0.4."
  84. " The database that will be converted is the"
  85. " one found in the config file, or the one given"
  86. " as command line argument."))
  87. parser.add_argument('-O', '--output', dest='output',
  88. #default=os.path.expanduser('~/.pwman/pwman-newdb.db'),
  89. help=("The name of the newly created database after "
  90. "converting."))
  91. return parser
  92. def get_conf(args):
  93. config_dir = os.path.expanduser("~/.pwman")
  94. if not os.path.isdir(config_dir):
  95. os.mkdir(config_dir)
  96. if not os.path.exists(args.cfile):
  97. config.set_defaults(default_config)
  98. else:
  99. config.load(args.cfile)
  100. return config
  101. def set_xsel(config, OSX):
  102. if not OSX:
  103. xselpath = which("xsel")
  104. config.set_value("Global", "xsel", xselpath)
  105. elif OSX:
  106. pbcopypath = which("pbcopy")
  107. config.set_value("Global", "xsel", pbcopypath)
  108. def set_win_colors(config):
  109. if 'win' in sys.platform:
  110. try:
  111. import colorama
  112. colorama.init()
  113. except ImportError:
  114. config.set_value("Global", "colors", 'no')
  115. def set_umask(config):
  116. # set umask before creating/opening any files
  117. try:
  118. umask = config.get_value("Global", "umask")
  119. if re.search(r'^\d{4}$', umask):
  120. os.umask(int(umask))
  121. else:
  122. raise ValueError
  123. except ValueError:
  124. print("Could not determine umask from config!")
  125. sys.exit(2)
  126. def set_db(args):
  127. if args.dbase:
  128. config.set_value("Database", "filename", args.dbase)
  129. config.set_value("Global", "save", "False")
  130. def set_algorithm(args, config):
  131. if args.algo:
  132. config.set_value("Encryption", "algorithm", args.algo)
  133. config.set_value("Global", "save", "False")
  134. def get_conf_options(args, OSX):
  135. config = get_conf(args)
  136. xselpath = config.get_value("Global", "xsel")
  137. if not xselpath:
  138. set_xsel(config, OSX)
  139. set_win_colors(config)
  140. set_db(args)
  141. set_umask(config)
  142. set_algorithm(args, config)
  143. dbtype = config.get_value("Database", "type")
  144. if not dbtype:
  145. print("Could not read the Database type from the config!")
  146. sys.exit(1)
  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 = 0.4
  155. return dbver