__init__.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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():
  70. parser = argparse.ArgumentParser(description=('pwman3 - a command line '
  71. 'password manager.'))
  72. parser.add_argument('-c', '--config', dest='cfile',
  73. default=os.path.expanduser("~/.pwman/config"),
  74. help='cofiguration file to read')
  75. parser.add_argument('-d', '--database', dest='dbase')
  76. parser.add_argument('-e', '--encryption', dest="algo",
  77. help=("Possible options are: AES(default), ARC2, ARC4,"
  78. " Blowfish, CAST, DES, DES3, IDEA, RC5"))
  79. parser.add_argument('-k', '--convert', dest='dbconvert',
  80. action='store_true', default=False,
  81. # os.path.expanduser('~/.pwman/pwman.db'),
  82. help=("Convert old DB format to version >= 0.4."
  83. " The database that will be converted is the"
  84. " one found in the config file, or the one given"
  85. " as command line argument."))
  86. parser.add_argument('-O', '--output', dest='output',
  87. #default=os.path.expanduser('~/.pwman/pwman-newdb.db'),
  88. help=("The name of the newly created database after "
  89. "converting."))
  90. return parser
  91. def get_conf(args):
  92. config_dir = os.path.expanduser("~/.pwman")
  93. if not os.path.isdir(config_dir):
  94. os.mkdir(config_dir)
  95. if not os.path.exists(args.cfile):
  96. config.set_defaults(default_config)
  97. else:
  98. config.load(args.cfile)
  99. return config
  100. def set_xsel(config, OSX):
  101. if not OSX:
  102. xselpath = which("xsel")
  103. config.set_value("Global", "xsel", xselpath)
  104. elif OSX:
  105. pbcopypath = which("pbcopy")
  106. config.set_value("Global", "xsel", pbcopypath)
  107. def set_win_colors(config):
  108. if 'win' in sys.platform:
  109. try:
  110. import colorama
  111. colorama.init()
  112. except ImportError:
  113. config.set_value("Global", "colors", 'no')
  114. def set_umask(config):
  115. # set umask before creating/opening any files
  116. try:
  117. umask = config.get_value("Global", "umask")
  118. if re.search(r'^\d{4}$', umask):
  119. os.umask(int(umask))
  120. else:
  121. raise ValueError
  122. except ValueError:
  123. print("Could not determine umask from config!")
  124. sys.exit(2)
  125. def set_db(args):
  126. if args.dbase:
  127. config.set_value("Database", "filename", args.dbase)
  128. config.set_value("Global", "save", "False")
  129. def set_algorithm(args, config):
  130. if args.algo:
  131. config.set_value("Encryption", "algorithm", args.algo)
  132. config.set_value("Global", "save", "False")
  133. def get_conf_options(args, OSX):
  134. config = get_conf(args)
  135. xselpath = config.get_value("Global", "xsel")
  136. if not xselpath:
  137. set_xsel(config, OSX)
  138. set_win_colors(config)
  139. set_db(args)
  140. set_umask(config)
  141. set_algorithm(args, config)
  142. dbtype = config.get_value("Database", "type")
  143. if not dbtype:
  144. print("Could not read the Database type from the config!")
  145. sys.exit(1)
  146. return xselpath, dbtype
  147. def get_db_version(config, dbtype, args):
  148. if os.path.exists(config.get_value("Database", "filename")):
  149. dbver = data.factory.check_db_version(dbtype)
  150. if dbver < 0.4 and not args.dbconvert:
  151. print(_db_warn)
  152. else:
  153. dbver = 0.4
  154. return dbver