pwman3 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/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. #============================================================================
  19. # Copyright (C) 2012 Oz Nahum <nahumoz@gmail.com>
  20. #============================================================================
  21. # Copyright (C) 2006 Ivan Kelly <ivan@ivankelly.net>
  22. #============================================================================
  23. import os
  24. import os.path
  25. import subprocess as sp
  26. _saveconfig = True
  27. import argparse
  28. parser = argparse.ArgumentParser(description='pwman3 - a command line password'\
  29. +'manager.')
  30. parser.add_argument('-c','--config', dest='cfile',
  31. default=os.path.expanduser("~/.pwman/config"),
  32. help='cofiguretion file to read')
  33. parser.add_argument('-d', '--database', dest='dbase',
  34. default=os.path.expanduser("~/.pwman/pwman.db"))
  35. parser.add_argument('-e', '--encryption', dest="algo", default="Blowfish",
  36. help="Possible options are: AES, ARC2, ARC4, "\
  37. +"Blowfish(default) CAST, DES, DES3, IDEA, RC5")
  38. parser.add_argument('-t','--test', help="Run pwman from current directory \
  39. without installation", action="store_true")
  40. args = parser.parse_args()
  41. if args.test:
  42. import sys
  43. sys.path.insert(0,os.getcwd())
  44. from pwman.util.crypto import CryptoEngine
  45. import getopt
  46. import sys
  47. if 'darwin' in sys.platform:
  48. from pwman.ui.cli import PwmanCliMac as PwmanCli
  49. OSX=True
  50. else:
  51. from pwman.ui.cli import PwmanCli
  52. import pwman.util.config as config
  53. import pwman.data.factory
  54. config_file=args.cfile
  55. def which(cmd):
  56. _, cmdname = os.path.split(cmd)
  57. for path in os.environ["PATH"].split(os.pathsep):
  58. cmd = os.path.join(path, cmdname)
  59. if os.path.isfile(cmd) and os.access(cmd, os.X_OK):
  60. return cmd
  61. return None
  62. try:
  63. config_dir = os.path.expanduser("~/.pwman")
  64. if not os.path.isdir(config_dir):
  65. os.mkdir(config_dir)
  66. config_file = os.path.join(config_dir, "config")
  67. default_config = {'Global': {'umask': '0100', 'colors': 'yes'},
  68. 'Database': {'type': 'SQLite',
  69. 'filename': os.path.join(config_dir,
  70. "pwman.db")},
  71. 'Encryption': {'algorithm': 'Blowfish'},
  72. 'Readline': {'history': os.path.join(config_dir,
  73. "history")}
  74. }
  75. config.set_defaults(default_config)
  76. if os.path.exists(config_file):
  77. config.load(config_file)
  78. xselpath = config.get_value("Global", "xselpath")
  79. elif not OSX :
  80. xselpath = which("xsel")
  81. config.set_value("Global", "xsel", xselpath)
  82. elif OSX:
  83. pbcopypath = which("pbcopy")
  84. config.set_value("Global", "xsel", pbcopypath)
  85. if args.dbase != config.get_value('Database', "filename"):
  86. config.set_value("Database", "filename", args.dbase)
  87. _saveconfig = False
  88. if args.algo != "Blowfish":
  89. config.set_value("Encryption", "algorithm", args.algo)
  90. _saveconfig = False
  91. # set umask before creating/opening any files
  92. umask = int(config.get_value("Global", "umask"))
  93. os.umask(umask)
  94. enc = CryptoEngine.get()
  95. dbtype = config.get_value("Database", "type")
  96. db = pwman.data.factory.create(dbtype)
  97. cli = PwmanCli(db, xselpath)
  98. except SystemExit, e:
  99. sys.exit(e)
  100. except Exception, e:
  101. print "Error: %s" % (e)
  102. sys.exit(-1)
  103. try:
  104. try:
  105. cli.cmdloop()
  106. except KeyboardInterrupt, e:
  107. print e
  108. finally:
  109. try:
  110. if _saveconfig:
  111. config.save(config_file)
  112. except Exception, e:
  113. print "Error: %s" % (e)
  114. sys.exit(-1)