pwman3 4.1 KB

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