pwman3 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. _saveconfig = True
  25. import argparse
  26. parser = argparse.ArgumentParser(description='pwman3 - a command line password'
  27. + ' manager.')
  28. parser.add_argument('-c', '--config', dest='cfile',
  29. default=os.path.expanduser("~/.pwman/config"),
  30. help='cofiguration file to read')
  31. parser.add_argument('-d', '--database', dest='dbase')
  32. parser.add_argument('-e', '--encryption', dest="algo",
  33. help="Possible options are: AES(default), ARC2, ARC4, "
  34. + "Blowfish, CAST, DES, DES3, IDEA, RC5")
  35. parser.add_argument('-k', '--convert', dest='dbconvert',
  36. action='store_true', default=False,
  37. # os.path.expanduser('~/.pwman/pwman.db'),
  38. help="Convert old DB format to Version >= 0.4.",
  39. )
  40. parser.add_argument('-t', '--test', help="Run pwman from current directory \
  41. without installation", action="store_true")
  42. args = parser.parse_args()
  43. import sys
  44. if args.test:
  45. sys.path.insert(0, os.getcwd())
  46. from pwman.util.crypto import CryptoEngine
  47. if 'darwin' in sys.platform:
  48. from pwman.ui.cli import PwmanCliMac as PwmanCli
  49. from pwman.ui.cli import PwmanCliMacNew as PwmanCliNew
  50. OSX = True
  51. else:
  52. from pwman.ui.cli import PwmanCli
  53. from pwman.ui.cli import PwmanCliNew
  54. OSX = False
  55. import pwman.util.config as config
  56. import pwman.data.factory
  57. from pwman.data.convertdb import PwmanConvertDB
  58. config_file = args.cfile
  59. def which(cmd):
  60. _, cmdname = os.path.split(cmd)
  61. for path in os.environ["PATH"].split(os.pathsep):
  62. cmd = os.path.join(path, cmdname)
  63. if os.path.isfile(cmd) and os.access(cmd, os.X_OK):
  64. return cmd
  65. return None
  66. try:
  67. config_dir = os.path.expanduser("~/.pwman")
  68. if not os.path.isdir(config_dir):
  69. os.mkdir(config_dir)
  70. config_file = os.path.join(config_dir, "config")
  71. # set cls_timout to negative number (e.g. -1) to disable
  72. default_config = {'Global': {'umask': '0100', 'colors': 'yes',
  73. 'cls_timeout': '5'
  74. },
  75. 'Database': {'type': 'SQLite',
  76. 'filename': os.path.join(config_dir,
  77. "pwman.db")},
  78. 'Encryption': {'algorithm': 'AES'},
  79. 'Readline': {'history': os.path.join(config_dir,
  80. "history")}
  81. }
  82. config.set_defaults(default_config)
  83. if os.path.exists(config_file):
  84. config.load(config_file)
  85. xselpath = config.get_value("Global", "xselpath")
  86. elif not OSX:
  87. xselpath = which("xsel")
  88. config.set_value("Global", "xsel", xselpath)
  89. elif OSX:
  90. pbcopypath = which("pbcopy")
  91. config.set_value("Global", "xsel", pbcopypath)
  92. if args.dbase:
  93. config.set_value("Database", "filename", args.dbase)
  94. _saveconfig = False
  95. if args.algo:
  96. config.set_value("Encryption", "algorithm", args.algo)
  97. _saveconfig = False
  98. # set umask before creating/opening any files
  99. umask = int(config.get_value("Global", "umask"))
  100. os.umask(umask)
  101. enc = CryptoEngine.get()
  102. dbtype = config.get_value("Database", "type")
  103. # if it is done here, we could do the following:
  104. # if db.ver == 0.4 :
  105. # db = pwman.data.factory.create(dbtyp, new_version)
  106. # else:
  107. # we use the old code untouched ... insecure, but
  108. # keeps backwards compatibility ...
  109. # if the database file exists check it's version
  110. # else: force version 0.4
  111. if os.path.exists(config.get_value("Database", "filename")):
  112. dbver = pwman.data.factory.check_db_version(dbtype)
  113. dbver = float(dbver.strip("\'"))
  114. else:
  115. dbver = 0.4
  116. # the method create could create an old instance that
  117. # accepts cPickle object or new style instance that
  118. # accepts only strings.
  119. # The user should be STRONGLY Prompted to CONVERT the
  120. # database to the new format using a command line tool.
  121. # version 0.5 pwman will depreciate that old and insecure
  122. # code ...
  123. if args.dbconvert:
  124. dbconvertor = PwmanConvertDB(args, config)
  125. status = dbconvertor.run()
  126. sys.exit(status)
  127. db = pwman.data.factory.create(dbtype, dbver)
  128. if dbver >= 0.4:
  129. cli = PwmanCliNew(db, xselpath)
  130. elif dbver < 0.4:
  131. cli = PwmanCli(db, xselpath)
  132. except SystemExit, e:
  133. sys.exit(e)
  134. try:
  135. try:
  136. cli.cmdloop()
  137. except KeyboardInterrupt, e:
  138. print e
  139. finally:
  140. try:
  141. if _saveconfig:
  142. config.save(config_file)
  143. except Exception, e:
  144. print "Error: %s" % (e)
  145. sys.exit(-1)