pwman3 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/python2
  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) 2006 Ivan Kelly <ivan@ivankelly.net>
  19. #============================================================================
  20. from pwman.util.crypto import CryptoEngine
  21. import getopt
  22. import sys
  23. if 'darwin' in sys.platform:
  24. from pwman.ui.cli import PwmanCliMac
  25. else:
  26. from pwman.ui.cli import PwmanCli
  27. import pwman.util.config as config
  28. import pwman.data.factory
  29. import os
  30. import os.path
  31. import subprocess as sp
  32. _saveconfig = True
  33. def print_help():
  34. print """Syntax: %s [options]
  35. -c, --config FILE Read configuration from FILE
  36. -d, --database FILE Use FILE as database
  37. -e, --encryption ALGO Use ALGO to encrypt data
  38. Possible options are:
  39. AES, ARC2, ARC4, Blowfish(default),
  40. CAST, DES, DES3, IDEA, RC5
  41. -h, --help Display this help and exit
  42. Please report bugs at https://github.com/pwman3/pwman3
  43. """ % (sys.argv[0])
  44. def which(cmd):
  45. _, cmdname = os.path.split(cmd)
  46. for path in os.environ["PATH"].split(os.pathsep):
  47. cmd = os.path.join(path, cmdname)
  48. if os.path.isfile(cmd) and os.access(cmd, os.X_OK):
  49. return cmd
  50. return None
  51. try:
  52. config_dir = os.path.expanduser("~/.pwman")
  53. if not os.path.isdir(config_dir):
  54. os.mkdir(config_dir)
  55. config_file = os.path.join(config_dir, "config")
  56. default_config = {'Global': {'umask': '0100', 'colors': 'yes'},
  57. 'Database': {'type': 'SQLite',
  58. 'filename': os.path.join(config_dir,
  59. "pwman.db")},
  60. 'Encryption': {'algorithm': 'Blowfish'},
  61. 'Readline': {'history': os.path.join(config_dir,
  62. "history")}
  63. }
  64. config.set_defaults(default_config)
  65. opts, args = getopt.getopt(sys.argv[1:], 'c:d:e:h',
  66. ["config=", "database=",
  67. "encryption=", "help"])
  68. for o in opts:
  69. if o[0] == '-c' or o[0] == "--config":
  70. config_file = os.path.expanduser(o[1])
  71. if o[0] == '-h' or o[0] == "--help":
  72. print_help()
  73. sys.exit(0)
  74. # if no config exists yet, look for xsel
  75. if os.path.exists(config_file):
  76. config.load(config_file)
  77. xselpath = config.get_value("Global", "xselpath")
  78. else:
  79. xselpath = which("xsel")
  80. config.set_value("Global", "xsel", xselpath)
  81. for o in opts:
  82. if o[0] == '-d' or o[0] == "--database":
  83. config.set_value("Database", "filename",
  84. os.path.expanduser(o[1]))
  85. _saveconfig = False
  86. if o[0] == '-e' or o[0] == "--encryption":
  87. config.set_value("Encryption", "algorithm", o[1])
  88. _saveconfig = False
  89. # set umask before creating/opening any files
  90. umask = int(config.get_value("Global", "umask"))
  91. os.umask(umask)
  92. enc = CryptoEngine.get()
  93. type = config.get_value("Database", "type")
  94. db = pwman.data.factory.create(type)
  95. cli = PwmanCli(db, xselpath)
  96. except SystemExit, e:
  97. sys.exit(e)
  98. except Exception, e:
  99. print "Error: %s" % (e)
  100. sys.exit(-1)
  101. try:
  102. try:
  103. cli.cmdloop()
  104. except KeyboardInterrupt, e:
  105. print
  106. finally:
  107. try:
  108. if _saveconfig:
  109. config.save(config_file)
  110. except Exception, e:
  111. print "Error: %s" % (e)
  112. sys.exit(-1)